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