blob: 729710273dcb034cdaa13bb8d27714f15f5fb2d3 [file] [log] [blame]
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001/*
Paul E. McKenney29766f12006-06-27 02:54:02 -07002 * Read-Copy Update module-based torture test facility
Paul E. McKenneya241ec62005-10-30 15:03:12 -08003 *
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 *
Josh Triplettb772e1d2006-10-04 02:17:13 -070018 * Copyright (C) IBM Corporation, 2005, 2006
Paul E. McKenneya241ec62005-10-30 15:03:12 -080019 *
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
Paul E. McKenneya71fca52009-09-18 10:28:19 -070021 * Josh Triplett <josh@freedesktop.org>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080022 *
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>
36#include <asm/atomic.h>
37#include <linux/bitops.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080038#include <linux/completion.h>
39#include <linux/moduleparam.h>
40#include <linux/percpu.h>
41#include <linux/notifier.h>
Paul E. McKenney343e9092008-12-15 16:13:07 -080042#include <linux/reboot.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070043#include <linux/freezer.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080044#include <linux/cpu.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080045#include <linux/delay.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080046#include <linux/stat.h>
Paul E. McKenneyb2896d22006-10-04 02:17:03 -070047#include <linux/srcu.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070048#include <linux/slab.h>
Harvey Harrisonf07767f2008-10-20 10:23:38 -070049#include <asm/byteorder.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080050
51MODULE_LICENSE("GPL");
Josh Triplettb772e1d2006-10-04 02:17:13 -070052MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
Paul E. McKenneya71fca52009-09-18 10:28:19 -070053 "Josh Triplett <josh@freedesktop.org>");
Paul E. McKenneya241ec62005-10-30 15:03:12 -080054
Josh Triplett48022112006-10-03 23:26:16 +020055static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
Josh Triplettb772e1d2006-10-04 02:17:13 -070056static int nfakewriters = 4; /* # fake writer threads */
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080057static int stat_interval; /* Interval between stats, in seconds. */
Paul E. McKenneya241ec62005-10-30 15:03:12 -080058 /* Defaults to "only at end of test". */
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080059static int verbose; /* Print more debug info. */
60static int test_no_idle_hz; /* Test RCU's support for tickless idle CPUs. */
Paul E. McKenneyd120f652008-06-18 05:21:44 -070061static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
62static int stutter = 5; /* Start/stop testing interval (in sec) */
Paul E. McKenney0729fbf2008-06-25 12:24:52 -070063static int irqreader = 1; /* RCU readers from irq (timers). */
Paul E. McKenneybf66f182010-01-04 15:09:10 -080064static int fqs_duration = 0; /* Duration of bursts (us), 0 to disable. */
65static int fqs_holdoff = 0; /* Hold time within burst (us). */
66static int fqs_stutter = 3; /* Wait time between bursts (s). */
Josh Triplett20d2e422006-10-04 02:17:15 -070067static char *torture_type = "rcu"; /* What RCU implementation to torture. */
Paul E. McKenneya241ec62005-10-30 15:03:12 -080068
Josh Triplettd6ad6712007-03-06 01:42:13 -080069module_param(nreaders, int, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080070MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
Josh Triplettd6ad6712007-03-06 01:42:13 -080071module_param(nfakewriters, int, 0444);
Josh Triplettb772e1d2006-10-04 02:17:13 -070072MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
Josh Triplettd6ad6712007-03-06 01:42:13 -080073module_param(stat_interval, int, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080074MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080075module_param(verbose, bool, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080076MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080077module_param(test_no_idle_hz, bool, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080078MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
Josh Triplettd6ad6712007-03-06 01:42:13 -080079module_param(shuffle_interval, int, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080080MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
Paul E. McKenneyd120f652008-06-18 05:21:44 -070081module_param(stutter, int, 0444);
82MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
Paul E. McKenney0729fbf2008-06-25 12:24:52 -070083module_param(irqreader, int, 0444);
84MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
Paul E. McKenneybf66f182010-01-04 15:09:10 -080085module_param(fqs_duration, int, 0444);
86MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
87module_param(fqs_holdoff, int, 0444);
88MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
89module_param(fqs_stutter, int, 0444);
90MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
Josh Triplettd6ad6712007-03-06 01:42:13 -080091module_param(torture_type, charp, 0444);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -070092MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070093
94#define TORTURE_FLAG "-torture:"
Paul E. McKenneya241ec62005-10-30 15:03:12 -080095#define PRINTK_STRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070096 do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -080097#define VERBOSE_PRINTK_STRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070098 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -080099#define VERBOSE_PRINTK_ERRSTRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700100 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800101
102static char printk_buf[4096];
103
104static int nrealreaders;
105static struct task_struct *writer_task;
Josh Triplettb772e1d2006-10-04 02:17:13 -0700106static struct task_struct **fakewriter_tasks;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800107static struct task_struct **reader_tasks;
108static struct task_struct *stats_task;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800109static struct task_struct *shuffler_task;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700110static struct task_struct *stutter_task;
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800111static struct task_struct *fqs_task;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800112
113#define RCU_TORTURE_PIPE_LEN 10
114
115struct rcu_torture {
116 struct rcu_head rtort_rcu;
117 int rtort_pipe_count;
118 struct list_head rtort_free;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800119 int rtort_mbtest;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800120};
121
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800122static LIST_HEAD(rcu_torture_freelist);
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700123static struct rcu_torture *rcu_torture_current;
124static long rcu_torture_current_version;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800125static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
126static DEFINE_SPINLOCK(rcu_torture_lock);
127static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
128 { 0 };
129static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
130 { 0 };
131static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700132static atomic_t n_rcu_torture_alloc;
133static atomic_t n_rcu_torture_alloc_fail;
134static atomic_t n_rcu_torture_free;
135static atomic_t n_rcu_torture_mberror;
136static atomic_t n_rcu_torture_error;
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700137static long n_rcu_torture_timers;
Josh Triplette3033732006-10-04 02:17:14 -0700138static struct list_head rcu_torture_removed;
Rusty Russell73d0a4b2009-03-30 22:05:16 -0600139static cpumask_var_t shuffle_tmp_mask;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800140
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700141static int stutter_pause_test;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700142
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700143#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
144#define RCUTORTURE_RUNNABLE_INIT 1
145#else
146#define RCUTORTURE_RUNNABLE_INIT 0
147#endif
148int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
149
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800150/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
151
152#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
153#define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
154#define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
155static int fullstop = FULLSTOP_RMMOD;
156DEFINE_MUTEX(fullstop_mutex); /* Protect fullstop transitions and spawning */
157 /* of kthreads. */
Paul E. McKenney343e9092008-12-15 16:13:07 -0800158
159/*
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800160 * Detect and respond to a system shutdown.
Paul E. McKenney343e9092008-12-15 16:13:07 -0800161 */
162static int
163rcutorture_shutdown_notify(struct notifier_block *unused1,
164 unsigned long unused2, void *unused3)
165{
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800166 mutex_lock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800167 if (fullstop == FULLSTOP_DONTSTOP)
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800168 fullstop = FULLSTOP_SHUTDOWN;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800169 else
170 printk(KERN_WARNING /* but going down anyway, so... */
171 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800172 mutex_unlock(&fullstop_mutex);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800173 return NOTIFY_DONE;
174}
175
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800176/*
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800177 * Absorb kthreads into a kernel function that won't return, so that
178 * they won't ever access module text or data again.
179 */
180static void rcutorture_shutdown_absorb(char *title)
181{
182 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
183 printk(KERN_NOTICE
184 "rcutorture thread %s parking due to system shutdown\n",
185 title);
186 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
187 }
188}
189
190/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800191 * Allocate an element from the rcu_tortures pool.
192 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800193static struct rcu_torture *
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800194rcu_torture_alloc(void)
195{
196 struct list_head *p;
197
Ingo Molnaradac1662006-01-25 19:50:12 +0100198 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800199 if (list_empty(&rcu_torture_freelist)) {
200 atomic_inc(&n_rcu_torture_alloc_fail);
Ingo Molnaradac1662006-01-25 19:50:12 +0100201 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800202 return NULL;
203 }
204 atomic_inc(&n_rcu_torture_alloc);
205 p = rcu_torture_freelist.next;
206 list_del_init(p);
Ingo Molnaradac1662006-01-25 19:50:12 +0100207 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800208 return container_of(p, struct rcu_torture, rtort_free);
209}
210
211/*
212 * Free an element to the rcu_tortures pool.
213 */
214static void
215rcu_torture_free(struct rcu_torture *p)
216{
217 atomic_inc(&n_rcu_torture_free);
Ingo Molnaradac1662006-01-25 19:50:12 +0100218 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800219 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
Ingo Molnaradac1662006-01-25 19:50:12 +0100220 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800221}
222
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800223struct rcu_random_state {
224 unsigned long rrs_state;
Josh Triplett75cfef32006-10-04 02:17:12 -0700225 long rrs_count;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800226};
227
228#define RCU_RANDOM_MULT 39916801 /* prime */
229#define RCU_RANDOM_ADD 479001701 /* prime */
230#define RCU_RANDOM_REFRESH 10000
231
232#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
233
234/*
235 * Crude but fast random-number generator. Uses a linear congruential
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700236 * generator, with occasional help from cpu_clock().
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800237 */
Josh Triplett75cfef32006-10-04 02:17:12 -0700238static unsigned long
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800239rcu_random(struct rcu_random_state *rrsp)
240{
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800241 if (--rrsp->rrs_count < 0) {
Peter Zijlstrac6763292010-05-25 10:48:51 +0200242 rrsp->rrs_state += (unsigned long)local_clock();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800243 rrsp->rrs_count = RCU_RANDOM_REFRESH;
244 }
245 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
246 return swahw32(rrsp->rrs_state);
247}
248
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700249static void
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800250rcu_stutter_wait(char *title)
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700251{
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800252 while (stutter_pause_test || !rcutorture_runnable) {
Paul E. McKenneye3d7be22008-06-22 13:06:38 -0700253 if (rcutorture_runnable)
254 schedule_timeout_interruptible(1);
255 else
Paul E. McKenney3ccf79f2008-06-22 14:02:55 -0700256 schedule_timeout_interruptible(round_jiffies_relative(HZ));
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800257 rcutorture_shutdown_absorb(title);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800258 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700259}
260
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800261/*
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700262 * Operations vector for selecting different types of tests.
263 */
264
265struct rcu_torture_ops {
266 void (*init)(void);
267 void (*cleanup)(void);
268 int (*readlock)(void);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700269 void (*read_delay)(struct rcu_random_state *rrsp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700270 void (*readunlock)(int idx);
271 int (*completed)(void);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700272 void (*deferred_free)(struct rcu_torture *p);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700273 void (*sync)(void);
Paul E. McKenney23269742008-05-12 21:21:05 +0200274 void (*cb_barrier)(void);
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800275 void (*fqs)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700276 int (*stats)(char *page);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700277 int irq_capable;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700278 char *name;
279};
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700280
281static struct rcu_torture_ops *cur_ops;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700282
283/*
284 * Definitions for rcu torture testing.
285 */
286
Josh Tripletta49a4af2006-09-29 01:59:30 -0700287static int rcu_torture_read_lock(void) __acquires(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700288{
289 rcu_read_lock();
290 return 0;
291}
292
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700293static void rcu_read_delay(struct rcu_random_state *rrsp)
294{
Josh Triplettb8d57a72009-09-08 15:54:35 -0700295 const unsigned long shortdelay_us = 200;
296 const unsigned long longdelay_ms = 50;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700297
Josh Triplettb8d57a72009-09-08 15:54:35 -0700298 /* We want a short delay sometimes to make a reader delay the grace
299 * period, and we want a long delay occasionally to trigger
300 * force_quiescent_state. */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700301
Josh Triplettb8d57a72009-09-08 15:54:35 -0700302 if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
303 mdelay(longdelay_ms);
304 if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
305 udelay(shortdelay_us);
Lai Jiangshane546f482010-06-21 16:57:42 +0800306#ifdef CONFIG_PREEMPT
307 if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
308 preempt_schedule(); /* No QS if preempt_disable() in effect */
309#endif
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700310}
311
Josh Tripletta49a4af2006-09-29 01:59:30 -0700312static void rcu_torture_read_unlock(int idx) __releases(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700313{
314 rcu_read_unlock();
315}
316
317static int rcu_torture_completed(void)
318{
319 return rcu_batches_completed();
320}
321
322static void
323rcu_torture_cb(struct rcu_head *p)
324{
325 int i;
326 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
327
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800328 if (fullstop != FULLSTOP_DONTSTOP) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700329 /* Test is ending, just drop callbacks on the floor. */
330 /* The next initialization will pick up the pieces. */
331 return;
332 }
333 i = rp->rtort_pipe_count;
334 if (i > RCU_TORTURE_PIPE_LEN)
335 i = RCU_TORTURE_PIPE_LEN;
336 atomic_inc(&rcu_torture_wcount[i]);
337 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
338 rp->rtort_mbtest = 0;
339 rcu_torture_free(rp);
340 } else
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700341 cur_ops->deferred_free(rp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700342}
343
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800344static int rcu_no_completed(void)
345{
346 return 0;
347}
348
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700349static void rcu_torture_deferred_free(struct rcu_torture *p)
350{
351 call_rcu(&p->rtort_rcu, rcu_torture_cb);
352}
353
354static struct rcu_torture_ops rcu_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700355 .init = NULL,
356 .cleanup = NULL,
357 .readlock = rcu_torture_read_lock,
358 .read_delay = rcu_read_delay,
359 .readunlock = rcu_torture_read_unlock,
360 .completed = rcu_torture_completed,
361 .deferred_free = rcu_torture_deferred_free,
362 .sync = synchronize_rcu,
363 .cb_barrier = rcu_barrier,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800364 .fqs = rcu_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700365 .stats = NULL,
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700366 .irq_capable = 1,
367 .name = "rcu"
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700368};
369
Josh Triplette3033732006-10-04 02:17:14 -0700370static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
371{
372 int i;
373 struct rcu_torture *rp;
374 struct rcu_torture *rp1;
375
376 cur_ops->sync();
377 list_add(&p->rtort_free, &rcu_torture_removed);
378 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
379 i = rp->rtort_pipe_count;
380 if (i > RCU_TORTURE_PIPE_LEN)
381 i = RCU_TORTURE_PIPE_LEN;
382 atomic_inc(&rcu_torture_wcount[i]);
383 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
384 rp->rtort_mbtest = 0;
385 list_del(&rp->rtort_free);
386 rcu_torture_free(rp);
387 }
388 }
389}
390
391static void rcu_sync_torture_init(void)
392{
393 INIT_LIST_HEAD(&rcu_torture_removed);
394}
395
Josh Triplett20d2e422006-10-04 02:17:15 -0700396static struct rcu_torture_ops rcu_sync_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700397 .init = rcu_sync_torture_init,
398 .cleanup = NULL,
399 .readlock = rcu_torture_read_lock,
400 .read_delay = rcu_read_delay,
401 .readunlock = rcu_torture_read_unlock,
402 .completed = rcu_torture_completed,
403 .deferred_free = rcu_sync_torture_deferred_free,
404 .sync = synchronize_rcu,
405 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800406 .fqs = rcu_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700407 .stats = NULL,
408 .irq_capable = 1,
409 .name = "rcu_sync"
Josh Triplett20d2e422006-10-04 02:17:15 -0700410};
411
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800412static struct rcu_torture_ops rcu_expedited_ops = {
413 .init = rcu_sync_torture_init,
414 .cleanup = NULL,
415 .readlock = rcu_torture_read_lock,
416 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
417 .readunlock = rcu_torture_read_unlock,
418 .completed = rcu_no_completed,
419 .deferred_free = rcu_sync_torture_deferred_free,
420 .sync = synchronize_rcu_expedited,
421 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800422 .fqs = rcu_force_quiescent_state,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800423 .stats = NULL,
424 .irq_capable = 1,
425 .name = "rcu_expedited"
426};
427
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700428/*
429 * Definitions for rcu_bh torture testing.
430 */
431
Josh Tripletta49a4af2006-09-29 01:59:30 -0700432static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700433{
434 rcu_read_lock_bh();
435 return 0;
436}
437
Josh Tripletta49a4af2006-09-29 01:59:30 -0700438static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700439{
440 rcu_read_unlock_bh();
441}
442
443static int rcu_bh_torture_completed(void)
444{
445 return rcu_batches_completed_bh();
446}
447
448static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
449{
450 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
451}
452
Josh Triplettb772e1d2006-10-04 02:17:13 -0700453struct rcu_bh_torture_synchronize {
454 struct rcu_head head;
455 struct completion completion;
456};
457
458static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
459{
460 struct rcu_bh_torture_synchronize *rcu;
461
462 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
463 complete(&rcu->completion);
464}
465
466static void rcu_bh_torture_synchronize(void)
467{
468 struct rcu_bh_torture_synchronize rcu;
469
Paul E. McKenney72d5a9f2010-05-10 17:12:17 -0700470 init_rcu_head_on_stack(&rcu.head);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700471 init_completion(&rcu.completion);
472 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
473 wait_for_completion(&rcu.completion);
Paul E. McKenney72d5a9f2010-05-10 17:12:17 -0700474 destroy_rcu_head_on_stack(&rcu.head);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700475}
476
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700477static struct rcu_torture_ops rcu_bh_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700478 .init = NULL,
479 .cleanup = NULL,
480 .readlock = rcu_bh_torture_read_lock,
481 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
482 .readunlock = rcu_bh_torture_read_unlock,
483 .completed = rcu_bh_torture_completed,
484 .deferred_free = rcu_bh_torture_deferred_free,
485 .sync = rcu_bh_torture_synchronize,
486 .cb_barrier = rcu_barrier_bh,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800487 .fqs = rcu_bh_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700488 .stats = NULL,
489 .irq_capable = 1,
490 .name = "rcu_bh"
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700491};
492
Josh Triplett11a14702006-10-04 02:17:16 -0700493static struct rcu_torture_ops rcu_bh_sync_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700494 .init = rcu_sync_torture_init,
495 .cleanup = NULL,
496 .readlock = rcu_bh_torture_read_lock,
497 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
498 .readunlock = rcu_bh_torture_read_unlock,
499 .completed = rcu_bh_torture_completed,
500 .deferred_free = rcu_sync_torture_deferred_free,
501 .sync = rcu_bh_torture_synchronize,
502 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800503 .fqs = rcu_bh_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700504 .stats = NULL,
505 .irq_capable = 1,
506 .name = "rcu_bh_sync"
Josh Triplett11a14702006-10-04 02:17:16 -0700507};
508
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700509/*
510 * Definitions for srcu torture testing.
511 */
512
513static struct srcu_struct srcu_ctl;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700514
515static void srcu_torture_init(void)
516{
517 init_srcu_struct(&srcu_ctl);
Josh Triplette3033732006-10-04 02:17:14 -0700518 rcu_sync_torture_init();
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700519}
520
521static void srcu_torture_cleanup(void)
522{
523 synchronize_srcu(&srcu_ctl);
524 cleanup_srcu_struct(&srcu_ctl);
525}
526
Josh Triplett012d3ca2006-12-06 20:40:19 -0800527static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700528{
529 return srcu_read_lock(&srcu_ctl);
530}
531
532static void srcu_read_delay(struct rcu_random_state *rrsp)
533{
534 long delay;
535 const long uspertick = 1000000 / HZ;
536 const long longdelay = 10;
537
538 /* We want there to be long-running readers, but not all the time. */
539
540 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
541 if (!delay)
542 schedule_timeout_interruptible(longdelay);
Lai Jiangshane546f482010-06-21 16:57:42 +0800543 else
544 rcu_read_delay(rrsp);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700545}
546
Josh Triplett012d3ca2006-12-06 20:40:19 -0800547static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700548{
549 srcu_read_unlock(&srcu_ctl, idx);
550}
551
552static int srcu_torture_completed(void)
553{
554 return srcu_batches_completed(&srcu_ctl);
555}
556
Josh Triplettb772e1d2006-10-04 02:17:13 -0700557static void srcu_torture_synchronize(void)
558{
559 synchronize_srcu(&srcu_ctl);
560}
561
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700562static int srcu_torture_stats(char *page)
563{
564 int cnt = 0;
565 int cpu;
566 int idx = srcu_ctl.completed & 0x1;
567
568 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
569 torture_type, TORTURE_FLAG, idx);
570 for_each_possible_cpu(cpu) {
571 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
572 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
573 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
574 }
575 cnt += sprintf(&page[cnt], "\n");
576 return cnt;
577}
578
579static struct rcu_torture_ops srcu_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700580 .init = srcu_torture_init,
581 .cleanup = srcu_torture_cleanup,
582 .readlock = srcu_torture_read_lock,
583 .read_delay = srcu_read_delay,
584 .readunlock = srcu_torture_read_unlock,
585 .completed = srcu_torture_completed,
586 .deferred_free = rcu_sync_torture_deferred_free,
587 .sync = srcu_torture_synchronize,
588 .cb_barrier = NULL,
589 .stats = srcu_torture_stats,
590 .name = "srcu"
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700591};
592
Paul E. McKenney804bb832009-10-25 19:03:52 -0700593static void srcu_torture_synchronize_expedited(void)
594{
595 synchronize_srcu_expedited(&srcu_ctl);
596}
597
598static struct rcu_torture_ops srcu_expedited_ops = {
599 .init = srcu_torture_init,
600 .cleanup = srcu_torture_cleanup,
601 .readlock = srcu_torture_read_lock,
602 .read_delay = srcu_read_delay,
603 .readunlock = srcu_torture_read_unlock,
604 .completed = srcu_torture_completed,
605 .deferred_free = rcu_sync_torture_deferred_free,
606 .sync = srcu_torture_synchronize_expedited,
607 .cb_barrier = NULL,
608 .stats = srcu_torture_stats,
609 .name = "srcu_expedited"
610};
611
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700612/*
613 * Definitions for sched torture testing.
614 */
615
616static int sched_torture_read_lock(void)
617{
618 preempt_disable();
619 return 0;
620}
621
622static void sched_torture_read_unlock(int idx)
623{
624 preempt_enable();
625}
626
Paul E. McKenney23269742008-05-12 21:21:05 +0200627static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
628{
629 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
630}
631
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700632static void sched_torture_synchronize(void)
633{
634 synchronize_sched();
635}
636
637static struct rcu_torture_ops sched_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700638 .init = rcu_sync_torture_init,
639 .cleanup = NULL,
640 .readlock = sched_torture_read_lock,
641 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
642 .readunlock = sched_torture_read_unlock,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800643 .completed = rcu_no_completed,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700644 .deferred_free = rcu_sched_torture_deferred_free,
645 .sync = sched_torture_synchronize,
646 .cb_barrier = rcu_barrier_sched,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800647 .fqs = rcu_sched_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700648 .stats = NULL,
649 .irq_capable = 1,
650 .name = "sched"
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700651};
652
Paul E. McKenney804bb832009-10-25 19:03:52 -0700653static struct rcu_torture_ops sched_sync_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700654 .init = rcu_sync_torture_init,
655 .cleanup = NULL,
656 .readlock = sched_torture_read_lock,
657 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
658 .readunlock = sched_torture_read_unlock,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800659 .completed = rcu_no_completed,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700660 .deferred_free = rcu_sync_torture_deferred_free,
661 .sync = sched_torture_synchronize,
662 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800663 .fqs = rcu_sched_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700664 .stats = NULL,
665 .name = "sched_sync"
666};
667
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700668static struct rcu_torture_ops sched_expedited_ops = {
669 .init = rcu_sync_torture_init,
670 .cleanup = NULL,
671 .readlock = sched_torture_read_lock,
672 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
673 .readunlock = sched_torture_read_unlock,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800674 .completed = rcu_no_completed,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700675 .deferred_free = rcu_sync_torture_deferred_free,
676 .sync = synchronize_sched_expedited,
677 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800678 .fqs = rcu_sched_force_quiescent_state,
Tejun Heo969c7922010-05-06 18:49:21 +0200679 .stats = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700680 .irq_capable = 1,
681 .name = "sched_expedited"
Paul E. McKenney23269742008-05-12 21:21:05 +0200682};
683
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700684/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800685 * RCU torture force-quiescent-state kthread. Repeatedly induces
686 * bursts of calls to force_quiescent_state(), increasing the probability
687 * of occurrence of some important types of race conditions.
688 */
689static int
690rcu_torture_fqs(void *arg)
691{
692 unsigned long fqs_resume_time;
693 int fqs_burst_remaining;
694
695 VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
696 do {
697 fqs_resume_time = jiffies + fqs_stutter * HZ;
698 while (jiffies - fqs_resume_time > LONG_MAX) {
699 schedule_timeout_interruptible(1);
700 }
701 fqs_burst_remaining = fqs_duration;
702 while (fqs_burst_remaining > 0) {
703 cur_ops->fqs();
704 udelay(fqs_holdoff);
705 fqs_burst_remaining -= fqs_holdoff;
706 }
707 rcu_stutter_wait("rcu_torture_fqs");
708 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
709 VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
710 rcutorture_shutdown_absorb("rcu_torture_fqs");
711 while (!kthread_should_stop())
712 schedule_timeout_uninterruptible(1);
713 return 0;
714}
715
716/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800717 * RCU torture writer kthread. Repeatedly substitutes a new structure
718 * for that pointed to by rcu_torture_current, freeing the old structure
719 * after a series of grace periods (the "pipeline").
720 */
721static int
722rcu_torture_writer(void *arg)
723{
724 int i;
725 long oldbatch = rcu_batches_completed();
726 struct rcu_torture *rp;
727 struct rcu_torture *old_rp;
728 static DEFINE_RCU_RANDOM(rand);
729
730 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800731 set_user_nice(current, 19);
732
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800733 do {
734 schedule_timeout_uninterruptible(1);
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700735 rp = rcu_torture_alloc();
736 if (rp == NULL)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800737 continue;
738 rp->rtort_pipe_count = 0;
739 udelay(rcu_random(&rand) & 0x3ff);
740 old_rp = rcu_torture_current;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800741 rp->rtort_mbtest = 1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800742 rcu_assign_pointer(rcu_torture_current, rp);
Paul E. McKenney9b2619a2009-09-23 09:50:43 -0700743 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
Josh Triplettc8e5b162007-05-08 00:33:20 -0700744 if (old_rp) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800745 i = old_rp->rtort_pipe_count;
746 if (i > RCU_TORTURE_PIPE_LEN)
747 i = RCU_TORTURE_PIPE_LEN;
748 atomic_inc(&rcu_torture_wcount[i]);
749 old_rp->rtort_pipe_count++;
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700750 cur_ops->deferred_free(old_rp);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800751 }
752 rcu_torture_current_version++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700753 oldbatch = cur_ops->completed();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800754 rcu_stutter_wait("rcu_torture_writer");
755 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800756 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800757 rcutorture_shutdown_absorb("rcu_torture_writer");
758 while (!kthread_should_stop())
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800759 schedule_timeout_uninterruptible(1);
760 return 0;
761}
762
763/*
Josh Triplettb772e1d2006-10-04 02:17:13 -0700764 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
765 * delay between calls.
766 */
767static int
768rcu_torture_fakewriter(void *arg)
769{
770 DEFINE_RCU_RANDOM(rand);
771
772 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
773 set_user_nice(current, 19);
774
775 do {
776 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
777 udelay(rcu_random(&rand) & 0x3ff);
778 cur_ops->sync();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800779 rcu_stutter_wait("rcu_torture_fakewriter");
780 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700781
782 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800783 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
784 while (!kthread_should_stop())
Josh Triplettb772e1d2006-10-04 02:17:13 -0700785 schedule_timeout_uninterruptible(1);
786 return 0;
787}
788
789/*
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700790 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
791 * incrementing the corresponding element of the pipeline array. The
792 * counter in the element should never be greater than 1, otherwise, the
793 * RCU implementation is broken.
794 */
795static void rcu_torture_timer(unsigned long unused)
796{
797 int idx;
798 int completed;
799 static DEFINE_RCU_RANDOM(rand);
800 static DEFINE_SPINLOCK(rand_lock);
801 struct rcu_torture *p;
802 int pipe_count;
803
804 idx = cur_ops->readlock();
805 completed = cur_ops->completed();
Paul E. McKenney632ee202010-02-22 17:04:45 -0800806 p = rcu_dereference_check(rcu_torture_current,
807 rcu_read_lock_held() ||
808 rcu_read_lock_bh_held() ||
809 rcu_read_lock_sched_held() ||
810 srcu_read_lock_held(&srcu_ctl));
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700811 if (p == NULL) {
812 /* Leave because rcu_torture_writer is not yet underway */
813 cur_ops->readunlock(idx);
814 return;
815 }
816 if (p->rtort_mbtest == 0)
817 atomic_inc(&n_rcu_torture_mberror);
818 spin_lock(&rand_lock);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700819 cur_ops->read_delay(&rand);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700820 n_rcu_torture_timers++;
821 spin_unlock(&rand_lock);
822 preempt_disable();
823 pipe_count = p->rtort_pipe_count;
824 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
825 /* Should not happen, but... */
826 pipe_count = RCU_TORTURE_PIPE_LEN;
827 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900828 __this_cpu_inc(rcu_torture_count[pipe_count]);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700829 completed = cur_ops->completed() - completed;
830 if (completed > RCU_TORTURE_PIPE_LEN) {
831 /* Should not happen, but... */
832 completed = RCU_TORTURE_PIPE_LEN;
833 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900834 __this_cpu_inc(rcu_torture_batch[completed]);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700835 preempt_enable();
836 cur_ops->readunlock(idx);
837}
838
839/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800840 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
841 * incrementing the corresponding element of the pipeline array. The
842 * counter in the element should never be greater than 1, otherwise, the
843 * RCU implementation is broken.
844 */
845static int
846rcu_torture_reader(void *arg)
847{
848 int completed;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700849 int idx;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800850 DEFINE_RCU_RANDOM(rand);
851 struct rcu_torture *p;
852 int pipe_count;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700853 struct timer_list t;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800854
855 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800856 set_user_nice(current, 19);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700857 if (irqreader && cur_ops->irq_capable)
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700858 setup_timer_on_stack(&t, rcu_torture_timer, 0);
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800859
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800860 do {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700861 if (irqreader && cur_ops->irq_capable) {
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700862 if (!timer_pending(&t))
Paul E. McKenney6155fec2010-02-22 17:05:04 -0800863 mod_timer(&t, jiffies + 1);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700864 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700865 idx = cur_ops->readlock();
866 completed = cur_ops->completed();
Paul E. McKenney632ee202010-02-22 17:04:45 -0800867 p = rcu_dereference_check(rcu_torture_current,
868 rcu_read_lock_held() ||
869 rcu_read_lock_bh_held() ||
870 rcu_read_lock_sched_held() ||
871 srcu_read_lock_held(&srcu_ctl));
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800872 if (p == NULL) {
873 /* Wait for rcu_torture_writer to get underway */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700874 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800875 schedule_timeout_interruptible(HZ);
876 continue;
877 }
Paul E. McKenney996417d2005-11-18 01:10:50 -0800878 if (p->rtort_mbtest == 0)
879 atomic_inc(&n_rcu_torture_mberror);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700880 cur_ops->read_delay(&rand);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800881 preempt_disable();
882 pipe_count = p->rtort_pipe_count;
883 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
884 /* Should not happen, but... */
885 pipe_count = RCU_TORTURE_PIPE_LEN;
886 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900887 __this_cpu_inc(rcu_torture_count[pipe_count]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700888 completed = cur_ops->completed() - completed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800889 if (completed > RCU_TORTURE_PIPE_LEN) {
890 /* Should not happen, but... */
891 completed = RCU_TORTURE_PIPE_LEN;
892 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900893 __this_cpu_inc(rcu_torture_batch[completed]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800894 preempt_enable();
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700895 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800896 schedule();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800897 rcu_stutter_wait("rcu_torture_reader");
898 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800899 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800900 rcutorture_shutdown_absorb("rcu_torture_reader");
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700901 if (irqreader && cur_ops->irq_capable)
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700902 del_timer_sync(&t);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800903 while (!kthread_should_stop())
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800904 schedule_timeout_uninterruptible(1);
905 return 0;
906}
907
908/*
909 * Create an RCU-torture statistics message in the specified buffer.
910 */
911static int
912rcu_torture_printk(char *page)
913{
914 int cnt = 0;
915 int cpu;
916 int i;
917 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
918 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
919
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800920 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800921 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
922 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
923 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
924 }
925 }
926 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
927 if (pipesummary[i] != 0)
928 break;
929 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700930 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800931 cnt += sprintf(&page[cnt],
Paul E. McKenney996417d2005-11-18 01:10:50 -0800932 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700933 "rtmbe: %d nt: %ld",
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800934 rcu_torture_current,
935 rcu_torture_current_version,
936 list_empty(&rcu_torture_freelist),
937 atomic_read(&n_rcu_torture_alloc),
938 atomic_read(&n_rcu_torture_alloc_fail),
Paul E. McKenney996417d2005-11-18 01:10:50 -0800939 atomic_read(&n_rcu_torture_free),
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700940 atomic_read(&n_rcu_torture_mberror),
941 n_rcu_torture_timers);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800942 if (atomic_read(&n_rcu_torture_mberror) != 0)
943 cnt += sprintf(&page[cnt], " !!!");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700944 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800945 if (i > 1) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800946 cnt += sprintf(&page[cnt], "!!! ");
Paul E. McKenney996417d2005-11-18 01:10:50 -0800947 atomic_inc(&n_rcu_torture_error);
Ingo Molnar5af970a2008-06-18 10:09:48 +0200948 WARN_ON_ONCE(1);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800949 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800950 cnt += sprintf(&page[cnt], "Reader Pipe: ");
951 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
952 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700953 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800954 cnt += sprintf(&page[cnt], "Reader Batch: ");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700955 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800956 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700957 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800958 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
959 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
960 cnt += sprintf(&page[cnt], " %d",
961 atomic_read(&rcu_torture_wcount[i]));
962 }
963 cnt += sprintf(&page[cnt], "\n");
Josh Triplettc8e5b162007-05-08 00:33:20 -0700964 if (cur_ops->stats)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700965 cnt += cur_ops->stats(&page[cnt]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800966 return cnt;
967}
968
969/*
970 * Print torture statistics. Caller must ensure that there is only
971 * one call to this function at a given time!!! This is normally
972 * accomplished by relying on the module system to only have one copy
973 * of the module loaded, and then by giving the rcu_torture_stats
974 * kthread full control (or the init/cleanup functions when rcu_torture_stats
975 * thread is not running).
976 */
977static void
978rcu_torture_stats_print(void)
979{
980 int cnt;
981
982 cnt = rcu_torture_printk(printk_buf);
983 printk(KERN_ALERT "%s", printk_buf);
984}
985
986/*
987 * Periodically prints torture statistics, if periodic statistics printing
988 * was specified via the stat_interval module parameter.
989 *
990 * No need to worry about fullstop here, since this one doesn't reference
991 * volatile state or register callbacks.
992 */
993static int
994rcu_torture_stats(void *arg)
995{
996 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
997 do {
998 schedule_timeout_interruptible(stat_interval * HZ);
999 rcu_torture_stats_print();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001000 rcutorture_shutdown_absorb("rcu_torture_stats");
1001 } while (!kthread_should_stop());
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001002 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1003 return 0;
1004}
1005
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001006static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
1007
1008/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1009 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1010 */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -07001011static void rcu_torture_shuffle_tasks(void)
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001012{
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001013 int i;
1014
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001015 cpumask_setall(shuffle_tmp_mask);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01001016 get_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001017
1018 /* No point in shuffling if there is only one online CPU (ex: UP) */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001019 if (num_online_cpus() == 1) {
1020 put_online_cpus();
1021 return;
1022 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001023
1024 if (rcu_idle_cpu != -1)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001025 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001026
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001027 set_cpus_allowed_ptr(current, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001028
Josh Triplettc8e5b162007-05-08 00:33:20 -07001029 if (reader_tasks) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001030 for (i = 0; i < nrealreaders; i++)
1031 if (reader_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -07001032 set_cpus_allowed_ptr(reader_tasks[i],
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001033 shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001034 }
1035
Josh Triplettc8e5b162007-05-08 00:33:20 -07001036 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001037 for (i = 0; i < nfakewriters; i++)
1038 if (fakewriter_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -07001039 set_cpus_allowed_ptr(fakewriter_tasks[i],
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001040 shuffle_tmp_mask);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001041 }
1042
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001043 if (writer_task)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001044 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001045
1046 if (stats_task)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001047 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001048
1049 if (rcu_idle_cpu == -1)
1050 rcu_idle_cpu = num_online_cpus() - 1;
1051 else
1052 rcu_idle_cpu--;
1053
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01001054 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001055}
1056
1057/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1058 * system to become idle at a time and cut off its timer ticks. This is meant
1059 * to test the support for such tickless idle CPU in RCU.
1060 */
1061static int
1062rcu_torture_shuffle(void *arg)
1063{
1064 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1065 do {
1066 schedule_timeout_interruptible(shuffle_interval * HZ);
1067 rcu_torture_shuffle_tasks();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001068 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1069 } while (!kthread_should_stop());
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001070 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1071 return 0;
1072}
1073
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001074/* Cause the rcutorture test to "stutter", starting and stopping all
1075 * threads periodically.
1076 */
1077static int
1078rcu_torture_stutter(void *arg)
1079{
1080 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1081 do {
1082 schedule_timeout_interruptible(stutter * HZ);
1083 stutter_pause_test = 1;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001084 if (!kthread_should_stop())
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001085 schedule_timeout_interruptible(stutter * HZ);
1086 stutter_pause_test = 0;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001087 rcutorture_shutdown_absorb("rcu_torture_stutter");
1088 } while (!kthread_should_stop());
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001089 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1090 return 0;
1091}
1092
Paul E. McKenney95c38322006-03-24 03:15:58 -08001093static inline void
1094rcu_torture_print_module_parms(char *tag)
1095{
Josh Triplettb772e1d2006-10-04 02:17:13 -07001096 printk(KERN_ALERT "%s" TORTURE_FLAG
1097 "--- %s: nreaders=%d nfakewriters=%d "
Paul E. McKenney95c38322006-03-24 03:15:58 -08001098 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001099 "shuffle_interval=%d stutter=%d irqreader=%d "
1100 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d\n",
Josh Triplettb772e1d2006-10-04 02:17:13 -07001101 torture_type, tag, nrealreaders, nfakewriters,
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001102 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001103 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter);
Paul E. McKenney95c38322006-03-24 03:15:58 -08001104}
1105
Paul E. McKenney343e9092008-12-15 16:13:07 -08001106static struct notifier_block rcutorture_nb = {
1107 .notifier_call = rcutorture_shutdown_notify,
1108};
1109
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001110static void
1111rcu_torture_cleanup(void)
1112{
1113 int i;
1114
Paul E. McKenney343e9092008-12-15 16:13:07 -08001115 mutex_lock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001116 if (fullstop == FULLSTOP_SHUTDOWN) {
1117 printk(KERN_WARNING /* but going down anyway, so... */
1118 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
Paul E. McKenney343e9092008-12-15 16:13:07 -08001119 mutex_unlock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001120 schedule_timeout_uninterruptible(10);
Paul E. McKenney343e9092008-12-15 16:13:07 -08001121 if (cur_ops->cb_barrier != NULL)
1122 cur_ops->cb_barrier();
1123 return;
1124 }
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001125 fullstop = FULLSTOP_RMMOD;
Paul E. McKenney343e9092008-12-15 16:13:07 -08001126 mutex_unlock(&fullstop_mutex);
1127 unregister_reboot_notifier(&rcutorture_nb);
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001128 if (stutter_task) {
1129 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1130 kthread_stop(stutter_task);
1131 }
1132 stutter_task = NULL;
Josh Triplettc8e5b162007-05-08 00:33:20 -07001133 if (shuffler_task) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001134 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1135 kthread_stop(shuffler_task);
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001136 free_cpumask_var(shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001137 }
1138 shuffler_task = NULL;
1139
Josh Triplettc8e5b162007-05-08 00:33:20 -07001140 if (writer_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001141 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1142 kthread_stop(writer_task);
1143 }
1144 writer_task = NULL;
1145
Josh Triplettc8e5b162007-05-08 00:33:20 -07001146 if (reader_tasks) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001147 for (i = 0; i < nrealreaders; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001148 if (reader_tasks[i]) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001149 VERBOSE_PRINTK_STRING(
1150 "Stopping rcu_torture_reader task");
1151 kthread_stop(reader_tasks[i]);
1152 }
1153 reader_tasks[i] = NULL;
1154 }
1155 kfree(reader_tasks);
1156 reader_tasks = NULL;
1157 }
1158 rcu_torture_current = NULL;
1159
Josh Triplettc8e5b162007-05-08 00:33:20 -07001160 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001161 for (i = 0; i < nfakewriters; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001162 if (fakewriter_tasks[i]) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001163 VERBOSE_PRINTK_STRING(
1164 "Stopping rcu_torture_fakewriter task");
1165 kthread_stop(fakewriter_tasks[i]);
1166 }
1167 fakewriter_tasks[i] = NULL;
1168 }
1169 kfree(fakewriter_tasks);
1170 fakewriter_tasks = NULL;
1171 }
1172
Josh Triplettc8e5b162007-05-08 00:33:20 -07001173 if (stats_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001174 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1175 kthread_stop(stats_task);
1176 }
1177 stats_task = NULL;
1178
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001179 if (fqs_task) {
1180 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1181 kthread_stop(fqs_task);
1182 }
1183 fqs_task = NULL;
1184
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001185 /* Wait for all RCU callbacks to fire. */
Paul E. McKenney23269742008-05-12 21:21:05 +02001186
1187 if (cur_ops->cb_barrier != NULL)
1188 cur_ops->cb_barrier();
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001189
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001190 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001191
Josh Triplettc8e5b162007-05-08 00:33:20 -07001192 if (cur_ops->cleanup)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001193 cur_ops->cleanup();
Paul E. McKenney95c38322006-03-24 03:15:58 -08001194 if (atomic_read(&n_rcu_torture_error))
1195 rcu_torture_print_module_parms("End of test: FAILURE");
1196 else
1197 rcu_torture_print_module_parms("End of test: SUCCESS");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001198}
1199
Josh Triplett6f8bc5002007-05-08 00:25:24 -07001200static int __init
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001201rcu_torture_init(void)
1202{
1203 int i;
1204 int cpu;
1205 int firsterr = 0;
Josh Triplettade5fb82007-05-08 00:33:22 -07001206 static struct rcu_torture_ops *torture_ops[] =
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -08001207 { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
1208 &rcu_bh_ops, &rcu_bh_sync_ops,
Paul E. McKenney804bb832009-10-25 19:03:52 -07001209 &srcu_ops, &srcu_expedited_ops,
1210 &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001211
Paul E. McKenney343e9092008-12-15 16:13:07 -08001212 mutex_lock(&fullstop_mutex);
1213
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001214 /* Process args and tell the world that the torturer is on the job. */
Josh Triplettade5fb82007-05-08 00:33:22 -07001215 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001216 cur_ops = torture_ops[i];
Josh Triplettade5fb82007-05-08 00:33:22 -07001217 if (strcmp(torture_type, cur_ops->name) == 0)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001218 break;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001219 }
Josh Triplettade5fb82007-05-08 00:33:22 -07001220 if (i == ARRAY_SIZE(torture_ops)) {
Paul E. McKenneycf886c42009-10-25 19:03:54 -07001221 printk(KERN_ALERT "rcu-torture: invalid torture type: \"%s\"\n",
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001222 torture_type);
Paul E. McKenneycf886c42009-10-25 19:03:54 -07001223 printk(KERN_ALERT "rcu-torture types:");
1224 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
1225 printk(KERN_ALERT " %s", torture_ops[i]->name);
1226 printk(KERN_ALERT "\n");
Paul E. McKenney343e9092008-12-15 16:13:07 -08001227 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya71fca52009-09-18 10:28:19 -07001228 return -EINVAL;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001229 }
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001230 if (cur_ops->fqs == NULL && fqs_duration != 0) {
1231 printk(KERN_ALERT "rcu-torture: ->fqs NULL and non-zero "
1232 "fqs_duration, fqs disabled.\n");
1233 fqs_duration = 0;
1234 }
Josh Triplettc8e5b162007-05-08 00:33:20 -07001235 if (cur_ops->init)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001236 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1237
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001238 if (nreaders >= 0)
1239 nrealreaders = nreaders;
1240 else
1241 nrealreaders = 2 * num_online_cpus();
Paul E. McKenney95c38322006-03-24 03:15:58 -08001242 rcu_torture_print_module_parms("Start of test");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001243 fullstop = FULLSTOP_DONTSTOP;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001244
1245 /* Set up the freelist. */
1246
1247 INIT_LIST_HEAD(&rcu_torture_freelist);
Ahmed S. Darwish788e7702007-05-08 00:33:14 -07001248 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
Paul E. McKenney996417d2005-11-18 01:10:50 -08001249 rcu_tortures[i].rtort_mbtest = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001250 list_add_tail(&rcu_tortures[i].rtort_free,
1251 &rcu_torture_freelist);
1252 }
1253
1254 /* Initialize the statistics so that each run gets its own numbers. */
1255
1256 rcu_torture_current = NULL;
1257 rcu_torture_current_version = 0;
1258 atomic_set(&n_rcu_torture_alloc, 0);
1259 atomic_set(&n_rcu_torture_alloc_fail, 0);
1260 atomic_set(&n_rcu_torture_free, 0);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001261 atomic_set(&n_rcu_torture_mberror, 0);
1262 atomic_set(&n_rcu_torture_error, 0);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001263 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1264 atomic_set(&rcu_torture_wcount[i], 0);
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001265 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001266 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1267 per_cpu(rcu_torture_count, cpu)[i] = 0;
1268 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1269 }
1270 }
1271
1272 /* Start up the kthreads. */
1273
1274 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1275 writer_task = kthread_run(rcu_torture_writer, NULL,
1276 "rcu_torture_writer");
1277 if (IS_ERR(writer_task)) {
1278 firsterr = PTR_ERR(writer_task);
1279 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1280 writer_task = NULL;
1281 goto unwind;
1282 }
Josh Triplettb772e1d2006-10-04 02:17:13 -07001283 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
Paul E. McKenneya71fca52009-09-18 10:28:19 -07001284 GFP_KERNEL);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001285 if (fakewriter_tasks == NULL) {
1286 VERBOSE_PRINTK_ERRSTRING("out of memory");
1287 firsterr = -ENOMEM;
1288 goto unwind;
1289 }
1290 for (i = 0; i < nfakewriters; i++) {
1291 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1292 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
Paul E. McKenneya71fca52009-09-18 10:28:19 -07001293 "rcu_torture_fakewriter");
Josh Triplettb772e1d2006-10-04 02:17:13 -07001294 if (IS_ERR(fakewriter_tasks[i])) {
1295 firsterr = PTR_ERR(fakewriter_tasks[i]);
1296 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1297 fakewriter_tasks[i] = NULL;
1298 goto unwind;
1299 }
1300 }
Josh Triplett2860aab2006-10-04 02:17:11 -07001301 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001302 GFP_KERNEL);
1303 if (reader_tasks == NULL) {
1304 VERBOSE_PRINTK_ERRSTRING("out of memory");
1305 firsterr = -ENOMEM;
1306 goto unwind;
1307 }
1308 for (i = 0; i < nrealreaders; i++) {
1309 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1310 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1311 "rcu_torture_reader");
1312 if (IS_ERR(reader_tasks[i])) {
1313 firsterr = PTR_ERR(reader_tasks[i]);
1314 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1315 reader_tasks[i] = NULL;
1316 goto unwind;
1317 }
1318 }
1319 if (stat_interval > 0) {
1320 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1321 stats_task = kthread_run(rcu_torture_stats, NULL,
1322 "rcu_torture_stats");
1323 if (IS_ERR(stats_task)) {
1324 firsterr = PTR_ERR(stats_task);
1325 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1326 stats_task = NULL;
1327 goto unwind;
1328 }
1329 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001330 if (test_no_idle_hz) {
1331 rcu_idle_cpu = num_online_cpus() - 1;
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001332
1333 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
1334 firsterr = -ENOMEM;
1335 VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
1336 goto unwind;
1337 }
1338
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001339 /* Create the shuffler thread */
1340 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1341 "rcu_torture_shuffle");
1342 if (IS_ERR(shuffler_task)) {
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001343 free_cpumask_var(shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001344 firsterr = PTR_ERR(shuffler_task);
1345 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1346 shuffler_task = NULL;
1347 goto unwind;
1348 }
1349 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001350 if (stutter < 0)
1351 stutter = 0;
1352 if (stutter) {
1353 /* Create the stutter thread */
1354 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1355 "rcu_torture_stutter");
1356 if (IS_ERR(stutter_task)) {
1357 firsterr = PTR_ERR(stutter_task);
1358 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1359 stutter_task = NULL;
1360 goto unwind;
1361 }
1362 }
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001363 if (fqs_duration < 0)
1364 fqs_duration = 0;
1365 if (fqs_duration) {
1366 /* Create the stutter thread */
1367 fqs_task = kthread_run(rcu_torture_fqs, NULL,
1368 "rcu_torture_fqs");
1369 if (IS_ERR(fqs_task)) {
1370 firsterr = PTR_ERR(fqs_task);
1371 VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
1372 fqs_task = NULL;
1373 goto unwind;
1374 }
1375 }
Paul E. McKenney343e9092008-12-15 16:13:07 -08001376 register_reboot_notifier(&rcutorture_nb);
1377 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001378 return 0;
1379
1380unwind:
Paul E. McKenney343e9092008-12-15 16:13:07 -08001381 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001382 rcu_torture_cleanup();
1383 return firsterr;
1384}
1385
1386module_init(rcu_torture_init);
1387module_exit(rcu_torture_cleanup);