729710273dcb034cdaa13bb8d27714f15f5fb2d3
[pandora-kernel.git] / kernel / rcutorture.c
1 /*
2  * Read-Copy Update module-based torture test facility
3  *
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.
8  *
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.
13  *
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.
17  *
18  * Copyright (C) IBM Corporation, 2005, 2006
19  *
20  * Authors: Paul E. McKenney <paulmck@us.ibm.com>
21  *        Josh Triplett <josh@freedesktop.org>
22  *
23  * See also:  Documentation/RCU/torture.txt
24  */
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/module.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/sched.h>
36 #include <asm/atomic.h>
37 #include <linux/bitops.h>
38 #include <linux/completion.h>
39 #include <linux/moduleparam.h>
40 #include <linux/percpu.h>
41 #include <linux/notifier.h>
42 #include <linux/reboot.h>
43 #include <linux/freezer.h>
44 #include <linux/cpu.h>
45 #include <linux/delay.h>
46 #include <linux/stat.h>
47 #include <linux/srcu.h>
48 #include <linux/slab.h>
49 #include <asm/byteorder.h>
50
51 MODULE_LICENSE("GPL");
52 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
53               "Josh Triplett <josh@freedesktop.org>");
54
55 static int nreaders = -1;       /* # reader threads, defaults to 2*ncpus */
56 static int nfakewriters = 4;    /* # fake writer threads */
57 static int stat_interval;       /* Interval between stats, in seconds. */
58                                 /*  Defaults to "only at end of test". */
59 static int verbose;             /* Print more debug info. */
60 static int test_no_idle_hz;     /* Test RCU's support for tickless idle CPUs. */
61 static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
62 static int stutter = 5;         /* Start/stop testing interval (in sec) */
63 static int irqreader = 1;       /* RCU readers from irq (timers). */
64 static int fqs_duration = 0;    /* Duration of bursts (us), 0 to disable. */
65 static int fqs_holdoff = 0;     /* Hold time within burst (us). */
66 static int fqs_stutter = 3;     /* Wait time between bursts (s). */
67 static char *torture_type = "rcu"; /* What RCU implementation to torture. */
68
69 module_param(nreaders, int, 0444);
70 MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
71 module_param(nfakewriters, int, 0444);
72 MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
73 module_param(stat_interval, int, 0444);
74 MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
75 module_param(verbose, bool, 0444);
76 MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
77 module_param(test_no_idle_hz, bool, 0444);
78 MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
79 module_param(shuffle_interval, int, 0444);
80 MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
81 module_param(stutter, int, 0444);
82 MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
83 module_param(irqreader, int, 0444);
84 MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
85 module_param(fqs_duration, int, 0444);
86 MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
87 module_param(fqs_holdoff, int, 0444);
88 MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
89 module_param(fqs_stutter, int, 0444);
90 MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
91 module_param(torture_type, charp, 0444);
92 MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
93
94 #define TORTURE_FLAG "-torture:"
95 #define PRINTK_STRING(s) \
96         do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
97 #define VERBOSE_PRINTK_STRING(s) \
98         do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
99 #define VERBOSE_PRINTK_ERRSTRING(s) \
100         do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
101
102 static char printk_buf[4096];
103
104 static int nrealreaders;
105 static struct task_struct *writer_task;
106 static struct task_struct **fakewriter_tasks;
107 static struct task_struct **reader_tasks;
108 static struct task_struct *stats_task;
109 static struct task_struct *shuffler_task;
110 static struct task_struct *stutter_task;
111 static struct task_struct *fqs_task;
112
113 #define RCU_TORTURE_PIPE_LEN 10
114
115 struct rcu_torture {
116         struct rcu_head rtort_rcu;
117         int rtort_pipe_count;
118         struct list_head rtort_free;
119         int rtort_mbtest;
120 };
121
122 static LIST_HEAD(rcu_torture_freelist);
123 static struct rcu_torture *rcu_torture_current;
124 static long rcu_torture_current_version;
125 static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
126 static DEFINE_SPINLOCK(rcu_torture_lock);
127 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
128         { 0 };
129 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
130         { 0 };
131 static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
132 static atomic_t n_rcu_torture_alloc;
133 static atomic_t n_rcu_torture_alloc_fail;
134 static atomic_t n_rcu_torture_free;
135 static atomic_t n_rcu_torture_mberror;
136 static atomic_t n_rcu_torture_error;
137 static long n_rcu_torture_timers;
138 static struct list_head rcu_torture_removed;
139 static cpumask_var_t shuffle_tmp_mask;
140
141 static int stutter_pause_test;
142
143 #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
144 #define RCUTORTURE_RUNNABLE_INIT 1
145 #else
146 #define RCUTORTURE_RUNNABLE_INIT 0
147 #endif
148 int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
149
150 /* Mediate rmmod and system shutdown.  Concurrent rmmod & shutdown illegal! */
151
152 #define FULLSTOP_DONTSTOP 0     /* Normal operation. */
153 #define FULLSTOP_SHUTDOWN 1     /* System shutdown with rcutorture running. */
154 #define FULLSTOP_RMMOD    2     /* Normal rmmod of rcutorture. */
155 static int fullstop = FULLSTOP_RMMOD;
156 DEFINE_MUTEX(fullstop_mutex);   /* Protect fullstop transitions and spawning */
157                                 /*  of kthreads. */
158
159 /*
160  * Detect and respond to a system shutdown.
161  */
162 static int
163 rcutorture_shutdown_notify(struct notifier_block *unused1,
164                            unsigned long unused2, void *unused3)
165 {
166         mutex_lock(&fullstop_mutex);
167         if (fullstop == FULLSTOP_DONTSTOP)
168                 fullstop = FULLSTOP_SHUTDOWN;
169         else
170                 printk(KERN_WARNING /* but going down anyway, so... */
171                        "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
172         mutex_unlock(&fullstop_mutex);
173         return NOTIFY_DONE;
174 }
175
176 /*
177  * Absorb kthreads into a kernel function that won't return, so that
178  * they won't ever access module text or data again.
179  */
180 static void rcutorture_shutdown_absorb(char *title)
181 {
182         if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
183                 printk(KERN_NOTICE
184                        "rcutorture thread %s parking due to system shutdown\n",
185                        title);
186                 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
187         }
188 }
189
190 /*
191  * Allocate an element from the rcu_tortures pool.
192  */
193 static struct rcu_torture *
194 rcu_torture_alloc(void)
195 {
196         struct list_head *p;
197
198         spin_lock_bh(&rcu_torture_lock);
199         if (list_empty(&rcu_torture_freelist)) {
200                 atomic_inc(&n_rcu_torture_alloc_fail);
201                 spin_unlock_bh(&rcu_torture_lock);
202                 return NULL;
203         }
204         atomic_inc(&n_rcu_torture_alloc);
205         p = rcu_torture_freelist.next;
206         list_del_init(p);
207         spin_unlock_bh(&rcu_torture_lock);
208         return container_of(p, struct rcu_torture, rtort_free);
209 }
210
211 /*
212  * Free an element to the rcu_tortures pool.
213  */
214 static void
215 rcu_torture_free(struct rcu_torture *p)
216 {
217         atomic_inc(&n_rcu_torture_free);
218         spin_lock_bh(&rcu_torture_lock);
219         list_add_tail(&p->rtort_free, &rcu_torture_freelist);
220         spin_unlock_bh(&rcu_torture_lock);
221 }
222
223 struct rcu_random_state {
224         unsigned long rrs_state;
225         long rrs_count;
226 };
227
228 #define RCU_RANDOM_MULT 39916801  /* prime */
229 #define RCU_RANDOM_ADD  479001701 /* prime */
230 #define RCU_RANDOM_REFRESH 10000
231
232 #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
233
234 /*
235  * Crude but fast random-number generator.  Uses a linear congruential
236  * generator, with occasional help from cpu_clock().
237  */
238 static unsigned long
239 rcu_random(struct rcu_random_state *rrsp)
240 {
241         if (--rrsp->rrs_count < 0) {
242                 rrsp->rrs_state += (unsigned long)local_clock();
243                 rrsp->rrs_count = RCU_RANDOM_REFRESH;
244         }
245         rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
246         return swahw32(rrsp->rrs_state);
247 }
248
249 static void
250 rcu_stutter_wait(char *title)
251 {
252         while (stutter_pause_test || !rcutorture_runnable) {
253                 if (rcutorture_runnable)
254                         schedule_timeout_interruptible(1);
255                 else
256                         schedule_timeout_interruptible(round_jiffies_relative(HZ));
257                 rcutorture_shutdown_absorb(title);
258         }
259 }
260
261 /*
262  * Operations vector for selecting different types of tests.
263  */
264
265 struct rcu_torture_ops {
266         void (*init)(void);
267         void (*cleanup)(void);
268         int (*readlock)(void);
269         void (*read_delay)(struct rcu_random_state *rrsp);
270         void (*readunlock)(int idx);
271         int (*completed)(void);
272         void (*deferred_free)(struct rcu_torture *p);
273         void (*sync)(void);
274         void (*cb_barrier)(void);
275         void (*fqs)(void);
276         int (*stats)(char *page);
277         int irq_capable;
278         char *name;
279 };
280
281 static struct rcu_torture_ops *cur_ops;
282
283 /*
284  * Definitions for rcu torture testing.
285  */
286
287 static int rcu_torture_read_lock(void) __acquires(RCU)
288 {
289         rcu_read_lock();
290         return 0;
291 }
292
293 static void rcu_read_delay(struct rcu_random_state *rrsp)
294 {
295         const unsigned long shortdelay_us = 200;
296         const unsigned long longdelay_ms = 50;
297
298         /* We want a short delay sometimes to make a reader delay the grace
299          * period, and we want a long delay occasionally to trigger
300          * force_quiescent_state. */
301
302         if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
303                 mdelay(longdelay_ms);
304         if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
305                 udelay(shortdelay_us);
306 #ifdef CONFIG_PREEMPT
307         if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
308                 preempt_schedule();  /* No QS if preempt_disable() in effect */
309 #endif
310 }
311
312 static void rcu_torture_read_unlock(int idx) __releases(RCU)
313 {
314         rcu_read_unlock();
315 }
316
317 static int rcu_torture_completed(void)
318 {
319         return rcu_batches_completed();
320 }
321
322 static void
323 rcu_torture_cb(struct rcu_head *p)
324 {
325         int i;
326         struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
327
328         if (fullstop != FULLSTOP_DONTSTOP) {
329                 /* Test is ending, just drop callbacks on the floor. */
330                 /* The next initialization will pick up the pieces. */
331                 return;
332         }
333         i = rp->rtort_pipe_count;
334         if (i > RCU_TORTURE_PIPE_LEN)
335                 i = RCU_TORTURE_PIPE_LEN;
336         atomic_inc(&rcu_torture_wcount[i]);
337         if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
338                 rp->rtort_mbtest = 0;
339                 rcu_torture_free(rp);
340         } else
341                 cur_ops->deferred_free(rp);
342 }
343
344 static int rcu_no_completed(void)
345 {
346         return 0;
347 }
348
349 static void rcu_torture_deferred_free(struct rcu_torture *p)
350 {
351         call_rcu(&p->rtort_rcu, rcu_torture_cb);
352 }
353
354 static struct rcu_torture_ops rcu_ops = {
355         .init           = NULL,
356         .cleanup        = NULL,
357         .readlock       = rcu_torture_read_lock,
358         .read_delay     = rcu_read_delay,
359         .readunlock     = rcu_torture_read_unlock,
360         .completed      = rcu_torture_completed,
361         .deferred_free  = rcu_torture_deferred_free,
362         .sync           = synchronize_rcu,
363         .cb_barrier     = rcu_barrier,
364         .fqs            = rcu_force_quiescent_state,
365         .stats          = NULL,
366         .irq_capable    = 1,
367         .name           = "rcu"
368 };
369
370 static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
371 {
372         int i;
373         struct rcu_torture *rp;
374         struct rcu_torture *rp1;
375
376         cur_ops->sync();
377         list_add(&p->rtort_free, &rcu_torture_removed);
378         list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
379                 i = rp->rtort_pipe_count;
380                 if (i > RCU_TORTURE_PIPE_LEN)
381                         i = RCU_TORTURE_PIPE_LEN;
382                 atomic_inc(&rcu_torture_wcount[i]);
383                 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
384                         rp->rtort_mbtest = 0;
385                         list_del(&rp->rtort_free);
386                         rcu_torture_free(rp);
387                 }
388         }
389 }
390
391 static void rcu_sync_torture_init(void)
392 {
393         INIT_LIST_HEAD(&rcu_torture_removed);
394 }
395
396 static struct rcu_torture_ops rcu_sync_ops = {
397         .init           = rcu_sync_torture_init,
398         .cleanup        = NULL,
399         .readlock       = rcu_torture_read_lock,
400         .read_delay     = rcu_read_delay,
401         .readunlock     = rcu_torture_read_unlock,
402         .completed      = rcu_torture_completed,
403         .deferred_free  = rcu_sync_torture_deferred_free,
404         .sync           = synchronize_rcu,
405         .cb_barrier     = NULL,
406         .fqs            = rcu_force_quiescent_state,
407         .stats          = NULL,
408         .irq_capable    = 1,
409         .name           = "rcu_sync"
410 };
411
412 static struct rcu_torture_ops rcu_expedited_ops = {
413         .init           = rcu_sync_torture_init,
414         .cleanup        = NULL,
415         .readlock       = rcu_torture_read_lock,
416         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
417         .readunlock     = rcu_torture_read_unlock,
418         .completed      = rcu_no_completed,
419         .deferred_free  = rcu_sync_torture_deferred_free,
420         .sync           = synchronize_rcu_expedited,
421         .cb_barrier     = NULL,
422         .fqs            = rcu_force_quiescent_state,
423         .stats          = NULL,
424         .irq_capable    = 1,
425         .name           = "rcu_expedited"
426 };
427
428 /*
429  * Definitions for rcu_bh torture testing.
430  */
431
432 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
433 {
434         rcu_read_lock_bh();
435         return 0;
436 }
437
438 static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
439 {
440         rcu_read_unlock_bh();
441 }
442
443 static int rcu_bh_torture_completed(void)
444 {
445         return rcu_batches_completed_bh();
446 }
447
448 static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
449 {
450         call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
451 }
452
453 struct rcu_bh_torture_synchronize {
454         struct rcu_head head;
455         struct completion completion;
456 };
457
458 static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
459 {
460         struct rcu_bh_torture_synchronize *rcu;
461
462         rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
463         complete(&rcu->completion);
464 }
465
466 static void rcu_bh_torture_synchronize(void)
467 {
468         struct rcu_bh_torture_synchronize rcu;
469
470         init_rcu_head_on_stack(&rcu.head);
471         init_completion(&rcu.completion);
472         call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
473         wait_for_completion(&rcu.completion);
474         destroy_rcu_head_on_stack(&rcu.head);
475 }
476
477 static struct rcu_torture_ops rcu_bh_ops = {
478         .init           = NULL,
479         .cleanup        = NULL,
480         .readlock       = rcu_bh_torture_read_lock,
481         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
482         .readunlock     = rcu_bh_torture_read_unlock,
483         .completed      = rcu_bh_torture_completed,
484         .deferred_free  = rcu_bh_torture_deferred_free,
485         .sync           = rcu_bh_torture_synchronize,
486         .cb_barrier     = rcu_barrier_bh,
487         .fqs            = rcu_bh_force_quiescent_state,
488         .stats          = NULL,
489         .irq_capable    = 1,
490         .name           = "rcu_bh"
491 };
492
493 static struct rcu_torture_ops rcu_bh_sync_ops = {
494         .init           = rcu_sync_torture_init,
495         .cleanup        = NULL,
496         .readlock       = rcu_bh_torture_read_lock,
497         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
498         .readunlock     = rcu_bh_torture_read_unlock,
499         .completed      = rcu_bh_torture_completed,
500         .deferred_free  = rcu_sync_torture_deferred_free,
501         .sync           = rcu_bh_torture_synchronize,
502         .cb_barrier     = NULL,
503         .fqs            = rcu_bh_force_quiescent_state,
504         .stats          = NULL,
505         .irq_capable    = 1,
506         .name           = "rcu_bh_sync"
507 };
508
509 /*
510  * Definitions for srcu torture testing.
511  */
512
513 static struct srcu_struct srcu_ctl;
514
515 static void srcu_torture_init(void)
516 {
517         init_srcu_struct(&srcu_ctl);
518         rcu_sync_torture_init();
519 }
520
521 static void srcu_torture_cleanup(void)
522 {
523         synchronize_srcu(&srcu_ctl);
524         cleanup_srcu_struct(&srcu_ctl);
525 }
526
527 static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
528 {
529         return srcu_read_lock(&srcu_ctl);
530 }
531
532 static void srcu_read_delay(struct rcu_random_state *rrsp)
533 {
534         long delay;
535         const long uspertick = 1000000 / HZ;
536         const long longdelay = 10;
537
538         /* We want there to be long-running readers, but not all the time. */
539
540         delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
541         if (!delay)
542                 schedule_timeout_interruptible(longdelay);
543         else
544                 rcu_read_delay(rrsp);
545 }
546
547 static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
548 {
549         srcu_read_unlock(&srcu_ctl, idx);
550 }
551
552 static int srcu_torture_completed(void)
553 {
554         return srcu_batches_completed(&srcu_ctl);
555 }
556
557 static void srcu_torture_synchronize(void)
558 {
559         synchronize_srcu(&srcu_ctl);
560 }
561
562 static int srcu_torture_stats(char *page)
563 {
564         int cnt = 0;
565         int cpu;
566         int idx = srcu_ctl.completed & 0x1;
567
568         cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
569                        torture_type, TORTURE_FLAG, idx);
570         for_each_possible_cpu(cpu) {
571                 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
572                                per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
573                                per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
574         }
575         cnt += sprintf(&page[cnt], "\n");
576         return cnt;
577 }
578
579 static struct rcu_torture_ops srcu_ops = {
580         .init           = srcu_torture_init,
581         .cleanup        = srcu_torture_cleanup,
582         .readlock       = srcu_torture_read_lock,
583         .read_delay     = srcu_read_delay,
584         .readunlock     = srcu_torture_read_unlock,
585         .completed      = srcu_torture_completed,
586         .deferred_free  = rcu_sync_torture_deferred_free,
587         .sync           = srcu_torture_synchronize,
588         .cb_barrier     = NULL,
589         .stats          = srcu_torture_stats,
590         .name           = "srcu"
591 };
592
593 static void srcu_torture_synchronize_expedited(void)
594 {
595         synchronize_srcu_expedited(&srcu_ctl);
596 }
597
598 static struct rcu_torture_ops srcu_expedited_ops = {
599         .init           = srcu_torture_init,
600         .cleanup        = srcu_torture_cleanup,
601         .readlock       = srcu_torture_read_lock,
602         .read_delay     = srcu_read_delay,
603         .readunlock     = srcu_torture_read_unlock,
604         .completed      = srcu_torture_completed,
605         .deferred_free  = rcu_sync_torture_deferred_free,
606         .sync           = srcu_torture_synchronize_expedited,
607         .cb_barrier     = NULL,
608         .stats          = srcu_torture_stats,
609         .name           = "srcu_expedited"
610 };
611
612 /*
613  * Definitions for sched torture testing.
614  */
615
616 static int sched_torture_read_lock(void)
617 {
618         preempt_disable();
619         return 0;
620 }
621
622 static void sched_torture_read_unlock(int idx)
623 {
624         preempt_enable();
625 }
626
627 static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
628 {
629         call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
630 }
631
632 static void sched_torture_synchronize(void)
633 {
634         synchronize_sched();
635 }
636
637 static struct rcu_torture_ops sched_ops = {
638         .init           = rcu_sync_torture_init,
639         .cleanup        = NULL,
640         .readlock       = sched_torture_read_lock,
641         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
642         .readunlock     = sched_torture_read_unlock,
643         .completed      = rcu_no_completed,
644         .deferred_free  = rcu_sched_torture_deferred_free,
645         .sync           = sched_torture_synchronize,
646         .cb_barrier     = rcu_barrier_sched,
647         .fqs            = rcu_sched_force_quiescent_state,
648         .stats          = NULL,
649         .irq_capable    = 1,
650         .name           = "sched"
651 };
652
653 static struct rcu_torture_ops sched_sync_ops = {
654         .init           = rcu_sync_torture_init,
655         .cleanup        = NULL,
656         .readlock       = sched_torture_read_lock,
657         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
658         .readunlock     = sched_torture_read_unlock,
659         .completed      = rcu_no_completed,
660         .deferred_free  = rcu_sync_torture_deferred_free,
661         .sync           = sched_torture_synchronize,
662         .cb_barrier     = NULL,
663         .fqs            = rcu_sched_force_quiescent_state,
664         .stats          = NULL,
665         .name           = "sched_sync"
666 };
667
668 static struct rcu_torture_ops sched_expedited_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_sync_torture_deferred_free,
676         .sync           = synchronize_sched_expedited,
677         .cb_barrier     = NULL,
678         .fqs            = rcu_sched_force_quiescent_state,
679         .stats          = NULL,
680         .irq_capable    = 1,
681         .name           = "sched_expedited"
682 };
683
684 /*
685  * RCU torture force-quiescent-state kthread.  Repeatedly induces
686  * bursts of calls to force_quiescent_state(), increasing the probability
687  * of occurrence of some important types of race conditions.
688  */
689 static int
690 rcu_torture_fqs(void *arg)
691 {
692         unsigned long fqs_resume_time;
693         int fqs_burst_remaining;
694
695         VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
696         do {
697                 fqs_resume_time = jiffies + fqs_stutter * HZ;
698                 while (jiffies - fqs_resume_time > LONG_MAX) {
699                         schedule_timeout_interruptible(1);
700                 }
701                 fqs_burst_remaining = fqs_duration;
702                 while (fqs_burst_remaining > 0) {
703                         cur_ops->fqs();
704                         udelay(fqs_holdoff);
705                         fqs_burst_remaining -= fqs_holdoff;
706                 }
707                 rcu_stutter_wait("rcu_torture_fqs");
708         } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
709         VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
710         rcutorture_shutdown_absorb("rcu_torture_fqs");
711         while (!kthread_should_stop())
712                 schedule_timeout_uninterruptible(1);
713         return 0;
714 }
715
716 /*
717  * RCU torture writer kthread.  Repeatedly substitutes a new structure
718  * for that pointed to by rcu_torture_current, freeing the old structure
719  * after a series of grace periods (the "pipeline").
720  */
721 static int
722 rcu_torture_writer(void *arg)
723 {
724         int i;
725         long oldbatch = rcu_batches_completed();
726         struct rcu_torture *rp;
727         struct rcu_torture *old_rp;
728         static DEFINE_RCU_RANDOM(rand);
729
730         VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
731         set_user_nice(current, 19);
732
733         do {
734                 schedule_timeout_uninterruptible(1);
735                 rp = rcu_torture_alloc();
736                 if (rp == NULL)
737                         continue;
738                 rp->rtort_pipe_count = 0;
739                 udelay(rcu_random(&rand) & 0x3ff);
740                 old_rp = rcu_torture_current;
741                 rp->rtort_mbtest = 1;
742                 rcu_assign_pointer(rcu_torture_current, rp);
743                 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
744                 if (old_rp) {
745                         i = old_rp->rtort_pipe_count;
746                         if (i > RCU_TORTURE_PIPE_LEN)
747                                 i = RCU_TORTURE_PIPE_LEN;
748                         atomic_inc(&rcu_torture_wcount[i]);
749                         old_rp->rtort_pipe_count++;
750                         cur_ops->deferred_free(old_rp);
751                 }
752                 rcu_torture_current_version++;
753                 oldbatch = cur_ops->completed();
754                 rcu_stutter_wait("rcu_torture_writer");
755         } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
756         VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
757         rcutorture_shutdown_absorb("rcu_torture_writer");
758         while (!kthread_should_stop())
759                 schedule_timeout_uninterruptible(1);
760         return 0;
761 }
762
763 /*
764  * RCU torture fake writer kthread.  Repeatedly calls sync, with a random
765  * delay between calls.
766  */
767 static int
768 rcu_torture_fakewriter(void *arg)
769 {
770         DEFINE_RCU_RANDOM(rand);
771
772         VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
773         set_user_nice(current, 19);
774
775         do {
776                 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
777                 udelay(rcu_random(&rand) & 0x3ff);
778                 cur_ops->sync();
779                 rcu_stutter_wait("rcu_torture_fakewriter");
780         } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
781
782         VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
783         rcutorture_shutdown_absorb("rcu_torture_fakewriter");
784         while (!kthread_should_stop())
785                 schedule_timeout_uninterruptible(1);
786         return 0;
787 }
788
789 /*
790  * RCU torture reader from timer handler.  Dereferences rcu_torture_current,
791  * incrementing the corresponding element of the pipeline array.  The
792  * counter in the element should never be greater than 1, otherwise, the
793  * RCU implementation is broken.
794  */
795 static void rcu_torture_timer(unsigned long unused)
796 {
797         int idx;
798         int completed;
799         static DEFINE_RCU_RANDOM(rand);
800         static DEFINE_SPINLOCK(rand_lock);
801         struct rcu_torture *p;
802         int pipe_count;
803
804         idx = cur_ops->readlock();
805         completed = cur_ops->completed();
806         p = rcu_dereference_check(rcu_torture_current,
807                                   rcu_read_lock_held() ||
808                                   rcu_read_lock_bh_held() ||
809                                   rcu_read_lock_sched_held() ||
810                                   srcu_read_lock_held(&srcu_ctl));
811         if (p == NULL) {
812                 /* Leave because rcu_torture_writer is not yet underway */
813                 cur_ops->readunlock(idx);
814                 return;
815         }
816         if (p->rtort_mbtest == 0)
817                 atomic_inc(&n_rcu_torture_mberror);
818         spin_lock(&rand_lock);
819         cur_ops->read_delay(&rand);
820         n_rcu_torture_timers++;
821         spin_unlock(&rand_lock);
822         preempt_disable();
823         pipe_count = p->rtort_pipe_count;
824         if (pipe_count > RCU_TORTURE_PIPE_LEN) {
825                 /* Should not happen, but... */
826                 pipe_count = RCU_TORTURE_PIPE_LEN;
827         }
828         __this_cpu_inc(rcu_torture_count[pipe_count]);
829         completed = cur_ops->completed() - completed;
830         if (completed > RCU_TORTURE_PIPE_LEN) {
831                 /* Should not happen, but... */
832                 completed = RCU_TORTURE_PIPE_LEN;
833         }
834         __this_cpu_inc(rcu_torture_batch[completed]);
835         preempt_enable();
836         cur_ops->readunlock(idx);
837 }
838
839 /*
840  * RCU torture reader kthread.  Repeatedly dereferences rcu_torture_current,
841  * incrementing the corresponding element of the pipeline array.  The
842  * counter in the element should never be greater than 1, otherwise, the
843  * RCU implementation is broken.
844  */
845 static int
846 rcu_torture_reader(void *arg)
847 {
848         int completed;
849         int idx;
850         DEFINE_RCU_RANDOM(rand);
851         struct rcu_torture *p;
852         int pipe_count;
853         struct timer_list t;
854
855         VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
856         set_user_nice(current, 19);
857         if (irqreader && cur_ops->irq_capable)
858                 setup_timer_on_stack(&t, rcu_torture_timer, 0);
859
860         do {
861                 if (irqreader && cur_ops->irq_capable) {
862                         if (!timer_pending(&t))
863                                 mod_timer(&t, jiffies + 1);
864                 }
865                 idx = cur_ops->readlock();
866                 completed = cur_ops->completed();
867                 p = rcu_dereference_check(rcu_torture_current,
868                                           rcu_read_lock_held() ||
869                                           rcu_read_lock_bh_held() ||
870                                           rcu_read_lock_sched_held() ||
871                                           srcu_read_lock_held(&srcu_ctl));
872                 if (p == NULL) {
873                         /* Wait for rcu_torture_writer to get underway */
874                         cur_ops->readunlock(idx);
875                         schedule_timeout_interruptible(HZ);
876                         continue;
877                 }
878                 if (p->rtort_mbtest == 0)
879                         atomic_inc(&n_rcu_torture_mberror);
880                 cur_ops->read_delay(&rand);
881                 preempt_disable();
882                 pipe_count = p->rtort_pipe_count;
883                 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
884                         /* Should not happen, but... */
885                         pipe_count = RCU_TORTURE_PIPE_LEN;
886                 }
887                 __this_cpu_inc(rcu_torture_count[pipe_count]);
888                 completed = cur_ops->completed() - completed;
889                 if (completed > RCU_TORTURE_PIPE_LEN) {
890                         /* Should not happen, but... */
891                         completed = RCU_TORTURE_PIPE_LEN;
892                 }
893                 __this_cpu_inc(rcu_torture_batch[completed]);
894                 preempt_enable();
895                 cur_ops->readunlock(idx);
896                 schedule();
897                 rcu_stutter_wait("rcu_torture_reader");
898         } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
899         VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
900         rcutorture_shutdown_absorb("rcu_torture_reader");
901         if (irqreader && cur_ops->irq_capable)
902                 del_timer_sync(&t);
903         while (!kthread_should_stop())
904                 schedule_timeout_uninterruptible(1);
905         return 0;
906 }
907
908 /*
909  * Create an RCU-torture statistics message in the specified buffer.
910  */
911 static int
912 rcu_torture_printk(char *page)
913 {
914         int cnt = 0;
915         int cpu;
916         int i;
917         long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
918         long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
919
920         for_each_possible_cpu(cpu) {
921                 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
922                         pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
923                         batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
924                 }
925         }
926         for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
927                 if (pipesummary[i] != 0)
928                         break;
929         }
930         cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
931         cnt += sprintf(&page[cnt],
932                        "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
933                        "rtmbe: %d nt: %ld",
934                        rcu_torture_current,
935                        rcu_torture_current_version,
936                        list_empty(&rcu_torture_freelist),
937                        atomic_read(&n_rcu_torture_alloc),
938                        atomic_read(&n_rcu_torture_alloc_fail),
939                        atomic_read(&n_rcu_torture_free),
940                        atomic_read(&n_rcu_torture_mberror),
941                        n_rcu_torture_timers);
942         if (atomic_read(&n_rcu_torture_mberror) != 0)
943                 cnt += sprintf(&page[cnt], " !!!");
944         cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
945         if (i > 1) {
946                 cnt += sprintf(&page[cnt], "!!! ");
947                 atomic_inc(&n_rcu_torture_error);
948                 WARN_ON_ONCE(1);
949         }
950         cnt += sprintf(&page[cnt], "Reader Pipe: ");
951         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
952                 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
953         cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
954         cnt += sprintf(&page[cnt], "Reader Batch: ");
955         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
956                 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
957         cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
958         cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
959         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
960                 cnt += sprintf(&page[cnt], " %d",
961                                atomic_read(&rcu_torture_wcount[i]));
962         }
963         cnt += sprintf(&page[cnt], "\n");
964         if (cur_ops->stats)
965                 cnt += cur_ops->stats(&page[cnt]);
966         return cnt;
967 }
968
969 /*
970  * Print torture statistics.  Caller must ensure that there is only
971  * one call to this function at a given time!!!  This is normally
972  * accomplished by relying on the module system to only have one copy
973  * of the module loaded, and then by giving the rcu_torture_stats
974  * kthread full control (or the init/cleanup functions when rcu_torture_stats
975  * thread is not running).
976  */
977 static void
978 rcu_torture_stats_print(void)
979 {
980         int cnt;
981
982         cnt = rcu_torture_printk(printk_buf);
983         printk(KERN_ALERT "%s", printk_buf);
984 }
985
986 /*
987  * Periodically prints torture statistics, if periodic statistics printing
988  * was specified via the stat_interval module parameter.
989  *
990  * No need to worry about fullstop here, since this one doesn't reference
991  * volatile state or register callbacks.
992  */
993 static int
994 rcu_torture_stats(void *arg)
995 {
996         VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
997         do {
998                 schedule_timeout_interruptible(stat_interval * HZ);
999                 rcu_torture_stats_print();
1000                 rcutorture_shutdown_absorb("rcu_torture_stats");
1001         } while (!kthread_should_stop());
1002         VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1003         return 0;
1004 }
1005
1006 static int rcu_idle_cpu;        /* Force all torture tasks off this CPU */
1007
1008 /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1009  * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1010  */
1011 static void rcu_torture_shuffle_tasks(void)
1012 {
1013         int i;
1014
1015         cpumask_setall(shuffle_tmp_mask);
1016         get_online_cpus();
1017
1018         /* No point in shuffling if there is only one online CPU (ex: UP) */
1019         if (num_online_cpus() == 1) {
1020                 put_online_cpus();
1021                 return;
1022         }
1023
1024         if (rcu_idle_cpu != -1)
1025                 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
1026
1027         set_cpus_allowed_ptr(current, shuffle_tmp_mask);
1028
1029         if (reader_tasks) {
1030                 for (i = 0; i < nrealreaders; i++)
1031                         if (reader_tasks[i])
1032                                 set_cpus_allowed_ptr(reader_tasks[i],
1033                                                      shuffle_tmp_mask);
1034         }
1035
1036         if (fakewriter_tasks) {
1037                 for (i = 0; i < nfakewriters; i++)
1038                         if (fakewriter_tasks[i])
1039                                 set_cpus_allowed_ptr(fakewriter_tasks[i],
1040                                                      shuffle_tmp_mask);
1041         }
1042
1043         if (writer_task)
1044                 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
1045
1046         if (stats_task)
1047                 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
1048
1049         if (rcu_idle_cpu == -1)
1050                 rcu_idle_cpu = num_online_cpus() - 1;
1051         else
1052                 rcu_idle_cpu--;
1053
1054         put_online_cpus();
1055 }
1056
1057 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1058  * system to become idle at a time and cut off its timer ticks. This is meant
1059  * to test the support for such tickless idle CPU in RCU.
1060  */
1061 static int
1062 rcu_torture_shuffle(void *arg)
1063 {
1064         VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1065         do {
1066                 schedule_timeout_interruptible(shuffle_interval * HZ);
1067                 rcu_torture_shuffle_tasks();
1068                 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1069         } while (!kthread_should_stop());
1070         VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1071         return 0;
1072 }
1073
1074 /* Cause the rcutorture test to "stutter", starting and stopping all
1075  * threads periodically.
1076  */
1077 static int
1078 rcu_torture_stutter(void *arg)
1079 {
1080         VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1081         do {
1082                 schedule_timeout_interruptible(stutter * HZ);
1083                 stutter_pause_test = 1;
1084                 if (!kthread_should_stop())
1085                         schedule_timeout_interruptible(stutter * HZ);
1086                 stutter_pause_test = 0;
1087                 rcutorture_shutdown_absorb("rcu_torture_stutter");
1088         } while (!kthread_should_stop());
1089         VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1090         return 0;
1091 }
1092
1093 static inline void
1094 rcu_torture_print_module_parms(char *tag)
1095 {
1096         printk(KERN_ALERT "%s" TORTURE_FLAG
1097                 "--- %s: nreaders=%d nfakewriters=%d "
1098                 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1099                 "shuffle_interval=%d stutter=%d irqreader=%d "
1100                 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d\n",
1101                 torture_type, tag, nrealreaders, nfakewriters,
1102                 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1103                 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter);
1104 }
1105
1106 static struct notifier_block rcutorture_nb = {
1107         .notifier_call = rcutorture_shutdown_notify,
1108 };
1109
1110 static void
1111 rcu_torture_cleanup(void)
1112 {
1113         int i;
1114
1115         mutex_lock(&fullstop_mutex);
1116         if (fullstop == FULLSTOP_SHUTDOWN) {
1117                 printk(KERN_WARNING /* but going down anyway, so... */
1118                        "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
1119                 mutex_unlock(&fullstop_mutex);
1120                 schedule_timeout_uninterruptible(10);
1121                 if (cur_ops->cb_barrier != NULL)
1122                         cur_ops->cb_barrier();
1123                 return;
1124         }
1125         fullstop = FULLSTOP_RMMOD;
1126         mutex_unlock(&fullstop_mutex);
1127         unregister_reboot_notifier(&rcutorture_nb);
1128         if (stutter_task) {
1129                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1130                 kthread_stop(stutter_task);
1131         }
1132         stutter_task = NULL;
1133         if (shuffler_task) {
1134                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1135                 kthread_stop(shuffler_task);
1136                 free_cpumask_var(shuffle_tmp_mask);
1137         }
1138         shuffler_task = NULL;
1139
1140         if (writer_task) {
1141                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1142                 kthread_stop(writer_task);
1143         }
1144         writer_task = NULL;
1145
1146         if (reader_tasks) {
1147                 for (i = 0; i < nrealreaders; i++) {
1148                         if (reader_tasks[i]) {
1149                                 VERBOSE_PRINTK_STRING(
1150                                         "Stopping rcu_torture_reader task");
1151                                 kthread_stop(reader_tasks[i]);
1152                         }
1153                         reader_tasks[i] = NULL;
1154                 }
1155                 kfree(reader_tasks);
1156                 reader_tasks = NULL;
1157         }
1158         rcu_torture_current = NULL;
1159
1160         if (fakewriter_tasks) {
1161                 for (i = 0; i < nfakewriters; i++) {
1162                         if (fakewriter_tasks[i]) {
1163                                 VERBOSE_PRINTK_STRING(
1164                                         "Stopping rcu_torture_fakewriter task");
1165                                 kthread_stop(fakewriter_tasks[i]);
1166                         }
1167                         fakewriter_tasks[i] = NULL;
1168                 }
1169                 kfree(fakewriter_tasks);
1170                 fakewriter_tasks = NULL;
1171         }
1172
1173         if (stats_task) {
1174                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1175                 kthread_stop(stats_task);
1176         }
1177         stats_task = NULL;
1178
1179         if (fqs_task) {
1180                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1181                 kthread_stop(fqs_task);
1182         }
1183         fqs_task = NULL;
1184
1185         /* Wait for all RCU callbacks to fire.  */
1186
1187         if (cur_ops->cb_barrier != NULL)
1188                 cur_ops->cb_barrier();
1189
1190         rcu_torture_stats_print();  /* -After- the stats thread is stopped! */
1191
1192         if (cur_ops->cleanup)
1193                 cur_ops->cleanup();
1194         if (atomic_read(&n_rcu_torture_error))
1195                 rcu_torture_print_module_parms("End of test: FAILURE");
1196         else
1197                 rcu_torture_print_module_parms("End of test: SUCCESS");
1198 }
1199
1200 static int __init
1201 rcu_torture_init(void)
1202 {
1203         int i;
1204         int cpu;
1205         int firsterr = 0;
1206         static struct rcu_torture_ops *torture_ops[] =
1207                 { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
1208                   &rcu_bh_ops, &rcu_bh_sync_ops,
1209                   &srcu_ops, &srcu_expedited_ops,
1210                   &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
1211
1212         mutex_lock(&fullstop_mutex);
1213
1214         /* Process args and tell the world that the torturer is on the job. */
1215         for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1216                 cur_ops = torture_ops[i];
1217                 if (strcmp(torture_type, cur_ops->name) == 0)
1218                         break;
1219         }
1220         if (i == ARRAY_SIZE(torture_ops)) {
1221                 printk(KERN_ALERT "rcu-torture: invalid torture type: \"%s\"\n",
1222                        torture_type);
1223                 printk(KERN_ALERT "rcu-torture types:");
1224                 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
1225                         printk(KERN_ALERT " %s", torture_ops[i]->name);
1226                 printk(KERN_ALERT "\n");
1227                 mutex_unlock(&fullstop_mutex);
1228                 return -EINVAL;
1229         }
1230         if (cur_ops->fqs == NULL && fqs_duration != 0) {
1231                 printk(KERN_ALERT "rcu-torture: ->fqs NULL and non-zero "
1232                                   "fqs_duration, fqs disabled.\n");
1233                 fqs_duration = 0;
1234         }
1235         if (cur_ops->init)
1236                 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1237
1238         if (nreaders >= 0)
1239                 nrealreaders = nreaders;
1240         else
1241                 nrealreaders = 2 * num_online_cpus();
1242         rcu_torture_print_module_parms("Start of test");
1243         fullstop = FULLSTOP_DONTSTOP;
1244
1245         /* Set up the freelist. */
1246
1247         INIT_LIST_HEAD(&rcu_torture_freelist);
1248         for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1249                 rcu_tortures[i].rtort_mbtest = 0;
1250                 list_add_tail(&rcu_tortures[i].rtort_free,
1251                               &rcu_torture_freelist);
1252         }
1253
1254         /* Initialize the statistics so that each run gets its own numbers. */
1255
1256         rcu_torture_current = NULL;
1257         rcu_torture_current_version = 0;
1258         atomic_set(&n_rcu_torture_alloc, 0);
1259         atomic_set(&n_rcu_torture_alloc_fail, 0);
1260         atomic_set(&n_rcu_torture_free, 0);
1261         atomic_set(&n_rcu_torture_mberror, 0);
1262         atomic_set(&n_rcu_torture_error, 0);
1263         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1264                 atomic_set(&rcu_torture_wcount[i], 0);
1265         for_each_possible_cpu(cpu) {
1266                 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1267                         per_cpu(rcu_torture_count, cpu)[i] = 0;
1268                         per_cpu(rcu_torture_batch, cpu)[i] = 0;
1269                 }
1270         }
1271
1272         /* Start up the kthreads. */
1273
1274         VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1275         writer_task = kthread_run(rcu_torture_writer, NULL,
1276                                   "rcu_torture_writer");
1277         if (IS_ERR(writer_task)) {
1278                 firsterr = PTR_ERR(writer_task);
1279                 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1280                 writer_task = NULL;
1281                 goto unwind;
1282         }
1283         fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1284                                    GFP_KERNEL);
1285         if (fakewriter_tasks == NULL) {
1286                 VERBOSE_PRINTK_ERRSTRING("out of memory");
1287                 firsterr = -ENOMEM;
1288                 goto unwind;
1289         }
1290         for (i = 0; i < nfakewriters; i++) {
1291                 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1292                 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1293                                                   "rcu_torture_fakewriter");
1294                 if (IS_ERR(fakewriter_tasks[i])) {
1295                         firsterr = PTR_ERR(fakewriter_tasks[i]);
1296                         VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1297                         fakewriter_tasks[i] = NULL;
1298                         goto unwind;
1299                 }
1300         }
1301         reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
1302                                GFP_KERNEL);
1303         if (reader_tasks == NULL) {
1304                 VERBOSE_PRINTK_ERRSTRING("out of memory");
1305                 firsterr = -ENOMEM;
1306                 goto unwind;
1307         }
1308         for (i = 0; i < nrealreaders; i++) {
1309                 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1310                 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1311                                               "rcu_torture_reader");
1312                 if (IS_ERR(reader_tasks[i])) {
1313                         firsterr = PTR_ERR(reader_tasks[i]);
1314                         VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1315                         reader_tasks[i] = NULL;
1316                         goto unwind;
1317                 }
1318         }
1319         if (stat_interval > 0) {
1320                 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1321                 stats_task = kthread_run(rcu_torture_stats, NULL,
1322                                         "rcu_torture_stats");
1323                 if (IS_ERR(stats_task)) {
1324                         firsterr = PTR_ERR(stats_task);
1325                         VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1326                         stats_task = NULL;
1327                         goto unwind;
1328                 }
1329         }
1330         if (test_no_idle_hz) {
1331                 rcu_idle_cpu = num_online_cpus() - 1;
1332
1333                 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
1334                         firsterr = -ENOMEM;
1335                         VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
1336                         goto unwind;
1337                 }
1338
1339                 /* Create the shuffler thread */
1340                 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1341                                           "rcu_torture_shuffle");
1342                 if (IS_ERR(shuffler_task)) {
1343                         free_cpumask_var(shuffle_tmp_mask);
1344                         firsterr = PTR_ERR(shuffler_task);
1345                         VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1346                         shuffler_task = NULL;
1347                         goto unwind;
1348                 }
1349         }
1350         if (stutter < 0)
1351                 stutter = 0;
1352         if (stutter) {
1353                 /* Create the stutter thread */
1354                 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1355                                           "rcu_torture_stutter");
1356                 if (IS_ERR(stutter_task)) {
1357                         firsterr = PTR_ERR(stutter_task);
1358                         VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1359                         stutter_task = NULL;
1360                         goto unwind;
1361                 }
1362         }
1363         if (fqs_duration < 0)
1364                 fqs_duration = 0;
1365         if (fqs_duration) {
1366                 /* Create the stutter thread */
1367                 fqs_task = kthread_run(rcu_torture_fqs, NULL,
1368                                        "rcu_torture_fqs");
1369                 if (IS_ERR(fqs_task)) {
1370                         firsterr = PTR_ERR(fqs_task);
1371                         VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
1372                         fqs_task = NULL;
1373                         goto unwind;
1374                 }
1375         }
1376         register_reboot_notifier(&rcutorture_nb);
1377         mutex_unlock(&fullstop_mutex);
1378         return 0;
1379
1380 unwind:
1381         mutex_unlock(&fullstop_mutex);
1382         rcu_torture_cleanup();
1383         return firsterr;
1384 }
1385
1386 module_init(rcu_torture_init);
1387 module_exit(rcu_torture_cleanup);