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