Add linux-next specific files for 20110801
[linux-2.6/next.git] / kernel / rcutorture.c
blobee60b76d05f95c06b20a0711bdae321c6db91498
1 /*
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, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) IBM Corporation, 2005, 2006
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
21 * Josh Triplett <josh@freedesktop.org>
23 * See also: Documentation/RCU/torture.txt
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/export.h>
29 #include <linux/kthread.h>
30 #include <linux/err.h>
31 #include <linux/spinlock.h>
32 #include <linux/smp.h>
33 #include <linux/rcupdate.h>
34 #include <linux/interrupt.h>
35 #include <linux/module.h>
36 #include <linux/sched.h>
37 #include <linux/atomic.h>
38 #include <linux/bitops.h>
39 #include <linux/completion.h>
40 #include <linux/moduleparam.h>
41 #include <linux/percpu.h>
42 #include <linux/notifier.h>
43 #include <linux/reboot.h>
44 #include <linux/freezer.h>
45 #include <linux/cpu.h>
46 #include <linux/delay.h>
47 #include <linux/stat.h>
48 #include <linux/srcu.h>
49 #include <linux/slab.h>
50 #include <asm/byteorder.h>
52 MODULE_LICENSE("GPL");
53 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
54 "Josh Triplett <josh@freedesktop.org>");
56 static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
57 static int nfakewriters = 4; /* # fake writer threads */
58 static int stat_interval; /* Interval between stats, in seconds. */
59 /* Defaults to "only at end of test". */
60 static int verbose; /* Print more debug info. */
61 static int test_no_idle_hz; /* Test RCU's support for tickless idle CPUs. */
62 static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
63 static int stutter = 5; /* Start/stop testing interval (in sec) */
64 static int irqreader = 1; /* RCU readers from irq (timers). */
65 static int fqs_duration = 0; /* Duration of bursts (us), 0 to disable. */
66 static int fqs_holdoff = 0; /* Hold time within burst (us). */
67 static int fqs_stutter = 3; /* Wait time between bursts (s). */
68 static int test_boost = 1; /* Test RCU prio boost: 0=no, 1=maybe, 2=yes. */
69 static int test_boost_interval = 7; /* Interval between boost tests, seconds. */
70 static int test_boost_duration = 4; /* Duration of each boost test, seconds. */
71 static char *torture_type = "rcu"; /* What RCU implementation to torture. */
73 module_param(nreaders, int, 0444);
74 MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
75 module_param(nfakewriters, int, 0444);
76 MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
77 module_param(stat_interval, int, 0444);
78 MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
79 module_param(verbose, bool, 0444);
80 MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
81 module_param(test_no_idle_hz, bool, 0444);
82 MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
83 module_param(shuffle_interval, int, 0444);
84 MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
85 module_param(stutter, int, 0444);
86 MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
87 module_param(irqreader, int, 0444);
88 MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
89 module_param(fqs_duration, int, 0444);
90 MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
91 module_param(fqs_holdoff, int, 0444);
92 MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
93 module_param(fqs_stutter, int, 0444);
94 MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
95 module_param(test_boost, int, 0444);
96 MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
97 module_param(test_boost_interval, int, 0444);
98 MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
99 module_param(test_boost_duration, int, 0444);
100 MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
101 module_param(torture_type, charp, 0444);
102 MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
104 #define TORTURE_FLAG "-torture:"
105 #define PRINTK_STRING(s) \
106 do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
107 #define VERBOSE_PRINTK_STRING(s) \
108 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
109 #define VERBOSE_PRINTK_ERRSTRING(s) \
110 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
112 static char printk_buf[4096];
114 static int nrealreaders;
115 static struct task_struct *writer_task;
116 static struct task_struct **fakewriter_tasks;
117 static struct task_struct **reader_tasks;
118 static struct task_struct *stats_task;
119 static struct task_struct *shuffler_task;
120 static struct task_struct *stutter_task;
121 static struct task_struct *fqs_task;
122 static struct task_struct *boost_tasks[NR_CPUS];
124 #define RCU_TORTURE_PIPE_LEN 10
126 struct rcu_torture {
127 struct rcu_head rtort_rcu;
128 int rtort_pipe_count;
129 struct list_head rtort_free;
130 int rtort_mbtest;
133 static LIST_HEAD(rcu_torture_freelist);
134 static struct rcu_torture __rcu *rcu_torture_current;
135 static unsigned long rcu_torture_current_version;
136 static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
137 static DEFINE_SPINLOCK(rcu_torture_lock);
138 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
139 { 0 };
140 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
141 { 0 };
142 static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
143 static atomic_t n_rcu_torture_alloc;
144 static atomic_t n_rcu_torture_alloc_fail;
145 static atomic_t n_rcu_torture_free;
146 static atomic_t n_rcu_torture_mberror;
147 static atomic_t n_rcu_torture_error;
148 static long n_rcu_torture_boost_ktrerror;
149 static long n_rcu_torture_boost_rterror;
150 static long n_rcu_torture_boost_failure;
151 static long n_rcu_torture_boosts;
152 static long n_rcu_torture_timers;
153 static struct list_head rcu_torture_removed;
154 static cpumask_var_t shuffle_tmp_mask;
156 static int stutter_pause_test;
158 #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
159 #define RCUTORTURE_RUNNABLE_INIT 1
160 #else
161 #define RCUTORTURE_RUNNABLE_INIT 0
162 #endif
163 int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
165 #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
166 #define rcu_can_boost() 1
167 #else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
168 #define rcu_can_boost() 0
169 #endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
171 static unsigned long boost_starttime; /* jiffies of next boost test start. */
172 DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
173 /* and boost task create/destroy. */
175 /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
177 #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
178 #define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
179 #define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
180 static int fullstop = FULLSTOP_RMMOD;
182 * Protect fullstop transitions and spawning of kthreads.
184 static DEFINE_MUTEX(fullstop_mutex);
187 * Detect and respond to a system shutdown.
189 static int
190 rcutorture_shutdown_notify(struct notifier_block *unused1,
191 unsigned long unused2, void *unused3)
193 mutex_lock(&fullstop_mutex);
194 if (fullstop == FULLSTOP_DONTSTOP)
195 fullstop = FULLSTOP_SHUTDOWN;
196 else
197 printk(KERN_WARNING /* but going down anyway, so... */
198 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
199 mutex_unlock(&fullstop_mutex);
200 return NOTIFY_DONE;
204 * Absorb kthreads into a kernel function that won't return, so that
205 * they won't ever access module text or data again.
207 static void rcutorture_shutdown_absorb(char *title)
209 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
210 printk(KERN_NOTICE
211 "rcutorture thread %s parking due to system shutdown\n",
212 title);
213 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
218 * Allocate an element from the rcu_tortures pool.
220 static struct rcu_torture *
221 rcu_torture_alloc(void)
223 struct list_head *p;
225 spin_lock_bh(&rcu_torture_lock);
226 if (list_empty(&rcu_torture_freelist)) {
227 atomic_inc(&n_rcu_torture_alloc_fail);
228 spin_unlock_bh(&rcu_torture_lock);
229 return NULL;
231 atomic_inc(&n_rcu_torture_alloc);
232 p = rcu_torture_freelist.next;
233 list_del_init(p);
234 spin_unlock_bh(&rcu_torture_lock);
235 return container_of(p, struct rcu_torture, rtort_free);
239 * Free an element to the rcu_tortures pool.
241 static void
242 rcu_torture_free(struct rcu_torture *p)
244 atomic_inc(&n_rcu_torture_free);
245 spin_lock_bh(&rcu_torture_lock);
246 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
247 spin_unlock_bh(&rcu_torture_lock);
250 struct rcu_random_state {
251 unsigned long rrs_state;
252 long rrs_count;
255 #define RCU_RANDOM_MULT 39916801 /* prime */
256 #define RCU_RANDOM_ADD 479001701 /* prime */
257 #define RCU_RANDOM_REFRESH 10000
259 #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
262 * Crude but fast random-number generator. Uses a linear congruential
263 * generator, with occasional help from cpu_clock().
265 static unsigned long
266 rcu_random(struct rcu_random_state *rrsp)
268 if (--rrsp->rrs_count < 0) {
269 rrsp->rrs_state += (unsigned long)local_clock();
270 rrsp->rrs_count = RCU_RANDOM_REFRESH;
272 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
273 return swahw32(rrsp->rrs_state);
276 static void
277 rcu_stutter_wait(char *title)
279 while (stutter_pause_test || !rcutorture_runnable) {
280 if (rcutorture_runnable)
281 schedule_timeout_interruptible(1);
282 else
283 schedule_timeout_interruptible(round_jiffies_relative(HZ));
284 rcutorture_shutdown_absorb(title);
289 * Operations vector for selecting different types of tests.
292 struct rcu_torture_ops {
293 void (*init)(void);
294 void (*cleanup)(void);
295 int (*readlock)(void);
296 void (*read_delay)(struct rcu_random_state *rrsp);
297 void (*readunlock)(int idx);
298 int (*completed)(void);
299 void (*deferred_free)(struct rcu_torture *p);
300 void (*sync)(void);
301 void (*cb_barrier)(void);
302 void (*fqs)(void);
303 int (*stats)(char *page);
304 int irq_capable;
305 int can_boost;
306 char *name;
309 static struct rcu_torture_ops *cur_ops;
312 * Definitions for rcu torture testing.
315 static int rcu_torture_read_lock(void) __acquires(RCU)
317 rcu_read_lock();
318 return 0;
321 static void rcu_read_delay(struct rcu_random_state *rrsp)
323 const unsigned long shortdelay_us = 200;
324 const unsigned long longdelay_ms = 50;
326 /* We want a short delay sometimes to make a reader delay the grace
327 * period, and we want a long delay occasionally to trigger
328 * force_quiescent_state. */
330 if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
331 mdelay(longdelay_ms);
332 if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
333 udelay(shortdelay_us);
334 #ifdef CONFIG_PREEMPT
335 if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
336 preempt_schedule(); /* No QS if preempt_disable() in effect */
337 #endif
340 static void rcu_torture_read_unlock(int idx) __releases(RCU)
342 rcu_read_unlock();
345 static int rcu_torture_completed(void)
347 return rcu_batches_completed();
350 static void
351 rcu_torture_cb(struct rcu_head *p)
353 int i;
354 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
356 if (fullstop != FULLSTOP_DONTSTOP) {
357 /* Test is ending, just drop callbacks on the floor. */
358 /* The next initialization will pick up the pieces. */
359 return;
361 i = rp->rtort_pipe_count;
362 if (i > RCU_TORTURE_PIPE_LEN)
363 i = RCU_TORTURE_PIPE_LEN;
364 atomic_inc(&rcu_torture_wcount[i]);
365 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
366 rp->rtort_mbtest = 0;
367 rcu_torture_free(rp);
368 } else
369 cur_ops->deferred_free(rp);
372 static int rcu_no_completed(void)
374 return 0;
377 static void rcu_torture_deferred_free(struct rcu_torture *p)
379 call_rcu(&p->rtort_rcu, rcu_torture_cb);
382 static struct rcu_torture_ops rcu_ops = {
383 .init = NULL,
384 .cleanup = NULL,
385 .readlock = rcu_torture_read_lock,
386 .read_delay = rcu_read_delay,
387 .readunlock = rcu_torture_read_unlock,
388 .completed = rcu_torture_completed,
389 .deferred_free = rcu_torture_deferred_free,
390 .sync = synchronize_rcu,
391 .cb_barrier = rcu_barrier,
392 .fqs = rcu_force_quiescent_state,
393 .stats = NULL,
394 .irq_capable = 1,
395 .can_boost = rcu_can_boost(),
396 .name = "rcu"
399 static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
401 int i;
402 struct rcu_torture *rp;
403 struct rcu_torture *rp1;
405 cur_ops->sync();
406 list_add(&p->rtort_free, &rcu_torture_removed);
407 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
408 i = rp->rtort_pipe_count;
409 if (i > RCU_TORTURE_PIPE_LEN)
410 i = RCU_TORTURE_PIPE_LEN;
411 atomic_inc(&rcu_torture_wcount[i]);
412 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
413 rp->rtort_mbtest = 0;
414 list_del(&rp->rtort_free);
415 rcu_torture_free(rp);
420 static void rcu_sync_torture_init(void)
422 INIT_LIST_HEAD(&rcu_torture_removed);
425 static struct rcu_torture_ops rcu_sync_ops = {
426 .init = rcu_sync_torture_init,
427 .cleanup = NULL,
428 .readlock = rcu_torture_read_lock,
429 .read_delay = rcu_read_delay,
430 .readunlock = rcu_torture_read_unlock,
431 .completed = rcu_torture_completed,
432 .deferred_free = rcu_sync_torture_deferred_free,
433 .sync = synchronize_rcu,
434 .cb_barrier = NULL,
435 .fqs = rcu_force_quiescent_state,
436 .stats = NULL,
437 .irq_capable = 1,
438 .can_boost = rcu_can_boost(),
439 .name = "rcu_sync"
442 static struct rcu_torture_ops rcu_expedited_ops = {
443 .init = rcu_sync_torture_init,
444 .cleanup = NULL,
445 .readlock = rcu_torture_read_lock,
446 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
447 .readunlock = rcu_torture_read_unlock,
448 .completed = rcu_no_completed,
449 .deferred_free = rcu_sync_torture_deferred_free,
450 .sync = synchronize_rcu_expedited,
451 .cb_barrier = NULL,
452 .fqs = rcu_force_quiescent_state,
453 .stats = NULL,
454 .irq_capable = 1,
455 .can_boost = rcu_can_boost(),
456 .name = "rcu_expedited"
460 * Definitions for rcu_bh torture testing.
463 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
465 rcu_read_lock_bh();
466 return 0;
469 static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
471 rcu_read_unlock_bh();
474 static int rcu_bh_torture_completed(void)
476 return rcu_batches_completed_bh();
479 static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
481 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
484 struct rcu_bh_torture_synchronize {
485 struct rcu_head head;
486 struct completion completion;
489 static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
491 struct rcu_bh_torture_synchronize *rcu;
493 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
494 complete(&rcu->completion);
497 static void rcu_bh_torture_synchronize(void)
499 struct rcu_bh_torture_synchronize rcu;
501 init_rcu_head_on_stack(&rcu.head);
502 init_completion(&rcu.completion);
503 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
504 wait_for_completion(&rcu.completion);
505 destroy_rcu_head_on_stack(&rcu.head);
508 static struct rcu_torture_ops rcu_bh_ops = {
509 .init = NULL,
510 .cleanup = NULL,
511 .readlock = rcu_bh_torture_read_lock,
512 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
513 .readunlock = rcu_bh_torture_read_unlock,
514 .completed = rcu_bh_torture_completed,
515 .deferred_free = rcu_bh_torture_deferred_free,
516 .sync = rcu_bh_torture_synchronize,
517 .cb_barrier = rcu_barrier_bh,
518 .fqs = rcu_bh_force_quiescent_state,
519 .stats = NULL,
520 .irq_capable = 1,
521 .name = "rcu_bh"
524 static struct rcu_torture_ops rcu_bh_sync_ops = {
525 .init = rcu_sync_torture_init,
526 .cleanup = NULL,
527 .readlock = rcu_bh_torture_read_lock,
528 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
529 .readunlock = rcu_bh_torture_read_unlock,
530 .completed = rcu_bh_torture_completed,
531 .deferred_free = rcu_sync_torture_deferred_free,
532 .sync = rcu_bh_torture_synchronize,
533 .cb_barrier = NULL,
534 .fqs = rcu_bh_force_quiescent_state,
535 .stats = NULL,
536 .irq_capable = 1,
537 .name = "rcu_bh_sync"
541 * Definitions for srcu torture testing.
544 static struct srcu_struct srcu_ctl;
546 static void srcu_torture_init(void)
548 init_srcu_struct(&srcu_ctl);
549 rcu_sync_torture_init();
552 static void srcu_torture_cleanup(void)
554 synchronize_srcu(&srcu_ctl);
555 cleanup_srcu_struct(&srcu_ctl);
558 static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
560 return srcu_read_lock(&srcu_ctl);
563 static void srcu_read_delay(struct rcu_random_state *rrsp)
565 long delay;
566 const long uspertick = 1000000 / HZ;
567 const long longdelay = 10;
569 /* We want there to be long-running readers, but not all the time. */
571 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
572 if (!delay)
573 schedule_timeout_interruptible(longdelay);
574 else
575 rcu_read_delay(rrsp);
578 static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
580 srcu_read_unlock(&srcu_ctl, idx);
583 static int srcu_torture_completed(void)
585 return srcu_batches_completed(&srcu_ctl);
588 static void srcu_torture_synchronize(void)
590 synchronize_srcu(&srcu_ctl);
593 static int srcu_torture_stats(char *page)
595 int cnt = 0;
596 int cpu;
597 int idx = srcu_ctl.completed & 0x1;
599 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
600 torture_type, TORTURE_FLAG, idx);
601 for_each_possible_cpu(cpu) {
602 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
603 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
604 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
606 cnt += sprintf(&page[cnt], "\n");
607 return cnt;
610 static struct rcu_torture_ops srcu_ops = {
611 .init = srcu_torture_init,
612 .cleanup = srcu_torture_cleanup,
613 .readlock = srcu_torture_read_lock,
614 .read_delay = srcu_read_delay,
615 .readunlock = srcu_torture_read_unlock,
616 .completed = srcu_torture_completed,
617 .deferred_free = rcu_sync_torture_deferred_free,
618 .sync = srcu_torture_synchronize,
619 .cb_barrier = NULL,
620 .stats = srcu_torture_stats,
621 .name = "srcu"
624 static void srcu_torture_synchronize_expedited(void)
626 synchronize_srcu_expedited(&srcu_ctl);
629 static struct rcu_torture_ops srcu_expedited_ops = {
630 .init = srcu_torture_init,
631 .cleanup = srcu_torture_cleanup,
632 .readlock = srcu_torture_read_lock,
633 .read_delay = srcu_read_delay,
634 .readunlock = srcu_torture_read_unlock,
635 .completed = srcu_torture_completed,
636 .deferred_free = rcu_sync_torture_deferred_free,
637 .sync = srcu_torture_synchronize_expedited,
638 .cb_barrier = NULL,
639 .stats = srcu_torture_stats,
640 .name = "srcu_expedited"
644 * Definitions for sched torture testing.
647 static int sched_torture_read_lock(void)
649 preempt_disable();
650 return 0;
653 static void sched_torture_read_unlock(int idx)
655 preempt_enable();
658 static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
660 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
663 static void sched_torture_synchronize(void)
665 synchronize_sched();
668 static struct rcu_torture_ops sched_ops = {
669 .init = rcu_sync_torture_init,
670 .cleanup = NULL,
671 .readlock = sched_torture_read_lock,
672 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
673 .readunlock = sched_torture_read_unlock,
674 .completed = rcu_no_completed,
675 .deferred_free = rcu_sched_torture_deferred_free,
676 .sync = sched_torture_synchronize,
677 .cb_barrier = rcu_barrier_sched,
678 .fqs = rcu_sched_force_quiescent_state,
679 .stats = NULL,
680 .irq_capable = 1,
681 .name = "sched"
684 static struct rcu_torture_ops sched_sync_ops = {
685 .init = rcu_sync_torture_init,
686 .cleanup = NULL,
687 .readlock = sched_torture_read_lock,
688 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
689 .readunlock = sched_torture_read_unlock,
690 .completed = rcu_no_completed,
691 .deferred_free = rcu_sync_torture_deferred_free,
692 .sync = sched_torture_synchronize,
693 .cb_barrier = NULL,
694 .fqs = rcu_sched_force_quiescent_state,
695 .stats = NULL,
696 .name = "sched_sync"
699 static struct rcu_torture_ops sched_expedited_ops = {
700 .init = rcu_sync_torture_init,
701 .cleanup = NULL,
702 .readlock = sched_torture_read_lock,
703 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
704 .readunlock = sched_torture_read_unlock,
705 .completed = rcu_no_completed,
706 .deferred_free = rcu_sync_torture_deferred_free,
707 .sync = synchronize_sched_expedited,
708 .cb_barrier = NULL,
709 .fqs = rcu_sched_force_quiescent_state,
710 .stats = NULL,
711 .irq_capable = 1,
712 .name = "sched_expedited"
716 * RCU torture priority-boost testing. Runs one real-time thread per
717 * CPU for moderate bursts, repeatedly registering RCU callbacks and
718 * spinning waiting for them to be invoked. If a given callback takes
719 * too long to be invoked, we assume that priority inversion has occurred.
722 struct rcu_boost_inflight {
723 struct rcu_head rcu;
724 int inflight;
727 static void rcu_torture_boost_cb(struct rcu_head *head)
729 struct rcu_boost_inflight *rbip =
730 container_of(head, struct rcu_boost_inflight, rcu);
732 smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
733 rbip->inflight = 0;
736 static int rcu_torture_boost(void *arg)
738 unsigned long call_rcu_time;
739 unsigned long endtime;
740 unsigned long oldstarttime;
741 struct rcu_boost_inflight rbi = { .inflight = 0 };
742 struct sched_param sp;
744 VERBOSE_PRINTK_STRING("rcu_torture_boost started");
746 /* Set real-time priority. */
747 sp.sched_priority = 1;
748 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
749 VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
750 n_rcu_torture_boost_rterror++;
753 init_rcu_head_on_stack(&rbi.rcu);
754 /* Each pass through the following loop does one boost-test cycle. */
755 do {
756 /* Wait for the next test interval. */
757 oldstarttime = boost_starttime;
758 while (jiffies - oldstarttime > ULONG_MAX / 2) {
759 schedule_timeout_uninterruptible(1);
760 rcu_stutter_wait("rcu_torture_boost");
761 if (kthread_should_stop() ||
762 fullstop != FULLSTOP_DONTSTOP)
763 goto checkwait;
766 /* Do one boost-test interval. */
767 endtime = oldstarttime + test_boost_duration * HZ;
768 call_rcu_time = jiffies;
769 while (jiffies - endtime > ULONG_MAX / 2) {
770 /* If we don't have a callback in flight, post one. */
771 if (!rbi.inflight) {
772 smp_mb(); /* RCU core before ->inflight = 1. */
773 rbi.inflight = 1;
774 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
775 if (jiffies - call_rcu_time >
776 test_boost_duration * HZ - HZ / 2) {
777 VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
778 n_rcu_torture_boost_failure++;
780 call_rcu_time = jiffies;
782 cond_resched();
783 rcu_stutter_wait("rcu_torture_boost");
784 if (kthread_should_stop() ||
785 fullstop != FULLSTOP_DONTSTOP)
786 goto checkwait;
790 * Set the start time of the next test interval.
791 * Yes, this is vulnerable to long delays, but such
792 * delays simply cause a false negative for the next
793 * interval. Besides, we are running at RT priority,
794 * so delays should be relatively rare.
796 while (oldstarttime == boost_starttime) {
797 if (mutex_trylock(&boost_mutex)) {
798 boost_starttime = jiffies +
799 test_boost_interval * HZ;
800 n_rcu_torture_boosts++;
801 mutex_unlock(&boost_mutex);
802 break;
804 schedule_timeout_uninterruptible(1);
807 /* Go do the stutter. */
808 checkwait: rcu_stutter_wait("rcu_torture_boost");
809 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
811 /* Clean up and exit. */
812 VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
813 destroy_rcu_head_on_stack(&rbi.rcu);
814 rcutorture_shutdown_absorb("rcu_torture_boost");
815 while (!kthread_should_stop() || rbi.inflight)
816 schedule_timeout_uninterruptible(1);
817 smp_mb(); /* order accesses to ->inflight before stack-frame death. */
818 return 0;
822 * RCU torture force-quiescent-state kthread. Repeatedly induces
823 * bursts of calls to force_quiescent_state(), increasing the probability
824 * of occurrence of some important types of race conditions.
826 static int
827 rcu_torture_fqs(void *arg)
829 unsigned long fqs_resume_time;
830 int fqs_burst_remaining;
832 VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
833 do {
834 fqs_resume_time = jiffies + fqs_stutter * HZ;
835 while (jiffies - fqs_resume_time > LONG_MAX) {
836 schedule_timeout_interruptible(1);
838 fqs_burst_remaining = fqs_duration;
839 while (fqs_burst_remaining > 0) {
840 cur_ops->fqs();
841 udelay(fqs_holdoff);
842 fqs_burst_remaining -= fqs_holdoff;
844 rcu_stutter_wait("rcu_torture_fqs");
845 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
846 VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
847 rcutorture_shutdown_absorb("rcu_torture_fqs");
848 while (!kthread_should_stop())
849 schedule_timeout_uninterruptible(1);
850 return 0;
854 * RCU torture writer kthread. Repeatedly substitutes a new structure
855 * for that pointed to by rcu_torture_current, freeing the old structure
856 * after a series of grace periods (the "pipeline").
858 static int
859 rcu_torture_writer(void *arg)
861 int i;
862 long oldbatch = rcu_batches_completed();
863 struct rcu_torture *rp;
864 struct rcu_torture *old_rp;
865 static DEFINE_RCU_RANDOM(rand);
867 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
868 set_user_nice(current, 19);
870 do {
871 schedule_timeout_uninterruptible(1);
872 rp = rcu_torture_alloc();
873 if (rp == NULL)
874 continue;
875 rp->rtort_pipe_count = 0;
876 udelay(rcu_random(&rand) & 0x3ff);
877 old_rp = rcu_dereference_check(rcu_torture_current,
878 current == writer_task);
879 rp->rtort_mbtest = 1;
880 rcu_assign_pointer(rcu_torture_current, rp);
881 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
882 if (old_rp) {
883 i = old_rp->rtort_pipe_count;
884 if (i > RCU_TORTURE_PIPE_LEN)
885 i = RCU_TORTURE_PIPE_LEN;
886 atomic_inc(&rcu_torture_wcount[i]);
887 old_rp->rtort_pipe_count++;
888 cur_ops->deferred_free(old_rp);
890 rcutorture_record_progress(++rcu_torture_current_version);
891 oldbatch = cur_ops->completed();
892 rcu_stutter_wait("rcu_torture_writer");
893 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
894 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
895 rcutorture_shutdown_absorb("rcu_torture_writer");
896 while (!kthread_should_stop())
897 schedule_timeout_uninterruptible(1);
898 return 0;
902 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
903 * delay between calls.
905 static int
906 rcu_torture_fakewriter(void *arg)
908 DEFINE_RCU_RANDOM(rand);
910 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
911 set_user_nice(current, 19);
913 do {
914 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
915 udelay(rcu_random(&rand) & 0x3ff);
916 cur_ops->sync();
917 rcu_stutter_wait("rcu_torture_fakewriter");
918 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
920 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
921 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
922 while (!kthread_should_stop())
923 schedule_timeout_uninterruptible(1);
924 return 0;
928 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
929 * incrementing the corresponding element of the pipeline array. The
930 * counter in the element should never be greater than 1, otherwise, the
931 * RCU implementation is broken.
933 static void rcu_torture_timer(unsigned long unused)
935 int idx;
936 int completed;
937 static DEFINE_RCU_RANDOM(rand);
938 static DEFINE_SPINLOCK(rand_lock);
939 struct rcu_torture *p;
940 int pipe_count;
942 idx = cur_ops->readlock();
943 completed = cur_ops->completed();
944 p = rcu_dereference_check(rcu_torture_current,
945 rcu_read_lock_bh_held() ||
946 rcu_read_lock_sched_held() ||
947 srcu_read_lock_held(&srcu_ctl));
948 if (p == NULL) {
949 /* Leave because rcu_torture_writer is not yet underway */
950 cur_ops->readunlock(idx);
951 return;
953 if (p->rtort_mbtest == 0)
954 atomic_inc(&n_rcu_torture_mberror);
955 spin_lock(&rand_lock);
956 cur_ops->read_delay(&rand);
957 n_rcu_torture_timers++;
958 spin_unlock(&rand_lock);
959 preempt_disable();
960 pipe_count = p->rtort_pipe_count;
961 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
962 /* Should not happen, but... */
963 pipe_count = RCU_TORTURE_PIPE_LEN;
965 __this_cpu_inc(rcu_torture_count[pipe_count]);
966 completed = cur_ops->completed() - completed;
967 if (completed > RCU_TORTURE_PIPE_LEN) {
968 /* Should not happen, but... */
969 completed = RCU_TORTURE_PIPE_LEN;
971 __this_cpu_inc(rcu_torture_batch[completed]);
972 preempt_enable();
973 cur_ops->readunlock(idx);
977 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
978 * incrementing the corresponding element of the pipeline array. The
979 * counter in the element should never be greater than 1, otherwise, the
980 * RCU implementation is broken.
982 static int
983 rcu_torture_reader(void *arg)
985 int completed;
986 int idx;
987 DEFINE_RCU_RANDOM(rand);
988 struct rcu_torture *p;
989 int pipe_count;
990 struct timer_list t;
992 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
993 set_user_nice(current, 19);
994 if (irqreader && cur_ops->irq_capable)
995 setup_timer_on_stack(&t, rcu_torture_timer, 0);
997 do {
998 if (irqreader && cur_ops->irq_capable) {
999 if (!timer_pending(&t))
1000 mod_timer(&t, jiffies + 1);
1002 idx = cur_ops->readlock();
1003 completed = cur_ops->completed();
1004 p = rcu_dereference_check(rcu_torture_current,
1005 rcu_read_lock_bh_held() ||
1006 rcu_read_lock_sched_held() ||
1007 srcu_read_lock_held(&srcu_ctl));
1008 if (p == NULL) {
1009 /* Wait for rcu_torture_writer to get underway */
1010 cur_ops->readunlock(idx);
1011 schedule_timeout_interruptible(HZ);
1012 continue;
1014 if (p->rtort_mbtest == 0)
1015 atomic_inc(&n_rcu_torture_mberror);
1016 cur_ops->read_delay(&rand);
1017 preempt_disable();
1018 pipe_count = p->rtort_pipe_count;
1019 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1020 /* Should not happen, but... */
1021 pipe_count = RCU_TORTURE_PIPE_LEN;
1023 __this_cpu_inc(rcu_torture_count[pipe_count]);
1024 completed = cur_ops->completed() - completed;
1025 if (completed > RCU_TORTURE_PIPE_LEN) {
1026 /* Should not happen, but... */
1027 completed = RCU_TORTURE_PIPE_LEN;
1029 __this_cpu_inc(rcu_torture_batch[completed]);
1030 preempt_enable();
1031 cur_ops->readunlock(idx);
1032 schedule();
1033 rcu_stutter_wait("rcu_torture_reader");
1034 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1035 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
1036 rcutorture_shutdown_absorb("rcu_torture_reader");
1037 if (irqreader && cur_ops->irq_capable)
1038 del_timer_sync(&t);
1039 while (!kthread_should_stop())
1040 schedule_timeout_uninterruptible(1);
1041 return 0;
1045 * Create an RCU-torture statistics message in the specified buffer.
1047 static int
1048 rcu_torture_printk(char *page)
1050 int cnt = 0;
1051 int cpu;
1052 int i;
1053 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1054 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1056 for_each_possible_cpu(cpu) {
1057 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1058 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1059 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1062 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1063 if (pipesummary[i] != 0)
1064 break;
1066 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
1067 cnt += sprintf(&page[cnt],
1068 "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d "
1069 "rtmbe: %d rtbke: %ld rtbre: %ld "
1070 "rtbf: %ld rtb: %ld nt: %ld",
1071 rcu_torture_current,
1072 rcu_torture_current_version,
1073 list_empty(&rcu_torture_freelist),
1074 atomic_read(&n_rcu_torture_alloc),
1075 atomic_read(&n_rcu_torture_alloc_fail),
1076 atomic_read(&n_rcu_torture_free),
1077 atomic_read(&n_rcu_torture_mberror),
1078 n_rcu_torture_boost_ktrerror,
1079 n_rcu_torture_boost_rterror,
1080 n_rcu_torture_boost_failure,
1081 n_rcu_torture_boosts,
1082 n_rcu_torture_timers);
1083 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
1084 n_rcu_torture_boost_ktrerror != 0 ||
1085 n_rcu_torture_boost_rterror != 0 ||
1086 n_rcu_torture_boost_failure != 0)
1087 cnt += sprintf(&page[cnt], " !!!");
1088 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1089 if (i > 1) {
1090 cnt += sprintf(&page[cnt], "!!! ");
1091 atomic_inc(&n_rcu_torture_error);
1092 WARN_ON_ONCE(1);
1094 cnt += sprintf(&page[cnt], "Reader Pipe: ");
1095 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1096 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
1097 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1098 cnt += sprintf(&page[cnt], "Reader Batch: ");
1099 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1100 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
1101 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1102 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
1103 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1104 cnt += sprintf(&page[cnt], " %d",
1105 atomic_read(&rcu_torture_wcount[i]));
1107 cnt += sprintf(&page[cnt], "\n");
1108 if (cur_ops->stats)
1109 cnt += cur_ops->stats(&page[cnt]);
1110 return cnt;
1114 * Print torture statistics. Caller must ensure that there is only
1115 * one call to this function at a given time!!! This is normally
1116 * accomplished by relying on the module system to only have one copy
1117 * of the module loaded, and then by giving the rcu_torture_stats
1118 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1119 * thread is not running).
1121 static void
1122 rcu_torture_stats_print(void)
1124 int cnt;
1126 cnt = rcu_torture_printk(printk_buf);
1127 printk(KERN_ALERT "%s", printk_buf);
1131 * Periodically prints torture statistics, if periodic statistics printing
1132 * was specified via the stat_interval module parameter.
1134 * No need to worry about fullstop here, since this one doesn't reference
1135 * volatile state or register callbacks.
1137 static int
1138 rcu_torture_stats(void *arg)
1140 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
1141 do {
1142 schedule_timeout_interruptible(stat_interval * HZ);
1143 rcu_torture_stats_print();
1144 rcutorture_shutdown_absorb("rcu_torture_stats");
1145 } while (!kthread_should_stop());
1146 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1147 return 0;
1150 static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
1152 /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1153 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1155 static void rcu_torture_shuffle_tasks(void)
1157 int i;
1159 cpumask_setall(shuffle_tmp_mask);
1160 get_online_cpus();
1162 /* No point in shuffling if there is only one online CPU (ex: UP) */
1163 if (num_online_cpus() == 1) {
1164 put_online_cpus();
1165 return;
1168 if (rcu_idle_cpu != -1)
1169 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
1171 set_cpus_allowed_ptr(current, shuffle_tmp_mask);
1173 if (reader_tasks) {
1174 for (i = 0; i < nrealreaders; i++)
1175 if (reader_tasks[i])
1176 set_cpus_allowed_ptr(reader_tasks[i],
1177 shuffle_tmp_mask);
1180 if (fakewriter_tasks) {
1181 for (i = 0; i < nfakewriters; i++)
1182 if (fakewriter_tasks[i])
1183 set_cpus_allowed_ptr(fakewriter_tasks[i],
1184 shuffle_tmp_mask);
1187 if (writer_task)
1188 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
1190 if (stats_task)
1191 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
1193 if (rcu_idle_cpu == -1)
1194 rcu_idle_cpu = num_online_cpus() - 1;
1195 else
1196 rcu_idle_cpu--;
1198 put_online_cpus();
1201 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1202 * system to become idle at a time and cut off its timer ticks. This is meant
1203 * to test the support for such tickless idle CPU in RCU.
1205 static int
1206 rcu_torture_shuffle(void *arg)
1208 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1209 do {
1210 schedule_timeout_interruptible(shuffle_interval * HZ);
1211 rcu_torture_shuffle_tasks();
1212 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1213 } while (!kthread_should_stop());
1214 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1215 return 0;
1218 /* Cause the rcutorture test to "stutter", starting and stopping all
1219 * threads periodically.
1221 static int
1222 rcu_torture_stutter(void *arg)
1224 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1225 do {
1226 schedule_timeout_interruptible(stutter * HZ);
1227 stutter_pause_test = 1;
1228 if (!kthread_should_stop())
1229 schedule_timeout_interruptible(stutter * HZ);
1230 stutter_pause_test = 0;
1231 rcutorture_shutdown_absorb("rcu_torture_stutter");
1232 } while (!kthread_should_stop());
1233 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1234 return 0;
1237 static inline void
1238 rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, char *tag)
1240 printk(KERN_ALERT "%s" TORTURE_FLAG
1241 "--- %s: nreaders=%d nfakewriters=%d "
1242 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1243 "shuffle_interval=%d stutter=%d irqreader=%d "
1244 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1245 "test_boost=%d/%d test_boost_interval=%d "
1246 "test_boost_duration=%d\n",
1247 torture_type, tag, nrealreaders, nfakewriters,
1248 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1249 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1250 test_boost, cur_ops->can_boost,
1251 test_boost_interval, test_boost_duration);
1254 static struct notifier_block rcutorture_shutdown_nb = {
1255 .notifier_call = rcutorture_shutdown_notify,
1258 static void rcutorture_booster_cleanup(int cpu)
1260 struct task_struct *t;
1262 if (boost_tasks[cpu] == NULL)
1263 return;
1264 mutex_lock(&boost_mutex);
1265 VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
1266 t = boost_tasks[cpu];
1267 boost_tasks[cpu] = NULL;
1268 mutex_unlock(&boost_mutex);
1270 /* This must be outside of the mutex, otherwise deadlock! */
1271 kthread_stop(t);
1274 static int rcutorture_booster_init(int cpu)
1276 int retval;
1278 if (boost_tasks[cpu] != NULL)
1279 return 0; /* Already created, nothing more to do. */
1281 /* Don't allow time recalculation while creating a new task. */
1282 mutex_lock(&boost_mutex);
1283 VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
1284 boost_tasks[cpu] = kthread_create(rcu_torture_boost, NULL,
1285 "rcu_torture_boost");
1286 if (IS_ERR(boost_tasks[cpu])) {
1287 retval = PTR_ERR(boost_tasks[cpu]);
1288 VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
1289 n_rcu_torture_boost_ktrerror++;
1290 boost_tasks[cpu] = NULL;
1291 mutex_unlock(&boost_mutex);
1292 return retval;
1294 kthread_bind(boost_tasks[cpu], cpu);
1295 wake_up_process(boost_tasks[cpu]);
1296 mutex_unlock(&boost_mutex);
1297 return 0;
1300 static int rcutorture_cpu_notify(struct notifier_block *self,
1301 unsigned long action, void *hcpu)
1303 long cpu = (long)hcpu;
1305 switch (action) {
1306 case CPU_ONLINE:
1307 case CPU_DOWN_FAILED:
1308 (void)rcutorture_booster_init(cpu);
1309 break;
1310 case CPU_DOWN_PREPARE:
1311 rcutorture_booster_cleanup(cpu);
1312 break;
1313 default:
1314 break;
1316 return NOTIFY_OK;
1319 static struct notifier_block rcutorture_cpu_nb = {
1320 .notifier_call = rcutorture_cpu_notify,
1323 static void
1324 rcu_torture_cleanup(void)
1326 int i;
1328 mutex_lock(&fullstop_mutex);
1329 rcutorture_record_test_transition();
1330 if (fullstop == FULLSTOP_SHUTDOWN) {
1331 printk(KERN_WARNING /* but going down anyway, so... */
1332 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
1333 mutex_unlock(&fullstop_mutex);
1334 schedule_timeout_uninterruptible(10);
1335 if (cur_ops->cb_barrier != NULL)
1336 cur_ops->cb_barrier();
1337 return;
1339 fullstop = FULLSTOP_RMMOD;
1340 mutex_unlock(&fullstop_mutex);
1341 unregister_reboot_notifier(&rcutorture_shutdown_nb);
1342 if (stutter_task) {
1343 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1344 kthread_stop(stutter_task);
1346 stutter_task = NULL;
1347 if (shuffler_task) {
1348 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1349 kthread_stop(shuffler_task);
1350 free_cpumask_var(shuffle_tmp_mask);
1352 shuffler_task = NULL;
1354 if (writer_task) {
1355 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1356 kthread_stop(writer_task);
1358 writer_task = NULL;
1360 if (reader_tasks) {
1361 for (i = 0; i < nrealreaders; i++) {
1362 if (reader_tasks[i]) {
1363 VERBOSE_PRINTK_STRING(
1364 "Stopping rcu_torture_reader task");
1365 kthread_stop(reader_tasks[i]);
1367 reader_tasks[i] = NULL;
1369 kfree(reader_tasks);
1370 reader_tasks = NULL;
1372 rcu_torture_current = NULL;
1374 if (fakewriter_tasks) {
1375 for (i = 0; i < nfakewriters; i++) {
1376 if (fakewriter_tasks[i]) {
1377 VERBOSE_PRINTK_STRING(
1378 "Stopping rcu_torture_fakewriter task");
1379 kthread_stop(fakewriter_tasks[i]);
1381 fakewriter_tasks[i] = NULL;
1383 kfree(fakewriter_tasks);
1384 fakewriter_tasks = NULL;
1387 if (stats_task) {
1388 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1389 kthread_stop(stats_task);
1391 stats_task = NULL;
1393 if (fqs_task) {
1394 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1395 kthread_stop(fqs_task);
1397 fqs_task = NULL;
1398 if ((test_boost == 1 && cur_ops->can_boost) ||
1399 test_boost == 2) {
1400 unregister_cpu_notifier(&rcutorture_cpu_nb);
1401 for_each_possible_cpu(i)
1402 rcutorture_booster_cleanup(i);
1405 /* Wait for all RCU callbacks to fire. */
1407 if (cur_ops->cb_barrier != NULL)
1408 cur_ops->cb_barrier();
1410 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
1412 if (cur_ops->cleanup)
1413 cur_ops->cleanup();
1414 if (atomic_read(&n_rcu_torture_error))
1415 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
1416 else
1417 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
1420 static int __init
1421 rcu_torture_init(void)
1423 int i;
1424 int cpu;
1425 int firsterr = 0;
1426 static struct rcu_torture_ops *torture_ops[] =
1427 { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
1428 &rcu_bh_ops, &rcu_bh_sync_ops,
1429 &srcu_ops, &srcu_expedited_ops,
1430 &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
1432 mutex_lock(&fullstop_mutex);
1434 /* Process args and tell the world that the torturer is on the job. */
1435 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1436 cur_ops = torture_ops[i];
1437 if (strcmp(torture_type, cur_ops->name) == 0)
1438 break;
1440 if (i == ARRAY_SIZE(torture_ops)) {
1441 printk(KERN_ALERT "rcu-torture: invalid torture type: \"%s\"\n",
1442 torture_type);
1443 printk(KERN_ALERT "rcu-torture types:");
1444 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
1445 printk(KERN_ALERT " %s", torture_ops[i]->name);
1446 printk(KERN_ALERT "\n");
1447 mutex_unlock(&fullstop_mutex);
1448 return -EINVAL;
1450 if (cur_ops->fqs == NULL && fqs_duration != 0) {
1451 printk(KERN_ALERT "rcu-torture: ->fqs NULL and non-zero "
1452 "fqs_duration, fqs disabled.\n");
1453 fqs_duration = 0;
1455 if (cur_ops->init)
1456 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1458 if (nreaders >= 0)
1459 nrealreaders = nreaders;
1460 else
1461 nrealreaders = 2 * num_online_cpus();
1462 rcu_torture_print_module_parms(cur_ops, "Start of test");
1463 fullstop = FULLSTOP_DONTSTOP;
1465 /* Set up the freelist. */
1467 INIT_LIST_HEAD(&rcu_torture_freelist);
1468 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1469 rcu_tortures[i].rtort_mbtest = 0;
1470 list_add_tail(&rcu_tortures[i].rtort_free,
1471 &rcu_torture_freelist);
1474 /* Initialize the statistics so that each run gets its own numbers. */
1476 rcu_torture_current = NULL;
1477 rcu_torture_current_version = 0;
1478 atomic_set(&n_rcu_torture_alloc, 0);
1479 atomic_set(&n_rcu_torture_alloc_fail, 0);
1480 atomic_set(&n_rcu_torture_free, 0);
1481 atomic_set(&n_rcu_torture_mberror, 0);
1482 atomic_set(&n_rcu_torture_error, 0);
1483 n_rcu_torture_boost_ktrerror = 0;
1484 n_rcu_torture_boost_rterror = 0;
1485 n_rcu_torture_boost_failure = 0;
1486 n_rcu_torture_boosts = 0;
1487 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1488 atomic_set(&rcu_torture_wcount[i], 0);
1489 for_each_possible_cpu(cpu) {
1490 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1491 per_cpu(rcu_torture_count, cpu)[i] = 0;
1492 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1496 /* Start up the kthreads. */
1498 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1499 writer_task = kthread_run(rcu_torture_writer, NULL,
1500 "rcu_torture_writer");
1501 if (IS_ERR(writer_task)) {
1502 firsterr = PTR_ERR(writer_task);
1503 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1504 writer_task = NULL;
1505 goto unwind;
1507 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1508 GFP_KERNEL);
1509 if (fakewriter_tasks == NULL) {
1510 VERBOSE_PRINTK_ERRSTRING("out of memory");
1511 firsterr = -ENOMEM;
1512 goto unwind;
1514 for (i = 0; i < nfakewriters; i++) {
1515 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1516 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1517 "rcu_torture_fakewriter");
1518 if (IS_ERR(fakewriter_tasks[i])) {
1519 firsterr = PTR_ERR(fakewriter_tasks[i]);
1520 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1521 fakewriter_tasks[i] = NULL;
1522 goto unwind;
1525 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
1526 GFP_KERNEL);
1527 if (reader_tasks == NULL) {
1528 VERBOSE_PRINTK_ERRSTRING("out of memory");
1529 firsterr = -ENOMEM;
1530 goto unwind;
1532 for (i = 0; i < nrealreaders; i++) {
1533 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1534 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1535 "rcu_torture_reader");
1536 if (IS_ERR(reader_tasks[i])) {
1537 firsterr = PTR_ERR(reader_tasks[i]);
1538 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1539 reader_tasks[i] = NULL;
1540 goto unwind;
1543 if (stat_interval > 0) {
1544 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1545 stats_task = kthread_run(rcu_torture_stats, NULL,
1546 "rcu_torture_stats");
1547 if (IS_ERR(stats_task)) {
1548 firsterr = PTR_ERR(stats_task);
1549 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1550 stats_task = NULL;
1551 goto unwind;
1554 if (test_no_idle_hz) {
1555 rcu_idle_cpu = num_online_cpus() - 1;
1557 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
1558 firsterr = -ENOMEM;
1559 VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
1560 goto unwind;
1563 /* Create the shuffler thread */
1564 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1565 "rcu_torture_shuffle");
1566 if (IS_ERR(shuffler_task)) {
1567 free_cpumask_var(shuffle_tmp_mask);
1568 firsterr = PTR_ERR(shuffler_task);
1569 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1570 shuffler_task = NULL;
1571 goto unwind;
1574 if (stutter < 0)
1575 stutter = 0;
1576 if (stutter) {
1577 /* Create the stutter thread */
1578 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1579 "rcu_torture_stutter");
1580 if (IS_ERR(stutter_task)) {
1581 firsterr = PTR_ERR(stutter_task);
1582 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1583 stutter_task = NULL;
1584 goto unwind;
1587 if (fqs_duration < 0)
1588 fqs_duration = 0;
1589 if (fqs_duration) {
1590 /* Create the stutter thread */
1591 fqs_task = kthread_run(rcu_torture_fqs, NULL,
1592 "rcu_torture_fqs");
1593 if (IS_ERR(fqs_task)) {
1594 firsterr = PTR_ERR(fqs_task);
1595 VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
1596 fqs_task = NULL;
1597 goto unwind;
1600 if (test_boost_interval < 1)
1601 test_boost_interval = 1;
1602 if (test_boost_duration < 2)
1603 test_boost_duration = 2;
1604 if ((test_boost == 1 && cur_ops->can_boost) ||
1605 test_boost == 2) {
1606 int retval;
1608 boost_starttime = jiffies + test_boost_interval * HZ;
1609 register_cpu_notifier(&rcutorture_cpu_nb);
1610 for_each_possible_cpu(i) {
1611 if (cpu_is_offline(i))
1612 continue; /* Heuristic: CPU can go offline. */
1613 retval = rcutorture_booster_init(i);
1614 if (retval < 0) {
1615 firsterr = retval;
1616 goto unwind;
1620 register_reboot_notifier(&rcutorture_shutdown_nb);
1621 rcutorture_record_test_transition();
1622 mutex_unlock(&fullstop_mutex);
1623 return 0;
1625 unwind:
1626 mutex_unlock(&fullstop_mutex);
1627 rcu_torture_cleanup();
1628 return firsterr;
1631 module_init(rcu_torture_init);
1632 module_exit(rcu_torture_cleanup);