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