blob: 1cff28db56b6a87cacc225a740e602c4603d2e50 [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>
Josh Triplettb772e1d2006-10-04 02:17:13 -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 "
53 "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). */
Josh Triplett20d2e422006-10-04 02:17:15 -070064static char *torture_type = "rcu"; /* What RCU implementation to torture. */
Paul E. McKenneya241ec62005-10-30 15:03:12 -080065
Josh Triplettd6ad6712007-03-06 01:42:13 -080066module_param(nreaders, int, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080067MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
Josh Triplettd6ad6712007-03-06 01:42:13 -080068module_param(nfakewriters, int, 0444);
Josh Triplettb772e1d2006-10-04 02:17:13 -070069MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
Josh Triplettd6ad6712007-03-06 01:42:13 -080070module_param(stat_interval, int, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080071MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080072module_param(verbose, bool, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080073MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080074module_param(test_no_idle_hz, bool, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080075MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
Josh Triplettd6ad6712007-03-06 01:42:13 -080076module_param(shuffle_interval, int, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080077MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
Paul E. McKenneyd120f652008-06-18 05:21:44 -070078module_param(stutter, int, 0444);
79MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
Paul E. McKenney0729fbf2008-06-25 12:24:52 -070080module_param(irqreader, int, 0444);
81MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
Josh Triplettd6ad6712007-03-06 01:42:13 -080082module_param(torture_type, charp, 0444);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -070083MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070084
85#define TORTURE_FLAG "-torture:"
Paul E. McKenneya241ec62005-10-30 15:03:12 -080086#define PRINTK_STRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070087 do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -080088#define VERBOSE_PRINTK_STRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070089 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -080090#define VERBOSE_PRINTK_ERRSTRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070091 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -080092
93static char printk_buf[4096];
94
95static int nrealreaders;
96static struct task_struct *writer_task;
Josh Triplettb772e1d2006-10-04 02:17:13 -070097static struct task_struct **fakewriter_tasks;
Paul E. McKenneya241ec62005-10-30 15:03:12 -080098static struct task_struct **reader_tasks;
99static struct task_struct *stats_task;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800100static struct task_struct *shuffler_task;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700101static struct task_struct *stutter_task;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800102
103#define RCU_TORTURE_PIPE_LEN 10
104
105struct rcu_torture {
106 struct rcu_head rtort_rcu;
107 int rtort_pipe_count;
108 struct list_head rtort_free;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800109 int rtort_mbtest;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800110};
111
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800112static LIST_HEAD(rcu_torture_freelist);
113static struct rcu_torture *rcu_torture_current = NULL;
114static long rcu_torture_current_version = 0;
115static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
116static DEFINE_SPINLOCK(rcu_torture_lock);
117static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
118 { 0 };
119static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
120 { 0 };
121static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700122static atomic_t n_rcu_torture_alloc;
123static atomic_t n_rcu_torture_alloc_fail;
124static atomic_t n_rcu_torture_free;
125static atomic_t n_rcu_torture_mberror;
126static atomic_t n_rcu_torture_error;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700127static long n_rcu_torture_timers = 0;
Josh Triplette3033732006-10-04 02:17:14 -0700128static struct list_head rcu_torture_removed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800129
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700130static int stutter_pause_test = 0;
131
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700132#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
133#define RCUTORTURE_RUNNABLE_INIT 1
134#else
135#define RCUTORTURE_RUNNABLE_INIT 0
136#endif
137int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
138
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800139#define FULLSTOP_SHUTDOWN 1 /* Bail due to system shutdown/panic. */
Paul E. McKenney343e9092008-12-15 16:13:07 -0800140#define FULLSTOP_CLEANUP 2 /* Orderly shutdown. */
141static int fullstop; /* stop generating callbacks at test end. */
142DEFINE_MUTEX(fullstop_mutex); /* protect fullstop transitions and */
143 /* spawning of kthreads. */
144
145/*
146 * Detect and respond to a signal-based shutdown.
147 */
148static int
149rcutorture_shutdown_notify(struct notifier_block *unused1,
150 unsigned long unused2, void *unused3)
151{
152 if (fullstop)
153 return NOTIFY_DONE;
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800154 mutex_lock(&fullstop_mutex);
155 if (!fullstop)
156 fullstop = FULLSTOP_SHUTDOWN;
157 mutex_unlock(&fullstop_mutex);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800158 return NOTIFY_DONE;
159}
160
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800161/*
162 * Allocate an element from the rcu_tortures pool.
163 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800164static struct rcu_torture *
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800165rcu_torture_alloc(void)
166{
167 struct list_head *p;
168
Ingo Molnaradac1662006-01-25 19:50:12 +0100169 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800170 if (list_empty(&rcu_torture_freelist)) {
171 atomic_inc(&n_rcu_torture_alloc_fail);
Ingo Molnaradac1662006-01-25 19:50:12 +0100172 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800173 return NULL;
174 }
175 atomic_inc(&n_rcu_torture_alloc);
176 p = rcu_torture_freelist.next;
177 list_del_init(p);
Ingo Molnaradac1662006-01-25 19:50:12 +0100178 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800179 return container_of(p, struct rcu_torture, rtort_free);
180}
181
182/*
183 * Free an element to the rcu_tortures pool.
184 */
185static void
186rcu_torture_free(struct rcu_torture *p)
187{
188 atomic_inc(&n_rcu_torture_free);
Ingo Molnaradac1662006-01-25 19:50:12 +0100189 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800190 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
Ingo Molnaradac1662006-01-25 19:50:12 +0100191 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800192}
193
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800194struct rcu_random_state {
195 unsigned long rrs_state;
Josh Triplett75cfef32006-10-04 02:17:12 -0700196 long rrs_count;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800197};
198
199#define RCU_RANDOM_MULT 39916801 /* prime */
200#define RCU_RANDOM_ADD 479001701 /* prime */
201#define RCU_RANDOM_REFRESH 10000
202
203#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
204
205/*
206 * Crude but fast random-number generator. Uses a linear congruential
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700207 * generator, with occasional help from cpu_clock().
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800208 */
Josh Triplett75cfef32006-10-04 02:17:12 -0700209static unsigned long
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800210rcu_random(struct rcu_random_state *rrsp)
211{
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800212 if (--rrsp->rrs_count < 0) {
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700213 rrsp->rrs_state +=
214 (unsigned long)cpu_clock(raw_smp_processor_id());
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800215 rrsp->rrs_count = RCU_RANDOM_REFRESH;
216 }
217 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
218 return swahw32(rrsp->rrs_state);
219}
220
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700221static void
222rcu_stutter_wait(void)
223{
Paul E. McKenney343e9092008-12-15 16:13:07 -0800224 while ((stutter_pause_test || !rcutorture_runnable) && !fullstop) {
Paul E. McKenneye3d7be22008-06-22 13:06:38 -0700225 if (rcutorture_runnable)
226 schedule_timeout_interruptible(1);
227 else
Paul E. McKenney3ccf79f2008-06-22 14:02:55 -0700228 schedule_timeout_interruptible(round_jiffies_relative(HZ));
Paul E. McKenney343e9092008-12-15 16:13:07 -0800229 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700230}
231
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800232/*
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700233 * Operations vector for selecting different types of tests.
234 */
235
236struct rcu_torture_ops {
237 void (*init)(void);
238 void (*cleanup)(void);
239 int (*readlock)(void);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700240 void (*readdelay)(struct rcu_random_state *rrsp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700241 void (*readunlock)(int idx);
242 int (*completed)(void);
243 void (*deferredfree)(struct rcu_torture *p);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700244 void (*sync)(void);
Paul E. McKenney23269742008-05-12 21:21:05 +0200245 void (*cb_barrier)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700246 int (*stats)(char *page);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700247 int irqcapable;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700248 char *name;
249};
250static struct rcu_torture_ops *cur_ops = NULL;
251
252/*
253 * Definitions for rcu torture testing.
254 */
255
Josh Tripletta49a4af2006-09-29 01:59:30 -0700256static int rcu_torture_read_lock(void) __acquires(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700257{
258 rcu_read_lock();
259 return 0;
260}
261
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700262static void rcu_read_delay(struct rcu_random_state *rrsp)
263{
264 long delay;
265 const long longdelay = 200;
266
267 /* We want there to be long-running readers, but not all the time. */
268
269 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay);
270 if (!delay)
271 udelay(longdelay);
272}
273
Josh Tripletta49a4af2006-09-29 01:59:30 -0700274static void rcu_torture_read_unlock(int idx) __releases(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700275{
276 rcu_read_unlock();
277}
278
279static int rcu_torture_completed(void)
280{
281 return rcu_batches_completed();
282}
283
284static void
285rcu_torture_cb(struct rcu_head *p)
286{
287 int i;
288 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
289
290 if (fullstop) {
291 /* Test is ending, just drop callbacks on the floor. */
292 /* The next initialization will pick up the pieces. */
293 return;
294 }
295 i = rp->rtort_pipe_count;
296 if (i > RCU_TORTURE_PIPE_LEN)
297 i = RCU_TORTURE_PIPE_LEN;
298 atomic_inc(&rcu_torture_wcount[i]);
299 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
300 rp->rtort_mbtest = 0;
301 rcu_torture_free(rp);
302 } else
303 cur_ops->deferredfree(rp);
304}
305
306static void rcu_torture_deferred_free(struct rcu_torture *p)
307{
308 call_rcu(&p->rtort_rcu, rcu_torture_cb);
309}
310
311static struct rcu_torture_ops rcu_ops = {
312 .init = NULL,
313 .cleanup = NULL,
314 .readlock = rcu_torture_read_lock,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700315 .readdelay = rcu_read_delay,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700316 .readunlock = rcu_torture_read_unlock,
317 .completed = rcu_torture_completed,
318 .deferredfree = rcu_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700319 .sync = synchronize_rcu,
Paul E. McKenney23269742008-05-12 21:21:05 +0200320 .cb_barrier = rcu_barrier,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700321 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700322 .irqcapable = 1,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700323 .name = "rcu"
324};
325
Josh Triplette3033732006-10-04 02:17:14 -0700326static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
327{
328 int i;
329 struct rcu_torture *rp;
330 struct rcu_torture *rp1;
331
332 cur_ops->sync();
333 list_add(&p->rtort_free, &rcu_torture_removed);
334 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
335 i = rp->rtort_pipe_count;
336 if (i > RCU_TORTURE_PIPE_LEN)
337 i = RCU_TORTURE_PIPE_LEN;
338 atomic_inc(&rcu_torture_wcount[i]);
339 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
340 rp->rtort_mbtest = 0;
341 list_del(&rp->rtort_free);
342 rcu_torture_free(rp);
343 }
344 }
345}
346
347static void rcu_sync_torture_init(void)
348{
349 INIT_LIST_HEAD(&rcu_torture_removed);
350}
351
Josh Triplett20d2e422006-10-04 02:17:15 -0700352static struct rcu_torture_ops rcu_sync_ops = {
353 .init = rcu_sync_torture_init,
354 .cleanup = NULL,
355 .readlock = rcu_torture_read_lock,
356 .readdelay = rcu_read_delay,
357 .readunlock = rcu_torture_read_unlock,
358 .completed = rcu_torture_completed,
359 .deferredfree = rcu_sync_torture_deferred_free,
360 .sync = synchronize_rcu,
Paul E. McKenney23269742008-05-12 21:21:05 +0200361 .cb_barrier = NULL,
Josh Triplett20d2e422006-10-04 02:17:15 -0700362 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700363 .irqcapable = 1,
Josh Triplett20d2e422006-10-04 02:17:15 -0700364 .name = "rcu_sync"
365};
366
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700367/*
368 * Definitions for rcu_bh torture testing.
369 */
370
Josh Tripletta49a4af2006-09-29 01:59:30 -0700371static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700372{
373 rcu_read_lock_bh();
374 return 0;
375}
376
Josh Tripletta49a4af2006-09-29 01:59:30 -0700377static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700378{
379 rcu_read_unlock_bh();
380}
381
382static int rcu_bh_torture_completed(void)
383{
384 return rcu_batches_completed_bh();
385}
386
387static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
388{
389 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
390}
391
Josh Triplettb772e1d2006-10-04 02:17:13 -0700392struct rcu_bh_torture_synchronize {
393 struct rcu_head head;
394 struct completion completion;
395};
396
397static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
398{
399 struct rcu_bh_torture_synchronize *rcu;
400
401 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
402 complete(&rcu->completion);
403}
404
405static void rcu_bh_torture_synchronize(void)
406{
407 struct rcu_bh_torture_synchronize rcu;
408
409 init_completion(&rcu.completion);
410 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
411 wait_for_completion(&rcu.completion);
412}
413
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700414static struct rcu_torture_ops rcu_bh_ops = {
415 .init = NULL,
416 .cleanup = NULL,
417 .readlock = rcu_bh_torture_read_lock,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700418 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700419 .readunlock = rcu_bh_torture_read_unlock,
420 .completed = rcu_bh_torture_completed,
421 .deferredfree = rcu_bh_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700422 .sync = rcu_bh_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200423 .cb_barrier = rcu_barrier_bh,
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700424 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700425 .irqcapable = 1,
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700426 .name = "rcu_bh"
427};
428
Josh Triplett11a14702006-10-04 02:17:16 -0700429static struct rcu_torture_ops rcu_bh_sync_ops = {
430 .init = rcu_sync_torture_init,
431 .cleanup = NULL,
432 .readlock = rcu_bh_torture_read_lock,
433 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
434 .readunlock = rcu_bh_torture_read_unlock,
435 .completed = rcu_bh_torture_completed,
436 .deferredfree = rcu_sync_torture_deferred_free,
437 .sync = rcu_bh_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200438 .cb_barrier = NULL,
Josh Triplett11a14702006-10-04 02:17:16 -0700439 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700440 .irqcapable = 1,
Josh Triplett11a14702006-10-04 02:17:16 -0700441 .name = "rcu_bh_sync"
442};
443
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700444/*
445 * Definitions for srcu torture testing.
446 */
447
448static struct srcu_struct srcu_ctl;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700449
450static void srcu_torture_init(void)
451{
452 init_srcu_struct(&srcu_ctl);
Josh Triplette3033732006-10-04 02:17:14 -0700453 rcu_sync_torture_init();
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700454}
455
456static void srcu_torture_cleanup(void)
457{
458 synchronize_srcu(&srcu_ctl);
459 cleanup_srcu_struct(&srcu_ctl);
460}
461
Josh Triplett012d3ca2006-12-06 20:40:19 -0800462static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700463{
464 return srcu_read_lock(&srcu_ctl);
465}
466
467static void srcu_read_delay(struct rcu_random_state *rrsp)
468{
469 long delay;
470 const long uspertick = 1000000 / HZ;
471 const long longdelay = 10;
472
473 /* We want there to be long-running readers, but not all the time. */
474
475 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
476 if (!delay)
477 schedule_timeout_interruptible(longdelay);
478}
479
Josh Triplett012d3ca2006-12-06 20:40:19 -0800480static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700481{
482 srcu_read_unlock(&srcu_ctl, idx);
483}
484
485static int srcu_torture_completed(void)
486{
487 return srcu_batches_completed(&srcu_ctl);
488}
489
Josh Triplettb772e1d2006-10-04 02:17:13 -0700490static void srcu_torture_synchronize(void)
491{
492 synchronize_srcu(&srcu_ctl);
493}
494
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700495static int srcu_torture_stats(char *page)
496{
497 int cnt = 0;
498 int cpu;
499 int idx = srcu_ctl.completed & 0x1;
500
501 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
502 torture_type, TORTURE_FLAG, idx);
503 for_each_possible_cpu(cpu) {
504 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
505 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
506 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
507 }
508 cnt += sprintf(&page[cnt], "\n");
509 return cnt;
510}
511
512static struct rcu_torture_ops srcu_ops = {
513 .init = srcu_torture_init,
514 .cleanup = srcu_torture_cleanup,
515 .readlock = srcu_torture_read_lock,
516 .readdelay = srcu_read_delay,
517 .readunlock = srcu_torture_read_unlock,
518 .completed = srcu_torture_completed,
Josh Triplette3033732006-10-04 02:17:14 -0700519 .deferredfree = rcu_sync_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700520 .sync = srcu_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200521 .cb_barrier = NULL,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700522 .stats = srcu_torture_stats,
523 .name = "srcu"
524};
525
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700526/*
527 * Definitions for sched torture testing.
528 */
529
530static int sched_torture_read_lock(void)
531{
532 preempt_disable();
533 return 0;
534}
535
536static void sched_torture_read_unlock(int idx)
537{
538 preempt_enable();
539}
540
541static int sched_torture_completed(void)
542{
543 return 0;
544}
545
Paul E. McKenney23269742008-05-12 21:21:05 +0200546static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
547{
548 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
549}
550
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700551static void sched_torture_synchronize(void)
552{
553 synchronize_sched();
554}
555
556static struct rcu_torture_ops sched_ops = {
557 .init = rcu_sync_torture_init,
558 .cleanup = NULL,
559 .readlock = sched_torture_read_lock,
560 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
561 .readunlock = sched_torture_read_unlock,
562 .completed = sched_torture_completed,
Paul E. McKenney23269742008-05-12 21:21:05 +0200563 .deferredfree = rcu_sched_torture_deferred_free,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700564 .sync = sched_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200565 .cb_barrier = rcu_barrier_sched,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700566 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700567 .irqcapable = 1,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700568 .name = "sched"
569};
570
Paul E. McKenney23269742008-05-12 21:21:05 +0200571static struct rcu_torture_ops sched_ops_sync = {
572 .init = rcu_sync_torture_init,
573 .cleanup = NULL,
574 .readlock = sched_torture_read_lock,
575 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
576 .readunlock = sched_torture_read_unlock,
577 .completed = sched_torture_completed,
578 .deferredfree = rcu_sync_torture_deferred_free,
579 .sync = sched_torture_synchronize,
580 .cb_barrier = NULL,
581 .stats = NULL,
582 .name = "sched_sync"
583};
584
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700585/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800586 * RCU torture writer kthread. Repeatedly substitutes a new structure
587 * for that pointed to by rcu_torture_current, freeing the old structure
588 * after a series of grace periods (the "pipeline").
589 */
590static int
591rcu_torture_writer(void *arg)
592{
593 int i;
594 long oldbatch = rcu_batches_completed();
595 struct rcu_torture *rp;
596 struct rcu_torture *old_rp;
597 static DEFINE_RCU_RANDOM(rand);
598
599 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800600 set_user_nice(current, 19);
601
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800602 do {
603 schedule_timeout_uninterruptible(1);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800604 if ((rp = rcu_torture_alloc()) == NULL)
605 continue;
606 rp->rtort_pipe_count = 0;
607 udelay(rcu_random(&rand) & 0x3ff);
608 old_rp = rcu_torture_current;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800609 rp->rtort_mbtest = 1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800610 rcu_assign_pointer(rcu_torture_current, rp);
611 smp_wmb();
Josh Triplettc8e5b162007-05-08 00:33:20 -0700612 if (old_rp) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800613 i = old_rp->rtort_pipe_count;
614 if (i > RCU_TORTURE_PIPE_LEN)
615 i = RCU_TORTURE_PIPE_LEN;
616 atomic_inc(&rcu_torture_wcount[i]);
617 old_rp->rtort_pipe_count++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700618 cur_ops->deferredfree(old_rp);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800619 }
620 rcu_torture_current_version++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700621 oldbatch = cur_ops->completed();
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700622 rcu_stutter_wait();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800623 } while (!kthread_should_stop() && !fullstop);
624 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800625 while (!kthread_should_stop() && fullstop != FULLSTOP_SHUTDOWN)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800626 schedule_timeout_uninterruptible(1);
627 return 0;
628}
629
630/*
Josh Triplettb772e1d2006-10-04 02:17:13 -0700631 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
632 * delay between calls.
633 */
634static int
635rcu_torture_fakewriter(void *arg)
636{
637 DEFINE_RCU_RANDOM(rand);
638
639 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
640 set_user_nice(current, 19);
641
642 do {
643 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
644 udelay(rcu_random(&rand) & 0x3ff);
645 cur_ops->sync();
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700646 rcu_stutter_wait();
Josh Triplettb772e1d2006-10-04 02:17:13 -0700647 } while (!kthread_should_stop() && !fullstop);
648
649 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800650 while (!kthread_should_stop() && fullstop != FULLSTOP_SHUTDOWN)
Josh Triplettb772e1d2006-10-04 02:17:13 -0700651 schedule_timeout_uninterruptible(1);
652 return 0;
653}
654
655/*
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700656 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
657 * incrementing the corresponding element of the pipeline array. The
658 * counter in the element should never be greater than 1, otherwise, the
659 * RCU implementation is broken.
660 */
661static void rcu_torture_timer(unsigned long unused)
662{
663 int idx;
664 int completed;
665 static DEFINE_RCU_RANDOM(rand);
666 static DEFINE_SPINLOCK(rand_lock);
667 struct rcu_torture *p;
668 int pipe_count;
669
670 idx = cur_ops->readlock();
671 completed = cur_ops->completed();
672 p = rcu_dereference(rcu_torture_current);
673 if (p == NULL) {
674 /* Leave because rcu_torture_writer is not yet underway */
675 cur_ops->readunlock(idx);
676 return;
677 }
678 if (p->rtort_mbtest == 0)
679 atomic_inc(&n_rcu_torture_mberror);
680 spin_lock(&rand_lock);
681 cur_ops->readdelay(&rand);
682 n_rcu_torture_timers++;
683 spin_unlock(&rand_lock);
684 preempt_disable();
685 pipe_count = p->rtort_pipe_count;
686 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
687 /* Should not happen, but... */
688 pipe_count = RCU_TORTURE_PIPE_LEN;
689 }
690 ++__get_cpu_var(rcu_torture_count)[pipe_count];
691 completed = cur_ops->completed() - completed;
692 if (completed > RCU_TORTURE_PIPE_LEN) {
693 /* Should not happen, but... */
694 completed = RCU_TORTURE_PIPE_LEN;
695 }
696 ++__get_cpu_var(rcu_torture_batch)[completed];
697 preempt_enable();
698 cur_ops->readunlock(idx);
699}
700
701/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800702 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
703 * incrementing the corresponding element of the pipeline array. The
704 * counter in the element should never be greater than 1, otherwise, the
705 * RCU implementation is broken.
706 */
707static int
708rcu_torture_reader(void *arg)
709{
710 int completed;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700711 int idx;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800712 DEFINE_RCU_RANDOM(rand);
713 struct rcu_torture *p;
714 int pipe_count;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700715 struct timer_list t;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800716
717 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800718 set_user_nice(current, 19);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700719 if (irqreader && cur_ops->irqcapable)
720 setup_timer_on_stack(&t, rcu_torture_timer, 0);
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800721
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800722 do {
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700723 if (irqreader && cur_ops->irqcapable) {
724 if (!timer_pending(&t))
725 mod_timer(&t, 1);
726 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700727 idx = cur_ops->readlock();
728 completed = cur_ops->completed();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800729 p = rcu_dereference(rcu_torture_current);
730 if (p == NULL) {
731 /* Wait for rcu_torture_writer to get underway */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700732 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800733 schedule_timeout_interruptible(HZ);
734 continue;
735 }
Paul E. McKenney996417d2005-11-18 01:10:50 -0800736 if (p->rtort_mbtest == 0)
737 atomic_inc(&n_rcu_torture_mberror);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700738 cur_ops->readdelay(&rand);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800739 preempt_disable();
740 pipe_count = p->rtort_pipe_count;
741 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
742 /* Should not happen, but... */
743 pipe_count = RCU_TORTURE_PIPE_LEN;
744 }
745 ++__get_cpu_var(rcu_torture_count)[pipe_count];
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700746 completed = cur_ops->completed() - completed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800747 if (completed > RCU_TORTURE_PIPE_LEN) {
748 /* Should not happen, but... */
749 completed = RCU_TORTURE_PIPE_LEN;
750 }
751 ++__get_cpu_var(rcu_torture_batch)[completed];
752 preempt_enable();
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700753 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800754 schedule();
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700755 rcu_stutter_wait();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800756 } while (!kthread_should_stop() && !fullstop);
757 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700758 if (irqreader && cur_ops->irqcapable)
759 del_timer_sync(&t);
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800760 while (!kthread_should_stop() && fullstop != FULLSTOP_SHUTDOWN)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800761 schedule_timeout_uninterruptible(1);
762 return 0;
763}
764
765/*
766 * Create an RCU-torture statistics message in the specified buffer.
767 */
768static int
769rcu_torture_printk(char *page)
770{
771 int cnt = 0;
772 int cpu;
773 int i;
774 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
775 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
776
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800777 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800778 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
779 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
780 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
781 }
782 }
783 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
784 if (pipesummary[i] != 0)
785 break;
786 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700787 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800788 cnt += sprintf(&page[cnt],
Paul E. McKenney996417d2005-11-18 01:10:50 -0800789 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700790 "rtmbe: %d nt: %ld",
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800791 rcu_torture_current,
792 rcu_torture_current_version,
793 list_empty(&rcu_torture_freelist),
794 atomic_read(&n_rcu_torture_alloc),
795 atomic_read(&n_rcu_torture_alloc_fail),
Paul E. McKenney996417d2005-11-18 01:10:50 -0800796 atomic_read(&n_rcu_torture_free),
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700797 atomic_read(&n_rcu_torture_mberror),
798 n_rcu_torture_timers);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800799 if (atomic_read(&n_rcu_torture_mberror) != 0)
800 cnt += sprintf(&page[cnt], " !!!");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700801 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800802 if (i > 1) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800803 cnt += sprintf(&page[cnt], "!!! ");
Paul E. McKenney996417d2005-11-18 01:10:50 -0800804 atomic_inc(&n_rcu_torture_error);
Ingo Molnar5af970a2008-06-18 10:09:48 +0200805 WARN_ON_ONCE(1);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800806 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800807 cnt += sprintf(&page[cnt], "Reader Pipe: ");
808 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
809 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700810 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800811 cnt += sprintf(&page[cnt], "Reader Batch: ");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700812 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800813 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700814 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800815 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
816 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
817 cnt += sprintf(&page[cnt], " %d",
818 atomic_read(&rcu_torture_wcount[i]));
819 }
820 cnt += sprintf(&page[cnt], "\n");
Josh Triplettc8e5b162007-05-08 00:33:20 -0700821 if (cur_ops->stats)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700822 cnt += cur_ops->stats(&page[cnt]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800823 return cnt;
824}
825
826/*
827 * Print torture statistics. Caller must ensure that there is only
828 * one call to this function at a given time!!! This is normally
829 * accomplished by relying on the module system to only have one copy
830 * of the module loaded, and then by giving the rcu_torture_stats
831 * kthread full control (or the init/cleanup functions when rcu_torture_stats
832 * thread is not running).
833 */
834static void
835rcu_torture_stats_print(void)
836{
837 int cnt;
838
839 cnt = rcu_torture_printk(printk_buf);
840 printk(KERN_ALERT "%s", printk_buf);
841}
842
843/*
844 * Periodically prints torture statistics, if periodic statistics printing
845 * was specified via the stat_interval module parameter.
846 *
847 * No need to worry about fullstop here, since this one doesn't reference
848 * volatile state or register callbacks.
849 */
850static int
851rcu_torture_stats(void *arg)
852{
853 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
854 do {
855 schedule_timeout_interruptible(stat_interval * HZ);
856 rcu_torture_stats_print();
Paul E. McKenney343e9092008-12-15 16:13:07 -0800857 } while (!kthread_should_stop() && !fullstop);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800858 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
859 return 0;
860}
861
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800862static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
863
864/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
865 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
866 */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700867static void rcu_torture_shuffle_tasks(void)
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800868{
Rusty Russellbd232f92009-01-01 10:12:26 +1030869 cpumask_var_t tmp_mask;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800870 int i;
871
Rusty Russellbd232f92009-01-01 10:12:26 +1030872 if (!alloc_cpumask_var(&tmp_mask, GFP_KERNEL))
873 BUG();
874
875 cpumask_setall(tmp_mask);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100876 get_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800877
878 /* No point in shuffling if there is only one online CPU (ex: UP) */
Rusty Russellbd232f92009-01-01 10:12:26 +1030879 if (num_online_cpus() == 1)
880 goto out;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800881
882 if (rcu_idle_cpu != -1)
Rusty Russellbd232f92009-01-01 10:12:26 +1030883 cpumask_clear_cpu(rcu_idle_cpu, tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800884
Rusty Russellbd232f92009-01-01 10:12:26 +1030885 set_cpus_allowed_ptr(current, tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800886
Josh Triplettc8e5b162007-05-08 00:33:20 -0700887 if (reader_tasks) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800888 for (i = 0; i < nrealreaders; i++)
889 if (reader_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -0700890 set_cpus_allowed_ptr(reader_tasks[i],
Rusty Russellbd232f92009-01-01 10:12:26 +1030891 tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800892 }
893
Josh Triplettc8e5b162007-05-08 00:33:20 -0700894 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -0700895 for (i = 0; i < nfakewriters; i++)
896 if (fakewriter_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -0700897 set_cpus_allowed_ptr(fakewriter_tasks[i],
Rusty Russellbd232f92009-01-01 10:12:26 +1030898 tmp_mask);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700899 }
900
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800901 if (writer_task)
Rusty Russellbd232f92009-01-01 10:12:26 +1030902 set_cpus_allowed_ptr(writer_task, tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800903
904 if (stats_task)
Rusty Russellbd232f92009-01-01 10:12:26 +1030905 set_cpus_allowed_ptr(stats_task, tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800906
907 if (rcu_idle_cpu == -1)
908 rcu_idle_cpu = num_online_cpus() - 1;
909 else
910 rcu_idle_cpu--;
911
Rusty Russellbd232f92009-01-01 10:12:26 +1030912out:
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100913 put_online_cpus();
Rusty Russellbd232f92009-01-01 10:12:26 +1030914 free_cpumask_var(tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800915}
916
917/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
918 * system to become idle at a time and cut off its timer ticks. This is meant
919 * to test the support for such tickless idle CPU in RCU.
920 */
921static int
922rcu_torture_shuffle(void *arg)
923{
924 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
925 do {
926 schedule_timeout_interruptible(shuffle_interval * HZ);
927 rcu_torture_shuffle_tasks();
Paul E. McKenney343e9092008-12-15 16:13:07 -0800928 } while (!kthread_should_stop() && !fullstop);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800929 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
930 return 0;
931}
932
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700933/* Cause the rcutorture test to "stutter", starting and stopping all
934 * threads periodically.
935 */
936static int
937rcu_torture_stutter(void *arg)
938{
939 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
940 do {
941 schedule_timeout_interruptible(stutter * HZ);
942 stutter_pause_test = 1;
Paul E. McKenney343e9092008-12-15 16:13:07 -0800943 if (!kthread_should_stop() && !fullstop)
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700944 schedule_timeout_interruptible(stutter * HZ);
945 stutter_pause_test = 0;
Paul E. McKenney343e9092008-12-15 16:13:07 -0800946 } while (!kthread_should_stop() && !fullstop);
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700947 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
948 return 0;
949}
950
Paul E. McKenney95c38322006-03-24 03:15:58 -0800951static inline void
952rcu_torture_print_module_parms(char *tag)
953{
Josh Triplettb772e1d2006-10-04 02:17:13 -0700954 printk(KERN_ALERT "%s" TORTURE_FLAG
955 "--- %s: nreaders=%d nfakewriters=%d "
Paul E. McKenney95c38322006-03-24 03:15:58 -0800956 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700957 "shuffle_interval=%d stutter=%d irqreader=%d\n",
Josh Triplettb772e1d2006-10-04 02:17:13 -0700958 torture_type, tag, nrealreaders, nfakewriters,
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700959 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700960 stutter, irqreader);
Paul E. McKenney95c38322006-03-24 03:15:58 -0800961}
962
Paul E. McKenney343e9092008-12-15 16:13:07 -0800963static struct notifier_block rcutorture_nb = {
964 .notifier_call = rcutorture_shutdown_notify,
965};
966
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800967static void
968rcu_torture_cleanup(void)
969{
970 int i;
971
Paul E. McKenney343e9092008-12-15 16:13:07 -0800972 mutex_lock(&fullstop_mutex);
973 if (!fullstop) {
974 /* If being signaled, let it happen, then exit. */
975 mutex_unlock(&fullstop_mutex);
976 schedule_timeout_interruptible(10 * HZ);
977 if (cur_ops->cb_barrier != NULL)
978 cur_ops->cb_barrier();
979 return;
980 }
981 fullstop = FULLSTOP_CLEANUP;
982 mutex_unlock(&fullstop_mutex);
983 unregister_reboot_notifier(&rcutorture_nb);
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700984 if (stutter_task) {
985 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
986 kthread_stop(stutter_task);
987 }
988 stutter_task = NULL;
Josh Triplettc8e5b162007-05-08 00:33:20 -0700989 if (shuffler_task) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800990 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
991 kthread_stop(shuffler_task);
992 }
993 shuffler_task = NULL;
994
Josh Triplettc8e5b162007-05-08 00:33:20 -0700995 if (writer_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800996 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
997 kthread_stop(writer_task);
998 }
999 writer_task = NULL;
1000
Josh Triplettc8e5b162007-05-08 00:33:20 -07001001 if (reader_tasks) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001002 for (i = 0; i < nrealreaders; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001003 if (reader_tasks[i]) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001004 VERBOSE_PRINTK_STRING(
1005 "Stopping rcu_torture_reader task");
1006 kthread_stop(reader_tasks[i]);
1007 }
1008 reader_tasks[i] = NULL;
1009 }
1010 kfree(reader_tasks);
1011 reader_tasks = NULL;
1012 }
1013 rcu_torture_current = NULL;
1014
Josh Triplettc8e5b162007-05-08 00:33:20 -07001015 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001016 for (i = 0; i < nfakewriters; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001017 if (fakewriter_tasks[i]) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001018 VERBOSE_PRINTK_STRING(
1019 "Stopping rcu_torture_fakewriter task");
1020 kthread_stop(fakewriter_tasks[i]);
1021 }
1022 fakewriter_tasks[i] = NULL;
1023 }
1024 kfree(fakewriter_tasks);
1025 fakewriter_tasks = NULL;
1026 }
1027
Josh Triplettc8e5b162007-05-08 00:33:20 -07001028 if (stats_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001029 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1030 kthread_stop(stats_task);
1031 }
1032 stats_task = NULL;
1033
1034 /* Wait for all RCU callbacks to fire. */
Paul E. McKenney23269742008-05-12 21:21:05 +02001035
1036 if (cur_ops->cb_barrier != NULL)
1037 cur_ops->cb_barrier();
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001038
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001039 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001040
Josh Triplettc8e5b162007-05-08 00:33:20 -07001041 if (cur_ops->cleanup)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001042 cur_ops->cleanup();
Paul E. McKenney95c38322006-03-24 03:15:58 -08001043 if (atomic_read(&n_rcu_torture_error))
1044 rcu_torture_print_module_parms("End of test: FAILURE");
1045 else
1046 rcu_torture_print_module_parms("End of test: SUCCESS");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001047}
1048
Josh Triplett6f8bc502007-05-08 00:25:24 -07001049static int __init
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001050rcu_torture_init(void)
1051{
1052 int i;
1053 int cpu;
1054 int firsterr = 0;
Josh Triplettade5fb82007-05-08 00:33:22 -07001055 static struct rcu_torture_ops *torture_ops[] =
1056 { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops,
Paul E. McKenney23269742008-05-12 21:21:05 +02001057 &srcu_ops, &sched_ops, &sched_ops_sync, };
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001058
Paul E. McKenney343e9092008-12-15 16:13:07 -08001059 mutex_lock(&fullstop_mutex);
1060
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001061 /* Process args and tell the world that the torturer is on the job. */
Josh Triplettade5fb82007-05-08 00:33:22 -07001062 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001063 cur_ops = torture_ops[i];
Josh Triplettade5fb82007-05-08 00:33:22 -07001064 if (strcmp(torture_type, cur_ops->name) == 0)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001065 break;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001066 }
Josh Triplettade5fb82007-05-08 00:33:22 -07001067 if (i == ARRAY_SIZE(torture_ops)) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001068 printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
1069 torture_type);
Paul E. McKenney343e9092008-12-15 16:13:07 -08001070 mutex_unlock(&fullstop_mutex);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001071 return (-EINVAL);
1072 }
Josh Triplettc8e5b162007-05-08 00:33:20 -07001073 if (cur_ops->init)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001074 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1075
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001076 if (nreaders >= 0)
1077 nrealreaders = nreaders;
1078 else
1079 nrealreaders = 2 * num_online_cpus();
Paul E. McKenney95c38322006-03-24 03:15:58 -08001080 rcu_torture_print_module_parms("Start of test");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001081 fullstop = 0;
1082
1083 /* Set up the freelist. */
1084
1085 INIT_LIST_HEAD(&rcu_torture_freelist);
Ahmed S. Darwish788e7702007-05-08 00:33:14 -07001086 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
Paul E. McKenney996417d2005-11-18 01:10:50 -08001087 rcu_tortures[i].rtort_mbtest = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001088 list_add_tail(&rcu_tortures[i].rtort_free,
1089 &rcu_torture_freelist);
1090 }
1091
1092 /* Initialize the statistics so that each run gets its own numbers. */
1093
1094 rcu_torture_current = NULL;
1095 rcu_torture_current_version = 0;
1096 atomic_set(&n_rcu_torture_alloc, 0);
1097 atomic_set(&n_rcu_torture_alloc_fail, 0);
1098 atomic_set(&n_rcu_torture_free, 0);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001099 atomic_set(&n_rcu_torture_mberror, 0);
1100 atomic_set(&n_rcu_torture_error, 0);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001101 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1102 atomic_set(&rcu_torture_wcount[i], 0);
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001103 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001104 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1105 per_cpu(rcu_torture_count, cpu)[i] = 0;
1106 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1107 }
1108 }
1109
1110 /* Start up the kthreads. */
1111
1112 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1113 writer_task = kthread_run(rcu_torture_writer, NULL,
1114 "rcu_torture_writer");
1115 if (IS_ERR(writer_task)) {
1116 firsterr = PTR_ERR(writer_task);
1117 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1118 writer_task = NULL;
1119 goto unwind;
1120 }
Josh Triplettb772e1d2006-10-04 02:17:13 -07001121 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1122 GFP_KERNEL);
1123 if (fakewriter_tasks == NULL) {
1124 VERBOSE_PRINTK_ERRSTRING("out of memory");
1125 firsterr = -ENOMEM;
1126 goto unwind;
1127 }
1128 for (i = 0; i < nfakewriters; i++) {
1129 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1130 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1131 "rcu_torture_fakewriter");
1132 if (IS_ERR(fakewriter_tasks[i])) {
1133 firsterr = PTR_ERR(fakewriter_tasks[i]);
1134 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1135 fakewriter_tasks[i] = NULL;
1136 goto unwind;
1137 }
1138 }
Josh Triplett2860aab2006-10-04 02:17:11 -07001139 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001140 GFP_KERNEL);
1141 if (reader_tasks == NULL) {
1142 VERBOSE_PRINTK_ERRSTRING("out of memory");
1143 firsterr = -ENOMEM;
1144 goto unwind;
1145 }
1146 for (i = 0; i < nrealreaders; i++) {
1147 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1148 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1149 "rcu_torture_reader");
1150 if (IS_ERR(reader_tasks[i])) {
1151 firsterr = PTR_ERR(reader_tasks[i]);
1152 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1153 reader_tasks[i] = NULL;
1154 goto unwind;
1155 }
1156 }
1157 if (stat_interval > 0) {
1158 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1159 stats_task = kthread_run(rcu_torture_stats, NULL,
1160 "rcu_torture_stats");
1161 if (IS_ERR(stats_task)) {
1162 firsterr = PTR_ERR(stats_task);
1163 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1164 stats_task = NULL;
1165 goto unwind;
1166 }
1167 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001168 if (test_no_idle_hz) {
1169 rcu_idle_cpu = num_online_cpus() - 1;
1170 /* Create the shuffler thread */
1171 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1172 "rcu_torture_shuffle");
1173 if (IS_ERR(shuffler_task)) {
1174 firsterr = PTR_ERR(shuffler_task);
1175 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1176 shuffler_task = NULL;
1177 goto unwind;
1178 }
1179 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001180 if (stutter < 0)
1181 stutter = 0;
1182 if (stutter) {
1183 /* Create the stutter thread */
1184 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1185 "rcu_torture_stutter");
1186 if (IS_ERR(stutter_task)) {
1187 firsterr = PTR_ERR(stutter_task);
1188 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1189 stutter_task = NULL;
1190 goto unwind;
1191 }
1192 }
Paul E. McKenney343e9092008-12-15 16:13:07 -08001193 register_reboot_notifier(&rcutorture_nb);
1194 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001195 return 0;
1196
1197unwind:
Paul E. McKenney343e9092008-12-15 16:13:07 -08001198 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001199 rcu_torture_cleanup();
1200 return firsterr;
1201}
1202
1203module_init(rcu_torture_init);
1204module_exit(rcu_torture_cleanup);