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