2 * Read-Copy Update module-based torture test facility
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
18 * Copyright (C) IBM Corporation, 2005, 2006
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
21 * Josh Triplett <josh@joshtriplett.org>
23 * See also: Documentation/RCU/torture.txt
26 #define pr_fmt(fmt) fmt
28 #include <linux/types.h>
29 #include <linux/kernel.h>
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/kthread.h>
33 #include <linux/err.h>
34 #include <linux/spinlock.h>
35 #include <linux/smp.h>
36 #include <linux/rcupdate.h>
37 #include <linux/interrupt.h>
38 #include <linux/sched/signal.h>
39 #include <uapi/linux/sched/types.h>
40 #include <linux/atomic.h>
41 #include <linux/bitops.h>
42 #include <linux/completion.h>
43 #include <linux/moduleparam.h>
44 #include <linux/percpu.h>
45 #include <linux/notifier.h>
46 #include <linux/reboot.h>
47 #include <linux/freezer.h>
48 #include <linux/cpu.h>
49 #include <linux/delay.h>
50 #include <linux/stat.h>
51 #include <linux/srcu.h>
52 #include <linux/slab.h>
53 #include <linux/trace_clock.h>
54 #include <asm/byteorder.h>
55 #include <linux/torture.h>
56 #include <linux/vmalloc.h>
57 #include <linux/sched/debug.h>
61 MODULE_LICENSE("GPL");
62 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and Josh Triplett <josh@joshtriplett.org>");
65 /* Bits for ->extendables field, extendables param, and related definitions. */
66 #define RCUTORTURE_RDR_SHIFT 8 /* Put SRCU index in upper bits. */
67 #define RCUTORTURE_RDR_MASK ((1 << RCUTORTURE_RDR_SHIFT) - 1)
68 #define RCUTORTURE_RDR_BH 0x1 /* Extend readers by disabling bh. */
69 #define RCUTORTURE_RDR_IRQ 0x2 /* ... disabling interrupts. */
70 #define RCUTORTURE_RDR_PREEMPT 0x4 /* ... disabling preemption. */
71 #define RCUTORTURE_RDR_RCU 0x8 /* ... entering another RCU reader. */
72 #define RCUTORTURE_RDR_NBITS 4 /* Number of bits defined above. */
73 #define RCUTORTURE_MAX_EXTEND (RCUTORTURE_RDR_BH | RCUTORTURE_RDR_IRQ | \
74 RCUTORTURE_RDR_PREEMPT)
75 #define RCUTORTURE_RDR_MAX_LOOPS 0x7 /* Maximum reader extensions. */
76 /* Must be power of two minus one. */
78 torture_param(int, cbflood_inter_holdoff, HZ,
79 "Holdoff between floods (jiffies)");
80 torture_param(int, cbflood_intra_holdoff, 1,
81 "Holdoff between bursts (jiffies)");
82 torture_param(int, cbflood_n_burst, 3, "# bursts in flood, zero to disable");
83 torture_param(int, cbflood_n_per_burst, 20000,
84 "# callbacks per burst in flood");
85 torture_param(int, extendables, RCUTORTURE_MAX_EXTEND,
86 "Extend readers by disabling bh (1), irqs (2), or preempt (4)");
87 torture_param(int, fqs_duration, 0,
88 "Duration of fqs bursts (us), 0 to disable");
89 torture_param(int, fqs_holdoff, 0, "Holdoff time within fqs bursts (us)");
90 torture_param(int, fqs_stutter, 3, "Wait time between fqs bursts (s)");
91 torture_param(bool, gp_cond, false, "Use conditional/async GP wait primitives");
92 torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
93 torture_param(bool, gp_normal, false,
94 "Use normal (non-expedited) GP wait primitives");
95 torture_param(bool, gp_sync, false, "Use synchronous GP wait primitives");
96 torture_param(int, irqreader, 1, "Allow RCU readers from irq handlers");
97 torture_param(int, n_barrier_cbs, 0,
98 "# of callbacks/kthreads for barrier testing");
99 torture_param(int, nfakewriters, 4, "Number of RCU fake writer threads");
100 torture_param(int, nreaders, -1, "Number of RCU reader threads");
101 torture_param(int, object_debug, 0,
102 "Enable debug-object double call_rcu() testing");
103 torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
104 torture_param(int, onoff_interval, 0,
105 "Time between CPU hotplugs (jiffies), 0=disable");
106 torture_param(int, shuffle_interval, 3, "Number of seconds between shuffles");
107 torture_param(int, shutdown_secs, 0, "Shutdown time (s), <= zero to disable.");
108 torture_param(int, stall_cpu, 0, "Stall duration (s), zero to disable.");
109 torture_param(int, stall_cpu_holdoff, 10,
110 "Time to wait before starting stall (s).");
111 torture_param(int, stall_cpu_irqsoff, 0, "Disable interrupts while stalling.");
112 torture_param(int, stat_interval, 60,
113 "Number of seconds between stats printk()s");
114 torture_param(int, stutter, 5, "Number of seconds to run/halt test");
115 torture_param(int, test_boost, 1, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
116 torture_param(int, test_boost_duration, 4,
117 "Duration of each boost test, seconds.");
118 torture_param(int, test_boost_interval, 7,
119 "Interval between boost tests, seconds.");
120 torture_param(bool, test_no_idle_hz, true,
121 "Test support for tickless idle CPUs");
122 torture_param(int, verbose, 1,
123 "Enable verbose debugging printk()s");
125 static char *torture_type = "rcu";
126 module_param(torture_type, charp, 0444);
127 MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, ...)");
129 static int nrealreaders;
130 static int ncbflooders;
131 static struct task_struct *writer_task;
132 static struct task_struct **fakewriter_tasks;
133 static struct task_struct **reader_tasks;
134 static struct task_struct *stats_task;
135 static struct task_struct **cbflood_task;
136 static struct task_struct *fqs_task;
137 static struct task_struct *boost_tasks[NR_CPUS];
138 static struct task_struct *stall_task;
139 static struct task_struct **barrier_cbs_tasks;
140 static struct task_struct *barrier_task;
142 #define RCU_TORTURE_PIPE_LEN 10
145 struct rcu_head rtort_rcu;
146 int rtort_pipe_count;
147 struct list_head rtort_free;
151 static LIST_HEAD(rcu_torture_freelist);
152 static struct rcu_torture __rcu *rcu_torture_current;
153 static unsigned long rcu_torture_current_version;
154 static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
155 static DEFINE_SPINLOCK(rcu_torture_lock);
156 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count);
157 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch);
158 static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
159 static atomic_t n_rcu_torture_alloc;
160 static atomic_t n_rcu_torture_alloc_fail;
161 static atomic_t n_rcu_torture_free;
162 static atomic_t n_rcu_torture_mberror;
163 static atomic_t n_rcu_torture_error;
164 static long n_rcu_torture_barrier_error;
165 static long n_rcu_torture_boost_ktrerror;
166 static long n_rcu_torture_boost_rterror;
167 static long n_rcu_torture_boost_failure;
168 static long n_rcu_torture_boosts;
169 static atomic_long_t n_rcu_torture_timers;
170 static long n_barrier_attempts;
171 static long n_barrier_successes;
172 static atomic_long_t n_cbfloods;
173 static struct list_head rcu_torture_removed;
175 static int rcu_torture_writer_state;
176 #define RTWS_FIXED_DELAY 0
178 #define RTWS_REPLACE 2
179 #define RTWS_DEF_FREE 3
180 #define RTWS_EXP_SYNC 4
181 #define RTWS_COND_GET 5
182 #define RTWS_COND_SYNC 6
184 #define RTWS_STUTTER 8
185 #define RTWS_STOPPING 9
186 static const char * const rcu_torture_writer_state_names[] = {
199 static const char *rcu_torture_writer_state_getname(void)
201 unsigned int i = READ_ONCE(rcu_torture_writer_state);
203 if (i >= ARRAY_SIZE(rcu_torture_writer_state_names))
205 return rcu_torture_writer_state_names[i];
208 #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
209 #define rcu_can_boost() 1
210 #else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
211 #define rcu_can_boost() 0
212 #endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
214 #ifdef CONFIG_RCU_TRACE
215 static u64 notrace rcu_trace_clock_local(void)
217 u64 ts = trace_clock_local();
219 (void)do_div(ts, NSEC_PER_USEC);
222 #else /* #ifdef CONFIG_RCU_TRACE */
223 static u64 notrace rcu_trace_clock_local(void)
227 #endif /* #else #ifdef CONFIG_RCU_TRACE */
229 static unsigned long boost_starttime; /* jiffies of next boost test start. */
230 static DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
231 /* and boost task create/destroy. */
232 static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
233 static bool barrier_phase; /* Test phase. */
234 static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
235 static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
236 static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
239 * Allocate an element from the rcu_tortures pool.
241 static struct rcu_torture *
242 rcu_torture_alloc(void)
246 spin_lock_bh(&rcu_torture_lock);
247 if (list_empty(&rcu_torture_freelist)) {
248 atomic_inc(&n_rcu_torture_alloc_fail);
249 spin_unlock_bh(&rcu_torture_lock);
252 atomic_inc(&n_rcu_torture_alloc);
253 p = rcu_torture_freelist.next;
255 spin_unlock_bh(&rcu_torture_lock);
256 return container_of(p, struct rcu_torture, rtort_free);
260 * Free an element to the rcu_tortures pool.
263 rcu_torture_free(struct rcu_torture *p)
265 atomic_inc(&n_rcu_torture_free);
266 spin_lock_bh(&rcu_torture_lock);
267 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
268 spin_unlock_bh(&rcu_torture_lock);
272 * Operations vector for selecting different types of tests.
275 struct rcu_torture_ops {
278 void (*cleanup)(void);
279 int (*readlock)(void);
280 void (*read_delay)(struct torture_random_state *rrsp);
281 void (*readunlock)(int idx);
282 unsigned long (*get_gp_seq)(void);
283 unsigned long (*gp_diff)(unsigned long new, unsigned long old);
284 void (*deferred_free)(struct rcu_torture *p);
286 void (*exp_sync)(void);
287 unsigned long (*get_state)(void);
288 void (*cond_sync)(unsigned long oldstate);
289 call_rcu_func_t call;
290 void (*cb_barrier)(void);
296 int ext_irq_conflict;
300 static struct rcu_torture_ops *cur_ops;
303 * Definitions for rcu torture testing.
306 static int rcu_torture_read_lock(void) __acquires(RCU)
312 static void rcu_read_delay(struct torture_random_state *rrsp)
314 unsigned long started;
315 unsigned long completed;
316 const unsigned long shortdelay_us = 200;
317 const unsigned long longdelay_ms = 50;
318 unsigned long long ts;
320 /* We want a short delay sometimes to make a reader delay the grace
321 * period, and we want a long delay occasionally to trigger
322 * force_quiescent_state. */
324 if (!(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms))) {
325 started = cur_ops->get_gp_seq();
326 ts = rcu_trace_clock_local();
327 mdelay(longdelay_ms);
328 completed = cur_ops->get_gp_seq();
329 do_trace_rcu_torture_read(cur_ops->name, NULL, ts,
332 if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
333 udelay(shortdelay_us);
334 if (!preempt_count() &&
335 !(torture_random(rrsp) % (nrealreaders * 500)))
336 torture_preempt_schedule(); /* QS only if preemptible. */
339 static void rcu_torture_read_unlock(int idx) __releases(RCU)
345 * Update callback in the pipe. This should be invoked after a grace period.
348 rcu_torture_pipe_update_one(struct rcu_torture *rp)
352 i = rp->rtort_pipe_count;
353 if (i > RCU_TORTURE_PIPE_LEN)
354 i = RCU_TORTURE_PIPE_LEN;
355 atomic_inc(&rcu_torture_wcount[i]);
356 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
357 rp->rtort_mbtest = 0;
364 * Update all callbacks in the pipe. Suitable for synchronous grace-period
368 rcu_torture_pipe_update(struct rcu_torture *old_rp)
370 struct rcu_torture *rp;
371 struct rcu_torture *rp1;
374 list_add(&old_rp->rtort_free, &rcu_torture_removed);
375 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
376 if (rcu_torture_pipe_update_one(rp)) {
377 list_del(&rp->rtort_free);
378 rcu_torture_free(rp);
384 rcu_torture_cb(struct rcu_head *p)
386 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
388 if (torture_must_stop_irq()) {
389 /* Test is ending, just drop callbacks on the floor. */
390 /* The next initialization will pick up the pieces. */
393 if (rcu_torture_pipe_update_one(rp))
394 rcu_torture_free(rp);
396 cur_ops->deferred_free(rp);
399 static unsigned long rcu_no_completed(void)
404 static void rcu_torture_deferred_free(struct rcu_torture *p)
406 call_rcu(&p->rtort_rcu, rcu_torture_cb);
409 static void rcu_sync_torture_init(void)
411 INIT_LIST_HEAD(&rcu_torture_removed);
414 static struct rcu_torture_ops rcu_ops = {
416 .init = rcu_sync_torture_init,
417 .readlock = rcu_torture_read_lock,
418 .read_delay = rcu_read_delay,
419 .readunlock = rcu_torture_read_unlock,
420 .get_gp_seq = rcu_get_gp_seq,
421 .gp_diff = rcu_seq_diff,
422 .deferred_free = rcu_torture_deferred_free,
423 .sync = synchronize_rcu,
424 .exp_sync = synchronize_rcu_expedited,
425 .get_state = get_state_synchronize_rcu,
426 .cond_sync = cond_synchronize_rcu,
428 .cb_barrier = rcu_barrier,
429 .fqs = rcu_force_quiescent_state,
432 .can_boost = rcu_can_boost(),
437 * Definitions for rcu_bh torture testing.
440 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
446 static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
448 rcu_read_unlock_bh();
451 static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
453 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
456 static struct rcu_torture_ops rcu_bh_ops = {
457 .ttype = RCU_BH_FLAVOR,
458 .init = rcu_sync_torture_init,
459 .readlock = rcu_bh_torture_read_lock,
460 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
461 .readunlock = rcu_bh_torture_read_unlock,
462 .get_gp_seq = rcu_bh_get_gp_seq,
463 .gp_diff = rcu_seq_diff,
464 .deferred_free = rcu_bh_torture_deferred_free,
465 .sync = synchronize_rcu_bh,
466 .exp_sync = synchronize_rcu_bh_expedited,
468 .cb_barrier = rcu_barrier_bh,
469 .fqs = rcu_bh_force_quiescent_state,
472 .extendables = (RCUTORTURE_RDR_BH | RCUTORTURE_RDR_IRQ),
473 .ext_irq_conflict = RCUTORTURE_RDR_RCU,
478 * Don't even think about trying any of these in real life!!!
479 * The names includes "busted", and they really means it!
480 * The only purpose of these functions is to provide a buggy RCU
481 * implementation to make sure that rcutorture correctly emits
482 * buggy-RCU error messages.
484 static void rcu_busted_torture_deferred_free(struct rcu_torture *p)
486 /* This is a deliberate bug for testing purposes only! */
487 rcu_torture_cb(&p->rtort_rcu);
490 static void synchronize_rcu_busted(void)
492 /* This is a deliberate bug for testing purposes only! */
496 call_rcu_busted(struct rcu_head *head, rcu_callback_t func)
498 /* This is a deliberate bug for testing purposes only! */
502 static struct rcu_torture_ops rcu_busted_ops = {
503 .ttype = INVALID_RCU_FLAVOR,
504 .init = rcu_sync_torture_init,
505 .readlock = rcu_torture_read_lock,
506 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
507 .readunlock = rcu_torture_read_unlock,
508 .get_gp_seq = rcu_no_completed,
509 .deferred_free = rcu_busted_torture_deferred_free,
510 .sync = synchronize_rcu_busted,
511 .exp_sync = synchronize_rcu_busted,
512 .call = call_rcu_busted,
521 * Definitions for srcu torture testing.
524 DEFINE_STATIC_SRCU(srcu_ctl);
525 static struct srcu_struct srcu_ctld;
526 static struct srcu_struct *srcu_ctlp = &srcu_ctl;
528 static int srcu_torture_read_lock(void) __acquires(srcu_ctlp)
530 return srcu_read_lock(srcu_ctlp);
533 static void srcu_read_delay(struct torture_random_state *rrsp)
536 const long uspertick = 1000000 / HZ;
537 const long longdelay = 10;
539 /* We want there to be long-running readers, but not all the time. */
541 delay = torture_random(rrsp) %
542 (nrealreaders * 2 * longdelay * uspertick);
543 if (!delay && in_task())
544 schedule_timeout_interruptible(longdelay);
546 rcu_read_delay(rrsp);
549 static void srcu_torture_read_unlock(int idx) __releases(srcu_ctlp)
551 srcu_read_unlock(srcu_ctlp, idx);
554 static unsigned long srcu_torture_completed(void)
556 return srcu_batches_completed(srcu_ctlp);
559 static void srcu_torture_deferred_free(struct rcu_torture *rp)
561 call_srcu(srcu_ctlp, &rp->rtort_rcu, rcu_torture_cb);
564 static void srcu_torture_synchronize(void)
566 synchronize_srcu(srcu_ctlp);
569 static void srcu_torture_call(struct rcu_head *head,
572 call_srcu(srcu_ctlp, head, func);
575 static void srcu_torture_barrier(void)
577 srcu_barrier(srcu_ctlp);
580 static void srcu_torture_stats(void)
582 srcu_torture_stats_print(srcu_ctlp, torture_type, TORTURE_FLAG);
585 static void srcu_torture_synchronize_expedited(void)
587 synchronize_srcu_expedited(srcu_ctlp);
590 static struct rcu_torture_ops srcu_ops = {
591 .ttype = SRCU_FLAVOR,
592 .init = rcu_sync_torture_init,
593 .readlock = srcu_torture_read_lock,
594 .read_delay = srcu_read_delay,
595 .readunlock = srcu_torture_read_unlock,
596 .get_gp_seq = srcu_torture_completed,
597 .deferred_free = srcu_torture_deferred_free,
598 .sync = srcu_torture_synchronize,
599 .exp_sync = srcu_torture_synchronize_expedited,
600 .call = srcu_torture_call,
601 .cb_barrier = srcu_torture_barrier,
602 .stats = srcu_torture_stats,
607 static void srcu_torture_init(void)
609 rcu_sync_torture_init();
610 WARN_ON(init_srcu_struct(&srcu_ctld));
611 srcu_ctlp = &srcu_ctld;
614 static void srcu_torture_cleanup(void)
616 static DEFINE_TORTURE_RANDOM(rand);
618 if (torture_random(&rand) & 0x800)
619 cleanup_srcu_struct(&srcu_ctld);
621 cleanup_srcu_struct_quiesced(&srcu_ctld);
622 srcu_ctlp = &srcu_ctl; /* In case of a later rcutorture run. */
625 /* As above, but dynamically allocated. */
626 static struct rcu_torture_ops srcud_ops = {
627 .ttype = SRCU_FLAVOR,
628 .init = srcu_torture_init,
629 .cleanup = srcu_torture_cleanup,
630 .readlock = srcu_torture_read_lock,
631 .read_delay = srcu_read_delay,
632 .readunlock = srcu_torture_read_unlock,
633 .get_gp_seq = srcu_torture_completed,
634 .deferred_free = srcu_torture_deferred_free,
635 .sync = srcu_torture_synchronize,
636 .exp_sync = srcu_torture_synchronize_expedited,
637 .call = srcu_torture_call,
638 .cb_barrier = srcu_torture_barrier,
639 .stats = srcu_torture_stats,
644 /* As above, but broken due to inappropriate reader extension. */
645 static struct rcu_torture_ops busted_srcud_ops = {
646 .ttype = SRCU_FLAVOR,
647 .init = srcu_torture_init,
648 .cleanup = srcu_torture_cleanup,
649 .readlock = srcu_torture_read_lock,
650 .read_delay = rcu_read_delay,
651 .readunlock = srcu_torture_read_unlock,
652 .get_gp_seq = srcu_torture_completed,
653 .deferred_free = srcu_torture_deferred_free,
654 .sync = srcu_torture_synchronize,
655 .exp_sync = srcu_torture_synchronize_expedited,
656 .call = srcu_torture_call,
657 .cb_barrier = srcu_torture_barrier,
658 .stats = srcu_torture_stats,
660 .extendables = RCUTORTURE_MAX_EXTEND,
661 .name = "busted_srcud"
665 * Definitions for sched torture testing.
668 static int sched_torture_read_lock(void)
674 static void sched_torture_read_unlock(int idx)
679 static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
681 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
684 static struct rcu_torture_ops sched_ops = {
685 .ttype = RCU_SCHED_FLAVOR,
686 .init = rcu_sync_torture_init,
687 .readlock = sched_torture_read_lock,
688 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
689 .readunlock = sched_torture_read_unlock,
690 .get_gp_seq = rcu_sched_get_gp_seq,
691 .gp_diff = rcu_seq_diff,
692 .deferred_free = rcu_sched_torture_deferred_free,
693 .sync = synchronize_sched,
694 .exp_sync = synchronize_sched_expedited,
695 .get_state = get_state_synchronize_sched,
696 .cond_sync = cond_synchronize_sched,
697 .call = call_rcu_sched,
698 .cb_barrier = rcu_barrier_sched,
699 .fqs = rcu_sched_force_quiescent_state,
702 .extendables = RCUTORTURE_MAX_EXTEND,
707 * Definitions for RCU-tasks torture testing.
710 static int tasks_torture_read_lock(void)
715 static void tasks_torture_read_unlock(int idx)
719 static void rcu_tasks_torture_deferred_free(struct rcu_torture *p)
721 call_rcu_tasks(&p->rtort_rcu, rcu_torture_cb);
724 static struct rcu_torture_ops tasks_ops = {
725 .ttype = RCU_TASKS_FLAVOR,
726 .init = rcu_sync_torture_init,
727 .readlock = tasks_torture_read_lock,
728 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
729 .readunlock = tasks_torture_read_unlock,
730 .get_gp_seq = rcu_no_completed,
731 .deferred_free = rcu_tasks_torture_deferred_free,
732 .sync = synchronize_rcu_tasks,
733 .exp_sync = synchronize_rcu_tasks,
734 .call = call_rcu_tasks,
735 .cb_barrier = rcu_barrier_tasks,
742 static unsigned long rcutorture_seq_diff(unsigned long new, unsigned long old)
744 if (!cur_ops->gp_diff)
746 return cur_ops->gp_diff(new, old);
749 static bool __maybe_unused torturing_tasks(void)
751 return cur_ops == &tasks_ops;
755 * RCU torture priority-boost testing. Runs one real-time thread per
756 * CPU for moderate bursts, repeatedly registering RCU callbacks and
757 * spinning waiting for them to be invoked. If a given callback takes
758 * too long to be invoked, we assume that priority inversion has occurred.
761 struct rcu_boost_inflight {
766 static void rcu_torture_boost_cb(struct rcu_head *head)
768 struct rcu_boost_inflight *rbip =
769 container_of(head, struct rcu_boost_inflight, rcu);
771 /* Ensure RCU-core accesses precede clearing ->inflight */
772 smp_store_release(&rbip->inflight, 0);
775 static int rcu_torture_boost(void *arg)
777 unsigned long call_rcu_time;
778 unsigned long endtime;
779 unsigned long oldstarttime;
780 struct rcu_boost_inflight rbi = { .inflight = 0 };
781 struct sched_param sp;
783 VERBOSE_TOROUT_STRING("rcu_torture_boost started");
785 /* Set real-time priority. */
786 sp.sched_priority = 1;
787 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
788 VERBOSE_TOROUT_STRING("rcu_torture_boost RT prio failed!");
789 n_rcu_torture_boost_rterror++;
792 init_rcu_head_on_stack(&rbi.rcu);
793 /* Each pass through the following loop does one boost-test cycle. */
795 /* Wait for the next test interval. */
796 oldstarttime = boost_starttime;
797 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
798 schedule_timeout_interruptible(oldstarttime - jiffies);
799 stutter_wait("rcu_torture_boost");
800 if (torture_must_stop())
804 /* Do one boost-test interval. */
805 endtime = oldstarttime + test_boost_duration * HZ;
806 call_rcu_time = jiffies;
807 while (ULONG_CMP_LT(jiffies, endtime)) {
808 /* If we don't have a callback in flight, post one. */
809 if (!smp_load_acquire(&rbi.inflight)) {
810 /* RCU core before ->inflight = 1. */
811 smp_store_release(&rbi.inflight, 1);
812 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
813 if (jiffies - call_rcu_time >
814 test_boost_duration * HZ - HZ / 2) {
815 VERBOSE_TOROUT_STRING("rcu_torture_boost boosting failed");
816 n_rcu_torture_boost_failure++;
818 call_rcu_time = jiffies;
820 stutter_wait("rcu_torture_boost");
821 if (torture_must_stop())
826 * Set the start time of the next test interval.
827 * Yes, this is vulnerable to long delays, but such
828 * delays simply cause a false negative for the next
829 * interval. Besides, we are running at RT priority,
830 * so delays should be relatively rare.
832 while (oldstarttime == boost_starttime &&
833 !kthread_should_stop()) {
834 if (mutex_trylock(&boost_mutex)) {
835 boost_starttime = jiffies +
836 test_boost_interval * HZ;
837 n_rcu_torture_boosts++;
838 mutex_unlock(&boost_mutex);
841 schedule_timeout_uninterruptible(1);
844 /* Go do the stutter. */
845 checkwait: stutter_wait("rcu_torture_boost");
846 } while (!torture_must_stop());
848 /* Clean up and exit. */
849 while (!kthread_should_stop() || smp_load_acquire(&rbi.inflight)) {
850 torture_shutdown_absorb("rcu_torture_boost");
851 schedule_timeout_uninterruptible(1);
853 destroy_rcu_head_on_stack(&rbi.rcu);
854 torture_kthread_stopping("rcu_torture_boost");
858 static void rcu_torture_cbflood_cb(struct rcu_head *rhp)
863 * RCU torture callback-flood kthread. Repeatedly induces bursts of calls
864 * to call_rcu() or analogous, increasing the probability of occurrence
865 * of callback-overflow corner cases.
868 rcu_torture_cbflood(void *arg)
873 struct rcu_head *rhp;
875 if (cbflood_n_per_burst > 0 &&
876 cbflood_inter_holdoff > 0 &&
877 cbflood_intra_holdoff > 0 &&
879 cur_ops->cb_barrier) {
880 rhp = vmalloc(array3_size(cbflood_n_burst,
886 VERBOSE_TOROUT_STRING("rcu_torture_cbflood disabled: Bad args or OOM");
889 VERBOSE_TOROUT_STRING("rcu_torture_cbflood task started");
891 schedule_timeout_interruptible(cbflood_inter_holdoff);
892 atomic_long_inc(&n_cbfloods);
893 WARN_ON(signal_pending(current));
894 for (i = 0; i < cbflood_n_burst; i++) {
895 for (j = 0; j < cbflood_n_per_burst; j++) {
896 cur_ops->call(&rhp[i * cbflood_n_per_burst + j],
897 rcu_torture_cbflood_cb);
899 schedule_timeout_interruptible(cbflood_intra_holdoff);
900 WARN_ON(signal_pending(current));
902 cur_ops->cb_barrier();
903 stutter_wait("rcu_torture_cbflood");
904 } while (!torture_must_stop());
907 torture_kthread_stopping("rcu_torture_cbflood");
912 * RCU torture force-quiescent-state kthread. Repeatedly induces
913 * bursts of calls to force_quiescent_state(), increasing the probability
914 * of occurrence of some important types of race conditions.
917 rcu_torture_fqs(void *arg)
919 unsigned long fqs_resume_time;
920 int fqs_burst_remaining;
922 VERBOSE_TOROUT_STRING("rcu_torture_fqs task started");
924 fqs_resume_time = jiffies + fqs_stutter * HZ;
925 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
926 !kthread_should_stop()) {
927 schedule_timeout_interruptible(1);
929 fqs_burst_remaining = fqs_duration;
930 while (fqs_burst_remaining > 0 &&
931 !kthread_should_stop()) {
934 fqs_burst_remaining -= fqs_holdoff;
936 stutter_wait("rcu_torture_fqs");
937 } while (!torture_must_stop());
938 torture_kthread_stopping("rcu_torture_fqs");
943 * RCU torture writer kthread. Repeatedly substitutes a new structure
944 * for that pointed to by rcu_torture_current, freeing the old structure
945 * after a series of grace periods (the "pipeline").
948 rcu_torture_writer(void *arg)
950 bool can_expedite = !rcu_gp_is_expedited() && !rcu_gp_is_normal();
952 unsigned long gp_snap;
953 bool gp_cond1 = gp_cond, gp_exp1 = gp_exp, gp_normal1 = gp_normal;
954 bool gp_sync1 = gp_sync;
956 struct rcu_torture *rp;
957 struct rcu_torture *old_rp;
958 static DEFINE_TORTURE_RANDOM(rand);
959 int synctype[] = { RTWS_DEF_FREE, RTWS_EXP_SYNC,
960 RTWS_COND_GET, RTWS_SYNC };
963 VERBOSE_TOROUT_STRING("rcu_torture_writer task started");
965 pr_alert("%s" TORTURE_FLAG
966 " GP expediting controlled from boot/sysfs for %s.\n",
967 torture_type, cur_ops->name);
969 /* Initialize synctype[] array. If none set, take default. */
970 if (!gp_cond1 && !gp_exp1 && !gp_normal1 && !gp_sync1)
971 gp_cond1 = gp_exp1 = gp_normal1 = gp_sync1 = true;
972 if (gp_cond1 && cur_ops->get_state && cur_ops->cond_sync) {
973 synctype[nsynctypes++] = RTWS_COND_GET;
974 pr_info("%s: Testing conditional GPs.\n", __func__);
975 } else if (gp_cond && (!cur_ops->get_state || !cur_ops->cond_sync)) {
976 pr_alert("%s: gp_cond without primitives.\n", __func__);
978 if (gp_exp1 && cur_ops->exp_sync) {
979 synctype[nsynctypes++] = RTWS_EXP_SYNC;
980 pr_info("%s: Testing expedited GPs.\n", __func__);
981 } else if (gp_exp && !cur_ops->exp_sync) {
982 pr_alert("%s: gp_exp without primitives.\n", __func__);
984 if (gp_normal1 && cur_ops->deferred_free) {
985 synctype[nsynctypes++] = RTWS_DEF_FREE;
986 pr_info("%s: Testing asynchronous GPs.\n", __func__);
987 } else if (gp_normal && !cur_ops->deferred_free) {
988 pr_alert("%s: gp_normal without primitives.\n", __func__);
990 if (gp_sync1 && cur_ops->sync) {
991 synctype[nsynctypes++] = RTWS_SYNC;
992 pr_info("%s: Testing normal GPs.\n", __func__);
993 } else if (gp_sync && !cur_ops->sync) {
994 pr_alert("%s: gp_sync without primitives.\n", __func__);
996 if (WARN_ONCE(nsynctypes == 0,
997 "rcu_torture_writer: No update-side primitives.\n")) {
999 * No updates primitives, so don't try updating.
1000 * The resulting test won't be testing much, hence the
1001 * above WARN_ONCE().
1003 rcu_torture_writer_state = RTWS_STOPPING;
1004 torture_kthread_stopping("rcu_torture_writer");
1008 rcu_torture_writer_state = RTWS_FIXED_DELAY;
1009 schedule_timeout_uninterruptible(1);
1010 rp = rcu_torture_alloc();
1013 rp->rtort_pipe_count = 0;
1014 rcu_torture_writer_state = RTWS_DELAY;
1015 udelay(torture_random(&rand) & 0x3ff);
1016 rcu_torture_writer_state = RTWS_REPLACE;
1017 old_rp = rcu_dereference_check(rcu_torture_current,
1018 current == writer_task);
1019 rp->rtort_mbtest = 1;
1020 rcu_assign_pointer(rcu_torture_current, rp);
1021 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
1023 i = old_rp->rtort_pipe_count;
1024 if (i > RCU_TORTURE_PIPE_LEN)
1025 i = RCU_TORTURE_PIPE_LEN;
1026 atomic_inc(&rcu_torture_wcount[i]);
1027 old_rp->rtort_pipe_count++;
1028 switch (synctype[torture_random(&rand) % nsynctypes]) {
1030 rcu_torture_writer_state = RTWS_DEF_FREE;
1031 cur_ops->deferred_free(old_rp);
1034 rcu_torture_writer_state = RTWS_EXP_SYNC;
1035 cur_ops->exp_sync();
1036 rcu_torture_pipe_update(old_rp);
1039 rcu_torture_writer_state = RTWS_COND_GET;
1040 gp_snap = cur_ops->get_state();
1041 i = torture_random(&rand) % 16;
1043 schedule_timeout_interruptible(i);
1044 udelay(torture_random(&rand) % 1000);
1045 rcu_torture_writer_state = RTWS_COND_SYNC;
1046 cur_ops->cond_sync(gp_snap);
1047 rcu_torture_pipe_update(old_rp);
1050 rcu_torture_writer_state = RTWS_SYNC;
1052 rcu_torture_pipe_update(old_rp);
1059 rcu_torture_current_version++;
1060 /* Cycle through nesting levels of rcu_expedite_gp() calls. */
1062 !(torture_random(&rand) & 0xff & (!!expediting - 1))) {
1063 WARN_ON_ONCE(expediting == 0 && rcu_gp_is_expedited());
1064 if (expediting >= 0)
1067 rcu_unexpedite_gp();
1068 if (++expediting > 3)
1069 expediting = -expediting;
1070 } else if (!can_expedite) { /* Disabled during boot, recheck. */
1071 can_expedite = !rcu_gp_is_expedited() &&
1072 !rcu_gp_is_normal();
1074 rcu_torture_writer_state = RTWS_STUTTER;
1075 stutter_wait("rcu_torture_writer");
1076 } while (!torture_must_stop());
1077 /* Reset expediting back to unexpedited. */
1079 expediting = -expediting;
1080 while (can_expedite && expediting++ < 0)
1081 rcu_unexpedite_gp();
1082 WARN_ON_ONCE(can_expedite && rcu_gp_is_expedited());
1084 pr_alert("%s" TORTURE_FLAG
1085 " Dynamic grace-period expediting was disabled.\n",
1087 rcu_torture_writer_state = RTWS_STOPPING;
1088 torture_kthread_stopping("rcu_torture_writer");
1093 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
1094 * delay between calls.
1097 rcu_torture_fakewriter(void *arg)
1099 DEFINE_TORTURE_RANDOM(rand);
1101 VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
1102 set_user_nice(current, MAX_NICE);
1105 schedule_timeout_uninterruptible(1 + torture_random(&rand)%10);
1106 udelay(torture_random(&rand) & 0x3ff);
1107 if (cur_ops->cb_barrier != NULL &&
1108 torture_random(&rand) % (nfakewriters * 8) == 0) {
1109 cur_ops->cb_barrier();
1110 } else if (gp_normal == gp_exp) {
1111 if (cur_ops->sync && torture_random(&rand) & 0x80)
1113 else if (cur_ops->exp_sync)
1114 cur_ops->exp_sync();
1115 } else if (gp_normal && cur_ops->sync) {
1117 } else if (cur_ops->exp_sync) {
1118 cur_ops->exp_sync();
1120 stutter_wait("rcu_torture_fakewriter");
1121 } while (!torture_must_stop());
1123 torture_kthread_stopping("rcu_torture_fakewriter");
1127 static void rcu_torture_timer_cb(struct rcu_head *rhp)
1133 * Do one extension of an RCU read-side critical section using the
1134 * current reader state in readstate (set to zero for initial entry
1135 * to extended critical section), set the new state as specified by
1136 * newstate (set to zero for final exit from extended critical section),
1137 * and random-number-generator state in trsp. If this is neither the
1138 * beginning or end of the critical section and if there was actually a
1139 * change, do a ->read_delay().
1141 static void rcutorture_one_extend(int *readstate, int newstate,
1142 struct torture_random_state *trsp)
1145 int idxold = *readstate;
1146 int statesnew = ~*readstate & newstate;
1147 int statesold = *readstate & ~newstate;
1149 WARN_ON_ONCE(idxold < 0);
1150 WARN_ON_ONCE((idxold >> RCUTORTURE_RDR_SHIFT) > 1);
1152 /* First, put new protection in place to avoid critical-section gap. */
1153 if (statesnew & RCUTORTURE_RDR_BH)
1155 if (statesnew & RCUTORTURE_RDR_IRQ)
1156 local_irq_disable();
1157 if (statesnew & RCUTORTURE_RDR_PREEMPT)
1159 if (statesnew & RCUTORTURE_RDR_RCU)
1160 idxnew = cur_ops->readlock() << RCUTORTURE_RDR_SHIFT;
1162 /* Next, remove old protection, irq first due to bh conflict. */
1163 if (statesold & RCUTORTURE_RDR_IRQ)
1165 if (statesold & RCUTORTURE_RDR_BH)
1167 if (statesold & RCUTORTURE_RDR_PREEMPT)
1169 if (statesold & RCUTORTURE_RDR_RCU)
1170 cur_ops->readunlock(idxold >> RCUTORTURE_RDR_SHIFT);
1172 /* Delay if neither beginning nor end and there was a change. */
1173 if ((statesnew || statesold) && *readstate && newstate)
1174 cur_ops->read_delay(trsp);
1176 /* Update the reader state. */
1178 idxnew = idxold & ~RCUTORTURE_RDR_MASK;
1179 WARN_ON_ONCE(idxnew < 0);
1180 WARN_ON_ONCE((idxnew >> RCUTORTURE_RDR_SHIFT) > 1);
1181 *readstate = idxnew | newstate;
1182 WARN_ON_ONCE((*readstate >> RCUTORTURE_RDR_SHIFT) < 0);
1183 WARN_ON_ONCE((*readstate >> RCUTORTURE_RDR_SHIFT) > 1);
1186 /* Return the biggest extendables mask given current RCU and boot parameters. */
1187 static int rcutorture_extend_mask_max(void)
1191 WARN_ON_ONCE(extendables & ~RCUTORTURE_MAX_EXTEND);
1192 mask = extendables & RCUTORTURE_MAX_EXTEND & cur_ops->extendables;
1193 mask = mask | RCUTORTURE_RDR_RCU;
1197 /* Return a random protection state mask, but with at least one bit set. */
1199 rcutorture_extend_mask(int oldmask, struct torture_random_state *trsp)
1201 int mask = rcutorture_extend_mask_max();
1202 unsigned long randmask1 = torture_random(trsp) >> 8;
1203 unsigned long randmask2 = randmask1 >> 1;
1205 WARN_ON_ONCE(mask >> RCUTORTURE_RDR_SHIFT);
1206 /* Half the time lots of bits, half the time only one bit. */
1207 if (randmask1 & 0x1)
1208 mask = mask & randmask2;
1210 mask = mask & (1 << (randmask2 % RCUTORTURE_RDR_NBITS));
1211 if ((mask & RCUTORTURE_RDR_IRQ) &&
1212 !(mask & RCUTORTURE_RDR_BH) &&
1213 (oldmask & RCUTORTURE_RDR_BH))
1214 mask |= RCUTORTURE_RDR_BH; /* Can't enable bh w/irq disabled. */
1215 if ((mask & RCUTORTURE_RDR_IRQ) &&
1216 !(mask & cur_ops->ext_irq_conflict) &&
1217 (oldmask & cur_ops->ext_irq_conflict))
1218 mask |= cur_ops->ext_irq_conflict; /* Or if readers object. */
1219 return mask ?: RCUTORTURE_RDR_RCU;
1223 * Do a randomly selected number of extensions of an existing RCU read-side
1226 static void rcutorture_loop_extend(int *readstate,
1227 struct torture_random_state *trsp)
1230 int mask = rcutorture_extend_mask_max();
1232 WARN_ON_ONCE(!*readstate); /* -Existing- RCU read-side critsect! */
1233 if (!((mask - 1) & mask))
1234 return; /* Current RCU flavor not extendable. */
1235 i = (torture_random(trsp) >> 3) & RCUTORTURE_RDR_MAX_LOOPS;
1237 mask = rcutorture_extend_mask(*readstate, trsp);
1238 rcutorture_one_extend(readstate, mask, trsp);
1243 * Do one read-side critical section, returning false if there was
1244 * no data to read. Can be invoked both from process context and
1245 * from a timer handler.
1247 static bool rcu_torture_one_read(struct torture_random_state *trsp)
1249 unsigned long started;
1250 unsigned long completed;
1252 struct rcu_torture *p;
1255 unsigned long long ts;
1257 newstate = rcutorture_extend_mask(readstate, trsp);
1258 rcutorture_one_extend(&readstate, newstate, trsp);
1259 started = cur_ops->get_gp_seq();
1260 ts = rcu_trace_clock_local();
1261 p = rcu_dereference_check(rcu_torture_current,
1262 rcu_read_lock_bh_held() ||
1263 rcu_read_lock_sched_held() ||
1264 srcu_read_lock_held(srcu_ctlp) ||
1267 /* Wait for rcu_torture_writer to get underway */
1268 rcutorture_one_extend(&readstate, 0, trsp);
1271 if (p->rtort_mbtest == 0)
1272 atomic_inc(&n_rcu_torture_mberror);
1273 rcutorture_loop_extend(&readstate, trsp);
1275 pipe_count = p->rtort_pipe_count;
1276 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1277 /* Should not happen, but... */
1278 pipe_count = RCU_TORTURE_PIPE_LEN;
1280 completed = cur_ops->get_gp_seq();
1281 if (pipe_count > 1) {
1282 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1283 ts, started, completed);
1284 rcu_ftrace_dump(DUMP_ALL);
1286 __this_cpu_inc(rcu_torture_count[pipe_count]);
1287 completed = rcutorture_seq_diff(completed, started);
1288 if (completed > RCU_TORTURE_PIPE_LEN) {
1289 /* Should not happen, but... */
1290 completed = RCU_TORTURE_PIPE_LEN;
1292 __this_cpu_inc(rcu_torture_batch[completed]);
1294 rcutorture_one_extend(&readstate, 0, trsp);
1295 WARN_ON_ONCE(readstate & RCUTORTURE_RDR_MASK);
1299 static DEFINE_TORTURE_RANDOM_PERCPU(rcu_torture_timer_rand);
1302 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
1303 * incrementing the corresponding element of the pipeline array. The
1304 * counter in the element should never be greater than 1, otherwise, the
1305 * RCU implementation is broken.
1307 static void rcu_torture_timer(struct timer_list *unused)
1309 atomic_long_inc(&n_rcu_torture_timers);
1310 (void)rcu_torture_one_read(this_cpu_ptr(&rcu_torture_timer_rand));
1312 /* Test call_rcu() invocation from interrupt handler. */
1313 if (cur_ops->call) {
1314 struct rcu_head *rhp = kmalloc(sizeof(*rhp), GFP_NOWAIT);
1317 cur_ops->call(rhp, rcu_torture_timer_cb);
1322 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
1323 * incrementing the corresponding element of the pipeline array. The
1324 * counter in the element should never be greater than 1, otherwise, the
1325 * RCU implementation is broken.
1328 rcu_torture_reader(void *arg)
1330 DEFINE_TORTURE_RANDOM(rand);
1331 struct timer_list t;
1333 VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
1334 set_user_nice(current, MAX_NICE);
1335 if (irqreader && cur_ops->irq_capable)
1336 timer_setup_on_stack(&t, rcu_torture_timer, 0);
1339 if (irqreader && cur_ops->irq_capable) {
1340 if (!timer_pending(&t))
1341 mod_timer(&t, jiffies + 1);
1343 if (!rcu_torture_one_read(&rand))
1344 schedule_timeout_interruptible(HZ);
1345 stutter_wait("rcu_torture_reader");
1346 } while (!torture_must_stop());
1347 if (irqreader && cur_ops->irq_capable) {
1349 destroy_timer_on_stack(&t);
1351 torture_kthread_stopping("rcu_torture_reader");
1356 * Print torture statistics. Caller must ensure that there is only
1357 * one call to this function at a given time!!! This is normally
1358 * accomplished by relying on the module system to only have one copy
1359 * of the module loaded, and then by giving the rcu_torture_stats
1360 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1361 * thread is not running).
1364 rcu_torture_stats_print(void)
1368 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1369 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1370 static unsigned long rtcv_snap = ULONG_MAX;
1371 static bool splatted;
1372 struct task_struct *wtp;
1374 for_each_possible_cpu(cpu) {
1375 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1376 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1377 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1380 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1381 if (pipesummary[i] != 0)
1385 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1386 pr_cont("rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
1387 rcu_torture_current,
1388 rcu_torture_current_version,
1389 list_empty(&rcu_torture_freelist),
1390 atomic_read(&n_rcu_torture_alloc),
1391 atomic_read(&n_rcu_torture_alloc_fail),
1392 atomic_read(&n_rcu_torture_free));
1393 pr_cont("rtmbe: %d rtbe: %ld rtbke: %ld rtbre: %ld ",
1394 atomic_read(&n_rcu_torture_mberror),
1395 n_rcu_torture_barrier_error,
1396 n_rcu_torture_boost_ktrerror,
1397 n_rcu_torture_boost_rterror);
1398 pr_cont("rtbf: %ld rtb: %ld nt: %ld ",
1399 n_rcu_torture_boost_failure,
1400 n_rcu_torture_boosts,
1401 atomic_long_read(&n_rcu_torture_timers));
1402 torture_onoff_stats();
1403 pr_cont("barrier: %ld/%ld:%ld ",
1404 n_barrier_successes,
1406 n_rcu_torture_barrier_error);
1407 pr_cont("cbflood: %ld\n", atomic_long_read(&n_cbfloods));
1409 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1410 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
1411 n_rcu_torture_barrier_error != 0 ||
1412 n_rcu_torture_boost_ktrerror != 0 ||
1413 n_rcu_torture_boost_rterror != 0 ||
1414 n_rcu_torture_boost_failure != 0 ||
1416 pr_cont("%s", "!!! ");
1417 atomic_inc(&n_rcu_torture_error);
1420 pr_cont("Reader Pipe: ");
1421 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1422 pr_cont(" %ld", pipesummary[i]);
1425 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1426 pr_cont("Reader Batch: ");
1427 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1428 pr_cont(" %ld", batchsummary[i]);
1431 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1432 pr_cont("Free-Block Circulation: ");
1433 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1434 pr_cont(" %d", atomic_read(&rcu_torture_wcount[i]));
1440 if (rtcv_snap == rcu_torture_current_version &&
1441 rcu_torture_current != NULL) {
1442 int __maybe_unused flags = 0;
1443 unsigned long __maybe_unused gp_seq = 0;
1445 rcutorture_get_gp_data(cur_ops->ttype,
1447 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp,
1449 wtp = READ_ONCE(writer_task);
1450 pr_alert("??? Writer stall state %s(%d) g%lu f%#x ->state %#lx cpu %d\n",
1451 rcu_torture_writer_state_getname(),
1452 rcu_torture_writer_state, gp_seq, flags,
1453 wtp == NULL ? ~0UL : wtp->state,
1454 wtp == NULL ? -1 : (int)task_cpu(wtp));
1455 if (!splatted && wtp) {
1456 sched_show_task(wtp);
1459 show_rcu_gp_kthreads();
1460 rcu_ftrace_dump(DUMP_ALL);
1462 rtcv_snap = rcu_torture_current_version;
1466 * Periodically prints torture statistics, if periodic statistics printing
1467 * was specified via the stat_interval module parameter.
1470 rcu_torture_stats(void *arg)
1472 VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
1474 schedule_timeout_interruptible(stat_interval * HZ);
1475 rcu_torture_stats_print();
1476 torture_shutdown_absorb("rcu_torture_stats");
1477 } while (!torture_must_stop());
1478 torture_kthread_stopping("rcu_torture_stats");
1483 rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
1485 pr_alert("%s" TORTURE_FLAG
1486 "--- %s: nreaders=%d nfakewriters=%d "
1487 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1488 "shuffle_interval=%d stutter=%d irqreader=%d "
1489 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1490 "test_boost=%d/%d test_boost_interval=%d "
1491 "test_boost_duration=%d shutdown_secs=%d "
1492 "stall_cpu=%d stall_cpu_holdoff=%d stall_cpu_irqsoff=%d "
1494 "onoff_interval=%d onoff_holdoff=%d\n",
1495 torture_type, tag, nrealreaders, nfakewriters,
1496 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1497 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1498 test_boost, cur_ops->can_boost,
1499 test_boost_interval, test_boost_duration, shutdown_secs,
1500 stall_cpu, stall_cpu_holdoff, stall_cpu_irqsoff,
1502 onoff_interval, onoff_holdoff);
1505 static int rcutorture_booster_cleanup(unsigned int cpu)
1507 struct task_struct *t;
1509 if (boost_tasks[cpu] == NULL)
1511 mutex_lock(&boost_mutex);
1512 t = boost_tasks[cpu];
1513 boost_tasks[cpu] = NULL;
1514 mutex_unlock(&boost_mutex);
1516 /* This must be outside of the mutex, otherwise deadlock! */
1517 torture_stop_kthread(rcu_torture_boost, t);
1521 static int rcutorture_booster_init(unsigned int cpu)
1525 if (boost_tasks[cpu] != NULL)
1526 return 0; /* Already created, nothing more to do. */
1528 /* Don't allow time recalculation while creating a new task. */
1529 mutex_lock(&boost_mutex);
1530 VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
1531 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1533 "rcu_torture_boost");
1534 if (IS_ERR(boost_tasks[cpu])) {
1535 retval = PTR_ERR(boost_tasks[cpu]);
1536 VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
1537 n_rcu_torture_boost_ktrerror++;
1538 boost_tasks[cpu] = NULL;
1539 mutex_unlock(&boost_mutex);
1542 kthread_bind(boost_tasks[cpu], cpu);
1543 wake_up_process(boost_tasks[cpu]);
1544 mutex_unlock(&boost_mutex);
1549 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1550 * induces a CPU stall for the time specified by stall_cpu.
1552 static int rcu_torture_stall(void *args)
1554 unsigned long stop_at;
1556 VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
1557 if (stall_cpu_holdoff > 0) {
1558 VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
1559 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
1560 VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
1562 if (!kthread_should_stop()) {
1563 stop_at = get_seconds() + stall_cpu;
1564 /* RCU CPU stall is expected behavior in following code. */
1566 if (stall_cpu_irqsoff)
1567 local_irq_disable();
1570 pr_alert("rcu_torture_stall start on CPU %d.\n",
1571 smp_processor_id());
1572 while (ULONG_CMP_LT(get_seconds(), stop_at))
1573 continue; /* Induce RCU CPU stall warning. */
1574 if (stall_cpu_irqsoff)
1579 pr_alert("rcu_torture_stall end.\n");
1581 torture_shutdown_absorb("rcu_torture_stall");
1582 while (!kthread_should_stop())
1583 schedule_timeout_interruptible(10 * HZ);
1587 /* Spawn CPU-stall kthread, if stall_cpu specified. */
1588 static int __init rcu_torture_stall_init(void)
1592 return torture_create_kthread(rcu_torture_stall, NULL, stall_task);
1595 /* Callback function for RCU barrier testing. */
1596 static void rcu_torture_barrier_cbf(struct rcu_head *rcu)
1598 atomic_inc(&barrier_cbs_invoked);
1601 /* kthread function to register callbacks used to test RCU barriers. */
1602 static int rcu_torture_barrier_cbs(void *arg)
1604 long myid = (long)arg;
1607 struct rcu_head rcu;
1609 init_rcu_head_on_stack(&rcu);
1610 VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
1611 set_user_nice(current, MAX_NICE);
1613 wait_event(barrier_cbs_wq[myid],
1615 smp_load_acquire(&barrier_phase)) != lastphase ||
1616 torture_must_stop());
1617 lastphase = newphase;
1618 if (torture_must_stop())
1621 * The above smp_load_acquire() ensures barrier_phase load
1622 * is ordered before the following ->call().
1624 local_irq_disable(); /* Just to test no-irq call_rcu(). */
1625 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1627 if (atomic_dec_and_test(&barrier_cbs_count))
1628 wake_up(&barrier_wq);
1629 } while (!torture_must_stop());
1630 if (cur_ops->cb_barrier != NULL)
1631 cur_ops->cb_barrier();
1632 destroy_rcu_head_on_stack(&rcu);
1633 torture_kthread_stopping("rcu_torture_barrier_cbs");
1637 /* kthread function to drive and coordinate RCU barrier testing. */
1638 static int rcu_torture_barrier(void *arg)
1642 VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
1644 atomic_set(&barrier_cbs_invoked, 0);
1645 atomic_set(&barrier_cbs_count, n_barrier_cbs);
1646 /* Ensure barrier_phase ordered after prior assignments. */
1647 smp_store_release(&barrier_phase, !barrier_phase);
1648 for (i = 0; i < n_barrier_cbs; i++)
1649 wake_up(&barrier_cbs_wq[i]);
1650 wait_event(barrier_wq,
1651 atomic_read(&barrier_cbs_count) == 0 ||
1652 torture_must_stop());
1653 if (torture_must_stop())
1655 n_barrier_attempts++;
1656 cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
1657 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1658 n_rcu_torture_barrier_error++;
1659 pr_err("barrier_cbs_invoked = %d, n_barrier_cbs = %d\n",
1660 atomic_read(&barrier_cbs_invoked),
1664 n_barrier_successes++;
1665 schedule_timeout_interruptible(HZ / 10);
1666 } while (!torture_must_stop());
1667 torture_kthread_stopping("rcu_torture_barrier");
1671 /* Initialize RCU barrier testing. */
1672 static int rcu_torture_barrier_init(void)
1677 if (n_barrier_cbs <= 0)
1679 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
1680 pr_alert("%s" TORTURE_FLAG
1681 " Call or barrier ops missing for %s,\n",
1682 torture_type, cur_ops->name);
1683 pr_alert("%s" TORTURE_FLAG
1684 " RCU barrier testing omitted from run.\n",
1688 atomic_set(&barrier_cbs_count, 0);
1689 atomic_set(&barrier_cbs_invoked, 0);
1691 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_tasks[0]),
1694 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_wq[0]), GFP_KERNEL);
1695 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
1697 for (i = 0; i < n_barrier_cbs; i++) {
1698 init_waitqueue_head(&barrier_cbs_wq[i]);
1699 ret = torture_create_kthread(rcu_torture_barrier_cbs,
1701 barrier_cbs_tasks[i]);
1705 return torture_create_kthread(rcu_torture_barrier, NULL, barrier_task);
1708 /* Clean up after RCU barrier testing. */
1709 static void rcu_torture_barrier_cleanup(void)
1713 torture_stop_kthread(rcu_torture_barrier, barrier_task);
1714 if (barrier_cbs_tasks != NULL) {
1715 for (i = 0; i < n_barrier_cbs; i++)
1716 torture_stop_kthread(rcu_torture_barrier_cbs,
1717 barrier_cbs_tasks[i]);
1718 kfree(barrier_cbs_tasks);
1719 barrier_cbs_tasks = NULL;
1721 if (barrier_cbs_wq != NULL) {
1722 kfree(barrier_cbs_wq);
1723 barrier_cbs_wq = NULL;
1727 static enum cpuhp_state rcutor_hp;
1730 rcu_torture_cleanup(void)
1733 unsigned long gp_seq = 0;
1736 if (torture_cleanup_begin()) {
1737 if (cur_ops->cb_barrier != NULL)
1738 cur_ops->cb_barrier();
1742 rcu_torture_barrier_cleanup();
1743 torture_stop_kthread(rcu_torture_stall, stall_task);
1744 torture_stop_kthread(rcu_torture_writer, writer_task);
1747 for (i = 0; i < nrealreaders; i++)
1748 torture_stop_kthread(rcu_torture_reader,
1750 kfree(reader_tasks);
1752 rcu_torture_current = NULL;
1754 if (fakewriter_tasks) {
1755 for (i = 0; i < nfakewriters; i++) {
1756 torture_stop_kthread(rcu_torture_fakewriter,
1757 fakewriter_tasks[i]);
1759 kfree(fakewriter_tasks);
1760 fakewriter_tasks = NULL;
1763 rcutorture_get_gp_data(cur_ops->ttype, &flags, &gp_seq);
1764 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp, &flags, &gp_seq);
1765 pr_alert("%s: End-test grace-period state: g%lu f%#x\n",
1766 cur_ops->name, gp_seq, flags);
1767 torture_stop_kthread(rcu_torture_stats, stats_task);
1768 torture_stop_kthread(rcu_torture_fqs, fqs_task);
1769 for (i = 0; i < ncbflooders; i++)
1770 torture_stop_kthread(rcu_torture_cbflood, cbflood_task[i]);
1771 if ((test_boost == 1 && cur_ops->can_boost) ||
1773 cpuhp_remove_state(rcutor_hp);
1776 * Wait for all RCU callbacks to fire, then do flavor-specific
1777 * cleanup operations.
1779 if (cur_ops->cb_barrier != NULL)
1780 cur_ops->cb_barrier();
1781 if (cur_ops->cleanup != NULL)
1784 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
1786 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
1787 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
1788 else if (torture_onoff_failures())
1789 rcu_torture_print_module_parms(cur_ops,
1790 "End of test: RCU_HOTPLUG");
1792 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
1793 torture_cleanup_end();
1796 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1797 static void rcu_torture_leak_cb(struct rcu_head *rhp)
1801 static void rcu_torture_err_cb(struct rcu_head *rhp)
1804 * This -might- happen due to race conditions, but is unlikely.
1805 * The scenario that leads to this happening is that the
1806 * first of the pair of duplicate callbacks is queued,
1807 * someone else starts a grace period that includes that
1808 * callback, then the second of the pair must wait for the
1809 * next grace period. Unlikely, but can happen. If it
1810 * does happen, the debug-objects subsystem won't have splatted.
1812 pr_alert("%s: duplicated callback was invoked.\n", KBUILD_MODNAME);
1814 #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1817 * Verify that double-free causes debug-objects to complain, but only
1818 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
1819 * cannot be carried out.
1821 static void rcu_test_debug_objects(void)
1823 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1824 struct rcu_head rh1;
1825 struct rcu_head rh2;
1827 init_rcu_head_on_stack(&rh1);
1828 init_rcu_head_on_stack(&rh2);
1829 pr_alert("%s: WARN: Duplicate call_rcu() test starting.\n", KBUILD_MODNAME);
1831 /* Try to queue the rh2 pair of callbacks for the same grace period. */
1832 preempt_disable(); /* Prevent preemption from interrupting test. */
1833 rcu_read_lock(); /* Make it impossible to finish a grace period. */
1834 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
1835 local_irq_disable(); /* Make it harder to start a new grace period. */
1836 call_rcu(&rh2, rcu_torture_leak_cb);
1837 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
1842 /* Wait for them all to get done so we can safely return. */
1844 pr_alert("%s: WARN: Duplicate call_rcu() test complete.\n", KBUILD_MODNAME);
1845 destroy_rcu_head_on_stack(&rh1);
1846 destroy_rcu_head_on_stack(&rh2);
1847 #else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1848 pr_alert("%s: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n", KBUILD_MODNAME);
1849 #endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1853 rcu_torture_init(void)
1858 static struct rcu_torture_ops *torture_ops[] = {
1859 &rcu_ops, &rcu_bh_ops, &rcu_busted_ops, &srcu_ops, &srcud_ops,
1860 &busted_srcud_ops, &sched_ops, &tasks_ops,
1863 if (!torture_init_begin(torture_type, verbose))
1866 /* Process args and tell the world that the torturer is on the job. */
1867 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1868 cur_ops = torture_ops[i];
1869 if (strcmp(torture_type, cur_ops->name) == 0)
1872 if (i == ARRAY_SIZE(torture_ops)) {
1873 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
1875 pr_alert("rcu-torture types:");
1876 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
1877 pr_alert(" %s", torture_ops[i]->name);
1882 if (cur_ops->fqs == NULL && fqs_duration != 0) {
1883 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
1889 if (nreaders >= 0) {
1890 nrealreaders = nreaders;
1892 nrealreaders = num_online_cpus() - 2 - nreaders;
1893 if (nrealreaders <= 0)
1896 rcu_torture_print_module_parms(cur_ops, "Start of test");
1898 /* Set up the freelist. */
1900 INIT_LIST_HEAD(&rcu_torture_freelist);
1901 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1902 rcu_tortures[i].rtort_mbtest = 0;
1903 list_add_tail(&rcu_tortures[i].rtort_free,
1904 &rcu_torture_freelist);
1907 /* Initialize the statistics so that each run gets its own numbers. */
1909 rcu_torture_current = NULL;
1910 rcu_torture_current_version = 0;
1911 atomic_set(&n_rcu_torture_alloc, 0);
1912 atomic_set(&n_rcu_torture_alloc_fail, 0);
1913 atomic_set(&n_rcu_torture_free, 0);
1914 atomic_set(&n_rcu_torture_mberror, 0);
1915 atomic_set(&n_rcu_torture_error, 0);
1916 n_rcu_torture_barrier_error = 0;
1917 n_rcu_torture_boost_ktrerror = 0;
1918 n_rcu_torture_boost_rterror = 0;
1919 n_rcu_torture_boost_failure = 0;
1920 n_rcu_torture_boosts = 0;
1921 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1922 atomic_set(&rcu_torture_wcount[i], 0);
1923 for_each_possible_cpu(cpu) {
1924 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1925 per_cpu(rcu_torture_count, cpu)[i] = 0;
1926 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1930 /* Start up the kthreads. */
1932 firsterr = torture_create_kthread(rcu_torture_writer, NULL,
1936 if (nfakewriters > 0) {
1937 fakewriter_tasks = kcalloc(nfakewriters,
1938 sizeof(fakewriter_tasks[0]),
1940 if (fakewriter_tasks == NULL) {
1941 VERBOSE_TOROUT_ERRSTRING("out of memory");
1946 for (i = 0; i < nfakewriters; i++) {
1947 firsterr = torture_create_kthread(rcu_torture_fakewriter,
1948 NULL, fakewriter_tasks[i]);
1952 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
1954 if (reader_tasks == NULL) {
1955 VERBOSE_TOROUT_ERRSTRING("out of memory");
1959 for (i = 0; i < nrealreaders; i++) {
1960 firsterr = torture_create_kthread(rcu_torture_reader, NULL,
1965 if (stat_interval > 0) {
1966 firsterr = torture_create_kthread(rcu_torture_stats, NULL,
1971 if (test_no_idle_hz && shuffle_interval > 0) {
1972 firsterr = torture_shuffle_init(shuffle_interval * HZ);
1979 firsterr = torture_stutter_init(stutter * HZ);
1983 if (fqs_duration < 0)
1986 /* Create the fqs thread */
1987 firsterr = torture_create_kthread(rcu_torture_fqs, NULL,
1992 if (test_boost_interval < 1)
1993 test_boost_interval = 1;
1994 if (test_boost_duration < 2)
1995 test_boost_duration = 2;
1996 if ((test_boost == 1 && cur_ops->can_boost) ||
1999 boost_starttime = jiffies + test_boost_interval * HZ;
2001 firsterr = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "RCU_TORTURE",
2002 rcutorture_booster_init,
2003 rcutorture_booster_cleanup);
2006 rcutor_hp = firsterr;
2008 firsterr = torture_shutdown_init(shutdown_secs, rcu_torture_cleanup);
2011 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval);
2014 firsterr = rcu_torture_stall_init();
2017 firsterr = rcu_torture_barrier_init();
2021 rcu_test_debug_objects();
2022 if (cbflood_n_burst > 0) {
2023 /* Create the cbflood threads */
2024 ncbflooders = (num_online_cpus() + 3) / 4;
2025 cbflood_task = kcalloc(ncbflooders, sizeof(*cbflood_task),
2027 if (!cbflood_task) {
2028 VERBOSE_TOROUT_ERRSTRING("out of memory");
2032 for (i = 0; i < ncbflooders; i++) {
2033 firsterr = torture_create_kthread(rcu_torture_cbflood,
2045 rcu_torture_cleanup();
2049 module_init(rcu_torture_init);
2050 module_exit(rcu_torture_cleanup);