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