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