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