ARM: Merge for-2635/samsung-hwmon
[linux-2.6-block.git] / kernel / rcutorture.c
CommitLineData
a241ec65 1/*
29766f1e 2 * Read-Copy Update module-based torture test facility
a241ec65
PM
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 *
b772e1dd 18 * Copyright (C) IBM Corporation, 2005, 2006
a241ec65
PM
19 *
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
a71fca58 21 * Josh Triplett <josh@freedesktop.org>
a241ec65
PM
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 <asm/atomic.h>
37#include <linux/bitops.h>
a241ec65
PM
38#include <linux/completion.h>
39#include <linux/moduleparam.h>
40#include <linux/percpu.h>
41#include <linux/notifier.h>
343e9099 42#include <linux/reboot.h>
83144186 43#include <linux/freezer.h>
a241ec65 44#include <linux/cpu.h>
a241ec65 45#include <linux/delay.h>
a241ec65 46#include <linux/stat.h>
b2896d2e 47#include <linux/srcu.h>
1aeb272c 48#include <linux/slab.h>
f07767fd 49#include <asm/byteorder.h>
a241ec65
PM
50
51MODULE_LICENSE("GPL");
b772e1dd 52MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
a71fca58 53 "Josh Triplett <josh@freedesktop.org>");
a241ec65 54
4802211c 55static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
b772e1dd 56static int nfakewriters = 4; /* # fake writer threads */
d84f5203 57static int stat_interval; /* Interval between stats, in seconds. */
a241ec65 58 /* Defaults to "only at end of test". */
d84f5203
SV
59static int verbose; /* Print more debug info. */
60static int test_no_idle_hz; /* Test RCU's support for tickless idle CPUs. */
d120f65f
PM
61static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
62static int stutter = 5; /* Start/stop testing interval (in sec) */
0729fbf3 63static int irqreader = 1; /* RCU readers from irq (timers). */
bf66f18e
PM
64static int fqs_duration = 0; /* Duration of bursts (us), 0 to disable. */
65static int fqs_holdoff = 0; /* Hold time within burst (us). */
66static int fqs_stutter = 3; /* Wait time between bursts (s). */
20d2e428 67static char *torture_type = "rcu"; /* What RCU implementation to torture. */
a241ec65 68
d6ad6711 69module_param(nreaders, int, 0444);
a241ec65 70MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
d6ad6711 71module_param(nfakewriters, int, 0444);
b772e1dd 72MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
d6ad6711 73module_param(stat_interval, int, 0444);
a241ec65 74MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
d6ad6711 75module_param(verbose, bool, 0444);
a241ec65 76MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
d6ad6711 77module_param(test_no_idle_hz, bool, 0444);
d84f5203 78MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
d6ad6711 79module_param(shuffle_interval, int, 0444);
d84f5203 80MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
d120f65f
PM
81module_param(stutter, int, 0444);
82MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
0729fbf3
PM
83module_param(irqreader, int, 0444);
84MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
bf66f18e
PM
85module_param(fqs_duration, int, 0444);
86MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
87module_param(fqs_holdoff, int, 0444);
88MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
89module_param(fqs_stutter, int, 0444);
90MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
d6ad6711 91module_param(torture_type, charp, 0444);
b2896d2e 92MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
72e9bb54
PM
93
94#define TORTURE_FLAG "-torture:"
a241ec65 95#define PRINTK_STRING(s) \
72e9bb54 96 do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
a241ec65 97#define VERBOSE_PRINTK_STRING(s) \
72e9bb54 98 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
a241ec65 99#define VERBOSE_PRINTK_ERRSTRING(s) \
72e9bb54 100 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
a241ec65
PM
101
102static char printk_buf[4096];
103
104static int nrealreaders;
105static struct task_struct *writer_task;
b772e1dd 106static struct task_struct **fakewriter_tasks;
a241ec65
PM
107static struct task_struct **reader_tasks;
108static struct task_struct *stats_task;
d84f5203 109static struct task_struct *shuffler_task;
d120f65f 110static struct task_struct *stutter_task;
bf66f18e 111static struct task_struct *fqs_task;
a241ec65
PM
112
113#define RCU_TORTURE_PIPE_LEN 10
114
115struct rcu_torture {
116 struct rcu_head rtort_rcu;
117 int rtort_pipe_count;
118 struct list_head rtort_free;
996417d2 119 int rtort_mbtest;
a241ec65
PM
120};
121
a241ec65 122static LIST_HEAD(rcu_torture_freelist);
a71fca58
PM
123static struct rcu_torture *rcu_torture_current;
124static long rcu_torture_current_version;
a241ec65
PM
125static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
126static DEFINE_SPINLOCK(rcu_torture_lock);
127static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
128 { 0 };
129static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
130 { 0 };
131static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
b2896d2e
PM
132static atomic_t n_rcu_torture_alloc;
133static atomic_t n_rcu_torture_alloc_fail;
134static atomic_t n_rcu_torture_free;
135static atomic_t n_rcu_torture_mberror;
136static atomic_t n_rcu_torture_error;
a71fca58 137static long n_rcu_torture_timers;
e3033736 138static struct list_head rcu_torture_removed;
73d0a4b1 139static cpumask_var_t shuffle_tmp_mask;
a241ec65 140
a71fca58 141static int stutter_pause_test;
d120f65f 142
31a72bce
PM
143#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
144#define RCUTORTURE_RUNNABLE_INIT 1
145#else
146#define RCUTORTURE_RUNNABLE_INIT 0
147#endif
148int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
149
c9d557c1
PM
150/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
151
152#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
153#define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
154#define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
155static int fullstop = FULLSTOP_RMMOD;
156DEFINE_MUTEX(fullstop_mutex); /* Protect fullstop transitions and spawning */
157 /* of kthreads. */
343e9099
PM
158
159/*
c9d557c1 160 * Detect and respond to a system shutdown.
343e9099
PM
161 */
162static int
163rcutorture_shutdown_notify(struct notifier_block *unused1,
164 unsigned long unused2, void *unused3)
165{
c59ab97e 166 mutex_lock(&fullstop_mutex);
c9d557c1 167 if (fullstop == FULLSTOP_DONTSTOP)
c59ab97e 168 fullstop = FULLSTOP_SHUTDOWN;
c9d557c1
PM
169 else
170 printk(KERN_WARNING /* but going down anyway, so... */
171 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
c59ab97e 172 mutex_unlock(&fullstop_mutex);
343e9099
PM
173 return NOTIFY_DONE;
174}
175
c9d557c1
PM
176/*
177 * Absorb kthreads into a kernel function that won't return, so that
178 * they won't ever access module text or data again.
179 */
180static void rcutorture_shutdown_absorb(char *title)
181{
182 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
183 printk(KERN_NOTICE
184 "rcutorture thread %s parking due to system shutdown\n",
185 title);
186 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
187 }
188}
189
a241ec65
PM
190/*
191 * Allocate an element from the rcu_tortures pool.
192 */
97a41e26 193static struct rcu_torture *
a241ec65
PM
194rcu_torture_alloc(void)
195{
196 struct list_head *p;
197
adac1665 198 spin_lock_bh(&rcu_torture_lock);
a241ec65
PM
199 if (list_empty(&rcu_torture_freelist)) {
200 atomic_inc(&n_rcu_torture_alloc_fail);
adac1665 201 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
202 return NULL;
203 }
204 atomic_inc(&n_rcu_torture_alloc);
205 p = rcu_torture_freelist.next;
206 list_del_init(p);
adac1665 207 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
208 return container_of(p, struct rcu_torture, rtort_free);
209}
210
211/*
212 * Free an element to the rcu_tortures pool.
213 */
214static void
215rcu_torture_free(struct rcu_torture *p)
216{
217 atomic_inc(&n_rcu_torture_free);
adac1665 218 spin_lock_bh(&rcu_torture_lock);
a241ec65 219 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
adac1665 220 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
221}
222
a241ec65
PM
223struct rcu_random_state {
224 unsigned long rrs_state;
75cfef32 225 long rrs_count;
a241ec65
PM
226};
227
228#define RCU_RANDOM_MULT 39916801 /* prime */
229#define RCU_RANDOM_ADD 479001701 /* prime */
230#define RCU_RANDOM_REFRESH 10000
231
232#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
233
234/*
235 * Crude but fast random-number generator. Uses a linear congruential
c17ac855 236 * generator, with occasional help from cpu_clock().
a241ec65 237 */
75cfef32 238static unsigned long
a241ec65
PM
239rcu_random(struct rcu_random_state *rrsp)
240{
a241ec65 241 if (--rrsp->rrs_count < 0) {
c17ac855
PM
242 rrsp->rrs_state +=
243 (unsigned long)cpu_clock(raw_smp_processor_id());
a241ec65
PM
244 rrsp->rrs_count = RCU_RANDOM_REFRESH;
245 }
246 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
247 return swahw32(rrsp->rrs_state);
248}
249
d120f65f 250static void
c9d557c1 251rcu_stutter_wait(char *title)
d120f65f 252{
c9d557c1 253 while (stutter_pause_test || !rcutorture_runnable) {
e3d7be27
PM
254 if (rcutorture_runnable)
255 schedule_timeout_interruptible(1);
256 else
3ccf79f4 257 schedule_timeout_interruptible(round_jiffies_relative(HZ));
c9d557c1 258 rcutorture_shutdown_absorb(title);
343e9099 259 }
d120f65f
PM
260}
261
72e9bb54
PM
262/*
263 * Operations vector for selecting different types of tests.
264 */
265
266struct rcu_torture_ops {
267 void (*init)(void);
268 void (*cleanup)(void);
269 int (*readlock)(void);
0acc512c 270 void (*read_delay)(struct rcu_random_state *rrsp);
72e9bb54
PM
271 void (*readunlock)(int idx);
272 int (*completed)(void);
0acc512c 273 void (*deferred_free)(struct rcu_torture *p);
b772e1dd 274 void (*sync)(void);
2326974d 275 void (*cb_barrier)(void);
bf66f18e 276 void (*fqs)(void);
72e9bb54 277 int (*stats)(char *page);
0acc512c 278 int irq_capable;
72e9bb54
PM
279 char *name;
280};
a71fca58
PM
281
282static struct rcu_torture_ops *cur_ops;
72e9bb54
PM
283
284/*
285 * Definitions for rcu torture testing.
286 */
287
a49a4af7 288static int rcu_torture_read_lock(void) __acquires(RCU)
72e9bb54
PM
289{
290 rcu_read_lock();
291 return 0;
292}
293
b2896d2e
PM
294static void rcu_read_delay(struct rcu_random_state *rrsp)
295{
b8d57a76
JT
296 const unsigned long shortdelay_us = 200;
297 const unsigned long longdelay_ms = 50;
b2896d2e 298
b8d57a76
JT
299 /* We want a short delay sometimes to make a reader delay the grace
300 * period, and we want a long delay occasionally to trigger
301 * force_quiescent_state. */
b2896d2e 302
b8d57a76
JT
303 if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
304 mdelay(longdelay_ms);
305 if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
306 udelay(shortdelay_us);
b2896d2e
PM
307}
308
a49a4af7 309static void rcu_torture_read_unlock(int idx) __releases(RCU)
72e9bb54
PM
310{
311 rcu_read_unlock();
312}
313
314static int rcu_torture_completed(void)
315{
316 return rcu_batches_completed();
317}
318
319static void
320rcu_torture_cb(struct rcu_head *p)
321{
322 int i;
323 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
324
c9d557c1 325 if (fullstop != FULLSTOP_DONTSTOP) {
72e9bb54
PM
326 /* Test is ending, just drop callbacks on the floor. */
327 /* The next initialization will pick up the pieces. */
328 return;
329 }
330 i = rp->rtort_pipe_count;
331 if (i > RCU_TORTURE_PIPE_LEN)
332 i = RCU_TORTURE_PIPE_LEN;
333 atomic_inc(&rcu_torture_wcount[i]);
334 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
335 rp->rtort_mbtest = 0;
336 rcu_torture_free(rp);
337 } else
0acc512c 338 cur_ops->deferred_free(rp);
72e9bb54
PM
339}
340
d9a3da06
PM
341static int rcu_no_completed(void)
342{
343 return 0;
344}
345
72e9bb54
PM
346static void rcu_torture_deferred_free(struct rcu_torture *p)
347{
348 call_rcu(&p->rtort_rcu, rcu_torture_cb);
349}
350
351static struct rcu_torture_ops rcu_ops = {
0acc512c
PM
352 .init = NULL,
353 .cleanup = NULL,
354 .readlock = rcu_torture_read_lock,
355 .read_delay = rcu_read_delay,
356 .readunlock = rcu_torture_read_unlock,
357 .completed = rcu_torture_completed,
358 .deferred_free = rcu_torture_deferred_free,
359 .sync = synchronize_rcu,
360 .cb_barrier = rcu_barrier,
bf66f18e 361 .fqs = rcu_force_quiescent_state,
0acc512c 362 .stats = NULL,
a71fca58
PM
363 .irq_capable = 1,
364 .name = "rcu"
72e9bb54
PM
365};
366
e3033736
JT
367static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
368{
369 int i;
370 struct rcu_torture *rp;
371 struct rcu_torture *rp1;
372
373 cur_ops->sync();
374 list_add(&p->rtort_free, &rcu_torture_removed);
375 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
376 i = rp->rtort_pipe_count;
377 if (i > RCU_TORTURE_PIPE_LEN)
378 i = RCU_TORTURE_PIPE_LEN;
379 atomic_inc(&rcu_torture_wcount[i]);
380 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
381 rp->rtort_mbtest = 0;
382 list_del(&rp->rtort_free);
383 rcu_torture_free(rp);
384 }
385 }
386}
387
388static void rcu_sync_torture_init(void)
389{
390 INIT_LIST_HEAD(&rcu_torture_removed);
391}
392
20d2e428 393static struct rcu_torture_ops rcu_sync_ops = {
0acc512c
PM
394 .init = rcu_sync_torture_init,
395 .cleanup = NULL,
396 .readlock = rcu_torture_read_lock,
397 .read_delay = rcu_read_delay,
398 .readunlock = rcu_torture_read_unlock,
399 .completed = rcu_torture_completed,
400 .deferred_free = rcu_sync_torture_deferred_free,
401 .sync = synchronize_rcu,
402 .cb_barrier = NULL,
bf66f18e 403 .fqs = rcu_force_quiescent_state,
0acc512c
PM
404 .stats = NULL,
405 .irq_capable = 1,
406 .name = "rcu_sync"
20d2e428
JT
407};
408
d9a3da06
PM
409static struct rcu_torture_ops rcu_expedited_ops = {
410 .init = rcu_sync_torture_init,
411 .cleanup = NULL,
412 .readlock = rcu_torture_read_lock,
413 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
414 .readunlock = rcu_torture_read_unlock,
415 .completed = rcu_no_completed,
416 .deferred_free = rcu_sync_torture_deferred_free,
417 .sync = synchronize_rcu_expedited,
418 .cb_barrier = NULL,
bf66f18e 419 .fqs = rcu_force_quiescent_state,
d9a3da06
PM
420 .stats = NULL,
421 .irq_capable = 1,
422 .name = "rcu_expedited"
423};
424
c32e0660
PM
425/*
426 * Definitions for rcu_bh torture testing.
427 */
428
a49a4af7 429static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
c32e0660
PM
430{
431 rcu_read_lock_bh();
432 return 0;
433}
434
a49a4af7 435static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
c32e0660
PM
436{
437 rcu_read_unlock_bh();
438}
439
440static int rcu_bh_torture_completed(void)
441{
442 return rcu_batches_completed_bh();
443}
444
445static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
446{
447 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
448}
449
b772e1dd
JT
450struct rcu_bh_torture_synchronize {
451 struct rcu_head head;
452 struct completion completion;
453};
454
455static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
456{
457 struct rcu_bh_torture_synchronize *rcu;
458
459 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
460 complete(&rcu->completion);
461}
462
463static void rcu_bh_torture_synchronize(void)
464{
465 struct rcu_bh_torture_synchronize rcu;
466
467 init_completion(&rcu.completion);
468 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
469 wait_for_completion(&rcu.completion);
470}
471
c32e0660 472static struct rcu_torture_ops rcu_bh_ops = {
0acc512c
PM
473 .init = NULL,
474 .cleanup = NULL,
475 .readlock = rcu_bh_torture_read_lock,
476 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
477 .readunlock = rcu_bh_torture_read_unlock,
478 .completed = rcu_bh_torture_completed,
479 .deferred_free = rcu_bh_torture_deferred_free,
480 .sync = rcu_bh_torture_synchronize,
481 .cb_barrier = rcu_barrier_bh,
bf66f18e 482 .fqs = rcu_bh_force_quiescent_state,
0acc512c
PM
483 .stats = NULL,
484 .irq_capable = 1,
485 .name = "rcu_bh"
c32e0660
PM
486};
487
11a14701 488static struct rcu_torture_ops rcu_bh_sync_ops = {
0acc512c
PM
489 .init = rcu_sync_torture_init,
490 .cleanup = NULL,
491 .readlock = rcu_bh_torture_read_lock,
492 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
493 .readunlock = rcu_bh_torture_read_unlock,
494 .completed = rcu_bh_torture_completed,
495 .deferred_free = rcu_sync_torture_deferred_free,
496 .sync = rcu_bh_torture_synchronize,
497 .cb_barrier = NULL,
bf66f18e 498 .fqs = rcu_bh_force_quiescent_state,
0acc512c
PM
499 .stats = NULL,
500 .irq_capable = 1,
501 .name = "rcu_bh_sync"
11a14701
JT
502};
503
b2896d2e
PM
504/*
505 * Definitions for srcu torture testing.
506 */
507
508static struct srcu_struct srcu_ctl;
b2896d2e
PM
509
510static void srcu_torture_init(void)
511{
512 init_srcu_struct(&srcu_ctl);
e3033736 513 rcu_sync_torture_init();
b2896d2e
PM
514}
515
516static void srcu_torture_cleanup(void)
517{
518 synchronize_srcu(&srcu_ctl);
519 cleanup_srcu_struct(&srcu_ctl);
520}
521
012d3ca8 522static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
b2896d2e
PM
523{
524 return srcu_read_lock(&srcu_ctl);
525}
526
527static void srcu_read_delay(struct rcu_random_state *rrsp)
528{
529 long delay;
530 const long uspertick = 1000000 / HZ;
531 const long longdelay = 10;
532
533 /* We want there to be long-running readers, but not all the time. */
534
535 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
536 if (!delay)
537 schedule_timeout_interruptible(longdelay);
538}
539
012d3ca8 540static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
b2896d2e
PM
541{
542 srcu_read_unlock(&srcu_ctl, idx);
543}
544
545static int srcu_torture_completed(void)
546{
547 return srcu_batches_completed(&srcu_ctl);
548}
549
b772e1dd
JT
550static void srcu_torture_synchronize(void)
551{
552 synchronize_srcu(&srcu_ctl);
553}
554
b2896d2e
PM
555static int srcu_torture_stats(char *page)
556{
557 int cnt = 0;
558 int cpu;
559 int idx = srcu_ctl.completed & 0x1;
560
561 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
562 torture_type, TORTURE_FLAG, idx);
563 for_each_possible_cpu(cpu) {
564 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
565 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
566 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
567 }
568 cnt += sprintf(&page[cnt], "\n");
569 return cnt;
570}
571
572static struct rcu_torture_ops srcu_ops = {
0acc512c
PM
573 .init = srcu_torture_init,
574 .cleanup = srcu_torture_cleanup,
575 .readlock = srcu_torture_read_lock,
576 .read_delay = srcu_read_delay,
577 .readunlock = srcu_torture_read_unlock,
578 .completed = srcu_torture_completed,
579 .deferred_free = rcu_sync_torture_deferred_free,
580 .sync = srcu_torture_synchronize,
581 .cb_barrier = NULL,
582 .stats = srcu_torture_stats,
583 .name = "srcu"
b2896d2e
PM
584};
585
804bb837
PM
586static void srcu_torture_synchronize_expedited(void)
587{
588 synchronize_srcu_expedited(&srcu_ctl);
589}
590
591static struct rcu_torture_ops srcu_expedited_ops = {
592 .init = srcu_torture_init,
593 .cleanup = srcu_torture_cleanup,
594 .readlock = srcu_torture_read_lock,
595 .read_delay = srcu_read_delay,
596 .readunlock = srcu_torture_read_unlock,
597 .completed = srcu_torture_completed,
598 .deferred_free = rcu_sync_torture_deferred_free,
599 .sync = srcu_torture_synchronize_expedited,
600 .cb_barrier = NULL,
601 .stats = srcu_torture_stats,
602 .name = "srcu_expedited"
603};
604
4b6c2cca
JT
605/*
606 * Definitions for sched torture testing.
607 */
608
609static int sched_torture_read_lock(void)
610{
611 preempt_disable();
612 return 0;
613}
614
615static void sched_torture_read_unlock(int idx)
616{
617 preempt_enable();
618}
619
2326974d
PM
620static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
621{
622 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
623}
624
4b6c2cca
JT
625static void sched_torture_synchronize(void)
626{
627 synchronize_sched();
628}
629
630static struct rcu_torture_ops sched_ops = {
0acc512c
PM
631 .init = rcu_sync_torture_init,
632 .cleanup = NULL,
633 .readlock = sched_torture_read_lock,
634 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
635 .readunlock = sched_torture_read_unlock,
d9a3da06 636 .completed = rcu_no_completed,
0acc512c
PM
637 .deferred_free = rcu_sched_torture_deferred_free,
638 .sync = sched_torture_synchronize,
639 .cb_barrier = rcu_barrier_sched,
bf66f18e 640 .fqs = rcu_sched_force_quiescent_state,
0acc512c
PM
641 .stats = NULL,
642 .irq_capable = 1,
643 .name = "sched"
4b6c2cca
JT
644};
645
804bb837 646static struct rcu_torture_ops sched_sync_ops = {
0acc512c
PM
647 .init = rcu_sync_torture_init,
648 .cleanup = NULL,
649 .readlock = sched_torture_read_lock,
650 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
651 .readunlock = sched_torture_read_unlock,
d9a3da06 652 .completed = rcu_no_completed,
0acc512c
PM
653 .deferred_free = rcu_sync_torture_deferred_free,
654 .sync = sched_torture_synchronize,
655 .cb_barrier = NULL,
bf66f18e 656 .fqs = rcu_sched_force_quiescent_state,
0acc512c
PM
657 .stats = NULL,
658 .name = "sched_sync"
659};
660
0acc512c
PM
661static struct rcu_torture_ops sched_expedited_ops = {
662 .init = rcu_sync_torture_init,
663 .cleanup = NULL,
664 .readlock = sched_torture_read_lock,
665 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
666 .readunlock = sched_torture_read_unlock,
d9a3da06 667 .completed = rcu_no_completed,
0acc512c
PM
668 .deferred_free = rcu_sync_torture_deferred_free,
669 .sync = synchronize_sched_expedited,
670 .cb_barrier = NULL,
bf66f18e 671 .fqs = rcu_sched_force_quiescent_state,
0acc512c
PM
672 .stats = rcu_expedited_torture_stats,
673 .irq_capable = 1,
674 .name = "sched_expedited"
2326974d
PM
675};
676
bf66f18e
PM
677/*
678 * RCU torture force-quiescent-state kthread. Repeatedly induces
679 * bursts of calls to force_quiescent_state(), increasing the probability
680 * of occurrence of some important types of race conditions.
681 */
682static int
683rcu_torture_fqs(void *arg)
684{
685 unsigned long fqs_resume_time;
686 int fqs_burst_remaining;
687
688 VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
689 do {
690 fqs_resume_time = jiffies + fqs_stutter * HZ;
691 while (jiffies - fqs_resume_time > LONG_MAX) {
692 schedule_timeout_interruptible(1);
693 }
694 fqs_burst_remaining = fqs_duration;
695 while (fqs_burst_remaining > 0) {
696 cur_ops->fqs();
697 udelay(fqs_holdoff);
698 fqs_burst_remaining -= fqs_holdoff;
699 }
700 rcu_stutter_wait("rcu_torture_fqs");
701 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
702 VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
703 rcutorture_shutdown_absorb("rcu_torture_fqs");
704 while (!kthread_should_stop())
705 schedule_timeout_uninterruptible(1);
706 return 0;
707}
708
a241ec65
PM
709/*
710 * RCU torture writer kthread. Repeatedly substitutes a new structure
711 * for that pointed to by rcu_torture_current, freeing the old structure
712 * after a series of grace periods (the "pipeline").
713 */
714static int
715rcu_torture_writer(void *arg)
716{
717 int i;
718 long oldbatch = rcu_batches_completed();
719 struct rcu_torture *rp;
720 struct rcu_torture *old_rp;
721 static DEFINE_RCU_RANDOM(rand);
722
723 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
dbdf65b1
IM
724 set_user_nice(current, 19);
725
a241ec65
PM
726 do {
727 schedule_timeout_uninterruptible(1);
a71fca58
PM
728 rp = rcu_torture_alloc();
729 if (rp == NULL)
a241ec65
PM
730 continue;
731 rp->rtort_pipe_count = 0;
732 udelay(rcu_random(&rand) & 0x3ff);
733 old_rp = rcu_torture_current;
996417d2 734 rp->rtort_mbtest = 1;
a241ec65 735 rcu_assign_pointer(rcu_torture_current, rp);
9b2619af 736 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
c8e5b163 737 if (old_rp) {
a241ec65
PM
738 i = old_rp->rtort_pipe_count;
739 if (i > RCU_TORTURE_PIPE_LEN)
740 i = RCU_TORTURE_PIPE_LEN;
741 atomic_inc(&rcu_torture_wcount[i]);
742 old_rp->rtort_pipe_count++;
0acc512c 743 cur_ops->deferred_free(old_rp);
a241ec65
PM
744 }
745 rcu_torture_current_version++;
72e9bb54 746 oldbatch = cur_ops->completed();
c9d557c1
PM
747 rcu_stutter_wait("rcu_torture_writer");
748 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
a241ec65 749 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
c9d557c1
PM
750 rcutorture_shutdown_absorb("rcu_torture_writer");
751 while (!kthread_should_stop())
a241ec65
PM
752 schedule_timeout_uninterruptible(1);
753 return 0;
754}
755
b772e1dd
JT
756/*
757 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
758 * delay between calls.
759 */
760static int
761rcu_torture_fakewriter(void *arg)
762{
763 DEFINE_RCU_RANDOM(rand);
764
765 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
766 set_user_nice(current, 19);
767
768 do {
769 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
770 udelay(rcu_random(&rand) & 0x3ff);
771 cur_ops->sync();
c9d557c1
PM
772 rcu_stutter_wait("rcu_torture_fakewriter");
773 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
b772e1dd
JT
774
775 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
c9d557c1
PM
776 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
777 while (!kthread_should_stop())
b772e1dd
JT
778 schedule_timeout_uninterruptible(1);
779 return 0;
780}
781
0729fbf3
PM
782/*
783 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
784 * incrementing the corresponding element of the pipeline array. The
785 * counter in the element should never be greater than 1, otherwise, the
786 * RCU implementation is broken.
787 */
788static void rcu_torture_timer(unsigned long unused)
789{
790 int idx;
791 int completed;
792 static DEFINE_RCU_RANDOM(rand);
793 static DEFINE_SPINLOCK(rand_lock);
794 struct rcu_torture *p;
795 int pipe_count;
796
797 idx = cur_ops->readlock();
798 completed = cur_ops->completed();
632ee200
PM
799 p = rcu_dereference_check(rcu_torture_current,
800 rcu_read_lock_held() ||
801 rcu_read_lock_bh_held() ||
802 rcu_read_lock_sched_held() ||
803 srcu_read_lock_held(&srcu_ctl));
0729fbf3
PM
804 if (p == NULL) {
805 /* Leave because rcu_torture_writer is not yet underway */
806 cur_ops->readunlock(idx);
807 return;
808 }
809 if (p->rtort_mbtest == 0)
810 atomic_inc(&n_rcu_torture_mberror);
811 spin_lock(&rand_lock);
0acc512c 812 cur_ops->read_delay(&rand);
0729fbf3
PM
813 n_rcu_torture_timers++;
814 spin_unlock(&rand_lock);
815 preempt_disable();
816 pipe_count = p->rtort_pipe_count;
817 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
818 /* Should not happen, but... */
819 pipe_count = RCU_TORTURE_PIPE_LEN;
820 }
dd17c8f7 821 __this_cpu_inc(rcu_torture_count[pipe_count]);
0729fbf3
PM
822 completed = cur_ops->completed() - completed;
823 if (completed > RCU_TORTURE_PIPE_LEN) {
824 /* Should not happen, but... */
825 completed = RCU_TORTURE_PIPE_LEN;
826 }
dd17c8f7 827 __this_cpu_inc(rcu_torture_batch[completed]);
0729fbf3
PM
828 preempt_enable();
829 cur_ops->readunlock(idx);
830}
831
a241ec65
PM
832/*
833 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
834 * incrementing the corresponding element of the pipeline array. The
835 * counter in the element should never be greater than 1, otherwise, the
836 * RCU implementation is broken.
837 */
838static int
839rcu_torture_reader(void *arg)
840{
841 int completed;
72e9bb54 842 int idx;
a241ec65
PM
843 DEFINE_RCU_RANDOM(rand);
844 struct rcu_torture *p;
845 int pipe_count;
0729fbf3 846 struct timer_list t;
a241ec65
PM
847
848 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
dbdf65b1 849 set_user_nice(current, 19);
0acc512c 850 if (irqreader && cur_ops->irq_capable)
0729fbf3 851 setup_timer_on_stack(&t, rcu_torture_timer, 0);
dbdf65b1 852
a241ec65 853 do {
0acc512c 854 if (irqreader && cur_ops->irq_capable) {
0729fbf3 855 if (!timer_pending(&t))
6155fec9 856 mod_timer(&t, jiffies + 1);
0729fbf3 857 }
72e9bb54
PM
858 idx = cur_ops->readlock();
859 completed = cur_ops->completed();
632ee200
PM
860 p = rcu_dereference_check(rcu_torture_current,
861 rcu_read_lock_held() ||
862 rcu_read_lock_bh_held() ||
863 rcu_read_lock_sched_held() ||
864 srcu_read_lock_held(&srcu_ctl));
a241ec65
PM
865 if (p == NULL) {
866 /* Wait for rcu_torture_writer to get underway */
72e9bb54 867 cur_ops->readunlock(idx);
a241ec65
PM
868 schedule_timeout_interruptible(HZ);
869 continue;
870 }
996417d2
PM
871 if (p->rtort_mbtest == 0)
872 atomic_inc(&n_rcu_torture_mberror);
0acc512c 873 cur_ops->read_delay(&rand);
a241ec65
PM
874 preempt_disable();
875 pipe_count = p->rtort_pipe_count;
876 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
877 /* Should not happen, but... */
878 pipe_count = RCU_TORTURE_PIPE_LEN;
879 }
dd17c8f7 880 __this_cpu_inc(rcu_torture_count[pipe_count]);
72e9bb54 881 completed = cur_ops->completed() - completed;
a241ec65
PM
882 if (completed > RCU_TORTURE_PIPE_LEN) {
883 /* Should not happen, but... */
884 completed = RCU_TORTURE_PIPE_LEN;
885 }
dd17c8f7 886 __this_cpu_inc(rcu_torture_batch[completed]);
a241ec65 887 preempt_enable();
72e9bb54 888 cur_ops->readunlock(idx);
a241ec65 889 schedule();
c9d557c1
PM
890 rcu_stutter_wait("rcu_torture_reader");
891 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
a241ec65 892 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
c9d557c1 893 rcutorture_shutdown_absorb("rcu_torture_reader");
0acc512c 894 if (irqreader && cur_ops->irq_capable)
0729fbf3 895 del_timer_sync(&t);
c9d557c1 896 while (!kthread_should_stop())
a241ec65
PM
897 schedule_timeout_uninterruptible(1);
898 return 0;
899}
900
901/*
902 * Create an RCU-torture statistics message in the specified buffer.
903 */
904static int
905rcu_torture_printk(char *page)
906{
907 int cnt = 0;
908 int cpu;
909 int i;
910 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
911 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
912
0a945022 913 for_each_possible_cpu(cpu) {
a241ec65
PM
914 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
915 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
916 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
917 }
918 }
919 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
920 if (pipesummary[i] != 0)
921 break;
922 }
72e9bb54 923 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
a241ec65 924 cnt += sprintf(&page[cnt],
996417d2 925 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
0729fbf3 926 "rtmbe: %d nt: %ld",
a241ec65
PM
927 rcu_torture_current,
928 rcu_torture_current_version,
929 list_empty(&rcu_torture_freelist),
930 atomic_read(&n_rcu_torture_alloc),
931 atomic_read(&n_rcu_torture_alloc_fail),
996417d2 932 atomic_read(&n_rcu_torture_free),
0729fbf3
PM
933 atomic_read(&n_rcu_torture_mberror),
934 n_rcu_torture_timers);
996417d2
PM
935 if (atomic_read(&n_rcu_torture_mberror) != 0)
936 cnt += sprintf(&page[cnt], " !!!");
72e9bb54 937 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
996417d2 938 if (i > 1) {
a241ec65 939 cnt += sprintf(&page[cnt], "!!! ");
996417d2 940 atomic_inc(&n_rcu_torture_error);
5af970a4 941 WARN_ON_ONCE(1);
996417d2 942 }
a241ec65
PM
943 cnt += sprintf(&page[cnt], "Reader Pipe: ");
944 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
945 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
72e9bb54 946 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
a241ec65 947 cnt += sprintf(&page[cnt], "Reader Batch: ");
72e9bb54 948 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
a241ec65 949 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
72e9bb54 950 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
a241ec65
PM
951 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
952 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
953 cnt += sprintf(&page[cnt], " %d",
954 atomic_read(&rcu_torture_wcount[i]));
955 }
956 cnt += sprintf(&page[cnt], "\n");
c8e5b163 957 if (cur_ops->stats)
72e9bb54 958 cnt += cur_ops->stats(&page[cnt]);
a241ec65
PM
959 return cnt;
960}
961
962/*
963 * Print torture statistics. Caller must ensure that there is only
964 * one call to this function at a given time!!! This is normally
965 * accomplished by relying on the module system to only have one copy
966 * of the module loaded, and then by giving the rcu_torture_stats
967 * kthread full control (or the init/cleanup functions when rcu_torture_stats
968 * thread is not running).
969 */
970static void
971rcu_torture_stats_print(void)
972{
973 int cnt;
974
975 cnt = rcu_torture_printk(printk_buf);
976 printk(KERN_ALERT "%s", printk_buf);
977}
978
979/*
980 * Periodically prints torture statistics, if periodic statistics printing
981 * was specified via the stat_interval module parameter.
982 *
983 * No need to worry about fullstop here, since this one doesn't reference
984 * volatile state or register callbacks.
985 */
986static int
987rcu_torture_stats(void *arg)
988{
989 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
990 do {
991 schedule_timeout_interruptible(stat_interval * HZ);
992 rcu_torture_stats_print();
c9d557c1
PM
993 rcutorture_shutdown_absorb("rcu_torture_stats");
994 } while (!kthread_should_stop());
a241ec65
PM
995 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
996 return 0;
997}
998
d84f5203
SV
999static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
1000
1001/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1002 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1003 */
b2896d2e 1004static void rcu_torture_shuffle_tasks(void)
d84f5203 1005{
d84f5203
SV
1006 int i;
1007
73d0a4b1 1008 cpumask_setall(shuffle_tmp_mask);
86ef5c9a 1009 get_online_cpus();
d84f5203
SV
1010
1011 /* No point in shuffling if there is only one online CPU (ex: UP) */
c9d557c1
PM
1012 if (num_online_cpus() == 1) {
1013 put_online_cpus();
1014 return;
1015 }
d84f5203
SV
1016
1017 if (rcu_idle_cpu != -1)
73d0a4b1 1018 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
d84f5203 1019
73d0a4b1 1020 set_cpus_allowed_ptr(current, shuffle_tmp_mask);
d84f5203 1021
c8e5b163 1022 if (reader_tasks) {
d84f5203
SV
1023 for (i = 0; i < nrealreaders; i++)
1024 if (reader_tasks[i])
f70316da 1025 set_cpus_allowed_ptr(reader_tasks[i],
73d0a4b1 1026 shuffle_tmp_mask);
d84f5203
SV
1027 }
1028
c8e5b163 1029 if (fakewriter_tasks) {
b772e1dd
JT
1030 for (i = 0; i < nfakewriters; i++)
1031 if (fakewriter_tasks[i])
f70316da 1032 set_cpus_allowed_ptr(fakewriter_tasks[i],
73d0a4b1 1033 shuffle_tmp_mask);
b772e1dd
JT
1034 }
1035
d84f5203 1036 if (writer_task)
73d0a4b1 1037 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
d84f5203
SV
1038
1039 if (stats_task)
73d0a4b1 1040 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
d84f5203
SV
1041
1042 if (rcu_idle_cpu == -1)
1043 rcu_idle_cpu = num_online_cpus() - 1;
1044 else
1045 rcu_idle_cpu--;
1046
86ef5c9a 1047 put_online_cpus();
d84f5203
SV
1048}
1049
1050/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1051 * system to become idle at a time and cut off its timer ticks. This is meant
1052 * to test the support for such tickless idle CPU in RCU.
1053 */
1054static int
1055rcu_torture_shuffle(void *arg)
1056{
1057 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1058 do {
1059 schedule_timeout_interruptible(shuffle_interval * HZ);
1060 rcu_torture_shuffle_tasks();
c9d557c1
PM
1061 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1062 } while (!kthread_should_stop());
d84f5203
SV
1063 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1064 return 0;
1065}
1066
d120f65f
PM
1067/* Cause the rcutorture test to "stutter", starting and stopping all
1068 * threads periodically.
1069 */
1070static int
1071rcu_torture_stutter(void *arg)
1072{
1073 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1074 do {
1075 schedule_timeout_interruptible(stutter * HZ);
1076 stutter_pause_test = 1;
c9d557c1 1077 if (!kthread_should_stop())
d120f65f
PM
1078 schedule_timeout_interruptible(stutter * HZ);
1079 stutter_pause_test = 0;
c9d557c1
PM
1080 rcutorture_shutdown_absorb("rcu_torture_stutter");
1081 } while (!kthread_should_stop());
d120f65f
PM
1082 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1083 return 0;
1084}
1085
95c38322
PM
1086static inline void
1087rcu_torture_print_module_parms(char *tag)
1088{
b772e1dd
JT
1089 printk(KERN_ALERT "%s" TORTURE_FLAG
1090 "--- %s: nreaders=%d nfakewriters=%d "
95c38322 1091 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
bf66f18e
PM
1092 "shuffle_interval=%d stutter=%d irqreader=%d "
1093 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d\n",
b772e1dd 1094 torture_type, tag, nrealreaders, nfakewriters,
d120f65f 1095 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
bf66f18e 1096 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter);
95c38322
PM
1097}
1098
343e9099
PM
1099static struct notifier_block rcutorture_nb = {
1100 .notifier_call = rcutorture_shutdown_notify,
1101};
1102
a241ec65
PM
1103static void
1104rcu_torture_cleanup(void)
1105{
1106 int i;
1107
343e9099 1108 mutex_lock(&fullstop_mutex);
c9d557c1
PM
1109 if (fullstop == FULLSTOP_SHUTDOWN) {
1110 printk(KERN_WARNING /* but going down anyway, so... */
1111 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
343e9099 1112 mutex_unlock(&fullstop_mutex);
c9d557c1 1113 schedule_timeout_uninterruptible(10);
343e9099
PM
1114 if (cur_ops->cb_barrier != NULL)
1115 cur_ops->cb_barrier();
1116 return;
1117 }
c9d557c1 1118 fullstop = FULLSTOP_RMMOD;
343e9099
PM
1119 mutex_unlock(&fullstop_mutex);
1120 unregister_reboot_notifier(&rcutorture_nb);
d120f65f
PM
1121 if (stutter_task) {
1122 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1123 kthread_stop(stutter_task);
1124 }
1125 stutter_task = NULL;
c8e5b163 1126 if (shuffler_task) {
d84f5203
SV
1127 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1128 kthread_stop(shuffler_task);
73d0a4b1 1129 free_cpumask_var(shuffle_tmp_mask);
d84f5203
SV
1130 }
1131 shuffler_task = NULL;
1132
c8e5b163 1133 if (writer_task) {
a241ec65
PM
1134 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1135 kthread_stop(writer_task);
1136 }
1137 writer_task = NULL;
1138
c8e5b163 1139 if (reader_tasks) {
a241ec65 1140 for (i = 0; i < nrealreaders; i++) {
c8e5b163 1141 if (reader_tasks[i]) {
a241ec65
PM
1142 VERBOSE_PRINTK_STRING(
1143 "Stopping rcu_torture_reader task");
1144 kthread_stop(reader_tasks[i]);
1145 }
1146 reader_tasks[i] = NULL;
1147 }
1148 kfree(reader_tasks);
1149 reader_tasks = NULL;
1150 }
1151 rcu_torture_current = NULL;
1152
c8e5b163 1153 if (fakewriter_tasks) {
b772e1dd 1154 for (i = 0; i < nfakewriters; i++) {
c8e5b163 1155 if (fakewriter_tasks[i]) {
b772e1dd
JT
1156 VERBOSE_PRINTK_STRING(
1157 "Stopping rcu_torture_fakewriter task");
1158 kthread_stop(fakewriter_tasks[i]);
1159 }
1160 fakewriter_tasks[i] = NULL;
1161 }
1162 kfree(fakewriter_tasks);
1163 fakewriter_tasks = NULL;
1164 }
1165
c8e5b163 1166 if (stats_task) {
a241ec65
PM
1167 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1168 kthread_stop(stats_task);
1169 }
1170 stats_task = NULL;
1171
bf66f18e
PM
1172 if (fqs_task) {
1173 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1174 kthread_stop(fqs_task);
1175 }
1176 fqs_task = NULL;
1177
a241ec65 1178 /* Wait for all RCU callbacks to fire. */
2326974d
PM
1179
1180 if (cur_ops->cb_barrier != NULL)
1181 cur_ops->cb_barrier();
a241ec65 1182
a241ec65 1183 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
72e9bb54 1184
c8e5b163 1185 if (cur_ops->cleanup)
72e9bb54 1186 cur_ops->cleanup();
95c38322
PM
1187 if (atomic_read(&n_rcu_torture_error))
1188 rcu_torture_print_module_parms("End of test: FAILURE");
1189 else
1190 rcu_torture_print_module_parms("End of test: SUCCESS");
a241ec65
PM
1191}
1192
6f8bc500 1193static int __init
a241ec65
PM
1194rcu_torture_init(void)
1195{
1196 int i;
1197 int cpu;
1198 int firsterr = 0;
ade5fb81 1199 static struct rcu_torture_ops *torture_ops[] =
d9a3da06
PM
1200 { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
1201 &rcu_bh_ops, &rcu_bh_sync_ops,
804bb837
PM
1202 &srcu_ops, &srcu_expedited_ops,
1203 &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
a241ec65 1204
343e9099
PM
1205 mutex_lock(&fullstop_mutex);
1206
a241ec65 1207 /* Process args and tell the world that the torturer is on the job. */
ade5fb81 1208 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
72e9bb54 1209 cur_ops = torture_ops[i];
ade5fb81 1210 if (strcmp(torture_type, cur_ops->name) == 0)
72e9bb54 1211 break;
72e9bb54 1212 }
ade5fb81 1213 if (i == ARRAY_SIZE(torture_ops)) {
cf886c44 1214 printk(KERN_ALERT "rcu-torture: invalid torture type: \"%s\"\n",
72e9bb54 1215 torture_type);
cf886c44
PM
1216 printk(KERN_ALERT "rcu-torture types:");
1217 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
1218 printk(KERN_ALERT " %s", torture_ops[i]->name);
1219 printk(KERN_ALERT "\n");
343e9099 1220 mutex_unlock(&fullstop_mutex);
a71fca58 1221 return -EINVAL;
72e9bb54 1222 }
bf66f18e
PM
1223 if (cur_ops->fqs == NULL && fqs_duration != 0) {
1224 printk(KERN_ALERT "rcu-torture: ->fqs NULL and non-zero "
1225 "fqs_duration, fqs disabled.\n");
1226 fqs_duration = 0;
1227 }
c8e5b163 1228 if (cur_ops->init)
72e9bb54
PM
1229 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1230
a241ec65
PM
1231 if (nreaders >= 0)
1232 nrealreaders = nreaders;
1233 else
1234 nrealreaders = 2 * num_online_cpus();
95c38322 1235 rcu_torture_print_module_parms("Start of test");
c9d557c1 1236 fullstop = FULLSTOP_DONTSTOP;
a241ec65
PM
1237
1238 /* Set up the freelist. */
1239
1240 INIT_LIST_HEAD(&rcu_torture_freelist);
788e770e 1241 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
996417d2 1242 rcu_tortures[i].rtort_mbtest = 0;
a241ec65
PM
1243 list_add_tail(&rcu_tortures[i].rtort_free,
1244 &rcu_torture_freelist);
1245 }
1246
1247 /* Initialize the statistics so that each run gets its own numbers. */
1248
1249 rcu_torture_current = NULL;
1250 rcu_torture_current_version = 0;
1251 atomic_set(&n_rcu_torture_alloc, 0);
1252 atomic_set(&n_rcu_torture_alloc_fail, 0);
1253 atomic_set(&n_rcu_torture_free, 0);
996417d2
PM
1254 atomic_set(&n_rcu_torture_mberror, 0);
1255 atomic_set(&n_rcu_torture_error, 0);
a241ec65
PM
1256 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1257 atomic_set(&rcu_torture_wcount[i], 0);
0a945022 1258 for_each_possible_cpu(cpu) {
a241ec65
PM
1259 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1260 per_cpu(rcu_torture_count, cpu)[i] = 0;
1261 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1262 }
1263 }
1264
1265 /* Start up the kthreads. */
1266
1267 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1268 writer_task = kthread_run(rcu_torture_writer, NULL,
1269 "rcu_torture_writer");
1270 if (IS_ERR(writer_task)) {
1271 firsterr = PTR_ERR(writer_task);
1272 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1273 writer_task = NULL;
1274 goto unwind;
1275 }
b772e1dd 1276 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
a71fca58 1277 GFP_KERNEL);
b772e1dd
JT
1278 if (fakewriter_tasks == NULL) {
1279 VERBOSE_PRINTK_ERRSTRING("out of memory");
1280 firsterr = -ENOMEM;
1281 goto unwind;
1282 }
1283 for (i = 0; i < nfakewriters; i++) {
1284 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1285 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
a71fca58 1286 "rcu_torture_fakewriter");
b772e1dd
JT
1287 if (IS_ERR(fakewriter_tasks[i])) {
1288 firsterr = PTR_ERR(fakewriter_tasks[i]);
1289 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1290 fakewriter_tasks[i] = NULL;
1291 goto unwind;
1292 }
1293 }
2860aaba 1294 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
a241ec65
PM
1295 GFP_KERNEL);
1296 if (reader_tasks == NULL) {
1297 VERBOSE_PRINTK_ERRSTRING("out of memory");
1298 firsterr = -ENOMEM;
1299 goto unwind;
1300 }
1301 for (i = 0; i < nrealreaders; i++) {
1302 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1303 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1304 "rcu_torture_reader");
1305 if (IS_ERR(reader_tasks[i])) {
1306 firsterr = PTR_ERR(reader_tasks[i]);
1307 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1308 reader_tasks[i] = NULL;
1309 goto unwind;
1310 }
1311 }
1312 if (stat_interval > 0) {
1313 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1314 stats_task = kthread_run(rcu_torture_stats, NULL,
1315 "rcu_torture_stats");
1316 if (IS_ERR(stats_task)) {
1317 firsterr = PTR_ERR(stats_task);
1318 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1319 stats_task = NULL;
1320 goto unwind;
1321 }
1322 }
d84f5203
SV
1323 if (test_no_idle_hz) {
1324 rcu_idle_cpu = num_online_cpus() - 1;
73d0a4b1
RR
1325
1326 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
1327 firsterr = -ENOMEM;
1328 VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
1329 goto unwind;
1330 }
1331
d84f5203
SV
1332 /* Create the shuffler thread */
1333 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1334 "rcu_torture_shuffle");
1335 if (IS_ERR(shuffler_task)) {
73d0a4b1 1336 free_cpumask_var(shuffle_tmp_mask);
d84f5203
SV
1337 firsterr = PTR_ERR(shuffler_task);
1338 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1339 shuffler_task = NULL;
1340 goto unwind;
1341 }
1342 }
d120f65f
PM
1343 if (stutter < 0)
1344 stutter = 0;
1345 if (stutter) {
1346 /* Create the stutter thread */
1347 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1348 "rcu_torture_stutter");
1349 if (IS_ERR(stutter_task)) {
1350 firsterr = PTR_ERR(stutter_task);
1351 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1352 stutter_task = NULL;
1353 goto unwind;
1354 }
1355 }
bf66f18e
PM
1356 if (fqs_duration < 0)
1357 fqs_duration = 0;
1358 if (fqs_duration) {
1359 /* Create the stutter thread */
1360 fqs_task = kthread_run(rcu_torture_fqs, NULL,
1361 "rcu_torture_fqs");
1362 if (IS_ERR(fqs_task)) {
1363 firsterr = PTR_ERR(fqs_task);
1364 VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
1365 fqs_task = NULL;
1366 goto unwind;
1367 }
1368 }
343e9099
PM
1369 register_reboot_notifier(&rcutorture_nb);
1370 mutex_unlock(&fullstop_mutex);
a241ec65
PM
1371 return 0;
1372
1373unwind:
343e9099 1374 mutex_unlock(&fullstop_mutex);
a241ec65
PM
1375 rcu_torture_cleanup();
1376 return firsterr;
1377}
1378
1379module_init(rcu_torture_init);
1380module_exit(rcu_torture_cleanup);