1 // SPDX-License-Identifier: GPL-2.0+
3 * Read-Copy Update module-based scalability-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>
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>
41 #include <linux/rcupdate_trace.h>
45 MODULE_LICENSE("GPL");
46 MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>");
48 #define SCALE_FLAG "-scale:"
49 #define SCALEOUT_STRING(s) \
50 pr_alert("%s" SCALE_FLAG " %s\n", scale_type, s)
51 #define VERBOSE_SCALEOUT_STRING(s) \
52 do { if (verbose) pr_alert("%s" SCALE_FLAG " %s\n", scale_type, s); } while (0)
53 #define SCALEOUT_ERRSTRING(s) \
54 pr_alert("%s" SCALE_FLAG "!!! %s\n", scale_type, s)
57 * The intended use cases for the nreaders and nwriters module parameters
60 * 1. Specify only the nr_cpus kernel boot parameter. This will
61 * set both nreaders and nwriters to the value specified by
62 * nr_cpus for a mixed reader/writer test.
64 * 2. Specify the nr_cpus kernel boot parameter, but set
65 * rcuscale.nreaders to zero. This will set nwriters to the
66 * value specified by nr_cpus for an update-only test.
68 * 3. Specify the nr_cpus kernel boot parameter, but set
69 * rcuscale.nwriters to zero. This will set nreaders to the
70 * value specified by nr_cpus for a read-only test.
72 * Various other use cases may of course be specified.
74 * Note that this test's readers are intended only as a test load for
75 * the writers. The reader scalability statistics will be overly
76 * pessimistic due to the per-critical-section interrupt disabling,
77 * test-end checks, and the pair of calls through pointers.
81 # define RCUSCALE_SHUTDOWN 0
83 # define RCUSCALE_SHUTDOWN 1
86 torture_param(bool, gp_async, false, "Use asynchronous GP wait primitives");
87 torture_param(int, gp_async_max, 1000, "Max # outstanding waits per writer");
88 torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
89 torture_param(int, holdoff, 10, "Holdoff time before test start (s)");
90 torture_param(int, minruntime, 0, "Minimum run time (s)");
91 torture_param(int, nreaders, -1, "Number of RCU reader threads");
92 torture_param(int, nwriters, -1, "Number of RCU updater threads");
93 torture_param(bool, shutdown, RCUSCALE_SHUTDOWN,
94 "Shutdown at end of scalability tests.");
95 torture_param(int, verbose, 1, "Enable verbose debugging printk()s");
96 torture_param(int, writer_holdoff, 0, "Holdoff (us) between GPs, zero to disable");
97 torture_param(int, writer_holdoff_jiffies, 0, "Holdoff (jiffies) between GPs, zero to disable");
98 torture_param(int, kfree_rcu_test, 0, "Do we run a kfree_rcu() scale test?");
99 torture_param(int, kfree_mult, 1, "Multiple of kfree_obj size to allocate.");
100 torture_param(int, kfree_by_call_rcu, 0, "Use call_rcu() to emulate kfree_rcu()?");
102 static char *scale_type = "rcu";
103 module_param(scale_type, charp, 0444);
104 MODULE_PARM_DESC(scale_type, "Type of RCU to scalability-test (rcu, srcu, ...)");
106 static int nrealreaders;
107 static int nrealwriters;
108 static struct task_struct **writer_tasks;
109 static struct task_struct **reader_tasks;
110 static struct task_struct *shutdown_task;
112 static u64 **writer_durations;
113 static int *writer_n_durations;
114 static atomic_t n_rcu_scale_reader_started;
115 static atomic_t n_rcu_scale_writer_started;
116 static atomic_t n_rcu_scale_writer_finished;
117 static wait_queue_head_t shutdown_wq;
118 static u64 t_rcu_scale_writer_started;
119 static u64 t_rcu_scale_writer_finished;
120 static unsigned long b_rcu_gp_test_started;
121 static unsigned long b_rcu_gp_test_finished;
122 static DEFINE_PER_CPU(atomic_t, n_async_inflight);
124 #define MAX_MEAS 10000
128 * Operations vector for selecting different types of tests.
131 struct rcu_scale_ops {
134 void (*cleanup)(void);
135 int (*readlock)(void);
136 void (*readunlock)(int idx);
137 unsigned long (*get_gp_seq)(void);
138 unsigned long (*gp_diff)(unsigned long new, unsigned long old);
139 unsigned long (*exp_completed)(void);
140 void (*async)(struct rcu_head *head, rcu_callback_t func);
141 void (*gp_barrier)(void);
143 void (*exp_sync)(void);
144 struct task_struct *(*rso_gp_kthread)(void);
148 static struct rcu_scale_ops *cur_ops;
151 * Definitions for rcu scalability testing.
154 static int rcu_scale_read_lock(void) __acquires(RCU)
160 static void rcu_scale_read_unlock(int idx) __releases(RCU)
165 static unsigned long __maybe_unused rcu_no_completed(void)
170 static void rcu_sync_scale_init(void)
174 static struct rcu_scale_ops rcu_ops = {
176 .init = rcu_sync_scale_init,
177 .readlock = rcu_scale_read_lock,
178 .readunlock = rcu_scale_read_unlock,
179 .get_gp_seq = rcu_get_gp_seq,
180 .gp_diff = rcu_seq_diff,
181 .exp_completed = rcu_exp_batches_completed,
182 .async = call_rcu_hurry,
183 .gp_barrier = rcu_barrier,
184 .sync = synchronize_rcu,
185 .exp_sync = synchronize_rcu_expedited,
190 * Definitions for srcu scalability testing.
193 DEFINE_STATIC_SRCU(srcu_ctl_scale);
194 static struct srcu_struct *srcu_ctlp = &srcu_ctl_scale;
196 static int srcu_scale_read_lock(void) __acquires(srcu_ctlp)
198 return srcu_read_lock(srcu_ctlp);
201 static void srcu_scale_read_unlock(int idx) __releases(srcu_ctlp)
203 srcu_read_unlock(srcu_ctlp, idx);
206 static unsigned long srcu_scale_completed(void)
208 return srcu_batches_completed(srcu_ctlp);
211 static void srcu_call_rcu(struct rcu_head *head, rcu_callback_t func)
213 call_srcu(srcu_ctlp, head, func);
216 static void srcu_rcu_barrier(void)
218 srcu_barrier(srcu_ctlp);
221 static void srcu_scale_synchronize(void)
223 synchronize_srcu(srcu_ctlp);
226 static void srcu_scale_synchronize_expedited(void)
228 synchronize_srcu_expedited(srcu_ctlp);
231 static struct rcu_scale_ops srcu_ops = {
232 .ptype = SRCU_FLAVOR,
233 .init = rcu_sync_scale_init,
234 .readlock = srcu_scale_read_lock,
235 .readunlock = srcu_scale_read_unlock,
236 .get_gp_seq = srcu_scale_completed,
237 .gp_diff = rcu_seq_diff,
238 .exp_completed = srcu_scale_completed,
239 .async = srcu_call_rcu,
240 .gp_barrier = srcu_rcu_barrier,
241 .sync = srcu_scale_synchronize,
242 .exp_sync = srcu_scale_synchronize_expedited,
246 static struct srcu_struct srcud;
248 static void srcu_sync_scale_init(void)
251 init_srcu_struct(srcu_ctlp);
254 static void srcu_sync_scale_cleanup(void)
256 cleanup_srcu_struct(srcu_ctlp);
259 static struct rcu_scale_ops srcud_ops = {
260 .ptype = SRCU_FLAVOR,
261 .init = srcu_sync_scale_init,
262 .cleanup = srcu_sync_scale_cleanup,
263 .readlock = srcu_scale_read_lock,
264 .readunlock = srcu_scale_read_unlock,
265 .get_gp_seq = srcu_scale_completed,
266 .gp_diff = rcu_seq_diff,
267 .exp_completed = srcu_scale_completed,
268 .async = srcu_call_rcu,
269 .gp_barrier = srcu_rcu_barrier,
270 .sync = srcu_scale_synchronize,
271 .exp_sync = srcu_scale_synchronize_expedited,
275 #ifdef CONFIG_TASKS_RCU
278 * Definitions for RCU-tasks scalability testing.
281 static int tasks_scale_read_lock(void)
286 static void tasks_scale_read_unlock(int idx)
290 static struct rcu_scale_ops tasks_ops = {
291 .ptype = RCU_TASKS_FLAVOR,
292 .init = rcu_sync_scale_init,
293 .readlock = tasks_scale_read_lock,
294 .readunlock = tasks_scale_read_unlock,
295 .get_gp_seq = rcu_no_completed,
296 .gp_diff = rcu_seq_diff,
297 .async = call_rcu_tasks,
298 .gp_barrier = rcu_barrier_tasks,
299 .sync = synchronize_rcu_tasks,
300 .exp_sync = synchronize_rcu_tasks,
301 .rso_gp_kthread = get_rcu_tasks_gp_kthread,
305 #define TASKS_OPS &tasks_ops,
307 #else // #ifdef CONFIG_TASKS_RCU
311 #endif // #else // #ifdef CONFIG_TASKS_RCU
313 #ifdef CONFIG_TASKS_RUDE_RCU
316 * Definitions for RCU-tasks-rude scalability testing.
319 static int tasks_rude_scale_read_lock(void)
324 static void tasks_rude_scale_read_unlock(int idx)
328 static struct rcu_scale_ops tasks_rude_ops = {
329 .ptype = RCU_TASKS_RUDE_FLAVOR,
330 .init = rcu_sync_scale_init,
331 .readlock = tasks_rude_scale_read_lock,
332 .readunlock = tasks_rude_scale_read_unlock,
333 .get_gp_seq = rcu_no_completed,
334 .gp_diff = rcu_seq_diff,
335 .async = call_rcu_tasks_rude,
336 .gp_barrier = rcu_barrier_tasks_rude,
337 .sync = synchronize_rcu_tasks_rude,
338 .exp_sync = synchronize_rcu_tasks_rude,
339 .rso_gp_kthread = get_rcu_tasks_rude_gp_kthread,
343 #define TASKS_RUDE_OPS &tasks_rude_ops,
345 #else // #ifdef CONFIG_TASKS_RUDE_RCU
347 #define TASKS_RUDE_OPS
349 #endif // #else // #ifdef CONFIG_TASKS_RUDE_RCU
351 #ifdef CONFIG_TASKS_TRACE_RCU
354 * Definitions for RCU-tasks-trace scalability testing.
357 static int tasks_trace_scale_read_lock(void)
359 rcu_read_lock_trace();
363 static void tasks_trace_scale_read_unlock(int idx)
365 rcu_read_unlock_trace();
368 static struct rcu_scale_ops tasks_tracing_ops = {
369 .ptype = RCU_TASKS_FLAVOR,
370 .init = rcu_sync_scale_init,
371 .readlock = tasks_trace_scale_read_lock,
372 .readunlock = tasks_trace_scale_read_unlock,
373 .get_gp_seq = rcu_no_completed,
374 .gp_diff = rcu_seq_diff,
375 .async = call_rcu_tasks_trace,
376 .gp_barrier = rcu_barrier_tasks_trace,
377 .sync = synchronize_rcu_tasks_trace,
378 .exp_sync = synchronize_rcu_tasks_trace,
379 .rso_gp_kthread = get_rcu_tasks_trace_gp_kthread,
380 .name = "tasks-tracing"
383 #define TASKS_TRACING_OPS &tasks_tracing_ops,
385 #else // #ifdef CONFIG_TASKS_TRACE_RCU
387 #define TASKS_TRACING_OPS
389 #endif // #else // #ifdef CONFIG_TASKS_TRACE_RCU
391 static unsigned long rcuscale_seq_diff(unsigned long new, unsigned long old)
393 if (!cur_ops->gp_diff)
395 return cur_ops->gp_diff(new, old);
399 * If scalability tests complete, wait for shutdown to commence.
401 static void rcu_scale_wait_shutdown(void)
403 cond_resched_tasks_rcu_qs();
404 if (atomic_read(&n_rcu_scale_writer_finished) < nrealwriters)
406 while (!torture_must_stop())
407 schedule_timeout_uninterruptible(1);
411 * RCU scalability reader kthread. Repeatedly does empty RCU read-side
412 * critical section, minimizing update-side interference. However, the
413 * point of this test is not to evaluate reader scalability, but instead
414 * to serve as a test load for update-side scalability testing.
417 rcu_scale_reader(void *arg)
423 VERBOSE_SCALEOUT_STRING("rcu_scale_reader task started");
424 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
425 set_user_nice(current, MAX_NICE);
426 atomic_inc(&n_rcu_scale_reader_started);
429 local_irq_save(flags);
430 idx = cur_ops->readlock();
431 cur_ops->readunlock(idx);
432 local_irq_restore(flags);
433 rcu_scale_wait_shutdown();
434 } while (!torture_must_stop());
435 torture_kthread_stopping("rcu_scale_reader");
440 * Callback function for asynchronous grace periods from rcu_scale_writer().
442 static void rcu_scale_async_cb(struct rcu_head *rhp)
444 atomic_dec(this_cpu_ptr(&n_async_inflight));
449 * RCU scale writer kthread. Repeatedly does a grace period.
452 rcu_scale_writer(void *arg)
458 struct rcu_head *rhp = NULL;
459 bool started = false, done = false, alldone = false;
461 DEFINE_TORTURE_RANDOM(tr);
463 u64 *wdpp = writer_durations[me];
465 VERBOSE_SCALEOUT_STRING("rcu_scale_writer task started");
467 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
468 current->flags |= PF_NO_SETAFFINITY;
469 sched_set_fifo_low(current);
472 schedule_timeout_idle(holdoff * HZ);
475 * Wait until rcu_end_inkernel_boot() is called for normal GP tests
476 * so that RCU is not always expedited for normal GP tests.
477 * The system_state test is approximate, but works well in practice.
479 while (!gp_exp && system_state != SYSTEM_RUNNING)
480 schedule_timeout_uninterruptible(1);
482 t = ktime_get_mono_fast_ns();
483 if (atomic_inc_return(&n_rcu_scale_writer_started) >= nrealwriters) {
484 t_rcu_scale_writer_started = t;
486 b_rcu_gp_test_started =
487 cur_ops->exp_completed() / 2;
489 b_rcu_gp_test_started = cur_ops->get_gp_seq();
493 jdone = jiffies + minruntime * HZ;
496 udelay(writer_holdoff);
497 if (writer_holdoff_jiffies)
498 schedule_timeout_idle(torture_random(&tr) % writer_holdoff_jiffies + 1);
500 *wdp = ktime_get_mono_fast_ns();
504 rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
505 if (rhp && atomic_read(this_cpu_ptr(&n_async_inflight)) < gp_async_max) {
506 atomic_inc(this_cpu_ptr(&n_async_inflight));
507 cur_ops->async(rhp, rcu_scale_async_cb);
509 } else if (!kthread_should_stop()) {
510 cur_ops->gp_barrier();
513 kfree(rhp); /* Because we are stopping. */
520 t = ktime_get_mono_fast_ns();
524 atomic_read(&n_rcu_scale_writer_started) >= nrealwriters)
526 if (!done && i >= MIN_MEAS && time_after(jiffies, jdone)) {
528 sched_set_normal(current, 0);
529 pr_alert("%s%s rcu_scale_writer %ld has %d measurements\n",
530 scale_type, SCALE_FLAG, me, MIN_MEAS);
531 if (atomic_inc_return(&n_rcu_scale_writer_finished) >=
533 schedule_timeout_interruptible(10);
534 rcu_ftrace_dump(DUMP_ALL);
535 SCALEOUT_STRING("Test complete");
536 t_rcu_scale_writer_finished = t;
538 b_rcu_gp_test_finished =
539 cur_ops->exp_completed() / 2;
541 b_rcu_gp_test_finished =
542 cur_ops->get_gp_seq();
545 smp_mb(); /* Assign before wake. */
546 wake_up(&shutdown_wq);
550 if (done && !alldone &&
551 atomic_read(&n_rcu_scale_writer_finished) >= nrealwriters)
553 if (started && !alldone && i < MAX_MEAS - 1)
555 rcu_scale_wait_shutdown();
556 } while (!torture_must_stop());
558 cur_ops->gp_barrier();
560 writer_n_durations[me] = i_max + 1;
561 torture_kthread_stopping("rcu_scale_writer");
566 rcu_scale_print_module_parms(struct rcu_scale_ops *cur_ops, const char *tag)
568 pr_alert("%s" SCALE_FLAG
569 "--- %s: gp_async=%d gp_async_max=%d gp_exp=%d holdoff=%d minruntime=%d nreaders=%d nwriters=%d writer_holdoff=%d writer_holdoff_jiffies=%d verbose=%d shutdown=%d\n",
570 scale_type, tag, gp_async, gp_async_max, gp_exp, holdoff, minruntime, nrealreaders, nrealwriters, writer_holdoff, writer_holdoff_jiffies, verbose, shutdown);
574 * Return the number if non-negative. If -1, the number of CPUs.
575 * If less than -1, that much less than the number of CPUs, but
578 static int compute_real(int n)
585 nr = num_online_cpus() + 1 + n;
593 * kfree_rcu() scalability tests: Start a kfree_rcu() loop on all CPUs for number
594 * of iterations and measure total time and number of GP for all iterations to complete.
597 torture_param(int, kfree_nthreads, -1, "Number of threads running loops of kfree_rcu().");
598 torture_param(int, kfree_alloc_num, 8000, "Number of allocations and frees done in an iteration.");
599 torture_param(int, kfree_loops, 10, "Number of loops doing kfree_alloc_num allocations and frees.");
600 torture_param(bool, kfree_rcu_test_double, false, "Do we run a kfree_rcu() double-argument scale test?");
601 torture_param(bool, kfree_rcu_test_single, false, "Do we run a kfree_rcu() single-argument scale test?");
603 static struct task_struct **kfree_reader_tasks;
604 static int kfree_nrealthreads;
605 static atomic_t n_kfree_scale_thread_started;
606 static atomic_t n_kfree_scale_thread_ended;
607 static struct task_struct *kthread_tp;
608 static u64 kthread_stime;
615 /* Used if doing RCU-kfree'ing via call_rcu(). */
616 static void kfree_call_rcu(struct rcu_head *rh)
618 struct kfree_obj *obj = container_of(rh, struct kfree_obj, rh);
624 kfree_scale_thread(void *arg)
628 struct kfree_obj *alloc_ptr;
629 u64 start_time, end_time;
630 long long mem_begin, mem_during = 0;
631 bool kfree_rcu_test_both;
632 DEFINE_TORTURE_RANDOM(tr);
634 VERBOSE_SCALEOUT_STRING("kfree_scale_thread task started");
635 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
636 set_user_nice(current, MAX_NICE);
637 kfree_rcu_test_both = (kfree_rcu_test_single == kfree_rcu_test_double);
639 start_time = ktime_get_mono_fast_ns();
641 if (atomic_inc_return(&n_kfree_scale_thread_started) >= kfree_nrealthreads) {
643 b_rcu_gp_test_started = cur_ops->exp_completed() / 2;
645 b_rcu_gp_test_started = cur_ops->get_gp_seq();
650 mem_during = mem_begin = si_mem_available();
651 } else if (loop % (kfree_loops / 4) == 0) {
652 mem_during = (mem_during + si_mem_available()) / 2;
655 for (i = 0; i < kfree_alloc_num; i++) {
656 alloc_ptr = kmalloc(kfree_mult * sizeof(struct kfree_obj), GFP_KERNEL);
660 if (kfree_by_call_rcu) {
661 call_rcu(&(alloc_ptr->rh), kfree_call_rcu);
665 // By default kfree_rcu_test_single and kfree_rcu_test_double are
666 // initialized to false. If both have the same value (false or true)
667 // both are randomly tested, otherwise only the one with value true
669 if ((kfree_rcu_test_single && !kfree_rcu_test_double) ||
670 (kfree_rcu_test_both && torture_random(&tr) & 0x800))
671 kfree_rcu_mightsleep(alloc_ptr);
673 kfree_rcu(alloc_ptr, rh);
677 } while (!torture_must_stop() && ++loop < kfree_loops);
679 if (atomic_inc_return(&n_kfree_scale_thread_ended) >= kfree_nrealthreads) {
680 end_time = ktime_get_mono_fast_ns();
683 b_rcu_gp_test_finished = cur_ops->exp_completed() / 2;
685 b_rcu_gp_test_finished = cur_ops->get_gp_seq();
687 pr_alert("Total time taken by all kfree'ers: %llu ns, loops: %d, batches: %ld, memory footprint: %lldMB\n",
688 (unsigned long long)(end_time - start_time), kfree_loops,
689 rcuscale_seq_diff(b_rcu_gp_test_finished, b_rcu_gp_test_started),
690 (mem_begin - mem_during) >> (20 - PAGE_SHIFT));
693 smp_mb(); /* Assign before wake. */
694 wake_up(&shutdown_wq);
698 torture_kthread_stopping("kfree_scale_thread");
703 kfree_scale_cleanup(void)
707 if (torture_cleanup_begin())
710 if (kfree_reader_tasks) {
711 for (i = 0; i < kfree_nrealthreads; i++)
712 torture_stop_kthread(kfree_scale_thread,
713 kfree_reader_tasks[i]);
714 kfree(kfree_reader_tasks);
717 torture_cleanup_end();
721 * shutdown kthread. Just waits to be awakened, then shuts down system.
724 kfree_scale_shutdown(void *arg)
726 wait_event_idle(shutdown_wq,
727 atomic_read(&n_kfree_scale_thread_ended) >= kfree_nrealthreads);
729 smp_mb(); /* Wake before output. */
731 kfree_scale_cleanup();
736 // Used if doing RCU-kfree'ing via call_rcu().
737 static unsigned long jiffies_at_lazy_cb;
738 static struct rcu_head lazy_test1_rh;
739 static int rcu_lazy_test1_cb_called;
740 static void call_rcu_lazy_test1(struct rcu_head *rh)
742 jiffies_at_lazy_cb = jiffies;
743 WRITE_ONCE(rcu_lazy_test1_cb_called, 1);
747 kfree_scale_init(void)
751 unsigned long jif_start;
752 unsigned long orig_jif;
754 pr_alert("%s" SCALE_FLAG
755 "--- kfree_rcu_test: kfree_mult=%d kfree_by_call_rcu=%d kfree_nthreads=%d kfree_alloc_num=%d kfree_loops=%d kfree_rcu_test_double=%d kfree_rcu_test_single=%d\n",
756 scale_type, kfree_mult, kfree_by_call_rcu, kfree_nthreads, kfree_alloc_num, kfree_loops, kfree_rcu_test_double, kfree_rcu_test_single);
758 // Also, do a quick self-test to ensure laziness is as much as
760 if (kfree_by_call_rcu && !IS_ENABLED(CONFIG_RCU_LAZY)) {
761 pr_alert("CONFIG_RCU_LAZY is disabled, falling back to kfree_rcu() for delayed RCU kfree'ing\n");
762 kfree_by_call_rcu = 0;
765 if (kfree_by_call_rcu) {
766 /* do a test to check the timeout. */
767 orig_jif = rcu_lazy_get_jiffies_till_flush();
769 rcu_lazy_set_jiffies_till_flush(2 * HZ);
773 jiffies_at_lazy_cb = 0;
774 call_rcu(&lazy_test1_rh, call_rcu_lazy_test1);
776 smp_cond_load_relaxed(&rcu_lazy_test1_cb_called, VAL == 1);
778 rcu_lazy_set_jiffies_till_flush(orig_jif);
780 if (WARN_ON_ONCE(jiffies_at_lazy_cb - jif_start < 2 * HZ)) {
781 pr_alert("ERROR: call_rcu() CBs are not being lazy as expected!\n");
786 if (WARN_ON_ONCE(jiffies_at_lazy_cb - jif_start > 3 * HZ)) {
787 pr_alert("ERROR: call_rcu() CBs are being too lazy!\n");
793 kfree_nrealthreads = compute_real(kfree_nthreads);
794 /* Start up the kthreads. */
796 init_waitqueue_head(&shutdown_wq);
797 firsterr = torture_create_kthread(kfree_scale_shutdown, NULL,
799 if (torture_init_error(firsterr))
801 schedule_timeout_uninterruptible(1);
804 pr_alert("kfree object size=%zu, kfree_by_call_rcu=%d\n",
805 kfree_mult * sizeof(struct kfree_obj),
808 kfree_reader_tasks = kcalloc(kfree_nrealthreads, sizeof(kfree_reader_tasks[0]),
810 if (kfree_reader_tasks == NULL) {
815 for (i = 0; i < kfree_nrealthreads; i++) {
816 firsterr = torture_create_kthread(kfree_scale_thread, (void *)i,
817 kfree_reader_tasks[i]);
818 if (torture_init_error(firsterr))
822 while (atomic_read(&n_kfree_scale_thread_started) < kfree_nrealthreads)
823 schedule_timeout_uninterruptible(1);
830 kfree_scale_cleanup();
835 rcu_scale_cleanup(void)
844 * Would like warning at start, but everything is expedited
845 * during the mid-boot phase, so have to wait till the end.
847 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp)
848 SCALEOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
849 if (rcu_gp_is_normal() && gp_exp)
850 SCALEOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
851 if (gp_exp && gp_async)
852 SCALEOUT_ERRSTRING("No expedited async GPs, so went with async!");
854 // If built-in, just report all of the GP kthread's CPU time.
855 if (IS_BUILTIN(CONFIG_RCU_SCALE_TEST) && !kthread_tp && cur_ops->rso_gp_kthread)
856 kthread_tp = cur_ops->rso_gp_kthread();
861 kthread_stime = kthread_tp->stime - kthread_stime;
862 us = div_u64_rem(kthread_stime, 1000, &ns);
863 pr_info("rcu_scale: Grace-period kthread CPU time: %llu.%03u us\n", us, ns);
864 show_rcu_gp_kthreads();
866 if (kfree_rcu_test) {
867 kfree_scale_cleanup();
871 if (torture_cleanup_begin())
874 torture_cleanup_end();
879 for (i = 0; i < nrealreaders; i++)
880 torture_stop_kthread(rcu_scale_reader,
886 for (i = 0; i < nrealwriters; i++) {
887 torture_stop_kthread(rcu_scale_writer,
889 if (!writer_n_durations)
891 j = writer_n_durations[i];
892 pr_alert("%s%s writer %d gps: %d\n",
893 scale_type, SCALE_FLAG, i, j);
896 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
897 scale_type, SCALE_FLAG,
898 t_rcu_scale_writer_started, t_rcu_scale_writer_finished,
899 t_rcu_scale_writer_finished -
900 t_rcu_scale_writer_started,
902 rcuscale_seq_diff(b_rcu_gp_test_finished,
903 b_rcu_gp_test_started));
904 for (i = 0; i < nrealwriters; i++) {
905 if (!writer_durations)
907 if (!writer_n_durations)
909 wdpp = writer_durations[i];
912 for (j = 0; j < writer_n_durations[i]; j++) {
914 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
915 scale_type, SCALE_FLAG,
918 schedule_timeout_uninterruptible(1);
920 kfree(writer_durations[i]);
923 kfree(writer_durations);
924 kfree(writer_n_durations);
927 /* Do torture-type-specific cleanup operations. */
928 if (cur_ops->cleanup != NULL)
931 torture_cleanup_end();
935 * RCU scalability shutdown kthread. Just waits to be awakened, then shuts
939 rcu_scale_shutdown(void *arg)
941 wait_event_idle(shutdown_wq, atomic_read(&n_rcu_scale_writer_finished) >= nrealwriters);
942 smp_mb(); /* Wake before output. */
953 static struct rcu_scale_ops *scale_ops[] = {
954 &rcu_ops, &srcu_ops, &srcud_ops, TASKS_OPS TASKS_RUDE_OPS TASKS_TRACING_OPS
957 if (!torture_init_begin(scale_type, verbose))
960 /* Process args and announce that the scalability'er is on the job. */
961 for (i = 0; i < ARRAY_SIZE(scale_ops); i++) {
962 cur_ops = scale_ops[i];
963 if (strcmp(scale_type, cur_ops->name) == 0)
966 if (i == ARRAY_SIZE(scale_ops)) {
967 pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type);
968 pr_alert("rcu-scale types:");
969 for (i = 0; i < ARRAY_SIZE(scale_ops); i++)
970 pr_cont(" %s", scale_ops[i]->name);
979 if (cur_ops->rso_gp_kthread) {
980 kthread_tp = cur_ops->rso_gp_kthread();
982 kthread_stime = kthread_tp->stime;
985 return kfree_scale_init();
987 nrealwriters = compute_real(nwriters);
988 nrealreaders = compute_real(nreaders);
989 atomic_set(&n_rcu_scale_reader_started, 0);
990 atomic_set(&n_rcu_scale_writer_started, 0);
991 atomic_set(&n_rcu_scale_writer_finished, 0);
992 rcu_scale_print_module_parms(cur_ops, "Start of test");
994 /* Start up the kthreads. */
997 init_waitqueue_head(&shutdown_wq);
998 firsterr = torture_create_kthread(rcu_scale_shutdown, NULL,
1000 if (torture_init_error(firsterr))
1002 schedule_timeout_uninterruptible(1);
1004 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
1006 if (reader_tasks == NULL) {
1007 SCALEOUT_ERRSTRING("out of memory");
1011 for (i = 0; i < nrealreaders; i++) {
1012 firsterr = torture_create_kthread(rcu_scale_reader, (void *)i,
1014 if (torture_init_error(firsterr))
1017 while (atomic_read(&n_rcu_scale_reader_started) < nrealreaders)
1018 schedule_timeout_uninterruptible(1);
1019 writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
1021 writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
1023 writer_n_durations =
1024 kcalloc(nrealwriters, sizeof(*writer_n_durations),
1026 if (!writer_tasks || !writer_durations || !writer_n_durations) {
1027 SCALEOUT_ERRSTRING("out of memory");
1031 for (i = 0; i < nrealwriters; i++) {
1032 writer_durations[i] =
1033 kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
1035 if (!writer_durations[i]) {
1039 firsterr = torture_create_kthread(rcu_scale_writer, (void *)i,
1041 if (torture_init_error(firsterr))
1049 rcu_scale_cleanup();
1051 WARN_ON(!IS_MODULE(CONFIG_RCU_SCALE_TEST));
1057 module_init(rcu_scale_init);
1058 module_exit(rcu_scale_cleanup);