rcu: Add duplicate-callback tests to rcutorture
[deliverable/linux.git] / kernel / rcutorture.c
CommitLineData
a241ec65 1/*
29766f1e 2 * Read-Copy Update module-based torture test facility
a241ec65
PM
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
b772e1dd 18 * Copyright (C) IBM Corporation, 2005, 2006
a241ec65
PM
19 *
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
a71fca58 21 * Josh Triplett <josh@freedesktop.org>
a241ec65
PM
22 *
23 * See also: Documentation/RCU/torture.txt
24 */
25#include <linux/types.h>
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/kthread.h>
30#include <linux/err.h>
31#include <linux/spinlock.h>
32#include <linux/smp.h>
33#include <linux/rcupdate.h>
34#include <linux/interrupt.h>
35#include <linux/sched.h>
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>
52494535 49#include <linux/trace_clock.h>
f07767fd 50#include <asm/byteorder.h>
a241ec65
PM
51
52MODULE_LICENSE("GPL");
5cf05ad7 53MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and 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 */
ab840f7a
PM
57static int stat_interval = 60; /* Interval between stats, in seconds. */
58 /* Zero means "only at end of test". */
d8e8ed95 59static bool verbose; /* Print more debug info. */
ab840f7a
PM
60static bool test_no_idle_hz = true;
61 /* Test RCU support for tickless idle CPUs. */
d120f65f
PM
62static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
63static int stutter = 5; /* Start/stop testing interval (in sec) */
0729fbf3 64static int irqreader = 1; /* RCU readers from irq (timers). */
d5f546d8
PM
65static int fqs_duration; /* Duration of bursts (us), 0 to disable. */
66static int fqs_holdoff; /* Hold time within burst (us). */
bf66f18e 67static int fqs_stutter = 3; /* Wait time between bursts (s). */
fae4b54f 68static int n_barrier_cbs; /* Number of callbacks to test RCU barriers. */
d2818df1 69static int object_debug; /* Test object-debug double call_rcu()?. */
b58bdcca 70static int onoff_interval; /* Wait time between CPU hotplugs, 0=disable. */
9b9ec9b9 71static int onoff_holdoff; /* Seconds after boot before CPU hotplugs. */
d5f546d8 72static int shutdown_secs; /* Shutdown time (s). <=0 for no shutdown. */
c13f3757
PM
73static int stall_cpu; /* CPU-stall duration (s). 0 for no stall. */
74static int stall_cpu_holdoff = 10; /* Time to wait until stall (s). */
8e8be45e
PM
75static int test_boost = 1; /* Test RCU prio boost: 0=no, 1=maybe, 2=yes. */
76static int test_boost_interval = 7; /* Interval between boost tests, seconds. */
77static int test_boost_duration = 4; /* Duration of each boost test, seconds. */
20d2e428 78static char *torture_type = "rcu"; /* What RCU implementation to torture. */
a241ec65 79
d6ad6711 80module_param(nreaders, int, 0444);
a241ec65 81MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
d6ad6711 82module_param(nfakewriters, int, 0444);
b772e1dd 83MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
3721bc1d 84module_param(stat_interval, int, 0644);
a241ec65 85MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
d6ad6711 86module_param(verbose, bool, 0444);
a241ec65 87MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
d6ad6711 88module_param(test_no_idle_hz, bool, 0444);
d84f5203 89MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
d6ad6711 90module_param(shuffle_interval, int, 0444);
d84f5203 91MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
d120f65f
PM
92module_param(stutter, int, 0444);
93MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
0729fbf3
PM
94module_param(irqreader, int, 0444);
95MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
bf66f18e
PM
96module_param(fqs_duration, int, 0444);
97MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
98module_param(fqs_holdoff, int, 0444);
99MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
100module_param(fqs_stutter, int, 0444);
101MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
fae4b54f
PM
102module_param(n_barrier_cbs, int, 0444);
103MODULE_PARM_DESC(n_barrier_cbs, "# of callbacks/kthreads for barrier testing");
d2818df1
PM
104module_param(object_debug, int, 0444);
105MODULE_PARM_DESC(object_debug, "Enable debug-object double call_rcu() testing");
b58bdcca
PM
106module_param(onoff_interval, int, 0444);
107MODULE_PARM_DESC(onoff_interval, "Time between CPU hotplugs (s), 0=disable");
9b9ec9b9
PM
108module_param(onoff_holdoff, int, 0444);
109MODULE_PARM_DESC(onoff_holdoff, "Time after boot before CPU hotplugs (s)");
d5f546d8
PM
110module_param(shutdown_secs, int, 0444);
111MODULE_PARM_DESC(shutdown_secs, "Shutdown time (s), zero to disable.");
c13f3757
PM
112module_param(stall_cpu, int, 0444);
113MODULE_PARM_DESC(stall_cpu, "Stall duration (s), zero to disable.");
114module_param(stall_cpu_holdoff, int, 0444);
115MODULE_PARM_DESC(stall_cpu_holdoff, "Time to wait before starting stall (s).");
8e8be45e
PM
116module_param(test_boost, int, 0444);
117MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
118module_param(test_boost_interval, int, 0444);
119MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
120module_param(test_boost_duration, int, 0444);
121MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
d6ad6711 122module_param(torture_type, charp, 0444);
b2896d2e 123MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
72e9bb54
PM
124
125#define TORTURE_FLAG "-torture:"
a241ec65 126#define PRINTK_STRING(s) \
2caa1e44 127 do { pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
a241ec65 128#define VERBOSE_PRINTK_STRING(s) \
2caa1e44 129 do { if (verbose) pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
a241ec65 130#define VERBOSE_PRINTK_ERRSTRING(s) \
2caa1e44 131 do { if (verbose) pr_alert("%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
a241ec65
PM
132
133static char printk_buf[4096];
134
135static int nrealreaders;
136static struct task_struct *writer_task;
b772e1dd 137static struct task_struct **fakewriter_tasks;
a241ec65
PM
138static struct task_struct **reader_tasks;
139static struct task_struct *stats_task;
d84f5203 140static struct task_struct *shuffler_task;
d120f65f 141static struct task_struct *stutter_task;
bf66f18e 142static struct task_struct *fqs_task;
8e8be45e 143static struct task_struct *boost_tasks[NR_CPUS];
d5f546d8 144static struct task_struct *shutdown_task;
b58bdcca
PM
145#ifdef CONFIG_HOTPLUG_CPU
146static struct task_struct *onoff_task;
147#endif /* #ifdef CONFIG_HOTPLUG_CPU */
c13f3757 148static struct task_struct *stall_task;
fae4b54f
PM
149static struct task_struct **barrier_cbs_tasks;
150static struct task_struct *barrier_task;
a241ec65
PM
151
152#define RCU_TORTURE_PIPE_LEN 10
153
154struct rcu_torture {
155 struct rcu_head rtort_rcu;
156 int rtort_pipe_count;
157 struct list_head rtort_free;
996417d2 158 int rtort_mbtest;
a241ec65
PM
159};
160
a241ec65 161static LIST_HEAD(rcu_torture_freelist);
0ddea0ea 162static struct rcu_torture __rcu *rcu_torture_current;
4a298656 163static unsigned long rcu_torture_current_version;
a241ec65
PM
164static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
165static DEFINE_SPINLOCK(rcu_torture_lock);
166static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
167 { 0 };
168static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
169 { 0 };
170static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
b2896d2e
PM
171static atomic_t n_rcu_torture_alloc;
172static atomic_t n_rcu_torture_alloc_fail;
173static atomic_t n_rcu_torture_free;
174static atomic_t n_rcu_torture_mberror;
175static atomic_t n_rcu_torture_error;
fae4b54f 176static long n_rcu_torture_barrier_error;
8e8be45e
PM
177static long n_rcu_torture_boost_ktrerror;
178static long n_rcu_torture_boost_rterror;
8e8be45e
PM
179static long n_rcu_torture_boost_failure;
180static long n_rcu_torture_boosts;
a71fca58 181static long n_rcu_torture_timers;
b58bdcca
PM
182static long n_offline_attempts;
183static long n_offline_successes;
13dbf914
PM
184static unsigned long sum_offline;
185static int min_offline = -1;
186static int max_offline;
b58bdcca
PM
187static long n_online_attempts;
188static long n_online_successes;
13dbf914
PM
189static unsigned long sum_online;
190static int min_online = -1;
191static int max_online;
fae4b54f
PM
192static long n_barrier_attempts;
193static long n_barrier_successes;
e3033736 194static struct list_head rcu_torture_removed;
73d0a4b1 195static cpumask_var_t shuffle_tmp_mask;
a241ec65 196
a71fca58 197static int stutter_pause_test;
d120f65f 198
31a72bce
PM
199#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
200#define RCUTORTURE_RUNNABLE_INIT 1
201#else
202#define RCUTORTURE_RUNNABLE_INIT 0
203#endif
204int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
bb3bf705
PM
205module_param(rcutorture_runnable, int, 0444);
206MODULE_PARM_DESC(rcutorture_runnable, "Start rcutorture at boot");
31a72bce 207
3acf4a9a 208#if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
8e8be45e 209#define rcu_can_boost() 1
3acf4a9a 210#else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
8e8be45e 211#define rcu_can_boost() 0
3acf4a9a 212#endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
8e8be45e 213
e4aa0da3
SR
214#ifdef CONFIG_RCU_TRACE
215static u64 notrace rcu_trace_clock_local(void)
216{
217 u64 ts = trace_clock_local();
218 unsigned long __maybe_unused ts_rem = do_div(ts, NSEC_PER_USEC);
219 return ts;
220}
221#else /* #ifdef CONFIG_RCU_TRACE */
222static u64 notrace rcu_trace_clock_local(void)
223{
224 return 0ULL;
225}
226#endif /* #else #ifdef CONFIG_RCU_TRACE */
227
d5f546d8 228static unsigned long shutdown_time; /* jiffies to system shutdown. */
8e8be45e
PM
229static unsigned long boost_starttime; /* jiffies of next boost test start. */
230DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
231 /* and boost task create/destroy. */
fae4b54f 232static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
c6ebcbb6 233static bool barrier_phase; /* Test phase. */
fae4b54f
PM
234static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
235static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
236static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
8e8be45e 237
c9d557c1
PM
238/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
239
240#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
241#define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
242#define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
243static int fullstop = FULLSTOP_RMMOD;
0ddea0ea
PM
244/*
245 * Protect fullstop transitions and spawning of kthreads.
246 */
247static DEFINE_MUTEX(fullstop_mutex);
343e9099 248
d5f546d8
PM
249/* Forward reference. */
250static void rcu_torture_cleanup(void);
251
343e9099 252/*
c9d557c1 253 * Detect and respond to a system shutdown.
343e9099
PM
254 */
255static int
256rcutorture_shutdown_notify(struct notifier_block *unused1,
257 unsigned long unused2, void *unused3)
258{
c59ab97e 259 mutex_lock(&fullstop_mutex);
c9d557c1 260 if (fullstop == FULLSTOP_DONTSTOP)
c59ab97e 261 fullstop = FULLSTOP_SHUTDOWN;
c9d557c1 262 else
2caa1e44 263 pr_warn(/* but going down anyway, so... */
c9d557c1 264 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
c59ab97e 265 mutex_unlock(&fullstop_mutex);
343e9099
PM
266 return NOTIFY_DONE;
267}
268
c9d557c1
PM
269/*
270 * Absorb kthreads into a kernel function that won't return, so that
271 * they won't ever access module text or data again.
272 */
e66c33d5 273static void rcutorture_shutdown_absorb(const char *title)
c9d557c1
PM
274{
275 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
2caa1e44 276 pr_notice(
c9d557c1
PM
277 "rcutorture thread %s parking due to system shutdown\n",
278 title);
279 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
280 }
281}
282
a241ec65
PM
283/*
284 * Allocate an element from the rcu_tortures pool.
285 */
97a41e26 286static struct rcu_torture *
a241ec65
PM
287rcu_torture_alloc(void)
288{
289 struct list_head *p;
290
adac1665 291 spin_lock_bh(&rcu_torture_lock);
a241ec65
PM
292 if (list_empty(&rcu_torture_freelist)) {
293 atomic_inc(&n_rcu_torture_alloc_fail);
adac1665 294 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
295 return NULL;
296 }
297 atomic_inc(&n_rcu_torture_alloc);
298 p = rcu_torture_freelist.next;
299 list_del_init(p);
adac1665 300 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
301 return container_of(p, struct rcu_torture, rtort_free);
302}
303
304/*
305 * Free an element to the rcu_tortures pool.
306 */
307static void
308rcu_torture_free(struct rcu_torture *p)
309{
310 atomic_inc(&n_rcu_torture_free);
adac1665 311 spin_lock_bh(&rcu_torture_lock);
a241ec65 312 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
adac1665 313 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
314}
315
a241ec65
PM
316struct rcu_random_state {
317 unsigned long rrs_state;
75cfef32 318 long rrs_count;
a241ec65
PM
319};
320
321#define RCU_RANDOM_MULT 39916801 /* prime */
322#define RCU_RANDOM_ADD 479001701 /* prime */
323#define RCU_RANDOM_REFRESH 10000
324
325#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
326
327/*
328 * Crude but fast random-number generator. Uses a linear congruential
c17ac855 329 * generator, with occasional help from cpu_clock().
a241ec65 330 */
75cfef32 331static unsigned long
a241ec65
PM
332rcu_random(struct rcu_random_state *rrsp)
333{
a241ec65 334 if (--rrsp->rrs_count < 0) {
c676329a 335 rrsp->rrs_state += (unsigned long)local_clock();
a241ec65
PM
336 rrsp->rrs_count = RCU_RANDOM_REFRESH;
337 }
338 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
339 return swahw32(rrsp->rrs_state);
340}
341
d120f65f 342static void
e66c33d5 343rcu_stutter_wait(const char *title)
d120f65f 344{
c9d557c1 345 while (stutter_pause_test || !rcutorture_runnable) {
e3d7be27
PM
346 if (rcutorture_runnable)
347 schedule_timeout_interruptible(1);
348 else
3ccf79f4 349 schedule_timeout_interruptible(round_jiffies_relative(HZ));
c9d557c1 350 rcutorture_shutdown_absorb(title);
343e9099 351 }
d120f65f
PM
352}
353
72e9bb54
PM
354/*
355 * Operations vector for selecting different types of tests.
356 */
357
358struct rcu_torture_ops {
359 void (*init)(void);
72e9bb54 360 int (*readlock)(void);
0acc512c 361 void (*read_delay)(struct rcu_random_state *rrsp);
72e9bb54
PM
362 void (*readunlock)(int idx);
363 int (*completed)(void);
0acc512c 364 void (*deferred_free)(struct rcu_torture *p);
b772e1dd 365 void (*sync)(void);
fae4b54f 366 void (*call)(struct rcu_head *head, void (*func)(struct rcu_head *rcu));
2326974d 367 void (*cb_barrier)(void);
bf66f18e 368 void (*fqs)(void);
72e9bb54 369 int (*stats)(char *page);
0acc512c 370 int irq_capable;
8e8be45e 371 int can_boost;
e66c33d5 372 const char *name;
72e9bb54 373};
a71fca58
PM
374
375static struct rcu_torture_ops *cur_ops;
72e9bb54
PM
376
377/*
378 * Definitions for rcu torture testing.
379 */
380
a49a4af7 381static int rcu_torture_read_lock(void) __acquires(RCU)
72e9bb54
PM
382{
383 rcu_read_lock();
384 return 0;
385}
386
b2896d2e
PM
387static void rcu_read_delay(struct rcu_random_state *rrsp)
388{
b8d57a76
JT
389 const unsigned long shortdelay_us = 200;
390 const unsigned long longdelay_ms = 50;
b2896d2e 391
b8d57a76
JT
392 /* We want a short delay sometimes to make a reader delay the grace
393 * period, and we want a long delay occasionally to trigger
394 * force_quiescent_state. */
b2896d2e 395
b8d57a76
JT
396 if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
397 mdelay(longdelay_ms);
398 if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
399 udelay(shortdelay_us);
e546f485
LJ
400#ifdef CONFIG_PREEMPT
401 if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
402 preempt_schedule(); /* No QS if preempt_disable() in effect */
403#endif
b2896d2e
PM
404}
405
a49a4af7 406static void rcu_torture_read_unlock(int idx) __releases(RCU)
72e9bb54
PM
407{
408 rcu_read_unlock();
409}
410
411static int rcu_torture_completed(void)
412{
413 return rcu_batches_completed();
414}
415
416static void
417rcu_torture_cb(struct rcu_head *p)
418{
419 int i;
420 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
421
c9d557c1 422 if (fullstop != FULLSTOP_DONTSTOP) {
72e9bb54
PM
423 /* Test is ending, just drop callbacks on the floor. */
424 /* The next initialization will pick up the pieces. */
425 return;
426 }
427 i = rp->rtort_pipe_count;
428 if (i > RCU_TORTURE_PIPE_LEN)
429 i = RCU_TORTURE_PIPE_LEN;
430 atomic_inc(&rcu_torture_wcount[i]);
431 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
432 rp->rtort_mbtest = 0;
433 rcu_torture_free(rp);
c701d5d9 434 } else {
0acc512c 435 cur_ops->deferred_free(rp);
c701d5d9 436 }
72e9bb54
PM
437}
438
d9a3da06
PM
439static int rcu_no_completed(void)
440{
441 return 0;
442}
443
72e9bb54
PM
444static void rcu_torture_deferred_free(struct rcu_torture *p)
445{
446 call_rcu(&p->rtort_rcu, rcu_torture_cb);
447}
448
449static struct rcu_torture_ops rcu_ops = {
0acc512c 450 .init = NULL,
0acc512c
PM
451 .readlock = rcu_torture_read_lock,
452 .read_delay = rcu_read_delay,
453 .readunlock = rcu_torture_read_unlock,
454 .completed = rcu_torture_completed,
455 .deferred_free = rcu_torture_deferred_free,
456 .sync = synchronize_rcu,
fae4b54f 457 .call = call_rcu,
0acc512c 458 .cb_barrier = rcu_barrier,
bf66f18e 459 .fqs = rcu_force_quiescent_state,
0acc512c 460 .stats = NULL,
a71fca58 461 .irq_capable = 1,
8e8be45e 462 .can_boost = rcu_can_boost(),
a71fca58 463 .name = "rcu"
72e9bb54
PM
464};
465
e3033736
JT
466static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
467{
468 int i;
469 struct rcu_torture *rp;
470 struct rcu_torture *rp1;
471
472 cur_ops->sync();
473 list_add(&p->rtort_free, &rcu_torture_removed);
474 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
475 i = rp->rtort_pipe_count;
476 if (i > RCU_TORTURE_PIPE_LEN)
477 i = RCU_TORTURE_PIPE_LEN;
478 atomic_inc(&rcu_torture_wcount[i]);
479 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
480 rp->rtort_mbtest = 0;
481 list_del(&rp->rtort_free);
482 rcu_torture_free(rp);
483 }
484 }
485}
486
487static void rcu_sync_torture_init(void)
488{
489 INIT_LIST_HEAD(&rcu_torture_removed);
490}
491
20d2e428 492static struct rcu_torture_ops rcu_sync_ops = {
0acc512c 493 .init = rcu_sync_torture_init,
0acc512c
PM
494 .readlock = rcu_torture_read_lock,
495 .read_delay = rcu_read_delay,
496 .readunlock = rcu_torture_read_unlock,
497 .completed = rcu_torture_completed,
498 .deferred_free = rcu_sync_torture_deferred_free,
499 .sync = synchronize_rcu,
fae4b54f 500 .call = NULL,
0acc512c 501 .cb_barrier = NULL,
bf66f18e 502 .fqs = rcu_force_quiescent_state,
0acc512c
PM
503 .stats = NULL,
504 .irq_capable = 1,
8e8be45e 505 .can_boost = rcu_can_boost(),
0acc512c 506 .name = "rcu_sync"
20d2e428
JT
507};
508
d9a3da06
PM
509static struct rcu_torture_ops rcu_expedited_ops = {
510 .init = rcu_sync_torture_init,
d9a3da06
PM
511 .readlock = rcu_torture_read_lock,
512 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
513 .readunlock = rcu_torture_read_unlock,
514 .completed = rcu_no_completed,
515 .deferred_free = rcu_sync_torture_deferred_free,
516 .sync = synchronize_rcu_expedited,
fae4b54f 517 .call = NULL,
d9a3da06 518 .cb_barrier = NULL,
bf66f18e 519 .fqs = rcu_force_quiescent_state,
d9a3da06
PM
520 .stats = NULL,
521 .irq_capable = 1,
8e8be45e 522 .can_boost = rcu_can_boost(),
d9a3da06
PM
523 .name = "rcu_expedited"
524};
525
c32e0660
PM
526/*
527 * Definitions for rcu_bh torture testing.
528 */
529
a49a4af7 530static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
c32e0660
PM
531{
532 rcu_read_lock_bh();
533 return 0;
534}
535
a49a4af7 536static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
c32e0660
PM
537{
538 rcu_read_unlock_bh();
539}
540
541static int rcu_bh_torture_completed(void)
542{
543 return rcu_batches_completed_bh();
544}
545
546static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
547{
548 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
549}
550
551static struct rcu_torture_ops rcu_bh_ops = {
0acc512c 552 .init = NULL,
0acc512c
PM
553 .readlock = rcu_bh_torture_read_lock,
554 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
555 .readunlock = rcu_bh_torture_read_unlock,
556 .completed = rcu_bh_torture_completed,
557 .deferred_free = rcu_bh_torture_deferred_free,
bdf2a436 558 .sync = synchronize_rcu_bh,
fae4b54f 559 .call = call_rcu_bh,
0acc512c 560 .cb_barrier = rcu_barrier_bh,
bf66f18e 561 .fqs = rcu_bh_force_quiescent_state,
0acc512c
PM
562 .stats = NULL,
563 .irq_capable = 1,
564 .name = "rcu_bh"
c32e0660
PM
565};
566
11a14701 567static struct rcu_torture_ops rcu_bh_sync_ops = {
0acc512c 568 .init = rcu_sync_torture_init,
0acc512c
PM
569 .readlock = rcu_bh_torture_read_lock,
570 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
571 .readunlock = rcu_bh_torture_read_unlock,
572 .completed = rcu_bh_torture_completed,
573 .deferred_free = rcu_sync_torture_deferred_free,
bdf2a436 574 .sync = synchronize_rcu_bh,
fae4b54f 575 .call = NULL,
0acc512c 576 .cb_barrier = NULL,
bf66f18e 577 .fqs = rcu_bh_force_quiescent_state,
0acc512c
PM
578 .stats = NULL,
579 .irq_capable = 1,
580 .name = "rcu_bh_sync"
11a14701
JT
581};
582
bdf2a436
PM
583static struct rcu_torture_ops rcu_bh_expedited_ops = {
584 .init = rcu_sync_torture_init,
bdf2a436
PM
585 .readlock = rcu_bh_torture_read_lock,
586 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
587 .readunlock = rcu_bh_torture_read_unlock,
588 .completed = rcu_bh_torture_completed,
589 .deferred_free = rcu_sync_torture_deferred_free,
590 .sync = synchronize_rcu_bh_expedited,
fae4b54f 591 .call = NULL,
bdf2a436
PM
592 .cb_barrier = NULL,
593 .fqs = rcu_bh_force_quiescent_state,
594 .stats = NULL,
595 .irq_capable = 1,
596 .name = "rcu_bh_expedited"
597};
598
b2896d2e
PM
599/*
600 * Definitions for srcu torture testing.
601 */
602
cda4dc81 603DEFINE_STATIC_SRCU(srcu_ctl);
b2896d2e 604
012d3ca8 605static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
b2896d2e
PM
606{
607 return srcu_read_lock(&srcu_ctl);
608}
609
610static void srcu_read_delay(struct rcu_random_state *rrsp)
611{
612 long delay;
613 const long uspertick = 1000000 / HZ;
614 const long longdelay = 10;
615
616 /* We want there to be long-running readers, but not all the time. */
617
618 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
619 if (!delay)
620 schedule_timeout_interruptible(longdelay);
e546f485
LJ
621 else
622 rcu_read_delay(rrsp);
b2896d2e
PM
623}
624
012d3ca8 625static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
b2896d2e
PM
626{
627 srcu_read_unlock(&srcu_ctl, idx);
628}
629
630static int srcu_torture_completed(void)
631{
632 return srcu_batches_completed(&srcu_ctl);
633}
634
9059c940
LJ
635static void srcu_torture_deferred_free(struct rcu_torture *rp)
636{
637 call_srcu(&srcu_ctl, &rp->rtort_rcu, rcu_torture_cb);
638}
639
b772e1dd
JT
640static void srcu_torture_synchronize(void)
641{
642 synchronize_srcu(&srcu_ctl);
643}
644
e3f8d378
PM
645static void srcu_torture_call(struct rcu_head *head,
646 void (*func)(struct rcu_head *head))
647{
648 call_srcu(&srcu_ctl, head, func);
649}
650
651static void srcu_torture_barrier(void)
652{
653 srcu_barrier(&srcu_ctl);
654}
655
b2896d2e
PM
656static int srcu_torture_stats(char *page)
657{
658 int cnt = 0;
659 int cpu;
660 int idx = srcu_ctl.completed & 0x1;
661
662 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
663 torture_type, TORTURE_FLAG, idx);
664 for_each_possible_cpu(cpu) {
cef50120 665 cnt += sprintf(&page[cnt], " %d(%lu,%lu)", cpu,
b2896d2e
PM
666 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
667 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
668 }
669 cnt += sprintf(&page[cnt], "\n");
670 return cnt;
671}
672
673static struct rcu_torture_ops srcu_ops = {
cda4dc81 674 .init = rcu_sync_torture_init,
0acc512c
PM
675 .readlock = srcu_torture_read_lock,
676 .read_delay = srcu_read_delay,
677 .readunlock = srcu_torture_read_unlock,
678 .completed = srcu_torture_completed,
9059c940 679 .deferred_free = srcu_torture_deferred_free,
0acc512c 680 .sync = srcu_torture_synchronize,
e3f8d378
PM
681 .call = srcu_torture_call,
682 .cb_barrier = srcu_torture_barrier,
0acc512c
PM
683 .stats = srcu_torture_stats,
684 .name = "srcu"
b2896d2e
PM
685};
686
9059c940 687static struct rcu_torture_ops srcu_sync_ops = {
cda4dc81 688 .init = rcu_sync_torture_init,
9059c940
LJ
689 .readlock = srcu_torture_read_lock,
690 .read_delay = srcu_read_delay,
691 .readunlock = srcu_torture_read_unlock,
692 .completed = srcu_torture_completed,
693 .deferred_free = rcu_sync_torture_deferred_free,
694 .sync = srcu_torture_synchronize,
695 .call = NULL,
696 .cb_barrier = NULL,
697 .stats = srcu_torture_stats,
698 .name = "srcu_sync"
699};
700
804bb837
PM
701static void srcu_torture_synchronize_expedited(void)
702{
703 synchronize_srcu_expedited(&srcu_ctl);
704}
705
706static struct rcu_torture_ops srcu_expedited_ops = {
cda4dc81 707 .init = rcu_sync_torture_init,
804bb837
PM
708 .readlock = srcu_torture_read_lock,
709 .read_delay = srcu_read_delay,
710 .readunlock = srcu_torture_read_unlock,
711 .completed = srcu_torture_completed,
712 .deferred_free = rcu_sync_torture_deferred_free,
713 .sync = srcu_torture_synchronize_expedited,
fae4b54f 714 .call = NULL,
804bb837
PM
715 .cb_barrier = NULL,
716 .stats = srcu_torture_stats,
717 .name = "srcu_expedited"
718};
719
4b6c2cca
JT
720/*
721 * Definitions for sched torture testing.
722 */
723
724static int sched_torture_read_lock(void)
725{
726 preempt_disable();
727 return 0;
728}
729
730static void sched_torture_read_unlock(int idx)
731{
732 preempt_enable();
733}
734
2326974d
PM
735static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
736{
737 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
738}
739
4b6c2cca 740static struct rcu_torture_ops sched_ops = {
0acc512c 741 .init = rcu_sync_torture_init,
0acc512c
PM
742 .readlock = sched_torture_read_lock,
743 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
744 .readunlock = sched_torture_read_unlock,
d9a3da06 745 .completed = rcu_no_completed,
0acc512c 746 .deferred_free = rcu_sched_torture_deferred_free,
bdf2a436 747 .sync = synchronize_sched,
0acc512c 748 .cb_barrier = rcu_barrier_sched,
bf66f18e 749 .fqs = rcu_sched_force_quiescent_state,
0acc512c
PM
750 .stats = NULL,
751 .irq_capable = 1,
752 .name = "sched"
4b6c2cca
JT
753};
754
804bb837 755static struct rcu_torture_ops sched_sync_ops = {
0acc512c 756 .init = rcu_sync_torture_init,
0acc512c
PM
757 .readlock = sched_torture_read_lock,
758 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
759 .readunlock = sched_torture_read_unlock,
d9a3da06 760 .completed = rcu_no_completed,
0acc512c 761 .deferred_free = rcu_sync_torture_deferred_free,
bdf2a436 762 .sync = synchronize_sched,
0acc512c 763 .cb_barrier = NULL,
bf66f18e 764 .fqs = rcu_sched_force_quiescent_state,
0acc512c
PM
765 .stats = NULL,
766 .name = "sched_sync"
767};
768
0acc512c
PM
769static struct rcu_torture_ops sched_expedited_ops = {
770 .init = rcu_sync_torture_init,
0acc512c
PM
771 .readlock = sched_torture_read_lock,
772 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
773 .readunlock = sched_torture_read_unlock,
d9a3da06 774 .completed = rcu_no_completed,
0acc512c
PM
775 .deferred_free = rcu_sync_torture_deferred_free,
776 .sync = synchronize_sched_expedited,
777 .cb_barrier = NULL,
bf66f18e 778 .fqs = rcu_sched_force_quiescent_state,
969c7921 779 .stats = NULL,
0acc512c
PM
780 .irq_capable = 1,
781 .name = "sched_expedited"
2326974d
PM
782};
783
8e8be45e
PM
784/*
785 * RCU torture priority-boost testing. Runs one real-time thread per
786 * CPU for moderate bursts, repeatedly registering RCU callbacks and
787 * spinning waiting for them to be invoked. If a given callback takes
788 * too long to be invoked, we assume that priority inversion has occurred.
789 */
790
791struct rcu_boost_inflight {
792 struct rcu_head rcu;
793 int inflight;
794};
795
796static void rcu_torture_boost_cb(struct rcu_head *head)
797{
798 struct rcu_boost_inflight *rbip =
799 container_of(head, struct rcu_boost_inflight, rcu);
800
801 smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
802 rbip->inflight = 0;
803}
804
805static int rcu_torture_boost(void *arg)
806{
807 unsigned long call_rcu_time;
808 unsigned long endtime;
809 unsigned long oldstarttime;
810 struct rcu_boost_inflight rbi = { .inflight = 0 };
811 struct sched_param sp;
812
813 VERBOSE_PRINTK_STRING("rcu_torture_boost started");
814
815 /* Set real-time priority. */
816 sp.sched_priority = 1;
817 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
818 VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
819 n_rcu_torture_boost_rterror++;
820 }
821
561190e3 822 init_rcu_head_on_stack(&rbi.rcu);
8e8be45e
PM
823 /* Each pass through the following loop does one boost-test cycle. */
824 do {
825 /* Wait for the next test interval. */
826 oldstarttime = boost_starttime;
93898fb1 827 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
0e11c8e8 828 schedule_timeout_interruptible(oldstarttime - jiffies);
8e8be45e
PM
829 rcu_stutter_wait("rcu_torture_boost");
830 if (kthread_should_stop() ||
831 fullstop != FULLSTOP_DONTSTOP)
832 goto checkwait;
833 }
834
835 /* Do one boost-test interval. */
836 endtime = oldstarttime + test_boost_duration * HZ;
837 call_rcu_time = jiffies;
93898fb1 838 while (ULONG_CMP_LT(jiffies, endtime)) {
8e8be45e
PM
839 /* If we don't have a callback in flight, post one. */
840 if (!rbi.inflight) {
841 smp_mb(); /* RCU core before ->inflight = 1. */
842 rbi.inflight = 1;
843 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
844 if (jiffies - call_rcu_time >
845 test_boost_duration * HZ - HZ / 2) {
846 VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
847 n_rcu_torture_boost_failure++;
848 }
849 call_rcu_time = jiffies;
850 }
851 cond_resched();
852 rcu_stutter_wait("rcu_torture_boost");
853 if (kthread_should_stop() ||
854 fullstop != FULLSTOP_DONTSTOP)
855 goto checkwait;
856 }
857
858 /*
859 * Set the start time of the next test interval.
860 * Yes, this is vulnerable to long delays, but such
861 * delays simply cause a false negative for the next
862 * interval. Besides, we are running at RT priority,
863 * so delays should be relatively rare.
864 */
ab8f11e5
PM
865 while (oldstarttime == boost_starttime &&
866 !kthread_should_stop()) {
8e8be45e
PM
867 if (mutex_trylock(&boost_mutex)) {
868 boost_starttime = jiffies +
869 test_boost_interval * HZ;
870 n_rcu_torture_boosts++;
871 mutex_unlock(&boost_mutex);
872 break;
873 }
874 schedule_timeout_uninterruptible(1);
875 }
876
877 /* Go do the stutter. */
878checkwait: rcu_stutter_wait("rcu_torture_boost");
879 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
880
881 /* Clean up and exit. */
882 VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
883 rcutorture_shutdown_absorb("rcu_torture_boost");
884 while (!kthread_should_stop() || rbi.inflight)
885 schedule_timeout_uninterruptible(1);
886 smp_mb(); /* order accesses to ->inflight before stack-frame death. */
9d68197c 887 destroy_rcu_head_on_stack(&rbi.rcu);
8e8be45e
PM
888 return 0;
889}
890
bf66f18e
PM
891/*
892 * RCU torture force-quiescent-state kthread. Repeatedly induces
893 * bursts of calls to force_quiescent_state(), increasing the probability
894 * of occurrence of some important types of race conditions.
895 */
896static int
897rcu_torture_fqs(void *arg)
898{
899 unsigned long fqs_resume_time;
900 int fqs_burst_remaining;
901
902 VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
903 do {
904 fqs_resume_time = jiffies + fqs_stutter * HZ;
93898fb1
PM
905 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
906 !kthread_should_stop()) {
bf66f18e
PM
907 schedule_timeout_interruptible(1);
908 }
909 fqs_burst_remaining = fqs_duration;
93898fb1
PM
910 while (fqs_burst_remaining > 0 &&
911 !kthread_should_stop()) {
bf66f18e
PM
912 cur_ops->fqs();
913 udelay(fqs_holdoff);
914 fqs_burst_remaining -= fqs_holdoff;
915 }
916 rcu_stutter_wait("rcu_torture_fqs");
917 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
918 VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
919 rcutorture_shutdown_absorb("rcu_torture_fqs");
920 while (!kthread_should_stop())
921 schedule_timeout_uninterruptible(1);
922 return 0;
923}
924
a241ec65
PM
925/*
926 * RCU torture writer kthread. Repeatedly substitutes a new structure
927 * for that pointed to by rcu_torture_current, freeing the old structure
928 * after a series of grace periods (the "pipeline").
929 */
930static int
931rcu_torture_writer(void *arg)
932{
933 int i;
934 long oldbatch = rcu_batches_completed();
935 struct rcu_torture *rp;
936 struct rcu_torture *old_rp;
937 static DEFINE_RCU_RANDOM(rand);
938
939 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
dbdf65b1
IM
940 set_user_nice(current, 19);
941
a241ec65
PM
942 do {
943 schedule_timeout_uninterruptible(1);
a71fca58
PM
944 rp = rcu_torture_alloc();
945 if (rp == NULL)
a241ec65
PM
946 continue;
947 rp->rtort_pipe_count = 0;
948 udelay(rcu_random(&rand) & 0x3ff);
0ddea0ea
PM
949 old_rp = rcu_dereference_check(rcu_torture_current,
950 current == writer_task);
996417d2 951 rp->rtort_mbtest = 1;
a241ec65 952 rcu_assign_pointer(rcu_torture_current, rp);
9b2619af 953 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
c8e5b163 954 if (old_rp) {
a241ec65
PM
955 i = old_rp->rtort_pipe_count;
956 if (i > RCU_TORTURE_PIPE_LEN)
957 i = RCU_TORTURE_PIPE_LEN;
958 atomic_inc(&rcu_torture_wcount[i]);
959 old_rp->rtort_pipe_count++;
0acc512c 960 cur_ops->deferred_free(old_rp);
a241ec65 961 }
4a298656 962 rcutorture_record_progress(++rcu_torture_current_version);
72e9bb54 963 oldbatch = cur_ops->completed();
c9d557c1
PM
964 rcu_stutter_wait("rcu_torture_writer");
965 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
a241ec65 966 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
c9d557c1
PM
967 rcutorture_shutdown_absorb("rcu_torture_writer");
968 while (!kthread_should_stop())
a241ec65
PM
969 schedule_timeout_uninterruptible(1);
970 return 0;
971}
972
b772e1dd
JT
973/*
974 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
975 * delay between calls.
976 */
977static int
978rcu_torture_fakewriter(void *arg)
979{
980 DEFINE_RCU_RANDOM(rand);
981
982 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
983 set_user_nice(current, 19);
984
985 do {
986 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
987 udelay(rcu_random(&rand) & 0x3ff);
72472a02
PM
988 if (cur_ops->cb_barrier != NULL &&
989 rcu_random(&rand) % (nfakewriters * 8) == 0)
990 cur_ops->cb_barrier();
991 else
992 cur_ops->sync();
c9d557c1
PM
993 rcu_stutter_wait("rcu_torture_fakewriter");
994 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
b772e1dd
JT
995
996 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
c9d557c1
PM
997 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
998 while (!kthread_should_stop())
b772e1dd
JT
999 schedule_timeout_uninterruptible(1);
1000 return 0;
1001}
1002
91afaf30
PM
1003void rcutorture_trace_dump(void)
1004{
1005 static atomic_t beenhere = ATOMIC_INIT(0);
1006
1007 if (atomic_read(&beenhere))
1008 return;
1009 if (atomic_xchg(&beenhere, 1) != 0)
1010 return;
91afaf30
PM
1011 ftrace_dump(DUMP_ALL);
1012}
1013
0729fbf3
PM
1014/*
1015 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
1016 * incrementing the corresponding element of the pipeline array. The
1017 * counter in the element should never be greater than 1, otherwise, the
1018 * RCU implementation is broken.
1019 */
1020static void rcu_torture_timer(unsigned long unused)
1021{
1022 int idx;
1023 int completed;
52494535 1024 int completed_end;
0729fbf3
PM
1025 static DEFINE_RCU_RANDOM(rand);
1026 static DEFINE_SPINLOCK(rand_lock);
1027 struct rcu_torture *p;
1028 int pipe_count;
52494535 1029 unsigned long long ts;
0729fbf3
PM
1030
1031 idx = cur_ops->readlock();
1032 completed = cur_ops->completed();
e4aa0da3 1033 ts = rcu_trace_clock_local();
632ee200 1034 p = rcu_dereference_check(rcu_torture_current,
632ee200
PM
1035 rcu_read_lock_bh_held() ||
1036 rcu_read_lock_sched_held() ||
1037 srcu_read_lock_held(&srcu_ctl));
0729fbf3
PM
1038 if (p == NULL) {
1039 /* Leave because rcu_torture_writer is not yet underway */
1040 cur_ops->readunlock(idx);
1041 return;
1042 }
1043 if (p->rtort_mbtest == 0)
1044 atomic_inc(&n_rcu_torture_mberror);
1045 spin_lock(&rand_lock);
0acc512c 1046 cur_ops->read_delay(&rand);
0729fbf3
PM
1047 n_rcu_torture_timers++;
1048 spin_unlock(&rand_lock);
1049 preempt_disable();
1050 pipe_count = p->rtort_pipe_count;
1051 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1052 /* Should not happen, but... */
1053 pipe_count = RCU_TORTURE_PIPE_LEN;
1054 }
52494535
PM
1055 completed_end = cur_ops->completed();
1056 if (pipe_count > 1) {
52494535
PM
1057 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
1058 completed, completed_end);
91afaf30 1059 rcutorture_trace_dump();
52494535 1060 }
dd17c8f7 1061 __this_cpu_inc(rcu_torture_count[pipe_count]);
52494535 1062 completed = completed_end - completed;
0729fbf3
PM
1063 if (completed > RCU_TORTURE_PIPE_LEN) {
1064 /* Should not happen, but... */
1065 completed = RCU_TORTURE_PIPE_LEN;
1066 }
dd17c8f7 1067 __this_cpu_inc(rcu_torture_batch[completed]);
0729fbf3
PM
1068 preempt_enable();
1069 cur_ops->readunlock(idx);
1070}
1071
a241ec65
PM
1072/*
1073 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
1074 * incrementing the corresponding element of the pipeline array. The
1075 * counter in the element should never be greater than 1, otherwise, the
1076 * RCU implementation is broken.
1077 */
1078static int
1079rcu_torture_reader(void *arg)
1080{
1081 int completed;
52494535 1082 int completed_end;
72e9bb54 1083 int idx;
a241ec65
PM
1084 DEFINE_RCU_RANDOM(rand);
1085 struct rcu_torture *p;
1086 int pipe_count;
0729fbf3 1087 struct timer_list t;
52494535 1088 unsigned long long ts;
a241ec65
PM
1089
1090 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
dbdf65b1 1091 set_user_nice(current, 19);
0acc512c 1092 if (irqreader && cur_ops->irq_capable)
0729fbf3 1093 setup_timer_on_stack(&t, rcu_torture_timer, 0);
dbdf65b1 1094
a241ec65 1095 do {
0acc512c 1096 if (irqreader && cur_ops->irq_capable) {
0729fbf3 1097 if (!timer_pending(&t))
6155fec9 1098 mod_timer(&t, jiffies + 1);
0729fbf3 1099 }
72e9bb54
PM
1100 idx = cur_ops->readlock();
1101 completed = cur_ops->completed();
e4aa0da3 1102 ts = rcu_trace_clock_local();
632ee200 1103 p = rcu_dereference_check(rcu_torture_current,
632ee200
PM
1104 rcu_read_lock_bh_held() ||
1105 rcu_read_lock_sched_held() ||
1106 srcu_read_lock_held(&srcu_ctl));
a241ec65
PM
1107 if (p == NULL) {
1108 /* Wait for rcu_torture_writer to get underway */
72e9bb54 1109 cur_ops->readunlock(idx);
a241ec65
PM
1110 schedule_timeout_interruptible(HZ);
1111 continue;
1112 }
996417d2
PM
1113 if (p->rtort_mbtest == 0)
1114 atomic_inc(&n_rcu_torture_mberror);
0acc512c 1115 cur_ops->read_delay(&rand);
a241ec65
PM
1116 preempt_disable();
1117 pipe_count = p->rtort_pipe_count;
1118 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1119 /* Should not happen, but... */
1120 pipe_count = RCU_TORTURE_PIPE_LEN;
1121 }
52494535
PM
1122 completed_end = cur_ops->completed();
1123 if (pipe_count > 1) {
52494535
PM
1124 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1125 ts, completed, completed_end);
91afaf30 1126 rcutorture_trace_dump();
52494535 1127 }
dd17c8f7 1128 __this_cpu_inc(rcu_torture_count[pipe_count]);
52494535 1129 completed = completed_end - completed;
a241ec65
PM
1130 if (completed > RCU_TORTURE_PIPE_LEN) {
1131 /* Should not happen, but... */
1132 completed = RCU_TORTURE_PIPE_LEN;
1133 }
dd17c8f7 1134 __this_cpu_inc(rcu_torture_batch[completed]);
a241ec65 1135 preempt_enable();
72e9bb54 1136 cur_ops->readunlock(idx);
a241ec65 1137 schedule();
c9d557c1
PM
1138 rcu_stutter_wait("rcu_torture_reader");
1139 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
a241ec65 1140 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
c9d557c1 1141 rcutorture_shutdown_absorb("rcu_torture_reader");
0acc512c 1142 if (irqreader && cur_ops->irq_capable)
0729fbf3 1143 del_timer_sync(&t);
c9d557c1 1144 while (!kthread_should_stop())
a241ec65
PM
1145 schedule_timeout_uninterruptible(1);
1146 return 0;
1147}
1148
1149/*
1150 * Create an RCU-torture statistics message in the specified buffer.
1151 */
1152static int
1153rcu_torture_printk(char *page)
1154{
1155 int cnt = 0;
1156 int cpu;
1157 int i;
1158 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1159 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1160
0a945022 1161 for_each_possible_cpu(cpu) {
a241ec65
PM
1162 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1163 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1164 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1165 }
1166 }
1167 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1168 if (pipesummary[i] != 0)
1169 break;
1170 }
72e9bb54 1171 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
a241ec65 1172 cnt += sprintf(&page[cnt],
5cf05ad7 1173 "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
a241ec65
PM
1174 rcu_torture_current,
1175 rcu_torture_current_version,
1176 list_empty(&rcu_torture_freelist),
1177 atomic_read(&n_rcu_torture_alloc),
1178 atomic_read(&n_rcu_torture_alloc_fail),
5cf05ad7
PM
1179 atomic_read(&n_rcu_torture_free));
1180 cnt += sprintf(&page[cnt], "rtmbe: %d rtbke: %ld rtbre: %ld ",
0729fbf3 1181 atomic_read(&n_rcu_torture_mberror),
8e8be45e 1182 n_rcu_torture_boost_ktrerror,
5cf05ad7
PM
1183 n_rcu_torture_boost_rterror);
1184 cnt += sprintf(&page[cnt], "rtbf: %ld rtb: %ld nt: %ld ",
8e8be45e
PM
1185 n_rcu_torture_boost_failure,
1186 n_rcu_torture_boosts,
5cf05ad7 1187 n_rcu_torture_timers);
13dbf914
PM
1188 cnt += sprintf(&page[cnt],
1189 "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
1190 n_online_successes, n_online_attempts,
1191 n_offline_successes, n_offline_attempts,
1192 min_online, max_online,
1193 min_offline, max_offline,
1194 sum_online, sum_offline, HZ);
5cf05ad7 1195 cnt += sprintf(&page[cnt], "barrier: %ld/%ld:%ld",
fae4b54f
PM
1196 n_barrier_successes,
1197 n_barrier_attempts,
1198 n_rcu_torture_barrier_error);
1199 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
8e8be45e 1200 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
fae4b54f 1201 n_rcu_torture_barrier_error != 0 ||
8e8be45e
PM
1202 n_rcu_torture_boost_ktrerror != 0 ||
1203 n_rcu_torture_boost_rterror != 0 ||
fae4b54f
PM
1204 n_rcu_torture_boost_failure != 0 ||
1205 i > 1) {
a241ec65 1206 cnt += sprintf(&page[cnt], "!!! ");
996417d2 1207 atomic_inc(&n_rcu_torture_error);
5af970a4 1208 WARN_ON_ONCE(1);
996417d2 1209 }
a241ec65
PM
1210 cnt += sprintf(&page[cnt], "Reader Pipe: ");
1211 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1212 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
72e9bb54 1213 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
a241ec65 1214 cnt += sprintf(&page[cnt], "Reader Batch: ");
72e9bb54 1215 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
a241ec65 1216 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
72e9bb54 1217 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
a241ec65
PM
1218 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
1219 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1220 cnt += sprintf(&page[cnt], " %d",
1221 atomic_read(&rcu_torture_wcount[i]));
1222 }
1223 cnt += sprintf(&page[cnt], "\n");
c8e5b163 1224 if (cur_ops->stats)
72e9bb54 1225 cnt += cur_ops->stats(&page[cnt]);
a241ec65
PM
1226 return cnt;
1227}
1228
1229/*
1230 * Print torture statistics. Caller must ensure that there is only
1231 * one call to this function at a given time!!! This is normally
1232 * accomplished by relying on the module system to only have one copy
1233 * of the module loaded, and then by giving the rcu_torture_stats
1234 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1235 * thread is not running).
1236 */
1237static void
1238rcu_torture_stats_print(void)
1239{
1240 int cnt;
1241
1242 cnt = rcu_torture_printk(printk_buf);
2caa1e44 1243 pr_alert("%s", printk_buf);
a241ec65
PM
1244}
1245
1246/*
1247 * Periodically prints torture statistics, if periodic statistics printing
1248 * was specified via the stat_interval module parameter.
1249 *
1250 * No need to worry about fullstop here, since this one doesn't reference
1251 * volatile state or register callbacks.
1252 */
1253static int
1254rcu_torture_stats(void *arg)
1255{
1256 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
1257 do {
1258 schedule_timeout_interruptible(stat_interval * HZ);
1259 rcu_torture_stats_print();
c9d557c1
PM
1260 rcutorture_shutdown_absorb("rcu_torture_stats");
1261 } while (!kthread_should_stop());
a241ec65
PM
1262 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1263 return 0;
1264}
1265
d84f5203
SV
1266static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
1267
1268/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1269 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1270 */
b2896d2e 1271static void rcu_torture_shuffle_tasks(void)
d84f5203 1272{
d84f5203
SV
1273 int i;
1274
73d0a4b1 1275 cpumask_setall(shuffle_tmp_mask);
86ef5c9a 1276 get_online_cpus();
d84f5203
SV
1277
1278 /* No point in shuffling if there is only one online CPU (ex: UP) */
c9d557c1
PM
1279 if (num_online_cpus() == 1) {
1280 put_online_cpus();
1281 return;
1282 }
d84f5203
SV
1283
1284 if (rcu_idle_cpu != -1)
73d0a4b1 1285 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
d84f5203 1286
73d0a4b1 1287 set_cpus_allowed_ptr(current, shuffle_tmp_mask);
d84f5203 1288
c8e5b163 1289 if (reader_tasks) {
d84f5203
SV
1290 for (i = 0; i < nrealreaders; i++)
1291 if (reader_tasks[i])
f70316da 1292 set_cpus_allowed_ptr(reader_tasks[i],
73d0a4b1 1293 shuffle_tmp_mask);
d84f5203 1294 }
c8e5b163 1295 if (fakewriter_tasks) {
b772e1dd
JT
1296 for (i = 0; i < nfakewriters; i++)
1297 if (fakewriter_tasks[i])
f70316da 1298 set_cpus_allowed_ptr(fakewriter_tasks[i],
73d0a4b1 1299 shuffle_tmp_mask);
b772e1dd 1300 }
d84f5203 1301 if (writer_task)
73d0a4b1 1302 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
d84f5203 1303 if (stats_task)
73d0a4b1 1304 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
0e11c8e8
PM
1305 if (stutter_task)
1306 set_cpus_allowed_ptr(stutter_task, shuffle_tmp_mask);
1307 if (fqs_task)
1308 set_cpus_allowed_ptr(fqs_task, shuffle_tmp_mask);
1309 if (shutdown_task)
1310 set_cpus_allowed_ptr(shutdown_task, shuffle_tmp_mask);
1311#ifdef CONFIG_HOTPLUG_CPU
1312 if (onoff_task)
1313 set_cpus_allowed_ptr(onoff_task, shuffle_tmp_mask);
1314#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1315 if (stall_task)
1316 set_cpus_allowed_ptr(stall_task, shuffle_tmp_mask);
1317 if (barrier_cbs_tasks)
1318 for (i = 0; i < n_barrier_cbs; i++)
1319 if (barrier_cbs_tasks[i])
1320 set_cpus_allowed_ptr(barrier_cbs_tasks[i],
1321 shuffle_tmp_mask);
1322 if (barrier_task)
1323 set_cpus_allowed_ptr(barrier_task, shuffle_tmp_mask);
d84f5203
SV
1324
1325 if (rcu_idle_cpu == -1)
1326 rcu_idle_cpu = num_online_cpus() - 1;
1327 else
1328 rcu_idle_cpu--;
1329
86ef5c9a 1330 put_online_cpus();
d84f5203
SV
1331}
1332
1333/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1334 * system to become idle at a time and cut off its timer ticks. This is meant
1335 * to test the support for such tickless idle CPU in RCU.
1336 */
1337static int
1338rcu_torture_shuffle(void *arg)
1339{
1340 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1341 do {
1342 schedule_timeout_interruptible(shuffle_interval * HZ);
1343 rcu_torture_shuffle_tasks();
c9d557c1
PM
1344 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1345 } while (!kthread_should_stop());
d84f5203
SV
1346 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1347 return 0;
1348}
1349
d120f65f
PM
1350/* Cause the rcutorture test to "stutter", starting and stopping all
1351 * threads periodically.
1352 */
1353static int
1354rcu_torture_stutter(void *arg)
1355{
1356 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1357 do {
1358 schedule_timeout_interruptible(stutter * HZ);
1359 stutter_pause_test = 1;
c9d557c1 1360 if (!kthread_should_stop())
d120f65f
PM
1361 schedule_timeout_interruptible(stutter * HZ);
1362 stutter_pause_test = 0;
c9d557c1
PM
1363 rcutorture_shutdown_absorb("rcu_torture_stutter");
1364 } while (!kthread_should_stop());
d120f65f
PM
1365 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1366 return 0;
1367}
1368
95c38322 1369static inline void
e66c33d5 1370rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
95c38322 1371{
2caa1e44
PM
1372 pr_alert("%s" TORTURE_FLAG
1373 "--- %s: nreaders=%d nfakewriters=%d "
1374 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1375 "shuffle_interval=%d stutter=%d irqreader=%d "
1376 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1377 "test_boost=%d/%d test_boost_interval=%d "
1378 "test_boost_duration=%d shutdown_secs=%d "
67afeed2
PM
1379 "stall_cpu=%d stall_cpu_holdoff=%d "
1380 "n_barrier_cbs=%d "
2caa1e44
PM
1381 "onoff_interval=%d onoff_holdoff=%d\n",
1382 torture_type, tag, nrealreaders, nfakewriters,
1383 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1384 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1385 test_boost, cur_ops->can_boost,
1386 test_boost_interval, test_boost_duration, shutdown_secs,
67afeed2
PM
1387 stall_cpu, stall_cpu_holdoff,
1388 n_barrier_cbs,
2caa1e44 1389 onoff_interval, onoff_holdoff);
95c38322
PM
1390}
1391
8e8be45e 1392static struct notifier_block rcutorture_shutdown_nb = {
343e9099
PM
1393 .notifier_call = rcutorture_shutdown_notify,
1394};
1395
8e8be45e
PM
1396static void rcutorture_booster_cleanup(int cpu)
1397{
1398 struct task_struct *t;
1399
1400 if (boost_tasks[cpu] == NULL)
1401 return;
1402 mutex_lock(&boost_mutex);
1403 VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
1404 t = boost_tasks[cpu];
1405 boost_tasks[cpu] = NULL;
1406 mutex_unlock(&boost_mutex);
1407
1408 /* This must be outside of the mutex, otherwise deadlock! */
1409 kthread_stop(t);
37e377d2 1410 boost_tasks[cpu] = NULL;
8e8be45e
PM
1411}
1412
1413static int rcutorture_booster_init(int cpu)
1414{
1415 int retval;
1416
1417 if (boost_tasks[cpu] != NULL)
1418 return 0; /* Already created, nothing more to do. */
1419
1420 /* Don't allow time recalculation while creating a new task. */
1421 mutex_lock(&boost_mutex);
1422 VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
1f288094
ED
1423 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1424 cpu_to_node(cpu),
1425 "rcu_torture_boost");
8e8be45e
PM
1426 if (IS_ERR(boost_tasks[cpu])) {
1427 retval = PTR_ERR(boost_tasks[cpu]);
1428 VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
1429 n_rcu_torture_boost_ktrerror++;
1430 boost_tasks[cpu] = NULL;
1431 mutex_unlock(&boost_mutex);
1432 return retval;
1433 }
1434 kthread_bind(boost_tasks[cpu], cpu);
1435 wake_up_process(boost_tasks[cpu]);
1436 mutex_unlock(&boost_mutex);
1437 return 0;
1438}
1439
d5f546d8
PM
1440/*
1441 * Cause the rcutorture test to shutdown the system after the test has
1442 * run for the time specified by the shutdown_secs module parameter.
1443 */
1444static int
1445rcu_torture_shutdown(void *arg)
1446{
1447 long delta;
1448 unsigned long jiffies_snap;
1449
1450 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
1451 jiffies_snap = ACCESS_ONCE(jiffies);
1452 while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
1453 !kthread_should_stop()) {
1454 delta = shutdown_time - jiffies_snap;
1455 if (verbose)
2caa1e44
PM
1456 pr_alert("%s" TORTURE_FLAG
1457 "rcu_torture_shutdown task: %lu jiffies remaining\n",
1458 torture_type, delta);
d5f546d8
PM
1459 schedule_timeout_interruptible(delta);
1460 jiffies_snap = ACCESS_ONCE(jiffies);
1461 }
b58bdcca 1462 if (kthread_should_stop()) {
d5f546d8
PM
1463 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
1464 return 0;
1465 }
1466
1467 /* OK, shut down the system. */
1468
1469 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
1470 shutdown_task = NULL; /* Avoid self-kill deadlock. */
1471 rcu_torture_cleanup(); /* Get the success/failure message. */
1472 kernel_power_off(); /* Shut down the system. */
1473 return 0;
1474}
1475
b58bdcca
PM
1476#ifdef CONFIG_HOTPLUG_CPU
1477
1478/*
1479 * Execute random CPU-hotplug operations at the interval specified
1480 * by the onoff_interval.
1481 */
49fb4c62 1482static int
b58bdcca
PM
1483rcu_torture_onoff(void *arg)
1484{
1485 int cpu;
13dbf914 1486 unsigned long delta;
b58bdcca
PM
1487 int maxcpu = -1;
1488 DEFINE_RCU_RANDOM(rand);
48983260 1489 int ret;
13dbf914 1490 unsigned long starttime;
b58bdcca
PM
1491
1492 VERBOSE_PRINTK_STRING("rcu_torture_onoff task started");
1493 for_each_online_cpu(cpu)
1494 maxcpu = cpu;
1495 WARN_ON(maxcpu < 0);
9b9ec9b9
PM
1496 if (onoff_holdoff > 0) {
1497 VERBOSE_PRINTK_STRING("rcu_torture_onoff begin holdoff");
1498 schedule_timeout_interruptible(onoff_holdoff * HZ);
1499 VERBOSE_PRINTK_STRING("rcu_torture_onoff end holdoff");
1500 }
b58bdcca
PM
1501 while (!kthread_should_stop()) {
1502 cpu = (rcu_random(&rand) >> 4) % (maxcpu + 1);
f220242a 1503 if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
b58bdcca 1504 if (verbose)
2caa1e44
PM
1505 pr_alert("%s" TORTURE_FLAG
1506 "rcu_torture_onoff task: offlining %d\n",
1507 torture_type, cpu);
13dbf914 1508 starttime = jiffies;
b58bdcca 1509 n_offline_attempts++;
48983260
PM
1510 ret = cpu_down(cpu);
1511 if (ret) {
1512 if (verbose)
1513 pr_alert("%s" TORTURE_FLAG
1514 "rcu_torture_onoff task: offline %d failed: errno %d\n",
1515 torture_type, cpu, ret);
1516 } else {
b58bdcca 1517 if (verbose)
2caa1e44
PM
1518 pr_alert("%s" TORTURE_FLAG
1519 "rcu_torture_onoff task: offlined %d\n",
1520 torture_type, cpu);
b58bdcca 1521 n_offline_successes++;
13dbf914
PM
1522 delta = jiffies - starttime;
1523 sum_offline += delta;
1524 if (min_offline < 0) {
1525 min_offline = delta;
1526 max_offline = delta;
1527 }
1528 if (min_offline > delta)
1529 min_offline = delta;
1530 if (max_offline < delta)
1531 max_offline = delta;
b58bdcca 1532 }
f220242a 1533 } else if (cpu_is_hotpluggable(cpu)) {
b58bdcca 1534 if (verbose)
2caa1e44
PM
1535 pr_alert("%s" TORTURE_FLAG
1536 "rcu_torture_onoff task: onlining %d\n",
1537 torture_type, cpu);
13dbf914 1538 starttime = jiffies;
b58bdcca
PM
1539 n_online_attempts++;
1540 if (cpu_up(cpu) == 0) {
1541 if (verbose)
2caa1e44
PM
1542 pr_alert("%s" TORTURE_FLAG
1543 "rcu_torture_onoff task: onlined %d\n",
1544 torture_type, cpu);
b58bdcca 1545 n_online_successes++;
13dbf914
PM
1546 delta = jiffies - starttime;
1547 sum_online += delta;
1548 if (min_online < 0) {
1549 min_online = delta;
1550 max_online = delta;
1551 }
1552 if (min_online > delta)
1553 min_online = delta;
1554 if (max_online < delta)
1555 max_online = delta;
b58bdcca
PM
1556 }
1557 }
1558 schedule_timeout_interruptible(onoff_interval * HZ);
1559 }
1560 VERBOSE_PRINTK_STRING("rcu_torture_onoff task stopping");
1561 return 0;
1562}
1563
49fb4c62 1564static int
b58bdcca
PM
1565rcu_torture_onoff_init(void)
1566{
3c1b1ce0
JL
1567 int ret;
1568
b58bdcca
PM
1569 if (onoff_interval <= 0)
1570 return 0;
1571 onoff_task = kthread_run(rcu_torture_onoff, NULL, "rcu_torture_onoff");
1572 if (IS_ERR(onoff_task)) {
3c1b1ce0 1573 ret = PTR_ERR(onoff_task);
b58bdcca 1574 onoff_task = NULL;
3c1b1ce0 1575 return ret;
b58bdcca
PM
1576 }
1577 return 0;
1578}
1579
1580static void rcu_torture_onoff_cleanup(void)
1581{
1582 if (onoff_task == NULL)
1583 return;
1584 VERBOSE_PRINTK_STRING("Stopping rcu_torture_onoff task");
1585 kthread_stop(onoff_task);
37e377d2 1586 onoff_task = NULL;
b58bdcca
PM
1587}
1588
1589#else /* #ifdef CONFIG_HOTPLUG_CPU */
1590
37e377d2 1591static int
b58bdcca
PM
1592rcu_torture_onoff_init(void)
1593{
37e377d2 1594 return 0;
b58bdcca
PM
1595}
1596
1597static void rcu_torture_onoff_cleanup(void)
1598{
1599}
1600
1601#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
1602
c13f3757
PM
1603/*
1604 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1605 * induces a CPU stall for the time specified by stall_cpu.
1606 */
49fb4c62 1607static int rcu_torture_stall(void *args)
c13f3757
PM
1608{
1609 unsigned long stop_at;
1610
1611 VERBOSE_PRINTK_STRING("rcu_torture_stall task started");
1612 if (stall_cpu_holdoff > 0) {
1613 VERBOSE_PRINTK_STRING("rcu_torture_stall begin holdoff");
1614 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
1615 VERBOSE_PRINTK_STRING("rcu_torture_stall end holdoff");
1616 }
1617 if (!kthread_should_stop()) {
1618 stop_at = get_seconds() + stall_cpu;
1619 /* RCU CPU stall is expected behavior in following code. */
2caa1e44 1620 pr_alert("rcu_torture_stall start.\n");
c13f3757
PM
1621 rcu_read_lock();
1622 preempt_disable();
1623 while (ULONG_CMP_LT(get_seconds(), stop_at))
1624 continue; /* Induce RCU CPU stall warning. */
1625 preempt_enable();
1626 rcu_read_unlock();
2caa1e44 1627 pr_alert("rcu_torture_stall end.\n");
c13f3757
PM
1628 }
1629 rcutorture_shutdown_absorb("rcu_torture_stall");
1630 while (!kthread_should_stop())
1631 schedule_timeout_interruptible(10 * HZ);
1632 return 0;
1633}
1634
1635/* Spawn CPU-stall kthread, if stall_cpu specified. */
1636static int __init rcu_torture_stall_init(void)
1637{
1638 int ret;
1639
1640 if (stall_cpu <= 0)
1641 return 0;
1642 stall_task = kthread_run(rcu_torture_stall, NULL, "rcu_torture_stall");
1643 if (IS_ERR(stall_task)) {
1644 ret = PTR_ERR(stall_task);
1645 stall_task = NULL;
1646 return ret;
1647 }
1648 return 0;
1649}
1650
1651/* Clean up after the CPU-stall kthread, if one was spawned. */
1652static void rcu_torture_stall_cleanup(void)
1653{
1654 if (stall_task == NULL)
1655 return;
1656 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stall_task.");
1657 kthread_stop(stall_task);
37e377d2 1658 stall_task = NULL;
c13f3757
PM
1659}
1660
fae4b54f
PM
1661/* Callback function for RCU barrier testing. */
1662void rcu_torture_barrier_cbf(struct rcu_head *rcu)
1663{
1664 atomic_inc(&barrier_cbs_invoked);
1665}
1666
1667/* kthread function to register callbacks used to test RCU barriers. */
1668static int rcu_torture_barrier_cbs(void *arg)
1669{
1670 long myid = (long)arg;
c6ebcbb6 1671 bool lastphase = 0;
fae4b54f
PM
1672 struct rcu_head rcu;
1673
1674 init_rcu_head_on_stack(&rcu);
1675 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task started");
1676 set_user_nice(current, 19);
1677 do {
1678 wait_event(barrier_cbs_wq[myid],
c6ebcbb6 1679 barrier_phase != lastphase ||
fae4b54f
PM
1680 kthread_should_stop() ||
1681 fullstop != FULLSTOP_DONTSTOP);
c6ebcbb6
PM
1682 lastphase = barrier_phase;
1683 smp_mb(); /* ensure barrier_phase load before ->call(). */
fae4b54f
PM
1684 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1685 break;
1686 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1687 if (atomic_dec_and_test(&barrier_cbs_count))
1688 wake_up(&barrier_wq);
1689 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1690 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task stopping");
1691 rcutorture_shutdown_absorb("rcu_torture_barrier_cbs");
1692 while (!kthread_should_stop())
1693 schedule_timeout_interruptible(1);
1694 cur_ops->cb_barrier();
1695 destroy_rcu_head_on_stack(&rcu);
1696 return 0;
1697}
1698
1699/* kthread function to drive and coordinate RCU barrier testing. */
1700static int rcu_torture_barrier(void *arg)
1701{
1702 int i;
1703
1704 VERBOSE_PRINTK_STRING("rcu_torture_barrier task starting");
1705 do {
1706 atomic_set(&barrier_cbs_invoked, 0);
1707 atomic_set(&barrier_cbs_count, n_barrier_cbs);
c6ebcbb6
PM
1708 smp_mb(); /* Ensure barrier_phase after prior assignments. */
1709 barrier_phase = !barrier_phase;
fae4b54f
PM
1710 for (i = 0; i < n_barrier_cbs; i++)
1711 wake_up(&barrier_cbs_wq[i]);
1712 wait_event(barrier_wq,
1713 atomic_read(&barrier_cbs_count) == 0 ||
1714 kthread_should_stop() ||
1715 fullstop != FULLSTOP_DONTSTOP);
1716 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1717 break;
1718 n_barrier_attempts++;
1719 cur_ops->cb_barrier();
1720 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1721 n_rcu_torture_barrier_error++;
1722 WARN_ON_ONCE(1);
1723 }
1724 n_barrier_successes++;
1725 schedule_timeout_interruptible(HZ / 10);
1726 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1727 VERBOSE_PRINTK_STRING("rcu_torture_barrier task stopping");
143aa672 1728 rcutorture_shutdown_absorb("rcu_torture_barrier");
fae4b54f
PM
1729 while (!kthread_should_stop())
1730 schedule_timeout_interruptible(1);
1731 return 0;
1732}
1733
1734/* Initialize RCU barrier testing. */
1735static int rcu_torture_barrier_init(void)
1736{
1737 int i;
1738 int ret;
1739
1740 if (n_barrier_cbs == 0)
1741 return 0;
1742 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
2caa1e44
PM
1743 pr_alert("%s" TORTURE_FLAG
1744 " Call or barrier ops missing for %s,\n",
1745 torture_type, cur_ops->name);
1746 pr_alert("%s" TORTURE_FLAG
1747 " RCU barrier testing omitted from run.\n",
1748 torture_type);
fae4b54f
PM
1749 return 0;
1750 }
1751 atomic_set(&barrier_cbs_count, 0);
1752 atomic_set(&barrier_cbs_invoked, 0);
1753 barrier_cbs_tasks =
1754 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
1755 GFP_KERNEL);
1756 barrier_cbs_wq =
1757 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
1758 GFP_KERNEL);
de5e6437 1759 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
fae4b54f
PM
1760 return -ENOMEM;
1761 for (i = 0; i < n_barrier_cbs; i++) {
1762 init_waitqueue_head(&barrier_cbs_wq[i]);
1763 barrier_cbs_tasks[i] = kthread_run(rcu_torture_barrier_cbs,
9059c940 1764 (void *)(long)i,
fae4b54f
PM
1765 "rcu_torture_barrier_cbs");
1766 if (IS_ERR(barrier_cbs_tasks[i])) {
1767 ret = PTR_ERR(barrier_cbs_tasks[i]);
1768 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier_cbs");
1769 barrier_cbs_tasks[i] = NULL;
1770 return ret;
1771 }
1772 }
1773 barrier_task = kthread_run(rcu_torture_barrier, NULL,
1774 "rcu_torture_barrier");
1775 if (IS_ERR(barrier_task)) {
1776 ret = PTR_ERR(barrier_task);
1777 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier");
1778 barrier_task = NULL;
1779 }
1780 return 0;
1781}
1782
1783/* Clean up after RCU barrier testing. */
1784static void rcu_torture_barrier_cleanup(void)
1785{
1786 int i;
1787
1788 if (barrier_task != NULL) {
1789 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier task");
1790 kthread_stop(barrier_task);
1791 barrier_task = NULL;
1792 }
1793 if (barrier_cbs_tasks != NULL) {
1794 for (i = 0; i < n_barrier_cbs; i++) {
1795 if (barrier_cbs_tasks[i] != NULL) {
1796 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier_cbs task");
1797 kthread_stop(barrier_cbs_tasks[i]);
1798 barrier_cbs_tasks[i] = NULL;
1799 }
1800 }
1801 kfree(barrier_cbs_tasks);
1802 barrier_cbs_tasks = NULL;
1803 }
1804 if (barrier_cbs_wq != NULL) {
1805 kfree(barrier_cbs_wq);
1806 barrier_cbs_wq = NULL;
1807 }
1808}
1809
8e8be45e
PM
1810static int rcutorture_cpu_notify(struct notifier_block *self,
1811 unsigned long action, void *hcpu)
1812{
1813 long cpu = (long)hcpu;
1814
1815 switch (action) {
1816 case CPU_ONLINE:
1817 case CPU_DOWN_FAILED:
1818 (void)rcutorture_booster_init(cpu);
1819 break;
1820 case CPU_DOWN_PREPARE:
1821 rcutorture_booster_cleanup(cpu);
1822 break;
1823 default:
1824 break;
1825 }
1826 return NOTIFY_OK;
1827}
1828
1829static struct notifier_block rcutorture_cpu_nb = {
1830 .notifier_call = rcutorture_cpu_notify,
1831};
1832
a241ec65
PM
1833static void
1834rcu_torture_cleanup(void)
1835{
1836 int i;
1837
343e9099 1838 mutex_lock(&fullstop_mutex);
4a298656 1839 rcutorture_record_test_transition();
c9d557c1 1840 if (fullstop == FULLSTOP_SHUTDOWN) {
2caa1e44 1841 pr_warn(/* but going down anyway, so... */
c9d557c1 1842 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
343e9099 1843 mutex_unlock(&fullstop_mutex);
c9d557c1 1844 schedule_timeout_uninterruptible(10);
343e9099
PM
1845 if (cur_ops->cb_barrier != NULL)
1846 cur_ops->cb_barrier();
1847 return;
1848 }
c9d557c1 1849 fullstop = FULLSTOP_RMMOD;
343e9099 1850 mutex_unlock(&fullstop_mutex);
8e8be45e 1851 unregister_reboot_notifier(&rcutorture_shutdown_nb);
fae4b54f 1852 rcu_torture_barrier_cleanup();
c13f3757 1853 rcu_torture_stall_cleanup();
d120f65f
PM
1854 if (stutter_task) {
1855 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1856 kthread_stop(stutter_task);
1857 }
1858 stutter_task = NULL;
c8e5b163 1859 if (shuffler_task) {
d84f5203
SV
1860 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1861 kthread_stop(shuffler_task);
73d0a4b1 1862 free_cpumask_var(shuffle_tmp_mask);
d84f5203
SV
1863 }
1864 shuffler_task = NULL;
1865
c8e5b163 1866 if (writer_task) {
a241ec65
PM
1867 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1868 kthread_stop(writer_task);
1869 }
1870 writer_task = NULL;
1871
c8e5b163 1872 if (reader_tasks) {
a241ec65 1873 for (i = 0; i < nrealreaders; i++) {
c8e5b163 1874 if (reader_tasks[i]) {
a241ec65
PM
1875 VERBOSE_PRINTK_STRING(
1876 "Stopping rcu_torture_reader task");
1877 kthread_stop(reader_tasks[i]);
1878 }
1879 reader_tasks[i] = NULL;
1880 }
1881 kfree(reader_tasks);
1882 reader_tasks = NULL;
1883 }
1884 rcu_torture_current = NULL;
1885
c8e5b163 1886 if (fakewriter_tasks) {
b772e1dd 1887 for (i = 0; i < nfakewriters; i++) {
c8e5b163 1888 if (fakewriter_tasks[i]) {
b772e1dd
JT
1889 VERBOSE_PRINTK_STRING(
1890 "Stopping rcu_torture_fakewriter task");
1891 kthread_stop(fakewriter_tasks[i]);
1892 }
1893 fakewriter_tasks[i] = NULL;
1894 }
1895 kfree(fakewriter_tasks);
1896 fakewriter_tasks = NULL;
1897 }
1898
c8e5b163 1899 if (stats_task) {
a241ec65
PM
1900 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1901 kthread_stop(stats_task);
1902 }
1903 stats_task = NULL;
1904
bf66f18e
PM
1905 if (fqs_task) {
1906 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1907 kthread_stop(fqs_task);
1908 }
1909 fqs_task = NULL;
8e8be45e
PM
1910 if ((test_boost == 1 && cur_ops->can_boost) ||
1911 test_boost == 2) {
1912 unregister_cpu_notifier(&rcutorture_cpu_nb);
1913 for_each_possible_cpu(i)
1914 rcutorture_booster_cleanup(i);
1915 }
d5f546d8
PM
1916 if (shutdown_task != NULL) {
1917 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
1918 kthread_stop(shutdown_task);
1919 }
37e377d2 1920 shutdown_task = NULL;
b58bdcca 1921 rcu_torture_onoff_cleanup();
bf66f18e 1922
a241ec65 1923 /* Wait for all RCU callbacks to fire. */
2326974d
PM
1924
1925 if (cur_ops->cb_barrier != NULL)
1926 cur_ops->cb_barrier();
a241ec65 1927
a241ec65 1928 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
72e9bb54 1929
fae4b54f 1930 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
8e8be45e 1931 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
091541bb
PM
1932 else if (n_online_successes != n_online_attempts ||
1933 n_offline_successes != n_offline_attempts)
1934 rcu_torture_print_module_parms(cur_ops,
1935 "End of test: RCU_HOTPLUG");
95c38322 1936 else
8e8be45e 1937 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
a241ec65
PM
1938}
1939
d2818df1
PM
1940#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1941static void rcu_torture_leak_cb(struct rcu_head *rhp)
1942{
1943}
1944
1945static void rcu_torture_err_cb(struct rcu_head *rhp)
1946{
1947 /*
1948 * This -might- happen due to race conditions, but is unlikely.
1949 * The scenario that leads to this happening is that the
1950 * first of the pair of duplicate callbacks is queued,
1951 * someone else starts a grace period that includes that
1952 * callback, then the second of the pair must wait for the
1953 * next grace period. Unlikely, but can happen. If it
1954 * does happen, the debug-objects subsystem won't have splatted.
1955 */
1956 pr_alert("rcutorture: duplicated callback was invoked.\n");
1957}
1958#endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1959
1960/*
1961 * Verify that double-free causes debug-objects to complain, but only
1962 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
1963 * cannot be carried out.
1964 */
1965static void rcu_test_debug_objects(void)
1966{
1967#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1968 struct rcu_head rh1;
1969 struct rcu_head rh2;
1970
1971 init_rcu_head_on_stack(&rh1);
1972 init_rcu_head_on_stack(&rh2);
1973 pr_alert("rcutorture: WARN: Duplicate call_rcu() test starting.\n");
1974
1975 /* Try to queue the rh2 pair of callbacks for the same grace period. */
1976 preempt_disable(); /* Prevent preemption from interrupting test. */
1977 rcu_read_lock(); /* Make it impossible to finish a grace period. */
1978 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
1979 local_irq_disable(); /* Make it harder to start a new grace period. */
1980 call_rcu(&rh2, rcu_torture_leak_cb);
1981 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
1982 local_irq_enable();
1983 rcu_read_unlock();
1984 preempt_enable();
1985
1986 /* Wait for them all to get done so we can safely return. */
1987 rcu_barrier();
1988 pr_alert("rcutorture: WARN: Duplicate call_rcu() test complete.\n");
1989 destroy_rcu_head_on_stack(&rh1);
1990 destroy_rcu_head_on_stack(&rh2);
1991#else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1992 pr_alert("rcutorture: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n");
1993#endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1994}
1995
6f8bc500 1996static int __init
a241ec65
PM
1997rcu_torture_init(void)
1998{
1999 int i;
2000 int cpu;
2001 int firsterr = 0;
fae4b54f 2002 int retval;
ade5fb81 2003 static struct rcu_torture_ops *torture_ops[] =
d9a3da06 2004 { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
bdf2a436 2005 &rcu_bh_ops, &rcu_bh_sync_ops, &rcu_bh_expedited_ops,
751a68b2 2006 &srcu_ops, &srcu_sync_ops, &srcu_expedited_ops,
804bb837 2007 &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
a241ec65 2008
343e9099
PM
2009 mutex_lock(&fullstop_mutex);
2010
a241ec65 2011 /* Process args and tell the world that the torturer is on the job. */
ade5fb81 2012 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
72e9bb54 2013 cur_ops = torture_ops[i];
ade5fb81 2014 if (strcmp(torture_type, cur_ops->name) == 0)
72e9bb54 2015 break;
72e9bb54 2016 }
ade5fb81 2017 if (i == ARRAY_SIZE(torture_ops)) {
2caa1e44
PM
2018 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
2019 torture_type);
2020 pr_alert("rcu-torture types:");
cf886c44 2021 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
2caa1e44
PM
2022 pr_alert(" %s", torture_ops[i]->name);
2023 pr_alert("\n");
343e9099 2024 mutex_unlock(&fullstop_mutex);
a71fca58 2025 return -EINVAL;
72e9bb54 2026 }
bf66f18e 2027 if (cur_ops->fqs == NULL && fqs_duration != 0) {
2caa1e44 2028 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
bf66f18e
PM
2029 fqs_duration = 0;
2030 }
c8e5b163 2031 if (cur_ops->init)
72e9bb54
PM
2032 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
2033
a241ec65
PM
2034 if (nreaders >= 0)
2035 nrealreaders = nreaders;
2036 else
2037 nrealreaders = 2 * num_online_cpus();
8e8be45e 2038 rcu_torture_print_module_parms(cur_ops, "Start of test");
c9d557c1 2039 fullstop = FULLSTOP_DONTSTOP;
a241ec65
PM
2040
2041 /* Set up the freelist. */
2042
2043 INIT_LIST_HEAD(&rcu_torture_freelist);
788e770e 2044 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
996417d2 2045 rcu_tortures[i].rtort_mbtest = 0;
a241ec65
PM
2046 list_add_tail(&rcu_tortures[i].rtort_free,
2047 &rcu_torture_freelist);
2048 }
2049
2050 /* Initialize the statistics so that each run gets its own numbers. */
2051
2052 rcu_torture_current = NULL;
2053 rcu_torture_current_version = 0;
2054 atomic_set(&n_rcu_torture_alloc, 0);
2055 atomic_set(&n_rcu_torture_alloc_fail, 0);
2056 atomic_set(&n_rcu_torture_free, 0);
996417d2
PM
2057 atomic_set(&n_rcu_torture_mberror, 0);
2058 atomic_set(&n_rcu_torture_error, 0);
fae4b54f 2059 n_rcu_torture_barrier_error = 0;
8e8be45e
PM
2060 n_rcu_torture_boost_ktrerror = 0;
2061 n_rcu_torture_boost_rterror = 0;
8e8be45e
PM
2062 n_rcu_torture_boost_failure = 0;
2063 n_rcu_torture_boosts = 0;
a241ec65
PM
2064 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
2065 atomic_set(&rcu_torture_wcount[i], 0);
0a945022 2066 for_each_possible_cpu(cpu) {
a241ec65
PM
2067 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
2068 per_cpu(rcu_torture_count, cpu)[i] = 0;
2069 per_cpu(rcu_torture_batch, cpu)[i] = 0;
2070 }
2071 }
2072
2073 /* Start up the kthreads. */
2074
2075 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
60f53782
PM
2076 writer_task = kthread_create(rcu_torture_writer, NULL,
2077 "rcu_torture_writer");
a241ec65
PM
2078 if (IS_ERR(writer_task)) {
2079 firsterr = PTR_ERR(writer_task);
2080 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
2081 writer_task = NULL;
2082 goto unwind;
2083 }
60f53782 2084 wake_up_process(writer_task);
b772e1dd 2085 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
a71fca58 2086 GFP_KERNEL);
b772e1dd
JT
2087 if (fakewriter_tasks == NULL) {
2088 VERBOSE_PRINTK_ERRSTRING("out of memory");
2089 firsterr = -ENOMEM;
2090 goto unwind;
2091 }
2092 for (i = 0; i < nfakewriters; i++) {
2093 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
2094 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
a71fca58 2095 "rcu_torture_fakewriter");
b772e1dd
JT
2096 if (IS_ERR(fakewriter_tasks[i])) {
2097 firsterr = PTR_ERR(fakewriter_tasks[i]);
2098 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
2099 fakewriter_tasks[i] = NULL;
2100 goto unwind;
2101 }
2102 }
2860aaba 2103 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
a241ec65
PM
2104 GFP_KERNEL);
2105 if (reader_tasks == NULL) {
2106 VERBOSE_PRINTK_ERRSTRING("out of memory");
2107 firsterr = -ENOMEM;
2108 goto unwind;
2109 }
2110 for (i = 0; i < nrealreaders; i++) {
2111 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
2112 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
2113 "rcu_torture_reader");
2114 if (IS_ERR(reader_tasks[i])) {
2115 firsterr = PTR_ERR(reader_tasks[i]);
2116 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
2117 reader_tasks[i] = NULL;
2118 goto unwind;
2119 }
2120 }
2121 if (stat_interval > 0) {
2122 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
2123 stats_task = kthread_run(rcu_torture_stats, NULL,
2124 "rcu_torture_stats");
2125 if (IS_ERR(stats_task)) {
2126 firsterr = PTR_ERR(stats_task);
2127 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
2128 stats_task = NULL;
2129 goto unwind;
2130 }
2131 }
d84f5203
SV
2132 if (test_no_idle_hz) {
2133 rcu_idle_cpu = num_online_cpus() - 1;
73d0a4b1
RR
2134
2135 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
2136 firsterr = -ENOMEM;
2137 VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
2138 goto unwind;
2139 }
2140
d84f5203
SV
2141 /* Create the shuffler thread */
2142 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
2143 "rcu_torture_shuffle");
2144 if (IS_ERR(shuffler_task)) {
73d0a4b1 2145 free_cpumask_var(shuffle_tmp_mask);
d84f5203
SV
2146 firsterr = PTR_ERR(shuffler_task);
2147 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
2148 shuffler_task = NULL;
2149 goto unwind;
2150 }
2151 }
d120f65f
PM
2152 if (stutter < 0)
2153 stutter = 0;
2154 if (stutter) {
2155 /* Create the stutter thread */
2156 stutter_task = kthread_run(rcu_torture_stutter, NULL,
2157 "rcu_torture_stutter");
2158 if (IS_ERR(stutter_task)) {
2159 firsterr = PTR_ERR(stutter_task);
2160 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
2161 stutter_task = NULL;
2162 goto unwind;
2163 }
2164 }
bf66f18e
PM
2165 if (fqs_duration < 0)
2166 fqs_duration = 0;
2167 if (fqs_duration) {
2168 /* Create the stutter thread */
2169 fqs_task = kthread_run(rcu_torture_fqs, NULL,
2170 "rcu_torture_fqs");
2171 if (IS_ERR(fqs_task)) {
2172 firsterr = PTR_ERR(fqs_task);
2173 VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
2174 fqs_task = NULL;
2175 goto unwind;
2176 }
2177 }
8e8be45e
PM
2178 if (test_boost_interval < 1)
2179 test_boost_interval = 1;
2180 if (test_boost_duration < 2)
2181 test_boost_duration = 2;
2182 if ((test_boost == 1 && cur_ops->can_boost) ||
2183 test_boost == 2) {
8e8be45e
PM
2184
2185 boost_starttime = jiffies + test_boost_interval * HZ;
2186 register_cpu_notifier(&rcutorture_cpu_nb);
2187 for_each_possible_cpu(i) {
2188 if (cpu_is_offline(i))
2189 continue; /* Heuristic: CPU can go offline. */
2190 retval = rcutorture_booster_init(i);
2191 if (retval < 0) {
2192 firsterr = retval;
2193 goto unwind;
2194 }
2195 }
2196 }
d5f546d8
PM
2197 if (shutdown_secs > 0) {
2198 shutdown_time = jiffies + shutdown_secs * HZ;
60f53782
PM
2199 shutdown_task = kthread_create(rcu_torture_shutdown, NULL,
2200 "rcu_torture_shutdown");
d5f546d8
PM
2201 if (IS_ERR(shutdown_task)) {
2202 firsterr = PTR_ERR(shutdown_task);
2203 VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
2204 shutdown_task = NULL;
2205 goto unwind;
2206 }
60f53782 2207 wake_up_process(shutdown_task);
d5f546d8 2208 }
37e377d2
PM
2209 i = rcu_torture_onoff_init();
2210 if (i != 0) {
2211 firsterr = i;
2212 goto unwind;
2213 }
8e8be45e 2214 register_reboot_notifier(&rcutorture_shutdown_nb);
37e377d2
PM
2215 i = rcu_torture_stall_init();
2216 if (i != 0) {
2217 firsterr = i;
2218 goto unwind;
2219 }
fae4b54f
PM
2220 retval = rcu_torture_barrier_init();
2221 if (retval != 0) {
2222 firsterr = retval;
2223 goto unwind;
2224 }
d2818df1
PM
2225 if (object_debug)
2226 rcu_test_debug_objects();
4a298656 2227 rcutorture_record_test_transition();
343e9099 2228 mutex_unlock(&fullstop_mutex);
a241ec65
PM
2229 return 0;
2230
2231unwind:
343e9099 2232 mutex_unlock(&fullstop_mutex);
a241ec65
PM
2233 rcu_torture_cleanup();
2234 return firsterr;
2235}
2236
2237module_init(rcu_torture_init);
2238module_exit(rcu_torture_cleanup);
This page took 0.688928 seconds and 5 git commands to generate.