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 torture_param(int, cbflood_inter_holdoff, HZ,
66 "Holdoff between floods (jiffies)");
67 torture_param(int, cbflood_intra_holdoff, 1,
68 "Holdoff between bursts (jiffies)");
69 torture_param(int, cbflood_n_burst, 3, "# bursts in flood, zero to disable");
70 torture_param(int, cbflood_n_per_burst, 20000,
71 "# callbacks per burst in flood");
72 torture_param(int, fqs_duration, 0,
73 "Duration of fqs bursts (us), 0 to disable");
74 torture_param(int, fqs_holdoff, 0, "Holdoff time within fqs bursts (us)");
75 torture_param(int, fqs_stutter, 3, "Wait time between fqs bursts (s)");
76 torture_param(bool, gp_cond, false, "Use conditional/async GP wait primitives");
77 torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
78 torture_param(bool, gp_normal, false,
79 "Use normal (non-expedited) GP wait primitives");
80 torture_param(bool, gp_sync, false, "Use synchronous GP wait primitives");
81 torture_param(int, irqreader, 1, "Allow RCU readers from irq handlers");
82 torture_param(int, n_barrier_cbs, 0,
83 "# of callbacks/kthreads for barrier testing");
84 torture_param(int, nfakewriters, 4, "Number of RCU fake writer threads");
85 torture_param(int, nreaders, -1, "Number of RCU reader threads");
86 torture_param(int, object_debug, 0,
87 "Enable debug-object double call_rcu() testing");
88 torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
89 torture_param(int, onoff_interval, 0,
90 "Time between CPU hotplugs (s), 0=disable");
91 torture_param(int, shuffle_interval, 3, "Number of seconds between shuffles");
92 torture_param(int, shutdown_secs, 0, "Shutdown time (s), <= zero to disable.");
93 torture_param(int, stall_cpu, 0, "Stall duration (s), zero to disable.");
94 torture_param(int, stall_cpu_holdoff, 10,
95 "Time to wait before starting stall (s).");
96 torture_param(int, stall_cpu_irqsoff, 0, "Disable interrupts while stalling.");
97 torture_param(int, stat_interval, 60,
98 "Number of seconds between stats printk()s");
99 torture_param(int, stutter, 5, "Number of seconds to run/halt test");
100 torture_param(int, test_boost, 1, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
101 torture_param(int, test_boost_duration, 4,
102 "Duration of each boost test, seconds.");
103 torture_param(int, test_boost_interval, 7,
104 "Interval between boost tests, seconds.");
105 torture_param(bool, test_no_idle_hz, true,
106 "Test support for tickless idle CPUs");
107 torture_param(int, verbose, 1,
108 "Enable verbose debugging printk()s");
110 static char *torture_type = "rcu";
111 module_param(torture_type, charp, 0444);
112 MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, ...)");
114 static int nrealreaders;
115 static int ncbflooders;
116 static struct task_struct *writer_task;
117 static struct task_struct **fakewriter_tasks;
118 static struct task_struct **reader_tasks;
119 static struct task_struct *stats_task;
120 static struct task_struct **cbflood_task;
121 static struct task_struct *fqs_task;
122 static struct task_struct *boost_tasks[NR_CPUS];
123 static struct task_struct *stall_task;
124 static struct task_struct **barrier_cbs_tasks;
125 static struct task_struct *barrier_task;
127 #define RCU_TORTURE_PIPE_LEN 10
130 struct rcu_head rtort_rcu;
131 int rtort_pipe_count;
132 struct list_head rtort_free;
136 static LIST_HEAD(rcu_torture_freelist);
137 static struct rcu_torture __rcu *rcu_torture_current;
138 static unsigned long rcu_torture_current_version;
139 static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
140 static DEFINE_SPINLOCK(rcu_torture_lock);
141 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count);
142 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch);
143 static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
144 static atomic_t n_rcu_torture_alloc;
145 static atomic_t n_rcu_torture_alloc_fail;
146 static atomic_t n_rcu_torture_free;
147 static atomic_t n_rcu_torture_mberror;
148 static atomic_t n_rcu_torture_error;
149 static long n_rcu_torture_barrier_error;
150 static long n_rcu_torture_boost_ktrerror;
151 static long n_rcu_torture_boost_rterror;
152 static long n_rcu_torture_boost_failure;
153 static long n_rcu_torture_boosts;
154 static long n_rcu_torture_timers;
155 static long n_barrier_attempts;
156 static long n_barrier_successes;
157 static atomic_long_t n_cbfloods;
158 static struct list_head rcu_torture_removed;
160 static int rcu_torture_writer_state;
161 #define RTWS_FIXED_DELAY 0
163 #define RTWS_REPLACE 2
164 #define RTWS_DEF_FREE 3
165 #define RTWS_EXP_SYNC 4
166 #define RTWS_COND_GET 5
167 #define RTWS_COND_SYNC 6
169 #define RTWS_STUTTER 8
170 #define RTWS_STOPPING 9
171 static const char * const rcu_torture_writer_state_names[] = {
184 static const char *rcu_torture_writer_state_getname(void)
186 unsigned int i = READ_ONCE(rcu_torture_writer_state);
188 if (i >= ARRAY_SIZE(rcu_torture_writer_state_names))
190 return rcu_torture_writer_state_names[i];
193 #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
194 #define rcu_can_boost() 1
195 #else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
196 #define rcu_can_boost() 0
197 #endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
199 #ifdef CONFIG_RCU_TRACE
200 static u64 notrace rcu_trace_clock_local(void)
202 u64 ts = trace_clock_local();
204 (void)do_div(ts, NSEC_PER_USEC);
207 #else /* #ifdef CONFIG_RCU_TRACE */
208 static u64 notrace rcu_trace_clock_local(void)
212 #endif /* #else #ifdef CONFIG_RCU_TRACE */
214 static unsigned long boost_starttime; /* jiffies of next boost test start. */
215 static DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
216 /* and boost task create/destroy. */
217 static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
218 static bool barrier_phase; /* Test phase. */
219 static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
220 static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
221 static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
224 * Allocate an element from the rcu_tortures pool.
226 static struct rcu_torture *
227 rcu_torture_alloc(void)
231 spin_lock_bh(&rcu_torture_lock);
232 if (list_empty(&rcu_torture_freelist)) {
233 atomic_inc(&n_rcu_torture_alloc_fail);
234 spin_unlock_bh(&rcu_torture_lock);
237 atomic_inc(&n_rcu_torture_alloc);
238 p = rcu_torture_freelist.next;
240 spin_unlock_bh(&rcu_torture_lock);
241 return container_of(p, struct rcu_torture, rtort_free);
245 * Free an element to the rcu_tortures pool.
248 rcu_torture_free(struct rcu_torture *p)
250 atomic_inc(&n_rcu_torture_free);
251 spin_lock_bh(&rcu_torture_lock);
252 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
253 spin_unlock_bh(&rcu_torture_lock);
257 * Operations vector for selecting different types of tests.
260 struct rcu_torture_ops {
263 void (*cleanup)(void);
264 int (*readlock)(void);
265 void (*read_delay)(struct torture_random_state *rrsp);
266 void (*readunlock)(int idx);
267 unsigned long (*get_gp_seq)(void);
268 void (*deferred_free)(struct rcu_torture *p);
270 void (*exp_sync)(void);
271 unsigned long (*get_state)(void);
272 void (*cond_sync)(unsigned long oldstate);
273 call_rcu_func_t call;
274 void (*cb_barrier)(void);
282 static struct rcu_torture_ops *cur_ops;
285 * Definitions for rcu torture testing.
288 static int rcu_torture_read_lock(void) __acquires(RCU)
294 static void rcu_read_delay(struct torture_random_state *rrsp)
296 unsigned long started;
297 unsigned long completed;
298 const unsigned long shortdelay_us = 200;
299 const unsigned long longdelay_ms = 50;
300 unsigned long long ts;
302 /* We want a short delay sometimes to make a reader delay the grace
303 * period, and we want a long delay occasionally to trigger
304 * force_quiescent_state. */
306 if (!(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms))) {
307 started = cur_ops->get_gp_seq();
308 ts = rcu_trace_clock_local();
309 mdelay(longdelay_ms);
310 completed = cur_ops->get_gp_seq();
311 do_trace_rcu_torture_read(cur_ops->name, NULL, ts,
314 if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
315 udelay(shortdelay_us);
316 if (!preempt_count() &&
317 !(torture_random(rrsp) % (nrealreaders * 500)))
318 torture_preempt_schedule(); /* QS only if preemptible. */
321 static void rcu_torture_read_unlock(int idx) __releases(RCU)
327 * Update callback in the pipe. This should be invoked after a grace period.
330 rcu_torture_pipe_update_one(struct rcu_torture *rp)
334 i = rp->rtort_pipe_count;
335 if (i > RCU_TORTURE_PIPE_LEN)
336 i = RCU_TORTURE_PIPE_LEN;
337 atomic_inc(&rcu_torture_wcount[i]);
338 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
339 rp->rtort_mbtest = 0;
346 * Update all callbacks in the pipe. Suitable for synchronous grace-period
350 rcu_torture_pipe_update(struct rcu_torture *old_rp)
352 struct rcu_torture *rp;
353 struct rcu_torture *rp1;
356 list_add(&old_rp->rtort_free, &rcu_torture_removed);
357 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
358 if (rcu_torture_pipe_update_one(rp)) {
359 list_del(&rp->rtort_free);
360 rcu_torture_free(rp);
366 rcu_torture_cb(struct rcu_head *p)
368 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
370 if (torture_must_stop_irq()) {
371 /* Test is ending, just drop callbacks on the floor. */
372 /* The next initialization will pick up the pieces. */
375 if (rcu_torture_pipe_update_one(rp))
376 rcu_torture_free(rp);
378 cur_ops->deferred_free(rp);
381 static unsigned long rcu_no_completed(void)
386 static void rcu_torture_deferred_free(struct rcu_torture *p)
388 call_rcu(&p->rtort_rcu, rcu_torture_cb);
391 static void rcu_sync_torture_init(void)
393 INIT_LIST_HEAD(&rcu_torture_removed);
396 static struct rcu_torture_ops rcu_ops = {
398 .init = rcu_sync_torture_init,
399 .readlock = rcu_torture_read_lock,
400 .read_delay = rcu_read_delay,
401 .readunlock = rcu_torture_read_unlock,
402 .get_gp_seq = rcu_get_gp_seq,
403 .deferred_free = rcu_torture_deferred_free,
404 .sync = synchronize_rcu,
405 .exp_sync = synchronize_rcu_expedited,
406 .get_state = get_state_synchronize_rcu,
407 .cond_sync = cond_synchronize_rcu,
409 .cb_barrier = rcu_barrier,
410 .fqs = rcu_force_quiescent_state,
413 .can_boost = rcu_can_boost(),
418 * Definitions for rcu_bh torture testing.
421 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
427 static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
429 rcu_read_unlock_bh();
432 static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
434 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
437 static struct rcu_torture_ops rcu_bh_ops = {
438 .ttype = RCU_BH_FLAVOR,
439 .init = rcu_sync_torture_init,
440 .readlock = rcu_bh_torture_read_lock,
441 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
442 .readunlock = rcu_bh_torture_read_unlock,
443 .get_gp_seq = rcu_bh_get_gp_seq,
444 .deferred_free = rcu_bh_torture_deferred_free,
445 .sync = synchronize_rcu_bh,
446 .exp_sync = synchronize_rcu_bh_expedited,
448 .cb_barrier = rcu_barrier_bh,
449 .fqs = rcu_bh_force_quiescent_state,
456 * Don't even think about trying any of these in real life!!!
457 * The names includes "busted", and they really means it!
458 * The only purpose of these functions is to provide a buggy RCU
459 * implementation to make sure that rcutorture correctly emits
460 * buggy-RCU error messages.
462 static void rcu_busted_torture_deferred_free(struct rcu_torture *p)
464 /* This is a deliberate bug for testing purposes only! */
465 rcu_torture_cb(&p->rtort_rcu);
468 static void synchronize_rcu_busted(void)
470 /* This is a deliberate bug for testing purposes only! */
474 call_rcu_busted(struct rcu_head *head, rcu_callback_t func)
476 /* This is a deliberate bug for testing purposes only! */
480 static struct rcu_torture_ops rcu_busted_ops = {
481 .ttype = INVALID_RCU_FLAVOR,
482 .init = rcu_sync_torture_init,
483 .readlock = rcu_torture_read_lock,
484 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
485 .readunlock = rcu_torture_read_unlock,
486 .get_gp_seq = rcu_no_completed,
487 .deferred_free = rcu_busted_torture_deferred_free,
488 .sync = synchronize_rcu_busted,
489 .exp_sync = synchronize_rcu_busted,
490 .call = call_rcu_busted,
499 * Definitions for srcu torture testing.
502 DEFINE_STATIC_SRCU(srcu_ctl);
503 static struct srcu_struct srcu_ctld;
504 static struct srcu_struct *srcu_ctlp = &srcu_ctl;
506 static int srcu_torture_read_lock(void) __acquires(srcu_ctlp)
508 return srcu_read_lock(srcu_ctlp);
511 static void srcu_read_delay(struct torture_random_state *rrsp)
514 const long uspertick = 1000000 / HZ;
515 const long longdelay = 10;
517 /* We want there to be long-running readers, but not all the time. */
519 delay = torture_random(rrsp) %
520 (nrealreaders * 2 * longdelay * uspertick);
521 if (!delay && in_task())
522 schedule_timeout_interruptible(longdelay);
524 rcu_read_delay(rrsp);
527 static void srcu_torture_read_unlock(int idx) __releases(srcu_ctlp)
529 srcu_read_unlock(srcu_ctlp, idx);
532 static unsigned long srcu_torture_completed(void)
534 return srcu_batches_completed(srcu_ctlp);
537 static void srcu_torture_deferred_free(struct rcu_torture *rp)
539 call_srcu(srcu_ctlp, &rp->rtort_rcu, rcu_torture_cb);
542 static void srcu_torture_synchronize(void)
544 synchronize_srcu(srcu_ctlp);
547 static void srcu_torture_call(struct rcu_head *head,
550 call_srcu(srcu_ctlp, head, func);
553 static void srcu_torture_barrier(void)
555 srcu_barrier(srcu_ctlp);
558 static void srcu_torture_stats(void)
560 srcu_torture_stats_print(srcu_ctlp, torture_type, TORTURE_FLAG);
563 static void srcu_torture_synchronize_expedited(void)
565 synchronize_srcu_expedited(srcu_ctlp);
568 static struct rcu_torture_ops srcu_ops = {
569 .ttype = SRCU_FLAVOR,
570 .init = rcu_sync_torture_init,
571 .readlock = srcu_torture_read_lock,
572 .read_delay = srcu_read_delay,
573 .readunlock = srcu_torture_read_unlock,
574 .get_gp_seq = srcu_torture_completed,
575 .deferred_free = srcu_torture_deferred_free,
576 .sync = srcu_torture_synchronize,
577 .exp_sync = srcu_torture_synchronize_expedited,
578 .call = srcu_torture_call,
579 .cb_barrier = srcu_torture_barrier,
580 .stats = srcu_torture_stats,
585 static void srcu_torture_init(void)
587 rcu_sync_torture_init();
588 WARN_ON(init_srcu_struct(&srcu_ctld));
589 srcu_ctlp = &srcu_ctld;
592 static void srcu_torture_cleanup(void)
594 static DEFINE_TORTURE_RANDOM(rand);
596 if (torture_random(&rand) & 0x800)
597 cleanup_srcu_struct(&srcu_ctld);
599 cleanup_srcu_struct_quiesced(&srcu_ctld);
600 srcu_ctlp = &srcu_ctl; /* In case of a later rcutorture run. */
603 /* As above, but dynamically allocated. */
604 static struct rcu_torture_ops srcud_ops = {
605 .ttype = SRCU_FLAVOR,
606 .init = srcu_torture_init,
607 .cleanup = srcu_torture_cleanup,
608 .readlock = srcu_torture_read_lock,
609 .read_delay = srcu_read_delay,
610 .readunlock = srcu_torture_read_unlock,
611 .get_gp_seq = srcu_torture_completed,
612 .deferred_free = srcu_torture_deferred_free,
613 .sync = srcu_torture_synchronize,
614 .exp_sync = srcu_torture_synchronize_expedited,
615 .call = srcu_torture_call,
616 .cb_barrier = srcu_torture_barrier,
617 .stats = srcu_torture_stats,
623 * Definitions for sched torture testing.
626 static int sched_torture_read_lock(void)
632 static void sched_torture_read_unlock(int idx)
637 static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
639 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
642 static struct rcu_torture_ops sched_ops = {
643 .ttype = RCU_SCHED_FLAVOR,
644 .init = rcu_sync_torture_init,
645 .readlock = sched_torture_read_lock,
646 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
647 .readunlock = sched_torture_read_unlock,
648 .get_gp_seq = rcu_sched_get_gp_seq,
649 .deferred_free = rcu_sched_torture_deferred_free,
650 .sync = synchronize_sched,
651 .exp_sync = synchronize_sched_expedited,
652 .get_state = get_state_synchronize_sched,
653 .cond_sync = cond_synchronize_sched,
654 .call = call_rcu_sched,
655 .cb_barrier = rcu_barrier_sched,
656 .fqs = rcu_sched_force_quiescent_state,
663 * Definitions for RCU-tasks torture testing.
666 static int tasks_torture_read_lock(void)
671 static void tasks_torture_read_unlock(int idx)
675 static void rcu_tasks_torture_deferred_free(struct rcu_torture *p)
677 call_rcu_tasks(&p->rtort_rcu, rcu_torture_cb);
680 static struct rcu_torture_ops tasks_ops = {
681 .ttype = RCU_TASKS_FLAVOR,
682 .init = rcu_sync_torture_init,
683 .readlock = tasks_torture_read_lock,
684 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
685 .readunlock = tasks_torture_read_unlock,
686 .get_gp_seq = rcu_no_completed,
687 .deferred_free = rcu_tasks_torture_deferred_free,
688 .sync = synchronize_rcu_tasks,
689 .exp_sync = synchronize_rcu_tasks,
690 .call = call_rcu_tasks,
691 .cb_barrier = rcu_barrier_tasks,
698 static bool __maybe_unused torturing_tasks(void)
700 return cur_ops == &tasks_ops;
704 * RCU torture priority-boost testing. Runs one real-time thread per
705 * CPU for moderate bursts, repeatedly registering RCU callbacks and
706 * spinning waiting for them to be invoked. If a given callback takes
707 * too long to be invoked, we assume that priority inversion has occurred.
710 struct rcu_boost_inflight {
715 static void rcu_torture_boost_cb(struct rcu_head *head)
717 struct rcu_boost_inflight *rbip =
718 container_of(head, struct rcu_boost_inflight, rcu);
720 /* Ensure RCU-core accesses precede clearing ->inflight */
721 smp_store_release(&rbip->inflight, 0);
724 static int rcu_torture_boost(void *arg)
726 unsigned long call_rcu_time;
727 unsigned long endtime;
728 unsigned long oldstarttime;
729 struct rcu_boost_inflight rbi = { .inflight = 0 };
730 struct sched_param sp;
732 VERBOSE_TOROUT_STRING("rcu_torture_boost started");
734 /* Set real-time priority. */
735 sp.sched_priority = 1;
736 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
737 VERBOSE_TOROUT_STRING("rcu_torture_boost RT prio failed!");
738 n_rcu_torture_boost_rterror++;
741 init_rcu_head_on_stack(&rbi.rcu);
742 /* Each pass through the following loop does one boost-test cycle. */
744 /* Wait for the next test interval. */
745 oldstarttime = boost_starttime;
746 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
747 schedule_timeout_interruptible(oldstarttime - jiffies);
748 stutter_wait("rcu_torture_boost");
749 if (torture_must_stop())
753 /* Do one boost-test interval. */
754 endtime = oldstarttime + test_boost_duration * HZ;
755 call_rcu_time = jiffies;
756 while (ULONG_CMP_LT(jiffies, endtime)) {
757 /* If we don't have a callback in flight, post one. */
758 if (!smp_load_acquire(&rbi.inflight)) {
759 /* RCU core before ->inflight = 1. */
760 smp_store_release(&rbi.inflight, 1);
761 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
762 if (jiffies - call_rcu_time >
763 test_boost_duration * HZ - HZ / 2) {
764 VERBOSE_TOROUT_STRING("rcu_torture_boost boosting failed");
765 n_rcu_torture_boost_failure++;
767 call_rcu_time = jiffies;
769 stutter_wait("rcu_torture_boost");
770 if (torture_must_stop())
775 * Set the start time of the next test interval.
776 * Yes, this is vulnerable to long delays, but such
777 * delays simply cause a false negative for the next
778 * interval. Besides, we are running at RT priority,
779 * so delays should be relatively rare.
781 while (oldstarttime == boost_starttime &&
782 !kthread_should_stop()) {
783 if (mutex_trylock(&boost_mutex)) {
784 boost_starttime = jiffies +
785 test_boost_interval * HZ;
786 n_rcu_torture_boosts++;
787 mutex_unlock(&boost_mutex);
790 schedule_timeout_uninterruptible(1);
793 /* Go do the stutter. */
794 checkwait: stutter_wait("rcu_torture_boost");
795 } while (!torture_must_stop());
797 /* Clean up and exit. */
798 while (!kthread_should_stop() || smp_load_acquire(&rbi.inflight)) {
799 torture_shutdown_absorb("rcu_torture_boost");
800 schedule_timeout_uninterruptible(1);
802 destroy_rcu_head_on_stack(&rbi.rcu);
803 torture_kthread_stopping("rcu_torture_boost");
807 static void rcu_torture_cbflood_cb(struct rcu_head *rhp)
812 * RCU torture callback-flood kthread. Repeatedly induces bursts of calls
813 * to call_rcu() or analogous, increasing the probability of occurrence
814 * of callback-overflow corner cases.
817 rcu_torture_cbflood(void *arg)
822 struct rcu_head *rhp;
824 if (cbflood_n_per_burst > 0 &&
825 cbflood_inter_holdoff > 0 &&
826 cbflood_intra_holdoff > 0 &&
828 cur_ops->cb_barrier) {
829 rhp = vmalloc(array3_size(cbflood_n_burst,
835 VERBOSE_TOROUT_STRING("rcu_torture_cbflood disabled: Bad args or OOM");
838 VERBOSE_TOROUT_STRING("rcu_torture_cbflood task started");
840 schedule_timeout_interruptible(cbflood_inter_holdoff);
841 atomic_long_inc(&n_cbfloods);
842 WARN_ON(signal_pending(current));
843 for (i = 0; i < cbflood_n_burst; i++) {
844 for (j = 0; j < cbflood_n_per_burst; j++) {
845 cur_ops->call(&rhp[i * cbflood_n_per_burst + j],
846 rcu_torture_cbflood_cb);
848 schedule_timeout_interruptible(cbflood_intra_holdoff);
849 WARN_ON(signal_pending(current));
851 cur_ops->cb_barrier();
852 stutter_wait("rcu_torture_cbflood");
853 } while (!torture_must_stop());
856 torture_kthread_stopping("rcu_torture_cbflood");
861 * RCU torture force-quiescent-state kthread. Repeatedly induces
862 * bursts of calls to force_quiescent_state(), increasing the probability
863 * of occurrence of some important types of race conditions.
866 rcu_torture_fqs(void *arg)
868 unsigned long fqs_resume_time;
869 int fqs_burst_remaining;
871 VERBOSE_TOROUT_STRING("rcu_torture_fqs task started");
873 fqs_resume_time = jiffies + fqs_stutter * HZ;
874 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
875 !kthread_should_stop()) {
876 schedule_timeout_interruptible(1);
878 fqs_burst_remaining = fqs_duration;
879 while (fqs_burst_remaining > 0 &&
880 !kthread_should_stop()) {
883 fqs_burst_remaining -= fqs_holdoff;
885 stutter_wait("rcu_torture_fqs");
886 } while (!torture_must_stop());
887 torture_kthread_stopping("rcu_torture_fqs");
892 * RCU torture writer kthread. Repeatedly substitutes a new structure
893 * for that pointed to by rcu_torture_current, freeing the old structure
894 * after a series of grace periods (the "pipeline").
897 rcu_torture_writer(void *arg)
899 bool can_expedite = !rcu_gp_is_expedited() && !rcu_gp_is_normal();
901 unsigned long gp_snap;
902 bool gp_cond1 = gp_cond, gp_exp1 = gp_exp, gp_normal1 = gp_normal;
903 bool gp_sync1 = gp_sync;
905 struct rcu_torture *rp;
906 struct rcu_torture *old_rp;
907 static DEFINE_TORTURE_RANDOM(rand);
908 int synctype[] = { RTWS_DEF_FREE, RTWS_EXP_SYNC,
909 RTWS_COND_GET, RTWS_SYNC };
912 VERBOSE_TOROUT_STRING("rcu_torture_writer task started");
914 pr_alert("%s" TORTURE_FLAG
915 " GP expediting controlled from boot/sysfs for %s.\n",
916 torture_type, cur_ops->name);
918 /* Initialize synctype[] array. If none set, take default. */
919 if (!gp_cond1 && !gp_exp1 && !gp_normal1 && !gp_sync1)
920 gp_cond1 = gp_exp1 = gp_normal1 = gp_sync1 = true;
921 if (gp_cond1 && cur_ops->get_state && cur_ops->cond_sync) {
922 synctype[nsynctypes++] = RTWS_COND_GET;
923 pr_info("%s: Testing conditional GPs.\n", __func__);
924 } else if (gp_cond && (!cur_ops->get_state || !cur_ops->cond_sync)) {
925 pr_alert("%s: gp_cond without primitives.\n", __func__);
927 if (gp_exp1 && cur_ops->exp_sync) {
928 synctype[nsynctypes++] = RTWS_EXP_SYNC;
929 pr_info("%s: Testing expedited GPs.\n", __func__);
930 } else if (gp_exp && !cur_ops->exp_sync) {
931 pr_alert("%s: gp_exp without primitives.\n", __func__);
933 if (gp_normal1 && cur_ops->deferred_free) {
934 synctype[nsynctypes++] = RTWS_DEF_FREE;
935 pr_info("%s: Testing asynchronous GPs.\n", __func__);
936 } else if (gp_normal && !cur_ops->deferred_free) {
937 pr_alert("%s: gp_normal without primitives.\n", __func__);
939 if (gp_sync1 && cur_ops->sync) {
940 synctype[nsynctypes++] = RTWS_SYNC;
941 pr_info("%s: Testing normal GPs.\n", __func__);
942 } else if (gp_sync && !cur_ops->sync) {
943 pr_alert("%s: gp_sync without primitives.\n", __func__);
945 if (WARN_ONCE(nsynctypes == 0,
946 "rcu_torture_writer: No update-side primitives.\n")) {
948 * No updates primitives, so don't try updating.
949 * The resulting test won't be testing much, hence the
952 rcu_torture_writer_state = RTWS_STOPPING;
953 torture_kthread_stopping("rcu_torture_writer");
957 rcu_torture_writer_state = RTWS_FIXED_DELAY;
958 schedule_timeout_uninterruptible(1);
959 rp = rcu_torture_alloc();
962 rp->rtort_pipe_count = 0;
963 rcu_torture_writer_state = RTWS_DELAY;
964 udelay(torture_random(&rand) & 0x3ff);
965 rcu_torture_writer_state = RTWS_REPLACE;
966 old_rp = rcu_dereference_check(rcu_torture_current,
967 current == writer_task);
968 rp->rtort_mbtest = 1;
969 rcu_assign_pointer(rcu_torture_current, rp);
970 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
972 i = old_rp->rtort_pipe_count;
973 if (i > RCU_TORTURE_PIPE_LEN)
974 i = RCU_TORTURE_PIPE_LEN;
975 atomic_inc(&rcu_torture_wcount[i]);
976 old_rp->rtort_pipe_count++;
977 switch (synctype[torture_random(&rand) % nsynctypes]) {
979 rcu_torture_writer_state = RTWS_DEF_FREE;
980 cur_ops->deferred_free(old_rp);
983 rcu_torture_writer_state = RTWS_EXP_SYNC;
985 rcu_torture_pipe_update(old_rp);
988 rcu_torture_writer_state = RTWS_COND_GET;
989 gp_snap = cur_ops->get_state();
990 i = torture_random(&rand) % 16;
992 schedule_timeout_interruptible(i);
993 udelay(torture_random(&rand) % 1000);
994 rcu_torture_writer_state = RTWS_COND_SYNC;
995 cur_ops->cond_sync(gp_snap);
996 rcu_torture_pipe_update(old_rp);
999 rcu_torture_writer_state = RTWS_SYNC;
1001 rcu_torture_pipe_update(old_rp);
1008 rcutorture_record_progress(++rcu_torture_current_version);
1009 /* Cycle through nesting levels of rcu_expedite_gp() calls. */
1011 !(torture_random(&rand) & 0xff & (!!expediting - 1))) {
1012 WARN_ON_ONCE(expediting == 0 && rcu_gp_is_expedited());
1013 if (expediting >= 0)
1016 rcu_unexpedite_gp();
1017 if (++expediting > 3)
1018 expediting = -expediting;
1019 } else if (!can_expedite) { /* Disabled during boot, recheck. */
1020 can_expedite = !rcu_gp_is_expedited() &&
1021 !rcu_gp_is_normal();
1023 rcu_torture_writer_state = RTWS_STUTTER;
1024 stutter_wait("rcu_torture_writer");
1025 } while (!torture_must_stop());
1026 /* Reset expediting back to unexpedited. */
1028 expediting = -expediting;
1029 while (can_expedite && expediting++ < 0)
1030 rcu_unexpedite_gp();
1031 WARN_ON_ONCE(can_expedite && rcu_gp_is_expedited());
1033 pr_alert("%s" TORTURE_FLAG
1034 " Dynamic grace-period expediting was disabled.\n",
1036 rcu_torture_writer_state = RTWS_STOPPING;
1037 torture_kthread_stopping("rcu_torture_writer");
1042 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
1043 * delay between calls.
1046 rcu_torture_fakewriter(void *arg)
1048 DEFINE_TORTURE_RANDOM(rand);
1050 VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
1051 set_user_nice(current, MAX_NICE);
1054 schedule_timeout_uninterruptible(1 + torture_random(&rand)%10);
1055 udelay(torture_random(&rand) & 0x3ff);
1056 if (cur_ops->cb_barrier != NULL &&
1057 torture_random(&rand) % (nfakewriters * 8) == 0) {
1058 cur_ops->cb_barrier();
1059 } else if (gp_normal == gp_exp) {
1060 if (cur_ops->sync && torture_random(&rand) & 0x80)
1062 else if (cur_ops->exp_sync)
1063 cur_ops->exp_sync();
1064 } else if (gp_normal && cur_ops->sync) {
1066 } else if (cur_ops->exp_sync) {
1067 cur_ops->exp_sync();
1069 stutter_wait("rcu_torture_fakewriter");
1070 } while (!torture_must_stop());
1072 torture_kthread_stopping("rcu_torture_fakewriter");
1076 static void rcu_torture_timer_cb(struct rcu_head *rhp)
1082 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
1083 * incrementing the corresponding element of the pipeline array. The
1084 * counter in the element should never be greater than 1, otherwise, the
1085 * RCU implementation is broken.
1087 static void rcu_torture_timer(struct timer_list *unused)
1090 unsigned long started;
1091 unsigned long completed;
1092 static DEFINE_TORTURE_RANDOM(rand);
1093 static DEFINE_SPINLOCK(rand_lock);
1094 struct rcu_torture *p;
1096 unsigned long long ts;
1098 idx = cur_ops->readlock();
1099 started = cur_ops->get_gp_seq();
1100 ts = rcu_trace_clock_local();
1101 p = rcu_dereference_check(rcu_torture_current,
1102 rcu_read_lock_bh_held() ||
1103 rcu_read_lock_sched_held() ||
1104 srcu_read_lock_held(srcu_ctlp) ||
1107 /* Leave because rcu_torture_writer is not yet underway */
1108 cur_ops->readunlock(idx);
1111 if (p->rtort_mbtest == 0)
1112 atomic_inc(&n_rcu_torture_mberror);
1113 spin_lock(&rand_lock);
1114 cur_ops->read_delay(&rand);
1115 n_rcu_torture_timers++;
1116 spin_unlock(&rand_lock);
1118 pipe_count = p->rtort_pipe_count;
1119 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1120 /* Should not happen, but... */
1121 pipe_count = RCU_TORTURE_PIPE_LEN;
1123 completed = cur_ops->get_gp_seq();
1124 if (pipe_count > 1) {
1125 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
1126 started, completed);
1127 rcu_ftrace_dump(DUMP_ALL);
1129 __this_cpu_inc(rcu_torture_count[pipe_count]);
1130 completed = completed - started;
1131 if (completed > ULONG_MAX >> 1)
1132 completed = 0; /* Not all gp_seq have full range. */
1133 if (completed > RCU_TORTURE_PIPE_LEN) {
1134 /* Should not happen, but... */
1135 completed = RCU_TORTURE_PIPE_LEN;
1137 __this_cpu_inc(rcu_torture_batch[completed]);
1139 cur_ops->readunlock(idx);
1141 /* Test call_rcu() invocation from interrupt handler. */
1142 if (cur_ops->call) {
1143 struct rcu_head *rhp = kmalloc(sizeof(*rhp), GFP_NOWAIT);
1146 cur_ops->call(rhp, rcu_torture_timer_cb);
1151 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
1152 * incrementing the corresponding element of the pipeline array. The
1153 * counter in the element should never be greater than 1, otherwise, the
1154 * RCU implementation is broken.
1157 rcu_torture_reader(void *arg)
1159 unsigned long started;
1160 unsigned long completed;
1162 DEFINE_TORTURE_RANDOM(rand);
1163 struct rcu_torture *p;
1165 struct timer_list t;
1166 unsigned long long ts;
1168 VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
1169 set_user_nice(current, MAX_NICE);
1170 if (irqreader && cur_ops->irq_capable)
1171 timer_setup_on_stack(&t, rcu_torture_timer, 0);
1174 if (irqreader && cur_ops->irq_capable) {
1175 if (!timer_pending(&t))
1176 mod_timer(&t, jiffies + 1);
1178 idx = cur_ops->readlock();
1179 started = cur_ops->get_gp_seq();
1180 ts = rcu_trace_clock_local();
1181 p = rcu_dereference_check(rcu_torture_current,
1182 rcu_read_lock_bh_held() ||
1183 rcu_read_lock_sched_held() ||
1184 srcu_read_lock_held(srcu_ctlp) ||
1187 /* Wait for rcu_torture_writer to get underway */
1188 cur_ops->readunlock(idx);
1189 schedule_timeout_interruptible(HZ);
1192 if (p->rtort_mbtest == 0)
1193 atomic_inc(&n_rcu_torture_mberror);
1194 cur_ops->read_delay(&rand);
1196 pipe_count = p->rtort_pipe_count;
1197 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1198 /* Should not happen, but... */
1199 pipe_count = RCU_TORTURE_PIPE_LEN;
1201 completed = cur_ops->get_gp_seq();
1202 if (pipe_count > 1) {
1203 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1204 ts, started, completed);
1205 rcu_ftrace_dump(DUMP_ALL);
1207 __this_cpu_inc(rcu_torture_count[pipe_count]);
1208 completed = completed - started;
1209 if (completed > ULONG_MAX >> 1)
1210 completed = 0; /* Not all gp_seq have full range. */
1211 if (completed > RCU_TORTURE_PIPE_LEN) {
1212 /* Should not happen, but... */
1213 completed = RCU_TORTURE_PIPE_LEN;
1215 __this_cpu_inc(rcu_torture_batch[completed]);
1217 cur_ops->readunlock(idx);
1218 stutter_wait("rcu_torture_reader");
1219 } while (!torture_must_stop());
1220 if (irqreader && cur_ops->irq_capable) {
1222 destroy_timer_on_stack(&t);
1224 torture_kthread_stopping("rcu_torture_reader");
1229 * Print torture statistics. Caller must ensure that there is only
1230 * one call to this function at a given time!!! This is normally
1231 * accomplished by relying on the module system to only have one copy
1232 * of the module loaded, and then by giving the rcu_torture_stats
1233 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1234 * thread is not running).
1237 rcu_torture_stats_print(void)
1241 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1242 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1243 static unsigned long rtcv_snap = ULONG_MAX;
1244 static bool splatted;
1245 struct task_struct *wtp;
1247 for_each_possible_cpu(cpu) {
1248 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1249 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1250 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1253 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1254 if (pipesummary[i] != 0)
1258 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1259 pr_cont("rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
1260 rcu_torture_current,
1261 rcu_torture_current_version,
1262 list_empty(&rcu_torture_freelist),
1263 atomic_read(&n_rcu_torture_alloc),
1264 atomic_read(&n_rcu_torture_alloc_fail),
1265 atomic_read(&n_rcu_torture_free));
1266 pr_cont("rtmbe: %d rtbe: %ld rtbke: %ld rtbre: %ld ",
1267 atomic_read(&n_rcu_torture_mberror),
1268 n_rcu_torture_barrier_error,
1269 n_rcu_torture_boost_ktrerror,
1270 n_rcu_torture_boost_rterror);
1271 pr_cont("rtbf: %ld rtb: %ld nt: %ld ",
1272 n_rcu_torture_boost_failure,
1273 n_rcu_torture_boosts,
1274 n_rcu_torture_timers);
1275 torture_onoff_stats();
1276 pr_cont("barrier: %ld/%ld:%ld ",
1277 n_barrier_successes,
1279 n_rcu_torture_barrier_error);
1280 pr_cont("cbflood: %ld\n", atomic_long_read(&n_cbfloods));
1282 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1283 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
1284 n_rcu_torture_barrier_error != 0 ||
1285 n_rcu_torture_boost_ktrerror != 0 ||
1286 n_rcu_torture_boost_rterror != 0 ||
1287 n_rcu_torture_boost_failure != 0 ||
1289 pr_cont("%s", "!!! ");
1290 atomic_inc(&n_rcu_torture_error);
1293 pr_cont("Reader Pipe: ");
1294 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1295 pr_cont(" %ld", pipesummary[i]);
1298 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1299 pr_cont("Reader Batch: ");
1300 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1301 pr_cont(" %ld", batchsummary[i]);
1304 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1305 pr_cont("Free-Block Circulation: ");
1306 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1307 pr_cont(" %d", atomic_read(&rcu_torture_wcount[i]));
1313 if (rtcv_snap == rcu_torture_current_version &&
1314 rcu_torture_current != NULL) {
1315 int __maybe_unused flags = 0;
1316 unsigned long __maybe_unused gp_seq = 0;
1318 rcutorture_get_gp_data(cur_ops->ttype,
1320 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp,
1322 wtp = READ_ONCE(writer_task);
1323 pr_alert("??? Writer stall state %s(%d) g%lu f%#x ->state %#lx cpu %d\n",
1324 rcu_torture_writer_state_getname(),
1325 rcu_torture_writer_state, gp_seq, flags,
1326 wtp == NULL ? ~0UL : wtp->state,
1327 wtp == NULL ? -1 : (int)task_cpu(wtp));
1328 if (!splatted && wtp) {
1329 sched_show_task(wtp);
1332 show_rcu_gp_kthreads();
1333 rcu_ftrace_dump(DUMP_ALL);
1335 rtcv_snap = rcu_torture_current_version;
1339 * Periodically prints torture statistics, if periodic statistics printing
1340 * was specified via the stat_interval module parameter.
1343 rcu_torture_stats(void *arg)
1345 VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
1347 schedule_timeout_interruptible(stat_interval * HZ);
1348 rcu_torture_stats_print();
1349 torture_shutdown_absorb("rcu_torture_stats");
1350 } while (!torture_must_stop());
1351 torture_kthread_stopping("rcu_torture_stats");
1356 rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
1358 pr_alert("%s" TORTURE_FLAG
1359 "--- %s: nreaders=%d nfakewriters=%d "
1360 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1361 "shuffle_interval=%d stutter=%d irqreader=%d "
1362 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1363 "test_boost=%d/%d test_boost_interval=%d "
1364 "test_boost_duration=%d shutdown_secs=%d "
1365 "stall_cpu=%d stall_cpu_holdoff=%d stall_cpu_irqsoff=%d "
1367 "onoff_interval=%d onoff_holdoff=%d\n",
1368 torture_type, tag, nrealreaders, nfakewriters,
1369 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1370 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1371 test_boost, cur_ops->can_boost,
1372 test_boost_interval, test_boost_duration, shutdown_secs,
1373 stall_cpu, stall_cpu_holdoff, stall_cpu_irqsoff,
1375 onoff_interval, onoff_holdoff);
1378 static int rcutorture_booster_cleanup(unsigned int cpu)
1380 struct task_struct *t;
1382 if (boost_tasks[cpu] == NULL)
1384 mutex_lock(&boost_mutex);
1385 t = boost_tasks[cpu];
1386 boost_tasks[cpu] = NULL;
1387 mutex_unlock(&boost_mutex);
1389 /* This must be outside of the mutex, otherwise deadlock! */
1390 torture_stop_kthread(rcu_torture_boost, t);
1394 static int rcutorture_booster_init(unsigned int cpu)
1398 if (boost_tasks[cpu] != NULL)
1399 return 0; /* Already created, nothing more to do. */
1401 /* Don't allow time recalculation while creating a new task. */
1402 mutex_lock(&boost_mutex);
1403 VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
1404 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1406 "rcu_torture_boost");
1407 if (IS_ERR(boost_tasks[cpu])) {
1408 retval = PTR_ERR(boost_tasks[cpu]);
1409 VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
1410 n_rcu_torture_boost_ktrerror++;
1411 boost_tasks[cpu] = NULL;
1412 mutex_unlock(&boost_mutex);
1415 kthread_bind(boost_tasks[cpu], cpu);
1416 wake_up_process(boost_tasks[cpu]);
1417 mutex_unlock(&boost_mutex);
1422 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1423 * induces a CPU stall for the time specified by stall_cpu.
1425 static int rcu_torture_stall(void *args)
1427 unsigned long stop_at;
1429 VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
1430 if (stall_cpu_holdoff > 0) {
1431 VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
1432 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
1433 VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
1435 if (!kthread_should_stop()) {
1436 stop_at = get_seconds() + stall_cpu;
1437 /* RCU CPU stall is expected behavior in following code. */
1439 if (stall_cpu_irqsoff)
1440 local_irq_disable();
1443 pr_alert("rcu_torture_stall start on CPU %d.\n",
1444 smp_processor_id());
1445 while (ULONG_CMP_LT(get_seconds(), stop_at))
1446 continue; /* Induce RCU CPU stall warning. */
1447 if (stall_cpu_irqsoff)
1452 pr_alert("rcu_torture_stall end.\n");
1454 torture_shutdown_absorb("rcu_torture_stall");
1455 while (!kthread_should_stop())
1456 schedule_timeout_interruptible(10 * HZ);
1460 /* Spawn CPU-stall kthread, if stall_cpu specified. */
1461 static int __init rcu_torture_stall_init(void)
1465 return torture_create_kthread(rcu_torture_stall, NULL, stall_task);
1468 /* Callback function for RCU barrier testing. */
1469 static void rcu_torture_barrier_cbf(struct rcu_head *rcu)
1471 atomic_inc(&barrier_cbs_invoked);
1474 /* kthread function to register callbacks used to test RCU barriers. */
1475 static int rcu_torture_barrier_cbs(void *arg)
1477 long myid = (long)arg;
1480 struct rcu_head rcu;
1482 init_rcu_head_on_stack(&rcu);
1483 VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
1484 set_user_nice(current, MAX_NICE);
1486 wait_event(barrier_cbs_wq[myid],
1488 smp_load_acquire(&barrier_phase)) != lastphase ||
1489 torture_must_stop());
1490 lastphase = newphase;
1491 if (torture_must_stop())
1494 * The above smp_load_acquire() ensures barrier_phase load
1495 * is ordered before the following ->call().
1497 local_irq_disable(); /* Just to test no-irq call_rcu(). */
1498 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1500 if (atomic_dec_and_test(&barrier_cbs_count))
1501 wake_up(&barrier_wq);
1502 } while (!torture_must_stop());
1503 if (cur_ops->cb_barrier != NULL)
1504 cur_ops->cb_barrier();
1505 destroy_rcu_head_on_stack(&rcu);
1506 torture_kthread_stopping("rcu_torture_barrier_cbs");
1510 /* kthread function to drive and coordinate RCU barrier testing. */
1511 static int rcu_torture_barrier(void *arg)
1515 VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
1517 atomic_set(&barrier_cbs_invoked, 0);
1518 atomic_set(&barrier_cbs_count, n_barrier_cbs);
1519 /* Ensure barrier_phase ordered after prior assignments. */
1520 smp_store_release(&barrier_phase, !barrier_phase);
1521 for (i = 0; i < n_barrier_cbs; i++)
1522 wake_up(&barrier_cbs_wq[i]);
1523 wait_event(barrier_wq,
1524 atomic_read(&barrier_cbs_count) == 0 ||
1525 torture_must_stop());
1526 if (torture_must_stop())
1528 n_barrier_attempts++;
1529 cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
1530 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1531 n_rcu_torture_barrier_error++;
1532 pr_err("barrier_cbs_invoked = %d, n_barrier_cbs = %d\n",
1533 atomic_read(&barrier_cbs_invoked),
1537 n_barrier_successes++;
1538 schedule_timeout_interruptible(HZ / 10);
1539 } while (!torture_must_stop());
1540 torture_kthread_stopping("rcu_torture_barrier");
1544 /* Initialize RCU barrier testing. */
1545 static int rcu_torture_barrier_init(void)
1550 if (n_barrier_cbs <= 0)
1552 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
1553 pr_alert("%s" TORTURE_FLAG
1554 " Call or barrier ops missing for %s,\n",
1555 torture_type, cur_ops->name);
1556 pr_alert("%s" TORTURE_FLAG
1557 " RCU barrier testing omitted from run.\n",
1561 atomic_set(&barrier_cbs_count, 0);
1562 atomic_set(&barrier_cbs_invoked, 0);
1564 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_tasks[0]),
1567 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_wq[0]), GFP_KERNEL);
1568 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
1570 for (i = 0; i < n_barrier_cbs; i++) {
1571 init_waitqueue_head(&barrier_cbs_wq[i]);
1572 ret = torture_create_kthread(rcu_torture_barrier_cbs,
1574 barrier_cbs_tasks[i]);
1578 return torture_create_kthread(rcu_torture_barrier, NULL, barrier_task);
1581 /* Clean up after RCU barrier testing. */
1582 static void rcu_torture_barrier_cleanup(void)
1586 torture_stop_kthread(rcu_torture_barrier, barrier_task);
1587 if (barrier_cbs_tasks != NULL) {
1588 for (i = 0; i < n_barrier_cbs; i++)
1589 torture_stop_kthread(rcu_torture_barrier_cbs,
1590 barrier_cbs_tasks[i]);
1591 kfree(barrier_cbs_tasks);
1592 barrier_cbs_tasks = NULL;
1594 if (barrier_cbs_wq != NULL) {
1595 kfree(barrier_cbs_wq);
1596 barrier_cbs_wq = NULL;
1600 static enum cpuhp_state rcutor_hp;
1603 rcu_torture_cleanup(void)
1606 unsigned long gp_seq = 0;
1609 rcutorture_record_test_transition();
1610 if (torture_cleanup_begin()) {
1611 if (cur_ops->cb_barrier != NULL)
1612 cur_ops->cb_barrier();
1616 rcu_torture_barrier_cleanup();
1617 torture_stop_kthread(rcu_torture_stall, stall_task);
1618 torture_stop_kthread(rcu_torture_writer, writer_task);
1621 for (i = 0; i < nrealreaders; i++)
1622 torture_stop_kthread(rcu_torture_reader,
1624 kfree(reader_tasks);
1626 rcu_torture_current = NULL;
1628 if (fakewriter_tasks) {
1629 for (i = 0; i < nfakewriters; i++) {
1630 torture_stop_kthread(rcu_torture_fakewriter,
1631 fakewriter_tasks[i]);
1633 kfree(fakewriter_tasks);
1634 fakewriter_tasks = NULL;
1637 rcutorture_get_gp_data(cur_ops->ttype, &flags, &gp_seq);
1638 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp, &flags, &gp_seq);
1639 pr_alert("%s: End-test grace-period state: g%lu f%#x\n",
1640 cur_ops->name, gp_seq, flags);
1641 torture_stop_kthread(rcu_torture_stats, stats_task);
1642 torture_stop_kthread(rcu_torture_fqs, fqs_task);
1643 for (i = 0; i < ncbflooders; i++)
1644 torture_stop_kthread(rcu_torture_cbflood, cbflood_task[i]);
1645 if ((test_boost == 1 && cur_ops->can_boost) ||
1647 cpuhp_remove_state(rcutor_hp);
1650 * Wait for all RCU callbacks to fire, then do flavor-specific
1651 * cleanup operations.
1653 if (cur_ops->cb_barrier != NULL)
1654 cur_ops->cb_barrier();
1655 if (cur_ops->cleanup != NULL)
1658 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
1660 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
1661 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
1662 else if (torture_onoff_failures())
1663 rcu_torture_print_module_parms(cur_ops,
1664 "End of test: RCU_HOTPLUG");
1666 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
1667 torture_cleanup_end();
1670 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1671 static void rcu_torture_leak_cb(struct rcu_head *rhp)
1675 static void rcu_torture_err_cb(struct rcu_head *rhp)
1678 * This -might- happen due to race conditions, but is unlikely.
1679 * The scenario that leads to this happening is that the
1680 * first of the pair of duplicate callbacks is queued,
1681 * someone else starts a grace period that includes that
1682 * callback, then the second of the pair must wait for the
1683 * next grace period. Unlikely, but can happen. If it
1684 * does happen, the debug-objects subsystem won't have splatted.
1686 pr_alert("%s: duplicated callback was invoked.\n", KBUILD_MODNAME);
1688 #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1691 * Verify that double-free causes debug-objects to complain, but only
1692 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
1693 * cannot be carried out.
1695 static void rcu_test_debug_objects(void)
1697 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1698 struct rcu_head rh1;
1699 struct rcu_head rh2;
1701 init_rcu_head_on_stack(&rh1);
1702 init_rcu_head_on_stack(&rh2);
1703 pr_alert("%s: WARN: Duplicate call_rcu() test starting.\n", KBUILD_MODNAME);
1705 /* Try to queue the rh2 pair of callbacks for the same grace period. */
1706 preempt_disable(); /* Prevent preemption from interrupting test. */
1707 rcu_read_lock(); /* Make it impossible to finish a grace period. */
1708 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
1709 local_irq_disable(); /* Make it harder to start a new grace period. */
1710 call_rcu(&rh2, rcu_torture_leak_cb);
1711 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
1716 /* Wait for them all to get done so we can safely return. */
1718 pr_alert("%s: WARN: Duplicate call_rcu() test complete.\n", KBUILD_MODNAME);
1719 destroy_rcu_head_on_stack(&rh1);
1720 destroy_rcu_head_on_stack(&rh2);
1721 #else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1722 pr_alert("%s: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n", KBUILD_MODNAME);
1723 #endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1727 rcu_torture_init(void)
1732 static struct rcu_torture_ops *torture_ops[] = {
1733 &rcu_ops, &rcu_bh_ops, &rcu_busted_ops, &srcu_ops, &srcud_ops,
1734 &sched_ops, &tasks_ops,
1737 if (!torture_init_begin(torture_type, verbose))
1740 /* Process args and tell the world that the torturer is on the job. */
1741 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1742 cur_ops = torture_ops[i];
1743 if (strcmp(torture_type, cur_ops->name) == 0)
1746 if (i == ARRAY_SIZE(torture_ops)) {
1747 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
1749 pr_alert("rcu-torture types:");
1750 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
1751 pr_alert(" %s", torture_ops[i]->name);
1756 if (cur_ops->fqs == NULL && fqs_duration != 0) {
1757 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
1763 if (nreaders >= 0) {
1764 nrealreaders = nreaders;
1766 nrealreaders = num_online_cpus() - 2 - nreaders;
1767 if (nrealreaders <= 0)
1770 rcu_torture_print_module_parms(cur_ops, "Start of test");
1772 /* Set up the freelist. */
1774 INIT_LIST_HEAD(&rcu_torture_freelist);
1775 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1776 rcu_tortures[i].rtort_mbtest = 0;
1777 list_add_tail(&rcu_tortures[i].rtort_free,
1778 &rcu_torture_freelist);
1781 /* Initialize the statistics so that each run gets its own numbers. */
1783 rcu_torture_current = NULL;
1784 rcu_torture_current_version = 0;
1785 atomic_set(&n_rcu_torture_alloc, 0);
1786 atomic_set(&n_rcu_torture_alloc_fail, 0);
1787 atomic_set(&n_rcu_torture_free, 0);
1788 atomic_set(&n_rcu_torture_mberror, 0);
1789 atomic_set(&n_rcu_torture_error, 0);
1790 n_rcu_torture_barrier_error = 0;
1791 n_rcu_torture_boost_ktrerror = 0;
1792 n_rcu_torture_boost_rterror = 0;
1793 n_rcu_torture_boost_failure = 0;
1794 n_rcu_torture_boosts = 0;
1795 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1796 atomic_set(&rcu_torture_wcount[i], 0);
1797 for_each_possible_cpu(cpu) {
1798 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1799 per_cpu(rcu_torture_count, cpu)[i] = 0;
1800 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1804 /* Start up the kthreads. */
1806 firsterr = torture_create_kthread(rcu_torture_writer, NULL,
1810 if (nfakewriters > 0) {
1811 fakewriter_tasks = kcalloc(nfakewriters,
1812 sizeof(fakewriter_tasks[0]),
1814 if (fakewriter_tasks == NULL) {
1815 VERBOSE_TOROUT_ERRSTRING("out of memory");
1820 for (i = 0; i < nfakewriters; i++) {
1821 firsterr = torture_create_kthread(rcu_torture_fakewriter,
1822 NULL, fakewriter_tasks[i]);
1826 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
1828 if (reader_tasks == NULL) {
1829 VERBOSE_TOROUT_ERRSTRING("out of memory");
1833 for (i = 0; i < nrealreaders; i++) {
1834 firsterr = torture_create_kthread(rcu_torture_reader, NULL,
1839 if (stat_interval > 0) {
1840 firsterr = torture_create_kthread(rcu_torture_stats, NULL,
1845 if (test_no_idle_hz && shuffle_interval > 0) {
1846 firsterr = torture_shuffle_init(shuffle_interval * HZ);
1853 firsterr = torture_stutter_init(stutter * HZ);
1857 if (fqs_duration < 0)
1860 /* Create the fqs thread */
1861 firsterr = torture_create_kthread(rcu_torture_fqs, NULL,
1866 if (test_boost_interval < 1)
1867 test_boost_interval = 1;
1868 if (test_boost_duration < 2)
1869 test_boost_duration = 2;
1870 if ((test_boost == 1 && cur_ops->can_boost) ||
1873 boost_starttime = jiffies + test_boost_interval * HZ;
1875 firsterr = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "RCU_TORTURE",
1876 rcutorture_booster_init,
1877 rcutorture_booster_cleanup);
1880 rcutor_hp = firsterr;
1882 firsterr = torture_shutdown_init(shutdown_secs, rcu_torture_cleanup);
1885 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval * HZ);
1888 firsterr = rcu_torture_stall_init();
1891 firsterr = rcu_torture_barrier_init();
1895 rcu_test_debug_objects();
1896 if (cbflood_n_burst > 0) {
1897 /* Create the cbflood threads */
1898 ncbflooders = (num_online_cpus() + 3) / 4;
1899 cbflood_task = kcalloc(ncbflooders, sizeof(*cbflood_task),
1901 if (!cbflood_task) {
1902 VERBOSE_TOROUT_ERRSTRING("out of memory");
1906 for (i = 0; i < ncbflooders; i++) {
1907 firsterr = torture_create_kthread(rcu_torture_cbflood,
1914 rcutorture_record_test_transition();
1920 rcu_torture_cleanup();
1924 module_init(rcu_torture_init);
1925 module_exit(rcu_torture_cleanup);