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