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