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