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