blob: 0ca7e9b290b06d55fabdc82ff8c44e18e9bd1ccd [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. */
60static int shuffle_interval = 5; /* Interval between shuffles (in sec)*/
Josh Triplett20d2e422006-10-04 02:17:15 -070061static char *torture_type = "rcu"; /* What RCU implementation to torture. */
Paul E. McKenneya241ec62005-10-30 15:03:12 -080062
Josh Triplettd6ad6712007-03-06 01:42:13 -080063module_param(nreaders, int, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080064MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
Josh Triplettd6ad6712007-03-06 01:42:13 -080065module_param(nfakewriters, int, 0444);
Josh Triplettb772e1d2006-10-04 02:17:13 -070066MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
Josh Triplettd6ad6712007-03-06 01:42:13 -080067module_param(stat_interval, int, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080068MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080069module_param(verbose, bool, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080070MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080071module_param(test_no_idle_hz, bool, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080072MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
Josh Triplettd6ad6712007-03-06 01:42:13 -080073module_param(shuffle_interval, int, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080074MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
Josh Triplettd6ad6712007-03-06 01:42:13 -080075module_param(torture_type, charp, 0444);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -070076MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070077
78#define TORTURE_FLAG "-torture:"
Paul E. McKenneya241ec62005-10-30 15:03:12 -080079#define PRINTK_STRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070080 do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -080081#define VERBOSE_PRINTK_STRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070082 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -080083#define VERBOSE_PRINTK_ERRSTRING(s) \
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070084 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -080085
86static char printk_buf[4096];
87
88static int nrealreaders;
89static struct task_struct *writer_task;
Josh Triplettb772e1d2006-10-04 02:17:13 -070090static struct task_struct **fakewriter_tasks;
Paul E. McKenneya241ec62005-10-30 15:03:12 -080091static struct task_struct **reader_tasks;
92static struct task_struct *stats_task;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080093static struct task_struct *shuffler_task;
Paul E. McKenneya241ec62005-10-30 15:03:12 -080094
95#define RCU_TORTURE_PIPE_LEN 10
96
97struct rcu_torture {
98 struct rcu_head rtort_rcu;
99 int rtort_pipe_count;
100 struct list_head rtort_free;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800101 int rtort_mbtest;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800102};
103
104static int fullstop = 0; /* stop generating callbacks at test end. */
105static LIST_HEAD(rcu_torture_freelist);
106static struct rcu_torture *rcu_torture_current = NULL;
107static long rcu_torture_current_version = 0;
108static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
109static DEFINE_SPINLOCK(rcu_torture_lock);
110static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
111 { 0 };
112static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
113 { 0 };
114static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700115static atomic_t n_rcu_torture_alloc;
116static atomic_t n_rcu_torture_alloc_fail;
117static atomic_t n_rcu_torture_free;
118static atomic_t n_rcu_torture_mberror;
119static atomic_t n_rcu_torture_error;
Josh Triplette3033732006-10-04 02:17:14 -0700120static struct list_head rcu_torture_removed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800121
122/*
123 * Allocate an element from the rcu_tortures pool.
124 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800125static struct rcu_torture *
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800126rcu_torture_alloc(void)
127{
128 struct list_head *p;
129
Ingo Molnaradac1662006-01-25 19:50:12 +0100130 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800131 if (list_empty(&rcu_torture_freelist)) {
132 atomic_inc(&n_rcu_torture_alloc_fail);
Ingo Molnaradac1662006-01-25 19:50:12 +0100133 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800134 return NULL;
135 }
136 atomic_inc(&n_rcu_torture_alloc);
137 p = rcu_torture_freelist.next;
138 list_del_init(p);
Ingo Molnaradac1662006-01-25 19:50:12 +0100139 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800140 return container_of(p, struct rcu_torture, rtort_free);
141}
142
143/*
144 * Free an element to the rcu_tortures pool.
145 */
146static void
147rcu_torture_free(struct rcu_torture *p)
148{
149 atomic_inc(&n_rcu_torture_free);
Ingo Molnaradac1662006-01-25 19:50:12 +0100150 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800151 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
Ingo Molnaradac1662006-01-25 19:50:12 +0100152 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800153}
154
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800155struct rcu_random_state {
156 unsigned long rrs_state;
Josh Triplett75cfef32006-10-04 02:17:12 -0700157 long rrs_count;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800158};
159
160#define RCU_RANDOM_MULT 39916801 /* prime */
161#define RCU_RANDOM_ADD 479001701 /* prime */
162#define RCU_RANDOM_REFRESH 10000
163
164#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
165
166/*
167 * Crude but fast random-number generator. Uses a linear congruential
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700168 * generator, with occasional help from cpu_clock().
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800169 */
Josh Triplett75cfef32006-10-04 02:17:12 -0700170static unsigned long
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800171rcu_random(struct rcu_random_state *rrsp)
172{
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800173 if (--rrsp->rrs_count < 0) {
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700174 rrsp->rrs_state +=
175 (unsigned long)cpu_clock(raw_smp_processor_id());
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800176 rrsp->rrs_count = RCU_RANDOM_REFRESH;
177 }
178 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
179 return swahw32(rrsp->rrs_state);
180}
181
182/*
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700183 * Operations vector for selecting different types of tests.
184 */
185
186struct rcu_torture_ops {
187 void (*init)(void);
188 void (*cleanup)(void);
189 int (*readlock)(void);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700190 void (*readdelay)(struct rcu_random_state *rrsp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700191 void (*readunlock)(int idx);
192 int (*completed)(void);
193 void (*deferredfree)(struct rcu_torture *p);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700194 void (*sync)(void);
Paul E. McKenney23269742008-05-12 21:21:05 +0200195 void (*cb_barrier)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700196 int (*stats)(char *page);
197 char *name;
198};
199static struct rcu_torture_ops *cur_ops = NULL;
200
201/*
202 * Definitions for rcu torture testing.
203 */
204
Josh Tripletta49a4af2006-09-29 01:59:30 -0700205static int rcu_torture_read_lock(void) __acquires(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700206{
207 rcu_read_lock();
208 return 0;
209}
210
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700211static void rcu_read_delay(struct rcu_random_state *rrsp)
212{
213 long delay;
214 const long longdelay = 200;
215
216 /* We want there to be long-running readers, but not all the time. */
217
218 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay);
219 if (!delay)
220 udelay(longdelay);
221}
222
Josh Tripletta49a4af2006-09-29 01:59:30 -0700223static void rcu_torture_read_unlock(int idx) __releases(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700224{
225 rcu_read_unlock();
226}
227
228static int rcu_torture_completed(void)
229{
230 return rcu_batches_completed();
231}
232
233static void
234rcu_torture_cb(struct rcu_head *p)
235{
236 int i;
237 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
238
239 if (fullstop) {
240 /* Test is ending, just drop callbacks on the floor. */
241 /* The next initialization will pick up the pieces. */
242 return;
243 }
244 i = rp->rtort_pipe_count;
245 if (i > RCU_TORTURE_PIPE_LEN)
246 i = RCU_TORTURE_PIPE_LEN;
247 atomic_inc(&rcu_torture_wcount[i]);
248 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
249 rp->rtort_mbtest = 0;
250 rcu_torture_free(rp);
251 } else
252 cur_ops->deferredfree(rp);
253}
254
255static void rcu_torture_deferred_free(struct rcu_torture *p)
256{
257 call_rcu(&p->rtort_rcu, rcu_torture_cb);
258}
259
260static struct rcu_torture_ops rcu_ops = {
261 .init = NULL,
262 .cleanup = NULL,
263 .readlock = rcu_torture_read_lock,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700264 .readdelay = rcu_read_delay,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700265 .readunlock = rcu_torture_read_unlock,
266 .completed = rcu_torture_completed,
267 .deferredfree = rcu_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700268 .sync = synchronize_rcu,
Paul E. McKenney23269742008-05-12 21:21:05 +0200269 .cb_barrier = rcu_barrier,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700270 .stats = NULL,
271 .name = "rcu"
272};
273
Josh Triplette3033732006-10-04 02:17:14 -0700274static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
275{
276 int i;
277 struct rcu_torture *rp;
278 struct rcu_torture *rp1;
279
280 cur_ops->sync();
281 list_add(&p->rtort_free, &rcu_torture_removed);
282 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
283 i = rp->rtort_pipe_count;
284 if (i > RCU_TORTURE_PIPE_LEN)
285 i = RCU_TORTURE_PIPE_LEN;
286 atomic_inc(&rcu_torture_wcount[i]);
287 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
288 rp->rtort_mbtest = 0;
289 list_del(&rp->rtort_free);
290 rcu_torture_free(rp);
291 }
292 }
293}
294
295static void rcu_sync_torture_init(void)
296{
297 INIT_LIST_HEAD(&rcu_torture_removed);
298}
299
Josh Triplett20d2e422006-10-04 02:17:15 -0700300static struct rcu_torture_ops rcu_sync_ops = {
301 .init = rcu_sync_torture_init,
302 .cleanup = NULL,
303 .readlock = rcu_torture_read_lock,
304 .readdelay = rcu_read_delay,
305 .readunlock = rcu_torture_read_unlock,
306 .completed = rcu_torture_completed,
307 .deferredfree = rcu_sync_torture_deferred_free,
308 .sync = synchronize_rcu,
Paul E. McKenney23269742008-05-12 21:21:05 +0200309 .cb_barrier = NULL,
Josh Triplett20d2e422006-10-04 02:17:15 -0700310 .stats = NULL,
311 .name = "rcu_sync"
312};
313
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700314/*
315 * Definitions for rcu_bh torture testing.
316 */
317
Josh Tripletta49a4af2006-09-29 01:59:30 -0700318static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700319{
320 rcu_read_lock_bh();
321 return 0;
322}
323
Josh Tripletta49a4af2006-09-29 01:59:30 -0700324static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700325{
326 rcu_read_unlock_bh();
327}
328
329static int rcu_bh_torture_completed(void)
330{
331 return rcu_batches_completed_bh();
332}
333
334static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
335{
336 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
337}
338
Josh Triplettb772e1d2006-10-04 02:17:13 -0700339struct rcu_bh_torture_synchronize {
340 struct rcu_head head;
341 struct completion completion;
342};
343
344static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
345{
346 struct rcu_bh_torture_synchronize *rcu;
347
348 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
349 complete(&rcu->completion);
350}
351
352static void rcu_bh_torture_synchronize(void)
353{
354 struct rcu_bh_torture_synchronize rcu;
355
356 init_completion(&rcu.completion);
357 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
358 wait_for_completion(&rcu.completion);
359}
360
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700361static struct rcu_torture_ops rcu_bh_ops = {
362 .init = NULL,
363 .cleanup = NULL,
364 .readlock = rcu_bh_torture_read_lock,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700365 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700366 .readunlock = rcu_bh_torture_read_unlock,
367 .completed = rcu_bh_torture_completed,
368 .deferredfree = rcu_bh_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700369 .sync = rcu_bh_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200370 .cb_barrier = rcu_barrier_bh,
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700371 .stats = NULL,
372 .name = "rcu_bh"
373};
374
Josh Triplett11a14702006-10-04 02:17:16 -0700375static struct rcu_torture_ops rcu_bh_sync_ops = {
376 .init = rcu_sync_torture_init,
377 .cleanup = NULL,
378 .readlock = rcu_bh_torture_read_lock,
379 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
380 .readunlock = rcu_bh_torture_read_unlock,
381 .completed = rcu_bh_torture_completed,
382 .deferredfree = rcu_sync_torture_deferred_free,
383 .sync = rcu_bh_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200384 .cb_barrier = NULL,
Josh Triplett11a14702006-10-04 02:17:16 -0700385 .stats = NULL,
386 .name = "rcu_bh_sync"
387};
388
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700389/*
390 * Definitions for srcu torture testing.
391 */
392
393static struct srcu_struct srcu_ctl;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700394
395static void srcu_torture_init(void)
396{
397 init_srcu_struct(&srcu_ctl);
Josh Triplette3033732006-10-04 02:17:14 -0700398 rcu_sync_torture_init();
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700399}
400
401static void srcu_torture_cleanup(void)
402{
403 synchronize_srcu(&srcu_ctl);
404 cleanup_srcu_struct(&srcu_ctl);
405}
406
Josh Triplett012d3ca2006-12-06 20:40:19 -0800407static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700408{
409 return srcu_read_lock(&srcu_ctl);
410}
411
412static void srcu_read_delay(struct rcu_random_state *rrsp)
413{
414 long delay;
415 const long uspertick = 1000000 / HZ;
416 const long longdelay = 10;
417
418 /* We want there to be long-running readers, but not all the time. */
419
420 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
421 if (!delay)
422 schedule_timeout_interruptible(longdelay);
423}
424
Josh Triplett012d3ca2006-12-06 20:40:19 -0800425static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700426{
427 srcu_read_unlock(&srcu_ctl, idx);
428}
429
430static int srcu_torture_completed(void)
431{
432 return srcu_batches_completed(&srcu_ctl);
433}
434
Josh Triplettb772e1d2006-10-04 02:17:13 -0700435static void srcu_torture_synchronize(void)
436{
437 synchronize_srcu(&srcu_ctl);
438}
439
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700440static int srcu_torture_stats(char *page)
441{
442 int cnt = 0;
443 int cpu;
444 int idx = srcu_ctl.completed & 0x1;
445
446 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
447 torture_type, TORTURE_FLAG, idx);
448 for_each_possible_cpu(cpu) {
449 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
450 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
451 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
452 }
453 cnt += sprintf(&page[cnt], "\n");
454 return cnt;
455}
456
457static struct rcu_torture_ops srcu_ops = {
458 .init = srcu_torture_init,
459 .cleanup = srcu_torture_cleanup,
460 .readlock = srcu_torture_read_lock,
461 .readdelay = srcu_read_delay,
462 .readunlock = srcu_torture_read_unlock,
463 .completed = srcu_torture_completed,
Josh Triplette3033732006-10-04 02:17:14 -0700464 .deferredfree = rcu_sync_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700465 .sync = srcu_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200466 .cb_barrier = NULL,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700467 .stats = srcu_torture_stats,
468 .name = "srcu"
469};
470
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700471/*
472 * Definitions for sched torture testing.
473 */
474
475static int sched_torture_read_lock(void)
476{
477 preempt_disable();
478 return 0;
479}
480
481static void sched_torture_read_unlock(int idx)
482{
483 preempt_enable();
484}
485
486static int sched_torture_completed(void)
487{
488 return 0;
489}
490
Paul E. McKenney23269742008-05-12 21:21:05 +0200491static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
492{
493 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
494}
495
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700496static void sched_torture_synchronize(void)
497{
498 synchronize_sched();
499}
500
501static struct rcu_torture_ops sched_ops = {
502 .init = rcu_sync_torture_init,
503 .cleanup = NULL,
504 .readlock = sched_torture_read_lock,
505 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
506 .readunlock = sched_torture_read_unlock,
507 .completed = sched_torture_completed,
Paul E. McKenney23269742008-05-12 21:21:05 +0200508 .deferredfree = rcu_sched_torture_deferred_free,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700509 .sync = sched_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200510 .cb_barrier = rcu_barrier_sched,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700511 .stats = NULL,
512 .name = "sched"
513};
514
Paul E. McKenney23269742008-05-12 21:21:05 +0200515static struct rcu_torture_ops sched_ops_sync = {
516 .init = rcu_sync_torture_init,
517 .cleanup = NULL,
518 .readlock = sched_torture_read_lock,
519 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
520 .readunlock = sched_torture_read_unlock,
521 .completed = sched_torture_completed,
522 .deferredfree = rcu_sync_torture_deferred_free,
523 .sync = sched_torture_synchronize,
524 .cb_barrier = NULL,
525 .stats = NULL,
526 .name = "sched_sync"
527};
528
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700529/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800530 * RCU torture writer kthread. Repeatedly substitutes a new structure
531 * for that pointed to by rcu_torture_current, freeing the old structure
532 * after a series of grace periods (the "pipeline").
533 */
534static int
535rcu_torture_writer(void *arg)
536{
537 int i;
538 long oldbatch = rcu_batches_completed();
539 struct rcu_torture *rp;
540 struct rcu_torture *old_rp;
541 static DEFINE_RCU_RANDOM(rand);
542
543 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800544 set_user_nice(current, 19);
545
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800546 do {
547 schedule_timeout_uninterruptible(1);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800548 if ((rp = rcu_torture_alloc()) == NULL)
549 continue;
550 rp->rtort_pipe_count = 0;
551 udelay(rcu_random(&rand) & 0x3ff);
552 old_rp = rcu_torture_current;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800553 rp->rtort_mbtest = 1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800554 rcu_assign_pointer(rcu_torture_current, rp);
555 smp_wmb();
Josh Triplettc8e5b162007-05-08 00:33:20 -0700556 if (old_rp) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800557 i = old_rp->rtort_pipe_count;
558 if (i > RCU_TORTURE_PIPE_LEN)
559 i = RCU_TORTURE_PIPE_LEN;
560 atomic_inc(&rcu_torture_wcount[i]);
561 old_rp->rtort_pipe_count++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700562 cur_ops->deferredfree(old_rp);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800563 }
564 rcu_torture_current_version++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700565 oldbatch = cur_ops->completed();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800566 } while (!kthread_should_stop() && !fullstop);
567 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
568 while (!kthread_should_stop())
569 schedule_timeout_uninterruptible(1);
570 return 0;
571}
572
573/*
Josh Triplettb772e1d2006-10-04 02:17:13 -0700574 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
575 * delay between calls.
576 */
577static int
578rcu_torture_fakewriter(void *arg)
579{
580 DEFINE_RCU_RANDOM(rand);
581
582 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
583 set_user_nice(current, 19);
584
585 do {
586 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
587 udelay(rcu_random(&rand) & 0x3ff);
588 cur_ops->sync();
589 } while (!kthread_should_stop() && !fullstop);
590
591 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
592 while (!kthread_should_stop())
593 schedule_timeout_uninterruptible(1);
594 return 0;
595}
596
597/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800598 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
599 * incrementing the corresponding element of the pipeline array. The
600 * counter in the element should never be greater than 1, otherwise, the
601 * RCU implementation is broken.
602 */
603static int
604rcu_torture_reader(void *arg)
605{
606 int completed;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700607 int idx;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800608 DEFINE_RCU_RANDOM(rand);
609 struct rcu_torture *p;
610 int pipe_count;
611
612 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800613 set_user_nice(current, 19);
614
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800615 do {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700616 idx = cur_ops->readlock();
617 completed = cur_ops->completed();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800618 p = rcu_dereference(rcu_torture_current);
619 if (p == NULL) {
620 /* Wait for rcu_torture_writer to get underway */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700621 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800622 schedule_timeout_interruptible(HZ);
623 continue;
624 }
Paul E. McKenney996417d2005-11-18 01:10:50 -0800625 if (p->rtort_mbtest == 0)
626 atomic_inc(&n_rcu_torture_mberror);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700627 cur_ops->readdelay(&rand);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800628 preempt_disable();
629 pipe_count = p->rtort_pipe_count;
630 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
631 /* Should not happen, but... */
632 pipe_count = RCU_TORTURE_PIPE_LEN;
633 }
634 ++__get_cpu_var(rcu_torture_count)[pipe_count];
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700635 completed = cur_ops->completed() - completed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800636 if (completed > RCU_TORTURE_PIPE_LEN) {
637 /* Should not happen, but... */
638 completed = RCU_TORTURE_PIPE_LEN;
639 }
640 ++__get_cpu_var(rcu_torture_batch)[completed];
641 preempt_enable();
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700642 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800643 schedule();
644 } while (!kthread_should_stop() && !fullstop);
645 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
646 while (!kthread_should_stop())
647 schedule_timeout_uninterruptible(1);
648 return 0;
649}
650
651/*
652 * Create an RCU-torture statistics message in the specified buffer.
653 */
654static int
655rcu_torture_printk(char *page)
656{
657 int cnt = 0;
658 int cpu;
659 int i;
660 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
661 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
662
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800663 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800664 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
665 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
666 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
667 }
668 }
669 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
670 if (pipesummary[i] != 0)
671 break;
672 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700673 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800674 cnt += sprintf(&page[cnt],
Paul E. McKenney996417d2005-11-18 01:10:50 -0800675 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
676 "rtmbe: %d",
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800677 rcu_torture_current,
678 rcu_torture_current_version,
679 list_empty(&rcu_torture_freelist),
680 atomic_read(&n_rcu_torture_alloc),
681 atomic_read(&n_rcu_torture_alloc_fail),
Paul E. McKenney996417d2005-11-18 01:10:50 -0800682 atomic_read(&n_rcu_torture_free),
683 atomic_read(&n_rcu_torture_mberror));
684 if (atomic_read(&n_rcu_torture_mberror) != 0)
685 cnt += sprintf(&page[cnt], " !!!");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700686 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800687 if (i > 1) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800688 cnt += sprintf(&page[cnt], "!!! ");
Paul E. McKenney996417d2005-11-18 01:10:50 -0800689 atomic_inc(&n_rcu_torture_error);
Ingo Molnar5af970a2008-06-18 10:09:48 +0200690 WARN_ON_ONCE(1);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800691 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800692 cnt += sprintf(&page[cnt], "Reader Pipe: ");
693 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
694 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700695 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800696 cnt += sprintf(&page[cnt], "Reader Batch: ");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700697 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800698 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700699 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800700 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
701 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
702 cnt += sprintf(&page[cnt], " %d",
703 atomic_read(&rcu_torture_wcount[i]));
704 }
705 cnt += sprintf(&page[cnt], "\n");
Josh Triplettc8e5b162007-05-08 00:33:20 -0700706 if (cur_ops->stats)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700707 cnt += cur_ops->stats(&page[cnt]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800708 return cnt;
709}
710
711/*
712 * Print torture statistics. Caller must ensure that there is only
713 * one call to this function at a given time!!! This is normally
714 * accomplished by relying on the module system to only have one copy
715 * of the module loaded, and then by giving the rcu_torture_stats
716 * kthread full control (or the init/cleanup functions when rcu_torture_stats
717 * thread is not running).
718 */
719static void
720rcu_torture_stats_print(void)
721{
722 int cnt;
723
724 cnt = rcu_torture_printk(printk_buf);
725 printk(KERN_ALERT "%s", printk_buf);
726}
727
728/*
729 * Periodically prints torture statistics, if periodic statistics printing
730 * was specified via the stat_interval module parameter.
731 *
732 * No need to worry about fullstop here, since this one doesn't reference
733 * volatile state or register callbacks.
734 */
735static int
736rcu_torture_stats(void *arg)
737{
738 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
739 do {
740 schedule_timeout_interruptible(stat_interval * HZ);
741 rcu_torture_stats_print();
742 } while (!kthread_should_stop());
743 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
744 return 0;
745}
746
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800747static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
748
749/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
750 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
751 */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700752static void rcu_torture_shuffle_tasks(void)
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800753{
Mike Travisf70316d2008-04-04 18:11:06 -0700754 cpumask_t tmp_mask;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800755 int i;
756
Mike Travisf70316d2008-04-04 18:11:06 -0700757 cpus_setall(tmp_mask);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100758 get_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800759
760 /* No point in shuffling if there is only one online CPU (ex: UP) */
761 if (num_online_cpus() == 1) {
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100762 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800763 return;
764 }
765
766 if (rcu_idle_cpu != -1)
767 cpu_clear(rcu_idle_cpu, tmp_mask);
768
Mike Travisf70316d2008-04-04 18:11:06 -0700769 set_cpus_allowed_ptr(current, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800770
Josh Triplettc8e5b162007-05-08 00:33:20 -0700771 if (reader_tasks) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800772 for (i = 0; i < nrealreaders; i++)
773 if (reader_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -0700774 set_cpus_allowed_ptr(reader_tasks[i],
775 &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800776 }
777
Josh Triplettc8e5b162007-05-08 00:33:20 -0700778 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -0700779 for (i = 0; i < nfakewriters; i++)
780 if (fakewriter_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -0700781 set_cpus_allowed_ptr(fakewriter_tasks[i],
782 &tmp_mask);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700783 }
784
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800785 if (writer_task)
Mike Travisf70316d2008-04-04 18:11:06 -0700786 set_cpus_allowed_ptr(writer_task, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800787
788 if (stats_task)
Mike Travisf70316d2008-04-04 18:11:06 -0700789 set_cpus_allowed_ptr(stats_task, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800790
791 if (rcu_idle_cpu == -1)
792 rcu_idle_cpu = num_online_cpus() - 1;
793 else
794 rcu_idle_cpu--;
795
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100796 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800797}
798
799/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
800 * system to become idle at a time and cut off its timer ticks. This is meant
801 * to test the support for such tickless idle CPU in RCU.
802 */
803static int
804rcu_torture_shuffle(void *arg)
805{
806 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
807 do {
808 schedule_timeout_interruptible(shuffle_interval * HZ);
809 rcu_torture_shuffle_tasks();
810 } while (!kthread_should_stop());
811 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
812 return 0;
813}
814
Paul E. McKenney95c38322006-03-24 03:15:58 -0800815static inline void
816rcu_torture_print_module_parms(char *tag)
817{
Josh Triplettb772e1d2006-10-04 02:17:13 -0700818 printk(KERN_ALERT "%s" TORTURE_FLAG
819 "--- %s: nreaders=%d nfakewriters=%d "
Paul E. McKenney95c38322006-03-24 03:15:58 -0800820 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
821 "shuffle_interval = %d\n",
Josh Triplettb772e1d2006-10-04 02:17:13 -0700822 torture_type, tag, nrealreaders, nfakewriters,
823 stat_interval, verbose, test_no_idle_hz, shuffle_interval);
Paul E. McKenney95c38322006-03-24 03:15:58 -0800824}
825
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800826static void
827rcu_torture_cleanup(void)
828{
829 int i;
830
831 fullstop = 1;
Josh Triplettc8e5b162007-05-08 00:33:20 -0700832 if (shuffler_task) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800833 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
834 kthread_stop(shuffler_task);
835 }
836 shuffler_task = NULL;
837
Josh Triplettc8e5b162007-05-08 00:33:20 -0700838 if (writer_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800839 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
840 kthread_stop(writer_task);
841 }
842 writer_task = NULL;
843
Josh Triplettc8e5b162007-05-08 00:33:20 -0700844 if (reader_tasks) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800845 for (i = 0; i < nrealreaders; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -0700846 if (reader_tasks[i]) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800847 VERBOSE_PRINTK_STRING(
848 "Stopping rcu_torture_reader task");
849 kthread_stop(reader_tasks[i]);
850 }
851 reader_tasks[i] = NULL;
852 }
853 kfree(reader_tasks);
854 reader_tasks = NULL;
855 }
856 rcu_torture_current = NULL;
857
Josh Triplettc8e5b162007-05-08 00:33:20 -0700858 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -0700859 for (i = 0; i < nfakewriters; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -0700860 if (fakewriter_tasks[i]) {
Josh Triplettb772e1d2006-10-04 02:17:13 -0700861 VERBOSE_PRINTK_STRING(
862 "Stopping rcu_torture_fakewriter task");
863 kthread_stop(fakewriter_tasks[i]);
864 }
865 fakewriter_tasks[i] = NULL;
866 }
867 kfree(fakewriter_tasks);
868 fakewriter_tasks = NULL;
869 }
870
Josh Triplettc8e5b162007-05-08 00:33:20 -0700871 if (stats_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800872 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
873 kthread_stop(stats_task);
874 }
875 stats_task = NULL;
876
877 /* Wait for all RCU callbacks to fire. */
Paul E. McKenney23269742008-05-12 21:21:05 +0200878
879 if (cur_ops->cb_barrier != NULL)
880 cur_ops->cb_barrier();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800881
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800882 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700883
Josh Triplettc8e5b162007-05-08 00:33:20 -0700884 if (cur_ops->cleanup)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700885 cur_ops->cleanup();
Paul E. McKenney95c38322006-03-24 03:15:58 -0800886 if (atomic_read(&n_rcu_torture_error))
887 rcu_torture_print_module_parms("End of test: FAILURE");
888 else
889 rcu_torture_print_module_parms("End of test: SUCCESS");
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800890}
891
Josh Triplett6f8bc5002007-05-08 00:25:24 -0700892static int __init
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800893rcu_torture_init(void)
894{
895 int i;
896 int cpu;
897 int firsterr = 0;
Josh Triplettade5fb82007-05-08 00:33:22 -0700898 static struct rcu_torture_ops *torture_ops[] =
899 { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops,
Paul E. McKenney23269742008-05-12 21:21:05 +0200900 &srcu_ops, &sched_ops, &sched_ops_sync, };
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800901
902 /* Process args and tell the world that the torturer is on the job. */
Josh Triplettade5fb82007-05-08 00:33:22 -0700903 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700904 cur_ops = torture_ops[i];
Josh Triplettade5fb82007-05-08 00:33:22 -0700905 if (strcmp(torture_type, cur_ops->name) == 0)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700906 break;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700907 }
Josh Triplettade5fb82007-05-08 00:33:22 -0700908 if (i == ARRAY_SIZE(torture_ops)) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700909 printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
910 torture_type);
911 return (-EINVAL);
912 }
Josh Triplettc8e5b162007-05-08 00:33:20 -0700913 if (cur_ops->init)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700914 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
915
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800916 if (nreaders >= 0)
917 nrealreaders = nreaders;
918 else
919 nrealreaders = 2 * num_online_cpus();
Paul E. McKenney95c38322006-03-24 03:15:58 -0800920 rcu_torture_print_module_parms("Start of test");
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800921 fullstop = 0;
922
923 /* Set up the freelist. */
924
925 INIT_LIST_HEAD(&rcu_torture_freelist);
Ahmed S. Darwish788e7702007-05-08 00:33:14 -0700926 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
Paul E. McKenney996417d2005-11-18 01:10:50 -0800927 rcu_tortures[i].rtort_mbtest = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800928 list_add_tail(&rcu_tortures[i].rtort_free,
929 &rcu_torture_freelist);
930 }
931
932 /* Initialize the statistics so that each run gets its own numbers. */
933
934 rcu_torture_current = NULL;
935 rcu_torture_current_version = 0;
936 atomic_set(&n_rcu_torture_alloc, 0);
937 atomic_set(&n_rcu_torture_alloc_fail, 0);
938 atomic_set(&n_rcu_torture_free, 0);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800939 atomic_set(&n_rcu_torture_mberror, 0);
940 atomic_set(&n_rcu_torture_error, 0);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800941 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
942 atomic_set(&rcu_torture_wcount[i], 0);
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800943 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800944 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
945 per_cpu(rcu_torture_count, cpu)[i] = 0;
946 per_cpu(rcu_torture_batch, cpu)[i] = 0;
947 }
948 }
949
950 /* Start up the kthreads. */
951
952 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
953 writer_task = kthread_run(rcu_torture_writer, NULL,
954 "rcu_torture_writer");
955 if (IS_ERR(writer_task)) {
956 firsterr = PTR_ERR(writer_task);
957 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
958 writer_task = NULL;
959 goto unwind;
960 }
Josh Triplettb772e1d2006-10-04 02:17:13 -0700961 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
962 GFP_KERNEL);
963 if (fakewriter_tasks == NULL) {
964 VERBOSE_PRINTK_ERRSTRING("out of memory");
965 firsterr = -ENOMEM;
966 goto unwind;
967 }
968 for (i = 0; i < nfakewriters; i++) {
969 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
970 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
971 "rcu_torture_fakewriter");
972 if (IS_ERR(fakewriter_tasks[i])) {
973 firsterr = PTR_ERR(fakewriter_tasks[i]);
974 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
975 fakewriter_tasks[i] = NULL;
976 goto unwind;
977 }
978 }
Josh Triplett2860aab2006-10-04 02:17:11 -0700979 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800980 GFP_KERNEL);
981 if (reader_tasks == NULL) {
982 VERBOSE_PRINTK_ERRSTRING("out of memory");
983 firsterr = -ENOMEM;
984 goto unwind;
985 }
986 for (i = 0; i < nrealreaders; i++) {
987 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
988 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
989 "rcu_torture_reader");
990 if (IS_ERR(reader_tasks[i])) {
991 firsterr = PTR_ERR(reader_tasks[i]);
992 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
993 reader_tasks[i] = NULL;
994 goto unwind;
995 }
996 }
997 if (stat_interval > 0) {
998 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
999 stats_task = kthread_run(rcu_torture_stats, NULL,
1000 "rcu_torture_stats");
1001 if (IS_ERR(stats_task)) {
1002 firsterr = PTR_ERR(stats_task);
1003 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1004 stats_task = NULL;
1005 goto unwind;
1006 }
1007 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001008 if (test_no_idle_hz) {
1009 rcu_idle_cpu = num_online_cpus() - 1;
1010 /* Create the shuffler thread */
1011 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1012 "rcu_torture_shuffle");
1013 if (IS_ERR(shuffler_task)) {
1014 firsterr = PTR_ERR(shuffler_task);
1015 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1016 shuffler_task = NULL;
1017 goto unwind;
1018 }
1019 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001020 return 0;
1021
1022unwind:
1023 rcu_torture_cleanup();
1024 return firsterr;
1025}
1026
1027module_init(rcu_torture_init);
1028module_exit(rcu_torture_cleanup);