rcu: Make RCU CPU stall warnings use ->gp_seq
[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>
a241ec65 58
25c36329
PM
59#include "rcu.h"
60
a241ec65 61MODULE_LICENSE("GPL");
e0198b29 62MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and Josh Triplett <josh@joshtriplett.org>");
a241ec65 63
4102adab 64
38706bc5
PM
65torture_param(int, cbflood_inter_holdoff, HZ,
66 "Holdoff between floods (jiffies)");
67torture_param(int, cbflood_intra_holdoff, 1,
68 "Holdoff between bursts (jiffies)");
69torture_param(int, cbflood_n_burst, 3, "# bursts in flood, zero to disable");
70torture_param(int, cbflood_n_per_burst, 20000,
71 "# callbacks per burst in flood");
9e250225
PM
72torture_param(int, fqs_duration, 0,
73 "Duration of fqs bursts (us), 0 to disable");
74torture_param(int, fqs_holdoff, 0, "Holdoff time within fqs bursts (us)");
75torture_param(int, fqs_stutter, 3, "Wait time between fqs bursts (s)");
a48f3fad 76torture_param(bool, gp_cond, false, "Use conditional/async GP wait primitives");
9e250225
PM
77torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
78torture_param(bool, gp_normal, false,
79 "Use normal (non-expedited) GP wait primitives");
f0bf8fab 80torture_param(bool, gp_sync, false, "Use synchronous GP wait primitives");
9e250225
PM
81torture_param(int, irqreader, 1, "Allow RCU readers from irq handlers");
82torture_param(int, n_barrier_cbs, 0,
83 "# of callbacks/kthreads for barrier testing");
84torture_param(int, nfakewriters, 4, "Number of RCU fake writer threads");
85torture_param(int, nreaders, -1, "Number of RCU reader threads");
86torture_param(int, object_debug, 0,
87 "Enable debug-object double call_rcu() testing");
88torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
89torture_param(int, onoff_interval, 0,
90 "Time between CPU hotplugs (s), 0=disable");
91torture_param(int, shuffle_interval, 3, "Number of seconds between shuffles");
92torture_param(int, shutdown_secs, 0, "Shutdown time (s), <= zero to disable.");
93torture_param(int, stall_cpu, 0, "Stall duration (s), zero to disable.");
94torture_param(int, stall_cpu_holdoff, 10,
95 "Time to wait before starting stall (s).");
2b1516e5 96torture_param(int, stall_cpu_irqsoff, 0, "Disable interrupts while stalling.");
9e250225
PM
97torture_param(int, stat_interval, 60,
98 "Number of seconds between stats printk()s");
99torture_param(int, stutter, 5, "Number of seconds to run/halt test");
100torture_param(int, test_boost, 1, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
101torture_param(int, test_boost_duration, 4,
102 "Duration of each boost test, seconds.");
103torture_param(int, test_boost_interval, 7,
104 "Interval between boost tests, seconds.");
105torture_param(bool, test_no_idle_hz, true,
106 "Test support for tickless idle CPUs");
90127d60 107torture_param(int, verbose, 1,
b5daa8f3 108 "Enable verbose debugging printk()s");
9e250225 109
d10453e9 110static char *torture_type = "rcu";
d6ad6711 111module_param(torture_type, charp, 0444);
d10453e9 112MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, ...)");
a241ec65 113
a241ec65 114static int nrealreaders;
38706bc5 115static int ncbflooders;
a241ec65 116static struct task_struct *writer_task;
b772e1dd 117static struct task_struct **fakewriter_tasks;
a241ec65
PM
118static struct task_struct **reader_tasks;
119static struct task_struct *stats_task;
38706bc5 120static struct task_struct **cbflood_task;
bf66f18e 121static struct task_struct *fqs_task;
8e8be45e 122static struct task_struct *boost_tasks[NR_CPUS];
c13f3757 123static struct task_struct *stall_task;
fae4b54f
PM
124static struct task_struct **barrier_cbs_tasks;
125static struct task_struct *barrier_task;
a241ec65
PM
126
127#define RCU_TORTURE_PIPE_LEN 10
128
129struct rcu_torture {
130 struct rcu_head rtort_rcu;
131 int rtort_pipe_count;
132 struct list_head rtort_free;
996417d2 133 int rtort_mbtest;
a241ec65
PM
134};
135
a241ec65 136static LIST_HEAD(rcu_torture_freelist);
0ddea0ea 137static struct rcu_torture __rcu *rcu_torture_current;
4a298656 138static unsigned long rcu_torture_current_version;
a241ec65
PM
139static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
140static DEFINE_SPINLOCK(rcu_torture_lock);
67522bee
PM
141static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count);
142static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch);
a241ec65 143static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
b2896d2e
PM
144static atomic_t n_rcu_torture_alloc;
145static atomic_t n_rcu_torture_alloc_fail;
146static atomic_t n_rcu_torture_free;
147static atomic_t n_rcu_torture_mberror;
148static atomic_t n_rcu_torture_error;
fae4b54f 149static long n_rcu_torture_barrier_error;
8e8be45e
PM
150static long n_rcu_torture_boost_ktrerror;
151static long n_rcu_torture_boost_rterror;
8e8be45e
PM
152static long n_rcu_torture_boost_failure;
153static long n_rcu_torture_boosts;
a71fca58 154static long n_rcu_torture_timers;
fae4b54f
PM
155static long n_barrier_attempts;
156static long n_barrier_successes;
38706bc5 157static atomic_long_t n_cbfloods;
e3033736 158static struct list_head rcu_torture_removed;
d120f65f 159
ad0dc7f9
PM
160static int rcu_torture_writer_state;
161#define RTWS_FIXED_DELAY 0
162#define RTWS_DELAY 1
163#define RTWS_REPLACE 2
164#define RTWS_DEF_FREE 3
165#define RTWS_EXP_SYNC 4
a48f3fad
PM
166#define RTWS_COND_GET 5
167#define RTWS_COND_SYNC 6
f0bf8fab
PM
168#define RTWS_SYNC 7
169#define RTWS_STUTTER 8
170#define RTWS_STOPPING 9
18aff33e
PM
171static const char * const rcu_torture_writer_state_names[] = {
172 "RTWS_FIXED_DELAY",
173 "RTWS_DELAY",
174 "RTWS_REPLACE",
175 "RTWS_DEF_FREE",
176 "RTWS_EXP_SYNC",
177 "RTWS_COND_GET",
178 "RTWS_COND_SYNC",
179 "RTWS_SYNC",
180 "RTWS_STUTTER",
181 "RTWS_STOPPING",
182};
183
184static const char *rcu_torture_writer_state_getname(void)
185{
186 unsigned int i = READ_ONCE(rcu_torture_writer_state);
187
188 if (i >= ARRAY_SIZE(rcu_torture_writer_state_names))
189 return "???";
190 return rcu_torture_writer_state_names[i];
191}
ad0dc7f9 192
3acf4a9a 193#if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
8e8be45e 194#define rcu_can_boost() 1
3acf4a9a 195#else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
8e8be45e 196#define rcu_can_boost() 0
3acf4a9a 197#endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
8e8be45e 198
e4aa0da3
SR
199#ifdef CONFIG_RCU_TRACE
200static u64 notrace rcu_trace_clock_local(void)
201{
202 u64 ts = trace_clock_local();
a3b7b6c2
PM
203
204 (void)do_div(ts, NSEC_PER_USEC);
e4aa0da3
SR
205 return ts;
206}
207#else /* #ifdef CONFIG_RCU_TRACE */
208static u64 notrace rcu_trace_clock_local(void)
209{
210 return 0ULL;
211}
212#endif /* #else #ifdef CONFIG_RCU_TRACE */
213
8e8be45e 214static unsigned long boost_starttime; /* jiffies of next boost test start. */
58ade2db 215static DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
8e8be45e 216 /* and boost task create/destroy. */
fae4b54f 217static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
c6ebcbb6 218static bool barrier_phase; /* Test phase. */
fae4b54f
PM
219static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
220static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
221static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
8e8be45e 222
a241ec65
PM
223/*
224 * Allocate an element from the rcu_tortures pool.
225 */
97a41e26 226static struct rcu_torture *
a241ec65
PM
227rcu_torture_alloc(void)
228{
229 struct list_head *p;
230
adac1665 231 spin_lock_bh(&rcu_torture_lock);
a241ec65
PM
232 if (list_empty(&rcu_torture_freelist)) {
233 atomic_inc(&n_rcu_torture_alloc_fail);
adac1665 234 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
235 return NULL;
236 }
237 atomic_inc(&n_rcu_torture_alloc);
238 p = rcu_torture_freelist.next;
239 list_del_init(p);
adac1665 240 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
241 return container_of(p, struct rcu_torture, rtort_free);
242}
243
244/*
245 * Free an element to the rcu_tortures pool.
246 */
247static void
248rcu_torture_free(struct rcu_torture *p)
249{
250 atomic_inc(&n_rcu_torture_free);
adac1665 251 spin_lock_bh(&rcu_torture_lock);
a241ec65 252 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
adac1665 253 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
254}
255
72e9bb54
PM
256/*
257 * Operations vector for selecting different types of tests.
258 */
259
260struct rcu_torture_ops {
ad0dc7f9 261 int ttype;
72e9bb54 262 void (*init)(void);
ca1d51ed 263 void (*cleanup)(void);
72e9bb54 264 int (*readlock)(void);
51b1130e 265 void (*read_delay)(struct torture_random_state *rrsp);
72e9bb54 266 void (*readunlock)(int idx);
17ef2fe9 267 unsigned long (*get_gp_seq)(void);
0acc512c 268 void (*deferred_free)(struct rcu_torture *p);
b772e1dd 269 void (*sync)(void);
2ec1f2d9 270 void (*exp_sync)(void);
a48f3fad
PM
271 unsigned long (*get_state)(void);
272 void (*cond_sync)(unsigned long oldstate);
db3e8db4 273 call_rcu_func_t call;
2326974d 274 void (*cb_barrier)(void);
bf66f18e 275 void (*fqs)(void);
eea203fe 276 void (*stats)(void);
0acc512c 277 int irq_capable;
8e8be45e 278 int can_boost;
e66c33d5 279 const char *name;
72e9bb54 280};
a71fca58
PM
281
282static struct rcu_torture_ops *cur_ops;
72e9bb54
PM
283
284/*
285 * Definitions for rcu torture testing.
286 */
287
a49a4af7 288static int rcu_torture_read_lock(void) __acquires(RCU)
72e9bb54
PM
289{
290 rcu_read_lock();
291 return 0;
292}
293
51b1130e 294static void rcu_read_delay(struct torture_random_state *rrsp)
b2896d2e 295{
d0af39e8
PM
296 unsigned long started;
297 unsigned long completed;
b8d57a76
JT
298 const unsigned long shortdelay_us = 200;
299 const unsigned long longdelay_ms = 50;
d0af39e8 300 unsigned long long ts;
b2896d2e 301
b8d57a76
JT
302 /* We want a short delay sometimes to make a reader delay the grace
303 * period, and we want a long delay occasionally to trigger
304 * force_quiescent_state. */
b2896d2e 305
d0af39e8 306 if (!(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms))) {
17ef2fe9 307 started = cur_ops->get_gp_seq();
d0af39e8 308 ts = rcu_trace_clock_local();
b8d57a76 309 mdelay(longdelay_ms);
17ef2fe9 310 completed = cur_ops->get_gp_seq();
d0af39e8
PM
311 do_trace_rcu_torture_read(cur_ops->name, NULL, ts,
312 started, completed);
313 }
51b1130e 314 if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
b8d57a76 315 udelay(shortdelay_us);
51b1130e 316 if (!preempt_count() &&
e8302739 317 !(torture_random(rrsp) % (nrealreaders * 500)))
cc1321c9 318 torture_preempt_schedule(); /* QS only if preemptible. */
b2896d2e
PM
319}
320
a49a4af7 321static void rcu_torture_read_unlock(int idx) __releases(RCU)
72e9bb54
PM
322{
323 rcu_read_unlock();
324}
325
a48f3fad
PM
326/*
327 * Update callback in the pipe. This should be invoked after a grace period.
328 */
329static bool
330rcu_torture_pipe_update_one(struct rcu_torture *rp)
331{
332 int i;
333
334 i = rp->rtort_pipe_count;
335 if (i > RCU_TORTURE_PIPE_LEN)
336 i = RCU_TORTURE_PIPE_LEN;
337 atomic_inc(&rcu_torture_wcount[i]);
338 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
339 rp->rtort_mbtest = 0;
340 return true;
341 }
342 return false;
343}
344
345/*
346 * Update all callbacks in the pipe. Suitable for synchronous grace-period
347 * primitives.
348 */
349static void
350rcu_torture_pipe_update(struct rcu_torture *old_rp)
351{
352 struct rcu_torture *rp;
353 struct rcu_torture *rp1;
354
355 if (old_rp)
356 list_add(&old_rp->rtort_free, &rcu_torture_removed);
357 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
358 if (rcu_torture_pipe_update_one(rp)) {
359 list_del(&rp->rtort_free);
360 rcu_torture_free(rp);
361 }
362 }
363}
364
72e9bb54
PM
365static void
366rcu_torture_cb(struct rcu_head *p)
367{
72e9bb54
PM
368 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
369
36970bb9 370 if (torture_must_stop_irq()) {
72e9bb54
PM
371 /* Test is ending, just drop callbacks on the floor. */
372 /* The next initialization will pick up the pieces. */
373 return;
374 }
a48f3fad 375 if (rcu_torture_pipe_update_one(rp))
72e9bb54 376 rcu_torture_free(rp);
a48f3fad 377 else
0acc512c 378 cur_ops->deferred_free(rp);
72e9bb54
PM
379}
380
6b80da42 381static unsigned long rcu_no_completed(void)
d9a3da06
PM
382{
383 return 0;
384}
385
72e9bb54
PM
386static void rcu_torture_deferred_free(struct rcu_torture *p)
387{
388 call_rcu(&p->rtort_rcu, rcu_torture_cb);
389}
390
e3033736
JT
391static void rcu_sync_torture_init(void)
392{
393 INIT_LIST_HEAD(&rcu_torture_removed);
394}
395
2ec1f2d9 396static struct rcu_torture_ops rcu_ops = {
ad0dc7f9 397 .ttype = RCU_FLAVOR,
0acc512c 398 .init = rcu_sync_torture_init,
0acc512c
PM
399 .readlock = rcu_torture_read_lock,
400 .read_delay = rcu_read_delay,
401 .readunlock = rcu_torture_read_unlock,
17ef2fe9 402 .get_gp_seq = rcu_get_gp_seq,
2ec1f2d9 403 .deferred_free = rcu_torture_deferred_free,
0acc512c 404 .sync = synchronize_rcu,
2ec1f2d9 405 .exp_sync = synchronize_rcu_expedited,
a48f3fad
PM
406 .get_state = get_state_synchronize_rcu,
407 .cond_sync = cond_synchronize_rcu,
2ec1f2d9
PM
408 .call = call_rcu,
409 .cb_barrier = rcu_barrier,
bf66f18e 410 .fqs = rcu_force_quiescent_state,
d9a3da06
PM
411 .stats = NULL,
412 .irq_capable = 1,
8e8be45e 413 .can_boost = rcu_can_boost(),
2ec1f2d9 414 .name = "rcu"
d9a3da06
PM
415};
416
c32e0660
PM
417/*
418 * Definitions for rcu_bh torture testing.
419 */
420
a49a4af7 421static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
c32e0660
PM
422{
423 rcu_read_lock_bh();
424 return 0;
425}
426
a49a4af7 427static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
c32e0660
PM
428{
429 rcu_read_unlock_bh();
430}
431
c32e0660
PM
432static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
433{
434 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
435}
436
437static struct rcu_torture_ops rcu_bh_ops = {
ad0dc7f9 438 .ttype = RCU_BH_FLAVOR,
2ec1f2d9 439 .init = rcu_sync_torture_init,
0acc512c
PM
440 .readlock = rcu_bh_torture_read_lock,
441 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
442 .readunlock = rcu_bh_torture_read_unlock,
17ef2fe9 443 .get_gp_seq = rcu_bh_get_gp_seq,
0acc512c 444 .deferred_free = rcu_bh_torture_deferred_free,
bdf2a436 445 .sync = synchronize_rcu_bh,
2ec1f2d9 446 .exp_sync = synchronize_rcu_bh_expedited,
fae4b54f 447 .call = call_rcu_bh,
0acc512c 448 .cb_barrier = rcu_barrier_bh,
bf66f18e 449 .fqs = rcu_bh_force_quiescent_state,
0acc512c
PM
450 .stats = NULL,
451 .irq_capable = 1,
452 .name = "rcu_bh"
c32e0660
PM
453};
454
ff20e251
PM
455/*
456 * Don't even think about trying any of these in real life!!!
457 * The names includes "busted", and they really means it!
458 * The only purpose of these functions is to provide a buggy RCU
459 * implementation to make sure that rcutorture correctly emits
460 * buggy-RCU error messages.
461 */
462static void rcu_busted_torture_deferred_free(struct rcu_torture *p)
463{
464 /* This is a deliberate bug for testing purposes only! */
465 rcu_torture_cb(&p->rtort_rcu);
466}
467
468static void synchronize_rcu_busted(void)
469{
470 /* This is a deliberate bug for testing purposes only! */
471}
472
473static void
b6a4ae76 474call_rcu_busted(struct rcu_head *head, rcu_callback_t func)
ff20e251
PM
475{
476 /* This is a deliberate bug for testing purposes only! */
477 func(head);
478}
479
480static struct rcu_torture_ops rcu_busted_ops = {
ad0dc7f9 481 .ttype = INVALID_RCU_FLAVOR,
ff20e251
PM
482 .init = rcu_sync_torture_init,
483 .readlock = rcu_torture_read_lock,
484 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
485 .readunlock = rcu_torture_read_unlock,
17ef2fe9 486 .get_gp_seq = rcu_no_completed,
ff20e251
PM
487 .deferred_free = rcu_busted_torture_deferred_free,
488 .sync = synchronize_rcu_busted,
489 .exp_sync = synchronize_rcu_busted,
490 .call = call_rcu_busted,
491 .cb_barrier = NULL,
492 .fqs = NULL,
493 .stats = NULL,
494 .irq_capable = 1,
b3c98314 495 .name = "busted"
ff20e251
PM
496};
497
b2896d2e
PM
498/*
499 * Definitions for srcu torture testing.
500 */
501
cda4dc81 502DEFINE_STATIC_SRCU(srcu_ctl);
ca1d51ed
PM
503static struct srcu_struct srcu_ctld;
504static struct srcu_struct *srcu_ctlp = &srcu_ctl;
b2896d2e 505
ca1d51ed 506static int srcu_torture_read_lock(void) __acquires(srcu_ctlp)
b2896d2e 507{
ca1d51ed 508 return srcu_read_lock(srcu_ctlp);
b2896d2e
PM
509}
510
51b1130e 511static void srcu_read_delay(struct torture_random_state *rrsp)
b2896d2e
PM
512{
513 long delay;
514 const long uspertick = 1000000 / HZ;
515 const long longdelay = 10;
516
517 /* We want there to be long-running readers, but not all the time. */
518
51b1130e
PM
519 delay = torture_random(rrsp) %
520 (nrealreaders * 2 * longdelay * uspertick);
5e741fa9 521 if (!delay && in_task())
b2896d2e 522 schedule_timeout_interruptible(longdelay);
e546f485
LJ
523 else
524 rcu_read_delay(rrsp);
b2896d2e
PM
525}
526
ca1d51ed 527static void srcu_torture_read_unlock(int idx) __releases(srcu_ctlp)
b2896d2e 528{
ca1d51ed 529 srcu_read_unlock(srcu_ctlp, idx);
b2896d2e
PM
530}
531
6b80da42 532static unsigned long srcu_torture_completed(void)
b2896d2e 533{
ca1d51ed 534 return srcu_batches_completed(srcu_ctlp);
b2896d2e
PM
535}
536
9059c940
LJ
537static void srcu_torture_deferred_free(struct rcu_torture *rp)
538{
ca1d51ed 539 call_srcu(srcu_ctlp, &rp->rtort_rcu, rcu_torture_cb);
9059c940
LJ
540}
541
b772e1dd
JT
542static void srcu_torture_synchronize(void)
543{
ca1d51ed 544 synchronize_srcu(srcu_ctlp);
b772e1dd
JT
545}
546
e3f8d378 547static void srcu_torture_call(struct rcu_head *head,
b6a4ae76 548 rcu_callback_t func)
e3f8d378 549{
ca1d51ed 550 call_srcu(srcu_ctlp, head, func);
e3f8d378
PM
551}
552
553static void srcu_torture_barrier(void)
554{
ca1d51ed 555 srcu_barrier(srcu_ctlp);
e3f8d378
PM
556}
557
eea203fe 558static void srcu_torture_stats(void)
b2896d2e 559{
115a1a52 560 srcu_torture_stats_print(srcu_ctlp, torture_type, TORTURE_FLAG);
b2896d2e
PM
561}
562
2ec1f2d9
PM
563static void srcu_torture_synchronize_expedited(void)
564{
ca1d51ed 565 synchronize_srcu_expedited(srcu_ctlp);
2ec1f2d9
PM
566}
567
b2896d2e 568static struct rcu_torture_ops srcu_ops = {
ad0dc7f9 569 .ttype = SRCU_FLAVOR,
cda4dc81 570 .init = rcu_sync_torture_init,
0acc512c
PM
571 .readlock = srcu_torture_read_lock,
572 .read_delay = srcu_read_delay,
573 .readunlock = srcu_torture_read_unlock,
17ef2fe9 574 .get_gp_seq = srcu_torture_completed,
9059c940 575 .deferred_free = srcu_torture_deferred_free,
0acc512c 576 .sync = srcu_torture_synchronize,
2ec1f2d9 577 .exp_sync = srcu_torture_synchronize_expedited,
e3f8d378
PM
578 .call = srcu_torture_call,
579 .cb_barrier = srcu_torture_barrier,
0acc512c 580 .stats = srcu_torture_stats,
5e741fa9 581 .irq_capable = 1,
0acc512c 582 .name = "srcu"
b2896d2e
PM
583};
584
ca1d51ed
PM
585static void srcu_torture_init(void)
586{
587 rcu_sync_torture_init();
588 WARN_ON(init_srcu_struct(&srcu_ctld));
589 srcu_ctlp = &srcu_ctld;
590}
591
592static void srcu_torture_cleanup(void)
593{
f7194ac3
PM
594 static DEFINE_TORTURE_RANDOM(rand);
595
596 if (torture_random(&rand) & 0x800)
597 cleanup_srcu_struct(&srcu_ctld);
598 else
599 cleanup_srcu_struct_quiesced(&srcu_ctld);
ca1d51ed
PM
600 srcu_ctlp = &srcu_ctl; /* In case of a later rcutorture run. */
601}
602
603/* As above, but dynamically allocated. */
604static struct rcu_torture_ops srcud_ops = {
605 .ttype = SRCU_FLAVOR,
606 .init = srcu_torture_init,
607 .cleanup = srcu_torture_cleanup,
608 .readlock = srcu_torture_read_lock,
609 .read_delay = srcu_read_delay,
610 .readunlock = srcu_torture_read_unlock,
17ef2fe9 611 .get_gp_seq = srcu_torture_completed,
ca1d51ed
PM
612 .deferred_free = srcu_torture_deferred_free,
613 .sync = srcu_torture_synchronize,
614 .exp_sync = srcu_torture_synchronize_expedited,
615 .call = srcu_torture_call,
616 .cb_barrier = srcu_torture_barrier,
617 .stats = srcu_torture_stats,
5e741fa9 618 .irq_capable = 1,
ca1d51ed
PM
619 .name = "srcud"
620};
621
4b6c2cca
JT
622/*
623 * Definitions for sched torture testing.
624 */
625
626static int sched_torture_read_lock(void)
627{
628 preempt_disable();
629 return 0;
630}
631
632static void sched_torture_read_unlock(int idx)
633{
634 preempt_enable();
635}
636
2326974d
PM
637static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
638{
639 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
640}
641
4b6c2cca 642static struct rcu_torture_ops sched_ops = {
ad0dc7f9 643 .ttype = RCU_SCHED_FLAVOR,
0acc512c 644 .init = rcu_sync_torture_init,
0acc512c
PM
645 .readlock = sched_torture_read_lock,
646 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
647 .readunlock = sched_torture_read_unlock,
17ef2fe9 648 .get_gp_seq = rcu_sched_get_gp_seq,
0acc512c 649 .deferred_free = rcu_sched_torture_deferred_free,
bdf2a436 650 .sync = synchronize_sched,
2ec1f2d9 651 .exp_sync = synchronize_sched_expedited,
24560056
PM
652 .get_state = get_state_synchronize_sched,
653 .cond_sync = cond_synchronize_sched,
2ec1f2d9 654 .call = call_rcu_sched,
0acc512c 655 .cb_barrier = rcu_barrier_sched,
bf66f18e 656 .fqs = rcu_sched_force_quiescent_state,
0acc512c
PM
657 .stats = NULL,
658 .irq_capable = 1,
659 .name = "sched"
4b6c2cca
JT
660};
661
69c60455
PM
662/*
663 * Definitions for RCU-tasks torture testing.
664 */
665
666static int tasks_torture_read_lock(void)
667{
668 return 0;
669}
670
671static void tasks_torture_read_unlock(int idx)
672{
673}
674
675static void rcu_tasks_torture_deferred_free(struct rcu_torture *p)
676{
677 call_rcu_tasks(&p->rtort_rcu, rcu_torture_cb);
678}
679
680static struct rcu_torture_ops tasks_ops = {
681 .ttype = RCU_TASKS_FLAVOR,
682 .init = rcu_sync_torture_init,
683 .readlock = tasks_torture_read_lock,
684 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
685 .readunlock = tasks_torture_read_unlock,
17ef2fe9 686 .get_gp_seq = rcu_no_completed,
69c60455
PM
687 .deferred_free = rcu_tasks_torture_deferred_free,
688 .sync = synchronize_rcu_tasks,
689 .exp_sync = synchronize_rcu_tasks,
690 .call = call_rcu_tasks,
691 .cb_barrier = rcu_barrier_tasks,
692 .fqs = NULL,
693 .stats = NULL,
694 .irq_capable = 1,
695 .name = "tasks"
696};
697
5be5d1a1
PM
698static bool __maybe_unused torturing_tasks(void)
699{
700 return cur_ops == &tasks_ops;
701}
702
8e8be45e
PM
703/*
704 * RCU torture priority-boost testing. Runs one real-time thread per
705 * CPU for moderate bursts, repeatedly registering RCU callbacks and
706 * spinning waiting for them to be invoked. If a given callback takes
707 * too long to be invoked, we assume that priority inversion has occurred.
708 */
709
710struct rcu_boost_inflight {
711 struct rcu_head rcu;
712 int inflight;
713};
714
715static void rcu_torture_boost_cb(struct rcu_head *head)
716{
717 struct rcu_boost_inflight *rbip =
718 container_of(head, struct rcu_boost_inflight, rcu);
719
6c7ed42c
PM
720 /* Ensure RCU-core accesses precede clearing ->inflight */
721 smp_store_release(&rbip->inflight, 0);
8e8be45e
PM
722}
723
724static int rcu_torture_boost(void *arg)
725{
726 unsigned long call_rcu_time;
727 unsigned long endtime;
728 unsigned long oldstarttime;
729 struct rcu_boost_inflight rbi = { .inflight = 0 };
730 struct sched_param sp;
731
5ccf60f2 732 VERBOSE_TOROUT_STRING("rcu_torture_boost started");
8e8be45e
PM
733
734 /* Set real-time priority. */
735 sp.sched_priority = 1;
736 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
5ccf60f2 737 VERBOSE_TOROUT_STRING("rcu_torture_boost RT prio failed!");
8e8be45e
PM
738 n_rcu_torture_boost_rterror++;
739 }
740
561190e3 741 init_rcu_head_on_stack(&rbi.rcu);
8e8be45e
PM
742 /* Each pass through the following loop does one boost-test cycle. */
743 do {
744 /* Wait for the next test interval. */
745 oldstarttime = boost_starttime;
93898fb1 746 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
0e11c8e8 747 schedule_timeout_interruptible(oldstarttime - jiffies);
628edaa5 748 stutter_wait("rcu_torture_boost");
36970bb9 749 if (torture_must_stop())
8e8be45e
PM
750 goto checkwait;
751 }
752
753 /* Do one boost-test interval. */
754 endtime = oldstarttime + test_boost_duration * HZ;
755 call_rcu_time = jiffies;
93898fb1 756 while (ULONG_CMP_LT(jiffies, endtime)) {
8e8be45e 757 /* If we don't have a callback in flight, post one. */
6c7ed42c
PM
758 if (!smp_load_acquire(&rbi.inflight)) {
759 /* RCU core before ->inflight = 1. */
760 smp_store_release(&rbi.inflight, 1);
8e8be45e
PM
761 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
762 if (jiffies - call_rcu_time >
763 test_boost_duration * HZ - HZ / 2) {
5ccf60f2 764 VERBOSE_TOROUT_STRING("rcu_torture_boost boosting failed");
8e8be45e
PM
765 n_rcu_torture_boost_failure++;
766 }
767 call_rcu_time = jiffies;
768 }
628edaa5 769 stutter_wait("rcu_torture_boost");
36970bb9 770 if (torture_must_stop())
8e8be45e
PM
771 goto checkwait;
772 }
773
774 /*
775 * Set the start time of the next test interval.
776 * Yes, this is vulnerable to long delays, but such
777 * delays simply cause a false negative for the next
778 * interval. Besides, we are running at RT priority,
779 * so delays should be relatively rare.
780 */
ab8f11e5
PM
781 while (oldstarttime == boost_starttime &&
782 !kthread_should_stop()) {
8e8be45e
PM
783 if (mutex_trylock(&boost_mutex)) {
784 boost_starttime = jiffies +
785 test_boost_interval * HZ;
786 n_rcu_torture_boosts++;
787 mutex_unlock(&boost_mutex);
788 break;
789 }
790 schedule_timeout_uninterruptible(1);
791 }
792
793 /* Go do the stutter. */
628edaa5 794checkwait: stutter_wait("rcu_torture_boost");
36970bb9 795 } while (!torture_must_stop());
8e8be45e
PM
796
797 /* Clean up and exit. */
6c7ed42c 798 while (!kthread_should_stop() || smp_load_acquire(&rbi.inflight)) {
7fafaac5 799 torture_shutdown_absorb("rcu_torture_boost");
8e8be45e 800 schedule_timeout_uninterruptible(1);
7fafaac5 801 }
9d68197c 802 destroy_rcu_head_on_stack(&rbi.rcu);
7fafaac5 803 torture_kthread_stopping("rcu_torture_boost");
8e8be45e
PM
804 return 0;
805}
806
38706bc5
PM
807static void rcu_torture_cbflood_cb(struct rcu_head *rhp)
808{
809}
810
811/*
812 * RCU torture callback-flood kthread. Repeatedly induces bursts of calls
813 * to call_rcu() or analogous, increasing the probability of occurrence
814 * of callback-overflow corner cases.
815 */
816static int
817rcu_torture_cbflood(void *arg)
818{
819 int err = 1;
820 int i;
821 int j;
822 struct rcu_head *rhp;
823
824 if (cbflood_n_per_burst > 0 &&
825 cbflood_inter_holdoff > 0 &&
826 cbflood_intra_holdoff > 0 &&
827 cur_ops->call &&
828 cur_ops->cb_barrier) {
42bc47b3
KC
829 rhp = vmalloc(array3_size(cbflood_n_burst,
830 cbflood_n_per_burst,
831 sizeof(*rhp)));
38706bc5
PM
832 err = !rhp;
833 }
834 if (err) {
835 VERBOSE_TOROUT_STRING("rcu_torture_cbflood disabled: Bad args or OOM");
3a0af333 836 goto wait_for_stop;
38706bc5
PM
837 }
838 VERBOSE_TOROUT_STRING("rcu_torture_cbflood task started");
839 do {
840 schedule_timeout_interruptible(cbflood_inter_holdoff);
841 atomic_long_inc(&n_cbfloods);
842 WARN_ON(signal_pending(current));
843 for (i = 0; i < cbflood_n_burst; i++) {
844 for (j = 0; j < cbflood_n_per_burst; j++) {
845 cur_ops->call(&rhp[i * cbflood_n_per_burst + j],
846 rcu_torture_cbflood_cb);
847 }
848 schedule_timeout_interruptible(cbflood_intra_holdoff);
849 WARN_ON(signal_pending(current));
850 }
851 cur_ops->cb_barrier();
852 stutter_wait("rcu_torture_cbflood");
853 } while (!torture_must_stop());
b8969d1a 854 vfree(rhp);
3a0af333 855wait_for_stop:
38706bc5
PM
856 torture_kthread_stopping("rcu_torture_cbflood");
857 return 0;
858}
859
bf66f18e
PM
860/*
861 * RCU torture force-quiescent-state kthread. Repeatedly induces
862 * bursts of calls to force_quiescent_state(), increasing the probability
863 * of occurrence of some important types of race conditions.
864 */
865static int
866rcu_torture_fqs(void *arg)
867{
868 unsigned long fqs_resume_time;
869 int fqs_burst_remaining;
870
5ccf60f2 871 VERBOSE_TOROUT_STRING("rcu_torture_fqs task started");
bf66f18e
PM
872 do {
873 fqs_resume_time = jiffies + fqs_stutter * HZ;
93898fb1
PM
874 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
875 !kthread_should_stop()) {
bf66f18e
PM
876 schedule_timeout_interruptible(1);
877 }
878 fqs_burst_remaining = fqs_duration;
93898fb1
PM
879 while (fqs_burst_remaining > 0 &&
880 !kthread_should_stop()) {
bf66f18e
PM
881 cur_ops->fqs();
882 udelay(fqs_holdoff);
883 fqs_burst_remaining -= fqs_holdoff;
884 }
628edaa5 885 stutter_wait("rcu_torture_fqs");
36970bb9 886 } while (!torture_must_stop());
7fafaac5 887 torture_kthread_stopping("rcu_torture_fqs");
bf66f18e
PM
888 return 0;
889}
890
a241ec65
PM
891/*
892 * RCU torture writer kthread. Repeatedly substitutes a new structure
893 * for that pointed to by rcu_torture_current, freeing the old structure
894 * after a series of grace periods (the "pipeline").
895 */
896static int
897rcu_torture_writer(void *arg)
898{
9efafb88 899 bool can_expedite = !rcu_gp_is_expedited() && !rcu_gp_is_normal();
4bb3c5f4 900 int expediting = 0;
a48f3fad
PM
901 unsigned long gp_snap;
902 bool gp_cond1 = gp_cond, gp_exp1 = gp_exp, gp_normal1 = gp_normal;
f0bf8fab 903 bool gp_sync1 = gp_sync;
a241ec65 904 int i;
a241ec65
PM
905 struct rcu_torture *rp;
906 struct rcu_torture *old_rp;
51b1130e 907 static DEFINE_TORTURE_RANDOM(rand);
f0bf8fab
PM
908 int synctype[] = { RTWS_DEF_FREE, RTWS_EXP_SYNC,
909 RTWS_COND_GET, RTWS_SYNC };
a48f3fad 910 int nsynctypes = 0;
a241ec65 911
5ccf60f2 912 VERBOSE_TOROUT_STRING("rcu_torture_writer task started");
f7c0e6ad 913 if (!can_expedite)
aa5a8988 914 pr_alert("%s" TORTURE_FLAG
f7c0e6ad 915 " GP expediting controlled from boot/sysfs for %s.\n",
aa5a8988 916 torture_type, cur_ops->name);
dbdf65b1 917
a48f3fad 918 /* Initialize synctype[] array. If none set, take default. */
c136f991 919 if (!gp_cond1 && !gp_exp1 && !gp_normal1 && !gp_sync1)
f0bf8fab 920 gp_cond1 = gp_exp1 = gp_normal1 = gp_sync1 = true;
db0c1a8a 921 if (gp_cond1 && cur_ops->get_state && cur_ops->cond_sync) {
a48f3fad 922 synctype[nsynctypes++] = RTWS_COND_GET;
db0c1a8a
PM
923 pr_info("%s: Testing conditional GPs.\n", __func__);
924 } else if (gp_cond && (!cur_ops->get_state || !cur_ops->cond_sync)) {
e0d31a34 925 pr_alert("%s: gp_cond without primitives.\n", __func__);
db0c1a8a
PM
926 }
927 if (gp_exp1 && cur_ops->exp_sync) {
a48f3fad 928 synctype[nsynctypes++] = RTWS_EXP_SYNC;
db0c1a8a
PM
929 pr_info("%s: Testing expedited GPs.\n", __func__);
930 } else if (gp_exp && !cur_ops->exp_sync) {
e0d31a34 931 pr_alert("%s: gp_exp without primitives.\n", __func__);
db0c1a8a
PM
932 }
933 if (gp_normal1 && cur_ops->deferred_free) {
a48f3fad 934 synctype[nsynctypes++] = RTWS_DEF_FREE;
db0c1a8a
PM
935 pr_info("%s: Testing asynchronous GPs.\n", __func__);
936 } else if (gp_normal && !cur_ops->deferred_free) {
e0d31a34 937 pr_alert("%s: gp_normal without primitives.\n", __func__);
db0c1a8a
PM
938 }
939 if (gp_sync1 && cur_ops->sync) {
f0bf8fab 940 synctype[nsynctypes++] = RTWS_SYNC;
db0c1a8a
PM
941 pr_info("%s: Testing normal GPs.\n", __func__);
942 } else if (gp_sync && !cur_ops->sync) {
e0d31a34 943 pr_alert("%s: gp_sync without primitives.\n", __func__);
db0c1a8a 944 }
a48f3fad
PM
945 if (WARN_ONCE(nsynctypes == 0,
946 "rcu_torture_writer: No update-side primitives.\n")) {
f0bf8fab
PM
947 /*
948 * No updates primitives, so don't try updating.
949 * The resulting test won't be testing much, hence the
950 * above WARN_ONCE().
951 */
a48f3fad
PM
952 rcu_torture_writer_state = RTWS_STOPPING;
953 torture_kthread_stopping("rcu_torture_writer");
954 }
955
a241ec65 956 do {
ad0dc7f9 957 rcu_torture_writer_state = RTWS_FIXED_DELAY;
a241ec65 958 schedule_timeout_uninterruptible(1);
a71fca58
PM
959 rp = rcu_torture_alloc();
960 if (rp == NULL)
a241ec65
PM
961 continue;
962 rp->rtort_pipe_count = 0;
ad0dc7f9 963 rcu_torture_writer_state = RTWS_DELAY;
51b1130e 964 udelay(torture_random(&rand) & 0x3ff);
ad0dc7f9 965 rcu_torture_writer_state = RTWS_REPLACE;
0ddea0ea
PM
966 old_rp = rcu_dereference_check(rcu_torture_current,
967 current == writer_task);
996417d2 968 rp->rtort_mbtest = 1;
a241ec65 969 rcu_assign_pointer(rcu_torture_current, rp);
9b2619af 970 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
c8e5b163 971 if (old_rp) {
a241ec65
PM
972 i = old_rp->rtort_pipe_count;
973 if (i > RCU_TORTURE_PIPE_LEN)
974 i = RCU_TORTURE_PIPE_LEN;
975 atomic_inc(&rcu_torture_wcount[i]);
976 old_rp->rtort_pipe_count++;
a48f3fad
PM
977 switch (synctype[torture_random(&rand) % nsynctypes]) {
978 case RTWS_DEF_FREE:
ad0dc7f9 979 rcu_torture_writer_state = RTWS_DEF_FREE;
2ec1f2d9 980 cur_ops->deferred_free(old_rp);
a48f3fad
PM
981 break;
982 case RTWS_EXP_SYNC:
ad0dc7f9 983 rcu_torture_writer_state = RTWS_EXP_SYNC;
2ec1f2d9 984 cur_ops->exp_sync();
a48f3fad
PM
985 rcu_torture_pipe_update(old_rp);
986 break;
987 case RTWS_COND_GET:
988 rcu_torture_writer_state = RTWS_COND_GET;
989 gp_snap = cur_ops->get_state();
990 i = torture_random(&rand) % 16;
991 if (i != 0)
992 schedule_timeout_interruptible(i);
993 udelay(torture_random(&rand) % 1000);
994 rcu_torture_writer_state = RTWS_COND_SYNC;
995 cur_ops->cond_sync(gp_snap);
996 rcu_torture_pipe_update(old_rp);
997 break;
f0bf8fab
PM
998 case RTWS_SYNC:
999 rcu_torture_writer_state = RTWS_SYNC;
1000 cur_ops->sync();
1001 rcu_torture_pipe_update(old_rp);
1002 break;
a48f3fad
PM
1003 default:
1004 WARN_ON_ONCE(1);
1005 break;
2ec1f2d9 1006 }
a241ec65 1007 }
4a298656 1008 rcutorture_record_progress(++rcu_torture_current_version);
4bb3c5f4
PM
1009 /* Cycle through nesting levels of rcu_expedite_gp() calls. */
1010 if (can_expedite &&
1011 !(torture_random(&rand) & 0xff & (!!expediting - 1))) {
1012 WARN_ON_ONCE(expediting == 0 && rcu_gp_is_expedited());
1013 if (expediting >= 0)
1014 rcu_expedite_gp();
1015 else
1016 rcu_unexpedite_gp();
1017 if (++expediting > 3)
1018 expediting = -expediting;
f7c0e6ad
PM
1019 } else if (!can_expedite) { /* Disabled during boot, recheck. */
1020 can_expedite = !rcu_gp_is_expedited() &&
1021 !rcu_gp_is_normal();
4bb3c5f4 1022 }
ad0dc7f9 1023 rcu_torture_writer_state = RTWS_STUTTER;
628edaa5 1024 stutter_wait("rcu_torture_writer");
36970bb9 1025 } while (!torture_must_stop());
4bb3c5f4
PM
1026 /* Reset expediting back to unexpedited. */
1027 if (expediting > 0)
1028 expediting = -expediting;
1029 while (can_expedite && expediting++ < 0)
1030 rcu_unexpedite_gp();
1031 WARN_ON_ONCE(can_expedite && rcu_gp_is_expedited());
f7c0e6ad
PM
1032 if (!can_expedite)
1033 pr_alert("%s" TORTURE_FLAG
1034 " Dynamic grace-period expediting was disabled.\n",
1035 torture_type);
ad0dc7f9 1036 rcu_torture_writer_state = RTWS_STOPPING;
7fafaac5 1037 torture_kthread_stopping("rcu_torture_writer");
a241ec65
PM
1038 return 0;
1039}
1040
b772e1dd
JT
1041/*
1042 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
1043 * delay between calls.
1044 */
1045static int
1046rcu_torture_fakewriter(void *arg)
1047{
51b1130e 1048 DEFINE_TORTURE_RANDOM(rand);
b772e1dd 1049
5ccf60f2 1050 VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
d277d868 1051 set_user_nice(current, MAX_NICE);
b772e1dd
JT
1052
1053 do {
51b1130e
PM
1054 schedule_timeout_uninterruptible(1 + torture_random(&rand)%10);
1055 udelay(torture_random(&rand) & 0x3ff);
72472a02 1056 if (cur_ops->cb_barrier != NULL &&
51b1130e 1057 torture_random(&rand) % (nfakewriters * 8) == 0) {
72472a02 1058 cur_ops->cb_barrier();
2ec1f2d9 1059 } else if (gp_normal == gp_exp) {
eb033993 1060 if (cur_ops->sync && torture_random(&rand) & 0x80)
2ec1f2d9 1061 cur_ops->sync();
eb033993 1062 else if (cur_ops->exp_sync)
2ec1f2d9 1063 cur_ops->exp_sync();
eb033993 1064 } else if (gp_normal && cur_ops->sync) {
72472a02 1065 cur_ops->sync();
eb033993 1066 } else if (cur_ops->exp_sync) {
2ec1f2d9
PM
1067 cur_ops->exp_sync();
1068 }
628edaa5 1069 stutter_wait("rcu_torture_fakewriter");
36970bb9 1070 } while (!torture_must_stop());
b772e1dd 1071
7fafaac5 1072 torture_kthread_stopping("rcu_torture_fakewriter");
b772e1dd
JT
1073 return 0;
1074}
1075
f34c8585
PM
1076static void rcu_torture_timer_cb(struct rcu_head *rhp)
1077{
1078 kfree(rhp);
1079}
1080
0729fbf3
PM
1081/*
1082 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
1083 * incrementing the corresponding element of the pipeline array. The
1084 * counter in the element should never be greater than 1, otherwise, the
1085 * RCU implementation is broken.
1086 */
fd30b717 1087static void rcu_torture_timer(struct timer_list *unused)
0729fbf3
PM
1088{
1089 int idx;
917963d0 1090 unsigned long started;
6b80da42 1091 unsigned long completed;
51b1130e 1092 static DEFINE_TORTURE_RANDOM(rand);
0729fbf3
PM
1093 static DEFINE_SPINLOCK(rand_lock);
1094 struct rcu_torture *p;
1095 int pipe_count;
52494535 1096 unsigned long long ts;
0729fbf3
PM
1097
1098 idx = cur_ops->readlock();
17ef2fe9 1099 started = cur_ops->get_gp_seq();
e4aa0da3 1100 ts = rcu_trace_clock_local();
632ee200 1101 p = rcu_dereference_check(rcu_torture_current,
632ee200
PM
1102 rcu_read_lock_bh_held() ||
1103 rcu_read_lock_sched_held() ||
5be5d1a1
PM
1104 srcu_read_lock_held(srcu_ctlp) ||
1105 torturing_tasks());
0729fbf3
PM
1106 if (p == NULL) {
1107 /* Leave because rcu_torture_writer is not yet underway */
1108 cur_ops->readunlock(idx);
1109 return;
1110 }
1111 if (p->rtort_mbtest == 0)
1112 atomic_inc(&n_rcu_torture_mberror);
1113 spin_lock(&rand_lock);
0acc512c 1114 cur_ops->read_delay(&rand);
0729fbf3
PM
1115 n_rcu_torture_timers++;
1116 spin_unlock(&rand_lock);
1117 preempt_disable();
1118 pipe_count = p->rtort_pipe_count;
1119 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1120 /* Should not happen, but... */
1121 pipe_count = RCU_TORTURE_PIPE_LEN;
1122 }
17ef2fe9 1123 completed = cur_ops->get_gp_seq();
52494535 1124 if (pipe_count > 1) {
52494535 1125 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
917963d0 1126 started, completed);
274529ba 1127 rcu_ftrace_dump(DUMP_ALL);
52494535 1128 }
dd17c8f7 1129 __this_cpu_inc(rcu_torture_count[pipe_count]);
917963d0 1130 completed = completed - started;
17ef2fe9
PM
1131 if (completed > ULONG_MAX >> 1)
1132 completed = 0; /* Not all gp_seq have full range. */
0729fbf3
PM
1133 if (completed > RCU_TORTURE_PIPE_LEN) {
1134 /* Should not happen, but... */
1135 completed = RCU_TORTURE_PIPE_LEN;
1136 }
dd17c8f7 1137 __this_cpu_inc(rcu_torture_batch[completed]);
0729fbf3
PM
1138 preempt_enable();
1139 cur_ops->readunlock(idx);
f34c8585
PM
1140
1141 /* Test call_rcu() invocation from interrupt handler. */
1142 if (cur_ops->call) {
1143 struct rcu_head *rhp = kmalloc(sizeof(*rhp), GFP_NOWAIT);
1144
1145 if (rhp)
1146 cur_ops->call(rhp, rcu_torture_timer_cb);
1147 }
0729fbf3
PM
1148}
1149
a241ec65
PM
1150/*
1151 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
1152 * incrementing the corresponding element of the pipeline array. The
1153 * counter in the element should never be greater than 1, otherwise, the
1154 * RCU implementation is broken.
1155 */
1156static int
1157rcu_torture_reader(void *arg)
1158{
917963d0 1159 unsigned long started;
6b80da42 1160 unsigned long completed;
72e9bb54 1161 int idx;
51b1130e 1162 DEFINE_TORTURE_RANDOM(rand);
a241ec65
PM
1163 struct rcu_torture *p;
1164 int pipe_count;
0729fbf3 1165 struct timer_list t;
52494535 1166 unsigned long long ts;
a241ec65 1167
5ccf60f2 1168 VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
d277d868 1169 set_user_nice(current, MAX_NICE);
0acc512c 1170 if (irqreader && cur_ops->irq_capable)
fd30b717 1171 timer_setup_on_stack(&t, rcu_torture_timer, 0);
dbdf65b1 1172
a241ec65 1173 do {
0acc512c 1174 if (irqreader && cur_ops->irq_capable) {
0729fbf3 1175 if (!timer_pending(&t))
6155fec9 1176 mod_timer(&t, jiffies + 1);
0729fbf3 1177 }
72e9bb54 1178 idx = cur_ops->readlock();
17ef2fe9 1179 started = cur_ops->get_gp_seq();
e4aa0da3 1180 ts = rcu_trace_clock_local();
632ee200 1181 p = rcu_dereference_check(rcu_torture_current,
632ee200
PM
1182 rcu_read_lock_bh_held() ||
1183 rcu_read_lock_sched_held() ||
5be5d1a1
PM
1184 srcu_read_lock_held(srcu_ctlp) ||
1185 torturing_tasks());
a241ec65
PM
1186 if (p == NULL) {
1187 /* Wait for rcu_torture_writer to get underway */
72e9bb54 1188 cur_ops->readunlock(idx);
a241ec65
PM
1189 schedule_timeout_interruptible(HZ);
1190 continue;
1191 }
996417d2
PM
1192 if (p->rtort_mbtest == 0)
1193 atomic_inc(&n_rcu_torture_mberror);
0acc512c 1194 cur_ops->read_delay(&rand);
a241ec65
PM
1195 preempt_disable();
1196 pipe_count = p->rtort_pipe_count;
1197 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1198 /* Should not happen, but... */
1199 pipe_count = RCU_TORTURE_PIPE_LEN;
1200 }
17ef2fe9 1201 completed = cur_ops->get_gp_seq();
52494535 1202 if (pipe_count > 1) {
52494535 1203 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
917963d0 1204 ts, started, completed);
274529ba 1205 rcu_ftrace_dump(DUMP_ALL);
52494535 1206 }
dd17c8f7 1207 __this_cpu_inc(rcu_torture_count[pipe_count]);
917963d0 1208 completed = completed - started;
17ef2fe9
PM
1209 if (completed > ULONG_MAX >> 1)
1210 completed = 0; /* Not all gp_seq have full range. */
a241ec65
PM
1211 if (completed > RCU_TORTURE_PIPE_LEN) {
1212 /* Should not happen, but... */
1213 completed = RCU_TORTURE_PIPE_LEN;
1214 }
dd17c8f7 1215 __this_cpu_inc(rcu_torture_batch[completed]);
a241ec65 1216 preempt_enable();
72e9bb54 1217 cur_ops->readunlock(idx);
628edaa5 1218 stutter_wait("rcu_torture_reader");
36970bb9 1219 } while (!torture_must_stop());
424c1b68 1220 if (irqreader && cur_ops->irq_capable) {
0729fbf3 1221 del_timer_sync(&t);
424c1b68
TG
1222 destroy_timer_on_stack(&t);
1223 }
7fafaac5 1224 torture_kthread_stopping("rcu_torture_reader");
a241ec65
PM
1225 return 0;
1226}
1227
1228/*
eea203fe
JP
1229 * Print torture statistics. Caller must ensure that there is only
1230 * one call to this function at a given time!!! This is normally
1231 * accomplished by relying on the module system to only have one copy
1232 * of the module loaded, and then by giving the rcu_torture_stats
1233 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1234 * thread is not running).
a241ec65 1235 */
d1008950 1236static void
eea203fe 1237rcu_torture_stats_print(void)
a241ec65 1238{
a241ec65
PM
1239 int cpu;
1240 int i;
1241 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1242 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
ad0dc7f9 1243 static unsigned long rtcv_snap = ULONG_MAX;
0032f4e8 1244 static bool splatted;
4ffa6699 1245 struct task_struct *wtp;
a241ec65 1246
0a945022 1247 for_each_possible_cpu(cpu) {
a241ec65
PM
1248 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1249 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1250 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1251 }
1252 }
1253 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1254 if (pipesummary[i] != 0)
1255 break;
1256 }
eea203fe
JP
1257
1258 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1259 pr_cont("rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
1260 rcu_torture_current,
1261 rcu_torture_current_version,
1262 list_empty(&rcu_torture_freelist),
1263 atomic_read(&n_rcu_torture_alloc),
1264 atomic_read(&n_rcu_torture_alloc_fail),
1265 atomic_read(&n_rcu_torture_free));
472213a6 1266 pr_cont("rtmbe: %d rtbe: %ld rtbke: %ld rtbre: %ld ",
eea203fe 1267 atomic_read(&n_rcu_torture_mberror),
472213a6 1268 n_rcu_torture_barrier_error,
eea203fe
JP
1269 n_rcu_torture_boost_ktrerror,
1270 n_rcu_torture_boost_rterror);
1271 pr_cont("rtbf: %ld rtb: %ld nt: %ld ",
1272 n_rcu_torture_boost_failure,
1273 n_rcu_torture_boosts,
1274 n_rcu_torture_timers);
1275 torture_onoff_stats();
38706bc5 1276 pr_cont("barrier: %ld/%ld:%ld ",
eea203fe
JP
1277 n_barrier_successes,
1278 n_barrier_attempts,
1279 n_rcu_torture_barrier_error);
38706bc5 1280 pr_cont("cbflood: %ld\n", atomic_long_read(&n_cbfloods));
eea203fe
JP
1281
1282 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
8e8be45e 1283 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
fae4b54f 1284 n_rcu_torture_barrier_error != 0 ||
8e8be45e
PM
1285 n_rcu_torture_boost_ktrerror != 0 ||
1286 n_rcu_torture_boost_rterror != 0 ||
fae4b54f
PM
1287 n_rcu_torture_boost_failure != 0 ||
1288 i > 1) {
eea203fe 1289 pr_cont("%s", "!!! ");
996417d2 1290 atomic_inc(&n_rcu_torture_error);
5af970a4 1291 WARN_ON_ONCE(1);
996417d2 1292 }
eea203fe 1293 pr_cont("Reader Pipe: ");
a241ec65 1294 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
eea203fe
JP
1295 pr_cont(" %ld", pipesummary[i]);
1296 pr_cont("\n");
1297
1298 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1299 pr_cont("Reader Batch: ");
72e9bb54 1300 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
eea203fe
JP
1301 pr_cont(" %ld", batchsummary[i]);
1302 pr_cont("\n");
1303
1304 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1305 pr_cont("Free-Block Circulation: ");
a241ec65 1306 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
eea203fe 1307 pr_cont(" %d", atomic_read(&rcu_torture_wcount[i]));
a241ec65 1308 }
eea203fe
JP
1309 pr_cont("\n");
1310
c8e5b163 1311 if (cur_ops->stats)
eea203fe 1312 cur_ops->stats();
ad0dc7f9
PM
1313 if (rtcv_snap == rcu_torture_current_version &&
1314 rcu_torture_current != NULL) {
7f6733c3
PM
1315 int __maybe_unused flags = 0;
1316 unsigned long __maybe_unused gpnum = 0;
1317 unsigned long __maybe_unused completed = 0;
ad0dc7f9
PM
1318
1319 rcutorture_get_gp_data(cur_ops->ttype,
1320 &flags, &gpnum, &completed);
7f6733c3
PM
1321 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp,
1322 &flags, &gpnum, &completed);
4ffa6699 1323 wtp = READ_ONCE(writer_task);
808de39c 1324 pr_alert("??? Writer stall state %s(%d) g%lu c%lu f%#x ->state %#lx cpu %d\n",
18aff33e 1325 rcu_torture_writer_state_getname(),
eea203fe 1326 rcu_torture_writer_state,
4ffa6699 1327 gpnum, completed, flags,
808de39c
PM
1328 wtp == NULL ? ~0UL : wtp->state,
1329 wtp == NULL ? -1 : (int)task_cpu(wtp));
0032f4e8
PM
1330 if (!splatted && wtp) {
1331 sched_show_task(wtp);
1332 splatted = true;
1333 }
afea227f 1334 show_rcu_gp_kthreads();
274529ba 1335 rcu_ftrace_dump(DUMP_ALL);
ad0dc7f9
PM
1336 }
1337 rtcv_snap = rcu_torture_current_version;
a241ec65
PM
1338}
1339
a241ec65
PM
1340/*
1341 * Periodically prints torture statistics, if periodic statistics printing
1342 * was specified via the stat_interval module parameter.
a241ec65
PM
1343 */
1344static int
1345rcu_torture_stats(void *arg)
1346{
5ccf60f2 1347 VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
a241ec65
PM
1348 do {
1349 schedule_timeout_interruptible(stat_interval * HZ);
1350 rcu_torture_stats_print();
f67a3356 1351 torture_shutdown_absorb("rcu_torture_stats");
36970bb9 1352 } while (!torture_must_stop());
7fafaac5 1353 torture_kthread_stopping("rcu_torture_stats");
d120f65f
PM
1354 return 0;
1355}
1356
95c38322 1357static inline void
e66c33d5 1358rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
95c38322 1359{
2caa1e44
PM
1360 pr_alert("%s" TORTURE_FLAG
1361 "--- %s: nreaders=%d nfakewriters=%d "
1362 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1363 "shuffle_interval=%d stutter=%d irqreader=%d "
1364 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1365 "test_boost=%d/%d test_boost_interval=%d "
1366 "test_boost_duration=%d shutdown_secs=%d "
2b1516e5 1367 "stall_cpu=%d stall_cpu_holdoff=%d stall_cpu_irqsoff=%d "
67afeed2 1368 "n_barrier_cbs=%d "
2caa1e44
PM
1369 "onoff_interval=%d onoff_holdoff=%d\n",
1370 torture_type, tag, nrealreaders, nfakewriters,
1371 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1372 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1373 test_boost, cur_ops->can_boost,
1374 test_boost_interval, test_boost_duration, shutdown_secs,
2b1516e5 1375 stall_cpu, stall_cpu_holdoff, stall_cpu_irqsoff,
67afeed2 1376 n_barrier_cbs,
2caa1e44 1377 onoff_interval, onoff_holdoff);
95c38322
PM
1378}
1379
0ffd374b 1380static int rcutorture_booster_cleanup(unsigned int cpu)
8e8be45e
PM
1381{
1382 struct task_struct *t;
1383
1384 if (boost_tasks[cpu] == NULL)
0ffd374b 1385 return 0;
8e8be45e 1386 mutex_lock(&boost_mutex);
8e8be45e
PM
1387 t = boost_tasks[cpu];
1388 boost_tasks[cpu] = NULL;
1389 mutex_unlock(&boost_mutex);
1390
1391 /* This must be outside of the mutex, otherwise deadlock! */
9c029b86 1392 torture_stop_kthread(rcu_torture_boost, t);
0ffd374b 1393 return 0;
8e8be45e
PM
1394}
1395
0ffd374b 1396static int rcutorture_booster_init(unsigned int cpu)
8e8be45e
PM
1397{
1398 int retval;
1399
1400 if (boost_tasks[cpu] != NULL)
1401 return 0; /* Already created, nothing more to do. */
1402
1403 /* Don't allow time recalculation while creating a new task. */
1404 mutex_lock(&boost_mutex);
5ccf60f2 1405 VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
1f288094
ED
1406 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1407 cpu_to_node(cpu),
1408 "rcu_torture_boost");
8e8be45e
PM
1409 if (IS_ERR(boost_tasks[cpu])) {
1410 retval = PTR_ERR(boost_tasks[cpu]);
5ccf60f2 1411 VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
8e8be45e
PM
1412 n_rcu_torture_boost_ktrerror++;
1413 boost_tasks[cpu] = NULL;
1414 mutex_unlock(&boost_mutex);
1415 return retval;
1416 }
1417 kthread_bind(boost_tasks[cpu], cpu);
1418 wake_up_process(boost_tasks[cpu]);
1419 mutex_unlock(&boost_mutex);
1420 return 0;
1421}
1422
c13f3757
PM
1423/*
1424 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1425 * induces a CPU stall for the time specified by stall_cpu.
1426 */
49fb4c62 1427static int rcu_torture_stall(void *args)
c13f3757
PM
1428{
1429 unsigned long stop_at;
1430
5ccf60f2 1431 VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
c13f3757 1432 if (stall_cpu_holdoff > 0) {
5ccf60f2 1433 VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
c13f3757 1434 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
5ccf60f2 1435 VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
c13f3757
PM
1436 }
1437 if (!kthread_should_stop()) {
1438 stop_at = get_seconds() + stall_cpu;
1439 /* RCU CPU stall is expected behavior in following code. */
c13f3757 1440 rcu_read_lock();
2b1516e5
PM
1441 if (stall_cpu_irqsoff)
1442 local_irq_disable();
1443 else
1444 preempt_disable();
1445 pr_alert("rcu_torture_stall start on CPU %d.\n",
1446 smp_processor_id());
c13f3757
PM
1447 while (ULONG_CMP_LT(get_seconds(), stop_at))
1448 continue; /* Induce RCU CPU stall warning. */
2b1516e5
PM
1449 if (stall_cpu_irqsoff)
1450 local_irq_enable();
1451 else
1452 preempt_enable();
c13f3757 1453 rcu_read_unlock();
2caa1e44 1454 pr_alert("rcu_torture_stall end.\n");
c13f3757 1455 }
f67a3356 1456 torture_shutdown_absorb("rcu_torture_stall");
c13f3757
PM
1457 while (!kthread_should_stop())
1458 schedule_timeout_interruptible(10 * HZ);
1459 return 0;
1460}
1461
1462/* Spawn CPU-stall kthread, if stall_cpu specified. */
1463static int __init rcu_torture_stall_init(void)
1464{
c13f3757
PM
1465 if (stall_cpu <= 0)
1466 return 0;
47cf29b9 1467 return torture_create_kthread(rcu_torture_stall, NULL, stall_task);
c13f3757
PM
1468}
1469
fae4b54f 1470/* Callback function for RCU barrier testing. */
b3b8a4d4 1471static void rcu_torture_barrier_cbf(struct rcu_head *rcu)
fae4b54f
PM
1472{
1473 atomic_inc(&barrier_cbs_invoked);
1474}
1475
1476/* kthread function to register callbacks used to test RCU barriers. */
1477static int rcu_torture_barrier_cbs(void *arg)
1478{
1479 long myid = (long)arg;
c6ebcbb6 1480 bool lastphase = 0;
78e4bc34 1481 bool newphase;
fae4b54f
PM
1482 struct rcu_head rcu;
1483
1484 init_rcu_head_on_stack(&rcu);
5ccf60f2 1485 VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
d277d868 1486 set_user_nice(current, MAX_NICE);
fae4b54f
PM
1487 do {
1488 wait_event(barrier_cbs_wq[myid],
78e4bc34 1489 (newphase =
6c7ed42c 1490 smp_load_acquire(&barrier_phase)) != lastphase ||
36970bb9 1491 torture_must_stop());
78e4bc34 1492 lastphase = newphase;
36970bb9 1493 if (torture_must_stop())
fae4b54f 1494 break;
6c7ed42c
PM
1495 /*
1496 * The above smp_load_acquire() ensures barrier_phase load
aab05738 1497 * is ordered before the following ->call().
6c7ed42c 1498 */
0aa67e75 1499 local_irq_disable(); /* Just to test no-irq call_rcu(). */
fae4b54f 1500 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
0aa67e75 1501 local_irq_enable();
fae4b54f
PM
1502 if (atomic_dec_and_test(&barrier_cbs_count))
1503 wake_up(&barrier_wq);
36970bb9 1504 } while (!torture_must_stop());
69c60455
PM
1505 if (cur_ops->cb_barrier != NULL)
1506 cur_ops->cb_barrier();
fae4b54f 1507 destroy_rcu_head_on_stack(&rcu);
7fafaac5 1508 torture_kthread_stopping("rcu_torture_barrier_cbs");
fae4b54f
PM
1509 return 0;
1510}
1511
1512/* kthread function to drive and coordinate RCU barrier testing. */
1513static int rcu_torture_barrier(void *arg)
1514{
1515 int i;
1516
5ccf60f2 1517 VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
fae4b54f
PM
1518 do {
1519 atomic_set(&barrier_cbs_invoked, 0);
1520 atomic_set(&barrier_cbs_count, n_barrier_cbs);
6c7ed42c
PM
1521 /* Ensure barrier_phase ordered after prior assignments. */
1522 smp_store_release(&barrier_phase, !barrier_phase);
fae4b54f
PM
1523 for (i = 0; i < n_barrier_cbs; i++)
1524 wake_up(&barrier_cbs_wq[i]);
1525 wait_event(barrier_wq,
1526 atomic_read(&barrier_cbs_count) == 0 ||
36970bb9
PM
1527 torture_must_stop());
1528 if (torture_must_stop())
fae4b54f
PM
1529 break;
1530 n_barrier_attempts++;
78e4bc34 1531 cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
fae4b54f
PM
1532 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1533 n_rcu_torture_barrier_error++;
7602de4a
PM
1534 pr_err("barrier_cbs_invoked = %d, n_barrier_cbs = %d\n",
1535 atomic_read(&barrier_cbs_invoked),
1536 n_barrier_cbs);
fae4b54f
PM
1537 WARN_ON_ONCE(1);
1538 }
1539 n_barrier_successes++;
1540 schedule_timeout_interruptible(HZ / 10);
36970bb9 1541 } while (!torture_must_stop());
7fafaac5 1542 torture_kthread_stopping("rcu_torture_barrier");
fae4b54f
PM
1543 return 0;
1544}
1545
1546/* Initialize RCU barrier testing. */
1547static int rcu_torture_barrier_init(void)
1548{
1549 int i;
1550 int ret;
1551
d9eba768 1552 if (n_barrier_cbs <= 0)
fae4b54f
PM
1553 return 0;
1554 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
2caa1e44
PM
1555 pr_alert("%s" TORTURE_FLAG
1556 " Call or barrier ops missing for %s,\n",
1557 torture_type, cur_ops->name);
1558 pr_alert("%s" TORTURE_FLAG
1559 " RCU barrier testing omitted from run.\n",
1560 torture_type);
fae4b54f
PM
1561 return 0;
1562 }
1563 atomic_set(&barrier_cbs_count, 0);
1564 atomic_set(&barrier_cbs_invoked, 0);
1565 barrier_cbs_tasks =
68a675d4 1566 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_tasks[0]),
fae4b54f
PM
1567 GFP_KERNEL);
1568 barrier_cbs_wq =
68a675d4 1569 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_wq[0]), GFP_KERNEL);
de5e6437 1570 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
fae4b54f
PM
1571 return -ENOMEM;
1572 for (i = 0; i < n_barrier_cbs; i++) {
1573 init_waitqueue_head(&barrier_cbs_wq[i]);
47cf29b9
PM
1574 ret = torture_create_kthread(rcu_torture_barrier_cbs,
1575 (void *)(long)i,
1576 barrier_cbs_tasks[i]);
1577 if (ret)
fae4b54f 1578 return ret;
fae4b54f 1579 }
47cf29b9 1580 return torture_create_kthread(rcu_torture_barrier, NULL, barrier_task);
fae4b54f
PM
1581}
1582
1583/* Clean up after RCU barrier testing. */
1584static void rcu_torture_barrier_cleanup(void)
1585{
1586 int i;
1587
9c029b86 1588 torture_stop_kthread(rcu_torture_barrier, barrier_task);
fae4b54f 1589 if (barrier_cbs_tasks != NULL) {
9c029b86
PM
1590 for (i = 0; i < n_barrier_cbs; i++)
1591 torture_stop_kthread(rcu_torture_barrier_cbs,
1592 barrier_cbs_tasks[i]);
fae4b54f
PM
1593 kfree(barrier_cbs_tasks);
1594 barrier_cbs_tasks = NULL;
1595 }
1596 if (barrier_cbs_wq != NULL) {
1597 kfree(barrier_cbs_wq);
1598 barrier_cbs_wq = NULL;
1599 }
1600}
1601
0ffd374b 1602static enum cpuhp_state rcutor_hp;
8e8be45e 1603
a241ec65
PM
1604static void
1605rcu_torture_cleanup(void)
1606{
034777d7
PM
1607 int flags = 0;
1608 unsigned long gpnum = 0;
1609 unsigned long completed = 0;
a241ec65
PM
1610 int i;
1611
4a298656 1612 rcutorture_record_test_transition();
d36a7a0d 1613 if (torture_cleanup_begin()) {
343e9099
PM
1614 if (cur_ops->cb_barrier != NULL)
1615 cur_ops->cb_barrier();
1616 return;
1617 }
d84f5203 1618
fae4b54f 1619 rcu_torture_barrier_cleanup();
9c029b86 1620 torture_stop_kthread(rcu_torture_stall, stall_task);
9c029b86 1621 torture_stop_kthread(rcu_torture_writer, writer_task);
a241ec65 1622
c8e5b163 1623 if (reader_tasks) {
9c029b86
PM
1624 for (i = 0; i < nrealreaders; i++)
1625 torture_stop_kthread(rcu_torture_reader,
1626 reader_tasks[i]);
a241ec65 1627 kfree(reader_tasks);
a241ec65
PM
1628 }
1629 rcu_torture_current = NULL;
1630
c8e5b163 1631 if (fakewriter_tasks) {
b772e1dd 1632 for (i = 0; i < nfakewriters; i++) {
9c029b86
PM
1633 torture_stop_kthread(rcu_torture_fakewriter,
1634 fakewriter_tasks[i]);
b772e1dd
JT
1635 }
1636 kfree(fakewriter_tasks);
1637 fakewriter_tasks = NULL;
1638 }
1639
034777d7
PM
1640 rcutorture_get_gp_data(cur_ops->ttype, &flags, &gpnum, &completed);
1641 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp,
1642 &flags, &gpnum, &completed);
1643 pr_alert("%s: End-test grace-period state: g%lu c%lu f%#x\n",
1644 cur_ops->name, gpnum, completed, flags);
9c029b86
PM
1645 torture_stop_kthread(rcu_torture_stats, stats_task);
1646 torture_stop_kthread(rcu_torture_fqs, fqs_task);
38706bc5
PM
1647 for (i = 0; i < ncbflooders; i++)
1648 torture_stop_kthread(rcu_torture_cbflood, cbflood_task[i]);
8e8be45e 1649 if ((test_boost == 1 && cur_ops->can_boost) ||
0ffd374b
SAS
1650 test_boost == 2)
1651 cpuhp_remove_state(rcutor_hp);
bf66f18e 1652
ca1d51ed
PM
1653 /*
1654 * Wait for all RCU callbacks to fire, then do flavor-specific
1655 * cleanup operations.
1656 */
2326974d
PM
1657 if (cur_ops->cb_barrier != NULL)
1658 cur_ops->cb_barrier();
ca1d51ed
PM
1659 if (cur_ops->cleanup != NULL)
1660 cur_ops->cleanup();
a241ec65 1661
a241ec65 1662 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
72e9bb54 1663
fae4b54f 1664 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
8e8be45e 1665 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
2e9e8081 1666 else if (torture_onoff_failures())
091541bb
PM
1667 rcu_torture_print_module_parms(cur_ops,
1668 "End of test: RCU_HOTPLUG");
95c38322 1669 else
8e8be45e 1670 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
d36a7a0d 1671 torture_cleanup_end();
a241ec65
PM
1672}
1673
d2818df1
PM
1674#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1675static void rcu_torture_leak_cb(struct rcu_head *rhp)
1676{
1677}
1678
1679static void rcu_torture_err_cb(struct rcu_head *rhp)
1680{
1681 /*
1682 * This -might- happen due to race conditions, but is unlikely.
1683 * The scenario that leads to this happening is that the
1684 * first of the pair of duplicate callbacks is queued,
1685 * someone else starts a grace period that includes that
1686 * callback, then the second of the pair must wait for the
1687 * next grace period. Unlikely, but can happen. If it
1688 * does happen, the debug-objects subsystem won't have splatted.
1689 */
e0d31a34 1690 pr_alert("%s: duplicated callback was invoked.\n", KBUILD_MODNAME);
d2818df1
PM
1691}
1692#endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1693
1694/*
1695 * Verify that double-free causes debug-objects to complain, but only
1696 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
1697 * cannot be carried out.
1698 */
1699static void rcu_test_debug_objects(void)
1700{
1701#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1702 struct rcu_head rh1;
1703 struct rcu_head rh2;
1704
1705 init_rcu_head_on_stack(&rh1);
1706 init_rcu_head_on_stack(&rh2);
e0d31a34 1707 pr_alert("%s: WARN: Duplicate call_rcu() test starting.\n", KBUILD_MODNAME);
d2818df1
PM
1708
1709 /* Try to queue the rh2 pair of callbacks for the same grace period. */
1710 preempt_disable(); /* Prevent preemption from interrupting test. */
1711 rcu_read_lock(); /* Make it impossible to finish a grace period. */
1712 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
1713 local_irq_disable(); /* Make it harder to start a new grace period. */
1714 call_rcu(&rh2, rcu_torture_leak_cb);
1715 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
1716 local_irq_enable();
1717 rcu_read_unlock();
1718 preempt_enable();
1719
1720 /* Wait for them all to get done so we can safely return. */
1721 rcu_barrier();
e0d31a34 1722 pr_alert("%s: WARN: Duplicate call_rcu() test complete.\n", KBUILD_MODNAME);
d2818df1
PM
1723 destroy_rcu_head_on_stack(&rh1);
1724 destroy_rcu_head_on_stack(&rh2);
1725#else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
e0d31a34 1726 pr_alert("%s: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n", KBUILD_MODNAME);
d2818df1
PM
1727#endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1728}
1729
6f8bc500 1730static int __init
a241ec65
PM
1731rcu_torture_init(void)
1732{
1733 int i;
1734 int cpu;
1735 int firsterr = 0;
2ec1f2d9 1736 static struct rcu_torture_ops *torture_ops[] = {
ca1d51ed 1737 &rcu_ops, &rcu_bh_ops, &rcu_busted_ops, &srcu_ops, &srcud_ops,
7e42776d 1738 &sched_ops, &tasks_ops,
2ec1f2d9 1739 };
a241ec65 1740
a2f2577d 1741 if (!torture_init_begin(torture_type, verbose))
5228084e 1742 return -EBUSY;
343e9099 1743
a241ec65 1744 /* Process args and tell the world that the torturer is on the job. */
ade5fb81 1745 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
72e9bb54 1746 cur_ops = torture_ops[i];
ade5fb81 1747 if (strcmp(torture_type, cur_ops->name) == 0)
72e9bb54 1748 break;
72e9bb54 1749 }
ade5fb81 1750 if (i == ARRAY_SIZE(torture_ops)) {
2caa1e44
PM
1751 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
1752 torture_type);
1753 pr_alert("rcu-torture types:");
cf886c44 1754 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
2caa1e44
PM
1755 pr_alert(" %s", torture_ops[i]->name);
1756 pr_alert("\n");
889d487a
PM
1757 firsterr = -EINVAL;
1758 goto unwind;
72e9bb54 1759 }
bf66f18e 1760 if (cur_ops->fqs == NULL && fqs_duration != 0) {
2caa1e44 1761 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
bf66f18e
PM
1762 fqs_duration = 0;
1763 }
c8e5b163 1764 if (cur_ops->init)
889d487a 1765 cur_ops->init();
72e9bb54 1766
64e4b43a 1767 if (nreaders >= 0) {
a241ec65 1768 nrealreaders = nreaders;
64e4b43a 1769 } else {
3838cc18 1770 nrealreaders = num_online_cpus() - 2 - nreaders;
64e4b43a
PM
1771 if (nrealreaders <= 0)
1772 nrealreaders = 1;
1773 }
8e8be45e 1774 rcu_torture_print_module_parms(cur_ops, "Start of test");
a241ec65
PM
1775
1776 /* Set up the freelist. */
1777
1778 INIT_LIST_HEAD(&rcu_torture_freelist);
788e770e 1779 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
996417d2 1780 rcu_tortures[i].rtort_mbtest = 0;
a241ec65
PM
1781 list_add_tail(&rcu_tortures[i].rtort_free,
1782 &rcu_torture_freelist);
1783 }
1784
1785 /* Initialize the statistics so that each run gets its own numbers. */
1786
1787 rcu_torture_current = NULL;
1788 rcu_torture_current_version = 0;
1789 atomic_set(&n_rcu_torture_alloc, 0);
1790 atomic_set(&n_rcu_torture_alloc_fail, 0);
1791 atomic_set(&n_rcu_torture_free, 0);
996417d2
PM
1792 atomic_set(&n_rcu_torture_mberror, 0);
1793 atomic_set(&n_rcu_torture_error, 0);
fae4b54f 1794 n_rcu_torture_barrier_error = 0;
8e8be45e
PM
1795 n_rcu_torture_boost_ktrerror = 0;
1796 n_rcu_torture_boost_rterror = 0;
8e8be45e
PM
1797 n_rcu_torture_boost_failure = 0;
1798 n_rcu_torture_boosts = 0;
a241ec65
PM
1799 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1800 atomic_set(&rcu_torture_wcount[i], 0);
0a945022 1801 for_each_possible_cpu(cpu) {
a241ec65
PM
1802 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1803 per_cpu(rcu_torture_count, cpu)[i] = 0;
1804 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1805 }
1806 }
1807
1808 /* Start up the kthreads. */
1809
47cf29b9
PM
1810 firsterr = torture_create_kthread(rcu_torture_writer, NULL,
1811 writer_task);
1812 if (firsterr)
a241ec65 1813 goto unwind;
4444d852 1814 if (nfakewriters > 0) {
68a675d4 1815 fakewriter_tasks = kcalloc(nfakewriters,
4444d852
PM
1816 sizeof(fakewriter_tasks[0]),
1817 GFP_KERNEL);
1818 if (fakewriter_tasks == NULL) {
1819 VERBOSE_TOROUT_ERRSTRING("out of memory");
1820 firsterr = -ENOMEM;
1821 goto unwind;
1822 }
b772e1dd
JT
1823 }
1824 for (i = 0; i < nfakewriters; i++) {
47cf29b9
PM
1825 firsterr = torture_create_kthread(rcu_torture_fakewriter,
1826 NULL, fakewriter_tasks[i]);
1827 if (firsterr)
b772e1dd 1828 goto unwind;
b772e1dd 1829 }
68a675d4 1830 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
a241ec65
PM
1831 GFP_KERNEL);
1832 if (reader_tasks == NULL) {
5ccf60f2 1833 VERBOSE_TOROUT_ERRSTRING("out of memory");
a241ec65
PM
1834 firsterr = -ENOMEM;
1835 goto unwind;
1836 }
1837 for (i = 0; i < nrealreaders; i++) {
47cf29b9
PM
1838 firsterr = torture_create_kthread(rcu_torture_reader, NULL,
1839 reader_tasks[i]);
1840 if (firsterr)
a241ec65 1841 goto unwind;
a241ec65
PM
1842 }
1843 if (stat_interval > 0) {
47cf29b9
PM
1844 firsterr = torture_create_kthread(rcu_torture_stats, NULL,
1845 stats_task);
1846 if (firsterr)
a241ec65 1847 goto unwind;
a241ec65 1848 }
e8e255f7 1849 if (test_no_idle_hz && shuffle_interval > 0) {
3808dc9f
PM
1850 firsterr = torture_shuffle_init(shuffle_interval * HZ);
1851 if (firsterr)
d84f5203 1852 goto unwind;
d84f5203 1853 }
d120f65f
PM
1854 if (stutter < 0)
1855 stutter = 0;
1856 if (stutter) {
628edaa5
PM
1857 firsterr = torture_stutter_init(stutter * HZ);
1858 if (firsterr)
d120f65f 1859 goto unwind;
d120f65f 1860 }
bf66f18e
PM
1861 if (fqs_duration < 0)
1862 fqs_duration = 0;
1863 if (fqs_duration) {
628edaa5 1864 /* Create the fqs thread */
d0d0606e
PM
1865 firsterr = torture_create_kthread(rcu_torture_fqs, NULL,
1866 fqs_task);
47cf29b9 1867 if (firsterr)
bf66f18e 1868 goto unwind;
bf66f18e 1869 }
8e8be45e
PM
1870 if (test_boost_interval < 1)
1871 test_boost_interval = 1;
1872 if (test_boost_duration < 2)
1873 test_boost_duration = 2;
1874 if ((test_boost == 1 && cur_ops->can_boost) ||
1875 test_boost == 2) {
8e8be45e
PM
1876
1877 boost_starttime = jiffies + test_boost_interval * HZ;
0ffd374b
SAS
1878
1879 firsterr = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "RCU_TORTURE",
1880 rcutorture_booster_init,
1881 rcutorture_booster_cleanup);
1882 if (firsterr < 0)
1883 goto unwind;
1884 rcutor_hp = firsterr;
8e8be45e 1885 }
01025ebc
PM
1886 firsterr = torture_shutdown_init(shutdown_secs, rcu_torture_cleanup);
1887 if (firsterr)
37e377d2 1888 goto unwind;
01025ebc
PM
1889 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval * HZ);
1890 if (firsterr)
37e377d2 1891 goto unwind;
01025ebc
PM
1892 firsterr = rcu_torture_stall_init();
1893 if (firsterr)
37e377d2 1894 goto unwind;
01025ebc
PM
1895 firsterr = rcu_torture_barrier_init();
1896 if (firsterr)
fae4b54f 1897 goto unwind;
d2818df1
PM
1898 if (object_debug)
1899 rcu_test_debug_objects();
38706bc5
PM
1900 if (cbflood_n_burst > 0) {
1901 /* Create the cbflood threads */
1902 ncbflooders = (num_online_cpus() + 3) / 4;
1903 cbflood_task = kcalloc(ncbflooders, sizeof(*cbflood_task),
1904 GFP_KERNEL);
1905 if (!cbflood_task) {
1906 VERBOSE_TOROUT_ERRSTRING("out of memory");
1907 firsterr = -ENOMEM;
1908 goto unwind;
1909 }
1910 for (i = 0; i < ncbflooders; i++) {
1911 firsterr = torture_create_kthread(rcu_torture_cbflood,
1912 NULL,
1913 cbflood_task[i]);
1914 if (firsterr)
1915 goto unwind;
1916 }
1917 }
4a298656 1918 rcutorture_record_test_transition();
b5daa8f3 1919 torture_init_end();
a241ec65
PM
1920 return 0;
1921
1922unwind:
b5daa8f3 1923 torture_init_end();
a241ec65
PM
1924 rcu_torture_cleanup();
1925 return firsterr;
1926}
1927
1928module_init(rcu_torture_init);
1929module_exit(rcu_torture_cleanup);