1 // SPDX-License-Identifier: GPL-2.0+
3 * Read-Copy Update module-based scalability-test facility
5 * Copyright (C) IBM Corporation, 2015
10 #define pr_fmt(fmt) fmt
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/kthread.h>
18 #include <linux/err.h>
19 #include <linux/spinlock.h>
20 #include <linux/smp.h>
21 #include <linux/rcupdate.h>
22 #include <linux/interrupt.h>
23 #include <linux/sched.h>
24 #include <uapi/linux/sched/types.h>
25 #include <linux/atomic.h>
26 #include <linux/bitops.h>
27 #include <linux/completion.h>
28 #include <linux/moduleparam.h>
29 #include <linux/percpu.h>
30 #include <linux/notifier.h>
31 #include <linux/reboot.h>
32 #include <linux/freezer.h>
33 #include <linux/cpu.h>
34 #include <linux/delay.h>
35 #include <linux/stat.h>
36 #include <linux/srcu.h>
37 #include <linux/slab.h>
38 #include <asm/byteorder.h>
39 #include <linux/torture.h>
40 #include <linux/vmalloc.h>
44 MODULE_LICENSE("GPL");
47 #define SCALE_FLAG "-scale:"
48 #define SCALEOUT_STRING(s) \
49 pr_alert("%s" SCALE_FLAG " %s\n", scale_type, s)
50 #define VERBOSE_SCALEOUT_STRING(s) \
51 do { if (verbose) pr_alert("%s" SCALE_FLAG " %s\n", scale_type, s); } while (0)
52 #define VERBOSE_SCALEOUT_ERRSTRING(s) \
53 do { if (verbose) pr_alert("%s" SCALE_FLAG "!!! %s\n", scale_type, s); } while (0)
56 * The intended use cases for the nreaders and nwriters module parameters
59 * 1. Specify only the nr_cpus kernel boot parameter. This will
60 * set both nreaders and nwriters to the value specified by
61 * nr_cpus for a mixed reader/writer test.
63 * 2. Specify the nr_cpus kernel boot parameter, but set
64 * rcuscale.nreaders to zero. This will set nwriters to the
65 * value specified by nr_cpus for an update-only test.
67 * 3. Specify the nr_cpus kernel boot parameter, but set
68 * rcuscale.nwriters to zero. This will set nreaders to the
69 * value specified by nr_cpus for a read-only test.
71 * Various other use cases may of course be specified.
73 * Note that this test's readers are intended only as a test load for
74 * the writers. The reader scalability statistics will be overly
75 * pessimistic due to the per-critical-section interrupt disabling,
76 * test-end checks, and the pair of calls through pointers.
80 # define RCUSCALE_SHUTDOWN 0
82 # define RCUSCALE_SHUTDOWN 1
85 torture_param(bool, gp_async, false, "Use asynchronous GP wait primitives");
86 torture_param(int, gp_async_max, 1000, "Max # outstanding waits per reader");
87 torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
88 torture_param(int, holdoff, 10, "Holdoff time before test start (s)");
89 torture_param(int, nreaders, -1, "Number of RCU reader threads");
90 torture_param(int, nwriters, -1, "Number of RCU updater threads");
91 torture_param(bool, shutdown, RCUSCALE_SHUTDOWN,
92 "Shutdown at end of scalability tests.");
93 torture_param(int, verbose, 1, "Enable verbose debugging printk()s");
94 torture_param(int, writer_holdoff, 0, "Holdoff (us) between GPs, zero to disable");
95 torture_param(int, kfree_rcu_test, 0, "Do we run a kfree_rcu() scale test?");
96 torture_param(int, kfree_mult, 1, "Multiple of kfree_obj size to allocate.");
98 static char *scale_type = "rcu";
99 module_param(scale_type, charp, 0444);
100 MODULE_PARM_DESC(scale_type, "Type of RCU to scalability-test (rcu, srcu, ...)");
102 static int nrealreaders;
103 static int nrealwriters;
104 static struct task_struct **writer_tasks;
105 static struct task_struct **reader_tasks;
106 static struct task_struct *shutdown_task;
108 static u64 **writer_durations;
109 static int *writer_n_durations;
110 static atomic_t n_rcu_scale_reader_started;
111 static atomic_t n_rcu_scale_writer_started;
112 static atomic_t n_rcu_scale_writer_finished;
113 static wait_queue_head_t shutdown_wq;
114 static u64 t_rcu_scale_writer_started;
115 static u64 t_rcu_scale_writer_finished;
116 static unsigned long b_rcu_gp_test_started;
117 static unsigned long b_rcu_gp_test_finished;
118 static DEFINE_PER_CPU(atomic_t, n_async_inflight);
120 #define MAX_MEAS 10000
124 * Operations vector for selecting different types of tests.
127 struct rcu_scale_ops {
130 void (*cleanup)(void);
131 int (*readlock)(void);
132 void (*readunlock)(int idx);
133 unsigned long (*get_gp_seq)(void);
134 unsigned long (*gp_diff)(unsigned long new, unsigned long old);
135 unsigned long (*exp_completed)(void);
136 void (*async)(struct rcu_head *head, rcu_callback_t func);
137 void (*gp_barrier)(void);
139 void (*exp_sync)(void);
143 static struct rcu_scale_ops *cur_ops;
146 * Definitions for rcu scalability testing.
149 static int rcu_scale_read_lock(void) __acquires(RCU)
155 static void rcu_scale_read_unlock(int idx) __releases(RCU)
160 static unsigned long __maybe_unused rcu_no_completed(void)
165 static void rcu_sync_scale_init(void)
169 static struct rcu_scale_ops rcu_ops = {
171 .init = rcu_sync_scale_init,
172 .readlock = rcu_scale_read_lock,
173 .readunlock = rcu_scale_read_unlock,
174 .get_gp_seq = rcu_get_gp_seq,
175 .gp_diff = rcu_seq_diff,
176 .exp_completed = rcu_exp_batches_completed,
178 .gp_barrier = rcu_barrier,
179 .sync = synchronize_rcu,
180 .exp_sync = synchronize_rcu_expedited,
185 * Definitions for srcu scalability testing.
188 DEFINE_STATIC_SRCU(srcu_ctl_scale);
189 static struct srcu_struct *srcu_ctlp = &srcu_ctl_scale;
191 static int srcu_scale_read_lock(void) __acquires(srcu_ctlp)
193 return srcu_read_lock(srcu_ctlp);
196 static void srcu_scale_read_unlock(int idx) __releases(srcu_ctlp)
198 srcu_read_unlock(srcu_ctlp, idx);
201 static unsigned long srcu_scale_completed(void)
203 return srcu_batches_completed(srcu_ctlp);
206 static void srcu_call_rcu(struct rcu_head *head, rcu_callback_t func)
208 call_srcu(srcu_ctlp, head, func);
211 static void srcu_rcu_barrier(void)
213 srcu_barrier(srcu_ctlp);
216 static void srcu_scale_synchronize(void)
218 synchronize_srcu(srcu_ctlp);
221 static void srcu_scale_synchronize_expedited(void)
223 synchronize_srcu_expedited(srcu_ctlp);
226 static struct rcu_scale_ops srcu_ops = {
227 .ptype = SRCU_FLAVOR,
228 .init = rcu_sync_scale_init,
229 .readlock = srcu_scale_read_lock,
230 .readunlock = srcu_scale_read_unlock,
231 .get_gp_seq = srcu_scale_completed,
232 .gp_diff = rcu_seq_diff,
233 .exp_completed = srcu_scale_completed,
234 .async = srcu_call_rcu,
235 .gp_barrier = srcu_rcu_barrier,
236 .sync = srcu_scale_synchronize,
237 .exp_sync = srcu_scale_synchronize_expedited,
241 static struct srcu_struct srcud;
243 static void srcu_sync_scale_init(void)
246 init_srcu_struct(srcu_ctlp);
249 static void srcu_sync_scale_cleanup(void)
251 cleanup_srcu_struct(srcu_ctlp);
254 static struct rcu_scale_ops srcud_ops = {
255 .ptype = SRCU_FLAVOR,
256 .init = srcu_sync_scale_init,
257 .cleanup = srcu_sync_scale_cleanup,
258 .readlock = srcu_scale_read_lock,
259 .readunlock = srcu_scale_read_unlock,
260 .get_gp_seq = srcu_scale_completed,
261 .gp_diff = rcu_seq_diff,
262 .exp_completed = srcu_scale_completed,
263 .async = srcu_call_rcu,
264 .gp_barrier = srcu_rcu_barrier,
265 .sync = srcu_scale_synchronize,
266 .exp_sync = srcu_scale_synchronize_expedited,
271 * Definitions for RCU-tasks scalability testing.
274 static int tasks_scale_read_lock(void)
279 static void tasks_scale_read_unlock(int idx)
283 static struct rcu_scale_ops tasks_ops = {
284 .ptype = RCU_TASKS_FLAVOR,
285 .init = rcu_sync_scale_init,
286 .readlock = tasks_scale_read_lock,
287 .readunlock = tasks_scale_read_unlock,
288 .get_gp_seq = rcu_no_completed,
289 .gp_diff = rcu_seq_diff,
290 .async = call_rcu_tasks,
291 .gp_barrier = rcu_barrier_tasks,
292 .sync = synchronize_rcu_tasks,
293 .exp_sync = synchronize_rcu_tasks,
297 static unsigned long rcuscale_seq_diff(unsigned long new, unsigned long old)
299 if (!cur_ops->gp_diff)
301 return cur_ops->gp_diff(new, old);
305 * If scalability tests complete, wait for shutdown to commence.
307 static void rcu_scale_wait_shutdown(void)
309 cond_resched_tasks_rcu_qs();
310 if (atomic_read(&n_rcu_scale_writer_finished) < nrealwriters)
312 while (!torture_must_stop())
313 schedule_timeout_uninterruptible(1);
317 * RCU scalability reader kthread. Repeatedly does empty RCU read-side
318 * critical section, minimizing update-side interference. However, the
319 * point of this test is not to evaluate reader scalability, but instead
320 * to serve as a test load for update-side scalability testing.
323 rcu_scale_reader(void *arg)
329 VERBOSE_SCALEOUT_STRING("rcu_scale_reader task started");
330 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
331 set_user_nice(current, MAX_NICE);
332 atomic_inc(&n_rcu_scale_reader_started);
335 local_irq_save(flags);
336 idx = cur_ops->readlock();
337 cur_ops->readunlock(idx);
338 local_irq_restore(flags);
339 rcu_scale_wait_shutdown();
340 } while (!torture_must_stop());
341 torture_kthread_stopping("rcu_scale_reader");
346 * Callback function for asynchronous grace periods from rcu_scale_writer().
348 static void rcu_scale_async_cb(struct rcu_head *rhp)
350 atomic_dec(this_cpu_ptr(&n_async_inflight));
355 * RCU scale writer kthread. Repeatedly does a grace period.
358 rcu_scale_writer(void *arg)
363 struct rcu_head *rhp = NULL;
364 bool started = false, done = false, alldone = false;
367 u64 *wdpp = writer_durations[me];
369 VERBOSE_SCALEOUT_STRING("rcu_scale_writer task started");
371 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
372 sched_set_fifo_low(current);
375 schedule_timeout_uninterruptible(holdoff * HZ);
378 * Wait until rcu_end_inkernel_boot() is called for normal GP tests
379 * so that RCU is not always expedited for normal GP tests.
380 * The system_state test is approximate, but works well in practice.
382 while (!gp_exp && system_state != SYSTEM_RUNNING)
383 schedule_timeout_uninterruptible(1);
385 t = ktime_get_mono_fast_ns();
386 if (atomic_inc_return(&n_rcu_scale_writer_started) >= nrealwriters) {
387 t_rcu_scale_writer_started = t;
389 b_rcu_gp_test_started =
390 cur_ops->exp_completed() / 2;
392 b_rcu_gp_test_started = cur_ops->get_gp_seq();
398 udelay(writer_holdoff);
400 *wdp = ktime_get_mono_fast_ns();
404 rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
405 if (rhp && atomic_read(this_cpu_ptr(&n_async_inflight)) < gp_async_max) {
406 atomic_inc(this_cpu_ptr(&n_async_inflight));
407 cur_ops->async(rhp, rcu_scale_async_cb);
409 } else if (!kthread_should_stop()) {
410 cur_ops->gp_barrier();
413 kfree(rhp); /* Because we are stopping. */
420 t = ktime_get_mono_fast_ns();
424 atomic_read(&n_rcu_scale_writer_started) >= nrealwriters)
426 if (!done && i >= MIN_MEAS) {
428 sched_set_normal(current, 0);
429 pr_alert("%s%s rcu_scale_writer %ld has %d measurements\n",
430 scale_type, SCALE_FLAG, me, MIN_MEAS);
431 if (atomic_inc_return(&n_rcu_scale_writer_finished) >=
433 schedule_timeout_interruptible(10);
434 rcu_ftrace_dump(DUMP_ALL);
435 SCALEOUT_STRING("Test complete");
436 t_rcu_scale_writer_finished = t;
438 b_rcu_gp_test_finished =
439 cur_ops->exp_completed() / 2;
441 b_rcu_gp_test_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_scale_writer_finished) >= nrealwriters)
453 if (started && !alldone && i < MAX_MEAS - 1)
455 rcu_scale_wait_shutdown();
456 } while (!torture_must_stop());
458 cur_ops->gp_barrier();
460 writer_n_durations[me] = i_max;
461 torture_kthread_stopping("rcu_scale_writer");
466 rcu_scale_print_module_parms(struct rcu_scale_ops *cur_ops, const char *tag)
468 pr_alert("%s" SCALE_FLAG
469 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
470 scale_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
474 rcu_scale_cleanup(void)
483 * Would like warning at start, but everything is expedited
484 * during the mid-boot phase, so have to wait till the end.
486 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp)
487 VERBOSE_SCALEOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
488 if (rcu_gp_is_normal() && gp_exp)
489 VERBOSE_SCALEOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
490 if (gp_exp && gp_async)
491 VERBOSE_SCALEOUT_ERRSTRING("No expedited async GPs, so went with async!");
493 if (torture_cleanup_begin())
496 torture_cleanup_end();
501 for (i = 0; i < nrealreaders; i++)
502 torture_stop_kthread(rcu_scale_reader,
508 for (i = 0; i < nrealwriters; i++) {
509 torture_stop_kthread(rcu_scale_writer,
511 if (!writer_n_durations)
513 j = writer_n_durations[i];
514 pr_alert("%s%s writer %d gps: %d\n",
515 scale_type, SCALE_FLAG, i, j);
518 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
519 scale_type, SCALE_FLAG,
520 t_rcu_scale_writer_started, t_rcu_scale_writer_finished,
521 t_rcu_scale_writer_finished -
522 t_rcu_scale_writer_started,
524 rcuscale_seq_diff(b_rcu_gp_test_finished,
525 b_rcu_gp_test_started));
526 for (i = 0; i < nrealwriters; i++) {
527 if (!writer_durations)
529 if (!writer_n_durations)
531 wdpp = writer_durations[i];
534 for (j = 0; j <= writer_n_durations[i]; j++) {
536 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
537 scale_type, SCALE_FLAG,
540 schedule_timeout_uninterruptible(1);
542 kfree(writer_durations[i]);
545 kfree(writer_durations);
546 kfree(writer_n_durations);
549 /* Do torture-type-specific cleanup operations. */
550 if (cur_ops->cleanup != NULL)
553 torture_cleanup_end();
557 * Return the number if non-negative. If -1, the number of CPUs.
558 * If less than -1, that much less than the number of CPUs, but
561 static int compute_real(int n)
568 nr = num_online_cpus() + 1 + n;
576 * RCU scalability shutdown kthread. Just waits to be awakened, then shuts
580 rcu_scale_shutdown(void *arg)
582 wait_event(shutdown_wq,
583 atomic_read(&n_rcu_scale_writer_finished) >= nrealwriters);
584 smp_mb(); /* Wake before output. */
591 * kfree_rcu() scalability tests: Start a kfree_rcu() loop on all CPUs for number
592 * of iterations and measure total time and number of GP for all iterations to complete.
595 torture_param(int, kfree_nthreads, -1, "Number of threads running loops of kfree_rcu().");
596 torture_param(int, kfree_alloc_num, 8000, "Number of allocations and frees done in an iteration.");
597 torture_param(int, kfree_loops, 10, "Number of loops doing kfree_alloc_num allocations and frees.");
599 static struct task_struct **kfree_reader_tasks;
600 static int kfree_nrealthreads;
601 static atomic_t n_kfree_scale_thread_started;
602 static atomic_t n_kfree_scale_thread_ended;
610 kfree_scale_thread(void *arg)
614 struct kfree_obj *alloc_ptr;
615 u64 start_time, end_time;
616 long long mem_begin, mem_during = 0;
618 VERBOSE_SCALEOUT_STRING("kfree_scale_thread task started");
619 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
620 set_user_nice(current, MAX_NICE);
622 start_time = ktime_get_mono_fast_ns();
624 if (atomic_inc_return(&n_kfree_scale_thread_started) >= kfree_nrealthreads) {
626 b_rcu_gp_test_started = cur_ops->exp_completed() / 2;
628 b_rcu_gp_test_started = cur_ops->get_gp_seq();
633 mem_during = mem_begin = si_mem_available();
634 } else if (loop % (kfree_loops / 4) == 0) {
635 mem_during = (mem_during + si_mem_available()) / 2;
638 for (i = 0; i < kfree_alloc_num; i++) {
639 alloc_ptr = kmalloc(kfree_mult * sizeof(struct kfree_obj), GFP_KERNEL);
643 kfree_rcu(alloc_ptr, rh);
647 } while (!torture_must_stop() && ++loop < kfree_loops);
649 if (atomic_inc_return(&n_kfree_scale_thread_ended) >= kfree_nrealthreads) {
650 end_time = ktime_get_mono_fast_ns();
653 b_rcu_gp_test_finished = cur_ops->exp_completed() / 2;
655 b_rcu_gp_test_finished = cur_ops->get_gp_seq();
657 pr_alert("Total time taken by all kfree'ers: %llu ns, loops: %d, batches: %ld, memory footprint: %lldMB\n",
658 (unsigned long long)(end_time - start_time), kfree_loops,
659 rcuscale_seq_diff(b_rcu_gp_test_finished, b_rcu_gp_test_started),
660 (mem_begin - mem_during) >> (20 - PAGE_SHIFT));
663 smp_mb(); /* Assign before wake. */
664 wake_up(&shutdown_wq);
668 torture_kthread_stopping("kfree_scale_thread");
673 kfree_scale_cleanup(void)
677 if (torture_cleanup_begin())
680 if (kfree_reader_tasks) {
681 for (i = 0; i < kfree_nrealthreads; i++)
682 torture_stop_kthread(kfree_scale_thread,
683 kfree_reader_tasks[i]);
684 kfree(kfree_reader_tasks);
687 torture_cleanup_end();
691 * shutdown kthread. Just waits to be awakened, then shuts down system.
694 kfree_scale_shutdown(void *arg)
696 wait_event(shutdown_wq,
697 atomic_read(&n_kfree_scale_thread_ended) >= kfree_nrealthreads);
699 smp_mb(); /* Wake before output. */
701 kfree_scale_cleanup();
707 kfree_scale_init(void)
712 kfree_nrealthreads = compute_real(kfree_nthreads);
713 /* Start up the kthreads. */
715 init_waitqueue_head(&shutdown_wq);
716 firsterr = torture_create_kthread(kfree_scale_shutdown, NULL,
720 schedule_timeout_uninterruptible(1);
723 pr_alert("kfree object size=%zu\n", kfree_mult * sizeof(struct kfree_obj));
725 kfree_reader_tasks = kcalloc(kfree_nrealthreads, sizeof(kfree_reader_tasks[0]),
727 if (kfree_reader_tasks == NULL) {
732 for (i = 0; i < kfree_nrealthreads; i++) {
733 firsterr = torture_create_kthread(kfree_scale_thread, (void *)i,
734 kfree_reader_tasks[i]);
739 while (atomic_read(&n_kfree_scale_thread_started) < kfree_nrealthreads)
740 schedule_timeout_uninterruptible(1);
747 kfree_scale_cleanup();
756 static struct rcu_scale_ops *scale_ops[] = {
757 &rcu_ops, &srcu_ops, &srcud_ops, &tasks_ops,
760 if (!torture_init_begin(scale_type, verbose))
763 /* Process args and announce that the scalability'er is on the job. */
764 for (i = 0; i < ARRAY_SIZE(scale_ops); i++) {
765 cur_ops = scale_ops[i];
766 if (strcmp(scale_type, cur_ops->name) == 0)
769 if (i == ARRAY_SIZE(scale_ops)) {
770 pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type);
771 pr_alert("rcu-scale types:");
772 for (i = 0; i < ARRAY_SIZE(scale_ops); i++)
773 pr_cont(" %s", scale_ops[i]->name);
775 WARN_ON(!IS_MODULE(CONFIG_RCU_SCALE_TEST));
784 return kfree_scale_init();
786 nrealwriters = compute_real(nwriters);
787 nrealreaders = compute_real(nreaders);
788 atomic_set(&n_rcu_scale_reader_started, 0);
789 atomic_set(&n_rcu_scale_writer_started, 0);
790 atomic_set(&n_rcu_scale_writer_finished, 0);
791 rcu_scale_print_module_parms(cur_ops, "Start of test");
793 /* Start up the kthreads. */
796 init_waitqueue_head(&shutdown_wq);
797 firsterr = torture_create_kthread(rcu_scale_shutdown, NULL,
801 schedule_timeout_uninterruptible(1);
803 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
805 if (reader_tasks == NULL) {
806 VERBOSE_SCALEOUT_ERRSTRING("out of memory");
810 for (i = 0; i < nrealreaders; i++) {
811 firsterr = torture_create_kthread(rcu_scale_reader, (void *)i,
816 while (atomic_read(&n_rcu_scale_reader_started) < nrealreaders)
817 schedule_timeout_uninterruptible(1);
818 writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
820 writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
823 kcalloc(nrealwriters, sizeof(*writer_n_durations),
825 if (!writer_tasks || !writer_durations || !writer_n_durations) {
826 VERBOSE_SCALEOUT_ERRSTRING("out of memory");
830 for (i = 0; i < nrealwriters; i++) {
831 writer_durations[i] =
832 kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
834 if (!writer_durations[i]) {
838 firsterr = torture_create_kthread(rcu_scale_writer, (void *)i,
852 module_init(rcu_scale_init);
853 module_exit(rcu_scale_cleanup);