1 // SPDX-License-Identifier: GPL-2.0+
3 * Read-Copy Update module-based performance-test facility
5 * Copyright (C) IBM Corporation, 2015
7 * Authors: Paul E. McKenney <paulmck@linux.ibm.com>
10 #define pr_fmt(fmt) fmt
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/kthread.h>
17 #include <linux/err.h>
18 #include <linux/spinlock.h>
19 #include <linux/smp.h>
20 #include <linux/rcupdate.h>
21 #include <linux/interrupt.h>
22 #include <linux/sched.h>
23 #include <uapi/linux/sched/types.h>
24 #include <linux/atomic.h>
25 #include <linux/bitops.h>
26 #include <linux/completion.h>
27 #include <linux/moduleparam.h>
28 #include <linux/percpu.h>
29 #include <linux/notifier.h>
30 #include <linux/reboot.h>
31 #include <linux/freezer.h>
32 #include <linux/cpu.h>
33 #include <linux/delay.h>
34 #include <linux/stat.h>
35 #include <linux/srcu.h>
36 #include <linux/slab.h>
37 #include <asm/byteorder.h>
38 #include <linux/torture.h>
39 #include <linux/vmalloc.h>
43 MODULE_LICENSE("GPL");
44 MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>");
46 #define PERF_FLAG "-perf:"
47 #define PERFOUT_STRING(s) \
48 pr_alert("%s" PERF_FLAG " %s\n", perf_type, s)
49 #define VERBOSE_PERFOUT_STRING(s) \
50 do { if (verbose) pr_alert("%s" PERF_FLAG " %s\n", perf_type, s); } while (0)
51 #define VERBOSE_PERFOUT_ERRSTRING(s) \
52 do { if (verbose) pr_alert("%s" PERF_FLAG "!!! %s\n", perf_type, s); } while (0)
55 * The intended use cases for the nreaders and nwriters module parameters
58 * 1. Specify only the nr_cpus kernel boot parameter. This will
59 * set both nreaders and nwriters to the value specified by
60 * nr_cpus for a mixed reader/writer test.
62 * 2. Specify the nr_cpus kernel boot parameter, but set
63 * rcuperf.nreaders to zero. This will set nwriters to the
64 * value specified by nr_cpus for an update-only test.
66 * 3. Specify the nr_cpus kernel boot parameter, but set
67 * rcuperf.nwriters to zero. This will set nreaders to the
68 * value specified by nr_cpus for a read-only test.
70 * Various other use cases may of course be specified.
74 # define RCUPERF_SHUTDOWN 0
76 # define RCUPERF_SHUTDOWN 1
79 torture_param(bool, gp_async
, false, "Use asynchronous GP wait primitives");
80 torture_param(int, gp_async_max
, 1000, "Max # outstanding waits per reader");
81 torture_param(bool, gp_exp
, false, "Use expedited GP wait primitives");
82 torture_param(int, holdoff
, 10, "Holdoff time before test start (s)");
83 torture_param(int, nreaders
, -1, "Number of RCU reader threads");
84 torture_param(int, nwriters
, -1, "Number of RCU updater threads");
85 torture_param(bool, shutdown
, RCUPERF_SHUTDOWN
,
86 "Shutdown at end of performance tests.");
87 torture_param(int, verbose
, 1, "Enable verbose debugging printk()s");
88 torture_param(int, writer_holdoff
, 0, "Holdoff (us) between GPs, zero to disable");
90 static char *perf_type
= "rcu";
91 module_param(perf_type
, charp
, 0444);
92 MODULE_PARM_DESC(perf_type
, "Type of RCU to performance-test (rcu, srcu, ...)");
94 static int nrealreaders
;
95 static int nrealwriters
;
96 static struct task_struct
**writer_tasks
;
97 static struct task_struct
**reader_tasks
;
98 static struct task_struct
*shutdown_task
;
100 static u64
**writer_durations
;
101 static int *writer_n_durations
;
102 static atomic_t n_rcu_perf_reader_started
;
103 static atomic_t n_rcu_perf_writer_started
;
104 static atomic_t n_rcu_perf_writer_finished
;
105 static wait_queue_head_t shutdown_wq
;
106 static u64 t_rcu_perf_writer_started
;
107 static u64 t_rcu_perf_writer_finished
;
108 static unsigned long b_rcu_perf_writer_started
;
109 static unsigned long b_rcu_perf_writer_finished
;
110 static DEFINE_PER_CPU(atomic_t
, n_async_inflight
);
112 #define MAX_MEAS 10000
116 * Operations vector for selecting different types of tests.
119 struct rcu_perf_ops
{
122 void (*cleanup
)(void);
123 int (*readlock
)(void);
124 void (*readunlock
)(int idx
);
125 unsigned long (*get_gp_seq
)(void);
126 unsigned long (*gp_diff
)(unsigned long new, unsigned long old
);
127 unsigned long (*exp_completed
)(void);
128 void (*async
)(struct rcu_head
*head
, rcu_callback_t func
);
129 void (*gp_barrier
)(void);
131 void (*exp_sync
)(void);
135 static struct rcu_perf_ops
*cur_ops
;
138 * Definitions for rcu perf testing.
141 static int rcu_perf_read_lock(void) __acquires(RCU
)
147 static void rcu_perf_read_unlock(int idx
) __releases(RCU
)
152 static unsigned long __maybe_unused
rcu_no_completed(void)
157 static void rcu_sync_perf_init(void)
161 static struct rcu_perf_ops rcu_ops
= {
163 .init
= rcu_sync_perf_init
,
164 .readlock
= rcu_perf_read_lock
,
165 .readunlock
= rcu_perf_read_unlock
,
166 .get_gp_seq
= rcu_get_gp_seq
,
167 .gp_diff
= rcu_seq_diff
,
168 .exp_completed
= rcu_exp_batches_completed
,
170 .gp_barrier
= rcu_barrier
,
171 .sync
= synchronize_rcu
,
172 .exp_sync
= synchronize_rcu_expedited
,
177 * Definitions for srcu perf testing.
180 DEFINE_STATIC_SRCU(srcu_ctl_perf
);
181 static struct srcu_struct
*srcu_ctlp
= &srcu_ctl_perf
;
183 static int srcu_perf_read_lock(void) __acquires(srcu_ctlp
)
185 return srcu_read_lock(srcu_ctlp
);
188 static void srcu_perf_read_unlock(int idx
) __releases(srcu_ctlp
)
190 srcu_read_unlock(srcu_ctlp
, idx
);
193 static unsigned long srcu_perf_completed(void)
195 return srcu_batches_completed(srcu_ctlp
);
198 static void srcu_call_rcu(struct rcu_head
*head
, rcu_callback_t func
)
200 call_srcu(srcu_ctlp
, head
, func
);
203 static void srcu_rcu_barrier(void)
205 srcu_barrier(srcu_ctlp
);
208 static void srcu_perf_synchronize(void)
210 synchronize_srcu(srcu_ctlp
);
213 static void srcu_perf_synchronize_expedited(void)
215 synchronize_srcu_expedited(srcu_ctlp
);
218 static struct rcu_perf_ops srcu_ops
= {
219 .ptype
= SRCU_FLAVOR
,
220 .init
= rcu_sync_perf_init
,
221 .readlock
= srcu_perf_read_lock
,
222 .readunlock
= srcu_perf_read_unlock
,
223 .get_gp_seq
= srcu_perf_completed
,
224 .gp_diff
= rcu_seq_diff
,
225 .exp_completed
= srcu_perf_completed
,
226 .async
= srcu_call_rcu
,
227 .gp_barrier
= srcu_rcu_barrier
,
228 .sync
= srcu_perf_synchronize
,
229 .exp_sync
= srcu_perf_synchronize_expedited
,
233 static struct srcu_struct srcud
;
235 static void srcu_sync_perf_init(void)
238 init_srcu_struct(srcu_ctlp
);
241 static void srcu_sync_perf_cleanup(void)
243 cleanup_srcu_struct(srcu_ctlp
);
246 static struct rcu_perf_ops srcud_ops
= {
247 .ptype
= SRCU_FLAVOR
,
248 .init
= srcu_sync_perf_init
,
249 .cleanup
= srcu_sync_perf_cleanup
,
250 .readlock
= srcu_perf_read_lock
,
251 .readunlock
= srcu_perf_read_unlock
,
252 .get_gp_seq
= srcu_perf_completed
,
253 .gp_diff
= rcu_seq_diff
,
254 .exp_completed
= srcu_perf_completed
,
255 .async
= srcu_call_rcu
,
256 .gp_barrier
= srcu_rcu_barrier
,
257 .sync
= srcu_perf_synchronize
,
258 .exp_sync
= srcu_perf_synchronize_expedited
,
263 * Definitions for RCU-tasks perf testing.
266 static int tasks_perf_read_lock(void)
271 static void tasks_perf_read_unlock(int idx
)
275 static struct rcu_perf_ops tasks_ops
= {
276 .ptype
= RCU_TASKS_FLAVOR
,
277 .init
= rcu_sync_perf_init
,
278 .readlock
= tasks_perf_read_lock
,
279 .readunlock
= tasks_perf_read_unlock
,
280 .get_gp_seq
= rcu_no_completed
,
281 .gp_diff
= rcu_seq_diff
,
282 .async
= call_rcu_tasks
,
283 .gp_barrier
= rcu_barrier_tasks
,
284 .sync
= synchronize_rcu_tasks
,
285 .exp_sync
= synchronize_rcu_tasks
,
289 static unsigned long rcuperf_seq_diff(unsigned long new, unsigned long old
)
291 if (!cur_ops
->gp_diff
)
293 return cur_ops
->gp_diff(new, old
);
297 * If performance tests complete, wait for shutdown to commence.
299 static void rcu_perf_wait_shutdown(void)
301 cond_resched_tasks_rcu_qs();
302 if (atomic_read(&n_rcu_perf_writer_finished
) < nrealwriters
)
304 while (!torture_must_stop())
305 schedule_timeout_uninterruptible(1);
309 * RCU perf reader kthread. Repeatedly does empty RCU read-side
310 * critical section, minimizing update-side interference.
313 rcu_perf_reader(void *arg
)
319 VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
320 set_cpus_allowed_ptr(current
, cpumask_of(me
% nr_cpu_ids
));
321 set_user_nice(current
, MAX_NICE
);
322 atomic_inc(&n_rcu_perf_reader_started
);
325 local_irq_save(flags
);
326 idx
= cur_ops
->readlock();
327 cur_ops
->readunlock(idx
);
328 local_irq_restore(flags
);
329 rcu_perf_wait_shutdown();
330 } while (!torture_must_stop());
331 torture_kthread_stopping("rcu_perf_reader");
336 * Callback function for asynchronous grace periods from rcu_perf_writer().
338 static void rcu_perf_async_cb(struct rcu_head
*rhp
)
340 atomic_dec(this_cpu_ptr(&n_async_inflight
));
345 * RCU perf writer kthread. Repeatedly does a grace period.
348 rcu_perf_writer(void *arg
)
353 struct rcu_head
*rhp
= NULL
;
354 struct sched_param sp
;
355 bool started
= false, done
= false, alldone
= false;
358 u64
*wdpp
= writer_durations
[me
];
360 VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
362 set_cpus_allowed_ptr(current
, cpumask_of(me
% nr_cpu_ids
));
363 sp
.sched_priority
= 1;
364 sched_setscheduler_nocheck(current
, SCHED_FIFO
, &sp
);
367 schedule_timeout_uninterruptible(holdoff
* HZ
);
370 * Wait until rcu_end_inkernel_boot() is called for normal GP tests
371 * so that RCU is not always expedited for normal GP tests.
372 * The system_state test is approximate, but works well in practice.
374 while (!gp_exp
&& system_state
!= SYSTEM_RUNNING
)
375 schedule_timeout_uninterruptible(1);
377 t
= ktime_get_mono_fast_ns();
378 if (atomic_inc_return(&n_rcu_perf_writer_started
) >= nrealwriters
) {
379 t_rcu_perf_writer_started
= t
;
381 b_rcu_perf_writer_started
=
382 cur_ops
->exp_completed() / 2;
384 b_rcu_perf_writer_started
= cur_ops
->get_gp_seq();
390 udelay(writer_holdoff
);
392 *wdp
= ktime_get_mono_fast_ns();
396 rhp
= kmalloc(sizeof(*rhp
), GFP_KERNEL
);
397 if (rhp
&& atomic_read(this_cpu_ptr(&n_async_inflight
)) < gp_async_max
) {
398 atomic_inc(this_cpu_ptr(&n_async_inflight
));
399 cur_ops
->async(rhp
, rcu_perf_async_cb
);
401 } else if (!kthread_should_stop()) {
402 cur_ops
->gp_barrier();
405 kfree(rhp
); /* Because we are stopping. */
412 t
= ktime_get_mono_fast_ns();
416 atomic_read(&n_rcu_perf_writer_started
) >= nrealwriters
)
418 if (!done
&& i
>= MIN_MEAS
) {
420 sp
.sched_priority
= 0;
421 sched_setscheduler_nocheck(current
,
423 pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n",
424 perf_type
, PERF_FLAG
, me
, MIN_MEAS
);
425 if (atomic_inc_return(&n_rcu_perf_writer_finished
) >=
427 schedule_timeout_interruptible(10);
428 rcu_ftrace_dump(DUMP_ALL
);
429 PERFOUT_STRING("Test complete");
430 t_rcu_perf_writer_finished
= t
;
432 b_rcu_perf_writer_finished
=
433 cur_ops
->exp_completed() / 2;
435 b_rcu_perf_writer_finished
=
436 cur_ops
->get_gp_seq();
439 smp_mb(); /* Assign before wake. */
440 wake_up(&shutdown_wq
);
444 if (done
&& !alldone
&&
445 atomic_read(&n_rcu_perf_writer_finished
) >= nrealwriters
)
447 if (started
&& !alldone
&& i
< MAX_MEAS
- 1)
449 rcu_perf_wait_shutdown();
450 } while (!torture_must_stop());
452 cur_ops
->gp_barrier();
454 writer_n_durations
[me
] = i_max
;
455 torture_kthread_stopping("rcu_perf_writer");
460 rcu_perf_print_module_parms(struct rcu_perf_ops
*cur_ops
, const char *tag
)
462 pr_alert("%s" PERF_FLAG
463 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
464 perf_type
, tag
, nrealreaders
, nrealwriters
, verbose
, shutdown
);
468 rcu_perf_cleanup(void)
477 * Would like warning at start, but everything is expedited
478 * during the mid-boot phase, so have to wait till the end.
480 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp
)
481 VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
482 if (rcu_gp_is_normal() && gp_exp
)
483 VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
484 if (gp_exp
&& gp_async
)
485 VERBOSE_PERFOUT_ERRSTRING("No expedited async GPs, so went with async!");
487 if (torture_cleanup_begin())
490 torture_cleanup_end();
495 for (i
= 0; i
< nrealreaders
; i
++)
496 torture_stop_kthread(rcu_perf_reader
,
502 for (i
= 0; i
< nrealwriters
; i
++) {
503 torture_stop_kthread(rcu_perf_writer
,
505 if (!writer_n_durations
)
507 j
= writer_n_durations
[i
];
508 pr_alert("%s%s writer %d gps: %d\n",
509 perf_type
, PERF_FLAG
, i
, j
);
512 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
513 perf_type
, PERF_FLAG
,
514 t_rcu_perf_writer_started
, t_rcu_perf_writer_finished
,
515 t_rcu_perf_writer_finished
-
516 t_rcu_perf_writer_started
,
518 rcuperf_seq_diff(b_rcu_perf_writer_finished
,
519 b_rcu_perf_writer_started
));
520 for (i
= 0; i
< nrealwriters
; i
++) {
521 if (!writer_durations
)
523 if (!writer_n_durations
)
525 wdpp
= writer_durations
[i
];
528 for (j
= 0; j
<= writer_n_durations
[i
]; j
++) {
530 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
531 perf_type
, PERF_FLAG
,
534 schedule_timeout_uninterruptible(1);
536 kfree(writer_durations
[i
]);
539 kfree(writer_durations
);
540 kfree(writer_n_durations
);
543 /* Do torture-type-specific cleanup operations. */
544 if (cur_ops
->cleanup
!= NULL
)
547 torture_cleanup_end();
551 * Return the number if non-negative. If -1, the number of CPUs.
552 * If less than -1, that much less than the number of CPUs, but
555 static int compute_real(int n
)
562 nr
= num_online_cpus() + 1 + n
;
570 * RCU perf shutdown kthread. Just waits to be awakened, then shuts
574 rcu_perf_shutdown(void *arg
)
577 wait_event(shutdown_wq
,
578 atomic_read(&n_rcu_perf_writer_finished
) >=
580 } while (atomic_read(&n_rcu_perf_writer_finished
) < nrealwriters
);
581 smp_mb(); /* Wake before output. */
592 static struct rcu_perf_ops
*perf_ops
[] = {
593 &rcu_ops
, &srcu_ops
, &srcud_ops
, &tasks_ops
,
596 if (!torture_init_begin(perf_type
, verbose
))
599 /* Process args and tell the world that the perf'er is on the job. */
600 for (i
= 0; i
< ARRAY_SIZE(perf_ops
); i
++) {
601 cur_ops
= perf_ops
[i
];
602 if (strcmp(perf_type
, cur_ops
->name
) == 0)
605 if (i
== ARRAY_SIZE(perf_ops
)) {
606 pr_alert("rcu-perf: invalid perf type: \"%s\"\n", perf_type
);
607 pr_alert("rcu-perf types:");
608 for (i
= 0; i
< ARRAY_SIZE(perf_ops
); i
++)
609 pr_cont(" %s", perf_ops
[i
]->name
);
611 WARN_ON(!IS_MODULE(CONFIG_RCU_PERF_TEST
));
619 nrealwriters
= compute_real(nwriters
);
620 nrealreaders
= compute_real(nreaders
);
621 atomic_set(&n_rcu_perf_reader_started
, 0);
622 atomic_set(&n_rcu_perf_writer_started
, 0);
623 atomic_set(&n_rcu_perf_writer_finished
, 0);
624 rcu_perf_print_module_parms(cur_ops
, "Start of test");
626 /* Start up the kthreads. */
629 init_waitqueue_head(&shutdown_wq
);
630 firsterr
= torture_create_kthread(rcu_perf_shutdown
, NULL
,
634 schedule_timeout_uninterruptible(1);
636 reader_tasks
= kcalloc(nrealreaders
, sizeof(reader_tasks
[0]),
638 if (reader_tasks
== NULL
) {
639 VERBOSE_PERFOUT_ERRSTRING("out of memory");
643 for (i
= 0; i
< nrealreaders
; i
++) {
644 firsterr
= torture_create_kthread(rcu_perf_reader
, (void *)i
,
649 while (atomic_read(&n_rcu_perf_reader_started
) < nrealreaders
)
650 schedule_timeout_uninterruptible(1);
651 writer_tasks
= kcalloc(nrealwriters
, sizeof(reader_tasks
[0]),
653 writer_durations
= kcalloc(nrealwriters
, sizeof(*writer_durations
),
656 kcalloc(nrealwriters
, sizeof(*writer_n_durations
),
658 if (!writer_tasks
|| !writer_durations
|| !writer_n_durations
) {
659 VERBOSE_PERFOUT_ERRSTRING("out of memory");
663 for (i
= 0; i
< nrealwriters
; i
++) {
664 writer_durations
[i
] =
665 kcalloc(MAX_MEAS
, sizeof(*writer_durations
[i
]),
667 if (!writer_durations
[i
]) {
671 firsterr
= torture_create_kthread(rcu_perf_writer
, (void *)i
,
685 module_init(rcu_perf_init
);
686 module_exit(rcu_perf_cleanup
);