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/seq_buf.h>
32 #include <linux/spinlock.h>
33 #include <linux/smp.h>
34 #include <linux/stat.h>
35 #include <linux/srcu.h>
36 #include <linux/slab.h>
37 #include <linux/torture.h>
38 #include <linux/types.h>
39 #include <linux/sched/clock.h>
43 #define SCALE_FLAG "-ref-scale: "
45 #define SCALEOUT(s, x...) \
46 pr_alert("%s" SCALE_FLAG s, scale_type, ## x)
48 #define VERBOSE_SCALEOUT(s, x...) \
51 pr_alert("%s" SCALE_FLAG s "\n", scale_type, ## x); \
54 static atomic_t verbose_batch_ctr;
56 #define VERBOSE_SCALEOUT_BATCH(s, x...) \
59 (verbose_batched <= 0 || \
60 !(atomic_inc_return(&verbose_batch_ctr) % verbose_batched))) { \
61 schedule_timeout_uninterruptible(1); \
62 pr_alert("%s" SCALE_FLAG s "\n", scale_type, ## x); \
66 #define SCALEOUT_ERRSTRING(s, x...) pr_alert("%s" SCALE_FLAG "!!! " s "\n", scale_type, ## x)
68 MODULE_DESCRIPTION("Scalability test for object reference mechanisms");
69 MODULE_LICENSE("GPL");
70 MODULE_AUTHOR("Joel Fernandes (Google) <joel@joelfernandes.org>");
72 static char *scale_type = "rcu";
73 module_param(scale_type, charp, 0444);
74 MODULE_PARM_DESC(scale_type, "Type of test (rcu, srcu, refcnt, rwsem, rwlock.");
76 torture_param(int, verbose, 0, "Enable verbose debugging printk()s");
77 torture_param(int, verbose_batched, 0, "Batch verbose debugging printk()s");
79 // Number of seconds to extend warm-up and cool-down for multiple guest OSes
80 torture_param(long, guest_os_delay, 0,
81 "Number of seconds to extend warm-up/cool-down for multiple guest OSes.");
82 // Wait until there are multiple CPUs before starting test.
83 torture_param(int, holdoff, IS_BUILTIN(CONFIG_RCU_REF_SCALE_TEST) ? 10 : 0,
84 "Holdoff time before test start (s)");
85 // Number of typesafe_lookup structures, that is, the degree of concurrency.
86 torture_param(long, lookup_instances, 0, "Number of typesafe_lookup structures.");
87 // Number of loops per experiment, all readers execute operations concurrently.
88 torture_param(long, loops, 10000, "Number of loops per experiment.");
89 // Number of readers, with -1 defaulting to about 75% of the CPUs.
90 torture_param(int, nreaders, -1, "Number of readers, -1 for 75% of CPUs.");
92 torture_param(int, nruns, 30, "Number of experiments to run.");
93 // Reader delay in nanoseconds, 0 for no delay.
94 torture_param(int, readdelay, 0, "Read-side delay in nanoseconds.");
97 # define REFSCALE_SHUTDOWN 0
99 # define REFSCALE_SHUTDOWN 1
102 torture_param(bool, shutdown, REFSCALE_SHUTDOWN,
103 "Shutdown at end of scalability tests.");
106 struct task_struct *task;
108 wait_queue_head_t wq;
109 u64 last_duration_ns;
112 static struct task_struct *shutdown_task;
113 static wait_queue_head_t shutdown_wq;
115 static struct task_struct *main_task;
116 static wait_queue_head_t main_wq;
117 static int shutdown_start;
119 static struct reader_task *reader_tasks;
121 // Number of readers that are part of the current experiment.
122 static atomic_t nreaders_exp;
124 // Use to wait for all threads to start.
125 static atomic_t n_init;
126 static atomic_t n_started;
127 static atomic_t n_warmedup;
128 static atomic_t n_cooleddown;
130 // Track which experiment is currently running.
133 // Operations vector for selecting different types of tests.
134 struct ref_scale_ops {
136 void (*cleanup)(void);
137 void (*readsection)(const int nloops);
138 void (*delaysection)(const int nloops, const int udl, const int ndl);
142 static const struct ref_scale_ops *cur_ops;
144 static void un_delay(const int udl, const int ndl)
152 static void ref_rcu_read_section(const int nloops)
156 for (i = nloops; i >= 0; i--) {
162 static void ref_rcu_delay_section(const int nloops, const int udl, const int ndl)
166 for (i = nloops; i >= 0; i--) {
173 static bool rcu_sync_scale_init(void)
178 static const struct ref_scale_ops rcu_ops = {
179 .init = rcu_sync_scale_init,
180 .readsection = ref_rcu_read_section,
181 .delaysection = ref_rcu_delay_section,
185 // Definitions for SRCU ref scale testing.
186 DEFINE_STATIC_SRCU(srcu_refctl_scale);
187 static struct srcu_struct *srcu_ctlp = &srcu_refctl_scale;
189 static void srcu_ref_scale_read_section(const int nloops)
194 for (i = nloops; i >= 0; i--) {
195 idx = srcu_read_lock(srcu_ctlp);
196 srcu_read_unlock(srcu_ctlp, idx);
200 static void srcu_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
205 for (i = nloops; i >= 0; i--) {
206 idx = srcu_read_lock(srcu_ctlp);
208 srcu_read_unlock(srcu_ctlp, idx);
212 static const struct ref_scale_ops srcu_ops = {
213 .init = rcu_sync_scale_init,
214 .readsection = srcu_ref_scale_read_section,
215 .delaysection = srcu_ref_scale_delay_section,
219 static void srcu_fast_ref_scale_read_section(const int nloops)
222 struct srcu_ctr __percpu *scp;
224 for (i = nloops; i >= 0; i--) {
225 scp = srcu_read_lock_fast(srcu_ctlp);
226 srcu_read_unlock_fast(srcu_ctlp, scp);
230 static void srcu_fast_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
233 struct srcu_ctr __percpu *scp;
235 for (i = nloops; i >= 0; i--) {
236 scp = srcu_read_lock_fast(srcu_ctlp);
238 srcu_read_unlock_fast(srcu_ctlp, scp);
242 static const struct ref_scale_ops srcu_fast_ops = {
243 .init = rcu_sync_scale_init,
244 .readsection = srcu_fast_ref_scale_read_section,
245 .delaysection = srcu_fast_ref_scale_delay_section,
249 static void srcu_lite_ref_scale_read_section(const int nloops)
254 for (i = nloops; i >= 0; i--) {
255 idx = srcu_read_lock_lite(srcu_ctlp);
256 srcu_read_unlock_lite(srcu_ctlp, idx);
260 static void srcu_lite_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
265 for (i = nloops; i >= 0; i--) {
266 idx = srcu_read_lock_lite(srcu_ctlp);
268 srcu_read_unlock_lite(srcu_ctlp, idx);
272 static const struct ref_scale_ops srcu_lite_ops = {
273 .init = rcu_sync_scale_init,
274 .readsection = srcu_lite_ref_scale_read_section,
275 .delaysection = srcu_lite_ref_scale_delay_section,
279 #ifdef CONFIG_TASKS_RCU
281 // Definitions for RCU Tasks ref scale testing: Empty read markers.
282 // These definitions also work for RCU Rude readers.
283 static void rcu_tasks_ref_scale_read_section(const int nloops)
287 for (i = nloops; i >= 0; i--)
291 static void rcu_tasks_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
295 for (i = nloops; i >= 0; i--)
299 static const struct ref_scale_ops rcu_tasks_ops = {
300 .init = rcu_sync_scale_init,
301 .readsection = rcu_tasks_ref_scale_read_section,
302 .delaysection = rcu_tasks_ref_scale_delay_section,
306 #define RCU_TASKS_OPS &rcu_tasks_ops,
308 #else // #ifdef CONFIG_TASKS_RCU
310 #define RCU_TASKS_OPS
312 #endif // #else // #ifdef CONFIG_TASKS_RCU
314 #ifdef CONFIG_TASKS_TRACE_RCU
316 // Definitions for RCU Tasks Trace ref scale testing.
317 static void rcu_trace_ref_scale_read_section(const int nloops)
321 for (i = nloops; i >= 0; i--) {
322 rcu_read_lock_trace();
323 rcu_read_unlock_trace();
327 static void rcu_trace_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
331 for (i = nloops; i >= 0; i--) {
332 rcu_read_lock_trace();
334 rcu_read_unlock_trace();
338 static const struct ref_scale_ops rcu_trace_ops = {
339 .init = rcu_sync_scale_init,
340 .readsection = rcu_trace_ref_scale_read_section,
341 .delaysection = rcu_trace_ref_scale_delay_section,
345 #define RCU_TRACE_OPS &rcu_trace_ops,
347 #else // #ifdef CONFIG_TASKS_TRACE_RCU
349 #define RCU_TRACE_OPS
351 #endif // #else // #ifdef CONFIG_TASKS_TRACE_RCU
353 // Definitions for reference count
354 static atomic_t refcnt;
356 static void ref_refcnt_section(const int nloops)
360 for (i = nloops; i >= 0; i--) {
366 static void ref_refcnt_delay_section(const int nloops, const int udl, const int ndl)
370 for (i = nloops; i >= 0; i--) {
377 static const struct ref_scale_ops refcnt_ops = {
378 .init = rcu_sync_scale_init,
379 .readsection = ref_refcnt_section,
380 .delaysection = ref_refcnt_delay_section,
384 // Definitions for rwlock
385 static rwlock_t test_rwlock;
387 static bool ref_rwlock_init(void)
389 rwlock_init(&test_rwlock);
393 static void ref_rwlock_section(const int nloops)
397 for (i = nloops; i >= 0; i--) {
398 read_lock(&test_rwlock);
399 read_unlock(&test_rwlock);
403 static void ref_rwlock_delay_section(const int nloops, const int udl, const int ndl)
407 for (i = nloops; i >= 0; i--) {
408 read_lock(&test_rwlock);
410 read_unlock(&test_rwlock);
414 static const struct ref_scale_ops rwlock_ops = {
415 .init = ref_rwlock_init,
416 .readsection = ref_rwlock_section,
417 .delaysection = ref_rwlock_delay_section,
421 // Definitions for rwsem
422 static struct rw_semaphore test_rwsem;
424 static bool ref_rwsem_init(void)
426 init_rwsem(&test_rwsem);
430 static void ref_rwsem_section(const int nloops)
434 for (i = nloops; i >= 0; i--) {
435 down_read(&test_rwsem);
436 up_read(&test_rwsem);
440 static void ref_rwsem_delay_section(const int nloops, const int udl, const int ndl)
444 for (i = nloops; i >= 0; i--) {
445 down_read(&test_rwsem);
447 up_read(&test_rwsem);
451 static const struct ref_scale_ops rwsem_ops = {
452 .init = ref_rwsem_init,
453 .readsection = ref_rwsem_section,
454 .delaysection = ref_rwsem_delay_section,
458 // Definitions for global spinlock
459 static DEFINE_RAW_SPINLOCK(test_lock);
461 static void ref_lock_section(const int nloops)
466 for (i = nloops; i >= 0; i--) {
467 raw_spin_lock(&test_lock);
468 raw_spin_unlock(&test_lock);
473 static void ref_lock_delay_section(const int nloops, const int udl, const int ndl)
478 for (i = nloops; i >= 0; i--) {
479 raw_spin_lock(&test_lock);
481 raw_spin_unlock(&test_lock);
486 static const struct ref_scale_ops lock_ops = {
487 .readsection = ref_lock_section,
488 .delaysection = ref_lock_delay_section,
492 // Definitions for global irq-save spinlock
494 static void ref_lock_irq_section(const int nloops)
500 for (i = nloops; i >= 0; i--) {
501 raw_spin_lock_irqsave(&test_lock, flags);
502 raw_spin_unlock_irqrestore(&test_lock, flags);
507 static void ref_lock_irq_delay_section(const int nloops, const int udl, const int ndl)
513 for (i = nloops; i >= 0; i--) {
514 raw_spin_lock_irqsave(&test_lock, flags);
516 raw_spin_unlock_irqrestore(&test_lock, flags);
521 static const struct ref_scale_ops lock_irq_ops = {
522 .readsection = ref_lock_irq_section,
523 .delaysection = ref_lock_irq_delay_section,
527 // Definitions acquire-release.
528 static DEFINE_PER_CPU(unsigned long, test_acqrel);
530 static void ref_acqrel_section(const int nloops)
536 for (i = nloops; i >= 0; i--) {
537 x = smp_load_acquire(this_cpu_ptr(&test_acqrel));
538 smp_store_release(this_cpu_ptr(&test_acqrel), x + 1);
543 static void ref_acqrel_delay_section(const int nloops, const int udl, const int ndl)
549 for (i = nloops; i >= 0; i--) {
550 x = smp_load_acquire(this_cpu_ptr(&test_acqrel));
552 smp_store_release(this_cpu_ptr(&test_acqrel), x + 1);
557 static const struct ref_scale_ops acqrel_ops = {
558 .readsection = ref_acqrel_section,
559 .delaysection = ref_acqrel_delay_section,
563 static volatile u64 stopopts;
565 static void ref_sched_clock_section(const int nloops)
571 for (i = nloops; i >= 0; i--)
577 static void ref_sched_clock_delay_section(const int nloops, const int udl, const int ndl)
583 for (i = nloops; i >= 0; i--) {
591 static const struct ref_scale_ops sched_clock_ops = {
592 .readsection = ref_sched_clock_section,
593 .delaysection = ref_sched_clock_delay_section,
594 .name = "sched-clock"
598 static void ref_clock_section(const int nloops)
604 for (i = nloops; i >= 0; i--)
605 x += ktime_get_real_fast_ns();
610 static void ref_clock_delay_section(const int nloops, const int udl, const int ndl)
616 for (i = nloops; i >= 0; i--) {
617 x += ktime_get_real_fast_ns();
624 static const struct ref_scale_ops clock_ops = {
625 .readsection = ref_clock_section,
626 .delaysection = ref_clock_delay_section,
630 static void ref_jiffies_section(const int nloops)
636 for (i = nloops; i >= 0; i--)
642 static void ref_jiffies_delay_section(const int nloops, const int udl, const int ndl)
648 for (i = nloops; i >= 0; i--) {
656 static const struct ref_scale_ops jiffies_ops = {
657 .readsection = ref_jiffies_section,
658 .delaysection = ref_jiffies_delay_section,
662 ////////////////////////////////////////////////////////////////////////
664 // Methods leveraging SLAB_TYPESAFE_BY_RCU.
667 // Item to look up in a typesafe manner. Array of pointers to these.
668 struct refscale_typesafe {
669 atomic_t rts_refctr; // Used by all flavors
671 seqlock_t rts_seqlock;
676 static struct kmem_cache *typesafe_kmem_cachep;
677 static struct refscale_typesafe **rtsarray;
678 static long rtsarray_size;
679 static DEFINE_TORTURE_RANDOM_PERCPU(refscale_rand);
680 static bool (*rts_acquire)(struct refscale_typesafe *rtsp, unsigned int *start);
681 static bool (*rts_release)(struct refscale_typesafe *rtsp, unsigned int start);
683 // Conditionally acquire an explicit in-structure reference count.
684 static bool typesafe_ref_acquire(struct refscale_typesafe *rtsp, unsigned int *start)
686 return atomic_inc_not_zero(&rtsp->rts_refctr);
689 // Unconditionally release an explicit in-structure reference count.
690 static bool typesafe_ref_release(struct refscale_typesafe *rtsp, unsigned int start)
692 if (!atomic_dec_return(&rtsp->rts_refctr)) {
693 WRITE_ONCE(rtsp->a, rtsp->a + 1);
694 kmem_cache_free(typesafe_kmem_cachep, rtsp);
699 // Unconditionally acquire an explicit in-structure spinlock.
700 static bool typesafe_lock_acquire(struct refscale_typesafe *rtsp, unsigned int *start)
702 spin_lock(&rtsp->rts_lock);
706 // Unconditionally release an explicit in-structure spinlock.
707 static bool typesafe_lock_release(struct refscale_typesafe *rtsp, unsigned int start)
709 spin_unlock(&rtsp->rts_lock);
713 // Unconditionally acquire an explicit in-structure sequence lock.
714 static bool typesafe_seqlock_acquire(struct refscale_typesafe *rtsp, unsigned int *start)
716 *start = read_seqbegin(&rtsp->rts_seqlock);
720 // Conditionally release an explicit in-structure sequence lock. Return
721 // true if this release was successful, that is, if no retry is required.
722 static bool typesafe_seqlock_release(struct refscale_typesafe *rtsp, unsigned int start)
724 return !read_seqretry(&rtsp->rts_seqlock, start);
727 // Do a read-side critical section with the specified delay in
728 // microseconds and nanoseconds inserted so as to increase probability
730 static void typesafe_delay_section(const int nloops, const int udl, const int ndl)
736 struct refscale_typesafe *rtsp;
739 for (i = nloops; i >= 0; i--) {
741 idx = torture_random(this_cpu_ptr(&refscale_rand)) % rtsarray_size;
745 rtsp = rcu_dereference(rtsarray[idx]);
746 a = READ_ONCE(rtsp->a);
747 if (!rts_acquire(rtsp, &start)) {
751 if (a != READ_ONCE(rtsp->a)) {
752 (void)rts_release(rtsp, start);
757 b = READ_ONCE(rtsp->a);
758 // Remember, seqlock read-side release can fail.
759 if (!rts_release(rtsp, start)) {
763 WARN_ONCE(a != b, "Re-read of ->a changed from %u to %u.\n", a, b);
766 WARN_ON_ONCE(a * a != b);
770 // Because the acquisition and release methods are expensive, there
771 // is no point in optimizing away the un_delay() function's two checks.
772 // Thus simply define typesafe_read_section() as a simple wrapper around
773 // typesafe_delay_section().
774 static void typesafe_read_section(const int nloops)
776 typesafe_delay_section(nloops, 0, 0);
779 // Allocate and initialize one refscale_typesafe structure.
780 static struct refscale_typesafe *typesafe_alloc_one(void)
782 struct refscale_typesafe *rtsp;
784 rtsp = kmem_cache_alloc(typesafe_kmem_cachep, GFP_KERNEL);
787 atomic_set(&rtsp->rts_refctr, 1);
788 WRITE_ONCE(rtsp->a, rtsp->a + 1);
789 WRITE_ONCE(rtsp->b, rtsp->a * rtsp->a);
793 // Slab-allocator constructor for refscale_typesafe structures created
794 // out of a new slab of system memory.
795 static void refscale_typesafe_ctor(void *rtsp_in)
797 struct refscale_typesafe *rtsp = rtsp_in;
799 spin_lock_init(&rtsp->rts_lock);
800 seqlock_init(&rtsp->rts_seqlock);
802 rtsp->a = torture_random(this_cpu_ptr(&refscale_rand));
806 static const struct ref_scale_ops typesafe_ref_ops;
807 static const struct ref_scale_ops typesafe_lock_ops;
808 static const struct ref_scale_ops typesafe_seqlock_ops;
810 // Initialize for a typesafe test.
811 static bool typesafe_init(void)
814 long si = lookup_instances;
816 typesafe_kmem_cachep = kmem_cache_create("refscale_typesafe",
817 sizeof(struct refscale_typesafe), sizeof(void *),
818 SLAB_TYPESAFE_BY_RCU, refscale_typesafe_ctor);
819 if (!typesafe_kmem_cachep)
822 si = -si * nr_cpu_ids;
826 rtsarray = kcalloc(si, sizeof(*rtsarray), GFP_KERNEL);
829 for (idx = 0; idx < rtsarray_size; idx++) {
830 rtsarray[idx] = typesafe_alloc_one();
834 if (cur_ops == &typesafe_ref_ops) {
835 rts_acquire = typesafe_ref_acquire;
836 rts_release = typesafe_ref_release;
837 } else if (cur_ops == &typesafe_lock_ops) {
838 rts_acquire = typesafe_lock_acquire;
839 rts_release = typesafe_lock_release;
840 } else if (cur_ops == &typesafe_seqlock_ops) {
841 rts_acquire = typesafe_seqlock_acquire;
842 rts_release = typesafe_seqlock_release;
850 // Clean up after a typesafe test.
851 static void typesafe_cleanup(void)
856 for (idx = 0; idx < rtsarray_size; idx++)
857 kmem_cache_free(typesafe_kmem_cachep, rtsarray[idx]);
862 kmem_cache_destroy(typesafe_kmem_cachep);
863 typesafe_kmem_cachep = NULL;
868 // The typesafe_init() function distinguishes these structures by address.
869 static const struct ref_scale_ops typesafe_ref_ops = {
870 .init = typesafe_init,
871 .cleanup = typesafe_cleanup,
872 .readsection = typesafe_read_section,
873 .delaysection = typesafe_delay_section,
874 .name = "typesafe_ref"
877 static const struct ref_scale_ops typesafe_lock_ops = {
878 .init = typesafe_init,
879 .cleanup = typesafe_cleanup,
880 .readsection = typesafe_read_section,
881 .delaysection = typesafe_delay_section,
882 .name = "typesafe_lock"
885 static const struct ref_scale_ops typesafe_seqlock_ops = {
886 .init = typesafe_init,
887 .cleanup = typesafe_cleanup,
888 .readsection = typesafe_read_section,
889 .delaysection = typesafe_delay_section,
890 .name = "typesafe_seqlock"
893 static void rcu_scale_one_reader(void)
896 cur_ops->readsection(loops);
898 cur_ops->delaysection(loops, readdelay / 1000, readdelay % 1000);
901 // Warm up cache, or, if needed run a series of rcu_scale_one_reader()
902 // to allow multiple rcuscale guest OSes to collect mutually valid data.
903 static void rcu_scale_warm_cool(void)
905 unsigned long jdone = jiffies + (guest_os_delay > 0 ? guest_os_delay * HZ : -1);
908 rcu_scale_one_reader();
910 } while (time_before(jiffies, jdone));
913 // Reader kthread. Repeatedly does empty RCU read-side
914 // critical section, minimizing update-side interference.
916 ref_scale_reader(void *arg)
920 struct reader_task *rt = &(reader_tasks[me]);
924 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: task started", me);
925 WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids)));
926 set_user_nice(current, MAX_NICE);
929 schedule_timeout_interruptible(holdoff * HZ);
931 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: waiting to start next experiment on cpu %d", me, raw_smp_processor_id());
933 // Wait for signal that this reader can start.
934 wait_event(rt->wq, (atomic_read(&nreaders_exp) && smp_load_acquire(&rt->start_reader)) ||
935 torture_must_stop());
937 if (torture_must_stop())
940 // Make sure that the CPU is affinitized appropriately during testing.
941 WARN_ON_ONCE(raw_smp_processor_id() != me % nr_cpu_ids);
943 WRITE_ONCE(rt->start_reader, 0);
944 if (!atomic_dec_return(&n_started))
945 while (atomic_read_acquire(&n_started))
948 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d started", me, exp_idx);
951 // To reduce noise, do an initial cache-warming invocation, check
952 // in, and then keep warming until everyone has checked in.
953 rcu_scale_one_reader();
954 if (!atomic_dec_return(&n_warmedup))
955 while (atomic_read_acquire(&n_warmedup))
956 rcu_scale_one_reader();
957 // Also keep interrupts disabled. This also has the effect
958 // of preventing entries into slow path for rcu_read_unlock().
959 local_irq_save(flags);
960 start = ktime_get_mono_fast_ns();
962 rcu_scale_one_reader();
964 duration = ktime_get_mono_fast_ns() - start;
965 local_irq_restore(flags);
967 rt->last_duration_ns = WARN_ON_ONCE(duration < 0) ? 0 : duration;
968 // To reduce runtime-skew noise, do maintain-load invocations until
970 if (!atomic_dec_return(&n_cooleddown))
971 while (atomic_read_acquire(&n_cooleddown))
972 rcu_scale_one_reader();
974 if (atomic_dec_and_test(&nreaders_exp))
977 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d ended, (readers remaining=%d)",
978 me, exp_idx, atomic_read(&nreaders_exp));
980 if (!torture_must_stop())
983 torture_kthread_stopping("ref_scale_reader");
987 static void reset_readers(void)
990 struct reader_task *rt;
992 for (i = 0; i < nreaders; i++) {
993 rt = &(reader_tasks[i]);
995 rt->last_duration_ns = 0;
999 // Print the results of each reader and return the sum of all their durations.
1000 static u64 process_durations(int n)
1003 struct reader_task *rt;
1008 buf = kmalloc(800 + 64, GFP_KERNEL);
1011 seq_buf_init(&s, buf, 800 + 64);
1013 seq_buf_printf(&s, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)",
1016 for (i = 0; i < n && !torture_must_stop(); i++) {
1017 rt = &(reader_tasks[i]);
1020 seq_buf_putc(&s, '\n');
1022 if (seq_buf_used(&s) >= 800) {
1023 pr_alert("%s", seq_buf_str(&s));
1027 seq_buf_printf(&s, "%d: %llu\t", i, rt->last_duration_ns);
1029 sum += rt->last_duration_ns;
1031 pr_alert("%s\n", seq_buf_str(&s));
1037 // The main_func is the main orchestrator, it performs a bunch of
1038 // experiments. For every experiment, it orders all the readers
1039 // involved to start and waits for them to finish the experiment. It
1040 // then reads their timestamps and starts the next experiment. Each
1041 // experiment progresses from 1 concurrent reader to N of them at which
1042 // point all the timestamps are printed.
1043 static int main_func(void *arg)
1050 set_cpus_allowed_ptr(current, cpumask_of(nreaders % nr_cpu_ids));
1051 set_user_nice(current, MAX_NICE);
1053 VERBOSE_SCALEOUT("main_func task started");
1054 result_avg = kzalloc(nruns * sizeof(*result_avg), GFP_KERNEL);
1055 buf = kzalloc(800 + 64, GFP_KERNEL);
1056 if (!result_avg || !buf) {
1057 SCALEOUT_ERRSTRING("out of memory");
1061 schedule_timeout_interruptible(holdoff * HZ);
1063 // Wait for all threads to start.
1064 atomic_inc(&n_init);
1065 while (atomic_read(&n_init) < nreaders + 1)
1066 schedule_timeout_uninterruptible(1);
1068 // Start exp readers up per experiment
1069 rcu_scale_warm_cool();
1070 for (exp = 0; exp < nruns && !torture_must_stop(); exp++) {
1071 if (torture_must_stop())
1075 atomic_set(&nreaders_exp, nreaders);
1076 atomic_set(&n_started, nreaders);
1077 atomic_set(&n_warmedup, nreaders);
1078 atomic_set(&n_cooleddown, nreaders);
1082 for (r = 0; r < nreaders; r++) {
1083 smp_store_release(&reader_tasks[r].start_reader, 1);
1084 wake_up(&reader_tasks[r].wq);
1087 VERBOSE_SCALEOUT("main_func: experiment started, waiting for %d readers",
1091 !atomic_read(&nreaders_exp) || torture_must_stop());
1093 VERBOSE_SCALEOUT("main_func: experiment ended");
1095 if (torture_must_stop())
1098 result_avg[exp] = div_u64(1000 * process_durations(nreaders), nreaders * loops);
1100 rcu_scale_warm_cool();
1102 // Print the average of all experiments
1103 SCALEOUT("END OF TEST. Calculating average duration per loop (nanoseconds)...\n");
1105 pr_alert("Runs\tTime(ns)\n");
1106 for (exp = 0; exp < nruns; exp++) {
1110 avg = div_u64_rem(result_avg[exp], 1000, &rem);
1111 sprintf(buf1, "%d\t%llu.%03u\n", exp + 1, avg, rem);
1113 if (strlen(buf) >= 800) {
1114 pr_alert("%s", buf);
1119 pr_alert("%s", buf);
1122 // This will shutdown everything including us.
1125 wake_up(&shutdown_wq);
1128 // Wait for torture to stop us
1129 while (!torture_must_stop())
1130 schedule_timeout_uninterruptible(1);
1133 torture_kthread_stopping("main_func");
1140 ref_scale_print_module_parms(const struct ref_scale_ops *cur_ops, const char *tag)
1142 pr_alert("%s" SCALE_FLAG
1143 "--- %s: verbose=%d verbose_batched=%d shutdown=%d holdoff=%d lookup_instances=%ld loops=%ld nreaders=%d nruns=%d readdelay=%d\n", scale_type, tag,
1144 verbose, verbose_batched, shutdown, holdoff, lookup_instances, loops, nreaders, nruns, readdelay);
1148 ref_scale_cleanup(void)
1152 if (torture_cleanup_begin())
1156 torture_cleanup_end();
1161 for (i = 0; i < nreaders; i++)
1162 torture_stop_kthread("ref_scale_reader",
1163 reader_tasks[i].task);
1165 kfree(reader_tasks);
1167 torture_stop_kthread("main_task", main_task);
1170 // Do scale-type-specific cleanup operations.
1171 if (cur_ops->cleanup != NULL)
1174 torture_cleanup_end();
1177 // Shutdown kthread. Just waits to be awakened, then shuts down system.
1179 ref_scale_shutdown(void *arg)
1181 wait_event_idle(shutdown_wq, shutdown_start);
1183 smp_mb(); // Wake before output.
1184 ref_scale_cleanup();
1191 ref_scale_init(void)
1195 static const struct ref_scale_ops *scale_ops[] = {
1196 &rcu_ops, &srcu_ops, &srcu_fast_ops, &srcu_lite_ops, RCU_TRACE_OPS RCU_TASKS_OPS
1197 &refcnt_ops, &rwlock_ops, &rwsem_ops, &lock_ops, &lock_irq_ops,
1198 &acqrel_ops, &sched_clock_ops, &clock_ops, &jiffies_ops,
1199 &typesafe_ref_ops, &typesafe_lock_ops, &typesafe_seqlock_ops,
1202 if (!torture_init_begin(scale_type, verbose))
1205 for (i = 0; i < ARRAY_SIZE(scale_ops); i++) {
1206 cur_ops = scale_ops[i];
1207 if (strcmp(scale_type, cur_ops->name) == 0)
1210 if (i == ARRAY_SIZE(scale_ops)) {
1211 pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type);
1212 pr_alert("rcu-scale types:");
1213 for (i = 0; i < ARRAY_SIZE(scale_ops); i++)
1214 pr_cont(" %s", scale_ops[i]->name);
1221 if (!cur_ops->init()) {
1222 firsterr = -EUCLEAN;
1226 ref_scale_print_module_parms(cur_ops, "Start of test");
1230 init_waitqueue_head(&shutdown_wq);
1231 firsterr = torture_create_kthread(ref_scale_shutdown, NULL,
1233 if (torture_init_error(firsterr))
1235 schedule_timeout_uninterruptible(1);
1238 // Reader tasks (default to ~75% of online CPUs).
1240 nreaders = (num_online_cpus() >> 1) + (num_online_cpus() >> 2);
1241 if (WARN_ONCE(loops <= 0, "%s: loops = %ld, adjusted to 1\n", __func__, loops))
1243 if (WARN_ONCE(nreaders <= 0, "%s: nreaders = %d, adjusted to 1\n", __func__, nreaders))
1245 if (WARN_ONCE(nruns <= 0, "%s: nruns = %d, adjusted to 1\n", __func__, nruns))
1247 reader_tasks = kcalloc(nreaders, sizeof(reader_tasks[0]),
1249 if (!reader_tasks) {
1250 SCALEOUT_ERRSTRING("out of memory");
1255 VERBOSE_SCALEOUT("Starting %d reader threads", nreaders);
1257 for (i = 0; i < nreaders; i++) {
1258 init_waitqueue_head(&reader_tasks[i].wq);
1259 firsterr = torture_create_kthread(ref_scale_reader, (void *)i,
1260 reader_tasks[i].task);
1261 if (torture_init_error(firsterr))
1266 init_waitqueue_head(&main_wq);
1267 firsterr = torture_create_kthread(main_func, NULL, main_task);
1268 if (torture_init_error(firsterr))
1276 ref_scale_cleanup();
1278 WARN_ON(!IS_MODULE(CONFIG_RCU_REF_SCALE_TEST));
1284 module_init(ref_scale_init);
1285 module_exit(ref_scale_cleanup);