blob: 1c329df60bccf7e1f58f0dc06f07fe39635374ec [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>
38#include <linux/module.h>
39#include <linux/completion.h>
40#include <linux/moduleparam.h>
41#include <linux/percpu.h>
42#include <linux/notifier.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080043#include <linux/cpu.h>
44#include <linux/random.h>
45#include <linux/delay.h>
46#include <linux/byteorder/swabb.h>
47#include <linux/stat.h>
Paul E. McKenneyb2896d22006-10-04 02:17:03 -070048#include <linux/srcu.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
Rusty Russell8d3b33f2006-03-25 03:07:05 -080063module_param(nreaders, int, 0);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080064MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
Josh Triplettb772e1d2006-10-04 02:17:13 -070065module_param(nfakewriters, int, 0);
66MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080067module_param(stat_interval, int, 0);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080068MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080069module_param(verbose, bool, 0);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080070MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080071module_param(test_no_idle_hz, bool, 0);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080072MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080073module_param(shuffle_interval, int, 0);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080074MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -070075module_param(torture_type, charp, 0);
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
168 * generator, with occasional help from get_random_bytes().
169 */
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{
173 long refresh;
174
175 if (--rrsp->rrs_count < 0) {
176 get_random_bytes(&refresh, sizeof(refresh));
177 rrsp->rrs_state += refresh;
178 rrsp->rrs_count = RCU_RANDOM_REFRESH;
179 }
180 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
181 return swahw32(rrsp->rrs_state);
182}
183
184/*
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700185 * Operations vector for selecting different types of tests.
186 */
187
188struct rcu_torture_ops {
189 void (*init)(void);
190 void (*cleanup)(void);
191 int (*readlock)(void);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700192 void (*readdelay)(struct rcu_random_state *rrsp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700193 void (*readunlock)(int idx);
194 int (*completed)(void);
195 void (*deferredfree)(struct rcu_torture *p);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700196 void (*sync)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700197 int (*stats)(char *page);
198 char *name;
199};
200static struct rcu_torture_ops *cur_ops = NULL;
201
202/*
203 * Definitions for rcu torture testing.
204 */
205
Josh Tripletta49a4af2006-09-29 01:59:30 -0700206static int rcu_torture_read_lock(void) __acquires(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700207{
208 rcu_read_lock();
209 return 0;
210}
211
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700212static void rcu_read_delay(struct rcu_random_state *rrsp)
213{
214 long delay;
215 const long longdelay = 200;
216
217 /* We want there to be long-running readers, but not all the time. */
218
219 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay);
220 if (!delay)
221 udelay(longdelay);
222}
223
Josh Tripletta49a4af2006-09-29 01:59:30 -0700224static void rcu_torture_read_unlock(int idx) __releases(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700225{
226 rcu_read_unlock();
227}
228
229static int rcu_torture_completed(void)
230{
231 return rcu_batches_completed();
232}
233
234static void
235rcu_torture_cb(struct rcu_head *p)
236{
237 int i;
238 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
239
240 if (fullstop) {
241 /* Test is ending, just drop callbacks on the floor. */
242 /* The next initialization will pick up the pieces. */
243 return;
244 }
245 i = rp->rtort_pipe_count;
246 if (i > RCU_TORTURE_PIPE_LEN)
247 i = RCU_TORTURE_PIPE_LEN;
248 atomic_inc(&rcu_torture_wcount[i]);
249 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
250 rp->rtort_mbtest = 0;
251 rcu_torture_free(rp);
252 } else
253 cur_ops->deferredfree(rp);
254}
255
256static void rcu_torture_deferred_free(struct rcu_torture *p)
257{
258 call_rcu(&p->rtort_rcu, rcu_torture_cb);
259}
260
261static struct rcu_torture_ops rcu_ops = {
262 .init = NULL,
263 .cleanup = NULL,
264 .readlock = rcu_torture_read_lock,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700265 .readdelay = rcu_read_delay,
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700266 .readunlock = rcu_torture_read_unlock,
267 .completed = rcu_torture_completed,
268 .deferredfree = rcu_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700269 .sync = synchronize_rcu,
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,
309 .stats = NULL,
310 .name = "rcu_sync"
311};
312
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700313/*
314 * Definitions for rcu_bh torture testing.
315 */
316
Josh Tripletta49a4af2006-09-29 01:59:30 -0700317static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700318{
319 rcu_read_lock_bh();
320 return 0;
321}
322
Josh Tripletta49a4af2006-09-29 01:59:30 -0700323static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700324{
325 rcu_read_unlock_bh();
326}
327
328static int rcu_bh_torture_completed(void)
329{
330 return rcu_batches_completed_bh();
331}
332
333static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
334{
335 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
336}
337
Josh Triplettb772e1d2006-10-04 02:17:13 -0700338struct rcu_bh_torture_synchronize {
339 struct rcu_head head;
340 struct completion completion;
341};
342
343static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
344{
345 struct rcu_bh_torture_synchronize *rcu;
346
347 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
348 complete(&rcu->completion);
349}
350
351static void rcu_bh_torture_synchronize(void)
352{
353 struct rcu_bh_torture_synchronize rcu;
354
355 init_completion(&rcu.completion);
356 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
357 wait_for_completion(&rcu.completion);
358}
359
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700360static struct rcu_torture_ops rcu_bh_ops = {
361 .init = NULL,
362 .cleanup = NULL,
363 .readlock = rcu_bh_torture_read_lock,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700364 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700365 .readunlock = rcu_bh_torture_read_unlock,
366 .completed = rcu_bh_torture_completed,
367 .deferredfree = rcu_bh_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700368 .sync = rcu_bh_torture_synchronize,
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700369 .stats = NULL,
370 .name = "rcu_bh"
371};
372
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700373/*
374 * Definitions for srcu torture testing.
375 */
376
377static struct srcu_struct srcu_ctl;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700378
379static void srcu_torture_init(void)
380{
381 init_srcu_struct(&srcu_ctl);
Josh Triplette3033732006-10-04 02:17:14 -0700382 rcu_sync_torture_init();
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700383}
384
385static void srcu_torture_cleanup(void)
386{
387 synchronize_srcu(&srcu_ctl);
388 cleanup_srcu_struct(&srcu_ctl);
389}
390
391static int srcu_torture_read_lock(void)
392{
393 return srcu_read_lock(&srcu_ctl);
394}
395
396static void srcu_read_delay(struct rcu_random_state *rrsp)
397{
398 long delay;
399 const long uspertick = 1000000 / HZ;
400 const long longdelay = 10;
401
402 /* We want there to be long-running readers, but not all the time. */
403
404 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
405 if (!delay)
406 schedule_timeout_interruptible(longdelay);
407}
408
409static void srcu_torture_read_unlock(int idx)
410{
411 srcu_read_unlock(&srcu_ctl, idx);
412}
413
414static int srcu_torture_completed(void)
415{
416 return srcu_batches_completed(&srcu_ctl);
417}
418
Josh Triplettb772e1d2006-10-04 02:17:13 -0700419static void srcu_torture_synchronize(void)
420{
421 synchronize_srcu(&srcu_ctl);
422}
423
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700424static int srcu_torture_stats(char *page)
425{
426 int cnt = 0;
427 int cpu;
428 int idx = srcu_ctl.completed & 0x1;
429
430 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
431 torture_type, TORTURE_FLAG, idx);
432 for_each_possible_cpu(cpu) {
433 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
434 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
435 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
436 }
437 cnt += sprintf(&page[cnt], "\n");
438 return cnt;
439}
440
441static struct rcu_torture_ops srcu_ops = {
442 .init = srcu_torture_init,
443 .cleanup = srcu_torture_cleanup,
444 .readlock = srcu_torture_read_lock,
445 .readdelay = srcu_read_delay,
446 .readunlock = srcu_torture_read_unlock,
447 .completed = srcu_torture_completed,
Josh Triplette3033732006-10-04 02:17:14 -0700448 .deferredfree = rcu_sync_torture_deferred_free,
Josh Triplettb772e1d2006-10-04 02:17:13 -0700449 .sync = srcu_torture_synchronize,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700450 .stats = srcu_torture_stats,
451 .name = "srcu"
452};
453
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700454static struct rcu_torture_ops *torture_ops[] =
Josh Triplett20d2e422006-10-04 02:17:15 -0700455 { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &srcu_ops, NULL };
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700456
457/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800458 * RCU torture writer kthread. Repeatedly substitutes a new structure
459 * for that pointed to by rcu_torture_current, freeing the old structure
460 * after a series of grace periods (the "pipeline").
461 */
462static int
463rcu_torture_writer(void *arg)
464{
465 int i;
466 long oldbatch = rcu_batches_completed();
467 struct rcu_torture *rp;
468 struct rcu_torture *old_rp;
469 static DEFINE_RCU_RANDOM(rand);
470
471 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800472 set_user_nice(current, 19);
473
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800474 do {
475 schedule_timeout_uninterruptible(1);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800476 if ((rp = rcu_torture_alloc()) == NULL)
477 continue;
478 rp->rtort_pipe_count = 0;
479 udelay(rcu_random(&rand) & 0x3ff);
480 old_rp = rcu_torture_current;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800481 rp->rtort_mbtest = 1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800482 rcu_assign_pointer(rcu_torture_current, rp);
483 smp_wmb();
484 if (old_rp != NULL) {
485 i = old_rp->rtort_pipe_count;
486 if (i > RCU_TORTURE_PIPE_LEN)
487 i = RCU_TORTURE_PIPE_LEN;
488 atomic_inc(&rcu_torture_wcount[i]);
489 old_rp->rtort_pipe_count++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700490 cur_ops->deferredfree(old_rp);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800491 }
492 rcu_torture_current_version++;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700493 oldbatch = cur_ops->completed();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800494 } while (!kthread_should_stop() && !fullstop);
495 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
496 while (!kthread_should_stop())
497 schedule_timeout_uninterruptible(1);
498 return 0;
499}
500
501/*
Josh Triplettb772e1d2006-10-04 02:17:13 -0700502 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
503 * delay between calls.
504 */
505static int
506rcu_torture_fakewriter(void *arg)
507{
508 DEFINE_RCU_RANDOM(rand);
509
510 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
511 set_user_nice(current, 19);
512
513 do {
514 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
515 udelay(rcu_random(&rand) & 0x3ff);
516 cur_ops->sync();
517 } while (!kthread_should_stop() && !fullstop);
518
519 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
520 while (!kthread_should_stop())
521 schedule_timeout_uninterruptible(1);
522 return 0;
523}
524
525/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800526 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
527 * incrementing the corresponding element of the pipeline array. The
528 * counter in the element should never be greater than 1, otherwise, the
529 * RCU implementation is broken.
530 */
531static int
532rcu_torture_reader(void *arg)
533{
534 int completed;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700535 int idx;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800536 DEFINE_RCU_RANDOM(rand);
537 struct rcu_torture *p;
538 int pipe_count;
539
540 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800541 set_user_nice(current, 19);
542
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800543 do {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700544 idx = cur_ops->readlock();
545 completed = cur_ops->completed();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800546 p = rcu_dereference(rcu_torture_current);
547 if (p == NULL) {
548 /* Wait for rcu_torture_writer to get underway */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700549 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800550 schedule_timeout_interruptible(HZ);
551 continue;
552 }
Paul E. McKenney996417d2005-11-18 01:10:50 -0800553 if (p->rtort_mbtest == 0)
554 atomic_inc(&n_rcu_torture_mberror);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700555 cur_ops->readdelay(&rand);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800556 preempt_disable();
557 pipe_count = p->rtort_pipe_count;
558 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
559 /* Should not happen, but... */
560 pipe_count = RCU_TORTURE_PIPE_LEN;
561 }
562 ++__get_cpu_var(rcu_torture_count)[pipe_count];
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700563 completed = cur_ops->completed() - completed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800564 if (completed > RCU_TORTURE_PIPE_LEN) {
565 /* Should not happen, but... */
566 completed = RCU_TORTURE_PIPE_LEN;
567 }
568 ++__get_cpu_var(rcu_torture_batch)[completed];
569 preempt_enable();
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700570 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800571 schedule();
572 } while (!kthread_should_stop() && !fullstop);
573 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
574 while (!kthread_should_stop())
575 schedule_timeout_uninterruptible(1);
576 return 0;
577}
578
579/*
580 * Create an RCU-torture statistics message in the specified buffer.
581 */
582static int
583rcu_torture_printk(char *page)
584{
585 int cnt = 0;
586 int cpu;
587 int i;
588 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
589 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
590
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800591 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800592 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
593 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
594 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
595 }
596 }
597 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
598 if (pipesummary[i] != 0)
599 break;
600 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700601 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800602 cnt += sprintf(&page[cnt],
Paul E. McKenney996417d2005-11-18 01:10:50 -0800603 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
604 "rtmbe: %d",
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800605 rcu_torture_current,
606 rcu_torture_current_version,
607 list_empty(&rcu_torture_freelist),
608 atomic_read(&n_rcu_torture_alloc),
609 atomic_read(&n_rcu_torture_alloc_fail),
Paul E. McKenney996417d2005-11-18 01:10:50 -0800610 atomic_read(&n_rcu_torture_free),
611 atomic_read(&n_rcu_torture_mberror));
612 if (atomic_read(&n_rcu_torture_mberror) != 0)
613 cnt += sprintf(&page[cnt], " !!!");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700614 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800615 if (i > 1) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800616 cnt += sprintf(&page[cnt], "!!! ");
Paul E. McKenney996417d2005-11-18 01:10:50 -0800617 atomic_inc(&n_rcu_torture_error);
618 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800619 cnt += sprintf(&page[cnt], "Reader Pipe: ");
620 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
621 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700622 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800623 cnt += sprintf(&page[cnt], "Reader Batch: ");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700624 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800625 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700626 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800627 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
628 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
629 cnt += sprintf(&page[cnt], " %d",
630 atomic_read(&rcu_torture_wcount[i]));
631 }
632 cnt += sprintf(&page[cnt], "\n");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700633 if (cur_ops->stats != NULL)
634 cnt += cur_ops->stats(&page[cnt]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800635 return cnt;
636}
637
638/*
639 * Print torture statistics. Caller must ensure that there is only
640 * one call to this function at a given time!!! This is normally
641 * accomplished by relying on the module system to only have one copy
642 * of the module loaded, and then by giving the rcu_torture_stats
643 * kthread full control (or the init/cleanup functions when rcu_torture_stats
644 * thread is not running).
645 */
646static void
647rcu_torture_stats_print(void)
648{
649 int cnt;
650
651 cnt = rcu_torture_printk(printk_buf);
652 printk(KERN_ALERT "%s", printk_buf);
653}
654
655/*
656 * Periodically prints torture statistics, if periodic statistics printing
657 * was specified via the stat_interval module parameter.
658 *
659 * No need to worry about fullstop here, since this one doesn't reference
660 * volatile state or register callbacks.
661 */
662static int
663rcu_torture_stats(void *arg)
664{
665 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
666 do {
667 schedule_timeout_interruptible(stat_interval * HZ);
668 rcu_torture_stats_print();
669 } while (!kthread_should_stop());
670 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
671 return 0;
672}
673
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800674static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
675
676/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
677 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
678 */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700679static void rcu_torture_shuffle_tasks(void)
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800680{
681 cpumask_t tmp_mask = CPU_MASK_ALL;
682 int i;
683
684 lock_cpu_hotplug();
685
686 /* No point in shuffling if there is only one online CPU (ex: UP) */
687 if (num_online_cpus() == 1) {
688 unlock_cpu_hotplug();
689 return;
690 }
691
692 if (rcu_idle_cpu != -1)
693 cpu_clear(rcu_idle_cpu, tmp_mask);
694
695 set_cpus_allowed(current, tmp_mask);
696
697 if (reader_tasks != NULL) {
698 for (i = 0; i < nrealreaders; i++)
699 if (reader_tasks[i])
700 set_cpus_allowed(reader_tasks[i], tmp_mask);
701 }
702
Josh Triplettb772e1d2006-10-04 02:17:13 -0700703 if (fakewriter_tasks != NULL) {
704 for (i = 0; i < nfakewriters; i++)
705 if (fakewriter_tasks[i])
706 set_cpus_allowed(fakewriter_tasks[i], tmp_mask);
707 }
708
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800709 if (writer_task)
710 set_cpus_allowed(writer_task, tmp_mask);
711
712 if (stats_task)
713 set_cpus_allowed(stats_task, tmp_mask);
714
715 if (rcu_idle_cpu == -1)
716 rcu_idle_cpu = num_online_cpus() - 1;
717 else
718 rcu_idle_cpu--;
719
720 unlock_cpu_hotplug();
721}
722
723/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
724 * system to become idle at a time and cut off its timer ticks. This is meant
725 * to test the support for such tickless idle CPU in RCU.
726 */
727static int
728rcu_torture_shuffle(void *arg)
729{
730 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
731 do {
732 schedule_timeout_interruptible(shuffle_interval * HZ);
733 rcu_torture_shuffle_tasks();
734 } while (!kthread_should_stop());
735 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
736 return 0;
737}
738
Paul E. McKenney95c38322006-03-24 03:15:58 -0800739static inline void
740rcu_torture_print_module_parms(char *tag)
741{
Josh Triplettb772e1d2006-10-04 02:17:13 -0700742 printk(KERN_ALERT "%s" TORTURE_FLAG
743 "--- %s: nreaders=%d nfakewriters=%d "
Paul E. McKenney95c38322006-03-24 03:15:58 -0800744 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
745 "shuffle_interval = %d\n",
Josh Triplettb772e1d2006-10-04 02:17:13 -0700746 torture_type, tag, nrealreaders, nfakewriters,
747 stat_interval, verbose, test_no_idle_hz, shuffle_interval);
Paul E. McKenney95c38322006-03-24 03:15:58 -0800748}
749
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800750static void
751rcu_torture_cleanup(void)
752{
753 int i;
754
755 fullstop = 1;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800756 if (shuffler_task != NULL) {
757 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
758 kthread_stop(shuffler_task);
759 }
760 shuffler_task = NULL;
761
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800762 if (writer_task != NULL) {
763 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
764 kthread_stop(writer_task);
765 }
766 writer_task = NULL;
767
768 if (reader_tasks != NULL) {
769 for (i = 0; i < nrealreaders; i++) {
770 if (reader_tasks[i] != NULL) {
771 VERBOSE_PRINTK_STRING(
772 "Stopping rcu_torture_reader task");
773 kthread_stop(reader_tasks[i]);
774 }
775 reader_tasks[i] = NULL;
776 }
777 kfree(reader_tasks);
778 reader_tasks = NULL;
779 }
780 rcu_torture_current = NULL;
781
Josh Triplettb772e1d2006-10-04 02:17:13 -0700782 if (fakewriter_tasks != NULL) {
783 for (i = 0; i < nfakewriters; i++) {
784 if (fakewriter_tasks[i] != NULL) {
785 VERBOSE_PRINTK_STRING(
786 "Stopping rcu_torture_fakewriter task");
787 kthread_stop(fakewriter_tasks[i]);
788 }
789 fakewriter_tasks[i] = NULL;
790 }
791 kfree(fakewriter_tasks);
792 fakewriter_tasks = NULL;
793 }
794
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800795 if (stats_task != NULL) {
796 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
797 kthread_stop(stats_task);
798 }
799 stats_task = NULL;
800
801 /* Wait for all RCU callbacks to fire. */
Srivatsa Vaddagiri89d46b82005-12-12 00:37:06 -0800802 rcu_barrier();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800803
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800804 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700805
806 if (cur_ops->cleanup != NULL)
807 cur_ops->cleanup();
Paul E. McKenney95c38322006-03-24 03:15:58 -0800808 if (atomic_read(&n_rcu_torture_error))
809 rcu_torture_print_module_parms("End of test: FAILURE");
810 else
811 rcu_torture_print_module_parms("End of test: SUCCESS");
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800812}
813
814static int
815rcu_torture_init(void)
816{
817 int i;
818 int cpu;
819 int firsterr = 0;
820
821 /* Process args and tell the world that the torturer is on the job. */
822
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700823 for (i = 0; cur_ops = torture_ops[i], cur_ops != NULL; i++) {
824 cur_ops = torture_ops[i];
825 if (strcmp(torture_type, cur_ops->name) == 0) {
826 break;
827 }
828 }
829 if (cur_ops == NULL) {
830 printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
831 torture_type);
832 return (-EINVAL);
833 }
834 if (cur_ops->init != NULL)
835 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
836
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800837 if (nreaders >= 0)
838 nrealreaders = nreaders;
839 else
840 nrealreaders = 2 * num_online_cpus();
Paul E. McKenney95c38322006-03-24 03:15:58 -0800841 rcu_torture_print_module_parms("Start of test");
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800842 fullstop = 0;
843
844 /* Set up the freelist. */
845
846 INIT_LIST_HEAD(&rcu_torture_freelist);
847 for (i = 0; i < sizeof(rcu_tortures) / sizeof(rcu_tortures[0]); i++) {
Paul E. McKenney996417d2005-11-18 01:10:50 -0800848 rcu_tortures[i].rtort_mbtest = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800849 list_add_tail(&rcu_tortures[i].rtort_free,
850 &rcu_torture_freelist);
851 }
852
853 /* Initialize the statistics so that each run gets its own numbers. */
854
855 rcu_torture_current = NULL;
856 rcu_torture_current_version = 0;
857 atomic_set(&n_rcu_torture_alloc, 0);
858 atomic_set(&n_rcu_torture_alloc_fail, 0);
859 atomic_set(&n_rcu_torture_free, 0);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800860 atomic_set(&n_rcu_torture_mberror, 0);
861 atomic_set(&n_rcu_torture_error, 0);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800862 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
863 atomic_set(&rcu_torture_wcount[i], 0);
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800864 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800865 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
866 per_cpu(rcu_torture_count, cpu)[i] = 0;
867 per_cpu(rcu_torture_batch, cpu)[i] = 0;
868 }
869 }
870
871 /* Start up the kthreads. */
872
873 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
874 writer_task = kthread_run(rcu_torture_writer, NULL,
875 "rcu_torture_writer");
876 if (IS_ERR(writer_task)) {
877 firsterr = PTR_ERR(writer_task);
878 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
879 writer_task = NULL;
880 goto unwind;
881 }
Josh Triplettb772e1d2006-10-04 02:17:13 -0700882 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
883 GFP_KERNEL);
884 if (fakewriter_tasks == NULL) {
885 VERBOSE_PRINTK_ERRSTRING("out of memory");
886 firsterr = -ENOMEM;
887 goto unwind;
888 }
889 for (i = 0; i < nfakewriters; i++) {
890 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
891 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
892 "rcu_torture_fakewriter");
893 if (IS_ERR(fakewriter_tasks[i])) {
894 firsterr = PTR_ERR(fakewriter_tasks[i]);
895 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
896 fakewriter_tasks[i] = NULL;
897 goto unwind;
898 }
899 }
Josh Triplett2860aab2006-10-04 02:17:11 -0700900 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800901 GFP_KERNEL);
902 if (reader_tasks == NULL) {
903 VERBOSE_PRINTK_ERRSTRING("out of memory");
904 firsterr = -ENOMEM;
905 goto unwind;
906 }
907 for (i = 0; i < nrealreaders; i++) {
908 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
909 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
910 "rcu_torture_reader");
911 if (IS_ERR(reader_tasks[i])) {
912 firsterr = PTR_ERR(reader_tasks[i]);
913 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
914 reader_tasks[i] = NULL;
915 goto unwind;
916 }
917 }
918 if (stat_interval > 0) {
919 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
920 stats_task = kthread_run(rcu_torture_stats, NULL,
921 "rcu_torture_stats");
922 if (IS_ERR(stats_task)) {
923 firsterr = PTR_ERR(stats_task);
924 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
925 stats_task = NULL;
926 goto unwind;
927 }
928 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800929 if (test_no_idle_hz) {
930 rcu_idle_cpu = num_online_cpus() - 1;
931 /* Create the shuffler thread */
932 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
933 "rcu_torture_shuffle");
934 if (IS_ERR(shuffler_task)) {
935 firsterr = PTR_ERR(shuffler_task);
936 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
937 shuffler_task = NULL;
938 goto unwind;
939 }
940 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800941 return 0;
942
943unwind:
944 rcu_torture_cleanup();
945 return firsterr;
946}
947
948module_init(rcu_torture_init);
949module_exit(rcu_torture_cleanup);