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