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