rcutorture: Explicitly test synchronous grace-period primitives
[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
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>
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 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");
d277d868 761 set_user_nice(current, MAX_NICE);
dbdf65b1 762
a48f3fad 763 /* Initialize synctype[] array. If none set, take default. */
f0bf8fab
PM
764 if (!gp_cond1 && !gp_exp1 && !gp_normal1 && !gp_sync)
765 gp_cond1 = gp_exp1 = gp_normal1 = gp_sync1 = true;
a48f3fad
PM
766 if (gp_cond1 && cur_ops->get_state && cur_ops->cond_sync)
767 synctype[nsynctypes++] = RTWS_COND_GET;
768 else if (gp_cond && (!cur_ops->get_state || !cur_ops->cond_sync))
769 pr_alert("rcu_torture_writer: gp_cond without primitives.\n");
770 if (gp_exp1 && cur_ops->exp_sync)
771 synctype[nsynctypes++] = RTWS_EXP_SYNC;
772 else if (gp_exp && !cur_ops->exp_sync)
773 pr_alert("rcu_torture_writer: gp_exp without primitives.\n");
774 if (gp_normal1 && cur_ops->deferred_free)
775 synctype[nsynctypes++] = RTWS_DEF_FREE;
776 else if (gp_normal && !cur_ops->deferred_free)
777 pr_alert("rcu_torture_writer: gp_normal without primitives.\n");
f0bf8fab
PM
778 if (gp_sync1 && cur_ops->sync)
779 synctype[nsynctypes++] = RTWS_SYNC;
780 else if (gp_sync && !cur_ops->sync)
781 pr_alert("rcu_torture_writer: gp_sync without primitives.\n");
a48f3fad
PM
782 if (WARN_ONCE(nsynctypes == 0,
783 "rcu_torture_writer: No update-side primitives.\n")) {
f0bf8fab
PM
784 /*
785 * No updates primitives, so don't try updating.
786 * The resulting test won't be testing much, hence the
787 * above WARN_ONCE().
788 */
a48f3fad
PM
789 rcu_torture_writer_state = RTWS_STOPPING;
790 torture_kthread_stopping("rcu_torture_writer");
791 }
792
a241ec65 793 do {
ad0dc7f9 794 rcu_torture_writer_state = RTWS_FIXED_DELAY;
a241ec65 795 schedule_timeout_uninterruptible(1);
a71fca58
PM
796 rp = rcu_torture_alloc();
797 if (rp == NULL)
a241ec65
PM
798 continue;
799 rp->rtort_pipe_count = 0;
ad0dc7f9 800 rcu_torture_writer_state = RTWS_DELAY;
51b1130e 801 udelay(torture_random(&rand) & 0x3ff);
ad0dc7f9 802 rcu_torture_writer_state = RTWS_REPLACE;
0ddea0ea
PM
803 old_rp = rcu_dereference_check(rcu_torture_current,
804 current == writer_task);
996417d2 805 rp->rtort_mbtest = 1;
a241ec65 806 rcu_assign_pointer(rcu_torture_current, rp);
9b2619af 807 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
c8e5b163 808 if (old_rp) {
a241ec65
PM
809 i = old_rp->rtort_pipe_count;
810 if (i > RCU_TORTURE_PIPE_LEN)
811 i = RCU_TORTURE_PIPE_LEN;
812 atomic_inc(&rcu_torture_wcount[i]);
813 old_rp->rtort_pipe_count++;
a48f3fad
PM
814 switch (synctype[torture_random(&rand) % nsynctypes]) {
815 case RTWS_DEF_FREE:
ad0dc7f9 816 rcu_torture_writer_state = RTWS_DEF_FREE;
2ec1f2d9 817 cur_ops->deferred_free(old_rp);
a48f3fad
PM
818 break;
819 case RTWS_EXP_SYNC:
ad0dc7f9 820 rcu_torture_writer_state = RTWS_EXP_SYNC;
2ec1f2d9 821 cur_ops->exp_sync();
a48f3fad
PM
822 rcu_torture_pipe_update(old_rp);
823 break;
824 case RTWS_COND_GET:
825 rcu_torture_writer_state = RTWS_COND_GET;
826 gp_snap = cur_ops->get_state();
827 i = torture_random(&rand) % 16;
828 if (i != 0)
829 schedule_timeout_interruptible(i);
830 udelay(torture_random(&rand) % 1000);
831 rcu_torture_writer_state = RTWS_COND_SYNC;
832 cur_ops->cond_sync(gp_snap);
833 rcu_torture_pipe_update(old_rp);
834 break;
f0bf8fab
PM
835 case RTWS_SYNC:
836 rcu_torture_writer_state = RTWS_SYNC;
837 cur_ops->sync();
838 rcu_torture_pipe_update(old_rp);
839 break;
a48f3fad
PM
840 default:
841 WARN_ON_ONCE(1);
842 break;
2ec1f2d9 843 }
a241ec65 844 }
4a298656 845 rcutorture_record_progress(++rcu_torture_current_version);
ad0dc7f9 846 rcu_torture_writer_state = RTWS_STUTTER;
628edaa5 847 stutter_wait("rcu_torture_writer");
36970bb9 848 } while (!torture_must_stop());
ad0dc7f9 849 rcu_torture_writer_state = RTWS_STOPPING;
7fafaac5 850 torture_kthread_stopping("rcu_torture_writer");
a241ec65
PM
851 return 0;
852}
853
b772e1dd
JT
854/*
855 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
856 * delay between calls.
857 */
858static int
859rcu_torture_fakewriter(void *arg)
860{
51b1130e 861 DEFINE_TORTURE_RANDOM(rand);
b772e1dd 862
5ccf60f2 863 VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
d277d868 864 set_user_nice(current, MAX_NICE);
b772e1dd
JT
865
866 do {
51b1130e
PM
867 schedule_timeout_uninterruptible(1 + torture_random(&rand)%10);
868 udelay(torture_random(&rand) & 0x3ff);
72472a02 869 if (cur_ops->cb_barrier != NULL &&
51b1130e 870 torture_random(&rand) % (nfakewriters * 8) == 0) {
72472a02 871 cur_ops->cb_barrier();
2ec1f2d9 872 } else if (gp_normal == gp_exp) {
51b1130e 873 if (torture_random(&rand) & 0x80)
2ec1f2d9
PM
874 cur_ops->sync();
875 else
876 cur_ops->exp_sync();
877 } else if (gp_normal) {
72472a02 878 cur_ops->sync();
2ec1f2d9
PM
879 } else {
880 cur_ops->exp_sync();
881 }
628edaa5 882 stutter_wait("rcu_torture_fakewriter");
36970bb9 883 } while (!torture_must_stop());
b772e1dd 884
7fafaac5 885 torture_kthread_stopping("rcu_torture_fakewriter");
b772e1dd
JT
886 return 0;
887}
888
b3b8a4d4 889static void rcutorture_trace_dump(void)
91afaf30
PM
890{
891 static atomic_t beenhere = ATOMIC_INIT(0);
892
893 if (atomic_read(&beenhere))
894 return;
895 if (atomic_xchg(&beenhere, 1) != 0)
896 return;
91afaf30
PM
897 ftrace_dump(DUMP_ALL);
898}
899
0729fbf3
PM
900/*
901 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
902 * incrementing the corresponding element of the pipeline array. The
903 * counter in the element should never be greater than 1, otherwise, the
904 * RCU implementation is broken.
905 */
906static void rcu_torture_timer(unsigned long unused)
907{
908 int idx;
909 int completed;
52494535 910 int completed_end;
51b1130e 911 static DEFINE_TORTURE_RANDOM(rand);
0729fbf3
PM
912 static DEFINE_SPINLOCK(rand_lock);
913 struct rcu_torture *p;
914 int pipe_count;
52494535 915 unsigned long long ts;
0729fbf3
PM
916
917 idx = cur_ops->readlock();
918 completed = cur_ops->completed();
e4aa0da3 919 ts = rcu_trace_clock_local();
632ee200 920 p = rcu_dereference_check(rcu_torture_current,
632ee200
PM
921 rcu_read_lock_bh_held() ||
922 rcu_read_lock_sched_held() ||
923 srcu_read_lock_held(&srcu_ctl));
0729fbf3
PM
924 if (p == NULL) {
925 /* Leave because rcu_torture_writer is not yet underway */
926 cur_ops->readunlock(idx);
927 return;
928 }
929 if (p->rtort_mbtest == 0)
930 atomic_inc(&n_rcu_torture_mberror);
931 spin_lock(&rand_lock);
0acc512c 932 cur_ops->read_delay(&rand);
0729fbf3
PM
933 n_rcu_torture_timers++;
934 spin_unlock(&rand_lock);
935 preempt_disable();
936 pipe_count = p->rtort_pipe_count;
937 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
938 /* Should not happen, but... */
939 pipe_count = RCU_TORTURE_PIPE_LEN;
940 }
52494535
PM
941 completed_end = cur_ops->completed();
942 if (pipe_count > 1) {
52494535
PM
943 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
944 completed, completed_end);
91afaf30 945 rcutorture_trace_dump();
52494535 946 }
dd17c8f7 947 __this_cpu_inc(rcu_torture_count[pipe_count]);
52494535 948 completed = completed_end - completed;
0729fbf3
PM
949 if (completed > RCU_TORTURE_PIPE_LEN) {
950 /* Should not happen, but... */
951 completed = RCU_TORTURE_PIPE_LEN;
952 }
dd17c8f7 953 __this_cpu_inc(rcu_torture_batch[completed]);
0729fbf3
PM
954 preempt_enable();
955 cur_ops->readunlock(idx);
956}
957
a241ec65
PM
958/*
959 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
960 * incrementing the corresponding element of the pipeline array. The
961 * counter in the element should never be greater than 1, otherwise, the
962 * RCU implementation is broken.
963 */
964static int
965rcu_torture_reader(void *arg)
966{
967 int completed;
52494535 968 int completed_end;
72e9bb54 969 int idx;
51b1130e 970 DEFINE_TORTURE_RANDOM(rand);
a241ec65
PM
971 struct rcu_torture *p;
972 int pipe_count;
0729fbf3 973 struct timer_list t;
52494535 974 unsigned long long ts;
a241ec65 975
5ccf60f2 976 VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
d277d868 977 set_user_nice(current, MAX_NICE);
0acc512c 978 if (irqreader && cur_ops->irq_capable)
0729fbf3 979 setup_timer_on_stack(&t, rcu_torture_timer, 0);
dbdf65b1 980
a241ec65 981 do {
0acc512c 982 if (irqreader && cur_ops->irq_capable) {
0729fbf3 983 if (!timer_pending(&t))
6155fec9 984 mod_timer(&t, jiffies + 1);
0729fbf3 985 }
72e9bb54
PM
986 idx = cur_ops->readlock();
987 completed = cur_ops->completed();
e4aa0da3 988 ts = rcu_trace_clock_local();
632ee200 989 p = rcu_dereference_check(rcu_torture_current,
632ee200
PM
990 rcu_read_lock_bh_held() ||
991 rcu_read_lock_sched_held() ||
992 srcu_read_lock_held(&srcu_ctl));
a241ec65
PM
993 if (p == NULL) {
994 /* Wait for rcu_torture_writer to get underway */
72e9bb54 995 cur_ops->readunlock(idx);
a241ec65
PM
996 schedule_timeout_interruptible(HZ);
997 continue;
998 }
996417d2
PM
999 if (p->rtort_mbtest == 0)
1000 atomic_inc(&n_rcu_torture_mberror);
0acc512c 1001 cur_ops->read_delay(&rand);
a241ec65
PM
1002 preempt_disable();
1003 pipe_count = p->rtort_pipe_count;
1004 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1005 /* Should not happen, but... */
1006 pipe_count = RCU_TORTURE_PIPE_LEN;
1007 }
52494535
PM
1008 completed_end = cur_ops->completed();
1009 if (pipe_count > 1) {
52494535
PM
1010 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1011 ts, completed, completed_end);
91afaf30 1012 rcutorture_trace_dump();
52494535 1013 }
dd17c8f7 1014 __this_cpu_inc(rcu_torture_count[pipe_count]);
52494535 1015 completed = completed_end - completed;
a241ec65
PM
1016 if (completed > RCU_TORTURE_PIPE_LEN) {
1017 /* Should not happen, but... */
1018 completed = RCU_TORTURE_PIPE_LEN;
1019 }
dd17c8f7 1020 __this_cpu_inc(rcu_torture_batch[completed]);
a241ec65 1021 preempt_enable();
72e9bb54 1022 cur_ops->readunlock(idx);
64e4b43a 1023 cond_resched();
628edaa5 1024 stutter_wait("rcu_torture_reader");
36970bb9 1025 } while (!torture_must_stop());
0acc512c 1026 if (irqreader && cur_ops->irq_capable)
0729fbf3 1027 del_timer_sync(&t);
7fafaac5 1028 torture_kthread_stopping("rcu_torture_reader");
a241ec65
PM
1029 return 0;
1030}
1031
1032/*
1033 * Create an RCU-torture statistics message in the specified buffer.
1034 */
d1008950 1035static void
a241ec65
PM
1036rcu_torture_printk(char *page)
1037{
a241ec65
PM
1038 int cpu;
1039 int i;
1040 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1041 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
ad0dc7f9 1042 static unsigned long rtcv_snap = ULONG_MAX;
a241ec65 1043
0a945022 1044 for_each_possible_cpu(cpu) {
a241ec65
PM
1045 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1046 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1047 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1048 }
1049 }
1050 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1051 if (pipesummary[i] != 0)
1052 break;
1053 }
d1008950
CG
1054 page += sprintf(page, "%s%s ", torture_type, TORTURE_FLAG);
1055 page += sprintf(page,
5cf05ad7 1056 "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
a241ec65
PM
1057 rcu_torture_current,
1058 rcu_torture_current_version,
1059 list_empty(&rcu_torture_freelist),
1060 atomic_read(&n_rcu_torture_alloc),
1061 atomic_read(&n_rcu_torture_alloc_fail),
5cf05ad7 1062 atomic_read(&n_rcu_torture_free));
d1008950 1063 page += sprintf(page, "rtmbe: %d rtbke: %ld rtbre: %ld ",
0729fbf3 1064 atomic_read(&n_rcu_torture_mberror),
8e8be45e 1065 n_rcu_torture_boost_ktrerror,
5cf05ad7 1066 n_rcu_torture_boost_rterror);
d1008950 1067 page += sprintf(page, "rtbf: %ld rtb: %ld nt: %ld ",
8e8be45e
PM
1068 n_rcu_torture_boost_failure,
1069 n_rcu_torture_boosts,
5cf05ad7 1070 n_rcu_torture_timers);
2e9e8081 1071 page = torture_onoff_stats(page);
d1008950 1072 page += sprintf(page, "barrier: %ld/%ld:%ld",
fae4b54f
PM
1073 n_barrier_successes,
1074 n_barrier_attempts,
1075 n_rcu_torture_barrier_error);
d1008950 1076 page += sprintf(page, "\n%s%s ", torture_type, TORTURE_FLAG);
8e8be45e 1077 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
fae4b54f 1078 n_rcu_torture_barrier_error != 0 ||
8e8be45e
PM
1079 n_rcu_torture_boost_ktrerror != 0 ||
1080 n_rcu_torture_boost_rterror != 0 ||
fae4b54f
PM
1081 n_rcu_torture_boost_failure != 0 ||
1082 i > 1) {
d1008950 1083 page += sprintf(page, "!!! ");
996417d2 1084 atomic_inc(&n_rcu_torture_error);
5af970a4 1085 WARN_ON_ONCE(1);
996417d2 1086 }
d1008950 1087 page += sprintf(page, "Reader Pipe: ");
a241ec65 1088 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
d1008950
CG
1089 page += sprintf(page, " %ld", pipesummary[i]);
1090 page += sprintf(page, "\n%s%s ", torture_type, TORTURE_FLAG);
1091 page += sprintf(page, "Reader Batch: ");
72e9bb54 1092 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
d1008950
CG
1093 page += sprintf(page, " %ld", batchsummary[i]);
1094 page += sprintf(page, "\n%s%s ", torture_type, TORTURE_FLAG);
1095 page += sprintf(page, "Free-Block Circulation: ");
a241ec65 1096 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
d1008950 1097 page += sprintf(page, " %d",
a241ec65
PM
1098 atomic_read(&rcu_torture_wcount[i]));
1099 }
d1008950 1100 page += sprintf(page, "\n");
c8e5b163 1101 if (cur_ops->stats)
d1008950 1102 cur_ops->stats(page);
ad0dc7f9
PM
1103 if (rtcv_snap == rcu_torture_current_version &&
1104 rcu_torture_current != NULL) {
1105 int __maybe_unused flags;
1106 unsigned long __maybe_unused gpnum;
1107 unsigned long __maybe_unused completed;
1108
1109 rcutorture_get_gp_data(cur_ops->ttype,
1110 &flags, &gpnum, &completed);
1111 page += sprintf(page,
1112 "??? Writer stall state %d g%lu c%lu f%#x\n",
1113 rcu_torture_writer_state,
1114 gpnum, completed, flags);
afea227f 1115 show_rcu_gp_kthreads();
945fa9c6 1116 rcutorture_trace_dump();
ad0dc7f9
PM
1117 }
1118 rtcv_snap = rcu_torture_current_version;
a241ec65
PM
1119}
1120
1121/*
1122 * Print torture statistics. Caller must ensure that there is only
1123 * one call to this function at a given time!!! This is normally
1124 * accomplished by relying on the module system to only have one copy
1125 * of the module loaded, and then by giving the rcu_torture_stats
1126 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1127 * thread is not running).
1128 */
1129static void
1130rcu_torture_stats_print(void)
1131{
d1008950
CG
1132 int size = nr_cpu_ids * 200 + 8192;
1133 char *buf;
a241ec65 1134
d1008950
CG
1135 buf = kmalloc(size, GFP_KERNEL);
1136 if (!buf) {
1137 pr_err("rcu-torture: Out of memory, need: %d", size);
1138 return;
1139 }
1140 rcu_torture_printk(buf);
1141 pr_alert("%s", buf);
1142 kfree(buf);
a241ec65
PM
1143}
1144
1145/*
1146 * Periodically prints torture statistics, if periodic statistics printing
1147 * was specified via the stat_interval module parameter.
a241ec65
PM
1148 */
1149static int
1150rcu_torture_stats(void *arg)
1151{
5ccf60f2 1152 VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
a241ec65
PM
1153 do {
1154 schedule_timeout_interruptible(stat_interval * HZ);
1155 rcu_torture_stats_print();
f67a3356 1156 torture_shutdown_absorb("rcu_torture_stats");
36970bb9 1157 } while (!torture_must_stop());
7fafaac5 1158 torture_kthread_stopping("rcu_torture_stats");
d120f65f
PM
1159 return 0;
1160}
1161
95c38322 1162static inline void
e66c33d5 1163rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
95c38322 1164{
2caa1e44
PM
1165 pr_alert("%s" TORTURE_FLAG
1166 "--- %s: nreaders=%d nfakewriters=%d "
1167 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1168 "shuffle_interval=%d stutter=%d irqreader=%d "
1169 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1170 "test_boost=%d/%d test_boost_interval=%d "
1171 "test_boost_duration=%d shutdown_secs=%d "
67afeed2
PM
1172 "stall_cpu=%d stall_cpu_holdoff=%d "
1173 "n_barrier_cbs=%d "
2caa1e44
PM
1174 "onoff_interval=%d onoff_holdoff=%d\n",
1175 torture_type, tag, nrealreaders, nfakewriters,
1176 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1177 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1178 test_boost, cur_ops->can_boost,
1179 test_boost_interval, test_boost_duration, shutdown_secs,
67afeed2
PM
1180 stall_cpu, stall_cpu_holdoff,
1181 n_barrier_cbs,
2caa1e44 1182 onoff_interval, onoff_holdoff);
95c38322
PM
1183}
1184
8e8be45e
PM
1185static void rcutorture_booster_cleanup(int cpu)
1186{
1187 struct task_struct *t;
1188
1189 if (boost_tasks[cpu] == NULL)
1190 return;
1191 mutex_lock(&boost_mutex);
8e8be45e
PM
1192 t = boost_tasks[cpu];
1193 boost_tasks[cpu] = NULL;
1194 mutex_unlock(&boost_mutex);
1195
1196 /* This must be outside of the mutex, otherwise deadlock! */
9c029b86 1197 torture_stop_kthread(rcu_torture_boost, t);
8e8be45e
PM
1198}
1199
1200static int rcutorture_booster_init(int cpu)
1201{
1202 int retval;
1203
1204 if (boost_tasks[cpu] != NULL)
1205 return 0; /* Already created, nothing more to do. */
1206
1207 /* Don't allow time recalculation while creating a new task. */
1208 mutex_lock(&boost_mutex);
5ccf60f2 1209 VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
1f288094
ED
1210 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1211 cpu_to_node(cpu),
1212 "rcu_torture_boost");
8e8be45e
PM
1213 if (IS_ERR(boost_tasks[cpu])) {
1214 retval = PTR_ERR(boost_tasks[cpu]);
5ccf60f2 1215 VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
8e8be45e
PM
1216 n_rcu_torture_boost_ktrerror++;
1217 boost_tasks[cpu] = NULL;
1218 mutex_unlock(&boost_mutex);
1219 return retval;
1220 }
1221 kthread_bind(boost_tasks[cpu], cpu);
1222 wake_up_process(boost_tasks[cpu]);
1223 mutex_unlock(&boost_mutex);
1224 return 0;
1225}
1226
c13f3757
PM
1227/*
1228 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1229 * induces a CPU stall for the time specified by stall_cpu.
1230 */
49fb4c62 1231static int rcu_torture_stall(void *args)
c13f3757
PM
1232{
1233 unsigned long stop_at;
1234
5ccf60f2 1235 VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
c13f3757 1236 if (stall_cpu_holdoff > 0) {
5ccf60f2 1237 VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
c13f3757 1238 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
5ccf60f2 1239 VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
c13f3757
PM
1240 }
1241 if (!kthread_should_stop()) {
1242 stop_at = get_seconds() + stall_cpu;
1243 /* RCU CPU stall is expected behavior in following code. */
2caa1e44 1244 pr_alert("rcu_torture_stall start.\n");
c13f3757
PM
1245 rcu_read_lock();
1246 preempt_disable();
1247 while (ULONG_CMP_LT(get_seconds(), stop_at))
1248 continue; /* Induce RCU CPU stall warning. */
1249 preempt_enable();
1250 rcu_read_unlock();
2caa1e44 1251 pr_alert("rcu_torture_stall end.\n");
c13f3757 1252 }
f67a3356 1253 torture_shutdown_absorb("rcu_torture_stall");
c13f3757
PM
1254 while (!kthread_should_stop())
1255 schedule_timeout_interruptible(10 * HZ);
1256 return 0;
1257}
1258
1259/* Spawn CPU-stall kthread, if stall_cpu specified. */
1260static int __init rcu_torture_stall_init(void)
1261{
c13f3757
PM
1262 if (stall_cpu <= 0)
1263 return 0;
47cf29b9 1264 return torture_create_kthread(rcu_torture_stall, NULL, stall_task);
c13f3757
PM
1265}
1266
fae4b54f 1267/* Callback function for RCU barrier testing. */
b3b8a4d4 1268static void rcu_torture_barrier_cbf(struct rcu_head *rcu)
fae4b54f
PM
1269{
1270 atomic_inc(&barrier_cbs_invoked);
1271}
1272
1273/* kthread function to register callbacks used to test RCU barriers. */
1274static int rcu_torture_barrier_cbs(void *arg)
1275{
1276 long myid = (long)arg;
c6ebcbb6 1277 bool lastphase = 0;
78e4bc34 1278 bool newphase;
fae4b54f
PM
1279 struct rcu_head rcu;
1280
1281 init_rcu_head_on_stack(&rcu);
5ccf60f2 1282 VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
d277d868 1283 set_user_nice(current, MAX_NICE);
fae4b54f
PM
1284 do {
1285 wait_event(barrier_cbs_wq[myid],
78e4bc34
PM
1286 (newphase =
1287 ACCESS_ONCE(barrier_phase)) != lastphase ||
36970bb9 1288 torture_must_stop());
78e4bc34 1289 lastphase = newphase;
c6ebcbb6 1290 smp_mb(); /* ensure barrier_phase load before ->call(). */
36970bb9 1291 if (torture_must_stop())
fae4b54f
PM
1292 break;
1293 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1294 if (atomic_dec_and_test(&barrier_cbs_count))
1295 wake_up(&barrier_wq);
36970bb9 1296 } while (!torture_must_stop());
fae4b54f
PM
1297 cur_ops->cb_barrier();
1298 destroy_rcu_head_on_stack(&rcu);
7fafaac5 1299 torture_kthread_stopping("rcu_torture_barrier_cbs");
fae4b54f
PM
1300 return 0;
1301}
1302
1303/* kthread function to drive and coordinate RCU barrier testing. */
1304static int rcu_torture_barrier(void *arg)
1305{
1306 int i;
1307
5ccf60f2 1308 VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
fae4b54f
PM
1309 do {
1310 atomic_set(&barrier_cbs_invoked, 0);
1311 atomic_set(&barrier_cbs_count, n_barrier_cbs);
c6ebcbb6
PM
1312 smp_mb(); /* Ensure barrier_phase after prior assignments. */
1313 barrier_phase = !barrier_phase;
fae4b54f
PM
1314 for (i = 0; i < n_barrier_cbs; i++)
1315 wake_up(&barrier_cbs_wq[i]);
1316 wait_event(barrier_wq,
1317 atomic_read(&barrier_cbs_count) == 0 ||
36970bb9
PM
1318 torture_must_stop());
1319 if (torture_must_stop())
fae4b54f
PM
1320 break;
1321 n_barrier_attempts++;
78e4bc34 1322 cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
fae4b54f
PM
1323 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1324 n_rcu_torture_barrier_error++;
1325 WARN_ON_ONCE(1);
1326 }
1327 n_barrier_successes++;
1328 schedule_timeout_interruptible(HZ / 10);
36970bb9 1329 } while (!torture_must_stop());
7fafaac5 1330 torture_kthread_stopping("rcu_torture_barrier");
fae4b54f
PM
1331 return 0;
1332}
1333
1334/* Initialize RCU barrier testing. */
1335static int rcu_torture_barrier_init(void)
1336{
1337 int i;
1338 int ret;
1339
1340 if (n_barrier_cbs == 0)
1341 return 0;
1342 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
2caa1e44
PM
1343 pr_alert("%s" TORTURE_FLAG
1344 " Call or barrier ops missing for %s,\n",
1345 torture_type, cur_ops->name);
1346 pr_alert("%s" TORTURE_FLAG
1347 " RCU barrier testing omitted from run.\n",
1348 torture_type);
fae4b54f
PM
1349 return 0;
1350 }
1351 atomic_set(&barrier_cbs_count, 0);
1352 atomic_set(&barrier_cbs_invoked, 0);
1353 barrier_cbs_tasks =
1354 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
1355 GFP_KERNEL);
1356 barrier_cbs_wq =
1357 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
1358 GFP_KERNEL);
de5e6437 1359 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
fae4b54f
PM
1360 return -ENOMEM;
1361 for (i = 0; i < n_barrier_cbs; i++) {
1362 init_waitqueue_head(&barrier_cbs_wq[i]);
47cf29b9
PM
1363 ret = torture_create_kthread(rcu_torture_barrier_cbs,
1364 (void *)(long)i,
1365 barrier_cbs_tasks[i]);
1366 if (ret)
fae4b54f 1367 return ret;
fae4b54f 1368 }
47cf29b9 1369 return torture_create_kthread(rcu_torture_barrier, NULL, barrier_task);
fae4b54f
PM
1370}
1371
1372/* Clean up after RCU barrier testing. */
1373static void rcu_torture_barrier_cleanup(void)
1374{
1375 int i;
1376
9c029b86 1377 torture_stop_kthread(rcu_torture_barrier, barrier_task);
fae4b54f 1378 if (barrier_cbs_tasks != NULL) {
9c029b86
PM
1379 for (i = 0; i < n_barrier_cbs; i++)
1380 torture_stop_kthread(rcu_torture_barrier_cbs,
1381 barrier_cbs_tasks[i]);
fae4b54f
PM
1382 kfree(barrier_cbs_tasks);
1383 barrier_cbs_tasks = NULL;
1384 }
1385 if (barrier_cbs_wq != NULL) {
1386 kfree(barrier_cbs_wq);
1387 barrier_cbs_wq = NULL;
1388 }
1389}
1390
8e8be45e
PM
1391static int rcutorture_cpu_notify(struct notifier_block *self,
1392 unsigned long action, void *hcpu)
1393{
1394 long cpu = (long)hcpu;
1395
1396 switch (action) {
1397 case CPU_ONLINE:
1398 case CPU_DOWN_FAILED:
1399 (void)rcutorture_booster_init(cpu);
1400 break;
1401 case CPU_DOWN_PREPARE:
1402 rcutorture_booster_cleanup(cpu);
1403 break;
1404 default:
1405 break;
1406 }
1407 return NOTIFY_OK;
1408}
1409
1410static struct notifier_block rcutorture_cpu_nb = {
1411 .notifier_call = rcutorture_cpu_notify,
1412};
1413
a241ec65
PM
1414static void
1415rcu_torture_cleanup(void)
1416{
1417 int i;
1418
4a298656 1419 rcutorture_record_test_transition();
cc47ae08 1420 if (torture_cleanup()) {
343e9099
PM
1421 if (cur_ops->cb_barrier != NULL)
1422 cur_ops->cb_barrier();
1423 return;
1424 }
d84f5203 1425
fae4b54f 1426 rcu_torture_barrier_cleanup();
9c029b86 1427 torture_stop_kthread(rcu_torture_stall, stall_task);
9c029b86 1428 torture_stop_kthread(rcu_torture_writer, writer_task);
a241ec65 1429
c8e5b163 1430 if (reader_tasks) {
9c029b86
PM
1431 for (i = 0; i < nrealreaders; i++)
1432 torture_stop_kthread(rcu_torture_reader,
1433 reader_tasks[i]);
a241ec65 1434 kfree(reader_tasks);
a241ec65
PM
1435 }
1436 rcu_torture_current = NULL;
1437
c8e5b163 1438 if (fakewriter_tasks) {
b772e1dd 1439 for (i = 0; i < nfakewriters; i++) {
9c029b86
PM
1440 torture_stop_kthread(rcu_torture_fakewriter,
1441 fakewriter_tasks[i]);
b772e1dd
JT
1442 }
1443 kfree(fakewriter_tasks);
1444 fakewriter_tasks = NULL;
1445 }
1446
9c029b86
PM
1447 torture_stop_kthread(rcu_torture_stats, stats_task);
1448 torture_stop_kthread(rcu_torture_fqs, fqs_task);
8e8be45e
PM
1449 if ((test_boost == 1 && cur_ops->can_boost) ||
1450 test_boost == 2) {
1451 unregister_cpu_notifier(&rcutorture_cpu_nb);
1452 for_each_possible_cpu(i)
1453 rcutorture_booster_cleanup(i);
1454 }
bf66f18e 1455
a241ec65 1456 /* Wait for all RCU callbacks to fire. */
2326974d
PM
1457
1458 if (cur_ops->cb_barrier != NULL)
1459 cur_ops->cb_barrier();
a241ec65 1460
a241ec65 1461 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
72e9bb54 1462
fae4b54f 1463 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
8e8be45e 1464 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
2e9e8081 1465 else if (torture_onoff_failures())
091541bb
PM
1466 rcu_torture_print_module_parms(cur_ops,
1467 "End of test: RCU_HOTPLUG");
95c38322 1468 else
8e8be45e 1469 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
a241ec65
PM
1470}
1471
d2818df1
PM
1472#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1473static void rcu_torture_leak_cb(struct rcu_head *rhp)
1474{
1475}
1476
1477static void rcu_torture_err_cb(struct rcu_head *rhp)
1478{
1479 /*
1480 * This -might- happen due to race conditions, but is unlikely.
1481 * The scenario that leads to this happening is that the
1482 * first of the pair of duplicate callbacks is queued,
1483 * someone else starts a grace period that includes that
1484 * callback, then the second of the pair must wait for the
1485 * next grace period. Unlikely, but can happen. If it
1486 * does happen, the debug-objects subsystem won't have splatted.
1487 */
1488 pr_alert("rcutorture: duplicated callback was invoked.\n");
1489}
1490#endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1491
1492/*
1493 * Verify that double-free causes debug-objects to complain, but only
1494 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
1495 * cannot be carried out.
1496 */
1497static void rcu_test_debug_objects(void)
1498{
1499#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1500 struct rcu_head rh1;
1501 struct rcu_head rh2;
1502
1503 init_rcu_head_on_stack(&rh1);
1504 init_rcu_head_on_stack(&rh2);
1505 pr_alert("rcutorture: WARN: Duplicate call_rcu() test starting.\n");
1506
1507 /* Try to queue the rh2 pair of callbacks for the same grace period. */
1508 preempt_disable(); /* Prevent preemption from interrupting test. */
1509 rcu_read_lock(); /* Make it impossible to finish a grace period. */
1510 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
1511 local_irq_disable(); /* Make it harder to start a new grace period. */
1512 call_rcu(&rh2, rcu_torture_leak_cb);
1513 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
1514 local_irq_enable();
1515 rcu_read_unlock();
1516 preempt_enable();
1517
1518 /* Wait for them all to get done so we can safely return. */
1519 rcu_barrier();
1520 pr_alert("rcutorture: WARN: Duplicate call_rcu() test complete.\n");
1521 destroy_rcu_head_on_stack(&rh1);
1522 destroy_rcu_head_on_stack(&rh2);
1523#else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1524 pr_alert("rcutorture: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n");
1525#endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1526}
1527
6f8bc500 1528static int __init
a241ec65
PM
1529rcu_torture_init(void)
1530{
1531 int i;
1532 int cpu;
1533 int firsterr = 0;
2ec1f2d9 1534 static struct rcu_torture_ops *torture_ops[] = {
ff20e251 1535 &rcu_ops, &rcu_bh_ops, &rcu_busted_ops, &srcu_ops, &sched_ops,
2ec1f2d9 1536 };
a241ec65 1537
628edaa5 1538 torture_init_begin(torture_type, verbose, &rcutorture_runnable);
343e9099 1539
a241ec65 1540 /* Process args and tell the world that the torturer is on the job. */
ade5fb81 1541 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
72e9bb54 1542 cur_ops = torture_ops[i];
ade5fb81 1543 if (strcmp(torture_type, cur_ops->name) == 0)
72e9bb54 1544 break;
72e9bb54 1545 }
ade5fb81 1546 if (i == ARRAY_SIZE(torture_ops)) {
2caa1e44
PM
1547 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
1548 torture_type);
1549 pr_alert("rcu-torture types:");
cf886c44 1550 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
2caa1e44
PM
1551 pr_alert(" %s", torture_ops[i]->name);
1552 pr_alert("\n");
b5daa8f3 1553 torture_init_end();
a71fca58 1554 return -EINVAL;
72e9bb54 1555 }
bf66f18e 1556 if (cur_ops->fqs == NULL && fqs_duration != 0) {
2caa1e44 1557 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
bf66f18e
PM
1558 fqs_duration = 0;
1559 }
c8e5b163 1560 if (cur_ops->init)
72e9bb54
PM
1561 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1562
64e4b43a 1563 if (nreaders >= 0) {
a241ec65 1564 nrealreaders = nreaders;
64e4b43a
PM
1565 } else {
1566 nrealreaders = num_online_cpus() - 1;
1567 if (nrealreaders <= 0)
1568 nrealreaders = 1;
1569 }
8e8be45e 1570 rcu_torture_print_module_parms(cur_ops, "Start of test");
a241ec65
PM
1571
1572 /* Set up the freelist. */
1573
1574 INIT_LIST_HEAD(&rcu_torture_freelist);
788e770e 1575 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
996417d2 1576 rcu_tortures[i].rtort_mbtest = 0;
a241ec65
PM
1577 list_add_tail(&rcu_tortures[i].rtort_free,
1578 &rcu_torture_freelist);
1579 }
1580
1581 /* Initialize the statistics so that each run gets its own numbers. */
1582
1583 rcu_torture_current = NULL;
1584 rcu_torture_current_version = 0;
1585 atomic_set(&n_rcu_torture_alloc, 0);
1586 atomic_set(&n_rcu_torture_alloc_fail, 0);
1587 atomic_set(&n_rcu_torture_free, 0);
996417d2
PM
1588 atomic_set(&n_rcu_torture_mberror, 0);
1589 atomic_set(&n_rcu_torture_error, 0);
fae4b54f 1590 n_rcu_torture_barrier_error = 0;
8e8be45e
PM
1591 n_rcu_torture_boost_ktrerror = 0;
1592 n_rcu_torture_boost_rterror = 0;
8e8be45e
PM
1593 n_rcu_torture_boost_failure = 0;
1594 n_rcu_torture_boosts = 0;
a241ec65
PM
1595 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1596 atomic_set(&rcu_torture_wcount[i], 0);
0a945022 1597 for_each_possible_cpu(cpu) {
a241ec65
PM
1598 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1599 per_cpu(rcu_torture_count, cpu)[i] = 0;
1600 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1601 }
1602 }
1603
1604 /* Start up the kthreads. */
1605
47cf29b9
PM
1606 firsterr = torture_create_kthread(rcu_torture_writer, NULL,
1607 writer_task);
1608 if (firsterr)
a241ec65 1609 goto unwind;
b772e1dd 1610 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
a71fca58 1611 GFP_KERNEL);
b772e1dd 1612 if (fakewriter_tasks == NULL) {
5ccf60f2 1613 VERBOSE_TOROUT_ERRSTRING("out of memory");
b772e1dd
JT
1614 firsterr = -ENOMEM;
1615 goto unwind;
1616 }
1617 for (i = 0; i < nfakewriters; i++) {
47cf29b9
PM
1618 firsterr = torture_create_kthread(rcu_torture_fakewriter,
1619 NULL, fakewriter_tasks[i]);
1620 if (firsterr)
b772e1dd 1621 goto unwind;
b772e1dd 1622 }
2860aaba 1623 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
a241ec65
PM
1624 GFP_KERNEL);
1625 if (reader_tasks == NULL) {
5ccf60f2 1626 VERBOSE_TOROUT_ERRSTRING("out of memory");
a241ec65
PM
1627 firsterr = -ENOMEM;
1628 goto unwind;
1629 }
1630 for (i = 0; i < nrealreaders; i++) {
47cf29b9
PM
1631 firsterr = torture_create_kthread(rcu_torture_reader, NULL,
1632 reader_tasks[i]);
1633 if (firsterr)
a241ec65 1634 goto unwind;
a241ec65
PM
1635 }
1636 if (stat_interval > 0) {
47cf29b9
PM
1637 firsterr = torture_create_kthread(rcu_torture_stats, NULL,
1638 stats_task);
1639 if (firsterr)
a241ec65 1640 goto unwind;
a241ec65 1641 }
d84f5203 1642 if (test_no_idle_hz) {
3808dc9f
PM
1643 firsterr = torture_shuffle_init(shuffle_interval * HZ);
1644 if (firsterr)
d84f5203 1645 goto unwind;
d84f5203 1646 }
d120f65f
PM
1647 if (stutter < 0)
1648 stutter = 0;
1649 if (stutter) {
628edaa5
PM
1650 firsterr = torture_stutter_init(stutter * HZ);
1651 if (firsterr)
d120f65f 1652 goto unwind;
d120f65f 1653 }
bf66f18e
PM
1654 if (fqs_duration < 0)
1655 fqs_duration = 0;
1656 if (fqs_duration) {
628edaa5 1657 /* Create the fqs thread */
d0d0606e
PM
1658 firsterr = torture_create_kthread(rcu_torture_fqs, NULL,
1659 fqs_task);
47cf29b9 1660 if (firsterr)
bf66f18e 1661 goto unwind;
bf66f18e 1662 }
8e8be45e
PM
1663 if (test_boost_interval < 1)
1664 test_boost_interval = 1;
1665 if (test_boost_duration < 2)
1666 test_boost_duration = 2;
1667 if ((test_boost == 1 && cur_ops->can_boost) ||
1668 test_boost == 2) {
8e8be45e
PM
1669
1670 boost_starttime = jiffies + test_boost_interval * HZ;
1671 register_cpu_notifier(&rcutorture_cpu_nb);
1672 for_each_possible_cpu(i) {
1673 if (cpu_is_offline(i))
1674 continue; /* Heuristic: CPU can go offline. */
01025ebc
PM
1675 firsterr = rcutorture_booster_init(i);
1676 if (firsterr)
8e8be45e 1677 goto unwind;
8e8be45e
PM
1678 }
1679 }
01025ebc
PM
1680 firsterr = torture_shutdown_init(shutdown_secs, rcu_torture_cleanup);
1681 if (firsterr)
37e377d2 1682 goto unwind;
01025ebc
PM
1683 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval * HZ);
1684 if (firsterr)
37e377d2 1685 goto unwind;
01025ebc
PM
1686 firsterr = rcu_torture_stall_init();
1687 if (firsterr)
37e377d2 1688 goto unwind;
01025ebc
PM
1689 firsterr = rcu_torture_barrier_init();
1690 if (firsterr)
fae4b54f 1691 goto unwind;
d2818df1
PM
1692 if (object_debug)
1693 rcu_test_debug_objects();
4a298656 1694 rcutorture_record_test_transition();
b5daa8f3 1695 torture_init_end();
a241ec65
PM
1696 return 0;
1697
1698unwind:
b5daa8f3 1699 torture_init_end();
a241ec65
PM
1700 rcu_torture_cleanup();
1701 return firsterr;
1702}
1703
1704module_init(rcu_torture_init);
1705module_exit(rcu_torture_cleanup);
This page took 0.695097 seconds and 5 git commands to generate.