blob: 27003e2421c77d20181cdc1aa45fb603ca5cc3d9 [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) */
Josh Triplett20d2e422006-10-04 02:17:15 -070062static char *torture_type = "rcu"; /* What RCU implementation to torture. */
Paul E. McKenneya241ec62005-10-30 15:03:12 -080063
Josh Triplettd6ad6712007-03-06 01:42:13 -080064module_param(nreaders, int, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080065MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
Josh Triplettd6ad6712007-03-06 01:42:13 -080066module_param(nfakewriters, int, 0444);
Josh Triplettb772e1d2006-10-04 02:17:13 -070067MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
Josh Triplettd6ad6712007-03-06 01:42:13 -080068module_param(stat_interval, int, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080069MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080070module_param(verbose, bool, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080071MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080072module_param(test_no_idle_hz, bool, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080073MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
Josh Triplettd6ad6712007-03-06 01:42:13 -080074module_param(shuffle_interval, int, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080075MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
Paul E. McKenneyd120f652008-06-18 05:21:44 -070076module_param(stutter, int, 0444);
77MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
Josh Triplettd6ad6712007-03-06 01:42:13 -080078module_param(torture_type, charp, 0444);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -070079MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070080
81#define TORTURE_FLAG "-torture:"
Paul E. McKenneya241ec62005-10-30 15:03:12 -080082#define PRINTK_STRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070083 do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -080084#define VERBOSE_PRINTK_STRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070085 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -080086#define VERBOSE_PRINTK_ERRSTRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070087 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -080088
89static char printk_buf[4096];
90
91static int nrealreaders;
92static struct task_struct *writer_task;
Josh Triplettb772e1d2006-10-04 02:17:13 -070093static struct task_struct **fakewriter_tasks;
Paul E. McKenneya241ec62005-10-30 15:03:12 -080094static struct task_struct **reader_tasks;
95static struct task_struct *stats_task;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080096static struct task_struct *shuffler_task;
Paul E. McKenneyd120f652008-06-18 05:21:44 -070097static struct task_struct *stutter_task;
Paul E. McKenneya241ec62005-10-30 15:03:12 -080098
99#define RCU_TORTURE_PIPE_LEN 10
100
101struct rcu_torture {
102 struct rcu_head rtort_rcu;
103 int rtort_pipe_count;
104 struct list_head rtort_free;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800105 int rtort_mbtest;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800106};
107
108static int fullstop = 0; /* stop generating callbacks at test end. */
109static LIST_HEAD(rcu_torture_freelist);
110static struct rcu_torture *rcu_torture_current = NULL;
111static long rcu_torture_current_version = 0;
112static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
113static DEFINE_SPINLOCK(rcu_torture_lock);
114static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
115 { 0 };
116static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
117 { 0 };
118static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700119static atomic_t n_rcu_torture_alloc;
120static atomic_t n_rcu_torture_alloc_fail;
121static atomic_t n_rcu_torture_free;
122static atomic_t n_rcu_torture_mberror;
123static atomic_t n_rcu_torture_error;
Josh Triplette3033732006-10-04 02:17:14 -0700124static struct list_head rcu_torture_removed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800125
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700126static int stutter_pause_test = 0;
127
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700128#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
129#define RCUTORTURE_RUNNABLE_INIT 1
130#else
131#define RCUTORTURE_RUNNABLE_INIT 0
132#endif
133int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
134
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800135/*
136 * Allocate an element from the rcu_tortures pool.
137 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800138static struct rcu_torture *
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800139rcu_torture_alloc(void)
140{
141 struct list_head *p;
142
Ingo Molnaradac1662006-01-25 19:50:12 +0100143 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800144 if (list_empty(&rcu_torture_freelist)) {
145 atomic_inc(&n_rcu_torture_alloc_fail);
Ingo Molnaradac1662006-01-25 19:50:12 +0100146 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800147 return NULL;
148 }
149 atomic_inc(&n_rcu_torture_alloc);
150 p = rcu_torture_freelist.next;
151 list_del_init(p);
Ingo Molnaradac1662006-01-25 19:50:12 +0100152 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800153 return container_of(p, struct rcu_torture, rtort_free);
154}
155
156/*
157 * Free an element to the rcu_tortures pool.
158 */
159static void
160rcu_torture_free(struct rcu_torture *p)
161{
162 atomic_inc(&n_rcu_torture_free);
Ingo Molnaradac1662006-01-25 19:50:12 +0100163 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800164 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
Ingo Molnaradac1662006-01-25 19:50:12 +0100165 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800166}
167
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800168struct rcu_random_state {
169 unsigned long rrs_state;
Josh Triplett75cfef32006-10-04 02:17:12 -0700170 long rrs_count;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800171};
172
173#define RCU_RANDOM_MULT 39916801 /* prime */
174#define RCU_RANDOM_ADD 479001701 /* prime */
175#define RCU_RANDOM_REFRESH 10000
176
177#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
178
179/*
180 * Crude but fast random-number generator. Uses a linear congruential
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700181 * generator, with occasional help from cpu_clock().
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800182 */
Josh Triplett75cfef32006-10-04 02:17:12 -0700183static unsigned long
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800184rcu_random(struct rcu_random_state *rrsp)
185{
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800186 if (--rrsp->rrs_count < 0) {
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700187 rrsp->rrs_state +=
188 (unsigned long)cpu_clock(raw_smp_processor_id());
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800189 rrsp->rrs_count = RCU_RANDOM_REFRESH;
190 }
191 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
192 return swahw32(rrsp->rrs_state);
193}
194
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700195static void
196rcu_stutter_wait(void)
197{
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700198 while (stutter_pause_test || !rcutorture_runnable)
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700199 schedule_timeout_interruptible(1);
200}
201
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800202/*
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700203 * Operations vector for selecting different types of tests.
204 */
205
206struct rcu_torture_ops {
207 void (*init)(void);
208 void (*cleanup)(void);
209 int (*readlock)(void);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700210 void (*readdelay)(struct rcu_random_state *rrsp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700211 void (*readunlock)(int idx);
212 int (*completed)(void);
213 void (*deferredfree)(struct rcu_torture *p);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700214 void (*sync)(void);
Paul E. McKenney23269742008-05-12 21:21:05 +0200215 void (*cb_barrier)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700216 int (*stats)(char *page);
217 char *name;
218};
219static struct rcu_torture_ops *cur_ops = NULL;
220
221/*
222 * Definitions for rcu torture testing.
223 */
224
Josh Tripletta49a4af2006-09-29 01:59:30 -0700225static int rcu_torture_read_lock(void) __acquires(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700226{
227 rcu_read_lock();
228 return 0;
229}
230
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700231static void rcu_read_delay(struct rcu_random_state *rrsp)
232{
233 long delay;
234 const long longdelay = 200;
235
236 /* We want there to be long-running readers, but not all the time. */
237
238 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay);
239 if (!delay)
240 udelay(longdelay);
241}
242
Josh Tripletta49a4af2006-09-29 01:59:30 -0700243static void rcu_torture_read_unlock(int idx) __releases(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700244{
245 rcu_read_unlock();
246}
247
248static int rcu_torture_completed(void)
249{
250 return rcu_batches_completed();
251}
252
253static void
254rcu_torture_cb(struct rcu_head *p)
255{
256 int i;
257 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
258
259 if (fullstop) {
260 /* Test is ending, just drop callbacks on the floor. */
261 /* The next initialization will pick up the pieces. */
262 return;
263 }
264 i = rp->rtort_pipe_count;
265 if (i > RCU_TORTURE_PIPE_LEN)
266 i = RCU_TORTURE_PIPE_LEN;
267 atomic_inc(&rcu_torture_wcount[i]);
268 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
269 rp->rtort_mbtest = 0;
270 rcu_torture_free(rp);
271 } else
272 cur_ops->deferredfree(rp);
273}
274
275static void rcu_torture_deferred_free(struct rcu_torture *p)
276{
277 call_rcu(&p->rtort_rcu, rcu_torture_cb);
278}
279
280static struct rcu_torture_ops rcu_ops = {
281 .init = NULL,
282 .cleanup = NULL,
283 .readlock = rcu_torture_read_lock,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700284 .readdelay = rcu_read_delay,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700285 .readunlock = rcu_torture_read_unlock,
286 .completed = rcu_torture_completed,
287 .deferredfree = rcu_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700288 .sync = synchronize_rcu,
Paul E. McKenney23269742008-05-12 21:21:05 +0200289 .cb_barrier = rcu_barrier,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700290 .stats = NULL,
291 .name = "rcu"
292};
293
Josh Triplette3033732006-10-04 02:17:14 -0700294static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
295{
296 int i;
297 struct rcu_torture *rp;
298 struct rcu_torture *rp1;
299
300 cur_ops->sync();
301 list_add(&p->rtort_free, &rcu_torture_removed);
302 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
303 i = rp->rtort_pipe_count;
304 if (i > RCU_TORTURE_PIPE_LEN)
305 i = RCU_TORTURE_PIPE_LEN;
306 atomic_inc(&rcu_torture_wcount[i]);
307 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
308 rp->rtort_mbtest = 0;
309 list_del(&rp->rtort_free);
310 rcu_torture_free(rp);
311 }
312 }
313}
314
315static void rcu_sync_torture_init(void)
316{
317 INIT_LIST_HEAD(&rcu_torture_removed);
318}
319
Josh Triplett20d2e422006-10-04 02:17:15 -0700320static struct rcu_torture_ops rcu_sync_ops = {
321 .init = rcu_sync_torture_init,
322 .cleanup = NULL,
323 .readlock = rcu_torture_read_lock,
324 .readdelay = rcu_read_delay,
325 .readunlock = rcu_torture_read_unlock,
326 .completed = rcu_torture_completed,
327 .deferredfree = rcu_sync_torture_deferred_free,
328 .sync = synchronize_rcu,
Paul E. McKenney23269742008-05-12 21:21:05 +0200329 .cb_barrier = NULL,
Josh Triplett20d2e422006-10-04 02:17:15 -0700330 .stats = NULL,
331 .name = "rcu_sync"
332};
333
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700334/*
335 * Definitions for rcu_bh torture testing.
336 */
337
Josh Tripletta49a4af2006-09-29 01:59:30 -0700338static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700339{
340 rcu_read_lock_bh();
341 return 0;
342}
343
Josh Tripletta49a4af2006-09-29 01:59:30 -0700344static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700345{
346 rcu_read_unlock_bh();
347}
348
349static int rcu_bh_torture_completed(void)
350{
351 return rcu_batches_completed_bh();
352}
353
354static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
355{
356 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
357}
358
Josh Triplettb772e1d2006-10-04 02:17:13 -0700359struct rcu_bh_torture_synchronize {
360 struct rcu_head head;
361 struct completion completion;
362};
363
364static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
365{
366 struct rcu_bh_torture_synchronize *rcu;
367
368 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
369 complete(&rcu->completion);
370}
371
372static void rcu_bh_torture_synchronize(void)
373{
374 struct rcu_bh_torture_synchronize rcu;
375
376 init_completion(&rcu.completion);
377 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
378 wait_for_completion(&rcu.completion);
379}
380
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700381static struct rcu_torture_ops rcu_bh_ops = {
382 .init = NULL,
383 .cleanup = NULL,
384 .readlock = rcu_bh_torture_read_lock,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700385 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700386 .readunlock = rcu_bh_torture_read_unlock,
387 .completed = rcu_bh_torture_completed,
388 .deferredfree = rcu_bh_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700389 .sync = rcu_bh_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200390 .cb_barrier = rcu_barrier_bh,
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700391 .stats = NULL,
392 .name = "rcu_bh"
393};
394
Josh Triplett11a14702006-10-04 02:17:16 -0700395static struct rcu_torture_ops rcu_bh_sync_ops = {
396 .init = rcu_sync_torture_init,
397 .cleanup = NULL,
398 .readlock = rcu_bh_torture_read_lock,
399 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
400 .readunlock = rcu_bh_torture_read_unlock,
401 .completed = rcu_bh_torture_completed,
402 .deferredfree = rcu_sync_torture_deferred_free,
403 .sync = rcu_bh_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200404 .cb_barrier = NULL,
Josh Triplett11a14702006-10-04 02:17:16 -0700405 .stats = NULL,
406 .name = "rcu_bh_sync"
407};
408
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700409/*
410 * Definitions for srcu torture testing.
411 */
412
413static struct srcu_struct srcu_ctl;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700414
415static void srcu_torture_init(void)
416{
417 init_srcu_struct(&srcu_ctl);
Josh Triplette3033732006-10-04 02:17:14 -0700418 rcu_sync_torture_init();
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700419}
420
421static void srcu_torture_cleanup(void)
422{
423 synchronize_srcu(&srcu_ctl);
424 cleanup_srcu_struct(&srcu_ctl);
425}
426
Josh Triplett012d3ca2006-12-06 20:40:19 -0800427static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700428{
429 return srcu_read_lock(&srcu_ctl);
430}
431
432static void srcu_read_delay(struct rcu_random_state *rrsp)
433{
434 long delay;
435 const long uspertick = 1000000 / HZ;
436 const long longdelay = 10;
437
438 /* We want there to be long-running readers, but not all the time. */
439
440 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
441 if (!delay)
442 schedule_timeout_interruptible(longdelay);
443}
444
Josh Triplett012d3ca2006-12-06 20:40:19 -0800445static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700446{
447 srcu_read_unlock(&srcu_ctl, idx);
448}
449
450static int srcu_torture_completed(void)
451{
452 return srcu_batches_completed(&srcu_ctl);
453}
454
Josh Triplettb772e1d2006-10-04 02:17:13 -0700455static void srcu_torture_synchronize(void)
456{
457 synchronize_srcu(&srcu_ctl);
458}
459
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700460static int srcu_torture_stats(char *page)
461{
462 int cnt = 0;
463 int cpu;
464 int idx = srcu_ctl.completed & 0x1;
465
466 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
467 torture_type, TORTURE_FLAG, idx);
468 for_each_possible_cpu(cpu) {
469 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
470 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
471 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
472 }
473 cnt += sprintf(&page[cnt], "\n");
474 return cnt;
475}
476
477static struct rcu_torture_ops srcu_ops = {
478 .init = srcu_torture_init,
479 .cleanup = srcu_torture_cleanup,
480 .readlock = srcu_torture_read_lock,
481 .readdelay = srcu_read_delay,
482 .readunlock = srcu_torture_read_unlock,
483 .completed = srcu_torture_completed,
Josh Triplette3033732006-10-04 02:17:14 -0700484 .deferredfree = rcu_sync_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700485 .sync = srcu_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200486 .cb_barrier = NULL,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700487 .stats = srcu_torture_stats,
488 .name = "srcu"
489};
490
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700491/*
492 * Definitions for sched torture testing.
493 */
494
495static int sched_torture_read_lock(void)
496{
497 preempt_disable();
498 return 0;
499}
500
501static void sched_torture_read_unlock(int idx)
502{
503 preempt_enable();
504}
505
506static int sched_torture_completed(void)
507{
508 return 0;
509}
510
Paul E. McKenney23269742008-05-12 21:21:05 +0200511static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
512{
513 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
514}
515
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700516static void sched_torture_synchronize(void)
517{
518 synchronize_sched();
519}
520
521static struct rcu_torture_ops sched_ops = {
522 .init = rcu_sync_torture_init,
523 .cleanup = NULL,
524 .readlock = sched_torture_read_lock,
525 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
526 .readunlock = sched_torture_read_unlock,
527 .completed = sched_torture_completed,
Paul E. McKenney23269742008-05-12 21:21:05 +0200528 .deferredfree = rcu_sched_torture_deferred_free,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700529 .sync = sched_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200530 .cb_barrier = rcu_barrier_sched,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700531 .stats = NULL,
532 .name = "sched"
533};
534
Paul E. McKenney23269742008-05-12 21:21:05 +0200535static struct rcu_torture_ops sched_ops_sync = {
536 .init = rcu_sync_torture_init,
537 .cleanup = NULL,
538 .readlock = sched_torture_read_lock,
539 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
540 .readunlock = sched_torture_read_unlock,
541 .completed = sched_torture_completed,
542 .deferredfree = rcu_sync_torture_deferred_free,
543 .sync = sched_torture_synchronize,
544 .cb_barrier = NULL,
545 .stats = NULL,
546 .name = "sched_sync"
547};
548
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700549/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800550 * RCU torture writer kthread. Repeatedly substitutes a new structure
551 * for that pointed to by rcu_torture_current, freeing the old structure
552 * after a series of grace periods (the "pipeline").
553 */
554static int
555rcu_torture_writer(void *arg)
556{
557 int i;
558 long oldbatch = rcu_batches_completed();
559 struct rcu_torture *rp;
560 struct rcu_torture *old_rp;
561 static DEFINE_RCU_RANDOM(rand);
562
563 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800564 set_user_nice(current, 19);
565
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800566 do {
567 schedule_timeout_uninterruptible(1);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800568 if ((rp = rcu_torture_alloc()) == NULL)
569 continue;
570 rp->rtort_pipe_count = 0;
571 udelay(rcu_random(&rand) & 0x3ff);
572 old_rp = rcu_torture_current;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800573 rp->rtort_mbtest = 1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800574 rcu_assign_pointer(rcu_torture_current, rp);
575 smp_wmb();
Josh Triplettc8e5b162007-05-08 00:33:20 -0700576 if (old_rp) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800577 i = old_rp->rtort_pipe_count;
578 if (i > RCU_TORTURE_PIPE_LEN)
579 i = RCU_TORTURE_PIPE_LEN;
580 atomic_inc(&rcu_torture_wcount[i]);
581 old_rp->rtort_pipe_count++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700582 cur_ops->deferredfree(old_rp);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800583 }
584 rcu_torture_current_version++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700585 oldbatch = cur_ops->completed();
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700586 rcu_stutter_wait();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800587 } while (!kthread_should_stop() && !fullstop);
588 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
589 while (!kthread_should_stop())
590 schedule_timeout_uninterruptible(1);
591 return 0;
592}
593
594/*
Josh Triplettb772e1d2006-10-04 02:17:13 -0700595 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
596 * delay between calls.
597 */
598static int
599rcu_torture_fakewriter(void *arg)
600{
601 DEFINE_RCU_RANDOM(rand);
602
603 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
604 set_user_nice(current, 19);
605
606 do {
607 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
608 udelay(rcu_random(&rand) & 0x3ff);
609 cur_ops->sync();
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700610 rcu_stutter_wait();
Josh Triplettb772e1d2006-10-04 02:17:13 -0700611 } while (!kthread_should_stop() && !fullstop);
612
613 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
614 while (!kthread_should_stop())
615 schedule_timeout_uninterruptible(1);
616 return 0;
617}
618
619/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800620 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
621 * incrementing the corresponding element of the pipeline array. The
622 * counter in the element should never be greater than 1, otherwise, the
623 * RCU implementation is broken.
624 */
625static int
626rcu_torture_reader(void *arg)
627{
628 int completed;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700629 int idx;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800630 DEFINE_RCU_RANDOM(rand);
631 struct rcu_torture *p;
632 int pipe_count;
633
634 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800635 set_user_nice(current, 19);
636
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800637 do {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700638 idx = cur_ops->readlock();
639 completed = cur_ops->completed();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800640 p = rcu_dereference(rcu_torture_current);
641 if (p == NULL) {
642 /* Wait for rcu_torture_writer to get underway */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700643 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800644 schedule_timeout_interruptible(HZ);
645 continue;
646 }
Paul E. McKenney996417d2005-11-18 01:10:50 -0800647 if (p->rtort_mbtest == 0)
648 atomic_inc(&n_rcu_torture_mberror);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700649 cur_ops->readdelay(&rand);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800650 preempt_disable();
651 pipe_count = p->rtort_pipe_count;
652 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
653 /* Should not happen, but... */
654 pipe_count = RCU_TORTURE_PIPE_LEN;
655 }
656 ++__get_cpu_var(rcu_torture_count)[pipe_count];
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700657 completed = cur_ops->completed() - completed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800658 if (completed > RCU_TORTURE_PIPE_LEN) {
659 /* Should not happen, but... */
660 completed = RCU_TORTURE_PIPE_LEN;
661 }
662 ++__get_cpu_var(rcu_torture_batch)[completed];
663 preempt_enable();
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700664 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800665 schedule();
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700666 rcu_stutter_wait();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800667 } while (!kthread_should_stop() && !fullstop);
668 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
669 while (!kthread_should_stop())
670 schedule_timeout_uninterruptible(1);
671 return 0;
672}
673
674/*
675 * Create an RCU-torture statistics message in the specified buffer.
676 */
677static int
678rcu_torture_printk(char *page)
679{
680 int cnt = 0;
681 int cpu;
682 int i;
683 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
684 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
685
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800686 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800687 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
688 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
689 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
690 }
691 }
692 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
693 if (pipesummary[i] != 0)
694 break;
695 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700696 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800697 cnt += sprintf(&page[cnt],
Paul E. McKenney996417d2005-11-18 01:10:50 -0800698 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
699 "rtmbe: %d",
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800700 rcu_torture_current,
701 rcu_torture_current_version,
702 list_empty(&rcu_torture_freelist),
703 atomic_read(&n_rcu_torture_alloc),
704 atomic_read(&n_rcu_torture_alloc_fail),
Paul E. McKenney996417d2005-11-18 01:10:50 -0800705 atomic_read(&n_rcu_torture_free),
706 atomic_read(&n_rcu_torture_mberror));
707 if (atomic_read(&n_rcu_torture_mberror) != 0)
708 cnt += sprintf(&page[cnt], " !!!");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700709 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800710 if (i > 1) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800711 cnt += sprintf(&page[cnt], "!!! ");
Paul E. McKenney996417d2005-11-18 01:10:50 -0800712 atomic_inc(&n_rcu_torture_error);
Ingo Molnar5af970a2008-06-18 10:09:48 +0200713 WARN_ON_ONCE(1);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800714 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800715 cnt += sprintf(&page[cnt], "Reader Pipe: ");
716 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
717 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700718 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800719 cnt += sprintf(&page[cnt], "Reader Batch: ");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700720 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800721 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700722 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800723 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
724 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
725 cnt += sprintf(&page[cnt], " %d",
726 atomic_read(&rcu_torture_wcount[i]));
727 }
728 cnt += sprintf(&page[cnt], "\n");
Josh Triplettc8e5b162007-05-08 00:33:20 -0700729 if (cur_ops->stats)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700730 cnt += cur_ops->stats(&page[cnt]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800731 return cnt;
732}
733
734/*
735 * Print torture statistics. Caller must ensure that there is only
736 * one call to this function at a given time!!! This is normally
737 * accomplished by relying on the module system to only have one copy
738 * of the module loaded, and then by giving the rcu_torture_stats
739 * kthread full control (or the init/cleanup functions when rcu_torture_stats
740 * thread is not running).
741 */
742static void
743rcu_torture_stats_print(void)
744{
745 int cnt;
746
747 cnt = rcu_torture_printk(printk_buf);
748 printk(KERN_ALERT "%s", printk_buf);
749}
750
751/*
752 * Periodically prints torture statistics, if periodic statistics printing
753 * was specified via the stat_interval module parameter.
754 *
755 * No need to worry about fullstop here, since this one doesn't reference
756 * volatile state or register callbacks.
757 */
758static int
759rcu_torture_stats(void *arg)
760{
761 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
762 do {
763 schedule_timeout_interruptible(stat_interval * HZ);
764 rcu_torture_stats_print();
765 } while (!kthread_should_stop());
766 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
767 return 0;
768}
769
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800770static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
771
772/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
773 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
774 */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700775static void rcu_torture_shuffle_tasks(void)
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800776{
Mike Travisf70316d2008-04-04 18:11:06 -0700777 cpumask_t tmp_mask;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800778 int i;
779
Mike Travisf70316d2008-04-04 18:11:06 -0700780 cpus_setall(tmp_mask);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100781 get_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800782
783 /* No point in shuffling if there is only one online CPU (ex: UP) */
784 if (num_online_cpus() == 1) {
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100785 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800786 return;
787 }
788
789 if (rcu_idle_cpu != -1)
790 cpu_clear(rcu_idle_cpu, tmp_mask);
791
Mike Travisf70316d2008-04-04 18:11:06 -0700792 set_cpus_allowed_ptr(current, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800793
Josh Triplettc8e5b162007-05-08 00:33:20 -0700794 if (reader_tasks) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800795 for (i = 0; i < nrealreaders; i++)
796 if (reader_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -0700797 set_cpus_allowed_ptr(reader_tasks[i],
798 &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800799 }
800
Josh Triplettc8e5b162007-05-08 00:33:20 -0700801 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -0700802 for (i = 0; i < nfakewriters; i++)
803 if (fakewriter_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -0700804 set_cpus_allowed_ptr(fakewriter_tasks[i],
805 &tmp_mask);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700806 }
807
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800808 if (writer_task)
Mike Travisf70316d2008-04-04 18:11:06 -0700809 set_cpus_allowed_ptr(writer_task, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800810
811 if (stats_task)
Mike Travisf70316d2008-04-04 18:11:06 -0700812 set_cpus_allowed_ptr(stats_task, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800813
814 if (rcu_idle_cpu == -1)
815 rcu_idle_cpu = num_online_cpus() - 1;
816 else
817 rcu_idle_cpu--;
818
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100819 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800820}
821
822/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
823 * system to become idle at a time and cut off its timer ticks. This is meant
824 * to test the support for such tickless idle CPU in RCU.
825 */
826static int
827rcu_torture_shuffle(void *arg)
828{
829 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
830 do {
831 schedule_timeout_interruptible(shuffle_interval * HZ);
832 rcu_torture_shuffle_tasks();
833 } while (!kthread_should_stop());
834 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
835 return 0;
836}
837
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700838/* Cause the rcutorture test to "stutter", starting and stopping all
839 * threads periodically.
840 */
841static int
842rcu_torture_stutter(void *arg)
843{
844 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
845 do {
846 schedule_timeout_interruptible(stutter * HZ);
847 stutter_pause_test = 1;
848 if (!kthread_should_stop())
849 schedule_timeout_interruptible(stutter * HZ);
850 stutter_pause_test = 0;
851 } while (!kthread_should_stop());
852 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
853 return 0;
854}
855
Paul E. McKenney95c38322006-03-24 03:15:58 -0800856static inline void
857rcu_torture_print_module_parms(char *tag)
858{
Josh Triplettb772e1d2006-10-04 02:17:13 -0700859 printk(KERN_ALERT "%s" TORTURE_FLAG
860 "--- %s: nreaders=%d nfakewriters=%d "
Paul E. McKenney95c38322006-03-24 03:15:58 -0800861 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700862 "shuffle_interval=%d stutter=%d\n",
Josh Triplettb772e1d2006-10-04 02:17:13 -0700863 torture_type, tag, nrealreaders, nfakewriters,
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700864 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
865 stutter);
Paul E. McKenney95c38322006-03-24 03:15:58 -0800866}
867
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800868static void
869rcu_torture_cleanup(void)
870{
871 int i;
872
873 fullstop = 1;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700874 if (stutter_task) {
875 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
876 kthread_stop(stutter_task);
877 }
878 stutter_task = NULL;
Josh Triplettc8e5b162007-05-08 00:33:20 -0700879 if (shuffler_task) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800880 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
881 kthread_stop(shuffler_task);
882 }
883 shuffler_task = NULL;
884
Josh Triplettc8e5b162007-05-08 00:33:20 -0700885 if (writer_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800886 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
887 kthread_stop(writer_task);
888 }
889 writer_task = NULL;
890
Josh Triplettc8e5b162007-05-08 00:33:20 -0700891 if (reader_tasks) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800892 for (i = 0; i < nrealreaders; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -0700893 if (reader_tasks[i]) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800894 VERBOSE_PRINTK_STRING(
895 "Stopping rcu_torture_reader task");
896 kthread_stop(reader_tasks[i]);
897 }
898 reader_tasks[i] = NULL;
899 }
900 kfree(reader_tasks);
901 reader_tasks = NULL;
902 }
903 rcu_torture_current = NULL;
904
Josh Triplettc8e5b162007-05-08 00:33:20 -0700905 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -0700906 for (i = 0; i < nfakewriters; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -0700907 if (fakewriter_tasks[i]) {
Josh Triplettb772e1d2006-10-04 02:17:13 -0700908 VERBOSE_PRINTK_STRING(
909 "Stopping rcu_torture_fakewriter task");
910 kthread_stop(fakewriter_tasks[i]);
911 }
912 fakewriter_tasks[i] = NULL;
913 }
914 kfree(fakewriter_tasks);
915 fakewriter_tasks = NULL;
916 }
917
Josh Triplettc8e5b162007-05-08 00:33:20 -0700918 if (stats_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800919 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
920 kthread_stop(stats_task);
921 }
922 stats_task = NULL;
923
924 /* Wait for all RCU callbacks to fire. */
Paul E. McKenney23269742008-05-12 21:21:05 +0200925
926 if (cur_ops->cb_barrier != NULL)
927 cur_ops->cb_barrier();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800928
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800929 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700930
Josh Triplettc8e5b162007-05-08 00:33:20 -0700931 if (cur_ops->cleanup)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700932 cur_ops->cleanup();
Paul E. McKenney95c38322006-03-24 03:15:58 -0800933 if (atomic_read(&n_rcu_torture_error))
934 rcu_torture_print_module_parms("End of test: FAILURE");
935 else
936 rcu_torture_print_module_parms("End of test: SUCCESS");
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800937}
938
Josh Triplett6f8bc502007-05-08 00:25:24 -0700939static int __init
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800940rcu_torture_init(void)
941{
942 int i;
943 int cpu;
944 int firsterr = 0;
Josh Triplettade5fb82007-05-08 00:33:22 -0700945 static struct rcu_torture_ops *torture_ops[] =
946 { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops,
Paul E. McKenney23269742008-05-12 21:21:05 +0200947 &srcu_ops, &sched_ops, &sched_ops_sync, };
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800948
949 /* Process args and tell the world that the torturer is on the job. */
Josh Triplettade5fb82007-05-08 00:33:22 -0700950 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700951 cur_ops = torture_ops[i];
Josh Triplettade5fb82007-05-08 00:33:22 -0700952 if (strcmp(torture_type, cur_ops->name) == 0)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700953 break;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700954 }
Josh Triplettade5fb82007-05-08 00:33:22 -0700955 if (i == ARRAY_SIZE(torture_ops)) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700956 printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
957 torture_type);
958 return (-EINVAL);
959 }
Josh Triplettc8e5b162007-05-08 00:33:20 -0700960 if (cur_ops->init)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700961 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
962
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800963 if (nreaders >= 0)
964 nrealreaders = nreaders;
965 else
966 nrealreaders = 2 * num_online_cpus();
Paul E. McKenney95c38322006-03-24 03:15:58 -0800967 rcu_torture_print_module_parms("Start of test");
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800968 fullstop = 0;
969
970 /* Set up the freelist. */
971
972 INIT_LIST_HEAD(&rcu_torture_freelist);
Ahmed S. Darwish788e7702007-05-08 00:33:14 -0700973 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
Paul E. McKenney996417d2005-11-18 01:10:50 -0800974 rcu_tortures[i].rtort_mbtest = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800975 list_add_tail(&rcu_tortures[i].rtort_free,
976 &rcu_torture_freelist);
977 }
978
979 /* Initialize the statistics so that each run gets its own numbers. */
980
981 rcu_torture_current = NULL;
982 rcu_torture_current_version = 0;
983 atomic_set(&n_rcu_torture_alloc, 0);
984 atomic_set(&n_rcu_torture_alloc_fail, 0);
985 atomic_set(&n_rcu_torture_free, 0);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800986 atomic_set(&n_rcu_torture_mberror, 0);
987 atomic_set(&n_rcu_torture_error, 0);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800988 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
989 atomic_set(&rcu_torture_wcount[i], 0);
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800990 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800991 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
992 per_cpu(rcu_torture_count, cpu)[i] = 0;
993 per_cpu(rcu_torture_batch, cpu)[i] = 0;
994 }
995 }
996
997 /* Start up the kthreads. */
998
999 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1000 writer_task = kthread_run(rcu_torture_writer, NULL,
1001 "rcu_torture_writer");
1002 if (IS_ERR(writer_task)) {
1003 firsterr = PTR_ERR(writer_task);
1004 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1005 writer_task = NULL;
1006 goto unwind;
1007 }
Josh Triplettb772e1d2006-10-04 02:17:13 -07001008 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1009 GFP_KERNEL);
1010 if (fakewriter_tasks == NULL) {
1011 VERBOSE_PRINTK_ERRSTRING("out of memory");
1012 firsterr = -ENOMEM;
1013 goto unwind;
1014 }
1015 for (i = 0; i < nfakewriters; i++) {
1016 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1017 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1018 "rcu_torture_fakewriter");
1019 if (IS_ERR(fakewriter_tasks[i])) {
1020 firsterr = PTR_ERR(fakewriter_tasks[i]);
1021 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1022 fakewriter_tasks[i] = NULL;
1023 goto unwind;
1024 }
1025 }
Josh Triplett2860aab2006-10-04 02:17:11 -07001026 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001027 GFP_KERNEL);
1028 if (reader_tasks == NULL) {
1029 VERBOSE_PRINTK_ERRSTRING("out of memory");
1030 firsterr = -ENOMEM;
1031 goto unwind;
1032 }
1033 for (i = 0; i < nrealreaders; i++) {
1034 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1035 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1036 "rcu_torture_reader");
1037 if (IS_ERR(reader_tasks[i])) {
1038 firsterr = PTR_ERR(reader_tasks[i]);
1039 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1040 reader_tasks[i] = NULL;
1041 goto unwind;
1042 }
1043 }
1044 if (stat_interval > 0) {
1045 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1046 stats_task = kthread_run(rcu_torture_stats, NULL,
1047 "rcu_torture_stats");
1048 if (IS_ERR(stats_task)) {
1049 firsterr = PTR_ERR(stats_task);
1050 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1051 stats_task = NULL;
1052 goto unwind;
1053 }
1054 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001055 if (test_no_idle_hz) {
1056 rcu_idle_cpu = num_online_cpus() - 1;
1057 /* Create the shuffler thread */
1058 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1059 "rcu_torture_shuffle");
1060 if (IS_ERR(shuffler_task)) {
1061 firsterr = PTR_ERR(shuffler_task);
1062 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1063 shuffler_task = NULL;
1064 goto unwind;
1065 }
1066 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001067 if (stutter < 0)
1068 stutter = 0;
1069 if (stutter) {
1070 /* Create the stutter thread */
1071 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1072 "rcu_torture_stutter");
1073 if (IS_ERR(stutter_task)) {
1074 firsterr = PTR_ERR(stutter_task);
1075 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1076 stutter_task = NULL;
1077 goto unwind;
1078 }
1079 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001080 return 0;
1081
1082unwind:
1083 rcu_torture_cleanup();
1084 return firsterr;
1085}
1086
1087module_init(rcu_torture_init);
1088module_exit(rcu_torture_cleanup);