rcutorture: Add cond_resched() to forward-progress free-up loop
[linux-block.git] / kernel / rcu / rcutorture.c
CommitLineData
2e24ce88 1// SPDX-License-Identifier: GPL-2.0+
a241ec65 2/*
29766f1e 3 * Read-Copy Update module-based torture test facility
a241ec65 4 *
b772e1dd 5 * Copyright (C) IBM Corporation, 2005, 2006
a241ec65 6 *
2e24ce88 7 * Authors: Paul E. McKenney <paulmck@linux.ibm.com>
e0198b29 8 * Josh Triplett <josh@joshtriplett.org>
a241ec65
PM
9 *
10 * See also: Documentation/RCU/torture.txt
11 */
60500037
PM
12
13#define pr_fmt(fmt) fmt
14
a241ec65
PM
15#include <linux/types.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/kthread.h>
20#include <linux/err.h>
21#include <linux/spinlock.h>
22#include <linux/smp.h>
23#include <linux/rcupdate.h>
24#include <linux/interrupt.h>
174cd4b1 25#include <linux/sched/signal.h>
ae7e81c0 26#include <uapi/linux/sched/types.h>
60063497 27#include <linux/atomic.h>
a241ec65 28#include <linux/bitops.h>
a241ec65
PM
29#include <linux/completion.h>
30#include <linux/moduleparam.h>
31#include <linux/percpu.h>
32#include <linux/notifier.h>
343e9099 33#include <linux/reboot.h>
83144186 34#include <linux/freezer.h>
a241ec65 35#include <linux/cpu.h>
a241ec65 36#include <linux/delay.h>
a241ec65 37#include <linux/stat.h>
b2896d2e 38#include <linux/srcu.h>
1aeb272c 39#include <linux/slab.h>
52494535 40#include <linux/trace_clock.h>
f07767fd 41#include <asm/byteorder.h>
51b1130e 42#include <linux/torture.h>
38706bc5 43#include <linux/vmalloc.h>
0032f4e8 44#include <linux/sched/debug.h>
450efca7 45#include <linux/sched/sysctl.h>
e0aff973 46#include <linux/oom.h>
a241ec65 47
25c36329
PM
48#include "rcu.h"
49
a241ec65 50MODULE_LICENSE("GPL");
2e24ce88 51MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com> and Josh Triplett <josh@joshtriplett.org>");
a241ec65 52
4102adab 53
2397d072
PM
54/* Bits for ->extendables field, extendables param, and related definitions. */
55#define RCUTORTURE_RDR_SHIFT 8 /* Put SRCU index in upper bits. */
56#define RCUTORTURE_RDR_MASK ((1 << RCUTORTURE_RDR_SHIFT) - 1)
2ceebc03
PM
57#define RCUTORTURE_RDR_BH 0x01 /* Extend readers by disabling bh. */
58#define RCUTORTURE_RDR_IRQ 0x02 /* ... disabling interrupts. */
59#define RCUTORTURE_RDR_PREEMPT 0x04 /* ... disabling preemption. */
60#define RCUTORTURE_RDR_RBH 0x08 /* ... rcu_read_lock_bh(). */
61#define RCUTORTURE_RDR_SCHED 0x10 /* ... rcu_read_lock_sched(). */
62#define RCUTORTURE_RDR_RCU 0x20 /* ... entering another RCU reader. */
63#define RCUTORTURE_RDR_NBITS 6 /* Number of bits defined above. */
64#define RCUTORTURE_MAX_EXTEND \
65 (RCUTORTURE_RDR_BH | RCUTORTURE_RDR_IRQ | RCUTORTURE_RDR_PREEMPT | \
66 RCUTORTURE_RDR_RBH | RCUTORTURE_RDR_SCHED)
2397d072
PM
67#define RCUTORTURE_RDR_MAX_LOOPS 0x7 /* Maximum reader extensions. */
68 /* Must be power of two minus one. */
c116dba6 69#define RCUTORTURE_RDR_MAX_SEGS (RCUTORTURE_RDR_MAX_LOOPS + 3)
2397d072 70
2397d072
PM
71torture_param(int, extendables, RCUTORTURE_MAX_EXTEND,
72 "Extend readers by disabling bh (1), irqs (2), or preempt (4)");
9e250225
PM
73torture_param(int, fqs_duration, 0,
74 "Duration of fqs bursts (us), 0 to disable");
75torture_param(int, fqs_holdoff, 0, "Holdoff time within fqs bursts (us)");
76torture_param(int, fqs_stutter, 3, "Wait time between fqs bursts (s)");
1b27291b
PM
77torture_param(bool, fwd_progress, 1, "Test grace-period forward progress");
78torture_param(int, fwd_progress_div, 4, "Fraction of CPU stall to wait");
79torture_param(int, fwd_progress_holdoff, 60,
80 "Time between forward-progress tests (s)");
81torture_param(bool, fwd_progress_need_resched, 1,
82 "Hide cond_resched() behind need_resched()");
a48f3fad 83torture_param(bool, gp_cond, false, "Use conditional/async GP wait primitives");
9e250225
PM
84torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
85torture_param(bool, gp_normal, false,
86 "Use normal (non-expedited) GP wait primitives");
f0bf8fab 87torture_param(bool, gp_sync, false, "Use synchronous GP wait primitives");
9e250225
PM
88torture_param(int, irqreader, 1, "Allow RCU readers from irq handlers");
89torture_param(int, n_barrier_cbs, 0,
90 "# of callbacks/kthreads for barrier testing");
91torture_param(int, nfakewriters, 4, "Number of RCU fake writer threads");
92torture_param(int, nreaders, -1, "Number of RCU reader threads");
93torture_param(int, object_debug, 0,
94 "Enable debug-object double call_rcu() testing");
95torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
96torture_param(int, onoff_interval, 0,
028be12b 97 "Time between CPU hotplugs (jiffies), 0=disable");
9e250225
PM
98torture_param(int, shuffle_interval, 3, "Number of seconds between shuffles");
99torture_param(int, shutdown_secs, 0, "Shutdown time (s), <= zero to disable.");
100torture_param(int, stall_cpu, 0, "Stall duration (s), zero to disable.");
101torture_param(int, stall_cpu_holdoff, 10,
102 "Time to wait before starting stall (s).");
2b1516e5 103torture_param(int, stall_cpu_irqsoff, 0, "Disable interrupts while stalling.");
9e250225
PM
104torture_param(int, stat_interval, 60,
105 "Number of seconds between stats printk()s");
106torture_param(int, stutter, 5, "Number of seconds to run/halt test");
107torture_param(int, test_boost, 1, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
108torture_param(int, test_boost_duration, 4,
109 "Duration of each boost test, seconds.");
110torture_param(int, test_boost_interval, 7,
111 "Interval between boost tests, seconds.");
112torture_param(bool, test_no_idle_hz, true,
113 "Test support for tickless idle CPUs");
90127d60 114torture_param(int, verbose, 1,
b5daa8f3 115 "Enable verbose debugging printk()s");
9e250225 116
d10453e9 117static char *torture_type = "rcu";
d6ad6711 118module_param(torture_type, charp, 0444);
c770c82a 119MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, srcu, ...)");
a241ec65 120
a241ec65
PM
121static int nrealreaders;
122static struct task_struct *writer_task;
b772e1dd 123static struct task_struct **fakewriter_tasks;
a241ec65
PM
124static struct task_struct **reader_tasks;
125static struct task_struct *stats_task;
bf66f18e 126static struct task_struct *fqs_task;
8e8be45e 127static struct task_struct *boost_tasks[NR_CPUS];
c13f3757 128static struct task_struct *stall_task;
1b27291b 129static struct task_struct *fwd_prog_task;
fae4b54f
PM
130static struct task_struct **barrier_cbs_tasks;
131static struct task_struct *barrier_task;
a241ec65
PM
132
133#define RCU_TORTURE_PIPE_LEN 10
134
135struct rcu_torture {
136 struct rcu_head rtort_rcu;
137 int rtort_pipe_count;
138 struct list_head rtort_free;
996417d2 139 int rtort_mbtest;
a241ec65
PM
140};
141
a241ec65 142static LIST_HEAD(rcu_torture_freelist);
0ddea0ea 143static struct rcu_torture __rcu *rcu_torture_current;
4a298656 144static unsigned long rcu_torture_current_version;
a241ec65
PM
145static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
146static DEFINE_SPINLOCK(rcu_torture_lock);
67522bee
PM
147static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count);
148static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch);
a241ec65 149static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
b2896d2e
PM
150static atomic_t n_rcu_torture_alloc;
151static atomic_t n_rcu_torture_alloc_fail;
152static atomic_t n_rcu_torture_free;
153static atomic_t n_rcu_torture_mberror;
154static atomic_t n_rcu_torture_error;
fae4b54f 155static long n_rcu_torture_barrier_error;
8e8be45e
PM
156static long n_rcu_torture_boost_ktrerror;
157static long n_rcu_torture_boost_rterror;
8e8be45e
PM
158static long n_rcu_torture_boost_failure;
159static long n_rcu_torture_boosts;
8da9a595 160static atomic_long_t n_rcu_torture_timers;
fae4b54f 161static long n_barrier_attempts;
bf5b6435 162static long n_barrier_successes; /* did rcu_barrier test succeed? */
e3033736 163static struct list_head rcu_torture_removed;
d120f65f 164
ad0dc7f9
PM
165static int rcu_torture_writer_state;
166#define RTWS_FIXED_DELAY 0
167#define RTWS_DELAY 1
168#define RTWS_REPLACE 2
169#define RTWS_DEF_FREE 3
170#define RTWS_EXP_SYNC 4
a48f3fad
PM
171#define RTWS_COND_GET 5
172#define RTWS_COND_SYNC 6
f0bf8fab
PM
173#define RTWS_SYNC 7
174#define RTWS_STUTTER 8
175#define RTWS_STOPPING 9
18aff33e
PM
176static const char * const rcu_torture_writer_state_names[] = {
177 "RTWS_FIXED_DELAY",
178 "RTWS_DELAY",
179 "RTWS_REPLACE",
180 "RTWS_DEF_FREE",
181 "RTWS_EXP_SYNC",
182 "RTWS_COND_GET",
183 "RTWS_COND_SYNC",
184 "RTWS_SYNC",
185 "RTWS_STUTTER",
186 "RTWS_STOPPING",
187};
188
c116dba6
PM
189/* Record reader segment types and duration for first failing read. */
190struct rt_read_seg {
191 int rt_readstate;
192 unsigned long rt_delay_jiffies;
193 unsigned long rt_delay_ms;
194 unsigned long rt_delay_us;
195 bool rt_preempted;
196};
197static int err_segs_recorded;
198static struct rt_read_seg err_segs[RCUTORTURE_RDR_MAX_SEGS];
199static int rt_read_nsegs;
200
18aff33e
PM
201static const char *rcu_torture_writer_state_getname(void)
202{
203 unsigned int i = READ_ONCE(rcu_torture_writer_state);
204
205 if (i >= ARRAY_SIZE(rcu_torture_writer_state_names))
206 return "???";
207 return rcu_torture_writer_state_names[i];
208}
ad0dc7f9 209
3acf4a9a 210#if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
8e8be45e 211#define rcu_can_boost() 1
3acf4a9a 212#else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
8e8be45e 213#define rcu_can_boost() 0
3acf4a9a 214#endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
8e8be45e 215
e4aa0da3
SR
216#ifdef CONFIG_RCU_TRACE
217static u64 notrace rcu_trace_clock_local(void)
218{
219 u64 ts = trace_clock_local();
a3b7b6c2
PM
220
221 (void)do_div(ts, NSEC_PER_USEC);
e4aa0da3
SR
222 return ts;
223}
224#else /* #ifdef CONFIG_RCU_TRACE */
225static u64 notrace rcu_trace_clock_local(void)
226{
227 return 0ULL;
228}
229#endif /* #else #ifdef CONFIG_RCU_TRACE */
230
8e8be45e 231static unsigned long boost_starttime; /* jiffies of next boost test start. */
58ade2db 232static DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
8e8be45e 233 /* and boost task create/destroy. */
fae4b54f 234static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
c6ebcbb6 235static bool barrier_phase; /* Test phase. */
fae4b54f
PM
236static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
237static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
238static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
8e8be45e 239
48718485
PM
240static bool rcu_fwd_cb_nodelay; /* Short rcu_torture_delay() delays. */
241
a241ec65
PM
242/*
243 * Allocate an element from the rcu_tortures pool.
244 */
97a41e26 245static struct rcu_torture *
a241ec65
PM
246rcu_torture_alloc(void)
247{
248 struct list_head *p;
249
adac1665 250 spin_lock_bh(&rcu_torture_lock);
a241ec65
PM
251 if (list_empty(&rcu_torture_freelist)) {
252 atomic_inc(&n_rcu_torture_alloc_fail);
adac1665 253 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
254 return NULL;
255 }
256 atomic_inc(&n_rcu_torture_alloc);
257 p = rcu_torture_freelist.next;
258 list_del_init(p);
adac1665 259 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
260 return container_of(p, struct rcu_torture, rtort_free);
261}
262
263/*
264 * Free an element to the rcu_tortures pool.
265 */
266static void
267rcu_torture_free(struct rcu_torture *p)
268{
269 atomic_inc(&n_rcu_torture_free);
adac1665 270 spin_lock_bh(&rcu_torture_lock);
a241ec65 271 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
adac1665 272 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
273}
274
72e9bb54
PM
275/*
276 * Operations vector for selecting different types of tests.
277 */
278
279struct rcu_torture_ops {
ad0dc7f9 280 int ttype;
72e9bb54 281 void (*init)(void);
ca1d51ed 282 void (*cleanup)(void);
72e9bb54 283 int (*readlock)(void);
c116dba6
PM
284 void (*read_delay)(struct torture_random_state *rrsp,
285 struct rt_read_seg *rtrsp);
72e9bb54 286 void (*readunlock)(int idx);
17ef2fe9 287 unsigned long (*get_gp_seq)(void);
d7219312 288 unsigned long (*gp_diff)(unsigned long new, unsigned long old);
0acc512c 289 void (*deferred_free)(struct rcu_torture *p);
b772e1dd 290 void (*sync)(void);
2ec1f2d9 291 void (*exp_sync)(void);
a48f3fad
PM
292 unsigned long (*get_state)(void);
293 void (*cond_sync)(unsigned long oldstate);
db3e8db4 294 call_rcu_func_t call;
2326974d 295 void (*cb_barrier)(void);
bf66f18e 296 void (*fqs)(void);
eea203fe 297 void (*stats)(void);
1b27291b 298 int (*stall_dur)(void);
0acc512c 299 int irq_capable;
8e8be45e 300 int can_boost;
2397d072 301 int extendables;
e66c33d5 302 const char *name;
72e9bb54 303};
a71fca58
PM
304
305static struct rcu_torture_ops *cur_ops;
72e9bb54
PM
306
307/*
308 * Definitions for rcu torture testing.
309 */
310
a49a4af7 311static int rcu_torture_read_lock(void) __acquires(RCU)
72e9bb54
PM
312{
313 rcu_read_lock();
314 return 0;
315}
316
c116dba6
PM
317static void
318rcu_read_delay(struct torture_random_state *rrsp, struct rt_read_seg *rtrsp)
b2896d2e 319{
d0af39e8
PM
320 unsigned long started;
321 unsigned long completed;
b8d57a76 322 const unsigned long shortdelay_us = 200;
1e696765 323 unsigned long longdelay_ms = 300;
d0af39e8 324 unsigned long long ts;
b2896d2e 325
b8d57a76
JT
326 /* We want a short delay sometimes to make a reader delay the grace
327 * period, and we want a long delay occasionally to trigger
328 * force_quiescent_state. */
b2896d2e 329
48718485
PM
330 if (!rcu_fwd_cb_nodelay &&
331 !(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms))) {
17ef2fe9 332 started = cur_ops->get_gp_seq();
d0af39e8 333 ts = rcu_trace_clock_local();
1e696765
PM
334 if (preempt_count() & (SOFTIRQ_MASK | HARDIRQ_MASK))
335 longdelay_ms = 5; /* Avoid triggering BH limits. */
b8d57a76 336 mdelay(longdelay_ms);
c116dba6 337 rtrsp->rt_delay_ms = longdelay_ms;
17ef2fe9 338 completed = cur_ops->get_gp_seq();
d0af39e8
PM
339 do_trace_rcu_torture_read(cur_ops->name, NULL, ts,
340 started, completed);
341 }
c116dba6 342 if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us))) {
b8d57a76 343 udelay(shortdelay_us);
c116dba6
PM
344 rtrsp->rt_delay_us = shortdelay_us;
345 }
51b1130e 346 if (!preempt_count() &&
c116dba6 347 !(torture_random(rrsp) % (nrealreaders * 500))) {
cc1321c9 348 torture_preempt_schedule(); /* QS only if preemptible. */
c116dba6
PM
349 rtrsp->rt_preempted = true;
350 }
b2896d2e
PM
351}
352
a49a4af7 353static void rcu_torture_read_unlock(int idx) __releases(RCU)
72e9bb54
PM
354{
355 rcu_read_unlock();
356}
357
a48f3fad
PM
358/*
359 * Update callback in the pipe. This should be invoked after a grace period.
360 */
361static bool
362rcu_torture_pipe_update_one(struct rcu_torture *rp)
363{
364 int i;
365
366 i = rp->rtort_pipe_count;
367 if (i > RCU_TORTURE_PIPE_LEN)
368 i = RCU_TORTURE_PIPE_LEN;
369 atomic_inc(&rcu_torture_wcount[i]);
370 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
371 rp->rtort_mbtest = 0;
372 return true;
373 }
374 return false;
375}
376
377/*
378 * Update all callbacks in the pipe. Suitable for synchronous grace-period
379 * primitives.
380 */
381static void
382rcu_torture_pipe_update(struct rcu_torture *old_rp)
383{
384 struct rcu_torture *rp;
385 struct rcu_torture *rp1;
386
387 if (old_rp)
388 list_add(&old_rp->rtort_free, &rcu_torture_removed);
389 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
390 if (rcu_torture_pipe_update_one(rp)) {
391 list_del(&rp->rtort_free);
392 rcu_torture_free(rp);
393 }
394 }
395}
396
72e9bb54
PM
397static void
398rcu_torture_cb(struct rcu_head *p)
399{
72e9bb54
PM
400 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
401
36970bb9 402 if (torture_must_stop_irq()) {
72e9bb54
PM
403 /* Test is ending, just drop callbacks on the floor. */
404 /* The next initialization will pick up the pieces. */
405 return;
406 }
a48f3fad 407 if (rcu_torture_pipe_update_one(rp))
72e9bb54 408 rcu_torture_free(rp);
a48f3fad 409 else
0acc512c 410 cur_ops->deferred_free(rp);
72e9bb54
PM
411}
412
6b80da42 413static unsigned long rcu_no_completed(void)
d9a3da06
PM
414{
415 return 0;
416}
417
72e9bb54
PM
418static void rcu_torture_deferred_free(struct rcu_torture *p)
419{
420 call_rcu(&p->rtort_rcu, rcu_torture_cb);
421}
422
e3033736
JT
423static void rcu_sync_torture_init(void)
424{
425 INIT_LIST_HEAD(&rcu_torture_removed);
426}
427
2ec1f2d9 428static struct rcu_torture_ops rcu_ops = {
ad0dc7f9 429 .ttype = RCU_FLAVOR,
0acc512c 430 .init = rcu_sync_torture_init,
0acc512c
PM
431 .readlock = rcu_torture_read_lock,
432 .read_delay = rcu_read_delay,
433 .readunlock = rcu_torture_read_unlock,
17ef2fe9 434 .get_gp_seq = rcu_get_gp_seq,
d7219312 435 .gp_diff = rcu_seq_diff,
2ec1f2d9 436 .deferred_free = rcu_torture_deferred_free,
0acc512c 437 .sync = synchronize_rcu,
2ec1f2d9 438 .exp_sync = synchronize_rcu_expedited,
a48f3fad
PM
439 .get_state = get_state_synchronize_rcu,
440 .cond_sync = cond_synchronize_rcu,
2ec1f2d9
PM
441 .call = call_rcu,
442 .cb_barrier = rcu_barrier,
bf66f18e 443 .fqs = rcu_force_quiescent_state,
d9a3da06 444 .stats = NULL,
1b27291b 445 .stall_dur = rcu_jiffies_till_stall_check,
d9a3da06 446 .irq_capable = 1,
8e8be45e 447 .can_boost = rcu_can_boost(),
c0335743 448 .extendables = RCUTORTURE_MAX_EXTEND,
2ec1f2d9 449 .name = "rcu"
d9a3da06
PM
450};
451
ff20e251
PM
452/*
453 * Don't even think about trying any of these in real life!!!
454 * The names includes "busted", and they really means it!
455 * The only purpose of these functions is to provide a buggy RCU
456 * implementation to make sure that rcutorture correctly emits
457 * buggy-RCU error messages.
458 */
459static void rcu_busted_torture_deferred_free(struct rcu_torture *p)
460{
461 /* This is a deliberate bug for testing purposes only! */
462 rcu_torture_cb(&p->rtort_rcu);
463}
464
465static void synchronize_rcu_busted(void)
466{
467 /* This is a deliberate bug for testing purposes only! */
468}
469
470static void
b6a4ae76 471call_rcu_busted(struct rcu_head *head, rcu_callback_t func)
ff20e251
PM
472{
473 /* This is a deliberate bug for testing purposes only! */
474 func(head);
475}
476
477static struct rcu_torture_ops rcu_busted_ops = {
ad0dc7f9 478 .ttype = INVALID_RCU_FLAVOR,
ff20e251
PM
479 .init = rcu_sync_torture_init,
480 .readlock = rcu_torture_read_lock,
481 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
482 .readunlock = rcu_torture_read_unlock,
17ef2fe9 483 .get_gp_seq = rcu_no_completed,
ff20e251
PM
484 .deferred_free = rcu_busted_torture_deferred_free,
485 .sync = synchronize_rcu_busted,
486 .exp_sync = synchronize_rcu_busted,
487 .call = call_rcu_busted,
488 .cb_barrier = NULL,
489 .fqs = NULL,
490 .stats = NULL,
491 .irq_capable = 1,
b3c98314 492 .name = "busted"
ff20e251
PM
493};
494
b2896d2e
PM
495/*
496 * Definitions for srcu torture testing.
497 */
498
cda4dc81 499DEFINE_STATIC_SRCU(srcu_ctl);
ca1d51ed
PM
500static struct srcu_struct srcu_ctld;
501static struct srcu_struct *srcu_ctlp = &srcu_ctl;
b2896d2e 502
ca1d51ed 503static int srcu_torture_read_lock(void) __acquires(srcu_ctlp)
b2896d2e 504{
ca1d51ed 505 return srcu_read_lock(srcu_ctlp);
b2896d2e
PM
506}
507
c116dba6
PM
508static void
509srcu_read_delay(struct torture_random_state *rrsp, struct rt_read_seg *rtrsp)
b2896d2e
PM
510{
511 long delay;
512 const long uspertick = 1000000 / HZ;
513 const long longdelay = 10;
514
515 /* We want there to be long-running readers, but not all the time. */
516
51b1130e
PM
517 delay = torture_random(rrsp) %
518 (nrealreaders * 2 * longdelay * uspertick);
c116dba6 519 if (!delay && in_task()) {
b2896d2e 520 schedule_timeout_interruptible(longdelay);
c116dba6
PM
521 rtrsp->rt_delay_jiffies = longdelay;
522 } else {
523 rcu_read_delay(rrsp, rtrsp);
524 }
b2896d2e
PM
525}
526
ca1d51ed 527static void srcu_torture_read_unlock(int idx) __releases(srcu_ctlp)
b2896d2e 528{
ca1d51ed 529 srcu_read_unlock(srcu_ctlp, idx);
b2896d2e
PM
530}
531
6b80da42 532static unsigned long srcu_torture_completed(void)
b2896d2e 533{
ca1d51ed 534 return srcu_batches_completed(srcu_ctlp);
b2896d2e
PM
535}
536
9059c940
LJ
537static void srcu_torture_deferred_free(struct rcu_torture *rp)
538{
ca1d51ed 539 call_srcu(srcu_ctlp, &rp->rtort_rcu, rcu_torture_cb);
9059c940
LJ
540}
541
b772e1dd
JT
542static void srcu_torture_synchronize(void)
543{
ca1d51ed 544 synchronize_srcu(srcu_ctlp);
b772e1dd
JT
545}
546
e3f8d378 547static void srcu_torture_call(struct rcu_head *head,
b6a4ae76 548 rcu_callback_t func)
e3f8d378 549{
ca1d51ed 550 call_srcu(srcu_ctlp, head, func);
e3f8d378
PM
551}
552
553static void srcu_torture_barrier(void)
554{
ca1d51ed 555 srcu_barrier(srcu_ctlp);
e3f8d378
PM
556}
557
eea203fe 558static void srcu_torture_stats(void)
b2896d2e 559{
115a1a52 560 srcu_torture_stats_print(srcu_ctlp, torture_type, TORTURE_FLAG);
b2896d2e
PM
561}
562
2ec1f2d9
PM
563static void srcu_torture_synchronize_expedited(void)
564{
ca1d51ed 565 synchronize_srcu_expedited(srcu_ctlp);
2ec1f2d9
PM
566}
567
b2896d2e 568static struct rcu_torture_ops srcu_ops = {
ad0dc7f9 569 .ttype = SRCU_FLAVOR,
cda4dc81 570 .init = rcu_sync_torture_init,
0acc512c
PM
571 .readlock = srcu_torture_read_lock,
572 .read_delay = srcu_read_delay,
573 .readunlock = srcu_torture_read_unlock,
17ef2fe9 574 .get_gp_seq = srcu_torture_completed,
9059c940 575 .deferred_free = srcu_torture_deferred_free,
0acc512c 576 .sync = srcu_torture_synchronize,
2ec1f2d9 577 .exp_sync = srcu_torture_synchronize_expedited,
e3f8d378
PM
578 .call = srcu_torture_call,
579 .cb_barrier = srcu_torture_barrier,
0acc512c 580 .stats = srcu_torture_stats,
5e741fa9 581 .irq_capable = 1,
0acc512c 582 .name = "srcu"
b2896d2e
PM
583};
584
ca1d51ed
PM
585static void srcu_torture_init(void)
586{
587 rcu_sync_torture_init();
588 WARN_ON(init_srcu_struct(&srcu_ctld));
589 srcu_ctlp = &srcu_ctld;
590}
591
592static void srcu_torture_cleanup(void)
593{
f5ad3991 594 cleanup_srcu_struct(&srcu_ctld);
ca1d51ed
PM
595 srcu_ctlp = &srcu_ctl; /* In case of a later rcutorture run. */
596}
597
598/* As above, but dynamically allocated. */
599static struct rcu_torture_ops srcud_ops = {
600 .ttype = SRCU_FLAVOR,
601 .init = srcu_torture_init,
602 .cleanup = srcu_torture_cleanup,
603 .readlock = srcu_torture_read_lock,
604 .read_delay = srcu_read_delay,
605 .readunlock = srcu_torture_read_unlock,
17ef2fe9 606 .get_gp_seq = srcu_torture_completed,
ca1d51ed
PM
607 .deferred_free = srcu_torture_deferred_free,
608 .sync = srcu_torture_synchronize,
609 .exp_sync = srcu_torture_synchronize_expedited,
610 .call = srcu_torture_call,
611 .cb_barrier = srcu_torture_barrier,
612 .stats = srcu_torture_stats,
5e741fa9 613 .irq_capable = 1,
ca1d51ed
PM
614 .name = "srcud"
615};
616
2397d072
PM
617/* As above, but broken due to inappropriate reader extension. */
618static struct rcu_torture_ops busted_srcud_ops = {
619 .ttype = SRCU_FLAVOR,
620 .init = srcu_torture_init,
621 .cleanup = srcu_torture_cleanup,
622 .readlock = srcu_torture_read_lock,
623 .read_delay = rcu_read_delay,
624 .readunlock = srcu_torture_read_unlock,
625 .get_gp_seq = srcu_torture_completed,
626 .deferred_free = srcu_torture_deferred_free,
627 .sync = srcu_torture_synchronize,
628 .exp_sync = srcu_torture_synchronize_expedited,
629 .call = srcu_torture_call,
630 .cb_barrier = srcu_torture_barrier,
631 .stats = srcu_torture_stats,
632 .irq_capable = 1,
633 .extendables = RCUTORTURE_MAX_EXTEND,
634 .name = "busted_srcud"
635};
636
69c60455
PM
637/*
638 * Definitions for RCU-tasks torture testing.
639 */
640
641static int tasks_torture_read_lock(void)
642{
643 return 0;
644}
645
646static void tasks_torture_read_unlock(int idx)
647{
648}
649
650static void rcu_tasks_torture_deferred_free(struct rcu_torture *p)
651{
652 call_rcu_tasks(&p->rtort_rcu, rcu_torture_cb);
653}
654
655static struct rcu_torture_ops tasks_ops = {
656 .ttype = RCU_TASKS_FLAVOR,
657 .init = rcu_sync_torture_init,
658 .readlock = tasks_torture_read_lock,
659 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
660 .readunlock = tasks_torture_read_unlock,
17ef2fe9 661 .get_gp_seq = rcu_no_completed,
69c60455
PM
662 .deferred_free = rcu_tasks_torture_deferred_free,
663 .sync = synchronize_rcu_tasks,
664 .exp_sync = synchronize_rcu_tasks,
665 .call = call_rcu_tasks,
666 .cb_barrier = rcu_barrier_tasks,
667 .fqs = NULL,
668 .stats = NULL,
669 .irq_capable = 1,
670 .name = "tasks"
671};
672
d7219312
PM
673static unsigned long rcutorture_seq_diff(unsigned long new, unsigned long old)
674{
675 if (!cur_ops->gp_diff)
676 return new - old;
677 return cur_ops->gp_diff(new, old);
678}
679
5be5d1a1
PM
680static bool __maybe_unused torturing_tasks(void)
681{
682 return cur_ops == &tasks_ops;
683}
684
8e8be45e
PM
685/*
686 * RCU torture priority-boost testing. Runs one real-time thread per
687 * CPU for moderate bursts, repeatedly registering RCU callbacks and
688 * spinning waiting for them to be invoked. If a given callback takes
689 * too long to be invoked, we assume that priority inversion has occurred.
690 */
691
692struct rcu_boost_inflight {
693 struct rcu_head rcu;
694 int inflight;
695};
696
697static void rcu_torture_boost_cb(struct rcu_head *head)
698{
699 struct rcu_boost_inflight *rbip =
700 container_of(head, struct rcu_boost_inflight, rcu);
701
6c7ed42c
PM
702 /* Ensure RCU-core accesses precede clearing ->inflight */
703 smp_store_release(&rbip->inflight, 0);
8e8be45e
PM
704}
705
450efca7
JFG
706static int old_rt_runtime = -1;
707
708static void rcu_torture_disable_rt_throttle(void)
709{
710 /*
711 * Disable RT throttling so that rcutorture's boost threads don't get
712 * throttled. Only possible if rcutorture is built-in otherwise the
713 * user should manually do this by setting the sched_rt_period_us and
714 * sched_rt_runtime sysctls.
715 */
716 if (!IS_BUILTIN(CONFIG_RCU_TORTURE_TEST) || old_rt_runtime != -1)
717 return;
718
719 old_rt_runtime = sysctl_sched_rt_runtime;
720 sysctl_sched_rt_runtime = -1;
721}
722
723static void rcu_torture_enable_rt_throttle(void)
724{
725 if (!IS_BUILTIN(CONFIG_RCU_TORTURE_TEST) || old_rt_runtime == -1)
726 return;
727
728 sysctl_sched_rt_runtime = old_rt_runtime;
729 old_rt_runtime = -1;
730}
731
3b745c89
JFG
732static bool rcu_torture_boost_failed(unsigned long start, unsigned long end)
733{
734 if (end - start > test_boost_duration * HZ - HZ / 2) {
735 VERBOSE_TOROUT_STRING("rcu_torture_boost boosting failed");
736 n_rcu_torture_boost_failure++;
737
738 return true; /* failed */
739 }
740
741 return false; /* passed */
742}
743
8e8be45e
PM
744static int rcu_torture_boost(void *arg)
745{
746 unsigned long call_rcu_time;
747 unsigned long endtime;
748 unsigned long oldstarttime;
749 struct rcu_boost_inflight rbi = { .inflight = 0 };
750 struct sched_param sp;
751
5ccf60f2 752 VERBOSE_TOROUT_STRING("rcu_torture_boost started");
8e8be45e
PM
753
754 /* Set real-time priority. */
755 sp.sched_priority = 1;
756 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
5ccf60f2 757 VERBOSE_TOROUT_STRING("rcu_torture_boost RT prio failed!");
8e8be45e
PM
758 n_rcu_torture_boost_rterror++;
759 }
760
561190e3 761 init_rcu_head_on_stack(&rbi.rcu);
8e8be45e
PM
762 /* Each pass through the following loop does one boost-test cycle. */
763 do {
3b745c89
JFG
764 /* Track if the test failed already in this test interval? */
765 bool failed = false;
766
767 /* Increment n_rcu_torture_boosts once per boost-test */
768 while (!kthread_should_stop()) {
769 if (mutex_trylock(&boost_mutex)) {
770 n_rcu_torture_boosts++;
771 mutex_unlock(&boost_mutex);
772 break;
773 }
774 schedule_timeout_uninterruptible(1);
775 }
776 if (kthread_should_stop())
777 goto checkwait;
778
8e8be45e
PM
779 /* Wait for the next test interval. */
780 oldstarttime = boost_starttime;
93898fb1 781 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
0e11c8e8 782 schedule_timeout_interruptible(oldstarttime - jiffies);
628edaa5 783 stutter_wait("rcu_torture_boost");
36970bb9 784 if (torture_must_stop())
8e8be45e
PM
785 goto checkwait;
786 }
787
788 /* Do one boost-test interval. */
789 endtime = oldstarttime + test_boost_duration * HZ;
790 call_rcu_time = jiffies;
93898fb1 791 while (ULONG_CMP_LT(jiffies, endtime)) {
8e8be45e 792 /* If we don't have a callback in flight, post one. */
6c7ed42c
PM
793 if (!smp_load_acquire(&rbi.inflight)) {
794 /* RCU core before ->inflight = 1. */
795 smp_store_release(&rbi.inflight, 1);
8e8be45e 796 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
3b745c89
JFG
797 /* Check if the boost test failed */
798 failed = failed ||
799 rcu_torture_boost_failed(call_rcu_time,
800 jiffies);
8e8be45e
PM
801 call_rcu_time = jiffies;
802 }
628edaa5 803 stutter_wait("rcu_torture_boost");
36970bb9 804 if (torture_must_stop())
8e8be45e
PM
805 goto checkwait;
806 }
807
3b745c89
JFG
808 /*
809 * If boost never happened, then inflight will always be 1, in
810 * this case the boost check would never happen in the above
811 * loop so do another one here.
812 */
813 if (!failed && smp_load_acquire(&rbi.inflight))
814 rcu_torture_boost_failed(call_rcu_time, jiffies);
815
8e8be45e
PM
816 /*
817 * Set the start time of the next test interval.
818 * Yes, this is vulnerable to long delays, but such
819 * delays simply cause a false negative for the next
820 * interval. Besides, we are running at RT priority,
821 * so delays should be relatively rare.
822 */
ab8f11e5
PM
823 while (oldstarttime == boost_starttime &&
824 !kthread_should_stop()) {
8e8be45e
PM
825 if (mutex_trylock(&boost_mutex)) {
826 boost_starttime = jiffies +
827 test_boost_interval * HZ;
8e8be45e
PM
828 mutex_unlock(&boost_mutex);
829 break;
830 }
831 schedule_timeout_uninterruptible(1);
832 }
833
834 /* Go do the stutter. */
628edaa5 835checkwait: stutter_wait("rcu_torture_boost");
36970bb9 836 } while (!torture_must_stop());
8e8be45e
PM
837
838 /* Clean up and exit. */
6c7ed42c 839 while (!kthread_should_stop() || smp_load_acquire(&rbi.inflight)) {
7fafaac5 840 torture_shutdown_absorb("rcu_torture_boost");
8e8be45e 841 schedule_timeout_uninterruptible(1);
7fafaac5 842 }
9d68197c 843 destroy_rcu_head_on_stack(&rbi.rcu);
7fafaac5 844 torture_kthread_stopping("rcu_torture_boost");
8e8be45e
PM
845 return 0;
846}
847
bf66f18e
PM
848/*
849 * RCU torture force-quiescent-state kthread. Repeatedly induces
850 * bursts of calls to force_quiescent_state(), increasing the probability
851 * of occurrence of some important types of race conditions.
852 */
853static int
854rcu_torture_fqs(void *arg)
855{
856 unsigned long fqs_resume_time;
857 int fqs_burst_remaining;
858
5ccf60f2 859 VERBOSE_TOROUT_STRING("rcu_torture_fqs task started");
bf66f18e
PM
860 do {
861 fqs_resume_time = jiffies + fqs_stutter * HZ;
93898fb1
PM
862 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
863 !kthread_should_stop()) {
bf66f18e
PM
864 schedule_timeout_interruptible(1);
865 }
866 fqs_burst_remaining = fqs_duration;
93898fb1
PM
867 while (fqs_burst_remaining > 0 &&
868 !kthread_should_stop()) {
bf66f18e
PM
869 cur_ops->fqs();
870 udelay(fqs_holdoff);
871 fqs_burst_remaining -= fqs_holdoff;
872 }
628edaa5 873 stutter_wait("rcu_torture_fqs");
36970bb9 874 } while (!torture_must_stop());
7fafaac5 875 torture_kthread_stopping("rcu_torture_fqs");
bf66f18e
PM
876 return 0;
877}
878
a241ec65
PM
879/*
880 * RCU torture writer kthread. Repeatedly substitutes a new structure
881 * for that pointed to by rcu_torture_current, freeing the old structure
882 * after a series of grace periods (the "pipeline").
883 */
884static int
885rcu_torture_writer(void *arg)
886{
9efafb88 887 bool can_expedite = !rcu_gp_is_expedited() && !rcu_gp_is_normal();
4bb3c5f4 888 int expediting = 0;
a48f3fad
PM
889 unsigned long gp_snap;
890 bool gp_cond1 = gp_cond, gp_exp1 = gp_exp, gp_normal1 = gp_normal;
f0bf8fab 891 bool gp_sync1 = gp_sync;
a241ec65 892 int i;
a241ec65
PM
893 struct rcu_torture *rp;
894 struct rcu_torture *old_rp;
51b1130e 895 static DEFINE_TORTURE_RANDOM(rand);
f0bf8fab
PM
896 int synctype[] = { RTWS_DEF_FREE, RTWS_EXP_SYNC,
897 RTWS_COND_GET, RTWS_SYNC };
a48f3fad 898 int nsynctypes = 0;
a241ec65 899
5ccf60f2 900 VERBOSE_TOROUT_STRING("rcu_torture_writer task started");
f7c0e6ad 901 if (!can_expedite)
aa5a8988 902 pr_alert("%s" TORTURE_FLAG
f7c0e6ad 903 " GP expediting controlled from boot/sysfs for %s.\n",
aa5a8988 904 torture_type, cur_ops->name);
dbdf65b1 905
a48f3fad 906 /* Initialize synctype[] array. If none set, take default. */
c136f991 907 if (!gp_cond1 && !gp_exp1 && !gp_normal1 && !gp_sync1)
f0bf8fab 908 gp_cond1 = gp_exp1 = gp_normal1 = gp_sync1 = true;
db0c1a8a 909 if (gp_cond1 && cur_ops->get_state && cur_ops->cond_sync) {
a48f3fad 910 synctype[nsynctypes++] = RTWS_COND_GET;
db0c1a8a
PM
911 pr_info("%s: Testing conditional GPs.\n", __func__);
912 } else if (gp_cond && (!cur_ops->get_state || !cur_ops->cond_sync)) {
e0d31a34 913 pr_alert("%s: gp_cond without primitives.\n", __func__);
db0c1a8a
PM
914 }
915 if (gp_exp1 && cur_ops->exp_sync) {
a48f3fad 916 synctype[nsynctypes++] = RTWS_EXP_SYNC;
db0c1a8a
PM
917 pr_info("%s: Testing expedited GPs.\n", __func__);
918 } else if (gp_exp && !cur_ops->exp_sync) {
e0d31a34 919 pr_alert("%s: gp_exp without primitives.\n", __func__);
db0c1a8a
PM
920 }
921 if (gp_normal1 && cur_ops->deferred_free) {
a48f3fad 922 synctype[nsynctypes++] = RTWS_DEF_FREE;
db0c1a8a
PM
923 pr_info("%s: Testing asynchronous GPs.\n", __func__);
924 } else if (gp_normal && !cur_ops->deferred_free) {
e0d31a34 925 pr_alert("%s: gp_normal without primitives.\n", __func__);
db0c1a8a
PM
926 }
927 if (gp_sync1 && cur_ops->sync) {
f0bf8fab 928 synctype[nsynctypes++] = RTWS_SYNC;
db0c1a8a
PM
929 pr_info("%s: Testing normal GPs.\n", __func__);
930 } else if (gp_sync && !cur_ops->sync) {
e0d31a34 931 pr_alert("%s: gp_sync without primitives.\n", __func__);
db0c1a8a 932 }
a48f3fad
PM
933 if (WARN_ONCE(nsynctypes == 0,
934 "rcu_torture_writer: No update-side primitives.\n")) {
f0bf8fab
PM
935 /*
936 * No updates primitives, so don't try updating.
937 * The resulting test won't be testing much, hence the
938 * above WARN_ONCE().
939 */
a48f3fad
PM
940 rcu_torture_writer_state = RTWS_STOPPING;
941 torture_kthread_stopping("rcu_torture_writer");
942 }
943
a241ec65 944 do {
ad0dc7f9 945 rcu_torture_writer_state = RTWS_FIXED_DELAY;
a241ec65 946 schedule_timeout_uninterruptible(1);
a71fca58
PM
947 rp = rcu_torture_alloc();
948 if (rp == NULL)
a241ec65
PM
949 continue;
950 rp->rtort_pipe_count = 0;
ad0dc7f9 951 rcu_torture_writer_state = RTWS_DELAY;
51b1130e 952 udelay(torture_random(&rand) & 0x3ff);
ad0dc7f9 953 rcu_torture_writer_state = RTWS_REPLACE;
0ddea0ea
PM
954 old_rp = rcu_dereference_check(rcu_torture_current,
955 current == writer_task);
996417d2 956 rp->rtort_mbtest = 1;
a241ec65 957 rcu_assign_pointer(rcu_torture_current, rp);
9b2619af 958 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
c8e5b163 959 if (old_rp) {
a241ec65
PM
960 i = old_rp->rtort_pipe_count;
961 if (i > RCU_TORTURE_PIPE_LEN)
962 i = RCU_TORTURE_PIPE_LEN;
963 atomic_inc(&rcu_torture_wcount[i]);
964 old_rp->rtort_pipe_count++;
a48f3fad
PM
965 switch (synctype[torture_random(&rand) % nsynctypes]) {
966 case RTWS_DEF_FREE:
ad0dc7f9 967 rcu_torture_writer_state = RTWS_DEF_FREE;
2ec1f2d9 968 cur_ops->deferred_free(old_rp);
a48f3fad
PM
969 break;
970 case RTWS_EXP_SYNC:
ad0dc7f9 971 rcu_torture_writer_state = RTWS_EXP_SYNC;
2ec1f2d9 972 cur_ops->exp_sync();
a48f3fad
PM
973 rcu_torture_pipe_update(old_rp);
974 break;
975 case RTWS_COND_GET:
976 rcu_torture_writer_state = RTWS_COND_GET;
977 gp_snap = cur_ops->get_state();
978 i = torture_random(&rand) % 16;
979 if (i != 0)
980 schedule_timeout_interruptible(i);
981 udelay(torture_random(&rand) % 1000);
982 rcu_torture_writer_state = RTWS_COND_SYNC;
983 cur_ops->cond_sync(gp_snap);
984 rcu_torture_pipe_update(old_rp);
985 break;
f0bf8fab
PM
986 case RTWS_SYNC:
987 rcu_torture_writer_state = RTWS_SYNC;
988 cur_ops->sync();
989 rcu_torture_pipe_update(old_rp);
990 break;
a48f3fad
PM
991 default:
992 WARN_ON_ONCE(1);
993 break;
2ec1f2d9 994 }
a241ec65 995 }
1b27291b
PM
996 WRITE_ONCE(rcu_torture_current_version,
997 rcu_torture_current_version + 1);
4bb3c5f4
PM
998 /* Cycle through nesting levels of rcu_expedite_gp() calls. */
999 if (can_expedite &&
1000 !(torture_random(&rand) & 0xff & (!!expediting - 1))) {
1001 WARN_ON_ONCE(expediting == 0 && rcu_gp_is_expedited());
1002 if (expediting >= 0)
1003 rcu_expedite_gp();
1004 else
1005 rcu_unexpedite_gp();
1006 if (++expediting > 3)
1007 expediting = -expediting;
f7c0e6ad
PM
1008 } else if (!can_expedite) { /* Disabled during boot, recheck. */
1009 can_expedite = !rcu_gp_is_expedited() &&
1010 !rcu_gp_is_normal();
4bb3c5f4 1011 }
ad0dc7f9 1012 rcu_torture_writer_state = RTWS_STUTTER;
474e59b4
PM
1013 if (stutter_wait("rcu_torture_writer"))
1014 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++)
1015 if (list_empty(&rcu_tortures[i].rtort_free))
1016 WARN_ON_ONCE(1);
36970bb9 1017 } while (!torture_must_stop());
4bb3c5f4
PM
1018 /* Reset expediting back to unexpedited. */
1019 if (expediting > 0)
1020 expediting = -expediting;
1021 while (can_expedite && expediting++ < 0)
1022 rcu_unexpedite_gp();
1023 WARN_ON_ONCE(can_expedite && rcu_gp_is_expedited());
f7c0e6ad
PM
1024 if (!can_expedite)
1025 pr_alert("%s" TORTURE_FLAG
1026 " Dynamic grace-period expediting was disabled.\n",
1027 torture_type);
ad0dc7f9 1028 rcu_torture_writer_state = RTWS_STOPPING;
7fafaac5 1029 torture_kthread_stopping("rcu_torture_writer");
a241ec65
PM
1030 return 0;
1031}
1032
b772e1dd
JT
1033/*
1034 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
1035 * delay between calls.
1036 */
1037static int
1038rcu_torture_fakewriter(void *arg)
1039{
51b1130e 1040 DEFINE_TORTURE_RANDOM(rand);
b772e1dd 1041
5ccf60f2 1042 VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
d277d868 1043 set_user_nice(current, MAX_NICE);
b772e1dd
JT
1044
1045 do {
51b1130e
PM
1046 schedule_timeout_uninterruptible(1 + torture_random(&rand)%10);
1047 udelay(torture_random(&rand) & 0x3ff);
72472a02 1048 if (cur_ops->cb_barrier != NULL &&
51b1130e 1049 torture_random(&rand) % (nfakewriters * 8) == 0) {
72472a02 1050 cur_ops->cb_barrier();
2ec1f2d9 1051 } else if (gp_normal == gp_exp) {
eb033993 1052 if (cur_ops->sync && torture_random(&rand) & 0x80)
2ec1f2d9 1053 cur_ops->sync();
eb033993 1054 else if (cur_ops->exp_sync)
2ec1f2d9 1055 cur_ops->exp_sync();
eb033993 1056 } else if (gp_normal && cur_ops->sync) {
72472a02 1057 cur_ops->sync();
eb033993 1058 } else if (cur_ops->exp_sync) {
2ec1f2d9
PM
1059 cur_ops->exp_sync();
1060 }
628edaa5 1061 stutter_wait("rcu_torture_fakewriter");
36970bb9 1062 } while (!torture_must_stop());
b772e1dd 1063
7fafaac5 1064 torture_kthread_stopping("rcu_torture_fakewriter");
b772e1dd
JT
1065 return 0;
1066}
1067
f34c8585
PM
1068static void rcu_torture_timer_cb(struct rcu_head *rhp)
1069{
1070 kfree(rhp);
1071}
1072
0729fbf3 1073/*
2397d072
PM
1074 * Do one extension of an RCU read-side critical section using the
1075 * current reader state in readstate (set to zero for initial entry
1076 * to extended critical section), set the new state as specified by
1077 * newstate (set to zero for final exit from extended critical section),
1078 * and random-number-generator state in trsp. If this is neither the
1079 * beginning or end of the critical section and if there was actually a
1080 * change, do a ->read_delay().
0729fbf3 1081 */
2397d072 1082static void rcutorture_one_extend(int *readstate, int newstate,
c116dba6
PM
1083 struct torture_random_state *trsp,
1084 struct rt_read_seg *rtrsp)
2397d072
PM
1085{
1086 int idxnew = -1;
1087 int idxold = *readstate;
1088 int statesnew = ~*readstate & newstate;
1089 int statesold = *readstate & ~newstate;
1090
1091 WARN_ON_ONCE(idxold < 0);
1092 WARN_ON_ONCE((idxold >> RCUTORTURE_RDR_SHIFT) > 1);
c116dba6 1093 rtrsp->rt_readstate = newstate;
2397d072
PM
1094
1095 /* First, put new protection in place to avoid critical-section gap. */
1096 if (statesnew & RCUTORTURE_RDR_BH)
1097 local_bh_disable();
1098 if (statesnew & RCUTORTURE_RDR_IRQ)
1099 local_irq_disable();
1100 if (statesnew & RCUTORTURE_RDR_PREEMPT)
1101 preempt_disable();
2ceebc03
PM
1102 if (statesnew & RCUTORTURE_RDR_RBH)
1103 rcu_read_lock_bh();
1104 if (statesnew & RCUTORTURE_RDR_SCHED)
1105 rcu_read_lock_sched();
2397d072
PM
1106 if (statesnew & RCUTORTURE_RDR_RCU)
1107 idxnew = cur_ops->readlock() << RCUTORTURE_RDR_SHIFT;
1108
1109 /* Next, remove old protection, irq first due to bh conflict. */
1110 if (statesold & RCUTORTURE_RDR_IRQ)
1111 local_irq_enable();
1112 if (statesold & RCUTORTURE_RDR_BH)
1113 local_bh_enable();
1114 if (statesold & RCUTORTURE_RDR_PREEMPT)
1115 preempt_enable();
2ceebc03
PM
1116 if (statesold & RCUTORTURE_RDR_RBH)
1117 rcu_read_unlock_bh();
1118 if (statesold & RCUTORTURE_RDR_SCHED)
1119 rcu_read_unlock_sched();
2397d072
PM
1120 if (statesold & RCUTORTURE_RDR_RCU)
1121 cur_ops->readunlock(idxold >> RCUTORTURE_RDR_SHIFT);
1122
1123 /* Delay if neither beginning nor end and there was a change. */
1124 if ((statesnew || statesold) && *readstate && newstate)
c116dba6 1125 cur_ops->read_delay(trsp, rtrsp);
2397d072
PM
1126
1127 /* Update the reader state. */
1128 if (idxnew == -1)
1129 idxnew = idxold & ~RCUTORTURE_RDR_MASK;
1130 WARN_ON_ONCE(idxnew < 0);
1131 WARN_ON_ONCE((idxnew >> RCUTORTURE_RDR_SHIFT) > 1);
1132 *readstate = idxnew | newstate;
1133 WARN_ON_ONCE((*readstate >> RCUTORTURE_RDR_SHIFT) < 0);
1134 WARN_ON_ONCE((*readstate >> RCUTORTURE_RDR_SHIFT) > 1);
1135}
1136
1137/* Return the biggest extendables mask given current RCU and boot parameters. */
1138static int rcutorture_extend_mask_max(void)
1139{
1140 int mask;
1141
1142 WARN_ON_ONCE(extendables & ~RCUTORTURE_MAX_EXTEND);
1143 mask = extendables & RCUTORTURE_MAX_EXTEND & cur_ops->extendables;
1144 mask = mask | RCUTORTURE_RDR_RCU;
1145 return mask;
1146}
1147
1148/* Return a random protection state mask, but with at least one bit set. */
1149static int
1150rcutorture_extend_mask(int oldmask, struct torture_random_state *trsp)
1151{
1152 int mask = rcutorture_extend_mask_max();
bf1bef50 1153 unsigned long randmask1 = torture_random(trsp) >> 8;
c116dba6 1154 unsigned long randmask2 = randmask1 >> 3;
2397d072
PM
1155
1156 WARN_ON_ONCE(mask >> RCUTORTURE_RDR_SHIFT);
a3b0e1e5 1157 /* Mostly only one bit (need preemption!), sometimes lots of bits. */
c116dba6 1158 if (!(randmask1 & 0x7))
bf1bef50
PM
1159 mask = mask & randmask2;
1160 else
1161 mask = mask & (1 << (randmask2 % RCUTORTURE_RDR_NBITS));
2ceebc03 1162 /* Can't enable bh w/irq disabled. */
2397d072 1163 if ((mask & RCUTORTURE_RDR_IRQ) &&
2ceebc03
PM
1164 ((!(mask & RCUTORTURE_RDR_BH) && (oldmask & RCUTORTURE_RDR_BH)) ||
1165 (!(mask & RCUTORTURE_RDR_RBH) && (oldmask & RCUTORTURE_RDR_RBH))))
1166 mask |= RCUTORTURE_RDR_BH | RCUTORTURE_RDR_RBH;
2397d072
PM
1167 return mask ?: RCUTORTURE_RDR_RCU;
1168}
1169
1170/*
1171 * Do a randomly selected number of extensions of an existing RCU read-side
1172 * critical section.
1173 */
c116dba6
PM
1174static struct rt_read_seg *
1175rcutorture_loop_extend(int *readstate, struct torture_random_state *trsp,
1176 struct rt_read_seg *rtrsp)
2397d072
PM
1177{
1178 int i;
c116dba6 1179 int j;
2397d072
PM
1180 int mask = rcutorture_extend_mask_max();
1181
1182 WARN_ON_ONCE(!*readstate); /* -Existing- RCU read-side critsect! */
1183 if (!((mask - 1) & mask))
c116dba6
PM
1184 return rtrsp; /* Current RCU reader not extendable. */
1185 /* Bias towards larger numbers of loops. */
1186 i = (torture_random(trsp) >> 3);
1187 i = ((i | (i >> 3)) & RCUTORTURE_RDR_MAX_LOOPS) + 1;
1188 for (j = 0; j < i; j++) {
2397d072 1189 mask = rcutorture_extend_mask(*readstate, trsp);
c116dba6 1190 rcutorture_one_extend(readstate, mask, trsp, &rtrsp[j]);
2397d072 1191 }
c116dba6 1192 return &rtrsp[j];
2397d072
PM
1193}
1194
6b06aa72
PM
1195/*
1196 * Do one read-side critical section, returning false if there was
1197 * no data to read. Can be invoked both from process context and
1198 * from a timer handler.
1199 */
1200static bool rcu_torture_one_read(struct torture_random_state *trsp)
0729fbf3 1201{
c116dba6 1202 int i;
917963d0 1203 unsigned long started;
6b80da42 1204 unsigned long completed;
2397d072 1205 int newstate;
0729fbf3
PM
1206 struct rcu_torture *p;
1207 int pipe_count;
2397d072 1208 int readstate = 0;
c116dba6
PM
1209 struct rt_read_seg rtseg[RCUTORTURE_RDR_MAX_SEGS] = { { 0 } };
1210 struct rt_read_seg *rtrsp = &rtseg[0];
1211 struct rt_read_seg *rtrsp1;
52494535 1212 unsigned long long ts;
0729fbf3 1213
2397d072 1214 newstate = rcutorture_extend_mask(readstate, trsp);
c116dba6 1215 rcutorture_one_extend(&readstate, newstate, trsp, rtrsp++);
17ef2fe9 1216 started = cur_ops->get_gp_seq();
e4aa0da3 1217 ts = rcu_trace_clock_local();
632ee200 1218 p = rcu_dereference_check(rcu_torture_current,
632ee200
PM
1219 rcu_read_lock_bh_held() ||
1220 rcu_read_lock_sched_held() ||
5be5d1a1
PM
1221 srcu_read_lock_held(srcu_ctlp) ||
1222 torturing_tasks());
0729fbf3 1223 if (p == NULL) {
6b06aa72 1224 /* Wait for rcu_torture_writer to get underway */
c116dba6 1225 rcutorture_one_extend(&readstate, 0, trsp, rtrsp);
6b06aa72 1226 return false;
0729fbf3
PM
1227 }
1228 if (p->rtort_mbtest == 0)
1229 atomic_inc(&n_rcu_torture_mberror);
c116dba6 1230 rtrsp = rcutorture_loop_extend(&readstate, trsp, rtrsp);
0729fbf3
PM
1231 preempt_disable();
1232 pipe_count = p->rtort_pipe_count;
1233 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1234 /* Should not happen, but... */
1235 pipe_count = RCU_TORTURE_PIPE_LEN;
1236 }
17ef2fe9 1237 completed = cur_ops->get_gp_seq();
52494535 1238 if (pipe_count > 1) {
6b06aa72
PM
1239 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1240 ts, started, completed);
274529ba 1241 rcu_ftrace_dump(DUMP_ALL);
52494535 1242 }
dd17c8f7 1243 __this_cpu_inc(rcu_torture_count[pipe_count]);
d7219312 1244 completed = rcutorture_seq_diff(completed, started);
0729fbf3
PM
1245 if (completed > RCU_TORTURE_PIPE_LEN) {
1246 /* Should not happen, but... */
1247 completed = RCU_TORTURE_PIPE_LEN;
1248 }
dd17c8f7 1249 __this_cpu_inc(rcu_torture_batch[completed]);
0729fbf3 1250 preempt_enable();
c116dba6 1251 rcutorture_one_extend(&readstate, 0, trsp, rtrsp);
2397d072 1252 WARN_ON_ONCE(readstate & RCUTORTURE_RDR_MASK);
c116dba6
PM
1253
1254 /* If error or close call, record the sequence of reader protections. */
1255 if ((pipe_count > 1 || completed > 1) && !xchg(&err_segs_recorded, 1)) {
1256 i = 0;
1257 for (rtrsp1 = &rtseg[0]; rtrsp1 < rtrsp; rtrsp1++)
1258 err_segs[i++] = *rtrsp1;
1259 rt_read_nsegs = i;
1260 }
1261
6b06aa72
PM
1262 return true;
1263}
1264
3025520e
PM
1265static DEFINE_TORTURE_RANDOM_PERCPU(rcu_torture_timer_rand);
1266
0729fbf3
PM
1267/*
1268 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
1269 * incrementing the corresponding element of the pipeline array. The
1270 * counter in the element should never be greater than 1, otherwise, the
1271 * RCU implementation is broken.
1272 */
fd30b717 1273static void rcu_torture_timer(struct timer_list *unused)
0729fbf3 1274{
8da9a595 1275 atomic_long_inc(&n_rcu_torture_timers);
241b4252 1276 (void)rcu_torture_one_read(this_cpu_ptr(&rcu_torture_timer_rand));
f34c8585
PM
1277
1278 /* Test call_rcu() invocation from interrupt handler. */
1279 if (cur_ops->call) {
1280 struct rcu_head *rhp = kmalloc(sizeof(*rhp), GFP_NOWAIT);
1281
1282 if (rhp)
1283 cur_ops->call(rhp, rcu_torture_timer_cb);
1284 }
0729fbf3
PM
1285}
1286
a241ec65
PM
1287/*
1288 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
1289 * incrementing the corresponding element of the pipeline array. The
1290 * counter in the element should never be greater than 1, otherwise, the
1291 * RCU implementation is broken.
1292 */
1293static int
1294rcu_torture_reader(void *arg)
1295{
444da518 1296 unsigned long lastsleep = jiffies;
c04dd09b
PM
1297 long myid = (long)arg;
1298 int mynumonline = myid;
51b1130e 1299 DEFINE_TORTURE_RANDOM(rand);
0729fbf3 1300 struct timer_list t;
a241ec65 1301
5ccf60f2 1302 VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
d277d868 1303 set_user_nice(current, MAX_NICE);
0acc512c 1304 if (irqreader && cur_ops->irq_capable)
fd30b717 1305 timer_setup_on_stack(&t, rcu_torture_timer, 0);
dbdf65b1 1306
a241ec65 1307 do {
0acc512c 1308 if (irqreader && cur_ops->irq_capable) {
0729fbf3 1309 if (!timer_pending(&t))
6155fec9 1310 mod_timer(&t, jiffies + 1);
0729fbf3 1311 }
6b06aa72 1312 if (!rcu_torture_one_read(&rand))
a241ec65 1313 schedule_timeout_interruptible(HZ);
444da518
PM
1314 if (time_after(jiffies, lastsleep)) {
1315 schedule_timeout_interruptible(1);
1316 lastsleep = jiffies + 10;
1317 }
c04dd09b
PM
1318 while (num_online_cpus() < mynumonline && !torture_must_stop())
1319 schedule_timeout_interruptible(HZ / 5);
628edaa5 1320 stutter_wait("rcu_torture_reader");
36970bb9 1321 } while (!torture_must_stop());
424c1b68 1322 if (irqreader && cur_ops->irq_capable) {
0729fbf3 1323 del_timer_sync(&t);
424c1b68
TG
1324 destroy_timer_on_stack(&t);
1325 }
7fafaac5 1326 torture_kthread_stopping("rcu_torture_reader");
a241ec65
PM
1327 return 0;
1328}
1329
1330/*
eea203fe
JP
1331 * Print torture statistics. Caller must ensure that there is only
1332 * one call to this function at a given time!!! This is normally
1333 * accomplished by relying on the module system to only have one copy
1334 * of the module loaded, and then by giving the rcu_torture_stats
1335 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1336 * thread is not running).
a241ec65 1337 */
d1008950 1338static void
eea203fe 1339rcu_torture_stats_print(void)
a241ec65 1340{
a241ec65
PM
1341 int cpu;
1342 int i;
1343 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1344 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
ad0dc7f9 1345 static unsigned long rtcv_snap = ULONG_MAX;
0032f4e8 1346 static bool splatted;
4ffa6699 1347 struct task_struct *wtp;
a241ec65 1348
0a945022 1349 for_each_possible_cpu(cpu) {
a241ec65
PM
1350 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1351 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1352 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1353 }
1354 }
1355 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1356 if (pipesummary[i] != 0)
1357 break;
1358 }
eea203fe
JP
1359
1360 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1361 pr_cont("rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
1362 rcu_torture_current,
1363 rcu_torture_current_version,
1364 list_empty(&rcu_torture_freelist),
1365 atomic_read(&n_rcu_torture_alloc),
1366 atomic_read(&n_rcu_torture_alloc_fail),
1367 atomic_read(&n_rcu_torture_free));
472213a6 1368 pr_cont("rtmbe: %d rtbe: %ld rtbke: %ld rtbre: %ld ",
eea203fe 1369 atomic_read(&n_rcu_torture_mberror),
472213a6 1370 n_rcu_torture_barrier_error,
eea203fe
JP
1371 n_rcu_torture_boost_ktrerror,
1372 n_rcu_torture_boost_rterror);
1373 pr_cont("rtbf: %ld rtb: %ld nt: %ld ",
1374 n_rcu_torture_boost_failure,
1375 n_rcu_torture_boosts,
8da9a595 1376 atomic_long_read(&n_rcu_torture_timers));
eea203fe 1377 torture_onoff_stats();
fc6f9c57 1378 pr_cont("barrier: %ld/%ld:%ld\n",
eea203fe
JP
1379 n_barrier_successes,
1380 n_barrier_attempts,
1381 n_rcu_torture_barrier_error);
1382
1383 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
8e8be45e 1384 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
fae4b54f 1385 n_rcu_torture_barrier_error != 0 ||
8e8be45e
PM
1386 n_rcu_torture_boost_ktrerror != 0 ||
1387 n_rcu_torture_boost_rterror != 0 ||
fae4b54f
PM
1388 n_rcu_torture_boost_failure != 0 ||
1389 i > 1) {
eea203fe 1390 pr_cont("%s", "!!! ");
996417d2 1391 atomic_inc(&n_rcu_torture_error);
5af970a4 1392 WARN_ON_ONCE(1);
996417d2 1393 }
eea203fe 1394 pr_cont("Reader Pipe: ");
a241ec65 1395 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
eea203fe
JP
1396 pr_cont(" %ld", pipesummary[i]);
1397 pr_cont("\n");
1398
1399 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1400 pr_cont("Reader Batch: ");
72e9bb54 1401 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
eea203fe
JP
1402 pr_cont(" %ld", batchsummary[i]);
1403 pr_cont("\n");
1404
1405 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1406 pr_cont("Free-Block Circulation: ");
a241ec65 1407 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
eea203fe 1408 pr_cont(" %d", atomic_read(&rcu_torture_wcount[i]));
a241ec65 1409 }
eea203fe
JP
1410 pr_cont("\n");
1411
c8e5b163 1412 if (cur_ops->stats)
eea203fe 1413 cur_ops->stats();
ad0dc7f9
PM
1414 if (rtcv_snap == rcu_torture_current_version &&
1415 rcu_torture_current != NULL) {
7f6733c3 1416 int __maybe_unused flags = 0;
aebc8264 1417 unsigned long __maybe_unused gp_seq = 0;
ad0dc7f9
PM
1418
1419 rcutorture_get_gp_data(cur_ops->ttype,
aebc8264 1420 &flags, &gp_seq);
7f6733c3 1421 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp,
aebc8264 1422 &flags, &gp_seq);
4ffa6699 1423 wtp = READ_ONCE(writer_task);
aebc8264 1424 pr_alert("??? Writer stall state %s(%d) g%lu f%#x ->state %#lx cpu %d\n",
18aff33e 1425 rcu_torture_writer_state_getname(),
aebc8264 1426 rcu_torture_writer_state, gp_seq, flags,
808de39c
PM
1427 wtp == NULL ? ~0UL : wtp->state,
1428 wtp == NULL ? -1 : (int)task_cpu(wtp));
0032f4e8
PM
1429 if (!splatted && wtp) {
1430 sched_show_task(wtp);
1431 splatted = true;
1432 }
afea227f 1433 show_rcu_gp_kthreads();
274529ba 1434 rcu_ftrace_dump(DUMP_ALL);
ad0dc7f9
PM
1435 }
1436 rtcv_snap = rcu_torture_current_version;
a241ec65
PM
1437}
1438
a241ec65
PM
1439/*
1440 * Periodically prints torture statistics, if periodic statistics printing
1441 * was specified via the stat_interval module parameter.
a241ec65
PM
1442 */
1443static int
1444rcu_torture_stats(void *arg)
1445{
5ccf60f2 1446 VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
a241ec65
PM
1447 do {
1448 schedule_timeout_interruptible(stat_interval * HZ);
1449 rcu_torture_stats_print();
f67a3356 1450 torture_shutdown_absorb("rcu_torture_stats");
36970bb9 1451 } while (!torture_must_stop());
7fafaac5 1452 torture_kthread_stopping("rcu_torture_stats");
d120f65f
PM
1453 return 0;
1454}
1455
eac45e58 1456static void
e66c33d5 1457rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
95c38322 1458{
2caa1e44
PM
1459 pr_alert("%s" TORTURE_FLAG
1460 "--- %s: nreaders=%d nfakewriters=%d "
1461 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1462 "shuffle_interval=%d stutter=%d irqreader=%d "
1463 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1464 "test_boost=%d/%d test_boost_interval=%d "
1465 "test_boost_duration=%d shutdown_secs=%d "
2b1516e5 1466 "stall_cpu=%d stall_cpu_holdoff=%d stall_cpu_irqsoff=%d "
67afeed2 1467 "n_barrier_cbs=%d "
2caa1e44
PM
1468 "onoff_interval=%d onoff_holdoff=%d\n",
1469 torture_type, tag, nrealreaders, nfakewriters,
1470 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1471 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1472 test_boost, cur_ops->can_boost,
1473 test_boost_interval, test_boost_duration, shutdown_secs,
2b1516e5 1474 stall_cpu, stall_cpu_holdoff, stall_cpu_irqsoff,
67afeed2 1475 n_barrier_cbs,
2caa1e44 1476 onoff_interval, onoff_holdoff);
95c38322
PM
1477}
1478
0ffd374b 1479static int rcutorture_booster_cleanup(unsigned int cpu)
8e8be45e
PM
1480{
1481 struct task_struct *t;
1482
1483 if (boost_tasks[cpu] == NULL)
0ffd374b 1484 return 0;
8e8be45e 1485 mutex_lock(&boost_mutex);
8e8be45e
PM
1486 t = boost_tasks[cpu];
1487 boost_tasks[cpu] = NULL;
450efca7 1488 rcu_torture_enable_rt_throttle();
8e8be45e
PM
1489 mutex_unlock(&boost_mutex);
1490
1491 /* This must be outside of the mutex, otherwise deadlock! */
9c029b86 1492 torture_stop_kthread(rcu_torture_boost, t);
0ffd374b 1493 return 0;
8e8be45e
PM
1494}
1495
0ffd374b 1496static int rcutorture_booster_init(unsigned int cpu)
8e8be45e
PM
1497{
1498 int retval;
1499
1500 if (boost_tasks[cpu] != NULL)
1501 return 0; /* Already created, nothing more to do. */
1502
1503 /* Don't allow time recalculation while creating a new task. */
1504 mutex_lock(&boost_mutex);
450efca7 1505 rcu_torture_disable_rt_throttle();
5ccf60f2 1506 VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
1f288094
ED
1507 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1508 cpu_to_node(cpu),
1509 "rcu_torture_boost");
8e8be45e
PM
1510 if (IS_ERR(boost_tasks[cpu])) {
1511 retval = PTR_ERR(boost_tasks[cpu]);
5ccf60f2 1512 VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
8e8be45e
PM
1513 n_rcu_torture_boost_ktrerror++;
1514 boost_tasks[cpu] = NULL;
1515 mutex_unlock(&boost_mutex);
1516 return retval;
1517 }
1518 kthread_bind(boost_tasks[cpu], cpu);
1519 wake_up_process(boost_tasks[cpu]);
1520 mutex_unlock(&boost_mutex);
1521 return 0;
1522}
1523
c13f3757
PM
1524/*
1525 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1526 * induces a CPU stall for the time specified by stall_cpu.
1527 */
49fb4c62 1528static int rcu_torture_stall(void *args)
c13f3757
PM
1529{
1530 unsigned long stop_at;
1531
5ccf60f2 1532 VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
c13f3757 1533 if (stall_cpu_holdoff > 0) {
5ccf60f2 1534 VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
c13f3757 1535 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
5ccf60f2 1536 VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
c13f3757
PM
1537 }
1538 if (!kthread_should_stop()) {
622be33f 1539 stop_at = ktime_get_seconds() + stall_cpu;
c13f3757 1540 /* RCU CPU stall is expected behavior in following code. */
c13f3757 1541 rcu_read_lock();
2b1516e5
PM
1542 if (stall_cpu_irqsoff)
1543 local_irq_disable();
1544 else
1545 preempt_disable();
1546 pr_alert("rcu_torture_stall start on CPU %d.\n",
1547 smp_processor_id());
622be33f
AB
1548 while (ULONG_CMP_LT((unsigned long)ktime_get_seconds(),
1549 stop_at))
c13f3757 1550 continue; /* Induce RCU CPU stall warning. */
2b1516e5
PM
1551 if (stall_cpu_irqsoff)
1552 local_irq_enable();
1553 else
1554 preempt_enable();
c13f3757 1555 rcu_read_unlock();
2caa1e44 1556 pr_alert("rcu_torture_stall end.\n");
c13f3757 1557 }
f67a3356 1558 torture_shutdown_absorb("rcu_torture_stall");
c13f3757
PM
1559 while (!kthread_should_stop())
1560 schedule_timeout_interruptible(10 * HZ);
1561 return 0;
1562}
1563
1564/* Spawn CPU-stall kthread, if stall_cpu specified. */
1565static int __init rcu_torture_stall_init(void)
1566{
c13f3757
PM
1567 if (stall_cpu <= 0)
1568 return 0;
47cf29b9 1569 return torture_create_kthread(rcu_torture_stall, NULL, stall_task);
c13f3757
PM
1570}
1571
9fdcb9af
PM
1572/* State structure for forward-progress self-propagating RCU callback. */
1573struct fwd_cb_state {
1574 struct rcu_head rh;
1575 int stop;
1576};
1577
1578/*
1579 * Forward-progress self-propagating RCU callback function. Because
1580 * callbacks run from softirq, this function is an implicit RCU read-side
1581 * critical section.
1582 */
1583static void rcu_torture_fwd_prog_cb(struct rcu_head *rhp)
1584{
1585 struct fwd_cb_state *fcsp = container_of(rhp, struct fwd_cb_state, rh);
1586
1587 if (READ_ONCE(fcsp->stop)) {
1588 WRITE_ONCE(fcsp->stop, 2);
1589 return;
1590 }
1591 cur_ops->call(&fcsp->rh, rcu_torture_fwd_prog_cb);
1592}
1593
48718485
PM
1594/* State for continuous-flood RCU callbacks. */
1595struct rcu_fwd_cb {
1596 struct rcu_head rh;
1597 struct rcu_fwd_cb *rfc_next;
1598 int rfc_gps;
1599};
1600static DEFINE_SPINLOCK(rcu_fwd_lock);
1601static struct rcu_fwd_cb *rcu_fwd_cb_head;
1602static struct rcu_fwd_cb **rcu_fwd_cb_tail = &rcu_fwd_cb_head;
1603static long n_launders_cb;
1604static unsigned long rcu_fwd_startat;
e0aff973 1605static bool rcu_fwd_emergency_stop;
48718485
PM
1606#define MAX_FWD_CB_JIFFIES (8 * HZ) /* Maximum CB test duration. */
1607#define MIN_FWD_CB_LAUNDERS 3 /* This many CB invocations to count. */
1608#define MIN_FWD_CBS_LAUNDERED 100 /* Number of counted CBs. */
2e57bf97 1609#define FWD_CBS_HIST_DIV 10 /* Histogram buckets/second. */
cd618d10
PM
1610struct rcu_launder_hist {
1611 long n_launders;
1612 unsigned long launder_gp_seq;
1613};
1614#define N_LAUNDERS_HIST (2 * MAX_FWD_CB_JIFFIES / (HZ / FWD_CBS_HIST_DIV))
1615static struct rcu_launder_hist n_launders_hist[N_LAUNDERS_HIST];
1616static unsigned long rcu_launder_gp_seq_start;
48718485 1617
1a682754
PM
1618static void rcu_torture_fwd_cb_hist(void)
1619{
cd618d10
PM
1620 unsigned long gps;
1621 unsigned long gps_old;
1a682754
PM
1622 int i;
1623 int j;
1624
1625 for (i = ARRAY_SIZE(n_launders_hist) - 1; i > 0; i--)
cd618d10 1626 if (n_launders_hist[i].n_launders > 0)
1a682754 1627 break;
73d665b1
PM
1628 pr_alert("%s: Callback-invocation histogram (duration %lu jiffies):",
1629 __func__, jiffies - rcu_fwd_startat);
cd618d10
PM
1630 gps_old = rcu_launder_gp_seq_start;
1631 for (j = 0; j <= i; j++) {
1632 gps = n_launders_hist[j].launder_gp_seq;
1633 pr_cont(" %ds/%d: %ld:%ld",
1634 j + 1, FWD_CBS_HIST_DIV, n_launders_hist[j].n_launders,
1635 rcutorture_seq_diff(gps, gps_old));
1636 gps_old = gps;
1637 }
1a682754
PM
1638 pr_cont("\n");
1639}
1640
48718485
PM
1641/* Callback function for continuous-flood RCU callbacks. */
1642static void rcu_torture_fwd_cb_cr(struct rcu_head *rhp)
1643{
2667ccce 1644 unsigned long flags;
48718485
PM
1645 int i;
1646 struct rcu_fwd_cb *rfcp = container_of(rhp, struct rcu_fwd_cb, rh);
1647 struct rcu_fwd_cb **rfcpp;
1648
1649 rfcp->rfc_next = NULL;
1650 rfcp->rfc_gps++;
2667ccce 1651 spin_lock_irqsave(&rcu_fwd_lock, flags);
48718485
PM
1652 rfcpp = rcu_fwd_cb_tail;
1653 rcu_fwd_cb_tail = &rfcp->rfc_next;
1654 WRITE_ONCE(*rfcpp, rfcp);
1655 WRITE_ONCE(n_launders_cb, n_launders_cb + 1);
2e57bf97 1656 i = ((jiffies - rcu_fwd_startat) / (HZ / FWD_CBS_HIST_DIV));
48718485
PM
1657 if (i >= ARRAY_SIZE(n_launders_hist))
1658 i = ARRAY_SIZE(n_launders_hist) - 1;
cd618d10
PM
1659 n_launders_hist[i].n_launders++;
1660 n_launders_hist[i].launder_gp_seq = cur_ops->get_gp_seq();
2667ccce
PM
1661 spin_unlock_irqrestore(&rcu_fwd_lock, flags);
1662}
1663
1664/*
1665 * Free all callbacks on the rcu_fwd_cb_head list, either because the
1666 * test is over or because we hit an OOM event.
1667 */
1668static unsigned long rcu_torture_fwd_prog_cbfree(void)
1669{
1670 unsigned long flags;
1671 unsigned long freed = 0;
1672 struct rcu_fwd_cb *rfcp;
1673
1674 for (;;) {
1675 spin_lock_irqsave(&rcu_fwd_lock, flags);
1676 rfcp = rcu_fwd_cb_head;
140e53f2
PM
1677 if (!rfcp) {
1678 spin_unlock_irqrestore(&rcu_fwd_lock, flags);
2667ccce 1679 break;
140e53f2 1680 }
2667ccce
PM
1681 rcu_fwd_cb_head = rfcp->rfc_next;
1682 if (!rcu_fwd_cb_head)
1683 rcu_fwd_cb_tail = &rcu_fwd_cb_head;
1684 spin_unlock_irqrestore(&rcu_fwd_lock, flags);
1685 kfree(rfcp);
1686 freed++;
140e53f2 1687 cond_resched();
2667ccce 1688 }
2667ccce 1689 return freed;
48718485
PM
1690}
1691
6b3de7a1
PM
1692/* Carry out need_resched()/cond_resched() forward-progress testing. */
1693static void rcu_torture_fwd_prog_nr(int *tested, int *tested_tries)
1b27291b 1694{
119248be 1695 unsigned long cver;
f4de46ed 1696 unsigned long dur;
7c590fcc 1697 struct fwd_cb_state fcs;
119248be 1698 unsigned long gps;
1b27291b 1699 int idx;
6b3de7a1
PM
1700 int sd;
1701 int sd4;
1702 bool selfpropcb = false;
1703 unsigned long stopat;
1704 static DEFINE_TORTURE_RANDOM(trs);
1705
1706 if (cur_ops->call && cur_ops->sync && cur_ops->cb_barrier) {
1707 init_rcu_head_on_stack(&fcs.rh);
1708 selfpropcb = true;
1709 }
1710
1711 /* Tight loop containing cond_resched(). */
1712 if (selfpropcb) {
1713 WRITE_ONCE(fcs.stop, 0);
1714 cur_ops->call(&fcs.rh, rcu_torture_fwd_prog_cb);
1715 }
1716 cver = READ_ONCE(rcu_torture_current_version);
1717 gps = cur_ops->get_gp_seq();
1718 sd = cur_ops->stall_dur() + 1;
1719 sd4 = (sd + fwd_progress_div - 1) / fwd_progress_div;
1720 dur = sd4 + torture_random(&trs) % (sd - sd4);
61670adc 1721 WRITE_ONCE(rcu_fwd_startat, jiffies);
6b3de7a1 1722 stopat = rcu_fwd_startat + dur;
e0aff973
PM
1723 while (time_before(jiffies, stopat) &&
1724 !READ_ONCE(rcu_fwd_emergency_stop) && !torture_must_stop()) {
6b3de7a1
PM
1725 idx = cur_ops->readlock();
1726 udelay(10);
1727 cur_ops->readunlock(idx);
1728 if (!fwd_progress_need_resched || need_resched())
1729 cond_resched();
1730 }
1731 (*tested_tries)++;
e0aff973
PM
1732 if (!time_before(jiffies, stopat) &&
1733 !READ_ONCE(rcu_fwd_emergency_stop) && !torture_must_stop()) {
6b3de7a1
PM
1734 (*tested)++;
1735 cver = READ_ONCE(rcu_torture_current_version) - cver;
1736 gps = rcutorture_seq_diff(cur_ops->get_gp_seq(), gps);
1737 WARN_ON(!cver && gps < 2);
1738 pr_alert("%s: Duration %ld cver %ld gps %ld\n", __func__, dur, cver, gps);
1739 }
1740 if (selfpropcb) {
1741 WRITE_ONCE(fcs.stop, 1);
1742 cur_ops->sync(); /* Wait for running CB to complete. */
1743 cur_ops->cb_barrier(); /* Wait for queued callbacks. */
1744 }
1745
1746 if (selfpropcb) {
1747 WARN_ON(READ_ONCE(fcs.stop) != 2);
1748 destroy_rcu_head_on_stack(&fcs.rh);
1749 }
1750}
1751
1752/* Carry out call_rcu() forward-progress testing. */
1753static void rcu_torture_fwd_prog_cr(void)
1754{
1755 unsigned long cver;
1756 unsigned long gps;
1757 int i;
48718485
PM
1758 long n_launders;
1759 long n_launders_cb_snap;
1760 long n_launders_sa;
1761 long n_max_cbs;
1762 long n_max_gps;
1763 struct rcu_fwd_cb *rfcp;
1764 struct rcu_fwd_cb *rfcpn;
1b27291b 1765 unsigned long stopat;
48718485 1766 unsigned long stoppedat;
6b3de7a1 1767
2667ccce
PM
1768 if (READ_ONCE(rcu_fwd_emergency_stop))
1769 return; /* Get out of the way quickly, no GP wait! */
1770
6b3de7a1
PM
1771 /* Loop continuously posting RCU callbacks. */
1772 WRITE_ONCE(rcu_fwd_cb_nodelay, true);
1773 cur_ops->sync(); /* Later readers see above write. */
61670adc 1774 WRITE_ONCE(rcu_fwd_startat, jiffies);
6b3de7a1
PM
1775 stopat = rcu_fwd_startat + MAX_FWD_CB_JIFFIES;
1776 n_launders = 0;
1777 n_launders_cb = 0;
1778 n_launders_sa = 0;
1779 n_max_cbs = 0;
1780 n_max_gps = 0;
1781 for (i = 0; i < ARRAY_SIZE(n_launders_hist); i++)
cd618d10 1782 n_launders_hist[i].n_launders = 0;
6b3de7a1
PM
1783 cver = READ_ONCE(rcu_torture_current_version);
1784 gps = cur_ops->get_gp_seq();
cd618d10 1785 rcu_launder_gp_seq_start = gps;
e0aff973
PM
1786 while (time_before(jiffies, stopat) &&
1787 !READ_ONCE(rcu_fwd_emergency_stop) && !torture_must_stop()) {
6b3de7a1
PM
1788 rfcp = READ_ONCE(rcu_fwd_cb_head);
1789 rfcpn = NULL;
1790 if (rfcp)
1791 rfcpn = READ_ONCE(rfcp->rfc_next);
1792 if (rfcpn) {
1793 if (rfcp->rfc_gps >= MIN_FWD_CB_LAUNDERS &&
1794 ++n_max_gps >= MIN_FWD_CBS_LAUNDERED)
1795 break;
1796 rcu_fwd_cb_head = rfcpn;
1797 n_launders++;
1798 n_launders_sa++;
1799 } else {
1800 rfcp = kmalloc(sizeof(*rfcp), GFP_KERNEL);
1801 if (WARN_ON_ONCE(!rfcp)) {
1802 schedule_timeout_interruptible(1);
1803 continue;
1804 }
1805 n_max_cbs++;
1806 n_launders_sa = 0;
1807 rfcp->rfc_gps = 0;
1808 }
1809 cur_ops->call(&rfcp->rh, rcu_torture_fwd_cb_cr);
1810 cond_resched();
1811 }
1812 stoppedat = jiffies;
1813 n_launders_cb_snap = READ_ONCE(n_launders_cb);
1814 cver = READ_ONCE(rcu_torture_current_version) - cver;
1815 gps = rcutorture_seq_diff(cur_ops->get_gp_seq(), gps);
1816 cur_ops->cb_barrier(); /* Wait for callbacks to be invoked. */
2667ccce
PM
1817 (void)rcu_torture_fwd_prog_cbfree();
1818
6b3de7a1 1819 WRITE_ONCE(rcu_fwd_cb_nodelay, false);
2667ccce 1820 if (!torture_must_stop() && !READ_ONCE(rcu_fwd_emergency_stop)) {
6b3de7a1
PM
1821 WARN_ON(n_max_gps < MIN_FWD_CBS_LAUNDERED);
1822 pr_alert("%s Duration %lu barrier: %lu pending %ld n_launders: %ld n_launders_sa: %ld n_max_gps: %ld n_max_cbs: %ld cver %ld gps %ld\n",
1823 __func__,
1824 stoppedat - rcu_fwd_startat, jiffies - stoppedat,
1825 n_launders + n_max_cbs - n_launders_cb_snap,
1826 n_launders, n_launders_sa,
1827 n_max_gps, n_max_cbs, cver, gps);
1a682754 1828 rcu_torture_fwd_cb_hist();
6b3de7a1
PM
1829 }
1830}
1831
e0aff973
PM
1832
1833/*
1834 * OOM notifier, but this only prints diagnostic information for the
1835 * current forward-progress test.
1836 */
1837static int rcutorture_oom_notify(struct notifier_block *self,
1838 unsigned long notused, void *nfreed)
1839{
2667ccce
PM
1840 WARN(1, "%s invoked upon OOM during forward-progress testing.\n",
1841 __func__);
1a682754 1842 rcu_torture_fwd_cb_hist();
d44ac1be 1843 rcu_fwd_progress_check(1 + (jiffies - READ_ONCE(rcu_fwd_startat)) / 2);
e0aff973 1844 WRITE_ONCE(rcu_fwd_emergency_stop, true);
2667ccce
PM
1845 smp_mb(); /* Emergency stop before free and wait to avoid hangs. */
1846 pr_info("%s: Freed %lu RCU callbacks.\n",
1847 __func__, rcu_torture_fwd_prog_cbfree());
1848 rcu_barrier();
1849 pr_info("%s: Freed %lu RCU callbacks.\n",
1850 __func__, rcu_torture_fwd_prog_cbfree());
1851 rcu_barrier();
1852 pr_info("%s: Freed %lu RCU callbacks.\n",
1853 __func__, rcu_torture_fwd_prog_cbfree());
1854 smp_mb(); /* Frees before return to avoid redoing OOM. */
1855 (*(unsigned long *)nfreed)++; /* Forward progress CBs freed! */
1856 pr_info("%s returning after OOM processing.\n", __func__);
e0aff973
PM
1857 return NOTIFY_OK;
1858}
1859
1860static struct notifier_block rcutorture_oom_nb = {
1861 .notifier_call = rcutorture_oom_notify
1862};
1863
6b3de7a1
PM
1864/* Carry out grace-period forward-progress testing. */
1865static int rcu_torture_fwd_prog(void *args)
1866{
f4de46ed 1867 int tested = 0;
152f4afb 1868 int tested_tries = 0;
1b27291b
PM
1869
1870 VERBOSE_TOROUT_STRING("rcu_torture_fwd_progress task started");
5ab7ab83 1871 rcu_bind_current_to_nocb();
fecad509
PM
1872 if (!IS_ENABLED(CONFIG_SMP) || !IS_ENABLED(CONFIG_RCU_BOOST))
1873 set_user_nice(current, MAX_NICE);
1b27291b
PM
1874 do {
1875 schedule_timeout_interruptible(fwd_progress_holdoff * HZ);
e0aff973
PM
1876 WRITE_ONCE(rcu_fwd_emergency_stop, false);
1877 register_oom_notifier(&rcutorture_oom_nb);
6b3de7a1
PM
1878 rcu_torture_fwd_prog_nr(&tested, &tested_tries);
1879 rcu_torture_fwd_prog_cr();
e0aff973 1880 unregister_oom_notifier(&rcutorture_oom_nb);
48718485 1881
1b27291b
PM
1882 /* Avoid slow periods, better to test when busy. */
1883 stutter_wait("rcu_torture_fwd_prog");
1884 } while (!torture_must_stop());
152f4afb
PM
1885 /* Short runs might not contain a valid forward-progress attempt. */
1886 WARN_ON(!tested && tested_tries >= 5);
f4de46ed 1887 pr_alert("%s: tested %d tested_tries %d\n", __func__, tested, tested_tries);
1b27291b
PM
1888 torture_kthread_stopping("rcu_torture_fwd_prog");
1889 return 0;
1890}
1891
1892/* If forward-progress checking is requested and feasible, spawn the thread. */
1893static int __init rcu_torture_fwd_prog_init(void)
1894{
1895 if (!fwd_progress)
1896 return 0; /* Not requested, so don't do it. */
5ac7cdc2
PM
1897 if (!cur_ops->stall_dur || cur_ops->stall_dur() <= 0 ||
1898 cur_ops == &rcu_busted_ops) {
1b27291b
PM
1899 VERBOSE_TOROUT_STRING("rcu_torture_fwd_prog_init: Disabled, unsupported by RCU flavor under test");
1900 return 0;
1901 }
1902 if (stall_cpu > 0) {
1903 VERBOSE_TOROUT_STRING("rcu_torture_fwd_prog_init: Disabled, conflicts with CPU-stall testing");
1904 if (IS_MODULE(CONFIG_RCU_TORTURE_TESTS))
1905 return -EINVAL; /* In module, can fail back to user. */
1906 WARN_ON(1); /* Make sure rcutorture notices conflict. */
1907 return 0;
1908 }
1909 if (fwd_progress_holdoff <= 0)
1910 fwd_progress_holdoff = 1;
1911 if (fwd_progress_div <= 0)
1912 fwd_progress_div = 4;
1913 return torture_create_kthread(rcu_torture_fwd_prog,
1914 NULL, fwd_prog_task);
1915}
1916
fae4b54f 1917/* Callback function for RCU barrier testing. */
b3b8a4d4 1918static void rcu_torture_barrier_cbf(struct rcu_head *rcu)
fae4b54f
PM
1919{
1920 atomic_inc(&barrier_cbs_invoked);
1921}
1922
1923/* kthread function to register callbacks used to test RCU barriers. */
1924static int rcu_torture_barrier_cbs(void *arg)
1925{
1926 long myid = (long)arg;
c6ebcbb6 1927 bool lastphase = 0;
78e4bc34 1928 bool newphase;
fae4b54f
PM
1929 struct rcu_head rcu;
1930
1931 init_rcu_head_on_stack(&rcu);
5ccf60f2 1932 VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
d277d868 1933 set_user_nice(current, MAX_NICE);
fae4b54f
PM
1934 do {
1935 wait_event(barrier_cbs_wq[myid],
78e4bc34 1936 (newphase =
6c7ed42c 1937 smp_load_acquire(&barrier_phase)) != lastphase ||
36970bb9 1938 torture_must_stop());
78e4bc34 1939 lastphase = newphase;
36970bb9 1940 if (torture_must_stop())
fae4b54f 1941 break;
6c7ed42c
PM
1942 /*
1943 * The above smp_load_acquire() ensures barrier_phase load
aab05738 1944 * is ordered before the following ->call().
6c7ed42c 1945 */
0aa67e75 1946 local_irq_disable(); /* Just to test no-irq call_rcu(). */
fae4b54f 1947 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
0aa67e75 1948 local_irq_enable();
fae4b54f
PM
1949 if (atomic_dec_and_test(&barrier_cbs_count))
1950 wake_up(&barrier_wq);
36970bb9 1951 } while (!torture_must_stop());
69c60455
PM
1952 if (cur_ops->cb_barrier != NULL)
1953 cur_ops->cb_barrier();
fae4b54f 1954 destroy_rcu_head_on_stack(&rcu);
7fafaac5 1955 torture_kthread_stopping("rcu_torture_barrier_cbs");
fae4b54f
PM
1956 return 0;
1957}
1958
1959/* kthread function to drive and coordinate RCU barrier testing. */
1960static int rcu_torture_barrier(void *arg)
1961{
1962 int i;
1963
5ccf60f2 1964 VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
fae4b54f
PM
1965 do {
1966 atomic_set(&barrier_cbs_invoked, 0);
1967 atomic_set(&barrier_cbs_count, n_barrier_cbs);
6c7ed42c
PM
1968 /* Ensure barrier_phase ordered after prior assignments. */
1969 smp_store_release(&barrier_phase, !barrier_phase);
fae4b54f
PM
1970 for (i = 0; i < n_barrier_cbs; i++)
1971 wake_up(&barrier_cbs_wq[i]);
1972 wait_event(barrier_wq,
1973 atomic_read(&barrier_cbs_count) == 0 ||
36970bb9
PM
1974 torture_must_stop());
1975 if (torture_must_stop())
fae4b54f
PM
1976 break;
1977 n_barrier_attempts++;
78e4bc34 1978 cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
fae4b54f
PM
1979 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1980 n_rcu_torture_barrier_error++;
7602de4a
PM
1981 pr_err("barrier_cbs_invoked = %d, n_barrier_cbs = %d\n",
1982 atomic_read(&barrier_cbs_invoked),
1983 n_barrier_cbs);
fae4b54f 1984 WARN_ON_ONCE(1);
bf5b6435
JFG
1985 } else {
1986 n_barrier_successes++;
fae4b54f 1987 }
fae4b54f 1988 schedule_timeout_interruptible(HZ / 10);
36970bb9 1989 } while (!torture_must_stop());
7fafaac5 1990 torture_kthread_stopping("rcu_torture_barrier");
fae4b54f
PM
1991 return 0;
1992}
1993
1994/* Initialize RCU barrier testing. */
1995static int rcu_torture_barrier_init(void)
1996{
1997 int i;
1998 int ret;
1999
d9eba768 2000 if (n_barrier_cbs <= 0)
fae4b54f
PM
2001 return 0;
2002 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
2caa1e44
PM
2003 pr_alert("%s" TORTURE_FLAG
2004 " Call or barrier ops missing for %s,\n",
2005 torture_type, cur_ops->name);
2006 pr_alert("%s" TORTURE_FLAG
2007 " RCU barrier testing omitted from run.\n",
2008 torture_type);
fae4b54f
PM
2009 return 0;
2010 }
2011 atomic_set(&barrier_cbs_count, 0);
2012 atomic_set(&barrier_cbs_invoked, 0);
2013 barrier_cbs_tasks =
68a675d4 2014 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_tasks[0]),
fae4b54f
PM
2015 GFP_KERNEL);
2016 barrier_cbs_wq =
68a675d4 2017 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_wq[0]), GFP_KERNEL);
de5e6437 2018 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
fae4b54f
PM
2019 return -ENOMEM;
2020 for (i = 0; i < n_barrier_cbs; i++) {
2021 init_waitqueue_head(&barrier_cbs_wq[i]);
47cf29b9
PM
2022 ret = torture_create_kthread(rcu_torture_barrier_cbs,
2023 (void *)(long)i,
2024 barrier_cbs_tasks[i]);
2025 if (ret)
fae4b54f 2026 return ret;
fae4b54f 2027 }
47cf29b9 2028 return torture_create_kthread(rcu_torture_barrier, NULL, barrier_task);
fae4b54f
PM
2029}
2030
2031/* Clean up after RCU barrier testing. */
2032static void rcu_torture_barrier_cleanup(void)
2033{
2034 int i;
2035
9c029b86 2036 torture_stop_kthread(rcu_torture_barrier, barrier_task);
fae4b54f 2037 if (barrier_cbs_tasks != NULL) {
9c029b86
PM
2038 for (i = 0; i < n_barrier_cbs; i++)
2039 torture_stop_kthread(rcu_torture_barrier_cbs,
2040 barrier_cbs_tasks[i]);
fae4b54f
PM
2041 kfree(barrier_cbs_tasks);
2042 barrier_cbs_tasks = NULL;
2043 }
2044 if (barrier_cbs_wq != NULL) {
2045 kfree(barrier_cbs_wq);
2046 barrier_cbs_wq = NULL;
2047 }
2048}
2049
4babd855
JFG
2050static bool rcu_torture_can_boost(void)
2051{
2052 static int boost_warn_once;
2053 int prio;
2054
2055 if (!(test_boost == 1 && cur_ops->can_boost) && test_boost != 2)
2056 return false;
2057
2058 prio = rcu_get_gp_kthreads_prio();
2059 if (!prio)
2060 return false;
2061
2062 if (prio < 2) {
2063 if (boost_warn_once == 1)
2064 return false;
2065
bf5b6435 2066 pr_alert("%s: WARN: RCU kthread priority too low to test boosting. Skipping RCU boost test. Try passing rcutree.kthread_prio > 1 on the kernel command line.\n", KBUILD_MODNAME);
4babd855
JFG
2067 boost_warn_once = 1;
2068 return false;
2069 }
2070
2071 return true;
2072}
2073
0ffd374b 2074static enum cpuhp_state rcutor_hp;
8e8be45e 2075
a241ec65
PM
2076static void
2077rcu_torture_cleanup(void)
2078{
c116dba6 2079 int firsttime;
034777d7 2080 int flags = 0;
aebc8264 2081 unsigned long gp_seq = 0;
a241ec65
PM
2082 int i;
2083
d36a7a0d 2084 if (torture_cleanup_begin()) {
343e9099
PM
2085 if (cur_ops->cb_barrier != NULL)
2086 cur_ops->cb_barrier();
2087 return;
2088 }
b813afae
PM
2089 if (!cur_ops) {
2090 torture_cleanup_end();
2091 return;
2092 }
d84f5203 2093
fae4b54f 2094 rcu_torture_barrier_cleanup();
1b27291b 2095 torture_stop_kthread(rcu_torture_fwd_prog, fwd_prog_task);
9c029b86 2096 torture_stop_kthread(rcu_torture_stall, stall_task);
9c029b86 2097 torture_stop_kthread(rcu_torture_writer, writer_task);
a241ec65 2098
c8e5b163 2099 if (reader_tasks) {
9c029b86
PM
2100 for (i = 0; i < nrealreaders; i++)
2101 torture_stop_kthread(rcu_torture_reader,
2102 reader_tasks[i]);
a241ec65 2103 kfree(reader_tasks);
a241ec65
PM
2104 }
2105 rcu_torture_current = NULL;
2106
c8e5b163 2107 if (fakewriter_tasks) {
b772e1dd 2108 for (i = 0; i < nfakewriters; i++) {
9c029b86
PM
2109 torture_stop_kthread(rcu_torture_fakewriter,
2110 fakewriter_tasks[i]);
b772e1dd
JT
2111 }
2112 kfree(fakewriter_tasks);
2113 fakewriter_tasks = NULL;
2114 }
2115
aebc8264
PM
2116 rcutorture_get_gp_data(cur_ops->ttype, &flags, &gp_seq);
2117 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp, &flags, &gp_seq);
2118 pr_alert("%s: End-test grace-period state: g%lu f%#x\n",
2119 cur_ops->name, gp_seq, flags);
9c029b86
PM
2120 torture_stop_kthread(rcu_torture_stats, stats_task);
2121 torture_stop_kthread(rcu_torture_fqs, fqs_task);
4babd855 2122 if (rcu_torture_can_boost())
0ffd374b 2123 cpuhp_remove_state(rcutor_hp);
bf66f18e 2124
ca1d51ed 2125 /*
62a1a945 2126 * Wait for all RCU callbacks to fire, then do torture-type-specific
ca1d51ed
PM
2127 * cleanup operations.
2128 */
2326974d
PM
2129 if (cur_ops->cb_barrier != NULL)
2130 cur_ops->cb_barrier();
ca1d51ed
PM
2131 if (cur_ops->cleanup != NULL)
2132 cur_ops->cleanup();
a241ec65 2133
a241ec65 2134 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
72e9bb54 2135
c116dba6
PM
2136 if (err_segs_recorded) {
2137 pr_alert("Failure/close-call rcutorture reader segments:\n");
2138 if (rt_read_nsegs == 0)
2139 pr_alert("\t: No segments recorded!!!\n");
2140 firsttime = 1;
2141 for (i = 0; i < rt_read_nsegs; i++) {
2142 pr_alert("\t%d: %#x ", i, err_segs[i].rt_readstate);
2143 if (err_segs[i].rt_delay_jiffies != 0) {
2144 pr_cont("%s%ldjiffies", firsttime ? "" : "+",
2145 err_segs[i].rt_delay_jiffies);
2146 firsttime = 0;
2147 }
2148 if (err_segs[i].rt_delay_ms != 0) {
2149 pr_cont("%s%ldms", firsttime ? "" : "+",
2150 err_segs[i].rt_delay_ms);
2151 firsttime = 0;
2152 }
2153 if (err_segs[i].rt_delay_us != 0) {
2154 pr_cont("%s%ldus", firsttime ? "" : "+",
2155 err_segs[i].rt_delay_us);
2156 firsttime = 0;
2157 }
2158 pr_cont("%s\n",
2159 err_segs[i].rt_preempted ? "preempted" : "");
2160
2161 }
2162 }
fae4b54f 2163 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
8e8be45e 2164 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
2e9e8081 2165 else if (torture_onoff_failures())
091541bb
PM
2166 rcu_torture_print_module_parms(cur_ops,
2167 "End of test: RCU_HOTPLUG");
95c38322 2168 else
8e8be45e 2169 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
d36a7a0d 2170 torture_cleanup_end();
a241ec65
PM
2171}
2172
d2818df1
PM
2173#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
2174static void rcu_torture_leak_cb(struct rcu_head *rhp)
2175{
2176}
2177
2178static void rcu_torture_err_cb(struct rcu_head *rhp)
2179{
2180 /*
2181 * This -might- happen due to race conditions, but is unlikely.
2182 * The scenario that leads to this happening is that the
2183 * first of the pair of duplicate callbacks is queued,
2184 * someone else starts a grace period that includes that
2185 * callback, then the second of the pair must wait for the
2186 * next grace period. Unlikely, but can happen. If it
2187 * does happen, the debug-objects subsystem won't have splatted.
2188 */
e0d31a34 2189 pr_alert("%s: duplicated callback was invoked.\n", KBUILD_MODNAME);
d2818df1
PM
2190}
2191#endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2192
2193/*
2194 * Verify that double-free causes debug-objects to complain, but only
2195 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
2196 * cannot be carried out.
2197 */
2198static void rcu_test_debug_objects(void)
2199{
2200#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
2201 struct rcu_head rh1;
2202 struct rcu_head rh2;
2203
2204 init_rcu_head_on_stack(&rh1);
2205 init_rcu_head_on_stack(&rh2);
e0d31a34 2206 pr_alert("%s: WARN: Duplicate call_rcu() test starting.\n", KBUILD_MODNAME);
d2818df1
PM
2207
2208 /* Try to queue the rh2 pair of callbacks for the same grace period. */
2209 preempt_disable(); /* Prevent preemption from interrupting test. */
2210 rcu_read_lock(); /* Make it impossible to finish a grace period. */
2211 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
2212 local_irq_disable(); /* Make it harder to start a new grace period. */
2213 call_rcu(&rh2, rcu_torture_leak_cb);
2214 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
2215 local_irq_enable();
2216 rcu_read_unlock();
2217 preempt_enable();
2218
2219 /* Wait for them all to get done so we can safely return. */
2220 rcu_barrier();
e0d31a34 2221 pr_alert("%s: WARN: Duplicate call_rcu() test complete.\n", KBUILD_MODNAME);
d2818df1
PM
2222 destroy_rcu_head_on_stack(&rh1);
2223 destroy_rcu_head_on_stack(&rh2);
2224#else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
e0d31a34 2225 pr_alert("%s: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n", KBUILD_MODNAME);
d2818df1
PM
2226#endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2227}
2228
3a6cb58f
PM
2229static void rcutorture_sync(void)
2230{
2231 static unsigned long n;
2232
2233 if (cur_ops->sync && !(++n & 0xfff))
2234 cur_ops->sync();
2235}
2236
6f8bc500 2237static int __init
a241ec65
PM
2238rcu_torture_init(void)
2239{
c04dd09b 2240 long i;
a241ec65
PM
2241 int cpu;
2242 int firsterr = 0;
2ec1f2d9 2243 static struct rcu_torture_ops *torture_ops[] = {
c770c82a
PM
2244 &rcu_ops, &rcu_busted_ops, &srcu_ops, &srcud_ops,
2245 &busted_srcud_ops, &tasks_ops,
2ec1f2d9 2246 };
a241ec65 2247
a2f2577d 2248 if (!torture_init_begin(torture_type, verbose))
5228084e 2249 return -EBUSY;
343e9099 2250
a241ec65 2251 /* Process args and tell the world that the torturer is on the job. */
ade5fb81 2252 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
72e9bb54 2253 cur_ops = torture_ops[i];
ade5fb81 2254 if (strcmp(torture_type, cur_ops->name) == 0)
72e9bb54 2255 break;
72e9bb54 2256 }
ade5fb81 2257 if (i == ARRAY_SIZE(torture_ops)) {
2caa1e44
PM
2258 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
2259 torture_type);
2260 pr_alert("rcu-torture types:");
cf886c44 2261 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
a7538352
JP
2262 pr_cont(" %s", torture_ops[i]->name);
2263 pr_cont("\n");
e746b558 2264 WARN_ON(!IS_MODULE(CONFIG_RCU_TORTURE_TEST));
889d487a 2265 firsterr = -EINVAL;
b813afae 2266 cur_ops = NULL;
889d487a 2267 goto unwind;
72e9bb54 2268 }
bf66f18e 2269 if (cur_ops->fqs == NULL && fqs_duration != 0) {
2caa1e44 2270 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
bf66f18e
PM
2271 fqs_duration = 0;
2272 }
c8e5b163 2273 if (cur_ops->init)
889d487a 2274 cur_ops->init();
72e9bb54 2275
64e4b43a 2276 if (nreaders >= 0) {
a241ec65 2277 nrealreaders = nreaders;
64e4b43a 2278 } else {
3838cc18 2279 nrealreaders = num_online_cpus() - 2 - nreaders;
64e4b43a
PM
2280 if (nrealreaders <= 0)
2281 nrealreaders = 1;
2282 }
8e8be45e 2283 rcu_torture_print_module_parms(cur_ops, "Start of test");
a241ec65
PM
2284
2285 /* Set up the freelist. */
2286
2287 INIT_LIST_HEAD(&rcu_torture_freelist);
788e770e 2288 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
996417d2 2289 rcu_tortures[i].rtort_mbtest = 0;
a241ec65
PM
2290 list_add_tail(&rcu_tortures[i].rtort_free,
2291 &rcu_torture_freelist);
2292 }
2293
2294 /* Initialize the statistics so that each run gets its own numbers. */
2295
2296 rcu_torture_current = NULL;
2297 rcu_torture_current_version = 0;
2298 atomic_set(&n_rcu_torture_alloc, 0);
2299 atomic_set(&n_rcu_torture_alloc_fail, 0);
2300 atomic_set(&n_rcu_torture_free, 0);
996417d2
PM
2301 atomic_set(&n_rcu_torture_mberror, 0);
2302 atomic_set(&n_rcu_torture_error, 0);
fae4b54f 2303 n_rcu_torture_barrier_error = 0;
8e8be45e
PM
2304 n_rcu_torture_boost_ktrerror = 0;
2305 n_rcu_torture_boost_rterror = 0;
8e8be45e
PM
2306 n_rcu_torture_boost_failure = 0;
2307 n_rcu_torture_boosts = 0;
a241ec65
PM
2308 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
2309 atomic_set(&rcu_torture_wcount[i], 0);
0a945022 2310 for_each_possible_cpu(cpu) {
a241ec65
PM
2311 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
2312 per_cpu(rcu_torture_count, cpu)[i] = 0;
2313 per_cpu(rcu_torture_batch, cpu)[i] = 0;
2314 }
2315 }
c116dba6
PM
2316 err_segs_recorded = 0;
2317 rt_read_nsegs = 0;
a241ec65
PM
2318
2319 /* Start up the kthreads. */
2320
47cf29b9
PM
2321 firsterr = torture_create_kthread(rcu_torture_writer, NULL,
2322 writer_task);
2323 if (firsterr)
a241ec65 2324 goto unwind;
4444d852 2325 if (nfakewriters > 0) {
68a675d4 2326 fakewriter_tasks = kcalloc(nfakewriters,
4444d852
PM
2327 sizeof(fakewriter_tasks[0]),
2328 GFP_KERNEL);
2329 if (fakewriter_tasks == NULL) {
2330 VERBOSE_TOROUT_ERRSTRING("out of memory");
2331 firsterr = -ENOMEM;
2332 goto unwind;
2333 }
b772e1dd
JT
2334 }
2335 for (i = 0; i < nfakewriters; i++) {
47cf29b9
PM
2336 firsterr = torture_create_kthread(rcu_torture_fakewriter,
2337 NULL, fakewriter_tasks[i]);
2338 if (firsterr)
b772e1dd 2339 goto unwind;
b772e1dd 2340 }
68a675d4 2341 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
a241ec65
PM
2342 GFP_KERNEL);
2343 if (reader_tasks == NULL) {
5ccf60f2 2344 VERBOSE_TOROUT_ERRSTRING("out of memory");
a241ec65
PM
2345 firsterr = -ENOMEM;
2346 goto unwind;
2347 }
2348 for (i = 0; i < nrealreaders; i++) {
c04dd09b 2349 firsterr = torture_create_kthread(rcu_torture_reader, (void *)i,
47cf29b9
PM
2350 reader_tasks[i]);
2351 if (firsterr)
a241ec65 2352 goto unwind;
a241ec65
PM
2353 }
2354 if (stat_interval > 0) {
47cf29b9
PM
2355 firsterr = torture_create_kthread(rcu_torture_stats, NULL,
2356 stats_task);
2357 if (firsterr)
a241ec65 2358 goto unwind;
a241ec65 2359 }
e8e255f7 2360 if (test_no_idle_hz && shuffle_interval > 0) {
3808dc9f
PM
2361 firsterr = torture_shuffle_init(shuffle_interval * HZ);
2362 if (firsterr)
d84f5203 2363 goto unwind;
d84f5203 2364 }
d120f65f
PM
2365 if (stutter < 0)
2366 stutter = 0;
2367 if (stutter) {
628edaa5
PM
2368 firsterr = torture_stutter_init(stutter * HZ);
2369 if (firsterr)
d120f65f 2370 goto unwind;
d120f65f 2371 }
bf66f18e
PM
2372 if (fqs_duration < 0)
2373 fqs_duration = 0;
2374 if (fqs_duration) {
628edaa5 2375 /* Create the fqs thread */
d0d0606e
PM
2376 firsterr = torture_create_kthread(rcu_torture_fqs, NULL,
2377 fqs_task);
47cf29b9 2378 if (firsterr)
bf66f18e 2379 goto unwind;
bf66f18e 2380 }
8e8be45e
PM
2381 if (test_boost_interval < 1)
2382 test_boost_interval = 1;
2383 if (test_boost_duration < 2)
2384 test_boost_duration = 2;
4babd855 2385 if (rcu_torture_can_boost()) {
8e8be45e
PM
2386
2387 boost_starttime = jiffies + test_boost_interval * HZ;
0ffd374b
SAS
2388
2389 firsterr = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "RCU_TORTURE",
2390 rcutorture_booster_init,
2391 rcutorture_booster_cleanup);
2392 if (firsterr < 0)
2393 goto unwind;
2394 rcutor_hp = firsterr;
8e8be45e 2395 }
01025ebc
PM
2396 firsterr = torture_shutdown_init(shutdown_secs, rcu_torture_cleanup);
2397 if (firsterr)
37e377d2 2398 goto unwind;
3a6cb58f
PM
2399 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval,
2400 rcutorture_sync);
01025ebc 2401 if (firsterr)
37e377d2 2402 goto unwind;
01025ebc 2403 firsterr = rcu_torture_stall_init();
1b27291b
PM
2404 if (firsterr)
2405 goto unwind;
2406 firsterr = rcu_torture_fwd_prog_init();
01025ebc 2407 if (firsterr)
37e377d2 2408 goto unwind;
01025ebc
PM
2409 firsterr = rcu_torture_barrier_init();
2410 if (firsterr)
fae4b54f 2411 goto unwind;
d2818df1
PM
2412 if (object_debug)
2413 rcu_test_debug_objects();
b5daa8f3 2414 torture_init_end();
a241ec65
PM
2415 return 0;
2416
2417unwind:
b5daa8f3 2418 torture_init_end();
a241ec65
PM
2419 rcu_torture_cleanup();
2420 return firsterr;
2421}
2422
2423module_init(rcu_torture_init);
2424module_exit(rcu_torture_cleanup);