blob: 98ae7d1682252f300d3aca7bb9cdf4348934d373 [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. McKenneya241ec62005-10-30 15:03:12 -0800128/*
129 * Allocate an element from the rcu_tortures pool.
130 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800131static struct rcu_torture *
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800132rcu_torture_alloc(void)
133{
134 struct list_head *p;
135
Ingo Molnaradac1662006-01-25 19:50:12 +0100136 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800137 if (list_empty(&rcu_torture_freelist)) {
138 atomic_inc(&n_rcu_torture_alloc_fail);
Ingo Molnaradac1662006-01-25 19:50:12 +0100139 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800140 return NULL;
141 }
142 atomic_inc(&n_rcu_torture_alloc);
143 p = rcu_torture_freelist.next;
144 list_del_init(p);
Ingo Molnaradac1662006-01-25 19:50:12 +0100145 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800146 return container_of(p, struct rcu_torture, rtort_free);
147}
148
149/*
150 * Free an element to the rcu_tortures pool.
151 */
152static void
153rcu_torture_free(struct rcu_torture *p)
154{
155 atomic_inc(&n_rcu_torture_free);
Ingo Molnaradac1662006-01-25 19:50:12 +0100156 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800157 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
Ingo Molnaradac1662006-01-25 19:50:12 +0100158 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800159}
160
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800161struct rcu_random_state {
162 unsigned long rrs_state;
Josh Triplett75cfef32006-10-04 02:17:12 -0700163 long rrs_count;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800164};
165
166#define RCU_RANDOM_MULT 39916801 /* prime */
167#define RCU_RANDOM_ADD 479001701 /* prime */
168#define RCU_RANDOM_REFRESH 10000
169
170#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
171
172/*
173 * Crude but fast random-number generator. Uses a linear congruential
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700174 * generator, with occasional help from cpu_clock().
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800175 */
Josh Triplett75cfef32006-10-04 02:17:12 -0700176static unsigned long
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800177rcu_random(struct rcu_random_state *rrsp)
178{
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800179 if (--rrsp->rrs_count < 0) {
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700180 rrsp->rrs_state +=
181 (unsigned long)cpu_clock(raw_smp_processor_id());
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800182 rrsp->rrs_count = RCU_RANDOM_REFRESH;
183 }
184 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
185 return swahw32(rrsp->rrs_state);
186}
187
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700188static void
189rcu_stutter_wait(void)
190{
191 while (stutter_pause_test)
192 schedule_timeout_interruptible(1);
193}
194
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800195/*
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700196 * Operations vector for selecting different types of tests.
197 */
198
199struct rcu_torture_ops {
200 void (*init)(void);
201 void (*cleanup)(void);
202 int (*readlock)(void);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700203 void (*readdelay)(struct rcu_random_state *rrsp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700204 void (*readunlock)(int idx);
205 int (*completed)(void);
206 void (*deferredfree)(struct rcu_torture *p);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700207 void (*sync)(void);
Paul E. McKenney23269742008-05-12 21:21:05 +0200208 void (*cb_barrier)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700209 int (*stats)(char *page);
210 char *name;
211};
212static struct rcu_torture_ops *cur_ops = NULL;
213
214/*
215 * Definitions for rcu torture testing.
216 */
217
Josh Tripletta49a4af2006-09-29 01:59:30 -0700218static int rcu_torture_read_lock(void) __acquires(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700219{
220 rcu_read_lock();
221 return 0;
222}
223
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700224static void rcu_read_delay(struct rcu_random_state *rrsp)
225{
226 long delay;
227 const long longdelay = 200;
228
229 /* We want there to be long-running readers, but not all the time. */
230
231 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay);
232 if (!delay)
233 udelay(longdelay);
234}
235
Josh Tripletta49a4af2006-09-29 01:59:30 -0700236static void rcu_torture_read_unlock(int idx) __releases(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700237{
238 rcu_read_unlock();
239}
240
241static int rcu_torture_completed(void)
242{
243 return rcu_batches_completed();
244}
245
246static void
247rcu_torture_cb(struct rcu_head *p)
248{
249 int i;
250 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
251
252 if (fullstop) {
253 /* Test is ending, just drop callbacks on the floor. */
254 /* The next initialization will pick up the pieces. */
255 return;
256 }
257 i = rp->rtort_pipe_count;
258 if (i > RCU_TORTURE_PIPE_LEN)
259 i = RCU_TORTURE_PIPE_LEN;
260 atomic_inc(&rcu_torture_wcount[i]);
261 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
262 rp->rtort_mbtest = 0;
263 rcu_torture_free(rp);
264 } else
265 cur_ops->deferredfree(rp);
266}
267
268static void rcu_torture_deferred_free(struct rcu_torture *p)
269{
270 call_rcu(&p->rtort_rcu, rcu_torture_cb);
271}
272
273static struct rcu_torture_ops rcu_ops = {
274 .init = NULL,
275 .cleanup = NULL,
276 .readlock = rcu_torture_read_lock,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700277 .readdelay = rcu_read_delay,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700278 .readunlock = rcu_torture_read_unlock,
279 .completed = rcu_torture_completed,
280 .deferredfree = rcu_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700281 .sync = synchronize_rcu,
Paul E. McKenney23269742008-05-12 21:21:05 +0200282 .cb_barrier = rcu_barrier,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700283 .stats = NULL,
284 .name = "rcu"
285};
286
Josh Triplette3033732006-10-04 02:17:14 -0700287static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
288{
289 int i;
290 struct rcu_torture *rp;
291 struct rcu_torture *rp1;
292
293 cur_ops->sync();
294 list_add(&p->rtort_free, &rcu_torture_removed);
295 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
296 i = rp->rtort_pipe_count;
297 if (i > RCU_TORTURE_PIPE_LEN)
298 i = RCU_TORTURE_PIPE_LEN;
299 atomic_inc(&rcu_torture_wcount[i]);
300 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
301 rp->rtort_mbtest = 0;
302 list_del(&rp->rtort_free);
303 rcu_torture_free(rp);
304 }
305 }
306}
307
308static void rcu_sync_torture_init(void)
309{
310 INIT_LIST_HEAD(&rcu_torture_removed);
311}
312
Josh Triplett20d2e422006-10-04 02:17:15 -0700313static struct rcu_torture_ops rcu_sync_ops = {
314 .init = rcu_sync_torture_init,
315 .cleanup = NULL,
316 .readlock = rcu_torture_read_lock,
317 .readdelay = rcu_read_delay,
318 .readunlock = rcu_torture_read_unlock,
319 .completed = rcu_torture_completed,
320 .deferredfree = rcu_sync_torture_deferred_free,
321 .sync = synchronize_rcu,
Paul E. McKenney23269742008-05-12 21:21:05 +0200322 .cb_barrier = NULL,
Josh Triplett20d2e422006-10-04 02:17:15 -0700323 .stats = NULL,
324 .name = "rcu_sync"
325};
326
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700327/*
328 * Definitions for rcu_bh torture testing.
329 */
330
Josh Tripletta49a4af2006-09-29 01:59:30 -0700331static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700332{
333 rcu_read_lock_bh();
334 return 0;
335}
336
Josh Tripletta49a4af2006-09-29 01:59:30 -0700337static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700338{
339 rcu_read_unlock_bh();
340}
341
342static int rcu_bh_torture_completed(void)
343{
344 return rcu_batches_completed_bh();
345}
346
347static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
348{
349 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
350}
351
Josh Triplettb772e1d2006-10-04 02:17:13 -0700352struct rcu_bh_torture_synchronize {
353 struct rcu_head head;
354 struct completion completion;
355};
356
357static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
358{
359 struct rcu_bh_torture_synchronize *rcu;
360
361 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
362 complete(&rcu->completion);
363}
364
365static void rcu_bh_torture_synchronize(void)
366{
367 struct rcu_bh_torture_synchronize rcu;
368
369 init_completion(&rcu.completion);
370 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
371 wait_for_completion(&rcu.completion);
372}
373
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700374static struct rcu_torture_ops rcu_bh_ops = {
375 .init = NULL,
376 .cleanup = NULL,
377 .readlock = rcu_bh_torture_read_lock,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700378 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700379 .readunlock = rcu_bh_torture_read_unlock,
380 .completed = rcu_bh_torture_completed,
381 .deferredfree = rcu_bh_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700382 .sync = rcu_bh_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200383 .cb_barrier = rcu_barrier_bh,
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700384 .stats = NULL,
385 .name = "rcu_bh"
386};
387
Josh Triplett11a14702006-10-04 02:17:16 -0700388static struct rcu_torture_ops rcu_bh_sync_ops = {
389 .init = rcu_sync_torture_init,
390 .cleanup = NULL,
391 .readlock = rcu_bh_torture_read_lock,
392 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
393 .readunlock = rcu_bh_torture_read_unlock,
394 .completed = rcu_bh_torture_completed,
395 .deferredfree = rcu_sync_torture_deferred_free,
396 .sync = rcu_bh_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200397 .cb_barrier = NULL,
Josh Triplett11a14702006-10-04 02:17:16 -0700398 .stats = NULL,
399 .name = "rcu_bh_sync"
400};
401
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700402/*
403 * Definitions for srcu torture testing.
404 */
405
406static struct srcu_struct srcu_ctl;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700407
408static void srcu_torture_init(void)
409{
410 init_srcu_struct(&srcu_ctl);
Josh Triplette3033732006-10-04 02:17:14 -0700411 rcu_sync_torture_init();
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700412}
413
414static void srcu_torture_cleanup(void)
415{
416 synchronize_srcu(&srcu_ctl);
417 cleanup_srcu_struct(&srcu_ctl);
418}
419
Josh Triplett012d3ca2006-12-06 20:40:19 -0800420static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700421{
422 return srcu_read_lock(&srcu_ctl);
423}
424
425static void srcu_read_delay(struct rcu_random_state *rrsp)
426{
427 long delay;
428 const long uspertick = 1000000 / HZ;
429 const long longdelay = 10;
430
431 /* We want there to be long-running readers, but not all the time. */
432
433 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
434 if (!delay)
435 schedule_timeout_interruptible(longdelay);
436}
437
Josh Triplett012d3ca2006-12-06 20:40:19 -0800438static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700439{
440 srcu_read_unlock(&srcu_ctl, idx);
441}
442
443static int srcu_torture_completed(void)
444{
445 return srcu_batches_completed(&srcu_ctl);
446}
447
Josh Triplettb772e1d2006-10-04 02:17:13 -0700448static void srcu_torture_synchronize(void)
449{
450 synchronize_srcu(&srcu_ctl);
451}
452
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700453static int srcu_torture_stats(char *page)
454{
455 int cnt = 0;
456 int cpu;
457 int idx = srcu_ctl.completed & 0x1;
458
459 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
460 torture_type, TORTURE_FLAG, idx);
461 for_each_possible_cpu(cpu) {
462 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
463 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
464 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
465 }
466 cnt += sprintf(&page[cnt], "\n");
467 return cnt;
468}
469
470static struct rcu_torture_ops srcu_ops = {
471 .init = srcu_torture_init,
472 .cleanup = srcu_torture_cleanup,
473 .readlock = srcu_torture_read_lock,
474 .readdelay = srcu_read_delay,
475 .readunlock = srcu_torture_read_unlock,
476 .completed = srcu_torture_completed,
Josh Triplette3033732006-10-04 02:17:14 -0700477 .deferredfree = rcu_sync_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700478 .sync = srcu_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200479 .cb_barrier = NULL,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700480 .stats = srcu_torture_stats,
481 .name = "srcu"
482};
483
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700484/*
485 * Definitions for sched torture testing.
486 */
487
488static int sched_torture_read_lock(void)
489{
490 preempt_disable();
491 return 0;
492}
493
494static void sched_torture_read_unlock(int idx)
495{
496 preempt_enable();
497}
498
499static int sched_torture_completed(void)
500{
501 return 0;
502}
503
Paul E. McKenney23269742008-05-12 21:21:05 +0200504static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
505{
506 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
507}
508
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700509static void sched_torture_synchronize(void)
510{
511 synchronize_sched();
512}
513
514static struct rcu_torture_ops sched_ops = {
515 .init = rcu_sync_torture_init,
516 .cleanup = NULL,
517 .readlock = sched_torture_read_lock,
518 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
519 .readunlock = sched_torture_read_unlock,
520 .completed = sched_torture_completed,
Paul E. McKenney23269742008-05-12 21:21:05 +0200521 .deferredfree = rcu_sched_torture_deferred_free,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700522 .sync = sched_torture_synchronize,
Paul E. McKenney23269742008-05-12 21:21:05 +0200523 .cb_barrier = rcu_barrier_sched,
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700524 .stats = NULL,
525 .name = "sched"
526};
527
Paul E. McKenney23269742008-05-12 21:21:05 +0200528static struct rcu_torture_ops sched_ops_sync = {
529 .init = rcu_sync_torture_init,
530 .cleanup = NULL,
531 .readlock = sched_torture_read_lock,
532 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
533 .readunlock = sched_torture_read_unlock,
534 .completed = sched_torture_completed,
535 .deferredfree = rcu_sync_torture_deferred_free,
536 .sync = sched_torture_synchronize,
537 .cb_barrier = NULL,
538 .stats = NULL,
539 .name = "sched_sync"
540};
541
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700542/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800543 * RCU torture writer kthread. Repeatedly substitutes a new structure
544 * for that pointed to by rcu_torture_current, freeing the old structure
545 * after a series of grace periods (the "pipeline").
546 */
547static int
548rcu_torture_writer(void *arg)
549{
550 int i;
551 long oldbatch = rcu_batches_completed();
552 struct rcu_torture *rp;
553 struct rcu_torture *old_rp;
554 static DEFINE_RCU_RANDOM(rand);
555
556 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800557 set_user_nice(current, 19);
558
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800559 do {
560 schedule_timeout_uninterruptible(1);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800561 if ((rp = rcu_torture_alloc()) == NULL)
562 continue;
563 rp->rtort_pipe_count = 0;
564 udelay(rcu_random(&rand) & 0x3ff);
565 old_rp = rcu_torture_current;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800566 rp->rtort_mbtest = 1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800567 rcu_assign_pointer(rcu_torture_current, rp);
568 smp_wmb();
Josh Triplettc8e5b162007-05-08 00:33:20 -0700569 if (old_rp) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800570 i = old_rp->rtort_pipe_count;
571 if (i > RCU_TORTURE_PIPE_LEN)
572 i = RCU_TORTURE_PIPE_LEN;
573 atomic_inc(&rcu_torture_wcount[i]);
574 old_rp->rtort_pipe_count++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700575 cur_ops->deferredfree(old_rp);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800576 }
577 rcu_torture_current_version++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700578 oldbatch = cur_ops->completed();
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700579 rcu_stutter_wait();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800580 } while (!kthread_should_stop() && !fullstop);
581 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
582 while (!kthread_should_stop())
583 schedule_timeout_uninterruptible(1);
584 return 0;
585}
586
587/*
Josh Triplettb772e1d2006-10-04 02:17:13 -0700588 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
589 * delay between calls.
590 */
591static int
592rcu_torture_fakewriter(void *arg)
593{
594 DEFINE_RCU_RANDOM(rand);
595
596 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
597 set_user_nice(current, 19);
598
599 do {
600 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
601 udelay(rcu_random(&rand) & 0x3ff);
602 cur_ops->sync();
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700603 rcu_stutter_wait();
Josh Triplettb772e1d2006-10-04 02:17:13 -0700604 } while (!kthread_should_stop() && !fullstop);
605
606 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
607 while (!kthread_should_stop())
608 schedule_timeout_uninterruptible(1);
609 return 0;
610}
611
612/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800613 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
614 * incrementing the corresponding element of the pipeline array. The
615 * counter in the element should never be greater than 1, otherwise, the
616 * RCU implementation is broken.
617 */
618static int
619rcu_torture_reader(void *arg)
620{
621 int completed;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700622 int idx;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800623 DEFINE_RCU_RANDOM(rand);
624 struct rcu_torture *p;
625 int pipe_count;
626
627 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800628 set_user_nice(current, 19);
629
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800630 do {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700631 idx = cur_ops->readlock();
632 completed = cur_ops->completed();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800633 p = rcu_dereference(rcu_torture_current);
634 if (p == NULL) {
635 /* Wait for rcu_torture_writer to get underway */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700636 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800637 schedule_timeout_interruptible(HZ);
638 continue;
639 }
Paul E. McKenney996417d2005-11-18 01:10:50 -0800640 if (p->rtort_mbtest == 0)
641 atomic_inc(&n_rcu_torture_mberror);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700642 cur_ops->readdelay(&rand);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800643 preempt_disable();
644 pipe_count = p->rtort_pipe_count;
645 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
646 /* Should not happen, but... */
647 pipe_count = RCU_TORTURE_PIPE_LEN;
648 }
649 ++__get_cpu_var(rcu_torture_count)[pipe_count];
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700650 completed = cur_ops->completed() - completed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800651 if (completed > RCU_TORTURE_PIPE_LEN) {
652 /* Should not happen, but... */
653 completed = RCU_TORTURE_PIPE_LEN;
654 }
655 ++__get_cpu_var(rcu_torture_batch)[completed];
656 preempt_enable();
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700657 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800658 schedule();
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700659 rcu_stutter_wait();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800660 } while (!kthread_should_stop() && !fullstop);
661 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
662 while (!kthread_should_stop())
663 schedule_timeout_uninterruptible(1);
664 return 0;
665}
666
667/*
668 * Create an RCU-torture statistics message in the specified buffer.
669 */
670static int
671rcu_torture_printk(char *page)
672{
673 int cnt = 0;
674 int cpu;
675 int i;
676 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
677 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
678
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800679 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800680 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
681 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
682 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
683 }
684 }
685 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
686 if (pipesummary[i] != 0)
687 break;
688 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700689 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800690 cnt += sprintf(&page[cnt],
Paul E. McKenney996417d2005-11-18 01:10:50 -0800691 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
692 "rtmbe: %d",
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800693 rcu_torture_current,
694 rcu_torture_current_version,
695 list_empty(&rcu_torture_freelist),
696 atomic_read(&n_rcu_torture_alloc),
697 atomic_read(&n_rcu_torture_alloc_fail),
Paul E. McKenney996417d2005-11-18 01:10:50 -0800698 atomic_read(&n_rcu_torture_free),
699 atomic_read(&n_rcu_torture_mberror));
700 if (atomic_read(&n_rcu_torture_mberror) != 0)
701 cnt += sprintf(&page[cnt], " !!!");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700702 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800703 if (i > 1) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800704 cnt += sprintf(&page[cnt], "!!! ");
Paul E. McKenney996417d2005-11-18 01:10:50 -0800705 atomic_inc(&n_rcu_torture_error);
Ingo Molnar5af970a2008-06-18 10:09:48 +0200706 WARN_ON_ONCE(1);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800707 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800708 cnt += sprintf(&page[cnt], "Reader Pipe: ");
709 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
710 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700711 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800712 cnt += sprintf(&page[cnt], "Reader Batch: ");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700713 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800714 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700715 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800716 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
717 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
718 cnt += sprintf(&page[cnt], " %d",
719 atomic_read(&rcu_torture_wcount[i]));
720 }
721 cnt += sprintf(&page[cnt], "\n");
Josh Triplettc8e5b162007-05-08 00:33:20 -0700722 if (cur_ops->stats)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700723 cnt += cur_ops->stats(&page[cnt]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800724 return cnt;
725}
726
727/*
728 * Print torture statistics. Caller must ensure that there is only
729 * one call to this function at a given time!!! This is normally
730 * accomplished by relying on the module system to only have one copy
731 * of the module loaded, and then by giving the rcu_torture_stats
732 * kthread full control (or the init/cleanup functions when rcu_torture_stats
733 * thread is not running).
734 */
735static void
736rcu_torture_stats_print(void)
737{
738 int cnt;
739
740 cnt = rcu_torture_printk(printk_buf);
741 printk(KERN_ALERT "%s", printk_buf);
742}
743
744/*
745 * Periodically prints torture statistics, if periodic statistics printing
746 * was specified via the stat_interval module parameter.
747 *
748 * No need to worry about fullstop here, since this one doesn't reference
749 * volatile state or register callbacks.
750 */
751static int
752rcu_torture_stats(void *arg)
753{
754 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
755 do {
756 schedule_timeout_interruptible(stat_interval * HZ);
757 rcu_torture_stats_print();
758 } while (!kthread_should_stop());
759 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
760 return 0;
761}
762
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800763static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
764
765/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
766 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
767 */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700768static void rcu_torture_shuffle_tasks(void)
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800769{
Mike Travisf70316d2008-04-04 18:11:06 -0700770 cpumask_t tmp_mask;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800771 int i;
772
Mike Travisf70316d2008-04-04 18:11:06 -0700773 cpus_setall(tmp_mask);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100774 get_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800775
776 /* No point in shuffling if there is only one online CPU (ex: UP) */
777 if (num_online_cpus() == 1) {
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100778 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800779 return;
780 }
781
782 if (rcu_idle_cpu != -1)
783 cpu_clear(rcu_idle_cpu, tmp_mask);
784
Mike Travisf70316d2008-04-04 18:11:06 -0700785 set_cpus_allowed_ptr(current, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800786
Josh Triplettc8e5b162007-05-08 00:33:20 -0700787 if (reader_tasks) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800788 for (i = 0; i < nrealreaders; i++)
789 if (reader_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -0700790 set_cpus_allowed_ptr(reader_tasks[i],
791 &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800792 }
793
Josh Triplettc8e5b162007-05-08 00:33:20 -0700794 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -0700795 for (i = 0; i < nfakewriters; i++)
796 if (fakewriter_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -0700797 set_cpus_allowed_ptr(fakewriter_tasks[i],
798 &tmp_mask);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700799 }
800
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800801 if (writer_task)
Mike Travisf70316d2008-04-04 18:11:06 -0700802 set_cpus_allowed_ptr(writer_task, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800803
804 if (stats_task)
Mike Travisf70316d2008-04-04 18:11:06 -0700805 set_cpus_allowed_ptr(stats_task, &tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800806
807 if (rcu_idle_cpu == -1)
808 rcu_idle_cpu = num_online_cpus() - 1;
809 else
810 rcu_idle_cpu--;
811
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100812 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800813}
814
815/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
816 * system to become idle at a time and cut off its timer ticks. This is meant
817 * to test the support for such tickless idle CPU in RCU.
818 */
819static int
820rcu_torture_shuffle(void *arg)
821{
822 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
823 do {
824 schedule_timeout_interruptible(shuffle_interval * HZ);
825 rcu_torture_shuffle_tasks();
826 } while (!kthread_should_stop());
827 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
828 return 0;
829}
830
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700831/* Cause the rcutorture test to "stutter", starting and stopping all
832 * threads periodically.
833 */
834static int
835rcu_torture_stutter(void *arg)
836{
837 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
838 do {
839 schedule_timeout_interruptible(stutter * HZ);
840 stutter_pause_test = 1;
841 if (!kthread_should_stop())
842 schedule_timeout_interruptible(stutter * HZ);
843 stutter_pause_test = 0;
844 } while (!kthread_should_stop());
845 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
846 return 0;
847}
848
Paul E. McKenney95c38322006-03-24 03:15:58 -0800849static inline void
850rcu_torture_print_module_parms(char *tag)
851{
Josh Triplettb772e1d2006-10-04 02:17:13 -0700852 printk(KERN_ALERT "%s" TORTURE_FLAG
853 "--- %s: nreaders=%d nfakewriters=%d "
Paul E. McKenney95c38322006-03-24 03:15:58 -0800854 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700855 "shuffle_interval=%d stutter=%d\n",
Josh Triplettb772e1d2006-10-04 02:17:13 -0700856 torture_type, tag, nrealreaders, nfakewriters,
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700857 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
858 stutter);
Paul E. McKenney95c38322006-03-24 03:15:58 -0800859}
860
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800861static void
862rcu_torture_cleanup(void)
863{
864 int i;
865
866 fullstop = 1;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700867 if (stutter_task) {
868 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
869 kthread_stop(stutter_task);
870 }
871 stutter_task = NULL;
Josh Triplettc8e5b162007-05-08 00:33:20 -0700872 if (shuffler_task) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800873 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
874 kthread_stop(shuffler_task);
875 }
876 shuffler_task = NULL;
877
Josh Triplettc8e5b162007-05-08 00:33:20 -0700878 if (writer_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800879 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
880 kthread_stop(writer_task);
881 }
882 writer_task = NULL;
883
Josh Triplettc8e5b162007-05-08 00:33:20 -0700884 if (reader_tasks) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800885 for (i = 0; i < nrealreaders; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -0700886 if (reader_tasks[i]) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800887 VERBOSE_PRINTK_STRING(
888 "Stopping rcu_torture_reader task");
889 kthread_stop(reader_tasks[i]);
890 }
891 reader_tasks[i] = NULL;
892 }
893 kfree(reader_tasks);
894 reader_tasks = NULL;
895 }
896 rcu_torture_current = NULL;
897
Josh Triplettc8e5b162007-05-08 00:33:20 -0700898 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -0700899 for (i = 0; i < nfakewriters; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -0700900 if (fakewriter_tasks[i]) {
Josh Triplettb772e1d2006-10-04 02:17:13 -0700901 VERBOSE_PRINTK_STRING(
902 "Stopping rcu_torture_fakewriter task");
903 kthread_stop(fakewriter_tasks[i]);
904 }
905 fakewriter_tasks[i] = NULL;
906 }
907 kfree(fakewriter_tasks);
908 fakewriter_tasks = NULL;
909 }
910
Josh Triplettc8e5b162007-05-08 00:33:20 -0700911 if (stats_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800912 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
913 kthread_stop(stats_task);
914 }
915 stats_task = NULL;
916
917 /* Wait for all RCU callbacks to fire. */
Paul E. McKenney23269742008-05-12 21:21:05 +0200918
919 if (cur_ops->cb_barrier != NULL)
920 cur_ops->cb_barrier();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800921
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800922 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700923
Josh Triplettc8e5b162007-05-08 00:33:20 -0700924 if (cur_ops->cleanup)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700925 cur_ops->cleanup();
Paul E. McKenney95c38322006-03-24 03:15:58 -0800926 if (atomic_read(&n_rcu_torture_error))
927 rcu_torture_print_module_parms("End of test: FAILURE");
928 else
929 rcu_torture_print_module_parms("End of test: SUCCESS");
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800930}
931
Josh Triplett6f8bc502007-05-08 00:25:24 -0700932static int __init
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800933rcu_torture_init(void)
934{
935 int i;
936 int cpu;
937 int firsterr = 0;
Josh Triplettade5fb82007-05-08 00:33:22 -0700938 static struct rcu_torture_ops *torture_ops[] =
939 { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops,
Paul E. McKenney23269742008-05-12 21:21:05 +0200940 &srcu_ops, &sched_ops, &sched_ops_sync, };
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800941
942 /* Process args and tell the world that the torturer is on the job. */
Josh Triplettade5fb82007-05-08 00:33:22 -0700943 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700944 cur_ops = torture_ops[i];
Josh Triplettade5fb82007-05-08 00:33:22 -0700945 if (strcmp(torture_type, cur_ops->name) == 0)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700946 break;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700947 }
Josh Triplettade5fb82007-05-08 00:33:22 -0700948 if (i == ARRAY_SIZE(torture_ops)) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700949 printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
950 torture_type);
951 return (-EINVAL);
952 }
Josh Triplettc8e5b162007-05-08 00:33:20 -0700953 if (cur_ops->init)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700954 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
955
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800956 if (nreaders >= 0)
957 nrealreaders = nreaders;
958 else
959 nrealreaders = 2 * num_online_cpus();
Paul E. McKenney95c38322006-03-24 03:15:58 -0800960 rcu_torture_print_module_parms("Start of test");
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800961 fullstop = 0;
962
963 /* Set up the freelist. */
964
965 INIT_LIST_HEAD(&rcu_torture_freelist);
Ahmed S. Darwish788e7702007-05-08 00:33:14 -0700966 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
Paul E. McKenney996417d2005-11-18 01:10:50 -0800967 rcu_tortures[i].rtort_mbtest = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800968 list_add_tail(&rcu_tortures[i].rtort_free,
969 &rcu_torture_freelist);
970 }
971
972 /* Initialize the statistics so that each run gets its own numbers. */
973
974 rcu_torture_current = NULL;
975 rcu_torture_current_version = 0;
976 atomic_set(&n_rcu_torture_alloc, 0);
977 atomic_set(&n_rcu_torture_alloc_fail, 0);
978 atomic_set(&n_rcu_torture_free, 0);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800979 atomic_set(&n_rcu_torture_mberror, 0);
980 atomic_set(&n_rcu_torture_error, 0);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800981 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
982 atomic_set(&rcu_torture_wcount[i], 0);
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800983 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800984 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
985 per_cpu(rcu_torture_count, cpu)[i] = 0;
986 per_cpu(rcu_torture_batch, cpu)[i] = 0;
987 }
988 }
989
990 /* Start up the kthreads. */
991
992 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
993 writer_task = kthread_run(rcu_torture_writer, NULL,
994 "rcu_torture_writer");
995 if (IS_ERR(writer_task)) {
996 firsterr = PTR_ERR(writer_task);
997 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
998 writer_task = NULL;
999 goto unwind;
1000 }
Josh Triplettb772e1d2006-10-04 02:17:13 -07001001 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1002 GFP_KERNEL);
1003 if (fakewriter_tasks == NULL) {
1004 VERBOSE_PRINTK_ERRSTRING("out of memory");
1005 firsterr = -ENOMEM;
1006 goto unwind;
1007 }
1008 for (i = 0; i < nfakewriters; i++) {
1009 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1010 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1011 "rcu_torture_fakewriter");
1012 if (IS_ERR(fakewriter_tasks[i])) {
1013 firsterr = PTR_ERR(fakewriter_tasks[i]);
1014 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1015 fakewriter_tasks[i] = NULL;
1016 goto unwind;
1017 }
1018 }
Josh Triplett2860aab2006-10-04 02:17:11 -07001019 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001020 GFP_KERNEL);
1021 if (reader_tasks == NULL) {
1022 VERBOSE_PRINTK_ERRSTRING("out of memory");
1023 firsterr = -ENOMEM;
1024 goto unwind;
1025 }
1026 for (i = 0; i < nrealreaders; i++) {
1027 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1028 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1029 "rcu_torture_reader");
1030 if (IS_ERR(reader_tasks[i])) {
1031 firsterr = PTR_ERR(reader_tasks[i]);
1032 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1033 reader_tasks[i] = NULL;
1034 goto unwind;
1035 }
1036 }
1037 if (stat_interval > 0) {
1038 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1039 stats_task = kthread_run(rcu_torture_stats, NULL,
1040 "rcu_torture_stats");
1041 if (IS_ERR(stats_task)) {
1042 firsterr = PTR_ERR(stats_task);
1043 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1044 stats_task = NULL;
1045 goto unwind;
1046 }
1047 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001048 if (test_no_idle_hz) {
1049 rcu_idle_cpu = num_online_cpus() - 1;
1050 /* Create the shuffler thread */
1051 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1052 "rcu_torture_shuffle");
1053 if (IS_ERR(shuffler_task)) {
1054 firsterr = PTR_ERR(shuffler_task);
1055 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1056 shuffler_task = NULL;
1057 goto unwind;
1058 }
1059 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001060 if (stutter < 0)
1061 stutter = 0;
1062 if (stutter) {
1063 /* Create the stutter thread */
1064 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1065 "rcu_torture_stutter");
1066 if (IS_ERR(stutter_task)) {
1067 firsterr = PTR_ERR(stutter_task);
1068 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1069 stutter_task = NULL;
1070 goto unwind;
1071 }
1072 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001073 return 0;
1074
1075unwind:
1076 rcu_torture_cleanup();
1077 return firsterr;
1078}
1079
1080module_init(rcu_torture_init);
1081module_exit(rcu_torture_cleanup);