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