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