rcu: Add rcutorture system-shutdown capability
[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>
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>
60063497 36#include <linux/atomic.h>
a241ec65 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). */
d5f546d8
PM
64static int fqs_duration; /* Duration of bursts (us), 0 to disable. */
65static int fqs_holdoff; /* Hold time within burst (us). */
bf66f18e 66static int fqs_stutter = 3; /* Wait time between bursts (s). */
d5f546d8 67static int shutdown_secs; /* Shutdown time (s). <=0 for no shutdown. */
8e8be45e
PM
68static int test_boost = 1; /* Test RCU prio boost: 0=no, 1=maybe, 2=yes. */
69static int test_boost_interval = 7; /* Interval between boost tests, seconds. */
70static int test_boost_duration = 4; /* Duration of each boost test, seconds. */
20d2e428 71static char *torture_type = "rcu"; /* What RCU implementation to torture. */
a241ec65 72
d6ad6711 73module_param(nreaders, int, 0444);
a241ec65 74MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
d6ad6711 75module_param(nfakewriters, int, 0444);
b772e1dd 76MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
3721bc1d 77module_param(stat_interval, int, 0644);
a241ec65 78MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
d6ad6711 79module_param(verbose, bool, 0444);
a241ec65 80MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
d6ad6711 81module_param(test_no_idle_hz, bool, 0444);
d84f5203 82MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
d6ad6711 83module_param(shuffle_interval, int, 0444);
d84f5203 84MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
d120f65f
PM
85module_param(stutter, int, 0444);
86MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
0729fbf3
PM
87module_param(irqreader, int, 0444);
88MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
bf66f18e
PM
89module_param(fqs_duration, int, 0444);
90MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
91module_param(fqs_holdoff, int, 0444);
92MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
93module_param(fqs_stutter, int, 0444);
94MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
d5f546d8
PM
95module_param(shutdown_secs, int, 0444);
96MODULE_PARM_DESC(shutdown_secs, "Shutdown time (s), zero to disable.");
8e8be45e
PM
97module_param(test_boost, int, 0444);
98MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
99module_param(test_boost_interval, int, 0444);
100MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
101module_param(test_boost_duration, int, 0444);
102MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
d6ad6711 103module_param(torture_type, charp, 0444);
b2896d2e 104MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
72e9bb54
PM
105
106#define TORTURE_FLAG "-torture:"
a241ec65 107#define PRINTK_STRING(s) \
72e9bb54 108 do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
a241ec65 109#define VERBOSE_PRINTK_STRING(s) \
72e9bb54 110 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
a241ec65 111#define VERBOSE_PRINTK_ERRSTRING(s) \
72e9bb54 112 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
a241ec65
PM
113
114static char printk_buf[4096];
115
116static int nrealreaders;
117static struct task_struct *writer_task;
b772e1dd 118static struct task_struct **fakewriter_tasks;
a241ec65
PM
119static struct task_struct **reader_tasks;
120static struct task_struct *stats_task;
d84f5203 121static struct task_struct *shuffler_task;
d120f65f 122static struct task_struct *stutter_task;
bf66f18e 123static struct task_struct *fqs_task;
8e8be45e 124static struct task_struct *boost_tasks[NR_CPUS];
d5f546d8 125static struct task_struct *shutdown_task;
a241ec65
PM
126
127#define RCU_TORTURE_PIPE_LEN 10
128
129struct rcu_torture {
130 struct rcu_head rtort_rcu;
131 int rtort_pipe_count;
132 struct list_head rtort_free;
996417d2 133 int rtort_mbtest;
a241ec65
PM
134};
135
a241ec65 136static LIST_HEAD(rcu_torture_freelist);
0ddea0ea 137static struct rcu_torture __rcu *rcu_torture_current;
4a298656 138static unsigned long rcu_torture_current_version;
a241ec65
PM
139static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
140static DEFINE_SPINLOCK(rcu_torture_lock);
141static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
142 { 0 };
143static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
144 { 0 };
145static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
b2896d2e
PM
146static atomic_t n_rcu_torture_alloc;
147static atomic_t n_rcu_torture_alloc_fail;
148static atomic_t n_rcu_torture_free;
149static atomic_t n_rcu_torture_mberror;
150static atomic_t n_rcu_torture_error;
8e8be45e
PM
151static long n_rcu_torture_boost_ktrerror;
152static long n_rcu_torture_boost_rterror;
8e8be45e
PM
153static long n_rcu_torture_boost_failure;
154static long n_rcu_torture_boosts;
a71fca58 155static long n_rcu_torture_timers;
e3033736 156static struct list_head rcu_torture_removed;
73d0a4b1 157static cpumask_var_t shuffle_tmp_mask;
a241ec65 158
a71fca58 159static int stutter_pause_test;
d120f65f 160
31a72bce
PM
161#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
162#define RCUTORTURE_RUNNABLE_INIT 1
163#else
164#define RCUTORTURE_RUNNABLE_INIT 0
165#endif
166int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
167
3acf4a9a 168#if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
8e8be45e 169#define rcu_can_boost() 1
3acf4a9a 170#else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
8e8be45e 171#define rcu_can_boost() 0
3acf4a9a 172#endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
8e8be45e 173
d5f546d8 174static unsigned long shutdown_time; /* jiffies to system shutdown. */
8e8be45e
PM
175static unsigned long boost_starttime; /* jiffies of next boost test start. */
176DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
177 /* and boost task create/destroy. */
178
c9d557c1
PM
179/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
180
181#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
182#define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
183#define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
184static int fullstop = FULLSTOP_RMMOD;
0ddea0ea
PM
185/*
186 * Protect fullstop transitions and spawning of kthreads.
187 */
188static DEFINE_MUTEX(fullstop_mutex);
343e9099 189
d5f546d8
PM
190/* Forward reference. */
191static void rcu_torture_cleanup(void);
192
343e9099 193/*
c9d557c1 194 * Detect and respond to a system shutdown.
343e9099
PM
195 */
196static int
197rcutorture_shutdown_notify(struct notifier_block *unused1,
198 unsigned long unused2, void *unused3)
199{
c59ab97e 200 mutex_lock(&fullstop_mutex);
c9d557c1 201 if (fullstop == FULLSTOP_DONTSTOP)
c59ab97e 202 fullstop = FULLSTOP_SHUTDOWN;
c9d557c1
PM
203 else
204 printk(KERN_WARNING /* but going down anyway, so... */
205 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
c59ab97e 206 mutex_unlock(&fullstop_mutex);
343e9099
PM
207 return NOTIFY_DONE;
208}
209
c9d557c1
PM
210/*
211 * Absorb kthreads into a kernel function that won't return, so that
212 * they won't ever access module text or data again.
213 */
214static void rcutorture_shutdown_absorb(char *title)
215{
216 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
217 printk(KERN_NOTICE
218 "rcutorture thread %s parking due to system shutdown\n",
219 title);
220 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
221 }
222}
223
a241ec65
PM
224/*
225 * Allocate an element from the rcu_tortures pool.
226 */
97a41e26 227static struct rcu_torture *
a241ec65
PM
228rcu_torture_alloc(void)
229{
230 struct list_head *p;
231
adac1665 232 spin_lock_bh(&rcu_torture_lock);
a241ec65
PM
233 if (list_empty(&rcu_torture_freelist)) {
234 atomic_inc(&n_rcu_torture_alloc_fail);
adac1665 235 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
236 return NULL;
237 }
238 atomic_inc(&n_rcu_torture_alloc);
239 p = rcu_torture_freelist.next;
240 list_del_init(p);
adac1665 241 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
242 return container_of(p, struct rcu_torture, rtort_free);
243}
244
245/*
246 * Free an element to the rcu_tortures pool.
247 */
248static void
249rcu_torture_free(struct rcu_torture *p)
250{
251 atomic_inc(&n_rcu_torture_free);
adac1665 252 spin_lock_bh(&rcu_torture_lock);
a241ec65 253 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
adac1665 254 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
255}
256
a241ec65
PM
257struct rcu_random_state {
258 unsigned long rrs_state;
75cfef32 259 long rrs_count;
a241ec65
PM
260};
261
262#define RCU_RANDOM_MULT 39916801 /* prime */
263#define RCU_RANDOM_ADD 479001701 /* prime */
264#define RCU_RANDOM_REFRESH 10000
265
266#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
267
268/*
269 * Crude but fast random-number generator. Uses a linear congruential
c17ac855 270 * generator, with occasional help from cpu_clock().
a241ec65 271 */
75cfef32 272static unsigned long
a241ec65
PM
273rcu_random(struct rcu_random_state *rrsp)
274{
a241ec65 275 if (--rrsp->rrs_count < 0) {
c676329a 276 rrsp->rrs_state += (unsigned long)local_clock();
a241ec65
PM
277 rrsp->rrs_count = RCU_RANDOM_REFRESH;
278 }
279 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
280 return swahw32(rrsp->rrs_state);
281}
282
d120f65f 283static void
c9d557c1 284rcu_stutter_wait(char *title)
d120f65f 285{
c9d557c1 286 while (stutter_pause_test || !rcutorture_runnable) {
e3d7be27
PM
287 if (rcutorture_runnable)
288 schedule_timeout_interruptible(1);
289 else
3ccf79f4 290 schedule_timeout_interruptible(round_jiffies_relative(HZ));
c9d557c1 291 rcutorture_shutdown_absorb(title);
343e9099 292 }
d120f65f
PM
293}
294
72e9bb54
PM
295/*
296 * Operations vector for selecting different types of tests.
297 */
298
299struct rcu_torture_ops {
300 void (*init)(void);
301 void (*cleanup)(void);
302 int (*readlock)(void);
0acc512c 303 void (*read_delay)(struct rcu_random_state *rrsp);
72e9bb54
PM
304 void (*readunlock)(int idx);
305 int (*completed)(void);
0acc512c 306 void (*deferred_free)(struct rcu_torture *p);
b772e1dd 307 void (*sync)(void);
2326974d 308 void (*cb_barrier)(void);
bf66f18e 309 void (*fqs)(void);
72e9bb54 310 int (*stats)(char *page);
0acc512c 311 int irq_capable;
8e8be45e 312 int can_boost;
72e9bb54
PM
313 char *name;
314};
a71fca58
PM
315
316static struct rcu_torture_ops *cur_ops;
72e9bb54
PM
317
318/*
319 * Definitions for rcu torture testing.
320 */
321
a49a4af7 322static int rcu_torture_read_lock(void) __acquires(RCU)
72e9bb54
PM
323{
324 rcu_read_lock();
325 return 0;
326}
327
b2896d2e
PM
328static void rcu_read_delay(struct rcu_random_state *rrsp)
329{
b8d57a76
JT
330 const unsigned long shortdelay_us = 200;
331 const unsigned long longdelay_ms = 50;
b2896d2e 332
b8d57a76
JT
333 /* We want a short delay sometimes to make a reader delay the grace
334 * period, and we want a long delay occasionally to trigger
335 * force_quiescent_state. */
b2896d2e 336
b8d57a76
JT
337 if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
338 mdelay(longdelay_ms);
339 if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
340 udelay(shortdelay_us);
e546f485
LJ
341#ifdef CONFIG_PREEMPT
342 if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
343 preempt_schedule(); /* No QS if preempt_disable() in effect */
344#endif
b2896d2e
PM
345}
346
a49a4af7 347static void rcu_torture_read_unlock(int idx) __releases(RCU)
72e9bb54
PM
348{
349 rcu_read_unlock();
350}
351
352static int rcu_torture_completed(void)
353{
354 return rcu_batches_completed();
355}
356
357static void
358rcu_torture_cb(struct rcu_head *p)
359{
360 int i;
361 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
362
c9d557c1 363 if (fullstop != FULLSTOP_DONTSTOP) {
72e9bb54
PM
364 /* Test is ending, just drop callbacks on the floor. */
365 /* The next initialization will pick up the pieces. */
366 return;
367 }
368 i = rp->rtort_pipe_count;
369 if (i > RCU_TORTURE_PIPE_LEN)
370 i = RCU_TORTURE_PIPE_LEN;
371 atomic_inc(&rcu_torture_wcount[i]);
372 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
373 rp->rtort_mbtest = 0;
374 rcu_torture_free(rp);
375 } else
0acc512c 376 cur_ops->deferred_free(rp);
72e9bb54
PM
377}
378
d9a3da06
PM
379static int rcu_no_completed(void)
380{
381 return 0;
382}
383
72e9bb54
PM
384static void rcu_torture_deferred_free(struct rcu_torture *p)
385{
386 call_rcu(&p->rtort_rcu, rcu_torture_cb);
387}
388
389static struct rcu_torture_ops rcu_ops = {
0acc512c
PM
390 .init = NULL,
391 .cleanup = NULL,
392 .readlock = rcu_torture_read_lock,
393 .read_delay = rcu_read_delay,
394 .readunlock = rcu_torture_read_unlock,
395 .completed = rcu_torture_completed,
396 .deferred_free = rcu_torture_deferred_free,
397 .sync = synchronize_rcu,
398 .cb_barrier = rcu_barrier,
bf66f18e 399 .fqs = rcu_force_quiescent_state,
0acc512c 400 .stats = NULL,
a71fca58 401 .irq_capable = 1,
8e8be45e 402 .can_boost = rcu_can_boost(),
a71fca58 403 .name = "rcu"
72e9bb54
PM
404};
405
e3033736
JT
406static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
407{
408 int i;
409 struct rcu_torture *rp;
410 struct rcu_torture *rp1;
411
412 cur_ops->sync();
413 list_add(&p->rtort_free, &rcu_torture_removed);
414 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
415 i = rp->rtort_pipe_count;
416 if (i > RCU_TORTURE_PIPE_LEN)
417 i = RCU_TORTURE_PIPE_LEN;
418 atomic_inc(&rcu_torture_wcount[i]);
419 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
420 rp->rtort_mbtest = 0;
421 list_del(&rp->rtort_free);
422 rcu_torture_free(rp);
423 }
424 }
425}
426
427static void rcu_sync_torture_init(void)
428{
429 INIT_LIST_HEAD(&rcu_torture_removed);
430}
431
20d2e428 432static struct rcu_torture_ops rcu_sync_ops = {
0acc512c
PM
433 .init = rcu_sync_torture_init,
434 .cleanup = NULL,
435 .readlock = rcu_torture_read_lock,
436 .read_delay = rcu_read_delay,
437 .readunlock = rcu_torture_read_unlock,
438 .completed = rcu_torture_completed,
439 .deferred_free = rcu_sync_torture_deferred_free,
440 .sync = synchronize_rcu,
441 .cb_barrier = NULL,
bf66f18e 442 .fqs = rcu_force_quiescent_state,
0acc512c
PM
443 .stats = NULL,
444 .irq_capable = 1,
8e8be45e 445 .can_boost = rcu_can_boost(),
0acc512c 446 .name = "rcu_sync"
20d2e428
JT
447};
448
d9a3da06
PM
449static struct rcu_torture_ops rcu_expedited_ops = {
450 .init = rcu_sync_torture_init,
451 .cleanup = NULL,
452 .readlock = rcu_torture_read_lock,
453 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
454 .readunlock = rcu_torture_read_unlock,
455 .completed = rcu_no_completed,
456 .deferred_free = rcu_sync_torture_deferred_free,
457 .sync = synchronize_rcu_expedited,
458 .cb_barrier = NULL,
bf66f18e 459 .fqs = rcu_force_quiescent_state,
d9a3da06
PM
460 .stats = NULL,
461 .irq_capable = 1,
8e8be45e 462 .can_boost = rcu_can_boost(),
d9a3da06
PM
463 .name = "rcu_expedited"
464};
465
c32e0660
PM
466/*
467 * Definitions for rcu_bh torture testing.
468 */
469
a49a4af7 470static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
c32e0660
PM
471{
472 rcu_read_lock_bh();
473 return 0;
474}
475
a49a4af7 476static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
c32e0660
PM
477{
478 rcu_read_unlock_bh();
479}
480
481static int rcu_bh_torture_completed(void)
482{
483 return rcu_batches_completed_bh();
484}
485
486static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
487{
488 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
489}
490
491static struct rcu_torture_ops rcu_bh_ops = {
0acc512c
PM
492 .init = NULL,
493 .cleanup = NULL,
494 .readlock = rcu_bh_torture_read_lock,
495 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
496 .readunlock = rcu_bh_torture_read_unlock,
497 .completed = rcu_bh_torture_completed,
498 .deferred_free = rcu_bh_torture_deferred_free,
bdf2a436 499 .sync = synchronize_rcu_bh,
0acc512c 500 .cb_barrier = rcu_barrier_bh,
bf66f18e 501 .fqs = rcu_bh_force_quiescent_state,
0acc512c
PM
502 .stats = NULL,
503 .irq_capable = 1,
504 .name = "rcu_bh"
c32e0660
PM
505};
506
11a14701 507static struct rcu_torture_ops rcu_bh_sync_ops = {
0acc512c
PM
508 .init = rcu_sync_torture_init,
509 .cleanup = NULL,
510 .readlock = rcu_bh_torture_read_lock,
511 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
512 .readunlock = rcu_bh_torture_read_unlock,
513 .completed = rcu_bh_torture_completed,
514 .deferred_free = rcu_sync_torture_deferred_free,
bdf2a436 515 .sync = synchronize_rcu_bh,
0acc512c 516 .cb_barrier = NULL,
bf66f18e 517 .fqs = rcu_bh_force_quiescent_state,
0acc512c
PM
518 .stats = NULL,
519 .irq_capable = 1,
520 .name = "rcu_bh_sync"
11a14701
JT
521};
522
bdf2a436
PM
523static struct rcu_torture_ops rcu_bh_expedited_ops = {
524 .init = rcu_sync_torture_init,
525 .cleanup = NULL,
526 .readlock = rcu_bh_torture_read_lock,
527 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
528 .readunlock = rcu_bh_torture_read_unlock,
529 .completed = rcu_bh_torture_completed,
530 .deferred_free = rcu_sync_torture_deferred_free,
531 .sync = synchronize_rcu_bh_expedited,
532 .cb_barrier = NULL,
533 .fqs = rcu_bh_force_quiescent_state,
534 .stats = NULL,
535 .irq_capable = 1,
536 .name = "rcu_bh_expedited"
537};
538
b2896d2e
PM
539/*
540 * Definitions for srcu torture testing.
541 */
542
543static struct srcu_struct srcu_ctl;
b2896d2e
PM
544
545static void srcu_torture_init(void)
546{
547 init_srcu_struct(&srcu_ctl);
e3033736 548 rcu_sync_torture_init();
b2896d2e
PM
549}
550
551static void srcu_torture_cleanup(void)
552{
553 synchronize_srcu(&srcu_ctl);
554 cleanup_srcu_struct(&srcu_ctl);
555}
556
012d3ca8 557static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
b2896d2e
PM
558{
559 return srcu_read_lock(&srcu_ctl);
560}
561
562static void srcu_read_delay(struct rcu_random_state *rrsp)
563{
564 long delay;
565 const long uspertick = 1000000 / HZ;
566 const long longdelay = 10;
567
568 /* We want there to be long-running readers, but not all the time. */
569
570 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
571 if (!delay)
572 schedule_timeout_interruptible(longdelay);
e546f485
LJ
573 else
574 rcu_read_delay(rrsp);
b2896d2e
PM
575}
576
012d3ca8 577static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
b2896d2e
PM
578{
579 srcu_read_unlock(&srcu_ctl, idx);
580}
581
582static int srcu_torture_completed(void)
583{
584 return srcu_batches_completed(&srcu_ctl);
585}
586
b772e1dd
JT
587static void srcu_torture_synchronize(void)
588{
589 synchronize_srcu(&srcu_ctl);
590}
591
b2896d2e
PM
592static int srcu_torture_stats(char *page)
593{
594 int cnt = 0;
595 int cpu;
596 int idx = srcu_ctl.completed & 0x1;
597
598 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
599 torture_type, TORTURE_FLAG, idx);
600 for_each_possible_cpu(cpu) {
601 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
602 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
603 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
604 }
605 cnt += sprintf(&page[cnt], "\n");
606 return cnt;
607}
608
609static struct rcu_torture_ops srcu_ops = {
0acc512c
PM
610 .init = srcu_torture_init,
611 .cleanup = srcu_torture_cleanup,
612 .readlock = srcu_torture_read_lock,
613 .read_delay = srcu_read_delay,
614 .readunlock = srcu_torture_read_unlock,
615 .completed = srcu_torture_completed,
616 .deferred_free = rcu_sync_torture_deferred_free,
617 .sync = srcu_torture_synchronize,
618 .cb_barrier = NULL,
619 .stats = srcu_torture_stats,
620 .name = "srcu"
b2896d2e
PM
621};
622
804bb837
PM
623static void srcu_torture_synchronize_expedited(void)
624{
625 synchronize_srcu_expedited(&srcu_ctl);
626}
627
628static struct rcu_torture_ops srcu_expedited_ops = {
629 .init = srcu_torture_init,
630 .cleanup = srcu_torture_cleanup,
631 .readlock = srcu_torture_read_lock,
632 .read_delay = srcu_read_delay,
633 .readunlock = srcu_torture_read_unlock,
634 .completed = srcu_torture_completed,
635 .deferred_free = rcu_sync_torture_deferred_free,
636 .sync = srcu_torture_synchronize_expedited,
637 .cb_barrier = NULL,
638 .stats = srcu_torture_stats,
639 .name = "srcu_expedited"
640};
641
4b6c2cca
JT
642/*
643 * Definitions for sched torture testing.
644 */
645
646static int sched_torture_read_lock(void)
647{
648 preempt_disable();
649 return 0;
650}
651
652static void sched_torture_read_unlock(int idx)
653{
654 preempt_enable();
655}
656
2326974d
PM
657static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
658{
659 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
660}
661
4b6c2cca 662static struct rcu_torture_ops sched_ops = {
0acc512c
PM
663 .init = rcu_sync_torture_init,
664 .cleanup = NULL,
665 .readlock = sched_torture_read_lock,
666 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
667 .readunlock = sched_torture_read_unlock,
d9a3da06 668 .completed = rcu_no_completed,
0acc512c 669 .deferred_free = rcu_sched_torture_deferred_free,
bdf2a436 670 .sync = synchronize_sched,
0acc512c 671 .cb_barrier = rcu_barrier_sched,
bf66f18e 672 .fqs = rcu_sched_force_quiescent_state,
0acc512c
PM
673 .stats = NULL,
674 .irq_capable = 1,
675 .name = "sched"
4b6c2cca
JT
676};
677
804bb837 678static struct rcu_torture_ops sched_sync_ops = {
0acc512c
PM
679 .init = rcu_sync_torture_init,
680 .cleanup = NULL,
681 .readlock = sched_torture_read_lock,
682 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
683 .readunlock = sched_torture_read_unlock,
d9a3da06 684 .completed = rcu_no_completed,
0acc512c 685 .deferred_free = rcu_sync_torture_deferred_free,
bdf2a436 686 .sync = synchronize_sched,
0acc512c 687 .cb_barrier = NULL,
bf66f18e 688 .fqs = rcu_sched_force_quiescent_state,
0acc512c
PM
689 .stats = NULL,
690 .name = "sched_sync"
691};
692
0acc512c
PM
693static struct rcu_torture_ops sched_expedited_ops = {
694 .init = rcu_sync_torture_init,
695 .cleanup = NULL,
696 .readlock = sched_torture_read_lock,
697 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
698 .readunlock = sched_torture_read_unlock,
d9a3da06 699 .completed = rcu_no_completed,
0acc512c
PM
700 .deferred_free = rcu_sync_torture_deferred_free,
701 .sync = synchronize_sched_expedited,
702 .cb_barrier = NULL,
bf66f18e 703 .fqs = rcu_sched_force_quiescent_state,
969c7921 704 .stats = NULL,
0acc512c
PM
705 .irq_capable = 1,
706 .name = "sched_expedited"
2326974d
PM
707};
708
8e8be45e
PM
709/*
710 * RCU torture priority-boost testing. Runs one real-time thread per
711 * CPU for moderate bursts, repeatedly registering RCU callbacks and
712 * spinning waiting for them to be invoked. If a given callback takes
713 * too long to be invoked, we assume that priority inversion has occurred.
714 */
715
716struct rcu_boost_inflight {
717 struct rcu_head rcu;
718 int inflight;
719};
720
721static void rcu_torture_boost_cb(struct rcu_head *head)
722{
723 struct rcu_boost_inflight *rbip =
724 container_of(head, struct rcu_boost_inflight, rcu);
725
726 smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
727 rbip->inflight = 0;
728}
729
730static int rcu_torture_boost(void *arg)
731{
732 unsigned long call_rcu_time;
733 unsigned long endtime;
734 unsigned long oldstarttime;
735 struct rcu_boost_inflight rbi = { .inflight = 0 };
736 struct sched_param sp;
737
738 VERBOSE_PRINTK_STRING("rcu_torture_boost started");
739
740 /* Set real-time priority. */
741 sp.sched_priority = 1;
742 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
743 VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
744 n_rcu_torture_boost_rterror++;
745 }
746
561190e3 747 init_rcu_head_on_stack(&rbi.rcu);
8e8be45e
PM
748 /* Each pass through the following loop does one boost-test cycle. */
749 do {
750 /* Wait for the next test interval. */
751 oldstarttime = boost_starttime;
93898fb1 752 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
8e8be45e
PM
753 schedule_timeout_uninterruptible(1);
754 rcu_stutter_wait("rcu_torture_boost");
755 if (kthread_should_stop() ||
756 fullstop != FULLSTOP_DONTSTOP)
757 goto checkwait;
758 }
759
760 /* Do one boost-test interval. */
761 endtime = oldstarttime + test_boost_duration * HZ;
762 call_rcu_time = jiffies;
93898fb1 763 while (ULONG_CMP_LT(jiffies, endtime)) {
8e8be45e
PM
764 /* If we don't have a callback in flight, post one. */
765 if (!rbi.inflight) {
766 smp_mb(); /* RCU core before ->inflight = 1. */
767 rbi.inflight = 1;
768 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
769 if (jiffies - call_rcu_time >
770 test_boost_duration * HZ - HZ / 2) {
771 VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
772 n_rcu_torture_boost_failure++;
773 }
774 call_rcu_time = jiffies;
775 }
776 cond_resched();
777 rcu_stutter_wait("rcu_torture_boost");
778 if (kthread_should_stop() ||
779 fullstop != FULLSTOP_DONTSTOP)
780 goto checkwait;
781 }
782
783 /*
784 * Set the start time of the next test interval.
785 * Yes, this is vulnerable to long delays, but such
786 * delays simply cause a false negative for the next
787 * interval. Besides, we are running at RT priority,
788 * so delays should be relatively rare.
789 */
ab8f11e5
PM
790 while (oldstarttime == boost_starttime &&
791 !kthread_should_stop()) {
8e8be45e
PM
792 if (mutex_trylock(&boost_mutex)) {
793 boost_starttime = jiffies +
794 test_boost_interval * HZ;
795 n_rcu_torture_boosts++;
796 mutex_unlock(&boost_mutex);
797 break;
798 }
799 schedule_timeout_uninterruptible(1);
800 }
801
802 /* Go do the stutter. */
803checkwait: rcu_stutter_wait("rcu_torture_boost");
804 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
805
806 /* Clean up and exit. */
807 VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
808 rcutorture_shutdown_absorb("rcu_torture_boost");
809 while (!kthread_should_stop() || rbi.inflight)
810 schedule_timeout_uninterruptible(1);
811 smp_mb(); /* order accesses to ->inflight before stack-frame death. */
9d68197c 812 destroy_rcu_head_on_stack(&rbi.rcu);
8e8be45e
PM
813 return 0;
814}
815
bf66f18e
PM
816/*
817 * RCU torture force-quiescent-state kthread. Repeatedly induces
818 * bursts of calls to force_quiescent_state(), increasing the probability
819 * of occurrence of some important types of race conditions.
820 */
821static int
822rcu_torture_fqs(void *arg)
823{
824 unsigned long fqs_resume_time;
825 int fqs_burst_remaining;
826
827 VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
828 do {
829 fqs_resume_time = jiffies + fqs_stutter * HZ;
93898fb1
PM
830 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
831 !kthread_should_stop()) {
bf66f18e
PM
832 schedule_timeout_interruptible(1);
833 }
834 fqs_burst_remaining = fqs_duration;
93898fb1
PM
835 while (fqs_burst_remaining > 0 &&
836 !kthread_should_stop()) {
bf66f18e
PM
837 cur_ops->fqs();
838 udelay(fqs_holdoff);
839 fqs_burst_remaining -= fqs_holdoff;
840 }
841 rcu_stutter_wait("rcu_torture_fqs");
842 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
843 VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
844 rcutorture_shutdown_absorb("rcu_torture_fqs");
845 while (!kthread_should_stop())
846 schedule_timeout_uninterruptible(1);
847 return 0;
848}
849
a241ec65
PM
850/*
851 * RCU torture writer kthread. Repeatedly substitutes a new structure
852 * for that pointed to by rcu_torture_current, freeing the old structure
853 * after a series of grace periods (the "pipeline").
854 */
855static int
856rcu_torture_writer(void *arg)
857{
858 int i;
859 long oldbatch = rcu_batches_completed();
860 struct rcu_torture *rp;
861 struct rcu_torture *old_rp;
862 static DEFINE_RCU_RANDOM(rand);
863
864 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
dbdf65b1
IM
865 set_user_nice(current, 19);
866
a241ec65
PM
867 do {
868 schedule_timeout_uninterruptible(1);
a71fca58
PM
869 rp = rcu_torture_alloc();
870 if (rp == NULL)
a241ec65
PM
871 continue;
872 rp->rtort_pipe_count = 0;
873 udelay(rcu_random(&rand) & 0x3ff);
0ddea0ea
PM
874 old_rp = rcu_dereference_check(rcu_torture_current,
875 current == writer_task);
996417d2 876 rp->rtort_mbtest = 1;
a241ec65 877 rcu_assign_pointer(rcu_torture_current, rp);
9b2619af 878 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
c8e5b163 879 if (old_rp) {
a241ec65
PM
880 i = old_rp->rtort_pipe_count;
881 if (i > RCU_TORTURE_PIPE_LEN)
882 i = RCU_TORTURE_PIPE_LEN;
883 atomic_inc(&rcu_torture_wcount[i]);
884 old_rp->rtort_pipe_count++;
0acc512c 885 cur_ops->deferred_free(old_rp);
a241ec65 886 }
4a298656 887 rcutorture_record_progress(++rcu_torture_current_version);
72e9bb54 888 oldbatch = cur_ops->completed();
c9d557c1
PM
889 rcu_stutter_wait("rcu_torture_writer");
890 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
a241ec65 891 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
c9d557c1
PM
892 rcutorture_shutdown_absorb("rcu_torture_writer");
893 while (!kthread_should_stop())
a241ec65
PM
894 schedule_timeout_uninterruptible(1);
895 return 0;
896}
897
b772e1dd
JT
898/*
899 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
900 * delay between calls.
901 */
902static int
903rcu_torture_fakewriter(void *arg)
904{
905 DEFINE_RCU_RANDOM(rand);
906
907 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
908 set_user_nice(current, 19);
909
910 do {
911 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
912 udelay(rcu_random(&rand) & 0x3ff);
913 cur_ops->sync();
c9d557c1
PM
914 rcu_stutter_wait("rcu_torture_fakewriter");
915 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
b772e1dd
JT
916
917 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
c9d557c1
PM
918 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
919 while (!kthread_should_stop())
b772e1dd
JT
920 schedule_timeout_uninterruptible(1);
921 return 0;
922}
923
91afaf30
PM
924void rcutorture_trace_dump(void)
925{
926 static atomic_t beenhere = ATOMIC_INIT(0);
927
928 if (atomic_read(&beenhere))
929 return;
930 if (atomic_xchg(&beenhere, 1) != 0)
931 return;
932 do_trace_rcu_torture_read(cur_ops->name, (struct rcu_head *)~0UL);
933 ftrace_dump(DUMP_ALL);
934}
935
0729fbf3
PM
936/*
937 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
938 * incrementing the corresponding element of the pipeline array. The
939 * counter in the element should never be greater than 1, otherwise, the
940 * RCU implementation is broken.
941 */
942static void rcu_torture_timer(unsigned long unused)
943{
944 int idx;
945 int completed;
946 static DEFINE_RCU_RANDOM(rand);
947 static DEFINE_SPINLOCK(rand_lock);
948 struct rcu_torture *p;
949 int pipe_count;
950
951 idx = cur_ops->readlock();
952 completed = cur_ops->completed();
632ee200 953 p = rcu_dereference_check(rcu_torture_current,
632ee200
PM
954 rcu_read_lock_bh_held() ||
955 rcu_read_lock_sched_held() ||
956 srcu_read_lock_held(&srcu_ctl));
91afaf30 957 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu);
0729fbf3
PM
958 if (p == NULL) {
959 /* Leave because rcu_torture_writer is not yet underway */
960 cur_ops->readunlock(idx);
961 return;
962 }
963 if (p->rtort_mbtest == 0)
964 atomic_inc(&n_rcu_torture_mberror);
965 spin_lock(&rand_lock);
0acc512c 966 cur_ops->read_delay(&rand);
0729fbf3
PM
967 n_rcu_torture_timers++;
968 spin_unlock(&rand_lock);
969 preempt_disable();
970 pipe_count = p->rtort_pipe_count;
971 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
972 /* Should not happen, but... */
973 pipe_count = RCU_TORTURE_PIPE_LEN;
974 }
91afaf30
PM
975 if (pipe_count > 1)
976 rcutorture_trace_dump();
dd17c8f7 977 __this_cpu_inc(rcu_torture_count[pipe_count]);
0729fbf3
PM
978 completed = cur_ops->completed() - completed;
979 if (completed > RCU_TORTURE_PIPE_LEN) {
980 /* Should not happen, but... */
981 completed = RCU_TORTURE_PIPE_LEN;
982 }
dd17c8f7 983 __this_cpu_inc(rcu_torture_batch[completed]);
0729fbf3
PM
984 preempt_enable();
985 cur_ops->readunlock(idx);
986}
987
a241ec65
PM
988/*
989 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
990 * incrementing the corresponding element of the pipeline array. The
991 * counter in the element should never be greater than 1, otherwise, the
992 * RCU implementation is broken.
993 */
994static int
995rcu_torture_reader(void *arg)
996{
997 int completed;
72e9bb54 998 int idx;
a241ec65
PM
999 DEFINE_RCU_RANDOM(rand);
1000 struct rcu_torture *p;
1001 int pipe_count;
0729fbf3 1002 struct timer_list t;
a241ec65
PM
1003
1004 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
dbdf65b1 1005 set_user_nice(current, 19);
0acc512c 1006 if (irqreader && cur_ops->irq_capable)
0729fbf3 1007 setup_timer_on_stack(&t, rcu_torture_timer, 0);
dbdf65b1 1008
a241ec65 1009 do {
0acc512c 1010 if (irqreader && cur_ops->irq_capable) {
0729fbf3 1011 if (!timer_pending(&t))
6155fec9 1012 mod_timer(&t, jiffies + 1);
0729fbf3 1013 }
72e9bb54
PM
1014 idx = cur_ops->readlock();
1015 completed = cur_ops->completed();
632ee200 1016 p = rcu_dereference_check(rcu_torture_current,
632ee200
PM
1017 rcu_read_lock_bh_held() ||
1018 rcu_read_lock_sched_held() ||
1019 srcu_read_lock_held(&srcu_ctl));
91afaf30 1020 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu);
a241ec65
PM
1021 if (p == NULL) {
1022 /* Wait for rcu_torture_writer to get underway */
72e9bb54 1023 cur_ops->readunlock(idx);
a241ec65
PM
1024 schedule_timeout_interruptible(HZ);
1025 continue;
1026 }
996417d2
PM
1027 if (p->rtort_mbtest == 0)
1028 atomic_inc(&n_rcu_torture_mberror);
0acc512c 1029 cur_ops->read_delay(&rand);
a241ec65
PM
1030 preempt_disable();
1031 pipe_count = p->rtort_pipe_count;
1032 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1033 /* Should not happen, but... */
1034 pipe_count = RCU_TORTURE_PIPE_LEN;
1035 }
91afaf30
PM
1036 if (pipe_count > 1)
1037 rcutorture_trace_dump();
dd17c8f7 1038 __this_cpu_inc(rcu_torture_count[pipe_count]);
72e9bb54 1039 completed = cur_ops->completed() - completed;
a241ec65
PM
1040 if (completed > RCU_TORTURE_PIPE_LEN) {
1041 /* Should not happen, but... */
1042 completed = RCU_TORTURE_PIPE_LEN;
1043 }
dd17c8f7 1044 __this_cpu_inc(rcu_torture_batch[completed]);
a241ec65 1045 preempt_enable();
72e9bb54 1046 cur_ops->readunlock(idx);
a241ec65 1047 schedule();
c9d557c1
PM
1048 rcu_stutter_wait("rcu_torture_reader");
1049 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
a241ec65 1050 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
c9d557c1 1051 rcutorture_shutdown_absorb("rcu_torture_reader");
0acc512c 1052 if (irqreader && cur_ops->irq_capable)
0729fbf3 1053 del_timer_sync(&t);
c9d557c1 1054 while (!kthread_should_stop())
a241ec65
PM
1055 schedule_timeout_uninterruptible(1);
1056 return 0;
1057}
1058
1059/*
1060 * Create an RCU-torture statistics message in the specified buffer.
1061 */
1062static int
1063rcu_torture_printk(char *page)
1064{
1065 int cnt = 0;
1066 int cpu;
1067 int i;
1068 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1069 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1070
0a945022 1071 for_each_possible_cpu(cpu) {
a241ec65
PM
1072 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1073 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1074 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1075 }
1076 }
1077 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1078 if (pipesummary[i] != 0)
1079 break;
1080 }
72e9bb54 1081 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
a241ec65 1082 cnt += sprintf(&page[cnt],
4a298656 1083 "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d "
67b98dba 1084 "rtmbe: %d rtbke: %ld rtbre: %ld "
8e8be45e 1085 "rtbf: %ld rtb: %ld nt: %ld",
a241ec65
PM
1086 rcu_torture_current,
1087 rcu_torture_current_version,
1088 list_empty(&rcu_torture_freelist),
1089 atomic_read(&n_rcu_torture_alloc),
1090 atomic_read(&n_rcu_torture_alloc_fail),
996417d2 1091 atomic_read(&n_rcu_torture_free),
0729fbf3 1092 atomic_read(&n_rcu_torture_mberror),
8e8be45e
PM
1093 n_rcu_torture_boost_ktrerror,
1094 n_rcu_torture_boost_rterror,
8e8be45e
PM
1095 n_rcu_torture_boost_failure,
1096 n_rcu_torture_boosts,
0729fbf3 1097 n_rcu_torture_timers);
8e8be45e
PM
1098 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
1099 n_rcu_torture_boost_ktrerror != 0 ||
1100 n_rcu_torture_boost_rterror != 0 ||
8e8be45e 1101 n_rcu_torture_boost_failure != 0)
996417d2 1102 cnt += sprintf(&page[cnt], " !!!");
72e9bb54 1103 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
996417d2 1104 if (i > 1) {
a241ec65 1105 cnt += sprintf(&page[cnt], "!!! ");
996417d2 1106 atomic_inc(&n_rcu_torture_error);
5af970a4 1107 WARN_ON_ONCE(1);
996417d2 1108 }
a241ec65
PM
1109 cnt += sprintf(&page[cnt], "Reader Pipe: ");
1110 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1111 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
72e9bb54 1112 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
a241ec65 1113 cnt += sprintf(&page[cnt], "Reader Batch: ");
72e9bb54 1114 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
a241ec65 1115 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
72e9bb54 1116 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
a241ec65
PM
1117 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
1118 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1119 cnt += sprintf(&page[cnt], " %d",
1120 atomic_read(&rcu_torture_wcount[i]));
1121 }
1122 cnt += sprintf(&page[cnt], "\n");
c8e5b163 1123 if (cur_ops->stats)
72e9bb54 1124 cnt += cur_ops->stats(&page[cnt]);
a241ec65
PM
1125 return cnt;
1126}
1127
1128/*
1129 * Print torture statistics. Caller must ensure that there is only
1130 * one call to this function at a given time!!! This is normally
1131 * accomplished by relying on the module system to only have one copy
1132 * of the module loaded, and then by giving the rcu_torture_stats
1133 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1134 * thread is not running).
1135 */
1136static void
1137rcu_torture_stats_print(void)
1138{
1139 int cnt;
1140
1141 cnt = rcu_torture_printk(printk_buf);
1142 printk(KERN_ALERT "%s", printk_buf);
1143}
1144
1145/*
1146 * Periodically prints torture statistics, if periodic statistics printing
1147 * was specified via the stat_interval module parameter.
1148 *
1149 * No need to worry about fullstop here, since this one doesn't reference
1150 * volatile state or register callbacks.
1151 */
1152static int
1153rcu_torture_stats(void *arg)
1154{
1155 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
1156 do {
1157 schedule_timeout_interruptible(stat_interval * HZ);
1158 rcu_torture_stats_print();
c9d557c1
PM
1159 rcutorture_shutdown_absorb("rcu_torture_stats");
1160 } while (!kthread_should_stop());
a241ec65
PM
1161 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1162 return 0;
1163}
1164
d84f5203
SV
1165static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
1166
1167/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1168 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1169 */
b2896d2e 1170static void rcu_torture_shuffle_tasks(void)
d84f5203 1171{
d84f5203
SV
1172 int i;
1173
73d0a4b1 1174 cpumask_setall(shuffle_tmp_mask);
86ef5c9a 1175 get_online_cpus();
d84f5203
SV
1176
1177 /* No point in shuffling if there is only one online CPU (ex: UP) */
c9d557c1
PM
1178 if (num_online_cpus() == 1) {
1179 put_online_cpus();
1180 return;
1181 }
d84f5203
SV
1182
1183 if (rcu_idle_cpu != -1)
73d0a4b1 1184 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
d84f5203 1185
73d0a4b1 1186 set_cpus_allowed_ptr(current, shuffle_tmp_mask);
d84f5203 1187
c8e5b163 1188 if (reader_tasks) {
d84f5203
SV
1189 for (i = 0; i < nrealreaders; i++)
1190 if (reader_tasks[i])
f70316da 1191 set_cpus_allowed_ptr(reader_tasks[i],
73d0a4b1 1192 shuffle_tmp_mask);
d84f5203
SV
1193 }
1194
c8e5b163 1195 if (fakewriter_tasks) {
b772e1dd
JT
1196 for (i = 0; i < nfakewriters; i++)
1197 if (fakewriter_tasks[i])
f70316da 1198 set_cpus_allowed_ptr(fakewriter_tasks[i],
73d0a4b1 1199 shuffle_tmp_mask);
b772e1dd
JT
1200 }
1201
d84f5203 1202 if (writer_task)
73d0a4b1 1203 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
d84f5203
SV
1204
1205 if (stats_task)
73d0a4b1 1206 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
d84f5203
SV
1207
1208 if (rcu_idle_cpu == -1)
1209 rcu_idle_cpu = num_online_cpus() - 1;
1210 else
1211 rcu_idle_cpu--;
1212
86ef5c9a 1213 put_online_cpus();
d84f5203
SV
1214}
1215
1216/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1217 * system to become idle at a time and cut off its timer ticks. This is meant
1218 * to test the support for such tickless idle CPU in RCU.
1219 */
1220static int
1221rcu_torture_shuffle(void *arg)
1222{
1223 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1224 do {
1225 schedule_timeout_interruptible(shuffle_interval * HZ);
1226 rcu_torture_shuffle_tasks();
c9d557c1
PM
1227 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1228 } while (!kthread_should_stop());
d84f5203
SV
1229 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1230 return 0;
1231}
1232
d120f65f
PM
1233/* Cause the rcutorture test to "stutter", starting and stopping all
1234 * threads periodically.
1235 */
1236static int
1237rcu_torture_stutter(void *arg)
1238{
1239 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1240 do {
1241 schedule_timeout_interruptible(stutter * HZ);
1242 stutter_pause_test = 1;
c9d557c1 1243 if (!kthread_should_stop())
d120f65f
PM
1244 schedule_timeout_interruptible(stutter * HZ);
1245 stutter_pause_test = 0;
c9d557c1
PM
1246 rcutorture_shutdown_absorb("rcu_torture_stutter");
1247 } while (!kthread_should_stop());
d120f65f
PM
1248 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1249 return 0;
1250}
1251
95c38322 1252static inline void
8e8be45e 1253rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, char *tag)
95c38322 1254{
b772e1dd
JT
1255 printk(KERN_ALERT "%s" TORTURE_FLAG
1256 "--- %s: nreaders=%d nfakewriters=%d "
95c38322 1257 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
bf66f18e 1258 "shuffle_interval=%d stutter=%d irqreader=%d "
8e8be45e
PM
1259 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1260 "test_boost=%d/%d test_boost_interval=%d "
d5f546d8 1261 "test_boost_duration=%d shutdown_secs=%d\n",
b772e1dd 1262 torture_type, tag, nrealreaders, nfakewriters,
d120f65f 1263 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
8e8be45e
PM
1264 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1265 test_boost, cur_ops->can_boost,
d5f546d8 1266 test_boost_interval, test_boost_duration, shutdown_secs);
95c38322
PM
1267}
1268
8e8be45e 1269static struct notifier_block rcutorture_shutdown_nb = {
343e9099
PM
1270 .notifier_call = rcutorture_shutdown_notify,
1271};
1272
8e8be45e
PM
1273static void rcutorture_booster_cleanup(int cpu)
1274{
1275 struct task_struct *t;
1276
1277 if (boost_tasks[cpu] == NULL)
1278 return;
1279 mutex_lock(&boost_mutex);
1280 VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
1281 t = boost_tasks[cpu];
1282 boost_tasks[cpu] = NULL;
1283 mutex_unlock(&boost_mutex);
1284
1285 /* This must be outside of the mutex, otherwise deadlock! */
1286 kthread_stop(t);
1287}
1288
1289static int rcutorture_booster_init(int cpu)
1290{
1291 int retval;
1292
1293 if (boost_tasks[cpu] != NULL)
1294 return 0; /* Already created, nothing more to do. */
1295
1296 /* Don't allow time recalculation while creating a new task. */
1297 mutex_lock(&boost_mutex);
1298 VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
1f288094
ED
1299 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1300 cpu_to_node(cpu),
1301 "rcu_torture_boost");
8e8be45e
PM
1302 if (IS_ERR(boost_tasks[cpu])) {
1303 retval = PTR_ERR(boost_tasks[cpu]);
1304 VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
1305 n_rcu_torture_boost_ktrerror++;
1306 boost_tasks[cpu] = NULL;
1307 mutex_unlock(&boost_mutex);
1308 return retval;
1309 }
1310 kthread_bind(boost_tasks[cpu], cpu);
1311 wake_up_process(boost_tasks[cpu]);
1312 mutex_unlock(&boost_mutex);
1313 return 0;
1314}
1315
d5f546d8
PM
1316/*
1317 * Cause the rcutorture test to shutdown the system after the test has
1318 * run for the time specified by the shutdown_secs module parameter.
1319 */
1320static int
1321rcu_torture_shutdown(void *arg)
1322{
1323 long delta;
1324 unsigned long jiffies_snap;
1325
1326 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
1327 jiffies_snap = ACCESS_ONCE(jiffies);
1328 while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
1329 !kthread_should_stop()) {
1330 delta = shutdown_time - jiffies_snap;
1331 if (verbose)
1332 printk(KERN_ALERT "%s" TORTURE_FLAG
1333 "rcu_torture_shutdown task: %lu "
1334 "jiffies remaining\n",
1335 torture_type, delta);
1336 schedule_timeout_interruptible(delta);
1337 jiffies_snap = ACCESS_ONCE(jiffies);
1338 }
1339 if (ULONG_CMP_LT(jiffies, shutdown_time)) {
1340 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
1341 return 0;
1342 }
1343
1344 /* OK, shut down the system. */
1345
1346 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
1347 shutdown_task = NULL; /* Avoid self-kill deadlock. */
1348 rcu_torture_cleanup(); /* Get the success/failure message. */
1349 kernel_power_off(); /* Shut down the system. */
1350 return 0;
1351}
1352
8e8be45e
PM
1353static int rcutorture_cpu_notify(struct notifier_block *self,
1354 unsigned long action, void *hcpu)
1355{
1356 long cpu = (long)hcpu;
1357
1358 switch (action) {
1359 case CPU_ONLINE:
1360 case CPU_DOWN_FAILED:
1361 (void)rcutorture_booster_init(cpu);
1362 break;
1363 case CPU_DOWN_PREPARE:
1364 rcutorture_booster_cleanup(cpu);
1365 break;
1366 default:
1367 break;
1368 }
1369 return NOTIFY_OK;
1370}
1371
1372static struct notifier_block rcutorture_cpu_nb = {
1373 .notifier_call = rcutorture_cpu_notify,
1374};
1375
a241ec65
PM
1376static void
1377rcu_torture_cleanup(void)
1378{
1379 int i;
1380
343e9099 1381 mutex_lock(&fullstop_mutex);
4a298656 1382 rcutorture_record_test_transition();
c9d557c1
PM
1383 if (fullstop == FULLSTOP_SHUTDOWN) {
1384 printk(KERN_WARNING /* but going down anyway, so... */
1385 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
343e9099 1386 mutex_unlock(&fullstop_mutex);
c9d557c1 1387 schedule_timeout_uninterruptible(10);
343e9099
PM
1388 if (cur_ops->cb_barrier != NULL)
1389 cur_ops->cb_barrier();
1390 return;
1391 }
c9d557c1 1392 fullstop = FULLSTOP_RMMOD;
343e9099 1393 mutex_unlock(&fullstop_mutex);
8e8be45e 1394 unregister_reboot_notifier(&rcutorture_shutdown_nb);
d120f65f
PM
1395 if (stutter_task) {
1396 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1397 kthread_stop(stutter_task);
1398 }
1399 stutter_task = NULL;
c8e5b163 1400 if (shuffler_task) {
d84f5203
SV
1401 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1402 kthread_stop(shuffler_task);
73d0a4b1 1403 free_cpumask_var(shuffle_tmp_mask);
d84f5203
SV
1404 }
1405 shuffler_task = NULL;
1406
c8e5b163 1407 if (writer_task) {
a241ec65
PM
1408 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1409 kthread_stop(writer_task);
1410 }
1411 writer_task = NULL;
1412
c8e5b163 1413 if (reader_tasks) {
a241ec65 1414 for (i = 0; i < nrealreaders; i++) {
c8e5b163 1415 if (reader_tasks[i]) {
a241ec65
PM
1416 VERBOSE_PRINTK_STRING(
1417 "Stopping rcu_torture_reader task");
1418 kthread_stop(reader_tasks[i]);
1419 }
1420 reader_tasks[i] = NULL;
1421 }
1422 kfree(reader_tasks);
1423 reader_tasks = NULL;
1424 }
1425 rcu_torture_current = NULL;
1426
c8e5b163 1427 if (fakewriter_tasks) {
b772e1dd 1428 for (i = 0; i < nfakewriters; i++) {
c8e5b163 1429 if (fakewriter_tasks[i]) {
b772e1dd
JT
1430 VERBOSE_PRINTK_STRING(
1431 "Stopping rcu_torture_fakewriter task");
1432 kthread_stop(fakewriter_tasks[i]);
1433 }
1434 fakewriter_tasks[i] = NULL;
1435 }
1436 kfree(fakewriter_tasks);
1437 fakewriter_tasks = NULL;
1438 }
1439
c8e5b163 1440 if (stats_task) {
a241ec65
PM
1441 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1442 kthread_stop(stats_task);
1443 }
1444 stats_task = NULL;
1445
bf66f18e
PM
1446 if (fqs_task) {
1447 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1448 kthread_stop(fqs_task);
1449 }
1450 fqs_task = NULL;
8e8be45e
PM
1451 if ((test_boost == 1 && cur_ops->can_boost) ||
1452 test_boost == 2) {
1453 unregister_cpu_notifier(&rcutorture_cpu_nb);
1454 for_each_possible_cpu(i)
1455 rcutorture_booster_cleanup(i);
1456 }
d5f546d8
PM
1457 if (shutdown_task != NULL) {
1458 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
1459 kthread_stop(shutdown_task);
1460 }
bf66f18e 1461
a241ec65 1462 /* Wait for all RCU callbacks to fire. */
2326974d
PM
1463
1464 if (cur_ops->cb_barrier != NULL)
1465 cur_ops->cb_barrier();
a241ec65 1466
a241ec65 1467 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
72e9bb54 1468
c8e5b163 1469 if (cur_ops->cleanup)
72e9bb54 1470 cur_ops->cleanup();
95c38322 1471 if (atomic_read(&n_rcu_torture_error))
8e8be45e 1472 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
95c38322 1473 else
8e8be45e 1474 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
a241ec65
PM
1475}
1476
6f8bc500 1477static int __init
a241ec65
PM
1478rcu_torture_init(void)
1479{
1480 int i;
1481 int cpu;
1482 int firsterr = 0;
ade5fb81 1483 static struct rcu_torture_ops *torture_ops[] =
d9a3da06 1484 { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
bdf2a436 1485 &rcu_bh_ops, &rcu_bh_sync_ops, &rcu_bh_expedited_ops,
804bb837
PM
1486 &srcu_ops, &srcu_expedited_ops,
1487 &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
a241ec65 1488
343e9099
PM
1489 mutex_lock(&fullstop_mutex);
1490
a241ec65 1491 /* Process args and tell the world that the torturer is on the job. */
ade5fb81 1492 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
72e9bb54 1493 cur_ops = torture_ops[i];
ade5fb81 1494 if (strcmp(torture_type, cur_ops->name) == 0)
72e9bb54 1495 break;
72e9bb54 1496 }
ade5fb81 1497 if (i == ARRAY_SIZE(torture_ops)) {
cf886c44 1498 printk(KERN_ALERT "rcu-torture: invalid torture type: \"%s\"\n",
72e9bb54 1499 torture_type);
cf886c44
PM
1500 printk(KERN_ALERT "rcu-torture types:");
1501 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
1502 printk(KERN_ALERT " %s", torture_ops[i]->name);
1503 printk(KERN_ALERT "\n");
343e9099 1504 mutex_unlock(&fullstop_mutex);
a71fca58 1505 return -EINVAL;
72e9bb54 1506 }
bf66f18e
PM
1507 if (cur_ops->fqs == NULL && fqs_duration != 0) {
1508 printk(KERN_ALERT "rcu-torture: ->fqs NULL and non-zero "
1509 "fqs_duration, fqs disabled.\n");
1510 fqs_duration = 0;
1511 }
c8e5b163 1512 if (cur_ops->init)
72e9bb54
PM
1513 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1514
a241ec65
PM
1515 if (nreaders >= 0)
1516 nrealreaders = nreaders;
1517 else
1518 nrealreaders = 2 * num_online_cpus();
8e8be45e 1519 rcu_torture_print_module_parms(cur_ops, "Start of test");
c9d557c1 1520 fullstop = FULLSTOP_DONTSTOP;
a241ec65
PM
1521
1522 /* Set up the freelist. */
1523
1524 INIT_LIST_HEAD(&rcu_torture_freelist);
788e770e 1525 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
996417d2 1526 rcu_tortures[i].rtort_mbtest = 0;
a241ec65
PM
1527 list_add_tail(&rcu_tortures[i].rtort_free,
1528 &rcu_torture_freelist);
1529 }
1530
1531 /* Initialize the statistics so that each run gets its own numbers. */
1532
1533 rcu_torture_current = NULL;
1534 rcu_torture_current_version = 0;
1535 atomic_set(&n_rcu_torture_alloc, 0);
1536 atomic_set(&n_rcu_torture_alloc_fail, 0);
1537 atomic_set(&n_rcu_torture_free, 0);
996417d2
PM
1538 atomic_set(&n_rcu_torture_mberror, 0);
1539 atomic_set(&n_rcu_torture_error, 0);
8e8be45e
PM
1540 n_rcu_torture_boost_ktrerror = 0;
1541 n_rcu_torture_boost_rterror = 0;
8e8be45e
PM
1542 n_rcu_torture_boost_failure = 0;
1543 n_rcu_torture_boosts = 0;
a241ec65
PM
1544 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1545 atomic_set(&rcu_torture_wcount[i], 0);
0a945022 1546 for_each_possible_cpu(cpu) {
a241ec65
PM
1547 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1548 per_cpu(rcu_torture_count, cpu)[i] = 0;
1549 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1550 }
1551 }
1552
1553 /* Start up the kthreads. */
1554
1555 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1556 writer_task = kthread_run(rcu_torture_writer, NULL,
1557 "rcu_torture_writer");
1558 if (IS_ERR(writer_task)) {
1559 firsterr = PTR_ERR(writer_task);
1560 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1561 writer_task = NULL;
1562 goto unwind;
1563 }
b772e1dd 1564 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
a71fca58 1565 GFP_KERNEL);
b772e1dd
JT
1566 if (fakewriter_tasks == NULL) {
1567 VERBOSE_PRINTK_ERRSTRING("out of memory");
1568 firsterr = -ENOMEM;
1569 goto unwind;
1570 }
1571 for (i = 0; i < nfakewriters; i++) {
1572 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1573 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
a71fca58 1574 "rcu_torture_fakewriter");
b772e1dd
JT
1575 if (IS_ERR(fakewriter_tasks[i])) {
1576 firsterr = PTR_ERR(fakewriter_tasks[i]);
1577 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1578 fakewriter_tasks[i] = NULL;
1579 goto unwind;
1580 }
1581 }
2860aaba 1582 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
a241ec65
PM
1583 GFP_KERNEL);
1584 if (reader_tasks == NULL) {
1585 VERBOSE_PRINTK_ERRSTRING("out of memory");
1586 firsterr = -ENOMEM;
1587 goto unwind;
1588 }
1589 for (i = 0; i < nrealreaders; i++) {
1590 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1591 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1592 "rcu_torture_reader");
1593 if (IS_ERR(reader_tasks[i])) {
1594 firsterr = PTR_ERR(reader_tasks[i]);
1595 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1596 reader_tasks[i] = NULL;
1597 goto unwind;
1598 }
1599 }
1600 if (stat_interval > 0) {
1601 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1602 stats_task = kthread_run(rcu_torture_stats, NULL,
1603 "rcu_torture_stats");
1604 if (IS_ERR(stats_task)) {
1605 firsterr = PTR_ERR(stats_task);
1606 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1607 stats_task = NULL;
1608 goto unwind;
1609 }
1610 }
d84f5203
SV
1611 if (test_no_idle_hz) {
1612 rcu_idle_cpu = num_online_cpus() - 1;
73d0a4b1
RR
1613
1614 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
1615 firsterr = -ENOMEM;
1616 VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
1617 goto unwind;
1618 }
1619
d84f5203
SV
1620 /* Create the shuffler thread */
1621 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1622 "rcu_torture_shuffle");
1623 if (IS_ERR(shuffler_task)) {
73d0a4b1 1624 free_cpumask_var(shuffle_tmp_mask);
d84f5203
SV
1625 firsterr = PTR_ERR(shuffler_task);
1626 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1627 shuffler_task = NULL;
1628 goto unwind;
1629 }
1630 }
d120f65f
PM
1631 if (stutter < 0)
1632 stutter = 0;
1633 if (stutter) {
1634 /* Create the stutter thread */
1635 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1636 "rcu_torture_stutter");
1637 if (IS_ERR(stutter_task)) {
1638 firsterr = PTR_ERR(stutter_task);
1639 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1640 stutter_task = NULL;
1641 goto unwind;
1642 }
1643 }
bf66f18e
PM
1644 if (fqs_duration < 0)
1645 fqs_duration = 0;
1646 if (fqs_duration) {
1647 /* Create the stutter thread */
1648 fqs_task = kthread_run(rcu_torture_fqs, NULL,
1649 "rcu_torture_fqs");
1650 if (IS_ERR(fqs_task)) {
1651 firsterr = PTR_ERR(fqs_task);
1652 VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
1653 fqs_task = NULL;
1654 goto unwind;
1655 }
1656 }
8e8be45e
PM
1657 if (test_boost_interval < 1)
1658 test_boost_interval = 1;
1659 if (test_boost_duration < 2)
1660 test_boost_duration = 2;
1661 if ((test_boost == 1 && cur_ops->can_boost) ||
1662 test_boost == 2) {
1663 int retval;
1664
1665 boost_starttime = jiffies + test_boost_interval * HZ;
1666 register_cpu_notifier(&rcutorture_cpu_nb);
1667 for_each_possible_cpu(i) {
1668 if (cpu_is_offline(i))
1669 continue; /* Heuristic: CPU can go offline. */
1670 retval = rcutorture_booster_init(i);
1671 if (retval < 0) {
1672 firsterr = retval;
1673 goto unwind;
1674 }
1675 }
1676 }
d5f546d8
PM
1677 if (shutdown_secs > 0) {
1678 shutdown_time = jiffies + shutdown_secs * HZ;
1679 shutdown_task = kthread_run(rcu_torture_shutdown, NULL,
1680 "rcu_torture_shutdown");
1681 if (IS_ERR(shutdown_task)) {
1682 firsterr = PTR_ERR(shutdown_task);
1683 VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
1684 shutdown_task = NULL;
1685 goto unwind;
1686 }
1687 }
8e8be45e 1688 register_reboot_notifier(&rcutorture_shutdown_nb);
4a298656 1689 rcutorture_record_test_transition();
343e9099 1690 mutex_unlock(&fullstop_mutex);
a241ec65
PM
1691 return 0;
1692
1693unwind:
343e9099 1694 mutex_unlock(&fullstop_mutex);
a241ec65
PM
1695 rcu_torture_cleanup();
1696 return firsterr;
1697}
1698
1699module_init(rcu_torture_init);
1700module_exit(rcu_torture_cleanup);