rcutorture: Print forward-progress test interval on error
[linux-block.git] / kernel / rcu / rcutorture.c
CommitLineData
a241ec65 1/*
29766f1e 2 * Read-Copy Update module-based torture test facility
a241ec65
PM
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
87de1cfd
PM
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
a241ec65 17 *
b772e1dd 18 * Copyright (C) IBM Corporation, 2005, 2006
a241ec65
PM
19 *
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
e0198b29 21 * Josh Triplett <josh@joshtriplett.org>
a241ec65
PM
22 *
23 * See also: Documentation/RCU/torture.txt
24 */
60500037
PM
25
26#define pr_fmt(fmt) fmt
27
a241ec65
PM
28#include <linux/types.h>
29#include <linux/kernel.h>
30#include <linux/init.h>
31#include <linux/module.h>
32#include <linux/kthread.h>
33#include <linux/err.h>
34#include <linux/spinlock.h>
35#include <linux/smp.h>
36#include <linux/rcupdate.h>
37#include <linux/interrupt.h>
174cd4b1 38#include <linux/sched/signal.h>
ae7e81c0 39#include <uapi/linux/sched/types.h>
60063497 40#include <linux/atomic.h>
a241ec65 41#include <linux/bitops.h>
a241ec65
PM
42#include <linux/completion.h>
43#include <linux/moduleparam.h>
44#include <linux/percpu.h>
45#include <linux/notifier.h>
343e9099 46#include <linux/reboot.h>
83144186 47#include <linux/freezer.h>
a241ec65 48#include <linux/cpu.h>
a241ec65 49#include <linux/delay.h>
a241ec65 50#include <linux/stat.h>
b2896d2e 51#include <linux/srcu.h>
1aeb272c 52#include <linux/slab.h>
52494535 53#include <linux/trace_clock.h>
f07767fd 54#include <asm/byteorder.h>
51b1130e 55#include <linux/torture.h>
38706bc5 56#include <linux/vmalloc.h>
0032f4e8 57#include <linux/sched/debug.h>
450efca7 58#include <linux/sched/sysctl.h>
a241ec65 59
25c36329
PM
60#include "rcu.h"
61
a241ec65 62MODULE_LICENSE("GPL");
e0198b29 63MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and Josh Triplett <josh@joshtriplett.org>");
a241ec65 64
4102adab 65
2397d072
PM
66/* Bits for ->extendables field, extendables param, and related definitions. */
67#define RCUTORTURE_RDR_SHIFT 8 /* Put SRCU index in upper bits. */
68#define RCUTORTURE_RDR_MASK ((1 << RCUTORTURE_RDR_SHIFT) - 1)
69#define RCUTORTURE_RDR_BH 0x1 /* Extend readers by disabling bh. */
70#define RCUTORTURE_RDR_IRQ 0x2 /* ... disabling interrupts. */
71#define RCUTORTURE_RDR_PREEMPT 0x4 /* ... disabling preemption. */
72#define RCUTORTURE_RDR_RCU 0x8 /* ... entering another RCU reader. */
bf1bef50 73#define RCUTORTURE_RDR_NBITS 4 /* Number of bits defined above. */
2397d072
PM
74#define RCUTORTURE_MAX_EXTEND (RCUTORTURE_RDR_BH | RCUTORTURE_RDR_IRQ | \
75 RCUTORTURE_RDR_PREEMPT)
76#define RCUTORTURE_RDR_MAX_LOOPS 0x7 /* Maximum reader extensions. */
77 /* Must be power of two minus one. */
78
38706bc5
PM
79torture_param(int, cbflood_inter_holdoff, HZ,
80 "Holdoff between floods (jiffies)");
81torture_param(int, cbflood_intra_holdoff, 1,
82 "Holdoff between bursts (jiffies)");
83torture_param(int, cbflood_n_burst, 3, "# bursts in flood, zero to disable");
84torture_param(int, cbflood_n_per_burst, 20000,
85 "# callbacks per burst in flood");
2397d072
PM
86torture_param(int, extendables, RCUTORTURE_MAX_EXTEND,
87 "Extend readers by disabling bh (1), irqs (2), or preempt (4)");
9e250225
PM
88torture_param(int, fqs_duration, 0,
89 "Duration of fqs bursts (us), 0 to disable");
90torture_param(int, fqs_holdoff, 0, "Holdoff time within fqs bursts (us)");
91torture_param(int, fqs_stutter, 3, "Wait time between fqs bursts (s)");
1b27291b
PM
92torture_param(bool, fwd_progress, 1, "Test grace-period forward progress");
93torture_param(int, fwd_progress_div, 4, "Fraction of CPU stall to wait");
94torture_param(int, fwd_progress_holdoff, 60,
95 "Time between forward-progress tests (s)");
96torture_param(bool, fwd_progress_need_resched, 1,
97 "Hide cond_resched() behind need_resched()");
a48f3fad 98torture_param(bool, gp_cond, false, "Use conditional/async GP wait primitives");
9e250225
PM
99torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
100torture_param(bool, gp_normal, false,
101 "Use normal (non-expedited) GP wait primitives");
f0bf8fab 102torture_param(bool, gp_sync, false, "Use synchronous GP wait primitives");
9e250225
PM
103torture_param(int, irqreader, 1, "Allow RCU readers from irq handlers");
104torture_param(int, n_barrier_cbs, 0,
105 "# of callbacks/kthreads for barrier testing");
106torture_param(int, nfakewriters, 4, "Number of RCU fake writer threads");
107torture_param(int, nreaders, -1, "Number of RCU reader threads");
108torture_param(int, object_debug, 0,
109 "Enable debug-object double call_rcu() testing");
110torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
111torture_param(int, onoff_interval, 0,
028be12b 112 "Time between CPU hotplugs (jiffies), 0=disable");
9e250225
PM
113torture_param(int, shuffle_interval, 3, "Number of seconds between shuffles");
114torture_param(int, shutdown_secs, 0, "Shutdown time (s), <= zero to disable.");
115torture_param(int, stall_cpu, 0, "Stall duration (s), zero to disable.");
116torture_param(int, stall_cpu_holdoff, 10,
117 "Time to wait before starting stall (s).");
2b1516e5 118torture_param(int, stall_cpu_irqsoff, 0, "Disable interrupts while stalling.");
9e250225
PM
119torture_param(int, stat_interval, 60,
120 "Number of seconds between stats printk()s");
121torture_param(int, stutter, 5, "Number of seconds to run/halt test");
122torture_param(int, test_boost, 1, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
123torture_param(int, test_boost_duration, 4,
124 "Duration of each boost test, seconds.");
125torture_param(int, test_boost_interval, 7,
126 "Interval between boost tests, seconds.");
127torture_param(bool, test_no_idle_hz, true,
128 "Test support for tickless idle CPUs");
90127d60 129torture_param(int, verbose, 1,
b5daa8f3 130 "Enable verbose debugging printk()s");
9e250225 131
d10453e9 132static char *torture_type = "rcu";
d6ad6711 133module_param(torture_type, charp, 0444);
d10453e9 134MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, ...)");
a241ec65 135
a241ec65 136static int nrealreaders;
38706bc5 137static int ncbflooders;
a241ec65 138static struct task_struct *writer_task;
b772e1dd 139static struct task_struct **fakewriter_tasks;
a241ec65
PM
140static struct task_struct **reader_tasks;
141static struct task_struct *stats_task;
38706bc5 142static struct task_struct **cbflood_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? */
38706bc5 180static atomic_long_t n_cbfloods;
e3033736 181static struct list_head rcu_torture_removed;
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
207static const char *rcu_torture_writer_state_getname(void)
208{
209 unsigned int i = READ_ONCE(rcu_torture_writer_state);
210
211 if (i >= ARRAY_SIZE(rcu_torture_writer_state_names))
212 return "???";
213 return rcu_torture_writer_state_names[i];
214}
ad0dc7f9 215
3acf4a9a 216#if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
8e8be45e 217#define rcu_can_boost() 1
3acf4a9a 218#else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
8e8be45e 219#define rcu_can_boost() 0
3acf4a9a 220#endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
8e8be45e 221
e4aa0da3
SR
222#ifdef CONFIG_RCU_TRACE
223static u64 notrace rcu_trace_clock_local(void)
224{
225 u64 ts = trace_clock_local();
a3b7b6c2
PM
226
227 (void)do_div(ts, NSEC_PER_USEC);
e4aa0da3
SR
228 return ts;
229}
230#else /* #ifdef CONFIG_RCU_TRACE */
231static u64 notrace rcu_trace_clock_local(void)
232{
233 return 0ULL;
234}
235#endif /* #else #ifdef CONFIG_RCU_TRACE */
236
8e8be45e 237static unsigned long boost_starttime; /* jiffies of next boost test start. */
58ade2db 238static DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
8e8be45e 239 /* and boost task create/destroy. */
fae4b54f 240static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
c6ebcbb6 241static bool barrier_phase; /* Test phase. */
fae4b54f
PM
242static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
243static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
244static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
8e8be45e 245
a241ec65
PM
246/*
247 * Allocate an element from the rcu_tortures pool.
248 */
97a41e26 249static struct rcu_torture *
a241ec65
PM
250rcu_torture_alloc(void)
251{
252 struct list_head *p;
253
adac1665 254 spin_lock_bh(&rcu_torture_lock);
a241ec65
PM
255 if (list_empty(&rcu_torture_freelist)) {
256 atomic_inc(&n_rcu_torture_alloc_fail);
adac1665 257 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
258 return NULL;
259 }
260 atomic_inc(&n_rcu_torture_alloc);
261 p = rcu_torture_freelist.next;
262 list_del_init(p);
adac1665 263 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
264 return container_of(p, struct rcu_torture, rtort_free);
265}
266
267/*
268 * Free an element to the rcu_tortures pool.
269 */
270static void
271rcu_torture_free(struct rcu_torture *p)
272{
273 atomic_inc(&n_rcu_torture_free);
adac1665 274 spin_lock_bh(&rcu_torture_lock);
a241ec65 275 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
adac1665 276 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
277}
278
72e9bb54
PM
279/*
280 * Operations vector for selecting different types of tests.
281 */
282
283struct rcu_torture_ops {
ad0dc7f9 284 int ttype;
72e9bb54 285 void (*init)(void);
ca1d51ed 286 void (*cleanup)(void);
72e9bb54 287 int (*readlock)(void);
51b1130e 288 void (*read_delay)(struct torture_random_state *rrsp);
72e9bb54 289 void (*readunlock)(int idx);
17ef2fe9 290 unsigned long (*get_gp_seq)(void);
d7219312 291 unsigned long (*gp_diff)(unsigned long new, unsigned long old);
0acc512c 292 void (*deferred_free)(struct rcu_torture *p);
b772e1dd 293 void (*sync)(void);
2ec1f2d9 294 void (*exp_sync)(void);
a48f3fad
PM
295 unsigned long (*get_state)(void);
296 void (*cond_sync)(unsigned long oldstate);
db3e8db4 297 call_rcu_func_t call;
2326974d 298 void (*cb_barrier)(void);
bf66f18e 299 void (*fqs)(void);
eea203fe 300 void (*stats)(void);
1b27291b 301 int (*stall_dur)(void);
0acc512c 302 int irq_capable;
8e8be45e 303 int can_boost;
2397d072
PM
304 int extendables;
305 int ext_irq_conflict;
e66c33d5 306 const char *name;
72e9bb54 307};
a71fca58
PM
308
309static struct rcu_torture_ops *cur_ops;
72e9bb54
PM
310
311/*
312 * Definitions for rcu torture testing.
313 */
314
a49a4af7 315static int rcu_torture_read_lock(void) __acquires(RCU)
72e9bb54
PM
316{
317 rcu_read_lock();
318 return 0;
319}
320
51b1130e 321static void rcu_read_delay(struct torture_random_state *rrsp)
b2896d2e 322{
d0af39e8
PM
323 unsigned long started;
324 unsigned long completed;
b8d57a76 325 const unsigned long shortdelay_us = 200;
1e696765 326 unsigned long longdelay_ms = 300;
d0af39e8 327 unsigned long long ts;
b2896d2e 328
b8d57a76
JT
329 /* We want a short delay sometimes to make a reader delay the grace
330 * period, and we want a long delay occasionally to trigger
331 * force_quiescent_state. */
b2896d2e 332
d0af39e8 333 if (!(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms))) {
17ef2fe9 334 started = cur_ops->get_gp_seq();
d0af39e8 335 ts = rcu_trace_clock_local();
1e696765
PM
336 if (preempt_count() & (SOFTIRQ_MASK | HARDIRQ_MASK))
337 longdelay_ms = 5; /* Avoid triggering BH limits. */
b8d57a76 338 mdelay(longdelay_ms);
17ef2fe9 339 completed = cur_ops->get_gp_seq();
d0af39e8
PM
340 do_trace_rcu_torture_read(cur_ops->name, NULL, ts,
341 started, completed);
342 }
51b1130e 343 if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
b8d57a76 344 udelay(shortdelay_us);
51b1130e 345 if (!preempt_count() &&
e8302739 346 !(torture_random(rrsp) % (nrealreaders * 500)))
cc1321c9 347 torture_preempt_schedule(); /* QS only if preemptible. */
b2896d2e
PM
348}
349
a49a4af7 350static void rcu_torture_read_unlock(int idx) __releases(RCU)
72e9bb54
PM
351{
352 rcu_read_unlock();
353}
354
a48f3fad
PM
355/*
356 * Update callback in the pipe. This should be invoked after a grace period.
357 */
358static bool
359rcu_torture_pipe_update_one(struct rcu_torture *rp)
360{
361 int i;
362
363 i = rp->rtort_pipe_count;
364 if (i > RCU_TORTURE_PIPE_LEN)
365 i = RCU_TORTURE_PIPE_LEN;
366 atomic_inc(&rcu_torture_wcount[i]);
367 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
368 rp->rtort_mbtest = 0;
369 return true;
370 }
371 return false;
372}
373
374/*
375 * Update all callbacks in the pipe. Suitable for synchronous grace-period
376 * primitives.
377 */
378static void
379rcu_torture_pipe_update(struct rcu_torture *old_rp)
380{
381 struct rcu_torture *rp;
382 struct rcu_torture *rp1;
383
384 if (old_rp)
385 list_add(&old_rp->rtort_free, &rcu_torture_removed);
386 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
387 if (rcu_torture_pipe_update_one(rp)) {
388 list_del(&rp->rtort_free);
389 rcu_torture_free(rp);
390 }
391 }
392}
393
72e9bb54
PM
394static void
395rcu_torture_cb(struct rcu_head *p)
396{
72e9bb54
PM
397 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
398
36970bb9 399 if (torture_must_stop_irq()) {
72e9bb54
PM
400 /* Test is ending, just drop callbacks on the floor. */
401 /* The next initialization will pick up the pieces. */
402 return;
403 }
a48f3fad 404 if (rcu_torture_pipe_update_one(rp))
72e9bb54 405 rcu_torture_free(rp);
a48f3fad 406 else
0acc512c 407 cur_ops->deferred_free(rp);
72e9bb54
PM
408}
409
6b80da42 410static unsigned long rcu_no_completed(void)
d9a3da06
PM
411{
412 return 0;
413}
414
72e9bb54
PM
415static void rcu_torture_deferred_free(struct rcu_torture *p)
416{
417 call_rcu(&p->rtort_rcu, rcu_torture_cb);
418}
419
e3033736
JT
420static void rcu_sync_torture_init(void)
421{
422 INIT_LIST_HEAD(&rcu_torture_removed);
423}
424
2ec1f2d9 425static struct rcu_torture_ops rcu_ops = {
ad0dc7f9 426 .ttype = RCU_FLAVOR,
0acc512c 427 .init = rcu_sync_torture_init,
0acc512c
PM
428 .readlock = rcu_torture_read_lock,
429 .read_delay = rcu_read_delay,
430 .readunlock = rcu_torture_read_unlock,
17ef2fe9 431 .get_gp_seq = rcu_get_gp_seq,
d7219312 432 .gp_diff = rcu_seq_diff,
2ec1f2d9 433 .deferred_free = rcu_torture_deferred_free,
0acc512c 434 .sync = synchronize_rcu,
2ec1f2d9 435 .exp_sync = synchronize_rcu_expedited,
a48f3fad
PM
436 .get_state = get_state_synchronize_rcu,
437 .cond_sync = cond_synchronize_rcu,
2ec1f2d9
PM
438 .call = call_rcu,
439 .cb_barrier = rcu_barrier,
bf66f18e 440 .fqs = rcu_force_quiescent_state,
d9a3da06 441 .stats = NULL,
1b27291b 442 .stall_dur = rcu_jiffies_till_stall_check,
d9a3da06 443 .irq_capable = 1,
8e8be45e 444 .can_boost = rcu_can_boost(),
2ec1f2d9 445 .name = "rcu"
d9a3da06
PM
446};
447
c32e0660
PM
448/*
449 * Definitions for rcu_bh torture testing.
450 */
451
a49a4af7 452static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
c32e0660
PM
453{
454 rcu_read_lock_bh();
455 return 0;
456}
457
a49a4af7 458static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
c32e0660
PM
459{
460 rcu_read_unlock_bh();
461}
462
c32e0660
PM
463static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
464{
465 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
466}
467
468static struct rcu_torture_ops rcu_bh_ops = {
ad0dc7f9 469 .ttype = RCU_BH_FLAVOR,
2ec1f2d9 470 .init = rcu_sync_torture_init,
0acc512c
PM
471 .readlock = rcu_bh_torture_read_lock,
472 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
473 .readunlock = rcu_bh_torture_read_unlock,
17ef2fe9 474 .get_gp_seq = rcu_bh_get_gp_seq,
d7219312 475 .gp_diff = rcu_seq_diff,
0acc512c 476 .deferred_free = rcu_bh_torture_deferred_free,
bdf2a436 477 .sync = synchronize_rcu_bh,
2ec1f2d9 478 .exp_sync = synchronize_rcu_bh_expedited,
fae4b54f 479 .call = call_rcu_bh,
0acc512c 480 .cb_barrier = rcu_barrier_bh,
bf66f18e 481 .fqs = rcu_bh_force_quiescent_state,
0acc512c
PM
482 .stats = NULL,
483 .irq_capable = 1,
2397d072
PM
484 .extendables = (RCUTORTURE_RDR_BH | RCUTORTURE_RDR_IRQ),
485 .ext_irq_conflict = RCUTORTURE_RDR_RCU,
0acc512c 486 .name = "rcu_bh"
c32e0660
PM
487};
488
ff20e251
PM
489/*
490 * Don't even think about trying any of these in real life!!!
491 * The names includes "busted", and they really means it!
492 * The only purpose of these functions is to provide a buggy RCU
493 * implementation to make sure that rcutorture correctly emits
494 * buggy-RCU error messages.
495 */
496static void rcu_busted_torture_deferred_free(struct rcu_torture *p)
497{
498 /* This is a deliberate bug for testing purposes only! */
499 rcu_torture_cb(&p->rtort_rcu);
500}
501
502static void synchronize_rcu_busted(void)
503{
504 /* This is a deliberate bug for testing purposes only! */
505}
506
507static void
b6a4ae76 508call_rcu_busted(struct rcu_head *head, rcu_callback_t func)
ff20e251
PM
509{
510 /* This is a deliberate bug for testing purposes only! */
511 func(head);
512}
513
514static struct rcu_torture_ops rcu_busted_ops = {
ad0dc7f9 515 .ttype = INVALID_RCU_FLAVOR,
ff20e251
PM
516 .init = rcu_sync_torture_init,
517 .readlock = rcu_torture_read_lock,
518 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
519 .readunlock = rcu_torture_read_unlock,
17ef2fe9 520 .get_gp_seq = rcu_no_completed,
ff20e251
PM
521 .deferred_free = rcu_busted_torture_deferred_free,
522 .sync = synchronize_rcu_busted,
523 .exp_sync = synchronize_rcu_busted,
524 .call = call_rcu_busted,
525 .cb_barrier = NULL,
526 .fqs = NULL,
527 .stats = NULL,
528 .irq_capable = 1,
b3c98314 529 .name = "busted"
ff20e251
PM
530};
531
b2896d2e
PM
532/*
533 * Definitions for srcu torture testing.
534 */
535
cda4dc81 536DEFINE_STATIC_SRCU(srcu_ctl);
ca1d51ed
PM
537static struct srcu_struct srcu_ctld;
538static struct srcu_struct *srcu_ctlp = &srcu_ctl;
b2896d2e 539
ca1d51ed 540static int srcu_torture_read_lock(void) __acquires(srcu_ctlp)
b2896d2e 541{
ca1d51ed 542 return srcu_read_lock(srcu_ctlp);
b2896d2e
PM
543}
544
51b1130e 545static void srcu_read_delay(struct torture_random_state *rrsp)
b2896d2e
PM
546{
547 long delay;
548 const long uspertick = 1000000 / HZ;
549 const long longdelay = 10;
550
551 /* We want there to be long-running readers, but not all the time. */
552
51b1130e
PM
553 delay = torture_random(rrsp) %
554 (nrealreaders * 2 * longdelay * uspertick);
5e741fa9 555 if (!delay && in_task())
b2896d2e 556 schedule_timeout_interruptible(longdelay);
e546f485
LJ
557 else
558 rcu_read_delay(rrsp);
b2896d2e
PM
559}
560
ca1d51ed 561static void srcu_torture_read_unlock(int idx) __releases(srcu_ctlp)
b2896d2e 562{
ca1d51ed 563 srcu_read_unlock(srcu_ctlp, idx);
b2896d2e
PM
564}
565
6b80da42 566static unsigned long srcu_torture_completed(void)
b2896d2e 567{
ca1d51ed 568 return srcu_batches_completed(srcu_ctlp);
b2896d2e
PM
569}
570
9059c940
LJ
571static void srcu_torture_deferred_free(struct rcu_torture *rp)
572{
ca1d51ed 573 call_srcu(srcu_ctlp, &rp->rtort_rcu, rcu_torture_cb);
9059c940
LJ
574}
575
b772e1dd
JT
576static void srcu_torture_synchronize(void)
577{
ca1d51ed 578 synchronize_srcu(srcu_ctlp);
b772e1dd
JT
579}
580
e3f8d378 581static void srcu_torture_call(struct rcu_head *head,
b6a4ae76 582 rcu_callback_t func)
e3f8d378 583{
ca1d51ed 584 call_srcu(srcu_ctlp, head, func);
e3f8d378
PM
585}
586
587static void srcu_torture_barrier(void)
588{
ca1d51ed 589 srcu_barrier(srcu_ctlp);
e3f8d378
PM
590}
591
eea203fe 592static void srcu_torture_stats(void)
b2896d2e 593{
115a1a52 594 srcu_torture_stats_print(srcu_ctlp, torture_type, TORTURE_FLAG);
b2896d2e
PM
595}
596
2ec1f2d9
PM
597static void srcu_torture_synchronize_expedited(void)
598{
ca1d51ed 599 synchronize_srcu_expedited(srcu_ctlp);
2ec1f2d9
PM
600}
601
b2896d2e 602static struct rcu_torture_ops srcu_ops = {
ad0dc7f9 603 .ttype = SRCU_FLAVOR,
cda4dc81 604 .init = rcu_sync_torture_init,
0acc512c
PM
605 .readlock = srcu_torture_read_lock,
606 .read_delay = srcu_read_delay,
607 .readunlock = srcu_torture_read_unlock,
17ef2fe9 608 .get_gp_seq = srcu_torture_completed,
9059c940 609 .deferred_free = srcu_torture_deferred_free,
0acc512c 610 .sync = srcu_torture_synchronize,
2ec1f2d9 611 .exp_sync = srcu_torture_synchronize_expedited,
e3f8d378
PM
612 .call = srcu_torture_call,
613 .cb_barrier = srcu_torture_barrier,
0acc512c 614 .stats = srcu_torture_stats,
5e741fa9 615 .irq_capable = 1,
0acc512c 616 .name = "srcu"
b2896d2e
PM
617};
618
ca1d51ed
PM
619static void srcu_torture_init(void)
620{
621 rcu_sync_torture_init();
622 WARN_ON(init_srcu_struct(&srcu_ctld));
623 srcu_ctlp = &srcu_ctld;
624}
625
626static void srcu_torture_cleanup(void)
627{
f7194ac3
PM
628 static DEFINE_TORTURE_RANDOM(rand);
629
630 if (torture_random(&rand) & 0x800)
631 cleanup_srcu_struct(&srcu_ctld);
632 else
633 cleanup_srcu_struct_quiesced(&srcu_ctld);
ca1d51ed
PM
634 srcu_ctlp = &srcu_ctl; /* In case of a later rcutorture run. */
635}
636
637/* As above, but dynamically allocated. */
638static struct rcu_torture_ops srcud_ops = {
639 .ttype = SRCU_FLAVOR,
640 .init = srcu_torture_init,
641 .cleanup = srcu_torture_cleanup,
642 .readlock = srcu_torture_read_lock,
643 .read_delay = srcu_read_delay,
644 .readunlock = srcu_torture_read_unlock,
17ef2fe9 645 .get_gp_seq = srcu_torture_completed,
ca1d51ed
PM
646 .deferred_free = srcu_torture_deferred_free,
647 .sync = srcu_torture_synchronize,
648 .exp_sync = srcu_torture_synchronize_expedited,
649 .call = srcu_torture_call,
650 .cb_barrier = srcu_torture_barrier,
651 .stats = srcu_torture_stats,
5e741fa9 652 .irq_capable = 1,
ca1d51ed
PM
653 .name = "srcud"
654};
655
2397d072
PM
656/* As above, but broken due to inappropriate reader extension. */
657static struct rcu_torture_ops busted_srcud_ops = {
658 .ttype = SRCU_FLAVOR,
659 .init = srcu_torture_init,
660 .cleanup = srcu_torture_cleanup,
661 .readlock = srcu_torture_read_lock,
662 .read_delay = rcu_read_delay,
663 .readunlock = srcu_torture_read_unlock,
664 .get_gp_seq = srcu_torture_completed,
665 .deferred_free = srcu_torture_deferred_free,
666 .sync = srcu_torture_synchronize,
667 .exp_sync = srcu_torture_synchronize_expedited,
668 .call = srcu_torture_call,
669 .cb_barrier = srcu_torture_barrier,
670 .stats = srcu_torture_stats,
671 .irq_capable = 1,
672 .extendables = RCUTORTURE_MAX_EXTEND,
673 .name = "busted_srcud"
674};
675
4b6c2cca
JT
676/*
677 * Definitions for sched torture testing.
678 */
679
680static int sched_torture_read_lock(void)
681{
682 preempt_disable();
683 return 0;
684}
685
686static void sched_torture_read_unlock(int idx)
687{
688 preempt_enable();
689}
690
2326974d
PM
691static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
692{
693 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
694}
695
4b6c2cca 696static struct rcu_torture_ops sched_ops = {
ad0dc7f9 697 .ttype = RCU_SCHED_FLAVOR,
0acc512c 698 .init = rcu_sync_torture_init,
0acc512c
PM
699 .readlock = sched_torture_read_lock,
700 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
701 .readunlock = sched_torture_read_unlock,
17ef2fe9 702 .get_gp_seq = rcu_sched_get_gp_seq,
d7219312 703 .gp_diff = rcu_seq_diff,
0acc512c 704 .deferred_free = rcu_sched_torture_deferred_free,
bdf2a436 705 .sync = synchronize_sched,
2ec1f2d9 706 .exp_sync = synchronize_sched_expedited,
24560056
PM
707 .get_state = get_state_synchronize_sched,
708 .cond_sync = cond_synchronize_sched,
2ec1f2d9 709 .call = call_rcu_sched,
0acc512c 710 .cb_barrier = rcu_barrier_sched,
bf66f18e 711 .fqs = rcu_sched_force_quiescent_state,
0acc512c
PM
712 .stats = NULL,
713 .irq_capable = 1,
2397d072 714 .extendables = RCUTORTURE_MAX_EXTEND,
0acc512c 715 .name = "sched"
4b6c2cca
JT
716};
717
69c60455
PM
718/*
719 * Definitions for RCU-tasks torture testing.
720 */
721
722static int tasks_torture_read_lock(void)
723{
724 return 0;
725}
726
727static void tasks_torture_read_unlock(int idx)
728{
729}
730
731static void rcu_tasks_torture_deferred_free(struct rcu_torture *p)
732{
733 call_rcu_tasks(&p->rtort_rcu, rcu_torture_cb);
734}
735
736static struct rcu_torture_ops tasks_ops = {
737 .ttype = RCU_TASKS_FLAVOR,
738 .init = rcu_sync_torture_init,
739 .readlock = tasks_torture_read_lock,
740 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
741 .readunlock = tasks_torture_read_unlock,
17ef2fe9 742 .get_gp_seq = rcu_no_completed,
69c60455
PM
743 .deferred_free = rcu_tasks_torture_deferred_free,
744 .sync = synchronize_rcu_tasks,
745 .exp_sync = synchronize_rcu_tasks,
746 .call = call_rcu_tasks,
747 .cb_barrier = rcu_barrier_tasks,
748 .fqs = NULL,
749 .stats = NULL,
750 .irq_capable = 1,
751 .name = "tasks"
752};
753
d7219312
PM
754static unsigned long rcutorture_seq_diff(unsigned long new, unsigned long old)
755{
756 if (!cur_ops->gp_diff)
757 return new - old;
758 return cur_ops->gp_diff(new, old);
759}
760
5be5d1a1
PM
761static bool __maybe_unused torturing_tasks(void)
762{
763 return cur_ops == &tasks_ops;
764}
765
8e8be45e
PM
766/*
767 * RCU torture priority-boost testing. Runs one real-time thread per
768 * CPU for moderate bursts, repeatedly registering RCU callbacks and
769 * spinning waiting for them to be invoked. If a given callback takes
770 * too long to be invoked, we assume that priority inversion has occurred.
771 */
772
773struct rcu_boost_inflight {
774 struct rcu_head rcu;
775 int inflight;
776};
777
778static void rcu_torture_boost_cb(struct rcu_head *head)
779{
780 struct rcu_boost_inflight *rbip =
781 container_of(head, struct rcu_boost_inflight, rcu);
782
6c7ed42c
PM
783 /* Ensure RCU-core accesses precede clearing ->inflight */
784 smp_store_release(&rbip->inflight, 0);
8e8be45e
PM
785}
786
450efca7
JFG
787static int old_rt_runtime = -1;
788
789static void rcu_torture_disable_rt_throttle(void)
790{
791 /*
792 * Disable RT throttling so that rcutorture's boost threads don't get
793 * throttled. Only possible if rcutorture is built-in otherwise the
794 * user should manually do this by setting the sched_rt_period_us and
795 * sched_rt_runtime sysctls.
796 */
797 if (!IS_BUILTIN(CONFIG_RCU_TORTURE_TEST) || old_rt_runtime != -1)
798 return;
799
800 old_rt_runtime = sysctl_sched_rt_runtime;
801 sysctl_sched_rt_runtime = -1;
802}
803
804static void rcu_torture_enable_rt_throttle(void)
805{
806 if (!IS_BUILTIN(CONFIG_RCU_TORTURE_TEST) || old_rt_runtime == -1)
807 return;
808
809 sysctl_sched_rt_runtime = old_rt_runtime;
810 old_rt_runtime = -1;
811}
812
3b745c89
JFG
813static bool rcu_torture_boost_failed(unsigned long start, unsigned long end)
814{
815 if (end - start > test_boost_duration * HZ - HZ / 2) {
816 VERBOSE_TOROUT_STRING("rcu_torture_boost boosting failed");
817 n_rcu_torture_boost_failure++;
818
819 return true; /* failed */
820 }
821
822 return false; /* passed */
823}
824
8e8be45e
PM
825static int rcu_torture_boost(void *arg)
826{
827 unsigned long call_rcu_time;
828 unsigned long endtime;
829 unsigned long oldstarttime;
830 struct rcu_boost_inflight rbi = { .inflight = 0 };
831 struct sched_param sp;
832
5ccf60f2 833 VERBOSE_TOROUT_STRING("rcu_torture_boost started");
8e8be45e
PM
834
835 /* Set real-time priority. */
836 sp.sched_priority = 1;
837 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
5ccf60f2 838 VERBOSE_TOROUT_STRING("rcu_torture_boost RT prio failed!");
8e8be45e
PM
839 n_rcu_torture_boost_rterror++;
840 }
841
561190e3 842 init_rcu_head_on_stack(&rbi.rcu);
8e8be45e
PM
843 /* Each pass through the following loop does one boost-test cycle. */
844 do {
3b745c89
JFG
845 /* Track if the test failed already in this test interval? */
846 bool failed = false;
847
848 /* Increment n_rcu_torture_boosts once per boost-test */
849 while (!kthread_should_stop()) {
850 if (mutex_trylock(&boost_mutex)) {
851 n_rcu_torture_boosts++;
852 mutex_unlock(&boost_mutex);
853 break;
854 }
855 schedule_timeout_uninterruptible(1);
856 }
857 if (kthread_should_stop())
858 goto checkwait;
859
8e8be45e
PM
860 /* Wait for the next test interval. */
861 oldstarttime = boost_starttime;
93898fb1 862 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
0e11c8e8 863 schedule_timeout_interruptible(oldstarttime - jiffies);
628edaa5 864 stutter_wait("rcu_torture_boost");
36970bb9 865 if (torture_must_stop())
8e8be45e
PM
866 goto checkwait;
867 }
868
869 /* Do one boost-test interval. */
870 endtime = oldstarttime + test_boost_duration * HZ;
871 call_rcu_time = jiffies;
93898fb1 872 while (ULONG_CMP_LT(jiffies, endtime)) {
8e8be45e 873 /* If we don't have a callback in flight, post one. */
6c7ed42c
PM
874 if (!smp_load_acquire(&rbi.inflight)) {
875 /* RCU core before ->inflight = 1. */
876 smp_store_release(&rbi.inflight, 1);
8e8be45e 877 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
3b745c89
JFG
878 /* Check if the boost test failed */
879 failed = failed ||
880 rcu_torture_boost_failed(call_rcu_time,
881 jiffies);
8e8be45e
PM
882 call_rcu_time = jiffies;
883 }
628edaa5 884 stutter_wait("rcu_torture_boost");
36970bb9 885 if (torture_must_stop())
8e8be45e
PM
886 goto checkwait;
887 }
888
3b745c89
JFG
889 /*
890 * If boost never happened, then inflight will always be 1, in
891 * this case the boost check would never happen in the above
892 * loop so do another one here.
893 */
894 if (!failed && smp_load_acquire(&rbi.inflight))
895 rcu_torture_boost_failed(call_rcu_time, jiffies);
896
8e8be45e
PM
897 /*
898 * Set the start time of the next test interval.
899 * Yes, this is vulnerable to long delays, but such
900 * delays simply cause a false negative for the next
901 * interval. Besides, we are running at RT priority,
902 * so delays should be relatively rare.
903 */
ab8f11e5
PM
904 while (oldstarttime == boost_starttime &&
905 !kthread_should_stop()) {
8e8be45e
PM
906 if (mutex_trylock(&boost_mutex)) {
907 boost_starttime = jiffies +
908 test_boost_interval * HZ;
8e8be45e
PM
909 mutex_unlock(&boost_mutex);
910 break;
911 }
912 schedule_timeout_uninterruptible(1);
913 }
914
915 /* Go do the stutter. */
628edaa5 916checkwait: stutter_wait("rcu_torture_boost");
36970bb9 917 } while (!torture_must_stop());
8e8be45e
PM
918
919 /* Clean up and exit. */
6c7ed42c 920 while (!kthread_should_stop() || smp_load_acquire(&rbi.inflight)) {
7fafaac5 921 torture_shutdown_absorb("rcu_torture_boost");
8e8be45e 922 schedule_timeout_uninterruptible(1);
7fafaac5 923 }
9d68197c 924 destroy_rcu_head_on_stack(&rbi.rcu);
7fafaac5 925 torture_kthread_stopping("rcu_torture_boost");
8e8be45e
PM
926 return 0;
927}
928
38706bc5
PM
929static void rcu_torture_cbflood_cb(struct rcu_head *rhp)
930{
931}
932
933/*
934 * RCU torture callback-flood kthread. Repeatedly induces bursts of calls
935 * to call_rcu() or analogous, increasing the probability of occurrence
936 * of callback-overflow corner cases.
937 */
938static int
939rcu_torture_cbflood(void *arg)
940{
941 int err = 1;
942 int i;
943 int j;
944 struct rcu_head *rhp;
945
946 if (cbflood_n_per_burst > 0 &&
947 cbflood_inter_holdoff > 0 &&
948 cbflood_intra_holdoff > 0 &&
949 cur_ops->call &&
950 cur_ops->cb_barrier) {
42bc47b3
KC
951 rhp = vmalloc(array3_size(cbflood_n_burst,
952 cbflood_n_per_burst,
953 sizeof(*rhp)));
38706bc5
PM
954 err = !rhp;
955 }
956 if (err) {
957 VERBOSE_TOROUT_STRING("rcu_torture_cbflood disabled: Bad args or OOM");
3a0af333 958 goto wait_for_stop;
38706bc5
PM
959 }
960 VERBOSE_TOROUT_STRING("rcu_torture_cbflood task started");
961 do {
962 schedule_timeout_interruptible(cbflood_inter_holdoff);
963 atomic_long_inc(&n_cbfloods);
964 WARN_ON(signal_pending(current));
965 for (i = 0; i < cbflood_n_burst; i++) {
966 for (j = 0; j < cbflood_n_per_burst; j++) {
967 cur_ops->call(&rhp[i * cbflood_n_per_burst + j],
968 rcu_torture_cbflood_cb);
969 }
970 schedule_timeout_interruptible(cbflood_intra_holdoff);
971 WARN_ON(signal_pending(current));
972 }
973 cur_ops->cb_barrier();
974 stutter_wait("rcu_torture_cbflood");
975 } while (!torture_must_stop());
b8969d1a 976 vfree(rhp);
3a0af333 977wait_for_stop:
38706bc5
PM
978 torture_kthread_stopping("rcu_torture_cbflood");
979 return 0;
980}
981
bf66f18e
PM
982/*
983 * RCU torture force-quiescent-state kthread. Repeatedly induces
984 * bursts of calls to force_quiescent_state(), increasing the probability
985 * of occurrence of some important types of race conditions.
986 */
987static int
988rcu_torture_fqs(void *arg)
989{
990 unsigned long fqs_resume_time;
991 int fqs_burst_remaining;
992
5ccf60f2 993 VERBOSE_TOROUT_STRING("rcu_torture_fqs task started");
bf66f18e
PM
994 do {
995 fqs_resume_time = jiffies + fqs_stutter * HZ;
93898fb1
PM
996 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
997 !kthread_should_stop()) {
bf66f18e
PM
998 schedule_timeout_interruptible(1);
999 }
1000 fqs_burst_remaining = fqs_duration;
93898fb1
PM
1001 while (fqs_burst_remaining > 0 &&
1002 !kthread_should_stop()) {
bf66f18e
PM
1003 cur_ops->fqs();
1004 udelay(fqs_holdoff);
1005 fqs_burst_remaining -= fqs_holdoff;
1006 }
628edaa5 1007 stutter_wait("rcu_torture_fqs");
36970bb9 1008 } while (!torture_must_stop());
7fafaac5 1009 torture_kthread_stopping("rcu_torture_fqs");
bf66f18e
PM
1010 return 0;
1011}
1012
a241ec65
PM
1013/*
1014 * RCU torture writer kthread. Repeatedly substitutes a new structure
1015 * for that pointed to by rcu_torture_current, freeing the old structure
1016 * after a series of grace periods (the "pipeline").
1017 */
1018static int
1019rcu_torture_writer(void *arg)
1020{
9efafb88 1021 bool can_expedite = !rcu_gp_is_expedited() && !rcu_gp_is_normal();
4bb3c5f4 1022 int expediting = 0;
a48f3fad
PM
1023 unsigned long gp_snap;
1024 bool gp_cond1 = gp_cond, gp_exp1 = gp_exp, gp_normal1 = gp_normal;
f0bf8fab 1025 bool gp_sync1 = gp_sync;
a241ec65 1026 int i;
a241ec65
PM
1027 struct rcu_torture *rp;
1028 struct rcu_torture *old_rp;
51b1130e 1029 static DEFINE_TORTURE_RANDOM(rand);
f0bf8fab
PM
1030 int synctype[] = { RTWS_DEF_FREE, RTWS_EXP_SYNC,
1031 RTWS_COND_GET, RTWS_SYNC };
a48f3fad 1032 int nsynctypes = 0;
a241ec65 1033
5ccf60f2 1034 VERBOSE_TOROUT_STRING("rcu_torture_writer task started");
f7c0e6ad 1035 if (!can_expedite)
aa5a8988 1036 pr_alert("%s" TORTURE_FLAG
f7c0e6ad 1037 " GP expediting controlled from boot/sysfs for %s.\n",
aa5a8988 1038 torture_type, cur_ops->name);
dbdf65b1 1039
a48f3fad 1040 /* Initialize synctype[] array. If none set, take default. */
c136f991 1041 if (!gp_cond1 && !gp_exp1 && !gp_normal1 && !gp_sync1)
f0bf8fab 1042 gp_cond1 = gp_exp1 = gp_normal1 = gp_sync1 = true;
db0c1a8a 1043 if (gp_cond1 && cur_ops->get_state && cur_ops->cond_sync) {
a48f3fad 1044 synctype[nsynctypes++] = RTWS_COND_GET;
db0c1a8a
PM
1045 pr_info("%s: Testing conditional GPs.\n", __func__);
1046 } else if (gp_cond && (!cur_ops->get_state || !cur_ops->cond_sync)) {
e0d31a34 1047 pr_alert("%s: gp_cond without primitives.\n", __func__);
db0c1a8a
PM
1048 }
1049 if (gp_exp1 && cur_ops->exp_sync) {
a48f3fad 1050 synctype[nsynctypes++] = RTWS_EXP_SYNC;
db0c1a8a
PM
1051 pr_info("%s: Testing expedited GPs.\n", __func__);
1052 } else if (gp_exp && !cur_ops->exp_sync) {
e0d31a34 1053 pr_alert("%s: gp_exp without primitives.\n", __func__);
db0c1a8a
PM
1054 }
1055 if (gp_normal1 && cur_ops->deferred_free) {
a48f3fad 1056 synctype[nsynctypes++] = RTWS_DEF_FREE;
db0c1a8a
PM
1057 pr_info("%s: Testing asynchronous GPs.\n", __func__);
1058 } else if (gp_normal && !cur_ops->deferred_free) {
e0d31a34 1059 pr_alert("%s: gp_normal without primitives.\n", __func__);
db0c1a8a
PM
1060 }
1061 if (gp_sync1 && cur_ops->sync) {
f0bf8fab 1062 synctype[nsynctypes++] = RTWS_SYNC;
db0c1a8a
PM
1063 pr_info("%s: Testing normal GPs.\n", __func__);
1064 } else if (gp_sync && !cur_ops->sync) {
e0d31a34 1065 pr_alert("%s: gp_sync without primitives.\n", __func__);
db0c1a8a 1066 }
a48f3fad
PM
1067 if (WARN_ONCE(nsynctypes == 0,
1068 "rcu_torture_writer: No update-side primitives.\n")) {
f0bf8fab
PM
1069 /*
1070 * No updates primitives, so don't try updating.
1071 * The resulting test won't be testing much, hence the
1072 * above WARN_ONCE().
1073 */
a48f3fad
PM
1074 rcu_torture_writer_state = RTWS_STOPPING;
1075 torture_kthread_stopping("rcu_torture_writer");
1076 }
1077
a241ec65 1078 do {
ad0dc7f9 1079 rcu_torture_writer_state = RTWS_FIXED_DELAY;
a241ec65 1080 schedule_timeout_uninterruptible(1);
a71fca58
PM
1081 rp = rcu_torture_alloc();
1082 if (rp == NULL)
a241ec65
PM
1083 continue;
1084 rp->rtort_pipe_count = 0;
ad0dc7f9 1085 rcu_torture_writer_state = RTWS_DELAY;
51b1130e 1086 udelay(torture_random(&rand) & 0x3ff);
ad0dc7f9 1087 rcu_torture_writer_state = RTWS_REPLACE;
0ddea0ea
PM
1088 old_rp = rcu_dereference_check(rcu_torture_current,
1089 current == writer_task);
996417d2 1090 rp->rtort_mbtest = 1;
a241ec65 1091 rcu_assign_pointer(rcu_torture_current, rp);
9b2619af 1092 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
c8e5b163 1093 if (old_rp) {
a241ec65
PM
1094 i = old_rp->rtort_pipe_count;
1095 if (i > RCU_TORTURE_PIPE_LEN)
1096 i = RCU_TORTURE_PIPE_LEN;
1097 atomic_inc(&rcu_torture_wcount[i]);
1098 old_rp->rtort_pipe_count++;
a48f3fad
PM
1099 switch (synctype[torture_random(&rand) % nsynctypes]) {
1100 case RTWS_DEF_FREE:
ad0dc7f9 1101 rcu_torture_writer_state = RTWS_DEF_FREE;
2ec1f2d9 1102 cur_ops->deferred_free(old_rp);
a48f3fad
PM
1103 break;
1104 case RTWS_EXP_SYNC:
ad0dc7f9 1105 rcu_torture_writer_state = RTWS_EXP_SYNC;
2ec1f2d9 1106 cur_ops->exp_sync();
a48f3fad
PM
1107 rcu_torture_pipe_update(old_rp);
1108 break;
1109 case RTWS_COND_GET:
1110 rcu_torture_writer_state = RTWS_COND_GET;
1111 gp_snap = cur_ops->get_state();
1112 i = torture_random(&rand) % 16;
1113 if (i != 0)
1114 schedule_timeout_interruptible(i);
1115 udelay(torture_random(&rand) % 1000);
1116 rcu_torture_writer_state = RTWS_COND_SYNC;
1117 cur_ops->cond_sync(gp_snap);
1118 rcu_torture_pipe_update(old_rp);
1119 break;
f0bf8fab
PM
1120 case RTWS_SYNC:
1121 rcu_torture_writer_state = RTWS_SYNC;
1122 cur_ops->sync();
1123 rcu_torture_pipe_update(old_rp);
1124 break;
a48f3fad
PM
1125 default:
1126 WARN_ON_ONCE(1);
1127 break;
2ec1f2d9 1128 }
a241ec65 1129 }
1b27291b
PM
1130 WRITE_ONCE(rcu_torture_current_version,
1131 rcu_torture_current_version + 1);
4bb3c5f4
PM
1132 /* Cycle through nesting levels of rcu_expedite_gp() calls. */
1133 if (can_expedite &&
1134 !(torture_random(&rand) & 0xff & (!!expediting - 1))) {
1135 WARN_ON_ONCE(expediting == 0 && rcu_gp_is_expedited());
1136 if (expediting >= 0)
1137 rcu_expedite_gp();
1138 else
1139 rcu_unexpedite_gp();
1140 if (++expediting > 3)
1141 expediting = -expediting;
f7c0e6ad
PM
1142 } else if (!can_expedite) { /* Disabled during boot, recheck. */
1143 can_expedite = !rcu_gp_is_expedited() &&
1144 !rcu_gp_is_normal();
4bb3c5f4 1145 }
ad0dc7f9 1146 rcu_torture_writer_state = RTWS_STUTTER;
628edaa5 1147 stutter_wait("rcu_torture_writer");
36970bb9 1148 } while (!torture_must_stop());
4bb3c5f4
PM
1149 /* Reset expediting back to unexpedited. */
1150 if (expediting > 0)
1151 expediting = -expediting;
1152 while (can_expedite && expediting++ < 0)
1153 rcu_unexpedite_gp();
1154 WARN_ON_ONCE(can_expedite && rcu_gp_is_expedited());
f7c0e6ad
PM
1155 if (!can_expedite)
1156 pr_alert("%s" TORTURE_FLAG
1157 " Dynamic grace-period expediting was disabled.\n",
1158 torture_type);
ad0dc7f9 1159 rcu_torture_writer_state = RTWS_STOPPING;
7fafaac5 1160 torture_kthread_stopping("rcu_torture_writer");
a241ec65
PM
1161 return 0;
1162}
1163
b772e1dd
JT
1164/*
1165 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
1166 * delay between calls.
1167 */
1168static int
1169rcu_torture_fakewriter(void *arg)
1170{
51b1130e 1171 DEFINE_TORTURE_RANDOM(rand);
b772e1dd 1172
5ccf60f2 1173 VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
d277d868 1174 set_user_nice(current, MAX_NICE);
b772e1dd
JT
1175
1176 do {
51b1130e
PM
1177 schedule_timeout_uninterruptible(1 + torture_random(&rand)%10);
1178 udelay(torture_random(&rand) & 0x3ff);
72472a02 1179 if (cur_ops->cb_barrier != NULL &&
51b1130e 1180 torture_random(&rand) % (nfakewriters * 8) == 0) {
72472a02 1181 cur_ops->cb_barrier();
2ec1f2d9 1182 } else if (gp_normal == gp_exp) {
eb033993 1183 if (cur_ops->sync && torture_random(&rand) & 0x80)
2ec1f2d9 1184 cur_ops->sync();
eb033993 1185 else if (cur_ops->exp_sync)
2ec1f2d9 1186 cur_ops->exp_sync();
eb033993 1187 } else if (gp_normal && cur_ops->sync) {
72472a02 1188 cur_ops->sync();
eb033993 1189 } else if (cur_ops->exp_sync) {
2ec1f2d9
PM
1190 cur_ops->exp_sync();
1191 }
628edaa5 1192 stutter_wait("rcu_torture_fakewriter");
36970bb9 1193 } while (!torture_must_stop());
b772e1dd 1194
7fafaac5 1195 torture_kthread_stopping("rcu_torture_fakewriter");
b772e1dd
JT
1196 return 0;
1197}
1198
f34c8585
PM
1199static void rcu_torture_timer_cb(struct rcu_head *rhp)
1200{
1201 kfree(rhp);
1202}
1203
0729fbf3 1204/*
2397d072
PM
1205 * Do one extension of an RCU read-side critical section using the
1206 * current reader state in readstate (set to zero for initial entry
1207 * to extended critical section), set the new state as specified by
1208 * newstate (set to zero for final exit from extended critical section),
1209 * and random-number-generator state in trsp. If this is neither the
1210 * beginning or end of the critical section and if there was actually a
1211 * change, do a ->read_delay().
0729fbf3 1212 */
2397d072
PM
1213static void rcutorture_one_extend(int *readstate, int newstate,
1214 struct torture_random_state *trsp)
1215{
1216 int idxnew = -1;
1217 int idxold = *readstate;
1218 int statesnew = ~*readstate & newstate;
1219 int statesold = *readstate & ~newstate;
1220
1221 WARN_ON_ONCE(idxold < 0);
1222 WARN_ON_ONCE((idxold >> RCUTORTURE_RDR_SHIFT) > 1);
1223
1224 /* First, put new protection in place to avoid critical-section gap. */
1225 if (statesnew & RCUTORTURE_RDR_BH)
1226 local_bh_disable();
1227 if (statesnew & RCUTORTURE_RDR_IRQ)
1228 local_irq_disable();
1229 if (statesnew & RCUTORTURE_RDR_PREEMPT)
1230 preempt_disable();
1231 if (statesnew & RCUTORTURE_RDR_RCU)
1232 idxnew = cur_ops->readlock() << RCUTORTURE_RDR_SHIFT;
1233
1234 /* Next, remove old protection, irq first due to bh conflict. */
1235 if (statesold & RCUTORTURE_RDR_IRQ)
1236 local_irq_enable();
1237 if (statesold & RCUTORTURE_RDR_BH)
1238 local_bh_enable();
1239 if (statesold & RCUTORTURE_RDR_PREEMPT)
1240 preempt_enable();
1241 if (statesold & RCUTORTURE_RDR_RCU)
1242 cur_ops->readunlock(idxold >> RCUTORTURE_RDR_SHIFT);
1243
1244 /* Delay if neither beginning nor end and there was a change. */
1245 if ((statesnew || statesold) && *readstate && newstate)
1246 cur_ops->read_delay(trsp);
1247
1248 /* Update the reader state. */
1249 if (idxnew == -1)
1250 idxnew = idxold & ~RCUTORTURE_RDR_MASK;
1251 WARN_ON_ONCE(idxnew < 0);
1252 WARN_ON_ONCE((idxnew >> RCUTORTURE_RDR_SHIFT) > 1);
1253 *readstate = idxnew | newstate;
1254 WARN_ON_ONCE((*readstate >> RCUTORTURE_RDR_SHIFT) < 0);
1255 WARN_ON_ONCE((*readstate >> RCUTORTURE_RDR_SHIFT) > 1);
1256}
1257
1258/* Return the biggest extendables mask given current RCU and boot parameters. */
1259static int rcutorture_extend_mask_max(void)
1260{
1261 int mask;
1262
1263 WARN_ON_ONCE(extendables & ~RCUTORTURE_MAX_EXTEND);
1264 mask = extendables & RCUTORTURE_MAX_EXTEND & cur_ops->extendables;
1265 mask = mask | RCUTORTURE_RDR_RCU;
1266 return mask;
1267}
1268
1269/* Return a random protection state mask, but with at least one bit set. */
1270static int
1271rcutorture_extend_mask(int oldmask, struct torture_random_state *trsp)
1272{
1273 int mask = rcutorture_extend_mask_max();
bf1bef50
PM
1274 unsigned long randmask1 = torture_random(trsp) >> 8;
1275 unsigned long randmask2 = randmask1 >> 1;
2397d072
PM
1276
1277 WARN_ON_ONCE(mask >> RCUTORTURE_RDR_SHIFT);
bf1bef50
PM
1278 /* Half the time lots of bits, half the time only one bit. */
1279 if (randmask1 & 0x1)
1280 mask = mask & randmask2;
1281 else
1282 mask = mask & (1 << (randmask2 % RCUTORTURE_RDR_NBITS));
2397d072
PM
1283 if ((mask & RCUTORTURE_RDR_IRQ) &&
1284 !(mask & RCUTORTURE_RDR_BH) &&
1285 (oldmask & RCUTORTURE_RDR_BH))
1286 mask |= RCUTORTURE_RDR_BH; /* Can't enable bh w/irq disabled. */
1287 if ((mask & RCUTORTURE_RDR_IRQ) &&
1288 !(mask & cur_ops->ext_irq_conflict) &&
1289 (oldmask & cur_ops->ext_irq_conflict))
1290 mask |= cur_ops->ext_irq_conflict; /* Or if readers object. */
1291 return mask ?: RCUTORTURE_RDR_RCU;
1292}
1293
1294/*
1295 * Do a randomly selected number of extensions of an existing RCU read-side
1296 * critical section.
1297 */
1298static void rcutorture_loop_extend(int *readstate,
1299 struct torture_random_state *trsp)
1300{
1301 int i;
1302 int mask = rcutorture_extend_mask_max();
1303
1304 WARN_ON_ONCE(!*readstate); /* -Existing- RCU read-side critsect! */
1305 if (!((mask - 1) & mask))
1306 return; /* Current RCU flavor not extendable. */
1307 i = (torture_random(trsp) >> 3) & RCUTORTURE_RDR_MAX_LOOPS;
1308 while (i--) {
1309 mask = rcutorture_extend_mask(*readstate, trsp);
1310 rcutorture_one_extend(readstate, mask, trsp);
1311 }
1312}
1313
6b06aa72
PM
1314/*
1315 * Do one read-side critical section, returning false if there was
1316 * no data to read. Can be invoked both from process context and
1317 * from a timer handler.
1318 */
1319static bool rcu_torture_one_read(struct torture_random_state *trsp)
0729fbf3 1320{
917963d0 1321 unsigned long started;
6b80da42 1322 unsigned long completed;
2397d072 1323 int newstate;
0729fbf3
PM
1324 struct rcu_torture *p;
1325 int pipe_count;
2397d072 1326 int readstate = 0;
52494535 1327 unsigned long long ts;
0729fbf3 1328
2397d072
PM
1329 newstate = rcutorture_extend_mask(readstate, trsp);
1330 rcutorture_one_extend(&readstate, newstate, trsp);
17ef2fe9 1331 started = cur_ops->get_gp_seq();
e4aa0da3 1332 ts = rcu_trace_clock_local();
632ee200 1333 p = rcu_dereference_check(rcu_torture_current,
632ee200
PM
1334 rcu_read_lock_bh_held() ||
1335 rcu_read_lock_sched_held() ||
5be5d1a1
PM
1336 srcu_read_lock_held(srcu_ctlp) ||
1337 torturing_tasks());
0729fbf3 1338 if (p == NULL) {
6b06aa72 1339 /* Wait for rcu_torture_writer to get underway */
2397d072 1340 rcutorture_one_extend(&readstate, 0, trsp);
6b06aa72 1341 return false;
0729fbf3
PM
1342 }
1343 if (p->rtort_mbtest == 0)
1344 atomic_inc(&n_rcu_torture_mberror);
2397d072 1345 rcutorture_loop_extend(&readstate, trsp);
0729fbf3
PM
1346 preempt_disable();
1347 pipe_count = p->rtort_pipe_count;
1348 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1349 /* Should not happen, but... */
1350 pipe_count = RCU_TORTURE_PIPE_LEN;
1351 }
17ef2fe9 1352 completed = cur_ops->get_gp_seq();
52494535 1353 if (pipe_count > 1) {
6b06aa72
PM
1354 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1355 ts, started, completed);
274529ba 1356 rcu_ftrace_dump(DUMP_ALL);
52494535 1357 }
dd17c8f7 1358 __this_cpu_inc(rcu_torture_count[pipe_count]);
d7219312 1359 completed = rcutorture_seq_diff(completed, started);
0729fbf3
PM
1360 if (completed > RCU_TORTURE_PIPE_LEN) {
1361 /* Should not happen, but... */
1362 completed = RCU_TORTURE_PIPE_LEN;
1363 }
dd17c8f7 1364 __this_cpu_inc(rcu_torture_batch[completed]);
0729fbf3 1365 preempt_enable();
2397d072
PM
1366 rcutorture_one_extend(&readstate, 0, trsp);
1367 WARN_ON_ONCE(readstate & RCUTORTURE_RDR_MASK);
6b06aa72
PM
1368 return true;
1369}
1370
3025520e
PM
1371static DEFINE_TORTURE_RANDOM_PERCPU(rcu_torture_timer_rand);
1372
0729fbf3
PM
1373/*
1374 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
1375 * incrementing the corresponding element of the pipeline array. The
1376 * counter in the element should never be greater than 1, otherwise, the
1377 * RCU implementation is broken.
1378 */
fd30b717 1379static void rcu_torture_timer(struct timer_list *unused)
0729fbf3 1380{
8da9a595 1381 atomic_long_inc(&n_rcu_torture_timers);
241b4252 1382 (void)rcu_torture_one_read(this_cpu_ptr(&rcu_torture_timer_rand));
f34c8585
PM
1383
1384 /* Test call_rcu() invocation from interrupt handler. */
1385 if (cur_ops->call) {
1386 struct rcu_head *rhp = kmalloc(sizeof(*rhp), GFP_NOWAIT);
1387
1388 if (rhp)
1389 cur_ops->call(rhp, rcu_torture_timer_cb);
1390 }
0729fbf3
PM
1391}
1392
a241ec65
PM
1393/*
1394 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
1395 * incrementing the corresponding element of the pipeline array. The
1396 * counter in the element should never be greater than 1, otherwise, the
1397 * RCU implementation is broken.
1398 */
1399static int
1400rcu_torture_reader(void *arg)
1401{
444da518 1402 unsigned long lastsleep = jiffies;
c04dd09b
PM
1403 long myid = (long)arg;
1404 int mynumonline = myid;
51b1130e 1405 DEFINE_TORTURE_RANDOM(rand);
0729fbf3 1406 struct timer_list t;
a241ec65 1407
5ccf60f2 1408 VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
d277d868 1409 set_user_nice(current, MAX_NICE);
0acc512c 1410 if (irqreader && cur_ops->irq_capable)
fd30b717 1411 timer_setup_on_stack(&t, rcu_torture_timer, 0);
dbdf65b1 1412
a241ec65 1413 do {
0acc512c 1414 if (irqreader && cur_ops->irq_capable) {
0729fbf3 1415 if (!timer_pending(&t))
6155fec9 1416 mod_timer(&t, jiffies + 1);
0729fbf3 1417 }
6b06aa72 1418 if (!rcu_torture_one_read(&rand))
a241ec65 1419 schedule_timeout_interruptible(HZ);
444da518
PM
1420 if (time_after(jiffies, lastsleep)) {
1421 schedule_timeout_interruptible(1);
1422 lastsleep = jiffies + 10;
1423 }
c04dd09b
PM
1424 while (num_online_cpus() < mynumonline && !torture_must_stop())
1425 schedule_timeout_interruptible(HZ / 5);
628edaa5 1426 stutter_wait("rcu_torture_reader");
36970bb9 1427 } while (!torture_must_stop());
424c1b68 1428 if (irqreader && cur_ops->irq_capable) {
0729fbf3 1429 del_timer_sync(&t);
424c1b68
TG
1430 destroy_timer_on_stack(&t);
1431 }
7fafaac5 1432 torture_kthread_stopping("rcu_torture_reader");
a241ec65
PM
1433 return 0;
1434}
1435
1436/*
eea203fe
JP
1437 * Print torture statistics. Caller must ensure that there is only
1438 * one call to this function at a given time!!! This is normally
1439 * accomplished by relying on the module system to only have one copy
1440 * of the module loaded, and then by giving the rcu_torture_stats
1441 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1442 * thread is not running).
a241ec65 1443 */
d1008950 1444static void
eea203fe 1445rcu_torture_stats_print(void)
a241ec65 1446{
a241ec65
PM
1447 int cpu;
1448 int i;
1449 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1450 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
ad0dc7f9 1451 static unsigned long rtcv_snap = ULONG_MAX;
0032f4e8 1452 static bool splatted;
4ffa6699 1453 struct task_struct *wtp;
a241ec65 1454
0a945022 1455 for_each_possible_cpu(cpu) {
a241ec65
PM
1456 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1457 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1458 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1459 }
1460 }
1461 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1462 if (pipesummary[i] != 0)
1463 break;
1464 }
eea203fe
JP
1465
1466 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1467 pr_cont("rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
1468 rcu_torture_current,
1469 rcu_torture_current_version,
1470 list_empty(&rcu_torture_freelist),
1471 atomic_read(&n_rcu_torture_alloc),
1472 atomic_read(&n_rcu_torture_alloc_fail),
1473 atomic_read(&n_rcu_torture_free));
472213a6 1474 pr_cont("rtmbe: %d rtbe: %ld rtbke: %ld rtbre: %ld ",
eea203fe 1475 atomic_read(&n_rcu_torture_mberror),
472213a6 1476 n_rcu_torture_barrier_error,
eea203fe
JP
1477 n_rcu_torture_boost_ktrerror,
1478 n_rcu_torture_boost_rterror);
1479 pr_cont("rtbf: %ld rtb: %ld nt: %ld ",
1480 n_rcu_torture_boost_failure,
1481 n_rcu_torture_boosts,
8da9a595 1482 atomic_long_read(&n_rcu_torture_timers));
eea203fe 1483 torture_onoff_stats();
38706bc5 1484 pr_cont("barrier: %ld/%ld:%ld ",
eea203fe
JP
1485 n_barrier_successes,
1486 n_barrier_attempts,
1487 n_rcu_torture_barrier_error);
38706bc5 1488 pr_cont("cbflood: %ld\n", atomic_long_read(&n_cbfloods));
eea203fe
JP
1489
1490 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
8e8be45e 1491 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
fae4b54f 1492 n_rcu_torture_barrier_error != 0 ||
8e8be45e
PM
1493 n_rcu_torture_boost_ktrerror != 0 ||
1494 n_rcu_torture_boost_rterror != 0 ||
fae4b54f
PM
1495 n_rcu_torture_boost_failure != 0 ||
1496 i > 1) {
eea203fe 1497 pr_cont("%s", "!!! ");
996417d2 1498 atomic_inc(&n_rcu_torture_error);
5af970a4 1499 WARN_ON_ONCE(1);
996417d2 1500 }
eea203fe 1501 pr_cont("Reader Pipe: ");
a241ec65 1502 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
eea203fe
JP
1503 pr_cont(" %ld", pipesummary[i]);
1504 pr_cont("\n");
1505
1506 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1507 pr_cont("Reader Batch: ");
72e9bb54 1508 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
eea203fe
JP
1509 pr_cont(" %ld", batchsummary[i]);
1510 pr_cont("\n");
1511
1512 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1513 pr_cont("Free-Block Circulation: ");
a241ec65 1514 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
eea203fe 1515 pr_cont(" %d", atomic_read(&rcu_torture_wcount[i]));
a241ec65 1516 }
eea203fe
JP
1517 pr_cont("\n");
1518
c8e5b163 1519 if (cur_ops->stats)
eea203fe 1520 cur_ops->stats();
ad0dc7f9
PM
1521 if (rtcv_snap == rcu_torture_current_version &&
1522 rcu_torture_current != NULL) {
7f6733c3 1523 int __maybe_unused flags = 0;
aebc8264 1524 unsigned long __maybe_unused gp_seq = 0;
ad0dc7f9
PM
1525
1526 rcutorture_get_gp_data(cur_ops->ttype,
aebc8264 1527 &flags, &gp_seq);
7f6733c3 1528 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp,
aebc8264 1529 &flags, &gp_seq);
4ffa6699 1530 wtp = READ_ONCE(writer_task);
aebc8264 1531 pr_alert("??? Writer stall state %s(%d) g%lu f%#x ->state %#lx cpu %d\n",
18aff33e 1532 rcu_torture_writer_state_getname(),
aebc8264 1533 rcu_torture_writer_state, gp_seq, flags,
808de39c
PM
1534 wtp == NULL ? ~0UL : wtp->state,
1535 wtp == NULL ? -1 : (int)task_cpu(wtp));
0032f4e8
PM
1536 if (!splatted && wtp) {
1537 sched_show_task(wtp);
1538 splatted = true;
1539 }
afea227f 1540 show_rcu_gp_kthreads();
274529ba 1541 rcu_ftrace_dump(DUMP_ALL);
ad0dc7f9
PM
1542 }
1543 rtcv_snap = rcu_torture_current_version;
a241ec65
PM
1544}
1545
a241ec65
PM
1546/*
1547 * Periodically prints torture statistics, if periodic statistics printing
1548 * was specified via the stat_interval module parameter.
a241ec65
PM
1549 */
1550static int
1551rcu_torture_stats(void *arg)
1552{
5ccf60f2 1553 VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
a241ec65
PM
1554 do {
1555 schedule_timeout_interruptible(stat_interval * HZ);
1556 rcu_torture_stats_print();
f67a3356 1557 torture_shutdown_absorb("rcu_torture_stats");
36970bb9 1558 } while (!torture_must_stop());
7fafaac5 1559 torture_kthread_stopping("rcu_torture_stats");
d120f65f
PM
1560 return 0;
1561}
1562
eac45e58 1563static void
e66c33d5 1564rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
95c38322 1565{
2caa1e44
PM
1566 pr_alert("%s" TORTURE_FLAG
1567 "--- %s: nreaders=%d nfakewriters=%d "
1568 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1569 "shuffle_interval=%d stutter=%d irqreader=%d "
1570 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1571 "test_boost=%d/%d test_boost_interval=%d "
1572 "test_boost_duration=%d shutdown_secs=%d "
2b1516e5 1573 "stall_cpu=%d stall_cpu_holdoff=%d stall_cpu_irqsoff=%d "
67afeed2 1574 "n_barrier_cbs=%d "
2caa1e44
PM
1575 "onoff_interval=%d onoff_holdoff=%d\n",
1576 torture_type, tag, nrealreaders, nfakewriters,
1577 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1578 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1579 test_boost, cur_ops->can_boost,
1580 test_boost_interval, test_boost_duration, shutdown_secs,
2b1516e5 1581 stall_cpu, stall_cpu_holdoff, stall_cpu_irqsoff,
67afeed2 1582 n_barrier_cbs,
2caa1e44 1583 onoff_interval, onoff_holdoff);
95c38322
PM
1584}
1585
0ffd374b 1586static int rcutorture_booster_cleanup(unsigned int cpu)
8e8be45e
PM
1587{
1588 struct task_struct *t;
1589
1590 if (boost_tasks[cpu] == NULL)
0ffd374b 1591 return 0;
8e8be45e 1592 mutex_lock(&boost_mutex);
8e8be45e
PM
1593 t = boost_tasks[cpu];
1594 boost_tasks[cpu] = NULL;
450efca7 1595 rcu_torture_enable_rt_throttle();
8e8be45e
PM
1596 mutex_unlock(&boost_mutex);
1597
1598 /* This must be outside of the mutex, otherwise deadlock! */
9c029b86 1599 torture_stop_kthread(rcu_torture_boost, t);
0ffd374b 1600 return 0;
8e8be45e
PM
1601}
1602
0ffd374b 1603static int rcutorture_booster_init(unsigned int cpu)
8e8be45e
PM
1604{
1605 int retval;
1606
1607 if (boost_tasks[cpu] != NULL)
1608 return 0; /* Already created, nothing more to do. */
1609
1610 /* Don't allow time recalculation while creating a new task. */
1611 mutex_lock(&boost_mutex);
450efca7 1612 rcu_torture_disable_rt_throttle();
5ccf60f2 1613 VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
1f288094
ED
1614 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1615 cpu_to_node(cpu),
1616 "rcu_torture_boost");
8e8be45e
PM
1617 if (IS_ERR(boost_tasks[cpu])) {
1618 retval = PTR_ERR(boost_tasks[cpu]);
5ccf60f2 1619 VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
8e8be45e
PM
1620 n_rcu_torture_boost_ktrerror++;
1621 boost_tasks[cpu] = NULL;
1622 mutex_unlock(&boost_mutex);
1623 return retval;
1624 }
1625 kthread_bind(boost_tasks[cpu], cpu);
1626 wake_up_process(boost_tasks[cpu]);
1627 mutex_unlock(&boost_mutex);
1628 return 0;
1629}
1630
c13f3757
PM
1631/*
1632 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1633 * induces a CPU stall for the time specified by stall_cpu.
1634 */
49fb4c62 1635static int rcu_torture_stall(void *args)
c13f3757
PM
1636{
1637 unsigned long stop_at;
1638
5ccf60f2 1639 VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
c13f3757 1640 if (stall_cpu_holdoff > 0) {
5ccf60f2 1641 VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
c13f3757 1642 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
5ccf60f2 1643 VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
c13f3757
PM
1644 }
1645 if (!kthread_should_stop()) {
622be33f 1646 stop_at = ktime_get_seconds() + stall_cpu;
c13f3757 1647 /* RCU CPU stall is expected behavior in following code. */
c13f3757 1648 rcu_read_lock();
2b1516e5
PM
1649 if (stall_cpu_irqsoff)
1650 local_irq_disable();
1651 else
1652 preempt_disable();
1653 pr_alert("rcu_torture_stall start on CPU %d.\n",
1654 smp_processor_id());
622be33f
AB
1655 while (ULONG_CMP_LT((unsigned long)ktime_get_seconds(),
1656 stop_at))
c13f3757 1657 continue; /* Induce RCU CPU stall warning. */
2b1516e5
PM
1658 if (stall_cpu_irqsoff)
1659 local_irq_enable();
1660 else
1661 preempt_enable();
c13f3757 1662 rcu_read_unlock();
2caa1e44 1663 pr_alert("rcu_torture_stall end.\n");
c13f3757 1664 }
f67a3356 1665 torture_shutdown_absorb("rcu_torture_stall");
c13f3757
PM
1666 while (!kthread_should_stop())
1667 schedule_timeout_interruptible(10 * HZ);
1668 return 0;
1669}
1670
1671/* Spawn CPU-stall kthread, if stall_cpu specified. */
1672static int __init rcu_torture_stall_init(void)
1673{
c13f3757
PM
1674 if (stall_cpu <= 0)
1675 return 0;
47cf29b9 1676 return torture_create_kthread(rcu_torture_stall, NULL, stall_task);
c13f3757
PM
1677}
1678
9fdcb9af
PM
1679/* State structure for forward-progress self-propagating RCU callback. */
1680struct fwd_cb_state {
1681 struct rcu_head rh;
1682 int stop;
1683};
1684
1685/*
1686 * Forward-progress self-propagating RCU callback function. Because
1687 * callbacks run from softirq, this function is an implicit RCU read-side
1688 * critical section.
1689 */
1690static void rcu_torture_fwd_prog_cb(struct rcu_head *rhp)
1691{
1692 struct fwd_cb_state *fcsp = container_of(rhp, struct fwd_cb_state, rh);
1693
1694 if (READ_ONCE(fcsp->stop)) {
1695 WRITE_ONCE(fcsp->stop, 2);
1696 return;
1697 }
1698 cur_ops->call(&fcsp->rh, rcu_torture_fwd_prog_cb);
1699}
1700
1b27291b
PM
1701/* Carry out grace-period forward-progress testing. */
1702static int rcu_torture_fwd_prog(void *args)
1703{
119248be 1704 unsigned long cver;
f4de46ed 1705 unsigned long dur;
9fdcb9af 1706 struct fwd_cb_state fcs = { .stop = 0 };
119248be 1707 unsigned long gps;
1b27291b 1708 int idx;
08a7a2ec
PM
1709 int sd;
1710 int sd4;
9fdcb9af 1711 bool selfpropcb = false;
1b27291b 1712 unsigned long stopat;
f4de46ed 1713 int tested = 0;
152f4afb 1714 int tested_tries = 0;
08a7a2ec 1715 static DEFINE_TORTURE_RANDOM(trs);
1b27291b
PM
1716
1717 VERBOSE_TOROUT_STRING("rcu_torture_fwd_progress task started");
fecad509
PM
1718 if (!IS_ENABLED(CONFIG_SMP) || !IS_ENABLED(CONFIG_RCU_BOOST))
1719 set_user_nice(current, MAX_NICE);
9fdcb9af
PM
1720 if (cur_ops->call && cur_ops->sync && cur_ops->cb_barrier) {
1721 init_rcu_head_on_stack(&fcs.rh);
1722 cur_ops->call(&fcs.rh, rcu_torture_fwd_prog_cb);
1723 selfpropcb = true;
1724 }
1b27291b
PM
1725 do {
1726 schedule_timeout_interruptible(fwd_progress_holdoff * HZ);
119248be
PM
1727 cver = READ_ONCE(rcu_torture_current_version);
1728 gps = cur_ops->get_gp_seq();
08a7a2ec
PM
1729 sd = cur_ops->stall_dur() + 1;
1730 sd4 = (sd + fwd_progress_div - 1) / fwd_progress_div;
f4de46ed
PM
1731 dur = sd4 + torture_random(&trs) % (sd - sd4);
1732 stopat = jiffies + dur;
1b27291b
PM
1733 while (time_before(jiffies, stopat) && !torture_must_stop()) {
1734 idx = cur_ops->readlock();
1735 udelay(10);
1736 cur_ops->readunlock(idx);
1737 if (!fwd_progress_need_resched || need_resched())
1738 cond_resched();
1739 }
152f4afb 1740 tested_tries++;
1b27291b 1741 if (!time_before(jiffies, stopat) && !torture_must_stop()) {
f4de46ed
PM
1742 tested++;
1743 cver = READ_ONCE(rcu_torture_current_version) - cver;
119248be 1744 gps = rcutorture_seq_diff(cur_ops->get_gp_seq(), gps);
f4de46ed
PM
1745 WARN_ON(!cver && gps < 2);
1746 pr_alert("%s: Duration %ld cver %ld gps %ld\n", __func__, dur, cver, gps);
1b27291b
PM
1747 }
1748 /* Avoid slow periods, better to test when busy. */
1749 stutter_wait("rcu_torture_fwd_prog");
1750 } while (!torture_must_stop());
9fdcb9af
PM
1751 if (selfpropcb) {
1752 WRITE_ONCE(fcs.stop, 1);
1753 cur_ops->sync(); /* Wait for running callback to complete. */
1754 cur_ops->cb_barrier(); /* Wait for queued callbacks. */
1755 WARN_ON(READ_ONCE(fcs.stop) != 2);
1756 destroy_rcu_head_on_stack(&fcs.rh);
1757 }
152f4afb
PM
1758 /* Short runs might not contain a valid forward-progress attempt. */
1759 WARN_ON(!tested && tested_tries >= 5);
f4de46ed 1760 pr_alert("%s: tested %d tested_tries %d\n", __func__, tested, tested_tries);
1b27291b
PM
1761 torture_kthread_stopping("rcu_torture_fwd_prog");
1762 return 0;
1763}
1764
1765/* If forward-progress checking is requested and feasible, spawn the thread. */
1766static int __init rcu_torture_fwd_prog_init(void)
1767{
1768 if (!fwd_progress)
1769 return 0; /* Not requested, so don't do it. */
1770 if (!cur_ops->stall_dur || cur_ops->stall_dur() <= 0) {
1771 VERBOSE_TOROUT_STRING("rcu_torture_fwd_prog_init: Disabled, unsupported by RCU flavor under test");
1772 return 0;
1773 }
1774 if (stall_cpu > 0) {
1775 VERBOSE_TOROUT_STRING("rcu_torture_fwd_prog_init: Disabled, conflicts with CPU-stall testing");
1776 if (IS_MODULE(CONFIG_RCU_TORTURE_TESTS))
1777 return -EINVAL; /* In module, can fail back to user. */
1778 WARN_ON(1); /* Make sure rcutorture notices conflict. */
1779 return 0;
1780 }
1781 if (fwd_progress_holdoff <= 0)
1782 fwd_progress_holdoff = 1;
1783 if (fwd_progress_div <= 0)
1784 fwd_progress_div = 4;
1785 return torture_create_kthread(rcu_torture_fwd_prog,
1786 NULL, fwd_prog_task);
1787}
1788
fae4b54f 1789/* Callback function for RCU barrier testing. */
b3b8a4d4 1790static void rcu_torture_barrier_cbf(struct rcu_head *rcu)
fae4b54f
PM
1791{
1792 atomic_inc(&barrier_cbs_invoked);
1793}
1794
1795/* kthread function to register callbacks used to test RCU barriers. */
1796static int rcu_torture_barrier_cbs(void *arg)
1797{
1798 long myid = (long)arg;
c6ebcbb6 1799 bool lastphase = 0;
78e4bc34 1800 bool newphase;
fae4b54f
PM
1801 struct rcu_head rcu;
1802
1803 init_rcu_head_on_stack(&rcu);
5ccf60f2 1804 VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
d277d868 1805 set_user_nice(current, MAX_NICE);
fae4b54f
PM
1806 do {
1807 wait_event(barrier_cbs_wq[myid],
78e4bc34 1808 (newphase =
6c7ed42c 1809 smp_load_acquire(&barrier_phase)) != lastphase ||
36970bb9 1810 torture_must_stop());
78e4bc34 1811 lastphase = newphase;
36970bb9 1812 if (torture_must_stop())
fae4b54f 1813 break;
6c7ed42c
PM
1814 /*
1815 * The above smp_load_acquire() ensures barrier_phase load
aab05738 1816 * is ordered before the following ->call().
6c7ed42c 1817 */
0aa67e75 1818 local_irq_disable(); /* Just to test no-irq call_rcu(). */
fae4b54f 1819 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
0aa67e75 1820 local_irq_enable();
fae4b54f
PM
1821 if (atomic_dec_and_test(&barrier_cbs_count))
1822 wake_up(&barrier_wq);
36970bb9 1823 } while (!torture_must_stop());
69c60455
PM
1824 if (cur_ops->cb_barrier != NULL)
1825 cur_ops->cb_barrier();
fae4b54f 1826 destroy_rcu_head_on_stack(&rcu);
7fafaac5 1827 torture_kthread_stopping("rcu_torture_barrier_cbs");
fae4b54f
PM
1828 return 0;
1829}
1830
1831/* kthread function to drive and coordinate RCU barrier testing. */
1832static int rcu_torture_barrier(void *arg)
1833{
1834 int i;
1835
5ccf60f2 1836 VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
fae4b54f
PM
1837 do {
1838 atomic_set(&barrier_cbs_invoked, 0);
1839 atomic_set(&barrier_cbs_count, n_barrier_cbs);
6c7ed42c
PM
1840 /* Ensure barrier_phase ordered after prior assignments. */
1841 smp_store_release(&barrier_phase, !barrier_phase);
fae4b54f
PM
1842 for (i = 0; i < n_barrier_cbs; i++)
1843 wake_up(&barrier_cbs_wq[i]);
1844 wait_event(barrier_wq,
1845 atomic_read(&barrier_cbs_count) == 0 ||
36970bb9
PM
1846 torture_must_stop());
1847 if (torture_must_stop())
fae4b54f
PM
1848 break;
1849 n_barrier_attempts++;
78e4bc34 1850 cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
fae4b54f
PM
1851 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1852 n_rcu_torture_barrier_error++;
7602de4a
PM
1853 pr_err("barrier_cbs_invoked = %d, n_barrier_cbs = %d\n",
1854 atomic_read(&barrier_cbs_invoked),
1855 n_barrier_cbs);
fae4b54f 1856 WARN_ON_ONCE(1);
bf5b6435
JFG
1857 } else {
1858 n_barrier_successes++;
fae4b54f 1859 }
fae4b54f 1860 schedule_timeout_interruptible(HZ / 10);
36970bb9 1861 } while (!torture_must_stop());
7fafaac5 1862 torture_kthread_stopping("rcu_torture_barrier");
fae4b54f
PM
1863 return 0;
1864}
1865
1866/* Initialize RCU barrier testing. */
1867static int rcu_torture_barrier_init(void)
1868{
1869 int i;
1870 int ret;
1871
d9eba768 1872 if (n_barrier_cbs <= 0)
fae4b54f
PM
1873 return 0;
1874 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
2caa1e44
PM
1875 pr_alert("%s" TORTURE_FLAG
1876 " Call or barrier ops missing for %s,\n",
1877 torture_type, cur_ops->name);
1878 pr_alert("%s" TORTURE_FLAG
1879 " RCU barrier testing omitted from run.\n",
1880 torture_type);
fae4b54f
PM
1881 return 0;
1882 }
1883 atomic_set(&barrier_cbs_count, 0);
1884 atomic_set(&barrier_cbs_invoked, 0);
1885 barrier_cbs_tasks =
68a675d4 1886 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_tasks[0]),
fae4b54f
PM
1887 GFP_KERNEL);
1888 barrier_cbs_wq =
68a675d4 1889 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_wq[0]), GFP_KERNEL);
de5e6437 1890 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
fae4b54f
PM
1891 return -ENOMEM;
1892 for (i = 0; i < n_barrier_cbs; i++) {
1893 init_waitqueue_head(&barrier_cbs_wq[i]);
47cf29b9
PM
1894 ret = torture_create_kthread(rcu_torture_barrier_cbs,
1895 (void *)(long)i,
1896 barrier_cbs_tasks[i]);
1897 if (ret)
fae4b54f 1898 return ret;
fae4b54f 1899 }
47cf29b9 1900 return torture_create_kthread(rcu_torture_barrier, NULL, barrier_task);
fae4b54f
PM
1901}
1902
1903/* Clean up after RCU barrier testing. */
1904static void rcu_torture_barrier_cleanup(void)
1905{
1906 int i;
1907
9c029b86 1908 torture_stop_kthread(rcu_torture_barrier, barrier_task);
fae4b54f 1909 if (barrier_cbs_tasks != NULL) {
9c029b86
PM
1910 for (i = 0; i < n_barrier_cbs; i++)
1911 torture_stop_kthread(rcu_torture_barrier_cbs,
1912 barrier_cbs_tasks[i]);
fae4b54f
PM
1913 kfree(barrier_cbs_tasks);
1914 barrier_cbs_tasks = NULL;
1915 }
1916 if (barrier_cbs_wq != NULL) {
1917 kfree(barrier_cbs_wq);
1918 barrier_cbs_wq = NULL;
1919 }
1920}
1921
4babd855
JFG
1922static bool rcu_torture_can_boost(void)
1923{
1924 static int boost_warn_once;
1925 int prio;
1926
1927 if (!(test_boost == 1 && cur_ops->can_boost) && test_boost != 2)
1928 return false;
1929
1930 prio = rcu_get_gp_kthreads_prio();
1931 if (!prio)
1932 return false;
1933
1934 if (prio < 2) {
1935 if (boost_warn_once == 1)
1936 return false;
1937
bf5b6435 1938 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
1939 boost_warn_once = 1;
1940 return false;
1941 }
1942
1943 return true;
1944}
1945
0ffd374b 1946static enum cpuhp_state rcutor_hp;
8e8be45e 1947
a241ec65
PM
1948static void
1949rcu_torture_cleanup(void)
1950{
034777d7 1951 int flags = 0;
aebc8264 1952 unsigned long gp_seq = 0;
a241ec65
PM
1953 int i;
1954
d36a7a0d 1955 if (torture_cleanup_begin()) {
343e9099
PM
1956 if (cur_ops->cb_barrier != NULL)
1957 cur_ops->cb_barrier();
1958 return;
1959 }
d84f5203 1960
fae4b54f 1961 rcu_torture_barrier_cleanup();
1b27291b 1962 torture_stop_kthread(rcu_torture_fwd_prog, fwd_prog_task);
9c029b86 1963 torture_stop_kthread(rcu_torture_stall, stall_task);
9c029b86 1964 torture_stop_kthread(rcu_torture_writer, writer_task);
a241ec65 1965
c8e5b163 1966 if (reader_tasks) {
9c029b86
PM
1967 for (i = 0; i < nrealreaders; i++)
1968 torture_stop_kthread(rcu_torture_reader,
1969 reader_tasks[i]);
a241ec65 1970 kfree(reader_tasks);
a241ec65
PM
1971 }
1972 rcu_torture_current = NULL;
1973
c8e5b163 1974 if (fakewriter_tasks) {
b772e1dd 1975 for (i = 0; i < nfakewriters; i++) {
9c029b86
PM
1976 torture_stop_kthread(rcu_torture_fakewriter,
1977 fakewriter_tasks[i]);
b772e1dd
JT
1978 }
1979 kfree(fakewriter_tasks);
1980 fakewriter_tasks = NULL;
1981 }
1982
aebc8264
PM
1983 rcutorture_get_gp_data(cur_ops->ttype, &flags, &gp_seq);
1984 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp, &flags, &gp_seq);
1985 pr_alert("%s: End-test grace-period state: g%lu f%#x\n",
1986 cur_ops->name, gp_seq, flags);
9c029b86
PM
1987 torture_stop_kthread(rcu_torture_stats, stats_task);
1988 torture_stop_kthread(rcu_torture_fqs, fqs_task);
38706bc5
PM
1989 for (i = 0; i < ncbflooders; i++)
1990 torture_stop_kthread(rcu_torture_cbflood, cbflood_task[i]);
4babd855 1991 if (rcu_torture_can_boost())
0ffd374b 1992 cpuhp_remove_state(rcutor_hp);
bf66f18e 1993
ca1d51ed
PM
1994 /*
1995 * Wait for all RCU callbacks to fire, then do flavor-specific
1996 * cleanup operations.
1997 */
2326974d
PM
1998 if (cur_ops->cb_barrier != NULL)
1999 cur_ops->cb_barrier();
ca1d51ed
PM
2000 if (cur_ops->cleanup != NULL)
2001 cur_ops->cleanup();
a241ec65 2002
a241ec65 2003 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
72e9bb54 2004
fae4b54f 2005 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
8e8be45e 2006 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
2e9e8081 2007 else if (torture_onoff_failures())
091541bb
PM
2008 rcu_torture_print_module_parms(cur_ops,
2009 "End of test: RCU_HOTPLUG");
95c38322 2010 else
8e8be45e 2011 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
d36a7a0d 2012 torture_cleanup_end();
a241ec65
PM
2013}
2014
d2818df1
PM
2015#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
2016static void rcu_torture_leak_cb(struct rcu_head *rhp)
2017{
2018}
2019
2020static void rcu_torture_err_cb(struct rcu_head *rhp)
2021{
2022 /*
2023 * This -might- happen due to race conditions, but is unlikely.
2024 * The scenario that leads to this happening is that the
2025 * first of the pair of duplicate callbacks is queued,
2026 * someone else starts a grace period that includes that
2027 * callback, then the second of the pair must wait for the
2028 * next grace period. Unlikely, but can happen. If it
2029 * does happen, the debug-objects subsystem won't have splatted.
2030 */
e0d31a34 2031 pr_alert("%s: duplicated callback was invoked.\n", KBUILD_MODNAME);
d2818df1
PM
2032}
2033#endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2034
2035/*
2036 * Verify that double-free causes debug-objects to complain, but only
2037 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
2038 * cannot be carried out.
2039 */
2040static void rcu_test_debug_objects(void)
2041{
2042#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
2043 struct rcu_head rh1;
2044 struct rcu_head rh2;
2045
2046 init_rcu_head_on_stack(&rh1);
2047 init_rcu_head_on_stack(&rh2);
e0d31a34 2048 pr_alert("%s: WARN: Duplicate call_rcu() test starting.\n", KBUILD_MODNAME);
d2818df1
PM
2049
2050 /* Try to queue the rh2 pair of callbacks for the same grace period. */
2051 preempt_disable(); /* Prevent preemption from interrupting test. */
2052 rcu_read_lock(); /* Make it impossible to finish a grace period. */
2053 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
2054 local_irq_disable(); /* Make it harder to start a new grace period. */
2055 call_rcu(&rh2, rcu_torture_leak_cb);
2056 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
2057 local_irq_enable();
2058 rcu_read_unlock();
2059 preempt_enable();
2060
2061 /* Wait for them all to get done so we can safely return. */
2062 rcu_barrier();
e0d31a34 2063 pr_alert("%s: WARN: Duplicate call_rcu() test complete.\n", KBUILD_MODNAME);
d2818df1
PM
2064 destroy_rcu_head_on_stack(&rh1);
2065 destroy_rcu_head_on_stack(&rh2);
2066#else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
e0d31a34 2067 pr_alert("%s: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n", KBUILD_MODNAME);
d2818df1
PM
2068#endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2069}
2070
6f8bc500 2071static int __init
a241ec65
PM
2072rcu_torture_init(void)
2073{
c04dd09b 2074 long i;
a241ec65
PM
2075 int cpu;
2076 int firsterr = 0;
2ec1f2d9 2077 static struct rcu_torture_ops *torture_ops[] = {
ca1d51ed 2078 &rcu_ops, &rcu_bh_ops, &rcu_busted_ops, &srcu_ops, &srcud_ops,
2397d072 2079 &busted_srcud_ops, &sched_ops, &tasks_ops,
2ec1f2d9 2080 };
a241ec65 2081
a2f2577d 2082 if (!torture_init_begin(torture_type, verbose))
5228084e 2083 return -EBUSY;
343e9099 2084
a241ec65 2085 /* Process args and tell the world that the torturer is on the job. */
ade5fb81 2086 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
72e9bb54 2087 cur_ops = torture_ops[i];
ade5fb81 2088 if (strcmp(torture_type, cur_ops->name) == 0)
72e9bb54 2089 break;
72e9bb54 2090 }
ade5fb81 2091 if (i == ARRAY_SIZE(torture_ops)) {
2caa1e44
PM
2092 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
2093 torture_type);
2094 pr_alert("rcu-torture types:");
cf886c44 2095 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
a7538352
JP
2096 pr_cont(" %s", torture_ops[i]->name);
2097 pr_cont("\n");
e746b558 2098 WARN_ON(!IS_MODULE(CONFIG_RCU_TORTURE_TEST));
889d487a
PM
2099 firsterr = -EINVAL;
2100 goto unwind;
72e9bb54 2101 }
bf66f18e 2102 if (cur_ops->fqs == NULL && fqs_duration != 0) {
2caa1e44 2103 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
bf66f18e
PM
2104 fqs_duration = 0;
2105 }
c8e5b163 2106 if (cur_ops->init)
889d487a 2107 cur_ops->init();
72e9bb54 2108
64e4b43a 2109 if (nreaders >= 0) {
a241ec65 2110 nrealreaders = nreaders;
64e4b43a 2111 } else {
3838cc18 2112 nrealreaders = num_online_cpus() - 2 - nreaders;
64e4b43a
PM
2113 if (nrealreaders <= 0)
2114 nrealreaders = 1;
2115 }
8e8be45e 2116 rcu_torture_print_module_parms(cur_ops, "Start of test");
a241ec65
PM
2117
2118 /* Set up the freelist. */
2119
2120 INIT_LIST_HEAD(&rcu_torture_freelist);
788e770e 2121 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
996417d2 2122 rcu_tortures[i].rtort_mbtest = 0;
a241ec65
PM
2123 list_add_tail(&rcu_tortures[i].rtort_free,
2124 &rcu_torture_freelist);
2125 }
2126
2127 /* Initialize the statistics so that each run gets its own numbers. */
2128
2129 rcu_torture_current = NULL;
2130 rcu_torture_current_version = 0;
2131 atomic_set(&n_rcu_torture_alloc, 0);
2132 atomic_set(&n_rcu_torture_alloc_fail, 0);
2133 atomic_set(&n_rcu_torture_free, 0);
996417d2
PM
2134 atomic_set(&n_rcu_torture_mberror, 0);
2135 atomic_set(&n_rcu_torture_error, 0);
fae4b54f 2136 n_rcu_torture_barrier_error = 0;
8e8be45e
PM
2137 n_rcu_torture_boost_ktrerror = 0;
2138 n_rcu_torture_boost_rterror = 0;
8e8be45e
PM
2139 n_rcu_torture_boost_failure = 0;
2140 n_rcu_torture_boosts = 0;
a241ec65
PM
2141 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
2142 atomic_set(&rcu_torture_wcount[i], 0);
0a945022 2143 for_each_possible_cpu(cpu) {
a241ec65
PM
2144 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
2145 per_cpu(rcu_torture_count, cpu)[i] = 0;
2146 per_cpu(rcu_torture_batch, cpu)[i] = 0;
2147 }
2148 }
2149
2150 /* Start up the kthreads. */
2151
47cf29b9
PM
2152 firsterr = torture_create_kthread(rcu_torture_writer, NULL,
2153 writer_task);
2154 if (firsterr)
a241ec65 2155 goto unwind;
4444d852 2156 if (nfakewriters > 0) {
68a675d4 2157 fakewriter_tasks = kcalloc(nfakewriters,
4444d852
PM
2158 sizeof(fakewriter_tasks[0]),
2159 GFP_KERNEL);
2160 if (fakewriter_tasks == NULL) {
2161 VERBOSE_TOROUT_ERRSTRING("out of memory");
2162 firsterr = -ENOMEM;
2163 goto unwind;
2164 }
b772e1dd
JT
2165 }
2166 for (i = 0; i < nfakewriters; i++) {
47cf29b9
PM
2167 firsterr = torture_create_kthread(rcu_torture_fakewriter,
2168 NULL, fakewriter_tasks[i]);
2169 if (firsterr)
b772e1dd 2170 goto unwind;
b772e1dd 2171 }
68a675d4 2172 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
a241ec65
PM
2173 GFP_KERNEL);
2174 if (reader_tasks == NULL) {
5ccf60f2 2175 VERBOSE_TOROUT_ERRSTRING("out of memory");
a241ec65
PM
2176 firsterr = -ENOMEM;
2177 goto unwind;
2178 }
2179 for (i = 0; i < nrealreaders; i++) {
c04dd09b 2180 firsterr = torture_create_kthread(rcu_torture_reader, (void *)i,
47cf29b9
PM
2181 reader_tasks[i]);
2182 if (firsterr)
a241ec65 2183 goto unwind;
a241ec65
PM
2184 }
2185 if (stat_interval > 0) {
47cf29b9
PM
2186 firsterr = torture_create_kthread(rcu_torture_stats, NULL,
2187 stats_task);
2188 if (firsterr)
a241ec65 2189 goto unwind;
a241ec65 2190 }
e8e255f7 2191 if (test_no_idle_hz && shuffle_interval > 0) {
3808dc9f
PM
2192 firsterr = torture_shuffle_init(shuffle_interval * HZ);
2193 if (firsterr)
d84f5203 2194 goto unwind;
d84f5203 2195 }
d120f65f
PM
2196 if (stutter < 0)
2197 stutter = 0;
2198 if (stutter) {
628edaa5
PM
2199 firsterr = torture_stutter_init(stutter * HZ);
2200 if (firsterr)
d120f65f 2201 goto unwind;
d120f65f 2202 }
bf66f18e
PM
2203 if (fqs_duration < 0)
2204 fqs_duration = 0;
2205 if (fqs_duration) {
628edaa5 2206 /* Create the fqs thread */
d0d0606e
PM
2207 firsterr = torture_create_kthread(rcu_torture_fqs, NULL,
2208 fqs_task);
47cf29b9 2209 if (firsterr)
bf66f18e 2210 goto unwind;
bf66f18e 2211 }
8e8be45e
PM
2212 if (test_boost_interval < 1)
2213 test_boost_interval = 1;
2214 if (test_boost_duration < 2)
2215 test_boost_duration = 2;
4babd855 2216 if (rcu_torture_can_boost()) {
8e8be45e
PM
2217
2218 boost_starttime = jiffies + test_boost_interval * HZ;
0ffd374b
SAS
2219
2220 firsterr = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "RCU_TORTURE",
2221 rcutorture_booster_init,
2222 rcutorture_booster_cleanup);
2223 if (firsterr < 0)
2224 goto unwind;
2225 rcutor_hp = firsterr;
8e8be45e 2226 }
01025ebc
PM
2227 firsterr = torture_shutdown_init(shutdown_secs, rcu_torture_cleanup);
2228 if (firsterr)
37e377d2 2229 goto unwind;
028be12b 2230 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval);
01025ebc 2231 if (firsterr)
37e377d2 2232 goto unwind;
01025ebc 2233 firsterr = rcu_torture_stall_init();
1b27291b
PM
2234 if (firsterr)
2235 goto unwind;
2236 firsterr = rcu_torture_fwd_prog_init();
01025ebc 2237 if (firsterr)
37e377d2 2238 goto unwind;
01025ebc
PM
2239 firsterr = rcu_torture_barrier_init();
2240 if (firsterr)
fae4b54f 2241 goto unwind;
d2818df1
PM
2242 if (object_debug)
2243 rcu_test_debug_objects();
38706bc5
PM
2244 if (cbflood_n_burst > 0) {
2245 /* Create the cbflood threads */
2246 ncbflooders = (num_online_cpus() + 3) / 4;
2247 cbflood_task = kcalloc(ncbflooders, sizeof(*cbflood_task),
2248 GFP_KERNEL);
2249 if (!cbflood_task) {
2250 VERBOSE_TOROUT_ERRSTRING("out of memory");
2251 firsterr = -ENOMEM;
2252 goto unwind;
2253 }
2254 for (i = 0; i < ncbflooders; i++) {
2255 firsterr = torture_create_kthread(rcu_torture_cbflood,
2256 NULL,
2257 cbflood_task[i]);
2258 if (firsterr)
2259 goto unwind;
2260 }
2261 }
b5daa8f3 2262 torture_init_end();
a241ec65
PM
2263 return 0;
2264
2265unwind:
b5daa8f3 2266 torture_init_end();
a241ec65
PM
2267 rcu_torture_cleanup();
2268 return firsterr;
2269}
2270
2271module_init(rcu_torture_init);
2272module_exit(rcu_torture_cleanup);