rcu: Increase rcutorture test coverage
[platform/adaptation/renesas_rcar/renesas_kernel.git] / kernel / rcutorture.c
1 /*
2  * Read-Copy Update module-based torture test facility
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Copyright (C) IBM Corporation, 2005, 2006
19  *
20  * Authors: Paul E. McKenney <paulmck@us.ibm.com>
21  *        Josh Triplett <josh@freedesktop.org>
22  *
23  * See also:  Documentation/RCU/torture.txt
24  */
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/kthread.h>
30 #include <linux/err.h>
31 #include <linux/spinlock.h>
32 #include <linux/smp.h>
33 #include <linux/rcupdate.h>
34 #include <linux/interrupt.h>
35 #include <linux/sched.h>
36 #include <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 <linux/trace_clock.h>
50 #include <asm/byteorder.h>
51
52 MODULE_LICENSE("GPL");
53 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and 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 = 60;  /* Interval between stats, in seconds. */
58                                 /*  Zero means "only at end of test". */
59 static bool verbose;            /* Print more debug info. */
60 static bool test_no_idle_hz = true;
61                                 /* Test RCU support for tickless idle CPUs. */
62 static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
63 static int stutter = 5;         /* Start/stop testing interval (in sec) */
64 static int irqreader = 1;       /* RCU readers from irq (timers). */
65 static int fqs_duration;        /* Duration of bursts (us), 0 to disable. */
66 static int fqs_holdoff;         /* Hold time within burst (us). */
67 static int fqs_stutter = 3;     /* Wait time between bursts (s). */
68 static bool gp_exp;             /* Use expedited GP wait primitives. */
69 static bool gp_normal;          /* Use normal GP wait primitives. */
70 static int n_barrier_cbs;       /* Number of callbacks to test RCU barriers. */
71 static int object_debug;        /* Test object-debug double call_rcu()?. */
72 static int onoff_interval;      /* Wait time between CPU hotplugs, 0=disable. */
73 static int onoff_holdoff;       /* Seconds after boot before CPU hotplugs. */
74 static int shutdown_secs;       /* Shutdown time (s).  <=0 for no shutdown. */
75 static int stall_cpu;           /* CPU-stall duration (s).  0 for no stall. */
76 static int stall_cpu_holdoff = 10; /* Time to wait until stall (s).  */
77 static int test_boost = 1;      /* Test RCU prio boost: 0=no, 1=maybe, 2=yes. */
78 static int test_boost_interval = 7; /* Interval between boost tests, seconds. */
79 static int test_boost_duration = 4; /* Duration of each boost test, seconds. */
80 static char *torture_type = "rcu"; /* What RCU implementation to torture. */
81
82 module_param(nreaders, int, 0444);
83 MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
84 module_param(nfakewriters, int, 0444);
85 MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
86 module_param(stat_interval, int, 0644);
87 MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
88 module_param(verbose, bool, 0444);
89 MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
90 module_param(test_no_idle_hz, bool, 0444);
91 MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
92 module_param(shuffle_interval, int, 0444);
93 MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
94 module_param(stutter, int, 0444);
95 MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
96 module_param(irqreader, int, 0444);
97 MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
98 module_param(fqs_duration, int, 0444);
99 MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
100 module_param(fqs_holdoff, int, 0444);
101 MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
102 module_param(fqs_stutter, int, 0444);
103 MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
104 module_param(gp_normal, bool, 0444);
105 MODULE_PARM_DESC(gp_normal, "Use normal (non-expedited) GP wait primitives");
106 module_param(gp_exp, bool, 0444);
107 MODULE_PARM_DESC(gp_exp, "Use expedited GP wait primitives");
108 module_param(n_barrier_cbs, int, 0444);
109 MODULE_PARM_DESC(n_barrier_cbs, "# of callbacks/kthreads for barrier testing");
110 module_param(object_debug, int, 0444);
111 MODULE_PARM_DESC(object_debug, "Enable debug-object double call_rcu() testing");
112 module_param(onoff_interval, int, 0444);
113 MODULE_PARM_DESC(onoff_interval, "Time between CPU hotplugs (s), 0=disable");
114 module_param(onoff_holdoff, int, 0444);
115 MODULE_PARM_DESC(onoff_holdoff, "Time after boot before CPU hotplugs (s)");
116 module_param(shutdown_secs, int, 0444);
117 MODULE_PARM_DESC(shutdown_secs, "Shutdown time (s), zero to disable.");
118 module_param(stall_cpu, int, 0444);
119 MODULE_PARM_DESC(stall_cpu, "Stall duration (s), zero to disable.");
120 module_param(stall_cpu_holdoff, int, 0444);
121 MODULE_PARM_DESC(stall_cpu_holdoff, "Time to wait before starting stall (s).");
122 module_param(test_boost, int, 0444);
123 MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
124 module_param(test_boost_interval, int, 0444);
125 MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
126 module_param(test_boost_duration, int, 0444);
127 MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
128 module_param(torture_type, charp, 0444);
129 MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
130
131 #define TORTURE_FLAG "-torture:"
132 #define PRINTK_STRING(s) \
133         do { pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
134 #define VERBOSE_PRINTK_STRING(s) \
135         do { if (verbose) pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
136 #define VERBOSE_PRINTK_ERRSTRING(s) \
137         do { if (verbose) pr_alert("%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
138
139 static char printk_buf[4096];
140
141 static int nrealreaders;
142 static struct task_struct *writer_task;
143 static struct task_struct **fakewriter_tasks;
144 static struct task_struct **reader_tasks;
145 static struct task_struct *stats_task;
146 static struct task_struct *shuffler_task;
147 static struct task_struct *stutter_task;
148 static struct task_struct *fqs_task;
149 static struct task_struct *boost_tasks[NR_CPUS];
150 static struct task_struct *shutdown_task;
151 #ifdef CONFIG_HOTPLUG_CPU
152 static struct task_struct *onoff_task;
153 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
154 static struct task_struct *stall_task;
155 static struct task_struct **barrier_cbs_tasks;
156 static struct task_struct *barrier_task;
157
158 #define RCU_TORTURE_PIPE_LEN 10
159
160 struct rcu_torture {
161         struct rcu_head rtort_rcu;
162         int rtort_pipe_count;
163         struct list_head rtort_free;
164         int rtort_mbtest;
165 };
166
167 static LIST_HEAD(rcu_torture_freelist);
168 static struct rcu_torture __rcu *rcu_torture_current;
169 static unsigned long rcu_torture_current_version;
170 static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
171 static DEFINE_SPINLOCK(rcu_torture_lock);
172 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
173         { 0 };
174 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
175         { 0 };
176 static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
177 static atomic_t n_rcu_torture_alloc;
178 static atomic_t n_rcu_torture_alloc_fail;
179 static atomic_t n_rcu_torture_free;
180 static atomic_t n_rcu_torture_mberror;
181 static atomic_t n_rcu_torture_error;
182 static long n_rcu_torture_barrier_error;
183 static long n_rcu_torture_boost_ktrerror;
184 static long n_rcu_torture_boost_rterror;
185 static long n_rcu_torture_boost_failure;
186 static long n_rcu_torture_boosts;
187 static long n_rcu_torture_timers;
188 static long n_offline_attempts;
189 static long n_offline_successes;
190 static unsigned long sum_offline;
191 static int min_offline = -1;
192 static int max_offline;
193 static long n_online_attempts;
194 static long n_online_successes;
195 static unsigned long sum_online;
196 static int min_online = -1;
197 static int max_online;
198 static long n_barrier_attempts;
199 static long n_barrier_successes;
200 static struct list_head rcu_torture_removed;
201 static cpumask_var_t shuffle_tmp_mask;
202
203 static int stutter_pause_test;
204
205 #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
206 #define RCUTORTURE_RUNNABLE_INIT 1
207 #else
208 #define RCUTORTURE_RUNNABLE_INIT 0
209 #endif
210 int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
211 module_param(rcutorture_runnable, int, 0444);
212 MODULE_PARM_DESC(rcutorture_runnable, "Start rcutorture at boot");
213
214 #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
215 #define rcu_can_boost() 1
216 #else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
217 #define rcu_can_boost() 0
218 #endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
219
220 #ifdef CONFIG_RCU_TRACE
221 static u64 notrace rcu_trace_clock_local(void)
222 {
223         u64 ts = trace_clock_local();
224         unsigned long __maybe_unused ts_rem = do_div(ts, NSEC_PER_USEC);
225         return ts;
226 }
227 #else /* #ifdef CONFIG_RCU_TRACE */
228 static u64 notrace rcu_trace_clock_local(void)
229 {
230         return 0ULL;
231 }
232 #endif /* #else #ifdef CONFIG_RCU_TRACE */
233
234 static unsigned long shutdown_time;     /* jiffies to system shutdown. */
235 static unsigned long boost_starttime;   /* jiffies of next boost test start. */
236 DEFINE_MUTEX(boost_mutex);              /* protect setting boost_starttime */
237                                         /*  and boost task create/destroy. */
238 static atomic_t barrier_cbs_count;      /* Barrier callbacks registered. */
239 static bool barrier_phase;              /* Test phase. */
240 static atomic_t barrier_cbs_invoked;    /* Barrier callbacks invoked. */
241 static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
242 static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
243
244 /* Mediate rmmod and system shutdown.  Concurrent rmmod & shutdown illegal! */
245
246 #define FULLSTOP_DONTSTOP 0     /* Normal operation. */
247 #define FULLSTOP_SHUTDOWN 1     /* System shutdown with rcutorture running. */
248 #define FULLSTOP_RMMOD    2     /* Normal rmmod of rcutorture. */
249 static int fullstop = FULLSTOP_RMMOD;
250 /*
251  * Protect fullstop transitions and spawning of kthreads.
252  */
253 static DEFINE_MUTEX(fullstop_mutex);
254
255 /* Forward reference. */
256 static void rcu_torture_cleanup(void);
257
258 /*
259  * Detect and respond to a system shutdown.
260  */
261 static int
262 rcutorture_shutdown_notify(struct notifier_block *unused1,
263                            unsigned long unused2, void *unused3)
264 {
265         mutex_lock(&fullstop_mutex);
266         if (fullstop == FULLSTOP_DONTSTOP)
267                 fullstop = FULLSTOP_SHUTDOWN;
268         else
269                 pr_warn(/* but going down anyway, so... */
270                        "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
271         mutex_unlock(&fullstop_mutex);
272         return NOTIFY_DONE;
273 }
274
275 /*
276  * Absorb kthreads into a kernel function that won't return, so that
277  * they won't ever access module text or data again.
278  */
279 static void rcutorture_shutdown_absorb(const char *title)
280 {
281         if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
282                 pr_notice(
283                        "rcutorture thread %s parking due to system shutdown\n",
284                        title);
285                 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
286         }
287 }
288
289 /*
290  * Allocate an element from the rcu_tortures pool.
291  */
292 static struct rcu_torture *
293 rcu_torture_alloc(void)
294 {
295         struct list_head *p;
296
297         spin_lock_bh(&rcu_torture_lock);
298         if (list_empty(&rcu_torture_freelist)) {
299                 atomic_inc(&n_rcu_torture_alloc_fail);
300                 spin_unlock_bh(&rcu_torture_lock);
301                 return NULL;
302         }
303         atomic_inc(&n_rcu_torture_alloc);
304         p = rcu_torture_freelist.next;
305         list_del_init(p);
306         spin_unlock_bh(&rcu_torture_lock);
307         return container_of(p, struct rcu_torture, rtort_free);
308 }
309
310 /*
311  * Free an element to the rcu_tortures pool.
312  */
313 static void
314 rcu_torture_free(struct rcu_torture *p)
315 {
316         atomic_inc(&n_rcu_torture_free);
317         spin_lock_bh(&rcu_torture_lock);
318         list_add_tail(&p->rtort_free, &rcu_torture_freelist);
319         spin_unlock_bh(&rcu_torture_lock);
320 }
321
322 struct rcu_random_state {
323         unsigned long rrs_state;
324         long rrs_count;
325 };
326
327 #define RCU_RANDOM_MULT 39916801  /* prime */
328 #define RCU_RANDOM_ADD  479001701 /* prime */
329 #define RCU_RANDOM_REFRESH 10000
330
331 #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
332
333 /*
334  * Crude but fast random-number generator.  Uses a linear congruential
335  * generator, with occasional help from cpu_clock().
336  */
337 static unsigned long
338 rcu_random(struct rcu_random_state *rrsp)
339 {
340         if (--rrsp->rrs_count < 0) {
341                 rrsp->rrs_state += (unsigned long)local_clock();
342                 rrsp->rrs_count = RCU_RANDOM_REFRESH;
343         }
344         rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
345         return swahw32(rrsp->rrs_state);
346 }
347
348 static void
349 rcu_stutter_wait(const char *title)
350 {
351         while (stutter_pause_test || !rcutorture_runnable) {
352                 if (rcutorture_runnable)
353                         schedule_timeout_interruptible(1);
354                 else
355                         schedule_timeout_interruptible(round_jiffies_relative(HZ));
356                 rcutorture_shutdown_absorb(title);
357         }
358 }
359
360 /*
361  * Operations vector for selecting different types of tests.
362  */
363
364 struct rcu_torture_ops {
365         void (*init)(void);
366         int (*readlock)(void);
367         void (*read_delay)(struct rcu_random_state *rrsp);
368         void (*readunlock)(int idx);
369         int (*completed)(void);
370         void (*deferred_free)(struct rcu_torture *p);
371         void (*sync)(void);
372         void (*exp_sync)(void);
373         void (*call)(struct rcu_head *head, void (*func)(struct rcu_head *rcu));
374         void (*cb_barrier)(void);
375         void (*fqs)(void);
376         int (*stats)(char *page);
377         int irq_capable;
378         int can_boost;
379         const char *name;
380 };
381
382 static struct rcu_torture_ops *cur_ops;
383
384 /*
385  * Definitions for rcu torture testing.
386  */
387
388 static int rcu_torture_read_lock(void) __acquires(RCU)
389 {
390         rcu_read_lock();
391         return 0;
392 }
393
394 static void rcu_read_delay(struct rcu_random_state *rrsp)
395 {
396         const unsigned long shortdelay_us = 200;
397         const unsigned long longdelay_ms = 50;
398
399         /* We want a short delay sometimes to make a reader delay the grace
400          * period, and we want a long delay occasionally to trigger
401          * force_quiescent_state. */
402
403         if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
404                 mdelay(longdelay_ms);
405         if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
406                 udelay(shortdelay_us);
407 #ifdef CONFIG_PREEMPT
408         if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
409                 preempt_schedule();  /* No QS if preempt_disable() in effect */
410 #endif
411 }
412
413 static void rcu_torture_read_unlock(int idx) __releases(RCU)
414 {
415         rcu_read_unlock();
416 }
417
418 static int rcu_torture_completed(void)
419 {
420         return rcu_batches_completed();
421 }
422
423 static void
424 rcu_torture_cb(struct rcu_head *p)
425 {
426         int i;
427         struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
428
429         if (fullstop != FULLSTOP_DONTSTOP) {
430                 /* Test is ending, just drop callbacks on the floor. */
431                 /* The next initialization will pick up the pieces. */
432                 return;
433         }
434         i = rp->rtort_pipe_count;
435         if (i > RCU_TORTURE_PIPE_LEN)
436                 i = RCU_TORTURE_PIPE_LEN;
437         atomic_inc(&rcu_torture_wcount[i]);
438         if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
439                 rp->rtort_mbtest = 0;
440                 rcu_torture_free(rp);
441         } else {
442                 cur_ops->deferred_free(rp);
443         }
444 }
445
446 static int rcu_no_completed(void)
447 {
448         return 0;
449 }
450
451 static void rcu_torture_deferred_free(struct rcu_torture *p)
452 {
453         call_rcu(&p->rtort_rcu, rcu_torture_cb);
454 }
455
456 static void rcu_sync_torture_init(void)
457 {
458         INIT_LIST_HEAD(&rcu_torture_removed);
459 }
460
461 static struct rcu_torture_ops rcu_ops = {
462         .init           = rcu_sync_torture_init,
463         .readlock       = rcu_torture_read_lock,
464         .read_delay     = rcu_read_delay,
465         .readunlock     = rcu_torture_read_unlock,
466         .completed      = rcu_torture_completed,
467         .deferred_free  = rcu_torture_deferred_free,
468         .sync           = synchronize_rcu,
469         .exp_sync       = synchronize_rcu_expedited,
470         .call           = call_rcu,
471         .cb_barrier     = rcu_barrier,
472         .fqs            = rcu_force_quiescent_state,
473         .stats          = NULL,
474         .irq_capable    = 1,
475         .can_boost      = rcu_can_boost(),
476         .name           = "rcu"
477 };
478
479 /*
480  * Definitions for rcu_bh torture testing.
481  */
482
483 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
484 {
485         rcu_read_lock_bh();
486         return 0;
487 }
488
489 static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
490 {
491         rcu_read_unlock_bh();
492 }
493
494 static int rcu_bh_torture_completed(void)
495 {
496         return rcu_batches_completed_bh();
497 }
498
499 static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
500 {
501         call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
502 }
503
504 static struct rcu_torture_ops rcu_bh_ops = {
505         .init           = rcu_sync_torture_init,
506         .readlock       = rcu_bh_torture_read_lock,
507         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
508         .readunlock     = rcu_bh_torture_read_unlock,
509         .completed      = rcu_bh_torture_completed,
510         .deferred_free  = rcu_bh_torture_deferred_free,
511         .sync           = synchronize_rcu_bh,
512         .exp_sync       = synchronize_rcu_bh_expedited,
513         .call           = call_rcu_bh,
514         .cb_barrier     = rcu_barrier_bh,
515         .fqs            = rcu_bh_force_quiescent_state,
516         .stats          = NULL,
517         .irq_capable    = 1,
518         .name           = "rcu_bh"
519 };
520
521 /*
522  * Definitions for srcu torture testing.
523  */
524
525 DEFINE_STATIC_SRCU(srcu_ctl);
526
527 static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
528 {
529         return srcu_read_lock(&srcu_ctl);
530 }
531
532 static void srcu_read_delay(struct rcu_random_state *rrsp)
533 {
534         long delay;
535         const long uspertick = 1000000 / HZ;
536         const long longdelay = 10;
537
538         /* We want there to be long-running readers, but not all the time. */
539
540         delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
541         if (!delay)
542                 schedule_timeout_interruptible(longdelay);
543         else
544                 rcu_read_delay(rrsp);
545 }
546
547 static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
548 {
549         srcu_read_unlock(&srcu_ctl, idx);
550 }
551
552 static int srcu_torture_completed(void)
553 {
554         return srcu_batches_completed(&srcu_ctl);
555 }
556
557 static void srcu_torture_deferred_free(struct rcu_torture *rp)
558 {
559         call_srcu(&srcu_ctl, &rp->rtort_rcu, rcu_torture_cb);
560 }
561
562 static void srcu_torture_synchronize(void)
563 {
564         synchronize_srcu(&srcu_ctl);
565 }
566
567 static void srcu_torture_call(struct rcu_head *head,
568                               void (*func)(struct rcu_head *head))
569 {
570         call_srcu(&srcu_ctl, head, func);
571 }
572
573 static void srcu_torture_barrier(void)
574 {
575         srcu_barrier(&srcu_ctl);
576 }
577
578 static int srcu_torture_stats(char *page)
579 {
580         int cnt = 0;
581         int cpu;
582         int idx = srcu_ctl.completed & 0x1;
583
584         cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
585                        torture_type, TORTURE_FLAG, idx);
586         for_each_possible_cpu(cpu) {
587                 cnt += sprintf(&page[cnt], " %d(%lu,%lu)", cpu,
588                                per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
589                                per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
590         }
591         cnt += sprintf(&page[cnt], "\n");
592         return cnt;
593 }
594
595 static void srcu_torture_synchronize_expedited(void)
596 {
597         synchronize_srcu_expedited(&srcu_ctl);
598 }
599
600 static struct rcu_torture_ops srcu_ops = {
601         .init           = rcu_sync_torture_init,
602         .readlock       = srcu_torture_read_lock,
603         .read_delay     = srcu_read_delay,
604         .readunlock     = srcu_torture_read_unlock,
605         .completed      = srcu_torture_completed,
606         .deferred_free  = srcu_torture_deferred_free,
607         .sync           = srcu_torture_synchronize,
608         .exp_sync       = srcu_torture_synchronize_expedited,
609         .call           = srcu_torture_call,
610         .cb_barrier     = srcu_torture_barrier,
611         .stats          = srcu_torture_stats,
612         .name           = "srcu"
613 };
614
615 /*
616  * Definitions for sched torture testing.
617  */
618
619 static int sched_torture_read_lock(void)
620 {
621         preempt_disable();
622         return 0;
623 }
624
625 static void sched_torture_read_unlock(int idx)
626 {
627         preempt_enable();
628 }
629
630 static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
631 {
632         call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
633 }
634
635 static struct rcu_torture_ops sched_ops = {
636         .init           = rcu_sync_torture_init,
637         .readlock       = sched_torture_read_lock,
638         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
639         .readunlock     = sched_torture_read_unlock,
640         .completed      = rcu_no_completed,
641         .deferred_free  = rcu_sched_torture_deferred_free,
642         .sync           = synchronize_sched,
643         .exp_sync       = synchronize_sched_expedited,
644         .call           = call_rcu_sched,
645         .cb_barrier     = rcu_barrier_sched,
646         .fqs            = rcu_sched_force_quiescent_state,
647         .stats          = NULL,
648         .irq_capable    = 1,
649         .name           = "sched"
650 };
651
652 /*
653  * RCU torture priority-boost testing.  Runs one real-time thread per
654  * CPU for moderate bursts, repeatedly registering RCU callbacks and
655  * spinning waiting for them to be invoked.  If a given callback takes
656  * too long to be invoked, we assume that priority inversion has occurred.
657  */
658
659 struct rcu_boost_inflight {
660         struct rcu_head rcu;
661         int inflight;
662 };
663
664 static void rcu_torture_boost_cb(struct rcu_head *head)
665 {
666         struct rcu_boost_inflight *rbip =
667                 container_of(head, struct rcu_boost_inflight, rcu);
668
669         smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
670         rbip->inflight = 0;
671 }
672
673 static int rcu_torture_boost(void *arg)
674 {
675         unsigned long call_rcu_time;
676         unsigned long endtime;
677         unsigned long oldstarttime;
678         struct rcu_boost_inflight rbi = { .inflight = 0 };
679         struct sched_param sp;
680
681         VERBOSE_PRINTK_STRING("rcu_torture_boost started");
682
683         /* Set real-time priority. */
684         sp.sched_priority = 1;
685         if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
686                 VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
687                 n_rcu_torture_boost_rterror++;
688         }
689
690         init_rcu_head_on_stack(&rbi.rcu);
691         /* Each pass through the following loop does one boost-test cycle. */
692         do {
693                 /* Wait for the next test interval. */
694                 oldstarttime = boost_starttime;
695                 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
696                         schedule_timeout_interruptible(oldstarttime - jiffies);
697                         rcu_stutter_wait("rcu_torture_boost");
698                         if (kthread_should_stop() ||
699                             fullstop != FULLSTOP_DONTSTOP)
700                                 goto checkwait;
701                 }
702
703                 /* Do one boost-test interval. */
704                 endtime = oldstarttime + test_boost_duration * HZ;
705                 call_rcu_time = jiffies;
706                 while (ULONG_CMP_LT(jiffies, endtime)) {
707                         /* If we don't have a callback in flight, post one. */
708                         if (!rbi.inflight) {
709                                 smp_mb(); /* RCU core before ->inflight = 1. */
710                                 rbi.inflight = 1;
711                                 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
712                                 if (jiffies - call_rcu_time >
713                                          test_boost_duration * HZ - HZ / 2) {
714                                         VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
715                                         n_rcu_torture_boost_failure++;
716                                 }
717                                 call_rcu_time = jiffies;
718                         }
719                         cond_resched();
720                         rcu_stutter_wait("rcu_torture_boost");
721                         if (kthread_should_stop() ||
722                             fullstop != FULLSTOP_DONTSTOP)
723                                 goto checkwait;
724                 }
725
726                 /*
727                  * Set the start time of the next test interval.
728                  * Yes, this is vulnerable to long delays, but such
729                  * delays simply cause a false negative for the next
730                  * interval.  Besides, we are running at RT priority,
731                  * so delays should be relatively rare.
732                  */
733                 while (oldstarttime == boost_starttime &&
734                        !kthread_should_stop()) {
735                         if (mutex_trylock(&boost_mutex)) {
736                                 boost_starttime = jiffies +
737                                                   test_boost_interval * HZ;
738                                 n_rcu_torture_boosts++;
739                                 mutex_unlock(&boost_mutex);
740                                 break;
741                         }
742                         schedule_timeout_uninterruptible(1);
743                 }
744
745                 /* Go do the stutter. */
746 checkwait:      rcu_stutter_wait("rcu_torture_boost");
747         } while (!kthread_should_stop() && fullstop  == FULLSTOP_DONTSTOP);
748
749         /* Clean up and exit. */
750         VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
751         rcutorture_shutdown_absorb("rcu_torture_boost");
752         while (!kthread_should_stop() || rbi.inflight)
753                 schedule_timeout_uninterruptible(1);
754         smp_mb(); /* order accesses to ->inflight before stack-frame death. */
755         destroy_rcu_head_on_stack(&rbi.rcu);
756         return 0;
757 }
758
759 /*
760  * RCU torture force-quiescent-state kthread.  Repeatedly induces
761  * bursts of calls to force_quiescent_state(), increasing the probability
762  * of occurrence of some important types of race conditions.
763  */
764 static int
765 rcu_torture_fqs(void *arg)
766 {
767         unsigned long fqs_resume_time;
768         int fqs_burst_remaining;
769
770         VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
771         do {
772                 fqs_resume_time = jiffies + fqs_stutter * HZ;
773                 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
774                        !kthread_should_stop()) {
775                         schedule_timeout_interruptible(1);
776                 }
777                 fqs_burst_remaining = fqs_duration;
778                 while (fqs_burst_remaining > 0 &&
779                        !kthread_should_stop()) {
780                         cur_ops->fqs();
781                         udelay(fqs_holdoff);
782                         fqs_burst_remaining -= fqs_holdoff;
783                 }
784                 rcu_stutter_wait("rcu_torture_fqs");
785         } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
786         VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
787         rcutorture_shutdown_absorb("rcu_torture_fqs");
788         while (!kthread_should_stop())
789                 schedule_timeout_uninterruptible(1);
790         return 0;
791 }
792
793 /*
794  * RCU torture writer kthread.  Repeatedly substitutes a new structure
795  * for that pointed to by rcu_torture_current, freeing the old structure
796  * after a series of grace periods (the "pipeline").
797  */
798 static int
799 rcu_torture_writer(void *arg)
800 {
801         bool exp;
802         int i;
803         long oldbatch = rcu_batches_completed();
804         struct rcu_torture *rp;
805         struct rcu_torture *rp1;
806         struct rcu_torture *old_rp;
807         static DEFINE_RCU_RANDOM(rand);
808
809         VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
810         set_user_nice(current, 19);
811
812         do {
813                 schedule_timeout_uninterruptible(1);
814                 rp = rcu_torture_alloc();
815                 if (rp == NULL)
816                         continue;
817                 rp->rtort_pipe_count = 0;
818                 udelay(rcu_random(&rand) & 0x3ff);
819                 old_rp = rcu_dereference_check(rcu_torture_current,
820                                                current == writer_task);
821                 rp->rtort_mbtest = 1;
822                 rcu_assign_pointer(rcu_torture_current, rp);
823                 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
824                 if (old_rp) {
825                         i = old_rp->rtort_pipe_count;
826                         if (i > RCU_TORTURE_PIPE_LEN)
827                                 i = RCU_TORTURE_PIPE_LEN;
828                         atomic_inc(&rcu_torture_wcount[i]);
829                         old_rp->rtort_pipe_count++;
830                         if (gp_normal == gp_exp)
831                                 exp = !!(rcu_random(&rand) & 0x80);
832                         else
833                                 exp = gp_exp;
834                         if (!exp) {
835                                 cur_ops->deferred_free(old_rp);
836                         } else {
837                                 cur_ops->exp_sync();
838                                 list_add(&old_rp->rtort_free,
839                                          &rcu_torture_removed);
840                                 list_for_each_entry_safe(rp, rp1,
841                                                          &rcu_torture_removed,
842                                                          rtort_free) {
843                                         i = rp->rtort_pipe_count;
844                                         if (i > RCU_TORTURE_PIPE_LEN)
845                                                 i = RCU_TORTURE_PIPE_LEN;
846                                         atomic_inc(&rcu_torture_wcount[i]);
847                                         if (++rp->rtort_pipe_count >=
848                                             RCU_TORTURE_PIPE_LEN) {
849                                                 rp->rtort_mbtest = 0;
850                                                 list_del(&rp->rtort_free);
851                                                 rcu_torture_free(rp);
852                                         }
853                                  }
854                         }
855                 }
856                 rcutorture_record_progress(++rcu_torture_current_version);
857                 oldbatch = cur_ops->completed();
858                 rcu_stutter_wait("rcu_torture_writer");
859         } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
860         VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
861         rcutorture_shutdown_absorb("rcu_torture_writer");
862         while (!kthread_should_stop())
863                 schedule_timeout_uninterruptible(1);
864         return 0;
865 }
866
867 /*
868  * RCU torture fake writer kthread.  Repeatedly calls sync, with a random
869  * delay between calls.
870  */
871 static int
872 rcu_torture_fakewriter(void *arg)
873 {
874         DEFINE_RCU_RANDOM(rand);
875
876         VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
877         set_user_nice(current, 19);
878
879         do {
880                 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
881                 udelay(rcu_random(&rand) & 0x3ff);
882                 if (cur_ops->cb_barrier != NULL &&
883                     rcu_random(&rand) % (nfakewriters * 8) == 0) {
884                         cur_ops->cb_barrier();
885                 } else if (gp_normal == gp_exp) {
886                         if (rcu_random(&rand) & 0x80)
887                                 cur_ops->sync();
888                         else
889                                 cur_ops->exp_sync();
890                 } else if (gp_normal) {
891                         cur_ops->sync();
892                 } else {
893                         cur_ops->exp_sync();
894                 }
895                 rcu_stutter_wait("rcu_torture_fakewriter");
896         } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
897
898         VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
899         rcutorture_shutdown_absorb("rcu_torture_fakewriter");
900         while (!kthread_should_stop())
901                 schedule_timeout_uninterruptible(1);
902         return 0;
903 }
904
905 void rcutorture_trace_dump(void)
906 {
907         static atomic_t beenhere = ATOMIC_INIT(0);
908
909         if (atomic_read(&beenhere))
910                 return;
911         if (atomic_xchg(&beenhere, 1) != 0)
912                 return;
913         ftrace_dump(DUMP_ALL);
914 }
915
916 /*
917  * RCU torture reader from timer handler.  Dereferences rcu_torture_current,
918  * incrementing the corresponding element of the pipeline array.  The
919  * counter in the element should never be greater than 1, otherwise, the
920  * RCU implementation is broken.
921  */
922 static void rcu_torture_timer(unsigned long unused)
923 {
924         int idx;
925         int completed;
926         int completed_end;
927         static DEFINE_RCU_RANDOM(rand);
928         static DEFINE_SPINLOCK(rand_lock);
929         struct rcu_torture *p;
930         int pipe_count;
931         unsigned long long ts;
932
933         idx = cur_ops->readlock();
934         completed = cur_ops->completed();
935         ts = rcu_trace_clock_local();
936         p = rcu_dereference_check(rcu_torture_current,
937                                   rcu_read_lock_bh_held() ||
938                                   rcu_read_lock_sched_held() ||
939                                   srcu_read_lock_held(&srcu_ctl));
940         if (p == NULL) {
941                 /* Leave because rcu_torture_writer is not yet underway */
942                 cur_ops->readunlock(idx);
943                 return;
944         }
945         if (p->rtort_mbtest == 0)
946                 atomic_inc(&n_rcu_torture_mberror);
947         spin_lock(&rand_lock);
948         cur_ops->read_delay(&rand);
949         n_rcu_torture_timers++;
950         spin_unlock(&rand_lock);
951         preempt_disable();
952         pipe_count = p->rtort_pipe_count;
953         if (pipe_count > RCU_TORTURE_PIPE_LEN) {
954                 /* Should not happen, but... */
955                 pipe_count = RCU_TORTURE_PIPE_LEN;
956         }
957         completed_end = cur_ops->completed();
958         if (pipe_count > 1) {
959                 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
960                                           completed, completed_end);
961                 rcutorture_trace_dump();
962         }
963         __this_cpu_inc(rcu_torture_count[pipe_count]);
964         completed = completed_end - completed;
965         if (completed > RCU_TORTURE_PIPE_LEN) {
966                 /* Should not happen, but... */
967                 completed = RCU_TORTURE_PIPE_LEN;
968         }
969         __this_cpu_inc(rcu_torture_batch[completed]);
970         preempt_enable();
971         cur_ops->readunlock(idx);
972 }
973
974 /*
975  * RCU torture reader kthread.  Repeatedly dereferences rcu_torture_current,
976  * incrementing the corresponding element of the pipeline array.  The
977  * counter in the element should never be greater than 1, otherwise, the
978  * RCU implementation is broken.
979  */
980 static int
981 rcu_torture_reader(void *arg)
982 {
983         int completed;
984         int completed_end;
985         int idx;
986         DEFINE_RCU_RANDOM(rand);
987         struct rcu_torture *p;
988         int pipe_count;
989         struct timer_list t;
990         unsigned long long ts;
991
992         VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
993         set_user_nice(current, 19);
994         if (irqreader && cur_ops->irq_capable)
995                 setup_timer_on_stack(&t, rcu_torture_timer, 0);
996
997         do {
998                 if (irqreader && cur_ops->irq_capable) {
999                         if (!timer_pending(&t))
1000                                 mod_timer(&t, jiffies + 1);
1001                 }
1002                 idx = cur_ops->readlock();
1003                 completed = cur_ops->completed();
1004                 ts = rcu_trace_clock_local();
1005                 p = rcu_dereference_check(rcu_torture_current,
1006                                           rcu_read_lock_bh_held() ||
1007                                           rcu_read_lock_sched_held() ||
1008                                           srcu_read_lock_held(&srcu_ctl));
1009                 if (p == NULL) {
1010                         /* Wait for rcu_torture_writer to get underway */
1011                         cur_ops->readunlock(idx);
1012                         schedule_timeout_interruptible(HZ);
1013                         continue;
1014                 }
1015                 if (p->rtort_mbtest == 0)
1016                         atomic_inc(&n_rcu_torture_mberror);
1017                 cur_ops->read_delay(&rand);
1018                 preempt_disable();
1019                 pipe_count = p->rtort_pipe_count;
1020                 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1021                         /* Should not happen, but... */
1022                         pipe_count = RCU_TORTURE_PIPE_LEN;
1023                 }
1024                 completed_end = cur_ops->completed();
1025                 if (pipe_count > 1) {
1026                         do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1027                                                   ts, completed, completed_end);
1028                         rcutorture_trace_dump();
1029                 }
1030                 __this_cpu_inc(rcu_torture_count[pipe_count]);
1031                 completed = completed_end - completed;
1032                 if (completed > RCU_TORTURE_PIPE_LEN) {
1033                         /* Should not happen, but... */
1034                         completed = RCU_TORTURE_PIPE_LEN;
1035                 }
1036                 __this_cpu_inc(rcu_torture_batch[completed]);
1037                 preempt_enable();
1038                 cur_ops->readunlock(idx);
1039                 schedule();
1040                 rcu_stutter_wait("rcu_torture_reader");
1041         } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1042         VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
1043         rcutorture_shutdown_absorb("rcu_torture_reader");
1044         if (irqreader && cur_ops->irq_capable)
1045                 del_timer_sync(&t);
1046         while (!kthread_should_stop())
1047                 schedule_timeout_uninterruptible(1);
1048         return 0;
1049 }
1050
1051 /*
1052  * Create an RCU-torture statistics message in the specified buffer.
1053  */
1054 static int
1055 rcu_torture_printk(char *page)
1056 {
1057         int cnt = 0;
1058         int cpu;
1059         int i;
1060         long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1061         long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1062
1063         for_each_possible_cpu(cpu) {
1064                 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1065                         pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1066                         batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1067                 }
1068         }
1069         for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1070                 if (pipesummary[i] != 0)
1071                         break;
1072         }
1073         cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
1074         cnt += sprintf(&page[cnt],
1075                        "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
1076                        rcu_torture_current,
1077                        rcu_torture_current_version,
1078                        list_empty(&rcu_torture_freelist),
1079                        atomic_read(&n_rcu_torture_alloc),
1080                        atomic_read(&n_rcu_torture_alloc_fail),
1081                        atomic_read(&n_rcu_torture_free));
1082         cnt += sprintf(&page[cnt], "rtmbe: %d rtbke: %ld rtbre: %ld ",
1083                        atomic_read(&n_rcu_torture_mberror),
1084                        n_rcu_torture_boost_ktrerror,
1085                        n_rcu_torture_boost_rterror);
1086         cnt += sprintf(&page[cnt], "rtbf: %ld rtb: %ld nt: %ld ",
1087                        n_rcu_torture_boost_failure,
1088                        n_rcu_torture_boosts,
1089                        n_rcu_torture_timers);
1090         cnt += sprintf(&page[cnt],
1091                        "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
1092                        n_online_successes, n_online_attempts,
1093                        n_offline_successes, n_offline_attempts,
1094                        min_online, max_online,
1095                        min_offline, max_offline,
1096                        sum_online, sum_offline, HZ);
1097         cnt += sprintf(&page[cnt], "barrier: %ld/%ld:%ld",
1098                        n_barrier_successes,
1099                        n_barrier_attempts,
1100                        n_rcu_torture_barrier_error);
1101         cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1102         if (atomic_read(&n_rcu_torture_mberror) != 0 ||
1103             n_rcu_torture_barrier_error != 0 ||
1104             n_rcu_torture_boost_ktrerror != 0 ||
1105             n_rcu_torture_boost_rterror != 0 ||
1106             n_rcu_torture_boost_failure != 0 ||
1107             i > 1) {
1108                 cnt += sprintf(&page[cnt], "!!! ");
1109                 atomic_inc(&n_rcu_torture_error);
1110                 WARN_ON_ONCE(1);
1111         }
1112         cnt += sprintf(&page[cnt], "Reader Pipe: ");
1113         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1114                 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
1115         cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1116         cnt += sprintf(&page[cnt], "Reader Batch: ");
1117         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1118                 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
1119         cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1120         cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
1121         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1122                 cnt += sprintf(&page[cnt], " %d",
1123                                atomic_read(&rcu_torture_wcount[i]));
1124         }
1125         cnt += sprintf(&page[cnt], "\n");
1126         if (cur_ops->stats)
1127                 cnt += cur_ops->stats(&page[cnt]);
1128         return cnt;
1129 }
1130
1131 /*
1132  * Print torture statistics.  Caller must ensure that there is only
1133  * one call to this function at a given time!!!  This is normally
1134  * accomplished by relying on the module system to only have one copy
1135  * of the module loaded, and then by giving the rcu_torture_stats
1136  * kthread full control (or the init/cleanup functions when rcu_torture_stats
1137  * thread is not running).
1138  */
1139 static void
1140 rcu_torture_stats_print(void)
1141 {
1142         int cnt;
1143
1144         cnt = rcu_torture_printk(printk_buf);
1145         pr_alert("%s", printk_buf);
1146 }
1147
1148 /*
1149  * Periodically prints torture statistics, if periodic statistics printing
1150  * was specified via the stat_interval module parameter.
1151  *
1152  * No need to worry about fullstop here, since this one doesn't reference
1153  * volatile state or register callbacks.
1154  */
1155 static int
1156 rcu_torture_stats(void *arg)
1157 {
1158         VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
1159         do {
1160                 schedule_timeout_interruptible(stat_interval * HZ);
1161                 rcu_torture_stats_print();
1162                 rcutorture_shutdown_absorb("rcu_torture_stats");
1163         } while (!kthread_should_stop());
1164         VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1165         return 0;
1166 }
1167
1168 static int rcu_idle_cpu;        /* Force all torture tasks off this CPU */
1169
1170 /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1171  * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1172  */
1173 static void rcu_torture_shuffle_tasks(void)
1174 {
1175         int i;
1176
1177         cpumask_setall(shuffle_tmp_mask);
1178         get_online_cpus();
1179
1180         /* No point in shuffling if there is only one online CPU (ex: UP) */
1181         if (num_online_cpus() == 1) {
1182                 put_online_cpus();
1183                 return;
1184         }
1185
1186         if (rcu_idle_cpu != -1)
1187                 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
1188
1189         set_cpus_allowed_ptr(current, shuffle_tmp_mask);
1190
1191         if (reader_tasks) {
1192                 for (i = 0; i < nrealreaders; i++)
1193                         if (reader_tasks[i])
1194                                 set_cpus_allowed_ptr(reader_tasks[i],
1195                                                      shuffle_tmp_mask);
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         if (writer_task)
1204                 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
1205         if (stats_task)
1206                 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
1207         if (stutter_task)
1208                 set_cpus_allowed_ptr(stutter_task, shuffle_tmp_mask);
1209         if (fqs_task)
1210                 set_cpus_allowed_ptr(fqs_task, shuffle_tmp_mask);
1211         if (shutdown_task)
1212                 set_cpus_allowed_ptr(shutdown_task, shuffle_tmp_mask);
1213 #ifdef CONFIG_HOTPLUG_CPU
1214         if (onoff_task)
1215                 set_cpus_allowed_ptr(onoff_task, shuffle_tmp_mask);
1216 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
1217         if (stall_task)
1218                 set_cpus_allowed_ptr(stall_task, shuffle_tmp_mask);
1219         if (barrier_cbs_tasks)
1220                 for (i = 0; i < n_barrier_cbs; i++)
1221                         if (barrier_cbs_tasks[i])
1222                                 set_cpus_allowed_ptr(barrier_cbs_tasks[i],
1223                                                      shuffle_tmp_mask);
1224         if (barrier_task)
1225                 set_cpus_allowed_ptr(barrier_task, shuffle_tmp_mask);
1226
1227         if (rcu_idle_cpu == -1)
1228                 rcu_idle_cpu = num_online_cpus() - 1;
1229         else
1230                 rcu_idle_cpu--;
1231
1232         put_online_cpus();
1233 }
1234
1235 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1236  * system to become idle at a time and cut off its timer ticks. This is meant
1237  * to test the support for such tickless idle CPU in RCU.
1238  */
1239 static int
1240 rcu_torture_shuffle(void *arg)
1241 {
1242         VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1243         do {
1244                 schedule_timeout_interruptible(shuffle_interval * HZ);
1245                 rcu_torture_shuffle_tasks();
1246                 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1247         } while (!kthread_should_stop());
1248         VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1249         return 0;
1250 }
1251
1252 /* Cause the rcutorture test to "stutter", starting and stopping all
1253  * threads periodically.
1254  */
1255 static int
1256 rcu_torture_stutter(void *arg)
1257 {
1258         VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1259         do {
1260                 schedule_timeout_interruptible(stutter * HZ);
1261                 stutter_pause_test = 1;
1262                 if (!kthread_should_stop())
1263                         schedule_timeout_interruptible(stutter * HZ);
1264                 stutter_pause_test = 0;
1265                 rcutorture_shutdown_absorb("rcu_torture_stutter");
1266         } while (!kthread_should_stop());
1267         VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1268         return 0;
1269 }
1270
1271 static inline void
1272 rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
1273 {
1274         pr_alert("%s" TORTURE_FLAG
1275                  "--- %s: nreaders=%d nfakewriters=%d "
1276                  "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1277                  "shuffle_interval=%d stutter=%d irqreader=%d "
1278                  "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1279                  "test_boost=%d/%d test_boost_interval=%d "
1280                  "test_boost_duration=%d shutdown_secs=%d "
1281                  "stall_cpu=%d stall_cpu_holdoff=%d "
1282                  "n_barrier_cbs=%d "
1283                  "onoff_interval=%d onoff_holdoff=%d\n",
1284                  torture_type, tag, nrealreaders, nfakewriters,
1285                  stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1286                  stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1287                  test_boost, cur_ops->can_boost,
1288                  test_boost_interval, test_boost_duration, shutdown_secs,
1289                  stall_cpu, stall_cpu_holdoff,
1290                  n_barrier_cbs,
1291                  onoff_interval, onoff_holdoff);
1292 }
1293
1294 static struct notifier_block rcutorture_shutdown_nb = {
1295         .notifier_call = rcutorture_shutdown_notify,
1296 };
1297
1298 static void rcutorture_booster_cleanup(int cpu)
1299 {
1300         struct task_struct *t;
1301
1302         if (boost_tasks[cpu] == NULL)
1303                 return;
1304         mutex_lock(&boost_mutex);
1305         VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
1306         t = boost_tasks[cpu];
1307         boost_tasks[cpu] = NULL;
1308         mutex_unlock(&boost_mutex);
1309
1310         /* This must be outside of the mutex, otherwise deadlock! */
1311         kthread_stop(t);
1312         boost_tasks[cpu] = NULL;
1313 }
1314
1315 static int rcutorture_booster_init(int cpu)
1316 {
1317         int retval;
1318
1319         if (boost_tasks[cpu] != NULL)
1320                 return 0;  /* Already created, nothing more to do. */
1321
1322         /* Don't allow time recalculation while creating a new task. */
1323         mutex_lock(&boost_mutex);
1324         VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
1325         boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1326                                                   cpu_to_node(cpu),
1327                                                   "rcu_torture_boost");
1328         if (IS_ERR(boost_tasks[cpu])) {
1329                 retval = PTR_ERR(boost_tasks[cpu]);
1330                 VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
1331                 n_rcu_torture_boost_ktrerror++;
1332                 boost_tasks[cpu] = NULL;
1333                 mutex_unlock(&boost_mutex);
1334                 return retval;
1335         }
1336         kthread_bind(boost_tasks[cpu], cpu);
1337         wake_up_process(boost_tasks[cpu]);
1338         mutex_unlock(&boost_mutex);
1339         return 0;
1340 }
1341
1342 /*
1343  * Cause the rcutorture test to shutdown the system after the test has
1344  * run for the time specified by the shutdown_secs module parameter.
1345  */
1346 static int
1347 rcu_torture_shutdown(void *arg)
1348 {
1349         long delta;
1350         unsigned long jiffies_snap;
1351
1352         VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
1353         jiffies_snap = ACCESS_ONCE(jiffies);
1354         while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
1355                !kthread_should_stop()) {
1356                 delta = shutdown_time - jiffies_snap;
1357                 if (verbose)
1358                         pr_alert("%s" TORTURE_FLAG
1359                                  "rcu_torture_shutdown task: %lu jiffies remaining\n",
1360                                  torture_type, delta);
1361                 schedule_timeout_interruptible(delta);
1362                 jiffies_snap = ACCESS_ONCE(jiffies);
1363         }
1364         if (kthread_should_stop()) {
1365                 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
1366                 return 0;
1367         }
1368
1369         /* OK, shut down the system. */
1370
1371         VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
1372         shutdown_task = NULL;   /* Avoid self-kill deadlock. */
1373         rcu_torture_cleanup();  /* Get the success/failure message. */
1374         kernel_power_off();     /* Shut down the system. */
1375         return 0;
1376 }
1377
1378 #ifdef CONFIG_HOTPLUG_CPU
1379
1380 /*
1381  * Execute random CPU-hotplug operations at the interval specified
1382  * by the onoff_interval.
1383  */
1384 static int
1385 rcu_torture_onoff(void *arg)
1386 {
1387         int cpu;
1388         unsigned long delta;
1389         int maxcpu = -1;
1390         DEFINE_RCU_RANDOM(rand);
1391         int ret;
1392         unsigned long starttime;
1393
1394         VERBOSE_PRINTK_STRING("rcu_torture_onoff task started");
1395         for_each_online_cpu(cpu)
1396                 maxcpu = cpu;
1397         WARN_ON(maxcpu < 0);
1398         if (onoff_holdoff > 0) {
1399                 VERBOSE_PRINTK_STRING("rcu_torture_onoff begin holdoff");
1400                 schedule_timeout_interruptible(onoff_holdoff * HZ);
1401                 VERBOSE_PRINTK_STRING("rcu_torture_onoff end holdoff");
1402         }
1403         while (!kthread_should_stop()) {
1404                 cpu = (rcu_random(&rand) >> 4) % (maxcpu + 1);
1405                 if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
1406                         if (verbose)
1407                                 pr_alert("%s" TORTURE_FLAG
1408                                          "rcu_torture_onoff task: offlining %d\n",
1409                                          torture_type, cpu);
1410                         starttime = jiffies;
1411                         n_offline_attempts++;
1412                         ret = cpu_down(cpu);
1413                         if (ret) {
1414                                 if (verbose)
1415                                         pr_alert("%s" TORTURE_FLAG
1416                                                  "rcu_torture_onoff task: offline %d failed: errno %d\n",
1417                                                  torture_type, cpu, ret);
1418                         } else {
1419                                 if (verbose)
1420                                         pr_alert("%s" TORTURE_FLAG
1421                                                  "rcu_torture_onoff task: offlined %d\n",
1422                                                  torture_type, cpu);
1423                                 n_offline_successes++;
1424                                 delta = jiffies - starttime;
1425                                 sum_offline += delta;
1426                                 if (min_offline < 0) {
1427                                         min_offline = delta;
1428                                         max_offline = delta;
1429                                 }
1430                                 if (min_offline > delta)
1431                                         min_offline = delta;
1432                                 if (max_offline < delta)
1433                                         max_offline = delta;
1434                         }
1435                 } else if (cpu_is_hotpluggable(cpu)) {
1436                         if (verbose)
1437                                 pr_alert("%s" TORTURE_FLAG
1438                                          "rcu_torture_onoff task: onlining %d\n",
1439                                          torture_type, cpu);
1440                         starttime = jiffies;
1441                         n_online_attempts++;
1442                         if (cpu_up(cpu) == 0) {
1443                                 if (verbose)
1444                                         pr_alert("%s" TORTURE_FLAG
1445                                                  "rcu_torture_onoff task: onlined %d\n",
1446                                                  torture_type, cpu);
1447                                 n_online_successes++;
1448                                 delta = jiffies - starttime;
1449                                 sum_online += delta;
1450                                 if (min_online < 0) {
1451                                         min_online = delta;
1452                                         max_online = delta;
1453                                 }
1454                                 if (min_online > delta)
1455                                         min_online = delta;
1456                                 if (max_online < delta)
1457                                         max_online = delta;
1458                         }
1459                 }
1460                 schedule_timeout_interruptible(onoff_interval * HZ);
1461         }
1462         VERBOSE_PRINTK_STRING("rcu_torture_onoff task stopping");
1463         return 0;
1464 }
1465
1466 static int
1467 rcu_torture_onoff_init(void)
1468 {
1469         int ret;
1470
1471         if (onoff_interval <= 0)
1472                 return 0;
1473         onoff_task = kthread_run(rcu_torture_onoff, NULL, "rcu_torture_onoff");
1474         if (IS_ERR(onoff_task)) {
1475                 ret = PTR_ERR(onoff_task);
1476                 onoff_task = NULL;
1477                 return ret;
1478         }
1479         return 0;
1480 }
1481
1482 static void rcu_torture_onoff_cleanup(void)
1483 {
1484         if (onoff_task == NULL)
1485                 return;
1486         VERBOSE_PRINTK_STRING("Stopping rcu_torture_onoff task");
1487         kthread_stop(onoff_task);
1488         onoff_task = NULL;
1489 }
1490
1491 #else /* #ifdef CONFIG_HOTPLUG_CPU */
1492
1493 static int
1494 rcu_torture_onoff_init(void)
1495 {
1496         return 0;
1497 }
1498
1499 static void rcu_torture_onoff_cleanup(void)
1500 {
1501 }
1502
1503 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
1504
1505 /*
1506  * CPU-stall kthread.  It waits as specified by stall_cpu_holdoff, then
1507  * induces a CPU stall for the time specified by stall_cpu.
1508  */
1509 static int rcu_torture_stall(void *args)
1510 {
1511         unsigned long stop_at;
1512
1513         VERBOSE_PRINTK_STRING("rcu_torture_stall task started");
1514         if (stall_cpu_holdoff > 0) {
1515                 VERBOSE_PRINTK_STRING("rcu_torture_stall begin holdoff");
1516                 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
1517                 VERBOSE_PRINTK_STRING("rcu_torture_stall end holdoff");
1518         }
1519         if (!kthread_should_stop()) {
1520                 stop_at = get_seconds() + stall_cpu;
1521                 /* RCU CPU stall is expected behavior in following code. */
1522                 pr_alert("rcu_torture_stall start.\n");
1523                 rcu_read_lock();
1524                 preempt_disable();
1525                 while (ULONG_CMP_LT(get_seconds(), stop_at))
1526                         continue;  /* Induce RCU CPU stall warning. */
1527                 preempt_enable();
1528                 rcu_read_unlock();
1529                 pr_alert("rcu_torture_stall end.\n");
1530         }
1531         rcutorture_shutdown_absorb("rcu_torture_stall");
1532         while (!kthread_should_stop())
1533                 schedule_timeout_interruptible(10 * HZ);
1534         return 0;
1535 }
1536
1537 /* Spawn CPU-stall kthread, if stall_cpu specified. */
1538 static int __init rcu_torture_stall_init(void)
1539 {
1540         int ret;
1541
1542         if (stall_cpu <= 0)
1543                 return 0;
1544         stall_task = kthread_run(rcu_torture_stall, NULL, "rcu_torture_stall");
1545         if (IS_ERR(stall_task)) {
1546                 ret = PTR_ERR(stall_task);
1547                 stall_task = NULL;
1548                 return ret;
1549         }
1550         return 0;
1551 }
1552
1553 /* Clean up after the CPU-stall kthread, if one was spawned. */
1554 static void rcu_torture_stall_cleanup(void)
1555 {
1556         if (stall_task == NULL)
1557                 return;
1558         VERBOSE_PRINTK_STRING("Stopping rcu_torture_stall_task.");
1559         kthread_stop(stall_task);
1560         stall_task = NULL;
1561 }
1562
1563 /* Callback function for RCU barrier testing. */
1564 void rcu_torture_barrier_cbf(struct rcu_head *rcu)
1565 {
1566         atomic_inc(&barrier_cbs_invoked);
1567 }
1568
1569 /* kthread function to register callbacks used to test RCU barriers. */
1570 static int rcu_torture_barrier_cbs(void *arg)
1571 {
1572         long myid = (long)arg;
1573         bool lastphase = 0;
1574         struct rcu_head rcu;
1575
1576         init_rcu_head_on_stack(&rcu);
1577         VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task started");
1578         set_user_nice(current, 19);
1579         do {
1580                 wait_event(barrier_cbs_wq[myid],
1581                            barrier_phase != lastphase ||
1582                            kthread_should_stop() ||
1583                            fullstop != FULLSTOP_DONTSTOP);
1584                 lastphase = barrier_phase;
1585                 smp_mb(); /* ensure barrier_phase load before ->call(). */
1586                 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1587                         break;
1588                 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1589                 if (atomic_dec_and_test(&barrier_cbs_count))
1590                         wake_up(&barrier_wq);
1591         } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1592         VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task stopping");
1593         rcutorture_shutdown_absorb("rcu_torture_barrier_cbs");
1594         while (!kthread_should_stop())
1595                 schedule_timeout_interruptible(1);
1596         cur_ops->cb_barrier();
1597         destroy_rcu_head_on_stack(&rcu);
1598         return 0;
1599 }
1600
1601 /* kthread function to drive and coordinate RCU barrier testing. */
1602 static int rcu_torture_barrier(void *arg)
1603 {
1604         int i;
1605
1606         VERBOSE_PRINTK_STRING("rcu_torture_barrier task starting");
1607         do {
1608                 atomic_set(&barrier_cbs_invoked, 0);
1609                 atomic_set(&barrier_cbs_count, n_barrier_cbs);
1610                 smp_mb(); /* Ensure barrier_phase after prior assignments. */
1611                 barrier_phase = !barrier_phase;
1612                 for (i = 0; i < n_barrier_cbs; i++)
1613                         wake_up(&barrier_cbs_wq[i]);
1614                 wait_event(barrier_wq,
1615                            atomic_read(&barrier_cbs_count) == 0 ||
1616                            kthread_should_stop() ||
1617                            fullstop != FULLSTOP_DONTSTOP);
1618                 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1619                         break;
1620                 n_barrier_attempts++;
1621                 cur_ops->cb_barrier();
1622                 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1623                         n_rcu_torture_barrier_error++;
1624                         WARN_ON_ONCE(1);
1625                 }
1626                 n_barrier_successes++;
1627                 schedule_timeout_interruptible(HZ / 10);
1628         } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1629         VERBOSE_PRINTK_STRING("rcu_torture_barrier task stopping");
1630         rcutorture_shutdown_absorb("rcu_torture_barrier");
1631         while (!kthread_should_stop())
1632                 schedule_timeout_interruptible(1);
1633         return 0;
1634 }
1635
1636 /* Initialize RCU barrier testing. */
1637 static int rcu_torture_barrier_init(void)
1638 {
1639         int i;
1640         int ret;
1641
1642         if (n_barrier_cbs == 0)
1643                 return 0;
1644         if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
1645                 pr_alert("%s" TORTURE_FLAG
1646                          " Call or barrier ops missing for %s,\n",
1647                          torture_type, cur_ops->name);
1648                 pr_alert("%s" TORTURE_FLAG
1649                          " RCU barrier testing omitted from run.\n",
1650                          torture_type);
1651                 return 0;
1652         }
1653         atomic_set(&barrier_cbs_count, 0);
1654         atomic_set(&barrier_cbs_invoked, 0);
1655         barrier_cbs_tasks =
1656                 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
1657                         GFP_KERNEL);
1658         barrier_cbs_wq =
1659                 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
1660                         GFP_KERNEL);
1661         if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
1662                 return -ENOMEM;
1663         for (i = 0; i < n_barrier_cbs; i++) {
1664                 init_waitqueue_head(&barrier_cbs_wq[i]);
1665                 barrier_cbs_tasks[i] = kthread_run(rcu_torture_barrier_cbs,
1666                                                    (void *)(long)i,
1667                                                    "rcu_torture_barrier_cbs");
1668                 if (IS_ERR(barrier_cbs_tasks[i])) {
1669                         ret = PTR_ERR(barrier_cbs_tasks[i]);
1670                         VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier_cbs");
1671                         barrier_cbs_tasks[i] = NULL;
1672                         return ret;
1673                 }
1674         }
1675         barrier_task = kthread_run(rcu_torture_barrier, NULL,
1676                                    "rcu_torture_barrier");
1677         if (IS_ERR(barrier_task)) {
1678                 ret = PTR_ERR(barrier_task);
1679                 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier");
1680                 barrier_task = NULL;
1681         }
1682         return 0;
1683 }
1684
1685 /* Clean up after RCU barrier testing. */
1686 static void rcu_torture_barrier_cleanup(void)
1687 {
1688         int i;
1689
1690         if (barrier_task != NULL) {
1691                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier task");
1692                 kthread_stop(barrier_task);
1693                 barrier_task = NULL;
1694         }
1695         if (barrier_cbs_tasks != NULL) {
1696                 for (i = 0; i < n_barrier_cbs; i++) {
1697                         if (barrier_cbs_tasks[i] != NULL) {
1698                                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier_cbs task");
1699                                 kthread_stop(barrier_cbs_tasks[i]);
1700                                 barrier_cbs_tasks[i] = NULL;
1701                         }
1702                 }
1703                 kfree(barrier_cbs_tasks);
1704                 barrier_cbs_tasks = NULL;
1705         }
1706         if (barrier_cbs_wq != NULL) {
1707                 kfree(barrier_cbs_wq);
1708                 barrier_cbs_wq = NULL;
1709         }
1710 }
1711
1712 static int rcutorture_cpu_notify(struct notifier_block *self,
1713                                  unsigned long action, void *hcpu)
1714 {
1715         long cpu = (long)hcpu;
1716
1717         switch (action) {
1718         case CPU_ONLINE:
1719         case CPU_DOWN_FAILED:
1720                 (void)rcutorture_booster_init(cpu);
1721                 break;
1722         case CPU_DOWN_PREPARE:
1723                 rcutorture_booster_cleanup(cpu);
1724                 break;
1725         default:
1726                 break;
1727         }
1728         return NOTIFY_OK;
1729 }
1730
1731 static struct notifier_block rcutorture_cpu_nb = {
1732         .notifier_call = rcutorture_cpu_notify,
1733 };
1734
1735 static void
1736 rcu_torture_cleanup(void)
1737 {
1738         int i;
1739
1740         mutex_lock(&fullstop_mutex);
1741         rcutorture_record_test_transition();
1742         if (fullstop == FULLSTOP_SHUTDOWN) {
1743                 pr_warn(/* but going down anyway, so... */
1744                        "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
1745                 mutex_unlock(&fullstop_mutex);
1746                 schedule_timeout_uninterruptible(10);
1747                 if (cur_ops->cb_barrier != NULL)
1748                         cur_ops->cb_barrier();
1749                 return;
1750         }
1751         fullstop = FULLSTOP_RMMOD;
1752         mutex_unlock(&fullstop_mutex);
1753         unregister_reboot_notifier(&rcutorture_shutdown_nb);
1754         rcu_torture_barrier_cleanup();
1755         rcu_torture_stall_cleanup();
1756         if (stutter_task) {
1757                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1758                 kthread_stop(stutter_task);
1759         }
1760         stutter_task = NULL;
1761         if (shuffler_task) {
1762                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1763                 kthread_stop(shuffler_task);
1764                 free_cpumask_var(shuffle_tmp_mask);
1765         }
1766         shuffler_task = NULL;
1767
1768         if (writer_task) {
1769                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1770                 kthread_stop(writer_task);
1771         }
1772         writer_task = NULL;
1773
1774         if (reader_tasks) {
1775                 for (i = 0; i < nrealreaders; i++) {
1776                         if (reader_tasks[i]) {
1777                                 VERBOSE_PRINTK_STRING(
1778                                         "Stopping rcu_torture_reader task");
1779                                 kthread_stop(reader_tasks[i]);
1780                         }
1781                         reader_tasks[i] = NULL;
1782                 }
1783                 kfree(reader_tasks);
1784                 reader_tasks = NULL;
1785         }
1786         rcu_torture_current = NULL;
1787
1788         if (fakewriter_tasks) {
1789                 for (i = 0; i < nfakewriters; i++) {
1790                         if (fakewriter_tasks[i]) {
1791                                 VERBOSE_PRINTK_STRING(
1792                                         "Stopping rcu_torture_fakewriter task");
1793                                 kthread_stop(fakewriter_tasks[i]);
1794                         }
1795                         fakewriter_tasks[i] = NULL;
1796                 }
1797                 kfree(fakewriter_tasks);
1798                 fakewriter_tasks = NULL;
1799         }
1800
1801         if (stats_task) {
1802                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1803                 kthread_stop(stats_task);
1804         }
1805         stats_task = NULL;
1806
1807         if (fqs_task) {
1808                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1809                 kthread_stop(fqs_task);
1810         }
1811         fqs_task = NULL;
1812         if ((test_boost == 1 && cur_ops->can_boost) ||
1813             test_boost == 2) {
1814                 unregister_cpu_notifier(&rcutorture_cpu_nb);
1815                 for_each_possible_cpu(i)
1816                         rcutorture_booster_cleanup(i);
1817         }
1818         if (shutdown_task != NULL) {
1819                 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
1820                 kthread_stop(shutdown_task);
1821         }
1822         shutdown_task = NULL;
1823         rcu_torture_onoff_cleanup();
1824
1825         /* Wait for all RCU callbacks to fire.  */
1826
1827         if (cur_ops->cb_barrier != NULL)
1828                 cur_ops->cb_barrier();
1829
1830         rcu_torture_stats_print();  /* -After- the stats thread is stopped! */
1831
1832         if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
1833                 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
1834         else if (n_online_successes != n_online_attempts ||
1835                  n_offline_successes != n_offline_attempts)
1836                 rcu_torture_print_module_parms(cur_ops,
1837                                                "End of test: RCU_HOTPLUG");
1838         else
1839                 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
1840 }
1841
1842 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1843 static void rcu_torture_leak_cb(struct rcu_head *rhp)
1844 {
1845 }
1846
1847 static void rcu_torture_err_cb(struct rcu_head *rhp)
1848 {
1849         /*
1850          * This -might- happen due to race conditions, but is unlikely.
1851          * The scenario that leads to this happening is that the
1852          * first of the pair of duplicate callbacks is queued,
1853          * someone else starts a grace period that includes that
1854          * callback, then the second of the pair must wait for the
1855          * next grace period.  Unlikely, but can happen.  If it
1856          * does happen, the debug-objects subsystem won't have splatted.
1857          */
1858         pr_alert("rcutorture: duplicated callback was invoked.\n");
1859 }
1860 #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1861
1862 /*
1863  * Verify that double-free causes debug-objects to complain, but only
1864  * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y.  Otherwise, say that the test
1865  * cannot be carried out.
1866  */
1867 static void rcu_test_debug_objects(void)
1868 {
1869 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1870         struct rcu_head rh1;
1871         struct rcu_head rh2;
1872
1873         init_rcu_head_on_stack(&rh1);
1874         init_rcu_head_on_stack(&rh2);
1875         pr_alert("rcutorture: WARN: Duplicate call_rcu() test starting.\n");
1876
1877         /* Try to queue the rh2 pair of callbacks for the same grace period. */
1878         preempt_disable(); /* Prevent preemption from interrupting test. */
1879         rcu_read_lock(); /* Make it impossible to finish a grace period. */
1880         call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
1881         local_irq_disable(); /* Make it harder to start a new grace period. */
1882         call_rcu(&rh2, rcu_torture_leak_cb);
1883         call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
1884         local_irq_enable();
1885         rcu_read_unlock();
1886         preempt_enable();
1887
1888         /* Wait for them all to get done so we can safely return. */
1889         rcu_barrier();
1890         pr_alert("rcutorture: WARN: Duplicate call_rcu() test complete.\n");
1891         destroy_rcu_head_on_stack(&rh1);
1892         destroy_rcu_head_on_stack(&rh2);
1893 #else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1894         pr_alert("rcutorture: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n");
1895 #endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1896 }
1897
1898 static int __init
1899 rcu_torture_init(void)
1900 {
1901         int i;
1902         int cpu;
1903         int firsterr = 0;
1904         int retval;
1905         static struct rcu_torture_ops *torture_ops[] = {
1906                 &rcu_ops, &rcu_bh_ops, &srcu_ops, &sched_ops,
1907         };
1908
1909         mutex_lock(&fullstop_mutex);
1910
1911         /* Process args and tell the world that the torturer is on the job. */
1912         for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1913                 cur_ops = torture_ops[i];
1914                 if (strcmp(torture_type, cur_ops->name) == 0)
1915                         break;
1916         }
1917         if (i == ARRAY_SIZE(torture_ops)) {
1918                 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
1919                          torture_type);
1920                 pr_alert("rcu-torture types:");
1921                 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
1922                         pr_alert(" %s", torture_ops[i]->name);
1923                 pr_alert("\n");
1924                 mutex_unlock(&fullstop_mutex);
1925                 return -EINVAL;
1926         }
1927         if (cur_ops->fqs == NULL && fqs_duration != 0) {
1928                 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
1929                 fqs_duration = 0;
1930         }
1931         if (cur_ops->init)
1932                 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1933
1934         if (nreaders >= 0)
1935                 nrealreaders = nreaders;
1936         else
1937                 nrealreaders = 2 * num_online_cpus();
1938         rcu_torture_print_module_parms(cur_ops, "Start of test");
1939         fullstop = FULLSTOP_DONTSTOP;
1940
1941         /* Set up the freelist. */
1942
1943         INIT_LIST_HEAD(&rcu_torture_freelist);
1944         for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1945                 rcu_tortures[i].rtort_mbtest = 0;
1946                 list_add_tail(&rcu_tortures[i].rtort_free,
1947                               &rcu_torture_freelist);
1948         }
1949
1950         /* Initialize the statistics so that each run gets its own numbers. */
1951
1952         rcu_torture_current = NULL;
1953         rcu_torture_current_version = 0;
1954         atomic_set(&n_rcu_torture_alloc, 0);
1955         atomic_set(&n_rcu_torture_alloc_fail, 0);
1956         atomic_set(&n_rcu_torture_free, 0);
1957         atomic_set(&n_rcu_torture_mberror, 0);
1958         atomic_set(&n_rcu_torture_error, 0);
1959         n_rcu_torture_barrier_error = 0;
1960         n_rcu_torture_boost_ktrerror = 0;
1961         n_rcu_torture_boost_rterror = 0;
1962         n_rcu_torture_boost_failure = 0;
1963         n_rcu_torture_boosts = 0;
1964         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1965                 atomic_set(&rcu_torture_wcount[i], 0);
1966         for_each_possible_cpu(cpu) {
1967                 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1968                         per_cpu(rcu_torture_count, cpu)[i] = 0;
1969                         per_cpu(rcu_torture_batch, cpu)[i] = 0;
1970                 }
1971         }
1972
1973         /* Start up the kthreads. */
1974
1975         VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1976         writer_task = kthread_create(rcu_torture_writer, NULL,
1977                                      "rcu_torture_writer");
1978         if (IS_ERR(writer_task)) {
1979                 firsterr = PTR_ERR(writer_task);
1980                 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1981                 writer_task = NULL;
1982                 goto unwind;
1983         }
1984         wake_up_process(writer_task);
1985         fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1986                                    GFP_KERNEL);
1987         if (fakewriter_tasks == NULL) {
1988                 VERBOSE_PRINTK_ERRSTRING("out of memory");
1989                 firsterr = -ENOMEM;
1990                 goto unwind;
1991         }
1992         for (i = 0; i < nfakewriters; i++) {
1993                 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1994                 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1995                                                   "rcu_torture_fakewriter");
1996                 if (IS_ERR(fakewriter_tasks[i])) {
1997                         firsterr = PTR_ERR(fakewriter_tasks[i]);
1998                         VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1999                         fakewriter_tasks[i] = NULL;
2000                         goto unwind;
2001                 }
2002         }
2003         reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
2004                                GFP_KERNEL);
2005         if (reader_tasks == NULL) {
2006                 VERBOSE_PRINTK_ERRSTRING("out of memory");
2007                 firsterr = -ENOMEM;
2008                 goto unwind;
2009         }
2010         for (i = 0; i < nrealreaders; i++) {
2011                 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
2012                 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
2013                                               "rcu_torture_reader");
2014                 if (IS_ERR(reader_tasks[i])) {
2015                         firsterr = PTR_ERR(reader_tasks[i]);
2016                         VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
2017                         reader_tasks[i] = NULL;
2018                         goto unwind;
2019                 }
2020         }
2021         if (stat_interval > 0) {
2022                 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
2023                 stats_task = kthread_run(rcu_torture_stats, NULL,
2024                                         "rcu_torture_stats");
2025                 if (IS_ERR(stats_task)) {
2026                         firsterr = PTR_ERR(stats_task);
2027                         VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
2028                         stats_task = NULL;
2029                         goto unwind;
2030                 }
2031         }
2032         if (test_no_idle_hz) {
2033                 rcu_idle_cpu = num_online_cpus() - 1;
2034
2035                 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
2036                         firsterr = -ENOMEM;
2037                         VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
2038                         goto unwind;
2039                 }
2040
2041                 /* Create the shuffler thread */
2042                 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
2043                                           "rcu_torture_shuffle");
2044                 if (IS_ERR(shuffler_task)) {
2045                         free_cpumask_var(shuffle_tmp_mask);
2046                         firsterr = PTR_ERR(shuffler_task);
2047                         VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
2048                         shuffler_task = NULL;
2049                         goto unwind;
2050                 }
2051         }
2052         if (stutter < 0)
2053                 stutter = 0;
2054         if (stutter) {
2055                 /* Create the stutter thread */
2056                 stutter_task = kthread_run(rcu_torture_stutter, NULL,
2057                                           "rcu_torture_stutter");
2058                 if (IS_ERR(stutter_task)) {
2059                         firsterr = PTR_ERR(stutter_task);
2060                         VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
2061                         stutter_task = NULL;
2062                         goto unwind;
2063                 }
2064         }
2065         if (fqs_duration < 0)
2066                 fqs_duration = 0;
2067         if (fqs_duration) {
2068                 /* Create the stutter thread */
2069                 fqs_task = kthread_run(rcu_torture_fqs, NULL,
2070                                        "rcu_torture_fqs");
2071                 if (IS_ERR(fqs_task)) {
2072                         firsterr = PTR_ERR(fqs_task);
2073                         VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
2074                         fqs_task = NULL;
2075                         goto unwind;
2076                 }
2077         }
2078         if (test_boost_interval < 1)
2079                 test_boost_interval = 1;
2080         if (test_boost_duration < 2)
2081                 test_boost_duration = 2;
2082         if ((test_boost == 1 && cur_ops->can_boost) ||
2083             test_boost == 2) {
2084
2085                 boost_starttime = jiffies + test_boost_interval * HZ;
2086                 register_cpu_notifier(&rcutorture_cpu_nb);
2087                 for_each_possible_cpu(i) {
2088                         if (cpu_is_offline(i))
2089                                 continue;  /* Heuristic: CPU can go offline. */
2090                         retval = rcutorture_booster_init(i);
2091                         if (retval < 0) {
2092                                 firsterr = retval;
2093                                 goto unwind;
2094                         }
2095                 }
2096         }
2097         if (shutdown_secs > 0) {
2098                 shutdown_time = jiffies + shutdown_secs * HZ;
2099                 shutdown_task = kthread_create(rcu_torture_shutdown, NULL,
2100                                                "rcu_torture_shutdown");
2101                 if (IS_ERR(shutdown_task)) {
2102                         firsterr = PTR_ERR(shutdown_task);
2103                         VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
2104                         shutdown_task = NULL;
2105                         goto unwind;
2106                 }
2107                 wake_up_process(shutdown_task);
2108         }
2109         i = rcu_torture_onoff_init();
2110         if (i != 0) {
2111                 firsterr = i;
2112                 goto unwind;
2113         }
2114         register_reboot_notifier(&rcutorture_shutdown_nb);
2115         i = rcu_torture_stall_init();
2116         if (i != 0) {
2117                 firsterr = i;
2118                 goto unwind;
2119         }
2120         retval = rcu_torture_barrier_init();
2121         if (retval != 0) {
2122                 firsterr = retval;
2123                 goto unwind;
2124         }
2125         if (object_debug)
2126                 rcu_test_debug_objects();
2127         rcutorture_record_test_transition();
2128         mutex_unlock(&fullstop_mutex);
2129         return 0;
2130
2131 unwind:
2132         mutex_unlock(&fullstop_mutex);
2133         rcu_torture_cleanup();
2134         return firsterr;
2135 }
2136
2137 module_init(rcu_torture_init);
2138 module_exit(rcu_torture_cleanup);