]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/rcu/rcutorture.c
UBUNTU: Ubuntu-5.15.0-39.42
[mirror_ubuntu-jammy-kernel.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 9 *
43cb5451 10 * See also: Documentation/RCU/torture.rst
a241ec65 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>
9cf8fc6f 23#include <linux/rcupdate_wait.h>
a241ec65 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>
d38e6dc6 47#include <linux/tick.h>
c1a76c0b 48#include <linux/rcupdate_trace.h>
5d73118a 49#include <linux/nmi.h>
a241ec65 50
25c36329
PM
51#include "rcu.h"
52
a241ec65 53MODULE_LICENSE("GPL");
2e24ce88 54MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com> and Josh Triplett <josh@joshtriplett.org>");
a241ec65 55
2397d072
PM
56/* Bits for ->extendables field, extendables param, and related definitions. */
57#define RCUTORTURE_RDR_SHIFT 8 /* Put SRCU index in upper bits. */
58#define RCUTORTURE_RDR_MASK ((1 << RCUTORTURE_RDR_SHIFT) - 1)
2ceebc03
PM
59#define RCUTORTURE_RDR_BH 0x01 /* Extend readers by disabling bh. */
60#define RCUTORTURE_RDR_IRQ 0x02 /* ... disabling interrupts. */
61#define RCUTORTURE_RDR_PREEMPT 0x04 /* ... disabling preemption. */
62#define RCUTORTURE_RDR_RBH 0x08 /* ... rcu_read_lock_bh(). */
63#define RCUTORTURE_RDR_SCHED 0x10 /* ... rcu_read_lock_sched(). */
64#define RCUTORTURE_RDR_RCU 0x20 /* ... entering another RCU reader. */
65#define RCUTORTURE_RDR_NBITS 6 /* Number of bits defined above. */
66#define RCUTORTURE_MAX_EXTEND \
67 (RCUTORTURE_RDR_BH | RCUTORTURE_RDR_IRQ | RCUTORTURE_RDR_PREEMPT | \
68 RCUTORTURE_RDR_RBH | RCUTORTURE_RDR_SCHED)
2397d072
PM
69#define RCUTORTURE_RDR_MAX_LOOPS 0x7 /* Maximum reader extensions. */
70 /* Must be power of two minus one. */
c116dba6 71#define RCUTORTURE_RDR_MAX_SEGS (RCUTORTURE_RDR_MAX_LOOPS + 3)
2397d072 72
2397d072
PM
73torture_param(int, extendables, RCUTORTURE_MAX_EXTEND,
74 "Extend readers by disabling bh (1), irqs (2), or preempt (4)");
9e250225
PM
75torture_param(int, fqs_duration, 0,
76 "Duration of fqs bursts (us), 0 to disable");
77torture_param(int, fqs_holdoff, 0, "Holdoff time within fqs bursts (us)");
78torture_param(int, fqs_stutter, 3, "Wait time between fqs bursts (s)");
1b27291b
PM
79torture_param(bool, fwd_progress, 1, "Test grace-period forward progress");
80torture_param(int, fwd_progress_div, 4, "Fraction of CPU stall to wait");
81torture_param(int, fwd_progress_holdoff, 60,
82 "Time between forward-progress tests (s)");
83torture_param(bool, fwd_progress_need_resched, 1,
84 "Hide cond_resched() behind need_resched()");
a48f3fad 85torture_param(bool, gp_cond, false, "Use conditional/async GP wait primitives");
9e250225
PM
86torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
87torture_param(bool, gp_normal, false,
88 "Use normal (non-expedited) GP wait primitives");
0fd0548d 89torture_param(bool, gp_poll, false, "Use polling GP wait primitives");
f0bf8fab 90torture_param(bool, gp_sync, false, "Use synchronous GP wait primitives");
9e250225 91torture_param(int, irqreader, 1, "Allow RCU readers from irq handlers");
d6855142 92torture_param(int, leakpointer, 0, "Leak pointer dereferences from readers");
9e250225
PM
93torture_param(int, n_barrier_cbs, 0,
94 "# of callbacks/kthreads for barrier testing");
95torture_param(int, nfakewriters, 4, "Number of RCU fake writer threads");
96torture_param(int, nreaders, -1, "Number of RCU reader threads");
97torture_param(int, object_debug, 0,
98 "Enable debug-object double call_rcu() testing");
99torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
100torture_param(int, onoff_interval, 0,
028be12b 101 "Time between CPU hotplugs (jiffies), 0=disable");
2c4319bd
PM
102torture_param(int, nocbs_nthreads, 0, "Number of NOCB toggle threads, 0 to disable");
103torture_param(int, nocbs_toggle, 1000, "Time between toggling nocb state (ms)");
4a5f133c
PM
104torture_param(int, read_exit_delay, 13,
105 "Delay between read-then-exit episodes (s)");
106torture_param(int, read_exit_burst, 16,
107 "# of read-then-exit bursts per episode, zero to disable");
9e250225
PM
108torture_param(int, shuffle_interval, 3, "Number of seconds between shuffles");
109torture_param(int, shutdown_secs, 0, "Shutdown time (s), <= zero to disable.");
110torture_param(int, stall_cpu, 0, "Stall duration (s), zero to disable.");
111torture_param(int, stall_cpu_holdoff, 10,
112 "Time to wait before starting stall (s).");
5d73118a
WLC
113torture_param(bool, stall_no_softlockup, false,
114 "Avoid softlockup warning during cpu stall.");
2b1516e5 115torture_param(int, stall_cpu_irqsoff, 0, "Disable interrupts while stalling.");
19a8ff95 116torture_param(int, stall_cpu_block, 0, "Sleep while stalling.");
55b2dcf5
PM
117torture_param(int, stall_gp_kthread, 0,
118 "Grace-period kthread stall duration (s).");
9e250225
PM
119torture_param(int, stat_interval, 60,
120 "Number of seconds between stats printk()s");
121torture_param(int, stutter, 5, "Number of seconds to run/halt test");
122torture_param(int, test_boost, 1, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
123torture_param(int, test_boost_duration, 4,
124 "Duration of each boost test, seconds.");
125torture_param(int, test_boost_interval, 7,
126 "Interval between boost tests, seconds.");
127torture_param(bool, test_no_idle_hz, true,
128 "Test support for tickless idle CPUs");
90127d60 129torture_param(int, verbose, 1,
b5daa8f3 130 "Enable verbose debugging printk()s");
9e250225 131
d10453e9 132static char *torture_type = "rcu";
d6ad6711 133module_param(torture_type, charp, 0444);
c770c82a 134MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, srcu, ...)");
a241ec65 135
2c4319bd 136static int nrealnocbers;
a241ec65
PM
137static int nrealreaders;
138static struct task_struct *writer_task;
b772e1dd 139static struct task_struct **fakewriter_tasks;
a241ec65 140static struct task_struct **reader_tasks;
2c4319bd 141static struct task_struct **nocb_tasks;
a241ec65 142static struct task_struct *stats_task;
bf66f18e 143static struct task_struct *fqs_task;
8e8be45e 144static struct task_struct *boost_tasks[NR_CPUS];
c13f3757 145static struct task_struct *stall_task;
1b27291b 146static struct task_struct *fwd_prog_task;
fae4b54f
PM
147static struct task_struct **barrier_cbs_tasks;
148static struct task_struct *barrier_task;
4a5f133c 149static struct task_struct *read_exit_task;
a241ec65
PM
150
151#define RCU_TORTURE_PIPE_LEN 10
152
00504537
PM
153// Mailbox-like structure to check RCU global memory ordering.
154struct rcu_torture_reader_check {
155 unsigned long rtc_myloops;
156 int rtc_chkrdr;
157 unsigned long rtc_chkloops;
158 int rtc_ready;
159 struct rcu_torture_reader_check *rtc_assigner;
160} ____cacheline_internodealigned_in_smp;
161
162// Update-side data structure used to check RCU readers.
a241ec65
PM
163struct rcu_torture {
164 struct rcu_head rtort_rcu;
165 int rtort_pipe_count;
166 struct list_head rtort_free;
996417d2 167 int rtort_mbtest;
00504537 168 struct rcu_torture_reader_check *rtort_chkp;
a241ec65
PM
169};
170
a241ec65 171static LIST_HEAD(rcu_torture_freelist);
0ddea0ea 172static struct rcu_torture __rcu *rcu_torture_current;
4a298656 173static unsigned long rcu_torture_current_version;
a241ec65
PM
174static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
175static DEFINE_SPINLOCK(rcu_torture_lock);
67522bee
PM
176static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count);
177static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch);
a241ec65 178static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
00504537 179static struct rcu_torture_reader_check *rcu_torture_reader_mbchk;
b2896d2e
PM
180static atomic_t n_rcu_torture_alloc;
181static atomic_t n_rcu_torture_alloc_fail;
182static atomic_t n_rcu_torture_free;
183static atomic_t n_rcu_torture_mberror;
00504537
PM
184static atomic_t n_rcu_torture_mbchk_fail;
185static atomic_t n_rcu_torture_mbchk_tries;
b2896d2e 186static atomic_t n_rcu_torture_error;
fae4b54f 187static long n_rcu_torture_barrier_error;
8e8be45e
PM
188static long n_rcu_torture_boost_ktrerror;
189static long n_rcu_torture_boost_rterror;
8e8be45e
PM
190static long n_rcu_torture_boost_failure;
191static long n_rcu_torture_boosts;
8da9a595 192static atomic_long_t n_rcu_torture_timers;
fae4b54f 193static long n_barrier_attempts;
bf5b6435 194static long n_barrier_successes; /* did rcu_barrier test succeed? */
4a5f133c 195static unsigned long n_read_exits;
e3033736 196static struct list_head rcu_torture_removed;
60013d5d 197static unsigned long shutdown_jiffies;
959954df 198static unsigned long start_gp_seq;
2c4319bd
PM
199static atomic_long_t n_nocb_offload;
200static atomic_long_t n_nocb_deoffload;
d120f65f 201
ad0dc7f9
PM
202static int rcu_torture_writer_state;
203#define RTWS_FIXED_DELAY 0
204#define RTWS_DELAY 1
205#define RTWS_REPLACE 2
206#define RTWS_DEF_FREE 3
207#define RTWS_EXP_SYNC 4
a48f3fad
PM
208#define RTWS_COND_GET 5
209#define RTWS_COND_SYNC 6
0fd0548d
PM
210#define RTWS_POLL_GET 7
211#define RTWS_POLL_WAIT 8
212#define RTWS_SYNC 9
213#define RTWS_STUTTER 10
214#define RTWS_STOPPING 11
18aff33e
PM
215static const char * const rcu_torture_writer_state_names[] = {
216 "RTWS_FIXED_DELAY",
217 "RTWS_DELAY",
218 "RTWS_REPLACE",
219 "RTWS_DEF_FREE",
220 "RTWS_EXP_SYNC",
221 "RTWS_COND_GET",
222 "RTWS_COND_SYNC",
0fd0548d
PM
223 "RTWS_POLL_GET",
224 "RTWS_POLL_WAIT",
18aff33e
PM
225 "RTWS_SYNC",
226 "RTWS_STUTTER",
227 "RTWS_STOPPING",
228};
229
c116dba6
PM
230/* Record reader segment types and duration for first failing read. */
231struct rt_read_seg {
232 int rt_readstate;
233 unsigned long rt_delay_jiffies;
234 unsigned long rt_delay_ms;
235 unsigned long rt_delay_us;
236 bool rt_preempted;
237};
238static int err_segs_recorded;
239static struct rt_read_seg err_segs[RCUTORTURE_RDR_MAX_SEGS];
240static int rt_read_nsegs;
241
18aff33e
PM
242static const char *rcu_torture_writer_state_getname(void)
243{
244 unsigned int i = READ_ONCE(rcu_torture_writer_state);
245
246 if (i >= ARRAY_SIZE(rcu_torture_writer_state_names))
247 return "???";
248 return rcu_torture_writer_state_names[i];
249}
ad0dc7f9 250
e4aa0da3
SR
251#ifdef CONFIG_RCU_TRACE
252static u64 notrace rcu_trace_clock_local(void)
253{
254 u64 ts = trace_clock_local();
a3b7b6c2
PM
255
256 (void)do_div(ts, NSEC_PER_USEC);
e4aa0da3
SR
257 return ts;
258}
259#else /* #ifdef CONFIG_RCU_TRACE */
260static u64 notrace rcu_trace_clock_local(void)
261{
262 return 0ULL;
263}
264#endif /* #else #ifdef CONFIG_RCU_TRACE */
265
60013d5d
PM
266/*
267 * Stop aggressive CPU-hog tests a bit before the end of the test in order
268 * to avoid interfering with test shutdown.
269 */
270static bool shutdown_time_arrived(void)
271{
272 return shutdown_secs && time_after(jiffies, shutdown_jiffies - 30 * HZ);
273}
274
8e8be45e 275static unsigned long boost_starttime; /* jiffies of next boost test start. */
58ade2db 276static DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
8e8be45e 277 /* and boost task create/destroy. */
fae4b54f 278static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
c6ebcbb6 279static bool barrier_phase; /* Test phase. */
fae4b54f
PM
280static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
281static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
282static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
8e8be45e 283
48718485
PM
284static bool rcu_fwd_cb_nodelay; /* Short rcu_torture_delay() delays. */
285
a241ec65
PM
286/*
287 * Allocate an element from the rcu_tortures pool.
288 */
97a41e26 289static struct rcu_torture *
a241ec65
PM
290rcu_torture_alloc(void)
291{
292 struct list_head *p;
293
adac1665 294 spin_lock_bh(&rcu_torture_lock);
a241ec65
PM
295 if (list_empty(&rcu_torture_freelist)) {
296 atomic_inc(&n_rcu_torture_alloc_fail);
adac1665 297 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
298 return NULL;
299 }
300 atomic_inc(&n_rcu_torture_alloc);
301 p = rcu_torture_freelist.next;
302 list_del_init(p);
adac1665 303 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
304 return container_of(p, struct rcu_torture, rtort_free);
305}
306
307/*
308 * Free an element to the rcu_tortures pool.
309 */
310static void
311rcu_torture_free(struct rcu_torture *p)
312{
313 atomic_inc(&n_rcu_torture_free);
adac1665 314 spin_lock_bh(&rcu_torture_lock);
a241ec65 315 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
adac1665 316 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
317}
318
72e9bb54
PM
319/*
320 * Operations vector for selecting different types of tests.
321 */
322
323struct rcu_torture_ops {
ad0dc7f9 324 int ttype;
72e9bb54 325 void (*init)(void);
ca1d51ed 326 void (*cleanup)(void);
72e9bb54 327 int (*readlock)(void);
c116dba6
PM
328 void (*read_delay)(struct torture_random_state *rrsp,
329 struct rt_read_seg *rtrsp);
72e9bb54 330 void (*readunlock)(int idx);
a5c095e0 331 int (*readlock_held)(void);
17ef2fe9 332 unsigned long (*get_gp_seq)(void);
d7219312 333 unsigned long (*gp_diff)(unsigned long new, unsigned long old);
0acc512c 334 void (*deferred_free)(struct rcu_torture *p);
b772e1dd 335 void (*sync)(void);
2ec1f2d9 336 void (*exp_sync)(void);
fd56f64b 337 unsigned long (*get_gp_state)(void);
0fd0548d
PM
338 unsigned long (*start_gp_poll)(void);
339 bool (*poll_gp_state)(unsigned long oldstate);
a48f3fad 340 void (*cond_sync)(unsigned long oldstate);
db3e8db4 341 call_rcu_func_t call;
2326974d 342 void (*cb_barrier)(void);
bf66f18e 343 void (*fqs)(void);
eea203fe 344 void (*stats)(void);
27c0f144 345 void (*gp_kthread_dbg)(void);
0260b92e 346 bool (*check_boost_failed)(unsigned long gp_state, int *cpup);
1b27291b 347 int (*stall_dur)(void);
0acc512c 348 int irq_capable;
8e8be45e 349 int can_boost;
2397d072 350 int extendables;
5eabea59 351 int slow_gps;
e66c33d5 352 const char *name;
72e9bb54 353};
a71fca58
PM
354
355static struct rcu_torture_ops *cur_ops;
72e9bb54
PM
356
357/*
358 * Definitions for rcu torture testing.
359 */
360
a5c095e0
PM
361static int torture_readlock_not_held(void)
362{
363 return rcu_read_lock_bh_held() || rcu_read_lock_sched_held();
364}
365
a49a4af7 366static int rcu_torture_read_lock(void) __acquires(RCU)
72e9bb54
PM
367{
368 rcu_read_lock();
369 return 0;
370}
371
c116dba6
PM
372static void
373rcu_read_delay(struct torture_random_state *rrsp, struct rt_read_seg *rtrsp)
b2896d2e 374{
d0af39e8
PM
375 unsigned long started;
376 unsigned long completed;
b8d57a76 377 const unsigned long shortdelay_us = 200;
1e696765 378 unsigned long longdelay_ms = 300;
d0af39e8 379 unsigned long long ts;
b2896d2e 380
b8d57a76
JT
381 /* We want a short delay sometimes to make a reader delay the grace
382 * period, and we want a long delay occasionally to trigger
383 * force_quiescent_state. */
b2896d2e 384
102c14d2 385 if (!READ_ONCE(rcu_fwd_cb_nodelay) &&
48718485 386 !(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms))) {
17ef2fe9 387 started = cur_ops->get_gp_seq();
d0af39e8 388 ts = rcu_trace_clock_local();
1e696765
PM
389 if (preempt_count() & (SOFTIRQ_MASK | HARDIRQ_MASK))
390 longdelay_ms = 5; /* Avoid triggering BH limits. */
b8d57a76 391 mdelay(longdelay_ms);
c116dba6 392 rtrsp->rt_delay_ms = longdelay_ms;
17ef2fe9 393 completed = cur_ops->get_gp_seq();
d0af39e8
PM
394 do_trace_rcu_torture_read(cur_ops->name, NULL, ts,
395 started, completed);
396 }
c116dba6 397 if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us))) {
b8d57a76 398 udelay(shortdelay_us);
c116dba6
PM
399 rtrsp->rt_delay_us = shortdelay_us;
400 }
51b1130e 401 if (!preempt_count() &&
c116dba6 402 !(torture_random(rrsp) % (nrealreaders * 500))) {
cc1321c9 403 torture_preempt_schedule(); /* QS only if preemptible. */
c116dba6
PM
404 rtrsp->rt_preempted = true;
405 }
b2896d2e
PM
406}
407
a49a4af7 408static void rcu_torture_read_unlock(int idx) __releases(RCU)
72e9bb54
PM
409{
410 rcu_read_unlock();
411}
412
a48f3fad
PM
413/*
414 * Update callback in the pipe. This should be invoked after a grace period.
415 */
416static bool
417rcu_torture_pipe_update_one(struct rcu_torture *rp)
418{
419 int i;
00504537 420 struct rcu_torture_reader_check *rtrcp = READ_ONCE(rp->rtort_chkp);
a48f3fad 421
00504537
PM
422 if (rtrcp) {
423 WRITE_ONCE(rp->rtort_chkp, NULL);
424 smp_store_release(&rtrcp->rtc_ready, 1); // Pair with smp_load_acquire().
425 }
20248910 426 i = READ_ONCE(rp->rtort_pipe_count);
a48f3fad
PM
427 if (i > RCU_TORTURE_PIPE_LEN)
428 i = RCU_TORTURE_PIPE_LEN;
429 atomic_inc(&rcu_torture_wcount[i]);
20248910
PM
430 WRITE_ONCE(rp->rtort_pipe_count, i + 1);
431 if (rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
a48f3fad
PM
432 rp->rtort_mbtest = 0;
433 return true;
434 }
435 return false;
436}
437
438/*
439 * Update all callbacks in the pipe. Suitable for synchronous grace-period
440 * primitives.
441 */
442static void
443rcu_torture_pipe_update(struct rcu_torture *old_rp)
444{
445 struct rcu_torture *rp;
446 struct rcu_torture *rp1;
447
448 if (old_rp)
449 list_add(&old_rp->rtort_free, &rcu_torture_removed);
450 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
451 if (rcu_torture_pipe_update_one(rp)) {
452 list_del(&rp->rtort_free);
453 rcu_torture_free(rp);
454 }
455 }
456}
457
72e9bb54
PM
458static void
459rcu_torture_cb(struct rcu_head *p)
460{
72e9bb54
PM
461 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
462
36970bb9 463 if (torture_must_stop_irq()) {
72e9bb54
PM
464 /* Test is ending, just drop callbacks on the floor. */
465 /* The next initialization will pick up the pieces. */
466 return;
467 }
a48f3fad 468 if (rcu_torture_pipe_update_one(rp))
72e9bb54 469 rcu_torture_free(rp);
a48f3fad 470 else
0acc512c 471 cur_ops->deferred_free(rp);
72e9bb54
PM
472}
473
6b80da42 474static unsigned long rcu_no_completed(void)
d9a3da06
PM
475{
476 return 0;
477}
478
72e9bb54
PM
479static void rcu_torture_deferred_free(struct rcu_torture *p)
480{
481 call_rcu(&p->rtort_rcu, rcu_torture_cb);
482}
483
e3033736
JT
484static void rcu_sync_torture_init(void)
485{
486 INIT_LIST_HEAD(&rcu_torture_removed);
487}
488
2ec1f2d9 489static struct rcu_torture_ops rcu_ops = {
0260b92e
PM
490 .ttype = RCU_FLAVOR,
491 .init = rcu_sync_torture_init,
492 .readlock = rcu_torture_read_lock,
493 .read_delay = rcu_read_delay,
494 .readunlock = rcu_torture_read_unlock,
495 .readlock_held = torture_readlock_not_held,
496 .get_gp_seq = rcu_get_gp_seq,
497 .gp_diff = rcu_seq_diff,
498 .deferred_free = rcu_torture_deferred_free,
499 .sync = synchronize_rcu,
500 .exp_sync = synchronize_rcu_expedited,
501 .get_gp_state = get_state_synchronize_rcu,
502 .start_gp_poll = start_poll_synchronize_rcu,
503 .poll_gp_state = poll_state_synchronize_rcu,
504 .cond_sync = cond_synchronize_rcu,
505 .call = call_rcu,
506 .cb_barrier = rcu_barrier,
507 .fqs = rcu_force_quiescent_state,
508 .stats = NULL,
509 .gp_kthread_dbg = show_rcu_gp_kthreads,
510 .check_boost_failed = rcu_check_boost_fail,
511 .stall_dur = rcu_jiffies_till_stall_check,
512 .irq_capable = 1,
513 .can_boost = IS_ENABLED(CONFIG_RCU_BOOST),
514 .extendables = RCUTORTURE_MAX_EXTEND,
515 .name = "rcu"
d9a3da06
PM
516};
517
ff20e251
PM
518/*
519 * Don't even think about trying any of these in real life!!!
520 * The names includes "busted", and they really means it!
521 * The only purpose of these functions is to provide a buggy RCU
522 * implementation to make sure that rcutorture correctly emits
523 * buggy-RCU error messages.
524 */
525static void rcu_busted_torture_deferred_free(struct rcu_torture *p)
526{
527 /* This is a deliberate bug for testing purposes only! */
528 rcu_torture_cb(&p->rtort_rcu);
529}
530
531static void synchronize_rcu_busted(void)
532{
533 /* This is a deliberate bug for testing purposes only! */
534}
535
536static void
b6a4ae76 537call_rcu_busted(struct rcu_head *head, rcu_callback_t func)
ff20e251
PM
538{
539 /* This is a deliberate bug for testing purposes only! */
540 func(head);
541}
542
543static struct rcu_torture_ops rcu_busted_ops = {
ad0dc7f9 544 .ttype = INVALID_RCU_FLAVOR,
ff20e251
PM
545 .init = rcu_sync_torture_init,
546 .readlock = rcu_torture_read_lock,
547 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
548 .readunlock = rcu_torture_read_unlock,
a5c095e0 549 .readlock_held = torture_readlock_not_held,
17ef2fe9 550 .get_gp_seq = rcu_no_completed,
ff20e251
PM
551 .deferred_free = rcu_busted_torture_deferred_free,
552 .sync = synchronize_rcu_busted,
553 .exp_sync = synchronize_rcu_busted,
554 .call = call_rcu_busted,
555 .cb_barrier = NULL,
556 .fqs = NULL,
557 .stats = NULL,
558 .irq_capable = 1,
b3c98314 559 .name = "busted"
ff20e251
PM
560};
561
b2896d2e
PM
562/*
563 * Definitions for srcu torture testing.
564 */
565
cda4dc81 566DEFINE_STATIC_SRCU(srcu_ctl);
ca1d51ed
PM
567static struct srcu_struct srcu_ctld;
568static struct srcu_struct *srcu_ctlp = &srcu_ctl;
b2896d2e 569
ca1d51ed 570static int srcu_torture_read_lock(void) __acquires(srcu_ctlp)
b2896d2e 571{
ca1d51ed 572 return srcu_read_lock(srcu_ctlp);
b2896d2e
PM
573}
574
c116dba6
PM
575static void
576srcu_read_delay(struct torture_random_state *rrsp, struct rt_read_seg *rtrsp)
b2896d2e
PM
577{
578 long delay;
579 const long uspertick = 1000000 / HZ;
580 const long longdelay = 10;
581
582 /* We want there to be long-running readers, but not all the time. */
583
51b1130e
PM
584 delay = torture_random(rrsp) %
585 (nrealreaders * 2 * longdelay * uspertick);
c116dba6 586 if (!delay && in_task()) {
b2896d2e 587 schedule_timeout_interruptible(longdelay);
c116dba6
PM
588 rtrsp->rt_delay_jiffies = longdelay;
589 } else {
590 rcu_read_delay(rrsp, rtrsp);
591 }
b2896d2e
PM
592}
593
ca1d51ed 594static void srcu_torture_read_unlock(int idx) __releases(srcu_ctlp)
b2896d2e 595{
ca1d51ed 596 srcu_read_unlock(srcu_ctlp, idx);
b2896d2e
PM
597}
598
a5c095e0
PM
599static int torture_srcu_read_lock_held(void)
600{
601 return srcu_read_lock_held(srcu_ctlp);
602}
603
6b80da42 604static unsigned long srcu_torture_completed(void)
b2896d2e 605{
ca1d51ed 606 return srcu_batches_completed(srcu_ctlp);
b2896d2e
PM
607}
608
9059c940
LJ
609static void srcu_torture_deferred_free(struct rcu_torture *rp)
610{
ca1d51ed 611 call_srcu(srcu_ctlp, &rp->rtort_rcu, rcu_torture_cb);
9059c940
LJ
612}
613
b772e1dd
JT
614static void srcu_torture_synchronize(void)
615{
ca1d51ed 616 synchronize_srcu(srcu_ctlp);
b772e1dd
JT
617}
618
0fd0548d
PM
619static unsigned long srcu_torture_get_gp_state(void)
620{
621 return get_state_synchronize_srcu(srcu_ctlp);
622}
623
624static unsigned long srcu_torture_start_gp_poll(void)
625{
626 return start_poll_synchronize_srcu(srcu_ctlp);
627}
628
629static bool srcu_torture_poll_gp_state(unsigned long oldstate)
630{
631 return poll_state_synchronize_srcu(srcu_ctlp, oldstate);
632}
633
e3f8d378 634static void srcu_torture_call(struct rcu_head *head,
b6a4ae76 635 rcu_callback_t func)
e3f8d378 636{
ca1d51ed 637 call_srcu(srcu_ctlp, head, func);
e3f8d378
PM
638}
639
640static void srcu_torture_barrier(void)
641{
ca1d51ed 642 srcu_barrier(srcu_ctlp);
e3f8d378
PM
643}
644
eea203fe 645static void srcu_torture_stats(void)
b2896d2e 646{
115a1a52 647 srcu_torture_stats_print(srcu_ctlp, torture_type, TORTURE_FLAG);
b2896d2e
PM
648}
649
2ec1f2d9
PM
650static void srcu_torture_synchronize_expedited(void)
651{
ca1d51ed 652 synchronize_srcu_expedited(srcu_ctlp);
2ec1f2d9
PM
653}
654
b2896d2e 655static struct rcu_torture_ops srcu_ops = {
ad0dc7f9 656 .ttype = SRCU_FLAVOR,
cda4dc81 657 .init = rcu_sync_torture_init,
0acc512c
PM
658 .readlock = srcu_torture_read_lock,
659 .read_delay = srcu_read_delay,
660 .readunlock = srcu_torture_read_unlock,
a5c095e0 661 .readlock_held = torture_srcu_read_lock_held,
17ef2fe9 662 .get_gp_seq = srcu_torture_completed,
9059c940 663 .deferred_free = srcu_torture_deferred_free,
0acc512c 664 .sync = srcu_torture_synchronize,
2ec1f2d9 665 .exp_sync = srcu_torture_synchronize_expedited,
0fd0548d
PM
666 .get_gp_state = srcu_torture_get_gp_state,
667 .start_gp_poll = srcu_torture_start_gp_poll,
668 .poll_gp_state = srcu_torture_poll_gp_state,
e3f8d378
PM
669 .call = srcu_torture_call,
670 .cb_barrier = srcu_torture_barrier,
0acc512c 671 .stats = srcu_torture_stats,
5e741fa9 672 .irq_capable = 1,
0acc512c 673 .name = "srcu"
b2896d2e
PM
674};
675
ca1d51ed
PM
676static void srcu_torture_init(void)
677{
678 rcu_sync_torture_init();
679 WARN_ON(init_srcu_struct(&srcu_ctld));
680 srcu_ctlp = &srcu_ctld;
681}
682
683static void srcu_torture_cleanup(void)
684{
f5ad3991 685 cleanup_srcu_struct(&srcu_ctld);
ca1d51ed
PM
686 srcu_ctlp = &srcu_ctl; /* In case of a later rcutorture run. */
687}
688
689/* As above, but dynamically allocated. */
690static struct rcu_torture_ops srcud_ops = {
691 .ttype = SRCU_FLAVOR,
692 .init = srcu_torture_init,
693 .cleanup = srcu_torture_cleanup,
694 .readlock = srcu_torture_read_lock,
695 .read_delay = srcu_read_delay,
696 .readunlock = srcu_torture_read_unlock,
a5c095e0 697 .readlock_held = torture_srcu_read_lock_held,
17ef2fe9 698 .get_gp_seq = srcu_torture_completed,
ca1d51ed
PM
699 .deferred_free = srcu_torture_deferred_free,
700 .sync = srcu_torture_synchronize,
701 .exp_sync = srcu_torture_synchronize_expedited,
702 .call = srcu_torture_call,
703 .cb_barrier = srcu_torture_barrier,
704 .stats = srcu_torture_stats,
5e741fa9 705 .irq_capable = 1,
ca1d51ed
PM
706 .name = "srcud"
707};
708
2397d072
PM
709/* As above, but broken due to inappropriate reader extension. */
710static struct rcu_torture_ops busted_srcud_ops = {
711 .ttype = SRCU_FLAVOR,
712 .init = srcu_torture_init,
713 .cleanup = srcu_torture_cleanup,
714 .readlock = srcu_torture_read_lock,
715 .read_delay = rcu_read_delay,
716 .readunlock = srcu_torture_read_unlock,
a5c095e0 717 .readlock_held = torture_srcu_read_lock_held,
2397d072
PM
718 .get_gp_seq = srcu_torture_completed,
719 .deferred_free = srcu_torture_deferred_free,
720 .sync = srcu_torture_synchronize,
721 .exp_sync = srcu_torture_synchronize_expedited,
722 .call = srcu_torture_call,
723 .cb_barrier = srcu_torture_barrier,
724 .stats = srcu_torture_stats,
725 .irq_capable = 1,
726 .extendables = RCUTORTURE_MAX_EXTEND,
727 .name = "busted_srcud"
728};
729
69c60455
PM
730/*
731 * Definitions for RCU-tasks torture testing.
732 */
733
734static int tasks_torture_read_lock(void)
735{
736 return 0;
737}
738
739static void tasks_torture_read_unlock(int idx)
740{
741}
742
743static void rcu_tasks_torture_deferred_free(struct rcu_torture *p)
744{
745 call_rcu_tasks(&p->rtort_rcu, rcu_torture_cb);
746}
747
9cf8fc6f
PM
748static void synchronize_rcu_mult_test(void)
749{
750 synchronize_rcu_mult(call_rcu_tasks, call_rcu);
751}
752
69c60455
PM
753static struct rcu_torture_ops tasks_ops = {
754 .ttype = RCU_TASKS_FLAVOR,
755 .init = rcu_sync_torture_init,
756 .readlock = tasks_torture_read_lock,
757 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
758 .readunlock = tasks_torture_read_unlock,
17ef2fe9 759 .get_gp_seq = rcu_no_completed,
69c60455
PM
760 .deferred_free = rcu_tasks_torture_deferred_free,
761 .sync = synchronize_rcu_tasks,
9cf8fc6f 762 .exp_sync = synchronize_rcu_mult_test,
69c60455
PM
763 .call = call_rcu_tasks,
764 .cb_barrier = rcu_barrier_tasks,
27c0f144 765 .gp_kthread_dbg = show_rcu_tasks_classic_gp_kthread,
69c60455
PM
766 .fqs = NULL,
767 .stats = NULL,
768 .irq_capable = 1,
5eabea59 769 .slow_gps = 1,
69c60455
PM
770 .name = "tasks"
771};
772
c682db55
PM
773/*
774 * Definitions for trivial CONFIG_PREEMPT=n-only torture testing.
775 * This implementation does not necessarily work well with CPU hotplug.
776 */
777
778static void synchronize_rcu_trivial(void)
779{
780 int cpu;
781
782 for_each_online_cpu(cpu) {
783 rcutorture_sched_setaffinity(current->pid, cpumask_of(cpu));
784 WARN_ON_ONCE(raw_smp_processor_id() != cpu);
785 }
786}
787
788static int rcu_torture_read_lock_trivial(void) __acquires(RCU)
789{
790 preempt_disable();
791 return 0;
792}
793
794static void rcu_torture_read_unlock_trivial(int idx) __releases(RCU)
795{
796 preempt_enable();
797}
798
799static struct rcu_torture_ops trivial_ops = {
800 .ttype = RCU_TRIVIAL_FLAVOR,
801 .init = rcu_sync_torture_init,
802 .readlock = rcu_torture_read_lock_trivial,
803 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
804 .readunlock = rcu_torture_read_unlock_trivial,
a5c095e0 805 .readlock_held = torture_readlock_not_held,
c682db55
PM
806 .get_gp_seq = rcu_no_completed,
807 .sync = synchronize_rcu_trivial,
808 .exp_sync = synchronize_rcu_trivial,
809 .fqs = NULL,
810 .stats = NULL,
811 .irq_capable = 1,
812 .name = "trivial"
813};
814
3d6e43c7
PM
815/*
816 * Definitions for rude RCU-tasks torture testing.
817 */
818
819static void rcu_tasks_rude_torture_deferred_free(struct rcu_torture *p)
820{
821 call_rcu_tasks_rude(&p->rtort_rcu, rcu_torture_cb);
822}
823
824static struct rcu_torture_ops tasks_rude_ops = {
825 .ttype = RCU_TASKS_RUDE_FLAVOR,
826 .init = rcu_sync_torture_init,
827 .readlock = rcu_torture_read_lock_trivial,
828 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
829 .readunlock = rcu_torture_read_unlock_trivial,
830 .get_gp_seq = rcu_no_completed,
831 .deferred_free = rcu_tasks_rude_torture_deferred_free,
832 .sync = synchronize_rcu_tasks_rude,
833 .exp_sync = synchronize_rcu_tasks_rude,
834 .call = call_rcu_tasks_rude,
835 .cb_barrier = rcu_barrier_tasks_rude,
27c0f144 836 .gp_kthread_dbg = show_rcu_tasks_rude_gp_kthread,
3d6e43c7
PM
837 .fqs = NULL,
838 .stats = NULL,
839 .irq_capable = 1,
840 .name = "tasks-rude"
841};
842
c1a76c0b
PM
843/*
844 * Definitions for tracing RCU-tasks torture testing.
845 */
846
847static int tasks_tracing_torture_read_lock(void)
848{
849 rcu_read_lock_trace();
850 return 0;
851}
852
853static void tasks_tracing_torture_read_unlock(int idx)
854{
855 rcu_read_unlock_trace();
856}
857
858static void rcu_tasks_tracing_torture_deferred_free(struct rcu_torture *p)
859{
860 call_rcu_tasks_trace(&p->rtort_rcu, rcu_torture_cb);
861}
862
863static struct rcu_torture_ops tasks_tracing_ops = {
864 .ttype = RCU_TASKS_TRACING_FLAVOR,
865 .init = rcu_sync_torture_init,
866 .readlock = tasks_tracing_torture_read_lock,
867 .read_delay = srcu_read_delay, /* just reuse srcu's version. */
868 .readunlock = tasks_tracing_torture_read_unlock,
a5c095e0 869 .readlock_held = rcu_read_lock_trace_held,
c1a76c0b
PM
870 .get_gp_seq = rcu_no_completed,
871 .deferred_free = rcu_tasks_tracing_torture_deferred_free,
872 .sync = synchronize_rcu_tasks_trace,
873 .exp_sync = synchronize_rcu_tasks_trace,
874 .call = call_rcu_tasks_trace,
875 .cb_barrier = rcu_barrier_tasks_trace,
27c0f144 876 .gp_kthread_dbg = show_rcu_tasks_trace_gp_kthread,
c1a76c0b
PM
877 .fqs = NULL,
878 .stats = NULL,
879 .irq_capable = 1,
880 .slow_gps = 1,
881 .name = "tasks-tracing"
882};
883
d7219312
PM
884static unsigned long rcutorture_seq_diff(unsigned long new, unsigned long old)
885{
886 if (!cur_ops->gp_diff)
887 return new - old;
888 return cur_ops->gp_diff(new, old);
889}
890
8e8be45e
PM
891/*
892 * RCU torture priority-boost testing. Runs one real-time thread per
ea6d962e
PM
893 * CPU for moderate bursts, repeatedly starting grace periods and waiting
894 * for them to complete. If a given grace period takes too long, we assume
895 * that priority inversion has occurred.
8e8be45e
PM
896 */
897
450efca7
JFG
898static int old_rt_runtime = -1;
899
900static void rcu_torture_disable_rt_throttle(void)
901{
902 /*
903 * Disable RT throttling so that rcutorture's boost threads don't get
904 * throttled. Only possible if rcutorture is built-in otherwise the
905 * user should manually do this by setting the sched_rt_period_us and
906 * sched_rt_runtime sysctls.
907 */
908 if (!IS_BUILTIN(CONFIG_RCU_TORTURE_TEST) || old_rt_runtime != -1)
909 return;
910
911 old_rt_runtime = sysctl_sched_rt_runtime;
912 sysctl_sched_rt_runtime = -1;
913}
914
915static void rcu_torture_enable_rt_throttle(void)
916{
917 if (!IS_BUILTIN(CONFIG_RCU_TORTURE_TEST) || old_rt_runtime == -1)
918 return;
919
920 sysctl_sched_rt_runtime = old_rt_runtime;
921 old_rt_runtime = -1;
922}
923
063f5a4d 924static bool rcu_torture_boost_failed(unsigned long gp_state, unsigned long *start)
3b745c89 925{
0260b92e 926 int cpu;
5e59fba5 927 static int dbg_done;
063f5a4d 928 unsigned long end = jiffies;
bcd4af44 929 bool gp_done;
0260b92e
PM
930 unsigned long j;
931 static unsigned long last_persist;
932 unsigned long lp;
933 unsigned long mininterval = test_boost_duration * HZ - HZ / 2;
5e59fba5 934
063f5a4d 935 if (end - *start > mininterval) {
7b9dad7a
PM
936 // Recheck after checking time to avoid false positives.
937 smp_mb(); // Time check before grace-period check.
938 if (cur_ops->poll_gp_state(gp_state))
939 return false; // passed, though perhaps just barely
0260b92e
PM
940 if (cur_ops->check_boost_failed && !cur_ops->check_boost_failed(gp_state, &cpu)) {
941 // At most one persisted message per boost test.
942 j = jiffies;
943 lp = READ_ONCE(last_persist);
944 if (time_after(j, lp + mininterval) && cmpxchg(&last_persist, lp, j) == lp)
945 pr_info("Boost inversion persisted: No QS from CPU %d\n", cpu);
946 return false; // passed on a technicality
947 }
3b745c89
JFG
948 VERBOSE_TOROUT_STRING("rcu_torture_boost boosting failed");
949 n_rcu_torture_boost_failure++;
ea6d962e
PM
950 if (!xchg(&dbg_done, 1) && cur_ops->gp_kthread_dbg) {
951 pr_info("Boost inversion thread ->rt_priority %u gp_state %lu jiffies %lu\n",
063f5a4d 952 current->rt_priority, gp_state, end - *start);
5e59fba5 953 cur_ops->gp_kthread_dbg();
bcd4af44
PM
954 // Recheck after print to flag grace period ending during splat.
955 gp_done = cur_ops->poll_gp_state(gp_state);
956 pr_info("Boost inversion: GP %lu %s.\n", gp_state,
957 gp_done ? "ended already" : "still pending");
3b745c89 958
ea6d962e 959 }
3b745c89 960
7b9dad7a 961 return true; // failed
063f5a4d
PM
962 } else if (cur_ops->check_boost_failed && !cur_ops->check_boost_failed(gp_state, NULL)) {
963 *start = jiffies;
3b745c89
JFG
964 }
965
7b9dad7a 966 return false; // passed
3b745c89
JFG
967}
968
8e8be45e
PM
969static int rcu_torture_boost(void *arg)
970{
8e8be45e 971 unsigned long endtime;
ea6d962e
PM
972 unsigned long gp_state;
973 unsigned long gp_state_time;
8e8be45e 974 unsigned long oldstarttime;
8e8be45e 975
5ccf60f2 976 VERBOSE_TOROUT_STRING("rcu_torture_boost started");
8e8be45e
PM
977
978 /* Set real-time priority. */
8b700983 979 sched_set_fifo_low(current);
8e8be45e
PM
980
981 /* Each pass through the following loop does one boost-test cycle. */
982 do {
5e59fba5 983 bool failed = false; // Test failed already in this test interval
ea6d962e 984 bool gp_initiated = false;
3b745c89 985
3b745c89
JFG
986 if (kthread_should_stop())
987 goto checkwait;
988
8e8be45e
PM
989 /* Wait for the next test interval. */
990 oldstarttime = boost_starttime;
3c80b402 991 while (time_before(jiffies, oldstarttime)) {
0e11c8e8 992 schedule_timeout_interruptible(oldstarttime - jiffies);
ab1b7880
PM
993 if (stutter_wait("rcu_torture_boost"))
994 sched_set_fifo_low(current);
36970bb9 995 if (torture_must_stop())
8e8be45e
PM
996 goto checkwait;
997 }
998
ea6d962e 999 // Do one boost-test interval.
8e8be45e 1000 endtime = oldstarttime + test_boost_duration * HZ;
3c80b402 1001 while (time_before(jiffies, endtime)) {
ea6d962e
PM
1002 // Has current GP gone too long?
1003 if (gp_initiated && !failed && !cur_ops->poll_gp_state(gp_state))
063f5a4d 1004 failed = rcu_torture_boost_failed(gp_state, &gp_state_time);
ea6d962e
PM
1005 // If we don't have a grace period in flight, start one.
1006 if (!gp_initiated || cur_ops->poll_gp_state(gp_state)) {
1007 gp_state = cur_ops->start_gp_poll();
1008 gp_initiated = true;
1009 gp_state_time = jiffies;
8e8be45e 1010 }
ea6d962e 1011 if (stutter_wait("rcu_torture_boost")) {
ab1b7880 1012 sched_set_fifo_low(current);
ea6d962e
PM
1013 // If the grace period already ended,
1014 // we don't know when that happened, so
1015 // start over.
1016 if (cur_ops->poll_gp_state(gp_state))
1017 gp_initiated = false;
1018 }
36970bb9 1019 if (torture_must_stop())
8e8be45e
PM
1020 goto checkwait;
1021 }
1022
ea6d962e
PM
1023 // In case the grace period extended beyond the end of the loop.
1024 if (gp_initiated && !failed && !cur_ops->poll_gp_state(gp_state))
063f5a4d 1025 rcu_torture_boost_failed(gp_state, &gp_state_time);
3b745c89 1026
8e8be45e
PM
1027 /*
1028 * Set the start time of the next test interval.
1029 * Yes, this is vulnerable to long delays, but such
1030 * delays simply cause a false negative for the next
1031 * interval. Besides, we are running at RT priority,
1032 * so delays should be relatively rare.
1033 */
ea6d962e 1034 while (oldstarttime == boost_starttime && !kthread_should_stop()) {
8e8be45e 1035 if (mutex_trylock(&boost_mutex)) {
8c7ec02e
PM
1036 if (oldstarttime == boost_starttime) {
1037 boost_starttime = jiffies + test_boost_interval * HZ;
1038 n_rcu_torture_boosts++;
1039 }
8e8be45e
PM
1040 mutex_unlock(&boost_mutex);
1041 break;
1042 }
1043 schedule_timeout_uninterruptible(1);
1044 }
1045
1046 /* Go do the stutter. */
ab1b7880
PM
1047checkwait: if (stutter_wait("rcu_torture_boost"))
1048 sched_set_fifo_low(current);
36970bb9 1049 } while (!torture_must_stop());
8e8be45e
PM
1050
1051 /* Clean up and exit. */
ea6d962e 1052 while (!kthread_should_stop()) {
7fafaac5 1053 torture_shutdown_absorb("rcu_torture_boost");
8e8be45e 1054 schedule_timeout_uninterruptible(1);
7fafaac5 1055 }
7fafaac5 1056 torture_kthread_stopping("rcu_torture_boost");
8e8be45e
PM
1057 return 0;
1058}
1059
bf66f18e
PM
1060/*
1061 * RCU torture force-quiescent-state kthread. Repeatedly induces
1062 * bursts of calls to force_quiescent_state(), increasing the probability
1063 * of occurrence of some important types of race conditions.
1064 */
1065static int
1066rcu_torture_fqs(void *arg)
1067{
1068 unsigned long fqs_resume_time;
1069 int fqs_burst_remaining;
ab1b7880 1070 int oldnice = task_nice(current);
bf66f18e 1071
5ccf60f2 1072 VERBOSE_TOROUT_STRING("rcu_torture_fqs task started");
bf66f18e
PM
1073 do {
1074 fqs_resume_time = jiffies + fqs_stutter * HZ;
3c80b402 1075 while (time_before(jiffies, fqs_resume_time) &&
93898fb1 1076 !kthread_should_stop()) {
bf66f18e
PM
1077 schedule_timeout_interruptible(1);
1078 }
1079 fqs_burst_remaining = fqs_duration;
93898fb1
PM
1080 while (fqs_burst_remaining > 0 &&
1081 !kthread_should_stop()) {
bf66f18e
PM
1082 cur_ops->fqs();
1083 udelay(fqs_holdoff);
1084 fqs_burst_remaining -= fqs_holdoff;
1085 }
ab1b7880
PM
1086 if (stutter_wait("rcu_torture_fqs"))
1087 sched_set_normal(current, oldnice);
36970bb9 1088 } while (!torture_must_stop());
7fafaac5 1089 torture_kthread_stopping("rcu_torture_fqs");
bf66f18e
PM
1090 return 0;
1091}
1092
18fbf307
PM
1093// Used by writers to randomly choose from the available grace-period
1094// primitives. The only purpose of the initialization is to size the array.
1095static int synctype[] = { RTWS_DEF_FREE, RTWS_EXP_SYNC, RTWS_COND_GET, RTWS_POLL_GET, RTWS_SYNC };
1096static int nsynctypes;
1097
a241ec65 1098/*
18fbf307 1099 * Determine which grace-period primitives are available.
a241ec65 1100 */
18fbf307 1101static void rcu_torture_write_types(void)
a241ec65 1102{
a48f3fad 1103 bool gp_cond1 = gp_cond, gp_exp1 = gp_exp, gp_normal1 = gp_normal;
0fd0548d 1104 bool gp_poll1 = gp_poll, gp_sync1 = gp_sync;
dbdf65b1 1105
a48f3fad 1106 /* Initialize synctype[] array. If none set, take default. */
0fd0548d
PM
1107 if (!gp_cond1 && !gp_exp1 && !gp_normal1 && !gp_poll1 && !gp_sync1)
1108 gp_cond1 = gp_exp1 = gp_normal1 = gp_poll1 = gp_sync1 = true;
fd56f64b 1109 if (gp_cond1 && cur_ops->get_gp_state && cur_ops->cond_sync) {
a48f3fad 1110 synctype[nsynctypes++] = RTWS_COND_GET;
db0c1a8a 1111 pr_info("%s: Testing conditional GPs.\n", __func__);
fd56f64b 1112 } else if (gp_cond && (!cur_ops->get_gp_state || !cur_ops->cond_sync)) {
e0d31a34 1113 pr_alert("%s: gp_cond without primitives.\n", __func__);
db0c1a8a
PM
1114 }
1115 if (gp_exp1 && cur_ops->exp_sync) {
a48f3fad 1116 synctype[nsynctypes++] = RTWS_EXP_SYNC;
db0c1a8a
PM
1117 pr_info("%s: Testing expedited GPs.\n", __func__);
1118 } else if (gp_exp && !cur_ops->exp_sync) {
e0d31a34 1119 pr_alert("%s: gp_exp without primitives.\n", __func__);
db0c1a8a
PM
1120 }
1121 if (gp_normal1 && cur_ops->deferred_free) {
a48f3fad 1122 synctype[nsynctypes++] = RTWS_DEF_FREE;
db0c1a8a
PM
1123 pr_info("%s: Testing asynchronous GPs.\n", __func__);
1124 } else if (gp_normal && !cur_ops->deferred_free) {
e0d31a34 1125 pr_alert("%s: gp_normal without primitives.\n", __func__);
db0c1a8a 1126 }
0fd0548d
PM
1127 if (gp_poll1 && cur_ops->start_gp_poll && cur_ops->poll_gp_state) {
1128 synctype[nsynctypes++] = RTWS_POLL_GET;
1129 pr_info("%s: Testing polling GPs.\n", __func__);
1130 } else if (gp_poll && (!cur_ops->start_gp_poll || !cur_ops->poll_gp_state)) {
1131 pr_alert("%s: gp_poll without primitives.\n", __func__);
1132 }
db0c1a8a 1133 if (gp_sync1 && cur_ops->sync) {
f0bf8fab 1134 synctype[nsynctypes++] = RTWS_SYNC;
db0c1a8a
PM
1135 pr_info("%s: Testing normal GPs.\n", __func__);
1136 } else if (gp_sync && !cur_ops->sync) {
e0d31a34 1137 pr_alert("%s: gp_sync without primitives.\n", __func__);
db0c1a8a 1138 }
18fbf307
PM
1139}
1140
1141/*
1142 * RCU torture writer kthread. Repeatedly substitutes a new structure
1143 * for that pointed to by rcu_torture_current, freeing the old structure
1144 * after a series of grace periods (the "pipeline").
1145 */
1146static int
1147rcu_torture_writer(void *arg)
1148{
1149 bool boot_ended;
1150 bool can_expedite = !rcu_gp_is_expedited() && !rcu_gp_is_normal();
1151 unsigned long cookie;
1152 int expediting = 0;
1153 unsigned long gp_snap;
1154 int i;
1155 int idx;
1156 int oldnice = task_nice(current);
1157 struct rcu_torture *rp;
1158 struct rcu_torture *old_rp;
1159 static DEFINE_TORTURE_RANDOM(rand);
1160 bool stutter_waited;
1161
1162 VERBOSE_TOROUT_STRING("rcu_torture_writer task started");
1163 if (!can_expedite)
1164 pr_alert("%s" TORTURE_FLAG
1165 " GP expediting controlled from boot/sysfs for %s.\n",
1166 torture_type, cur_ops->name);
a48f3fad
PM
1167 if (WARN_ONCE(nsynctypes == 0,
1168 "rcu_torture_writer: No update-side primitives.\n")) {
f0bf8fab
PM
1169 /*
1170 * No updates primitives, so don't try updating.
1171 * The resulting test won't be testing much, hence the
1172 * above WARN_ONCE().
1173 */
a48f3fad
PM
1174 rcu_torture_writer_state = RTWS_STOPPING;
1175 torture_kthread_stopping("rcu_torture_writer");
1176 }
1177
a241ec65 1178 do {
ad0dc7f9 1179 rcu_torture_writer_state = RTWS_FIXED_DELAY;
1eba0ef9 1180 torture_hrtimeout_us(500, 1000, &rand);
a71fca58
PM
1181 rp = rcu_torture_alloc();
1182 if (rp == NULL)
a241ec65
PM
1183 continue;
1184 rp->rtort_pipe_count = 0;
ad0dc7f9 1185 rcu_torture_writer_state = RTWS_DELAY;
51b1130e 1186 udelay(torture_random(&rand) & 0x3ff);
ad0dc7f9 1187 rcu_torture_writer_state = RTWS_REPLACE;
0ddea0ea
PM
1188 old_rp = rcu_dereference_check(rcu_torture_current,
1189 current == writer_task);
996417d2 1190 rp->rtort_mbtest = 1;
a241ec65 1191 rcu_assign_pointer(rcu_torture_current, rp);
9b2619af 1192 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
c8e5b163 1193 if (old_rp) {
a241ec65
PM
1194 i = old_rp->rtort_pipe_count;
1195 if (i > RCU_TORTURE_PIPE_LEN)
1196 i = RCU_TORTURE_PIPE_LEN;
1197 atomic_inc(&rcu_torture_wcount[i]);
20248910
PM
1198 WRITE_ONCE(old_rp->rtort_pipe_count,
1199 old_rp->rtort_pipe_count + 1);
0fd0548d
PM
1200 if (cur_ops->get_gp_state && cur_ops->poll_gp_state) {
1201 idx = cur_ops->readlock();
1202 cookie = cur_ops->get_gp_state();
1203 WARN_ONCE(rcu_torture_writer_state != RTWS_DEF_FREE &&
1204 cur_ops->poll_gp_state(cookie),
1205 "%s: Cookie check 1 failed %s(%d) %lu->%lu\n",
1206 __func__,
1207 rcu_torture_writer_state_getname(),
1208 rcu_torture_writer_state,
1209 cookie, cur_ops->get_gp_state());
1210 cur_ops->readunlock(idx);
1211 }
a48f3fad
PM
1212 switch (synctype[torture_random(&rand) % nsynctypes]) {
1213 case RTWS_DEF_FREE:
ad0dc7f9 1214 rcu_torture_writer_state = RTWS_DEF_FREE;
2ec1f2d9 1215 cur_ops->deferred_free(old_rp);
a48f3fad
PM
1216 break;
1217 case RTWS_EXP_SYNC:
ad0dc7f9 1218 rcu_torture_writer_state = RTWS_EXP_SYNC;
2ec1f2d9 1219 cur_ops->exp_sync();
a48f3fad
PM
1220 rcu_torture_pipe_update(old_rp);
1221 break;
1222 case RTWS_COND_GET:
1223 rcu_torture_writer_state = RTWS_COND_GET;
fd56f64b 1224 gp_snap = cur_ops->get_gp_state();
ea31fd9c 1225 torture_hrtimeout_jiffies(torture_random(&rand) % 16, &rand);
a48f3fad
PM
1226 rcu_torture_writer_state = RTWS_COND_SYNC;
1227 cur_ops->cond_sync(gp_snap);
1228 rcu_torture_pipe_update(old_rp);
1229 break;
0fd0548d
PM
1230 case RTWS_POLL_GET:
1231 rcu_torture_writer_state = RTWS_POLL_GET;
1232 gp_snap = cur_ops->start_gp_poll();
1233 rcu_torture_writer_state = RTWS_POLL_WAIT;
ea31fd9c
PM
1234 while (!cur_ops->poll_gp_state(gp_snap))
1235 torture_hrtimeout_jiffies(torture_random(&rand) % 16,
1236 &rand);
0fd0548d
PM
1237 rcu_torture_pipe_update(old_rp);
1238 break;
f0bf8fab
PM
1239 case RTWS_SYNC:
1240 rcu_torture_writer_state = RTWS_SYNC;
1241 cur_ops->sync();
1242 rcu_torture_pipe_update(old_rp);
1243 break;
a48f3fad
PM
1244 default:
1245 WARN_ON_ONCE(1);
1246 break;
2ec1f2d9 1247 }
a241ec65 1248 }
1b27291b
PM
1249 WRITE_ONCE(rcu_torture_current_version,
1250 rcu_torture_current_version + 1);
4bb3c5f4
PM
1251 /* Cycle through nesting levels of rcu_expedite_gp() calls. */
1252 if (can_expedite &&
1253 !(torture_random(&rand) & 0xff & (!!expediting - 1))) {
1254 WARN_ON_ONCE(expediting == 0 && rcu_gp_is_expedited());
1255 if (expediting >= 0)
1256 rcu_expedite_gp();
1257 else
1258 rcu_unexpedite_gp();
1259 if (++expediting > 3)
1260 expediting = -expediting;
f7c0e6ad
PM
1261 } else if (!can_expedite) { /* Disabled during boot, recheck. */
1262 can_expedite = !rcu_gp_is_expedited() &&
1263 !rcu_gp_is_normal();
4bb3c5f4 1264 }
ad0dc7f9 1265 rcu_torture_writer_state = RTWS_STUTTER;
12a910e3 1266 boot_ended = rcu_inkernel_boot_has_ended();
ab1b7880
PM
1267 stutter_waited = stutter_wait("rcu_torture_writer");
1268 if (stutter_waited &&
5eabea59 1269 !READ_ONCE(rcu_fwd_cb_nodelay) &&
3432d765 1270 !cur_ops->slow_gps &&
59ee0326 1271 !torture_must_stop() &&
12a910e3 1272 boot_ended)
474e59b4 1273 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++)
e8516c64
PM
1274 if (list_empty(&rcu_tortures[i].rtort_free) &&
1275 rcu_access_pointer(rcu_torture_current) !=
34aa34b8
PM
1276 &rcu_tortures[i]) {
1277 rcu_ftrace_dump(DUMP_ALL);
e8516c64 1278 WARN(1, "%s: rtort_pipe_count: %d\n", __func__, rcu_tortures[i].rtort_pipe_count);
34aa34b8 1279 }
ab1b7880
PM
1280 if (stutter_waited)
1281 sched_set_normal(current, oldnice);
36970bb9 1282 } while (!torture_must_stop());
cae7cc6b 1283 rcu_torture_current = NULL; // Let stats task know that we are done.
4bb3c5f4
PM
1284 /* Reset expediting back to unexpedited. */
1285 if (expediting > 0)
1286 expediting = -expediting;
1287 while (can_expedite && expediting++ < 0)
1288 rcu_unexpedite_gp();
1289 WARN_ON_ONCE(can_expedite && rcu_gp_is_expedited());
f7c0e6ad
PM
1290 if (!can_expedite)
1291 pr_alert("%s" TORTURE_FLAG
1292 " Dynamic grace-period expediting was disabled.\n",
1293 torture_type);
ad0dc7f9 1294 rcu_torture_writer_state = RTWS_STOPPING;
7fafaac5 1295 torture_kthread_stopping("rcu_torture_writer");
a241ec65
PM
1296 return 0;
1297}
1298
b772e1dd
JT
1299/*
1300 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
1301 * delay between calls.
1302 */
1303static int
1304rcu_torture_fakewriter(void *arg)
1305{
682189a3 1306 unsigned long gp_snap;
51b1130e 1307 DEFINE_TORTURE_RANDOM(rand);
b772e1dd 1308
5ccf60f2 1309 VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
d277d868 1310 set_user_nice(current, MAX_NICE);
b772e1dd
JT
1311
1312 do {
1eba0ef9 1313 torture_hrtimeout_jiffies(torture_random(&rand) % 10, &rand);
72472a02 1314 if (cur_ops->cb_barrier != NULL &&
51b1130e 1315 torture_random(&rand) % (nfakewriters * 8) == 0) {
72472a02 1316 cur_ops->cb_barrier();
682189a3
PM
1317 } else {
1318 switch (synctype[torture_random(&rand) % nsynctypes]) {
1319 case RTWS_DEF_FREE:
1320 break;
1321 case RTWS_EXP_SYNC:
2ec1f2d9 1322 cur_ops->exp_sync();
682189a3
PM
1323 break;
1324 case RTWS_COND_GET:
1325 gp_snap = cur_ops->get_gp_state();
ea31fd9c 1326 torture_hrtimeout_jiffies(torture_random(&rand) % 16, &rand);
682189a3
PM
1327 cur_ops->cond_sync(gp_snap);
1328 break;
1329 case RTWS_POLL_GET:
1330 gp_snap = cur_ops->start_gp_poll();
1331 while (!cur_ops->poll_gp_state(gp_snap)) {
ea31fd9c
PM
1332 torture_hrtimeout_jiffies(torture_random(&rand) % 16,
1333 &rand);
682189a3
PM
1334 }
1335 break;
1336 case RTWS_SYNC:
1337 cur_ops->sync();
1338 break;
1339 default:
1340 WARN_ON_ONCE(1);
1341 break;
1342 }
2ec1f2d9 1343 }
628edaa5 1344 stutter_wait("rcu_torture_fakewriter");
36970bb9 1345 } while (!torture_must_stop());
b772e1dd 1346
7fafaac5 1347 torture_kthread_stopping("rcu_torture_fakewriter");
b772e1dd
JT
1348 return 0;
1349}
1350
f34c8585
PM
1351static void rcu_torture_timer_cb(struct rcu_head *rhp)
1352{
1353 kfree(rhp);
1354}
1355
00504537
PM
1356// Set up and carry out testing of RCU's global memory ordering
1357static void rcu_torture_reader_do_mbchk(long myid, struct rcu_torture *rtp,
1358 struct torture_random_state *trsp)
1359{
1360 unsigned long loops;
1afb95fe 1361 int noc = torture_num_online_cpus();
00504537
PM
1362 int rdrchked;
1363 int rdrchker;
1364 struct rcu_torture_reader_check *rtrcp; // Me.
1365 struct rcu_torture_reader_check *rtrcp_assigner; // Assigned us to do checking.
1366 struct rcu_torture_reader_check *rtrcp_chked; // Reader being checked.
1367 struct rcu_torture_reader_check *rtrcp_chker; // Reader doing checking when not me.
1368
1369 if (myid < 0)
1370 return; // Don't try this from timer handlers.
1371
1372 // Increment my counter.
1373 rtrcp = &rcu_torture_reader_mbchk[myid];
1374 WRITE_ONCE(rtrcp->rtc_myloops, rtrcp->rtc_myloops + 1);
1375
1376 // Attempt to assign someone else some checking work.
1377 rdrchked = torture_random(trsp) % nrealreaders;
1378 rtrcp_chked = &rcu_torture_reader_mbchk[rdrchked];
1379 rdrchker = torture_random(trsp) % nrealreaders;
1380 rtrcp_chker = &rcu_torture_reader_mbchk[rdrchker];
1381 if (rdrchked != myid && rdrchked != rdrchker && noc >= rdrchked && noc >= rdrchker &&
1382 smp_load_acquire(&rtrcp->rtc_chkrdr) < 0 && // Pairs with smp_store_release below.
1383 !READ_ONCE(rtp->rtort_chkp) &&
1384 !smp_load_acquire(&rtrcp_chker->rtc_assigner)) { // Pairs with smp_store_release below.
1385 rtrcp->rtc_chkloops = READ_ONCE(rtrcp_chked->rtc_myloops);
1386 WARN_ON_ONCE(rtrcp->rtc_chkrdr >= 0);
1387 rtrcp->rtc_chkrdr = rdrchked;
1388 WARN_ON_ONCE(rtrcp->rtc_ready); // This gets set after the grace period ends.
1389 if (cmpxchg_relaxed(&rtrcp_chker->rtc_assigner, NULL, rtrcp) ||
1390 cmpxchg_relaxed(&rtp->rtort_chkp, NULL, rtrcp))
1391 (void)cmpxchg_relaxed(&rtrcp_chker->rtc_assigner, rtrcp, NULL); // Back out.
1392 }
1393
1394 // If assigned some completed work, do it!
1395 rtrcp_assigner = READ_ONCE(rtrcp->rtc_assigner);
1396 if (!rtrcp_assigner || !smp_load_acquire(&rtrcp_assigner->rtc_ready))
1397 return; // No work or work not yet ready.
1398 rdrchked = rtrcp_assigner->rtc_chkrdr;
1399 if (WARN_ON_ONCE(rdrchked < 0))
1400 return;
1401 rtrcp_chked = &rcu_torture_reader_mbchk[rdrchked];
1402 loops = READ_ONCE(rtrcp_chked->rtc_myloops);
1403 atomic_inc(&n_rcu_torture_mbchk_tries);
1404 if (ULONG_CMP_LT(loops, rtrcp_assigner->rtc_chkloops))
1405 atomic_inc(&n_rcu_torture_mbchk_fail);
1406 rtrcp_assigner->rtc_chkloops = loops + ULONG_MAX / 2;
1407 rtrcp_assigner->rtc_ready = 0;
1408 smp_store_release(&rtrcp->rtc_assigner, NULL); // Someone else can assign us work.
1409 smp_store_release(&rtrcp_assigner->rtc_chkrdr, -1); // Assigner can again assign.
1410}
1411
0729fbf3 1412/*
2397d072
PM
1413 * Do one extension of an RCU read-side critical section using the
1414 * current reader state in readstate (set to zero for initial entry
1415 * to extended critical section), set the new state as specified by
1416 * newstate (set to zero for final exit from extended critical section),
1417 * and random-number-generator state in trsp. If this is neither the
1418 * beginning or end of the critical section and if there was actually a
1419 * change, do a ->read_delay().
0729fbf3 1420 */
2397d072 1421static void rcutorture_one_extend(int *readstate, int newstate,
c116dba6
PM
1422 struct torture_random_state *trsp,
1423 struct rt_read_seg *rtrsp)
2397d072 1424{
52b1fc3f 1425 unsigned long flags;
2397d072
PM
1426 int idxnew = -1;
1427 int idxold = *readstate;
1428 int statesnew = ~*readstate & newstate;
1429 int statesold = *readstate & ~newstate;
1430
1431 WARN_ON_ONCE(idxold < 0);
1432 WARN_ON_ONCE((idxold >> RCUTORTURE_RDR_SHIFT) > 1);
c116dba6 1433 rtrsp->rt_readstate = newstate;
2397d072
PM
1434
1435 /* First, put new protection in place to avoid critical-section gap. */
1436 if (statesnew & RCUTORTURE_RDR_BH)
1437 local_bh_disable();
055440c2
SW
1438 if (statesnew & RCUTORTURE_RDR_RBH)
1439 rcu_read_lock_bh();
2397d072
PM
1440 if (statesnew & RCUTORTURE_RDR_IRQ)
1441 local_irq_disable();
1442 if (statesnew & RCUTORTURE_RDR_PREEMPT)
1443 preempt_disable();
2ceebc03
PM
1444 if (statesnew & RCUTORTURE_RDR_SCHED)
1445 rcu_read_lock_sched();
2397d072
PM
1446 if (statesnew & RCUTORTURE_RDR_RCU)
1447 idxnew = cur_ops->readlock() << RCUTORTURE_RDR_SHIFT;
1448
055440c2
SW
1449 /*
1450 * Next, remove old protection, in decreasing order of strength
1451 * to avoid unlock paths that aren't safe in the stronger
1452 * context. Namely: BH can not be enabled with disabled interrupts.
1453 * Additionally PREEMPT_RT requires that BH is enabled in preemptible
1454 * context.
1455 */
2397d072
PM
1456 if (statesold & RCUTORTURE_RDR_IRQ)
1457 local_irq_enable();
2397d072
PM
1458 if (statesold & RCUTORTURE_RDR_PREEMPT)
1459 preempt_enable();
2ceebc03
PM
1460 if (statesold & RCUTORTURE_RDR_SCHED)
1461 rcu_read_unlock_sched();
055440c2
SW
1462 if (statesold & RCUTORTURE_RDR_BH)
1463 local_bh_enable();
1464 if (statesold & RCUTORTURE_RDR_RBH)
1465 rcu_read_unlock_bh();
52b1fc3f
PM
1466 if (statesold & RCUTORTURE_RDR_RCU) {
1467 bool lockit = !statesnew && !(torture_random(trsp) & 0xffff);
1468
1469 if (lockit)
1470 raw_spin_lock_irqsave(&current->pi_lock, flags);
2397d072 1471 cur_ops->readunlock(idxold >> RCUTORTURE_RDR_SHIFT);
52b1fc3f
PM
1472 if (lockit)
1473 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
1474 }
2397d072
PM
1475
1476 /* Delay if neither beginning nor end and there was a change. */
1477 if ((statesnew || statesold) && *readstate && newstate)
c116dba6 1478 cur_ops->read_delay(trsp, rtrsp);
2397d072
PM
1479
1480 /* Update the reader state. */
1481 if (idxnew == -1)
1482 idxnew = idxold & ~RCUTORTURE_RDR_MASK;
1483 WARN_ON_ONCE(idxnew < 0);
1484 WARN_ON_ONCE((idxnew >> RCUTORTURE_RDR_SHIFT) > 1);
1485 *readstate = idxnew | newstate;
1486 WARN_ON_ONCE((*readstate >> RCUTORTURE_RDR_SHIFT) < 0);
1487 WARN_ON_ONCE((*readstate >> RCUTORTURE_RDR_SHIFT) > 1);
1488}
1489
1490/* Return the biggest extendables mask given current RCU and boot parameters. */
1491static int rcutorture_extend_mask_max(void)
1492{
1493 int mask;
1494
1495 WARN_ON_ONCE(extendables & ~RCUTORTURE_MAX_EXTEND);
1496 mask = extendables & RCUTORTURE_MAX_EXTEND & cur_ops->extendables;
1497 mask = mask | RCUTORTURE_RDR_RCU;
1498 return mask;
1499}
1500
1501/* Return a random protection state mask, but with at least one bit set. */
1502static int
1503rcutorture_extend_mask(int oldmask, struct torture_random_state *trsp)
1504{
1505 int mask = rcutorture_extend_mask_max();
bf1bef50 1506 unsigned long randmask1 = torture_random(trsp) >> 8;
c116dba6 1507 unsigned long randmask2 = randmask1 >> 3;
055440c2
SW
1508 unsigned long preempts = RCUTORTURE_RDR_PREEMPT | RCUTORTURE_RDR_SCHED;
1509 unsigned long preempts_irq = preempts | RCUTORTURE_RDR_IRQ;
1510 unsigned long bhs = RCUTORTURE_RDR_BH | RCUTORTURE_RDR_RBH;
2397d072
PM
1511
1512 WARN_ON_ONCE(mask >> RCUTORTURE_RDR_SHIFT);
a3b0e1e5 1513 /* Mostly only one bit (need preemption!), sometimes lots of bits. */
c116dba6 1514 if (!(randmask1 & 0x7))
bf1bef50
PM
1515 mask = mask & randmask2;
1516 else
1517 mask = mask & (1 << (randmask2 % RCUTORTURE_RDR_NBITS));
055440c2
SW
1518
1519 /*
1520 * Can't enable bh w/irq disabled.
1521 */
1522 if (mask & RCUTORTURE_RDR_IRQ)
1523 mask |= oldmask & bhs;
1524
1525 /*
1526 * Ideally these sequences would be detected in debug builds
1527 * (regardless of RT), but until then don't stop testing
1528 * them on non-RT.
1529 */
1530 if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
1531 /* Can't modify BH in atomic context */
1532 if (oldmask & preempts_irq)
1533 mask &= ~bhs;
1534 if ((oldmask | mask) & preempts_irq)
1535 mask |= oldmask & bhs;
1536 }
1537
2397d072
PM
1538 return mask ?: RCUTORTURE_RDR_RCU;
1539}
1540
1541/*
1542 * Do a randomly selected number of extensions of an existing RCU read-side
1543 * critical section.
1544 */
c116dba6
PM
1545static struct rt_read_seg *
1546rcutorture_loop_extend(int *readstate, struct torture_random_state *trsp,
1547 struct rt_read_seg *rtrsp)
2397d072
PM
1548{
1549 int i;
c116dba6 1550 int j;
2397d072
PM
1551 int mask = rcutorture_extend_mask_max();
1552
1553 WARN_ON_ONCE(!*readstate); /* -Existing- RCU read-side critsect! */
1554 if (!((mask - 1) & mask))
c116dba6
PM
1555 return rtrsp; /* Current RCU reader not extendable. */
1556 /* Bias towards larger numbers of loops. */
1557 i = (torture_random(trsp) >> 3);
1558 i = ((i | (i >> 3)) & RCUTORTURE_RDR_MAX_LOOPS) + 1;
1559 for (j = 0; j < i; j++) {
2397d072 1560 mask = rcutorture_extend_mask(*readstate, trsp);
c116dba6 1561 rcutorture_one_extend(readstate, mask, trsp, &rtrsp[j]);
2397d072 1562 }
c116dba6 1563 return &rtrsp[j];
2397d072
PM
1564}
1565
6b06aa72
PM
1566/*
1567 * Do one read-side critical section, returning false if there was
1568 * no data to read. Can be invoked both from process context and
1569 * from a timer handler.
1570 */
00504537 1571static bool rcu_torture_one_read(struct torture_random_state *trsp, long myid)
0729fbf3 1572{
bc480a63 1573 unsigned long cookie;
c116dba6 1574 int i;
917963d0 1575 unsigned long started;
6b80da42 1576 unsigned long completed;
2397d072 1577 int newstate;
0729fbf3
PM
1578 struct rcu_torture *p;
1579 int pipe_count;
2397d072 1580 int readstate = 0;
c116dba6
PM
1581 struct rt_read_seg rtseg[RCUTORTURE_RDR_MAX_SEGS] = { { 0 } };
1582 struct rt_read_seg *rtrsp = &rtseg[0];
1583 struct rt_read_seg *rtrsp1;
52494535 1584 unsigned long long ts;
0729fbf3 1585
77522751 1586 WARN_ON_ONCE(!rcu_is_watching());
2397d072 1587 newstate = rcutorture_extend_mask(readstate, trsp);
c116dba6 1588 rcutorture_one_extend(&readstate, newstate, trsp, rtrsp++);
bc480a63
PM
1589 if (cur_ops->get_gp_state && cur_ops->poll_gp_state)
1590 cookie = cur_ops->get_gp_state();
17ef2fe9 1591 started = cur_ops->get_gp_seq();
e4aa0da3 1592 ts = rcu_trace_clock_local();
632ee200 1593 p = rcu_dereference_check(rcu_torture_current,
a5c095e0 1594 !cur_ops->readlock_held || cur_ops->readlock_held());
0729fbf3 1595 if (p == NULL) {
6b06aa72 1596 /* Wait for rcu_torture_writer to get underway */
c116dba6 1597 rcutorture_one_extend(&readstate, 0, trsp, rtrsp);
6b06aa72 1598 return false;
0729fbf3
PM
1599 }
1600 if (p->rtort_mbtest == 0)
1601 atomic_inc(&n_rcu_torture_mberror);
00504537 1602 rcu_torture_reader_do_mbchk(myid, p, trsp);
c116dba6 1603 rtrsp = rcutorture_loop_extend(&readstate, trsp, rtrsp);
0729fbf3 1604 preempt_disable();
20248910 1605 pipe_count = READ_ONCE(p->rtort_pipe_count);
0729fbf3
PM
1606 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1607 /* Should not happen, but... */
1608 pipe_count = RCU_TORTURE_PIPE_LEN;
1609 }
17ef2fe9 1610 completed = cur_ops->get_gp_seq();
52494535 1611 if (pipe_count > 1) {
6b06aa72
PM
1612 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1613 ts, started, completed);
274529ba 1614 rcu_ftrace_dump(DUMP_ALL);
52494535 1615 }
dd17c8f7 1616 __this_cpu_inc(rcu_torture_count[pipe_count]);
d7219312 1617 completed = rcutorture_seq_diff(completed, started);
0729fbf3
PM
1618 if (completed > RCU_TORTURE_PIPE_LEN) {
1619 /* Should not happen, but... */
1620 completed = RCU_TORTURE_PIPE_LEN;
1621 }
dd17c8f7 1622 __this_cpu_inc(rcu_torture_batch[completed]);
0729fbf3 1623 preempt_enable();
bc480a63
PM
1624 if (cur_ops->get_gp_state && cur_ops->poll_gp_state)
1625 WARN_ONCE(cur_ops->poll_gp_state(cookie),
7ac3fdf0 1626 "%s: Cookie check 2 failed %s(%d) %lu->%lu\n",
bc480a63
PM
1627 __func__,
1628 rcu_torture_writer_state_getname(),
1629 rcu_torture_writer_state,
1630 cookie, cur_ops->get_gp_state());
c116dba6 1631 rcutorture_one_extend(&readstate, 0, trsp, rtrsp);
2397d072 1632 WARN_ON_ONCE(readstate & RCUTORTURE_RDR_MASK);
d6855142
PM
1633 // This next splat is expected behavior if leakpointer, especially
1634 // for CONFIG_RCU_STRICT_GRACE_PERIOD=y kernels.
1635 WARN_ON_ONCE(leakpointer && READ_ONCE(p->rtort_pipe_count) > 1);
c116dba6
PM
1636
1637 /* If error or close call, record the sequence of reader protections. */
1638 if ((pipe_count > 1 || completed > 1) && !xchg(&err_segs_recorded, 1)) {
1639 i = 0;
1640 for (rtrsp1 = &rtseg[0]; rtrsp1 < rtrsp; rtrsp1++)
1641 err_segs[i++] = *rtrsp1;
1642 rt_read_nsegs = i;
1643 }
1644
6b06aa72
PM
1645 return true;
1646}
1647
3025520e
PM
1648static DEFINE_TORTURE_RANDOM_PERCPU(rcu_torture_timer_rand);
1649
0729fbf3
PM
1650/*
1651 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
1652 * incrementing the corresponding element of the pipeline array. The
1653 * counter in the element should never be greater than 1, otherwise, the
1654 * RCU implementation is broken.
1655 */
fd30b717 1656static void rcu_torture_timer(struct timer_list *unused)
0729fbf3 1657{
8da9a595 1658 atomic_long_inc(&n_rcu_torture_timers);
00504537 1659 (void)rcu_torture_one_read(this_cpu_ptr(&rcu_torture_timer_rand), -1);
f34c8585
PM
1660
1661 /* Test call_rcu() invocation from interrupt handler. */
1662 if (cur_ops->call) {
1663 struct rcu_head *rhp = kmalloc(sizeof(*rhp), GFP_NOWAIT);
1664
1665 if (rhp)
1666 cur_ops->call(rhp, rcu_torture_timer_cb);
1667 }
0729fbf3
PM
1668}
1669
a241ec65
PM
1670/*
1671 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
1672 * incrementing the corresponding element of the pipeline array. The
1673 * counter in the element should never be greater than 1, otherwise, the
1674 * RCU implementation is broken.
1675 */
1676static int
1677rcu_torture_reader(void *arg)
1678{
444da518 1679 unsigned long lastsleep = jiffies;
c04dd09b
PM
1680 long myid = (long)arg;
1681 int mynumonline = myid;
51b1130e 1682 DEFINE_TORTURE_RANDOM(rand);
0729fbf3 1683 struct timer_list t;
a241ec65 1684
5ccf60f2 1685 VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
d277d868 1686 set_user_nice(current, MAX_NICE);
0acc512c 1687 if (irqreader && cur_ops->irq_capable)
fd30b717 1688 timer_setup_on_stack(&t, rcu_torture_timer, 0);
d38e6dc6 1689 tick_dep_set_task(current, TICK_DEP_BIT_RCU);
a241ec65 1690 do {
0acc512c 1691 if (irqreader && cur_ops->irq_capable) {
0729fbf3 1692 if (!timer_pending(&t))
6155fec9 1693 mod_timer(&t, jiffies + 1);
0729fbf3 1694 }
00504537 1695 if (!rcu_torture_one_read(&rand, myid) && !torture_must_stop())
a241ec65 1696 schedule_timeout_interruptible(HZ);
d38e6dc6 1697 if (time_after(jiffies, lastsleep) && !torture_must_stop()) {
1eba0ef9 1698 torture_hrtimeout_us(500, 1000, &rand);
444da518
PM
1699 lastsleep = jiffies + 10;
1700 }
1afb95fe 1701 while (torture_num_online_cpus() < mynumonline && !torture_must_stop())
c04dd09b 1702 schedule_timeout_interruptible(HZ / 5);
628edaa5 1703 stutter_wait("rcu_torture_reader");
36970bb9 1704 } while (!torture_must_stop());
424c1b68 1705 if (irqreader && cur_ops->irq_capable) {
0729fbf3 1706 del_timer_sync(&t);
424c1b68
TG
1707 destroy_timer_on_stack(&t);
1708 }
d38e6dc6 1709 tick_dep_clear_task(current, TICK_DEP_BIT_RCU);
7fafaac5 1710 torture_kthread_stopping("rcu_torture_reader");
a241ec65
PM
1711 return 0;
1712}
1713
2c4319bd
PM
1714/*
1715 * Randomly Toggle CPUs' callback-offload state. This uses hrtimers to
1716 * increase race probabilities and fuzzes the interval between toggling.
1717 */
1718static int rcu_nocb_toggle(void *arg)
1719{
1720 int cpu;
1721 int maxcpu = -1;
1722 int oldnice = task_nice(current);
1723 long r;
1724 DEFINE_TORTURE_RANDOM(rand);
1725 ktime_t toggle_delay;
1726 unsigned long toggle_fuzz;
1727 ktime_t toggle_interval = ms_to_ktime(nocbs_toggle);
1728
1729 VERBOSE_TOROUT_STRING("rcu_nocb_toggle task started");
1730 while (!rcu_inkernel_boot_has_ended())
1731 schedule_timeout_interruptible(HZ / 10);
1732 for_each_online_cpu(cpu)
1733 maxcpu = cpu;
1734 WARN_ON(maxcpu < 0);
1735 if (toggle_interval > ULONG_MAX)
1736 toggle_fuzz = ULONG_MAX >> 3;
1737 else
1738 toggle_fuzz = toggle_interval >> 3;
1739 if (toggle_fuzz <= 0)
1740 toggle_fuzz = NSEC_PER_USEC;
1741 do {
1742 r = torture_random(&rand);
1743 cpu = (r >> 4) % (maxcpu + 1);
1744 if (r & 0x1) {
1745 rcu_nocb_cpu_offload(cpu);
1746 atomic_long_inc(&n_nocb_offload);
1747 } else {
1748 rcu_nocb_cpu_deoffload(cpu);
1749 atomic_long_inc(&n_nocb_deoffload);
1750 }
1751 toggle_delay = torture_random(&rand) % toggle_fuzz + toggle_interval;
1752 set_current_state(TASK_INTERRUPTIBLE);
1753 schedule_hrtimeout(&toggle_delay, HRTIMER_MODE_REL);
1754 if (stutter_wait("rcu_nocb_toggle"))
1755 sched_set_normal(current, oldnice);
1756 } while (!torture_must_stop());
1757 torture_kthread_stopping("rcu_nocb_toggle");
1758 return 0;
1759}
1760
a241ec65 1761/*
eea203fe
JP
1762 * Print torture statistics. Caller must ensure that there is only
1763 * one call to this function at a given time!!! This is normally
1764 * accomplished by relying on the module system to only have one copy
1765 * of the module loaded, and then by giving the rcu_torture_stats
1766 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1767 * thread is not running).
a241ec65 1768 */
d1008950 1769static void
eea203fe 1770rcu_torture_stats_print(void)
a241ec65 1771{
a241ec65
PM
1772 int cpu;
1773 int i;
1774 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1775 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
5396d31d 1776 struct rcu_torture *rtcp;
ad0dc7f9 1777 static unsigned long rtcv_snap = ULONG_MAX;
0032f4e8 1778 static bool splatted;
4ffa6699 1779 struct task_struct *wtp;
a241ec65 1780
0a945022 1781 for_each_possible_cpu(cpu) {
a241ec65 1782 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
f042a436
PM
1783 pipesummary[i] += READ_ONCE(per_cpu(rcu_torture_count, cpu)[i]);
1784 batchsummary[i] += READ_ONCE(per_cpu(rcu_torture_batch, cpu)[i]);
a241ec65
PM
1785 }
1786 }
1787 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1788 if (pipesummary[i] != 0)
1789 break;
1790 }
eea203fe
JP
1791
1792 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
5396d31d 1793 rtcp = rcu_access_pointer(rcu_torture_current);
354ea05d 1794 pr_cont("rtc: %p %s: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
5396d31d
PM
1795 rtcp,
1796 rtcp && !rcu_stall_is_suppressed_at_boot() ? "ver" : "VER",
eea203fe
JP
1797 rcu_torture_current_version,
1798 list_empty(&rcu_torture_freelist),
1799 atomic_read(&n_rcu_torture_alloc),
1800 atomic_read(&n_rcu_torture_alloc_fail),
1801 atomic_read(&n_rcu_torture_free));
00504537 1802 pr_cont("rtmbe: %d rtmbkf: %d/%d rtbe: %ld rtbke: %ld rtbre: %ld ",
eea203fe 1803 atomic_read(&n_rcu_torture_mberror),
00504537 1804 atomic_read(&n_rcu_torture_mbchk_fail), atomic_read(&n_rcu_torture_mbchk_tries),
472213a6 1805 n_rcu_torture_barrier_error,
eea203fe
JP
1806 n_rcu_torture_boost_ktrerror,
1807 n_rcu_torture_boost_rterror);
1808 pr_cont("rtbf: %ld rtb: %ld nt: %ld ",
1809 n_rcu_torture_boost_failure,
1810 n_rcu_torture_boosts,
8da9a595 1811 atomic_long_read(&n_rcu_torture_timers));
eea203fe 1812 torture_onoff_stats();
4a5f133c 1813 pr_cont("barrier: %ld/%ld:%ld ",
c9527beb
PM
1814 data_race(n_barrier_successes),
1815 data_race(n_barrier_attempts),
1816 data_race(n_rcu_torture_barrier_error));
f759081e 1817 pr_cont("read-exits: %ld ", data_race(n_read_exits)); // Statistic.
2c4319bd
PM
1818 pr_cont("nocb-toggles: %ld:%ld\n",
1819 atomic_long_read(&n_nocb_offload), atomic_long_read(&n_nocb_deoffload));
eea203fe
JP
1820
1821 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
8b5ddf8b 1822 if (atomic_read(&n_rcu_torture_mberror) ||
00504537 1823 atomic_read(&n_rcu_torture_mbchk_fail) ||
8b5ddf8b
PM
1824 n_rcu_torture_barrier_error || n_rcu_torture_boost_ktrerror ||
1825 n_rcu_torture_boost_rterror || n_rcu_torture_boost_failure ||
fae4b54f 1826 i > 1) {
eea203fe 1827 pr_cont("%s", "!!! ");
996417d2 1828 atomic_inc(&n_rcu_torture_error);
8b5ddf8b 1829 WARN_ON_ONCE(atomic_read(&n_rcu_torture_mberror));
00504537 1830 WARN_ON_ONCE(atomic_read(&n_rcu_torture_mbchk_fail));
8b5ddf8b
PM
1831 WARN_ON_ONCE(n_rcu_torture_barrier_error); // rcu_barrier()
1832 WARN_ON_ONCE(n_rcu_torture_boost_ktrerror); // no boost kthread
1833 WARN_ON_ONCE(n_rcu_torture_boost_rterror); // can't set RT prio
5e59fba5 1834 WARN_ON_ONCE(n_rcu_torture_boost_failure); // boost failed (TIMER_SOFTIRQ RT prio?)
8b5ddf8b 1835 WARN_ON_ONCE(i > 1); // Too-short grace period
996417d2 1836 }
eea203fe 1837 pr_cont("Reader Pipe: ");
a241ec65 1838 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
eea203fe
JP
1839 pr_cont(" %ld", pipesummary[i]);
1840 pr_cont("\n");
1841
1842 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1843 pr_cont("Reader Batch: ");
72e9bb54 1844 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
eea203fe
JP
1845 pr_cont(" %ld", batchsummary[i]);
1846 pr_cont("\n");
1847
1848 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1849 pr_cont("Free-Block Circulation: ");
a241ec65 1850 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
eea203fe 1851 pr_cont(" %d", atomic_read(&rcu_torture_wcount[i]));
a241ec65 1852 }
eea203fe
JP
1853 pr_cont("\n");
1854
c8e5b163 1855 if (cur_ops->stats)
eea203fe 1856 cur_ops->stats();
ad0dc7f9 1857 if (rtcv_snap == rcu_torture_current_version &&
5396d31d
PM
1858 rcu_access_pointer(rcu_torture_current) &&
1859 !rcu_stall_is_suppressed()) {
7f6733c3 1860 int __maybe_unused flags = 0;
aebc8264 1861 unsigned long __maybe_unused gp_seq = 0;
ad0dc7f9
PM
1862
1863 rcutorture_get_gp_data(cur_ops->ttype,
aebc8264 1864 &flags, &gp_seq);
7f6733c3 1865 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp,
aebc8264 1866 &flags, &gp_seq);
4ffa6699 1867 wtp = READ_ONCE(writer_task);
2f064a59 1868 pr_alert("??? Writer stall state %s(%d) g%lu f%#x ->state %#x cpu %d\n",
18aff33e 1869 rcu_torture_writer_state_getname(),
aebc8264 1870 rcu_torture_writer_state, gp_seq, flags,
2f064a59 1871 wtp == NULL ? ~0U : wtp->__state,
808de39c 1872 wtp == NULL ? -1 : (int)task_cpu(wtp));
0032f4e8
PM
1873 if (!splatted && wtp) {
1874 sched_show_task(wtp);
1875 splatted = true;
1876 }
27c0f144
PM
1877 if (cur_ops->gp_kthread_dbg)
1878 cur_ops->gp_kthread_dbg();
274529ba 1879 rcu_ftrace_dump(DUMP_ALL);
ad0dc7f9
PM
1880 }
1881 rtcv_snap = rcu_torture_current_version;
a241ec65
PM
1882}
1883
a241ec65
PM
1884/*
1885 * Periodically prints torture statistics, if periodic statistics printing
1886 * was specified via the stat_interval module parameter.
a241ec65
PM
1887 */
1888static int
1889rcu_torture_stats(void *arg)
1890{
5ccf60f2 1891 VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
a241ec65
PM
1892 do {
1893 schedule_timeout_interruptible(stat_interval * HZ);
1894 rcu_torture_stats_print();
f67a3356 1895 torture_shutdown_absorb("rcu_torture_stats");
36970bb9 1896 } while (!torture_must_stop());
7fafaac5 1897 torture_kthread_stopping("rcu_torture_stats");
d120f65f
PM
1898 return 0;
1899}
1900
7ab2bd31
PM
1901/* Test mem_dump_obj() and friends. */
1902static void rcu_torture_mem_dump_obj(void)
1903{
1904 struct rcu_head *rhp;
1905 struct kmem_cache *kcp;
1906 static int z;
1907
1908 kcp = kmem_cache_create("rcuscale", 136, 8, SLAB_STORE_USER, NULL);
1909 rhp = kmem_cache_alloc(kcp, GFP_KERNEL);
1910 pr_alert("mem_dump_obj() slab test: rcu_torture_stats = %px, &rhp = %px, rhp = %px, &z = %px\n", stats_task, &rhp, rhp, &z);
1911 pr_alert("mem_dump_obj(ZERO_SIZE_PTR):");
1912 mem_dump_obj(ZERO_SIZE_PTR);
1913 pr_alert("mem_dump_obj(NULL):");
1914 mem_dump_obj(NULL);
1915 pr_alert("mem_dump_obj(%px):", &rhp);
1916 mem_dump_obj(&rhp);
1917 pr_alert("mem_dump_obj(%px):", rhp);
1918 mem_dump_obj(rhp);
1919 pr_alert("mem_dump_obj(%px):", &rhp->func);
1920 mem_dump_obj(&rhp->func);
1921 pr_alert("mem_dump_obj(%px):", &z);
1922 mem_dump_obj(&z);
1923 kmem_cache_free(kcp, rhp);
1924 kmem_cache_destroy(kcp);
1925 rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
1926 pr_alert("mem_dump_obj() kmalloc test: rcu_torture_stats = %px, &rhp = %px, rhp = %px\n", stats_task, &rhp, rhp);
1927 pr_alert("mem_dump_obj(kmalloc %px):", rhp);
1928 mem_dump_obj(rhp);
1929 pr_alert("mem_dump_obj(kmalloc %px):", &rhp->func);
1930 mem_dump_obj(&rhp->func);
1931 kfree(rhp);
1932 rhp = vmalloc(4096);
1933 pr_alert("mem_dump_obj() vmalloc test: rcu_torture_stats = %px, &rhp = %px, rhp = %px\n", stats_task, &rhp, rhp);
1934 pr_alert("mem_dump_obj(vmalloc %px):", rhp);
1935 mem_dump_obj(rhp);
1936 pr_alert("mem_dump_obj(vmalloc %px):", &rhp->func);
1937 mem_dump_obj(&rhp->func);
1938 vfree(rhp);
1939}
1940
eac45e58 1941static void
e66c33d5 1942rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
95c38322 1943{
2caa1e44
PM
1944 pr_alert("%s" TORTURE_FLAG
1945 "--- %s: nreaders=%d nfakewriters=%d "
1946 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1947 "shuffle_interval=%d stutter=%d irqreader=%d "
1948 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1949 "test_boost=%d/%d test_boost_interval=%d "
1950 "test_boost_duration=%d shutdown_secs=%d "
2b1516e5 1951 "stall_cpu=%d stall_cpu_holdoff=%d stall_cpu_irqsoff=%d "
19a8ff95 1952 "stall_cpu_block=%d "
67afeed2 1953 "n_barrier_cbs=%d "
4a5f133c 1954 "onoff_interval=%d onoff_holdoff=%d "
2c4319bd
PM
1955 "read_exit_delay=%d read_exit_burst=%d "
1956 "nocbs_nthreads=%d nocbs_toggle=%d\n",
2caa1e44
PM
1957 torture_type, tag, nrealreaders, nfakewriters,
1958 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1959 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1960 test_boost, cur_ops->can_boost,
1961 test_boost_interval, test_boost_duration, shutdown_secs,
2b1516e5 1962 stall_cpu, stall_cpu_holdoff, stall_cpu_irqsoff,
19a8ff95 1963 stall_cpu_block,
67afeed2 1964 n_barrier_cbs,
4a5f133c 1965 onoff_interval, onoff_holdoff,
2c4319bd
PM
1966 read_exit_delay, read_exit_burst,
1967 nocbs_nthreads, nocbs_toggle);
95c38322
PM
1968}
1969
0ffd374b 1970static int rcutorture_booster_cleanup(unsigned int cpu)
8e8be45e
PM
1971{
1972 struct task_struct *t;
1973
1974 if (boost_tasks[cpu] == NULL)
0ffd374b 1975 return 0;
8e8be45e 1976 mutex_lock(&boost_mutex);
8e8be45e
PM
1977 t = boost_tasks[cpu];
1978 boost_tasks[cpu] = NULL;
450efca7 1979 rcu_torture_enable_rt_throttle();
8e8be45e
PM
1980 mutex_unlock(&boost_mutex);
1981
1982 /* This must be outside of the mutex, otherwise deadlock! */
9c029b86 1983 torture_stop_kthread(rcu_torture_boost, t);
0ffd374b 1984 return 0;
8e8be45e
PM
1985}
1986
0ffd374b 1987static int rcutorture_booster_init(unsigned int cpu)
8e8be45e
PM
1988{
1989 int retval;
1990
1991 if (boost_tasks[cpu] != NULL)
1992 return 0; /* Already created, nothing more to do. */
1993
1994 /* Don't allow time recalculation while creating a new task. */
1995 mutex_lock(&boost_mutex);
450efca7 1996 rcu_torture_disable_rt_throttle();
5ccf60f2 1997 VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
1f288094
ED
1998 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1999 cpu_to_node(cpu),
2000 "rcu_torture_boost");
8e8be45e
PM
2001 if (IS_ERR(boost_tasks[cpu])) {
2002 retval = PTR_ERR(boost_tasks[cpu]);
5ccf60f2 2003 VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
8e8be45e
PM
2004 n_rcu_torture_boost_ktrerror++;
2005 boost_tasks[cpu] = NULL;
2006 mutex_unlock(&boost_mutex);
2007 return retval;
2008 }
2009 kthread_bind(boost_tasks[cpu], cpu);
2010 wake_up_process(boost_tasks[cpu]);
2011 mutex_unlock(&boost_mutex);
2012 return 0;
2013}
2014
c13f3757
PM
2015/*
2016 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
2017 * induces a CPU stall for the time specified by stall_cpu.
2018 */
49fb4c62 2019static int rcu_torture_stall(void *args)
c13f3757 2020{
19a8ff95 2021 int idx;
c13f3757
PM
2022 unsigned long stop_at;
2023
5ccf60f2 2024 VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
c13f3757 2025 if (stall_cpu_holdoff > 0) {
5ccf60f2 2026 VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
c13f3757 2027 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
5ccf60f2 2028 VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
c13f3757 2029 }
55b2dcf5
PM
2030 if (!kthread_should_stop() && stall_gp_kthread > 0) {
2031 VERBOSE_TOROUT_STRING("rcu_torture_stall begin GP stall");
2032 rcu_gp_set_torture_wait(stall_gp_kthread * HZ);
2033 for (idx = 0; idx < stall_gp_kthread + 2; idx++) {
2034 if (kthread_should_stop())
2035 break;
2036 schedule_timeout_uninterruptible(HZ);
2037 }
2038 }
2039 if (!kthread_should_stop() && stall_cpu > 0) {
2040 VERBOSE_TOROUT_STRING("rcu_torture_stall begin CPU stall");
622be33f 2041 stop_at = ktime_get_seconds() + stall_cpu;
c13f3757 2042 /* RCU CPU stall is expected behavior in following code. */
19a8ff95 2043 idx = cur_ops->readlock();
2b1516e5
PM
2044 if (stall_cpu_irqsoff)
2045 local_irq_disable();
19a8ff95 2046 else if (!stall_cpu_block)
2b1516e5 2047 preempt_disable();
0a27fff3
SZ
2048 pr_alert("%s start on CPU %d.\n",
2049 __func__, raw_smp_processor_id());
622be33f
AB
2050 while (ULONG_CMP_LT((unsigned long)ktime_get_seconds(),
2051 stop_at))
59e83666
PM
2052 if (stall_cpu_block) {
2053#ifdef CONFIG_PREEMPTION
2054 preempt_schedule();
2055#else
19a8ff95 2056 schedule_timeout_uninterruptible(HZ);
59e83666 2057#endif
5d73118a
WLC
2058 } else if (stall_no_softlockup) {
2059 touch_softlockup_watchdog();
59e83666 2060 }
2b1516e5
PM
2061 if (stall_cpu_irqsoff)
2062 local_irq_enable();
19a8ff95 2063 else if (!stall_cpu_block)
2b1516e5 2064 preempt_enable();
19a8ff95 2065 cur_ops->readunlock(idx);
c13f3757 2066 }
0a27fff3 2067 pr_alert("%s end.\n", __func__);
f67a3356 2068 torture_shutdown_absorb("rcu_torture_stall");
c13f3757
PM
2069 while (!kthread_should_stop())
2070 schedule_timeout_interruptible(10 * HZ);
2071 return 0;
2072}
2073
2074/* Spawn CPU-stall kthread, if stall_cpu specified. */
2075static int __init rcu_torture_stall_init(void)
2076{
55b2dcf5 2077 if (stall_cpu <= 0 && stall_gp_kthread <= 0)
c13f3757 2078 return 0;
47cf29b9 2079 return torture_create_kthread(rcu_torture_stall, NULL, stall_task);
c13f3757
PM
2080}
2081
9fdcb9af
PM
2082/* State structure for forward-progress self-propagating RCU callback. */
2083struct fwd_cb_state {
2084 struct rcu_head rh;
2085 int stop;
2086};
2087
2088/*
2089 * Forward-progress self-propagating RCU callback function. Because
2090 * callbacks run from softirq, this function is an implicit RCU read-side
2091 * critical section.
2092 */
2093static void rcu_torture_fwd_prog_cb(struct rcu_head *rhp)
2094{
2095 struct fwd_cb_state *fcsp = container_of(rhp, struct fwd_cb_state, rh);
2096
2097 if (READ_ONCE(fcsp->stop)) {
2098 WRITE_ONCE(fcsp->stop, 2);
2099 return;
2100 }
2101 cur_ops->call(&fcsp->rh, rcu_torture_fwd_prog_cb);
2102}
2103
48718485
PM
2104/* State for continuous-flood RCU callbacks. */
2105struct rcu_fwd_cb {
2106 struct rcu_head rh;
2107 struct rcu_fwd_cb *rfc_next;
6b1b8325 2108 struct rcu_fwd *rfc_rfp;
48718485
PM
2109 int rfc_gps;
2110};
a289e608 2111
48718485
PM
2112#define MAX_FWD_CB_JIFFIES (8 * HZ) /* Maximum CB test duration. */
2113#define MIN_FWD_CB_LAUNDERS 3 /* This many CB invocations to count. */
2114#define MIN_FWD_CBS_LAUNDERED 100 /* Number of counted CBs. */
2e57bf97 2115#define FWD_CBS_HIST_DIV 10 /* Histogram buckets/second. */
a289e608
PM
2116#define N_LAUNDERS_HIST (2 * MAX_FWD_CB_JIFFIES / (HZ / FWD_CBS_HIST_DIV))
2117
cd618d10
PM
2118struct rcu_launder_hist {
2119 long n_launders;
2120 unsigned long launder_gp_seq;
2121};
48718485 2122
a289e608
PM
2123struct rcu_fwd {
2124 spinlock_t rcu_fwd_lock;
2125 struct rcu_fwd_cb *rcu_fwd_cb_head;
2126 struct rcu_fwd_cb **rcu_fwd_cb_tail;
2127 long n_launders_cb;
2128 unsigned long rcu_fwd_startat;
2129 struct rcu_launder_hist n_launders_hist[N_LAUNDERS_HIST];
2130 unsigned long rcu_launder_gp_seq_start;
2131};
2132
57f60202 2133static DEFINE_MUTEX(rcu_fwd_mutex);
afbc1574
JY
2134static struct rcu_fwd *rcu_fwds;
2135static bool rcu_fwd_emergency_stop;
48718485 2136
6b1b8325 2137static void rcu_torture_fwd_cb_hist(struct rcu_fwd *rfp)
1a682754 2138{
cd618d10
PM
2139 unsigned long gps;
2140 unsigned long gps_old;
1a682754
PM
2141 int i;
2142 int j;
2143
6b1b8325
PM
2144 for (i = ARRAY_SIZE(rfp->n_launders_hist) - 1; i > 0; i--)
2145 if (rfp->n_launders_hist[i].n_launders > 0)
1a682754 2146 break;
73d665b1 2147 pr_alert("%s: Callback-invocation histogram (duration %lu jiffies):",
6b1b8325
PM
2148 __func__, jiffies - rfp->rcu_fwd_startat);
2149 gps_old = rfp->rcu_launder_gp_seq_start;
cd618d10 2150 for (j = 0; j <= i; j++) {
6b1b8325 2151 gps = rfp->n_launders_hist[j].launder_gp_seq;
cd618d10 2152 pr_cont(" %ds/%d: %ld:%ld",
a289e608 2153 j + 1, FWD_CBS_HIST_DIV,
6b1b8325 2154 rfp->n_launders_hist[j].n_launders,
cd618d10
PM
2155 rcutorture_seq_diff(gps, gps_old));
2156 gps_old = gps;
2157 }
1a682754
PM
2158 pr_cont("\n");
2159}
2160
48718485
PM
2161/* Callback function for continuous-flood RCU callbacks. */
2162static void rcu_torture_fwd_cb_cr(struct rcu_head *rhp)
2163{
2667ccce 2164 unsigned long flags;
48718485
PM
2165 int i;
2166 struct rcu_fwd_cb *rfcp = container_of(rhp, struct rcu_fwd_cb, rh);
2167 struct rcu_fwd_cb **rfcpp;
6b1b8325 2168 struct rcu_fwd *rfp = rfcp->rfc_rfp;
48718485
PM
2169
2170 rfcp->rfc_next = NULL;
2171 rfcp->rfc_gps++;
6b1b8325
PM
2172 spin_lock_irqsave(&rfp->rcu_fwd_lock, flags);
2173 rfcpp = rfp->rcu_fwd_cb_tail;
2174 rfp->rcu_fwd_cb_tail = &rfcp->rfc_next;
48718485 2175 WRITE_ONCE(*rfcpp, rfcp);
6b1b8325
PM
2176 WRITE_ONCE(rfp->n_launders_cb, rfp->n_launders_cb + 1);
2177 i = ((jiffies - rfp->rcu_fwd_startat) / (HZ / FWD_CBS_HIST_DIV));
2178 if (i >= ARRAY_SIZE(rfp->n_launders_hist))
2179 i = ARRAY_SIZE(rfp->n_launders_hist) - 1;
2180 rfp->n_launders_hist[i].n_launders++;
2181 rfp->n_launders_hist[i].launder_gp_seq = cur_ops->get_gp_seq();
2182 spin_unlock_irqrestore(&rfp->rcu_fwd_lock, flags);
2667ccce
PM
2183}
2184
ab21f608 2185// Give the scheduler a chance, even on nohz_full CPUs.
bd1bfc51 2186static void rcu_torture_fwd_prog_cond_resched(unsigned long iter)
ab21f608 2187{
90326f05 2188 if (IS_ENABLED(CONFIG_PREEMPTION) && IS_ENABLED(CONFIG_NO_HZ_FULL)) {
bd1bfc51
PM
2189 // Real call_rcu() floods hit userspace, so emulate that.
2190 if (need_resched() || (iter & 0xfff))
ab21f608 2191 schedule();
d38e6dc6 2192 return;
ab21f608 2193 }
d38e6dc6
PM
2194 // No userspace emulation: CB invocation throttles call_rcu()
2195 cond_resched();
ab21f608
PM
2196}
2197
2667ccce
PM
2198/*
2199 * Free all callbacks on the rcu_fwd_cb_head list, either because the
2200 * test is over or because we hit an OOM event.
2201 */
6764100b 2202static unsigned long rcu_torture_fwd_prog_cbfree(struct rcu_fwd *rfp)
2667ccce
PM
2203{
2204 unsigned long flags;
2205 unsigned long freed = 0;
2206 struct rcu_fwd_cb *rfcp;
2207
2208 for (;;) {
6764100b
PM
2209 spin_lock_irqsave(&rfp->rcu_fwd_lock, flags);
2210 rfcp = rfp->rcu_fwd_cb_head;
140e53f2 2211 if (!rfcp) {
6764100b 2212 spin_unlock_irqrestore(&rfp->rcu_fwd_lock, flags);
2667ccce 2213 break;
140e53f2 2214 }
6764100b
PM
2215 rfp->rcu_fwd_cb_head = rfcp->rfc_next;
2216 if (!rfp->rcu_fwd_cb_head)
2217 rfp->rcu_fwd_cb_tail = &rfp->rcu_fwd_cb_head;
2218 spin_unlock_irqrestore(&rfp->rcu_fwd_lock, flags);
2667ccce
PM
2219 kfree(rfcp);
2220 freed++;
bd1bfc51 2221 rcu_torture_fwd_prog_cond_resched(freed);
79ba7ff5
PM
2222 if (tick_nohz_full_enabled()) {
2223 local_irq_save(flags);
2224 rcu_momentary_dyntick_idle();
2225 local_irq_restore(flags);
2226 }
2667ccce 2227 }
2667ccce 2228 return freed;
48718485
PM
2229}
2230
6b3de7a1 2231/* Carry out need_resched()/cond_resched() forward-progress testing. */
6b1b8325
PM
2232static void rcu_torture_fwd_prog_nr(struct rcu_fwd *rfp,
2233 int *tested, int *tested_tries)
1b27291b 2234{
119248be 2235 unsigned long cver;
f4de46ed 2236 unsigned long dur;
7c590fcc 2237 struct fwd_cb_state fcs;
119248be 2238 unsigned long gps;
1b27291b 2239 int idx;
6b3de7a1
PM
2240 int sd;
2241 int sd4;
2242 bool selfpropcb = false;
2243 unsigned long stopat;
2244 static DEFINE_TORTURE_RANDOM(trs);
2245
a7eb937b
PM
2246 if (!cur_ops->sync)
2247 return; // Cannot do need_resched() forward progress testing without ->sync.
2248 if (cur_ops->call && cur_ops->cb_barrier) {
6b3de7a1
PM
2249 init_rcu_head_on_stack(&fcs.rh);
2250 selfpropcb = true;
2251 }
2252
2253 /* Tight loop containing cond_resched(). */
e8516c64
PM
2254 WRITE_ONCE(rcu_fwd_cb_nodelay, true);
2255 cur_ops->sync(); /* Later readers see above write. */
6b3de7a1
PM
2256 if (selfpropcb) {
2257 WRITE_ONCE(fcs.stop, 0);
2258 cur_ops->call(&fcs.rh, rcu_torture_fwd_prog_cb);
2259 }
2260 cver = READ_ONCE(rcu_torture_current_version);
2261 gps = cur_ops->get_gp_seq();
2262 sd = cur_ops->stall_dur() + 1;
2263 sd4 = (sd + fwd_progress_div - 1) / fwd_progress_div;
2264 dur = sd4 + torture_random(&trs) % (sd - sd4);
6b1b8325
PM
2265 WRITE_ONCE(rfp->rcu_fwd_startat, jiffies);
2266 stopat = rfp->rcu_fwd_startat + dur;
e0aff973 2267 while (time_before(jiffies, stopat) &&
60013d5d 2268 !shutdown_time_arrived() &&
e0aff973 2269 !READ_ONCE(rcu_fwd_emergency_stop) && !torture_must_stop()) {
6b3de7a1
PM
2270 idx = cur_ops->readlock();
2271 udelay(10);
2272 cur_ops->readunlock(idx);
2273 if (!fwd_progress_need_resched || need_resched())
fbbd5e35 2274 cond_resched();
6b3de7a1
PM
2275 }
2276 (*tested_tries)++;
e0aff973 2277 if (!time_before(jiffies, stopat) &&
60013d5d 2278 !shutdown_time_arrived() &&
e0aff973 2279 !READ_ONCE(rcu_fwd_emergency_stop) && !torture_must_stop()) {
6b3de7a1
PM
2280 (*tested)++;
2281 cver = READ_ONCE(rcu_torture_current_version) - cver;
2282 gps = rcutorture_seq_diff(cur_ops->get_gp_seq(), gps);
2283 WARN_ON(!cver && gps < 2);
2284 pr_alert("%s: Duration %ld cver %ld gps %ld\n", __func__, dur, cver, gps);
2285 }
2286 if (selfpropcb) {
2287 WRITE_ONCE(fcs.stop, 1);
2288 cur_ops->sync(); /* Wait for running CB to complete. */
2289 cur_ops->cb_barrier(); /* Wait for queued callbacks. */
2290 }
2291
2292 if (selfpropcb) {
2293 WARN_ON(READ_ONCE(fcs.stop) != 2);
2294 destroy_rcu_head_on_stack(&fcs.rh);
2295 }
e8516c64
PM
2296 schedule_timeout_uninterruptible(HZ / 10); /* Let kthreads recover. */
2297 WRITE_ONCE(rcu_fwd_cb_nodelay, false);
6b3de7a1
PM
2298}
2299
2300/* Carry out call_rcu() forward-progress testing. */
6b1b8325 2301static void rcu_torture_fwd_prog_cr(struct rcu_fwd *rfp)
6b3de7a1
PM
2302{
2303 unsigned long cver;
79ba7ff5 2304 unsigned long flags;
6b3de7a1
PM
2305 unsigned long gps;
2306 int i;
48718485
PM
2307 long n_launders;
2308 long n_launders_cb_snap;
2309 long n_launders_sa;
2310 long n_max_cbs;
2311 long n_max_gps;
2312 struct rcu_fwd_cb *rfcp;
2313 struct rcu_fwd_cb *rfcpn;
1b27291b 2314 unsigned long stopat;
48718485 2315 unsigned long stoppedat;
6b3de7a1 2316
2667ccce
PM
2317 if (READ_ONCE(rcu_fwd_emergency_stop))
2318 return; /* Get out of the way quickly, no GP wait! */
c682db55
PM
2319 if (!cur_ops->call)
2320 return; /* Can't do call_rcu() fwd prog without ->call. */
2667ccce 2321
6b3de7a1
PM
2322 /* Loop continuously posting RCU callbacks. */
2323 WRITE_ONCE(rcu_fwd_cb_nodelay, true);
2324 cur_ops->sync(); /* Later readers see above write. */
6b1b8325
PM
2325 WRITE_ONCE(rfp->rcu_fwd_startat, jiffies);
2326 stopat = rfp->rcu_fwd_startat + MAX_FWD_CB_JIFFIES;
6b3de7a1 2327 n_launders = 0;
6b1b8325 2328 rfp->n_launders_cb = 0; // Hoist initialization for multi-kthread
6b3de7a1
PM
2329 n_launders_sa = 0;
2330 n_max_cbs = 0;
2331 n_max_gps = 0;
6b1b8325
PM
2332 for (i = 0; i < ARRAY_SIZE(rfp->n_launders_hist); i++)
2333 rfp->n_launders_hist[i].n_launders = 0;
6b3de7a1
PM
2334 cver = READ_ONCE(rcu_torture_current_version);
2335 gps = cur_ops->get_gp_seq();
6b1b8325 2336 rfp->rcu_launder_gp_seq_start = gps;
d38e6dc6 2337 tick_dep_set_task(current, TICK_DEP_BIT_RCU);
e0aff973 2338 while (time_before(jiffies, stopat) &&
60013d5d 2339 !shutdown_time_arrived() &&
e0aff973 2340 !READ_ONCE(rcu_fwd_emergency_stop) && !torture_must_stop()) {
6b1b8325 2341 rfcp = READ_ONCE(rfp->rcu_fwd_cb_head);
6b3de7a1
PM
2342 rfcpn = NULL;
2343 if (rfcp)
2344 rfcpn = READ_ONCE(rfcp->rfc_next);
2345 if (rfcpn) {
2346 if (rfcp->rfc_gps >= MIN_FWD_CB_LAUNDERS &&
2347 ++n_max_gps >= MIN_FWD_CBS_LAUNDERED)
2348 break;
6b1b8325 2349 rfp->rcu_fwd_cb_head = rfcpn;
6b3de7a1
PM
2350 n_launders++;
2351 n_launders_sa++;
2352 } else {
2353 rfcp = kmalloc(sizeof(*rfcp), GFP_KERNEL);
2354 if (WARN_ON_ONCE(!rfcp)) {
2355 schedule_timeout_interruptible(1);
2356 continue;
2357 }
2358 n_max_cbs++;
2359 n_launders_sa = 0;
2360 rfcp->rfc_gps = 0;
6b1b8325 2361 rfcp->rfc_rfp = rfp;
6b3de7a1
PM
2362 }
2363 cur_ops->call(&rfcp->rh, rcu_torture_fwd_cb_cr);
bd1bfc51 2364 rcu_torture_fwd_prog_cond_resched(n_launders + n_max_cbs);
79ba7ff5
PM
2365 if (tick_nohz_full_enabled()) {
2366 local_irq_save(flags);
2367 rcu_momentary_dyntick_idle();
2368 local_irq_restore(flags);
2369 }
6b3de7a1
PM
2370 }
2371 stoppedat = jiffies;
6b1b8325 2372 n_launders_cb_snap = READ_ONCE(rfp->n_launders_cb);
6b3de7a1
PM
2373 cver = READ_ONCE(rcu_torture_current_version) - cver;
2374 gps = rcutorture_seq_diff(cur_ops->get_gp_seq(), gps);
2375 cur_ops->cb_barrier(); /* Wait for callbacks to be invoked. */
6764100b 2376 (void)rcu_torture_fwd_prog_cbfree(rfp);
2667ccce 2377
60013d5d
PM
2378 if (!torture_must_stop() && !READ_ONCE(rcu_fwd_emergency_stop) &&
2379 !shutdown_time_arrived()) {
6b3de7a1
PM
2380 WARN_ON(n_max_gps < MIN_FWD_CBS_LAUNDERED);
2381 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",
2382 __func__,
6b1b8325 2383 stoppedat - rfp->rcu_fwd_startat, jiffies - stoppedat,
6b3de7a1
PM
2384 n_launders + n_max_cbs - n_launders_cb_snap,
2385 n_launders, n_launders_sa,
2386 n_max_gps, n_max_cbs, cver, gps);
6b1b8325 2387 rcu_torture_fwd_cb_hist(rfp);
6b3de7a1 2388 }
e8516c64 2389 schedule_timeout_uninterruptible(HZ); /* Let CBs drain. */
d38e6dc6 2390 tick_dep_clear_task(current, TICK_DEP_BIT_RCU);
e8516c64 2391 WRITE_ONCE(rcu_fwd_cb_nodelay, false);
6b3de7a1
PM
2392}
2393
e0aff973
PM
2394
2395/*
2396 * OOM notifier, but this only prints diagnostic information for the
2397 * current forward-progress test.
2398 */
2399static int rcutorture_oom_notify(struct notifier_block *self,
2400 unsigned long notused, void *nfreed)
2401{
57f60202 2402 struct rcu_fwd *rfp;
6764100b 2403
57f60202
PM
2404 mutex_lock(&rcu_fwd_mutex);
2405 rfp = rcu_fwds;
2406 if (!rfp) {
2407 mutex_unlock(&rcu_fwd_mutex);
2408 return NOTIFY_OK;
2409 }
2667ccce
PM
2410 WARN(1, "%s invoked upon OOM during forward-progress testing.\n",
2411 __func__);
6764100b
PM
2412 rcu_torture_fwd_cb_hist(rfp);
2413 rcu_fwd_progress_check(1 + (jiffies - READ_ONCE(rfp->rcu_fwd_startat)) / 2);
e0aff973 2414 WRITE_ONCE(rcu_fwd_emergency_stop, true);
2667ccce
PM
2415 smp_mb(); /* Emergency stop before free and wait to avoid hangs. */
2416 pr_info("%s: Freed %lu RCU callbacks.\n",
6764100b 2417 __func__, rcu_torture_fwd_prog_cbfree(rfp));
2667ccce
PM
2418 rcu_barrier();
2419 pr_info("%s: Freed %lu RCU callbacks.\n",
6764100b 2420 __func__, rcu_torture_fwd_prog_cbfree(rfp));
2667ccce
PM
2421 rcu_barrier();
2422 pr_info("%s: Freed %lu RCU callbacks.\n",
6764100b 2423 __func__, rcu_torture_fwd_prog_cbfree(rfp));
2667ccce
PM
2424 smp_mb(); /* Frees before return to avoid redoing OOM. */
2425 (*(unsigned long *)nfreed)++; /* Forward progress CBs freed! */
2426 pr_info("%s returning after OOM processing.\n", __func__);
57f60202 2427 mutex_unlock(&rcu_fwd_mutex);
e0aff973
PM
2428 return NOTIFY_OK;
2429}
2430
2431static struct notifier_block rcutorture_oom_nb = {
2432 .notifier_call = rcutorture_oom_notify
2433};
2434
6b3de7a1
PM
2435/* Carry out grace-period forward-progress testing. */
2436static int rcu_torture_fwd_prog(void *args)
2437{
ab1b7880 2438 int oldnice = task_nice(current);
6b1b8325 2439 struct rcu_fwd *rfp = args;
f4de46ed 2440 int tested = 0;
152f4afb 2441 int tested_tries = 0;
1b27291b
PM
2442
2443 VERBOSE_TOROUT_STRING("rcu_torture_fwd_progress task started");
5ab7ab83 2444 rcu_bind_current_to_nocb();
fecad509
PM
2445 if (!IS_ENABLED(CONFIG_SMP) || !IS_ENABLED(CONFIG_RCU_BOOST))
2446 set_user_nice(current, MAX_NICE);
1b27291b
PM
2447 do {
2448 schedule_timeout_interruptible(fwd_progress_holdoff * HZ);
e0aff973 2449 WRITE_ONCE(rcu_fwd_emergency_stop, false);
43550809
PM
2450 if (!IS_ENABLED(CONFIG_TINY_RCU) ||
2451 rcu_inkernel_boot_has_ended())
2452 rcu_torture_fwd_prog_nr(rfp, &tested, &tested_tries);
2453 if (rcu_inkernel_boot_has_ended())
2454 rcu_torture_fwd_prog_cr(rfp);
48718485 2455
1b27291b 2456 /* Avoid slow periods, better to test when busy. */
ab1b7880
PM
2457 if (stutter_wait("rcu_torture_fwd_prog"))
2458 sched_set_normal(current, oldnice);
1b27291b 2459 } while (!torture_must_stop());
152f4afb
PM
2460 /* Short runs might not contain a valid forward-progress attempt. */
2461 WARN_ON(!tested && tested_tries >= 5);
f4de46ed 2462 pr_alert("%s: tested %d tested_tries %d\n", __func__, tested, tested_tries);
1b27291b
PM
2463 torture_kthread_stopping("rcu_torture_fwd_prog");
2464 return 0;
2465}
2466
2467/* If forward-progress checking is requested and feasible, spawn the thread. */
2468static int __init rcu_torture_fwd_prog_init(void)
2469{
5155be99 2470 struct rcu_fwd *rfp;
6764100b 2471
1b27291b
PM
2472 if (!fwd_progress)
2473 return 0; /* Not requested, so don't do it. */
a7eb937b
PM
2474 if ((!cur_ops->sync && !cur_ops->call) ||
2475 !cur_ops->stall_dur || cur_ops->stall_dur() <= 0 || cur_ops == &rcu_busted_ops) {
1b27291b
PM
2476 VERBOSE_TOROUT_STRING("rcu_torture_fwd_prog_init: Disabled, unsupported by RCU flavor under test");
2477 return 0;
2478 }
2479 if (stall_cpu > 0) {
2480 VERBOSE_TOROUT_STRING("rcu_torture_fwd_prog_init: Disabled, conflicts with CPU-stall testing");
2481 if (IS_MODULE(CONFIG_RCU_TORTURE_TESTS))
2482 return -EINVAL; /* In module, can fail back to user. */
2483 WARN_ON(1); /* Make sure rcutorture notices conflict. */
2484 return 0;
2485 }
2486 if (fwd_progress_holdoff <= 0)
2487 fwd_progress_holdoff = 1;
2488 if (fwd_progress_div <= 0)
2489 fwd_progress_div = 4;
5155be99
PM
2490 rfp = kzalloc(sizeof(*rfp), GFP_KERNEL);
2491 if (!rfp)
2492 return -ENOMEM;
2493 spin_lock_init(&rfp->rcu_fwd_lock);
2494 rfp->rcu_fwd_cb_tail = &rfp->rcu_fwd_cb_head;
57f60202 2495 mutex_lock(&rcu_fwd_mutex);
c8fa6371 2496 rcu_fwds = rfp;
57f60202 2497 mutex_unlock(&rcu_fwd_mutex);
299c7d94 2498 register_oom_notifier(&rcutorture_oom_nb);
6764100b 2499 return torture_create_kthread(rcu_torture_fwd_prog, rfp, fwd_prog_task);
1b27291b
PM
2500}
2501
c8fa6371
PM
2502static void rcu_torture_fwd_prog_cleanup(void)
2503{
2504 struct rcu_fwd *rfp;
2505
2506 torture_stop_kthread(rcu_torture_fwd_prog, fwd_prog_task);
2507 rfp = rcu_fwds;
57f60202 2508 mutex_lock(&rcu_fwd_mutex);
c8fa6371 2509 rcu_fwds = NULL;
57f60202 2510 mutex_unlock(&rcu_fwd_mutex);
299c7d94 2511 unregister_oom_notifier(&rcutorture_oom_nb);
c8fa6371
PM
2512 kfree(rfp);
2513}
2514
fae4b54f 2515/* Callback function for RCU barrier testing. */
b3b8a4d4 2516static void rcu_torture_barrier_cbf(struct rcu_head *rcu)
fae4b54f
PM
2517{
2518 atomic_inc(&barrier_cbs_invoked);
2519}
2520
50d4b629
PM
2521/* IPI handler to get callback posted on desired CPU, if online. */
2522static void rcu_torture_barrier1cb(void *rcu_void)
2523{
2524 struct rcu_head *rhp = rcu_void;
2525
2526 cur_ops->call(rhp, rcu_torture_barrier_cbf);
2527}
2528
fae4b54f
PM
2529/* kthread function to register callbacks used to test RCU barriers. */
2530static int rcu_torture_barrier_cbs(void *arg)
2531{
2532 long myid = (long)arg;
8f43d591 2533 bool lastphase = false;
78e4bc34 2534 bool newphase;
fae4b54f
PM
2535 struct rcu_head rcu;
2536
2537 init_rcu_head_on_stack(&rcu);
5ccf60f2 2538 VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
d277d868 2539 set_user_nice(current, MAX_NICE);
fae4b54f
PM
2540 do {
2541 wait_event(barrier_cbs_wq[myid],
78e4bc34 2542 (newphase =
6c7ed42c 2543 smp_load_acquire(&barrier_phase)) != lastphase ||
36970bb9 2544 torture_must_stop());
78e4bc34 2545 lastphase = newphase;
36970bb9 2546 if (torture_must_stop())
fae4b54f 2547 break;
6c7ed42c
PM
2548 /*
2549 * The above smp_load_acquire() ensures barrier_phase load
aab05738 2550 * is ordered before the following ->call().
6c7ed42c 2551 */
50d4b629
PM
2552 if (smp_call_function_single(myid, rcu_torture_barrier1cb,
2553 &rcu, 1)) {
2554 // IPI failed, so use direct call from current CPU.
2555 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
2556 }
fae4b54f
PM
2557 if (atomic_dec_and_test(&barrier_cbs_count))
2558 wake_up(&barrier_wq);
36970bb9 2559 } while (!torture_must_stop());
69c60455
PM
2560 if (cur_ops->cb_barrier != NULL)
2561 cur_ops->cb_barrier();
fae4b54f 2562 destroy_rcu_head_on_stack(&rcu);
7fafaac5 2563 torture_kthread_stopping("rcu_torture_barrier_cbs");
fae4b54f
PM
2564 return 0;
2565}
2566
2567/* kthread function to drive and coordinate RCU barrier testing. */
2568static int rcu_torture_barrier(void *arg)
2569{
2570 int i;
2571
5ccf60f2 2572 VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
fae4b54f
PM
2573 do {
2574 atomic_set(&barrier_cbs_invoked, 0);
2575 atomic_set(&barrier_cbs_count, n_barrier_cbs);
6c7ed42c
PM
2576 /* Ensure barrier_phase ordered after prior assignments. */
2577 smp_store_release(&barrier_phase, !barrier_phase);
fae4b54f
PM
2578 for (i = 0; i < n_barrier_cbs; i++)
2579 wake_up(&barrier_cbs_wq[i]);
2580 wait_event(barrier_wq,
2581 atomic_read(&barrier_cbs_count) == 0 ||
36970bb9
PM
2582 torture_must_stop());
2583 if (torture_must_stop())
fae4b54f
PM
2584 break;
2585 n_barrier_attempts++;
78e4bc34 2586 cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
fae4b54f
PM
2587 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
2588 n_rcu_torture_barrier_error++;
7602de4a
PM
2589 pr_err("barrier_cbs_invoked = %d, n_barrier_cbs = %d\n",
2590 atomic_read(&barrier_cbs_invoked),
2591 n_barrier_cbs);
9470a18f
PM
2592 WARN_ON(1);
2593 // Wait manually for the remaining callbacks
2594 i = 0;
2595 do {
2596 if (WARN_ON(i++ > HZ))
2597 i = INT_MIN;
2598 schedule_timeout_interruptible(1);
2599 cur_ops->cb_barrier();
2600 } while (atomic_read(&barrier_cbs_invoked) !=
2601 n_barrier_cbs &&
2602 !torture_must_stop());
2603 smp_mb(); // Can't trust ordering if broken.
2604 if (!torture_must_stop())
2605 pr_err("Recovered: barrier_cbs_invoked = %d\n",
2606 atomic_read(&barrier_cbs_invoked));
bf5b6435
JFG
2607 } else {
2608 n_barrier_successes++;
fae4b54f 2609 }
fae4b54f 2610 schedule_timeout_interruptible(HZ / 10);
36970bb9 2611 } while (!torture_must_stop());
7fafaac5 2612 torture_kthread_stopping("rcu_torture_barrier");
fae4b54f
PM
2613 return 0;
2614}
2615
2616/* Initialize RCU barrier testing. */
2617static int rcu_torture_barrier_init(void)
2618{
2619 int i;
2620 int ret;
2621
d9eba768 2622 if (n_barrier_cbs <= 0)
fae4b54f
PM
2623 return 0;
2624 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
2caa1e44
PM
2625 pr_alert("%s" TORTURE_FLAG
2626 " Call or barrier ops missing for %s,\n",
2627 torture_type, cur_ops->name);
2628 pr_alert("%s" TORTURE_FLAG
2629 " RCU barrier testing omitted from run.\n",
2630 torture_type);
fae4b54f
PM
2631 return 0;
2632 }
2633 atomic_set(&barrier_cbs_count, 0);
2634 atomic_set(&barrier_cbs_invoked, 0);
2635 barrier_cbs_tasks =
68a675d4 2636 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_tasks[0]),
fae4b54f
PM
2637 GFP_KERNEL);
2638 barrier_cbs_wq =
68a675d4 2639 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_wq[0]), GFP_KERNEL);
de5e6437 2640 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
fae4b54f
PM
2641 return -ENOMEM;
2642 for (i = 0; i < n_barrier_cbs; i++) {
2643 init_waitqueue_head(&barrier_cbs_wq[i]);
47cf29b9
PM
2644 ret = torture_create_kthread(rcu_torture_barrier_cbs,
2645 (void *)(long)i,
2646 barrier_cbs_tasks[i]);
2647 if (ret)
fae4b54f 2648 return ret;
fae4b54f 2649 }
47cf29b9 2650 return torture_create_kthread(rcu_torture_barrier, NULL, barrier_task);
fae4b54f
PM
2651}
2652
2653/* Clean up after RCU barrier testing. */
2654static void rcu_torture_barrier_cleanup(void)
2655{
2656 int i;
2657
9c029b86 2658 torture_stop_kthread(rcu_torture_barrier, barrier_task);
fae4b54f 2659 if (barrier_cbs_tasks != NULL) {
9c029b86
PM
2660 for (i = 0; i < n_barrier_cbs; i++)
2661 torture_stop_kthread(rcu_torture_barrier_cbs,
2662 barrier_cbs_tasks[i]);
fae4b54f
PM
2663 kfree(barrier_cbs_tasks);
2664 barrier_cbs_tasks = NULL;
2665 }
2666 if (barrier_cbs_wq != NULL) {
2667 kfree(barrier_cbs_wq);
2668 barrier_cbs_wq = NULL;
2669 }
2670}
2671
4babd855
JFG
2672static bool rcu_torture_can_boost(void)
2673{
2674 static int boost_warn_once;
2675 int prio;
2676
2677 if (!(test_boost == 1 && cur_ops->can_boost) && test_boost != 2)
2678 return false;
ea6d962e 2679 if (!cur_ops->start_gp_poll || !cur_ops->poll_gp_state)
5e59fba5 2680 return false;
4babd855
JFG
2681
2682 prio = rcu_get_gp_kthreads_prio();
2683 if (!prio)
2684 return false;
2685
2686 if (prio < 2) {
ea6d962e 2687 if (boost_warn_once == 1)
4babd855
JFG
2688 return false;
2689
bf5b6435 2690 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
2691 boost_warn_once = 1;
2692 return false;
2693 }
2694
2695 return true;
2696}
2697
4a5f133c
PM
2698static bool read_exit_child_stop;
2699static bool read_exit_child_stopped;
2700static wait_queue_head_t read_exit_wq;
2701
2702// Child kthread which just does an rcutorture reader and exits.
2703static int rcu_torture_read_exit_child(void *trsp_in)
2704{
2705 struct torture_random_state *trsp = trsp_in;
2706
2707 set_user_nice(current, MAX_NICE);
2708 // Minimize time between reading and exiting.
2709 while (!kthread_should_stop())
2710 schedule_timeout_uninterruptible(1);
00504537 2711 (void)rcu_torture_one_read(trsp, -1);
4a5f133c
PM
2712 return 0;
2713}
2714
2715// Parent kthread which creates and destroys read-exit child kthreads.
2716static int rcu_torture_read_exit(void *unused)
2717{
2718 int count = 0;
2719 bool errexit = false;
2720 int i;
2721 struct task_struct *tsp;
2722 DEFINE_TORTURE_RANDOM(trs);
2723
2724 // Allocate and initialize.
2725 set_user_nice(current, MAX_NICE);
2726 VERBOSE_TOROUT_STRING("rcu_torture_read_exit: Start of test");
2727
2728 // Each pass through this loop does one read-exit episode.
2729 do {
2730 if (++count > read_exit_burst) {
2731 VERBOSE_TOROUT_STRING("rcu_torture_read_exit: End of episode");
2732 rcu_barrier(); // Wait for task_struct free, avoid OOM.
2733 for (i = 0; i < read_exit_delay; i++) {
2734 schedule_timeout_uninterruptible(HZ);
2735 if (READ_ONCE(read_exit_child_stop))
2736 break;
2737 }
2738 if (!READ_ONCE(read_exit_child_stop))
2739 VERBOSE_TOROUT_STRING("rcu_torture_read_exit: Start of episode");
2740 count = 0;
2741 }
2742 if (READ_ONCE(read_exit_child_stop))
2743 break;
2744 // Spawn child.
2745 tsp = kthread_run(rcu_torture_read_exit_child,
2746 &trs, "%s",
2747 "rcu_torture_read_exit_child");
2748 if (IS_ERR(tsp)) {
2749 VERBOSE_TOROUT_ERRSTRING("out of memory");
2750 errexit = true;
2751 tsp = NULL;
2752 break;
2753 }
2754 cond_resched();
2755 kthread_stop(tsp);
2756 n_read_exits ++;
2757 stutter_wait("rcu_torture_read_exit");
2758 } while (!errexit && !READ_ONCE(read_exit_child_stop));
2759
2760 // Clean up and exit.
2761 smp_store_release(&read_exit_child_stopped, true); // After reaping.
2762 smp_mb(); // Store before wakeup.
2763 wake_up(&read_exit_wq);
2764 while (!torture_must_stop())
2765 schedule_timeout_uninterruptible(1);
2766 torture_kthread_stopping("rcu_torture_read_exit");
2767 return 0;
2768}
2769
2770static int rcu_torture_read_exit_init(void)
2771{
2772 if (read_exit_burst <= 0)
2773 return -EINVAL;
2774 init_waitqueue_head(&read_exit_wq);
2775 read_exit_child_stop = false;
2776 read_exit_child_stopped = false;
2777 return torture_create_kthread(rcu_torture_read_exit, NULL,
2778 read_exit_task);
2779}
2780
2781static void rcu_torture_read_exit_cleanup(void)
2782{
2783 if (!read_exit_task)
2784 return;
2785 WRITE_ONCE(read_exit_child_stop, true);
2786 smp_mb(); // Above write before wait.
2787 wait_event(read_exit_wq, smp_load_acquire(&read_exit_child_stopped));
2788 torture_stop_kthread(rcutorture_read_exit, read_exit_task);
2789}
2790
0ffd374b 2791static enum cpuhp_state rcutor_hp;
8e8be45e 2792
a241ec65
PM
2793static void
2794rcu_torture_cleanup(void)
2795{
c116dba6 2796 int firsttime;
034777d7 2797 int flags = 0;
aebc8264 2798 unsigned long gp_seq = 0;
a241ec65
PM
2799 int i;
2800
d36a7a0d 2801 if (torture_cleanup_begin()) {
343e9099
PM
2802 if (cur_ops->cb_barrier != NULL)
2803 cur_ops->cb_barrier();
2804 return;
2805 }
b813afae
PM
2806 if (!cur_ops) {
2807 torture_cleanup_end();
2808 return;
2809 }
d84f5203 2810
27c0f144
PM
2811 if (cur_ops->gp_kthread_dbg)
2812 cur_ops->gp_kthread_dbg();
4a5f133c 2813 rcu_torture_read_exit_cleanup();
fae4b54f 2814 rcu_torture_barrier_cleanup();
c8fa6371 2815 rcu_torture_fwd_prog_cleanup();
9c029b86 2816 torture_stop_kthread(rcu_torture_stall, stall_task);
9c029b86 2817 torture_stop_kthread(rcu_torture_writer, writer_task);
a241ec65 2818
2c4319bd
PM
2819 if (nocb_tasks) {
2820 for (i = 0; i < nrealnocbers; i++)
2821 torture_stop_kthread(rcu_nocb_toggle, nocb_tasks[i]);
2822 kfree(nocb_tasks);
2823 nocb_tasks = NULL;
2824 }
2825
c8e5b163 2826 if (reader_tasks) {
9c029b86
PM
2827 for (i = 0; i < nrealreaders; i++)
2828 torture_stop_kthread(rcu_torture_reader,
2829 reader_tasks[i]);
a241ec65 2830 kfree(reader_tasks);
293b93d6 2831 reader_tasks = NULL;
a241ec65 2832 }
00504537
PM
2833 kfree(rcu_torture_reader_mbchk);
2834 rcu_torture_reader_mbchk = NULL;
a241ec65 2835
c8e5b163 2836 if (fakewriter_tasks) {
293b93d6 2837 for (i = 0; i < nfakewriters; i++)
9c029b86
PM
2838 torture_stop_kthread(rcu_torture_fakewriter,
2839 fakewriter_tasks[i]);
b772e1dd
JT
2840 kfree(fakewriter_tasks);
2841 fakewriter_tasks = NULL;
2842 }
2843
aebc8264
PM
2844 rcutorture_get_gp_data(cur_ops->ttype, &flags, &gp_seq);
2845 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp, &flags, &gp_seq);
959954df
JFG
2846 pr_alert("%s: End-test grace-period state: g%ld f%#x total-gps=%ld\n",
2847 cur_ops->name, (long)gp_seq, flags,
2848 rcutorture_seq_diff(gp_seq, start_gp_seq));
9c029b86
PM
2849 torture_stop_kthread(rcu_torture_stats, stats_task);
2850 torture_stop_kthread(rcu_torture_fqs, fqs_task);
4babd855 2851 if (rcu_torture_can_boost())
0ffd374b 2852 cpuhp_remove_state(rcutor_hp);
bf66f18e 2853
ca1d51ed 2854 /*
62a1a945 2855 * Wait for all RCU callbacks to fire, then do torture-type-specific
ca1d51ed
PM
2856 * cleanup operations.
2857 */
2326974d
PM
2858 if (cur_ops->cb_barrier != NULL)
2859 cur_ops->cb_barrier();
ca1d51ed
PM
2860 if (cur_ops->cleanup != NULL)
2861 cur_ops->cleanup();
a241ec65 2862
7ab2bd31
PM
2863 rcu_torture_mem_dump_obj();
2864
a241ec65 2865 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
72e9bb54 2866
c116dba6
PM
2867 if (err_segs_recorded) {
2868 pr_alert("Failure/close-call rcutorture reader segments:\n");
2869 if (rt_read_nsegs == 0)
2870 pr_alert("\t: No segments recorded!!!\n");
2871 firsttime = 1;
2872 for (i = 0; i < rt_read_nsegs; i++) {
2873 pr_alert("\t%d: %#x ", i, err_segs[i].rt_readstate);
2874 if (err_segs[i].rt_delay_jiffies != 0) {
2875 pr_cont("%s%ldjiffies", firsttime ? "" : "+",
2876 err_segs[i].rt_delay_jiffies);
2877 firsttime = 0;
2878 }
2879 if (err_segs[i].rt_delay_ms != 0) {
2880 pr_cont("%s%ldms", firsttime ? "" : "+",
2881 err_segs[i].rt_delay_ms);
2882 firsttime = 0;
2883 }
2884 if (err_segs[i].rt_delay_us != 0) {
2885 pr_cont("%s%ldus", firsttime ? "" : "+",
2886 err_segs[i].rt_delay_us);
2887 firsttime = 0;
2888 }
2889 pr_cont("%s\n",
2890 err_segs[i].rt_preempted ? "preempted" : "");
2891
2892 }
2893 }
fae4b54f 2894 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
8e8be45e 2895 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
2e9e8081 2896 else if (torture_onoff_failures())
091541bb
PM
2897 rcu_torture_print_module_parms(cur_ops,
2898 "End of test: RCU_HOTPLUG");
95c38322 2899 else
8e8be45e 2900 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
d36a7a0d 2901 torture_cleanup_end();
a241ec65
PM
2902}
2903
d2818df1
PM
2904#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
2905static void rcu_torture_leak_cb(struct rcu_head *rhp)
2906{
2907}
2908
2909static void rcu_torture_err_cb(struct rcu_head *rhp)
2910{
2911 /*
2912 * This -might- happen due to race conditions, but is unlikely.
2913 * The scenario that leads to this happening is that the
2914 * first of the pair of duplicate callbacks is queued,
2915 * someone else starts a grace period that includes that
2916 * callback, then the second of the pair must wait for the
2917 * next grace period. Unlikely, but can happen. If it
2918 * does happen, the debug-objects subsystem won't have splatted.
2919 */
e0d31a34 2920 pr_alert("%s: duplicated callback was invoked.\n", KBUILD_MODNAME);
d2818df1
PM
2921}
2922#endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2923
2924/*
2925 * Verify that double-free causes debug-objects to complain, but only
2926 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
2927 * cannot be carried out.
2928 */
2929static void rcu_test_debug_objects(void)
2930{
2931#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
2932 struct rcu_head rh1;
2933 struct rcu_head rh2;
edf7b841 2934 struct rcu_head *rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
d2818df1
PM
2935
2936 init_rcu_head_on_stack(&rh1);
2937 init_rcu_head_on_stack(&rh2);
e0d31a34 2938 pr_alert("%s: WARN: Duplicate call_rcu() test starting.\n", KBUILD_MODNAME);
d2818df1
PM
2939
2940 /* Try to queue the rh2 pair of callbacks for the same grace period. */
2941 preempt_disable(); /* Prevent preemption from interrupting test. */
2942 rcu_read_lock(); /* Make it impossible to finish a grace period. */
2943 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
2944 local_irq_disable(); /* Make it harder to start a new grace period. */
2945 call_rcu(&rh2, rcu_torture_leak_cb);
2946 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
edf7b841
PM
2947 if (rhp) {
2948 call_rcu(rhp, rcu_torture_leak_cb);
2949 call_rcu(rhp, rcu_torture_err_cb); /* Another duplicate callback. */
2950 }
d2818df1
PM
2951 local_irq_enable();
2952 rcu_read_unlock();
2953 preempt_enable();
2954
2955 /* Wait for them all to get done so we can safely return. */
2956 rcu_barrier();
e0d31a34 2957 pr_alert("%s: WARN: Duplicate call_rcu() test complete.\n", KBUILD_MODNAME);
d2818df1
PM
2958 destroy_rcu_head_on_stack(&rh1);
2959 destroy_rcu_head_on_stack(&rh2);
2960#else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
e0d31a34 2961 pr_alert("%s: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n", KBUILD_MODNAME);
d2818df1
PM
2962#endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2963}
2964
3a6cb58f
PM
2965static void rcutorture_sync(void)
2966{
2967 static unsigned long n;
2968
2969 if (cur_ops->sync && !(++n & 0xfff))
2970 cur_ops->sync();
2971}
2972
6f8bc500 2973static int __init
a241ec65
PM
2974rcu_torture_init(void)
2975{
c04dd09b 2976 long i;
a241ec65
PM
2977 int cpu;
2978 int firsterr = 0;
959954df
JFG
2979 int flags = 0;
2980 unsigned long gp_seq = 0;
2ec1f2d9 2981 static struct rcu_torture_ops *torture_ops[] = {
c770c82a 2982 &rcu_ops, &rcu_busted_ops, &srcu_ops, &srcud_ops,
c1a76c0b
PM
2983 &busted_srcud_ops, &tasks_ops, &tasks_rude_ops,
2984 &tasks_tracing_ops, &trivial_ops,
2ec1f2d9 2985 };
a241ec65 2986
a2f2577d 2987 if (!torture_init_begin(torture_type, verbose))
5228084e 2988 return -EBUSY;
343e9099 2989
a241ec65 2990 /* Process args and tell the world that the torturer is on the job. */
ade5fb81 2991 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
72e9bb54 2992 cur_ops = torture_ops[i];
ade5fb81 2993 if (strcmp(torture_type, cur_ops->name) == 0)
72e9bb54 2994 break;
72e9bb54 2995 }
ade5fb81 2996 if (i == ARRAY_SIZE(torture_ops)) {
2caa1e44
PM
2997 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
2998 torture_type);
2999 pr_alert("rcu-torture types:");
cf886c44 3000 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
a7538352
JP
3001 pr_cont(" %s", torture_ops[i]->name);
3002 pr_cont("\n");
889d487a 3003 firsterr = -EINVAL;
b813afae 3004 cur_ops = NULL;
889d487a 3005 goto unwind;
72e9bb54 3006 }
bf66f18e 3007 if (cur_ops->fqs == NULL && fqs_duration != 0) {
2caa1e44 3008 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
bf66f18e
PM
3009 fqs_duration = 0;
3010 }
c8e5b163 3011 if (cur_ops->init)
889d487a 3012 cur_ops->init();
72e9bb54 3013
64e4b43a 3014 if (nreaders >= 0) {
a241ec65 3015 nrealreaders = nreaders;
64e4b43a 3016 } else {
3838cc18 3017 nrealreaders = num_online_cpus() - 2 - nreaders;
64e4b43a
PM
3018 if (nrealreaders <= 0)
3019 nrealreaders = 1;
3020 }
8e8be45e 3021 rcu_torture_print_module_parms(cur_ops, "Start of test");
959954df
JFG
3022 rcutorture_get_gp_data(cur_ops->ttype, &flags, &gp_seq);
3023 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp, &flags, &gp_seq);
3024 start_gp_seq = gp_seq;
3025 pr_alert("%s: Start-test grace-period state: g%ld f%#x\n",
3026 cur_ops->name, (long)gp_seq, flags);
a241ec65
PM
3027
3028 /* Set up the freelist. */
3029
3030 INIT_LIST_HEAD(&rcu_torture_freelist);
788e770e 3031 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
996417d2 3032 rcu_tortures[i].rtort_mbtest = 0;
a241ec65
PM
3033 list_add_tail(&rcu_tortures[i].rtort_free,
3034 &rcu_torture_freelist);
3035 }
3036
3037 /* Initialize the statistics so that each run gets its own numbers. */
3038
3039 rcu_torture_current = NULL;
3040 rcu_torture_current_version = 0;
3041 atomic_set(&n_rcu_torture_alloc, 0);
3042 atomic_set(&n_rcu_torture_alloc_fail, 0);
3043 atomic_set(&n_rcu_torture_free, 0);
996417d2 3044 atomic_set(&n_rcu_torture_mberror, 0);
00504537
PM
3045 atomic_set(&n_rcu_torture_mbchk_fail, 0);
3046 atomic_set(&n_rcu_torture_mbchk_tries, 0);
996417d2 3047 atomic_set(&n_rcu_torture_error, 0);
fae4b54f 3048 n_rcu_torture_barrier_error = 0;
8e8be45e
PM
3049 n_rcu_torture_boost_ktrerror = 0;
3050 n_rcu_torture_boost_rterror = 0;
8e8be45e
PM
3051 n_rcu_torture_boost_failure = 0;
3052 n_rcu_torture_boosts = 0;
a241ec65
PM
3053 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
3054 atomic_set(&rcu_torture_wcount[i], 0);
0a945022 3055 for_each_possible_cpu(cpu) {
a241ec65
PM
3056 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
3057 per_cpu(rcu_torture_count, cpu)[i] = 0;
3058 per_cpu(rcu_torture_batch, cpu)[i] = 0;
3059 }
3060 }
c116dba6
PM
3061 err_segs_recorded = 0;
3062 rt_read_nsegs = 0;
a241ec65
PM
3063
3064 /* Start up the kthreads. */
3065
18fbf307 3066 rcu_torture_write_types();
47cf29b9
PM
3067 firsterr = torture_create_kthread(rcu_torture_writer, NULL,
3068 writer_task);
3069 if (firsterr)
a241ec65 3070 goto unwind;
4444d852 3071 if (nfakewriters > 0) {
68a675d4 3072 fakewriter_tasks = kcalloc(nfakewriters,
4444d852
PM
3073 sizeof(fakewriter_tasks[0]),
3074 GFP_KERNEL);
3075 if (fakewriter_tasks == NULL) {
3076 VERBOSE_TOROUT_ERRSTRING("out of memory");
3077 firsterr = -ENOMEM;
3078 goto unwind;
3079 }
b772e1dd
JT
3080 }
3081 for (i = 0; i < nfakewriters; i++) {
47cf29b9
PM
3082 firsterr = torture_create_kthread(rcu_torture_fakewriter,
3083 NULL, fakewriter_tasks[i]);
3084 if (firsterr)
b772e1dd 3085 goto unwind;
b772e1dd 3086 }
68a675d4 3087 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
a241ec65 3088 GFP_KERNEL);
00504537
PM
3089 rcu_torture_reader_mbchk = kcalloc(nrealreaders, sizeof(*rcu_torture_reader_mbchk),
3090 GFP_KERNEL);
3091 if (!reader_tasks || !rcu_torture_reader_mbchk) {
5ccf60f2 3092 VERBOSE_TOROUT_ERRSTRING("out of memory");
a241ec65
PM
3093 firsterr = -ENOMEM;
3094 goto unwind;
3095 }
3096 for (i = 0; i < nrealreaders; i++) {
00504537 3097 rcu_torture_reader_mbchk[i].rtc_chkrdr = -1;
c04dd09b 3098 firsterr = torture_create_kthread(rcu_torture_reader, (void *)i,
47cf29b9
PM
3099 reader_tasks[i]);
3100 if (firsterr)
a241ec65 3101 goto unwind;
a241ec65 3102 }
2c4319bd
PM
3103 nrealnocbers = nocbs_nthreads;
3104 if (WARN_ON(nrealnocbers < 0))
3105 nrealnocbers = 1;
3106 if (WARN_ON(nocbs_toggle < 0))
3107 nocbs_toggle = HZ;
3108 if (nrealnocbers > 0) {
3109 nocb_tasks = kcalloc(nrealnocbers, sizeof(nocb_tasks[0]), GFP_KERNEL);
3110 if (nocb_tasks == NULL) {
3111 VERBOSE_TOROUT_ERRSTRING("out of memory");
3112 firsterr = -ENOMEM;
3113 goto unwind;
3114 }
3115 } else {
3116 nocb_tasks = NULL;
3117 }
3118 for (i = 0; i < nrealnocbers; i++) {
3119 firsterr = torture_create_kthread(rcu_nocb_toggle, NULL, nocb_tasks[i]);
3120 if (firsterr)
3121 goto unwind;
3122 }
a241ec65 3123 if (stat_interval > 0) {
47cf29b9
PM
3124 firsterr = torture_create_kthread(rcu_torture_stats, NULL,
3125 stats_task);
3126 if (firsterr)
a241ec65 3127 goto unwind;
a241ec65 3128 }
e8e255f7 3129 if (test_no_idle_hz && shuffle_interval > 0) {
3808dc9f
PM
3130 firsterr = torture_shuffle_init(shuffle_interval * HZ);
3131 if (firsterr)
d84f5203 3132 goto unwind;
d84f5203 3133 }
d120f65f
PM
3134 if (stutter < 0)
3135 stutter = 0;
3136 if (stutter) {
ff3bf92d
PM
3137 int t;
3138
3139 t = cur_ops->stall_dur ? cur_ops->stall_dur() : stutter * HZ;
3140 firsterr = torture_stutter_init(stutter * HZ, t);
628edaa5 3141 if (firsterr)
d120f65f 3142 goto unwind;
d120f65f 3143 }
bf66f18e
PM
3144 if (fqs_duration < 0)
3145 fqs_duration = 0;
3146 if (fqs_duration) {
628edaa5 3147 /* Create the fqs thread */
d0d0606e
PM
3148 firsterr = torture_create_kthread(rcu_torture_fqs, NULL,
3149 fqs_task);
47cf29b9 3150 if (firsterr)
bf66f18e 3151 goto unwind;
bf66f18e 3152 }
8e8be45e
PM
3153 if (test_boost_interval < 1)
3154 test_boost_interval = 1;
3155 if (test_boost_duration < 2)
3156 test_boost_duration = 2;
4babd855 3157 if (rcu_torture_can_boost()) {
8e8be45e
PM
3158
3159 boost_starttime = jiffies + test_boost_interval * HZ;
0ffd374b
SAS
3160
3161 firsterr = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "RCU_TORTURE",
3162 rcutorture_booster_init,
3163 rcutorture_booster_cleanup);
3164 if (firsterr < 0)
3165 goto unwind;
3166 rcutor_hp = firsterr;
ea6d962e
PM
3167
3168 // Testing RCU priority boosting requires rcutorture do
3169 // some serious abuse. Counter this by running ksoftirqd
3170 // at higher priority.
3171 if (IS_BUILTIN(CONFIG_RCU_TORTURE_TEST)) {
3172 for_each_online_cpu(cpu) {
3173 struct sched_param sp;
3174 struct task_struct *t;
3175
3176 t = per_cpu(ksoftirqd, cpu);
3177 WARN_ON_ONCE(!t);
3178 sp.sched_priority = 2;
3179 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
3180 }
3181 }
8e8be45e 3182 }
60013d5d 3183 shutdown_jiffies = jiffies + shutdown_secs * HZ;
01025ebc
PM
3184 firsterr = torture_shutdown_init(shutdown_secs, rcu_torture_cleanup);
3185 if (firsterr)
37e377d2 3186 goto unwind;
3a6cb58f
PM
3187 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval,
3188 rcutorture_sync);
01025ebc 3189 if (firsterr)
37e377d2 3190 goto unwind;
01025ebc 3191 firsterr = rcu_torture_stall_init();
1b27291b
PM
3192 if (firsterr)
3193 goto unwind;
3194 firsterr = rcu_torture_fwd_prog_init();
01025ebc 3195 if (firsterr)
37e377d2 3196 goto unwind;
01025ebc 3197 firsterr = rcu_torture_barrier_init();
4a5f133c
PM
3198 if (firsterr)
3199 goto unwind;
3200 firsterr = rcu_torture_read_exit_init();
01025ebc 3201 if (firsterr)
fae4b54f 3202 goto unwind;
d2818df1
PM
3203 if (object_debug)
3204 rcu_test_debug_objects();
b5daa8f3 3205 torture_init_end();
a241ec65
PM
3206 return 0;
3207
3208unwind:
b5daa8f3 3209 torture_init_end();
a241ec65 3210 rcu_torture_cleanup();
4994684c
PM
3211 if (shutdown_secs) {
3212 WARN_ON(!IS_MODULE(CONFIG_RCU_TORTURE_TEST));
3213 kernel_power_off();
3214 }
a241ec65
PM
3215 return firsterr;
3216}
3217
3218module_init(rcu_torture_init);
3219module_exit(rcu_torture_cleanup);