blob: 90b5b123f7a1ee2814686ed791a4d1e45c95935b [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>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070042#include <linux/freezer.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080043#include <linux/cpu.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080044#include <linux/delay.h>
45#include <linux/byteorder/swabb.h>
46#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>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080049
50MODULE_LICENSE("GPL");
Josh Triplettb772e1d2006-10-04 02:17:13 -070051MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
52 "Josh Triplett <josh@freedesktop.org>");
Paul E. McKenneya241ec62005-10-30 15:03:12 -080053
Josh Triplett48022112006-10-03 23:26:16 +020054static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
Josh Triplettb772e1d2006-10-04 02:17:13 -070055static int nfakewriters = 4; /* # fake writer threads */
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080056static int stat_interval; /* Interval between stats, in seconds. */
Paul E. McKenneya241ec62005-10-30 15:03:12 -080057 /* Defaults to "only at end of test". */
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080058static int verbose; /* Print more debug info. */
59static int test_no_idle_hz; /* Test RCU's support for tickless idle CPUs. */
Paul E. McKenneyd120f652008-06-18 05:21:44 -070060static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
61static int stutter = 5; /* Start/stop testing interval (in sec) */
Paul E. McKenney0729fbf2008-06-25 12:24:52 -070062static int irqreader = 1; /* RCU readers from irq (timers). */
Josh Triplett20d2e422006-10-04 02:17:15 -070063static char *torture_type = "rcu"; /* What RCU implementation to torture. */
Paul E. McKenneya241ec62005-10-30 15:03:12 -080064
Josh Triplettd6ad6712007-03-06 01:42:13 -080065module_param(nreaders, int, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080066MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
Josh Triplettd6ad6712007-03-06 01:42:13 -080067module_param(nfakewriters, int, 0444);
Josh Triplettb772e1d2006-10-04 02:17:13 -070068MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
Josh Triplettd6ad6712007-03-06 01:42:13 -080069module_param(stat_interval, int, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080070MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080071module_param(verbose, bool, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080072MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080073module_param(test_no_idle_hz, bool, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080074MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
Josh Triplettd6ad6712007-03-06 01:42:13 -080075module_param(shuffle_interval, int, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080076MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
Paul E. McKenneyd120f652008-06-18 05:21:44 -070077module_param(stutter, int, 0444);
78MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
Paul E. McKenney0729fbf2008-06-25 12:24:52 -070079module_param(irqreader, int, 0444);
80MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
Josh Triplettd6ad6712007-03-06 01:42:13 -080081module_param(torture_type, charp, 0444);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -070082MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070083
84#define TORTURE_FLAG "-torture:"
Paul E. McKenneya241ec62005-10-30 15:03:12 -080085#define PRINTK_STRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070086 do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -080087#define VERBOSE_PRINTK_STRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070088 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -080089#define VERBOSE_PRINTK_ERRSTRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070090 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -080091
92static char printk_buf[4096];
93
94static int nrealreaders;
95static struct task_struct *writer_task;
Josh Triplettb772e1d2006-10-04 02:17:13 -070096static struct task_struct **fakewriter_tasks;
Paul E. McKenneya241ec62005-10-30 15:03:12 -080097static struct task_struct **reader_tasks;
98static struct task_struct *stats_task;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080099static struct task_struct *shuffler_task;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700100static struct task_struct *stutter_task;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800101
102#define RCU_TORTURE_PIPE_LEN 10
103
104struct rcu_torture {
105 struct rcu_head rtort_rcu;
106 int rtort_pipe_count;
107 struct list_head rtort_free;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800108 int rtort_mbtest;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800109};
110
111static int fullstop = 0; /* stop generating callbacks at test end. */
112static 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. McKenneya241ec62005-10-30 15:03:12 -0800139/*
140 * Allocate an element from the rcu_tortures pool.
141 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800142static struct rcu_torture *
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800143rcu_torture_alloc(void)
144{
145 struct list_head *p;
146
Ingo Molnaradac1662006-01-25 19:50:12 +0100147 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800148 if (list_empty(&rcu_torture_freelist)) {
149 atomic_inc(&n_rcu_torture_alloc_fail);
Ingo Molnaradac1662006-01-25 19:50:12 +0100150 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800151 return NULL;
152 }
153 atomic_inc(&n_rcu_torture_alloc);
154 p = rcu_torture_freelist.next;
155 list_del_init(p);
Ingo Molnaradac1662006-01-25 19:50:12 +0100156 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800157 return container_of(p, struct rcu_torture, rtort_free);
158}
159
160/*
161 * Free an element to the rcu_tortures pool.
162 */
163static void
164rcu_torture_free(struct rcu_torture *p)
165{
166 atomic_inc(&n_rcu_torture_free);
Ingo Molnaradac1662006-01-25 19:50:12 +0100167 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800168 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
Ingo Molnaradac1662006-01-25 19:50:12 +0100169 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800170}
171
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800172struct rcu_random_state {
173 unsigned long rrs_state;
Josh Triplett75cfef32006-10-04 02:17:12 -0700174 long rrs_count;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800175};
176
177#define RCU_RANDOM_MULT 39916801 /* prime */
178#define RCU_RANDOM_ADD 479001701 /* prime */
179#define RCU_RANDOM_REFRESH 10000
180
181#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
182
183/*
184 * Crude but fast random-number generator. Uses a linear congruential
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700185 * generator, with occasional help from cpu_clock().
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800186 */
Josh Triplett75cfef32006-10-04 02:17:12 -0700187static unsigned long
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800188rcu_random(struct rcu_random_state *rrsp)
189{
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800190 if (--rrsp->rrs_count < 0) {
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700191 rrsp->rrs_state +=
192 (unsigned long)cpu_clock(raw_smp_processor_id());
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800193 rrsp->rrs_count = RCU_RANDOM_REFRESH;
194 }
195 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
196 return swahw32(rrsp->rrs_state);
197}
198
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700199static void
200rcu_stutter_wait(void)
201{
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700202 while (stutter_pause_test || !rcutorture_runnable)
Paul E. McKenneye3d7be22008-06-22 13:06:38 -0700203 if (rcutorture_runnable)
204 schedule_timeout_interruptible(1);
205 else
Paul E. McKenney3ccf79f2008-06-22 14:02:55 -0700206 schedule_timeout_interruptible(round_jiffies_relative(HZ));
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700207}
208
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800209/*
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700210 * Operations vector for selecting different types of tests.
211 */
212
213struct rcu_torture_ops {
214 void (*init)(void);
215 void (*cleanup)(void);
216 int (*readlock)(void);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700217 void (*readdelay)(struct rcu_random_state *rrsp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700218 void (*readunlock)(int idx);
219 int (*completed)(void);
220 void (*deferredfree)(struct rcu_torture *p);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700221 void (*sync)(void);
Paul E. McKenney23269742008-05-12 21:21:05 +0200222 void (*cb_barrier)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700223 int (*stats)(char *page);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700224 int irqcapable;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700225 char *name;
226};
227static struct rcu_torture_ops *cur_ops = NULL;
228
229/*
230 * Definitions for rcu torture testing.
231 */
232
Josh Tripletta49a4af2006-09-29 01:59:30 -0700233static int rcu_torture_read_lock(void) __acquires(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700234{
235 rcu_read_lock();
236 return 0;
237}
238
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700239static void rcu_read_delay(struct rcu_random_state *rrsp)
240{
241 long delay;
242 const long longdelay = 200;
243
244 /* We want there to be long-running readers, but not all the time. */
245
246 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay);
247 if (!delay)
248 udelay(longdelay);
249}
250
Josh Tripletta49a4af2006-09-29 01:59:30 -0700251static void rcu_torture_read_unlock(int idx) __releases(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700252{
253 rcu_read_unlock();
254}
255
256static int rcu_torture_completed(void)
257{
258 return rcu_batches_completed();
259}
260
261static void
262rcu_torture_cb(struct rcu_head *p)
263{
264 int i;
265 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
266
267 if (fullstop) {
268 /* Test is ending, just drop callbacks on the floor. */
269 /* The next initialization will pick up the pieces. */
270 return;
271 }
272 i = rp->rtort_pipe_count;
273 if (i > RCU_TORTURE_PIPE_LEN)
274 i = RCU_TORTURE_PIPE_LEN;
275 atomic_inc(&rcu_torture_wcount[i]);
276 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
277 rp->rtort_mbtest = 0;
278 rcu_torture_free(rp);
279 } else
280 cur_ops->deferredfree(rp);
281}
282
283static void rcu_torture_deferred_free(struct rcu_torture *p)
284{
285 call_rcu(&p->rtort_rcu, rcu_torture_cb);
286}
287
288static struct rcu_torture_ops rcu_ops = {
289 .init = NULL,
290 .cleanup = NULL,
291 .readlock = rcu_torture_read_lock,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700292 .readdelay = rcu_read_delay,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700293 .readunlock = rcu_torture_read_unlock,
294 .completed = rcu_torture_completed,
295 .deferredfree = rcu_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700296 .sync = synchronize_rcu,
Paul E. McKenney23269742008-05-12 21:21:05 +0200297 .cb_barrier = rcu_barrier,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700298 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700299 .irqcapable = 1,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700300 .name = "rcu"
301};
302
Josh Triplette3033732006-10-04 02:17:14 -0700303static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
304{
305 int i;
306 struct rcu_torture *rp;
307 struct rcu_torture *rp1;
308
309 cur_ops->sync();
310 list_add(&p->rtort_free, &rcu_torture_removed);
311 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
312 i = rp->rtort_pipe_count;
313 if (i > RCU_TORTURE_PIPE_LEN)
314 i = RCU_TORTURE_PIPE_LEN;
315 atomic_inc(&rcu_torture_wcount[i]);
316 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
317 rp->rtort_mbtest = 0;
318 list_del(&rp->rtort_free);
319 rcu_torture_free(rp);
320 }
321 }
322}
323
324static void rcu_sync_torture_init(void)
325{
326 INIT_LIST_HEAD(&rcu_torture_removed);
327}
328
Josh Triplett20d2e422006-10-04 02:17:15 -0700329static struct rcu_torture_ops rcu_sync_ops = {
330 .init = rcu_sync_torture_init,
331 .cleanup = NULL,
332 .readlock = rcu_torture_read_lock,
333 .readdelay = rcu_read_delay,
334 .readunlock = rcu_torture_read_unlock,
335 .completed = rcu_torture_completed,
336 .deferredfree = rcu_sync_torture_deferred_free,
337 .sync = synchronize_rcu,
Paul E. McKenney23269742008-05-12 21:21:05 +0200338 .cb_barrier = NULL,
Josh Triplett20d2e422006-10-04 02:17:15 -0700339 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700340 .irqcapable = 1,
Josh Triplett20d2e422006-10-04 02:17:15 -0700341 .name = "rcu_sync"
342};
343
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700344/*
345 * Definitions for rcu_bh torture testing.
346 */
347
Josh Tripletta49a4af2006-09-29 01:59:30 -0700348static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700349{
350 rcu_read_lock_bh();
351 return 0;
352}
353
Josh Tripletta49a4af2006-09-29 01:59:30 -0700354static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700355{
356 rcu_read_unlock_bh();
357}
358
359static int rcu_bh_torture_completed(void)
360{
361 return rcu_batches_completed_bh();
362}
363
364static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
365{
366 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
367}
368
Josh Triplettb772e1d2006-10-04 02:17:13 -0700369struct rcu_bh_torture_synchronize {
370 struct rcu_head head;
371 struct completion completion;
372};
373
374static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
375{
376 struct rcu_bh_torture_synchronize *rcu;
377
378 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
379 complete(&rcu->completion);
380}
381
382static void rcu_bh_torture_synchronize(void)
383{
384 struct rcu_bh_torture_synchronize rcu;
385
386 init_completion(&rcu.completion);
387 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
388 wait_for_completion(&rcu.completion);
389}
390
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700391static struct rcu_torture_ops rcu_bh_ops = {
392 .init = NULL,
393 .cleanup = NULL,
394 .readlock = rcu_bh_torture_read_lock,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700395 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700396 .readunlock = rcu_bh_torture_read_unlock,
397 .completed = rcu_bh_torture_completed,
398 .deferredfree = rcu_bh_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700399 .sync = rcu_bh_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200400 .cb_barrier = rcu_barrier_bh,
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700401 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700402 .irqcapable = 1,
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700403 .name = "rcu_bh"
404};
405
Josh Triplett11a14702006-10-04 02:17:16 -0700406static struct rcu_torture_ops rcu_bh_sync_ops = {
407 .init = rcu_sync_torture_init,
408 .cleanup = NULL,
409 .readlock = rcu_bh_torture_read_lock,
410 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
411 .readunlock = rcu_bh_torture_read_unlock,
412 .completed = rcu_bh_torture_completed,
413 .deferredfree = rcu_sync_torture_deferred_free,
414 .sync = rcu_bh_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200415 .cb_barrier = NULL,
Josh Triplett11a14702006-10-04 02:17:16 -0700416 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700417 .irqcapable = 1,
Josh Triplett11a14702006-10-04 02:17:16 -0700418 .name = "rcu_bh_sync"
419};
420
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700421/*
422 * Definitions for srcu torture testing.
423 */
424
425static struct srcu_struct srcu_ctl;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700426
427static void srcu_torture_init(void)
428{
429 init_srcu_struct(&srcu_ctl);
Josh Triplette3033732006-10-04 02:17:14 -0700430 rcu_sync_torture_init();
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700431}
432
433static void srcu_torture_cleanup(void)
434{
435 synchronize_srcu(&srcu_ctl);
436 cleanup_srcu_struct(&srcu_ctl);
437}
438
Josh Triplett012d3ca2006-12-06 20:40:19 -0800439static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700440{
441 return srcu_read_lock(&srcu_ctl);
442}
443
444static void srcu_read_delay(struct rcu_random_state *rrsp)
445{
446 long delay;
447 const long uspertick = 1000000 / HZ;
448 const long longdelay = 10;
449
450 /* We want there to be long-running readers, but not all the time. */
451
452 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
453 if (!delay)
454 schedule_timeout_interruptible(longdelay);
455}
456
Josh Triplett012d3ca2006-12-06 20:40:19 -0800457static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700458{
459 srcu_read_unlock(&srcu_ctl, idx);
460}
461
462static int srcu_torture_completed(void)
463{
464 return srcu_batches_completed(&srcu_ctl);
465}
466
Josh Triplettb772e1d2006-10-04 02:17:13 -0700467static void srcu_torture_synchronize(void)
468{
469 synchronize_srcu(&srcu_ctl);
470}
471
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700472static int srcu_torture_stats(char *page)
473{
474 int cnt = 0;
475 int cpu;
476 int idx = srcu_ctl.completed & 0x1;
477
478 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
479 torture_type, TORTURE_FLAG, idx);
480 for_each_possible_cpu(cpu) {
481 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
482 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
483 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
484 }
485 cnt += sprintf(&page[cnt], "\n");
486 return cnt;
487}
488
489static struct rcu_torture_ops srcu_ops = {
490 .init = srcu_torture_init,
491 .cleanup = srcu_torture_cleanup,
492 .readlock = srcu_torture_read_lock,
493 .readdelay = srcu_read_delay,
494 .readunlock = srcu_torture_read_unlock,
495 .completed = srcu_torture_completed,
Josh Triplette3033732006-10-04 02:17:14 -0700496 .deferredfree = rcu_sync_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700497 .sync = srcu_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200498 .cb_barrier = NULL,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700499 .stats = srcu_torture_stats,
500 .name = "srcu"
501};
502
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700503/*
504 * Definitions for sched torture testing.
505 */
506
507static int sched_torture_read_lock(void)
508{
509 preempt_disable();
510 return 0;
511}
512
513static void sched_torture_read_unlock(int idx)
514{
515 preempt_enable();
516}
517
518static int sched_torture_completed(void)
519{
520 return 0;
521}
522
Paul E. McKenney23269742008-05-12 21:21:05 +0200523static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
524{
525 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
526}
527
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700528static void sched_torture_synchronize(void)
529{
530 synchronize_sched();
531}
532
533static struct rcu_torture_ops sched_ops = {
534 .init = rcu_sync_torture_init,
535 .cleanup = NULL,
536 .readlock = sched_torture_read_lock,
537 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
538 .readunlock = sched_torture_read_unlock,
539 .completed = sched_torture_completed,
Paul E. McKenney23269742008-05-12 21:21:05 +0200540 .deferredfree = rcu_sched_torture_deferred_free,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700541 .sync = sched_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200542 .cb_barrier = rcu_barrier_sched,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700543 .stats = NULL,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700544 .irqcapable = 1,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700545 .name = "sched"
546};
547
Paul E. McKenney23269742008-05-12 21:21:05 +0200548static struct rcu_torture_ops sched_ops_sync = {
549 .init = rcu_sync_torture_init,
550 .cleanup = NULL,
551 .readlock = sched_torture_read_lock,
552 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
553 .readunlock = sched_torture_read_unlock,
554 .completed = sched_torture_completed,
555 .deferredfree = rcu_sync_torture_deferred_free,
556 .sync = sched_torture_synchronize,
557 .cb_barrier = NULL,
558 .stats = NULL,
559 .name = "sched_sync"
560};
561
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700562/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800563 * RCU torture writer kthread. Repeatedly substitutes a new structure
564 * for that pointed to by rcu_torture_current, freeing the old structure
565 * after a series of grace periods (the "pipeline").
566 */
567static int
568rcu_torture_writer(void *arg)
569{
570 int i;
571 long oldbatch = rcu_batches_completed();
572 struct rcu_torture *rp;
573 struct rcu_torture *old_rp;
574 static DEFINE_RCU_RANDOM(rand);
575
576 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800577 set_user_nice(current, 19);
578
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800579 do {
580 schedule_timeout_uninterruptible(1);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800581 if ((rp = rcu_torture_alloc()) == NULL)
582 continue;
583 rp->rtort_pipe_count = 0;
584 udelay(rcu_random(&rand) & 0x3ff);
585 old_rp = rcu_torture_current;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800586 rp->rtort_mbtest = 1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800587 rcu_assign_pointer(rcu_torture_current, rp);
588 smp_wmb();
Josh Triplettc8e5b162007-05-08 00:33:20 -0700589 if (old_rp) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800590 i = old_rp->rtort_pipe_count;
591 if (i > RCU_TORTURE_PIPE_LEN)
592 i = RCU_TORTURE_PIPE_LEN;
593 atomic_inc(&rcu_torture_wcount[i]);
594 old_rp->rtort_pipe_count++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700595 cur_ops->deferredfree(old_rp);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800596 }
597 rcu_torture_current_version++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700598 oldbatch = cur_ops->completed();
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700599 rcu_stutter_wait();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800600 } while (!kthread_should_stop() && !fullstop);
601 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
602 while (!kthread_should_stop())
603 schedule_timeout_uninterruptible(1);
604 return 0;
605}
606
607/*
Josh Triplettb772e1d2006-10-04 02:17:13 -0700608 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
609 * delay between calls.
610 */
611static int
612rcu_torture_fakewriter(void *arg)
613{
614 DEFINE_RCU_RANDOM(rand);
615
616 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
617 set_user_nice(current, 19);
618
619 do {
620 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
621 udelay(rcu_random(&rand) & 0x3ff);
622 cur_ops->sync();
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700623 rcu_stutter_wait();
Josh Triplettb772e1d2006-10-04 02:17:13 -0700624 } while (!kthread_should_stop() && !fullstop);
625
626 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
627 while (!kthread_should_stop())
628 schedule_timeout_uninterruptible(1);
629 return 0;
630}
631
632/*
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700633 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
634 * incrementing the corresponding element of the pipeline array. The
635 * counter in the element should never be greater than 1, otherwise, the
636 * RCU implementation is broken.
637 */
638static void rcu_torture_timer(unsigned long unused)
639{
640 int idx;
641 int completed;
642 static DEFINE_RCU_RANDOM(rand);
643 static DEFINE_SPINLOCK(rand_lock);
644 struct rcu_torture *p;
645 int pipe_count;
646
647 idx = cur_ops->readlock();
648 completed = cur_ops->completed();
649 p = rcu_dereference(rcu_torture_current);
650 if (p == NULL) {
651 /* Leave because rcu_torture_writer is not yet underway */
652 cur_ops->readunlock(idx);
653 return;
654 }
655 if (p->rtort_mbtest == 0)
656 atomic_inc(&n_rcu_torture_mberror);
657 spin_lock(&rand_lock);
658 cur_ops->readdelay(&rand);
659 n_rcu_torture_timers++;
660 spin_unlock(&rand_lock);
661 preempt_disable();
662 pipe_count = p->rtort_pipe_count;
663 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
664 /* Should not happen, but... */
665 pipe_count = RCU_TORTURE_PIPE_LEN;
666 }
667 ++__get_cpu_var(rcu_torture_count)[pipe_count];
668 completed = cur_ops->completed() - completed;
669 if (completed > RCU_TORTURE_PIPE_LEN) {
670 /* Should not happen, but... */
671 completed = RCU_TORTURE_PIPE_LEN;
672 }
673 ++__get_cpu_var(rcu_torture_batch)[completed];
674 preempt_enable();
675 cur_ops->readunlock(idx);
676}
677
678/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800679 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
680 * incrementing the corresponding element of the pipeline array. The
681 * counter in the element should never be greater than 1, otherwise, the
682 * RCU implementation is broken.
683 */
684static int
685rcu_torture_reader(void *arg)
686{
687 int completed;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700688 int idx;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800689 DEFINE_RCU_RANDOM(rand);
690 struct rcu_torture *p;
691 int pipe_count;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700692 struct timer_list t;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800693
694 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800695 set_user_nice(current, 19);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700696 if (irqreader && cur_ops->irqcapable)
697 setup_timer_on_stack(&t, rcu_torture_timer, 0);
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800698
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800699 do {
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700700 if (irqreader && cur_ops->irqcapable) {
701 if (!timer_pending(&t))
702 mod_timer(&t, 1);
703 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700704 idx = cur_ops->readlock();
705 completed = cur_ops->completed();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800706 p = rcu_dereference(rcu_torture_current);
707 if (p == NULL) {
708 /* Wait for rcu_torture_writer to get underway */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700709 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800710 schedule_timeout_interruptible(HZ);
711 continue;
712 }
Paul E. McKenney996417d2005-11-18 01:10:50 -0800713 if (p->rtort_mbtest == 0)
714 atomic_inc(&n_rcu_torture_mberror);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700715 cur_ops->readdelay(&rand);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800716 preempt_disable();
717 pipe_count = p->rtort_pipe_count;
718 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
719 /* Should not happen, but... */
720 pipe_count = RCU_TORTURE_PIPE_LEN;
721 }
722 ++__get_cpu_var(rcu_torture_count)[pipe_count];
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700723 completed = cur_ops->completed() - completed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800724 if (completed > RCU_TORTURE_PIPE_LEN) {
725 /* Should not happen, but... */
726 completed = RCU_TORTURE_PIPE_LEN;
727 }
728 ++__get_cpu_var(rcu_torture_batch)[completed];
729 preempt_enable();
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700730 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800731 schedule();
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700732 rcu_stutter_wait();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800733 } while (!kthread_should_stop() && !fullstop);
734 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700735 if (irqreader && cur_ops->irqcapable)
736 del_timer_sync(&t);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800737 while (!kthread_should_stop())
738 schedule_timeout_uninterruptible(1);
739 return 0;
740}
741
742/*
743 * Create an RCU-torture statistics message in the specified buffer.
744 */
745static int
746rcu_torture_printk(char *page)
747{
748 int cnt = 0;
749 int cpu;
750 int i;
751 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
752 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
753
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800754 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800755 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
756 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
757 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
758 }
759 }
760 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
761 if (pipesummary[i] != 0)
762 break;
763 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700764 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800765 cnt += sprintf(&page[cnt],
Paul E. McKenney996417d2005-11-18 01:10:50 -0800766 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700767 "rtmbe: %d nt: %ld",
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800768 rcu_torture_current,
769 rcu_torture_current_version,
770 list_empty(&rcu_torture_freelist),
771 atomic_read(&n_rcu_torture_alloc),
772 atomic_read(&n_rcu_torture_alloc_fail),
Paul E. McKenney996417d2005-11-18 01:10:50 -0800773 atomic_read(&n_rcu_torture_free),
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700774 atomic_read(&n_rcu_torture_mberror),
775 n_rcu_torture_timers);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800776 if (atomic_read(&n_rcu_torture_mberror) != 0)
777 cnt += sprintf(&page[cnt], " !!!");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700778 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800779 if (i > 1) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800780 cnt += sprintf(&page[cnt], "!!! ");
Paul E. McKenney996417d2005-11-18 01:10:50 -0800781 atomic_inc(&n_rcu_torture_error);
Ingo Molnar5af970a2008-06-18 10:09:48 +0200782 WARN_ON_ONCE(1);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800783 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800784 cnt += sprintf(&page[cnt], "Reader Pipe: ");
785 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
786 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700787 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800788 cnt += sprintf(&page[cnt], "Reader Batch: ");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700789 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800790 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700791 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800792 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
793 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
794 cnt += sprintf(&page[cnt], " %d",
795 atomic_read(&rcu_torture_wcount[i]));
796 }
797 cnt += sprintf(&page[cnt], "\n");
Josh Triplettc8e5b162007-05-08 00:33:20 -0700798 if (cur_ops->stats)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700799 cnt += cur_ops->stats(&page[cnt]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800800 return cnt;
801}
802
803/*
804 * Print torture statistics. Caller must ensure that there is only
805 * one call to this function at a given time!!! This is normally
806 * accomplished by relying on the module system to only have one copy
807 * of the module loaded, and then by giving the rcu_torture_stats
808 * kthread full control (or the init/cleanup functions when rcu_torture_stats
809 * thread is not running).
810 */
811static void
812rcu_torture_stats_print(void)
813{
814 int cnt;
815
816 cnt = rcu_torture_printk(printk_buf);
817 printk(KERN_ALERT "%s", printk_buf);
818}
819
820/*
821 * Periodically prints torture statistics, if periodic statistics printing
822 * was specified via the stat_interval module parameter.
823 *
824 * No need to worry about fullstop here, since this one doesn't reference
825 * volatile state or register callbacks.
826 */
827static int
828rcu_torture_stats(void *arg)
829{
830 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
831 do {
832 schedule_timeout_interruptible(stat_interval * HZ);
833 rcu_torture_stats_print();
834 } while (!kthread_should_stop());
835 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
836 return 0;
837}
838
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800839static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
840
841/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
842 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
843 */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700844static void rcu_torture_shuffle_tasks(void)
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800845{
Mike Travisf70316d2008-04-04 18:11:06 -0700846 cpumask_t tmp_mask;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800847 int i;
848
Mike Travisf70316d2008-04-04 18:11:06 -0700849 cpus_setall(tmp_mask);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100850 get_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800851
852 /* No point in shuffling if there is only one online CPU (ex: UP) */
853 if (num_online_cpus() == 1) {
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100854 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800855 return;
856 }
857
858 if (rcu_idle_cpu != -1)
859 cpu_clear(rcu_idle_cpu, tmp_mask);
860
Mike Travisf70316d2008-04-04 18:11:06 -0700861 set_cpus_allowed_ptr(current, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800862
Josh Triplettc8e5b162007-05-08 00:33:20 -0700863 if (reader_tasks) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800864 for (i = 0; i < nrealreaders; i++)
865 if (reader_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -0700866 set_cpus_allowed_ptr(reader_tasks[i],
867 &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800868 }
869
Josh Triplettc8e5b162007-05-08 00:33:20 -0700870 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -0700871 for (i = 0; i < nfakewriters; i++)
872 if (fakewriter_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -0700873 set_cpus_allowed_ptr(fakewriter_tasks[i],
874 &tmp_mask);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700875 }
876
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800877 if (writer_task)
Mike Travisf70316d2008-04-04 18:11:06 -0700878 set_cpus_allowed_ptr(writer_task, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800879
880 if (stats_task)
Mike Travisf70316d2008-04-04 18:11:06 -0700881 set_cpus_allowed_ptr(stats_task, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800882
883 if (rcu_idle_cpu == -1)
884 rcu_idle_cpu = num_online_cpus() - 1;
885 else
886 rcu_idle_cpu--;
887
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100888 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800889}
890
891/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
892 * system to become idle at a time and cut off its timer ticks. This is meant
893 * to test the support for such tickless idle CPU in RCU.
894 */
895static int
896rcu_torture_shuffle(void *arg)
897{
898 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
899 do {
900 schedule_timeout_interruptible(shuffle_interval * HZ);
901 rcu_torture_shuffle_tasks();
902 } while (!kthread_should_stop());
903 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
904 return 0;
905}
906
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700907/* Cause the rcutorture test to "stutter", starting and stopping all
908 * threads periodically.
909 */
910static int
911rcu_torture_stutter(void *arg)
912{
913 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
914 do {
915 schedule_timeout_interruptible(stutter * HZ);
916 stutter_pause_test = 1;
917 if (!kthread_should_stop())
918 schedule_timeout_interruptible(stutter * HZ);
919 stutter_pause_test = 0;
920 } while (!kthread_should_stop());
921 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
922 return 0;
923}
924
Paul E. McKenney95c38322006-03-24 03:15:58 -0800925static inline void
926rcu_torture_print_module_parms(char *tag)
927{
Josh Triplettb772e1d2006-10-04 02:17:13 -0700928 printk(KERN_ALERT "%s" TORTURE_FLAG
929 "--- %s: nreaders=%d nfakewriters=%d "
Paul E. McKenney95c38322006-03-24 03:15:58 -0800930 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700931 "shuffle_interval=%d stutter=%d irqreader=%d\n",
Josh Triplettb772e1d2006-10-04 02:17:13 -0700932 torture_type, tag, nrealreaders, nfakewriters,
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700933 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700934 stutter, irqreader);
Paul E. McKenney95c38322006-03-24 03:15:58 -0800935}
936
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800937static void
938rcu_torture_cleanup(void)
939{
940 int i;
941
942 fullstop = 1;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700943 if (stutter_task) {
944 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
945 kthread_stop(stutter_task);
946 }
947 stutter_task = NULL;
Josh Triplettc8e5b162007-05-08 00:33:20 -0700948 if (shuffler_task) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800949 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
950 kthread_stop(shuffler_task);
951 }
952 shuffler_task = NULL;
953
Josh Triplettc8e5b162007-05-08 00:33:20 -0700954 if (writer_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800955 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
956 kthread_stop(writer_task);
957 }
958 writer_task = NULL;
959
Josh Triplettc8e5b162007-05-08 00:33:20 -0700960 if (reader_tasks) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800961 for (i = 0; i < nrealreaders; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -0700962 if (reader_tasks[i]) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800963 VERBOSE_PRINTK_STRING(
964 "Stopping rcu_torture_reader task");
965 kthread_stop(reader_tasks[i]);
966 }
967 reader_tasks[i] = NULL;
968 }
969 kfree(reader_tasks);
970 reader_tasks = NULL;
971 }
972 rcu_torture_current = NULL;
973
Josh Triplettc8e5b162007-05-08 00:33:20 -0700974 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -0700975 for (i = 0; i < nfakewriters; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -0700976 if (fakewriter_tasks[i]) {
Josh Triplettb772e1d2006-10-04 02:17:13 -0700977 VERBOSE_PRINTK_STRING(
978 "Stopping rcu_torture_fakewriter task");
979 kthread_stop(fakewriter_tasks[i]);
980 }
981 fakewriter_tasks[i] = NULL;
982 }
983 kfree(fakewriter_tasks);
984 fakewriter_tasks = NULL;
985 }
986
Josh Triplettc8e5b162007-05-08 00:33:20 -0700987 if (stats_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800988 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
989 kthread_stop(stats_task);
990 }
991 stats_task = NULL;
992
993 /* Wait for all RCU callbacks to fire. */
Paul E. McKenney23269742008-05-12 21:21:05 +0200994
995 if (cur_ops->cb_barrier != NULL)
996 cur_ops->cb_barrier();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800997
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800998 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700999
Josh Triplettc8e5b162007-05-08 00:33:20 -07001000 if (cur_ops->cleanup)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001001 cur_ops->cleanup();
Paul E. McKenney95c38322006-03-24 03:15:58 -08001002 if (atomic_read(&n_rcu_torture_error))
1003 rcu_torture_print_module_parms("End of test: FAILURE");
1004 else
1005 rcu_torture_print_module_parms("End of test: SUCCESS");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001006}
1007
Josh Triplett6f8bc502007-05-08 00:25:24 -07001008static int __init
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001009rcu_torture_init(void)
1010{
1011 int i;
1012 int cpu;
1013 int firsterr = 0;
Josh Triplettade5fb82007-05-08 00:33:22 -07001014 static struct rcu_torture_ops *torture_ops[] =
1015 { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops,
Paul E. McKenney23269742008-05-12 21:21:05 +02001016 &srcu_ops, &sched_ops, &sched_ops_sync, };
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001017
1018 /* Process args and tell the world that the torturer is on the job. */
Josh Triplettade5fb82007-05-08 00:33:22 -07001019 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001020 cur_ops = torture_ops[i];
Josh Triplettade5fb82007-05-08 00:33:22 -07001021 if (strcmp(torture_type, cur_ops->name) == 0)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001022 break;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001023 }
Josh Triplettade5fb82007-05-08 00:33:22 -07001024 if (i == ARRAY_SIZE(torture_ops)) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001025 printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
1026 torture_type);
1027 return (-EINVAL);
1028 }
Josh Triplettc8e5b162007-05-08 00:33:20 -07001029 if (cur_ops->init)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001030 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1031
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001032 if (nreaders >= 0)
1033 nrealreaders = nreaders;
1034 else
1035 nrealreaders = 2 * num_online_cpus();
Paul E. McKenney95c38322006-03-24 03:15:58 -08001036 rcu_torture_print_module_parms("Start of test");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001037 fullstop = 0;
1038
1039 /* Set up the freelist. */
1040
1041 INIT_LIST_HEAD(&rcu_torture_freelist);
Ahmed S. Darwish788e7702007-05-08 00:33:14 -07001042 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
Paul E. McKenney996417d2005-11-18 01:10:50 -08001043 rcu_tortures[i].rtort_mbtest = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001044 list_add_tail(&rcu_tortures[i].rtort_free,
1045 &rcu_torture_freelist);
1046 }
1047
1048 /* Initialize the statistics so that each run gets its own numbers. */
1049
1050 rcu_torture_current = NULL;
1051 rcu_torture_current_version = 0;
1052 atomic_set(&n_rcu_torture_alloc, 0);
1053 atomic_set(&n_rcu_torture_alloc_fail, 0);
1054 atomic_set(&n_rcu_torture_free, 0);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001055 atomic_set(&n_rcu_torture_mberror, 0);
1056 atomic_set(&n_rcu_torture_error, 0);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001057 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1058 atomic_set(&rcu_torture_wcount[i], 0);
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001059 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001060 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1061 per_cpu(rcu_torture_count, cpu)[i] = 0;
1062 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1063 }
1064 }
1065
1066 /* Start up the kthreads. */
1067
1068 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1069 writer_task = kthread_run(rcu_torture_writer, NULL,
1070 "rcu_torture_writer");
1071 if (IS_ERR(writer_task)) {
1072 firsterr = PTR_ERR(writer_task);
1073 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1074 writer_task = NULL;
1075 goto unwind;
1076 }
Josh Triplettb772e1d2006-10-04 02:17:13 -07001077 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1078 GFP_KERNEL);
1079 if (fakewriter_tasks == NULL) {
1080 VERBOSE_PRINTK_ERRSTRING("out of memory");
1081 firsterr = -ENOMEM;
1082 goto unwind;
1083 }
1084 for (i = 0; i < nfakewriters; i++) {
1085 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1086 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1087 "rcu_torture_fakewriter");
1088 if (IS_ERR(fakewriter_tasks[i])) {
1089 firsterr = PTR_ERR(fakewriter_tasks[i]);
1090 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1091 fakewriter_tasks[i] = NULL;
1092 goto unwind;
1093 }
1094 }
Josh Triplett2860aab2006-10-04 02:17:11 -07001095 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001096 GFP_KERNEL);
1097 if (reader_tasks == NULL) {
1098 VERBOSE_PRINTK_ERRSTRING("out of memory");
1099 firsterr = -ENOMEM;
1100 goto unwind;
1101 }
1102 for (i = 0; i < nrealreaders; i++) {
1103 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1104 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1105 "rcu_torture_reader");
1106 if (IS_ERR(reader_tasks[i])) {
1107 firsterr = PTR_ERR(reader_tasks[i]);
1108 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1109 reader_tasks[i] = NULL;
1110 goto unwind;
1111 }
1112 }
1113 if (stat_interval > 0) {
1114 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1115 stats_task = kthread_run(rcu_torture_stats, NULL,
1116 "rcu_torture_stats");
1117 if (IS_ERR(stats_task)) {
1118 firsterr = PTR_ERR(stats_task);
1119 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1120 stats_task = NULL;
1121 goto unwind;
1122 }
1123 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001124 if (test_no_idle_hz) {
1125 rcu_idle_cpu = num_online_cpus() - 1;
1126 /* Create the shuffler thread */
1127 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1128 "rcu_torture_shuffle");
1129 if (IS_ERR(shuffler_task)) {
1130 firsterr = PTR_ERR(shuffler_task);
1131 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1132 shuffler_task = NULL;
1133 goto unwind;
1134 }
1135 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001136 if (stutter < 0)
1137 stutter = 0;
1138 if (stutter) {
1139 /* Create the stutter thread */
1140 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1141 "rcu_torture_stutter");
1142 if (IS_ERR(stutter_task)) {
1143 firsterr = PTR_ERR(stutter_task);
1144 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1145 stutter_task = NULL;
1146 goto unwind;
1147 }
1148 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001149 return 0;
1150
1151unwind:
1152 rcu_torture_cleanup();
1153 return firsterr;
1154}
1155
1156module_init(rcu_torture_init);
1157module_exit(rcu_torture_cleanup);