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