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