Merge branch 'linus' into core/rcu
[linux-2.6-block.git] / kernel / rcutorture.c
CommitLineData
a241ec65 1/*
29766f1e 2 * Read-Copy Update module-based torture test facility
a241ec65
PM
3 *
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 *
b772e1dd 18 * Copyright (C) IBM Corporation, 2005, 2006
a241ec65
PM
19 *
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
b772e1dd 21 * Josh Triplett <josh@freedesktop.org>
a241ec65
PM
22 *
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>
a241ec65
PM
38#include <linux/completion.h>
39#include <linux/moduleparam.h>
40#include <linux/percpu.h>
41#include <linux/notifier.h>
83144186 42#include <linux/freezer.h>
a241ec65 43#include <linux/cpu.h>
a241ec65
PM
44#include <linux/delay.h>
45#include <linux/byteorder/swabb.h>
46#include <linux/stat.h>
b2896d2e 47#include <linux/srcu.h>
1aeb272c 48#include <linux/slab.h>
a241ec65
PM
49
50MODULE_LICENSE("GPL");
b772e1dd
JT
51MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
52 "Josh Triplett <josh@freedesktop.org>");
a241ec65 53
4802211c 54static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
b772e1dd 55static int nfakewriters = 4; /* # fake writer threads */
d84f5203 56static int stat_interval; /* Interval between stats, in seconds. */
a241ec65 57 /* Defaults to "only at end of test". */
d84f5203
SV
58static int verbose; /* Print more debug info. */
59static int test_no_idle_hz; /* Test RCU's support for tickless idle CPUs. */
d120f65f
PM
60static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
61static int stutter = 5; /* Start/stop testing interval (in sec) */
20d2e428 62static char *torture_type = "rcu"; /* What RCU implementation to torture. */
a241ec65 63
d6ad6711 64module_param(nreaders, int, 0444);
a241ec65 65MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
d6ad6711 66module_param(nfakewriters, int, 0444);
b772e1dd 67MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
d6ad6711 68module_param(stat_interval, int, 0444);
a241ec65 69MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
d6ad6711 70module_param(verbose, bool, 0444);
a241ec65 71MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
d6ad6711 72module_param(test_no_idle_hz, bool, 0444);
d84f5203 73MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
d6ad6711 74module_param(shuffle_interval, int, 0444);
d84f5203 75MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
d120f65f
PM
76module_param(stutter, int, 0444);
77MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
d6ad6711 78module_param(torture_type, charp, 0444);
b2896d2e 79MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
72e9bb54
PM
80
81#define TORTURE_FLAG "-torture:"
a241ec65 82#define PRINTK_STRING(s) \
72e9bb54 83 do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
a241ec65 84#define VERBOSE_PRINTK_STRING(s) \
72e9bb54 85 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
a241ec65 86#define VERBOSE_PRINTK_ERRSTRING(s) \
72e9bb54 87 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
a241ec65
PM
88
89static char printk_buf[4096];
90
91static int nrealreaders;
92static struct task_struct *writer_task;
b772e1dd 93static struct task_struct **fakewriter_tasks;
a241ec65
PM
94static struct task_struct **reader_tasks;
95static struct task_struct *stats_task;
d84f5203 96static struct task_struct *shuffler_task;
d120f65f 97static struct task_struct *stutter_task;
a241ec65
PM
98
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;
996417d2 105 int rtort_mbtest;
a241ec65
PM
106};
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];
b2896d2e
PM
119static 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;
e3033736 124static struct list_head rcu_torture_removed;
a241ec65 125
d120f65f
PM
126static int stutter_pause_test = 0;
127
31a72bce
PM
128#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
129#define RCUTORTURE_RUNNABLE_INIT 1
130#else
131#define RCUTORTURE_RUNNABLE_INIT 0
132#endif
133int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
134
a241ec65
PM
135/*
136 * Allocate an element from the rcu_tortures pool.
137 */
97a41e26 138static struct rcu_torture *
a241ec65
PM
139rcu_torture_alloc(void)
140{
141 struct list_head *p;
142
adac1665 143 spin_lock_bh(&rcu_torture_lock);
a241ec65
PM
144 if (list_empty(&rcu_torture_freelist)) {
145 atomic_inc(&n_rcu_torture_alloc_fail);
adac1665 146 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
147 return NULL;
148 }
149 atomic_inc(&n_rcu_torture_alloc);
150 p = rcu_torture_freelist.next;
151 list_del_init(p);
adac1665 152 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
153 return container_of(p, struct rcu_torture, rtort_free);
154}
155
156/*
157 * Free an element to the rcu_tortures pool.
158 */
159static void
160rcu_torture_free(struct rcu_torture *p)
161{
162 atomic_inc(&n_rcu_torture_free);
adac1665 163 spin_lock_bh(&rcu_torture_lock);
a241ec65 164 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
adac1665 165 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
166}
167
a241ec65
PM
168struct rcu_random_state {
169 unsigned long rrs_state;
75cfef32 170 long rrs_count;
a241ec65
PM
171};
172
173#define RCU_RANDOM_MULT 39916801 /* prime */
174#define RCU_RANDOM_ADD 479001701 /* prime */
175#define RCU_RANDOM_REFRESH 10000
176
177#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
178
179/*
180 * Crude but fast random-number generator. Uses a linear congruential
c17ac855 181 * generator, with occasional help from cpu_clock().
a241ec65 182 */
75cfef32 183static unsigned long
a241ec65
PM
184rcu_random(struct rcu_random_state *rrsp)
185{
a241ec65 186 if (--rrsp->rrs_count < 0) {
c17ac855
PM
187 rrsp->rrs_state +=
188 (unsigned long)cpu_clock(raw_smp_processor_id());
a241ec65
PM
189 rrsp->rrs_count = RCU_RANDOM_REFRESH;
190 }
191 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
192 return swahw32(rrsp->rrs_state);
193}
194
d120f65f
PM
195static void
196rcu_stutter_wait(void)
197{
31a72bce 198 while (stutter_pause_test || !rcutorture_runnable)
d120f65f
PM
199 schedule_timeout_interruptible(1);
200}
201
72e9bb54
PM
202/*
203 * Operations vector for selecting different types of tests.
204 */
205
206struct rcu_torture_ops {
207 void (*init)(void);
208 void (*cleanup)(void);
209 int (*readlock)(void);
b2896d2e 210 void (*readdelay)(struct rcu_random_state *rrsp);
72e9bb54
PM
211 void (*readunlock)(int idx);
212 int (*completed)(void);
213 void (*deferredfree)(struct rcu_torture *p);
b772e1dd 214 void (*sync)(void);
2326974d 215 void (*cb_barrier)(void);
72e9bb54
PM
216 int (*stats)(char *page);
217 char *name;
218};
219static struct rcu_torture_ops *cur_ops = NULL;
220
221/*
222 * Definitions for rcu torture testing.
223 */
224
a49a4af7 225static int rcu_torture_read_lock(void) __acquires(RCU)
72e9bb54
PM
226{
227 rcu_read_lock();
228 return 0;
229}
230
b2896d2e
PM
231static void rcu_read_delay(struct rcu_random_state *rrsp)
232{
233 long delay;
234 const long longdelay = 200;
235
236 /* We want there to be long-running readers, but not all the time. */
237
238 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay);
239 if (!delay)
240 udelay(longdelay);
241}
242
a49a4af7 243static void rcu_torture_read_unlock(int idx) __releases(RCU)
72e9bb54
PM
244{
245 rcu_read_unlock();
246}
247
248static int rcu_torture_completed(void)
249{
250 return rcu_batches_completed();
251}
252
253static void
254rcu_torture_cb(struct rcu_head *p)
255{
256 int i;
257 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
258
259 if (fullstop) {
260 /* Test is ending, just drop callbacks on the floor. */
261 /* The next initialization will pick up the pieces. */
262 return;
263 }
264 i = rp->rtort_pipe_count;
265 if (i > RCU_TORTURE_PIPE_LEN)
266 i = RCU_TORTURE_PIPE_LEN;
267 atomic_inc(&rcu_torture_wcount[i]);
268 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
269 rp->rtort_mbtest = 0;
270 rcu_torture_free(rp);
271 } else
272 cur_ops->deferredfree(rp);
273}
274
275static void rcu_torture_deferred_free(struct rcu_torture *p)
276{
277 call_rcu(&p->rtort_rcu, rcu_torture_cb);
278}
279
280static struct rcu_torture_ops rcu_ops = {
281 .init = NULL,
282 .cleanup = NULL,
283 .readlock = rcu_torture_read_lock,
b2896d2e 284 .readdelay = rcu_read_delay,
72e9bb54
PM
285 .readunlock = rcu_torture_read_unlock,
286 .completed = rcu_torture_completed,
287 .deferredfree = rcu_torture_deferred_free,
b772e1dd 288 .sync = synchronize_rcu,
2326974d 289 .cb_barrier = rcu_barrier,
72e9bb54
PM
290 .stats = NULL,
291 .name = "rcu"
292};
293
e3033736
JT
294static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
295{
296 int i;
297 struct rcu_torture *rp;
298 struct rcu_torture *rp1;
299
300 cur_ops->sync();
301 list_add(&p->rtort_free, &rcu_torture_removed);
302 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
303 i = rp->rtort_pipe_count;
304 if (i > RCU_TORTURE_PIPE_LEN)
305 i = RCU_TORTURE_PIPE_LEN;
306 atomic_inc(&rcu_torture_wcount[i]);
307 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
308 rp->rtort_mbtest = 0;
309 list_del(&rp->rtort_free);
310 rcu_torture_free(rp);
311 }
312 }
313}
314
315static void rcu_sync_torture_init(void)
316{
317 INIT_LIST_HEAD(&rcu_torture_removed);
318}
319
20d2e428
JT
320static struct rcu_torture_ops rcu_sync_ops = {
321 .init = rcu_sync_torture_init,
322 .cleanup = NULL,
323 .readlock = rcu_torture_read_lock,
324 .readdelay = rcu_read_delay,
325 .readunlock = rcu_torture_read_unlock,
326 .completed = rcu_torture_completed,
327 .deferredfree = rcu_sync_torture_deferred_free,
328 .sync = synchronize_rcu,
2326974d 329 .cb_barrier = NULL,
20d2e428
JT
330 .stats = NULL,
331 .name = "rcu_sync"
332};
333
c32e0660
PM
334/*
335 * Definitions for rcu_bh torture testing.
336 */
337
a49a4af7 338static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
c32e0660
PM
339{
340 rcu_read_lock_bh();
341 return 0;
342}
343
a49a4af7 344static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
c32e0660
PM
345{
346 rcu_read_unlock_bh();
347}
348
349static int rcu_bh_torture_completed(void)
350{
351 return rcu_batches_completed_bh();
352}
353
354static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
355{
356 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
357}
358
b772e1dd
JT
359struct rcu_bh_torture_synchronize {
360 struct rcu_head head;
361 struct completion completion;
362};
363
364static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
365{
366 struct rcu_bh_torture_synchronize *rcu;
367
368 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
369 complete(&rcu->completion);
370}
371
372static void rcu_bh_torture_synchronize(void)
373{
374 struct rcu_bh_torture_synchronize rcu;
375
376 init_completion(&rcu.completion);
377 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
378 wait_for_completion(&rcu.completion);
379}
380
c32e0660
PM
381static struct rcu_torture_ops rcu_bh_ops = {
382 .init = NULL,
383 .cleanup = NULL,
384 .readlock = rcu_bh_torture_read_lock,
b2896d2e 385 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
c32e0660
PM
386 .readunlock = rcu_bh_torture_read_unlock,
387 .completed = rcu_bh_torture_completed,
388 .deferredfree = rcu_bh_torture_deferred_free,
b772e1dd 389 .sync = rcu_bh_torture_synchronize,
2326974d 390 .cb_barrier = rcu_barrier_bh,
c32e0660
PM
391 .stats = NULL,
392 .name = "rcu_bh"
393};
394
11a14701
JT
395static struct rcu_torture_ops rcu_bh_sync_ops = {
396 .init = rcu_sync_torture_init,
397 .cleanup = NULL,
398 .readlock = rcu_bh_torture_read_lock,
399 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
400 .readunlock = rcu_bh_torture_read_unlock,
401 .completed = rcu_bh_torture_completed,
402 .deferredfree = rcu_sync_torture_deferred_free,
403 .sync = rcu_bh_torture_synchronize,
2326974d 404 .cb_barrier = NULL,
11a14701
JT
405 .stats = NULL,
406 .name = "rcu_bh_sync"
407};
408
b2896d2e
PM
409/*
410 * Definitions for srcu torture testing.
411 */
412
413static struct srcu_struct srcu_ctl;
b2896d2e
PM
414
415static void srcu_torture_init(void)
416{
417 init_srcu_struct(&srcu_ctl);
e3033736 418 rcu_sync_torture_init();
b2896d2e
PM
419}
420
421static void srcu_torture_cleanup(void)
422{
423 synchronize_srcu(&srcu_ctl);
424 cleanup_srcu_struct(&srcu_ctl);
425}
426
012d3ca8 427static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
b2896d2e
PM
428{
429 return srcu_read_lock(&srcu_ctl);
430}
431
432static void srcu_read_delay(struct rcu_random_state *rrsp)
433{
434 long delay;
435 const long uspertick = 1000000 / HZ;
436 const long longdelay = 10;
437
438 /* We want there to be long-running readers, but not all the time. */
439
440 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
441 if (!delay)
442 schedule_timeout_interruptible(longdelay);
443}
444
012d3ca8 445static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
b2896d2e
PM
446{
447 srcu_read_unlock(&srcu_ctl, idx);
448}
449
450static int srcu_torture_completed(void)
451{
452 return srcu_batches_completed(&srcu_ctl);
453}
454
b772e1dd
JT
455static void srcu_torture_synchronize(void)
456{
457 synchronize_srcu(&srcu_ctl);
458}
459
b2896d2e
PM
460static int srcu_torture_stats(char *page)
461{
462 int cnt = 0;
463 int cpu;
464 int idx = srcu_ctl.completed & 0x1;
465
466 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
467 torture_type, TORTURE_FLAG, idx);
468 for_each_possible_cpu(cpu) {
469 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
470 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
471 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
472 }
473 cnt += sprintf(&page[cnt], "\n");
474 return cnt;
475}
476
477static struct rcu_torture_ops srcu_ops = {
478 .init = srcu_torture_init,
479 .cleanup = srcu_torture_cleanup,
480 .readlock = srcu_torture_read_lock,
481 .readdelay = srcu_read_delay,
482 .readunlock = srcu_torture_read_unlock,
483 .completed = srcu_torture_completed,
e3033736 484 .deferredfree = rcu_sync_torture_deferred_free,
b772e1dd 485 .sync = srcu_torture_synchronize,
2326974d 486 .cb_barrier = NULL,
b2896d2e
PM
487 .stats = srcu_torture_stats,
488 .name = "srcu"
489};
490
4b6c2cca
JT
491/*
492 * Definitions for sched torture testing.
493 */
494
495static int sched_torture_read_lock(void)
496{
497 preempt_disable();
498 return 0;
499}
500
501static void sched_torture_read_unlock(int idx)
502{
503 preempt_enable();
504}
505
506static int sched_torture_completed(void)
507{
508 return 0;
509}
510
2326974d
PM
511static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
512{
513 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
514}
515
4b6c2cca
JT
516static void sched_torture_synchronize(void)
517{
518 synchronize_sched();
519}
520
521static struct rcu_torture_ops sched_ops = {
522 .init = rcu_sync_torture_init,
523 .cleanup = NULL,
524 .readlock = sched_torture_read_lock,
525 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
526 .readunlock = sched_torture_read_unlock,
527 .completed = sched_torture_completed,
2326974d 528 .deferredfree = rcu_sched_torture_deferred_free,
4b6c2cca 529 .sync = sched_torture_synchronize,
2326974d 530 .cb_barrier = rcu_barrier_sched,
4b6c2cca
JT
531 .stats = NULL,
532 .name = "sched"
533};
534
2326974d
PM
535static struct rcu_torture_ops sched_ops_sync = {
536 .init = rcu_sync_torture_init,
537 .cleanup = NULL,
538 .readlock = sched_torture_read_lock,
539 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
540 .readunlock = sched_torture_read_unlock,
541 .completed = sched_torture_completed,
542 .deferredfree = rcu_sync_torture_deferred_free,
543 .sync = sched_torture_synchronize,
544 .cb_barrier = NULL,
545 .stats = NULL,
546 .name = "sched_sync"
547};
548
a241ec65
PM
549/*
550 * RCU torture writer kthread. Repeatedly substitutes a new structure
551 * for that pointed to by rcu_torture_current, freeing the old structure
552 * after a series of grace periods (the "pipeline").
553 */
554static int
555rcu_torture_writer(void *arg)
556{
557 int i;
558 long oldbatch = rcu_batches_completed();
559 struct rcu_torture *rp;
560 struct rcu_torture *old_rp;
561 static DEFINE_RCU_RANDOM(rand);
562
563 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
dbdf65b1
IM
564 set_user_nice(current, 19);
565
a241ec65
PM
566 do {
567 schedule_timeout_uninterruptible(1);
a241ec65
PM
568 if ((rp = rcu_torture_alloc()) == NULL)
569 continue;
570 rp->rtort_pipe_count = 0;
571 udelay(rcu_random(&rand) & 0x3ff);
572 old_rp = rcu_torture_current;
996417d2 573 rp->rtort_mbtest = 1;
a241ec65
PM
574 rcu_assign_pointer(rcu_torture_current, rp);
575 smp_wmb();
c8e5b163 576 if (old_rp) {
a241ec65
PM
577 i = old_rp->rtort_pipe_count;
578 if (i > RCU_TORTURE_PIPE_LEN)
579 i = RCU_TORTURE_PIPE_LEN;
580 atomic_inc(&rcu_torture_wcount[i]);
581 old_rp->rtort_pipe_count++;
72e9bb54 582 cur_ops->deferredfree(old_rp);
a241ec65
PM
583 }
584 rcu_torture_current_version++;
72e9bb54 585 oldbatch = cur_ops->completed();
d120f65f 586 rcu_stutter_wait();
a241ec65
PM
587 } while (!kthread_should_stop() && !fullstop);
588 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
589 while (!kthread_should_stop())
590 schedule_timeout_uninterruptible(1);
591 return 0;
592}
593
b772e1dd
JT
594/*
595 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
596 * delay between calls.
597 */
598static int
599rcu_torture_fakewriter(void *arg)
600{
601 DEFINE_RCU_RANDOM(rand);
602
603 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
604 set_user_nice(current, 19);
605
606 do {
607 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
608 udelay(rcu_random(&rand) & 0x3ff);
609 cur_ops->sync();
d120f65f 610 rcu_stutter_wait();
b772e1dd
JT
611 } while (!kthread_should_stop() && !fullstop);
612
613 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
614 while (!kthread_should_stop())
615 schedule_timeout_uninterruptible(1);
616 return 0;
617}
618
a241ec65
PM
619/*
620 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
621 * incrementing the corresponding element of the pipeline array. The
622 * counter in the element should never be greater than 1, otherwise, the
623 * RCU implementation is broken.
624 */
625static int
626rcu_torture_reader(void *arg)
627{
628 int completed;
72e9bb54 629 int idx;
a241ec65
PM
630 DEFINE_RCU_RANDOM(rand);
631 struct rcu_torture *p;
632 int pipe_count;
633
634 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
dbdf65b1
IM
635 set_user_nice(current, 19);
636
a241ec65 637 do {
72e9bb54
PM
638 idx = cur_ops->readlock();
639 completed = cur_ops->completed();
a241ec65
PM
640 p = rcu_dereference(rcu_torture_current);
641 if (p == NULL) {
642 /* Wait for rcu_torture_writer to get underway */
72e9bb54 643 cur_ops->readunlock(idx);
a241ec65
PM
644 schedule_timeout_interruptible(HZ);
645 continue;
646 }
996417d2
PM
647 if (p->rtort_mbtest == 0)
648 atomic_inc(&n_rcu_torture_mberror);
b2896d2e 649 cur_ops->readdelay(&rand);
a241ec65
PM
650 preempt_disable();
651 pipe_count = p->rtort_pipe_count;
652 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
653 /* Should not happen, but... */
654 pipe_count = RCU_TORTURE_PIPE_LEN;
655 }
656 ++__get_cpu_var(rcu_torture_count)[pipe_count];
72e9bb54 657 completed = cur_ops->completed() - completed;
a241ec65
PM
658 if (completed > RCU_TORTURE_PIPE_LEN) {
659 /* Should not happen, but... */
660 completed = RCU_TORTURE_PIPE_LEN;
661 }
662 ++__get_cpu_var(rcu_torture_batch)[completed];
663 preempt_enable();
72e9bb54 664 cur_ops->readunlock(idx);
a241ec65 665 schedule();
d120f65f 666 rcu_stutter_wait();
a241ec65
PM
667 } while (!kthread_should_stop() && !fullstop);
668 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
669 while (!kthread_should_stop())
670 schedule_timeout_uninterruptible(1);
671 return 0;
672}
673
674/*
675 * Create an RCU-torture statistics message in the specified buffer.
676 */
677static int
678rcu_torture_printk(char *page)
679{
680 int cnt = 0;
681 int cpu;
682 int i;
683 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
684 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
685
0a945022 686 for_each_possible_cpu(cpu) {
a241ec65
PM
687 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
688 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
689 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
690 }
691 }
692 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
693 if (pipesummary[i] != 0)
694 break;
695 }
72e9bb54 696 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
a241ec65 697 cnt += sprintf(&page[cnt],
996417d2
PM
698 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
699 "rtmbe: %d",
a241ec65
PM
700 rcu_torture_current,
701 rcu_torture_current_version,
702 list_empty(&rcu_torture_freelist),
703 atomic_read(&n_rcu_torture_alloc),
704 atomic_read(&n_rcu_torture_alloc_fail),
996417d2
PM
705 atomic_read(&n_rcu_torture_free),
706 atomic_read(&n_rcu_torture_mberror));
707 if (atomic_read(&n_rcu_torture_mberror) != 0)
708 cnt += sprintf(&page[cnt], " !!!");
72e9bb54 709 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
996417d2 710 if (i > 1) {
a241ec65 711 cnt += sprintf(&page[cnt], "!!! ");
996417d2 712 atomic_inc(&n_rcu_torture_error);
5af970a4 713 WARN_ON_ONCE(1);
996417d2 714 }
a241ec65
PM
715 cnt += sprintf(&page[cnt], "Reader Pipe: ");
716 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
717 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
72e9bb54 718 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
a241ec65 719 cnt += sprintf(&page[cnt], "Reader Batch: ");
72e9bb54 720 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
a241ec65 721 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
72e9bb54 722 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
a241ec65
PM
723 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
724 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
725 cnt += sprintf(&page[cnt], " %d",
726 atomic_read(&rcu_torture_wcount[i]));
727 }
728 cnt += sprintf(&page[cnt], "\n");
c8e5b163 729 if (cur_ops->stats)
72e9bb54 730 cnt += cur_ops->stats(&page[cnt]);
a241ec65
PM
731 return cnt;
732}
733
734/*
735 * Print torture statistics. Caller must ensure that there is only
736 * one call to this function at a given time!!! This is normally
737 * accomplished by relying on the module system to only have one copy
738 * of the module loaded, and then by giving the rcu_torture_stats
739 * kthread full control (or the init/cleanup functions when rcu_torture_stats
740 * thread is not running).
741 */
742static void
743rcu_torture_stats_print(void)
744{
745 int cnt;
746
747 cnt = rcu_torture_printk(printk_buf);
748 printk(KERN_ALERT "%s", printk_buf);
749}
750
751/*
752 * Periodically prints torture statistics, if periodic statistics printing
753 * was specified via the stat_interval module parameter.
754 *
755 * No need to worry about fullstop here, since this one doesn't reference
756 * volatile state or register callbacks.
757 */
758static int
759rcu_torture_stats(void *arg)
760{
761 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
762 do {
763 schedule_timeout_interruptible(stat_interval * HZ);
764 rcu_torture_stats_print();
765 } while (!kthread_should_stop());
766 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
767 return 0;
768}
769
d84f5203
SV
770static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
771
772/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
773 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
774 */
b2896d2e 775static void rcu_torture_shuffle_tasks(void)
d84f5203 776{
f70316da 777 cpumask_t tmp_mask;
d84f5203
SV
778 int i;
779
f70316da 780 cpus_setall(tmp_mask);
86ef5c9a 781 get_online_cpus();
d84f5203
SV
782
783 /* No point in shuffling if there is only one online CPU (ex: UP) */
784 if (num_online_cpus() == 1) {
86ef5c9a 785 put_online_cpus();
d84f5203
SV
786 return;
787 }
788
789 if (rcu_idle_cpu != -1)
790 cpu_clear(rcu_idle_cpu, tmp_mask);
791
f70316da 792 set_cpus_allowed_ptr(current, &tmp_mask);
d84f5203 793
c8e5b163 794 if (reader_tasks) {
d84f5203
SV
795 for (i = 0; i < nrealreaders; i++)
796 if (reader_tasks[i])
f70316da
MT
797 set_cpus_allowed_ptr(reader_tasks[i],
798 &tmp_mask);
d84f5203
SV
799 }
800
c8e5b163 801 if (fakewriter_tasks) {
b772e1dd
JT
802 for (i = 0; i < nfakewriters; i++)
803 if (fakewriter_tasks[i])
f70316da
MT
804 set_cpus_allowed_ptr(fakewriter_tasks[i],
805 &tmp_mask);
b772e1dd
JT
806 }
807
d84f5203 808 if (writer_task)
f70316da 809 set_cpus_allowed_ptr(writer_task, &tmp_mask);
d84f5203
SV
810
811 if (stats_task)
f70316da 812 set_cpus_allowed_ptr(stats_task, &tmp_mask);
d84f5203
SV
813
814 if (rcu_idle_cpu == -1)
815 rcu_idle_cpu = num_online_cpus() - 1;
816 else
817 rcu_idle_cpu--;
818
86ef5c9a 819 put_online_cpus();
d84f5203
SV
820}
821
822/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
823 * system to become idle at a time and cut off its timer ticks. This is meant
824 * to test the support for such tickless idle CPU in RCU.
825 */
826static int
827rcu_torture_shuffle(void *arg)
828{
829 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
830 do {
831 schedule_timeout_interruptible(shuffle_interval * HZ);
832 rcu_torture_shuffle_tasks();
833 } while (!kthread_should_stop());
834 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
835 return 0;
836}
837
d120f65f
PM
838/* Cause the rcutorture test to "stutter", starting and stopping all
839 * threads periodically.
840 */
841static int
842rcu_torture_stutter(void *arg)
843{
844 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
845 do {
846 schedule_timeout_interruptible(stutter * HZ);
847 stutter_pause_test = 1;
848 if (!kthread_should_stop())
849 schedule_timeout_interruptible(stutter * HZ);
850 stutter_pause_test = 0;
851 } while (!kthread_should_stop());
852 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
853 return 0;
854}
855
95c38322
PM
856static inline void
857rcu_torture_print_module_parms(char *tag)
858{
b772e1dd
JT
859 printk(KERN_ALERT "%s" TORTURE_FLAG
860 "--- %s: nreaders=%d nfakewriters=%d "
95c38322 861 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
d120f65f 862 "shuffle_interval=%d stutter=%d\n",
b772e1dd 863 torture_type, tag, nrealreaders, nfakewriters,
d120f65f
PM
864 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
865 stutter);
95c38322
PM
866}
867
a241ec65
PM
868static void
869rcu_torture_cleanup(void)
870{
871 int i;
872
873 fullstop = 1;
d120f65f
PM
874 if (stutter_task) {
875 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
876 kthread_stop(stutter_task);
877 }
878 stutter_task = NULL;
c8e5b163 879 if (shuffler_task) {
d84f5203
SV
880 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
881 kthread_stop(shuffler_task);
882 }
883 shuffler_task = NULL;
884
c8e5b163 885 if (writer_task) {
a241ec65
PM
886 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
887 kthread_stop(writer_task);
888 }
889 writer_task = NULL;
890
c8e5b163 891 if (reader_tasks) {
a241ec65 892 for (i = 0; i < nrealreaders; i++) {
c8e5b163 893 if (reader_tasks[i]) {
a241ec65
PM
894 VERBOSE_PRINTK_STRING(
895 "Stopping rcu_torture_reader task");
896 kthread_stop(reader_tasks[i]);
897 }
898 reader_tasks[i] = NULL;
899 }
900 kfree(reader_tasks);
901 reader_tasks = NULL;
902 }
903 rcu_torture_current = NULL;
904
c8e5b163 905 if (fakewriter_tasks) {
b772e1dd 906 for (i = 0; i < nfakewriters; i++) {
c8e5b163 907 if (fakewriter_tasks[i]) {
b772e1dd
JT
908 VERBOSE_PRINTK_STRING(
909 "Stopping rcu_torture_fakewriter task");
910 kthread_stop(fakewriter_tasks[i]);
911 }
912 fakewriter_tasks[i] = NULL;
913 }
914 kfree(fakewriter_tasks);
915 fakewriter_tasks = NULL;
916 }
917
c8e5b163 918 if (stats_task) {
a241ec65
PM
919 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
920 kthread_stop(stats_task);
921 }
922 stats_task = NULL;
923
924 /* Wait for all RCU callbacks to fire. */
2326974d
PM
925
926 if (cur_ops->cb_barrier != NULL)
927 cur_ops->cb_barrier();
a241ec65 928
a241ec65 929 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
72e9bb54 930
c8e5b163 931 if (cur_ops->cleanup)
72e9bb54 932 cur_ops->cleanup();
95c38322
PM
933 if (atomic_read(&n_rcu_torture_error))
934 rcu_torture_print_module_parms("End of test: FAILURE");
935 else
936 rcu_torture_print_module_parms("End of test: SUCCESS");
a241ec65
PM
937}
938
6f8bc500 939static int __init
a241ec65
PM
940rcu_torture_init(void)
941{
942 int i;
943 int cpu;
944 int firsterr = 0;
ade5fb81
JT
945 static struct rcu_torture_ops *torture_ops[] =
946 { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops,
2326974d 947 &srcu_ops, &sched_ops, &sched_ops_sync, };
a241ec65
PM
948
949 /* Process args and tell the world that the torturer is on the job. */
ade5fb81 950 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
72e9bb54 951 cur_ops = torture_ops[i];
ade5fb81 952 if (strcmp(torture_type, cur_ops->name) == 0)
72e9bb54 953 break;
72e9bb54 954 }
ade5fb81 955 if (i == ARRAY_SIZE(torture_ops)) {
72e9bb54
PM
956 printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
957 torture_type);
958 return (-EINVAL);
959 }
c8e5b163 960 if (cur_ops->init)
72e9bb54
PM
961 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
962
a241ec65
PM
963 if (nreaders >= 0)
964 nrealreaders = nreaders;
965 else
966 nrealreaders = 2 * num_online_cpus();
95c38322 967 rcu_torture_print_module_parms("Start of test");
a241ec65
PM
968 fullstop = 0;
969
970 /* Set up the freelist. */
971
972 INIT_LIST_HEAD(&rcu_torture_freelist);
788e770e 973 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
996417d2 974 rcu_tortures[i].rtort_mbtest = 0;
a241ec65
PM
975 list_add_tail(&rcu_tortures[i].rtort_free,
976 &rcu_torture_freelist);
977 }
978
979 /* Initialize the statistics so that each run gets its own numbers. */
980
981 rcu_torture_current = NULL;
982 rcu_torture_current_version = 0;
983 atomic_set(&n_rcu_torture_alloc, 0);
984 atomic_set(&n_rcu_torture_alloc_fail, 0);
985 atomic_set(&n_rcu_torture_free, 0);
996417d2
PM
986 atomic_set(&n_rcu_torture_mberror, 0);
987 atomic_set(&n_rcu_torture_error, 0);
a241ec65
PM
988 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
989 atomic_set(&rcu_torture_wcount[i], 0);
0a945022 990 for_each_possible_cpu(cpu) {
a241ec65
PM
991 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
992 per_cpu(rcu_torture_count, cpu)[i] = 0;
993 per_cpu(rcu_torture_batch, cpu)[i] = 0;
994 }
995 }
996
997 /* Start up the kthreads. */
998
999 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1000 writer_task = kthread_run(rcu_torture_writer, NULL,
1001 "rcu_torture_writer");
1002 if (IS_ERR(writer_task)) {
1003 firsterr = PTR_ERR(writer_task);
1004 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1005 writer_task = NULL;
1006 goto unwind;
1007 }
b772e1dd
JT
1008 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1009 GFP_KERNEL);
1010 if (fakewriter_tasks == NULL) {
1011 VERBOSE_PRINTK_ERRSTRING("out of memory");
1012 firsterr = -ENOMEM;
1013 goto unwind;
1014 }
1015 for (i = 0; i < nfakewriters; i++) {
1016 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1017 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1018 "rcu_torture_fakewriter");
1019 if (IS_ERR(fakewriter_tasks[i])) {
1020 firsterr = PTR_ERR(fakewriter_tasks[i]);
1021 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1022 fakewriter_tasks[i] = NULL;
1023 goto unwind;
1024 }
1025 }
2860aaba 1026 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
a241ec65
PM
1027 GFP_KERNEL);
1028 if (reader_tasks == NULL) {
1029 VERBOSE_PRINTK_ERRSTRING("out of memory");
1030 firsterr = -ENOMEM;
1031 goto unwind;
1032 }
1033 for (i = 0; i < nrealreaders; i++) {
1034 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1035 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1036 "rcu_torture_reader");
1037 if (IS_ERR(reader_tasks[i])) {
1038 firsterr = PTR_ERR(reader_tasks[i]);
1039 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1040 reader_tasks[i] = NULL;
1041 goto unwind;
1042 }
1043 }
1044 if (stat_interval > 0) {
1045 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1046 stats_task = kthread_run(rcu_torture_stats, NULL,
1047 "rcu_torture_stats");
1048 if (IS_ERR(stats_task)) {
1049 firsterr = PTR_ERR(stats_task);
1050 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1051 stats_task = NULL;
1052 goto unwind;
1053 }
1054 }
d84f5203
SV
1055 if (test_no_idle_hz) {
1056 rcu_idle_cpu = num_online_cpus() - 1;
1057 /* Create the shuffler thread */
1058 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1059 "rcu_torture_shuffle");
1060 if (IS_ERR(shuffler_task)) {
1061 firsterr = PTR_ERR(shuffler_task);
1062 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1063 shuffler_task = NULL;
1064 goto unwind;
1065 }
1066 }
d120f65f
PM
1067 if (stutter < 0)
1068 stutter = 0;
1069 if (stutter) {
1070 /* Create the stutter thread */
1071 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1072 "rcu_torture_stutter");
1073 if (IS_ERR(stutter_task)) {
1074 firsterr = PTR_ERR(stutter_task);
1075 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1076 stutter_task = NULL;
1077 goto unwind;
1078 }
1079 }
a241ec65
PM
1080 return 0;
1081
1082unwind:
1083 rcu_torture_cleanup();
1084 return firsterr;
1085}
1086
1087module_init(rcu_torture_init);
1088module_exit(rcu_torture_cleanup);