rcutorture: Abstract torture_param()
[deliverable/linux.git] / kernel / rcu / rcutorture.c
CommitLineData
a241ec65 1/*
29766f1e 2 * Read-Copy Update module-based torture test facility
a241ec65
PM
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
b772e1dd 18 * Copyright (C) IBM Corporation, 2005, 2006
a241ec65
PM
19 *
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
a71fca58 21 * Josh Triplett <josh@freedesktop.org>
a241ec65
PM
22 *
23 * See also: Documentation/RCU/torture.txt
24 */
25#include <linux/types.h>
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/kthread.h>
30#include <linux/err.h>
31#include <linux/spinlock.h>
32#include <linux/smp.h>
33#include <linux/rcupdate.h>
34#include <linux/interrupt.h>
35#include <linux/sched.h>
60063497 36#include <linux/atomic.h>
a241ec65 37#include <linux/bitops.h>
a241ec65
PM
38#include <linux/completion.h>
39#include <linux/moduleparam.h>
40#include <linux/percpu.h>
41#include <linux/notifier.h>
343e9099 42#include <linux/reboot.h>
83144186 43#include <linux/freezer.h>
a241ec65 44#include <linux/cpu.h>
a241ec65 45#include <linux/delay.h>
a241ec65 46#include <linux/stat.h>
b2896d2e 47#include <linux/srcu.h>
1aeb272c 48#include <linux/slab.h>
52494535 49#include <linux/trace_clock.h>
f07767fd 50#include <asm/byteorder.h>
51b1130e 51#include <linux/torture.h>
a241ec65
PM
52
53MODULE_LICENSE("GPL");
5cf05ad7 54MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and Josh Triplett <josh@freedesktop.org>");
a241ec65 55
4102adab
PM
56MODULE_ALIAS("rcutorture");
57#ifdef MODULE_PARAM_PREFIX
58#undef MODULE_PARAM_PREFIX
59#endif
60#define MODULE_PARAM_PREFIX "rcutorture."
61
9e250225
PM
62torture_param(int, fqs_duration, 0,
63 "Duration of fqs bursts (us), 0 to disable");
64torture_param(int, fqs_holdoff, 0, "Holdoff time within fqs bursts (us)");
65torture_param(int, fqs_stutter, 3, "Wait time between fqs bursts (s)");
66torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
67torture_param(bool, gp_normal, false,
68 "Use normal (non-expedited) GP wait primitives");
69torture_param(int, irqreader, 1, "Allow RCU readers from irq handlers");
70torture_param(int, n_barrier_cbs, 0,
71 "# of callbacks/kthreads for barrier testing");
72torture_param(int, nfakewriters, 4, "Number of RCU fake writer threads");
73torture_param(int, nreaders, -1, "Number of RCU reader threads");
74torture_param(int, object_debug, 0,
75 "Enable debug-object double call_rcu() testing");
76torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
77torture_param(int, onoff_interval, 0,
78 "Time between CPU hotplugs (s), 0=disable");
79torture_param(int, shuffle_interval, 3, "Number of seconds between shuffles");
80torture_param(int, shutdown_secs, 0, "Shutdown time (s), <= zero to disable.");
81torture_param(int, stall_cpu, 0, "Stall duration (s), zero to disable.");
82torture_param(int, stall_cpu_holdoff, 10,
83 "Time to wait before starting stall (s).");
84torture_param(int, stat_interval, 60,
85 "Number of seconds between stats printk()s");
86torture_param(int, stutter, 5, "Number of seconds to run/halt test");
87torture_param(int, test_boost, 1, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
88torture_param(int, test_boost_duration, 4,
89 "Duration of each boost test, seconds.");
90torture_param(int, test_boost_interval, 7,
91 "Interval between boost tests, seconds.");
92torture_param(bool, test_no_idle_hz, true,
93 "Test support for tickless idle CPUs");
94torture_param(bool, verbose, false, "Enable verbose debugging printk()s");
95
d10453e9 96static char *torture_type = "rcu";
d6ad6711 97module_param(torture_type, charp, 0444);
d10453e9 98MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, ...)");
72e9bb54
PM
99
100#define TORTURE_FLAG "-torture:"
a241ec65 101#define PRINTK_STRING(s) \
2caa1e44 102 do { pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
a241ec65 103#define VERBOSE_PRINTK_STRING(s) \
2caa1e44 104 do { if (verbose) pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
a241ec65 105#define VERBOSE_PRINTK_ERRSTRING(s) \
2caa1e44 106 do { if (verbose) pr_alert("%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
a241ec65 107
a241ec65
PM
108static int nrealreaders;
109static struct task_struct *writer_task;
b772e1dd 110static struct task_struct **fakewriter_tasks;
a241ec65
PM
111static struct task_struct **reader_tasks;
112static struct task_struct *stats_task;
d84f5203 113static struct task_struct *shuffler_task;
d120f65f 114static struct task_struct *stutter_task;
bf66f18e 115static struct task_struct *fqs_task;
8e8be45e 116static struct task_struct *boost_tasks[NR_CPUS];
d5f546d8 117static struct task_struct *shutdown_task;
b58bdcca
PM
118#ifdef CONFIG_HOTPLUG_CPU
119static struct task_struct *onoff_task;
120#endif /* #ifdef CONFIG_HOTPLUG_CPU */
c13f3757 121static struct task_struct *stall_task;
fae4b54f
PM
122static struct task_struct **barrier_cbs_tasks;
123static struct task_struct *barrier_task;
a241ec65
PM
124
125#define RCU_TORTURE_PIPE_LEN 10
126
127struct rcu_torture {
128 struct rcu_head rtort_rcu;
129 int rtort_pipe_count;
130 struct list_head rtort_free;
996417d2 131 int rtort_mbtest;
a241ec65
PM
132};
133
a241ec65 134static LIST_HEAD(rcu_torture_freelist);
0ddea0ea 135static struct rcu_torture __rcu *rcu_torture_current;
4a298656 136static unsigned long rcu_torture_current_version;
a241ec65
PM
137static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
138static DEFINE_SPINLOCK(rcu_torture_lock);
806274c0
PM
139static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1],
140 rcu_torture_count) = { 0 };
141static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1],
142 rcu_torture_batch) = { 0 };
a241ec65 143static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
b2896d2e
PM
144static atomic_t n_rcu_torture_alloc;
145static atomic_t n_rcu_torture_alloc_fail;
146static atomic_t n_rcu_torture_free;
147static atomic_t n_rcu_torture_mberror;
148static atomic_t n_rcu_torture_error;
fae4b54f 149static long n_rcu_torture_barrier_error;
8e8be45e
PM
150static long n_rcu_torture_boost_ktrerror;
151static long n_rcu_torture_boost_rterror;
8e8be45e
PM
152static long n_rcu_torture_boost_failure;
153static long n_rcu_torture_boosts;
a71fca58 154static long n_rcu_torture_timers;
b58bdcca
PM
155static long n_offline_attempts;
156static long n_offline_successes;
13dbf914
PM
157static unsigned long sum_offline;
158static int min_offline = -1;
159static int max_offline;
b58bdcca
PM
160static long n_online_attempts;
161static long n_online_successes;
13dbf914
PM
162static unsigned long sum_online;
163static int min_online = -1;
164static int max_online;
fae4b54f
PM
165static long n_barrier_attempts;
166static long n_barrier_successes;
e3033736 167static struct list_head rcu_torture_removed;
73d0a4b1 168static cpumask_var_t shuffle_tmp_mask;
a241ec65 169
a71fca58 170static int stutter_pause_test;
d120f65f 171
31a72bce
PM
172#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
173#define RCUTORTURE_RUNNABLE_INIT 1
174#else
175#define RCUTORTURE_RUNNABLE_INIT 0
176#endif
177int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
bb3bf705
PM
178module_param(rcutorture_runnable, int, 0444);
179MODULE_PARM_DESC(rcutorture_runnable, "Start rcutorture at boot");
31a72bce 180
3acf4a9a 181#if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
8e8be45e 182#define rcu_can_boost() 1
3acf4a9a 183#else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
8e8be45e 184#define rcu_can_boost() 0
3acf4a9a 185#endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
8e8be45e 186
e4aa0da3
SR
187#ifdef CONFIG_RCU_TRACE
188static u64 notrace rcu_trace_clock_local(void)
189{
190 u64 ts = trace_clock_local();
191 unsigned long __maybe_unused ts_rem = do_div(ts, NSEC_PER_USEC);
192 return ts;
193}
194#else /* #ifdef CONFIG_RCU_TRACE */
195static u64 notrace rcu_trace_clock_local(void)
196{
197 return 0ULL;
198}
199#endif /* #else #ifdef CONFIG_RCU_TRACE */
200
d5f546d8 201static unsigned long shutdown_time; /* jiffies to system shutdown. */
8e8be45e
PM
202static unsigned long boost_starttime; /* jiffies of next boost test start. */
203DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
204 /* and boost task create/destroy. */
fae4b54f 205static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
c6ebcbb6 206static bool barrier_phase; /* Test phase. */
fae4b54f
PM
207static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
208static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
209static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
8e8be45e 210
c9d557c1
PM
211/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
212
213#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
214#define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
215#define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
216static int fullstop = FULLSTOP_RMMOD;
0ddea0ea
PM
217/*
218 * Protect fullstop transitions and spawning of kthreads.
219 */
220static DEFINE_MUTEX(fullstop_mutex);
343e9099 221
d5f546d8
PM
222/* Forward reference. */
223static void rcu_torture_cleanup(void);
224
343e9099 225/*
c9d557c1 226 * Detect and respond to a system shutdown.
343e9099
PM
227 */
228static int
229rcutorture_shutdown_notify(struct notifier_block *unused1,
230 unsigned long unused2, void *unused3)
231{
c59ab97e 232 mutex_lock(&fullstop_mutex);
c9d557c1 233 if (fullstop == FULLSTOP_DONTSTOP)
c59ab97e 234 fullstop = FULLSTOP_SHUTDOWN;
c9d557c1 235 else
2caa1e44 236 pr_warn(/* but going down anyway, so... */
c9d557c1 237 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
c59ab97e 238 mutex_unlock(&fullstop_mutex);
343e9099
PM
239 return NOTIFY_DONE;
240}
241
c9d557c1
PM
242/*
243 * Absorb kthreads into a kernel function that won't return, so that
244 * they won't ever access module text or data again.
245 */
e66c33d5 246static void rcutorture_shutdown_absorb(const char *title)
c9d557c1
PM
247{
248 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
2caa1e44 249 pr_notice(
c9d557c1
PM
250 "rcutorture thread %s parking due to system shutdown\n",
251 title);
252 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
253 }
254}
255
a241ec65
PM
256/*
257 * Allocate an element from the rcu_tortures pool.
258 */
97a41e26 259static struct rcu_torture *
a241ec65
PM
260rcu_torture_alloc(void)
261{
262 struct list_head *p;
263
adac1665 264 spin_lock_bh(&rcu_torture_lock);
a241ec65
PM
265 if (list_empty(&rcu_torture_freelist)) {
266 atomic_inc(&n_rcu_torture_alloc_fail);
adac1665 267 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
268 return NULL;
269 }
270 atomic_inc(&n_rcu_torture_alloc);
271 p = rcu_torture_freelist.next;
272 list_del_init(p);
adac1665 273 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
274 return container_of(p, struct rcu_torture, rtort_free);
275}
276
277/*
278 * Free an element to the rcu_tortures pool.
279 */
280static void
281rcu_torture_free(struct rcu_torture *p)
282{
283 atomic_inc(&n_rcu_torture_free);
adac1665 284 spin_lock_bh(&rcu_torture_lock);
a241ec65 285 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
adac1665 286 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
287}
288
d120f65f 289static void
e66c33d5 290rcu_stutter_wait(const char *title)
d120f65f 291{
c9d557c1 292 while (stutter_pause_test || !rcutorture_runnable) {
e3d7be27
PM
293 if (rcutorture_runnable)
294 schedule_timeout_interruptible(1);
295 else
3ccf79f4 296 schedule_timeout_interruptible(round_jiffies_relative(HZ));
c9d557c1 297 rcutorture_shutdown_absorb(title);
343e9099 298 }
d120f65f
PM
299}
300
72e9bb54
PM
301/*
302 * Operations vector for selecting different types of tests.
303 */
304
305struct rcu_torture_ops {
306 void (*init)(void);
72e9bb54 307 int (*readlock)(void);
51b1130e 308 void (*read_delay)(struct torture_random_state *rrsp);
72e9bb54
PM
309 void (*readunlock)(int idx);
310 int (*completed)(void);
0acc512c 311 void (*deferred_free)(struct rcu_torture *p);
b772e1dd 312 void (*sync)(void);
2ec1f2d9 313 void (*exp_sync)(void);
fae4b54f 314 void (*call)(struct rcu_head *head, void (*func)(struct rcu_head *rcu));
2326974d 315 void (*cb_barrier)(void);
bf66f18e 316 void (*fqs)(void);
d1008950 317 void (*stats)(char *page);
0acc512c 318 int irq_capable;
8e8be45e 319 int can_boost;
e66c33d5 320 const char *name;
72e9bb54 321};
a71fca58
PM
322
323static struct rcu_torture_ops *cur_ops;
72e9bb54
PM
324
325/*
326 * Definitions for rcu torture testing.
327 */
328
a49a4af7 329static int rcu_torture_read_lock(void) __acquires(RCU)
72e9bb54
PM
330{
331 rcu_read_lock();
332 return 0;
333}
334
51b1130e 335static void rcu_read_delay(struct torture_random_state *rrsp)
b2896d2e 336{
b8d57a76
JT
337 const unsigned long shortdelay_us = 200;
338 const unsigned long longdelay_ms = 50;
b2896d2e 339
b8d57a76
JT
340 /* We want a short delay sometimes to make a reader delay the grace
341 * period, and we want a long delay occasionally to trigger
342 * force_quiescent_state. */
b2896d2e 343
51b1130e 344 if (!(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
b8d57a76 345 mdelay(longdelay_ms);
51b1130e 346 if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
b8d57a76 347 udelay(shortdelay_us);
e546f485 348#ifdef CONFIG_PREEMPT
51b1130e
PM
349 if (!preempt_count() &&
350 !(torture_random(rrsp) % (nrealreaders * 20000)))
e546f485
LJ
351 preempt_schedule(); /* No QS if preempt_disable() in effect */
352#endif
b2896d2e
PM
353}
354
a49a4af7 355static void rcu_torture_read_unlock(int idx) __releases(RCU)
72e9bb54
PM
356{
357 rcu_read_unlock();
358}
359
360static int rcu_torture_completed(void)
361{
362 return rcu_batches_completed();
363}
364
365static void
366rcu_torture_cb(struct rcu_head *p)
367{
368 int i;
369 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
370
c9d557c1 371 if (fullstop != FULLSTOP_DONTSTOP) {
72e9bb54
PM
372 /* Test is ending, just drop callbacks on the floor. */
373 /* The next initialization will pick up the pieces. */
374 return;
375 }
376 i = rp->rtort_pipe_count;
377 if (i > RCU_TORTURE_PIPE_LEN)
378 i = RCU_TORTURE_PIPE_LEN;
379 atomic_inc(&rcu_torture_wcount[i]);
380 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
381 rp->rtort_mbtest = 0;
382 rcu_torture_free(rp);
c701d5d9 383 } else {
0acc512c 384 cur_ops->deferred_free(rp);
c701d5d9 385 }
72e9bb54
PM
386}
387
d9a3da06
PM
388static int rcu_no_completed(void)
389{
390 return 0;
391}
392
72e9bb54
PM
393static void rcu_torture_deferred_free(struct rcu_torture *p)
394{
395 call_rcu(&p->rtort_rcu, rcu_torture_cb);
396}
397
e3033736
JT
398static void rcu_sync_torture_init(void)
399{
400 INIT_LIST_HEAD(&rcu_torture_removed);
401}
402
2ec1f2d9 403static struct rcu_torture_ops rcu_ops = {
0acc512c 404 .init = rcu_sync_torture_init,
0acc512c
PM
405 .readlock = rcu_torture_read_lock,
406 .read_delay = rcu_read_delay,
407 .readunlock = rcu_torture_read_unlock,
408 .completed = rcu_torture_completed,
2ec1f2d9 409 .deferred_free = rcu_torture_deferred_free,
0acc512c 410 .sync = synchronize_rcu,
2ec1f2d9
PM
411 .exp_sync = synchronize_rcu_expedited,
412 .call = call_rcu,
413 .cb_barrier = rcu_barrier,
bf66f18e 414 .fqs = rcu_force_quiescent_state,
d9a3da06
PM
415 .stats = NULL,
416 .irq_capable = 1,
8e8be45e 417 .can_boost = rcu_can_boost(),
2ec1f2d9 418 .name = "rcu"
d9a3da06
PM
419};
420
c32e0660
PM
421/*
422 * Definitions for rcu_bh torture testing.
423 */
424
a49a4af7 425static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
c32e0660
PM
426{
427 rcu_read_lock_bh();
428 return 0;
429}
430
a49a4af7 431static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
c32e0660
PM
432{
433 rcu_read_unlock_bh();
434}
435
436static int rcu_bh_torture_completed(void)
437{
438 return rcu_batches_completed_bh();
439}
440
441static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
442{
443 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
444}
445
446static struct rcu_torture_ops rcu_bh_ops = {
2ec1f2d9 447 .init = rcu_sync_torture_init,
0acc512c
PM
448 .readlock = rcu_bh_torture_read_lock,
449 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
450 .readunlock = rcu_bh_torture_read_unlock,
451 .completed = rcu_bh_torture_completed,
452 .deferred_free = rcu_bh_torture_deferred_free,
bdf2a436 453 .sync = synchronize_rcu_bh,
2ec1f2d9 454 .exp_sync = synchronize_rcu_bh_expedited,
fae4b54f 455 .call = call_rcu_bh,
0acc512c 456 .cb_barrier = rcu_barrier_bh,
bf66f18e 457 .fqs = rcu_bh_force_quiescent_state,
0acc512c
PM
458 .stats = NULL,
459 .irq_capable = 1,
460 .name = "rcu_bh"
c32e0660
PM
461};
462
b2896d2e
PM
463/*
464 * Definitions for srcu torture testing.
465 */
466
cda4dc81 467DEFINE_STATIC_SRCU(srcu_ctl);
b2896d2e 468
012d3ca8 469static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
b2896d2e
PM
470{
471 return srcu_read_lock(&srcu_ctl);
472}
473
51b1130e 474static void srcu_read_delay(struct torture_random_state *rrsp)
b2896d2e
PM
475{
476 long delay;
477 const long uspertick = 1000000 / HZ;
478 const long longdelay = 10;
479
480 /* We want there to be long-running readers, but not all the time. */
481
51b1130e
PM
482 delay = torture_random(rrsp) %
483 (nrealreaders * 2 * longdelay * uspertick);
b2896d2e
PM
484 if (!delay)
485 schedule_timeout_interruptible(longdelay);
e546f485
LJ
486 else
487 rcu_read_delay(rrsp);
b2896d2e
PM
488}
489
012d3ca8 490static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
b2896d2e
PM
491{
492 srcu_read_unlock(&srcu_ctl, idx);
493}
494
495static int srcu_torture_completed(void)
496{
497 return srcu_batches_completed(&srcu_ctl);
498}
499
9059c940
LJ
500static void srcu_torture_deferred_free(struct rcu_torture *rp)
501{
502 call_srcu(&srcu_ctl, &rp->rtort_rcu, rcu_torture_cb);
503}
504
b772e1dd
JT
505static void srcu_torture_synchronize(void)
506{
507 synchronize_srcu(&srcu_ctl);
508}
509
e3f8d378
PM
510static void srcu_torture_call(struct rcu_head *head,
511 void (*func)(struct rcu_head *head))
512{
513 call_srcu(&srcu_ctl, head, func);
514}
515
516static void srcu_torture_barrier(void)
517{
518 srcu_barrier(&srcu_ctl);
519}
520
d1008950 521static void srcu_torture_stats(char *page)
b2896d2e 522{
b2896d2e
PM
523 int cpu;
524 int idx = srcu_ctl.completed & 0x1;
525
d1008950 526 page += sprintf(page, "%s%s per-CPU(idx=%d):",
b2896d2e
PM
527 torture_type, TORTURE_FLAG, idx);
528 for_each_possible_cpu(cpu) {
d1008950 529 page += sprintf(page, " %d(%lu,%lu)", cpu,
b2896d2e
PM
530 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
531 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
532 }
d1008950 533 sprintf(page, "\n");
b2896d2e
PM
534}
535
2ec1f2d9
PM
536static void srcu_torture_synchronize_expedited(void)
537{
538 synchronize_srcu_expedited(&srcu_ctl);
539}
540
b2896d2e 541static struct rcu_torture_ops srcu_ops = {
cda4dc81 542 .init = rcu_sync_torture_init,
0acc512c
PM
543 .readlock = srcu_torture_read_lock,
544 .read_delay = srcu_read_delay,
545 .readunlock = srcu_torture_read_unlock,
546 .completed = srcu_torture_completed,
9059c940 547 .deferred_free = srcu_torture_deferred_free,
0acc512c 548 .sync = srcu_torture_synchronize,
2ec1f2d9 549 .exp_sync = srcu_torture_synchronize_expedited,
e3f8d378
PM
550 .call = srcu_torture_call,
551 .cb_barrier = srcu_torture_barrier,
0acc512c
PM
552 .stats = srcu_torture_stats,
553 .name = "srcu"
b2896d2e
PM
554};
555
4b6c2cca
JT
556/*
557 * Definitions for sched torture testing.
558 */
559
560static int sched_torture_read_lock(void)
561{
562 preempt_disable();
563 return 0;
564}
565
566static void sched_torture_read_unlock(int idx)
567{
568 preempt_enable();
569}
570
2326974d
PM
571static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
572{
573 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
574}
575
4b6c2cca 576static struct rcu_torture_ops sched_ops = {
0acc512c 577 .init = rcu_sync_torture_init,
0acc512c
PM
578 .readlock = sched_torture_read_lock,
579 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
580 .readunlock = sched_torture_read_unlock,
d9a3da06 581 .completed = rcu_no_completed,
0acc512c 582 .deferred_free = rcu_sched_torture_deferred_free,
bdf2a436 583 .sync = synchronize_sched,
2ec1f2d9
PM
584 .exp_sync = synchronize_sched_expedited,
585 .call = call_rcu_sched,
0acc512c 586 .cb_barrier = rcu_barrier_sched,
bf66f18e 587 .fqs = rcu_sched_force_quiescent_state,
0acc512c
PM
588 .stats = NULL,
589 .irq_capable = 1,
590 .name = "sched"
4b6c2cca
JT
591};
592
8e8be45e
PM
593/*
594 * RCU torture priority-boost testing. Runs one real-time thread per
595 * CPU for moderate bursts, repeatedly registering RCU callbacks and
596 * spinning waiting for them to be invoked. If a given callback takes
597 * too long to be invoked, we assume that priority inversion has occurred.
598 */
599
600struct rcu_boost_inflight {
601 struct rcu_head rcu;
602 int inflight;
603};
604
605static void rcu_torture_boost_cb(struct rcu_head *head)
606{
607 struct rcu_boost_inflight *rbip =
608 container_of(head, struct rcu_boost_inflight, rcu);
609
610 smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
611 rbip->inflight = 0;
612}
613
614static int rcu_torture_boost(void *arg)
615{
616 unsigned long call_rcu_time;
617 unsigned long endtime;
618 unsigned long oldstarttime;
619 struct rcu_boost_inflight rbi = { .inflight = 0 };
620 struct sched_param sp;
621
622 VERBOSE_PRINTK_STRING("rcu_torture_boost started");
623
624 /* Set real-time priority. */
625 sp.sched_priority = 1;
626 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
627 VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
628 n_rcu_torture_boost_rterror++;
629 }
630
561190e3 631 init_rcu_head_on_stack(&rbi.rcu);
8e8be45e
PM
632 /* Each pass through the following loop does one boost-test cycle. */
633 do {
634 /* Wait for the next test interval. */
635 oldstarttime = boost_starttime;
93898fb1 636 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
0e11c8e8 637 schedule_timeout_interruptible(oldstarttime - jiffies);
8e8be45e
PM
638 rcu_stutter_wait("rcu_torture_boost");
639 if (kthread_should_stop() ||
640 fullstop != FULLSTOP_DONTSTOP)
641 goto checkwait;
642 }
643
644 /* Do one boost-test interval. */
645 endtime = oldstarttime + test_boost_duration * HZ;
646 call_rcu_time = jiffies;
93898fb1 647 while (ULONG_CMP_LT(jiffies, endtime)) {
8e8be45e
PM
648 /* If we don't have a callback in flight, post one. */
649 if (!rbi.inflight) {
650 smp_mb(); /* RCU core before ->inflight = 1. */
651 rbi.inflight = 1;
652 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
653 if (jiffies - call_rcu_time >
654 test_boost_duration * HZ - HZ / 2) {
655 VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
656 n_rcu_torture_boost_failure++;
657 }
658 call_rcu_time = jiffies;
659 }
660 cond_resched();
661 rcu_stutter_wait("rcu_torture_boost");
662 if (kthread_should_stop() ||
663 fullstop != FULLSTOP_DONTSTOP)
664 goto checkwait;
665 }
666
667 /*
668 * Set the start time of the next test interval.
669 * Yes, this is vulnerable to long delays, but such
670 * delays simply cause a false negative for the next
671 * interval. Besides, we are running at RT priority,
672 * so delays should be relatively rare.
673 */
ab8f11e5
PM
674 while (oldstarttime == boost_starttime &&
675 !kthread_should_stop()) {
8e8be45e
PM
676 if (mutex_trylock(&boost_mutex)) {
677 boost_starttime = jiffies +
678 test_boost_interval * HZ;
679 n_rcu_torture_boosts++;
680 mutex_unlock(&boost_mutex);
681 break;
682 }
683 schedule_timeout_uninterruptible(1);
684 }
685
686 /* Go do the stutter. */
687checkwait: rcu_stutter_wait("rcu_torture_boost");
688 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
689
690 /* Clean up and exit. */
691 VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
692 rcutorture_shutdown_absorb("rcu_torture_boost");
693 while (!kthread_should_stop() || rbi.inflight)
694 schedule_timeout_uninterruptible(1);
695 smp_mb(); /* order accesses to ->inflight before stack-frame death. */
9d68197c 696 destroy_rcu_head_on_stack(&rbi.rcu);
8e8be45e
PM
697 return 0;
698}
699
bf66f18e
PM
700/*
701 * RCU torture force-quiescent-state kthread. Repeatedly induces
702 * bursts of calls to force_quiescent_state(), increasing the probability
703 * of occurrence of some important types of race conditions.
704 */
705static int
706rcu_torture_fqs(void *arg)
707{
708 unsigned long fqs_resume_time;
709 int fqs_burst_remaining;
710
711 VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
712 do {
713 fqs_resume_time = jiffies + fqs_stutter * HZ;
93898fb1
PM
714 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
715 !kthread_should_stop()) {
bf66f18e
PM
716 schedule_timeout_interruptible(1);
717 }
718 fqs_burst_remaining = fqs_duration;
93898fb1
PM
719 while (fqs_burst_remaining > 0 &&
720 !kthread_should_stop()) {
bf66f18e
PM
721 cur_ops->fqs();
722 udelay(fqs_holdoff);
723 fqs_burst_remaining -= fqs_holdoff;
724 }
725 rcu_stutter_wait("rcu_torture_fqs");
726 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
727 VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
728 rcutorture_shutdown_absorb("rcu_torture_fqs");
729 while (!kthread_should_stop())
730 schedule_timeout_uninterruptible(1);
731 return 0;
732}
733
a241ec65
PM
734/*
735 * RCU torture writer kthread. Repeatedly substitutes a new structure
736 * for that pointed to by rcu_torture_current, freeing the old structure
737 * after a series of grace periods (the "pipeline").
738 */
739static int
740rcu_torture_writer(void *arg)
741{
2ec1f2d9 742 bool exp;
a241ec65 743 int i;
a241ec65 744 struct rcu_torture *rp;
2ec1f2d9 745 struct rcu_torture *rp1;
a241ec65 746 struct rcu_torture *old_rp;
51b1130e 747 static DEFINE_TORTURE_RANDOM(rand);
a241ec65
PM
748
749 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
dbdf65b1
IM
750 set_user_nice(current, 19);
751
a241ec65
PM
752 do {
753 schedule_timeout_uninterruptible(1);
a71fca58
PM
754 rp = rcu_torture_alloc();
755 if (rp == NULL)
a241ec65
PM
756 continue;
757 rp->rtort_pipe_count = 0;
51b1130e 758 udelay(torture_random(&rand) & 0x3ff);
0ddea0ea
PM
759 old_rp = rcu_dereference_check(rcu_torture_current,
760 current == writer_task);
996417d2 761 rp->rtort_mbtest = 1;
a241ec65 762 rcu_assign_pointer(rcu_torture_current, rp);
9b2619af 763 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
c8e5b163 764 if (old_rp) {
a241ec65
PM
765 i = old_rp->rtort_pipe_count;
766 if (i > RCU_TORTURE_PIPE_LEN)
767 i = RCU_TORTURE_PIPE_LEN;
768 atomic_inc(&rcu_torture_wcount[i]);
769 old_rp->rtort_pipe_count++;
2ec1f2d9 770 if (gp_normal == gp_exp)
51b1130e 771 exp = !!(torture_random(&rand) & 0x80);
2ec1f2d9
PM
772 else
773 exp = gp_exp;
774 if (!exp) {
775 cur_ops->deferred_free(old_rp);
776 } else {
777 cur_ops->exp_sync();
778 list_add(&old_rp->rtort_free,
779 &rcu_torture_removed);
780 list_for_each_entry_safe(rp, rp1,
781 &rcu_torture_removed,
782 rtort_free) {
783 i = rp->rtort_pipe_count;
784 if (i > RCU_TORTURE_PIPE_LEN)
785 i = RCU_TORTURE_PIPE_LEN;
786 atomic_inc(&rcu_torture_wcount[i]);
787 if (++rp->rtort_pipe_count >=
788 RCU_TORTURE_PIPE_LEN) {
789 rp->rtort_mbtest = 0;
790 list_del(&rp->rtort_free);
791 rcu_torture_free(rp);
792 }
793 }
794 }
a241ec65 795 }
4a298656 796 rcutorture_record_progress(++rcu_torture_current_version);
c9d557c1
PM
797 rcu_stutter_wait("rcu_torture_writer");
798 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
a241ec65 799 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
c9d557c1
PM
800 rcutorture_shutdown_absorb("rcu_torture_writer");
801 while (!kthread_should_stop())
a241ec65
PM
802 schedule_timeout_uninterruptible(1);
803 return 0;
804}
805
b772e1dd
JT
806/*
807 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
808 * delay between calls.
809 */
810static int
811rcu_torture_fakewriter(void *arg)
812{
51b1130e 813 DEFINE_TORTURE_RANDOM(rand);
b772e1dd
JT
814
815 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
816 set_user_nice(current, 19);
817
818 do {
51b1130e
PM
819 schedule_timeout_uninterruptible(1 + torture_random(&rand)%10);
820 udelay(torture_random(&rand) & 0x3ff);
72472a02 821 if (cur_ops->cb_barrier != NULL &&
51b1130e 822 torture_random(&rand) % (nfakewriters * 8) == 0) {
72472a02 823 cur_ops->cb_barrier();
2ec1f2d9 824 } else if (gp_normal == gp_exp) {
51b1130e 825 if (torture_random(&rand) & 0x80)
2ec1f2d9
PM
826 cur_ops->sync();
827 else
828 cur_ops->exp_sync();
829 } else if (gp_normal) {
72472a02 830 cur_ops->sync();
2ec1f2d9
PM
831 } else {
832 cur_ops->exp_sync();
833 }
c9d557c1
PM
834 rcu_stutter_wait("rcu_torture_fakewriter");
835 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
b772e1dd
JT
836
837 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
c9d557c1
PM
838 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
839 while (!kthread_should_stop())
b772e1dd
JT
840 schedule_timeout_uninterruptible(1);
841 return 0;
842}
843
91afaf30
PM
844void rcutorture_trace_dump(void)
845{
846 static atomic_t beenhere = ATOMIC_INIT(0);
847
848 if (atomic_read(&beenhere))
849 return;
850 if (atomic_xchg(&beenhere, 1) != 0)
851 return;
91afaf30
PM
852 ftrace_dump(DUMP_ALL);
853}
854
0729fbf3
PM
855/*
856 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
857 * incrementing the corresponding element of the pipeline array. The
858 * counter in the element should never be greater than 1, otherwise, the
859 * RCU implementation is broken.
860 */
861static void rcu_torture_timer(unsigned long unused)
862{
863 int idx;
864 int completed;
52494535 865 int completed_end;
51b1130e 866 static DEFINE_TORTURE_RANDOM(rand);
0729fbf3
PM
867 static DEFINE_SPINLOCK(rand_lock);
868 struct rcu_torture *p;
869 int pipe_count;
52494535 870 unsigned long long ts;
0729fbf3
PM
871
872 idx = cur_ops->readlock();
873 completed = cur_ops->completed();
e4aa0da3 874 ts = rcu_trace_clock_local();
632ee200 875 p = rcu_dereference_check(rcu_torture_current,
632ee200
PM
876 rcu_read_lock_bh_held() ||
877 rcu_read_lock_sched_held() ||
878 srcu_read_lock_held(&srcu_ctl));
0729fbf3
PM
879 if (p == NULL) {
880 /* Leave because rcu_torture_writer is not yet underway */
881 cur_ops->readunlock(idx);
882 return;
883 }
884 if (p->rtort_mbtest == 0)
885 atomic_inc(&n_rcu_torture_mberror);
886 spin_lock(&rand_lock);
0acc512c 887 cur_ops->read_delay(&rand);
0729fbf3
PM
888 n_rcu_torture_timers++;
889 spin_unlock(&rand_lock);
890 preempt_disable();
891 pipe_count = p->rtort_pipe_count;
892 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
893 /* Should not happen, but... */
894 pipe_count = RCU_TORTURE_PIPE_LEN;
895 }
52494535
PM
896 completed_end = cur_ops->completed();
897 if (pipe_count > 1) {
52494535
PM
898 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
899 completed, completed_end);
91afaf30 900 rcutorture_trace_dump();
52494535 901 }
dd17c8f7 902 __this_cpu_inc(rcu_torture_count[pipe_count]);
52494535 903 completed = completed_end - completed;
0729fbf3
PM
904 if (completed > RCU_TORTURE_PIPE_LEN) {
905 /* Should not happen, but... */
906 completed = RCU_TORTURE_PIPE_LEN;
907 }
dd17c8f7 908 __this_cpu_inc(rcu_torture_batch[completed]);
0729fbf3
PM
909 preempt_enable();
910 cur_ops->readunlock(idx);
911}
912
a241ec65
PM
913/*
914 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
915 * incrementing the corresponding element of the pipeline array. The
916 * counter in the element should never be greater than 1, otherwise, the
917 * RCU implementation is broken.
918 */
919static int
920rcu_torture_reader(void *arg)
921{
922 int completed;
52494535 923 int completed_end;
72e9bb54 924 int idx;
51b1130e 925 DEFINE_TORTURE_RANDOM(rand);
a241ec65
PM
926 struct rcu_torture *p;
927 int pipe_count;
0729fbf3 928 struct timer_list t;
52494535 929 unsigned long long ts;
a241ec65
PM
930
931 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
dbdf65b1 932 set_user_nice(current, 19);
0acc512c 933 if (irqreader && cur_ops->irq_capable)
0729fbf3 934 setup_timer_on_stack(&t, rcu_torture_timer, 0);
dbdf65b1 935
a241ec65 936 do {
0acc512c 937 if (irqreader && cur_ops->irq_capable) {
0729fbf3 938 if (!timer_pending(&t))
6155fec9 939 mod_timer(&t, jiffies + 1);
0729fbf3 940 }
72e9bb54
PM
941 idx = cur_ops->readlock();
942 completed = cur_ops->completed();
e4aa0da3 943 ts = rcu_trace_clock_local();
632ee200 944 p = rcu_dereference_check(rcu_torture_current,
632ee200
PM
945 rcu_read_lock_bh_held() ||
946 rcu_read_lock_sched_held() ||
947 srcu_read_lock_held(&srcu_ctl));
a241ec65
PM
948 if (p == NULL) {
949 /* Wait for rcu_torture_writer to get underway */
72e9bb54 950 cur_ops->readunlock(idx);
a241ec65
PM
951 schedule_timeout_interruptible(HZ);
952 continue;
953 }
996417d2
PM
954 if (p->rtort_mbtest == 0)
955 atomic_inc(&n_rcu_torture_mberror);
0acc512c 956 cur_ops->read_delay(&rand);
a241ec65
PM
957 preempt_disable();
958 pipe_count = p->rtort_pipe_count;
959 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
960 /* Should not happen, but... */
961 pipe_count = RCU_TORTURE_PIPE_LEN;
962 }
52494535
PM
963 completed_end = cur_ops->completed();
964 if (pipe_count > 1) {
52494535
PM
965 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
966 ts, completed, completed_end);
91afaf30 967 rcutorture_trace_dump();
52494535 968 }
dd17c8f7 969 __this_cpu_inc(rcu_torture_count[pipe_count]);
52494535 970 completed = completed_end - completed;
a241ec65
PM
971 if (completed > RCU_TORTURE_PIPE_LEN) {
972 /* Should not happen, but... */
973 completed = RCU_TORTURE_PIPE_LEN;
974 }
dd17c8f7 975 __this_cpu_inc(rcu_torture_batch[completed]);
a241ec65 976 preempt_enable();
72e9bb54 977 cur_ops->readunlock(idx);
a241ec65 978 schedule();
c9d557c1
PM
979 rcu_stutter_wait("rcu_torture_reader");
980 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
a241ec65 981 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
c9d557c1 982 rcutorture_shutdown_absorb("rcu_torture_reader");
0acc512c 983 if (irqreader && cur_ops->irq_capable)
0729fbf3 984 del_timer_sync(&t);
c9d557c1 985 while (!kthread_should_stop())
a241ec65
PM
986 schedule_timeout_uninterruptible(1);
987 return 0;
988}
989
990/*
991 * Create an RCU-torture statistics message in the specified buffer.
992 */
d1008950 993static void
a241ec65
PM
994rcu_torture_printk(char *page)
995{
a241ec65
PM
996 int cpu;
997 int i;
998 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
999 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1000
0a945022 1001 for_each_possible_cpu(cpu) {
a241ec65
PM
1002 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1003 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1004 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1005 }
1006 }
1007 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1008 if (pipesummary[i] != 0)
1009 break;
1010 }
d1008950
CG
1011 page += sprintf(page, "%s%s ", torture_type, TORTURE_FLAG);
1012 page += sprintf(page,
5cf05ad7 1013 "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
a241ec65
PM
1014 rcu_torture_current,
1015 rcu_torture_current_version,
1016 list_empty(&rcu_torture_freelist),
1017 atomic_read(&n_rcu_torture_alloc),
1018 atomic_read(&n_rcu_torture_alloc_fail),
5cf05ad7 1019 atomic_read(&n_rcu_torture_free));
d1008950 1020 page += sprintf(page, "rtmbe: %d rtbke: %ld rtbre: %ld ",
0729fbf3 1021 atomic_read(&n_rcu_torture_mberror),
8e8be45e 1022 n_rcu_torture_boost_ktrerror,
5cf05ad7 1023 n_rcu_torture_boost_rterror);
d1008950 1024 page += sprintf(page, "rtbf: %ld rtb: %ld nt: %ld ",
8e8be45e
PM
1025 n_rcu_torture_boost_failure,
1026 n_rcu_torture_boosts,
5cf05ad7 1027 n_rcu_torture_timers);
d1008950 1028 page += sprintf(page,
13dbf914
PM
1029 "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
1030 n_online_successes, n_online_attempts,
1031 n_offline_successes, n_offline_attempts,
1032 min_online, max_online,
1033 min_offline, max_offline,
1034 sum_online, sum_offline, HZ);
d1008950 1035 page += sprintf(page, "barrier: %ld/%ld:%ld",
fae4b54f
PM
1036 n_barrier_successes,
1037 n_barrier_attempts,
1038 n_rcu_torture_barrier_error);
d1008950 1039 page += sprintf(page, "\n%s%s ", torture_type, TORTURE_FLAG);
8e8be45e 1040 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
fae4b54f 1041 n_rcu_torture_barrier_error != 0 ||
8e8be45e
PM
1042 n_rcu_torture_boost_ktrerror != 0 ||
1043 n_rcu_torture_boost_rterror != 0 ||
fae4b54f
PM
1044 n_rcu_torture_boost_failure != 0 ||
1045 i > 1) {
d1008950 1046 page += sprintf(page, "!!! ");
996417d2 1047 atomic_inc(&n_rcu_torture_error);
5af970a4 1048 WARN_ON_ONCE(1);
996417d2 1049 }
d1008950 1050 page += sprintf(page, "Reader Pipe: ");
a241ec65 1051 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
d1008950
CG
1052 page += sprintf(page, " %ld", pipesummary[i]);
1053 page += sprintf(page, "\n%s%s ", torture_type, TORTURE_FLAG);
1054 page += sprintf(page, "Reader Batch: ");
72e9bb54 1055 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
d1008950
CG
1056 page += sprintf(page, " %ld", batchsummary[i]);
1057 page += sprintf(page, "\n%s%s ", torture_type, TORTURE_FLAG);
1058 page += sprintf(page, "Free-Block Circulation: ");
a241ec65 1059 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
d1008950 1060 page += sprintf(page, " %d",
a241ec65
PM
1061 atomic_read(&rcu_torture_wcount[i]));
1062 }
d1008950 1063 page += sprintf(page, "\n");
c8e5b163 1064 if (cur_ops->stats)
d1008950 1065 cur_ops->stats(page);
a241ec65
PM
1066}
1067
1068/*
1069 * Print torture statistics. Caller must ensure that there is only
1070 * one call to this function at a given time!!! This is normally
1071 * accomplished by relying on the module system to only have one copy
1072 * of the module loaded, and then by giving the rcu_torture_stats
1073 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1074 * thread is not running).
1075 */
1076static void
1077rcu_torture_stats_print(void)
1078{
d1008950
CG
1079 int size = nr_cpu_ids * 200 + 8192;
1080 char *buf;
a241ec65 1081
d1008950
CG
1082 buf = kmalloc(size, GFP_KERNEL);
1083 if (!buf) {
1084 pr_err("rcu-torture: Out of memory, need: %d", size);
1085 return;
1086 }
1087 rcu_torture_printk(buf);
1088 pr_alert("%s", buf);
1089 kfree(buf);
a241ec65
PM
1090}
1091
1092/*
1093 * Periodically prints torture statistics, if periodic statistics printing
1094 * was specified via the stat_interval module parameter.
1095 *
1096 * No need to worry about fullstop here, since this one doesn't reference
1097 * volatile state or register callbacks.
1098 */
1099static int
1100rcu_torture_stats(void *arg)
1101{
1102 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
1103 do {
1104 schedule_timeout_interruptible(stat_interval * HZ);
1105 rcu_torture_stats_print();
c9d557c1
PM
1106 rcutorture_shutdown_absorb("rcu_torture_stats");
1107 } while (!kthread_should_stop());
a241ec65
PM
1108 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1109 return 0;
1110}
1111
d84f5203
SV
1112static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
1113
1114/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1115 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1116 */
b2896d2e 1117static void rcu_torture_shuffle_tasks(void)
d84f5203 1118{
d84f5203
SV
1119 int i;
1120
73d0a4b1 1121 cpumask_setall(shuffle_tmp_mask);
86ef5c9a 1122 get_online_cpus();
d84f5203
SV
1123
1124 /* No point in shuffling if there is only one online CPU (ex: UP) */
c9d557c1
PM
1125 if (num_online_cpus() == 1) {
1126 put_online_cpus();
1127 return;
1128 }
d84f5203
SV
1129
1130 if (rcu_idle_cpu != -1)
73d0a4b1 1131 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
d84f5203 1132
73d0a4b1 1133 set_cpus_allowed_ptr(current, shuffle_tmp_mask);
d84f5203 1134
c8e5b163 1135 if (reader_tasks) {
d84f5203
SV
1136 for (i = 0; i < nrealreaders; i++)
1137 if (reader_tasks[i])
f70316da 1138 set_cpus_allowed_ptr(reader_tasks[i],
73d0a4b1 1139 shuffle_tmp_mask);
d84f5203 1140 }
c8e5b163 1141 if (fakewriter_tasks) {
b772e1dd
JT
1142 for (i = 0; i < nfakewriters; i++)
1143 if (fakewriter_tasks[i])
f70316da 1144 set_cpus_allowed_ptr(fakewriter_tasks[i],
73d0a4b1 1145 shuffle_tmp_mask);
b772e1dd 1146 }
d84f5203 1147 if (writer_task)
73d0a4b1 1148 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
d84f5203 1149 if (stats_task)
73d0a4b1 1150 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
0e11c8e8
PM
1151 if (stutter_task)
1152 set_cpus_allowed_ptr(stutter_task, shuffle_tmp_mask);
1153 if (fqs_task)
1154 set_cpus_allowed_ptr(fqs_task, shuffle_tmp_mask);
1155 if (shutdown_task)
1156 set_cpus_allowed_ptr(shutdown_task, shuffle_tmp_mask);
1157#ifdef CONFIG_HOTPLUG_CPU
1158 if (onoff_task)
1159 set_cpus_allowed_ptr(onoff_task, shuffle_tmp_mask);
1160#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1161 if (stall_task)
1162 set_cpus_allowed_ptr(stall_task, shuffle_tmp_mask);
1163 if (barrier_cbs_tasks)
1164 for (i = 0; i < n_barrier_cbs; i++)
1165 if (barrier_cbs_tasks[i])
1166 set_cpus_allowed_ptr(barrier_cbs_tasks[i],
1167 shuffle_tmp_mask);
1168 if (barrier_task)
1169 set_cpus_allowed_ptr(barrier_task, shuffle_tmp_mask);
d84f5203
SV
1170
1171 if (rcu_idle_cpu == -1)
1172 rcu_idle_cpu = num_online_cpus() - 1;
1173 else
1174 rcu_idle_cpu--;
1175
86ef5c9a 1176 put_online_cpus();
d84f5203
SV
1177}
1178
1179/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1180 * system to become idle at a time and cut off its timer ticks. This is meant
1181 * to test the support for such tickless idle CPU in RCU.
1182 */
1183static int
1184rcu_torture_shuffle(void *arg)
1185{
1186 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1187 do {
1188 schedule_timeout_interruptible(shuffle_interval * HZ);
1189 rcu_torture_shuffle_tasks();
c9d557c1
PM
1190 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1191 } while (!kthread_should_stop());
d84f5203
SV
1192 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1193 return 0;
1194}
1195
d120f65f
PM
1196/* Cause the rcutorture test to "stutter", starting and stopping all
1197 * threads periodically.
1198 */
1199static int
1200rcu_torture_stutter(void *arg)
1201{
1202 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1203 do {
1204 schedule_timeout_interruptible(stutter * HZ);
1205 stutter_pause_test = 1;
c9d557c1 1206 if (!kthread_should_stop())
d120f65f
PM
1207 schedule_timeout_interruptible(stutter * HZ);
1208 stutter_pause_test = 0;
c9d557c1
PM
1209 rcutorture_shutdown_absorb("rcu_torture_stutter");
1210 } while (!kthread_should_stop());
d120f65f
PM
1211 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1212 return 0;
1213}
1214
95c38322 1215static inline void
e66c33d5 1216rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
95c38322 1217{
2caa1e44
PM
1218 pr_alert("%s" TORTURE_FLAG
1219 "--- %s: nreaders=%d nfakewriters=%d "
1220 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1221 "shuffle_interval=%d stutter=%d irqreader=%d "
1222 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1223 "test_boost=%d/%d test_boost_interval=%d "
1224 "test_boost_duration=%d shutdown_secs=%d "
67afeed2
PM
1225 "stall_cpu=%d stall_cpu_holdoff=%d "
1226 "n_barrier_cbs=%d "
2caa1e44
PM
1227 "onoff_interval=%d onoff_holdoff=%d\n",
1228 torture_type, tag, nrealreaders, nfakewriters,
1229 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1230 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1231 test_boost, cur_ops->can_boost,
1232 test_boost_interval, test_boost_duration, shutdown_secs,
67afeed2
PM
1233 stall_cpu, stall_cpu_holdoff,
1234 n_barrier_cbs,
2caa1e44 1235 onoff_interval, onoff_holdoff);
95c38322
PM
1236}
1237
8e8be45e 1238static struct notifier_block rcutorture_shutdown_nb = {
343e9099
PM
1239 .notifier_call = rcutorture_shutdown_notify,
1240};
1241
8e8be45e
PM
1242static void rcutorture_booster_cleanup(int cpu)
1243{
1244 struct task_struct *t;
1245
1246 if (boost_tasks[cpu] == NULL)
1247 return;
1248 mutex_lock(&boost_mutex);
1249 VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
1250 t = boost_tasks[cpu];
1251 boost_tasks[cpu] = NULL;
1252 mutex_unlock(&boost_mutex);
1253
1254 /* This must be outside of the mutex, otherwise deadlock! */
1255 kthread_stop(t);
37e377d2 1256 boost_tasks[cpu] = NULL;
8e8be45e
PM
1257}
1258
1259static int rcutorture_booster_init(int cpu)
1260{
1261 int retval;
1262
1263 if (boost_tasks[cpu] != NULL)
1264 return 0; /* Already created, nothing more to do. */
1265
1266 /* Don't allow time recalculation while creating a new task. */
1267 mutex_lock(&boost_mutex);
1268 VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
1f288094
ED
1269 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1270 cpu_to_node(cpu),
1271 "rcu_torture_boost");
8e8be45e
PM
1272 if (IS_ERR(boost_tasks[cpu])) {
1273 retval = PTR_ERR(boost_tasks[cpu]);
1274 VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
1275 n_rcu_torture_boost_ktrerror++;
1276 boost_tasks[cpu] = NULL;
1277 mutex_unlock(&boost_mutex);
1278 return retval;
1279 }
1280 kthread_bind(boost_tasks[cpu], cpu);
1281 wake_up_process(boost_tasks[cpu]);
1282 mutex_unlock(&boost_mutex);
1283 return 0;
1284}
1285
d5f546d8
PM
1286/*
1287 * Cause the rcutorture test to shutdown the system after the test has
1288 * run for the time specified by the shutdown_secs module parameter.
1289 */
1290static int
1291rcu_torture_shutdown(void *arg)
1292{
1293 long delta;
1294 unsigned long jiffies_snap;
1295
1296 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
1297 jiffies_snap = ACCESS_ONCE(jiffies);
1298 while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
1299 !kthread_should_stop()) {
1300 delta = shutdown_time - jiffies_snap;
1301 if (verbose)
2caa1e44
PM
1302 pr_alert("%s" TORTURE_FLAG
1303 "rcu_torture_shutdown task: %lu jiffies remaining\n",
1304 torture_type, delta);
d5f546d8
PM
1305 schedule_timeout_interruptible(delta);
1306 jiffies_snap = ACCESS_ONCE(jiffies);
1307 }
b58bdcca 1308 if (kthread_should_stop()) {
d5f546d8
PM
1309 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
1310 return 0;
1311 }
1312
1313 /* OK, shut down the system. */
1314
1315 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
1316 shutdown_task = NULL; /* Avoid self-kill deadlock. */
1317 rcu_torture_cleanup(); /* Get the success/failure message. */
1318 kernel_power_off(); /* Shut down the system. */
1319 return 0;
1320}
1321
b58bdcca
PM
1322#ifdef CONFIG_HOTPLUG_CPU
1323
1324/*
1325 * Execute random CPU-hotplug operations at the interval specified
1326 * by the onoff_interval.
1327 */
49fb4c62 1328static int
b58bdcca
PM
1329rcu_torture_onoff(void *arg)
1330{
1331 int cpu;
13dbf914 1332 unsigned long delta;
b58bdcca 1333 int maxcpu = -1;
51b1130e 1334 DEFINE_TORTURE_RANDOM(rand);
48983260 1335 int ret;
13dbf914 1336 unsigned long starttime;
b58bdcca
PM
1337
1338 VERBOSE_PRINTK_STRING("rcu_torture_onoff task started");
1339 for_each_online_cpu(cpu)
1340 maxcpu = cpu;
1341 WARN_ON(maxcpu < 0);
9b9ec9b9
PM
1342 if (onoff_holdoff > 0) {
1343 VERBOSE_PRINTK_STRING("rcu_torture_onoff begin holdoff");
1344 schedule_timeout_interruptible(onoff_holdoff * HZ);
1345 VERBOSE_PRINTK_STRING("rcu_torture_onoff end holdoff");
1346 }
b58bdcca 1347 while (!kthread_should_stop()) {
51b1130e 1348 cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
f220242a 1349 if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
b58bdcca 1350 if (verbose)
2caa1e44
PM
1351 pr_alert("%s" TORTURE_FLAG
1352 "rcu_torture_onoff task: offlining %d\n",
1353 torture_type, cpu);
13dbf914 1354 starttime = jiffies;
b58bdcca 1355 n_offline_attempts++;
48983260
PM
1356 ret = cpu_down(cpu);
1357 if (ret) {
1358 if (verbose)
1359 pr_alert("%s" TORTURE_FLAG
1360 "rcu_torture_onoff task: offline %d failed: errno %d\n",
1361 torture_type, cpu, ret);
1362 } else {
b58bdcca 1363 if (verbose)
2caa1e44
PM
1364 pr_alert("%s" TORTURE_FLAG
1365 "rcu_torture_onoff task: offlined %d\n",
1366 torture_type, cpu);
b58bdcca 1367 n_offline_successes++;
13dbf914
PM
1368 delta = jiffies - starttime;
1369 sum_offline += delta;
1370 if (min_offline < 0) {
1371 min_offline = delta;
1372 max_offline = delta;
1373 }
1374 if (min_offline > delta)
1375 min_offline = delta;
1376 if (max_offline < delta)
1377 max_offline = delta;
b58bdcca 1378 }
f220242a 1379 } else if (cpu_is_hotpluggable(cpu)) {
b58bdcca 1380 if (verbose)
2caa1e44
PM
1381 pr_alert("%s" TORTURE_FLAG
1382 "rcu_torture_onoff task: onlining %d\n",
1383 torture_type, cpu);
13dbf914 1384 starttime = jiffies;
b58bdcca 1385 n_online_attempts++;
7a6a4107
PM
1386 ret = cpu_up(cpu);
1387 if (ret) {
1388 if (verbose)
1389 pr_alert("%s" TORTURE_FLAG
1390 "rcu_torture_onoff task: online %d failed: errno %d\n",
1391 torture_type, cpu, ret);
1392 } else {
b58bdcca 1393 if (verbose)
2caa1e44
PM
1394 pr_alert("%s" TORTURE_FLAG
1395 "rcu_torture_onoff task: onlined %d\n",
1396 torture_type, cpu);
b58bdcca 1397 n_online_successes++;
13dbf914
PM
1398 delta = jiffies - starttime;
1399 sum_online += delta;
1400 if (min_online < 0) {
1401 min_online = delta;
1402 max_online = delta;
1403 }
1404 if (min_online > delta)
1405 min_online = delta;
1406 if (max_online < delta)
1407 max_online = delta;
b58bdcca
PM
1408 }
1409 }
1410 schedule_timeout_interruptible(onoff_interval * HZ);
1411 }
1412 VERBOSE_PRINTK_STRING("rcu_torture_onoff task stopping");
1413 return 0;
1414}
1415
49fb4c62 1416static int
b58bdcca
PM
1417rcu_torture_onoff_init(void)
1418{
3c1b1ce0
JL
1419 int ret;
1420
b58bdcca
PM
1421 if (onoff_interval <= 0)
1422 return 0;
1423 onoff_task = kthread_run(rcu_torture_onoff, NULL, "rcu_torture_onoff");
1424 if (IS_ERR(onoff_task)) {
3c1b1ce0 1425 ret = PTR_ERR(onoff_task);
b58bdcca 1426 onoff_task = NULL;
3c1b1ce0 1427 return ret;
b58bdcca
PM
1428 }
1429 return 0;
1430}
1431
1432static void rcu_torture_onoff_cleanup(void)
1433{
1434 if (onoff_task == NULL)
1435 return;
1436 VERBOSE_PRINTK_STRING("Stopping rcu_torture_onoff task");
1437 kthread_stop(onoff_task);
37e377d2 1438 onoff_task = NULL;
b58bdcca
PM
1439}
1440
1441#else /* #ifdef CONFIG_HOTPLUG_CPU */
1442
37e377d2 1443static int
b58bdcca
PM
1444rcu_torture_onoff_init(void)
1445{
37e377d2 1446 return 0;
b58bdcca
PM
1447}
1448
1449static void rcu_torture_onoff_cleanup(void)
1450{
1451}
1452
1453#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
1454
c13f3757
PM
1455/*
1456 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1457 * induces a CPU stall for the time specified by stall_cpu.
1458 */
49fb4c62 1459static int rcu_torture_stall(void *args)
c13f3757
PM
1460{
1461 unsigned long stop_at;
1462
1463 VERBOSE_PRINTK_STRING("rcu_torture_stall task started");
1464 if (stall_cpu_holdoff > 0) {
1465 VERBOSE_PRINTK_STRING("rcu_torture_stall begin holdoff");
1466 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
1467 VERBOSE_PRINTK_STRING("rcu_torture_stall end holdoff");
1468 }
1469 if (!kthread_should_stop()) {
1470 stop_at = get_seconds() + stall_cpu;
1471 /* RCU CPU stall is expected behavior in following code. */
2caa1e44 1472 pr_alert("rcu_torture_stall start.\n");
c13f3757
PM
1473 rcu_read_lock();
1474 preempt_disable();
1475 while (ULONG_CMP_LT(get_seconds(), stop_at))
1476 continue; /* Induce RCU CPU stall warning. */
1477 preempt_enable();
1478 rcu_read_unlock();
2caa1e44 1479 pr_alert("rcu_torture_stall end.\n");
c13f3757
PM
1480 }
1481 rcutorture_shutdown_absorb("rcu_torture_stall");
1482 while (!kthread_should_stop())
1483 schedule_timeout_interruptible(10 * HZ);
1484 return 0;
1485}
1486
1487/* Spawn CPU-stall kthread, if stall_cpu specified. */
1488static int __init rcu_torture_stall_init(void)
1489{
1490 int ret;
1491
1492 if (stall_cpu <= 0)
1493 return 0;
1494 stall_task = kthread_run(rcu_torture_stall, NULL, "rcu_torture_stall");
1495 if (IS_ERR(stall_task)) {
1496 ret = PTR_ERR(stall_task);
1497 stall_task = NULL;
1498 return ret;
1499 }
1500 return 0;
1501}
1502
1503/* Clean up after the CPU-stall kthread, if one was spawned. */
1504static void rcu_torture_stall_cleanup(void)
1505{
1506 if (stall_task == NULL)
1507 return;
1508 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stall_task.");
1509 kthread_stop(stall_task);
37e377d2 1510 stall_task = NULL;
c13f3757
PM
1511}
1512
fae4b54f
PM
1513/* Callback function for RCU barrier testing. */
1514void rcu_torture_barrier_cbf(struct rcu_head *rcu)
1515{
1516 atomic_inc(&barrier_cbs_invoked);
1517}
1518
1519/* kthread function to register callbacks used to test RCU barriers. */
1520static int rcu_torture_barrier_cbs(void *arg)
1521{
1522 long myid = (long)arg;
c6ebcbb6 1523 bool lastphase = 0;
78e4bc34 1524 bool newphase;
fae4b54f
PM
1525 struct rcu_head rcu;
1526
1527 init_rcu_head_on_stack(&rcu);
1528 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task started");
1529 set_user_nice(current, 19);
1530 do {
1531 wait_event(barrier_cbs_wq[myid],
78e4bc34
PM
1532 (newphase =
1533 ACCESS_ONCE(barrier_phase)) != lastphase ||
fae4b54f
PM
1534 kthread_should_stop() ||
1535 fullstop != FULLSTOP_DONTSTOP);
78e4bc34 1536 lastphase = newphase;
c6ebcbb6 1537 smp_mb(); /* ensure barrier_phase load before ->call(). */
fae4b54f
PM
1538 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1539 break;
1540 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1541 if (atomic_dec_and_test(&barrier_cbs_count))
1542 wake_up(&barrier_wq);
1543 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1544 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task stopping");
1545 rcutorture_shutdown_absorb("rcu_torture_barrier_cbs");
1546 while (!kthread_should_stop())
1547 schedule_timeout_interruptible(1);
1548 cur_ops->cb_barrier();
1549 destroy_rcu_head_on_stack(&rcu);
1550 return 0;
1551}
1552
1553/* kthread function to drive and coordinate RCU barrier testing. */
1554static int rcu_torture_barrier(void *arg)
1555{
1556 int i;
1557
1558 VERBOSE_PRINTK_STRING("rcu_torture_barrier task starting");
1559 do {
1560 atomic_set(&barrier_cbs_invoked, 0);
1561 atomic_set(&barrier_cbs_count, n_barrier_cbs);
c6ebcbb6
PM
1562 smp_mb(); /* Ensure barrier_phase after prior assignments. */
1563 barrier_phase = !barrier_phase;
fae4b54f
PM
1564 for (i = 0; i < n_barrier_cbs; i++)
1565 wake_up(&barrier_cbs_wq[i]);
1566 wait_event(barrier_wq,
1567 atomic_read(&barrier_cbs_count) == 0 ||
1568 kthread_should_stop() ||
1569 fullstop != FULLSTOP_DONTSTOP);
1570 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1571 break;
1572 n_barrier_attempts++;
78e4bc34 1573 cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
fae4b54f
PM
1574 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1575 n_rcu_torture_barrier_error++;
1576 WARN_ON_ONCE(1);
1577 }
1578 n_barrier_successes++;
1579 schedule_timeout_interruptible(HZ / 10);
1580 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1581 VERBOSE_PRINTK_STRING("rcu_torture_barrier task stopping");
143aa672 1582 rcutorture_shutdown_absorb("rcu_torture_barrier");
fae4b54f
PM
1583 while (!kthread_should_stop())
1584 schedule_timeout_interruptible(1);
1585 return 0;
1586}
1587
1588/* Initialize RCU barrier testing. */
1589static int rcu_torture_barrier_init(void)
1590{
1591 int i;
1592 int ret;
1593
1594 if (n_barrier_cbs == 0)
1595 return 0;
1596 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
2caa1e44
PM
1597 pr_alert("%s" TORTURE_FLAG
1598 " Call or barrier ops missing for %s,\n",
1599 torture_type, cur_ops->name);
1600 pr_alert("%s" TORTURE_FLAG
1601 " RCU barrier testing omitted from run.\n",
1602 torture_type);
fae4b54f
PM
1603 return 0;
1604 }
1605 atomic_set(&barrier_cbs_count, 0);
1606 atomic_set(&barrier_cbs_invoked, 0);
1607 barrier_cbs_tasks =
1608 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
1609 GFP_KERNEL);
1610 barrier_cbs_wq =
1611 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
1612 GFP_KERNEL);
de5e6437 1613 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
fae4b54f
PM
1614 return -ENOMEM;
1615 for (i = 0; i < n_barrier_cbs; i++) {
1616 init_waitqueue_head(&barrier_cbs_wq[i]);
1617 barrier_cbs_tasks[i] = kthread_run(rcu_torture_barrier_cbs,
9059c940 1618 (void *)(long)i,
fae4b54f
PM
1619 "rcu_torture_barrier_cbs");
1620 if (IS_ERR(barrier_cbs_tasks[i])) {
1621 ret = PTR_ERR(barrier_cbs_tasks[i]);
1622 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier_cbs");
1623 barrier_cbs_tasks[i] = NULL;
1624 return ret;
1625 }
1626 }
1627 barrier_task = kthread_run(rcu_torture_barrier, NULL,
1628 "rcu_torture_barrier");
1629 if (IS_ERR(barrier_task)) {
1630 ret = PTR_ERR(barrier_task);
1631 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier");
1632 barrier_task = NULL;
1633 }
1634 return 0;
1635}
1636
1637/* Clean up after RCU barrier testing. */
1638static void rcu_torture_barrier_cleanup(void)
1639{
1640 int i;
1641
1642 if (barrier_task != NULL) {
1643 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier task");
1644 kthread_stop(barrier_task);
1645 barrier_task = NULL;
1646 }
1647 if (barrier_cbs_tasks != NULL) {
1648 for (i = 0; i < n_barrier_cbs; i++) {
1649 if (barrier_cbs_tasks[i] != NULL) {
1650 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier_cbs task");
1651 kthread_stop(barrier_cbs_tasks[i]);
1652 barrier_cbs_tasks[i] = NULL;
1653 }
1654 }
1655 kfree(barrier_cbs_tasks);
1656 barrier_cbs_tasks = NULL;
1657 }
1658 if (barrier_cbs_wq != NULL) {
1659 kfree(barrier_cbs_wq);
1660 barrier_cbs_wq = NULL;
1661 }
1662}
1663
8e8be45e
PM
1664static int rcutorture_cpu_notify(struct notifier_block *self,
1665 unsigned long action, void *hcpu)
1666{
1667 long cpu = (long)hcpu;
1668
1669 switch (action) {
1670 case CPU_ONLINE:
1671 case CPU_DOWN_FAILED:
1672 (void)rcutorture_booster_init(cpu);
1673 break;
1674 case CPU_DOWN_PREPARE:
1675 rcutorture_booster_cleanup(cpu);
1676 break;
1677 default:
1678 break;
1679 }
1680 return NOTIFY_OK;
1681}
1682
1683static struct notifier_block rcutorture_cpu_nb = {
1684 .notifier_call = rcutorture_cpu_notify,
1685};
1686
a241ec65
PM
1687static void
1688rcu_torture_cleanup(void)
1689{
1690 int i;
1691
343e9099 1692 mutex_lock(&fullstop_mutex);
4a298656 1693 rcutorture_record_test_transition();
c9d557c1 1694 if (fullstop == FULLSTOP_SHUTDOWN) {
2caa1e44 1695 pr_warn(/* but going down anyway, so... */
c9d557c1 1696 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
343e9099 1697 mutex_unlock(&fullstop_mutex);
c9d557c1 1698 schedule_timeout_uninterruptible(10);
343e9099
PM
1699 if (cur_ops->cb_barrier != NULL)
1700 cur_ops->cb_barrier();
1701 return;
1702 }
c9d557c1 1703 fullstop = FULLSTOP_RMMOD;
343e9099 1704 mutex_unlock(&fullstop_mutex);
8e8be45e 1705 unregister_reboot_notifier(&rcutorture_shutdown_nb);
fae4b54f 1706 rcu_torture_barrier_cleanup();
c13f3757 1707 rcu_torture_stall_cleanup();
d120f65f
PM
1708 if (stutter_task) {
1709 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1710 kthread_stop(stutter_task);
1711 }
1712 stutter_task = NULL;
c8e5b163 1713 if (shuffler_task) {
d84f5203
SV
1714 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1715 kthread_stop(shuffler_task);
73d0a4b1 1716 free_cpumask_var(shuffle_tmp_mask);
d84f5203
SV
1717 }
1718 shuffler_task = NULL;
1719
c8e5b163 1720 if (writer_task) {
a241ec65
PM
1721 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1722 kthread_stop(writer_task);
1723 }
1724 writer_task = NULL;
1725
c8e5b163 1726 if (reader_tasks) {
a241ec65 1727 for (i = 0; i < nrealreaders; i++) {
c8e5b163 1728 if (reader_tasks[i]) {
a241ec65
PM
1729 VERBOSE_PRINTK_STRING(
1730 "Stopping rcu_torture_reader task");
1731 kthread_stop(reader_tasks[i]);
1732 }
1733 reader_tasks[i] = NULL;
1734 }
1735 kfree(reader_tasks);
1736 reader_tasks = NULL;
1737 }
1738 rcu_torture_current = NULL;
1739
c8e5b163 1740 if (fakewriter_tasks) {
b772e1dd 1741 for (i = 0; i < nfakewriters; i++) {
c8e5b163 1742 if (fakewriter_tasks[i]) {
b772e1dd
JT
1743 VERBOSE_PRINTK_STRING(
1744 "Stopping rcu_torture_fakewriter task");
1745 kthread_stop(fakewriter_tasks[i]);
1746 }
1747 fakewriter_tasks[i] = NULL;
1748 }
1749 kfree(fakewriter_tasks);
1750 fakewriter_tasks = NULL;
1751 }
1752
c8e5b163 1753 if (stats_task) {
a241ec65
PM
1754 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1755 kthread_stop(stats_task);
1756 }
1757 stats_task = NULL;
1758
bf66f18e
PM
1759 if (fqs_task) {
1760 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1761 kthread_stop(fqs_task);
1762 }
1763 fqs_task = NULL;
8e8be45e
PM
1764 if ((test_boost == 1 && cur_ops->can_boost) ||
1765 test_boost == 2) {
1766 unregister_cpu_notifier(&rcutorture_cpu_nb);
1767 for_each_possible_cpu(i)
1768 rcutorture_booster_cleanup(i);
1769 }
d5f546d8
PM
1770 if (shutdown_task != NULL) {
1771 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
1772 kthread_stop(shutdown_task);
1773 }
37e377d2 1774 shutdown_task = NULL;
b58bdcca 1775 rcu_torture_onoff_cleanup();
bf66f18e 1776
a241ec65 1777 /* Wait for all RCU callbacks to fire. */
2326974d
PM
1778
1779 if (cur_ops->cb_barrier != NULL)
1780 cur_ops->cb_barrier();
a241ec65 1781
a241ec65 1782 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
72e9bb54 1783
fae4b54f 1784 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
8e8be45e 1785 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
091541bb
PM
1786 else if (n_online_successes != n_online_attempts ||
1787 n_offline_successes != n_offline_attempts)
1788 rcu_torture_print_module_parms(cur_ops,
1789 "End of test: RCU_HOTPLUG");
95c38322 1790 else
8e8be45e 1791 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
a241ec65
PM
1792}
1793
d2818df1
PM
1794#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1795static void rcu_torture_leak_cb(struct rcu_head *rhp)
1796{
1797}
1798
1799static void rcu_torture_err_cb(struct rcu_head *rhp)
1800{
1801 /*
1802 * This -might- happen due to race conditions, but is unlikely.
1803 * The scenario that leads to this happening is that the
1804 * first of the pair of duplicate callbacks is queued,
1805 * someone else starts a grace period that includes that
1806 * callback, then the second of the pair must wait for the
1807 * next grace period. Unlikely, but can happen. If it
1808 * does happen, the debug-objects subsystem won't have splatted.
1809 */
1810 pr_alert("rcutorture: duplicated callback was invoked.\n");
1811}
1812#endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1813
1814/*
1815 * Verify that double-free causes debug-objects to complain, but only
1816 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
1817 * cannot be carried out.
1818 */
1819static void rcu_test_debug_objects(void)
1820{
1821#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1822 struct rcu_head rh1;
1823 struct rcu_head rh2;
1824
1825 init_rcu_head_on_stack(&rh1);
1826 init_rcu_head_on_stack(&rh2);
1827 pr_alert("rcutorture: WARN: Duplicate call_rcu() test starting.\n");
1828
1829 /* Try to queue the rh2 pair of callbacks for the same grace period. */
1830 preempt_disable(); /* Prevent preemption from interrupting test. */
1831 rcu_read_lock(); /* Make it impossible to finish a grace period. */
1832 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
1833 local_irq_disable(); /* Make it harder to start a new grace period. */
1834 call_rcu(&rh2, rcu_torture_leak_cb);
1835 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
1836 local_irq_enable();
1837 rcu_read_unlock();
1838 preempt_enable();
1839
1840 /* Wait for them all to get done so we can safely return. */
1841 rcu_barrier();
1842 pr_alert("rcutorture: WARN: Duplicate call_rcu() test complete.\n");
1843 destroy_rcu_head_on_stack(&rh1);
1844 destroy_rcu_head_on_stack(&rh2);
1845#else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1846 pr_alert("rcutorture: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n");
1847#endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1848}
1849
6f8bc500 1850static int __init
a241ec65
PM
1851rcu_torture_init(void)
1852{
1853 int i;
1854 int cpu;
1855 int firsterr = 0;
fae4b54f 1856 int retval;
2ec1f2d9
PM
1857 static struct rcu_torture_ops *torture_ops[] = {
1858 &rcu_ops, &rcu_bh_ops, &srcu_ops, &sched_ops,
1859 };
a241ec65 1860
343e9099
PM
1861 mutex_lock(&fullstop_mutex);
1862
a241ec65 1863 /* Process args and tell the world that the torturer is on the job. */
ade5fb81 1864 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
72e9bb54 1865 cur_ops = torture_ops[i];
ade5fb81 1866 if (strcmp(torture_type, cur_ops->name) == 0)
72e9bb54 1867 break;
72e9bb54 1868 }
ade5fb81 1869 if (i == ARRAY_SIZE(torture_ops)) {
2caa1e44
PM
1870 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
1871 torture_type);
1872 pr_alert("rcu-torture types:");
cf886c44 1873 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
2caa1e44
PM
1874 pr_alert(" %s", torture_ops[i]->name);
1875 pr_alert("\n");
343e9099 1876 mutex_unlock(&fullstop_mutex);
a71fca58 1877 return -EINVAL;
72e9bb54 1878 }
bf66f18e 1879 if (cur_ops->fqs == NULL && fqs_duration != 0) {
2caa1e44 1880 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
bf66f18e
PM
1881 fqs_duration = 0;
1882 }
c8e5b163 1883 if (cur_ops->init)
72e9bb54
PM
1884 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1885
a241ec65
PM
1886 if (nreaders >= 0)
1887 nrealreaders = nreaders;
1888 else
1889 nrealreaders = 2 * num_online_cpus();
8e8be45e 1890 rcu_torture_print_module_parms(cur_ops, "Start of test");
c9d557c1 1891 fullstop = FULLSTOP_DONTSTOP;
a241ec65
PM
1892
1893 /* Set up the freelist. */
1894
1895 INIT_LIST_HEAD(&rcu_torture_freelist);
788e770e 1896 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
996417d2 1897 rcu_tortures[i].rtort_mbtest = 0;
a241ec65
PM
1898 list_add_tail(&rcu_tortures[i].rtort_free,
1899 &rcu_torture_freelist);
1900 }
1901
1902 /* Initialize the statistics so that each run gets its own numbers. */
1903
1904 rcu_torture_current = NULL;
1905 rcu_torture_current_version = 0;
1906 atomic_set(&n_rcu_torture_alloc, 0);
1907 atomic_set(&n_rcu_torture_alloc_fail, 0);
1908 atomic_set(&n_rcu_torture_free, 0);
996417d2
PM
1909 atomic_set(&n_rcu_torture_mberror, 0);
1910 atomic_set(&n_rcu_torture_error, 0);
fae4b54f 1911 n_rcu_torture_barrier_error = 0;
8e8be45e
PM
1912 n_rcu_torture_boost_ktrerror = 0;
1913 n_rcu_torture_boost_rterror = 0;
8e8be45e
PM
1914 n_rcu_torture_boost_failure = 0;
1915 n_rcu_torture_boosts = 0;
a241ec65
PM
1916 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1917 atomic_set(&rcu_torture_wcount[i], 0);
0a945022 1918 for_each_possible_cpu(cpu) {
a241ec65
PM
1919 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1920 per_cpu(rcu_torture_count, cpu)[i] = 0;
1921 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1922 }
1923 }
1924
1925 /* Start up the kthreads. */
1926
1927 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
60f53782
PM
1928 writer_task = kthread_create(rcu_torture_writer, NULL,
1929 "rcu_torture_writer");
a241ec65
PM
1930 if (IS_ERR(writer_task)) {
1931 firsterr = PTR_ERR(writer_task);
1932 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1933 writer_task = NULL;
1934 goto unwind;
1935 }
60f53782 1936 wake_up_process(writer_task);
b772e1dd 1937 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
a71fca58 1938 GFP_KERNEL);
b772e1dd
JT
1939 if (fakewriter_tasks == NULL) {
1940 VERBOSE_PRINTK_ERRSTRING("out of memory");
1941 firsterr = -ENOMEM;
1942 goto unwind;
1943 }
1944 for (i = 0; i < nfakewriters; i++) {
1945 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1946 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
a71fca58 1947 "rcu_torture_fakewriter");
b772e1dd
JT
1948 if (IS_ERR(fakewriter_tasks[i])) {
1949 firsterr = PTR_ERR(fakewriter_tasks[i]);
1950 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1951 fakewriter_tasks[i] = NULL;
1952 goto unwind;
1953 }
1954 }
2860aaba 1955 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
a241ec65
PM
1956 GFP_KERNEL);
1957 if (reader_tasks == NULL) {
1958 VERBOSE_PRINTK_ERRSTRING("out of memory");
1959 firsterr = -ENOMEM;
1960 goto unwind;
1961 }
1962 for (i = 0; i < nrealreaders; i++) {
1963 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1964 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1965 "rcu_torture_reader");
1966 if (IS_ERR(reader_tasks[i])) {
1967 firsterr = PTR_ERR(reader_tasks[i]);
1968 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1969 reader_tasks[i] = NULL;
1970 goto unwind;
1971 }
1972 }
1973 if (stat_interval > 0) {
1974 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1975 stats_task = kthread_run(rcu_torture_stats, NULL,
1976 "rcu_torture_stats");
1977 if (IS_ERR(stats_task)) {
1978 firsterr = PTR_ERR(stats_task);
1979 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1980 stats_task = NULL;
1981 goto unwind;
1982 }
1983 }
d84f5203
SV
1984 if (test_no_idle_hz) {
1985 rcu_idle_cpu = num_online_cpus() - 1;
73d0a4b1
RR
1986
1987 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
1988 firsterr = -ENOMEM;
1989 VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
1990 goto unwind;
1991 }
1992
d84f5203
SV
1993 /* Create the shuffler thread */
1994 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1995 "rcu_torture_shuffle");
1996 if (IS_ERR(shuffler_task)) {
73d0a4b1 1997 free_cpumask_var(shuffle_tmp_mask);
d84f5203
SV
1998 firsterr = PTR_ERR(shuffler_task);
1999 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
2000 shuffler_task = NULL;
2001 goto unwind;
2002 }
2003 }
d120f65f
PM
2004 if (stutter < 0)
2005 stutter = 0;
2006 if (stutter) {
2007 /* Create the stutter thread */
2008 stutter_task = kthread_run(rcu_torture_stutter, NULL,
2009 "rcu_torture_stutter");
2010 if (IS_ERR(stutter_task)) {
2011 firsterr = PTR_ERR(stutter_task);
2012 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
2013 stutter_task = NULL;
2014 goto unwind;
2015 }
2016 }
bf66f18e
PM
2017 if (fqs_duration < 0)
2018 fqs_duration = 0;
2019 if (fqs_duration) {
2020 /* Create the stutter thread */
2021 fqs_task = kthread_run(rcu_torture_fqs, NULL,
2022 "rcu_torture_fqs");
2023 if (IS_ERR(fqs_task)) {
2024 firsterr = PTR_ERR(fqs_task);
2025 VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
2026 fqs_task = NULL;
2027 goto unwind;
2028 }
2029 }
8e8be45e
PM
2030 if (test_boost_interval < 1)
2031 test_boost_interval = 1;
2032 if (test_boost_duration < 2)
2033 test_boost_duration = 2;
2034 if ((test_boost == 1 && cur_ops->can_boost) ||
2035 test_boost == 2) {
8e8be45e
PM
2036
2037 boost_starttime = jiffies + test_boost_interval * HZ;
2038 register_cpu_notifier(&rcutorture_cpu_nb);
2039 for_each_possible_cpu(i) {
2040 if (cpu_is_offline(i))
2041 continue; /* Heuristic: CPU can go offline. */
2042 retval = rcutorture_booster_init(i);
2043 if (retval < 0) {
2044 firsterr = retval;
2045 goto unwind;
2046 }
2047 }
2048 }
d5f546d8
PM
2049 if (shutdown_secs > 0) {
2050 shutdown_time = jiffies + shutdown_secs * HZ;
60f53782
PM
2051 shutdown_task = kthread_create(rcu_torture_shutdown, NULL,
2052 "rcu_torture_shutdown");
d5f546d8
PM
2053 if (IS_ERR(shutdown_task)) {
2054 firsterr = PTR_ERR(shutdown_task);
2055 VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
2056 shutdown_task = NULL;
2057 goto unwind;
2058 }
60f53782 2059 wake_up_process(shutdown_task);
d5f546d8 2060 }
37e377d2
PM
2061 i = rcu_torture_onoff_init();
2062 if (i != 0) {
2063 firsterr = i;
2064 goto unwind;
2065 }
8e8be45e 2066 register_reboot_notifier(&rcutorture_shutdown_nb);
37e377d2
PM
2067 i = rcu_torture_stall_init();
2068 if (i != 0) {
2069 firsterr = i;
2070 goto unwind;
2071 }
fae4b54f
PM
2072 retval = rcu_torture_barrier_init();
2073 if (retval != 0) {
2074 firsterr = retval;
2075 goto unwind;
2076 }
d2818df1
PM
2077 if (object_debug)
2078 rcu_test_debug_objects();
4a298656 2079 rcutorture_record_test_transition();
343e9099 2080 mutex_unlock(&fullstop_mutex);
a241ec65
PM
2081 return 0;
2082
2083unwind:
343e9099 2084 mutex_unlock(&fullstop_mutex);
a241ec65
PM
2085 rcu_torture_cleanup();
2086 return firsterr;
2087}
2088
2089module_init(rcu_torture_init);
2090module_exit(rcu_torture_cleanup);
This page took 0.652575 seconds and 5 git commands to generate.