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