1 // SPDX-License-Identifier: GPL-2.0+
3 // Scalability test comparing RCU vs other mechanisms
4 // for acquiring references on objects.
6 // Copyright (C) Google, 2020.
8 // Author: Joel Fernandes <joel@joelfernandes.org>
10 #define pr_fmt(fmt) fmt
12 #include <linux/atomic.h>
13 #include <linux/bitops.h>
14 #include <linux/completion.h>
15 #include <linux/cpu.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/kthread.h>
21 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/notifier.h>
26 #include <linux/percpu.h>
27 #include <linux/rcupdate.h>
28 #include <linux/rcupdate_trace.h>
29 #include <linux/reboot.h>
30 #include <linux/sched.h>
31 #include <linux/spinlock.h>
32 #include <linux/smp.h>
33 #include <linux/stat.h>
34 #include <linux/srcu.h>
35 #include <linux/slab.h>
36 #include <linux/torture.h>
37 #include <linux/types.h>
41 #define SCALE_FLAG "-ref-scale: "
43 #define SCALEOUT(s, x...) \
44 pr_alert("%s" SCALE_FLAG s, scale_type, ## x)
46 #define VERBOSE_SCALEOUT(s, x...) \
47 do { if (verbose) pr_alert("%s" SCALE_FLAG s, scale_type, ## x); } while (0)
49 static atomic_t verbose_batch_ctr;
51 #define VERBOSE_SCALEOUT_BATCH(s, x...) \
54 (verbose_batched <= 0 || \
55 !(atomic_inc_return(&verbose_batch_ctr) % verbose_batched))) { \
56 schedule_timeout_uninterruptible(1); \
57 pr_alert("%s" SCALE_FLAG s, scale_type, ## x); \
61 #define SCALEOUT_ERRSTRING(s, x...) pr_alert("%s" SCALE_FLAG "!!! " s, scale_type, ## x)
63 MODULE_LICENSE("GPL");
64 MODULE_AUTHOR("Joel Fernandes (Google) <joel@joelfernandes.org>");
66 static char *scale_type = "rcu";
67 module_param(scale_type, charp, 0444);
68 MODULE_PARM_DESC(scale_type, "Type of test (rcu, srcu, refcnt, rwsem, rwlock.");
70 torture_param(int, verbose, 0, "Enable verbose debugging printk()s");
71 torture_param(int, verbose_batched, 0, "Batch verbose debugging printk()s");
73 // Wait until there are multiple CPUs before starting test.
74 torture_param(int, holdoff, IS_BUILTIN(CONFIG_RCU_REF_SCALE_TEST) ? 10 : 0,
75 "Holdoff time before test start (s)");
76 // Number of loops per experiment, all readers execute operations concurrently.
77 torture_param(long, loops, 10000, "Number of loops per experiment.");
78 // Number of readers, with -1 defaulting to about 75% of the CPUs.
79 torture_param(int, nreaders, -1, "Number of readers, -1 for 75% of CPUs.");
81 torture_param(int, nruns, 30, "Number of experiments to run.");
82 // Reader delay in nanoseconds, 0 for no delay.
83 torture_param(int, readdelay, 0, "Read-side delay in nanoseconds.");
86 # define REFSCALE_SHUTDOWN 0
88 # define REFSCALE_SHUTDOWN 1
91 torture_param(bool, shutdown, REFSCALE_SHUTDOWN,
92 "Shutdown at end of scalability tests.");
95 struct task_struct *task;
101 static struct task_struct *shutdown_task;
102 static wait_queue_head_t shutdown_wq;
104 static struct task_struct *main_task;
105 static wait_queue_head_t main_wq;
106 static int shutdown_start;
108 static struct reader_task *reader_tasks;
110 // Number of readers that are part of the current experiment.
111 static atomic_t nreaders_exp;
113 // Use to wait for all threads to start.
114 static atomic_t n_init;
115 static atomic_t n_started;
116 static atomic_t n_warmedup;
117 static atomic_t n_cooleddown;
119 // Track which experiment is currently running.
122 // Operations vector for selecting different types of tests.
123 struct ref_scale_ops {
125 void (*cleanup)(void);
126 void (*readsection)(const int nloops);
127 void (*delaysection)(const int nloops, const int udl, const int ndl);
131 static struct ref_scale_ops *cur_ops;
133 static void un_delay(const int udl, const int ndl)
141 static void ref_rcu_read_section(const int nloops)
145 for (i = nloops; i >= 0; i--) {
151 static void ref_rcu_delay_section(const int nloops, const int udl, const int ndl)
155 for (i = nloops; i >= 0; i--) {
162 static void rcu_sync_scale_init(void)
166 static struct ref_scale_ops rcu_ops = {
167 .init = rcu_sync_scale_init,
168 .readsection = ref_rcu_read_section,
169 .delaysection = ref_rcu_delay_section,
173 // Definitions for SRCU ref scale testing.
174 DEFINE_STATIC_SRCU(srcu_refctl_scale);
175 static struct srcu_struct *srcu_ctlp = &srcu_refctl_scale;
177 static void srcu_ref_scale_read_section(const int nloops)
182 for (i = nloops; i >= 0; i--) {
183 idx = srcu_read_lock(srcu_ctlp);
184 srcu_read_unlock(srcu_ctlp, idx);
188 static void srcu_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
193 for (i = nloops; i >= 0; i--) {
194 idx = srcu_read_lock(srcu_ctlp);
196 srcu_read_unlock(srcu_ctlp, idx);
200 static struct ref_scale_ops srcu_ops = {
201 .init = rcu_sync_scale_init,
202 .readsection = srcu_ref_scale_read_section,
203 .delaysection = srcu_ref_scale_delay_section,
207 // Definitions for RCU Tasks ref scale testing: Empty read markers.
208 // These definitions also work for RCU Rude readers.
209 static void rcu_tasks_ref_scale_read_section(const int nloops)
213 for (i = nloops; i >= 0; i--)
217 static void rcu_tasks_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
221 for (i = nloops; i >= 0; i--)
225 static struct ref_scale_ops rcu_tasks_ops = {
226 .init = rcu_sync_scale_init,
227 .readsection = rcu_tasks_ref_scale_read_section,
228 .delaysection = rcu_tasks_ref_scale_delay_section,
232 // Definitions for RCU Tasks Trace ref scale testing.
233 static void rcu_trace_ref_scale_read_section(const int nloops)
237 for (i = nloops; i >= 0; i--) {
238 rcu_read_lock_trace();
239 rcu_read_unlock_trace();
243 static void rcu_trace_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
247 for (i = nloops; i >= 0; i--) {
248 rcu_read_lock_trace();
250 rcu_read_unlock_trace();
254 static struct ref_scale_ops rcu_trace_ops = {
255 .init = rcu_sync_scale_init,
256 .readsection = rcu_trace_ref_scale_read_section,
257 .delaysection = rcu_trace_ref_scale_delay_section,
261 // Definitions for reference count
262 static atomic_t refcnt;
264 static void ref_refcnt_section(const int nloops)
268 for (i = nloops; i >= 0; i--) {
274 static void ref_refcnt_delay_section(const int nloops, const int udl, const int ndl)
278 for (i = nloops; i >= 0; i--) {
285 static struct ref_scale_ops refcnt_ops = {
286 .init = rcu_sync_scale_init,
287 .readsection = ref_refcnt_section,
288 .delaysection = ref_refcnt_delay_section,
292 // Definitions for rwlock
293 static rwlock_t test_rwlock;
295 static void ref_rwlock_init(void)
297 rwlock_init(&test_rwlock);
300 static void ref_rwlock_section(const int nloops)
304 for (i = nloops; i >= 0; i--) {
305 read_lock(&test_rwlock);
306 read_unlock(&test_rwlock);
310 static void ref_rwlock_delay_section(const int nloops, const int udl, const int ndl)
314 for (i = nloops; i >= 0; i--) {
315 read_lock(&test_rwlock);
317 read_unlock(&test_rwlock);
321 static struct ref_scale_ops rwlock_ops = {
322 .init = ref_rwlock_init,
323 .readsection = ref_rwlock_section,
324 .delaysection = ref_rwlock_delay_section,
328 // Definitions for rwsem
329 static struct rw_semaphore test_rwsem;
331 static void ref_rwsem_init(void)
333 init_rwsem(&test_rwsem);
336 static void ref_rwsem_section(const int nloops)
340 for (i = nloops; i >= 0; i--) {
341 down_read(&test_rwsem);
342 up_read(&test_rwsem);
346 static void ref_rwsem_delay_section(const int nloops, const int udl, const int ndl)
350 for (i = nloops; i >= 0; i--) {
351 down_read(&test_rwsem);
353 up_read(&test_rwsem);
357 static struct ref_scale_ops rwsem_ops = {
358 .init = ref_rwsem_init,
359 .readsection = ref_rwsem_section,
360 .delaysection = ref_rwsem_delay_section,
364 // Definitions for global spinlock
365 static DEFINE_SPINLOCK(test_lock);
367 static void ref_lock_section(const int nloops)
372 for (i = nloops; i >= 0; i--) {
373 spin_lock(&test_lock);
374 spin_unlock(&test_lock);
379 static void ref_lock_delay_section(const int nloops, const int udl, const int ndl)
384 for (i = nloops; i >= 0; i--) {
385 spin_lock(&test_lock);
387 spin_unlock(&test_lock);
392 static struct ref_scale_ops lock_ops = {
393 .readsection = ref_lock_section,
394 .delaysection = ref_lock_delay_section,
398 // Definitions for global irq-save spinlock
400 static void ref_lock_irq_section(const int nloops)
406 for (i = nloops; i >= 0; i--) {
407 spin_lock_irqsave(&test_lock, flags);
408 spin_unlock_irqrestore(&test_lock, flags);
413 static void ref_lock_irq_delay_section(const int nloops, const int udl, const int ndl)
419 for (i = nloops; i >= 0; i--) {
420 spin_lock_irqsave(&test_lock, flags);
422 spin_unlock_irqrestore(&test_lock, flags);
427 static struct ref_scale_ops lock_irq_ops = {
428 .readsection = ref_lock_irq_section,
429 .delaysection = ref_lock_irq_delay_section,
433 // Definitions acquire-release.
434 static DEFINE_PER_CPU(unsigned long, test_acqrel);
436 static void ref_acqrel_section(const int nloops)
442 for (i = nloops; i >= 0; i--) {
443 x = smp_load_acquire(this_cpu_ptr(&test_acqrel));
444 smp_store_release(this_cpu_ptr(&test_acqrel), x + 1);
449 static void ref_acqrel_delay_section(const int nloops, const int udl, const int ndl)
455 for (i = nloops; i >= 0; i--) {
456 x = smp_load_acquire(this_cpu_ptr(&test_acqrel));
458 smp_store_release(this_cpu_ptr(&test_acqrel), x + 1);
463 static struct ref_scale_ops acqrel_ops = {
464 .readsection = ref_acqrel_section,
465 .delaysection = ref_acqrel_delay_section,
469 static volatile u64 stopopts;
471 static void ref_clock_section(const int nloops)
477 for (i = nloops; i >= 0; i--)
478 x += ktime_get_real_fast_ns();
483 static void ref_clock_delay_section(const int nloops, const int udl, const int ndl)
489 for (i = nloops; i >= 0; i--) {
490 x += ktime_get_real_fast_ns();
497 static struct ref_scale_ops clock_ops = {
498 .readsection = ref_clock_section,
499 .delaysection = ref_clock_delay_section,
503 static void rcu_scale_one_reader(void)
506 cur_ops->readsection(loops);
508 cur_ops->delaysection(loops, readdelay / 1000, readdelay % 1000);
511 // Reader kthread. Repeatedly does empty RCU read-side
512 // critical section, minimizing update-side interference.
514 ref_scale_reader(void *arg)
518 struct reader_task *rt = &(reader_tasks[me]);
522 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: task started", me);
523 WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids)));
524 set_user_nice(current, MAX_NICE);
527 schedule_timeout_interruptible(holdoff * HZ);
529 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: waiting to start next experiment on cpu %d", me, raw_smp_processor_id());
531 // Wait for signal that this reader can start.
532 wait_event(rt->wq, (atomic_read(&nreaders_exp) && smp_load_acquire(&rt->start_reader)) ||
533 torture_must_stop());
535 if (torture_must_stop())
538 // Make sure that the CPU is affinitized appropriately during testing.
539 WARN_ON_ONCE(raw_smp_processor_id() != me);
541 WRITE_ONCE(rt->start_reader, 0);
542 if (!atomic_dec_return(&n_started))
543 while (atomic_read_acquire(&n_started))
546 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d started", me, exp_idx);
549 // To reduce noise, do an initial cache-warming invocation, check
550 // in, and then keep warming until everyone has checked in.
551 rcu_scale_one_reader();
552 if (!atomic_dec_return(&n_warmedup))
553 while (atomic_read_acquire(&n_warmedup))
554 rcu_scale_one_reader();
555 // Also keep interrupts disabled. This also has the effect
556 // of preventing entries into slow path for rcu_read_unlock().
557 local_irq_save(flags);
558 start = ktime_get_mono_fast_ns();
560 rcu_scale_one_reader();
562 duration = ktime_get_mono_fast_ns() - start;
563 local_irq_restore(flags);
565 rt->last_duration_ns = WARN_ON_ONCE(duration < 0) ? 0 : duration;
566 // To reduce runtime-skew noise, do maintain-load invocations until
568 if (!atomic_dec_return(&n_cooleddown))
569 while (atomic_read_acquire(&n_cooleddown))
570 rcu_scale_one_reader();
572 if (atomic_dec_and_test(&nreaders_exp))
575 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d ended, (readers remaining=%d)",
576 me, exp_idx, atomic_read(&nreaders_exp));
578 if (!torture_must_stop())
581 torture_kthread_stopping("ref_scale_reader");
585 static void reset_readers(void)
588 struct reader_task *rt;
590 for (i = 0; i < nreaders; i++) {
591 rt = &(reader_tasks[i]);
593 rt->last_duration_ns = 0;
597 // Print the results of each reader and return the sum of all their durations.
598 static u64 process_durations(int n)
601 struct reader_task *rt;
606 buf = kmalloc(800 + 64, GFP_KERNEL);
610 sprintf(buf, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)",
613 for (i = 0; i < n && !torture_must_stop(); i++) {
614 rt = &(reader_tasks[i]);
615 sprintf(buf1, "%d: %llu\t", i, rt->last_duration_ns);
619 if (strlen(buf) >= 800) {
625 sum += rt->last_duration_ns;
627 pr_alert("%s\n", buf);
633 // The main_func is the main orchestrator, it performs a bunch of
634 // experiments. For every experiment, it orders all the readers
635 // involved to start and waits for them to finish the experiment. It
636 // then reads their timestamps and starts the next experiment. Each
637 // experiment progresses from 1 concurrent reader to N of them at which
638 // point all the timestamps are printed.
639 static int main_func(void *arg)
646 set_cpus_allowed_ptr(current, cpumask_of(nreaders % nr_cpu_ids));
647 set_user_nice(current, MAX_NICE);
649 VERBOSE_SCALEOUT("main_func task started");
650 result_avg = kzalloc(nruns * sizeof(*result_avg), GFP_KERNEL);
651 buf = kzalloc(800 + 64, GFP_KERNEL);
652 if (!result_avg || !buf) {
653 SCALEOUT_ERRSTRING("out of memory");
657 schedule_timeout_interruptible(holdoff * HZ);
659 // Wait for all threads to start.
661 while (atomic_read(&n_init) < nreaders + 1)
662 schedule_timeout_uninterruptible(1);
664 // Start exp readers up per experiment
665 for (exp = 0; exp < nruns && !torture_must_stop(); exp++) {
666 if (torture_must_stop())
670 atomic_set(&nreaders_exp, nreaders);
671 atomic_set(&n_started, nreaders);
672 atomic_set(&n_warmedup, nreaders);
673 atomic_set(&n_cooleddown, nreaders);
677 for (r = 0; r < nreaders; r++) {
678 smp_store_release(&reader_tasks[r].start_reader, 1);
679 wake_up(&reader_tasks[r].wq);
682 VERBOSE_SCALEOUT("main_func: experiment started, waiting for %d readers",
686 !atomic_read(&nreaders_exp) || torture_must_stop());
688 VERBOSE_SCALEOUT("main_func: experiment ended");
690 if (torture_must_stop())
693 result_avg[exp] = div_u64(1000 * process_durations(nreaders), nreaders * loops);
696 // Print the average of all experiments
697 SCALEOUT("END OF TEST. Calculating average duration per loop (nanoseconds)...\n");
699 pr_alert("Runs\tTime(ns)\n");
700 for (exp = 0; exp < nruns; exp++) {
704 avg = div_u64_rem(result_avg[exp], 1000, &rem);
705 sprintf(buf1, "%d\t%llu.%03u\n", exp + 1, avg, rem);
707 if (strlen(buf) >= 800) {
716 // This will shutdown everything including us.
719 wake_up(&shutdown_wq);
722 // Wait for torture to stop us
723 while (!torture_must_stop())
724 schedule_timeout_uninterruptible(1);
727 torture_kthread_stopping("main_func");
734 ref_scale_print_module_parms(struct ref_scale_ops *cur_ops, const char *tag)
736 pr_alert("%s" SCALE_FLAG
737 "--- %s: verbose=%d shutdown=%d holdoff=%d loops=%ld nreaders=%d nruns=%d readdelay=%d\n", scale_type, tag,
738 verbose, shutdown, holdoff, loops, nreaders, nruns, readdelay);
742 ref_scale_cleanup(void)
746 if (torture_cleanup_begin())
750 torture_cleanup_end();
755 for (i = 0; i < nreaders; i++)
756 torture_stop_kthread("ref_scale_reader",
757 reader_tasks[i].task);
761 torture_stop_kthread("main_task", main_task);
764 // Do scale-type-specific cleanup operations.
765 if (cur_ops->cleanup != NULL)
768 torture_cleanup_end();
771 // Shutdown kthread. Just waits to be awakened, then shuts down system.
773 ref_scale_shutdown(void *arg)
775 wait_event(shutdown_wq, shutdown_start);
777 smp_mb(); // Wake before output.
789 static struct ref_scale_ops *scale_ops[] = {
790 &rcu_ops, &srcu_ops, &rcu_trace_ops, &rcu_tasks_ops, &refcnt_ops, &rwlock_ops,
791 &rwsem_ops, &lock_ops, &lock_irq_ops, &acqrel_ops, &clock_ops,
794 if (!torture_init_begin(scale_type, verbose))
797 for (i = 0; i < ARRAY_SIZE(scale_ops); i++) {
798 cur_ops = scale_ops[i];
799 if (strcmp(scale_type, cur_ops->name) == 0)
802 if (i == ARRAY_SIZE(scale_ops)) {
803 pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type);
804 pr_alert("rcu-scale types:");
805 for (i = 0; i < ARRAY_SIZE(scale_ops); i++)
806 pr_cont(" %s", scale_ops[i]->name);
815 ref_scale_print_module_parms(cur_ops, "Start of test");
819 init_waitqueue_head(&shutdown_wq);
820 firsterr = torture_create_kthread(ref_scale_shutdown, NULL,
822 if (torture_init_error(firsterr))
824 schedule_timeout_uninterruptible(1);
827 // Reader tasks (default to ~75% of online CPUs).
829 nreaders = (num_online_cpus() >> 1) + (num_online_cpus() >> 2);
830 if (WARN_ONCE(loops <= 0, "%s: loops = %ld, adjusted to 1\n", __func__, loops))
832 if (WARN_ONCE(nreaders <= 0, "%s: nreaders = %d, adjusted to 1\n", __func__, nreaders))
834 if (WARN_ONCE(nruns <= 0, "%s: nruns = %d, adjusted to 1\n", __func__, nruns))
836 reader_tasks = kcalloc(nreaders, sizeof(reader_tasks[0]),
839 SCALEOUT_ERRSTRING("out of memory");
844 VERBOSE_SCALEOUT("Starting %d reader threads\n", nreaders);
846 for (i = 0; i < nreaders; i++) {
847 firsterr = torture_create_kthread(ref_scale_reader, (void *)i,
848 reader_tasks[i].task);
849 if (torture_init_error(firsterr))
852 init_waitqueue_head(&(reader_tasks[i].wq));
856 init_waitqueue_head(&main_wq);
857 firsterr = torture_create_kthread(main_func, NULL, main_task);
858 if (torture_init_error(firsterr))
868 WARN_ON(!IS_MODULE(CONFIG_RCU_REF_SCALE_TEST));
874 module_init(ref_scale_init);
875 module_exit(ref_scale_cleanup);