rcu, rcutorture: make quiescent rcutorture less power-hungry
[linux-2.6.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/freezer.h>
43 #include <linux/cpu.h>
44 #include <linux/delay.h>
45 #include <linux/byteorder/swabb.h>
46 #include <linux/stat.h>
47 #include <linux/srcu.h>
48 #include <linux/slab.h>
49
50 MODULE_LICENSE("GPL");
51 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
52               "Josh Triplett <josh@freedesktop.org>");
53
54 static int nreaders = -1;       /* # reader threads, defaults to 2*ncpus */
55 static int nfakewriters = 4;    /* # fake writer threads */
56 static int stat_interval;       /* Interval between stats, in seconds. */
57                                 /*  Defaults to "only at end of test". */
58 static int verbose;             /* Print more debug info. */
59 static int test_no_idle_hz;     /* Test RCU's support for tickless idle CPUs. */
60 static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
61 static int stutter = 5;         /* Start/stop testing interval (in sec) */
62 static char *torture_type = "rcu"; /* What RCU implementation to torture. */
63
64 module_param(nreaders, int, 0444);
65 MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
66 module_param(nfakewriters, int, 0444);
67 MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
68 module_param(stat_interval, int, 0444);
69 MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
70 module_param(verbose, bool, 0444);
71 MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
72 module_param(test_no_idle_hz, bool, 0444);
73 MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
74 module_param(shuffle_interval, int, 0444);
75 MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
76 module_param(stutter, int, 0444);
77 MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
78 module_param(torture_type, charp, 0444);
79 MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
80
81 #define TORTURE_FLAG "-torture:"
82 #define PRINTK_STRING(s) \
83         do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
84 #define VERBOSE_PRINTK_STRING(s) \
85         do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
86 #define VERBOSE_PRINTK_ERRSTRING(s) \
87         do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
88
89 static char printk_buf[4096];
90
91 static int nrealreaders;
92 static struct task_struct *writer_task;
93 static struct task_struct **fakewriter_tasks;
94 static struct task_struct **reader_tasks;
95 static struct task_struct *stats_task;
96 static struct task_struct *shuffler_task;
97 static struct task_struct *stutter_task;
98
99 #define RCU_TORTURE_PIPE_LEN 10
100
101 struct rcu_torture {
102         struct rcu_head rtort_rcu;
103         int rtort_pipe_count;
104         struct list_head rtort_free;
105         int rtort_mbtest;
106 };
107
108 static int fullstop = 0;        /* stop generating callbacks at test end. */
109 static LIST_HEAD(rcu_torture_freelist);
110 static struct rcu_torture *rcu_torture_current = NULL;
111 static long rcu_torture_current_version = 0;
112 static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
113 static DEFINE_SPINLOCK(rcu_torture_lock);
114 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
115         { 0 };
116 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
117         { 0 };
118 static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
119 static atomic_t n_rcu_torture_alloc;
120 static atomic_t n_rcu_torture_alloc_fail;
121 static atomic_t n_rcu_torture_free;
122 static atomic_t n_rcu_torture_mberror;
123 static atomic_t n_rcu_torture_error;
124 static struct list_head rcu_torture_removed;
125
126 static int stutter_pause_test = 0;
127
128 #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
129 #define RCUTORTURE_RUNNABLE_INIT 1
130 #else
131 #define RCUTORTURE_RUNNABLE_INIT 0
132 #endif
133 int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
134
135 /*
136  * Allocate an element from the rcu_tortures pool.
137  */
138 static struct rcu_torture *
139 rcu_torture_alloc(void)
140 {
141         struct list_head *p;
142
143         spin_lock_bh(&rcu_torture_lock);
144         if (list_empty(&rcu_torture_freelist)) {
145                 atomic_inc(&n_rcu_torture_alloc_fail);
146                 spin_unlock_bh(&rcu_torture_lock);
147                 return NULL;
148         }
149         atomic_inc(&n_rcu_torture_alloc);
150         p = rcu_torture_freelist.next;
151         list_del_init(p);
152         spin_unlock_bh(&rcu_torture_lock);
153         return container_of(p, struct rcu_torture, rtort_free);
154 }
155
156 /*
157  * Free an element to the rcu_tortures pool.
158  */
159 static void
160 rcu_torture_free(struct rcu_torture *p)
161 {
162         atomic_inc(&n_rcu_torture_free);
163         spin_lock_bh(&rcu_torture_lock);
164         list_add_tail(&p->rtort_free, &rcu_torture_freelist);
165         spin_unlock_bh(&rcu_torture_lock);
166 }
167
168 struct rcu_random_state {
169         unsigned long rrs_state;
170         long rrs_count;
171 };
172
173 #define RCU_RANDOM_MULT 39916801  /* prime */
174 #define RCU_RANDOM_ADD  479001701 /* prime */
175 #define RCU_RANDOM_REFRESH 10000
176
177 #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
178
179 /*
180  * Crude but fast random-number generator.  Uses a linear congruential
181  * generator, with occasional help from cpu_clock().
182  */
183 static unsigned long
184 rcu_random(struct rcu_random_state *rrsp)
185 {
186         if (--rrsp->rrs_count < 0) {
187                 rrsp->rrs_state +=
188                         (unsigned long)cpu_clock(raw_smp_processor_id());
189                 rrsp->rrs_count = RCU_RANDOM_REFRESH;
190         }
191         rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
192         return swahw32(rrsp->rrs_state);
193 }
194
195 static void
196 rcu_stutter_wait(void)
197 {
198         while (stutter_pause_test || !rcutorture_runnable)
199                 if (rcutorture_runnable)
200                         schedule_timeout_interruptible(1);
201                 else
202                         schedule_timeout_interruptible(HZ);
203 }
204
205 /*
206  * Operations vector for selecting different types of tests.
207  */
208
209 struct rcu_torture_ops {
210         void (*init)(void);
211         void (*cleanup)(void);
212         int (*readlock)(void);
213         void (*readdelay)(struct rcu_random_state *rrsp);
214         void (*readunlock)(int idx);
215         int (*completed)(void);
216         void (*deferredfree)(struct rcu_torture *p);
217         void (*sync)(void);
218         void (*cb_barrier)(void);
219         int (*stats)(char *page);
220         char *name;
221 };
222 static struct rcu_torture_ops *cur_ops = NULL;
223
224 /*
225  * Definitions for rcu torture testing.
226  */
227
228 static int rcu_torture_read_lock(void) __acquires(RCU)
229 {
230         rcu_read_lock();
231         return 0;
232 }
233
234 static void rcu_read_delay(struct rcu_random_state *rrsp)
235 {
236         long delay;
237         const long longdelay = 200;
238
239         /* We want there to be long-running readers, but not all the time. */
240
241         delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay);
242         if (!delay)
243                 udelay(longdelay);
244 }
245
246 static void rcu_torture_read_unlock(int idx) __releases(RCU)
247 {
248         rcu_read_unlock();
249 }
250
251 static int rcu_torture_completed(void)
252 {
253         return rcu_batches_completed();
254 }
255
256 static void
257 rcu_torture_cb(struct rcu_head *p)
258 {
259         int i;
260         struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
261
262         if (fullstop) {
263                 /* Test is ending, just drop callbacks on the floor. */
264                 /* The next initialization will pick up the pieces. */
265                 return;
266         }
267         i = rp->rtort_pipe_count;
268         if (i > RCU_TORTURE_PIPE_LEN)
269                 i = RCU_TORTURE_PIPE_LEN;
270         atomic_inc(&rcu_torture_wcount[i]);
271         if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
272                 rp->rtort_mbtest = 0;
273                 rcu_torture_free(rp);
274         } else
275                 cur_ops->deferredfree(rp);
276 }
277
278 static void rcu_torture_deferred_free(struct rcu_torture *p)
279 {
280         call_rcu(&p->rtort_rcu, rcu_torture_cb);
281 }
282
283 static struct rcu_torture_ops rcu_ops = {
284         .init = NULL,
285         .cleanup = NULL,
286         .readlock = rcu_torture_read_lock,
287         .readdelay = rcu_read_delay,
288         .readunlock = rcu_torture_read_unlock,
289         .completed = rcu_torture_completed,
290         .deferredfree = rcu_torture_deferred_free,
291         .sync = synchronize_rcu,
292         .cb_barrier = rcu_barrier,
293         .stats = NULL,
294         .name = "rcu"
295 };
296
297 static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
298 {
299         int i;
300         struct rcu_torture *rp;
301         struct rcu_torture *rp1;
302
303         cur_ops->sync();
304         list_add(&p->rtort_free, &rcu_torture_removed);
305         list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
306                 i = rp->rtort_pipe_count;
307                 if (i > RCU_TORTURE_PIPE_LEN)
308                         i = RCU_TORTURE_PIPE_LEN;
309                 atomic_inc(&rcu_torture_wcount[i]);
310                 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
311                         rp->rtort_mbtest = 0;
312                         list_del(&rp->rtort_free);
313                         rcu_torture_free(rp);
314                 }
315         }
316 }
317
318 static void rcu_sync_torture_init(void)
319 {
320         INIT_LIST_HEAD(&rcu_torture_removed);
321 }
322
323 static struct rcu_torture_ops rcu_sync_ops = {
324         .init = rcu_sync_torture_init,
325         .cleanup = NULL,
326         .readlock = rcu_torture_read_lock,
327         .readdelay = rcu_read_delay,
328         .readunlock = rcu_torture_read_unlock,
329         .completed = rcu_torture_completed,
330         .deferredfree = rcu_sync_torture_deferred_free,
331         .sync = synchronize_rcu,
332         .cb_barrier = NULL,
333         .stats = NULL,
334         .name = "rcu_sync"
335 };
336
337 /*
338  * Definitions for rcu_bh torture testing.
339  */
340
341 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
342 {
343         rcu_read_lock_bh();
344         return 0;
345 }
346
347 static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
348 {
349         rcu_read_unlock_bh();
350 }
351
352 static int rcu_bh_torture_completed(void)
353 {
354         return rcu_batches_completed_bh();
355 }
356
357 static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
358 {
359         call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
360 }
361
362 struct rcu_bh_torture_synchronize {
363         struct rcu_head head;
364         struct completion completion;
365 };
366
367 static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
368 {
369         struct rcu_bh_torture_synchronize *rcu;
370
371         rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
372         complete(&rcu->completion);
373 }
374
375 static void rcu_bh_torture_synchronize(void)
376 {
377         struct rcu_bh_torture_synchronize rcu;
378
379         init_completion(&rcu.completion);
380         call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
381         wait_for_completion(&rcu.completion);
382 }
383
384 static struct rcu_torture_ops rcu_bh_ops = {
385         .init = NULL,
386         .cleanup = NULL,
387         .readlock = rcu_bh_torture_read_lock,
388         .readdelay = rcu_read_delay,  /* just reuse rcu's version. */
389         .readunlock = rcu_bh_torture_read_unlock,
390         .completed = rcu_bh_torture_completed,
391         .deferredfree = rcu_bh_torture_deferred_free,
392         .sync = rcu_bh_torture_synchronize,
393         .cb_barrier = rcu_barrier_bh,
394         .stats = NULL,
395         .name = "rcu_bh"
396 };
397
398 static struct rcu_torture_ops rcu_bh_sync_ops = {
399         .init = rcu_sync_torture_init,
400         .cleanup = NULL,
401         .readlock = rcu_bh_torture_read_lock,
402         .readdelay = rcu_read_delay,  /* just reuse rcu's version. */
403         .readunlock = rcu_bh_torture_read_unlock,
404         .completed = rcu_bh_torture_completed,
405         .deferredfree = rcu_sync_torture_deferred_free,
406         .sync = rcu_bh_torture_synchronize,
407         .cb_barrier = NULL,
408         .stats = NULL,
409         .name = "rcu_bh_sync"
410 };
411
412 /*
413  * Definitions for srcu torture testing.
414  */
415
416 static struct srcu_struct srcu_ctl;
417
418 static void srcu_torture_init(void)
419 {
420         init_srcu_struct(&srcu_ctl);
421         rcu_sync_torture_init();
422 }
423
424 static void srcu_torture_cleanup(void)
425 {
426         synchronize_srcu(&srcu_ctl);
427         cleanup_srcu_struct(&srcu_ctl);
428 }
429
430 static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
431 {
432         return srcu_read_lock(&srcu_ctl);
433 }
434
435 static void srcu_read_delay(struct rcu_random_state *rrsp)
436 {
437         long delay;
438         const long uspertick = 1000000 / HZ;
439         const long longdelay = 10;
440
441         /* We want there to be long-running readers, but not all the time. */
442
443         delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
444         if (!delay)
445                 schedule_timeout_interruptible(longdelay);
446 }
447
448 static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
449 {
450         srcu_read_unlock(&srcu_ctl, idx);
451 }
452
453 static int srcu_torture_completed(void)
454 {
455         return srcu_batches_completed(&srcu_ctl);
456 }
457
458 static void srcu_torture_synchronize(void)
459 {
460         synchronize_srcu(&srcu_ctl);
461 }
462
463 static int srcu_torture_stats(char *page)
464 {
465         int cnt = 0;
466         int cpu;
467         int idx = srcu_ctl.completed & 0x1;
468
469         cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
470                        torture_type, TORTURE_FLAG, idx);
471         for_each_possible_cpu(cpu) {
472                 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
473                                per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
474                                per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
475         }
476         cnt += sprintf(&page[cnt], "\n");
477         return cnt;
478 }
479
480 static struct rcu_torture_ops srcu_ops = {
481         .init = srcu_torture_init,
482         .cleanup = srcu_torture_cleanup,
483         .readlock = srcu_torture_read_lock,
484         .readdelay = srcu_read_delay,
485         .readunlock = srcu_torture_read_unlock,
486         .completed = srcu_torture_completed,
487         .deferredfree = rcu_sync_torture_deferred_free,
488         .sync = srcu_torture_synchronize,
489         .cb_barrier = NULL,
490         .stats = srcu_torture_stats,
491         .name = "srcu"
492 };
493
494 /*
495  * Definitions for sched torture testing.
496  */
497
498 static int sched_torture_read_lock(void)
499 {
500         preempt_disable();
501         return 0;
502 }
503
504 static void sched_torture_read_unlock(int idx)
505 {
506         preempt_enable();
507 }
508
509 static int sched_torture_completed(void)
510 {
511         return 0;
512 }
513
514 static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
515 {
516         call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
517 }
518
519 static void sched_torture_synchronize(void)
520 {
521         synchronize_sched();
522 }
523
524 static struct rcu_torture_ops sched_ops = {
525         .init = rcu_sync_torture_init,
526         .cleanup = NULL,
527         .readlock = sched_torture_read_lock,
528         .readdelay = rcu_read_delay,  /* just reuse rcu's version. */
529         .readunlock = sched_torture_read_unlock,
530         .completed = sched_torture_completed,
531         .deferredfree = rcu_sched_torture_deferred_free,
532         .sync = sched_torture_synchronize,
533         .cb_barrier = rcu_barrier_sched,
534         .stats = NULL,
535         .name = "sched"
536 };
537
538 static struct rcu_torture_ops sched_ops_sync = {
539         .init = rcu_sync_torture_init,
540         .cleanup = NULL,
541         .readlock = sched_torture_read_lock,
542         .readdelay = rcu_read_delay,  /* just reuse rcu's version. */
543         .readunlock = sched_torture_read_unlock,
544         .completed = sched_torture_completed,
545         .deferredfree = rcu_sync_torture_deferred_free,
546         .sync = sched_torture_synchronize,
547         .cb_barrier = NULL,
548         .stats = NULL,
549         .name = "sched_sync"
550 };
551
552 /*
553  * RCU torture writer kthread.  Repeatedly substitutes a new structure
554  * for that pointed to by rcu_torture_current, freeing the old structure
555  * after a series of grace periods (the "pipeline").
556  */
557 static int
558 rcu_torture_writer(void *arg)
559 {
560         int i;
561         long oldbatch = rcu_batches_completed();
562         struct rcu_torture *rp;
563         struct rcu_torture *old_rp;
564         static DEFINE_RCU_RANDOM(rand);
565
566         VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
567         set_user_nice(current, 19);
568
569         do {
570                 schedule_timeout_uninterruptible(1);
571                 if ((rp = rcu_torture_alloc()) == NULL)
572                         continue;
573                 rp->rtort_pipe_count = 0;
574                 udelay(rcu_random(&rand) & 0x3ff);
575                 old_rp = rcu_torture_current;
576                 rp->rtort_mbtest = 1;
577                 rcu_assign_pointer(rcu_torture_current, rp);
578                 smp_wmb();
579                 if (old_rp) {
580                         i = old_rp->rtort_pipe_count;
581                         if (i > RCU_TORTURE_PIPE_LEN)
582                                 i = RCU_TORTURE_PIPE_LEN;
583                         atomic_inc(&rcu_torture_wcount[i]);
584                         old_rp->rtort_pipe_count++;
585                         cur_ops->deferredfree(old_rp);
586                 }
587                 rcu_torture_current_version++;
588                 oldbatch = cur_ops->completed();
589                 rcu_stutter_wait();
590         } while (!kthread_should_stop() && !fullstop);
591         VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
592         while (!kthread_should_stop())
593                 schedule_timeout_uninterruptible(1);
594         return 0;
595 }
596
597 /*
598  * RCU torture fake writer kthread.  Repeatedly calls sync, with a random
599  * delay between calls.
600  */
601 static int
602 rcu_torture_fakewriter(void *arg)
603 {
604         DEFINE_RCU_RANDOM(rand);
605
606         VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
607         set_user_nice(current, 19);
608
609         do {
610                 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
611                 udelay(rcu_random(&rand) & 0x3ff);
612                 cur_ops->sync();
613                 rcu_stutter_wait();
614         } while (!kthread_should_stop() && !fullstop);
615
616         VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
617         while (!kthread_should_stop())
618                 schedule_timeout_uninterruptible(1);
619         return 0;
620 }
621
622 /*
623  * RCU torture reader kthread.  Repeatedly dereferences rcu_torture_current,
624  * incrementing the corresponding element of the pipeline array.  The
625  * counter in the element should never be greater than 1, otherwise, the
626  * RCU implementation is broken.
627  */
628 static int
629 rcu_torture_reader(void *arg)
630 {
631         int completed;
632         int idx;
633         DEFINE_RCU_RANDOM(rand);
634         struct rcu_torture *p;
635         int pipe_count;
636
637         VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
638         set_user_nice(current, 19);
639
640         do {
641                 idx = cur_ops->readlock();
642                 completed = cur_ops->completed();
643                 p = rcu_dereference(rcu_torture_current);
644                 if (p == NULL) {
645                         /* Wait for rcu_torture_writer to get underway */
646                         cur_ops->readunlock(idx);
647                         schedule_timeout_interruptible(HZ);
648                         continue;
649                 }
650                 if (p->rtort_mbtest == 0)
651                         atomic_inc(&n_rcu_torture_mberror);
652                 cur_ops->readdelay(&rand);
653                 preempt_disable();
654                 pipe_count = p->rtort_pipe_count;
655                 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
656                         /* Should not happen, but... */
657                         pipe_count = RCU_TORTURE_PIPE_LEN;
658                 }
659                 ++__get_cpu_var(rcu_torture_count)[pipe_count];
660                 completed = cur_ops->completed() - completed;
661                 if (completed > RCU_TORTURE_PIPE_LEN) {
662                         /* Should not happen, but... */
663                         completed = RCU_TORTURE_PIPE_LEN;
664                 }
665                 ++__get_cpu_var(rcu_torture_batch)[completed];
666                 preempt_enable();
667                 cur_ops->readunlock(idx);
668                 schedule();
669                 rcu_stutter_wait();
670         } while (!kthread_should_stop() && !fullstop);
671         VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
672         while (!kthread_should_stop())
673                 schedule_timeout_uninterruptible(1);
674         return 0;
675 }
676
677 /*
678  * Create an RCU-torture statistics message in the specified buffer.
679  */
680 static int
681 rcu_torture_printk(char *page)
682 {
683         int cnt = 0;
684         int cpu;
685         int i;
686         long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
687         long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
688
689         for_each_possible_cpu(cpu) {
690                 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
691                         pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
692                         batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
693                 }
694         }
695         for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
696                 if (pipesummary[i] != 0)
697                         break;
698         }
699         cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
700         cnt += sprintf(&page[cnt],
701                        "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
702                        "rtmbe: %d",
703                        rcu_torture_current,
704                        rcu_torture_current_version,
705                        list_empty(&rcu_torture_freelist),
706                        atomic_read(&n_rcu_torture_alloc),
707                        atomic_read(&n_rcu_torture_alloc_fail),
708                        atomic_read(&n_rcu_torture_free),
709                        atomic_read(&n_rcu_torture_mberror));
710         if (atomic_read(&n_rcu_torture_mberror) != 0)
711                 cnt += sprintf(&page[cnt], " !!!");
712         cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
713         if (i > 1) {
714                 cnt += sprintf(&page[cnt], "!!! ");
715                 atomic_inc(&n_rcu_torture_error);
716                 WARN_ON_ONCE(1);
717         }
718         cnt += sprintf(&page[cnt], "Reader Pipe: ");
719         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
720                 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
721         cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
722         cnt += sprintf(&page[cnt], "Reader Batch: ");
723         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
724                 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
725         cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
726         cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
727         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
728                 cnt += sprintf(&page[cnt], " %d",
729                                atomic_read(&rcu_torture_wcount[i]));
730         }
731         cnt += sprintf(&page[cnt], "\n");
732         if (cur_ops->stats)
733                 cnt += cur_ops->stats(&page[cnt]);
734         return cnt;
735 }
736
737 /*
738  * Print torture statistics.  Caller must ensure that there is only
739  * one call to this function at a given time!!!  This is normally
740  * accomplished by relying on the module system to only have one copy
741  * of the module loaded, and then by giving the rcu_torture_stats
742  * kthread full control (or the init/cleanup functions when rcu_torture_stats
743  * thread is not running).
744  */
745 static void
746 rcu_torture_stats_print(void)
747 {
748         int cnt;
749
750         cnt = rcu_torture_printk(printk_buf);
751         printk(KERN_ALERT "%s", printk_buf);
752 }
753
754 /*
755  * Periodically prints torture statistics, if periodic statistics printing
756  * was specified via the stat_interval module parameter.
757  *
758  * No need to worry about fullstop here, since this one doesn't reference
759  * volatile state or register callbacks.
760  */
761 static int
762 rcu_torture_stats(void *arg)
763 {
764         VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
765         do {
766                 schedule_timeout_interruptible(stat_interval * HZ);
767                 rcu_torture_stats_print();
768         } while (!kthread_should_stop());
769         VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
770         return 0;
771 }
772
773 static int rcu_idle_cpu;        /* Force all torture tasks off this CPU */
774
775 /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
776  * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
777  */
778 static void rcu_torture_shuffle_tasks(void)
779 {
780         cpumask_t tmp_mask;
781         int i;
782
783         cpus_setall(tmp_mask);
784         get_online_cpus();
785
786         /* No point in shuffling if there is only one online CPU (ex: UP) */
787         if (num_online_cpus() == 1) {
788                 put_online_cpus();
789                 return;
790         }
791
792         if (rcu_idle_cpu != -1)
793                 cpu_clear(rcu_idle_cpu, tmp_mask);
794
795         set_cpus_allowed_ptr(current, &tmp_mask);
796
797         if (reader_tasks) {
798                 for (i = 0; i < nrealreaders; i++)
799                         if (reader_tasks[i])
800                                 set_cpus_allowed_ptr(reader_tasks[i],
801                                                      &tmp_mask);
802         }
803
804         if (fakewriter_tasks) {
805                 for (i = 0; i < nfakewriters; i++)
806                         if (fakewriter_tasks[i])
807                                 set_cpus_allowed_ptr(fakewriter_tasks[i],
808                                                      &tmp_mask);
809         }
810
811         if (writer_task)
812                 set_cpus_allowed_ptr(writer_task, &tmp_mask);
813
814         if (stats_task)
815                 set_cpus_allowed_ptr(stats_task, &tmp_mask);
816
817         if (rcu_idle_cpu == -1)
818                 rcu_idle_cpu = num_online_cpus() - 1;
819         else
820                 rcu_idle_cpu--;
821
822         put_online_cpus();
823 }
824
825 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
826  * system to become idle at a time and cut off its timer ticks. This is meant
827  * to test the support for such tickless idle CPU in RCU.
828  */
829 static int
830 rcu_torture_shuffle(void *arg)
831 {
832         VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
833         do {
834                 schedule_timeout_interruptible(shuffle_interval * HZ);
835                 rcu_torture_shuffle_tasks();
836         } while (!kthread_should_stop());
837         VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
838         return 0;
839 }
840
841 /* Cause the rcutorture test to "stutter", starting and stopping all
842  * threads periodically.
843  */
844 static int
845 rcu_torture_stutter(void *arg)
846 {
847         VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
848         do {
849                 schedule_timeout_interruptible(stutter * HZ);
850                 stutter_pause_test = 1;
851                 if (!kthread_should_stop())
852                         schedule_timeout_interruptible(stutter * HZ);
853                 stutter_pause_test = 0;
854         } while (!kthread_should_stop());
855         VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
856         return 0;
857 }
858
859 static inline void
860 rcu_torture_print_module_parms(char *tag)
861 {
862         printk(KERN_ALERT "%s" TORTURE_FLAG
863                 "--- %s: nreaders=%d nfakewriters=%d "
864                 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
865                 "shuffle_interval=%d stutter=%d\n",
866                 torture_type, tag, nrealreaders, nfakewriters,
867                 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
868                 stutter);
869 }
870
871 static void
872 rcu_torture_cleanup(void)
873 {
874         int i;
875
876         fullstop = 1;
877         if (stutter_task) {
878                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
879                 kthread_stop(stutter_task);
880         }
881         stutter_task = NULL;
882         if (shuffler_task) {
883                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
884                 kthread_stop(shuffler_task);
885         }
886         shuffler_task = NULL;
887
888         if (writer_task) {
889                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
890                 kthread_stop(writer_task);
891         }
892         writer_task = NULL;
893
894         if (reader_tasks) {
895                 for (i = 0; i < nrealreaders; i++) {
896                         if (reader_tasks[i]) {
897                                 VERBOSE_PRINTK_STRING(
898                                         "Stopping rcu_torture_reader task");
899                                 kthread_stop(reader_tasks[i]);
900                         }
901                         reader_tasks[i] = NULL;
902                 }
903                 kfree(reader_tasks);
904                 reader_tasks = NULL;
905         }
906         rcu_torture_current = NULL;
907
908         if (fakewriter_tasks) {
909                 for (i = 0; i < nfakewriters; i++) {
910                         if (fakewriter_tasks[i]) {
911                                 VERBOSE_PRINTK_STRING(
912                                         "Stopping rcu_torture_fakewriter task");
913                                 kthread_stop(fakewriter_tasks[i]);
914                         }
915                         fakewriter_tasks[i] = NULL;
916                 }
917                 kfree(fakewriter_tasks);
918                 fakewriter_tasks = NULL;
919         }
920
921         if (stats_task) {
922                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
923                 kthread_stop(stats_task);
924         }
925         stats_task = NULL;
926
927         /* Wait for all RCU callbacks to fire.  */
928
929         if (cur_ops->cb_barrier != NULL)
930                 cur_ops->cb_barrier();
931
932         rcu_torture_stats_print();  /* -After- the stats thread is stopped! */
933
934         if (cur_ops->cleanup)
935                 cur_ops->cleanup();
936         if (atomic_read(&n_rcu_torture_error))
937                 rcu_torture_print_module_parms("End of test: FAILURE");
938         else
939                 rcu_torture_print_module_parms("End of test: SUCCESS");
940 }
941
942 static int __init
943 rcu_torture_init(void)
944 {
945         int i;
946         int cpu;
947         int firsterr = 0;
948         static struct rcu_torture_ops *torture_ops[] =
949                 { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops,
950                   &srcu_ops, &sched_ops, &sched_ops_sync, };
951
952         /* Process args and tell the world that the torturer is on the job. */
953         for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
954                 cur_ops = torture_ops[i];
955                 if (strcmp(torture_type, cur_ops->name) == 0)
956                         break;
957         }
958         if (i == ARRAY_SIZE(torture_ops)) {
959                 printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
960                        torture_type);
961                 return (-EINVAL);
962         }
963         if (cur_ops->init)
964                 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
965
966         if (nreaders >= 0)
967                 nrealreaders = nreaders;
968         else
969                 nrealreaders = 2 * num_online_cpus();
970         rcu_torture_print_module_parms("Start of test");
971         fullstop = 0;
972
973         /* Set up the freelist. */
974
975         INIT_LIST_HEAD(&rcu_torture_freelist);
976         for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
977                 rcu_tortures[i].rtort_mbtest = 0;
978                 list_add_tail(&rcu_tortures[i].rtort_free,
979                               &rcu_torture_freelist);
980         }
981
982         /* Initialize the statistics so that each run gets its own numbers. */
983
984         rcu_torture_current = NULL;
985         rcu_torture_current_version = 0;
986         atomic_set(&n_rcu_torture_alloc, 0);
987         atomic_set(&n_rcu_torture_alloc_fail, 0);
988         atomic_set(&n_rcu_torture_free, 0);
989         atomic_set(&n_rcu_torture_mberror, 0);
990         atomic_set(&n_rcu_torture_error, 0);
991         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
992                 atomic_set(&rcu_torture_wcount[i], 0);
993         for_each_possible_cpu(cpu) {
994                 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
995                         per_cpu(rcu_torture_count, cpu)[i] = 0;
996                         per_cpu(rcu_torture_batch, cpu)[i] = 0;
997                 }
998         }
999
1000         /* Start up the kthreads. */
1001
1002         VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1003         writer_task = kthread_run(rcu_torture_writer, NULL,
1004                                   "rcu_torture_writer");
1005         if (IS_ERR(writer_task)) {
1006                 firsterr = PTR_ERR(writer_task);
1007                 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1008                 writer_task = NULL;
1009                 goto unwind;
1010         }
1011         fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1012                                    GFP_KERNEL);
1013         if (fakewriter_tasks == NULL) {
1014                 VERBOSE_PRINTK_ERRSTRING("out of memory");
1015                 firsterr = -ENOMEM;
1016                 goto unwind;
1017         }
1018         for (i = 0; i < nfakewriters; i++) {
1019                 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1020                 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1021                                                   "rcu_torture_fakewriter");
1022                 if (IS_ERR(fakewriter_tasks[i])) {
1023                         firsterr = PTR_ERR(fakewriter_tasks[i]);
1024                         VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1025                         fakewriter_tasks[i] = NULL;
1026                         goto unwind;
1027                 }
1028         }
1029         reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
1030                                GFP_KERNEL);
1031         if (reader_tasks == NULL) {
1032                 VERBOSE_PRINTK_ERRSTRING("out of memory");
1033                 firsterr = -ENOMEM;
1034                 goto unwind;
1035         }
1036         for (i = 0; i < nrealreaders; i++) {
1037                 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1038                 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1039                                               "rcu_torture_reader");
1040                 if (IS_ERR(reader_tasks[i])) {
1041                         firsterr = PTR_ERR(reader_tasks[i]);
1042                         VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1043                         reader_tasks[i] = NULL;
1044                         goto unwind;
1045                 }
1046         }
1047         if (stat_interval > 0) {
1048                 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1049                 stats_task = kthread_run(rcu_torture_stats, NULL,
1050                                         "rcu_torture_stats");
1051                 if (IS_ERR(stats_task)) {
1052                         firsterr = PTR_ERR(stats_task);
1053                         VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1054                         stats_task = NULL;
1055                         goto unwind;
1056                 }
1057         }
1058         if (test_no_idle_hz) {
1059                 rcu_idle_cpu = num_online_cpus() - 1;
1060                 /* Create the shuffler thread */
1061                 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1062                                           "rcu_torture_shuffle");
1063                 if (IS_ERR(shuffler_task)) {
1064                         firsterr = PTR_ERR(shuffler_task);
1065                         VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1066                         shuffler_task = NULL;
1067                         goto unwind;
1068                 }
1069         }
1070         if (stutter < 0)
1071                 stutter = 0;
1072         if (stutter) {
1073                 /* Create the stutter thread */
1074                 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1075                                           "rcu_torture_stutter");
1076                 if (IS_ERR(stutter_task)) {
1077                         firsterr = PTR_ERR(stutter_task);
1078                         VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1079                         stutter_task = NULL;
1080                         goto unwind;
1081                 }
1082         }
1083         return 0;
1084
1085 unwind:
1086         rcu_torture_cleanup();
1087         return firsterr;
1088 }
1089
1090 module_init(rcu_torture_init);
1091 module_exit(rcu_torture_cleanup);