blob: 7c4142a79f0ab5a2ef80300b3d5be3188bdec8c7 [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. McKenneyc9d557c2009-01-07 14:33:30 -0800139/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
140
141#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
142#define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
143#define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
144static int fullstop = FULLSTOP_RMMOD;
145DEFINE_MUTEX(fullstop_mutex); /* Protect fullstop transitions and spawning */
146 /* of kthreads. */
Paul E. McKenney343e9092008-12-15 16:13:07 -0800147
148/*
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800149 * Detect and respond to a system shutdown.
Paul E. McKenney343e9092008-12-15 16:13:07 -0800150 */
151static int
152rcutorture_shutdown_notify(struct notifier_block *unused1,
153 unsigned long unused2, void *unused3)
154{
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800155 mutex_lock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800156 if (fullstop == FULLSTOP_DONTSTOP)
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800157 fullstop = FULLSTOP_SHUTDOWN;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800158 else
159 printk(KERN_WARNING /* but going down anyway, so... */
160 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800161 mutex_unlock(&fullstop_mutex);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800162 return NOTIFY_DONE;
163}
164
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800165/*
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800166 * Absorb kthreads into a kernel function that won't return, so that
167 * they won't ever access module text or data again.
168 */
169static void rcutorture_shutdown_absorb(char *title)
170{
171 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
172 printk(KERN_NOTICE
173 "rcutorture thread %s parking due to system shutdown\n",
174 title);
175 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
176 }
177}
178
179/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800180 * Allocate an element from the rcu_tortures pool.
181 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800182static struct rcu_torture *
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800183rcu_torture_alloc(void)
184{
185 struct list_head *p;
186
Ingo Molnaradac1662006-01-25 19:50:12 +0100187 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800188 if (list_empty(&rcu_torture_freelist)) {
189 atomic_inc(&n_rcu_torture_alloc_fail);
Ingo Molnaradac1662006-01-25 19:50:12 +0100190 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800191 return NULL;
192 }
193 atomic_inc(&n_rcu_torture_alloc);
194 p = rcu_torture_freelist.next;
195 list_del_init(p);
Ingo Molnaradac1662006-01-25 19:50:12 +0100196 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800197 return container_of(p, struct rcu_torture, rtort_free);
198}
199
200/*
201 * Free an element to the rcu_tortures pool.
202 */
203static void
204rcu_torture_free(struct rcu_torture *p)
205{
206 atomic_inc(&n_rcu_torture_free);
Ingo Molnaradac1662006-01-25 19:50:12 +0100207 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800208 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
Ingo Molnaradac1662006-01-25 19:50:12 +0100209 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800210}
211
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800212struct rcu_random_state {
213 unsigned long rrs_state;
Josh Triplett75cfef32006-10-04 02:17:12 -0700214 long rrs_count;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800215};
216
217#define RCU_RANDOM_MULT 39916801 /* prime */
218#define RCU_RANDOM_ADD 479001701 /* prime */
219#define RCU_RANDOM_REFRESH 10000
220
221#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
222
223/*
224 * Crude but fast random-number generator. Uses a linear congruential
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700225 * generator, with occasional help from cpu_clock().
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800226 */
Josh Triplett75cfef32006-10-04 02:17:12 -0700227static unsigned long
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800228rcu_random(struct rcu_random_state *rrsp)
229{
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800230 if (--rrsp->rrs_count < 0) {
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700231 rrsp->rrs_state +=
232 (unsigned long)cpu_clock(raw_smp_processor_id());
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800233 rrsp->rrs_count = RCU_RANDOM_REFRESH;
234 }
235 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
236 return swahw32(rrsp->rrs_state);
237}
238
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700239static void
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800240rcu_stutter_wait(char *title)
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700241{
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800242 while (stutter_pause_test || !rcutorture_runnable) {
Paul E. McKenneye3d7be22008-06-22 13:06:38 -0700243 if (rcutorture_runnable)
244 schedule_timeout_interruptible(1);
245 else
Paul E. McKenney3ccf79f2008-06-22 14:02:55 -0700246 schedule_timeout_interruptible(round_jiffies_relative(HZ));
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800247 rcutorture_shutdown_absorb(title);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800248 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700249}
250
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800251/*
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700252 * Operations vector for selecting different types of tests.
253 */
254
255struct rcu_torture_ops {
256 void (*init)(void);
257 void (*cleanup)(void);
258 int (*readlock)(void);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700259 void (*readdelay)(struct rcu_random_state *rrsp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700260 void (*readunlock)(int idx);
261 int (*completed)(void);
262 void (*deferredfree)(struct rcu_torture *p);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700263 void (*sync)(void);
Paul E. McKenney23269742008-05-12 21:21:05 +0200264 void (*cb_barrier)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700265 int (*stats)(char *page);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700266 int irqcapable;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700267 char *name;
268};
269static struct rcu_torture_ops *cur_ops = NULL;
270
271/*
272 * Definitions for rcu torture testing.
273 */
274
Josh Tripletta49a4af2006-09-29 01:59:30 -0700275static int rcu_torture_read_lock(void) __acquires(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700276{
277 rcu_read_lock();
278 return 0;
279}
280
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700281static void rcu_read_delay(struct rcu_random_state *rrsp)
282{
283 long delay;
284 const long longdelay = 200;
285
286 /* We want there to be long-running readers, but not all the time. */
287
288 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay);
289 if (!delay)
290 udelay(longdelay);
291}
292
Josh Tripletta49a4af2006-09-29 01:59:30 -0700293static void rcu_torture_read_unlock(int idx) __releases(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700294{
295 rcu_read_unlock();
296}
297
298static int rcu_torture_completed(void)
299{
300 return rcu_batches_completed();
301}
302
303static void
304rcu_torture_cb(struct rcu_head *p)
305{
306 int i;
307 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
308
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800309 if (fullstop != FULLSTOP_DONTSTOP) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700310 /* Test is ending, just drop callbacks on the floor. */
311 /* The next initialization will pick up the pieces. */
312 return;
313 }
314 i = rp->rtort_pipe_count;
315 if (i > RCU_TORTURE_PIPE_LEN)
316 i = RCU_TORTURE_PIPE_LEN;
317 atomic_inc(&rcu_torture_wcount[i]);
318 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
319 rp->rtort_mbtest = 0;
320 rcu_torture_free(rp);
321 } else
322 cur_ops->deferredfree(rp);
323}
324
325static void rcu_torture_deferred_free(struct rcu_torture *p)
326{
327 call_rcu(&p->rtort_rcu, rcu_torture_cb);
328}
329
330static struct rcu_torture_ops rcu_ops = {
331 .init = NULL,
332 .cleanup = NULL,
333 .readlock = rcu_torture_read_lock,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700334 .readdelay = rcu_read_delay,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700335 .readunlock = rcu_torture_read_unlock,
336 .completed = rcu_torture_completed,
337 .deferredfree = rcu_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700338 .sync = synchronize_rcu,
Paul E. McKenney23269742008-05-12 21:21:05 +0200339 .cb_barrier = rcu_barrier,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700340 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700341 .irqcapable = 1,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700342 .name = "rcu"
343};
344
Josh Triplette3033732006-10-04 02:17:14 -0700345static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
346{
347 int i;
348 struct rcu_torture *rp;
349 struct rcu_torture *rp1;
350
351 cur_ops->sync();
352 list_add(&p->rtort_free, &rcu_torture_removed);
353 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
354 i = rp->rtort_pipe_count;
355 if (i > RCU_TORTURE_PIPE_LEN)
356 i = RCU_TORTURE_PIPE_LEN;
357 atomic_inc(&rcu_torture_wcount[i]);
358 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
359 rp->rtort_mbtest = 0;
360 list_del(&rp->rtort_free);
361 rcu_torture_free(rp);
362 }
363 }
364}
365
366static void rcu_sync_torture_init(void)
367{
368 INIT_LIST_HEAD(&rcu_torture_removed);
369}
370
Josh Triplett20d2e422006-10-04 02:17:15 -0700371static struct rcu_torture_ops rcu_sync_ops = {
372 .init = rcu_sync_torture_init,
373 .cleanup = NULL,
374 .readlock = rcu_torture_read_lock,
375 .readdelay = rcu_read_delay,
376 .readunlock = rcu_torture_read_unlock,
377 .completed = rcu_torture_completed,
378 .deferredfree = rcu_sync_torture_deferred_free,
379 .sync = synchronize_rcu,
Paul E. McKenney23269742008-05-12 21:21:05 +0200380 .cb_barrier = NULL,
Josh Triplett20d2e422006-10-04 02:17:15 -0700381 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700382 .irqcapable = 1,
Josh Triplett20d2e422006-10-04 02:17:15 -0700383 .name = "rcu_sync"
384};
385
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700386/*
387 * Definitions for rcu_bh torture testing.
388 */
389
Josh Tripletta49a4af2006-09-29 01:59:30 -0700390static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700391{
392 rcu_read_lock_bh();
393 return 0;
394}
395
Josh Tripletta49a4af2006-09-29 01:59:30 -0700396static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700397{
398 rcu_read_unlock_bh();
399}
400
401static int rcu_bh_torture_completed(void)
402{
403 return rcu_batches_completed_bh();
404}
405
406static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
407{
408 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
409}
410
Josh Triplettb772e1d2006-10-04 02:17:13 -0700411struct rcu_bh_torture_synchronize {
412 struct rcu_head head;
413 struct completion completion;
414};
415
416static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
417{
418 struct rcu_bh_torture_synchronize *rcu;
419
420 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
421 complete(&rcu->completion);
422}
423
424static void rcu_bh_torture_synchronize(void)
425{
426 struct rcu_bh_torture_synchronize rcu;
427
428 init_completion(&rcu.completion);
429 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
430 wait_for_completion(&rcu.completion);
431}
432
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700433static struct rcu_torture_ops rcu_bh_ops = {
434 .init = NULL,
435 .cleanup = NULL,
436 .readlock = rcu_bh_torture_read_lock,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700437 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700438 .readunlock = rcu_bh_torture_read_unlock,
439 .completed = rcu_bh_torture_completed,
440 .deferredfree = rcu_bh_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700441 .sync = rcu_bh_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200442 .cb_barrier = rcu_barrier_bh,
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700443 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700444 .irqcapable = 1,
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700445 .name = "rcu_bh"
446};
447
Josh Triplett11a14702006-10-04 02:17:16 -0700448static struct rcu_torture_ops rcu_bh_sync_ops = {
449 .init = rcu_sync_torture_init,
450 .cleanup = NULL,
451 .readlock = rcu_bh_torture_read_lock,
452 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
453 .readunlock = rcu_bh_torture_read_unlock,
454 .completed = rcu_bh_torture_completed,
455 .deferredfree = rcu_sync_torture_deferred_free,
456 .sync = rcu_bh_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200457 .cb_barrier = NULL,
Josh Triplett11a14702006-10-04 02:17:16 -0700458 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700459 .irqcapable = 1,
Josh Triplett11a14702006-10-04 02:17:16 -0700460 .name = "rcu_bh_sync"
461};
462
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700463/*
464 * Definitions for srcu torture testing.
465 */
466
467static struct srcu_struct srcu_ctl;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700468
469static void srcu_torture_init(void)
470{
471 init_srcu_struct(&srcu_ctl);
Josh Triplette3033732006-10-04 02:17:14 -0700472 rcu_sync_torture_init();
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700473}
474
475static void srcu_torture_cleanup(void)
476{
477 synchronize_srcu(&srcu_ctl);
478 cleanup_srcu_struct(&srcu_ctl);
479}
480
Josh Triplett012d3ca2006-12-06 20:40:19 -0800481static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700482{
483 return srcu_read_lock(&srcu_ctl);
484}
485
486static void srcu_read_delay(struct rcu_random_state *rrsp)
487{
488 long delay;
489 const long uspertick = 1000000 / HZ;
490 const long longdelay = 10;
491
492 /* We want there to be long-running readers, but not all the time. */
493
494 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
495 if (!delay)
496 schedule_timeout_interruptible(longdelay);
497}
498
Josh Triplett012d3ca2006-12-06 20:40:19 -0800499static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700500{
501 srcu_read_unlock(&srcu_ctl, idx);
502}
503
504static int srcu_torture_completed(void)
505{
506 return srcu_batches_completed(&srcu_ctl);
507}
508
Josh Triplettb772e1d2006-10-04 02:17:13 -0700509static void srcu_torture_synchronize(void)
510{
511 synchronize_srcu(&srcu_ctl);
512}
513
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700514static int srcu_torture_stats(char *page)
515{
516 int cnt = 0;
517 int cpu;
518 int idx = srcu_ctl.completed & 0x1;
519
520 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
521 torture_type, TORTURE_FLAG, idx);
522 for_each_possible_cpu(cpu) {
523 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
524 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
525 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
526 }
527 cnt += sprintf(&page[cnt], "\n");
528 return cnt;
529}
530
531static struct rcu_torture_ops srcu_ops = {
532 .init = srcu_torture_init,
533 .cleanup = srcu_torture_cleanup,
534 .readlock = srcu_torture_read_lock,
535 .readdelay = srcu_read_delay,
536 .readunlock = srcu_torture_read_unlock,
537 .completed = srcu_torture_completed,
Josh Triplette3033732006-10-04 02:17:14 -0700538 .deferredfree = rcu_sync_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700539 .sync = srcu_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200540 .cb_barrier = NULL,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700541 .stats = srcu_torture_stats,
542 .name = "srcu"
543};
544
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700545/*
546 * Definitions for sched torture testing.
547 */
548
549static int sched_torture_read_lock(void)
550{
551 preempt_disable();
552 return 0;
553}
554
555static void sched_torture_read_unlock(int idx)
556{
557 preempt_enable();
558}
559
560static int sched_torture_completed(void)
561{
562 return 0;
563}
564
Paul E. McKenney23269742008-05-12 21:21:05 +0200565static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
566{
567 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
568}
569
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700570static void sched_torture_synchronize(void)
571{
572 synchronize_sched();
573}
574
575static struct rcu_torture_ops sched_ops = {
576 .init = rcu_sync_torture_init,
577 .cleanup = NULL,
578 .readlock = sched_torture_read_lock,
579 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
580 .readunlock = sched_torture_read_unlock,
581 .completed = sched_torture_completed,
Paul E. McKenney23269742008-05-12 21:21:05 +0200582 .deferredfree = rcu_sched_torture_deferred_free,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700583 .sync = sched_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200584 .cb_barrier = rcu_barrier_sched,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700585 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700586 .irqcapable = 1,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700587 .name = "sched"
588};
589
Paul E. McKenney23269742008-05-12 21:21:05 +0200590static struct rcu_torture_ops sched_ops_sync = {
591 .init = rcu_sync_torture_init,
592 .cleanup = NULL,
593 .readlock = sched_torture_read_lock,
594 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
595 .readunlock = sched_torture_read_unlock,
596 .completed = sched_torture_completed,
597 .deferredfree = rcu_sync_torture_deferred_free,
598 .sync = sched_torture_synchronize,
599 .cb_barrier = NULL,
600 .stats = NULL,
601 .name = "sched_sync"
602};
603
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700604/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800605 * RCU torture writer kthread. Repeatedly substitutes a new structure
606 * for that pointed to by rcu_torture_current, freeing the old structure
607 * after a series of grace periods (the "pipeline").
608 */
609static int
610rcu_torture_writer(void *arg)
611{
612 int i;
613 long oldbatch = rcu_batches_completed();
614 struct rcu_torture *rp;
615 struct rcu_torture *old_rp;
616 static DEFINE_RCU_RANDOM(rand);
617
618 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800619 set_user_nice(current, 19);
620
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800621 do {
622 schedule_timeout_uninterruptible(1);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800623 if ((rp = rcu_torture_alloc()) == NULL)
624 continue;
625 rp->rtort_pipe_count = 0;
626 udelay(rcu_random(&rand) & 0x3ff);
627 old_rp = rcu_torture_current;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800628 rp->rtort_mbtest = 1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800629 rcu_assign_pointer(rcu_torture_current, rp);
630 smp_wmb();
Josh Triplettc8e5b162007-05-08 00:33:20 -0700631 if (old_rp) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800632 i = old_rp->rtort_pipe_count;
633 if (i > RCU_TORTURE_PIPE_LEN)
634 i = RCU_TORTURE_PIPE_LEN;
635 atomic_inc(&rcu_torture_wcount[i]);
636 old_rp->rtort_pipe_count++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700637 cur_ops->deferredfree(old_rp);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800638 }
639 rcu_torture_current_version++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700640 oldbatch = cur_ops->completed();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800641 rcu_stutter_wait("rcu_torture_writer");
642 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800643 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800644 rcutorture_shutdown_absorb("rcu_torture_writer");
645 while (!kthread_should_stop())
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800646 schedule_timeout_uninterruptible(1);
647 return 0;
648}
649
650/*
Josh Triplettb772e1d2006-10-04 02:17:13 -0700651 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
652 * delay between calls.
653 */
654static int
655rcu_torture_fakewriter(void *arg)
656{
657 DEFINE_RCU_RANDOM(rand);
658
659 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
660 set_user_nice(current, 19);
661
662 do {
663 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
664 udelay(rcu_random(&rand) & 0x3ff);
665 cur_ops->sync();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800666 rcu_stutter_wait("rcu_torture_fakewriter");
667 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700668
669 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800670 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
671 while (!kthread_should_stop())
Josh Triplettb772e1d2006-10-04 02:17:13 -0700672 schedule_timeout_uninterruptible(1);
673 return 0;
674}
675
676/*
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700677 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
678 * incrementing the corresponding element of the pipeline array. The
679 * counter in the element should never be greater than 1, otherwise, the
680 * RCU implementation is broken.
681 */
682static void rcu_torture_timer(unsigned long unused)
683{
684 int idx;
685 int completed;
686 static DEFINE_RCU_RANDOM(rand);
687 static DEFINE_SPINLOCK(rand_lock);
688 struct rcu_torture *p;
689 int pipe_count;
690
691 idx = cur_ops->readlock();
692 completed = cur_ops->completed();
693 p = rcu_dereference(rcu_torture_current);
694 if (p == NULL) {
695 /* Leave because rcu_torture_writer is not yet underway */
696 cur_ops->readunlock(idx);
697 return;
698 }
699 if (p->rtort_mbtest == 0)
700 atomic_inc(&n_rcu_torture_mberror);
701 spin_lock(&rand_lock);
702 cur_ops->readdelay(&rand);
703 n_rcu_torture_timers++;
704 spin_unlock(&rand_lock);
705 preempt_disable();
706 pipe_count = p->rtort_pipe_count;
707 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
708 /* Should not happen, but... */
709 pipe_count = RCU_TORTURE_PIPE_LEN;
710 }
711 ++__get_cpu_var(rcu_torture_count)[pipe_count];
712 completed = cur_ops->completed() - completed;
713 if (completed > RCU_TORTURE_PIPE_LEN) {
714 /* Should not happen, but... */
715 completed = RCU_TORTURE_PIPE_LEN;
716 }
717 ++__get_cpu_var(rcu_torture_batch)[completed];
718 preempt_enable();
719 cur_ops->readunlock(idx);
720}
721
722/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800723 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
724 * incrementing the corresponding element of the pipeline array. The
725 * counter in the element should never be greater than 1, otherwise, the
726 * RCU implementation is broken.
727 */
728static int
729rcu_torture_reader(void *arg)
730{
731 int completed;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700732 int idx;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800733 DEFINE_RCU_RANDOM(rand);
734 struct rcu_torture *p;
735 int pipe_count;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700736 struct timer_list t;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800737
738 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800739 set_user_nice(current, 19);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700740 if (irqreader && cur_ops->irqcapable)
741 setup_timer_on_stack(&t, rcu_torture_timer, 0);
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800742
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800743 do {
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700744 if (irqreader && cur_ops->irqcapable) {
745 if (!timer_pending(&t))
746 mod_timer(&t, 1);
747 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700748 idx = cur_ops->readlock();
749 completed = cur_ops->completed();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800750 p = rcu_dereference(rcu_torture_current);
751 if (p == NULL) {
752 /* Wait for rcu_torture_writer to get underway */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700753 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800754 schedule_timeout_interruptible(HZ);
755 continue;
756 }
Paul E. McKenney996417d2005-11-18 01:10:50 -0800757 if (p->rtort_mbtest == 0)
758 atomic_inc(&n_rcu_torture_mberror);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700759 cur_ops->readdelay(&rand);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800760 preempt_disable();
761 pipe_count = p->rtort_pipe_count;
762 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
763 /* Should not happen, but... */
764 pipe_count = RCU_TORTURE_PIPE_LEN;
765 }
766 ++__get_cpu_var(rcu_torture_count)[pipe_count];
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700767 completed = cur_ops->completed() - completed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800768 if (completed > RCU_TORTURE_PIPE_LEN) {
769 /* Should not happen, but... */
770 completed = RCU_TORTURE_PIPE_LEN;
771 }
772 ++__get_cpu_var(rcu_torture_batch)[completed];
773 preempt_enable();
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700774 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800775 schedule();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800776 rcu_stutter_wait("rcu_torture_reader");
777 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800778 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800779 rcutorture_shutdown_absorb("rcu_torture_reader");
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700780 if (irqreader && cur_ops->irqcapable)
781 del_timer_sync(&t);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800782 while (!kthread_should_stop())
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800783 schedule_timeout_uninterruptible(1);
784 return 0;
785}
786
787/*
788 * Create an RCU-torture statistics message in the specified buffer.
789 */
790static int
791rcu_torture_printk(char *page)
792{
793 int cnt = 0;
794 int cpu;
795 int i;
796 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
797 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
798
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800799 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800800 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
801 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
802 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
803 }
804 }
805 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
806 if (pipesummary[i] != 0)
807 break;
808 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700809 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800810 cnt += sprintf(&page[cnt],
Paul E. McKenney996417d2005-11-18 01:10:50 -0800811 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700812 "rtmbe: %d nt: %ld",
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800813 rcu_torture_current,
814 rcu_torture_current_version,
815 list_empty(&rcu_torture_freelist),
816 atomic_read(&n_rcu_torture_alloc),
817 atomic_read(&n_rcu_torture_alloc_fail),
Paul E. McKenney996417d2005-11-18 01:10:50 -0800818 atomic_read(&n_rcu_torture_free),
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700819 atomic_read(&n_rcu_torture_mberror),
820 n_rcu_torture_timers);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800821 if (atomic_read(&n_rcu_torture_mberror) != 0)
822 cnt += sprintf(&page[cnt], " !!!");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700823 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800824 if (i > 1) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800825 cnt += sprintf(&page[cnt], "!!! ");
Paul E. McKenney996417d2005-11-18 01:10:50 -0800826 atomic_inc(&n_rcu_torture_error);
Ingo Molnar5af970a2008-06-18 10:09:48 +0200827 WARN_ON_ONCE(1);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800828 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800829 cnt += sprintf(&page[cnt], "Reader Pipe: ");
830 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
831 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700832 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800833 cnt += sprintf(&page[cnt], "Reader Batch: ");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700834 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800835 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700836 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800837 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
838 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
839 cnt += sprintf(&page[cnt], " %d",
840 atomic_read(&rcu_torture_wcount[i]));
841 }
842 cnt += sprintf(&page[cnt], "\n");
Josh Triplettc8e5b162007-05-08 00:33:20 -0700843 if (cur_ops->stats)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700844 cnt += cur_ops->stats(&page[cnt]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800845 return cnt;
846}
847
848/*
849 * Print torture statistics. Caller must ensure that there is only
850 * one call to this function at a given time!!! This is normally
851 * accomplished by relying on the module system to only have one copy
852 * of the module loaded, and then by giving the rcu_torture_stats
853 * kthread full control (or the init/cleanup functions when rcu_torture_stats
854 * thread is not running).
855 */
856static void
857rcu_torture_stats_print(void)
858{
859 int cnt;
860
861 cnt = rcu_torture_printk(printk_buf);
862 printk(KERN_ALERT "%s", printk_buf);
863}
864
865/*
866 * Periodically prints torture statistics, if periodic statistics printing
867 * was specified via the stat_interval module parameter.
868 *
869 * No need to worry about fullstop here, since this one doesn't reference
870 * volatile state or register callbacks.
871 */
872static int
873rcu_torture_stats(void *arg)
874{
875 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
876 do {
877 schedule_timeout_interruptible(stat_interval * HZ);
878 rcu_torture_stats_print();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800879 rcutorture_shutdown_absorb("rcu_torture_stats");
880 } while (!kthread_should_stop());
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800881 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
882 return 0;
883}
884
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800885static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
886
887/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
888 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
889 */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700890static void rcu_torture_shuffle_tasks(void)
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800891{
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800892 cpumask_t tmp_mask;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800893 int i;
894
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800895 cpus_setall(tmp_mask);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100896 get_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800897
898 /* No point in shuffling if there is only one online CPU (ex: UP) */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800899 if (num_online_cpus() == 1) {
900 put_online_cpus();
901 return;
902 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800903
904 if (rcu_idle_cpu != -1)
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800905 cpu_clear(rcu_idle_cpu, tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800906
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800907 set_cpus_allowed_ptr(current, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800908
Josh Triplettc8e5b162007-05-08 00:33:20 -0700909 if (reader_tasks) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800910 for (i = 0; i < nrealreaders; i++)
911 if (reader_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -0700912 set_cpus_allowed_ptr(reader_tasks[i],
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800913 &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800914 }
915
Josh Triplettc8e5b162007-05-08 00:33:20 -0700916 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -0700917 for (i = 0; i < nfakewriters; i++)
918 if (fakewriter_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -0700919 set_cpus_allowed_ptr(fakewriter_tasks[i],
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800920 &tmp_mask);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700921 }
922
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800923 if (writer_task)
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800924 set_cpus_allowed_ptr(writer_task, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800925
926 if (stats_task)
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800927 set_cpus_allowed_ptr(stats_task, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800928
929 if (rcu_idle_cpu == -1)
930 rcu_idle_cpu = num_online_cpus() - 1;
931 else
932 rcu_idle_cpu--;
933
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100934 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800935}
936
937/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
938 * system to become idle at a time and cut off its timer ticks. This is meant
939 * to test the support for such tickless idle CPU in RCU.
940 */
941static int
942rcu_torture_shuffle(void *arg)
943{
944 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
945 do {
946 schedule_timeout_interruptible(shuffle_interval * HZ);
947 rcu_torture_shuffle_tasks();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800948 rcutorture_shutdown_absorb("rcu_torture_shuffle");
949 } while (!kthread_should_stop());
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800950 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
951 return 0;
952}
953
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700954/* Cause the rcutorture test to "stutter", starting and stopping all
955 * threads periodically.
956 */
957static int
958rcu_torture_stutter(void *arg)
959{
960 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
961 do {
962 schedule_timeout_interruptible(stutter * HZ);
963 stutter_pause_test = 1;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800964 if (!kthread_should_stop())
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700965 schedule_timeout_interruptible(stutter * HZ);
966 stutter_pause_test = 0;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800967 rcutorture_shutdown_absorb("rcu_torture_stutter");
968 } while (!kthread_should_stop());
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700969 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
970 return 0;
971}
972
Paul E. McKenney95c38322006-03-24 03:15:58 -0800973static inline void
974rcu_torture_print_module_parms(char *tag)
975{
Josh Triplettb772e1d2006-10-04 02:17:13 -0700976 printk(KERN_ALERT "%s" TORTURE_FLAG
977 "--- %s: nreaders=%d nfakewriters=%d "
Paul E. McKenney95c38322006-03-24 03:15:58 -0800978 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700979 "shuffle_interval=%d stutter=%d irqreader=%d\n",
Josh Triplettb772e1d2006-10-04 02:17:13 -0700980 torture_type, tag, nrealreaders, nfakewriters,
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700981 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700982 stutter, irqreader);
Paul E. McKenney95c38322006-03-24 03:15:58 -0800983}
984
Paul E. McKenney343e9092008-12-15 16:13:07 -0800985static struct notifier_block rcutorture_nb = {
986 .notifier_call = rcutorture_shutdown_notify,
987};
988
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800989static void
990rcu_torture_cleanup(void)
991{
992 int i;
993
Paul E. McKenney343e9092008-12-15 16:13:07 -0800994 mutex_lock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800995 if (fullstop == FULLSTOP_SHUTDOWN) {
996 printk(KERN_WARNING /* but going down anyway, so... */
997 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
Paul E. McKenney343e9092008-12-15 16:13:07 -0800998 mutex_unlock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800999 schedule_timeout_uninterruptible(10);
Paul E. McKenney343e9092008-12-15 16:13:07 -08001000 if (cur_ops->cb_barrier != NULL)
1001 cur_ops->cb_barrier();
1002 return;
1003 }
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001004 fullstop = FULLSTOP_RMMOD;
Paul E. McKenney343e9092008-12-15 16:13:07 -08001005 mutex_unlock(&fullstop_mutex);
1006 unregister_reboot_notifier(&rcutorture_nb);
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001007 if (stutter_task) {
1008 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1009 kthread_stop(stutter_task);
1010 }
1011 stutter_task = NULL;
Josh Triplettc8e5b162007-05-08 00:33:20 -07001012 if (shuffler_task) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001013 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1014 kthread_stop(shuffler_task);
1015 }
1016 shuffler_task = NULL;
1017
Josh Triplettc8e5b162007-05-08 00:33:20 -07001018 if (writer_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001019 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1020 kthread_stop(writer_task);
1021 }
1022 writer_task = NULL;
1023
Josh Triplettc8e5b162007-05-08 00:33:20 -07001024 if (reader_tasks) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001025 for (i = 0; i < nrealreaders; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001026 if (reader_tasks[i]) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001027 VERBOSE_PRINTK_STRING(
1028 "Stopping rcu_torture_reader task");
1029 kthread_stop(reader_tasks[i]);
1030 }
1031 reader_tasks[i] = NULL;
1032 }
1033 kfree(reader_tasks);
1034 reader_tasks = NULL;
1035 }
1036 rcu_torture_current = NULL;
1037
Josh Triplettc8e5b162007-05-08 00:33:20 -07001038 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001039 for (i = 0; i < nfakewriters; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001040 if (fakewriter_tasks[i]) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001041 VERBOSE_PRINTK_STRING(
1042 "Stopping rcu_torture_fakewriter task");
1043 kthread_stop(fakewriter_tasks[i]);
1044 }
1045 fakewriter_tasks[i] = NULL;
1046 }
1047 kfree(fakewriter_tasks);
1048 fakewriter_tasks = NULL;
1049 }
1050
Josh Triplettc8e5b162007-05-08 00:33:20 -07001051 if (stats_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001052 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1053 kthread_stop(stats_task);
1054 }
1055 stats_task = NULL;
1056
1057 /* Wait for all RCU callbacks to fire. */
Paul E. McKenney23269742008-05-12 21:21:05 +02001058
1059 if (cur_ops->cb_barrier != NULL)
1060 cur_ops->cb_barrier();
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001061
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001062 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001063
Josh Triplettc8e5b162007-05-08 00:33:20 -07001064 if (cur_ops->cleanup)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001065 cur_ops->cleanup();
Paul E. McKenney95c38322006-03-24 03:15:58 -08001066 if (atomic_read(&n_rcu_torture_error))
1067 rcu_torture_print_module_parms("End of test: FAILURE");
1068 else
1069 rcu_torture_print_module_parms("End of test: SUCCESS");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001070}
1071
Josh Triplett6f8bc502007-05-08 00:25:24 -07001072static int __init
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001073rcu_torture_init(void)
1074{
1075 int i;
1076 int cpu;
1077 int firsterr = 0;
Josh Triplettade5fb82007-05-08 00:33:22 -07001078 static struct rcu_torture_ops *torture_ops[] =
1079 { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops,
Paul E. McKenney23269742008-05-12 21:21:05 +02001080 &srcu_ops, &sched_ops, &sched_ops_sync, };
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001081
Paul E. McKenney343e9092008-12-15 16:13:07 -08001082 mutex_lock(&fullstop_mutex);
1083
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001084 /* Process args and tell the world that the torturer is on the job. */
Josh Triplettade5fb82007-05-08 00:33:22 -07001085 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001086 cur_ops = torture_ops[i];
Josh Triplettade5fb82007-05-08 00:33:22 -07001087 if (strcmp(torture_type, cur_ops->name) == 0)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001088 break;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001089 }
Josh Triplettade5fb82007-05-08 00:33:22 -07001090 if (i == ARRAY_SIZE(torture_ops)) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001091 printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
1092 torture_type);
Paul E. McKenney343e9092008-12-15 16:13:07 -08001093 mutex_unlock(&fullstop_mutex);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001094 return (-EINVAL);
1095 }
Josh Triplettc8e5b162007-05-08 00:33:20 -07001096 if (cur_ops->init)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001097 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1098
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001099 if (nreaders >= 0)
1100 nrealreaders = nreaders;
1101 else
1102 nrealreaders = 2 * num_online_cpus();
Paul E. McKenney95c38322006-03-24 03:15:58 -08001103 rcu_torture_print_module_parms("Start of test");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001104 fullstop = FULLSTOP_DONTSTOP;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001105
1106 /* Set up the freelist. */
1107
1108 INIT_LIST_HEAD(&rcu_torture_freelist);
Ahmed S. Darwish788e7702007-05-08 00:33:14 -07001109 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
Paul E. McKenney996417d2005-11-18 01:10:50 -08001110 rcu_tortures[i].rtort_mbtest = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001111 list_add_tail(&rcu_tortures[i].rtort_free,
1112 &rcu_torture_freelist);
1113 }
1114
1115 /* Initialize the statistics so that each run gets its own numbers. */
1116
1117 rcu_torture_current = NULL;
1118 rcu_torture_current_version = 0;
1119 atomic_set(&n_rcu_torture_alloc, 0);
1120 atomic_set(&n_rcu_torture_alloc_fail, 0);
1121 atomic_set(&n_rcu_torture_free, 0);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001122 atomic_set(&n_rcu_torture_mberror, 0);
1123 atomic_set(&n_rcu_torture_error, 0);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001124 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1125 atomic_set(&rcu_torture_wcount[i], 0);
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001126 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001127 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1128 per_cpu(rcu_torture_count, cpu)[i] = 0;
1129 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1130 }
1131 }
1132
1133 /* Start up the kthreads. */
1134
1135 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1136 writer_task = kthread_run(rcu_torture_writer, NULL,
1137 "rcu_torture_writer");
1138 if (IS_ERR(writer_task)) {
1139 firsterr = PTR_ERR(writer_task);
1140 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1141 writer_task = NULL;
1142 goto unwind;
1143 }
Josh Triplettb772e1d2006-10-04 02:17:13 -07001144 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1145 GFP_KERNEL);
1146 if (fakewriter_tasks == NULL) {
1147 VERBOSE_PRINTK_ERRSTRING("out of memory");
1148 firsterr = -ENOMEM;
1149 goto unwind;
1150 }
1151 for (i = 0; i < nfakewriters; i++) {
1152 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1153 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1154 "rcu_torture_fakewriter");
1155 if (IS_ERR(fakewriter_tasks[i])) {
1156 firsterr = PTR_ERR(fakewriter_tasks[i]);
1157 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1158 fakewriter_tasks[i] = NULL;
1159 goto unwind;
1160 }
1161 }
Josh Triplett2860aab2006-10-04 02:17:11 -07001162 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001163 GFP_KERNEL);
1164 if (reader_tasks == NULL) {
1165 VERBOSE_PRINTK_ERRSTRING("out of memory");
1166 firsterr = -ENOMEM;
1167 goto unwind;
1168 }
1169 for (i = 0; i < nrealreaders; i++) {
1170 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1171 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1172 "rcu_torture_reader");
1173 if (IS_ERR(reader_tasks[i])) {
1174 firsterr = PTR_ERR(reader_tasks[i]);
1175 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1176 reader_tasks[i] = NULL;
1177 goto unwind;
1178 }
1179 }
1180 if (stat_interval > 0) {
1181 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1182 stats_task = kthread_run(rcu_torture_stats, NULL,
1183 "rcu_torture_stats");
1184 if (IS_ERR(stats_task)) {
1185 firsterr = PTR_ERR(stats_task);
1186 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1187 stats_task = NULL;
1188 goto unwind;
1189 }
1190 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001191 if (test_no_idle_hz) {
1192 rcu_idle_cpu = num_online_cpus() - 1;
1193 /* Create the shuffler thread */
1194 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1195 "rcu_torture_shuffle");
1196 if (IS_ERR(shuffler_task)) {
1197 firsterr = PTR_ERR(shuffler_task);
1198 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1199 shuffler_task = NULL;
1200 goto unwind;
1201 }
1202 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001203 if (stutter < 0)
1204 stutter = 0;
1205 if (stutter) {
1206 /* Create the stutter thread */
1207 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1208 "rcu_torture_stutter");
1209 if (IS_ERR(stutter_task)) {
1210 firsterr = PTR_ERR(stutter_task);
1211 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1212 stutter_task = NULL;
1213 goto unwind;
1214 }
1215 }
Paul E. McKenney343e9092008-12-15 16:13:07 -08001216 register_reboot_notifier(&rcutorture_nb);
1217 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001218 return 0;
1219
1220unwind:
Paul E. McKenney343e9092008-12-15 16:13:07 -08001221 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001222 rcu_torture_cleanup();
1223 return firsterr;
1224}
1225
1226module_init(rcu_torture_init);
1227module_exit(rcu_torture_cleanup);