]> Git Repo - linux.git/blame - kernel/rcu/rcutorture.c
Linux 6.14-rc3
[linux.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 <[email protected]>
e0198b29 8 * Josh Triplett <[email protected]>
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>
7c1b3e0c 24#include <linux/rcu_notifier.h>
a241ec65 25#include <linux/interrupt.h>
174cd4b1 26#include <linux/sched/signal.h>
ae7e81c0 27#include <uapi/linux/sched/types.h>
60063497 28#include <linux/atomic.h>
a241ec65 29#include <linux/bitops.h>
a241ec65
PM
30#include <linux/completion.h>
31#include <linux/moduleparam.h>
32#include <linux/percpu.h>
33#include <linux/notifier.h>
343e9099 34#include <linux/reboot.h>
83144186 35#include <linux/freezer.h>
a241ec65 36#include <linux/cpu.h>
a241ec65 37#include <linux/delay.h>
a241ec65 38#include <linux/stat.h>
b2896d2e 39#include <linux/srcu.h>
1aeb272c 40#include <linux/slab.h>
52494535 41#include <linux/trace_clock.h>
f07767fd 42#include <asm/byteorder.h>
51b1130e 43#include <linux/torture.h>
38706bc5 44#include <linux/vmalloc.h>
0032f4e8 45#include <linux/sched/debug.h>
450efca7 46#include <linux/sched/sysctl.h>
e0aff973 47#include <linux/oom.h>
d38e6dc6 48#include <linux/tick.h>
c1a76c0b 49#include <linux/rcupdate_trace.h>
5ff7c9f9 50#include <linux/nmi.h>
a241ec65 51
25c36329
PM
52#include "rcu.h"
53
b9f147cd 54MODULE_DESCRIPTION("Read-Copy Update module-based torture test facility");
a241ec65 55MODULE_LICENSE("GPL");
2e24ce88 56MODULE_AUTHOR("Paul E. McKenney <[email protected]> and Josh Triplett <[email protected]>");
a241ec65 57
2397d072 58/* Bits for ->extendables field, extendables param, and related definitions. */
1c3d5398 59#define RCUTORTURE_RDR_SHIFT_1 8 /* Put SRCU index in upper bits. */
37a1decb
PM
60#define RCUTORTURE_RDR_MASK_1 (0xff << RCUTORTURE_RDR_SHIFT_1)
61#define RCUTORTURE_RDR_SHIFT_2 16 /* Put SRCU index in upper bits. */
62#define RCUTORTURE_RDR_MASK_2 (0xff << RCUTORTURE_RDR_SHIFT_2)
2ceebc03
PM
63#define RCUTORTURE_RDR_BH 0x01 /* Extend readers by disabling bh. */
64#define RCUTORTURE_RDR_IRQ 0x02 /* ... disabling interrupts. */
65#define RCUTORTURE_RDR_PREEMPT 0x04 /* ... disabling preemption. */
66#define RCUTORTURE_RDR_RBH 0x08 /* ... rcu_read_lock_bh(). */
67#define RCUTORTURE_RDR_SCHED 0x10 /* ... rcu_read_lock_sched(). */
1c3d5398
PM
68#define RCUTORTURE_RDR_RCU_1 0x20 /* ... entering another RCU reader. */
69#define RCUTORTURE_RDR_RCU_2 0x40 /* ... entering another RCU reader. */
70#define RCUTORTURE_RDR_NBITS 7 /* Number of bits defined above. */
2ceebc03
PM
71#define RCUTORTURE_MAX_EXTEND \
72 (RCUTORTURE_RDR_BH | RCUTORTURE_RDR_IRQ | RCUTORTURE_RDR_PREEMPT | \
73 RCUTORTURE_RDR_RBH | RCUTORTURE_RDR_SCHED)
37a1decb
PM
74#define RCUTORTURE_RDR_ALLBITS \
75 (RCUTORTURE_MAX_EXTEND | RCUTORTURE_RDR_RCU_1 | RCUTORTURE_RDR_RCU_2 | \
76 RCUTORTURE_RDR_MASK_1 | RCUTORTURE_RDR_MASK_2)
2397d072
PM
77#define RCUTORTURE_RDR_MAX_LOOPS 0x7 /* Maximum reader extensions. */
78 /* Must be power of two minus one. */
c116dba6 79#define RCUTORTURE_RDR_MAX_SEGS (RCUTORTURE_RDR_MAX_LOOPS + 3)
2397d072 80
2397d072
PM
81torture_param(int, extendables, RCUTORTURE_MAX_EXTEND,
82 "Extend readers by disabling bh (1), irqs (2), or preempt (4)");
cb506e13 83torture_param(int, fqs_duration, 0, "Duration of fqs bursts (us), 0 to disable");
9e250225
PM
84torture_param(int, fqs_holdoff, 0, "Holdoff time within fqs bursts (us)");
85torture_param(int, fqs_stutter, 3, "Wait time between fqs bursts (s)");
cb506e13 86torture_param(int, fwd_progress, 1, "Number of grace-period forward progress tasks (0 to disable)");
1b27291b 87torture_param(int, fwd_progress_div, 4, "Fraction of CPU stall to wait");
cb506e13
PM
88torture_param(int, fwd_progress_holdoff, 60, "Time between forward-progress tests (s)");
89torture_param(bool, fwd_progress_need_resched, 1, "Hide cond_resched() behind need_resched()");
a48f3fad 90torture_param(bool, gp_cond, false, "Use conditional/async GP wait primitives");
11d62f0f 91torture_param(bool, gp_cond_exp, false, "Use conditional/async expedited GP wait primitives");
b6fe4917 92torture_param(bool, gp_cond_full, false, "Use conditional/async full-state GP wait primitives");
8df13f01
PM
93torture_param(bool, gp_cond_exp_full, false,
94 "Use conditional/async full-stateexpedited GP wait primitives");
282e06cc
PM
95torture_param(int, gp_cond_wi, 16 * USEC_PER_SEC / HZ,
96 "Wait interval for normal conditional grace periods, us (default 16 jiffies)");
97torture_param(int, gp_cond_wi_exp, 128,
98 "Wait interval for expedited conditional grace periods, us (default 128 us)");
9e250225 99torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
cb506e13 100torture_param(bool, gp_normal, false, "Use normal (non-expedited) GP wait primitives");
0fd0548d 101torture_param(bool, gp_poll, false, "Use polling GP wait primitives");
11d62f0f 102torture_param(bool, gp_poll_exp, false, "Use polling expedited GP wait primitives");
76ea3641 103torture_param(bool, gp_poll_full, false, "Use polling full-state GP wait primitives");
6c502b14 104torture_param(bool, gp_poll_exp_full, false, "Use polling full-state expedited GP wait primitives");
282e06cc
PM
105torture_param(int, gp_poll_wi, 16 * USEC_PER_SEC / HZ,
106 "Wait interval for normal polled grace periods, us (default 16 jiffies)");
107torture_param(int, gp_poll_wi_exp, 128,
108 "Wait interval for expedited polled grace periods, us (default 128 us)");
f0bf8fab 109torture_param(bool, gp_sync, false, "Use synchronous GP wait primitives");
9e250225 110torture_param(int, irqreader, 1, "Allow RCU readers from irq handlers");
d6855142 111torture_param(int, leakpointer, 0, "Leak pointer dereferences from readers");
cb506e13 112torture_param(int, n_barrier_cbs, 0, "# of callbacks/kthreads for barrier testing");
9e250225
PM
113torture_param(int, nfakewriters, 4, "Number of RCU fake writer threads");
114torture_param(int, nreaders, -1, "Number of RCU reader threads");
cb506e13 115torture_param(int, object_debug, 0, "Enable debug-object double call_rcu() testing");
9e250225 116torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
cb506e13 117torture_param(int, onoff_interval, 0, "Time between CPU hotplugs (jiffies), 0=disable");
2c4319bd
PM
118torture_param(int, nocbs_nthreads, 0, "Number of NOCB toggle threads, 0 to disable");
119torture_param(int, nocbs_toggle, 1000, "Time between toggling nocb state (ms)");
584975cc
PM
120torture_param(int, preempt_duration, 0, "Preemption duration (ms), zero to disable");
121torture_param(int, preempt_interval, MSEC_PER_SEC, "Interval between preemptions (ms)");
cb506e13
PM
122torture_param(int, read_exit_delay, 13, "Delay between read-then-exit episodes (s)");
123torture_param(int, read_exit_burst, 16, "# of read-then-exit bursts per episode, zero to disable");
0fef924e 124torture_param(int, reader_flavor, SRCU_READ_FLAVOR_NORMAL, "Reader flavors to use, one per bit.");
9e250225
PM
125torture_param(int, shuffle_interval, 3, "Number of seconds between shuffles");
126torture_param(int, shutdown_secs, 0, "Shutdown time (s), <= zero to disable.");
127torture_param(int, stall_cpu, 0, "Stall duration (s), zero to disable.");
cb506e13
PM
128torture_param(int, stall_cpu_holdoff, 10, "Time to wait before starting stall (s).");
129torture_param(bool, stall_no_softlockup, false, "Avoid softlockup warning during cpu stall.");
2b1516e5 130torture_param(int, stall_cpu_irqsoff, 0, "Disable interrupts while stalling.");
19a8ff95 131torture_param(int, stall_cpu_block, 0, "Sleep while stalling.");
58cb3210 132torture_param(int, stall_cpu_repeat, 0, "Number of additional stalls after the first one.");
cb506e13
PM
133torture_param(int, stall_gp_kthread, 0, "Grace-period kthread stall duration (s).");
134torture_param(int, stat_interval, 60, "Number of seconds between stats printk()s");
9e250225
PM
135torture_param(int, stutter, 5, "Number of seconds to run/halt test");
136torture_param(int, test_boost, 1, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
cb506e13
PM
137torture_param(int, test_boost_duration, 4, "Duration of each boost test, seconds.");
138torture_param(int, test_boost_interval, 7, "Interval between boost tests, seconds.");
808a9d67 139torture_param(int, test_nmis, 0, "End-test NMI tests, 0 to disable.");
cb506e13 140torture_param(bool, test_no_idle_hz, true, "Test support for tickless idle CPUs");
d94f12e8 141torture_param(int, test_srcu_lockdep, 0, "Test specified SRCU deadlock scenario.");
cb506e13 142torture_param(int, verbose, 1, "Enable verbose debugging printk()s");
9e250225 143
d10453e9 144static char *torture_type = "rcu";
d6ad6711 145module_param(torture_type, charp, 0444);
c770c82a 146MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, srcu, ...)");
a241ec65 147
2c4319bd 148static int nrealnocbers;
a241ec65
PM
149static int nrealreaders;
150static struct task_struct *writer_task;
b772e1dd 151static struct task_struct **fakewriter_tasks;
a241ec65 152static struct task_struct **reader_tasks;
2c4319bd 153static struct task_struct **nocb_tasks;
a241ec65 154static struct task_struct *stats_task;
bf66f18e 155static struct task_struct *fqs_task;
8e8be45e 156static struct task_struct *boost_tasks[NR_CPUS];
c13f3757 157static struct task_struct *stall_task;
82e31003 158static struct task_struct **fwd_prog_tasks;
fae4b54f
PM
159static struct task_struct **barrier_cbs_tasks;
160static struct task_struct *barrier_task;
4a5f133c 161static struct task_struct *read_exit_task;
584975cc 162static struct task_struct *preempt_task;
a241ec65
PM
163
164#define RCU_TORTURE_PIPE_LEN 10
165
00504537
PM
166// Mailbox-like structure to check RCU global memory ordering.
167struct rcu_torture_reader_check {
168 unsigned long rtc_myloops;
169 int rtc_chkrdr;
170 unsigned long rtc_chkloops;
171 int rtc_ready;
172 struct rcu_torture_reader_check *rtc_assigner;
173} ____cacheline_internodealigned_in_smp;
174
175// Update-side data structure used to check RCU readers.
a241ec65
PM
176struct rcu_torture {
177 struct rcu_head rtort_rcu;
178 int rtort_pipe_count;
179 struct list_head rtort_free;
996417d2 180 int rtort_mbtest;
00504537 181 struct rcu_torture_reader_check *rtort_chkp;
a241ec65
PM
182};
183
a241ec65 184static LIST_HEAD(rcu_torture_freelist);
0ddea0ea 185static struct rcu_torture __rcu *rcu_torture_current;
4a298656 186static unsigned long rcu_torture_current_version;
a241ec65
PM
187static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
188static DEFINE_SPINLOCK(rcu_torture_lock);
67522bee
PM
189static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count);
190static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch);
a241ec65 191static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
00504537 192static struct rcu_torture_reader_check *rcu_torture_reader_mbchk;
b2896d2e
PM
193static atomic_t n_rcu_torture_alloc;
194static atomic_t n_rcu_torture_alloc_fail;
195static atomic_t n_rcu_torture_free;
196static atomic_t n_rcu_torture_mberror;
00504537
PM
197static atomic_t n_rcu_torture_mbchk_fail;
198static atomic_t n_rcu_torture_mbchk_tries;
b2896d2e 199static atomic_t n_rcu_torture_error;
fae4b54f 200static long n_rcu_torture_barrier_error;
8e8be45e 201static long n_rcu_torture_boost_ktrerror;
8e8be45e
PM
202static long n_rcu_torture_boost_failure;
203static long n_rcu_torture_boosts;
8da9a595 204static atomic_long_t n_rcu_torture_timers;
fae4b54f 205static long n_barrier_attempts;
bf5b6435 206static long n_barrier_successes; /* did rcu_barrier test succeed? */
4a5f133c 207static unsigned long n_read_exits;
e3033736 208static struct list_head rcu_torture_removed;
60013d5d 209static unsigned long shutdown_jiffies;
959954df 210static unsigned long start_gp_seq;
2c4319bd
PM
211static atomic_long_t n_nocb_offload;
212static atomic_long_t n_nocb_deoffload;
d120f65f 213
ad0dc7f9
PM
214static int rcu_torture_writer_state;
215#define RTWS_FIXED_DELAY 0
216#define RTWS_DELAY 1
217#define RTWS_REPLACE 2
218#define RTWS_DEF_FREE 3
219#define RTWS_EXP_SYNC 4
a48f3fad 220#define RTWS_COND_GET 5
b6fe4917
PM
221#define RTWS_COND_GET_FULL 6
222#define RTWS_COND_GET_EXP 7
8df13f01
PM
223#define RTWS_COND_GET_EXP_FULL 8
224#define RTWS_COND_SYNC 9
225#define RTWS_COND_SYNC_FULL 10
226#define RTWS_COND_SYNC_EXP 11
227#define RTWS_COND_SYNC_EXP_FULL 12
228#define RTWS_POLL_GET 13
229#define RTWS_POLL_GET_FULL 14
230#define RTWS_POLL_GET_EXP 15
231#define RTWS_POLL_GET_EXP_FULL 16
232#define RTWS_POLL_WAIT 17
233#define RTWS_POLL_WAIT_FULL 18
234#define RTWS_POLL_WAIT_EXP 19
235#define RTWS_POLL_WAIT_EXP_FULL 20
236#define RTWS_SYNC 21
237#define RTWS_STUTTER 22
238#define RTWS_STOPPING 23
18aff33e
PM
239static const char * const rcu_torture_writer_state_names[] = {
240 "RTWS_FIXED_DELAY",
241 "RTWS_DELAY",
242 "RTWS_REPLACE",
243 "RTWS_DEF_FREE",
244 "RTWS_EXP_SYNC",
245 "RTWS_COND_GET",
b6fe4917 246 "RTWS_COND_GET_FULL",
11d62f0f 247 "RTWS_COND_GET_EXP",
8df13f01 248 "RTWS_COND_GET_EXP_FULL",
18aff33e 249 "RTWS_COND_SYNC",
b6fe4917 250 "RTWS_COND_SYNC_FULL",
11d62f0f 251 "RTWS_COND_SYNC_EXP",
8df13f01 252 "RTWS_COND_SYNC_EXP_FULL",
0fd0548d 253 "RTWS_POLL_GET",
76ea3641 254 "RTWS_POLL_GET_FULL",
11d62f0f 255 "RTWS_POLL_GET_EXP",
6c502b14 256 "RTWS_POLL_GET_EXP_FULL",
0fd0548d 257 "RTWS_POLL_WAIT",
76ea3641 258 "RTWS_POLL_WAIT_FULL",
11d62f0f 259 "RTWS_POLL_WAIT_EXP",
6c502b14 260 "RTWS_POLL_WAIT_EXP_FULL",
18aff33e
PM
261 "RTWS_SYNC",
262 "RTWS_STUTTER",
263 "RTWS_STOPPING",
264};
265
c116dba6
PM
266/* Record reader segment types and duration for first failing read. */
267struct rt_read_seg {
268 int rt_readstate;
269 unsigned long rt_delay_jiffies;
270 unsigned long rt_delay_ms;
271 unsigned long rt_delay_us;
272 bool rt_preempted;
579a05da 273 int rt_cpu;
885a6f47 274 int rt_end_cpu;
c116dba6
PM
275};
276static int err_segs_recorded;
277static struct rt_read_seg err_segs[RCUTORTURE_RDR_MAX_SEGS];
278static int rt_read_nsegs;
0f38c06c 279static int rt_read_preempted;
c116dba6 280
18aff33e
PM
281static const char *rcu_torture_writer_state_getname(void)
282{
283 unsigned int i = READ_ONCE(rcu_torture_writer_state);
284
285 if (i >= ARRAY_SIZE(rcu_torture_writer_state_names))
286 return "???";
287 return rcu_torture_writer_state_names[i];
288}
ad0dc7f9 289
e4aa0da3
SR
290#ifdef CONFIG_RCU_TRACE
291static u64 notrace rcu_trace_clock_local(void)
292{
293 u64 ts = trace_clock_local();
a3b7b6c2
PM
294
295 (void)do_div(ts, NSEC_PER_USEC);
e4aa0da3
SR
296 return ts;
297}
298#else /* #ifdef CONFIG_RCU_TRACE */
299static u64 notrace rcu_trace_clock_local(void)
300{
301 return 0ULL;
302}
303#endif /* #else #ifdef CONFIG_RCU_TRACE */
304
60013d5d
PM
305/*
306 * Stop aggressive CPU-hog tests a bit before the end of the test in order
307 * to avoid interfering with test shutdown.
308 */
309static bool shutdown_time_arrived(void)
310{
311 return shutdown_secs && time_after(jiffies, shutdown_jiffies - 30 * HZ);
312}
313
8e8be45e 314static unsigned long boost_starttime; /* jiffies of next boost test start. */
58ade2db 315static DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
8e8be45e 316 /* and boost task create/destroy. */
fae4b54f 317static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
c6ebcbb6 318static bool barrier_phase; /* Test phase. */
fae4b54f
PM
319static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
320static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
321static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
8e8be45e 322
e22ef8df 323static atomic_t rcu_fwd_cb_nodelay; /* Short rcu_torture_delay() delays. */
48718485 324
a241ec65
PM
325/*
326 * Allocate an element from the rcu_tortures pool.
327 */
97a41e26 328static struct rcu_torture *
a241ec65
PM
329rcu_torture_alloc(void)
330{
331 struct list_head *p;
332
adac1665 333 spin_lock_bh(&rcu_torture_lock);
a241ec65
PM
334 if (list_empty(&rcu_torture_freelist)) {
335 atomic_inc(&n_rcu_torture_alloc_fail);
adac1665 336 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
337 return NULL;
338 }
339 atomic_inc(&n_rcu_torture_alloc);
340 p = rcu_torture_freelist.next;
341 list_del_init(p);
adac1665 342 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
343 return container_of(p, struct rcu_torture, rtort_free);
344}
345
346/*
347 * Free an element to the rcu_tortures pool.
348 */
349static void
350rcu_torture_free(struct rcu_torture *p)
351{
352 atomic_inc(&n_rcu_torture_free);
adac1665 353 spin_lock_bh(&rcu_torture_lock);
a241ec65 354 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
adac1665 355 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
356}
357
72e9bb54
PM
358/*
359 * Operations vector for selecting different types of tests.
360 */
361
362struct rcu_torture_ops {
ad0dc7f9 363 int ttype;
72e9bb54 364 void (*init)(void);
ca1d51ed 365 void (*cleanup)(void);
72e9bb54 366 int (*readlock)(void);
c116dba6
PM
367 void (*read_delay)(struct torture_random_state *rrsp,
368 struct rt_read_seg *rtrsp);
72e9bb54 369 void (*readunlock)(int idx);
ec9d6356
PM
370 int (*readlock_held)(void); // lockdep.
371 int (*readlock_nesting)(void); // actual nesting, if available, -1 if not.
17ef2fe9 372 unsigned long (*get_gp_seq)(void);
d7219312 373 unsigned long (*gp_diff)(unsigned long new, unsigned long old);
0acc512c 374 void (*deferred_free)(struct rcu_torture *p);
b772e1dd 375 void (*sync)(void);
2ec1f2d9 376 void (*exp_sync)(void);
11d62f0f
PM
377 unsigned long (*get_gp_state_exp)(void);
378 unsigned long (*start_gp_poll_exp)(void);
6c502b14 379 void (*start_gp_poll_exp_full)(struct rcu_gp_oldstate *rgosp);
11d62f0f
PM
380 bool (*poll_gp_state_exp)(unsigned long oldstate);
381 void (*cond_sync_exp)(unsigned long oldstate);
8df13f01 382 void (*cond_sync_exp_full)(struct rcu_gp_oldstate *rgosp);
1324d95b 383 unsigned long (*get_comp_state)(void);
1d5ebc35 384 void (*get_comp_state_full)(struct rcu_gp_oldstate *rgosp);
1324d95b 385 bool (*same_gp_state)(unsigned long oldstate1, unsigned long oldstate2);
1d5ebc35 386 bool (*same_gp_state_full)(struct rcu_gp_oldstate *rgosp1, struct rcu_gp_oldstate *rgosp2);
fd56f64b 387 unsigned long (*get_gp_state)(void);
3fdefca9 388 void (*get_gp_state_full)(struct rcu_gp_oldstate *rgosp);
0fd0548d 389 unsigned long (*start_gp_poll)(void);
76ea3641 390 void (*start_gp_poll_full)(struct rcu_gp_oldstate *rgosp);
0fd0548d 391 bool (*poll_gp_state)(unsigned long oldstate);
91a967fd 392 bool (*poll_gp_state_full)(struct rcu_gp_oldstate *rgosp);
ed7d2f1a 393 bool (*poll_need_2gp)(bool poll, bool poll_full);
a48f3fad 394 void (*cond_sync)(unsigned long oldstate);
b6fe4917 395 void (*cond_sync_full)(struct rcu_gp_oldstate *rgosp);
72ed29f6
PM
396 int poll_active;
397 int poll_active_full;
db3e8db4 398 call_rcu_func_t call;
2326974d 399 void (*cb_barrier)(void);
bf66f18e 400 void (*fqs)(void);
eea203fe 401 void (*stats)(void);
27c0f144 402 void (*gp_kthread_dbg)(void);
0260b92e 403 bool (*check_boost_failed)(unsigned long gp_state, int *cpup);
1b27291b 404 int (*stall_dur)(void);
dddcddef 405 void (*get_gp_data)(int *flags, unsigned long *gp_seq);
1c67318b
Z
406 void (*gp_slow_register)(atomic_t *rgssp);
407 void (*gp_slow_unregister)(atomic_t *rgssp);
0f38c06c 408 bool (*reader_blocked)(void);
613b00fb 409 long cbflood_max;
0acc512c 410 int irq_capable;
8e8be45e 411 int can_boost;
2397d072 412 int extendables;
5eabea59 413 int slow_gps;
340170fe 414 int no_pi_lock;
43b39caf 415 int debug_objects;
de2ad0e7 416 int start_poll_irqsoff;
e66c33d5 417 const char *name;
72e9bb54 418};
a71fca58
PM
419
420static struct rcu_torture_ops *cur_ops;
72e9bb54
PM
421
422/*
423 * Definitions for rcu torture testing.
424 */
425
a5c095e0
PM
426static int torture_readlock_not_held(void)
427{
428 return rcu_read_lock_bh_held() || rcu_read_lock_sched_held();
429}
430
27366159 431static int rcu_torture_read_lock(void)
72e9bb54
PM
432{
433 rcu_read_lock();
434 return 0;
435}
436
c116dba6
PM
437static void
438rcu_read_delay(struct torture_random_state *rrsp, struct rt_read_seg *rtrsp)
b2896d2e 439{
d0af39e8
PM
440 unsigned long started;
441 unsigned long completed;
b8d57a76 442 const unsigned long shortdelay_us = 200;
1e696765 443 unsigned long longdelay_ms = 300;
d0af39e8 444 unsigned long long ts;
b2896d2e 445
b8d57a76
JT
446 /* We want a short delay sometimes to make a reader delay the grace
447 * period, and we want a long delay occasionally to trigger
448 * force_quiescent_state. */
b2896d2e 449
e22ef8df 450 if (!atomic_read(&rcu_fwd_cb_nodelay) &&
48718485 451 !(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms))) {
17ef2fe9 452 started = cur_ops->get_gp_seq();
d0af39e8 453 ts = rcu_trace_clock_local();
1e696765
PM
454 if (preempt_count() & (SOFTIRQ_MASK | HARDIRQ_MASK))
455 longdelay_ms = 5; /* Avoid triggering BH limits. */
b8d57a76 456 mdelay(longdelay_ms);
c116dba6 457 rtrsp->rt_delay_ms = longdelay_ms;
17ef2fe9 458 completed = cur_ops->get_gp_seq();
d0af39e8
PM
459 do_trace_rcu_torture_read(cur_ops->name, NULL, ts,
460 started, completed);
461 }
c116dba6 462 if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us))) {
b8d57a76 463 udelay(shortdelay_us);
c116dba6
PM
464 rtrsp->rt_delay_us = shortdelay_us;
465 }
51b1130e 466 if (!preempt_count() &&
223f16b8 467 !(torture_random(rrsp) % (nrealreaders * 500)))
cc1321c9 468 torture_preempt_schedule(); /* QS only if preemptible. */
b2896d2e
PM
469}
470
27366159 471static void rcu_torture_read_unlock(int idx)
72e9bb54
PM
472{
473 rcu_read_unlock();
474}
475
ec9d6356
PM
476static int rcu_torture_readlock_nesting(void)
477{
478 if (IS_ENABLED(CONFIG_PREEMPT_RCU))
479 return rcu_preempt_depth();
480 if (IS_ENABLED(CONFIG_PREEMPT_COUNT))
481 return (preempt_count() & PREEMPT_MASK);
482 return -1;
483}
484
a48f3fad
PM
485/*
486 * Update callback in the pipe. This should be invoked after a grace period.
487 */
488static bool
489rcu_torture_pipe_update_one(struct rcu_torture *rp)
490{
491 int i;
00504537 492 struct rcu_torture_reader_check *rtrcp = READ_ONCE(rp->rtort_chkp);
a48f3fad 493
00504537
PM
494 if (rtrcp) {
495 WRITE_ONCE(rp->rtort_chkp, NULL);
496 smp_store_release(&rtrcp->rtc_ready, 1); // Pair with smp_load_acquire().
497 }
8d0f9a66 498 i = rp->rtort_pipe_count;
a48f3fad
PM
499 if (i > RCU_TORTURE_PIPE_LEN)
500 i = RCU_TORTURE_PIPE_LEN;
501 atomic_inc(&rcu_torture_wcount[i]);
20248910 502 WRITE_ONCE(rp->rtort_pipe_count, i + 1);
c507e195 503 ASSERT_EXCLUSIVE_WRITER(rp->rtort_pipe_count);
a10e3cbf 504 if (i + 1 >= RCU_TORTURE_PIPE_LEN) {
a48f3fad
PM
505 rp->rtort_mbtest = 0;
506 return true;
507 }
508 return false;
509}
510
511/*
512 * Update all callbacks in the pipe. Suitable for synchronous grace-period
513 * primitives.
514 */
515static void
516rcu_torture_pipe_update(struct rcu_torture *old_rp)
517{
518 struct rcu_torture *rp;
519 struct rcu_torture *rp1;
520
521 if (old_rp)
522 list_add(&old_rp->rtort_free, &rcu_torture_removed);
523 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
524 if (rcu_torture_pipe_update_one(rp)) {
525 list_del(&rp->rtort_free);
526 rcu_torture_free(rp);
527 }
528 }
529}
530
72e9bb54
PM
531static void
532rcu_torture_cb(struct rcu_head *p)
533{
72e9bb54
PM
534 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
535
36970bb9 536 if (torture_must_stop_irq()) {
72e9bb54
PM
537 /* Test is ending, just drop callbacks on the floor. */
538 /* The next initialization will pick up the pieces. */
539 return;
540 }
a48f3fad 541 if (rcu_torture_pipe_update_one(rp))
72e9bb54 542 rcu_torture_free(rp);
a48f3fad 543 else
0acc512c 544 cur_ops->deferred_free(rp);
72e9bb54
PM
545}
546
6b80da42 547static unsigned long rcu_no_completed(void)
d9a3da06
PM
548{
549 return 0;
550}
551
72e9bb54
PM
552static void rcu_torture_deferred_free(struct rcu_torture *p)
553{
405d8e91 554 call_rcu_hurry(&p->rtort_rcu, rcu_torture_cb);
72e9bb54
PM
555}
556
e3033736
JT
557static void rcu_sync_torture_init(void)
558{
559 INIT_LIST_HEAD(&rcu_torture_removed);
560}
561
ed7d2f1a
PM
562static bool rcu_poll_need_2gp(bool poll, bool poll_full)
563{
b3cdd0a7 564 return poll;
ed7d2f1a
PM
565}
566
2ec1f2d9 567static struct rcu_torture_ops rcu_ops = {
0260b92e
PM
568 .ttype = RCU_FLAVOR,
569 .init = rcu_sync_torture_init,
570 .readlock = rcu_torture_read_lock,
571 .read_delay = rcu_read_delay,
572 .readunlock = rcu_torture_read_unlock,
573 .readlock_held = torture_readlock_not_held,
ec9d6356 574 .readlock_nesting = rcu_torture_readlock_nesting,
0260b92e
PM
575 .get_gp_seq = rcu_get_gp_seq,
576 .gp_diff = rcu_seq_diff,
577 .deferred_free = rcu_torture_deferred_free,
578 .sync = synchronize_rcu,
579 .exp_sync = synchronize_rcu_expedited,
1324d95b 580 .same_gp_state = same_state_synchronize_rcu,
1d5ebc35 581 .same_gp_state_full = same_state_synchronize_rcu_full,
1324d95b 582 .get_comp_state = get_completed_synchronize_rcu,
1d5ebc35 583 .get_comp_state_full = get_completed_synchronize_rcu_full,
0260b92e 584 .get_gp_state = get_state_synchronize_rcu,
3fdefca9 585 .get_gp_state_full = get_state_synchronize_rcu_full,
0260b92e 586 .start_gp_poll = start_poll_synchronize_rcu,
76ea3641 587 .start_gp_poll_full = start_poll_synchronize_rcu_full,
0260b92e 588 .poll_gp_state = poll_state_synchronize_rcu,
91a967fd 589 .poll_gp_state_full = poll_state_synchronize_rcu_full,
ed7d2f1a 590 .poll_need_2gp = rcu_poll_need_2gp,
0260b92e 591 .cond_sync = cond_synchronize_rcu,
b6fe4917 592 .cond_sync_full = cond_synchronize_rcu_full,
72ed29f6
PM
593 .poll_active = NUM_ACTIVE_RCU_POLL_OLDSTATE,
594 .poll_active_full = NUM_ACTIVE_RCU_POLL_FULL_OLDSTATE,
11d62f0f
PM
595 .get_gp_state_exp = get_state_synchronize_rcu,
596 .start_gp_poll_exp = start_poll_synchronize_rcu_expedited,
6c502b14 597 .start_gp_poll_exp_full = start_poll_synchronize_rcu_expedited_full,
11d62f0f
PM
598 .poll_gp_state_exp = poll_state_synchronize_rcu,
599 .cond_sync_exp = cond_synchronize_rcu_expedited,
4569cf60 600 .cond_sync_exp_full = cond_synchronize_rcu_expedited_full,
405d8e91 601 .call = call_rcu_hurry,
0260b92e
PM
602 .cb_barrier = rcu_barrier,
603 .fqs = rcu_force_quiescent_state,
0260b92e
PM
604 .gp_kthread_dbg = show_rcu_gp_kthreads,
605 .check_boost_failed = rcu_check_boost_fail,
606 .stall_dur = rcu_jiffies_till_stall_check,
dddcddef 607 .get_gp_data = rcutorture_get_gp_data,
1c67318b
Z
608 .gp_slow_register = rcu_gp_slow_register,
609 .gp_slow_unregister = rcu_gp_slow_unregister,
0f38c06c
PM
610 .reader_blocked = IS_ENABLED(CONFIG_RCU_TORTURE_TEST_LOG_CPU)
611 ? has_rcu_reader_blocked
612 : NULL,
0260b92e
PM
613 .irq_capable = 1,
614 .can_boost = IS_ENABLED(CONFIG_RCU_BOOST),
615 .extendables = RCUTORTURE_MAX_EXTEND,
43b39caf 616 .debug_objects = 1,
de2ad0e7 617 .start_poll_irqsoff = 1,
0260b92e 618 .name = "rcu"
d9a3da06
PM
619};
620
ff20e251
PM
621/*
622 * Don't even think about trying any of these in real life!!!
623 * The names includes "busted", and they really means it!
624 * The only purpose of these functions is to provide a buggy RCU
625 * implementation to make sure that rcutorture correctly emits
626 * buggy-RCU error messages.
627 */
628static void rcu_busted_torture_deferred_free(struct rcu_torture *p)
629{
630 /* This is a deliberate bug for testing purposes only! */
631 rcu_torture_cb(&p->rtort_rcu);
632}
633
634static void synchronize_rcu_busted(void)
635{
636 /* This is a deliberate bug for testing purposes only! */
637}
638
639static void
b6a4ae76 640call_rcu_busted(struct rcu_head *head, rcu_callback_t func)
ff20e251
PM
641{
642 /* This is a deliberate bug for testing purposes only! */
643 func(head);
644}
645
646static struct rcu_torture_ops rcu_busted_ops = {
ad0dc7f9 647 .ttype = INVALID_RCU_FLAVOR,
ff20e251
PM
648 .init = rcu_sync_torture_init,
649 .readlock = rcu_torture_read_lock,
650 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
651 .readunlock = rcu_torture_read_unlock,
a5c095e0 652 .readlock_held = torture_readlock_not_held,
17ef2fe9 653 .get_gp_seq = rcu_no_completed,
ff20e251
PM
654 .deferred_free = rcu_busted_torture_deferred_free,
655 .sync = synchronize_rcu_busted,
656 .exp_sync = synchronize_rcu_busted,
657 .call = call_rcu_busted,
ff20e251 658 .irq_capable = 1,
b27a34f9 659 .extendables = RCUTORTURE_MAX_EXTEND,
b3c98314 660 .name = "busted"
ff20e251
PM
661};
662
b2896d2e
PM
663/*
664 * Definitions for srcu torture testing.
665 */
666
cda4dc81 667DEFINE_STATIC_SRCU(srcu_ctl);
ca1d51ed
PM
668static struct srcu_struct srcu_ctld;
669static struct srcu_struct *srcu_ctlp = &srcu_ctl;
2e83b879 670static struct rcu_torture_ops srcud_ops;
b2896d2e 671
dddcddef
Z
672static void srcu_get_gp_data(int *flags, unsigned long *gp_seq)
673{
674 srcutorture_get_gp_data(srcu_ctlp, flags, gp_seq);
675}
676
27366159 677static int srcu_torture_read_lock(void)
b2896d2e 678{
95a5de21
PM
679 int idx;
680 int ret = 0;
681
0fef924e 682 if ((reader_flavor & SRCU_READ_FLAVOR_NORMAL) || !(reader_flavor & SRCU_READ_FLAVOR_ALL)) {
95a5de21
PM
683 idx = srcu_read_lock(srcu_ctlp);
684 WARN_ON_ONCE(idx & ~0x1);
685 ret += idx;
686 }
0fef924e 687 if (reader_flavor & SRCU_READ_FLAVOR_NMI) {
95a5de21
PM
688 idx = srcu_read_lock_nmisafe(srcu_ctlp);
689 WARN_ON_ONCE(idx & ~0x1);
690 ret += idx << 1;
691 }
0fef924e 692 if (reader_flavor & SRCU_READ_FLAVOR_LITE) {
43349fc4
PM
693 idx = srcu_read_lock_lite(srcu_ctlp);
694 WARN_ON_ONCE(idx & ~0x1);
695 ret += idx << 2;
696 }
95a5de21 697 return ret;
b2896d2e
PM
698}
699
c116dba6
PM
700static void
701srcu_read_delay(struct torture_random_state *rrsp, struct rt_read_seg *rtrsp)
b2896d2e
PM
702{
703 long delay;
704 const long uspertick = 1000000 / HZ;
705 const long longdelay = 10;
706
707 /* We want there to be long-running readers, but not all the time. */
708
51b1130e
PM
709 delay = torture_random(rrsp) %
710 (nrealreaders * 2 * longdelay * uspertick);
c116dba6 711 if (!delay && in_task()) {
b2896d2e 712 schedule_timeout_interruptible(longdelay);
c116dba6
PM
713 rtrsp->rt_delay_jiffies = longdelay;
714 } else {
715 rcu_read_delay(rrsp, rtrsp);
716 }
b2896d2e
PM
717}
718
27366159 719static void srcu_torture_read_unlock(int idx)
b2896d2e 720{
95a5de21 721 WARN_ON_ONCE((reader_flavor && (idx & ~reader_flavor)) || (!reader_flavor && (idx & ~0x1)));
0fef924e 722 if (reader_flavor & SRCU_READ_FLAVOR_LITE)
43349fc4 723 srcu_read_unlock_lite(srcu_ctlp, (idx & 0x4) >> 2);
0fef924e 724 if (reader_flavor & SRCU_READ_FLAVOR_NMI)
95a5de21 725 srcu_read_unlock_nmisafe(srcu_ctlp, (idx & 0x2) >> 1);
0fef924e 726 if ((reader_flavor & SRCU_READ_FLAVOR_NORMAL) || !(reader_flavor & SRCU_READ_FLAVOR_ALL))
95a5de21 727 srcu_read_unlock(srcu_ctlp, idx & 0x1);
b2896d2e
PM
728}
729
a5c095e0
PM
730static int torture_srcu_read_lock_held(void)
731{
732 return srcu_read_lock_held(srcu_ctlp);
733}
734
6b80da42 735static unsigned long srcu_torture_completed(void)
b2896d2e 736{
ca1d51ed 737 return srcu_batches_completed(srcu_ctlp);
b2896d2e
PM
738}
739
9059c940
LJ
740static void srcu_torture_deferred_free(struct rcu_torture *rp)
741{
ca1d51ed 742 call_srcu(srcu_ctlp, &rp->rtort_rcu, rcu_torture_cb);
9059c940
LJ
743}
744
b772e1dd
JT
745static void srcu_torture_synchronize(void)
746{
ca1d51ed 747 synchronize_srcu(srcu_ctlp);
b772e1dd
JT
748}
749
0fd0548d
PM
750static unsigned long srcu_torture_get_gp_state(void)
751{
752 return get_state_synchronize_srcu(srcu_ctlp);
753}
754
755static unsigned long srcu_torture_start_gp_poll(void)
756{
757 return start_poll_synchronize_srcu(srcu_ctlp);
758}
759
760static bool srcu_torture_poll_gp_state(unsigned long oldstate)
761{
762 return poll_state_synchronize_srcu(srcu_ctlp, oldstate);
763}
764
e3f8d378 765static void srcu_torture_call(struct rcu_head *head,
b6a4ae76 766 rcu_callback_t func)
e3f8d378 767{
ca1d51ed 768 call_srcu(srcu_ctlp, head, func);
e3f8d378
PM
769}
770
771static void srcu_torture_barrier(void)
772{
ca1d51ed 773 srcu_barrier(srcu_ctlp);
e3f8d378
PM
774}
775
eea203fe 776static void srcu_torture_stats(void)
b2896d2e 777{
115a1a52 778 srcu_torture_stats_print(srcu_ctlp, torture_type, TORTURE_FLAG);
b2896d2e
PM
779}
780
2ec1f2d9
PM
781static void srcu_torture_synchronize_expedited(void)
782{
ca1d51ed 783 synchronize_srcu_expedited(srcu_ctlp);
2ec1f2d9
PM
784}
785
b2896d2e 786static struct rcu_torture_ops srcu_ops = {
ad0dc7f9 787 .ttype = SRCU_FLAVOR,
cda4dc81 788 .init = rcu_sync_torture_init,
0acc512c
PM
789 .readlock = srcu_torture_read_lock,
790 .read_delay = srcu_read_delay,
791 .readunlock = srcu_torture_read_unlock,
a5c095e0 792 .readlock_held = torture_srcu_read_lock_held,
17ef2fe9 793 .get_gp_seq = srcu_torture_completed,
9059c940 794 .deferred_free = srcu_torture_deferred_free,
0acc512c 795 .sync = srcu_torture_synchronize,
2ec1f2d9 796 .exp_sync = srcu_torture_synchronize_expedited,
1bc5bb9a
PM
797 .same_gp_state = same_state_synchronize_srcu,
798 .get_comp_state = get_completed_synchronize_srcu,
0fd0548d
PM
799 .get_gp_state = srcu_torture_get_gp_state,
800 .start_gp_poll = srcu_torture_start_gp_poll,
801 .poll_gp_state = srcu_torture_poll_gp_state,
72ed29f6 802 .poll_active = NUM_ACTIVE_SRCU_POLL_OLDSTATE,
e3f8d378
PM
803 .call = srcu_torture_call,
804 .cb_barrier = srcu_torture_barrier,
0acc512c 805 .stats = srcu_torture_stats,
dddcddef 806 .get_gp_data = srcu_get_gp_data,
9c0f1c7f 807 .cbflood_max = 50000,
5e741fa9 808 .irq_capable = 1,
340170fe 809 .no_pi_lock = IS_ENABLED(CONFIG_TINY_SRCU),
43b39caf 810 .debug_objects = 1,
0acc512c 811 .name = "srcu"
b2896d2e
PM
812};
813
ca1d51ed
PM
814static void srcu_torture_init(void)
815{
816 rcu_sync_torture_init();
817 WARN_ON(init_srcu_struct(&srcu_ctld));
818 srcu_ctlp = &srcu_ctld;
819}
820
821static void srcu_torture_cleanup(void)
822{
f5ad3991 823 cleanup_srcu_struct(&srcu_ctld);
ca1d51ed
PM
824 srcu_ctlp = &srcu_ctl; /* In case of a later rcutorture run. */
825}
826
827/* As above, but dynamically allocated. */
828static struct rcu_torture_ops srcud_ops = {
829 .ttype = SRCU_FLAVOR,
830 .init = srcu_torture_init,
831 .cleanup = srcu_torture_cleanup,
832 .readlock = srcu_torture_read_lock,
833 .read_delay = srcu_read_delay,
834 .readunlock = srcu_torture_read_unlock,
a5c095e0 835 .readlock_held = torture_srcu_read_lock_held,
17ef2fe9 836 .get_gp_seq = srcu_torture_completed,
ca1d51ed
PM
837 .deferred_free = srcu_torture_deferred_free,
838 .sync = srcu_torture_synchronize,
839 .exp_sync = srcu_torture_synchronize_expedited,
1bc5bb9a
PM
840 .same_gp_state = same_state_synchronize_srcu,
841 .get_comp_state = get_completed_synchronize_srcu,
599d97e3
PM
842 .get_gp_state = srcu_torture_get_gp_state,
843 .start_gp_poll = srcu_torture_start_gp_poll,
844 .poll_gp_state = srcu_torture_poll_gp_state,
72ed29f6 845 .poll_active = NUM_ACTIVE_SRCU_POLL_OLDSTATE,
ca1d51ed
PM
846 .call = srcu_torture_call,
847 .cb_barrier = srcu_torture_barrier,
848 .stats = srcu_torture_stats,
dddcddef 849 .get_gp_data = srcu_get_gp_data,
9c0f1c7f 850 .cbflood_max = 50000,
5e741fa9 851 .irq_capable = 1,
340170fe 852 .no_pi_lock = IS_ENABLED(CONFIG_TINY_SRCU),
43b39caf 853 .debug_objects = 1,
ca1d51ed
PM
854 .name = "srcud"
855};
856
2397d072
PM
857/* As above, but broken due to inappropriate reader extension. */
858static struct rcu_torture_ops busted_srcud_ops = {
859 .ttype = SRCU_FLAVOR,
860 .init = srcu_torture_init,
861 .cleanup = srcu_torture_cleanup,
862 .readlock = srcu_torture_read_lock,
863 .read_delay = rcu_read_delay,
864 .readunlock = srcu_torture_read_unlock,
a5c095e0 865 .readlock_held = torture_srcu_read_lock_held,
2397d072
PM
866 .get_gp_seq = srcu_torture_completed,
867 .deferred_free = srcu_torture_deferred_free,
868 .sync = srcu_torture_synchronize,
869 .exp_sync = srcu_torture_synchronize_expedited,
870 .call = srcu_torture_call,
871 .cb_barrier = srcu_torture_barrier,
872 .stats = srcu_torture_stats,
873 .irq_capable = 1,
340170fe 874 .no_pi_lock = IS_ENABLED(CONFIG_TINY_SRCU),
2397d072
PM
875 .extendables = RCUTORTURE_MAX_EXTEND,
876 .name = "busted_srcud"
877};
878
40c1278a
PM
879/*
880 * Definitions for trivial CONFIG_PREEMPT=n-only torture testing.
881 * This implementation does not necessarily work well with CPU hotplug.
882 */
883
884static void synchronize_rcu_trivial(void)
885{
886 int cpu;
887
888 for_each_online_cpu(cpu) {
0203b485 889 torture_sched_setaffinity(current->pid, cpumask_of(cpu), true);
40c1278a
PM
890 WARN_ON_ONCE(raw_smp_processor_id() != cpu);
891 }
892}
893
27366159 894static int rcu_torture_read_lock_trivial(void)
40c1278a
PM
895{
896 preempt_disable();
897 return 0;
898}
899
27366159 900static void rcu_torture_read_unlock_trivial(int idx)
40c1278a
PM
901{
902 preempt_enable();
903}
904
905static struct rcu_torture_ops trivial_ops = {
906 .ttype = RCU_TRIVIAL_FLAVOR,
907 .init = rcu_sync_torture_init,
908 .readlock = rcu_torture_read_lock_trivial,
909 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
910 .readunlock = rcu_torture_read_unlock_trivial,
911 .readlock_held = torture_readlock_not_held,
912 .get_gp_seq = rcu_no_completed,
913 .sync = synchronize_rcu_trivial,
914 .exp_sync = synchronize_rcu_trivial,
40c1278a
PM
915 .irq_capable = 1,
916 .name = "trivial"
917};
918
3b6e1dd4
PM
919#ifdef CONFIG_TASKS_RCU
920
69c60455
PM
921/*
922 * Definitions for RCU-tasks torture testing.
923 */
924
925static int tasks_torture_read_lock(void)
926{
927 return 0;
928}
929
930static void tasks_torture_read_unlock(int idx)
931{
932}
933
934static void rcu_tasks_torture_deferred_free(struct rcu_torture *p)
935{
936 call_rcu_tasks(&p->rtort_rcu, rcu_torture_cb);
937}
938
9cf8fc6f
PM
939static void synchronize_rcu_mult_test(void)
940{
405d8e91 941 synchronize_rcu_mult(call_rcu_tasks, call_rcu_hurry);
9cf8fc6f
PM
942}
943
69c60455
PM
944static struct rcu_torture_ops tasks_ops = {
945 .ttype = RCU_TASKS_FLAVOR,
946 .init = rcu_sync_torture_init,
947 .readlock = tasks_torture_read_lock,
948 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
949 .readunlock = tasks_torture_read_unlock,
17ef2fe9 950 .get_gp_seq = rcu_no_completed,
69c60455
PM
951 .deferred_free = rcu_tasks_torture_deferred_free,
952 .sync = synchronize_rcu_tasks,
9cf8fc6f 953 .exp_sync = synchronize_rcu_mult_test,
69c60455
PM
954 .call = call_rcu_tasks,
955 .cb_barrier = rcu_barrier_tasks,
27c0f144 956 .gp_kthread_dbg = show_rcu_tasks_classic_gp_kthread,
dddcddef 957 .get_gp_data = rcu_tasks_get_gp_data,
69c60455 958 .irq_capable = 1,
5eabea59 959 .slow_gps = 1,
69c60455
PM
960 .name = "tasks"
961};
962
3b6e1dd4 963#define TASKS_OPS &tasks_ops,
c682db55 964
3b6e1dd4 965#else // #ifdef CONFIG_TASKS_RCU
c682db55 966
3b6e1dd4 967#define TASKS_OPS
c682db55 968
3b6e1dd4 969#endif // #else #ifdef CONFIG_TASKS_RCU
c682db55 970
c682db55 971
4c3f7b0e 972#ifdef CONFIG_TASKS_RUDE_RCU
c682db55 973
3d6e43c7
PM
974/*
975 * Definitions for rude RCU-tasks torture testing.
976 */
977
3d6e43c7
PM
978static struct rcu_torture_ops tasks_rude_ops = {
979 .ttype = RCU_TASKS_RUDE_FLAVOR,
980 .init = rcu_sync_torture_init,
981 .readlock = rcu_torture_read_lock_trivial,
982 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
983 .readunlock = rcu_torture_read_unlock_trivial,
984 .get_gp_seq = rcu_no_completed,
3d6e43c7
PM
985 .sync = synchronize_rcu_tasks_rude,
986 .exp_sync = synchronize_rcu_tasks_rude,
27c0f144 987 .gp_kthread_dbg = show_rcu_tasks_rude_gp_kthread,
dddcddef 988 .get_gp_data = rcu_tasks_rude_get_gp_data,
613b00fb 989 .cbflood_max = 50000,
3d6e43c7
PM
990 .irq_capable = 1,
991 .name = "tasks-rude"
992};
993
4c3f7b0e
PM
994#define TASKS_RUDE_OPS &tasks_rude_ops,
995
996#else // #ifdef CONFIG_TASKS_RUDE_RCU
997
998#define TASKS_RUDE_OPS
999
1000#endif // #else #ifdef CONFIG_TASKS_RUDE_RCU
1001
1002
40c1278a
PM
1003#ifdef CONFIG_TASKS_TRACE_RCU
1004
c1a76c0b
PM
1005/*
1006 * Definitions for tracing RCU-tasks torture testing.
1007 */
1008
1009static int tasks_tracing_torture_read_lock(void)
1010{
1011 rcu_read_lock_trace();
1012 return 0;
1013}
1014
1015static void tasks_tracing_torture_read_unlock(int idx)
1016{
1017 rcu_read_unlock_trace();
1018}
1019
1020static void rcu_tasks_tracing_torture_deferred_free(struct rcu_torture *p)
1021{
1022 call_rcu_tasks_trace(&p->rtort_rcu, rcu_torture_cb);
1023}
1024
1025static struct rcu_torture_ops tasks_tracing_ops = {
1026 .ttype = RCU_TASKS_TRACING_FLAVOR,
1027 .init = rcu_sync_torture_init,
1028 .readlock = tasks_tracing_torture_read_lock,
1029 .read_delay = srcu_read_delay, /* just reuse srcu's version. */
1030 .readunlock = tasks_tracing_torture_read_unlock,
a5c095e0 1031 .readlock_held = rcu_read_lock_trace_held,
c1a76c0b
PM
1032 .get_gp_seq = rcu_no_completed,
1033 .deferred_free = rcu_tasks_tracing_torture_deferred_free,
1034 .sync = synchronize_rcu_tasks_trace,
1035 .exp_sync = synchronize_rcu_tasks_trace,
1036 .call = call_rcu_tasks_trace,
1037 .cb_barrier = rcu_barrier_tasks_trace,
27c0f144 1038 .gp_kthread_dbg = show_rcu_tasks_trace_gp_kthread,
dddcddef 1039 .get_gp_data = rcu_tasks_trace_get_gp_data,
613b00fb 1040 .cbflood_max = 50000,
c1a76c0b
PM
1041 .irq_capable = 1,
1042 .slow_gps = 1,
1043 .name = "tasks-tracing"
1044};
1045
40c1278a
PM
1046#define TASKS_TRACING_OPS &tasks_tracing_ops,
1047
1048#else // #ifdef CONFIG_TASKS_TRACE_RCU
1049
1050#define TASKS_TRACING_OPS
1051
1052#endif // #else #ifdef CONFIG_TASKS_TRACE_RCU
1053
1054
d7219312
PM
1055static unsigned long rcutorture_seq_diff(unsigned long new, unsigned long old)
1056{
1057 if (!cur_ops->gp_diff)
1058 return new - old;
1059 return cur_ops->gp_diff(new, old);
1060}
1061
8e8be45e
PM
1062/*
1063 * RCU torture priority-boost testing. Runs one real-time thread per
ea6d962e
PM
1064 * CPU for moderate bursts, repeatedly starting grace periods and waiting
1065 * for them to complete. If a given grace period takes too long, we assume
1066 * that priority inversion has occurred.
8e8be45e
PM
1067 */
1068
450efca7
JFG
1069static int old_rt_runtime = -1;
1070
1071static void rcu_torture_disable_rt_throttle(void)
1072{
1073 /*
1074 * Disable RT throttling so that rcutorture's boost threads don't get
1075 * throttled. Only possible if rcutorture is built-in otherwise the
1076 * user should manually do this by setting the sched_rt_period_us and
1077 * sched_rt_runtime sysctls.
1078 */
1079 if (!IS_BUILTIN(CONFIG_RCU_TORTURE_TEST) || old_rt_runtime != -1)
1080 return;
1081
1082 old_rt_runtime = sysctl_sched_rt_runtime;
1083 sysctl_sched_rt_runtime = -1;
1084}
1085
1086static void rcu_torture_enable_rt_throttle(void)
1087{
1088 if (!IS_BUILTIN(CONFIG_RCU_TORTURE_TEST) || old_rt_runtime == -1)
1089 return;
1090
1091 sysctl_sched_rt_runtime = old_rt_runtime;
1092 old_rt_runtime = -1;
1093}
1094
063f5a4d 1095static bool rcu_torture_boost_failed(unsigned long gp_state, unsigned long *start)
3b745c89 1096{
0260b92e 1097 int cpu;
5e59fba5 1098 static int dbg_done;
063f5a4d 1099 unsigned long end = jiffies;
bcd4af44 1100 bool gp_done;
0260b92e
PM
1101 unsigned long j;
1102 static unsigned long last_persist;
1103 unsigned long lp;
1104 unsigned long mininterval = test_boost_duration * HZ - HZ / 2;
5e59fba5 1105
063f5a4d 1106 if (end - *start > mininterval) {
7b9dad7a
PM
1107 // Recheck after checking time to avoid false positives.
1108 smp_mb(); // Time check before grace-period check.
1109 if (cur_ops->poll_gp_state(gp_state))
1110 return false; // passed, though perhaps just barely
0260b92e
PM
1111 if (cur_ops->check_boost_failed && !cur_ops->check_boost_failed(gp_state, &cpu)) {
1112 // At most one persisted message per boost test.
1113 j = jiffies;
1114 lp = READ_ONCE(last_persist);
80e935c8
PM
1115 if (time_after(j, lp + mininterval) &&
1116 cmpxchg(&last_persist, lp, j) == lp) {
1117 if (cpu < 0)
1118 pr_info("Boost inversion persisted: QS from all CPUs\n");
1119 else
1120 pr_info("Boost inversion persisted: No QS from CPU %d\n", cpu);
1121 }
0260b92e
PM
1122 return false; // passed on a technicality
1123 }
3b745c89
JFG
1124 VERBOSE_TOROUT_STRING("rcu_torture_boost boosting failed");
1125 n_rcu_torture_boost_failure++;
ea6d962e
PM
1126 if (!xchg(&dbg_done, 1) && cur_ops->gp_kthread_dbg) {
1127 pr_info("Boost inversion thread ->rt_priority %u gp_state %lu jiffies %lu\n",
063f5a4d 1128 current->rt_priority, gp_state, end - *start);
5e59fba5 1129 cur_ops->gp_kthread_dbg();
bcd4af44
PM
1130 // Recheck after print to flag grace period ending during splat.
1131 gp_done = cur_ops->poll_gp_state(gp_state);
1132 pr_info("Boost inversion: GP %lu %s.\n", gp_state,
1133 gp_done ? "ended already" : "still pending");
3b745c89 1134
ea6d962e 1135 }
3b745c89 1136
7b9dad7a 1137 return true; // failed
063f5a4d
PM
1138 } else if (cur_ops->check_boost_failed && !cur_ops->check_boost_failed(gp_state, NULL)) {
1139 *start = jiffies;
3b745c89
JFG
1140 }
1141
7b9dad7a 1142 return false; // passed
3b745c89
JFG
1143}
1144
8e8be45e
PM
1145static int rcu_torture_boost(void *arg)
1146{
8e8be45e 1147 unsigned long endtime;
ea6d962e
PM
1148 unsigned long gp_state;
1149 unsigned long gp_state_time;
8e8be45e 1150 unsigned long oldstarttime;
8e8be45e 1151
5ccf60f2 1152 VERBOSE_TOROUT_STRING("rcu_torture_boost started");
8e8be45e
PM
1153
1154 /* Set real-time priority. */
8b700983 1155 sched_set_fifo_low(current);
8e8be45e
PM
1156
1157 /* Each pass through the following loop does one boost-test cycle. */
1158 do {
5e59fba5 1159 bool failed = false; // Test failed already in this test interval
ea6d962e 1160 bool gp_initiated = false;
3b745c89 1161
3b745c89
JFG
1162 if (kthread_should_stop())
1163 goto checkwait;
1164
8e8be45e 1165 /* Wait for the next test interval. */
a47f9f13 1166 oldstarttime = READ_ONCE(boost_starttime);
3c80b402 1167 while (time_before(jiffies, oldstarttime)) {
0e11c8e8 1168 schedule_timeout_interruptible(oldstarttime - jiffies);
ab1b7880
PM
1169 if (stutter_wait("rcu_torture_boost"))
1170 sched_set_fifo_low(current);
36970bb9 1171 if (torture_must_stop())
8e8be45e
PM
1172 goto checkwait;
1173 }
1174
ea6d962e 1175 // Do one boost-test interval.
8e8be45e 1176 endtime = oldstarttime + test_boost_duration * HZ;
3c80b402 1177 while (time_before(jiffies, endtime)) {
ea6d962e
PM
1178 // Has current GP gone too long?
1179 if (gp_initiated && !failed && !cur_ops->poll_gp_state(gp_state))
063f5a4d 1180 failed = rcu_torture_boost_failed(gp_state, &gp_state_time);
ea6d962e
PM
1181 // If we don't have a grace period in flight, start one.
1182 if (!gp_initiated || cur_ops->poll_gp_state(gp_state)) {
1183 gp_state = cur_ops->start_gp_poll();
1184 gp_initiated = true;
1185 gp_state_time = jiffies;
8e8be45e 1186 }
ea6d962e 1187 if (stutter_wait("rcu_torture_boost")) {
ab1b7880 1188 sched_set_fifo_low(current);
ea6d962e
PM
1189 // If the grace period already ended,
1190 // we don't know when that happened, so
1191 // start over.
1192 if (cur_ops->poll_gp_state(gp_state))
1193 gp_initiated = false;
1194 }
36970bb9 1195 if (torture_must_stop())
8e8be45e
PM
1196 goto checkwait;
1197 }
1198
ea6d962e
PM
1199 // In case the grace period extended beyond the end of the loop.
1200 if (gp_initiated && !failed && !cur_ops->poll_gp_state(gp_state))
063f5a4d 1201 rcu_torture_boost_failed(gp_state, &gp_state_time);
3b745c89 1202
8e8be45e
PM
1203 /*
1204 * Set the start time of the next test interval.
1205 * Yes, this is vulnerable to long delays, but such
1206 * delays simply cause a false negative for the next
1207 * interval. Besides, we are running at RT priority,
1208 * so delays should be relatively rare.
1209 */
a47f9f13 1210 while (oldstarttime == READ_ONCE(boost_starttime) && !kthread_should_stop()) {
8e8be45e 1211 if (mutex_trylock(&boost_mutex)) {
8c7ec02e 1212 if (oldstarttime == boost_starttime) {
a47f9f13
PM
1213 WRITE_ONCE(boost_starttime,
1214 jiffies + test_boost_interval * HZ);
8c7ec02e
PM
1215 n_rcu_torture_boosts++;
1216 }
8e8be45e
PM
1217 mutex_unlock(&boost_mutex);
1218 break;
1219 }
66bcb132 1220 schedule_timeout_uninterruptible(HZ / 20);
8e8be45e
PM
1221 }
1222
1223 /* Go do the stutter. */
ab1b7880
PM
1224checkwait: if (stutter_wait("rcu_torture_boost"))
1225 sched_set_fifo_low(current);
36970bb9 1226 } while (!torture_must_stop());
8e8be45e
PM
1227
1228 /* Clean up and exit. */
ea6d962e 1229 while (!kthread_should_stop()) {
7fafaac5 1230 torture_shutdown_absorb("rcu_torture_boost");
66bcb132 1231 schedule_timeout_uninterruptible(HZ / 20);
7fafaac5 1232 }
7fafaac5 1233 torture_kthread_stopping("rcu_torture_boost");
8e8be45e
PM
1234 return 0;
1235}
1236
bf66f18e
PM
1237/*
1238 * RCU torture force-quiescent-state kthread. Repeatedly induces
1239 * bursts of calls to force_quiescent_state(), increasing the probability
1240 * of occurrence of some important types of race conditions.
1241 */
1242static int
1243rcu_torture_fqs(void *arg)
1244{
1245 unsigned long fqs_resume_time;
1246 int fqs_burst_remaining;
ab1b7880 1247 int oldnice = task_nice(current);
bf66f18e 1248
5ccf60f2 1249 VERBOSE_TOROUT_STRING("rcu_torture_fqs task started");
bf66f18e
PM
1250 do {
1251 fqs_resume_time = jiffies + fqs_stutter * HZ;
3c80b402 1252 while (time_before(jiffies, fqs_resume_time) &&
93898fb1 1253 !kthread_should_stop()) {
66bcb132 1254 schedule_timeout_interruptible(HZ / 20);
bf66f18e
PM
1255 }
1256 fqs_burst_remaining = fqs_duration;
93898fb1
PM
1257 while (fqs_burst_remaining > 0 &&
1258 !kthread_should_stop()) {
bf66f18e
PM
1259 cur_ops->fqs();
1260 udelay(fqs_holdoff);
1261 fqs_burst_remaining -= fqs_holdoff;
1262 }
ab1b7880
PM
1263 if (stutter_wait("rcu_torture_fqs"))
1264 sched_set_normal(current, oldnice);
36970bb9 1265 } while (!torture_must_stop());
7fafaac5 1266 torture_kthread_stopping("rcu_torture_fqs");
bf66f18e
PM
1267 return 0;
1268}
1269
11d62f0f
PM
1270// Used by writers to randomly choose from the available grace-period primitives.
1271static int synctype[ARRAY_SIZE(rcu_torture_writer_state_names)] = { };
18fbf307
PM
1272static int nsynctypes;
1273
a241ec65 1274/*
18fbf307 1275 * Determine which grace-period primitives are available.
a241ec65 1276 */
18fbf307 1277static void rcu_torture_write_types(void)
a241ec65 1278{
b6fe4917 1279 bool gp_cond1 = gp_cond, gp_cond_exp1 = gp_cond_exp, gp_cond_full1 = gp_cond_full;
8df13f01
PM
1280 bool gp_cond_exp_full1 = gp_cond_exp_full, gp_exp1 = gp_exp, gp_poll_exp1 = gp_poll_exp;
1281 bool gp_poll_exp_full1 = gp_poll_exp_full, gp_normal1 = gp_normal, gp_poll1 = gp_poll;
1282 bool gp_poll_full1 = gp_poll_full, gp_sync1 = gp_sync;
dbdf65b1 1283
a48f3fad 1284 /* Initialize synctype[] array. If none set, take default. */
5d7801f2
PM
1285 if (!gp_cond1 &&
1286 !gp_cond_exp1 &&
1287 !gp_cond_full1 &&
1288 !gp_cond_exp_full1 &&
1289 !gp_exp1 &&
1290 !gp_poll_exp1 &&
1291 !gp_poll_exp_full1 &&
1292 !gp_normal1 &&
1293 !gp_poll1 &&
1294 !gp_poll_full1 &&
1295 !gp_sync1) {
1296 gp_cond1 = true;
1297 gp_cond_exp1 = true;
1298 gp_cond_full1 = true;
1299 gp_cond_exp_full1 = true;
1300 gp_exp1 = true;
1301 gp_poll_exp1 = true;
1302 gp_poll_exp_full1 = true;
1303 gp_normal1 = true;
1304 gp_poll1 = true;
1305 gp_poll_full1 = true;
1306 gp_sync1 = true;
1307 }
fd56f64b 1308 if (gp_cond1 && cur_ops->get_gp_state && cur_ops->cond_sync) {
a48f3fad 1309 synctype[nsynctypes++] = RTWS_COND_GET;
db0c1a8a 1310 pr_info("%s: Testing conditional GPs.\n", __func__);
fd56f64b 1311 } else if (gp_cond && (!cur_ops->get_gp_state || !cur_ops->cond_sync)) {
e0d31a34 1312 pr_alert("%s: gp_cond without primitives.\n", __func__);
db0c1a8a 1313 }
11d62f0f
PM
1314 if (gp_cond_exp1 && cur_ops->get_gp_state_exp && cur_ops->cond_sync_exp) {
1315 synctype[nsynctypes++] = RTWS_COND_GET_EXP;
1316 pr_info("%s: Testing conditional expedited GPs.\n", __func__);
1317 } else if (gp_cond_exp && (!cur_ops->get_gp_state_exp || !cur_ops->cond_sync_exp)) {
1318 pr_alert("%s: gp_cond_exp without primitives.\n", __func__);
1319 }
b6fe4917
PM
1320 if (gp_cond_full1 && cur_ops->get_gp_state && cur_ops->cond_sync_full) {
1321 synctype[nsynctypes++] = RTWS_COND_GET_FULL;
1322 pr_info("%s: Testing conditional full-state GPs.\n", __func__);
1323 } else if (gp_cond_full && (!cur_ops->get_gp_state || !cur_ops->cond_sync_full)) {
1324 pr_alert("%s: gp_cond_full without primitives.\n", __func__);
1325 }
8df13f01
PM
1326 if (gp_cond_exp_full1 && cur_ops->get_gp_state_exp && cur_ops->cond_sync_exp_full) {
1327 synctype[nsynctypes++] = RTWS_COND_GET_EXP_FULL;
1328 pr_info("%s: Testing conditional full-state expedited GPs.\n", __func__);
1329 } else if (gp_cond_exp_full &&
1330 (!cur_ops->get_gp_state_exp || !cur_ops->cond_sync_exp_full)) {
1331 pr_alert("%s: gp_cond_exp_full without primitives.\n", __func__);
1332 }
db0c1a8a 1333 if (gp_exp1 && cur_ops->exp_sync) {
a48f3fad 1334 synctype[nsynctypes++] = RTWS_EXP_SYNC;
db0c1a8a
PM
1335 pr_info("%s: Testing expedited GPs.\n", __func__);
1336 } else if (gp_exp && !cur_ops->exp_sync) {
e0d31a34 1337 pr_alert("%s: gp_exp without primitives.\n", __func__);
db0c1a8a
PM
1338 }
1339 if (gp_normal1 && cur_ops->deferred_free) {
a48f3fad 1340 synctype[nsynctypes++] = RTWS_DEF_FREE;
db0c1a8a
PM
1341 pr_info("%s: Testing asynchronous GPs.\n", __func__);
1342 } else if (gp_normal && !cur_ops->deferred_free) {
e0d31a34 1343 pr_alert("%s: gp_normal without primitives.\n", __func__);
db0c1a8a 1344 }
1324d95b
PM
1345 if (gp_poll1 && cur_ops->get_comp_state && cur_ops->same_gp_state &&
1346 cur_ops->start_gp_poll && cur_ops->poll_gp_state) {
0fd0548d
PM
1347 synctype[nsynctypes++] = RTWS_POLL_GET;
1348 pr_info("%s: Testing polling GPs.\n", __func__);
1349 } else if (gp_poll && (!cur_ops->start_gp_poll || !cur_ops->poll_gp_state)) {
1350 pr_alert("%s: gp_poll without primitives.\n", __func__);
1351 }
1d5ebc35
PM
1352 if (gp_poll_full1 && cur_ops->get_comp_state_full && cur_ops->same_gp_state_full
1353 && cur_ops->start_gp_poll_full && cur_ops->poll_gp_state_full) {
76ea3641
PM
1354 synctype[nsynctypes++] = RTWS_POLL_GET_FULL;
1355 pr_info("%s: Testing polling full-state GPs.\n", __func__);
1356 } else if (gp_poll_full && (!cur_ops->start_gp_poll_full || !cur_ops->poll_gp_state_full)) {
1357 pr_alert("%s: gp_poll_full without primitives.\n", __func__);
1358 }
11d62f0f
PM
1359 if (gp_poll_exp1 && cur_ops->start_gp_poll_exp && cur_ops->poll_gp_state_exp) {
1360 synctype[nsynctypes++] = RTWS_POLL_GET_EXP;
1361 pr_info("%s: Testing polling expedited GPs.\n", __func__);
1362 } else if (gp_poll_exp && (!cur_ops->start_gp_poll_exp || !cur_ops->poll_gp_state_exp)) {
1363 pr_alert("%s: gp_poll_exp without primitives.\n", __func__);
1364 }
6c502b14
PM
1365 if (gp_poll_exp_full1 && cur_ops->start_gp_poll_exp_full && cur_ops->poll_gp_state_full) {
1366 synctype[nsynctypes++] = RTWS_POLL_GET_EXP_FULL;
1367 pr_info("%s: Testing polling full-state expedited GPs.\n", __func__);
1368 } else if (gp_poll_exp_full &&
1369 (!cur_ops->start_gp_poll_exp_full || !cur_ops->poll_gp_state_full)) {
1370 pr_alert("%s: gp_poll_exp_full without primitives.\n", __func__);
1371 }
db0c1a8a 1372 if (gp_sync1 && cur_ops->sync) {
f0bf8fab 1373 synctype[nsynctypes++] = RTWS_SYNC;
db0c1a8a
PM
1374 pr_info("%s: Testing normal GPs.\n", __func__);
1375 } else if (gp_sync && !cur_ops->sync) {
e0d31a34 1376 pr_alert("%s: gp_sync without primitives.\n", __func__);
db0c1a8a 1377 }
20cee0b3 1378 pr_alert("%s: Testing %d update types.\n", __func__, nsynctypes);
282e06cc 1379 pr_info("%s: gp_cond_wi %d gp_cond_wi_exp %d gp_poll_wi %d gp_poll_wi_exp %d\n", __func__, gp_cond_wi, gp_cond_wi_exp, gp_poll_wi, gp_poll_wi_exp);
18fbf307
PM
1380}
1381
ccb42229
PM
1382/*
1383 * Do the specified rcu_torture_writer() synchronous grace period,
1384 * while also testing out the polled APIs. Note well that the single-CPU
1385 * grace-period optimizations must be accounted for.
1386 */
1387static void do_rtws_sync(struct torture_random_state *trsp, void (*sync)(void))
1388{
1389 unsigned long cookie;
1390 struct rcu_gp_oldstate cookie_full;
1391 bool dopoll;
1392 bool dopoll_full;
1393 unsigned long r = torture_random(trsp);
1394
1395 dopoll = cur_ops->get_gp_state && cur_ops->poll_gp_state && !(r & 0x300);
1396 dopoll_full = cur_ops->get_gp_state_full && cur_ops->poll_gp_state_full && !(r & 0xc00);
1397 if (dopoll || dopoll_full)
1398 cpus_read_lock();
1399 if (dopoll)
1400 cookie = cur_ops->get_gp_state();
1401 if (dopoll_full)
1402 cur_ops->get_gp_state_full(&cookie_full);
ed7d2f1a 1403 if (cur_ops->poll_need_2gp && cur_ops->poll_need_2gp(dopoll, dopoll_full))
ccb42229
PM
1404 sync();
1405 sync();
1406 WARN_ONCE(dopoll && !cur_ops->poll_gp_state(cookie),
1407 "%s: Cookie check 3 failed %pS() online %*pbl.",
1408 __func__, sync, cpumask_pr_args(cpu_online_mask));
1409 WARN_ONCE(dopoll_full && !cur_ops->poll_gp_state_full(&cookie_full),
1410 "%s: Cookie check 4 failed %pS() online %*pbl",
1411 __func__, sync, cpumask_pr_args(cpu_online_mask));
1412 if (dopoll || dopoll_full)
1413 cpus_read_unlock();
1414}
1415
18fbf307
PM
1416/*
1417 * RCU torture writer kthread. Repeatedly substitutes a new structure
1418 * for that pointed to by rcu_torture_current, freeing the old structure
1419 * after a series of grace periods (the "pipeline").
1420 */
1421static int
1422rcu_torture_writer(void *arg)
1423{
1424 bool boot_ended;
1425 bool can_expedite = !rcu_gp_is_expedited() && !rcu_gp_is_normal();
1426 unsigned long cookie;
91a967fd 1427 struct rcu_gp_oldstate cookie_full;
18fbf307
PM
1428 int expediting = 0;
1429 unsigned long gp_snap;
1324d95b 1430 unsigned long gp_snap1;
76ea3641 1431 struct rcu_gp_oldstate gp_snap_full;
1d5ebc35 1432 struct rcu_gp_oldstate gp_snap1_full;
18fbf307
PM
1433 int i;
1434 int idx;
1435 int oldnice = task_nice(current);
72ed29f6
PM
1436 struct rcu_gp_oldstate *rgo = NULL;
1437 int rgo_size = 0;
18fbf307
PM
1438 struct rcu_torture *rp;
1439 struct rcu_torture *old_rp;
1440 static DEFINE_TORTURE_RANDOM(rand);
fd2a749d 1441 unsigned long stallsdone = jiffies;
18fbf307 1442 bool stutter_waited;
72ed29f6
PM
1443 unsigned long *ulo = NULL;
1444 int ulo_size = 0;
18fbf307 1445
fd2a749d
PM
1446 // If a new stall test is added, this must be adjusted.
1447 if (stall_cpu_holdoff + stall_gp_kthread + stall_cpu)
58cb3210
PM
1448 stallsdone += (stall_cpu_holdoff + stall_gp_kthread + stall_cpu + 60) *
1449 HZ * (stall_cpu_repeat + 1);
18fbf307
PM
1450 VERBOSE_TOROUT_STRING("rcu_torture_writer task started");
1451 if (!can_expedite)
1452 pr_alert("%s" TORTURE_FLAG
1453 " GP expediting controlled from boot/sysfs for %s.\n",
1454 torture_type, cur_ops->name);
a48f3fad 1455 if (WARN_ONCE(nsynctypes == 0,
80dcee69 1456 "%s: No update-side primitives.\n", __func__)) {
f0bf8fab
PM
1457 /*
1458 * No updates primitives, so don't try updating.
1459 * The resulting test won't be testing much, hence the
1460 * above WARN_ONCE().
1461 */
a48f3fad
PM
1462 rcu_torture_writer_state = RTWS_STOPPING;
1463 torture_kthread_stopping("rcu_torture_writer");
80dcee69 1464 return 0;
a48f3fad 1465 }
72ed29f6
PM
1466 if (cur_ops->poll_active > 0) {
1467 ulo = kzalloc(cur_ops->poll_active * sizeof(ulo[0]), GFP_KERNEL);
1468 if (!WARN_ON(!ulo))
1469 ulo_size = cur_ops->poll_active;
1470 }
1471 if (cur_ops->poll_active_full > 0) {
1472 rgo = kzalloc(cur_ops->poll_active_full * sizeof(rgo[0]), GFP_KERNEL);
1473 if (!WARN_ON(!rgo))
1474 rgo_size = cur_ops->poll_active_full;
1475 }
a48f3fad 1476
a241ec65 1477 do {
ad0dc7f9 1478 rcu_torture_writer_state = RTWS_FIXED_DELAY;
1eba0ef9 1479 torture_hrtimeout_us(500, 1000, &rand);
a71fca58
PM
1480 rp = rcu_torture_alloc();
1481 if (rp == NULL)
a241ec65
PM
1482 continue;
1483 rp->rtort_pipe_count = 0;
c507e195 1484 ASSERT_EXCLUSIVE_WRITER(rp->rtort_pipe_count);
ad0dc7f9 1485 rcu_torture_writer_state = RTWS_DELAY;
51b1130e 1486 udelay(torture_random(&rand) & 0x3ff);
ad0dc7f9 1487 rcu_torture_writer_state = RTWS_REPLACE;
0ddea0ea
PM
1488 old_rp = rcu_dereference_check(rcu_torture_current,
1489 current == writer_task);
996417d2 1490 rp->rtort_mbtest = 1;
a241ec65 1491 rcu_assign_pointer(rcu_torture_current, rp);
9b2619af 1492 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
c8e5b163 1493 if (old_rp) {
a241ec65
PM
1494 i = old_rp->rtort_pipe_count;
1495 if (i > RCU_TORTURE_PIPE_LEN)
1496 i = RCU_TORTURE_PIPE_LEN;
1497 atomic_inc(&rcu_torture_wcount[i]);
20248910
PM
1498 WRITE_ONCE(old_rp->rtort_pipe_count,
1499 old_rp->rtort_pipe_count + 1);
c507e195 1500 ASSERT_EXCLUSIVE_WRITER(old_rp->rtort_pipe_count);
d594231a
PM
1501
1502 // Make sure readers block polled grace periods.
0fd0548d
PM
1503 if (cur_ops->get_gp_state && cur_ops->poll_gp_state) {
1504 idx = cur_ops->readlock();
1505 cookie = cur_ops->get_gp_state();
37d6ade3 1506 WARN_ONCE(cur_ops->poll_gp_state(cookie),
0fd0548d
PM
1507 "%s: Cookie check 1 failed %s(%d) %lu->%lu\n",
1508 __func__,
1509 rcu_torture_writer_state_getname(),
1510 rcu_torture_writer_state,
1511 cookie, cur_ops->get_gp_state());
cf3b1501
PM
1512 if (cur_ops->get_comp_state) {
1513 cookie = cur_ops->get_comp_state();
d0eac20f
PM
1514 WARN_ON_ONCE(!cur_ops->poll_gp_state(cookie));
1515 }
0fd0548d
PM
1516 cur_ops->readunlock(idx);
1517 }
d594231a
PM
1518 if (cur_ops->get_gp_state_full && cur_ops->poll_gp_state_full) {
1519 idx = cur_ops->readlock();
1520 cur_ops->get_gp_state_full(&cookie_full);
1521 WARN_ONCE(cur_ops->poll_gp_state_full(&cookie_full),
1522 "%s: Cookie check 5 failed %s(%d) online %*pbl\n",
1523 __func__,
1524 rcu_torture_writer_state_getname(),
1525 rcu_torture_writer_state,
1526 cpumask_pr_args(cpu_online_mask));
cf3b1501
PM
1527 if (cur_ops->get_comp_state_full) {
1528 cur_ops->get_comp_state_full(&cookie_full);
d594231a
PM
1529 WARN_ON_ONCE(!cur_ops->poll_gp_state_full(&cookie_full));
1530 }
1531 cur_ops->readunlock(idx);
91a967fd 1532 }
a48f3fad
PM
1533 switch (synctype[torture_random(&rand) % nsynctypes]) {
1534 case RTWS_DEF_FREE:
ad0dc7f9 1535 rcu_torture_writer_state = RTWS_DEF_FREE;
2ec1f2d9 1536 cur_ops->deferred_free(old_rp);
a48f3fad
PM
1537 break;
1538 case RTWS_EXP_SYNC:
ad0dc7f9 1539 rcu_torture_writer_state = RTWS_EXP_SYNC;
ccb42229 1540 do_rtws_sync(&rand, cur_ops->exp_sync);
a48f3fad
PM
1541 rcu_torture_pipe_update(old_rp);
1542 break;
1543 case RTWS_COND_GET:
1544 rcu_torture_writer_state = RTWS_COND_GET;
fd56f64b 1545 gp_snap = cur_ops->get_gp_state();
282e06cc
PM
1546 torture_hrtimeout_us(torture_random(&rand) % gp_cond_wi,
1547 1000, &rand);
a48f3fad
PM
1548 rcu_torture_writer_state = RTWS_COND_SYNC;
1549 cur_ops->cond_sync(gp_snap);
1550 rcu_torture_pipe_update(old_rp);
1551 break;
11d62f0f
PM
1552 case RTWS_COND_GET_EXP:
1553 rcu_torture_writer_state = RTWS_COND_GET_EXP;
1554 gp_snap = cur_ops->get_gp_state_exp();
282e06cc
PM
1555 torture_hrtimeout_us(torture_random(&rand) % gp_cond_wi_exp,
1556 1000, &rand);
11d62f0f
PM
1557 rcu_torture_writer_state = RTWS_COND_SYNC_EXP;
1558 cur_ops->cond_sync_exp(gp_snap);
1559 rcu_torture_pipe_update(old_rp);
1560 break;
b6fe4917
PM
1561 case RTWS_COND_GET_FULL:
1562 rcu_torture_writer_state = RTWS_COND_GET_FULL;
1563 cur_ops->get_gp_state_full(&gp_snap_full);
282e06cc
PM
1564 torture_hrtimeout_us(torture_random(&rand) % gp_cond_wi,
1565 1000, &rand);
b6fe4917
PM
1566 rcu_torture_writer_state = RTWS_COND_SYNC_FULL;
1567 cur_ops->cond_sync_full(&gp_snap_full);
1568 rcu_torture_pipe_update(old_rp);
1569 break;
8df13f01
PM
1570 case RTWS_COND_GET_EXP_FULL:
1571 rcu_torture_writer_state = RTWS_COND_GET_EXP_FULL;
1572 cur_ops->get_gp_state_full(&gp_snap_full);
282e06cc
PM
1573 torture_hrtimeout_us(torture_random(&rand) % gp_cond_wi_exp,
1574 1000, &rand);
8df13f01
PM
1575 rcu_torture_writer_state = RTWS_COND_SYNC_EXP_FULL;
1576 cur_ops->cond_sync_exp_full(&gp_snap_full);
1577 rcu_torture_pipe_update(old_rp);
1578 break;
0fd0548d
PM
1579 case RTWS_POLL_GET:
1580 rcu_torture_writer_state = RTWS_POLL_GET;
72ed29f6 1581 for (i = 0; i < ulo_size; i++)
1324d95b 1582 ulo[i] = cur_ops->get_comp_state();
0fd0548d
PM
1583 gp_snap = cur_ops->start_gp_poll();
1584 rcu_torture_writer_state = RTWS_POLL_WAIT;
1324d95b
PM
1585 while (!cur_ops->poll_gp_state(gp_snap)) {
1586 gp_snap1 = cur_ops->get_gp_state();
72ed29f6 1587 for (i = 0; i < ulo_size; i++)
1324d95b
PM
1588 if (cur_ops->poll_gp_state(ulo[i]) ||
1589 cur_ops->same_gp_state(ulo[i], gp_snap1)) {
1590 ulo[i] = gp_snap1;
1591 break;
1592 }
72ed29f6 1593 WARN_ON_ONCE(ulo_size > 0 && i >= ulo_size);
282e06cc
PM
1594 torture_hrtimeout_us(torture_random(&rand) % gp_poll_wi,
1595 1000, &rand);
1324d95b 1596 }
0fd0548d
PM
1597 rcu_torture_pipe_update(old_rp);
1598 break;
76ea3641
PM
1599 case RTWS_POLL_GET_FULL:
1600 rcu_torture_writer_state = RTWS_POLL_GET_FULL;
72ed29f6 1601 for (i = 0; i < rgo_size; i++)
1d5ebc35 1602 cur_ops->get_comp_state_full(&rgo[i]);
76ea3641
PM
1603 cur_ops->start_gp_poll_full(&gp_snap_full);
1604 rcu_torture_writer_state = RTWS_POLL_WAIT_FULL;
1d5ebc35
PM
1605 while (!cur_ops->poll_gp_state_full(&gp_snap_full)) {
1606 cur_ops->get_gp_state_full(&gp_snap1_full);
72ed29f6 1607 for (i = 0; i < rgo_size; i++)
1d5ebc35
PM
1608 if (cur_ops->poll_gp_state_full(&rgo[i]) ||
1609 cur_ops->same_gp_state_full(&rgo[i],
1610 &gp_snap1_full)) {
1611 rgo[i] = gp_snap1_full;
1612 break;
1613 }
72ed29f6 1614 WARN_ON_ONCE(rgo_size > 0 && i >= rgo_size);
282e06cc
PM
1615 torture_hrtimeout_us(torture_random(&rand) % gp_poll_wi,
1616 1000, &rand);
1d5ebc35 1617 }
76ea3641
PM
1618 rcu_torture_pipe_update(old_rp);
1619 break;
11d62f0f
PM
1620 case RTWS_POLL_GET_EXP:
1621 rcu_torture_writer_state = RTWS_POLL_GET_EXP;
1622 gp_snap = cur_ops->start_gp_poll_exp();
1623 rcu_torture_writer_state = RTWS_POLL_WAIT_EXP;
1624 while (!cur_ops->poll_gp_state_exp(gp_snap))
282e06cc
PM
1625 torture_hrtimeout_us(torture_random(&rand) % gp_poll_wi_exp,
1626 1000, &rand);
11d62f0f
PM
1627 rcu_torture_pipe_update(old_rp);
1628 break;
6c502b14
PM
1629 case RTWS_POLL_GET_EXP_FULL:
1630 rcu_torture_writer_state = RTWS_POLL_GET_EXP_FULL;
1631 cur_ops->start_gp_poll_exp_full(&gp_snap_full);
1632 rcu_torture_writer_state = RTWS_POLL_WAIT_EXP_FULL;
1633 while (!cur_ops->poll_gp_state_full(&gp_snap_full))
282e06cc
PM
1634 torture_hrtimeout_us(torture_random(&rand) % gp_poll_wi_exp,
1635 1000, &rand);
6c502b14
PM
1636 rcu_torture_pipe_update(old_rp);
1637 break;
f0bf8fab
PM
1638 case RTWS_SYNC:
1639 rcu_torture_writer_state = RTWS_SYNC;
ccb42229 1640 do_rtws_sync(&rand, cur_ops->sync);
f0bf8fab
PM
1641 rcu_torture_pipe_update(old_rp);
1642 break;
a48f3fad
PM
1643 default:
1644 WARN_ON_ONCE(1);
1645 break;
2ec1f2d9 1646 }
a241ec65 1647 }
1b27291b
PM
1648 WRITE_ONCE(rcu_torture_current_version,
1649 rcu_torture_current_version + 1);
4bb3c5f4
PM
1650 /* Cycle through nesting levels of rcu_expedite_gp() calls. */
1651 if (can_expedite &&
1652 !(torture_random(&rand) & 0xff & (!!expediting - 1))) {
1653 WARN_ON_ONCE(expediting == 0 && rcu_gp_is_expedited());
1654 if (expediting >= 0)
1655 rcu_expedite_gp();
1656 else
1657 rcu_unexpedite_gp();
1658 if (++expediting > 3)
1659 expediting = -expediting;
f7c0e6ad
PM
1660 } else if (!can_expedite) { /* Disabled during boot, recheck. */
1661 can_expedite = !rcu_gp_is_expedited() &&
1662 !rcu_gp_is_normal();
4bb3c5f4 1663 }
ad0dc7f9 1664 rcu_torture_writer_state = RTWS_STUTTER;
12a910e3 1665 boot_ended = rcu_inkernel_boot_has_ended();
ab1b7880
PM
1666 stutter_waited = stutter_wait("rcu_torture_writer");
1667 if (stutter_waited &&
e22ef8df 1668 !atomic_read(&rcu_fwd_cb_nodelay) &&
3432d765 1669 !cur_ops->slow_gps &&
59ee0326 1670 !torture_must_stop() &&
fd2a749d
PM
1671 boot_ended &&
1672 time_after(jiffies, stallsdone))
474e59b4 1673 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++)
e8516c64 1674 if (list_empty(&rcu_tortures[i].rtort_free) &&
fd2a749d 1675 rcu_access_pointer(rcu_torture_current) != &rcu_tortures[i]) {
14c0017c 1676 tracing_off();
e38bf06d
Z
1677 if (cur_ops->gp_kthread_dbg)
1678 cur_ops->gp_kthread_dbg();
e8516c64 1679 WARN(1, "%s: rtort_pipe_count: %d\n", __func__, rcu_tortures[i].rtort_pipe_count);
14c0017c 1680 rcu_ftrace_dump(DUMP_ALL);
34aa34b8 1681 }
ab1b7880
PM
1682 if (stutter_waited)
1683 sched_set_normal(current, oldnice);
36970bb9 1684 } while (!torture_must_stop());
cae7cc6b 1685 rcu_torture_current = NULL; // Let stats task know that we are done.
4bb3c5f4
PM
1686 /* Reset expediting back to unexpedited. */
1687 if (expediting > 0)
1688 expediting = -expediting;
1689 while (can_expedite && expediting++ < 0)
1690 rcu_unexpedite_gp();
1691 WARN_ON_ONCE(can_expedite && rcu_gp_is_expedited());
f7c0e6ad
PM
1692 if (!can_expedite)
1693 pr_alert("%s" TORTURE_FLAG
1694 " Dynamic grace-period expediting was disabled.\n",
1695 torture_type);
72ed29f6
PM
1696 kfree(ulo);
1697 kfree(rgo);
ad0dc7f9 1698 rcu_torture_writer_state = RTWS_STOPPING;
7fafaac5 1699 torture_kthread_stopping("rcu_torture_writer");
a241ec65
PM
1700 return 0;
1701}
1702
b772e1dd
JT
1703/*
1704 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
1705 * delay between calls.
1706 */
1707static int
1708rcu_torture_fakewriter(void *arg)
1709{
682189a3 1710 unsigned long gp_snap;
76ea3641 1711 struct rcu_gp_oldstate gp_snap_full;
51b1130e 1712 DEFINE_TORTURE_RANDOM(rand);
b772e1dd 1713
5ccf60f2 1714 VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
d277d868 1715 set_user_nice(current, MAX_NICE);
b772e1dd 1716
39b3cab9
DV
1717 if (WARN_ONCE(nsynctypes == 0,
1718 "%s: No update-side primitives.\n", __func__)) {
1719 /*
1720 * No updates primitives, so don't try updating.
1721 * The resulting test won't be testing much, hence the
1722 * above WARN_ONCE().
1723 */
1724 torture_kthread_stopping("rcu_torture_fakewriter");
1725 return 0;
1726 }
1727
b772e1dd 1728 do {
1eba0ef9 1729 torture_hrtimeout_jiffies(torture_random(&rand) % 10, &rand);
72472a02 1730 if (cur_ops->cb_barrier != NULL &&
51b1130e 1731 torture_random(&rand) % (nfakewriters * 8) == 0) {
72472a02 1732 cur_ops->cb_barrier();
682189a3
PM
1733 } else {
1734 switch (synctype[torture_random(&rand) % nsynctypes]) {
1735 case RTWS_DEF_FREE:
1736 break;
1737 case RTWS_EXP_SYNC:
2ec1f2d9 1738 cur_ops->exp_sync();
682189a3
PM
1739 break;
1740 case RTWS_COND_GET:
1741 gp_snap = cur_ops->get_gp_state();
ea31fd9c 1742 torture_hrtimeout_jiffies(torture_random(&rand) % 16, &rand);
682189a3
PM
1743 cur_ops->cond_sync(gp_snap);
1744 break;
11d62f0f
PM
1745 case RTWS_COND_GET_EXP:
1746 gp_snap = cur_ops->get_gp_state_exp();
1747 torture_hrtimeout_jiffies(torture_random(&rand) % 16, &rand);
1748 cur_ops->cond_sync_exp(gp_snap);
1749 break;
b6fe4917
PM
1750 case RTWS_COND_GET_FULL:
1751 cur_ops->get_gp_state_full(&gp_snap_full);
1752 torture_hrtimeout_jiffies(torture_random(&rand) % 16, &rand);
1753 cur_ops->cond_sync_full(&gp_snap_full);
1754 break;
8df13f01
PM
1755 case RTWS_COND_GET_EXP_FULL:
1756 cur_ops->get_gp_state_full(&gp_snap_full);
1757 torture_hrtimeout_jiffies(torture_random(&rand) % 16, &rand);
1758 cur_ops->cond_sync_exp_full(&gp_snap_full);
1759 break;
682189a3 1760 case RTWS_POLL_GET:
de2ad0e7
PM
1761 if (cur_ops->start_poll_irqsoff)
1762 local_irq_disable();
682189a3 1763 gp_snap = cur_ops->start_gp_poll();
de2ad0e7
PM
1764 if (cur_ops->start_poll_irqsoff)
1765 local_irq_enable();
682189a3 1766 while (!cur_ops->poll_gp_state(gp_snap)) {
ea31fd9c
PM
1767 torture_hrtimeout_jiffies(torture_random(&rand) % 16,
1768 &rand);
682189a3
PM
1769 }
1770 break;
76ea3641 1771 case RTWS_POLL_GET_FULL:
de2ad0e7
PM
1772 if (cur_ops->start_poll_irqsoff)
1773 local_irq_disable();
76ea3641 1774 cur_ops->start_gp_poll_full(&gp_snap_full);
de2ad0e7
PM
1775 if (cur_ops->start_poll_irqsoff)
1776 local_irq_enable();
76ea3641
PM
1777 while (!cur_ops->poll_gp_state_full(&gp_snap_full)) {
1778 torture_hrtimeout_jiffies(torture_random(&rand) % 16,
1779 &rand);
1780 }
1781 break;
11d62f0f
PM
1782 case RTWS_POLL_GET_EXP:
1783 gp_snap = cur_ops->start_gp_poll_exp();
1784 while (!cur_ops->poll_gp_state_exp(gp_snap)) {
1785 torture_hrtimeout_jiffies(torture_random(&rand) % 16,
1786 &rand);
1787 }
1788 break;
6c502b14
PM
1789 case RTWS_POLL_GET_EXP_FULL:
1790 cur_ops->start_gp_poll_exp_full(&gp_snap_full);
1791 while (!cur_ops->poll_gp_state_full(&gp_snap_full)) {
1792 torture_hrtimeout_jiffies(torture_random(&rand) % 16,
1793 &rand);
1794 }
1795 break;
682189a3
PM
1796 case RTWS_SYNC:
1797 cur_ops->sync();
1798 break;
1799 default:
1800 WARN_ON_ONCE(1);
1801 break;
1802 }
2ec1f2d9 1803 }
628edaa5 1804 stutter_wait("rcu_torture_fakewriter");
36970bb9 1805 } while (!torture_must_stop());
b772e1dd 1806
7fafaac5 1807 torture_kthread_stopping("rcu_torture_fakewriter");
b772e1dd
JT
1808 return 0;
1809}
1810
f34c8585
PM
1811static void rcu_torture_timer_cb(struct rcu_head *rhp)
1812{
1813 kfree(rhp);
1814}
1815
00504537
PM
1816// Set up and carry out testing of RCU's global memory ordering
1817static void rcu_torture_reader_do_mbchk(long myid, struct rcu_torture *rtp,
1818 struct torture_random_state *trsp)
1819{
1820 unsigned long loops;
1afb95fe 1821 int noc = torture_num_online_cpus();
00504537
PM
1822 int rdrchked;
1823 int rdrchker;
1824 struct rcu_torture_reader_check *rtrcp; // Me.
1825 struct rcu_torture_reader_check *rtrcp_assigner; // Assigned us to do checking.
1826 struct rcu_torture_reader_check *rtrcp_chked; // Reader being checked.
1827 struct rcu_torture_reader_check *rtrcp_chker; // Reader doing checking when not me.
1828
1829 if (myid < 0)
1830 return; // Don't try this from timer handlers.
1831
1832 // Increment my counter.
1833 rtrcp = &rcu_torture_reader_mbchk[myid];
1834 WRITE_ONCE(rtrcp->rtc_myloops, rtrcp->rtc_myloops + 1);
1835
1836 // Attempt to assign someone else some checking work.
1837 rdrchked = torture_random(trsp) % nrealreaders;
1838 rtrcp_chked = &rcu_torture_reader_mbchk[rdrchked];
1839 rdrchker = torture_random(trsp) % nrealreaders;
1840 rtrcp_chker = &rcu_torture_reader_mbchk[rdrchker];
1841 if (rdrchked != myid && rdrchked != rdrchker && noc >= rdrchked && noc >= rdrchker &&
1842 smp_load_acquire(&rtrcp->rtc_chkrdr) < 0 && // Pairs with smp_store_release below.
1843 !READ_ONCE(rtp->rtort_chkp) &&
1844 !smp_load_acquire(&rtrcp_chker->rtc_assigner)) { // Pairs with smp_store_release below.
1845 rtrcp->rtc_chkloops = READ_ONCE(rtrcp_chked->rtc_myloops);
1846 WARN_ON_ONCE(rtrcp->rtc_chkrdr >= 0);
1847 rtrcp->rtc_chkrdr = rdrchked;
1848 WARN_ON_ONCE(rtrcp->rtc_ready); // This gets set after the grace period ends.
1849 if (cmpxchg_relaxed(&rtrcp_chker->rtc_assigner, NULL, rtrcp) ||
1850 cmpxchg_relaxed(&rtp->rtort_chkp, NULL, rtrcp))
1851 (void)cmpxchg_relaxed(&rtrcp_chker->rtc_assigner, rtrcp, NULL); // Back out.
1852 }
1853
1854 // If assigned some completed work, do it!
1855 rtrcp_assigner = READ_ONCE(rtrcp->rtc_assigner);
1856 if (!rtrcp_assigner || !smp_load_acquire(&rtrcp_assigner->rtc_ready))
1857 return; // No work or work not yet ready.
1858 rdrchked = rtrcp_assigner->rtc_chkrdr;
1859 if (WARN_ON_ONCE(rdrchked < 0))
1860 return;
1861 rtrcp_chked = &rcu_torture_reader_mbchk[rdrchked];
1862 loops = READ_ONCE(rtrcp_chked->rtc_myloops);
1863 atomic_inc(&n_rcu_torture_mbchk_tries);
1864 if (ULONG_CMP_LT(loops, rtrcp_assigner->rtc_chkloops))
1865 atomic_inc(&n_rcu_torture_mbchk_fail);
1866 rtrcp_assigner->rtc_chkloops = loops + ULONG_MAX / 2;
1867 rtrcp_assigner->rtc_ready = 0;
1868 smp_store_release(&rtrcp->rtc_assigner, NULL); // Someone else can assign us work.
1869 smp_store_release(&rtrcp_assigner->rtc_chkrdr, -1); // Assigner can again assign.
1870}
1871
ec9d6356 1872// Verify the specified RCUTORTURE_RDR* state.
c31569ee 1873#define ROEC_ARGS "%s %s: Current %#x To add %#x To remove %#x preempt_count() %#x\n", __func__, s, curstate, new, old, preempt_count()
ec9d6356
PM
1874static void rcutorture_one_extend_check(char *s, int curstate, int new, int old, bool insoftirq)
1875{
1876 if (!IS_ENABLED(CONFIG_RCU_TORTURE_TEST_CHK_RDR_STATE))
1877 return;
1878
1879 WARN_ONCE(!(curstate & RCUTORTURE_RDR_IRQ) && irqs_disabled(), ROEC_ARGS);
1880 WARN_ONCE((curstate & RCUTORTURE_RDR_IRQ) && !irqs_disabled(), ROEC_ARGS);
1881
1882 // If CONFIG_PREEMPT_COUNT=n, further checks are unreliable.
1883 if (!IS_ENABLED(CONFIG_PREEMPT_COUNT))
1884 return;
1885
1886 WARN_ONCE((curstate & (RCUTORTURE_RDR_BH | RCUTORTURE_RDR_RBH)) &&
1887 !(preempt_count() & SOFTIRQ_MASK), ROEC_ARGS);
1888 WARN_ONCE((curstate & (RCUTORTURE_RDR_PREEMPT | RCUTORTURE_RDR_SCHED)) &&
1889 !(preempt_count() & PREEMPT_MASK), ROEC_ARGS);
1890 WARN_ONCE(cur_ops->readlock_nesting &&
1891 (curstate & (RCUTORTURE_RDR_RCU_1 | RCUTORTURE_RDR_RCU_2)) &&
1892 cur_ops->readlock_nesting() == 0, ROEC_ARGS);
1893
1894 // Timer handlers have all sorts of stuff disabled, so ignore
1895 // unintended disabling.
1896 if (insoftirq)
1897 return;
1898
1899 WARN_ONCE(cur_ops->extendables &&
1900 !(curstate & (RCUTORTURE_RDR_BH | RCUTORTURE_RDR_RBH)) &&
1901 (preempt_count() & SOFTIRQ_MASK), ROEC_ARGS);
1902 WARN_ONCE(cur_ops->extendables &&
1903 !(curstate & (RCUTORTURE_RDR_PREEMPT | RCUTORTURE_RDR_SCHED)) &&
1904 (preempt_count() & PREEMPT_MASK), ROEC_ARGS);
1905 WARN_ONCE(cur_ops->readlock_nesting &&
1906 !(curstate & (RCUTORTURE_RDR_RCU_1 | RCUTORTURE_RDR_RCU_2)) &&
1907 cur_ops->readlock_nesting() > 0, ROEC_ARGS);
1908}
1909
0729fbf3 1910/*
2397d072
PM
1911 * Do one extension of an RCU read-side critical section using the
1912 * current reader state in readstate (set to zero for initial entry
1913 * to extended critical section), set the new state as specified by
1914 * newstate (set to zero for final exit from extended critical section),
1915 * and random-number-generator state in trsp. If this is neither the
1916 * beginning or end of the critical section and if there was actually a
1917 * change, do a ->read_delay().
0729fbf3 1918 */
ec9d6356 1919static void rcutorture_one_extend(int *readstate, int newstate, bool insoftirq,
c116dba6
PM
1920 struct torture_random_state *trsp,
1921 struct rt_read_seg *rtrsp)
2397d072 1922{
885a6f47 1923 bool first;
52b1fc3f 1924 unsigned long flags;
1c3d5398
PM
1925 int idxnew1 = -1;
1926 int idxnew2 = -1;
1927 int idxold1 = *readstate;
1928 int idxold2 = idxold1;
2397d072
PM
1929 int statesnew = ~*readstate & newstate;
1930 int statesold = *readstate & ~newstate;
1931
885a6f47 1932 first = idxold1 == 0;
1c3d5398 1933 WARN_ON_ONCE(idxold2 < 0);
37a1decb 1934 WARN_ON_ONCE(idxold2 & ~RCUTORTURE_RDR_ALLBITS);
ec9d6356 1935 rcutorture_one_extend_check("before change", idxold1, statesnew, statesold, insoftirq);
c116dba6 1936 rtrsp->rt_readstate = newstate;
2397d072
PM
1937
1938 /* First, put new protection in place to avoid critical-section gap. */
1939 if (statesnew & RCUTORTURE_RDR_BH)
1940 local_bh_disable();
71921a96
SW
1941 if (statesnew & RCUTORTURE_RDR_RBH)
1942 rcu_read_lock_bh();
2397d072
PM
1943 if (statesnew & RCUTORTURE_RDR_IRQ)
1944 local_irq_disable();
1945 if (statesnew & RCUTORTURE_RDR_PREEMPT)
1946 preempt_disable();
2ceebc03
PM
1947 if (statesnew & RCUTORTURE_RDR_SCHED)
1948 rcu_read_lock_sched();
1c3d5398 1949 if (statesnew & RCUTORTURE_RDR_RCU_1)
37a1decb 1950 idxnew1 = (cur_ops->readlock() << RCUTORTURE_RDR_SHIFT_1) & RCUTORTURE_RDR_MASK_1;
1c3d5398 1951 if (statesnew & RCUTORTURE_RDR_RCU_2)
37a1decb 1952 idxnew2 = (cur_ops->readlock() << RCUTORTURE_RDR_SHIFT_2) & RCUTORTURE_RDR_MASK_2;
2397d072 1953
ec9d6356
PM
1954 // Complain unless both the old and the new protection is in place.
1955 rcutorture_one_extend_check("during change",
1956 idxold1 | statesnew, statesnew, statesold, insoftirq);
1957
885a6f47
PM
1958 // Sample CPU under both sets of protections to reduce confusion.
1959 if (IS_ENABLED(CONFIG_RCU_TORTURE_TEST_LOG_CPU)) {
1960 int cpu = raw_smp_processor_id();
1961 rtrsp->rt_cpu = cpu;
223f16b8 1962 if (!first) {
885a6f47 1963 rtrsp[-1].rt_end_cpu = cpu;
223f16b8
PM
1964 if (cur_ops->reader_blocked)
1965 rtrsp[-1].rt_preempted = cur_ops->reader_blocked();
1966 }
885a6f47
PM
1967 }
1968
71921a96
SW
1969 /*
1970 * Next, remove old protection, in decreasing order of strength
1971 * to avoid unlock paths that aren't safe in the stronger
1972 * context. Namely: BH can not be enabled with disabled interrupts.
1973 * Additionally PREEMPT_RT requires that BH is enabled in preemptible
1974 * context.
1975 */
2397d072
PM
1976 if (statesold & RCUTORTURE_RDR_IRQ)
1977 local_irq_enable();
2397d072
PM
1978 if (statesold & RCUTORTURE_RDR_PREEMPT)
1979 preempt_enable();
2ceebc03
PM
1980 if (statesold & RCUTORTURE_RDR_SCHED)
1981 rcu_read_unlock_sched();
71921a96
SW
1982 if (statesold & RCUTORTURE_RDR_BH)
1983 local_bh_enable();
1984 if (statesold & RCUTORTURE_RDR_RBH)
1985 rcu_read_unlock_bh();
1c3d5398 1986 if (statesold & RCUTORTURE_RDR_RCU_2) {
37a1decb 1987 cur_ops->readunlock((idxold2 & RCUTORTURE_RDR_MASK_2) >> RCUTORTURE_RDR_SHIFT_2);
1c3d5398
PM
1988 WARN_ON_ONCE(idxnew2 != -1);
1989 idxold2 = 0;
1990 }
1991 if (statesold & RCUTORTURE_RDR_RCU_1) {
340170fe 1992 bool lockit;
52b1fc3f 1993
340170fe 1994 lockit = !cur_ops->no_pi_lock && !statesnew && !(torture_random(trsp) & 0xffff);
52b1fc3f
PM
1995 if (lockit)
1996 raw_spin_lock_irqsave(&current->pi_lock, flags);
37a1decb 1997 cur_ops->readunlock((idxold1 & RCUTORTURE_RDR_MASK_1) >> RCUTORTURE_RDR_SHIFT_1);
1c3d5398
PM
1998 WARN_ON_ONCE(idxnew1 != -1);
1999 idxold1 = 0;
52b1fc3f
PM
2000 if (lockit)
2001 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
2002 }
2397d072
PM
2003
2004 /* Delay if neither beginning nor end and there was a change. */
2005 if ((statesnew || statesold) && *readstate && newstate)
c116dba6 2006 cur_ops->read_delay(trsp, rtrsp);
2397d072
PM
2007
2008 /* Update the reader state. */
1c3d5398
PM
2009 if (idxnew1 == -1)
2010 idxnew1 = idxold1 & RCUTORTURE_RDR_MASK_1;
2011 WARN_ON_ONCE(idxnew1 < 0);
1c3d5398
PM
2012 if (idxnew2 == -1)
2013 idxnew2 = idxold2 & RCUTORTURE_RDR_MASK_2;
2014 WARN_ON_ONCE(idxnew2 < 0);
1c3d5398
PM
2015 *readstate = idxnew1 | idxnew2 | newstate;
2016 WARN_ON_ONCE(*readstate < 0);
37a1decb
PM
2017 if (WARN_ON_ONCE(*readstate & ~RCUTORTURE_RDR_ALLBITS))
2018 pr_info("Unexpected readstate value of %#x\n", *readstate);
ec9d6356 2019 rcutorture_one_extend_check("after change", *readstate, statesnew, statesold, insoftirq);
2397d072
PM
2020}
2021
2022/* Return the biggest extendables mask given current RCU and boot parameters. */
2023static int rcutorture_extend_mask_max(void)
2024{
2025 int mask;
2026
2027 WARN_ON_ONCE(extendables & ~RCUTORTURE_MAX_EXTEND);
2028 mask = extendables & RCUTORTURE_MAX_EXTEND & cur_ops->extendables;
1c3d5398 2029 mask = mask | RCUTORTURE_RDR_RCU_1 | RCUTORTURE_RDR_RCU_2;
2397d072
PM
2030 return mask;
2031}
2032
2033/* Return a random protection state mask, but with at least one bit set. */
2034static int
2035rcutorture_extend_mask(int oldmask, struct torture_random_state *trsp)
2036{
2037 int mask = rcutorture_extend_mask_max();
bc19e86e 2038 unsigned long randmask1 = torture_random(trsp);
c116dba6 2039 unsigned long randmask2 = randmask1 >> 3;
71921a96
SW
2040 unsigned long preempts = RCUTORTURE_RDR_PREEMPT | RCUTORTURE_RDR_SCHED;
2041 unsigned long preempts_irq = preempts | RCUTORTURE_RDR_IRQ;
2042 unsigned long bhs = RCUTORTURE_RDR_BH | RCUTORTURE_RDR_RBH;
2397d072 2043
37a1decb 2044 WARN_ON_ONCE(mask >> RCUTORTURE_RDR_SHIFT_1); // Can't have reader idx bits.
a3b0e1e5 2045 /* Mostly only one bit (need preemption!), sometimes lots of bits. */
c116dba6 2046 if (!(randmask1 & 0x7))
bf1bef50
PM
2047 mask = mask & randmask2;
2048 else
2049 mask = mask & (1 << (randmask2 % RCUTORTURE_RDR_NBITS));
71921a96 2050
1c3d5398
PM
2051 // Can't have nested RCU reader without outer RCU reader.
2052 if (!(mask & RCUTORTURE_RDR_RCU_1) && (mask & RCUTORTURE_RDR_RCU_2)) {
2053 if (oldmask & RCUTORTURE_RDR_RCU_1)
2054 mask &= ~RCUTORTURE_RDR_RCU_2;
2055 else
2056 mask |= RCUTORTURE_RDR_RCU_1;
2057 }
2058
71921a96
SW
2059 /*
2060 * Can't enable bh w/irq disabled.
2061 */
2062 if (mask & RCUTORTURE_RDR_IRQ)
2063 mask |= oldmask & bhs;
2064
2065 /*
2066 * Ideally these sequences would be detected in debug builds
2067 * (regardless of RT), but until then don't stop testing
2068 * them on non-RT.
2069 */
2070 if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
2071 /* Can't modify BH in atomic context */
2072 if (oldmask & preempts_irq)
2073 mask &= ~bhs;
2074 if ((oldmask | mask) & preempts_irq)
2075 mask |= oldmask & bhs;
2076 }
2077
1c3d5398 2078 return mask ?: RCUTORTURE_RDR_RCU_1;
2397d072
PM
2079}
2080
2081/*
2082 * Do a randomly selected number of extensions of an existing RCU read-side
2083 * critical section.
2084 */
c116dba6 2085static struct rt_read_seg *
ec9d6356 2086rcutorture_loop_extend(int *readstate, bool insoftirq, struct torture_random_state *trsp,
c116dba6 2087 struct rt_read_seg *rtrsp)
2397d072
PM
2088{
2089 int i;
c116dba6 2090 int j;
2397d072
PM
2091 int mask = rcutorture_extend_mask_max();
2092
2093 WARN_ON_ONCE(!*readstate); /* -Existing- RCU read-side critsect! */
2094 if (!((mask - 1) & mask))
c116dba6
PM
2095 return rtrsp; /* Current RCU reader not extendable. */
2096 /* Bias towards larger numbers of loops. */
bc19e86e 2097 i = torture_random(trsp);
c116dba6
PM
2098 i = ((i | (i >> 3)) & RCUTORTURE_RDR_MAX_LOOPS) + 1;
2099 for (j = 0; j < i; j++) {
2397d072 2100 mask = rcutorture_extend_mask(*readstate, trsp);
ec9d6356 2101 rcutorture_one_extend(readstate, mask, insoftirq, trsp, &rtrsp[j]);
2397d072 2102 }
c116dba6 2103 return &rtrsp[j];
2397d072
PM
2104}
2105
6b06aa72
PM
2106/*
2107 * Do one read-side critical section, returning false if there was
2108 * no data to read. Can be invoked both from process context and
2109 * from a timer handler.
2110 */
00504537 2111static bool rcu_torture_one_read(struct torture_random_state *trsp, long myid)
0729fbf3 2112{
967c298d 2113 bool checkpolling = !(torture_random(trsp) & 0xfff);
bc480a63 2114 unsigned long cookie;
f4754ad2 2115 struct rcu_gp_oldstate cookie_full;
c116dba6 2116 int i;
917963d0 2117 unsigned long started;
6b80da42 2118 unsigned long completed;
2397d072 2119 int newstate;
0729fbf3
PM
2120 struct rcu_torture *p;
2121 int pipe_count;
0f38c06c 2122 bool preempted = false;
2397d072 2123 int readstate = 0;
c116dba6
PM
2124 struct rt_read_seg rtseg[RCUTORTURE_RDR_MAX_SEGS] = { { 0 } };
2125 struct rt_read_seg *rtrsp = &rtseg[0];
2126 struct rt_read_seg *rtrsp1;
52494535 2127 unsigned long long ts;
0729fbf3 2128
77522751 2129 WARN_ON_ONCE(!rcu_is_watching());
2397d072 2130 newstate = rcutorture_extend_mask(readstate, trsp);
ec9d6356 2131 rcutorture_one_extend(&readstate, newstate, myid < 0, trsp, rtrsp++);
967c298d
PM
2132 if (checkpolling) {
2133 if (cur_ops->get_gp_state && cur_ops->poll_gp_state)
2134 cookie = cur_ops->get_gp_state();
2135 if (cur_ops->get_gp_state_full && cur_ops->poll_gp_state_full)
2136 cur_ops->get_gp_state_full(&cookie_full);
2137 }
17ef2fe9 2138 started = cur_ops->get_gp_seq();
e4aa0da3 2139 ts = rcu_trace_clock_local();
632ee200 2140 p = rcu_dereference_check(rcu_torture_current,
a5c095e0 2141 !cur_ops->readlock_held || cur_ops->readlock_held());
0729fbf3 2142 if (p == NULL) {
6b06aa72 2143 /* Wait for rcu_torture_writer to get underway */
ec9d6356 2144 rcutorture_one_extend(&readstate, 0, myid < 0, trsp, rtrsp);
6b06aa72 2145 return false;
0729fbf3
PM
2146 }
2147 if (p->rtort_mbtest == 0)
2148 atomic_inc(&n_rcu_torture_mberror);
00504537 2149 rcu_torture_reader_do_mbchk(myid, p, trsp);
ec9d6356 2150 rtrsp = rcutorture_loop_extend(&readstate, myid < 0, trsp, rtrsp);
0729fbf3 2151 preempt_disable();
20248910 2152 pipe_count = READ_ONCE(p->rtort_pipe_count);
0729fbf3 2153 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
8b9b443f
PM
2154 // Should not happen in a correct RCU implementation,
2155 // happens quite often for torture_type=busted.
0729fbf3
PM
2156 pipe_count = RCU_TORTURE_PIPE_LEN;
2157 }
17ef2fe9 2158 completed = cur_ops->get_gp_seq();
52494535 2159 if (pipe_count > 1) {
6b06aa72
PM
2160 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
2161 ts, started, completed);
274529ba 2162 rcu_ftrace_dump(DUMP_ALL);
52494535 2163 }
dd17c8f7 2164 __this_cpu_inc(rcu_torture_count[pipe_count]);
d7219312 2165 completed = rcutorture_seq_diff(completed, started);
0729fbf3
PM
2166 if (completed > RCU_TORTURE_PIPE_LEN) {
2167 /* Should not happen, but... */
2168 completed = RCU_TORTURE_PIPE_LEN;
2169 }
dd17c8f7 2170 __this_cpu_inc(rcu_torture_batch[completed]);
0729fbf3 2171 preempt_enable();
967c298d
PM
2172 if (checkpolling) {
2173 if (cur_ops->get_gp_state && cur_ops->poll_gp_state)
2174 WARN_ONCE(cur_ops->poll_gp_state(cookie),
2175 "%s: Cookie check 2 failed %s(%d) %lu->%lu\n",
2176 __func__,
2177 rcu_torture_writer_state_getname(),
2178 rcu_torture_writer_state,
2179 cookie, cur_ops->get_gp_state());
2180 if (cur_ops->get_gp_state_full && cur_ops->poll_gp_state_full)
2181 WARN_ONCE(cur_ops->poll_gp_state_full(&cookie_full),
2182 "%s: Cookie check 6 failed %s(%d) online %*pbl\n",
2183 __func__,
2184 rcu_torture_writer_state_getname(),
2185 rcu_torture_writer_state,
2186 cpumask_pr_args(cpu_online_mask));
2187 }
0f38c06c
PM
2188 if (cur_ops->reader_blocked)
2189 preempted = cur_ops->reader_blocked();
ec9d6356 2190 rcutorture_one_extend(&readstate, 0, myid < 0, trsp, rtrsp);
902d82e6 2191 WARN_ON_ONCE(readstate);
d6855142
PM
2192 // This next splat is expected behavior if leakpointer, especially
2193 // for CONFIG_RCU_STRICT_GRACE_PERIOD=y kernels.
2194 WARN_ON_ONCE(leakpointer && READ_ONCE(p->rtort_pipe_count) > 1);
c116dba6
PM
2195
2196 /* If error or close call, record the sequence of reader protections. */
2197 if ((pipe_count > 1 || completed > 1) && !xchg(&err_segs_recorded, 1)) {
2198 i = 0;
2199 for (rtrsp1 = &rtseg[0]; rtrsp1 < rtrsp; rtrsp1++)
2200 err_segs[i++] = *rtrsp1;
2201 rt_read_nsegs = i;
0f38c06c 2202 rt_read_preempted = preempted;
c116dba6
PM
2203 }
2204
6b06aa72
PM
2205 return true;
2206}
2207
3025520e
PM
2208static DEFINE_TORTURE_RANDOM_PERCPU(rcu_torture_timer_rand);
2209
0729fbf3
PM
2210/*
2211 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
2212 * incrementing the corresponding element of the pipeline array. The
2213 * counter in the element should never be greater than 1, otherwise, the
2214 * RCU implementation is broken.
2215 */
fd30b717 2216static void rcu_torture_timer(struct timer_list *unused)
0729fbf3 2217{
8da9a595 2218 atomic_long_inc(&n_rcu_torture_timers);
00504537 2219 (void)rcu_torture_one_read(this_cpu_ptr(&rcu_torture_timer_rand), -1);
f34c8585
PM
2220
2221 /* Test call_rcu() invocation from interrupt handler. */
2222 if (cur_ops->call) {
2223 struct rcu_head *rhp = kmalloc(sizeof(*rhp), GFP_NOWAIT);
2224
2225 if (rhp)
2226 cur_ops->call(rhp, rcu_torture_timer_cb);
2227 }
0729fbf3
PM
2228}
2229
a241ec65
PM
2230/*
2231 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
2232 * incrementing the corresponding element of the pipeline array. The
2233 * counter in the element should never be greater than 1, otherwise, the
2234 * RCU implementation is broken.
2235 */
2236static int
2237rcu_torture_reader(void *arg)
2238{
444da518 2239 unsigned long lastsleep = jiffies;
c04dd09b
PM
2240 long myid = (long)arg;
2241 int mynumonline = myid;
51b1130e 2242 DEFINE_TORTURE_RANDOM(rand);
0729fbf3 2243 struct timer_list t;
a241ec65 2244
5ccf60f2 2245 VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
d277d868 2246 set_user_nice(current, MAX_NICE);
0acc512c 2247 if (irqreader && cur_ops->irq_capable)
fd30b717 2248 timer_setup_on_stack(&t, rcu_torture_timer, 0);
d38e6dc6 2249 tick_dep_set_task(current, TICK_DEP_BIT_RCU);
a241ec65 2250 do {
0acc512c 2251 if (irqreader && cur_ops->irq_capable) {
0729fbf3 2252 if (!timer_pending(&t))
6155fec9 2253 mod_timer(&t, jiffies + 1);
0729fbf3 2254 }
00504537 2255 if (!rcu_torture_one_read(&rand, myid) && !torture_must_stop())
a241ec65 2256 schedule_timeout_interruptible(HZ);
d38e6dc6 2257 if (time_after(jiffies, lastsleep) && !torture_must_stop()) {
1eba0ef9 2258 torture_hrtimeout_us(500, 1000, &rand);
444da518
PM
2259 lastsleep = jiffies + 10;
2260 }
1afb95fe 2261 while (torture_num_online_cpus() < mynumonline && !torture_must_stop())
c04dd09b 2262 schedule_timeout_interruptible(HZ / 5);
628edaa5 2263 stutter_wait("rcu_torture_reader");
36970bb9 2264 } while (!torture_must_stop());
424c1b68 2265 if (irqreader && cur_ops->irq_capable) {
0729fbf3 2266 del_timer_sync(&t);
424c1b68
TG
2267 destroy_timer_on_stack(&t);
2268 }
d38e6dc6 2269 tick_dep_clear_task(current, TICK_DEP_BIT_RCU);
7fafaac5 2270 torture_kthread_stopping("rcu_torture_reader");
a241ec65
PM
2271 return 0;
2272}
2273
2c4319bd
PM
2274/*
2275 * Randomly Toggle CPUs' callback-offload state. This uses hrtimers to
2276 * increase race probabilities and fuzzes the interval between toggling.
2277 */
2278static int rcu_nocb_toggle(void *arg)
2279{
2280 int cpu;
2281 int maxcpu = -1;
2282 int oldnice = task_nice(current);
2283 long r;
2284 DEFINE_TORTURE_RANDOM(rand);
2285 ktime_t toggle_delay;
2286 unsigned long toggle_fuzz;
2287 ktime_t toggle_interval = ms_to_ktime(nocbs_toggle);
2288
2289 VERBOSE_TOROUT_STRING("rcu_nocb_toggle task started");
2290 while (!rcu_inkernel_boot_has_ended())
2291 schedule_timeout_interruptible(HZ / 10);
771a92b8 2292 for_each_possible_cpu(cpu)
2c4319bd
PM
2293 maxcpu = cpu;
2294 WARN_ON(maxcpu < 0);
2295 if (toggle_interval > ULONG_MAX)
2296 toggle_fuzz = ULONG_MAX >> 3;
2297 else
2298 toggle_fuzz = toggle_interval >> 3;
2299 if (toggle_fuzz <= 0)
2300 toggle_fuzz = NSEC_PER_USEC;
2301 do {
2302 r = torture_random(&rand);
bc19e86e 2303 cpu = (r >> 1) % (maxcpu + 1);
2c4319bd
PM
2304 if (r & 0x1) {
2305 rcu_nocb_cpu_offload(cpu);
2306 atomic_long_inc(&n_nocb_offload);
2307 } else {
2308 rcu_nocb_cpu_deoffload(cpu);
2309 atomic_long_inc(&n_nocb_deoffload);
2310 }
2311 toggle_delay = torture_random(&rand) % toggle_fuzz + toggle_interval;
2312 set_current_state(TASK_INTERRUPTIBLE);
2313 schedule_hrtimeout(&toggle_delay, HRTIMER_MODE_REL);
2314 if (stutter_wait("rcu_nocb_toggle"))
2315 sched_set_normal(current, oldnice);
2316 } while (!torture_must_stop());
2317 torture_kthread_stopping("rcu_nocb_toggle");
2318 return 0;
2319}
2320
a241ec65 2321/*
eea203fe
JP
2322 * Print torture statistics. Caller must ensure that there is only
2323 * one call to this function at a given time!!! This is normally
2324 * accomplished by relying on the module system to only have one copy
2325 * of the module loaded, and then by giving the rcu_torture_stats
2326 * kthread full control (or the init/cleanup functions when rcu_torture_stats
2327 * thread is not running).
a241ec65 2328 */
d1008950 2329static void
eea203fe 2330rcu_torture_stats_print(void)
a241ec65 2331{
a241ec65
PM
2332 int cpu;
2333 int i;
2334 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
2335 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
5396d31d 2336 struct rcu_torture *rtcp;
ad0dc7f9 2337 static unsigned long rtcv_snap = ULONG_MAX;
0032f4e8 2338 static bool splatted;
4ffa6699 2339 struct task_struct *wtp;
a241ec65 2340
0a945022 2341 for_each_possible_cpu(cpu) {
a241ec65 2342 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
f042a436
PM
2343 pipesummary[i] += READ_ONCE(per_cpu(rcu_torture_count, cpu)[i]);
2344 batchsummary[i] += READ_ONCE(per_cpu(rcu_torture_batch, cpu)[i]);
a241ec65
PM
2345 }
2346 }
8c0666d3 2347 for (i = RCU_TORTURE_PIPE_LEN; i >= 0; i--) {
a241ec65
PM
2348 if (pipesummary[i] != 0)
2349 break;
2350 }
eea203fe
JP
2351
2352 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
5396d31d 2353 rtcp = rcu_access_pointer(rcu_torture_current);
354ea05d 2354 pr_cont("rtc: %p %s: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
5396d31d
PM
2355 rtcp,
2356 rtcp && !rcu_stall_is_suppressed_at_boot() ? "ver" : "VER",
eea203fe
JP
2357 rcu_torture_current_version,
2358 list_empty(&rcu_torture_freelist),
2359 atomic_read(&n_rcu_torture_alloc),
2360 atomic_read(&n_rcu_torture_alloc_fail),
2361 atomic_read(&n_rcu_torture_free));
fdcf87a3 2362 pr_cont("rtmbe: %d rtmbkf: %d/%d rtbe: %ld rtbke: %ld ",
eea203fe 2363 atomic_read(&n_rcu_torture_mberror),
00504537 2364 atomic_read(&n_rcu_torture_mbchk_fail), atomic_read(&n_rcu_torture_mbchk_tries),
472213a6 2365 n_rcu_torture_barrier_error,
fdcf87a3 2366 n_rcu_torture_boost_ktrerror);
eea203fe
JP
2367 pr_cont("rtbf: %ld rtb: %ld nt: %ld ",
2368 n_rcu_torture_boost_failure,
2369 n_rcu_torture_boosts,
8da9a595 2370 atomic_long_read(&n_rcu_torture_timers));
eea203fe 2371 torture_onoff_stats();
4a5f133c 2372 pr_cont("barrier: %ld/%ld:%ld ",
c9527beb
PM
2373 data_race(n_barrier_successes),
2374 data_race(n_barrier_attempts),
2375 data_race(n_rcu_torture_barrier_error));
f759081e 2376 pr_cont("read-exits: %ld ", data_race(n_read_exits)); // Statistic.
2c4319bd
PM
2377 pr_cont("nocb-toggles: %ld:%ld\n",
2378 atomic_long_read(&n_nocb_offload), atomic_long_read(&n_nocb_deoffload));
eea203fe
JP
2379
2380 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
8b5ddf8b 2381 if (atomic_read(&n_rcu_torture_mberror) ||
00504537 2382 atomic_read(&n_rcu_torture_mbchk_fail) ||
8b5ddf8b 2383 n_rcu_torture_barrier_error || n_rcu_torture_boost_ktrerror ||
fdcf87a3 2384 n_rcu_torture_boost_failure || i > 1) {
eea203fe 2385 pr_cont("%s", "!!! ");
996417d2 2386 atomic_inc(&n_rcu_torture_error);
8b5ddf8b 2387 WARN_ON_ONCE(atomic_read(&n_rcu_torture_mberror));
00504537 2388 WARN_ON_ONCE(atomic_read(&n_rcu_torture_mbchk_fail));
8b5ddf8b
PM
2389 WARN_ON_ONCE(n_rcu_torture_barrier_error); // rcu_barrier()
2390 WARN_ON_ONCE(n_rcu_torture_boost_ktrerror); // no boost kthread
5e59fba5 2391 WARN_ON_ONCE(n_rcu_torture_boost_failure); // boost failed (TIMER_SOFTIRQ RT prio?)
8b5ddf8b 2392 WARN_ON_ONCE(i > 1); // Too-short grace period
996417d2 2393 }
eea203fe 2394 pr_cont("Reader Pipe: ");
a241ec65 2395 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
eea203fe
JP
2396 pr_cont(" %ld", pipesummary[i]);
2397 pr_cont("\n");
2398
2399 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
2400 pr_cont("Reader Batch: ");
72e9bb54 2401 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
eea203fe
JP
2402 pr_cont(" %ld", batchsummary[i]);
2403 pr_cont("\n");
2404
2405 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
2406 pr_cont("Free-Block Circulation: ");
a241ec65 2407 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
eea203fe 2408 pr_cont(" %d", atomic_read(&rcu_torture_wcount[i]));
a241ec65 2409 }
eea203fe
JP
2410 pr_cont("\n");
2411
c8e5b163 2412 if (cur_ops->stats)
eea203fe 2413 cur_ops->stats();
ad0dc7f9 2414 if (rtcv_snap == rcu_torture_current_version &&
5396d31d
PM
2415 rcu_access_pointer(rcu_torture_current) &&
2416 !rcu_stall_is_suppressed()) {
7f6733c3 2417 int __maybe_unused flags = 0;
aebc8264 2418 unsigned long __maybe_unused gp_seq = 0;
ad0dc7f9 2419
dddcddef
Z
2420 if (cur_ops->get_gp_data)
2421 cur_ops->get_gp_data(&flags, &gp_seq);
4ffa6699 2422 wtp = READ_ONCE(writer_task);
2f064a59 2423 pr_alert("??? Writer stall state %s(%d) g%lu f%#x ->state %#x cpu %d\n",
18aff33e 2424 rcu_torture_writer_state_getname(),
aebc8264 2425 rcu_torture_writer_state, gp_seq, flags,
2f064a59 2426 wtp == NULL ? ~0U : wtp->__state,
808de39c 2427 wtp == NULL ? -1 : (int)task_cpu(wtp));
0032f4e8
PM
2428 if (!splatted && wtp) {
2429 sched_show_task(wtp);
2430 splatted = true;
2431 }
27c0f144
PM
2432 if (cur_ops->gp_kthread_dbg)
2433 cur_ops->gp_kthread_dbg();
274529ba 2434 rcu_ftrace_dump(DUMP_ALL);
ad0dc7f9
PM
2435 }
2436 rtcv_snap = rcu_torture_current_version;
a241ec65
PM
2437}
2438
a241ec65
PM
2439/*
2440 * Periodically prints torture statistics, if periodic statistics printing
2441 * was specified via the stat_interval module parameter.
a241ec65
PM
2442 */
2443static int
2444rcu_torture_stats(void *arg)
2445{
5ccf60f2 2446 VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
a241ec65
PM
2447 do {
2448 schedule_timeout_interruptible(stat_interval * HZ);
2449 rcu_torture_stats_print();
f67a3356 2450 torture_shutdown_absorb("rcu_torture_stats");
36970bb9 2451 } while (!torture_must_stop());
7fafaac5 2452 torture_kthread_stopping("rcu_torture_stats");
d120f65f
PM
2453 return 0;
2454}
2455
7ab2bd31
PM
2456/* Test mem_dump_obj() and friends. */
2457static void rcu_torture_mem_dump_obj(void)
2458{
2459 struct rcu_head *rhp;
2460 struct kmem_cache *kcp;
2461 static int z;
2462
2463 kcp = kmem_cache_create("rcuscale", 136, 8, SLAB_STORE_USER, NULL);
1a5ca5e0
LQ
2464 if (WARN_ON_ONCE(!kcp))
2465 return;
7ab2bd31 2466 rhp = kmem_cache_alloc(kcp, GFP_KERNEL);
1a5ca5e0
LQ
2467 if (WARN_ON_ONCE(!rhp)) {
2468 kmem_cache_destroy(kcp);
2469 return;
2470 }
7ab2bd31
PM
2471 pr_alert("mem_dump_obj() slab test: rcu_torture_stats = %px, &rhp = %px, rhp = %px, &z = %px\n", stats_task, &rhp, rhp, &z);
2472 pr_alert("mem_dump_obj(ZERO_SIZE_PTR):");
2473 mem_dump_obj(ZERO_SIZE_PTR);
2474 pr_alert("mem_dump_obj(NULL):");
2475 mem_dump_obj(NULL);
2476 pr_alert("mem_dump_obj(%px):", &rhp);
2477 mem_dump_obj(&rhp);
2478 pr_alert("mem_dump_obj(%px):", rhp);
2479 mem_dump_obj(rhp);
2480 pr_alert("mem_dump_obj(%px):", &rhp->func);
2481 mem_dump_obj(&rhp->func);
2482 pr_alert("mem_dump_obj(%px):", &z);
2483 mem_dump_obj(&z);
2484 kmem_cache_free(kcp, rhp);
2485 kmem_cache_destroy(kcp);
2486 rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
1a5ca5e0
LQ
2487 if (WARN_ON_ONCE(!rhp))
2488 return;
7ab2bd31
PM
2489 pr_alert("mem_dump_obj() kmalloc test: rcu_torture_stats = %px, &rhp = %px, rhp = %px\n", stats_task, &rhp, rhp);
2490 pr_alert("mem_dump_obj(kmalloc %px):", rhp);
2491 mem_dump_obj(rhp);
2492 pr_alert("mem_dump_obj(kmalloc %px):", &rhp->func);
2493 mem_dump_obj(&rhp->func);
2494 kfree(rhp);
2495 rhp = vmalloc(4096);
1a5ca5e0
LQ
2496 if (WARN_ON_ONCE(!rhp))
2497 return;
7ab2bd31
PM
2498 pr_alert("mem_dump_obj() vmalloc test: rcu_torture_stats = %px, &rhp = %px, rhp = %px\n", stats_task, &rhp, rhp);
2499 pr_alert("mem_dump_obj(vmalloc %px):", rhp);
2500 mem_dump_obj(rhp);
2501 pr_alert("mem_dump_obj(vmalloc %px):", &rhp->func);
2502 mem_dump_obj(&rhp->func);
2503 vfree(rhp);
2504}
2505
eac45e58 2506static void
e66c33d5 2507rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
95c38322 2508{
2caa1e44
PM
2509 pr_alert("%s" TORTURE_FLAG
2510 "--- %s: nreaders=%d nfakewriters=%d "
2511 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
2512 "shuffle_interval=%d stutter=%d irqreader=%d "
2513 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
2514 "test_boost=%d/%d test_boost_interval=%d "
2515 "test_boost_duration=%d shutdown_secs=%d "
2b1516e5 2516 "stall_cpu=%d stall_cpu_holdoff=%d stall_cpu_irqsoff=%d "
58cb3210 2517 "stall_cpu_block=%d stall_cpu_repeat=%d "
67afeed2 2518 "n_barrier_cbs=%d "
4a5f133c 2519 "onoff_interval=%d onoff_holdoff=%d "
2c4319bd 2520 "read_exit_delay=%d read_exit_burst=%d "
95a5de21 2521 "reader_flavor=%x "
808a9d67 2522 "nocbs_nthreads=%d nocbs_toggle=%d "
584975cc
PM
2523 "test_nmis=%d "
2524 "preempt_duration=%d preempt_interval=%d\n",
2caa1e44
PM
2525 torture_type, tag, nrealreaders, nfakewriters,
2526 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
2527 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
2528 test_boost, cur_ops->can_boost,
2529 test_boost_interval, test_boost_duration, shutdown_secs,
2b1516e5 2530 stall_cpu, stall_cpu_holdoff, stall_cpu_irqsoff,
58cb3210 2531 stall_cpu_block, stall_cpu_repeat,
67afeed2 2532 n_barrier_cbs,
4a5f133c 2533 onoff_interval, onoff_holdoff,
2c4319bd 2534 read_exit_delay, read_exit_burst,
95a5de21 2535 reader_flavor,
808a9d67 2536 nocbs_nthreads, nocbs_toggle,
584975cc
PM
2537 test_nmis,
2538 preempt_duration, preempt_interval);
95c38322
PM
2539}
2540
0ffd374b 2541static int rcutorture_booster_cleanup(unsigned int cpu)
8e8be45e
PM
2542{
2543 struct task_struct *t;
2544
2545 if (boost_tasks[cpu] == NULL)
0ffd374b 2546 return 0;
8e8be45e 2547 mutex_lock(&boost_mutex);
8e8be45e
PM
2548 t = boost_tasks[cpu];
2549 boost_tasks[cpu] = NULL;
450efca7 2550 rcu_torture_enable_rt_throttle();
8e8be45e
PM
2551 mutex_unlock(&boost_mutex);
2552
2553 /* This must be outside of the mutex, otherwise deadlock! */
9c029b86 2554 torture_stop_kthread(rcu_torture_boost, t);
0ffd374b 2555 return 0;
8e8be45e
PM
2556}
2557
0ffd374b 2558static int rcutorture_booster_init(unsigned int cpu)
8e8be45e
PM
2559{
2560 int retval;
2561
2562 if (boost_tasks[cpu] != NULL)
2563 return 0; /* Already created, nothing more to do. */
2564
3002153a
FW
2565 // Testing RCU priority boosting requires rcutorture do
2566 // some serious abuse. Counter this by running ksoftirqd
2567 // at higher priority.
2568 if (IS_BUILTIN(CONFIG_RCU_TORTURE_TEST)) {
2569 struct sched_param sp;
2570 struct task_struct *t;
2571
2572 t = per_cpu(ksoftirqd, cpu);
2573 WARN_ON_ONCE(!t);
2574 sp.sched_priority = 2;
2575 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
49a17639
SAS
2576#ifdef CONFIG_IRQ_FORCED_THREADING
2577 if (force_irqthreads()) {
2578 t = per_cpu(ktimerd, cpu);
2579 WARN_ON_ONCE(!t);
2580 sp.sched_priority = 2;
2581 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
2582 }
2583#endif
3002153a
FW
2584 }
2585
8e8be45e
PM
2586 /* Don't allow time recalculation while creating a new task. */
2587 mutex_lock(&boost_mutex);
450efca7 2588 rcu_torture_disable_rt_throttle();
5ccf60f2 2589 VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
3b9cb4ba
CH
2590 boost_tasks[cpu] = kthread_run_on_cpu(rcu_torture_boost, NULL,
2591 cpu, "rcu_torture_boost_%u");
8e8be45e
PM
2592 if (IS_ERR(boost_tasks[cpu])) {
2593 retval = PTR_ERR(boost_tasks[cpu]);
5ccf60f2 2594 VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
8e8be45e
PM
2595 n_rcu_torture_boost_ktrerror++;
2596 boost_tasks[cpu] = NULL;
2597 mutex_unlock(&boost_mutex);
2598 return retval;
2599 }
8e8be45e
PM
2600 mutex_unlock(&boost_mutex);
2601 return 0;
2602}
2603
7c1b3e0c
PM
2604static int rcu_torture_stall_nf(struct notifier_block *nb, unsigned long v, void *ptr)
2605{
2606 pr_info("%s: v=%lu, duration=%lu.\n", __func__, v, (unsigned long)ptr);
2607 return NOTIFY_OK;
2608}
2609
2610static struct notifier_block rcu_torture_stall_block = {
2611 .notifier_call = rcu_torture_stall_nf,
2612};
2613
c13f3757
PM
2614/*
2615 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
fd2a749d
PM
2616 * induces a CPU stall for the time specified by stall_cpu. If a new
2617 * stall test is added, stallsdone in rcu_torture_writer() must be adjusted.
c13f3757 2618 */
58cb3210 2619static void rcu_torture_stall_one(int rep, int irqsoff)
c13f3757 2620{
19a8ff95 2621 int idx;
c13f3757
PM
2622 unsigned long stop_at;
2623
c13f3757 2624 if (stall_cpu_holdoff > 0) {
5ccf60f2 2625 VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
c13f3757 2626 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
5ccf60f2 2627 VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
c13f3757 2628 }
55b2dcf5
PM
2629 if (!kthread_should_stop() && stall_gp_kthread > 0) {
2630 VERBOSE_TOROUT_STRING("rcu_torture_stall begin GP stall");
2631 rcu_gp_set_torture_wait(stall_gp_kthread * HZ);
2632 for (idx = 0; idx < stall_gp_kthread + 2; idx++) {
2633 if (kthread_should_stop())
2634 break;
2635 schedule_timeout_uninterruptible(HZ);
2636 }
2637 }
2638 if (!kthread_should_stop() && stall_cpu > 0) {
2639 VERBOSE_TOROUT_STRING("rcu_torture_stall begin CPU stall");
622be33f 2640 stop_at = ktime_get_seconds() + stall_cpu;
c13f3757 2641 /* RCU CPU stall is expected behavior in following code. */
19a8ff95 2642 idx = cur_ops->readlock();
58cb3210 2643 if (irqsoff)
2b1516e5 2644 local_irq_disable();
19a8ff95 2645 else if (!stall_cpu_block)
2b1516e5 2646 preempt_disable();
58cb3210
PM
2647 pr_alert("%s start stall episode %d on CPU %d.\n",
2648 __func__, rep + 1, raw_smp_processor_id());
431315a5
Z
2649 while (ULONG_CMP_LT((unsigned long)ktime_get_seconds(), stop_at) &&
2650 !kthread_should_stop())
59e83666
PM
2651 if (stall_cpu_block) {
2652#ifdef CONFIG_PREEMPTION
2653 preempt_schedule();
2654#else
19a8ff95 2655 schedule_timeout_uninterruptible(HZ);
59e83666 2656#endif
5ff7c9f9
WLC
2657 } else if (stall_no_softlockup) {
2658 touch_softlockup_watchdog();
59e83666 2659 }
58cb3210 2660 if (irqsoff)
2b1516e5 2661 local_irq_enable();
19a8ff95 2662 else if (!stall_cpu_block)
2b1516e5 2663 preempt_enable();
19a8ff95 2664 cur_ops->readunlock(idx);
c13f3757 2665 }
58cb3210
PM
2666}
2667
2668/*
2669 * CPU-stall kthread. Invokes rcu_torture_stall_one() once, and then as many
2670 * additional times as specified by the stall_cpu_repeat module parameter.
2671 * Note that stall_cpu_irqsoff is ignored on the second and subsequent
2672 * stall.
2673 */
2674static int rcu_torture_stall(void *args)
2675{
2676 int i;
2677 int repeat = stall_cpu_repeat;
2678 int ret;
2679
2680 VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
2681 if (repeat < 0) {
2682 repeat = 0;
2683 WARN_ON_ONCE(IS_BUILTIN(CONFIG_RCU_TORTURE_TEST));
2684 }
2685 if (rcu_cpu_stall_notifiers) {
2686 ret = rcu_stall_chain_notifier_register(&rcu_torture_stall_block);
2687 if (ret)
2688 pr_info("%s: rcu_stall_chain_notifier_register() returned %d, %sexpected.\n",
2689 __func__, ret, !IS_ENABLED(CONFIG_RCU_STALL_COMMON) ? "un" : "");
2690 }
2691 for (i = 0; i <= repeat; i++) {
2692 if (kthread_should_stop())
2693 break;
2694 rcu_torture_stall_one(i, i == 0 ? stall_cpu_irqsoff : 0);
2695 }
0a27fff3 2696 pr_alert("%s end.\n", __func__);
4e58aaee 2697 if (rcu_cpu_stall_notifiers && !ret) {
7c1b3e0c
PM
2698 ret = rcu_stall_chain_notifier_unregister(&rcu_torture_stall_block);
2699 if (ret)
2700 pr_info("%s: rcu_stall_chain_notifier_unregister() returned %d.\n", __func__, ret);
2701 }
f67a3356 2702 torture_shutdown_absorb("rcu_torture_stall");
c13f3757
PM
2703 while (!kthread_should_stop())
2704 schedule_timeout_interruptible(10 * HZ);
2705 return 0;
2706}
2707
2708/* Spawn CPU-stall kthread, if stall_cpu specified. */
2709static int __init rcu_torture_stall_init(void)
2710{
55b2dcf5 2711 if (stall_cpu <= 0 && stall_gp_kthread <= 0)
c13f3757 2712 return 0;
47cf29b9 2713 return torture_create_kthread(rcu_torture_stall, NULL, stall_task);
c13f3757
PM
2714}
2715
9fdcb9af
PM
2716/* State structure for forward-progress self-propagating RCU callback. */
2717struct fwd_cb_state {
2718 struct rcu_head rh;
2719 int stop;
2720};
2721
2722/*
2723 * Forward-progress self-propagating RCU callback function. Because
2724 * callbacks run from softirq, this function is an implicit RCU read-side
2725 * critical section.
2726 */
2727static void rcu_torture_fwd_prog_cb(struct rcu_head *rhp)
2728{
2729 struct fwd_cb_state *fcsp = container_of(rhp, struct fwd_cb_state, rh);
2730
2731 if (READ_ONCE(fcsp->stop)) {
2732 WRITE_ONCE(fcsp->stop, 2);
2733 return;
2734 }
2735 cur_ops->call(&fcsp->rh, rcu_torture_fwd_prog_cb);
2736}
2737
48718485
PM
2738/* State for continuous-flood RCU callbacks. */
2739struct rcu_fwd_cb {
2740 struct rcu_head rh;
2741 struct rcu_fwd_cb *rfc_next;
6b1b8325 2742 struct rcu_fwd *rfc_rfp;
48718485
PM
2743 int rfc_gps;
2744};
a289e608 2745
48718485
PM
2746#define MAX_FWD_CB_JIFFIES (8 * HZ) /* Maximum CB test duration. */
2747#define MIN_FWD_CB_LAUNDERS 3 /* This many CB invocations to count. */
2748#define MIN_FWD_CBS_LAUNDERED 100 /* Number of counted CBs. */
2e57bf97 2749#define FWD_CBS_HIST_DIV 10 /* Histogram buckets/second. */
a289e608
PM
2750#define N_LAUNDERS_HIST (2 * MAX_FWD_CB_JIFFIES / (HZ / FWD_CBS_HIST_DIV))
2751
cd618d10
PM
2752struct rcu_launder_hist {
2753 long n_launders;
2754 unsigned long launder_gp_seq;
2755};
48718485 2756
a289e608
PM
2757struct rcu_fwd {
2758 spinlock_t rcu_fwd_lock;
2759 struct rcu_fwd_cb *rcu_fwd_cb_head;
2760 struct rcu_fwd_cb **rcu_fwd_cb_tail;
2761 long n_launders_cb;
2762 unsigned long rcu_fwd_startat;
2763 struct rcu_launder_hist n_launders_hist[N_LAUNDERS_HIST];
2764 unsigned long rcu_launder_gp_seq_start;
82e31003 2765 int rcu_fwd_id;
a289e608
PM
2766};
2767
57f60202 2768static DEFINE_MUTEX(rcu_fwd_mutex);
afbc1574 2769static struct rcu_fwd *rcu_fwds;
82e31003 2770static unsigned long rcu_fwd_seq;
53b541fb 2771static atomic_long_t rcu_fwd_max_cbs;
afbc1574 2772static bool rcu_fwd_emergency_stop;
48718485 2773
6b1b8325 2774static void rcu_torture_fwd_cb_hist(struct rcu_fwd *rfp)
1a682754 2775{
cd618d10
PM
2776 unsigned long gps;
2777 unsigned long gps_old;
1a682754
PM
2778 int i;
2779 int j;
2780
6b1b8325
PM
2781 for (i = ARRAY_SIZE(rfp->n_launders_hist) - 1; i > 0; i--)
2782 if (rfp->n_launders_hist[i].n_launders > 0)
1a682754 2783 break;
82e31003
PM
2784 pr_alert("%s: Callback-invocation histogram %d (duration %lu jiffies):",
2785 __func__, rfp->rcu_fwd_id, jiffies - rfp->rcu_fwd_startat);
6b1b8325 2786 gps_old = rfp->rcu_launder_gp_seq_start;
cd618d10 2787 for (j = 0; j <= i; j++) {
6b1b8325 2788 gps = rfp->n_launders_hist[j].launder_gp_seq;
cd618d10 2789 pr_cont(" %ds/%d: %ld:%ld",
a289e608 2790 j + 1, FWD_CBS_HIST_DIV,
6b1b8325 2791 rfp->n_launders_hist[j].n_launders,
cd618d10
PM
2792 rcutorture_seq_diff(gps, gps_old));
2793 gps_old = gps;
2794 }
1a682754
PM
2795 pr_cont("\n");
2796}
2797
48718485
PM
2798/* Callback function for continuous-flood RCU callbacks. */
2799static void rcu_torture_fwd_cb_cr(struct rcu_head *rhp)
2800{
2667ccce 2801 unsigned long flags;
48718485
PM
2802 int i;
2803 struct rcu_fwd_cb *rfcp = container_of(rhp, struct rcu_fwd_cb, rh);
2804 struct rcu_fwd_cb **rfcpp;
6b1b8325 2805 struct rcu_fwd *rfp = rfcp->rfc_rfp;
48718485
PM
2806
2807 rfcp->rfc_next = NULL;
2808 rfcp->rfc_gps++;
6b1b8325
PM
2809 spin_lock_irqsave(&rfp->rcu_fwd_lock, flags);
2810 rfcpp = rfp->rcu_fwd_cb_tail;
2811 rfp->rcu_fwd_cb_tail = &rfcp->rfc_next;
6040072f 2812 smp_store_release(rfcpp, rfcp);
6b1b8325
PM
2813 WRITE_ONCE(rfp->n_launders_cb, rfp->n_launders_cb + 1);
2814 i = ((jiffies - rfp->rcu_fwd_startat) / (HZ / FWD_CBS_HIST_DIV));
2815 if (i >= ARRAY_SIZE(rfp->n_launders_hist))
2816 i = ARRAY_SIZE(rfp->n_launders_hist) - 1;
2817 rfp->n_launders_hist[i].n_launders++;
2818 rfp->n_launders_hist[i].launder_gp_seq = cur_ops->get_gp_seq();
2819 spin_unlock_irqrestore(&rfp->rcu_fwd_lock, flags);
2667ccce
PM
2820}
2821
ab21f608 2822// Give the scheduler a chance, even on nohz_full CPUs.
bd1bfc51 2823static void rcu_torture_fwd_prog_cond_resched(unsigned long iter)
ab21f608 2824{
90326f05 2825 if (IS_ENABLED(CONFIG_PREEMPTION) && IS_ENABLED(CONFIG_NO_HZ_FULL)) {
bd1bfc51
PM
2826 // Real call_rcu() floods hit userspace, so emulate that.
2827 if (need_resched() || (iter & 0xfff))
ab21f608 2828 schedule();
d38e6dc6 2829 return;
ab21f608 2830 }
d38e6dc6
PM
2831 // No userspace emulation: CB invocation throttles call_rcu()
2832 cond_resched();
ab21f608
PM
2833}
2834
2667ccce
PM
2835/*
2836 * Free all callbacks on the rcu_fwd_cb_head list, either because the
2837 * test is over or because we hit an OOM event.
2838 */
6764100b 2839static unsigned long rcu_torture_fwd_prog_cbfree(struct rcu_fwd *rfp)
2667ccce
PM
2840{
2841 unsigned long flags;
2842 unsigned long freed = 0;
2843 struct rcu_fwd_cb *rfcp;
2844
2845 for (;;) {
6764100b
PM
2846 spin_lock_irqsave(&rfp->rcu_fwd_lock, flags);
2847 rfcp = rfp->rcu_fwd_cb_head;
140e53f2 2848 if (!rfcp) {
6764100b 2849 spin_unlock_irqrestore(&rfp->rcu_fwd_lock, flags);
2667ccce 2850 break;
140e53f2 2851 }
6764100b
PM
2852 rfp->rcu_fwd_cb_head = rfcp->rfc_next;
2853 if (!rfp->rcu_fwd_cb_head)
2854 rfp->rcu_fwd_cb_tail = &rfp->rcu_fwd_cb_head;
2855 spin_unlock_irqrestore(&rfp->rcu_fwd_lock, flags);
2667ccce
PM
2856 kfree(rfcp);
2857 freed++;
bd1bfc51 2858 rcu_torture_fwd_prog_cond_resched(freed);
79ba7ff5
PM
2859 if (tick_nohz_full_enabled()) {
2860 local_irq_save(flags);
32a9f26e 2861 rcu_momentary_eqs();
79ba7ff5
PM
2862 local_irq_restore(flags);
2863 }
2667ccce 2864 }
2667ccce 2865 return freed;
48718485
PM
2866}
2867
6b3de7a1 2868/* Carry out need_resched()/cond_resched() forward-progress testing. */
6b1b8325
PM
2869static void rcu_torture_fwd_prog_nr(struct rcu_fwd *rfp,
2870 int *tested, int *tested_tries)
1b27291b 2871{
119248be 2872 unsigned long cver;
f4de46ed 2873 unsigned long dur;
7c590fcc 2874 struct fwd_cb_state fcs;
119248be 2875 unsigned long gps;
1b27291b 2876 int idx;
6b3de7a1
PM
2877 int sd;
2878 int sd4;
2879 bool selfpropcb = false;
2880 unsigned long stopat;
2881 static DEFINE_TORTURE_RANDOM(trs);
2882
05b72465 2883 pr_alert("%s: Starting forward-progress test %d\n", __func__, rfp->rcu_fwd_id);
a7eb937b
PM
2884 if (!cur_ops->sync)
2885 return; // Cannot do need_resched() forward progress testing without ->sync.
2886 if (cur_ops->call && cur_ops->cb_barrier) {
6b3de7a1
PM
2887 init_rcu_head_on_stack(&fcs.rh);
2888 selfpropcb = true;
2889 }
2890
2891 /* Tight loop containing cond_resched(). */
e22ef8df 2892 atomic_inc(&rcu_fwd_cb_nodelay);
e8516c64 2893 cur_ops->sync(); /* Later readers see above write. */
6b3de7a1
PM
2894 if (selfpropcb) {
2895 WRITE_ONCE(fcs.stop, 0);
2896 cur_ops->call(&fcs.rh, rcu_torture_fwd_prog_cb);
2897 }
2898 cver = READ_ONCE(rcu_torture_current_version);
2899 gps = cur_ops->get_gp_seq();
2900 sd = cur_ops->stall_dur() + 1;
2901 sd4 = (sd + fwd_progress_div - 1) / fwd_progress_div;
2902 dur = sd4 + torture_random(&trs) % (sd - sd4);
6b1b8325
PM
2903 WRITE_ONCE(rfp->rcu_fwd_startat, jiffies);
2904 stopat = rfp->rcu_fwd_startat + dur;
e0aff973 2905 while (time_before(jiffies, stopat) &&
60013d5d 2906 !shutdown_time_arrived() &&
e0aff973 2907 !READ_ONCE(rcu_fwd_emergency_stop) && !torture_must_stop()) {
6b3de7a1
PM
2908 idx = cur_ops->readlock();
2909 udelay(10);
2910 cur_ops->readunlock(idx);
2911 if (!fwd_progress_need_resched || need_resched())
fbbd5e35 2912 cond_resched();
6b3de7a1
PM
2913 }
2914 (*tested_tries)++;
e0aff973 2915 if (!time_before(jiffies, stopat) &&
60013d5d 2916 !shutdown_time_arrived() &&
e0aff973 2917 !READ_ONCE(rcu_fwd_emergency_stop) && !torture_must_stop()) {
6b3de7a1
PM
2918 (*tested)++;
2919 cver = READ_ONCE(rcu_torture_current_version) - cver;
2920 gps = rcutorture_seq_diff(cur_ops->get_gp_seq(), gps);
2921 WARN_ON(!cver && gps < 2);
82e31003
PM
2922 pr_alert("%s: %d Duration %ld cver %ld gps %ld\n", __func__,
2923 rfp->rcu_fwd_id, dur, cver, gps);
6b3de7a1
PM
2924 }
2925 if (selfpropcb) {
2926 WRITE_ONCE(fcs.stop, 1);
2927 cur_ops->sync(); /* Wait for running CB to complete. */
05b72465 2928 pr_alert("%s: Waiting for CBs: %pS() %d\n", __func__, cur_ops->cb_barrier, rfp->rcu_fwd_id);
6b3de7a1
PM
2929 cur_ops->cb_barrier(); /* Wait for queued callbacks. */
2930 }
2931
2932 if (selfpropcb) {
2933 WARN_ON(READ_ONCE(fcs.stop) != 2);
2934 destroy_rcu_head_on_stack(&fcs.rh);
2935 }
e8516c64 2936 schedule_timeout_uninterruptible(HZ / 10); /* Let kthreads recover. */
e22ef8df 2937 atomic_dec(&rcu_fwd_cb_nodelay);
6b3de7a1
PM
2938}
2939
2940/* Carry out call_rcu() forward-progress testing. */
6b1b8325 2941static void rcu_torture_fwd_prog_cr(struct rcu_fwd *rfp)
6b3de7a1
PM
2942{
2943 unsigned long cver;
79ba7ff5 2944 unsigned long flags;
6b3de7a1
PM
2945 unsigned long gps;
2946 int i;
48718485
PM
2947 long n_launders;
2948 long n_launders_cb_snap;
2949 long n_launders_sa;
2950 long n_max_cbs;
2951 long n_max_gps;
2952 struct rcu_fwd_cb *rfcp;
2953 struct rcu_fwd_cb *rfcpn;
1b27291b 2954 unsigned long stopat;
48718485 2955 unsigned long stoppedat;
6b3de7a1 2956
05b72465 2957 pr_alert("%s: Starting forward-progress test %d\n", __func__, rfp->rcu_fwd_id);
2667ccce
PM
2958 if (READ_ONCE(rcu_fwd_emergency_stop))
2959 return; /* Get out of the way quickly, no GP wait! */
c682db55
PM
2960 if (!cur_ops->call)
2961 return; /* Can't do call_rcu() fwd prog without ->call. */
2667ccce 2962
6b3de7a1 2963 /* Loop continuously posting RCU callbacks. */
e22ef8df 2964 atomic_inc(&rcu_fwd_cb_nodelay);
6b3de7a1 2965 cur_ops->sync(); /* Later readers see above write. */
6b1b8325
PM
2966 WRITE_ONCE(rfp->rcu_fwd_startat, jiffies);
2967 stopat = rfp->rcu_fwd_startat + MAX_FWD_CB_JIFFIES;
6b3de7a1 2968 n_launders = 0;
6b1b8325 2969 rfp->n_launders_cb = 0; // Hoist initialization for multi-kthread
6b3de7a1
PM
2970 n_launders_sa = 0;
2971 n_max_cbs = 0;
2972 n_max_gps = 0;
6b1b8325
PM
2973 for (i = 0; i < ARRAY_SIZE(rfp->n_launders_hist); i++)
2974 rfp->n_launders_hist[i].n_launders = 0;
6b3de7a1
PM
2975 cver = READ_ONCE(rcu_torture_current_version);
2976 gps = cur_ops->get_gp_seq();
6b1b8325 2977 rfp->rcu_launder_gp_seq_start = gps;
d38e6dc6 2978 tick_dep_set_task(current, TICK_DEP_BIT_RCU);
e0aff973 2979 while (time_before(jiffies, stopat) &&
60013d5d 2980 !shutdown_time_arrived() &&
e0aff973 2981 !READ_ONCE(rcu_fwd_emergency_stop) && !torture_must_stop()) {
6b1b8325 2982 rfcp = READ_ONCE(rfp->rcu_fwd_cb_head);
6b3de7a1
PM
2983 rfcpn = NULL;
2984 if (rfcp)
2985 rfcpn = READ_ONCE(rfcp->rfc_next);
2986 if (rfcpn) {
2987 if (rfcp->rfc_gps >= MIN_FWD_CB_LAUNDERS &&
2988 ++n_max_gps >= MIN_FWD_CBS_LAUNDERED)
2989 break;
6b1b8325 2990 rfp->rcu_fwd_cb_head = rfcpn;
6b3de7a1
PM
2991 n_launders++;
2992 n_launders_sa++;
613b00fb 2993 } else if (!cur_ops->cbflood_max || cur_ops->cbflood_max > n_max_cbs) {
6b3de7a1
PM
2994 rfcp = kmalloc(sizeof(*rfcp), GFP_KERNEL);
2995 if (WARN_ON_ONCE(!rfcp)) {
2996 schedule_timeout_interruptible(1);
2997 continue;
2998 }
2999 n_max_cbs++;
3000 n_launders_sa = 0;
3001 rfcp->rfc_gps = 0;
6b1b8325 3002 rfcp->rfc_rfp = rfp;
613b00fb
PM
3003 } else {
3004 rfcp = NULL;
6b3de7a1 3005 }
613b00fb
PM
3006 if (rfcp)
3007 cur_ops->call(&rfcp->rh, rcu_torture_fwd_cb_cr);
bd1bfc51 3008 rcu_torture_fwd_prog_cond_resched(n_launders + n_max_cbs);
79ba7ff5
PM
3009 if (tick_nohz_full_enabled()) {
3010 local_irq_save(flags);
32a9f26e 3011 rcu_momentary_eqs();
79ba7ff5
PM
3012 local_irq_restore(flags);
3013 }
6b3de7a1
PM
3014 }
3015 stoppedat = jiffies;
6b1b8325 3016 n_launders_cb_snap = READ_ONCE(rfp->n_launders_cb);
6b3de7a1
PM
3017 cver = READ_ONCE(rcu_torture_current_version) - cver;
3018 gps = rcutorture_seq_diff(cur_ops->get_gp_seq(), gps);
05b72465 3019 pr_alert("%s: Waiting for CBs: %pS() %d\n", __func__, cur_ops->cb_barrier, rfp->rcu_fwd_id);
6b3de7a1 3020 cur_ops->cb_barrier(); /* Wait for callbacks to be invoked. */
6764100b 3021 (void)rcu_torture_fwd_prog_cbfree(rfp);
2667ccce 3022
60013d5d
PM
3023 if (!torture_must_stop() && !READ_ONCE(rcu_fwd_emergency_stop) &&
3024 !shutdown_time_arrived()) {
f8039457
PM
3025 if (WARN_ON(n_max_gps < MIN_FWD_CBS_LAUNDERED) && cur_ops->gp_kthread_dbg)
3026 cur_ops->gp_kthread_dbg();
0a0467af 3027 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 #online %u\n",
6b3de7a1 3028 __func__,
6b1b8325 3029 stoppedat - rfp->rcu_fwd_startat, jiffies - stoppedat,
6b3de7a1
PM
3030 n_launders + n_max_cbs - n_launders_cb_snap,
3031 n_launders, n_launders_sa,
0a0467af 3032 n_max_gps, n_max_cbs, cver, gps, num_online_cpus());
53b541fb 3033 atomic_long_add(n_max_cbs, &rcu_fwd_max_cbs);
89440d2d 3034 mutex_lock(&rcu_fwd_mutex); // Serialize histograms.
6b1b8325 3035 rcu_torture_fwd_cb_hist(rfp);
89440d2d 3036 mutex_unlock(&rcu_fwd_mutex);
6b3de7a1 3037 }
e8516c64 3038 schedule_timeout_uninterruptible(HZ); /* Let CBs drain. */
d38e6dc6 3039 tick_dep_clear_task(current, TICK_DEP_BIT_RCU);
e22ef8df 3040 atomic_dec(&rcu_fwd_cb_nodelay);
6b3de7a1
PM
3041}
3042
e0aff973
PM
3043
3044/*
3045 * OOM notifier, but this only prints diagnostic information for the
3046 * current forward-progress test.
3047 */
3048static int rcutorture_oom_notify(struct notifier_block *self,
3049 unsigned long notused, void *nfreed)
3050{
82e31003
PM
3051 int i;
3052 long ncbs;
57f60202 3053 struct rcu_fwd *rfp;
6764100b 3054
57f60202
PM
3055 mutex_lock(&rcu_fwd_mutex);
3056 rfp = rcu_fwds;
3057 if (!rfp) {
3058 mutex_unlock(&rcu_fwd_mutex);
3059 return NOTIFY_OK;
3060 }
2667ccce
PM
3061 WARN(1, "%s invoked upon OOM during forward-progress testing.\n",
3062 __func__);
82e31003
PM
3063 for (i = 0; i < fwd_progress; i++) {
3064 rcu_torture_fwd_cb_hist(&rfp[i]);
3065 rcu_fwd_progress_check(1 + (jiffies - READ_ONCE(rfp[i].rcu_fwd_startat)) / 2);
3066 }
e0aff973 3067 WRITE_ONCE(rcu_fwd_emergency_stop, true);
2667ccce 3068 smp_mb(); /* Emergency stop before free and wait to avoid hangs. */
82e31003
PM
3069 ncbs = 0;
3070 for (i = 0; i < fwd_progress; i++)
3071 ncbs += rcu_torture_fwd_prog_cbfree(&rfp[i]);
3072 pr_info("%s: Freed %lu RCU callbacks.\n", __func__, ncbs);
48297a22 3073 cur_ops->cb_barrier();
82e31003
PM
3074 ncbs = 0;
3075 for (i = 0; i < fwd_progress; i++)
3076 ncbs += rcu_torture_fwd_prog_cbfree(&rfp[i]);
3077 pr_info("%s: Freed %lu RCU callbacks.\n", __func__, ncbs);
48297a22 3078 cur_ops->cb_barrier();
82e31003
PM
3079 ncbs = 0;
3080 for (i = 0; i < fwd_progress; i++)
3081 ncbs += rcu_torture_fwd_prog_cbfree(&rfp[i]);
3082 pr_info("%s: Freed %lu RCU callbacks.\n", __func__, ncbs);
2667ccce
PM
3083 smp_mb(); /* Frees before return to avoid redoing OOM. */
3084 (*(unsigned long *)nfreed)++; /* Forward progress CBs freed! */
3085 pr_info("%s returning after OOM processing.\n", __func__);
57f60202 3086 mutex_unlock(&rcu_fwd_mutex);
e0aff973
PM
3087 return NOTIFY_OK;
3088}
3089
3090static struct notifier_block rcutorture_oom_nb = {
3091 .notifier_call = rcutorture_oom_notify
3092};
3093
6b3de7a1
PM
3094/* Carry out grace-period forward-progress testing. */
3095static int rcu_torture_fwd_prog(void *args)
3096{
53b541fb
PM
3097 bool firsttime = true;
3098 long max_cbs;
ab1b7880 3099 int oldnice = task_nice(current);
82e31003 3100 unsigned long oldseq = READ_ONCE(rcu_fwd_seq);
6b1b8325 3101 struct rcu_fwd *rfp = args;
f4de46ed 3102 int tested = 0;
152f4afb 3103 int tested_tries = 0;
1b27291b
PM
3104
3105 VERBOSE_TOROUT_STRING("rcu_torture_fwd_progress task started");
5ab7ab83 3106 rcu_bind_current_to_nocb();
fecad509
PM
3107 if (!IS_ENABLED(CONFIG_SMP) || !IS_ENABLED(CONFIG_RCU_BOOST))
3108 set_user_nice(current, MAX_NICE);
1b27291b 3109 do {
82e31003
PM
3110 if (!rfp->rcu_fwd_id) {
3111 schedule_timeout_interruptible(fwd_progress_holdoff * HZ);
3112 WRITE_ONCE(rcu_fwd_emergency_stop, false);
53b541fb
PM
3113 if (!firsttime) {
3114 max_cbs = atomic_long_xchg(&rcu_fwd_max_cbs, 0);
3115 pr_alert("%s n_max_cbs: %ld\n", __func__, max_cbs);
3116 }
3117 firsttime = false;
82e31003
PM
3118 WRITE_ONCE(rcu_fwd_seq, rcu_fwd_seq + 1);
3119 } else {
02b51a1c 3120 while (READ_ONCE(rcu_fwd_seq) == oldseq && !torture_must_stop())
66bcb132 3121 schedule_timeout_interruptible(HZ / 20);
82e31003
PM
3122 oldseq = READ_ONCE(rcu_fwd_seq);
3123 }
3124 pr_alert("%s: Starting forward-progress test %d\n", __func__, rfp->rcu_fwd_id);
3125 if (rcu_inkernel_boot_has_ended() && torture_num_online_cpus() > rfp->rcu_fwd_id)
3126 rcu_torture_fwd_prog_cr(rfp);
613b00fb
PM
3127 if ((cur_ops->stall_dur && cur_ops->stall_dur() > 0) &&
3128 (!IS_ENABLED(CONFIG_TINY_RCU) ||
3129 (rcu_inkernel_boot_has_ended() &&
3130 torture_num_online_cpus() > rfp->rcu_fwd_id)))
43550809 3131 rcu_torture_fwd_prog_nr(rfp, &tested, &tested_tries);
48718485 3132
1b27291b 3133 /* Avoid slow periods, better to test when busy. */
ab1b7880
PM
3134 if (stutter_wait("rcu_torture_fwd_prog"))
3135 sched_set_normal(current, oldnice);
1b27291b 3136 } while (!torture_must_stop());
152f4afb 3137 /* Short runs might not contain a valid forward-progress attempt. */
82e31003
PM
3138 if (!rfp->rcu_fwd_id) {
3139 WARN_ON(!tested && tested_tries >= 5);
3140 pr_alert("%s: tested %d tested_tries %d\n", __func__, tested, tested_tries);
3141 }
1b27291b
PM
3142 torture_kthread_stopping("rcu_torture_fwd_prog");
3143 return 0;
3144}
3145
3146/* If forward-progress checking is requested and feasible, spawn the thread. */
3147static int __init rcu_torture_fwd_prog_init(void)
3148{
82e31003
PM
3149 int i;
3150 int ret = 0;
5155be99 3151 struct rcu_fwd *rfp;
6764100b 3152
1b27291b
PM
3153 if (!fwd_progress)
3154 return 0; /* Not requested, so don't do it. */
82e31003
PM
3155 if (fwd_progress >= nr_cpu_ids) {
3156 VERBOSE_TOROUT_STRING("rcu_torture_fwd_prog_init: Limiting fwd_progress to # CPUs.\n");
3157 fwd_progress = nr_cpu_ids;
3158 } else if (fwd_progress < 0) {
3159 fwd_progress = nr_cpu_ids;
3160 }
a7eb937b 3161 if ((!cur_ops->sync && !cur_ops->call) ||
613b00fb
PM
3162 (!cur_ops->cbflood_max && (!cur_ops->stall_dur || cur_ops->stall_dur() <= 0)) ||
3163 cur_ops == &rcu_busted_ops) {
1b27291b 3164 VERBOSE_TOROUT_STRING("rcu_torture_fwd_prog_init: Disabled, unsupported by RCU flavor under test");
82e31003 3165 fwd_progress = 0;
1b27291b
PM
3166 return 0;
3167 }
a2ab1e45
PM
3168 if (stall_cpu > 0 || (preempt_duration > 0 && IS_ENABLED(CONFIG_RCU_NOCB_CPU))) {
3169 VERBOSE_TOROUT_STRING("rcu_torture_fwd_prog_init: Disabled, conflicts with CPU-stall and/or preemption testing");
82e31003 3170 fwd_progress = 0;
3ac85878 3171 if (IS_MODULE(CONFIG_RCU_TORTURE_TEST))
1b27291b 3172 return -EINVAL; /* In module, can fail back to user. */
a2ab1e45 3173 WARN_ON(1); /* Make sure rcutorture scripting notices conflict. */
1b27291b
PM
3174 return 0;
3175 }
3176 if (fwd_progress_holdoff <= 0)
3177 fwd_progress_holdoff = 1;
3178 if (fwd_progress_div <= 0)
3179 fwd_progress_div = 4;
82e31003
PM
3180 rfp = kcalloc(fwd_progress, sizeof(*rfp), GFP_KERNEL);
3181 fwd_prog_tasks = kcalloc(fwd_progress, sizeof(*fwd_prog_tasks), GFP_KERNEL);
3182 if (!rfp || !fwd_prog_tasks) {
3183 kfree(rfp);
3184 kfree(fwd_prog_tasks);
3185 fwd_prog_tasks = NULL;
3186 fwd_progress = 0;
5155be99 3187 return -ENOMEM;
82e31003
PM
3188 }
3189 for (i = 0; i < fwd_progress; i++) {
3190 spin_lock_init(&rfp[i].rcu_fwd_lock);
3191 rfp[i].rcu_fwd_cb_tail = &rfp[i].rcu_fwd_cb_head;
3192 rfp[i].rcu_fwd_id = i;
3193 }
57f60202 3194 mutex_lock(&rcu_fwd_mutex);
c8fa6371 3195 rcu_fwds = rfp;
57f60202 3196 mutex_unlock(&rcu_fwd_mutex);
299c7d94 3197 register_oom_notifier(&rcutorture_oom_nb);
82e31003
PM
3198 for (i = 0; i < fwd_progress; i++) {
3199 ret = torture_create_kthread(rcu_torture_fwd_prog, &rcu_fwds[i], fwd_prog_tasks[i]);
3200 if (ret) {
3201 fwd_progress = i;
3202 return ret;
3203 }
3204 }
3205 return 0;
1b27291b
PM
3206}
3207
c8fa6371
PM
3208static void rcu_torture_fwd_prog_cleanup(void)
3209{
82e31003 3210 int i;
c8fa6371
PM
3211 struct rcu_fwd *rfp;
3212
82e31003
PM
3213 if (!rcu_fwds || !fwd_prog_tasks)
3214 return;
3215 for (i = 0; i < fwd_progress; i++)
3216 torture_stop_kthread(rcu_torture_fwd_prog, fwd_prog_tasks[i]);
3217 unregister_oom_notifier(&rcutorture_oom_nb);
57f60202 3218 mutex_lock(&rcu_fwd_mutex);
82e31003 3219 rfp = rcu_fwds;
c8fa6371 3220 rcu_fwds = NULL;
57f60202 3221 mutex_unlock(&rcu_fwd_mutex);
c8fa6371 3222 kfree(rfp);
82e31003
PM
3223 kfree(fwd_prog_tasks);
3224 fwd_prog_tasks = NULL;
c8fa6371
PM
3225}
3226
fae4b54f 3227/* Callback function for RCU barrier testing. */
b3b8a4d4 3228static void rcu_torture_barrier_cbf(struct rcu_head *rcu)
fae4b54f
PM
3229{
3230 atomic_inc(&barrier_cbs_invoked);
3231}
3232
50d4b629 3233/* IPI handler to get callback posted on desired CPU, if online. */
668c0406 3234static int rcu_torture_barrier1cb(void *rcu_void)
50d4b629
PM
3235{
3236 struct rcu_head *rhp = rcu_void;
3237
3238 cur_ops->call(rhp, rcu_torture_barrier_cbf);
668c0406 3239 return 0;
50d4b629
PM
3240}
3241
fae4b54f
PM
3242/* kthread function to register callbacks used to test RCU barriers. */
3243static int rcu_torture_barrier_cbs(void *arg)
3244{
3245 long myid = (long)arg;
8f43d591 3246 bool lastphase = false;
78e4bc34 3247 bool newphase;
fae4b54f
PM
3248 struct rcu_head rcu;
3249
3250 init_rcu_head_on_stack(&rcu);
5ccf60f2 3251 VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
d277d868 3252 set_user_nice(current, MAX_NICE);
fae4b54f
PM
3253 do {
3254 wait_event(barrier_cbs_wq[myid],
78e4bc34 3255 (newphase =
6c7ed42c 3256 smp_load_acquire(&barrier_phase)) != lastphase ||
36970bb9 3257 torture_must_stop());
78e4bc34 3258 lastphase = newphase;
36970bb9 3259 if (torture_must_stop())
fae4b54f 3260 break;
6c7ed42c
PM
3261 /*
3262 * The above smp_load_acquire() ensures barrier_phase load
aab05738 3263 * is ordered before the following ->call().
6c7ed42c 3264 */
668c0406 3265 if (smp_call_on_cpu(myid, rcu_torture_barrier1cb, &rcu, 1))
50d4b629 3266 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
668c0406 3267
fae4b54f
PM
3268 if (atomic_dec_and_test(&barrier_cbs_count))
3269 wake_up(&barrier_wq);
36970bb9 3270 } while (!torture_must_stop());
69c60455
PM
3271 if (cur_ops->cb_barrier != NULL)
3272 cur_ops->cb_barrier();
fae4b54f 3273 destroy_rcu_head_on_stack(&rcu);
7fafaac5 3274 torture_kthread_stopping("rcu_torture_barrier_cbs");
fae4b54f
PM
3275 return 0;
3276}
3277
3278/* kthread function to drive and coordinate RCU barrier testing. */
3279static int rcu_torture_barrier(void *arg)
3280{
3281 int i;
3282
5ccf60f2 3283 VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
fae4b54f
PM
3284 do {
3285 atomic_set(&barrier_cbs_invoked, 0);
3286 atomic_set(&barrier_cbs_count, n_barrier_cbs);
6c7ed42c
PM
3287 /* Ensure barrier_phase ordered after prior assignments. */
3288 smp_store_release(&barrier_phase, !barrier_phase);
fae4b54f
PM
3289 for (i = 0; i < n_barrier_cbs; i++)
3290 wake_up(&barrier_cbs_wq[i]);
3291 wait_event(barrier_wq,
3292 atomic_read(&barrier_cbs_count) == 0 ||
36970bb9
PM
3293 torture_must_stop());
3294 if (torture_must_stop())
fae4b54f
PM
3295 break;
3296 n_barrier_attempts++;
78e4bc34 3297 cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
fae4b54f
PM
3298 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
3299 n_rcu_torture_barrier_error++;
7602de4a
PM
3300 pr_err("barrier_cbs_invoked = %d, n_barrier_cbs = %d\n",
3301 atomic_read(&barrier_cbs_invoked),
3302 n_barrier_cbs);
9470a18f
PM
3303 WARN_ON(1);
3304 // Wait manually for the remaining callbacks
3305 i = 0;
3306 do {
3307 if (WARN_ON(i++ > HZ))
3308 i = INT_MIN;
3309 schedule_timeout_interruptible(1);
3310 cur_ops->cb_barrier();
3311 } while (atomic_read(&barrier_cbs_invoked) !=
3312 n_barrier_cbs &&
3313 !torture_must_stop());
3314 smp_mb(); // Can't trust ordering if broken.
3315 if (!torture_must_stop())
3316 pr_err("Recovered: barrier_cbs_invoked = %d\n",
3317 atomic_read(&barrier_cbs_invoked));
bf5b6435
JFG
3318 } else {
3319 n_barrier_successes++;
fae4b54f 3320 }
fae4b54f 3321 schedule_timeout_interruptible(HZ / 10);
36970bb9 3322 } while (!torture_must_stop());
7fafaac5 3323 torture_kthread_stopping("rcu_torture_barrier");
fae4b54f
PM
3324 return 0;
3325}
3326
3327/* Initialize RCU barrier testing. */
3328static int rcu_torture_barrier_init(void)
3329{
3330 int i;
3331 int ret;
3332
d9eba768 3333 if (n_barrier_cbs <= 0)
fae4b54f
PM
3334 return 0;
3335 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
2caa1e44
PM
3336 pr_alert("%s" TORTURE_FLAG
3337 " Call or barrier ops missing for %s,\n",
3338 torture_type, cur_ops->name);
3339 pr_alert("%s" TORTURE_FLAG
3340 " RCU barrier testing omitted from run.\n",
3341 torture_type);
fae4b54f
PM
3342 return 0;
3343 }
3344 atomic_set(&barrier_cbs_count, 0);
3345 atomic_set(&barrier_cbs_invoked, 0);
3346 barrier_cbs_tasks =
68a675d4 3347 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_tasks[0]),
fae4b54f
PM
3348 GFP_KERNEL);
3349 barrier_cbs_wq =
68a675d4 3350 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_wq[0]), GFP_KERNEL);
de5e6437 3351 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
fae4b54f
PM
3352 return -ENOMEM;
3353 for (i = 0; i < n_barrier_cbs; i++) {
3354 init_waitqueue_head(&barrier_cbs_wq[i]);
47cf29b9
PM
3355 ret = torture_create_kthread(rcu_torture_barrier_cbs,
3356 (void *)(long)i,
3357 barrier_cbs_tasks[i]);
3358 if (ret)
fae4b54f 3359 return ret;
fae4b54f 3360 }
47cf29b9 3361 return torture_create_kthread(rcu_torture_barrier, NULL, barrier_task);
fae4b54f
PM
3362}
3363
3364/* Clean up after RCU barrier testing. */
3365static void rcu_torture_barrier_cleanup(void)
3366{
3367 int i;
3368
9c029b86 3369 torture_stop_kthread(rcu_torture_barrier, barrier_task);
fae4b54f 3370 if (barrier_cbs_tasks != NULL) {
9c029b86
PM
3371 for (i = 0; i < n_barrier_cbs; i++)
3372 torture_stop_kthread(rcu_torture_barrier_cbs,
3373 barrier_cbs_tasks[i]);
fae4b54f
PM
3374 kfree(barrier_cbs_tasks);
3375 barrier_cbs_tasks = NULL;
3376 }
3377 if (barrier_cbs_wq != NULL) {
3378 kfree(barrier_cbs_wq);
3379 barrier_cbs_wq = NULL;
3380 }
3381}
3382
4babd855
JFG
3383static bool rcu_torture_can_boost(void)
3384{
3385 static int boost_warn_once;
3386 int prio;
3387
3388 if (!(test_boost == 1 && cur_ops->can_boost) && test_boost != 2)
3389 return false;
ea6d962e 3390 if (!cur_ops->start_gp_poll || !cur_ops->poll_gp_state)
5e59fba5 3391 return false;
4babd855
JFG
3392
3393 prio = rcu_get_gp_kthreads_prio();
3394 if (!prio)
3395 return false;
3396
3397 if (prio < 2) {
ea6d962e 3398 if (boost_warn_once == 1)
4babd855
JFG
3399 return false;
3400
bf5b6435 3401 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
3402 boost_warn_once = 1;
3403 return false;
3404 }
3405
3406 return true;
3407}
3408
4a5f133c
PM
3409static bool read_exit_child_stop;
3410static bool read_exit_child_stopped;
3411static wait_queue_head_t read_exit_wq;
3412
3413// Child kthread which just does an rcutorture reader and exits.
3414static int rcu_torture_read_exit_child(void *trsp_in)
3415{
3416 struct torture_random_state *trsp = trsp_in;
3417
3418 set_user_nice(current, MAX_NICE);
3419 // Minimize time between reading and exiting.
3420 while (!kthread_should_stop())
66bcb132 3421 schedule_timeout_uninterruptible(HZ / 20);
00504537 3422 (void)rcu_torture_one_read(trsp, -1);
4a5f133c
PM
3423 return 0;
3424}
3425
3426// Parent kthread which creates and destroys read-exit child kthreads.
3427static int rcu_torture_read_exit(void *unused)
3428{
4a5f133c
PM
3429 bool errexit = false;
3430 int i;
3431 struct task_struct *tsp;
3432 DEFINE_TORTURE_RANDOM(trs);
3433
3434 // Allocate and initialize.
3435 set_user_nice(current, MAX_NICE);
3436 VERBOSE_TOROUT_STRING("rcu_torture_read_exit: Start of test");
3437
3438 // Each pass through this loop does one read-exit episode.
3439 do {
d984114e
PM
3440 VERBOSE_TOROUT_STRING("rcu_torture_read_exit: Start of episode");
3441 for (i = 0; i < read_exit_burst; i++) {
3442 if (READ_ONCE(read_exit_child_stop))
3443 break;
3444 stutter_wait("rcu_torture_read_exit");
3445 // Spawn child.
3446 tsp = kthread_run(rcu_torture_read_exit_child,
3447 &trs, "%s", "rcu_torture_read_exit_child");
3448 if (IS_ERR(tsp)) {
3449 TOROUT_ERRSTRING("out of memory");
3450 errexit = true;
3451 break;
4a5f133c 3452 }
d984114e
PM
3453 cond_resched();
3454 kthread_stop(tsp);
3455 n_read_exits++;
4a5f133c 3456 }
d984114e
PM
3457 VERBOSE_TOROUT_STRING("rcu_torture_read_exit: End of episode");
3458 rcu_barrier(); // Wait for task_struct free, avoid OOM.
3459 i = 0;
3460 for (; !errexit && !READ_ONCE(read_exit_child_stop) && i < read_exit_delay; i++)
3461 schedule_timeout_uninterruptible(HZ);
4a5f133c
PM
3462 } while (!errexit && !READ_ONCE(read_exit_child_stop));
3463
3464 // Clean up and exit.
3465 smp_store_release(&read_exit_child_stopped, true); // After reaping.
3466 smp_mb(); // Store before wakeup.
3467 wake_up(&read_exit_wq);
3468 while (!torture_must_stop())
66bcb132 3469 schedule_timeout_uninterruptible(HZ / 20);
4a5f133c
PM
3470 torture_kthread_stopping("rcu_torture_read_exit");
3471 return 0;
3472}
3473
3474static int rcu_torture_read_exit_init(void)
3475{
3476 if (read_exit_burst <= 0)
fda84866 3477 return 0;
4a5f133c
PM
3478 init_waitqueue_head(&read_exit_wq);
3479 read_exit_child_stop = false;
3480 read_exit_child_stopped = false;
3481 return torture_create_kthread(rcu_torture_read_exit, NULL,
3482 read_exit_task);
3483}
3484
3485static void rcu_torture_read_exit_cleanup(void)
3486{
3487 if (!read_exit_task)
3488 return;
3489 WRITE_ONCE(read_exit_child_stop, true);
3490 smp_mb(); // Above write before wait.
3491 wait_event(read_exit_wq, smp_load_acquire(&read_exit_child_stopped));
3492 torture_stop_kthread(rcutorture_read_exit, read_exit_task);
3493}
3494
808a9d67
PM
3495static void rcutorture_test_nmis(int n)
3496{
3497#if IS_BUILTIN(CONFIG_RCU_TORTURE_TEST)
3498 int cpu;
3499 int dumpcpu;
3500 int i;
3501
3502 for (i = 0; i < n; i++) {
3503 preempt_disable();
3504 cpu = smp_processor_id();
3505 dumpcpu = cpu + 1;
3506 if (dumpcpu >= nr_cpu_ids)
3507 dumpcpu = 0;
3508 pr_alert("%s: CPU %d invoking dump_cpu_task(%d)\n", __func__, cpu, dumpcpu);
3509 dump_cpu_task(dumpcpu);
3510 preempt_enable();
3511 schedule_timeout_uninterruptible(15 * HZ);
3512 }
3513#else // #if IS_BUILTIN(CONFIG_RCU_TORTURE_TEST)
3514 WARN_ONCE(n, "Non-zero rcutorture.test_nmis=%d permitted only when rcutorture is built in.\n", test_nmis);
3515#endif // #else // #if IS_BUILTIN(CONFIG_RCU_TORTURE_TEST)
3516}
3517
584975cc
PM
3518// Randomly preempt online CPUs.
3519static int rcu_torture_preempt(void *unused)
3520{
3521 int cpu = -1;
3522 DEFINE_TORTURE_RANDOM(rand);
3523
3524 schedule_timeout_idle(stall_cpu_holdoff);
3525 do {
3526 // Wait for preempt_interval ms with up to 100us fuzz.
3527 torture_hrtimeout_ms(preempt_interval, 100, &rand);
3528 // Select online CPU.
3529 cpu = cpumask_next(cpu, cpu_online_mask);
3530 if (cpu >= nr_cpu_ids)
3531 cpu = cpumask_next(-1, cpu_online_mask);
3532 WARN_ON_ONCE(cpu >= nr_cpu_ids);
3533 // Move to that CPU, if can't do so, retry later.
3534 if (torture_sched_setaffinity(current->pid, cpumask_of(cpu), false))
3535 continue;
3536 // Preempt at high-ish priority, then reset to normal.
3537 sched_set_fifo(current);
3538 torture_sched_setaffinity(current->pid, cpu_present_mask, true);
3539 mdelay(preempt_duration);
3540 sched_set_normal(current, 0);
3541 stutter_wait("rcu_torture_preempt");
3542 } while (!torture_must_stop());
3543 torture_kthread_stopping("rcu_torture_preempt");
3544 return 0;
3545}
3546
0ffd374b 3547static enum cpuhp_state rcutor_hp;
8e8be45e 3548
a241ec65
PM
3549static void
3550rcu_torture_cleanup(void)
3551{
c116dba6 3552 int firsttime;
034777d7 3553 int flags = 0;
aebc8264 3554 unsigned long gp_seq = 0;
a241ec65
PM
3555 int i;
3556
d36a7a0d 3557 if (torture_cleanup_begin()) {
6f81bd6a
PM
3558 if (cur_ops->cb_barrier != NULL) {
3559 pr_info("%s: Invoking %pS().\n", __func__, cur_ops->cb_barrier);
343e9099 3560 cur_ops->cb_barrier();
6f81bd6a 3561 }
1c67318b
Z
3562 if (cur_ops->gp_slow_unregister)
3563 cur_ops->gp_slow_unregister(NULL);
343e9099
PM
3564 return;
3565 }
b813afae
PM
3566 if (!cur_ops) {
3567 torture_cleanup_end();
3568 return;
3569 }
d84f5203 3570
808a9d67
PM
3571 rcutorture_test_nmis(test_nmis);
3572
27c0f144
PM
3573 if (cur_ops->gp_kthread_dbg)
3574 cur_ops->gp_kthread_dbg();
584975cc 3575 torture_stop_kthread(rcu_torture_preempt, preempt_task);
4a5f133c 3576 rcu_torture_read_exit_cleanup();
fae4b54f 3577 rcu_torture_barrier_cleanup();
c8fa6371 3578 rcu_torture_fwd_prog_cleanup();
9c029b86 3579 torture_stop_kthread(rcu_torture_stall, stall_task);
9c029b86 3580 torture_stop_kthread(rcu_torture_writer, writer_task);
a241ec65 3581
2c4319bd
PM
3582 if (nocb_tasks) {
3583 for (i = 0; i < nrealnocbers; i++)
3584 torture_stop_kthread(rcu_nocb_toggle, nocb_tasks[i]);
3585 kfree(nocb_tasks);
3586 nocb_tasks = NULL;
3587 }
3588
c8e5b163 3589 if (reader_tasks) {
9c029b86
PM
3590 for (i = 0; i < nrealreaders; i++)
3591 torture_stop_kthread(rcu_torture_reader,
3592 reader_tasks[i]);
a241ec65 3593 kfree(reader_tasks);
293b93d6 3594 reader_tasks = NULL;
a241ec65 3595 }
00504537
PM
3596 kfree(rcu_torture_reader_mbchk);
3597 rcu_torture_reader_mbchk = NULL;
a241ec65 3598
c8e5b163 3599 if (fakewriter_tasks) {
293b93d6 3600 for (i = 0; i < nfakewriters; i++)
9c029b86
PM
3601 torture_stop_kthread(rcu_torture_fakewriter,
3602 fakewriter_tasks[i]);
b772e1dd
JT
3603 kfree(fakewriter_tasks);
3604 fakewriter_tasks = NULL;
3605 }
3606
dddcddef
Z
3607 if (cur_ops->get_gp_data)
3608 cur_ops->get_gp_data(&flags, &gp_seq);
959954df
JFG
3609 pr_alert("%s: End-test grace-period state: g%ld f%#x total-gps=%ld\n",
3610 cur_ops->name, (long)gp_seq, flags,
3611 rcutorture_seq_diff(gp_seq, start_gp_seq));
9c029b86
PM
3612 torture_stop_kthread(rcu_torture_stats, stats_task);
3613 torture_stop_kthread(rcu_torture_fqs, fqs_task);
fd13fe16 3614 if (rcu_torture_can_boost() && rcutor_hp >= 0)
0ffd374b 3615 cpuhp_remove_state(rcutor_hp);
bf66f18e 3616
ca1d51ed 3617 /*
62a1a945 3618 * Wait for all RCU callbacks to fire, then do torture-type-specific
ca1d51ed
PM
3619 * cleanup operations.
3620 */
6f81bd6a
PM
3621 if (cur_ops->cb_barrier != NULL) {
3622 pr_info("%s: Invoking %pS().\n", __func__, cur_ops->cb_barrier);
2326974d 3623 cur_ops->cb_barrier();
6f81bd6a 3624 }
ca1d51ed
PM
3625 if (cur_ops->cleanup != NULL)
3626 cur_ops->cleanup();
a241ec65 3627
7ab2bd31
PM
3628 rcu_torture_mem_dump_obj();
3629
a241ec65 3630 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
72e9bb54 3631
c116dba6
PM
3632 if (err_segs_recorded) {
3633 pr_alert("Failure/close-call rcutorture reader segments:\n");
3634 if (rt_read_nsegs == 0)
3635 pr_alert("\t: No segments recorded!!!\n");
3636 firsttime = 1;
3637 for (i = 0; i < rt_read_nsegs; i++) {
16338e7c 3638 pr_alert("\t%d: %#4x", i, err_segs[i].rt_readstate);
c116dba6
PM
3639 if (err_segs[i].rt_delay_jiffies != 0) {
3640 pr_cont("%s%ldjiffies", firsttime ? "" : "+",
3641 err_segs[i].rt_delay_jiffies);
3642 firsttime = 0;
3643 }
885a6f47
PM
3644 if (IS_ENABLED(CONFIG_RCU_TORTURE_TEST_LOG_CPU)) {
3645 pr_cont(" CPU %2d", err_segs[i].rt_cpu);
3646 if (err_segs[i].rt_cpu != err_segs[i].rt_end_cpu)
3647 pr_cont("->%-2d", err_segs[i].rt_end_cpu);
3648 else
3649 pr_cont(" ...");
3650 }
c116dba6 3651 if (err_segs[i].rt_delay_ms != 0) {
16338e7c 3652 pr_cont(" %s%ldms", firsttime ? "" : "+",
c116dba6
PM
3653 err_segs[i].rt_delay_ms);
3654 firsttime = 0;
3655 }
3656 if (err_segs[i].rt_delay_us != 0) {
16338e7c 3657 pr_cont(" %s%ldus", firsttime ? "" : "+",
c116dba6
PM
3658 err_segs[i].rt_delay_us);
3659 firsttime = 0;
3660 }
16338e7c
PM
3661 pr_cont("%s", err_segs[i].rt_preempted ? " preempted" : "");
3662 if (err_segs[i].rt_readstate & RCUTORTURE_RDR_BH)
3663 pr_cont(" BH");
3664 if (err_segs[i].rt_readstate & RCUTORTURE_RDR_IRQ)
3665 pr_cont(" IRQ");
3666 if (err_segs[i].rt_readstate & RCUTORTURE_RDR_PREEMPT)
3667 pr_cont(" PREEMPT");
3668 if (err_segs[i].rt_readstate & RCUTORTURE_RDR_RBH)
3669 pr_cont(" RBH");
3670 if (err_segs[i].rt_readstate & RCUTORTURE_RDR_SCHED)
3671 pr_cont(" SCHED");
3672 if (err_segs[i].rt_readstate & RCUTORTURE_RDR_RCU_1)
3673 pr_cont(" RCU_1");
3674 if (err_segs[i].rt_readstate & RCUTORTURE_RDR_RCU_2)
3675 pr_cont(" RCU_2");
579a05da 3676 pr_cont("\n");
c116dba6
PM
3677
3678 }
0f38c06c
PM
3679 if (rt_read_preempted)
3680 pr_alert("\tReader was preempted.\n");
c116dba6 3681 }
fae4b54f 3682 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
8e8be45e 3683 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
2e9e8081 3684 else if (torture_onoff_failures())
091541bb
PM
3685 rcu_torture_print_module_parms(cur_ops,
3686 "End of test: RCU_HOTPLUG");
95c38322 3687 else
8e8be45e 3688 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
d36a7a0d 3689 torture_cleanup_end();
1c67318b
Z
3690 if (cur_ops->gp_slow_unregister)
3691 cur_ops->gp_slow_unregister(NULL);
a241ec65
PM
3692}
3693
d2818df1
PM
3694static void rcu_torture_leak_cb(struct rcu_head *rhp)
3695{
3696}
3697
3698static void rcu_torture_err_cb(struct rcu_head *rhp)
3699{
3700 /*
3701 * This -might- happen due to race conditions, but is unlikely.
3702 * The scenario that leads to this happening is that the
3703 * first of the pair of duplicate callbacks is queued,
3704 * someone else starts a grace period that includes that
3705 * callback, then the second of the pair must wait for the
3706 * next grace period. Unlikely, but can happen. If it
3707 * does happen, the debug-objects subsystem won't have splatted.
3708 */
e0d31a34 3709 pr_alert("%s: duplicated callback was invoked.\n", KBUILD_MODNAME);
d2818df1 3710}
d2818df1
PM
3711
3712/*
3713 * Verify that double-free causes debug-objects to complain, but only
3714 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
3715 * cannot be carried out.
3716 */
3717static void rcu_test_debug_objects(void)
3718{
d2818df1
PM
3719 struct rcu_head rh1;
3720 struct rcu_head rh2;
43b39caf
Z
3721 int idx;
3722
3723 if (!IS_ENABLED(CONFIG_DEBUG_OBJECTS_RCU_HEAD)) {
3724 pr_alert("%s: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_%s()\n",
3725 KBUILD_MODNAME, cur_ops->name);
3726 return;
3727 }
3728
3729 if (WARN_ON_ONCE(cur_ops->debug_objects &&
3730 (!cur_ops->call || !cur_ops->cb_barrier)))
3731 return;
3732
edf7b841 3733 struct rcu_head *rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
d2818df1
PM
3734
3735 init_rcu_head_on_stack(&rh1);
3736 init_rcu_head_on_stack(&rh2);
43b39caf 3737 pr_alert("%s: WARN: Duplicate call_%s() test starting.\n", KBUILD_MODNAME, cur_ops->name);
d2818df1
PM
3738
3739 /* Try to queue the rh2 pair of callbacks for the same grace period. */
43b39caf
Z
3740 idx = cur_ops->readlock(); /* Make it impossible to finish a grace period. */
3741 cur_ops->call(&rh1, rcu_torture_leak_cb); /* Start grace period. */
3742 cur_ops->call(&rh2, rcu_torture_leak_cb);
3743 cur_ops->call(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
edf7b841 3744 if (rhp) {
43b39caf
Z
3745 cur_ops->call(rhp, rcu_torture_leak_cb);
3746 cur_ops->call(rhp, rcu_torture_err_cb); /* Another duplicate callback. */
edf7b841 3747 }
43b39caf 3748 cur_ops->readunlock(idx);
d2818df1
PM
3749
3750 /* Wait for them all to get done so we can safely return. */
43b39caf
Z
3751 cur_ops->cb_barrier();
3752 pr_alert("%s: WARN: Duplicate call_%s() test complete.\n", KBUILD_MODNAME, cur_ops->name);
d2818df1
PM
3753 destroy_rcu_head_on_stack(&rh1);
3754 destroy_rcu_head_on_stack(&rh2);
98ea2032 3755 kfree(rhp);
d2818df1
PM
3756}
3757
3a6cb58f
PM
3758static void rcutorture_sync(void)
3759{
3760 static unsigned long n;
3761
3762 if (cur_ops->sync && !(++n & 0xfff))
3763 cur_ops->sync();
3764}
3765
d94f12e8
PM
3766static DEFINE_MUTEX(mut0);
3767static DEFINE_MUTEX(mut1);
3768static DEFINE_MUTEX(mut2);
3769static DEFINE_MUTEX(mut3);
3770static DEFINE_MUTEX(mut4);
3771static DEFINE_MUTEX(mut5);
3772static DEFINE_MUTEX(mut6);
3773static DEFINE_MUTEX(mut7);
3774static DEFINE_MUTEX(mut8);
3775static DEFINE_MUTEX(mut9);
3776
3777static DECLARE_RWSEM(rwsem0);
3778static DECLARE_RWSEM(rwsem1);
3779static DECLARE_RWSEM(rwsem2);
3780static DECLARE_RWSEM(rwsem3);
3781static DECLARE_RWSEM(rwsem4);
3782static DECLARE_RWSEM(rwsem5);
3783static DECLARE_RWSEM(rwsem6);
3784static DECLARE_RWSEM(rwsem7);
3785static DECLARE_RWSEM(rwsem8);
3786static DECLARE_RWSEM(rwsem9);
3787
3788DEFINE_STATIC_SRCU(srcu0);
3789DEFINE_STATIC_SRCU(srcu1);
3790DEFINE_STATIC_SRCU(srcu2);
3791DEFINE_STATIC_SRCU(srcu3);
3792DEFINE_STATIC_SRCU(srcu4);
3793DEFINE_STATIC_SRCU(srcu5);
3794DEFINE_STATIC_SRCU(srcu6);
3795DEFINE_STATIC_SRCU(srcu7);
3796DEFINE_STATIC_SRCU(srcu8);
3797DEFINE_STATIC_SRCU(srcu9);
3798
3799static int srcu_lockdep_next(const char *f, const char *fl, const char *fs, const char *fu, int i,
3800 int cyclelen, int deadlock)
3801{
3802 int j = i + 1;
3803
3804 if (j >= cyclelen)
3805 j = deadlock ? 0 : -1;
3806 if (j >= 0)
3807 pr_info("%s: %s(%d), %s(%d), %s(%d)\n", f, fl, i, fs, j, fu, i);
3808 else
3809 pr_info("%s: %s(%d), %s(%d)\n", f, fl, i, fu, i);
3810 return j;
3811}
3812
3813// Test lockdep on SRCU-based deadlock scenarios.
3814static void rcu_torture_init_srcu_lockdep(void)
3815{
3816 int cyclelen;
3817 int deadlock;
3818 bool err = false;
3819 int i;
3820 int j;
3821 int idx;
3822 struct mutex *muts[] = { &mut0, &mut1, &mut2, &mut3, &mut4,
3823 &mut5, &mut6, &mut7, &mut8, &mut9 };
3824 struct rw_semaphore *rwsems[] = { &rwsem0, &rwsem1, &rwsem2, &rwsem3, &rwsem4,
3825 &rwsem5, &rwsem6, &rwsem7, &rwsem8, &rwsem9 };
3826 struct srcu_struct *srcus[] = { &srcu0, &srcu1, &srcu2, &srcu3, &srcu4,
3827 &srcu5, &srcu6, &srcu7, &srcu8, &srcu9 };
3828 int testtype;
3829
3830 if (!test_srcu_lockdep)
3831 return;
3832
3833 deadlock = test_srcu_lockdep / 1000;
3834 testtype = (test_srcu_lockdep / 10) % 100;
3835 cyclelen = test_srcu_lockdep % 10;
3836 WARN_ON_ONCE(ARRAY_SIZE(muts) != ARRAY_SIZE(srcus));
3837 if (WARN_ONCE(deadlock != !!deadlock,
3838 "%s: test_srcu_lockdep=%d and deadlock digit %d must be zero or one.\n",
3839 __func__, test_srcu_lockdep, deadlock))
3840 err = true;
3841 if (WARN_ONCE(cyclelen <= 0,
3842 "%s: test_srcu_lockdep=%d and cycle-length digit %d must be greater than zero.\n",
3843 __func__, test_srcu_lockdep, cyclelen))
3844 err = true;
3845 if (err)
3846 goto err_out;
3847
3848 if (testtype == 0) {
3849 pr_info("%s: test_srcu_lockdep = %05d: SRCU %d-way %sdeadlock.\n",
3850 __func__, test_srcu_lockdep, cyclelen, deadlock ? "" : "non-");
3851 if (deadlock && cyclelen == 1)
3852 pr_info("%s: Expect hang.\n", __func__);
3853 for (i = 0; i < cyclelen; i++) {
3854 j = srcu_lockdep_next(__func__, "srcu_read_lock", "synchronize_srcu",
3855 "srcu_read_unlock", i, cyclelen, deadlock);
3856 idx = srcu_read_lock(srcus[i]);
3857 if (j >= 0)
3858 synchronize_srcu(srcus[j]);
3859 srcu_read_unlock(srcus[i], idx);
3860 }
3861 return;
3862 }
3863
3864 if (testtype == 1) {
3865 pr_info("%s: test_srcu_lockdep = %05d: SRCU/mutex %d-way %sdeadlock.\n",
3866 __func__, test_srcu_lockdep, cyclelen, deadlock ? "" : "non-");
3867 for (i = 0; i < cyclelen; i++) {
3868 pr_info("%s: srcu_read_lock(%d), mutex_lock(%d), mutex_unlock(%d), srcu_read_unlock(%d)\n",
3869 __func__, i, i, i, i);
3870 idx = srcu_read_lock(srcus[i]);
3871 mutex_lock(muts[i]);
3872 mutex_unlock(muts[i]);
3873 srcu_read_unlock(srcus[i], idx);
3874
3875 j = srcu_lockdep_next(__func__, "mutex_lock", "synchronize_srcu",
3876 "mutex_unlock", i, cyclelen, deadlock);
3877 mutex_lock(muts[i]);
3878 if (j >= 0)
3879 synchronize_srcu(srcus[j]);
3880 mutex_unlock(muts[i]);
3881 }
3882 return;
3883 }
3884
3885 if (testtype == 2) {
3886 pr_info("%s: test_srcu_lockdep = %05d: SRCU/rwsem %d-way %sdeadlock.\n",
3887 __func__, test_srcu_lockdep, cyclelen, deadlock ? "" : "non-");
3888 for (i = 0; i < cyclelen; i++) {
3889 pr_info("%s: srcu_read_lock(%d), down_read(%d), up_read(%d), srcu_read_unlock(%d)\n",
3890 __func__, i, i, i, i);
3891 idx = srcu_read_lock(srcus[i]);
3892 down_read(rwsems[i]);
3893 up_read(rwsems[i]);
3894 srcu_read_unlock(srcus[i], idx);
3895
3896 j = srcu_lockdep_next(__func__, "down_write", "synchronize_srcu",
3897 "up_write", i, cyclelen, deadlock);
3898 down_write(rwsems[i]);
3899 if (j >= 0)
3900 synchronize_srcu(srcus[j]);
3901 up_write(rwsems[i]);
3902 }
3903 return;
3904 }
3905
6e204488
PM
3906#ifdef CONFIG_TASKS_TRACE_RCU
3907 if (testtype == 3) {
3908 pr_info("%s: test_srcu_lockdep = %05d: SRCU and Tasks Trace RCU %d-way %sdeadlock.\n",
3909 __func__, test_srcu_lockdep, cyclelen, deadlock ? "" : "non-");
3910 if (deadlock && cyclelen == 1)
3911 pr_info("%s: Expect hang.\n", __func__);
3912 for (i = 0; i < cyclelen; i++) {
3913 char *fl = i == 0 ? "rcu_read_lock_trace" : "srcu_read_lock";
3914 char *fs = i == cyclelen - 1 ? "synchronize_rcu_tasks_trace"
3915 : "synchronize_srcu";
3916 char *fu = i == 0 ? "rcu_read_unlock_trace" : "srcu_read_unlock";
3917
3918 j = srcu_lockdep_next(__func__, fl, fs, fu, i, cyclelen, deadlock);
3919 if (i == 0)
3920 rcu_read_lock_trace();
3921 else
3922 idx = srcu_read_lock(srcus[i]);
3923 if (j >= 0) {
3924 if (i == cyclelen - 1)
3925 synchronize_rcu_tasks_trace();
3926 else
3927 synchronize_srcu(srcus[j]);
3928 }
3929 if (i == 0)
3930 rcu_read_unlock_trace();
3931 else
3932 srcu_read_unlock(srcus[i], idx);
3933 }
3934 return;
3935 }
3936#endif // #ifdef CONFIG_TASKS_TRACE_RCU
3937
d94f12e8
PM
3938err_out:
3939 pr_info("%s: test_srcu_lockdep = %05d does nothing.\n", __func__, test_srcu_lockdep);
3940 pr_info("%s: test_srcu_lockdep = DNNL.\n", __func__);
3941 pr_info("%s: D: Deadlock if nonzero.\n", __func__);
6e204488 3942 pr_info("%s: NN: Test number, 0=SRCU, 1=SRCU/mutex, 2=SRCU/rwsem, 3=SRCU/Tasks Trace RCU.\n", __func__);
d94f12e8 3943 pr_info("%s: L: Cycle length.\n", __func__);
6e204488
PM
3944 if (!IS_ENABLED(CONFIG_TASKS_TRACE_RCU))
3945 pr_info("%s: NN=3 disallowed because kernel is built with CONFIG_TASKS_TRACE_RCU=n\n", __func__);
d94f12e8
PM
3946}
3947
6f8bc500 3948static int __init
a241ec65
PM
3949rcu_torture_init(void)
3950{
c04dd09b 3951 long i;
a241ec65
PM
3952 int cpu;
3953 int firsterr = 0;
959954df
JFG
3954 int flags = 0;
3955 unsigned long gp_seq = 0;
2ec1f2d9 3956 static struct rcu_torture_ops *torture_ops[] = {
40c1278a 3957 &rcu_ops, &rcu_busted_ops, &srcu_ops, &srcud_ops, &busted_srcud_ops,
4c3f7b0e 3958 TASKS_OPS TASKS_RUDE_OPS TASKS_TRACING_OPS
40c1278a 3959 &trivial_ops,
2ec1f2d9 3960 };
a241ec65 3961
a2f2577d 3962 if (!torture_init_begin(torture_type, verbose))
5228084e 3963 return -EBUSY;
343e9099 3964
a241ec65 3965 /* Process args and tell the world that the torturer is on the job. */
ade5fb81 3966 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
72e9bb54 3967 cur_ops = torture_ops[i];
ade5fb81 3968 if (strcmp(torture_type, cur_ops->name) == 0)
72e9bb54 3969 break;
72e9bb54 3970 }
ade5fb81 3971 if (i == ARRAY_SIZE(torture_ops)) {
2caa1e44
PM
3972 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
3973 torture_type);
3974 pr_alert("rcu-torture types:");
cf886c44 3975 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
a7538352
JP
3976 pr_cont(" %s", torture_ops[i]->name);
3977 pr_cont("\n");
889d487a 3978 firsterr = -EINVAL;
b813afae 3979 cur_ops = NULL;
889d487a 3980 goto unwind;
72e9bb54 3981 }
bf66f18e 3982 if (cur_ops->fqs == NULL && fqs_duration != 0) {
2caa1e44 3983 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
bf66f18e
PM
3984 fqs_duration = 0;
3985 }
4f02ac23
Z
3986 if (nocbs_nthreads != 0 && (cur_ops != &rcu_ops ||
3987 !IS_ENABLED(CONFIG_RCU_NOCB_CPU))) {
3988 pr_alert("rcu-torture types: %s and CONFIG_RCU_NOCB_CPU=%d, nocb toggle disabled.\n",
3989 cur_ops->name, IS_ENABLED(CONFIG_RCU_NOCB_CPU));
3990 nocbs_nthreads = 0;
3991 }
c8e5b163 3992 if (cur_ops->init)
889d487a 3993 cur_ops->init();
72e9bb54 3994
d94f12e8
PM
3995 rcu_torture_init_srcu_lockdep();
3996
64e4b43a 3997 if (nreaders >= 0) {
a241ec65 3998 nrealreaders = nreaders;
64e4b43a 3999 } else {
3838cc18 4000 nrealreaders = num_online_cpus() - 2 - nreaders;
64e4b43a
PM
4001 if (nrealreaders <= 0)
4002 nrealreaders = 1;
4003 }
8e8be45e 4004 rcu_torture_print_module_parms(cur_ops, "Start of test");
dddcddef
Z
4005 if (cur_ops->get_gp_data)
4006 cur_ops->get_gp_data(&flags, &gp_seq);
959954df
JFG
4007 start_gp_seq = gp_seq;
4008 pr_alert("%s: Start-test grace-period state: g%ld f%#x\n",
4009 cur_ops->name, (long)gp_seq, flags);
a241ec65
PM
4010
4011 /* Set up the freelist. */
4012
4013 INIT_LIST_HEAD(&rcu_torture_freelist);
788e770e 4014 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
996417d2 4015 rcu_tortures[i].rtort_mbtest = 0;
a241ec65
PM
4016 list_add_tail(&rcu_tortures[i].rtort_free,
4017 &rcu_torture_freelist);
4018 }
4019
4020 /* Initialize the statistics so that each run gets its own numbers. */
4021
4022 rcu_torture_current = NULL;
4023 rcu_torture_current_version = 0;
4024 atomic_set(&n_rcu_torture_alloc, 0);
4025 atomic_set(&n_rcu_torture_alloc_fail, 0);
4026 atomic_set(&n_rcu_torture_free, 0);
996417d2 4027 atomic_set(&n_rcu_torture_mberror, 0);
00504537
PM
4028 atomic_set(&n_rcu_torture_mbchk_fail, 0);
4029 atomic_set(&n_rcu_torture_mbchk_tries, 0);
996417d2 4030 atomic_set(&n_rcu_torture_error, 0);
fae4b54f 4031 n_rcu_torture_barrier_error = 0;
8e8be45e 4032 n_rcu_torture_boost_ktrerror = 0;
8e8be45e
PM
4033 n_rcu_torture_boost_failure = 0;
4034 n_rcu_torture_boosts = 0;
a241ec65
PM
4035 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
4036 atomic_set(&rcu_torture_wcount[i], 0);
0a945022 4037 for_each_possible_cpu(cpu) {
a241ec65
PM
4038 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
4039 per_cpu(rcu_torture_count, cpu)[i] = 0;
4040 per_cpu(rcu_torture_batch, cpu)[i] = 0;
4041 }
4042 }
c116dba6
PM
4043 err_segs_recorded = 0;
4044 rt_read_nsegs = 0;
a241ec65
PM
4045
4046 /* Start up the kthreads. */
4047
18fbf307 4048 rcu_torture_write_types();
47cf29b9
PM
4049 firsterr = torture_create_kthread(rcu_torture_writer, NULL,
4050 writer_task);
efeff6b3 4051 if (torture_init_error(firsterr))
a241ec65 4052 goto unwind;
4444d852 4053 if (nfakewriters > 0) {
68a675d4 4054 fakewriter_tasks = kcalloc(nfakewriters,
4444d852
PM
4055 sizeof(fakewriter_tasks[0]),
4056 GFP_KERNEL);
4057 if (fakewriter_tasks == NULL) {
81faa4f6 4058 TOROUT_ERRSTRING("out of memory");
4444d852
PM
4059 firsterr = -ENOMEM;
4060 goto unwind;
4061 }
b772e1dd
JT
4062 }
4063 for (i = 0; i < nfakewriters; i++) {
47cf29b9
PM
4064 firsterr = torture_create_kthread(rcu_torture_fakewriter,
4065 NULL, fakewriter_tasks[i]);
efeff6b3 4066 if (torture_init_error(firsterr))
b772e1dd 4067 goto unwind;
b772e1dd 4068 }
68a675d4 4069 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
a241ec65 4070 GFP_KERNEL);
00504537
PM
4071 rcu_torture_reader_mbchk = kcalloc(nrealreaders, sizeof(*rcu_torture_reader_mbchk),
4072 GFP_KERNEL);
4073 if (!reader_tasks || !rcu_torture_reader_mbchk) {
81faa4f6 4074 TOROUT_ERRSTRING("out of memory");
a241ec65
PM
4075 firsterr = -ENOMEM;
4076 goto unwind;
4077 }
4078 for (i = 0; i < nrealreaders; i++) {
00504537 4079 rcu_torture_reader_mbchk[i].rtc_chkrdr = -1;
c04dd09b 4080 firsterr = torture_create_kthread(rcu_torture_reader, (void *)i,
47cf29b9 4081 reader_tasks[i]);
efeff6b3 4082 if (torture_init_error(firsterr))
a241ec65 4083 goto unwind;
a241ec65 4084 }
2c4319bd
PM
4085 nrealnocbers = nocbs_nthreads;
4086 if (WARN_ON(nrealnocbers < 0))
4087 nrealnocbers = 1;
4088 if (WARN_ON(nocbs_toggle < 0))
4089 nocbs_toggle = HZ;
4090 if (nrealnocbers > 0) {
4091 nocb_tasks = kcalloc(nrealnocbers, sizeof(nocb_tasks[0]), GFP_KERNEL);
4092 if (nocb_tasks == NULL) {
81faa4f6 4093 TOROUT_ERRSTRING("out of memory");
2c4319bd
PM
4094 firsterr = -ENOMEM;
4095 goto unwind;
4096 }
4097 } else {
4098 nocb_tasks = NULL;
4099 }
4100 for (i = 0; i < nrealnocbers; i++) {
4101 firsterr = torture_create_kthread(rcu_nocb_toggle, NULL, nocb_tasks[i]);
efeff6b3 4102 if (torture_init_error(firsterr))
2c4319bd
PM
4103 goto unwind;
4104 }
a241ec65 4105 if (stat_interval > 0) {
47cf29b9
PM
4106 firsterr = torture_create_kthread(rcu_torture_stats, NULL,
4107 stats_task);
efeff6b3 4108 if (torture_init_error(firsterr))
a241ec65 4109 goto unwind;
a241ec65 4110 }
e8e255f7 4111 if (test_no_idle_hz && shuffle_interval > 0) {
3808dc9f 4112 firsterr = torture_shuffle_init(shuffle_interval * HZ);
efeff6b3 4113 if (torture_init_error(firsterr))
d84f5203 4114 goto unwind;
d84f5203 4115 }
d120f65f
PM
4116 if (stutter < 0)
4117 stutter = 0;
4118 if (stutter) {
ff3bf92d
PM
4119 int t;
4120
4121 t = cur_ops->stall_dur ? cur_ops->stall_dur() : stutter * HZ;
4122 firsterr = torture_stutter_init(stutter * HZ, t);
efeff6b3 4123 if (torture_init_error(firsterr))
d120f65f 4124 goto unwind;
d120f65f 4125 }
bf66f18e
PM
4126 if (fqs_duration < 0)
4127 fqs_duration = 0;
90f1015d
Z
4128 if (fqs_holdoff < 0)
4129 fqs_holdoff = 0;
4130 if (fqs_duration && fqs_holdoff) {
628edaa5 4131 /* Create the fqs thread */
d0d0606e
PM
4132 firsterr = torture_create_kthread(rcu_torture_fqs, NULL,
4133 fqs_task);
efeff6b3 4134 if (torture_init_error(firsterr))
bf66f18e 4135 goto unwind;
bf66f18e 4136 }
8e8be45e
PM
4137 if (test_boost_interval < 1)
4138 test_boost_interval = 1;
4139 if (test_boost_duration < 2)
4140 test_boost_duration = 2;
4babd855 4141 if (rcu_torture_can_boost()) {
8e8be45e
PM
4142
4143 boost_starttime = jiffies + test_boost_interval * HZ;
0ffd374b
SAS
4144
4145 firsterr = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "RCU_TORTURE",
4146 rcutorture_booster_init,
4147 rcutorture_booster_cleanup);
0ffd374b 4148 rcutor_hp = firsterr;
efeff6b3 4149 if (torture_init_error(firsterr))
0ffd374b 4150 goto unwind;
8e8be45e 4151 }
60013d5d 4152 shutdown_jiffies = jiffies + shutdown_secs * HZ;
01025ebc 4153 firsterr = torture_shutdown_init(shutdown_secs, rcu_torture_cleanup);
efeff6b3 4154 if (torture_init_error(firsterr))
37e377d2 4155 goto unwind;
3a6cb58f
PM
4156 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval,
4157 rcutorture_sync);
efeff6b3 4158 if (torture_init_error(firsterr))
37e377d2 4159 goto unwind;
01025ebc 4160 firsterr = rcu_torture_stall_init();
efeff6b3 4161 if (torture_init_error(firsterr))
1b27291b
PM
4162 goto unwind;
4163 firsterr = rcu_torture_fwd_prog_init();
efeff6b3 4164 if (torture_init_error(firsterr))
37e377d2 4165 goto unwind;
01025ebc 4166 firsterr = rcu_torture_barrier_init();
efeff6b3 4167 if (torture_init_error(firsterr))
4a5f133c
PM
4168 goto unwind;
4169 firsterr = rcu_torture_read_exit_init();
efeff6b3 4170 if (torture_init_error(firsterr))
fae4b54f 4171 goto unwind;
584975cc
PM
4172 if (preempt_duration > 0) {
4173 firsterr = torture_create_kthread(rcu_torture_preempt, NULL, preempt_task);
4174 if (torture_init_error(firsterr))
4175 goto unwind;
4176 }
d2818df1
PM
4177 if (object_debug)
4178 rcu_test_debug_objects();
b5daa8f3 4179 torture_init_end();
1c67318b
Z
4180 if (cur_ops->gp_slow_register && !WARN_ON_ONCE(!cur_ops->gp_slow_unregister))
4181 cur_ops->gp_slow_register(&rcu_fwd_cb_nodelay);
a241ec65
PM
4182 return 0;
4183
4184unwind:
b5daa8f3 4185 torture_init_end();
a241ec65 4186 rcu_torture_cleanup();
4994684c
PM
4187 if (shutdown_secs) {
4188 WARN_ON(!IS_MODULE(CONFIG_RCU_TORTURE_TEST));
4189 kernel_power_off();
4190 }
a241ec65
PM
4191 return firsterr;
4192}
4193
4194module_init(rcu_torture_init);
4195module_exit(rcu_torture_cleanup);
This page took 2.244411 seconds and 5 git commands to generate.