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