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