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