rcu: Control rcutorture startup from kernel boot parameters
[firefly-linux-kernel-4.4.55.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 <linux/atomic.h>
37 #include <linux/bitops.h>
38 #include <linux/completion.h>
39 #include <linux/moduleparam.h>
40 #include <linux/percpu.h>
41 #include <linux/notifier.h>
42 #include <linux/reboot.h>
43 #include <linux/freezer.h>
44 #include <linux/cpu.h>
45 #include <linux/delay.h>
46 #include <linux/stat.h>
47 #include <linux/srcu.h>
48 #include <linux/slab.h>
49 #include <asm/byteorder.h>
50
51 MODULE_LICENSE("GPL");
52 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
53               "Josh Triplett <josh@freedesktop.org>");
54
55 static int nreaders = -1;       /* # reader threads, defaults to 2*ncpus */
56 static int nfakewriters = 4;    /* # fake writer threads */
57 static int stat_interval;       /* Interval between stats, in seconds. */
58                                 /*  Defaults to "only at end of test". */
59 static int verbose;             /* Print more debug info. */
60 static int test_no_idle_hz;     /* Test RCU's support for tickless idle CPUs. */
61 static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
62 static int stutter = 5;         /* Start/stop testing interval (in sec) */
63 static int irqreader = 1;       /* RCU readers from irq (timers). */
64 static int fqs_duration;        /* Duration of bursts (us), 0 to disable. */
65 static int fqs_holdoff;         /* Hold time within burst (us). */
66 static int fqs_stutter = 3;     /* Wait time between bursts (s). */
67 static int shutdown_secs;       /* Shutdown time (s).  <=0 for no shutdown. */
68 static int test_boost = 1;      /* Test RCU prio boost: 0=no, 1=maybe, 2=yes. */
69 static int test_boost_interval = 7; /* Interval between boost tests, seconds. */
70 static int test_boost_duration = 4; /* Duration of each boost test, seconds. */
71 static char *torture_type = "rcu"; /* What RCU implementation to torture. */
72
73 module_param(nreaders, int, 0444);
74 MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
75 module_param(nfakewriters, int, 0444);
76 MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
77 module_param(stat_interval, int, 0644);
78 MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
79 module_param(verbose, bool, 0444);
80 MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
81 module_param(test_no_idle_hz, bool, 0444);
82 MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
83 module_param(shuffle_interval, int, 0444);
84 MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
85 module_param(stutter, int, 0444);
86 MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
87 module_param(irqreader, int, 0444);
88 MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
89 module_param(fqs_duration, int, 0444);
90 MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
91 module_param(fqs_holdoff, int, 0444);
92 MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
93 module_param(fqs_stutter, int, 0444);
94 MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
95 module_param(shutdown_secs, int, 0444);
96 MODULE_PARM_DESC(shutdown_secs, "Shutdown time (s), zero to disable.");
97 module_param(test_boost, int, 0444);
98 MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
99 module_param(test_boost_interval, int, 0444);
100 MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
101 module_param(test_boost_duration, int, 0444);
102 MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
103 module_param(torture_type, charp, 0444);
104 MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
105
106 #define TORTURE_FLAG "-torture:"
107 #define PRINTK_STRING(s) \
108         do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
109 #define VERBOSE_PRINTK_STRING(s) \
110         do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
111 #define VERBOSE_PRINTK_ERRSTRING(s) \
112         do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
113
114 static char printk_buf[4096];
115
116 static int nrealreaders;
117 static struct task_struct *writer_task;
118 static struct task_struct **fakewriter_tasks;
119 static struct task_struct **reader_tasks;
120 static struct task_struct *stats_task;
121 static struct task_struct *shuffler_task;
122 static struct task_struct *stutter_task;
123 static struct task_struct *fqs_task;
124 static struct task_struct *boost_tasks[NR_CPUS];
125 static struct task_struct *shutdown_task;
126
127 #define RCU_TORTURE_PIPE_LEN 10
128
129 struct rcu_torture {
130         struct rcu_head rtort_rcu;
131         int rtort_pipe_count;
132         struct list_head rtort_free;
133         int rtort_mbtest;
134 };
135
136 static LIST_HEAD(rcu_torture_freelist);
137 static struct rcu_torture __rcu *rcu_torture_current;
138 static unsigned long rcu_torture_current_version;
139 static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
140 static DEFINE_SPINLOCK(rcu_torture_lock);
141 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
142         { 0 };
143 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
144         { 0 };
145 static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
146 static atomic_t n_rcu_torture_alloc;
147 static atomic_t n_rcu_torture_alloc_fail;
148 static atomic_t n_rcu_torture_free;
149 static atomic_t n_rcu_torture_mberror;
150 static atomic_t n_rcu_torture_error;
151 static long n_rcu_torture_boost_ktrerror;
152 static long n_rcu_torture_boost_rterror;
153 static long n_rcu_torture_boost_failure;
154 static long n_rcu_torture_boosts;
155 static long n_rcu_torture_timers;
156 static struct list_head rcu_torture_removed;
157 static cpumask_var_t shuffle_tmp_mask;
158
159 static int stutter_pause_test;
160
161 #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
162 #define RCUTORTURE_RUNNABLE_INIT 1
163 #else
164 #define RCUTORTURE_RUNNABLE_INIT 0
165 #endif
166 int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
167 module_param(rcutorture_runnable, int, 0444);
168 MODULE_PARM_DESC(rcutorture_runnable, "Start rcutorture at boot");
169
170 #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
171 #define rcu_can_boost() 1
172 #else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
173 #define rcu_can_boost() 0
174 #endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
175
176 static unsigned long shutdown_time;     /* jiffies to system shutdown. */
177 static unsigned long boost_starttime;   /* jiffies of next boost test start. */
178 DEFINE_MUTEX(boost_mutex);              /* protect setting boost_starttime */
179                                         /*  and boost task create/destroy. */
180
181 /* Mediate rmmod and system shutdown.  Concurrent rmmod & shutdown illegal! */
182
183 #define FULLSTOP_DONTSTOP 0     /* Normal operation. */
184 #define FULLSTOP_SHUTDOWN 1     /* System shutdown with rcutorture running. */
185 #define FULLSTOP_RMMOD    2     /* Normal rmmod of rcutorture. */
186 static int fullstop = FULLSTOP_RMMOD;
187 /*
188  * Protect fullstop transitions and spawning of kthreads.
189  */
190 static DEFINE_MUTEX(fullstop_mutex);
191
192 /* Forward reference. */
193 static void rcu_torture_cleanup(void);
194
195 /*
196  * Detect and respond to a system shutdown.
197  */
198 static int
199 rcutorture_shutdown_notify(struct notifier_block *unused1,
200                            unsigned long unused2, void *unused3)
201 {
202         mutex_lock(&fullstop_mutex);
203         if (fullstop == FULLSTOP_DONTSTOP)
204                 fullstop = FULLSTOP_SHUTDOWN;
205         else
206                 printk(KERN_WARNING /* but going down anyway, so... */
207                        "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
208         mutex_unlock(&fullstop_mutex);
209         return NOTIFY_DONE;
210 }
211
212 /*
213  * Absorb kthreads into a kernel function that won't return, so that
214  * they won't ever access module text or data again.
215  */
216 static void rcutorture_shutdown_absorb(char *title)
217 {
218         if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
219                 printk(KERN_NOTICE
220                        "rcutorture thread %s parking due to system shutdown\n",
221                        title);
222                 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
223         }
224 }
225
226 /*
227  * Allocate an element from the rcu_tortures pool.
228  */
229 static struct rcu_torture *
230 rcu_torture_alloc(void)
231 {
232         struct list_head *p;
233
234         spin_lock_bh(&rcu_torture_lock);
235         if (list_empty(&rcu_torture_freelist)) {
236                 atomic_inc(&n_rcu_torture_alloc_fail);
237                 spin_unlock_bh(&rcu_torture_lock);
238                 return NULL;
239         }
240         atomic_inc(&n_rcu_torture_alloc);
241         p = rcu_torture_freelist.next;
242         list_del_init(p);
243         spin_unlock_bh(&rcu_torture_lock);
244         return container_of(p, struct rcu_torture, rtort_free);
245 }
246
247 /*
248  * Free an element to the rcu_tortures pool.
249  */
250 static void
251 rcu_torture_free(struct rcu_torture *p)
252 {
253         atomic_inc(&n_rcu_torture_free);
254         spin_lock_bh(&rcu_torture_lock);
255         list_add_tail(&p->rtort_free, &rcu_torture_freelist);
256         spin_unlock_bh(&rcu_torture_lock);
257 }
258
259 struct rcu_random_state {
260         unsigned long rrs_state;
261         long rrs_count;
262 };
263
264 #define RCU_RANDOM_MULT 39916801  /* prime */
265 #define RCU_RANDOM_ADD  479001701 /* prime */
266 #define RCU_RANDOM_REFRESH 10000
267
268 #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
269
270 /*
271  * Crude but fast random-number generator.  Uses a linear congruential
272  * generator, with occasional help from cpu_clock().
273  */
274 static unsigned long
275 rcu_random(struct rcu_random_state *rrsp)
276 {
277         if (--rrsp->rrs_count < 0) {
278                 rrsp->rrs_state += (unsigned long)local_clock();
279                 rrsp->rrs_count = RCU_RANDOM_REFRESH;
280         }
281         rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
282         return swahw32(rrsp->rrs_state);
283 }
284
285 static void
286 rcu_stutter_wait(char *title)
287 {
288         while (stutter_pause_test || !rcutorture_runnable) {
289                 if (rcutorture_runnable)
290                         schedule_timeout_interruptible(1);
291                 else
292                         schedule_timeout_interruptible(round_jiffies_relative(HZ));
293                 rcutorture_shutdown_absorb(title);
294         }
295 }
296
297 /*
298  * Operations vector for selecting different types of tests.
299  */
300
301 struct rcu_torture_ops {
302         void (*init)(void);
303         void (*cleanup)(void);
304         int (*readlock)(void);
305         void (*read_delay)(struct rcu_random_state *rrsp);
306         void (*readunlock)(int idx);
307         int (*completed)(void);
308         void (*deferred_free)(struct rcu_torture *p);
309         void (*sync)(void);
310         void (*cb_barrier)(void);
311         void (*fqs)(void);
312         int (*stats)(char *page);
313         int irq_capable;
314         int can_boost;
315         char *name;
316 };
317
318 static struct rcu_torture_ops *cur_ops;
319
320 /*
321  * Definitions for rcu torture testing.
322  */
323
324 static int rcu_torture_read_lock(void) __acquires(RCU)
325 {
326         rcu_read_lock();
327         return 0;
328 }
329
330 static void rcu_read_delay(struct rcu_random_state *rrsp)
331 {
332         const unsigned long shortdelay_us = 200;
333         const unsigned long longdelay_ms = 50;
334
335         /* We want a short delay sometimes to make a reader delay the grace
336          * period, and we want a long delay occasionally to trigger
337          * force_quiescent_state. */
338
339         if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
340                 mdelay(longdelay_ms);
341         if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
342                 udelay(shortdelay_us);
343 #ifdef CONFIG_PREEMPT
344         if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
345                 preempt_schedule();  /* No QS if preempt_disable() in effect */
346 #endif
347 }
348
349 static void rcu_torture_read_unlock(int idx) __releases(RCU)
350 {
351         rcu_read_unlock();
352 }
353
354 static int rcu_torture_completed(void)
355 {
356         return rcu_batches_completed();
357 }
358
359 static void
360 rcu_torture_cb(struct rcu_head *p)
361 {
362         int i;
363         struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
364
365         if (fullstop != FULLSTOP_DONTSTOP) {
366                 /* Test is ending, just drop callbacks on the floor. */
367                 /* The next initialization will pick up the pieces. */
368                 return;
369         }
370         i = rp->rtort_pipe_count;
371         if (i > RCU_TORTURE_PIPE_LEN)
372                 i = RCU_TORTURE_PIPE_LEN;
373         atomic_inc(&rcu_torture_wcount[i]);
374         if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
375                 rp->rtort_mbtest = 0;
376                 rcu_torture_free(rp);
377         } else
378                 cur_ops->deferred_free(rp);
379 }
380
381 static int rcu_no_completed(void)
382 {
383         return 0;
384 }
385
386 static void rcu_torture_deferred_free(struct rcu_torture *p)
387 {
388         call_rcu(&p->rtort_rcu, rcu_torture_cb);
389 }
390
391 static struct rcu_torture_ops rcu_ops = {
392         .init           = NULL,
393         .cleanup        = NULL,
394         .readlock       = rcu_torture_read_lock,
395         .read_delay     = rcu_read_delay,
396         .readunlock     = rcu_torture_read_unlock,
397         .completed      = rcu_torture_completed,
398         .deferred_free  = rcu_torture_deferred_free,
399         .sync           = synchronize_rcu,
400         .cb_barrier     = rcu_barrier,
401         .fqs            = rcu_force_quiescent_state,
402         .stats          = NULL,
403         .irq_capable    = 1,
404         .can_boost      = rcu_can_boost(),
405         .name           = "rcu"
406 };
407
408 static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
409 {
410         int i;
411         struct rcu_torture *rp;
412         struct rcu_torture *rp1;
413
414         cur_ops->sync();
415         list_add(&p->rtort_free, &rcu_torture_removed);
416         list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
417                 i = rp->rtort_pipe_count;
418                 if (i > RCU_TORTURE_PIPE_LEN)
419                         i = RCU_TORTURE_PIPE_LEN;
420                 atomic_inc(&rcu_torture_wcount[i]);
421                 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
422                         rp->rtort_mbtest = 0;
423                         list_del(&rp->rtort_free);
424                         rcu_torture_free(rp);
425                 }
426         }
427 }
428
429 static void rcu_sync_torture_init(void)
430 {
431         INIT_LIST_HEAD(&rcu_torture_removed);
432 }
433
434 static struct rcu_torture_ops rcu_sync_ops = {
435         .init           = rcu_sync_torture_init,
436         .cleanup        = NULL,
437         .readlock       = rcu_torture_read_lock,
438         .read_delay     = rcu_read_delay,
439         .readunlock     = rcu_torture_read_unlock,
440         .completed      = rcu_torture_completed,
441         .deferred_free  = rcu_sync_torture_deferred_free,
442         .sync           = synchronize_rcu,
443         .cb_barrier     = NULL,
444         .fqs            = rcu_force_quiescent_state,
445         .stats          = NULL,
446         .irq_capable    = 1,
447         .can_boost      = rcu_can_boost(),
448         .name           = "rcu_sync"
449 };
450
451 static struct rcu_torture_ops rcu_expedited_ops = {
452         .init           = rcu_sync_torture_init,
453         .cleanup        = NULL,
454         .readlock       = rcu_torture_read_lock,
455         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
456         .readunlock     = rcu_torture_read_unlock,
457         .completed      = rcu_no_completed,
458         .deferred_free  = rcu_sync_torture_deferred_free,
459         .sync           = synchronize_rcu_expedited,
460         .cb_barrier     = NULL,
461         .fqs            = rcu_force_quiescent_state,
462         .stats          = NULL,
463         .irq_capable    = 1,
464         .can_boost      = rcu_can_boost(),
465         .name           = "rcu_expedited"
466 };
467
468 /*
469  * Definitions for rcu_bh torture testing.
470  */
471
472 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
473 {
474         rcu_read_lock_bh();
475         return 0;
476 }
477
478 static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
479 {
480         rcu_read_unlock_bh();
481 }
482
483 static int rcu_bh_torture_completed(void)
484 {
485         return rcu_batches_completed_bh();
486 }
487
488 static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
489 {
490         call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
491 }
492
493 static struct rcu_torture_ops rcu_bh_ops = {
494         .init           = NULL,
495         .cleanup        = NULL,
496         .readlock       = rcu_bh_torture_read_lock,
497         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
498         .readunlock     = rcu_bh_torture_read_unlock,
499         .completed      = rcu_bh_torture_completed,
500         .deferred_free  = rcu_bh_torture_deferred_free,
501         .sync           = synchronize_rcu_bh,
502         .cb_barrier     = rcu_barrier_bh,
503         .fqs            = rcu_bh_force_quiescent_state,
504         .stats          = NULL,
505         .irq_capable    = 1,
506         .name           = "rcu_bh"
507 };
508
509 static struct rcu_torture_ops rcu_bh_sync_ops = {
510         .init           = rcu_sync_torture_init,
511         .cleanup        = NULL,
512         .readlock       = rcu_bh_torture_read_lock,
513         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
514         .readunlock     = rcu_bh_torture_read_unlock,
515         .completed      = rcu_bh_torture_completed,
516         .deferred_free  = rcu_sync_torture_deferred_free,
517         .sync           = synchronize_rcu_bh,
518         .cb_barrier     = NULL,
519         .fqs            = rcu_bh_force_quiescent_state,
520         .stats          = NULL,
521         .irq_capable    = 1,
522         .name           = "rcu_bh_sync"
523 };
524
525 static struct rcu_torture_ops rcu_bh_expedited_ops = {
526         .init           = rcu_sync_torture_init,
527         .cleanup        = NULL,
528         .readlock       = rcu_bh_torture_read_lock,
529         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
530         .readunlock     = rcu_bh_torture_read_unlock,
531         .completed      = rcu_bh_torture_completed,
532         .deferred_free  = rcu_sync_torture_deferred_free,
533         .sync           = synchronize_rcu_bh_expedited,
534         .cb_barrier     = NULL,
535         .fqs            = rcu_bh_force_quiescent_state,
536         .stats          = NULL,
537         .irq_capable    = 1,
538         .name           = "rcu_bh_expedited"
539 };
540
541 /*
542  * Definitions for srcu torture testing.
543  */
544
545 static struct srcu_struct srcu_ctl;
546
547 static void srcu_torture_init(void)
548 {
549         init_srcu_struct(&srcu_ctl);
550         rcu_sync_torture_init();
551 }
552
553 static void srcu_torture_cleanup(void)
554 {
555         synchronize_srcu(&srcu_ctl);
556         cleanup_srcu_struct(&srcu_ctl);
557 }
558
559 static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
560 {
561         return srcu_read_lock(&srcu_ctl);
562 }
563
564 static void srcu_read_delay(struct rcu_random_state *rrsp)
565 {
566         long delay;
567         const long uspertick = 1000000 / HZ;
568         const long longdelay = 10;
569
570         /* We want there to be long-running readers, but not all the time. */
571
572         delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
573         if (!delay)
574                 schedule_timeout_interruptible(longdelay);
575         else
576                 rcu_read_delay(rrsp);
577 }
578
579 static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
580 {
581         srcu_read_unlock(&srcu_ctl, idx);
582 }
583
584 static int srcu_torture_completed(void)
585 {
586         return srcu_batches_completed(&srcu_ctl);
587 }
588
589 static void srcu_torture_synchronize(void)
590 {
591         synchronize_srcu(&srcu_ctl);
592 }
593
594 static int srcu_torture_stats(char *page)
595 {
596         int cnt = 0;
597         int cpu;
598         int idx = srcu_ctl.completed & 0x1;
599
600         cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
601                        torture_type, TORTURE_FLAG, idx);
602         for_each_possible_cpu(cpu) {
603                 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
604                                per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
605                                per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
606         }
607         cnt += sprintf(&page[cnt], "\n");
608         return cnt;
609 }
610
611 static struct rcu_torture_ops srcu_ops = {
612         .init           = srcu_torture_init,
613         .cleanup        = srcu_torture_cleanup,
614         .readlock       = srcu_torture_read_lock,
615         .read_delay     = srcu_read_delay,
616         .readunlock     = srcu_torture_read_unlock,
617         .completed      = srcu_torture_completed,
618         .deferred_free  = rcu_sync_torture_deferred_free,
619         .sync           = srcu_torture_synchronize,
620         .cb_barrier     = NULL,
621         .stats          = srcu_torture_stats,
622         .name           = "srcu"
623 };
624
625 static void srcu_torture_synchronize_expedited(void)
626 {
627         synchronize_srcu_expedited(&srcu_ctl);
628 }
629
630 static struct rcu_torture_ops srcu_expedited_ops = {
631         .init           = srcu_torture_init,
632         .cleanup        = srcu_torture_cleanup,
633         .readlock       = srcu_torture_read_lock,
634         .read_delay     = srcu_read_delay,
635         .readunlock     = srcu_torture_read_unlock,
636         .completed      = srcu_torture_completed,
637         .deferred_free  = rcu_sync_torture_deferred_free,
638         .sync           = srcu_torture_synchronize_expedited,
639         .cb_barrier     = NULL,
640         .stats          = srcu_torture_stats,
641         .name           = "srcu_expedited"
642 };
643
644 /*
645  * Definitions for sched torture testing.
646  */
647
648 static int sched_torture_read_lock(void)
649 {
650         preempt_disable();
651         return 0;
652 }
653
654 static void sched_torture_read_unlock(int idx)
655 {
656         preempt_enable();
657 }
658
659 static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
660 {
661         call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
662 }
663
664 static struct rcu_torture_ops sched_ops = {
665         .init           = rcu_sync_torture_init,
666         .cleanup        = NULL,
667         .readlock       = sched_torture_read_lock,
668         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
669         .readunlock     = sched_torture_read_unlock,
670         .completed      = rcu_no_completed,
671         .deferred_free  = rcu_sched_torture_deferred_free,
672         .sync           = synchronize_sched,
673         .cb_barrier     = rcu_barrier_sched,
674         .fqs            = rcu_sched_force_quiescent_state,
675         .stats          = NULL,
676         .irq_capable    = 1,
677         .name           = "sched"
678 };
679
680 static struct rcu_torture_ops sched_sync_ops = {
681         .init           = rcu_sync_torture_init,
682         .cleanup        = NULL,
683         .readlock       = sched_torture_read_lock,
684         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
685         .readunlock     = sched_torture_read_unlock,
686         .completed      = rcu_no_completed,
687         .deferred_free  = rcu_sync_torture_deferred_free,
688         .sync           = synchronize_sched,
689         .cb_barrier     = NULL,
690         .fqs            = rcu_sched_force_quiescent_state,
691         .stats          = NULL,
692         .name           = "sched_sync"
693 };
694
695 static struct rcu_torture_ops sched_expedited_ops = {
696         .init           = rcu_sync_torture_init,
697         .cleanup        = NULL,
698         .readlock       = sched_torture_read_lock,
699         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
700         .readunlock     = sched_torture_read_unlock,
701         .completed      = rcu_no_completed,
702         .deferred_free  = rcu_sync_torture_deferred_free,
703         .sync           = synchronize_sched_expedited,
704         .cb_barrier     = NULL,
705         .fqs            = rcu_sched_force_quiescent_state,
706         .stats          = NULL,
707         .irq_capable    = 1,
708         .name           = "sched_expedited"
709 };
710
711 /*
712  * RCU torture priority-boost testing.  Runs one real-time thread per
713  * CPU for moderate bursts, repeatedly registering RCU callbacks and
714  * spinning waiting for them to be invoked.  If a given callback takes
715  * too long to be invoked, we assume that priority inversion has occurred.
716  */
717
718 struct rcu_boost_inflight {
719         struct rcu_head rcu;
720         int inflight;
721 };
722
723 static void rcu_torture_boost_cb(struct rcu_head *head)
724 {
725         struct rcu_boost_inflight *rbip =
726                 container_of(head, struct rcu_boost_inflight, rcu);
727
728         smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
729         rbip->inflight = 0;
730 }
731
732 static int rcu_torture_boost(void *arg)
733 {
734         unsigned long call_rcu_time;
735         unsigned long endtime;
736         unsigned long oldstarttime;
737         struct rcu_boost_inflight rbi = { .inflight = 0 };
738         struct sched_param sp;
739
740         VERBOSE_PRINTK_STRING("rcu_torture_boost started");
741
742         /* Set real-time priority. */
743         sp.sched_priority = 1;
744         if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
745                 VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
746                 n_rcu_torture_boost_rterror++;
747         }
748
749         init_rcu_head_on_stack(&rbi.rcu);
750         /* Each pass through the following loop does one boost-test cycle. */
751         do {
752                 /* Wait for the next test interval. */
753                 oldstarttime = boost_starttime;
754                 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
755                         schedule_timeout_uninterruptible(1);
756                         rcu_stutter_wait("rcu_torture_boost");
757                         if (kthread_should_stop() ||
758                             fullstop != FULLSTOP_DONTSTOP)
759                                 goto checkwait;
760                 }
761
762                 /* Do one boost-test interval. */
763                 endtime = oldstarttime + test_boost_duration * HZ;
764                 call_rcu_time = jiffies;
765                 while (ULONG_CMP_LT(jiffies, endtime)) {
766                         /* If we don't have a callback in flight, post one. */
767                         if (!rbi.inflight) {
768                                 smp_mb(); /* RCU core before ->inflight = 1. */
769                                 rbi.inflight = 1;
770                                 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
771                                 if (jiffies - call_rcu_time >
772                                          test_boost_duration * HZ - HZ / 2) {
773                                         VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
774                                         n_rcu_torture_boost_failure++;
775                                 }
776                                 call_rcu_time = jiffies;
777                         }
778                         cond_resched();
779                         rcu_stutter_wait("rcu_torture_boost");
780                         if (kthread_should_stop() ||
781                             fullstop != FULLSTOP_DONTSTOP)
782                                 goto checkwait;
783                 }
784
785                 /*
786                  * Set the start time of the next test interval.
787                  * Yes, this is vulnerable to long delays, but such
788                  * delays simply cause a false negative for the next
789                  * interval.  Besides, we are running at RT priority,
790                  * so delays should be relatively rare.
791                  */
792                 while (oldstarttime == boost_starttime &&
793                        !kthread_should_stop()) {
794                         if (mutex_trylock(&boost_mutex)) {
795                                 boost_starttime = jiffies +
796                                                   test_boost_interval * HZ;
797                                 n_rcu_torture_boosts++;
798                                 mutex_unlock(&boost_mutex);
799                                 break;
800                         }
801                         schedule_timeout_uninterruptible(1);
802                 }
803
804                 /* Go do the stutter. */
805 checkwait:      rcu_stutter_wait("rcu_torture_boost");
806         } while (!kthread_should_stop() && fullstop  == FULLSTOP_DONTSTOP);
807
808         /* Clean up and exit. */
809         VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
810         rcutorture_shutdown_absorb("rcu_torture_boost");
811         while (!kthread_should_stop() || rbi.inflight)
812                 schedule_timeout_uninterruptible(1);
813         smp_mb(); /* order accesses to ->inflight before stack-frame death. */
814         destroy_rcu_head_on_stack(&rbi.rcu);
815         return 0;
816 }
817
818 /*
819  * RCU torture force-quiescent-state kthread.  Repeatedly induces
820  * bursts of calls to force_quiescent_state(), increasing the probability
821  * of occurrence of some important types of race conditions.
822  */
823 static int
824 rcu_torture_fqs(void *arg)
825 {
826         unsigned long fqs_resume_time;
827         int fqs_burst_remaining;
828
829         VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
830         do {
831                 fqs_resume_time = jiffies + fqs_stutter * HZ;
832                 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
833                        !kthread_should_stop()) {
834                         schedule_timeout_interruptible(1);
835                 }
836                 fqs_burst_remaining = fqs_duration;
837                 while (fqs_burst_remaining > 0 &&
838                        !kthread_should_stop()) {
839                         cur_ops->fqs();
840                         udelay(fqs_holdoff);
841                         fqs_burst_remaining -= fqs_holdoff;
842                 }
843                 rcu_stutter_wait("rcu_torture_fqs");
844         } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
845         VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
846         rcutorture_shutdown_absorb("rcu_torture_fqs");
847         while (!kthread_should_stop())
848                 schedule_timeout_uninterruptible(1);
849         return 0;
850 }
851
852 /*
853  * RCU torture writer kthread.  Repeatedly substitutes a new structure
854  * for that pointed to by rcu_torture_current, freeing the old structure
855  * after a series of grace periods (the "pipeline").
856  */
857 static int
858 rcu_torture_writer(void *arg)
859 {
860         int i;
861         long oldbatch = rcu_batches_completed();
862         struct rcu_torture *rp;
863         struct rcu_torture *old_rp;
864         static DEFINE_RCU_RANDOM(rand);
865
866         VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
867         set_user_nice(current, 19);
868
869         do {
870                 schedule_timeout_uninterruptible(1);
871                 rp = rcu_torture_alloc();
872                 if (rp == NULL)
873                         continue;
874                 rp->rtort_pipe_count = 0;
875                 udelay(rcu_random(&rand) & 0x3ff);
876                 old_rp = rcu_dereference_check(rcu_torture_current,
877                                                current == writer_task);
878                 rp->rtort_mbtest = 1;
879                 rcu_assign_pointer(rcu_torture_current, rp);
880                 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
881                 if (old_rp) {
882                         i = old_rp->rtort_pipe_count;
883                         if (i > RCU_TORTURE_PIPE_LEN)
884                                 i = RCU_TORTURE_PIPE_LEN;
885                         atomic_inc(&rcu_torture_wcount[i]);
886                         old_rp->rtort_pipe_count++;
887                         cur_ops->deferred_free(old_rp);
888                 }
889                 rcutorture_record_progress(++rcu_torture_current_version);
890                 oldbatch = cur_ops->completed();
891                 rcu_stutter_wait("rcu_torture_writer");
892         } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
893         VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
894         rcutorture_shutdown_absorb("rcu_torture_writer");
895         while (!kthread_should_stop())
896                 schedule_timeout_uninterruptible(1);
897         return 0;
898 }
899
900 /*
901  * RCU torture fake writer kthread.  Repeatedly calls sync, with a random
902  * delay between calls.
903  */
904 static int
905 rcu_torture_fakewriter(void *arg)
906 {
907         DEFINE_RCU_RANDOM(rand);
908
909         VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
910         set_user_nice(current, 19);
911
912         do {
913                 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
914                 udelay(rcu_random(&rand) & 0x3ff);
915                 cur_ops->sync();
916                 rcu_stutter_wait("rcu_torture_fakewriter");
917         } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
918
919         VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
920         rcutorture_shutdown_absorb("rcu_torture_fakewriter");
921         while (!kthread_should_stop())
922                 schedule_timeout_uninterruptible(1);
923         return 0;
924 }
925
926 void rcutorture_trace_dump(void)
927 {
928         static atomic_t beenhere = ATOMIC_INIT(0);
929
930         if (atomic_read(&beenhere))
931                 return;
932         if (atomic_xchg(&beenhere, 1) != 0)
933                 return;
934         do_trace_rcu_torture_read(cur_ops->name, (struct rcu_head *)~0UL);
935         ftrace_dump(DUMP_ALL);
936 }
937
938 /*
939  * RCU torture reader from timer handler.  Dereferences rcu_torture_current,
940  * incrementing the corresponding element of the pipeline array.  The
941  * counter in the element should never be greater than 1, otherwise, the
942  * RCU implementation is broken.
943  */
944 static void rcu_torture_timer(unsigned long unused)
945 {
946         int idx;
947         int completed;
948         static DEFINE_RCU_RANDOM(rand);
949         static DEFINE_SPINLOCK(rand_lock);
950         struct rcu_torture *p;
951         int pipe_count;
952
953         idx = cur_ops->readlock();
954         completed = cur_ops->completed();
955         p = rcu_dereference_check(rcu_torture_current,
956                                   rcu_read_lock_bh_held() ||
957                                   rcu_read_lock_sched_held() ||
958                                   srcu_read_lock_held(&srcu_ctl));
959         do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu);
960         if (p == NULL) {
961                 /* Leave because rcu_torture_writer is not yet underway */
962                 cur_ops->readunlock(idx);
963                 return;
964         }
965         if (p->rtort_mbtest == 0)
966                 atomic_inc(&n_rcu_torture_mberror);
967         spin_lock(&rand_lock);
968         cur_ops->read_delay(&rand);
969         n_rcu_torture_timers++;
970         spin_unlock(&rand_lock);
971         preempt_disable();
972         pipe_count = p->rtort_pipe_count;
973         if (pipe_count > RCU_TORTURE_PIPE_LEN) {
974                 /* Should not happen, but... */
975                 pipe_count = RCU_TORTURE_PIPE_LEN;
976         }
977         if (pipe_count > 1)
978                 rcutorture_trace_dump();
979         __this_cpu_inc(rcu_torture_count[pipe_count]);
980         completed = cur_ops->completed() - completed;
981         if (completed > RCU_TORTURE_PIPE_LEN) {
982                 /* Should not happen, but... */
983                 completed = RCU_TORTURE_PIPE_LEN;
984         }
985         __this_cpu_inc(rcu_torture_batch[completed]);
986         preempt_enable();
987         cur_ops->readunlock(idx);
988 }
989
990 /*
991  * RCU torture reader kthread.  Repeatedly dereferences rcu_torture_current,
992  * incrementing the corresponding element of the pipeline array.  The
993  * counter in the element should never be greater than 1, otherwise, the
994  * RCU implementation is broken.
995  */
996 static int
997 rcu_torture_reader(void *arg)
998 {
999         int completed;
1000         int idx;
1001         DEFINE_RCU_RANDOM(rand);
1002         struct rcu_torture *p;
1003         int pipe_count;
1004         struct timer_list t;
1005
1006         VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
1007         set_user_nice(current, 19);
1008         if (irqreader && cur_ops->irq_capable)
1009                 setup_timer_on_stack(&t, rcu_torture_timer, 0);
1010
1011         do {
1012                 if (irqreader && cur_ops->irq_capable) {
1013                         if (!timer_pending(&t))
1014                                 mod_timer(&t, jiffies + 1);
1015                 }
1016                 idx = cur_ops->readlock();
1017                 completed = cur_ops->completed();
1018                 p = rcu_dereference_check(rcu_torture_current,
1019                                           rcu_read_lock_bh_held() ||
1020                                           rcu_read_lock_sched_held() ||
1021                                           srcu_read_lock_held(&srcu_ctl));
1022                 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu);
1023                 if (p == NULL) {
1024                         /* Wait for rcu_torture_writer to get underway */
1025                         cur_ops->readunlock(idx);
1026                         schedule_timeout_interruptible(HZ);
1027                         continue;
1028                 }
1029                 if (p->rtort_mbtest == 0)
1030                         atomic_inc(&n_rcu_torture_mberror);
1031                 cur_ops->read_delay(&rand);
1032                 preempt_disable();
1033                 pipe_count = p->rtort_pipe_count;
1034                 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1035                         /* Should not happen, but... */
1036                         pipe_count = RCU_TORTURE_PIPE_LEN;
1037                 }
1038                 if (pipe_count > 1)
1039                         rcutorture_trace_dump();
1040                 __this_cpu_inc(rcu_torture_count[pipe_count]);
1041                 completed = cur_ops->completed() - completed;
1042                 if (completed > RCU_TORTURE_PIPE_LEN) {
1043                         /* Should not happen, but... */
1044                         completed = RCU_TORTURE_PIPE_LEN;
1045                 }
1046                 __this_cpu_inc(rcu_torture_batch[completed]);
1047                 preempt_enable();
1048                 cur_ops->readunlock(idx);
1049                 schedule();
1050                 rcu_stutter_wait("rcu_torture_reader");
1051         } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1052         VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
1053         rcutorture_shutdown_absorb("rcu_torture_reader");
1054         if (irqreader && cur_ops->irq_capable)
1055                 del_timer_sync(&t);
1056         while (!kthread_should_stop())
1057                 schedule_timeout_uninterruptible(1);
1058         return 0;
1059 }
1060
1061 /*
1062  * Create an RCU-torture statistics message in the specified buffer.
1063  */
1064 static int
1065 rcu_torture_printk(char *page)
1066 {
1067         int cnt = 0;
1068         int cpu;
1069         int i;
1070         long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1071         long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1072
1073         for_each_possible_cpu(cpu) {
1074                 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1075                         pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1076                         batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1077                 }
1078         }
1079         for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1080                 if (pipesummary[i] != 0)
1081                         break;
1082         }
1083         cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
1084         cnt += sprintf(&page[cnt],
1085                        "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d "
1086                        "rtmbe: %d rtbke: %ld rtbre: %ld "
1087                        "rtbf: %ld rtb: %ld nt: %ld",
1088                        rcu_torture_current,
1089                        rcu_torture_current_version,
1090                        list_empty(&rcu_torture_freelist),
1091                        atomic_read(&n_rcu_torture_alloc),
1092                        atomic_read(&n_rcu_torture_alloc_fail),
1093                        atomic_read(&n_rcu_torture_free),
1094                        atomic_read(&n_rcu_torture_mberror),
1095                        n_rcu_torture_boost_ktrerror,
1096                        n_rcu_torture_boost_rterror,
1097                        n_rcu_torture_boost_failure,
1098                        n_rcu_torture_boosts,
1099                        n_rcu_torture_timers);
1100         if (atomic_read(&n_rcu_torture_mberror) != 0 ||
1101             n_rcu_torture_boost_ktrerror != 0 ||
1102             n_rcu_torture_boost_rterror != 0 ||
1103             n_rcu_torture_boost_failure != 0)
1104                 cnt += sprintf(&page[cnt], " !!!");
1105         cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1106         if (i > 1) {
1107                 cnt += sprintf(&page[cnt], "!!! ");
1108                 atomic_inc(&n_rcu_torture_error);
1109                 WARN_ON_ONCE(1);
1110         }
1111         cnt += sprintf(&page[cnt], "Reader Pipe: ");
1112         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1113                 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
1114         cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1115         cnt += sprintf(&page[cnt], "Reader Batch: ");
1116         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1117                 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
1118         cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1119         cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
1120         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1121                 cnt += sprintf(&page[cnt], " %d",
1122                                atomic_read(&rcu_torture_wcount[i]));
1123         }
1124         cnt += sprintf(&page[cnt], "\n");
1125         if (cur_ops->stats)
1126                 cnt += cur_ops->stats(&page[cnt]);
1127         return cnt;
1128 }
1129
1130 /*
1131  * Print torture statistics.  Caller must ensure that there is only
1132  * one call to this function at a given time!!!  This is normally
1133  * accomplished by relying on the module system to only have one copy
1134  * of the module loaded, and then by giving the rcu_torture_stats
1135  * kthread full control (or the init/cleanup functions when rcu_torture_stats
1136  * thread is not running).
1137  */
1138 static void
1139 rcu_torture_stats_print(void)
1140 {
1141         int cnt;
1142
1143         cnt = rcu_torture_printk(printk_buf);
1144         printk(KERN_ALERT "%s", printk_buf);
1145 }
1146
1147 /*
1148  * Periodically prints torture statistics, if periodic statistics printing
1149  * was specified via the stat_interval module parameter.
1150  *
1151  * No need to worry about fullstop here, since this one doesn't reference
1152  * volatile state or register callbacks.
1153  */
1154 static int
1155 rcu_torture_stats(void *arg)
1156 {
1157         VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
1158         do {
1159                 schedule_timeout_interruptible(stat_interval * HZ);
1160                 rcu_torture_stats_print();
1161                 rcutorture_shutdown_absorb("rcu_torture_stats");
1162         } while (!kthread_should_stop());
1163         VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1164         return 0;
1165 }
1166
1167 static int rcu_idle_cpu;        /* Force all torture tasks off this CPU */
1168
1169 /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1170  * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1171  */
1172 static void rcu_torture_shuffle_tasks(void)
1173 {
1174         int i;
1175
1176         cpumask_setall(shuffle_tmp_mask);
1177         get_online_cpus();
1178
1179         /* No point in shuffling if there is only one online CPU (ex: UP) */
1180         if (num_online_cpus() == 1) {
1181                 put_online_cpus();
1182                 return;
1183         }
1184
1185         if (rcu_idle_cpu != -1)
1186                 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
1187
1188         set_cpus_allowed_ptr(current, shuffle_tmp_mask);
1189
1190         if (reader_tasks) {
1191                 for (i = 0; i < nrealreaders; i++)
1192                         if (reader_tasks[i])
1193                                 set_cpus_allowed_ptr(reader_tasks[i],
1194                                                      shuffle_tmp_mask);
1195         }
1196
1197         if (fakewriter_tasks) {
1198                 for (i = 0; i < nfakewriters; i++)
1199                         if (fakewriter_tasks[i])
1200                                 set_cpus_allowed_ptr(fakewriter_tasks[i],
1201                                                      shuffle_tmp_mask);
1202         }
1203
1204         if (writer_task)
1205                 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
1206
1207         if (stats_task)
1208                 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
1209
1210         if (rcu_idle_cpu == -1)
1211                 rcu_idle_cpu = num_online_cpus() - 1;
1212         else
1213                 rcu_idle_cpu--;
1214
1215         put_online_cpus();
1216 }
1217
1218 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1219  * system to become idle at a time and cut off its timer ticks. This is meant
1220  * to test the support for such tickless idle CPU in RCU.
1221  */
1222 static int
1223 rcu_torture_shuffle(void *arg)
1224 {
1225         VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1226         do {
1227                 schedule_timeout_interruptible(shuffle_interval * HZ);
1228                 rcu_torture_shuffle_tasks();
1229                 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1230         } while (!kthread_should_stop());
1231         VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1232         return 0;
1233 }
1234
1235 /* Cause the rcutorture test to "stutter", starting and stopping all
1236  * threads periodically.
1237  */
1238 static int
1239 rcu_torture_stutter(void *arg)
1240 {
1241         VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1242         do {
1243                 schedule_timeout_interruptible(stutter * HZ);
1244                 stutter_pause_test = 1;
1245                 if (!kthread_should_stop())
1246                         schedule_timeout_interruptible(stutter * HZ);
1247                 stutter_pause_test = 0;
1248                 rcutorture_shutdown_absorb("rcu_torture_stutter");
1249         } while (!kthread_should_stop());
1250         VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1251         return 0;
1252 }
1253
1254 static inline void
1255 rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, char *tag)
1256 {
1257         printk(KERN_ALERT "%s" TORTURE_FLAG
1258                 "--- %s: nreaders=%d nfakewriters=%d "
1259                 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1260                 "shuffle_interval=%d stutter=%d irqreader=%d "
1261                 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1262                 "test_boost=%d/%d test_boost_interval=%d "
1263                 "test_boost_duration=%d shutdown_secs=%d\n",
1264                 torture_type, tag, nrealreaders, nfakewriters,
1265                 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1266                 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1267                 test_boost, cur_ops->can_boost,
1268                 test_boost_interval, test_boost_duration, shutdown_secs);
1269 }
1270
1271 static struct notifier_block rcutorture_shutdown_nb = {
1272         .notifier_call = rcutorture_shutdown_notify,
1273 };
1274
1275 static void rcutorture_booster_cleanup(int cpu)
1276 {
1277         struct task_struct *t;
1278
1279         if (boost_tasks[cpu] == NULL)
1280                 return;
1281         mutex_lock(&boost_mutex);
1282         VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
1283         t = boost_tasks[cpu];
1284         boost_tasks[cpu] = NULL;
1285         mutex_unlock(&boost_mutex);
1286
1287         /* This must be outside of the mutex, otherwise deadlock! */
1288         kthread_stop(t);
1289 }
1290
1291 static int rcutorture_booster_init(int cpu)
1292 {
1293         int retval;
1294
1295         if (boost_tasks[cpu] != NULL)
1296                 return 0;  /* Already created, nothing more to do. */
1297
1298         /* Don't allow time recalculation while creating a new task. */
1299         mutex_lock(&boost_mutex);
1300         VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
1301         boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1302                                                   cpu_to_node(cpu),
1303                                                   "rcu_torture_boost");
1304         if (IS_ERR(boost_tasks[cpu])) {
1305                 retval = PTR_ERR(boost_tasks[cpu]);
1306                 VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
1307                 n_rcu_torture_boost_ktrerror++;
1308                 boost_tasks[cpu] = NULL;
1309                 mutex_unlock(&boost_mutex);
1310                 return retval;
1311         }
1312         kthread_bind(boost_tasks[cpu], cpu);
1313         wake_up_process(boost_tasks[cpu]);
1314         mutex_unlock(&boost_mutex);
1315         return 0;
1316 }
1317
1318 /*
1319  * Cause the rcutorture test to shutdown the system after the test has
1320  * run for the time specified by the shutdown_secs module parameter.
1321  */
1322 static int
1323 rcu_torture_shutdown(void *arg)
1324 {
1325         long delta;
1326         unsigned long jiffies_snap;
1327
1328         VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
1329         jiffies_snap = ACCESS_ONCE(jiffies);
1330         while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
1331                !kthread_should_stop()) {
1332                 delta = shutdown_time - jiffies_snap;
1333                 if (verbose)
1334                         printk(KERN_ALERT "%s" TORTURE_FLAG
1335                                "rcu_torture_shutdown task: %lu "
1336                                "jiffies remaining\n",
1337                                torture_type, delta);
1338                 schedule_timeout_interruptible(delta);
1339                 jiffies_snap = ACCESS_ONCE(jiffies);
1340         }
1341         if (ULONG_CMP_LT(jiffies, shutdown_time)) {
1342                 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
1343                 return 0;
1344         }
1345
1346         /* OK, shut down the system. */
1347
1348         VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
1349         shutdown_task = NULL;   /* Avoid self-kill deadlock. */
1350         rcu_torture_cleanup();  /* Get the success/failure message. */
1351         kernel_power_off();     /* Shut down the system. */
1352         return 0;
1353 }
1354
1355 static int rcutorture_cpu_notify(struct notifier_block *self,
1356                                  unsigned long action, void *hcpu)
1357 {
1358         long cpu = (long)hcpu;
1359
1360         switch (action) {
1361         case CPU_ONLINE:
1362         case CPU_DOWN_FAILED:
1363                 (void)rcutorture_booster_init(cpu);
1364                 break;
1365         case CPU_DOWN_PREPARE:
1366                 rcutorture_booster_cleanup(cpu);
1367                 break;
1368         default:
1369                 break;
1370         }
1371         return NOTIFY_OK;
1372 }
1373
1374 static struct notifier_block rcutorture_cpu_nb = {
1375         .notifier_call = rcutorture_cpu_notify,
1376 };
1377
1378 static void
1379 rcu_torture_cleanup(void)
1380 {
1381         int i;
1382
1383         mutex_lock(&fullstop_mutex);
1384         rcutorture_record_test_transition();
1385         if (fullstop == FULLSTOP_SHUTDOWN) {
1386                 printk(KERN_WARNING /* but going down anyway, so... */
1387                        "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
1388                 mutex_unlock(&fullstop_mutex);
1389                 schedule_timeout_uninterruptible(10);
1390                 if (cur_ops->cb_barrier != NULL)
1391                         cur_ops->cb_barrier();
1392                 return;
1393         }
1394         fullstop = FULLSTOP_RMMOD;
1395         mutex_unlock(&fullstop_mutex);
1396         unregister_reboot_notifier(&rcutorture_shutdown_nb);
1397         if (stutter_task) {
1398                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1399                 kthread_stop(stutter_task);
1400         }
1401         stutter_task = NULL;
1402         if (shuffler_task) {
1403                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1404                 kthread_stop(shuffler_task);
1405                 free_cpumask_var(shuffle_tmp_mask);
1406         }
1407         shuffler_task = NULL;
1408
1409         if (writer_task) {
1410                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1411                 kthread_stop(writer_task);
1412         }
1413         writer_task = NULL;
1414
1415         if (reader_tasks) {
1416                 for (i = 0; i < nrealreaders; i++) {
1417                         if (reader_tasks[i]) {
1418                                 VERBOSE_PRINTK_STRING(
1419                                         "Stopping rcu_torture_reader task");
1420                                 kthread_stop(reader_tasks[i]);
1421                         }
1422                         reader_tasks[i] = NULL;
1423                 }
1424                 kfree(reader_tasks);
1425                 reader_tasks = NULL;
1426         }
1427         rcu_torture_current = NULL;
1428
1429         if (fakewriter_tasks) {
1430                 for (i = 0; i < nfakewriters; i++) {
1431                         if (fakewriter_tasks[i]) {
1432                                 VERBOSE_PRINTK_STRING(
1433                                         "Stopping rcu_torture_fakewriter task");
1434                                 kthread_stop(fakewriter_tasks[i]);
1435                         }
1436                         fakewriter_tasks[i] = NULL;
1437                 }
1438                 kfree(fakewriter_tasks);
1439                 fakewriter_tasks = NULL;
1440         }
1441
1442         if (stats_task) {
1443                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1444                 kthread_stop(stats_task);
1445         }
1446         stats_task = NULL;
1447
1448         if (fqs_task) {
1449                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1450                 kthread_stop(fqs_task);
1451         }
1452         fqs_task = NULL;
1453         if ((test_boost == 1 && cur_ops->can_boost) ||
1454             test_boost == 2) {
1455                 unregister_cpu_notifier(&rcutorture_cpu_nb);
1456                 for_each_possible_cpu(i)
1457                         rcutorture_booster_cleanup(i);
1458         }
1459         if (shutdown_task != NULL) {
1460                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
1461                 kthread_stop(shutdown_task);
1462         }
1463
1464         /* Wait for all RCU callbacks to fire.  */
1465
1466         if (cur_ops->cb_barrier != NULL)
1467                 cur_ops->cb_barrier();
1468
1469         rcu_torture_stats_print();  /* -After- the stats thread is stopped! */
1470
1471         if (cur_ops->cleanup)
1472                 cur_ops->cleanup();
1473         if (atomic_read(&n_rcu_torture_error))
1474                 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
1475         else
1476                 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
1477 }
1478
1479 static int __init
1480 rcu_torture_init(void)
1481 {
1482         int i;
1483         int cpu;
1484         int firsterr = 0;
1485         static struct rcu_torture_ops *torture_ops[] =
1486                 { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
1487                   &rcu_bh_ops, &rcu_bh_sync_ops, &rcu_bh_expedited_ops,
1488                   &srcu_ops, &srcu_expedited_ops,
1489                   &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
1490
1491         mutex_lock(&fullstop_mutex);
1492
1493         /* Process args and tell the world that the torturer is on the job. */
1494         for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1495                 cur_ops = torture_ops[i];
1496                 if (strcmp(torture_type, cur_ops->name) == 0)
1497                         break;
1498         }
1499         if (i == ARRAY_SIZE(torture_ops)) {
1500                 printk(KERN_ALERT "rcu-torture: invalid torture type: \"%s\"\n",
1501                        torture_type);
1502                 printk(KERN_ALERT "rcu-torture types:");
1503                 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
1504                         printk(KERN_ALERT " %s", torture_ops[i]->name);
1505                 printk(KERN_ALERT "\n");
1506                 mutex_unlock(&fullstop_mutex);
1507                 return -EINVAL;
1508         }
1509         if (cur_ops->fqs == NULL && fqs_duration != 0) {
1510                 printk(KERN_ALERT "rcu-torture: ->fqs NULL and non-zero "
1511                                   "fqs_duration, fqs disabled.\n");
1512                 fqs_duration = 0;
1513         }
1514         if (cur_ops->init)
1515                 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1516
1517         if (nreaders >= 0)
1518                 nrealreaders = nreaders;
1519         else
1520                 nrealreaders = 2 * num_online_cpus();
1521         rcu_torture_print_module_parms(cur_ops, "Start of test");
1522         fullstop = FULLSTOP_DONTSTOP;
1523
1524         /* Set up the freelist. */
1525
1526         INIT_LIST_HEAD(&rcu_torture_freelist);
1527         for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1528                 rcu_tortures[i].rtort_mbtest = 0;
1529                 list_add_tail(&rcu_tortures[i].rtort_free,
1530                               &rcu_torture_freelist);
1531         }
1532
1533         /* Initialize the statistics so that each run gets its own numbers. */
1534
1535         rcu_torture_current = NULL;
1536         rcu_torture_current_version = 0;
1537         atomic_set(&n_rcu_torture_alloc, 0);
1538         atomic_set(&n_rcu_torture_alloc_fail, 0);
1539         atomic_set(&n_rcu_torture_free, 0);
1540         atomic_set(&n_rcu_torture_mberror, 0);
1541         atomic_set(&n_rcu_torture_error, 0);
1542         n_rcu_torture_boost_ktrerror = 0;
1543         n_rcu_torture_boost_rterror = 0;
1544         n_rcu_torture_boost_failure = 0;
1545         n_rcu_torture_boosts = 0;
1546         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1547                 atomic_set(&rcu_torture_wcount[i], 0);
1548         for_each_possible_cpu(cpu) {
1549                 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1550                         per_cpu(rcu_torture_count, cpu)[i] = 0;
1551                         per_cpu(rcu_torture_batch, cpu)[i] = 0;
1552                 }
1553         }
1554
1555         /* Start up the kthreads. */
1556
1557         VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1558         writer_task = kthread_run(rcu_torture_writer, NULL,
1559                                   "rcu_torture_writer");
1560         if (IS_ERR(writer_task)) {
1561                 firsterr = PTR_ERR(writer_task);
1562                 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1563                 writer_task = NULL;
1564                 goto unwind;
1565         }
1566         fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1567                                    GFP_KERNEL);
1568         if (fakewriter_tasks == NULL) {
1569                 VERBOSE_PRINTK_ERRSTRING("out of memory");
1570                 firsterr = -ENOMEM;
1571                 goto unwind;
1572         }
1573         for (i = 0; i < nfakewriters; i++) {
1574                 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1575                 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1576                                                   "rcu_torture_fakewriter");
1577                 if (IS_ERR(fakewriter_tasks[i])) {
1578                         firsterr = PTR_ERR(fakewriter_tasks[i]);
1579                         VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1580                         fakewriter_tasks[i] = NULL;
1581                         goto unwind;
1582                 }
1583         }
1584         reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
1585                                GFP_KERNEL);
1586         if (reader_tasks == NULL) {
1587                 VERBOSE_PRINTK_ERRSTRING("out of memory");
1588                 firsterr = -ENOMEM;
1589                 goto unwind;
1590         }
1591         for (i = 0; i < nrealreaders; i++) {
1592                 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1593                 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1594                                               "rcu_torture_reader");
1595                 if (IS_ERR(reader_tasks[i])) {
1596                         firsterr = PTR_ERR(reader_tasks[i]);
1597                         VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1598                         reader_tasks[i] = NULL;
1599                         goto unwind;
1600                 }
1601         }
1602         if (stat_interval > 0) {
1603                 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1604                 stats_task = kthread_run(rcu_torture_stats, NULL,
1605                                         "rcu_torture_stats");
1606                 if (IS_ERR(stats_task)) {
1607                         firsterr = PTR_ERR(stats_task);
1608                         VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1609                         stats_task = NULL;
1610                         goto unwind;
1611                 }
1612         }
1613         if (test_no_idle_hz) {
1614                 rcu_idle_cpu = num_online_cpus() - 1;
1615
1616                 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
1617                         firsterr = -ENOMEM;
1618                         VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
1619                         goto unwind;
1620                 }
1621
1622                 /* Create the shuffler thread */
1623                 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1624                                           "rcu_torture_shuffle");
1625                 if (IS_ERR(shuffler_task)) {
1626                         free_cpumask_var(shuffle_tmp_mask);
1627                         firsterr = PTR_ERR(shuffler_task);
1628                         VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1629                         shuffler_task = NULL;
1630                         goto unwind;
1631                 }
1632         }
1633         if (stutter < 0)
1634                 stutter = 0;
1635         if (stutter) {
1636                 /* Create the stutter thread */
1637                 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1638                                           "rcu_torture_stutter");
1639                 if (IS_ERR(stutter_task)) {
1640                         firsterr = PTR_ERR(stutter_task);
1641                         VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1642                         stutter_task = NULL;
1643                         goto unwind;
1644                 }
1645         }
1646         if (fqs_duration < 0)
1647                 fqs_duration = 0;
1648         if (fqs_duration) {
1649                 /* Create the stutter thread */
1650                 fqs_task = kthread_run(rcu_torture_fqs, NULL,
1651                                        "rcu_torture_fqs");
1652                 if (IS_ERR(fqs_task)) {
1653                         firsterr = PTR_ERR(fqs_task);
1654                         VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
1655                         fqs_task = NULL;
1656                         goto unwind;
1657                 }
1658         }
1659         if (test_boost_interval < 1)
1660                 test_boost_interval = 1;
1661         if (test_boost_duration < 2)
1662                 test_boost_duration = 2;
1663         if ((test_boost == 1 && cur_ops->can_boost) ||
1664             test_boost == 2) {
1665                 int retval;
1666
1667                 boost_starttime = jiffies + test_boost_interval * HZ;
1668                 register_cpu_notifier(&rcutorture_cpu_nb);
1669                 for_each_possible_cpu(i) {
1670                         if (cpu_is_offline(i))
1671                                 continue;  /* Heuristic: CPU can go offline. */
1672                         retval = rcutorture_booster_init(i);
1673                         if (retval < 0) {
1674                                 firsterr = retval;
1675                                 goto unwind;
1676                         }
1677                 }
1678         }
1679         if (shutdown_secs > 0) {
1680                 shutdown_time = jiffies + shutdown_secs * HZ;
1681                 shutdown_task = kthread_run(rcu_torture_shutdown, NULL,
1682                                             "rcu_torture_shutdown");
1683                 if (IS_ERR(shutdown_task)) {
1684                         firsterr = PTR_ERR(shutdown_task);
1685                         VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
1686                         shutdown_task = NULL;
1687                         goto unwind;
1688                 }
1689         }
1690         register_reboot_notifier(&rcutorture_shutdown_nb);
1691         rcutorture_record_test_transition();
1692         mutex_unlock(&fullstop_mutex);
1693         return 0;
1694
1695 unwind:
1696         mutex_unlock(&fullstop_mutex);
1697         rcu_torture_cleanup();
1698         return firsterr;
1699 }
1700
1701 module_init(rcu_torture_init);
1702 module_exit(rcu_torture_cleanup);