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, rcu_bh, ...)");
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 static int rcu_perf_writer_state
;
115 #define RTWS_BARRIER 2
116 #define RTWS_EXP_SYNC 3
119 #define RTWS_STOPPING 6
121 #define MAX_MEAS 10000
125 * Operations vector for selecting different types of tests.
128 struct rcu_perf_ops
{
131 void (*cleanup
)(void);
132 int (*readlock
)(void);
133 void (*readunlock
)(int idx
);
134 unsigned long (*get_gp_seq
)(void);
135 unsigned long (*gp_diff
)(unsigned long new, unsigned long old
);
136 unsigned long (*exp_completed
)(void);
137 void (*async
)(struct rcu_head
*head
, rcu_callback_t func
);
138 void (*gp_barrier
)(void);
140 void (*exp_sync
)(void);
144 static struct rcu_perf_ops
*cur_ops
;
147 * Definitions for rcu perf testing.
150 static int rcu_perf_read_lock(void) __acquires(RCU
)
156 static void rcu_perf_read_unlock(int idx
) __releases(RCU
)
161 static unsigned long __maybe_unused
rcu_no_completed(void)
166 static void rcu_sync_perf_init(void)
170 static struct rcu_perf_ops rcu_ops
= {
172 .init
= rcu_sync_perf_init
,
173 .readlock
= rcu_perf_read_lock
,
174 .readunlock
= rcu_perf_read_unlock
,
175 .get_gp_seq
= rcu_get_gp_seq
,
176 .gp_diff
= rcu_seq_diff
,
177 .exp_completed
= rcu_exp_batches_completed
,
179 .gp_barrier
= rcu_barrier
,
180 .sync
= synchronize_rcu
,
181 .exp_sync
= synchronize_rcu_expedited
,
186 * Definitions for srcu perf testing.
189 DEFINE_STATIC_SRCU(srcu_ctl_perf
);
190 static struct srcu_struct
*srcu_ctlp
= &srcu_ctl_perf
;
192 static int srcu_perf_read_lock(void) __acquires(srcu_ctlp
)
194 return srcu_read_lock(srcu_ctlp
);
197 static void srcu_perf_read_unlock(int idx
) __releases(srcu_ctlp
)
199 srcu_read_unlock(srcu_ctlp
, idx
);
202 static unsigned long srcu_perf_completed(void)
204 return srcu_batches_completed(srcu_ctlp
);
207 static void srcu_call_rcu(struct rcu_head
*head
, rcu_callback_t func
)
209 call_srcu(srcu_ctlp
, head
, func
);
212 static void srcu_rcu_barrier(void)
214 srcu_barrier(srcu_ctlp
);
217 static void srcu_perf_synchronize(void)
219 synchronize_srcu(srcu_ctlp
);
222 static void srcu_perf_synchronize_expedited(void)
224 synchronize_srcu_expedited(srcu_ctlp
);
227 static struct rcu_perf_ops srcu_ops
= {
228 .ptype
= SRCU_FLAVOR
,
229 .init
= rcu_sync_perf_init
,
230 .readlock
= srcu_perf_read_lock
,
231 .readunlock
= srcu_perf_read_unlock
,
232 .get_gp_seq
= srcu_perf_completed
,
233 .gp_diff
= rcu_seq_diff
,
234 .exp_completed
= srcu_perf_completed
,
235 .async
= srcu_call_rcu
,
236 .gp_barrier
= srcu_rcu_barrier
,
237 .sync
= srcu_perf_synchronize
,
238 .exp_sync
= srcu_perf_synchronize_expedited
,
242 static struct srcu_struct srcud
;
244 static void srcu_sync_perf_init(void)
247 init_srcu_struct(srcu_ctlp
);
250 static void srcu_sync_perf_cleanup(void)
252 cleanup_srcu_struct(srcu_ctlp
);
255 static struct rcu_perf_ops srcud_ops
= {
256 .ptype
= SRCU_FLAVOR
,
257 .init
= srcu_sync_perf_init
,
258 .cleanup
= srcu_sync_perf_cleanup
,
259 .readlock
= srcu_perf_read_lock
,
260 .readunlock
= srcu_perf_read_unlock
,
261 .get_gp_seq
= srcu_perf_completed
,
262 .gp_diff
= rcu_seq_diff
,
263 .exp_completed
= srcu_perf_completed
,
264 .async
= srcu_call_rcu
,
265 .gp_barrier
= srcu_rcu_barrier
,
266 .sync
= srcu_perf_synchronize
,
267 .exp_sync
= srcu_perf_synchronize_expedited
,
272 * Definitions for RCU-tasks perf testing.
275 static int tasks_perf_read_lock(void)
280 static void tasks_perf_read_unlock(int idx
)
284 static struct rcu_perf_ops tasks_ops
= {
285 .ptype
= RCU_TASKS_FLAVOR
,
286 .init
= rcu_sync_perf_init
,
287 .readlock
= tasks_perf_read_lock
,
288 .readunlock
= tasks_perf_read_unlock
,
289 .get_gp_seq
= rcu_no_completed
,
290 .gp_diff
= rcu_seq_diff
,
291 .async
= call_rcu_tasks
,
292 .gp_barrier
= rcu_barrier_tasks
,
293 .sync
= synchronize_rcu_tasks
,
294 .exp_sync
= synchronize_rcu_tasks
,
298 static unsigned long rcuperf_seq_diff(unsigned long new, unsigned long old
)
300 if (!cur_ops
->gp_diff
)
302 return cur_ops
->gp_diff(new, old
);
306 * If performance tests complete, wait for shutdown to commence.
308 static void rcu_perf_wait_shutdown(void)
310 cond_resched_tasks_rcu_qs();
311 if (atomic_read(&n_rcu_perf_writer_finished
) < nrealwriters
)
313 while (!torture_must_stop())
314 schedule_timeout_uninterruptible(1);
318 * RCU perf reader kthread. Repeatedly does empty RCU read-side
319 * critical section, minimizing update-side interference.
322 rcu_perf_reader(void *arg
)
328 VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
329 set_cpus_allowed_ptr(current
, cpumask_of(me
% nr_cpu_ids
));
330 set_user_nice(current
, MAX_NICE
);
331 atomic_inc(&n_rcu_perf_reader_started
);
334 local_irq_save(flags
);
335 idx
= cur_ops
->readlock();
336 cur_ops
->readunlock(idx
);
337 local_irq_restore(flags
);
338 rcu_perf_wait_shutdown();
339 } while (!torture_must_stop());
340 torture_kthread_stopping("rcu_perf_reader");
345 * Callback function for asynchronous grace periods from rcu_perf_writer().
347 static void rcu_perf_async_cb(struct rcu_head
*rhp
)
349 atomic_dec(this_cpu_ptr(&n_async_inflight
));
354 * RCU perf writer kthread. Repeatedly does a grace period.
357 rcu_perf_writer(void *arg
)
362 struct rcu_head
*rhp
= NULL
;
363 struct sched_param sp
;
364 bool started
= false, done
= false, alldone
= false;
367 u64
*wdpp
= writer_durations
[me
];
369 VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
371 set_cpus_allowed_ptr(current
, cpumask_of(me
% nr_cpu_ids
));
372 sp
.sched_priority
= 1;
373 sched_setscheduler_nocheck(current
, SCHED_FIFO
, &sp
);
376 schedule_timeout_uninterruptible(holdoff
* HZ
);
378 t
= ktime_get_mono_fast_ns();
379 if (atomic_inc_return(&n_rcu_perf_writer_started
) >= nrealwriters
) {
380 t_rcu_perf_writer_started
= t
;
382 b_rcu_perf_writer_started
=
383 cur_ops
->exp_completed() / 2;
385 b_rcu_perf_writer_started
= cur_ops
->get_gp_seq();
391 udelay(writer_holdoff
);
393 *wdp
= ktime_get_mono_fast_ns();
397 rhp
= kmalloc(sizeof(*rhp
), GFP_KERNEL
);
398 if (rhp
&& atomic_read(this_cpu_ptr(&n_async_inflight
)) < gp_async_max
) {
399 rcu_perf_writer_state
= RTWS_ASYNC
;
400 atomic_inc(this_cpu_ptr(&n_async_inflight
));
401 cur_ops
->async(rhp
, rcu_perf_async_cb
);
403 } else if (!kthread_should_stop()) {
404 rcu_perf_writer_state
= RTWS_BARRIER
;
405 cur_ops
->gp_barrier();
408 kfree(rhp
); /* Because we are stopping. */
411 rcu_perf_writer_state
= RTWS_EXP_SYNC
;
414 rcu_perf_writer_state
= RTWS_SYNC
;
417 rcu_perf_writer_state
= RTWS_IDLE
;
418 t
= ktime_get_mono_fast_ns();
422 atomic_read(&n_rcu_perf_writer_started
) >= nrealwriters
)
424 if (!done
&& i
>= MIN_MEAS
) {
426 sp
.sched_priority
= 0;
427 sched_setscheduler_nocheck(current
,
429 pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n",
430 perf_type
, PERF_FLAG
, me
, MIN_MEAS
);
431 if (atomic_inc_return(&n_rcu_perf_writer_finished
) >=
433 schedule_timeout_interruptible(10);
434 rcu_ftrace_dump(DUMP_ALL
);
435 PERFOUT_STRING("Test complete");
436 t_rcu_perf_writer_finished
= t
;
438 b_rcu_perf_writer_finished
=
439 cur_ops
->exp_completed() / 2;
441 b_rcu_perf_writer_finished
=
442 cur_ops
->get_gp_seq();
445 smp_mb(); /* Assign before wake. */
446 wake_up(&shutdown_wq
);
450 if (done
&& !alldone
&&
451 atomic_read(&n_rcu_perf_writer_finished
) >= nrealwriters
)
453 if (started
&& !alldone
&& i
< MAX_MEAS
- 1)
455 rcu_perf_wait_shutdown();
456 } while (!torture_must_stop());
458 rcu_perf_writer_state
= RTWS_BARRIER
;
459 cur_ops
->gp_barrier();
461 rcu_perf_writer_state
= RTWS_STOPPING
;
462 writer_n_durations
[me
] = i_max
;
463 torture_kthread_stopping("rcu_perf_writer");
468 rcu_perf_print_module_parms(struct rcu_perf_ops
*cur_ops
, const char *tag
)
470 pr_alert("%s" PERF_FLAG
471 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
472 perf_type
, tag
, nrealreaders
, nrealwriters
, verbose
, shutdown
);
476 rcu_perf_cleanup(void)
485 * Would like warning at start, but everything is expedited
486 * during the mid-boot phase, so have to wait till the end.
488 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp
)
489 VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
490 if (rcu_gp_is_normal() && gp_exp
)
491 VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
492 if (gp_exp
&& gp_async
)
493 VERBOSE_PERFOUT_ERRSTRING("No expedited async GPs, so went with async!");
495 if (torture_cleanup_begin())
498 torture_cleanup_end();
503 for (i
= 0; i
< nrealreaders
; i
++)
504 torture_stop_kthread(rcu_perf_reader
,
510 for (i
= 0; i
< nrealwriters
; i
++) {
511 torture_stop_kthread(rcu_perf_writer
,
513 if (!writer_n_durations
)
515 j
= writer_n_durations
[i
];
516 pr_alert("%s%s writer %d gps: %d\n",
517 perf_type
, PERF_FLAG
, i
, j
);
520 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
521 perf_type
, PERF_FLAG
,
522 t_rcu_perf_writer_started
, t_rcu_perf_writer_finished
,
523 t_rcu_perf_writer_finished
-
524 t_rcu_perf_writer_started
,
526 rcuperf_seq_diff(b_rcu_perf_writer_finished
,
527 b_rcu_perf_writer_started
));
528 for (i
= 0; i
< nrealwriters
; i
++) {
529 if (!writer_durations
)
531 if (!writer_n_durations
)
533 wdpp
= writer_durations
[i
];
536 for (j
= 0; j
<= writer_n_durations
[i
]; j
++) {
538 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
539 perf_type
, PERF_FLAG
,
542 schedule_timeout_uninterruptible(1);
544 kfree(writer_durations
[i
]);
547 kfree(writer_durations
);
548 kfree(writer_n_durations
);
551 /* Do torture-type-specific cleanup operations. */
552 if (cur_ops
->cleanup
!= NULL
)
555 torture_cleanup_end();
559 * Return the number if non-negative. If -1, the number of CPUs.
560 * If less than -1, that much less than the number of CPUs, but
563 static int compute_real(int n
)
570 nr
= num_online_cpus() + 1 + n
;
578 * RCU perf shutdown kthread. Just waits to be awakened, then shuts
582 rcu_perf_shutdown(void *arg
)
585 wait_event(shutdown_wq
,
586 atomic_read(&n_rcu_perf_writer_finished
) >=
588 } while (atomic_read(&n_rcu_perf_writer_finished
) < nrealwriters
);
589 smp_mb(); /* Wake before output. */
600 static struct rcu_perf_ops
*perf_ops
[] = {
601 &rcu_ops
, &srcu_ops
, &srcud_ops
, &tasks_ops
,
604 if (!torture_init_begin(perf_type
, verbose
))
607 /* Process args and tell the world that the perf'er is on the job. */
608 for (i
= 0; i
< ARRAY_SIZE(perf_ops
); i
++) {
609 cur_ops
= perf_ops
[i
];
610 if (strcmp(perf_type
, cur_ops
->name
) == 0)
613 if (i
== ARRAY_SIZE(perf_ops
)) {
614 pr_alert("rcu-perf: invalid perf type: \"%s\"\n", perf_type
);
615 pr_alert("rcu-perf types:");
616 for (i
= 0; i
< ARRAY_SIZE(perf_ops
); i
++)
617 pr_cont(" %s", perf_ops
[i
]->name
);
619 WARN_ON(!IS_MODULE(CONFIG_RCU_PERF_TEST
));
627 nrealwriters
= compute_real(nwriters
);
628 nrealreaders
= compute_real(nreaders
);
629 atomic_set(&n_rcu_perf_reader_started
, 0);
630 atomic_set(&n_rcu_perf_writer_started
, 0);
631 atomic_set(&n_rcu_perf_writer_finished
, 0);
632 rcu_perf_print_module_parms(cur_ops
, "Start of test");
634 /* Start up the kthreads. */
637 init_waitqueue_head(&shutdown_wq
);
638 firsterr
= torture_create_kthread(rcu_perf_shutdown
, NULL
,
642 schedule_timeout_uninterruptible(1);
644 reader_tasks
= kcalloc(nrealreaders
, sizeof(reader_tasks
[0]),
646 if (reader_tasks
== NULL
) {
647 VERBOSE_PERFOUT_ERRSTRING("out of memory");
651 for (i
= 0; i
< nrealreaders
; i
++) {
652 firsterr
= torture_create_kthread(rcu_perf_reader
, (void *)i
,
657 while (atomic_read(&n_rcu_perf_reader_started
) < nrealreaders
)
658 schedule_timeout_uninterruptible(1);
659 writer_tasks
= kcalloc(nrealwriters
, sizeof(reader_tasks
[0]),
661 writer_durations
= kcalloc(nrealwriters
, sizeof(*writer_durations
),
664 kcalloc(nrealwriters
, sizeof(*writer_n_durations
),
666 if (!writer_tasks
|| !writer_durations
|| !writer_n_durations
) {
667 VERBOSE_PERFOUT_ERRSTRING("out of memory");
671 for (i
= 0; i
< nrealwriters
; i
++) {
672 writer_durations
[i
] =
673 kcalloc(MAX_MEAS
, sizeof(*writer_durations
[i
]),
675 if (!writer_durations
[i
]) {
679 firsterr
= torture_create_kthread(rcu_perf_writer
, (void *)i
,
693 module_init(rcu_perf_init
);
694 module_exit(rcu_perf_cleanup
);