perf bench: Modify builtin-bench.c for processing common options
[linux-2.6/kvm.git] / kernel / rcutorture.c
blob697c0a0229d47026c07434910d7dc231e2d1bb97
1 /*
2 * Read-Copy Update module-based torture test facility
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.
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.
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.
18 * Copyright (C) IBM Corporation, 2005, 2006
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
21 * Josh Triplett <josh@freedesktop.org>
23 * See also: Documentation/RCU/torture.txt
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/completion.h>
39 #include <linux/moduleparam.h>
40 #include <linux/percpu.h>
41 #include <linux/notifier.h>
42 #include <linux/reboot.h>
43 #include <linux/freezer.h>
44 #include <linux/cpu.h>
45 #include <linux/delay.h>
46 #include <linux/stat.h>
47 #include <linux/srcu.h>
48 #include <linux/slab.h>
49 #include <asm/byteorder.h>
51 MODULE_LICENSE("GPL");
52 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
53 "Josh Triplett <josh@freedesktop.org>");
55 static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
56 static int nfakewriters = 4; /* # fake writer threads */
57 static int stat_interval; /* Interval between stats, in seconds. */
58 /* Defaults to "only at end of test". */
59 static int verbose; /* Print more debug info. */
60 static int test_no_idle_hz; /* Test RCU's support for tickless idle CPUs. */
61 static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
62 static int stutter = 5; /* Start/stop testing interval (in sec) */
63 static int irqreader = 1; /* RCU readers from irq (timers). */
64 static char *torture_type = "rcu"; /* What RCU implementation to torture. */
66 module_param(nreaders, int, 0444);
67 MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
68 module_param(nfakewriters, int, 0444);
69 MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
70 module_param(stat_interval, int, 0444);
71 MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
72 module_param(verbose, bool, 0444);
73 MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
74 module_param(test_no_idle_hz, bool, 0444);
75 MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
76 module_param(shuffle_interval, int, 0444);
77 MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
78 module_param(stutter, int, 0444);
79 MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
80 module_param(irqreader, int, 0444);
81 MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
82 module_param(torture_type, charp, 0444);
83 MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
85 #define TORTURE_FLAG "-torture:"
86 #define PRINTK_STRING(s) \
87 do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
88 #define VERBOSE_PRINTK_STRING(s) \
89 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
90 #define VERBOSE_PRINTK_ERRSTRING(s) \
91 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
93 static char printk_buf[4096];
95 static int nrealreaders;
96 static struct task_struct *writer_task;
97 static struct task_struct **fakewriter_tasks;
98 static struct task_struct **reader_tasks;
99 static struct task_struct *stats_task;
100 static struct task_struct *shuffler_task;
101 static struct task_struct *stutter_task;
103 #define RCU_TORTURE_PIPE_LEN 10
105 struct rcu_torture {
106 struct rcu_head rtort_rcu;
107 int rtort_pipe_count;
108 struct list_head rtort_free;
109 int rtort_mbtest;
112 static LIST_HEAD(rcu_torture_freelist);
113 static struct rcu_torture *rcu_torture_current;
114 static long rcu_torture_current_version;
115 static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
116 static DEFINE_SPINLOCK(rcu_torture_lock);
117 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
118 { 0 };
119 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
120 { 0 };
121 static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
122 static atomic_t n_rcu_torture_alloc;
123 static atomic_t n_rcu_torture_alloc_fail;
124 static atomic_t n_rcu_torture_free;
125 static atomic_t n_rcu_torture_mberror;
126 static atomic_t n_rcu_torture_error;
127 static long n_rcu_torture_timers;
128 static struct list_head rcu_torture_removed;
129 static cpumask_var_t shuffle_tmp_mask;
131 static int stutter_pause_test;
133 #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
134 #define RCUTORTURE_RUNNABLE_INIT 1
135 #else
136 #define RCUTORTURE_RUNNABLE_INIT 0
137 #endif
138 int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
140 /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
142 #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
143 #define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
144 #define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
145 static int fullstop = FULLSTOP_RMMOD;
146 DEFINE_MUTEX(fullstop_mutex); /* Protect fullstop transitions and spawning */
147 /* of kthreads. */
150 * Detect and respond to a system shutdown.
152 static int
153 rcutorture_shutdown_notify(struct notifier_block *unused1,
154 unsigned long unused2, void *unused3)
156 mutex_lock(&fullstop_mutex);
157 if (fullstop == FULLSTOP_DONTSTOP)
158 fullstop = FULLSTOP_SHUTDOWN;
159 else
160 printk(KERN_WARNING /* but going down anyway, so... */
161 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
162 mutex_unlock(&fullstop_mutex);
163 return NOTIFY_DONE;
167 * Absorb kthreads into a kernel function that won't return, so that
168 * they won't ever access module text or data again.
170 static void rcutorture_shutdown_absorb(char *title)
172 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
173 printk(KERN_NOTICE
174 "rcutorture thread %s parking due to system shutdown\n",
175 title);
176 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
181 * Allocate an element from the rcu_tortures pool.
183 static struct rcu_torture *
184 rcu_torture_alloc(void)
186 struct list_head *p;
188 spin_lock_bh(&rcu_torture_lock);
189 if (list_empty(&rcu_torture_freelist)) {
190 atomic_inc(&n_rcu_torture_alloc_fail);
191 spin_unlock_bh(&rcu_torture_lock);
192 return NULL;
194 atomic_inc(&n_rcu_torture_alloc);
195 p = rcu_torture_freelist.next;
196 list_del_init(p);
197 spin_unlock_bh(&rcu_torture_lock);
198 return container_of(p, struct rcu_torture, rtort_free);
202 * Free an element to the rcu_tortures pool.
204 static void
205 rcu_torture_free(struct rcu_torture *p)
207 atomic_inc(&n_rcu_torture_free);
208 spin_lock_bh(&rcu_torture_lock);
209 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
210 spin_unlock_bh(&rcu_torture_lock);
213 struct rcu_random_state {
214 unsigned long rrs_state;
215 long rrs_count;
218 #define RCU_RANDOM_MULT 39916801 /* prime */
219 #define RCU_RANDOM_ADD 479001701 /* prime */
220 #define RCU_RANDOM_REFRESH 10000
222 #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
225 * Crude but fast random-number generator. Uses a linear congruential
226 * generator, with occasional help from cpu_clock().
228 static unsigned long
229 rcu_random(struct rcu_random_state *rrsp)
231 if (--rrsp->rrs_count < 0) {
232 rrsp->rrs_state +=
233 (unsigned long)cpu_clock(raw_smp_processor_id());
234 rrsp->rrs_count = RCU_RANDOM_REFRESH;
236 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
237 return swahw32(rrsp->rrs_state);
240 static void
241 rcu_stutter_wait(char *title)
243 while (stutter_pause_test || !rcutorture_runnable) {
244 if (rcutorture_runnable)
245 schedule_timeout_interruptible(1);
246 else
247 schedule_timeout_interruptible(round_jiffies_relative(HZ));
248 rcutorture_shutdown_absorb(title);
253 * Operations vector for selecting different types of tests.
256 struct rcu_torture_ops {
257 void (*init)(void);
258 void (*cleanup)(void);
259 int (*readlock)(void);
260 void (*read_delay)(struct rcu_random_state *rrsp);
261 void (*readunlock)(int idx);
262 int (*completed)(void);
263 void (*deferred_free)(struct rcu_torture *p);
264 void (*sync)(void);
265 void (*cb_barrier)(void);
266 int (*stats)(char *page);
267 int irq_capable;
268 char *name;
271 static struct rcu_torture_ops *cur_ops;
274 * Definitions for rcu torture testing.
277 static int rcu_torture_read_lock(void) __acquires(RCU)
279 rcu_read_lock();
280 return 0;
283 static void rcu_read_delay(struct rcu_random_state *rrsp)
285 const unsigned long shortdelay_us = 200;
286 const unsigned long longdelay_ms = 50;
288 /* We want a short delay sometimes to make a reader delay the grace
289 * period, and we want a long delay occasionally to trigger
290 * force_quiescent_state. */
292 if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
293 mdelay(longdelay_ms);
294 if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
295 udelay(shortdelay_us);
298 static void rcu_torture_read_unlock(int idx) __releases(RCU)
300 rcu_read_unlock();
303 static int rcu_torture_completed(void)
305 return rcu_batches_completed();
308 static void
309 rcu_torture_cb(struct rcu_head *p)
311 int i;
312 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
314 if (fullstop != FULLSTOP_DONTSTOP) {
315 /* Test is ending, just drop callbacks on the floor. */
316 /* The next initialization will pick up the pieces. */
317 return;
319 i = rp->rtort_pipe_count;
320 if (i > RCU_TORTURE_PIPE_LEN)
321 i = RCU_TORTURE_PIPE_LEN;
322 atomic_inc(&rcu_torture_wcount[i]);
323 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
324 rp->rtort_mbtest = 0;
325 rcu_torture_free(rp);
326 } else
327 cur_ops->deferred_free(rp);
330 static void rcu_torture_deferred_free(struct rcu_torture *p)
332 call_rcu(&p->rtort_rcu, rcu_torture_cb);
335 static struct rcu_torture_ops rcu_ops = {
336 .init = NULL,
337 .cleanup = NULL,
338 .readlock = rcu_torture_read_lock,
339 .read_delay = rcu_read_delay,
340 .readunlock = rcu_torture_read_unlock,
341 .completed = rcu_torture_completed,
342 .deferred_free = rcu_torture_deferred_free,
343 .sync = synchronize_rcu,
344 .cb_barrier = rcu_barrier,
345 .stats = NULL,
346 .irq_capable = 1,
347 .name = "rcu"
350 static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
352 int i;
353 struct rcu_torture *rp;
354 struct rcu_torture *rp1;
356 cur_ops->sync();
357 list_add(&p->rtort_free, &rcu_torture_removed);
358 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
359 i = rp->rtort_pipe_count;
360 if (i > RCU_TORTURE_PIPE_LEN)
361 i = RCU_TORTURE_PIPE_LEN;
362 atomic_inc(&rcu_torture_wcount[i]);
363 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
364 rp->rtort_mbtest = 0;
365 list_del(&rp->rtort_free);
366 rcu_torture_free(rp);
371 static void rcu_sync_torture_init(void)
373 INIT_LIST_HEAD(&rcu_torture_removed);
376 static struct rcu_torture_ops rcu_sync_ops = {
377 .init = rcu_sync_torture_init,
378 .cleanup = NULL,
379 .readlock = rcu_torture_read_lock,
380 .read_delay = rcu_read_delay,
381 .readunlock = rcu_torture_read_unlock,
382 .completed = rcu_torture_completed,
383 .deferred_free = rcu_sync_torture_deferred_free,
384 .sync = synchronize_rcu,
385 .cb_barrier = NULL,
386 .stats = NULL,
387 .irq_capable = 1,
388 .name = "rcu_sync"
392 * Definitions for rcu_bh torture testing.
395 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
397 rcu_read_lock_bh();
398 return 0;
401 static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
403 rcu_read_unlock_bh();
406 static int rcu_bh_torture_completed(void)
408 return rcu_batches_completed_bh();
411 static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
413 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
416 struct rcu_bh_torture_synchronize {
417 struct rcu_head head;
418 struct completion completion;
421 static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
423 struct rcu_bh_torture_synchronize *rcu;
425 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
426 complete(&rcu->completion);
429 static void rcu_bh_torture_synchronize(void)
431 struct rcu_bh_torture_synchronize rcu;
433 init_completion(&rcu.completion);
434 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
435 wait_for_completion(&rcu.completion);
438 static struct rcu_torture_ops rcu_bh_ops = {
439 .init = NULL,
440 .cleanup = NULL,
441 .readlock = rcu_bh_torture_read_lock,
442 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
443 .readunlock = rcu_bh_torture_read_unlock,
444 .completed = rcu_bh_torture_completed,
445 .deferred_free = rcu_bh_torture_deferred_free,
446 .sync = rcu_bh_torture_synchronize,
447 .cb_barrier = rcu_barrier_bh,
448 .stats = NULL,
449 .irq_capable = 1,
450 .name = "rcu_bh"
453 static struct rcu_torture_ops rcu_bh_sync_ops = {
454 .init = rcu_sync_torture_init,
455 .cleanup = NULL,
456 .readlock = rcu_bh_torture_read_lock,
457 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
458 .readunlock = rcu_bh_torture_read_unlock,
459 .completed = rcu_bh_torture_completed,
460 .deferred_free = rcu_sync_torture_deferred_free,
461 .sync = rcu_bh_torture_synchronize,
462 .cb_barrier = NULL,
463 .stats = NULL,
464 .irq_capable = 1,
465 .name = "rcu_bh_sync"
469 * Definitions for srcu torture testing.
472 static struct srcu_struct srcu_ctl;
474 static void srcu_torture_init(void)
476 init_srcu_struct(&srcu_ctl);
477 rcu_sync_torture_init();
480 static void srcu_torture_cleanup(void)
482 synchronize_srcu(&srcu_ctl);
483 cleanup_srcu_struct(&srcu_ctl);
486 static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
488 return srcu_read_lock(&srcu_ctl);
491 static void srcu_read_delay(struct rcu_random_state *rrsp)
493 long delay;
494 const long uspertick = 1000000 / HZ;
495 const long longdelay = 10;
497 /* We want there to be long-running readers, but not all the time. */
499 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
500 if (!delay)
501 schedule_timeout_interruptible(longdelay);
504 static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
506 srcu_read_unlock(&srcu_ctl, idx);
509 static int srcu_torture_completed(void)
511 return srcu_batches_completed(&srcu_ctl);
514 static void srcu_torture_synchronize(void)
516 synchronize_srcu(&srcu_ctl);
519 static int srcu_torture_stats(char *page)
521 int cnt = 0;
522 int cpu;
523 int idx = srcu_ctl.completed & 0x1;
525 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
526 torture_type, TORTURE_FLAG, idx);
527 for_each_possible_cpu(cpu) {
528 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
529 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
530 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
532 cnt += sprintf(&page[cnt], "\n");
533 return cnt;
536 static struct rcu_torture_ops srcu_ops = {
537 .init = srcu_torture_init,
538 .cleanup = srcu_torture_cleanup,
539 .readlock = srcu_torture_read_lock,
540 .read_delay = srcu_read_delay,
541 .readunlock = srcu_torture_read_unlock,
542 .completed = srcu_torture_completed,
543 .deferred_free = rcu_sync_torture_deferred_free,
544 .sync = srcu_torture_synchronize,
545 .cb_barrier = NULL,
546 .stats = srcu_torture_stats,
547 .name = "srcu"
551 * Definitions for sched torture testing.
554 static int sched_torture_read_lock(void)
556 preempt_disable();
557 return 0;
560 static void sched_torture_read_unlock(int idx)
562 preempt_enable();
565 static int sched_torture_completed(void)
567 return 0;
570 static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
572 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
575 static void sched_torture_synchronize(void)
577 synchronize_sched();
580 static struct rcu_torture_ops sched_ops = {
581 .init = rcu_sync_torture_init,
582 .cleanup = NULL,
583 .readlock = sched_torture_read_lock,
584 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
585 .readunlock = sched_torture_read_unlock,
586 .completed = sched_torture_completed,
587 .deferred_free = rcu_sched_torture_deferred_free,
588 .sync = sched_torture_synchronize,
589 .cb_barrier = rcu_barrier_sched,
590 .stats = NULL,
591 .irq_capable = 1,
592 .name = "sched"
595 static struct rcu_torture_ops sched_ops_sync = {
596 .init = rcu_sync_torture_init,
597 .cleanup = NULL,
598 .readlock = sched_torture_read_lock,
599 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
600 .readunlock = sched_torture_read_unlock,
601 .completed = sched_torture_completed,
602 .deferred_free = rcu_sync_torture_deferred_free,
603 .sync = sched_torture_synchronize,
604 .cb_barrier = NULL,
605 .stats = NULL,
606 .name = "sched_sync"
609 static struct rcu_torture_ops sched_expedited_ops = {
610 .init = rcu_sync_torture_init,
611 .cleanup = NULL,
612 .readlock = sched_torture_read_lock,
613 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
614 .readunlock = sched_torture_read_unlock,
615 .completed = sched_torture_completed,
616 .deferred_free = rcu_sync_torture_deferred_free,
617 .sync = synchronize_sched_expedited,
618 .cb_barrier = NULL,
619 .stats = rcu_expedited_torture_stats,
620 .irq_capable = 1,
621 .name = "sched_expedited"
625 * RCU torture writer kthread. Repeatedly substitutes a new structure
626 * for that pointed to by rcu_torture_current, freeing the old structure
627 * after a series of grace periods (the "pipeline").
629 static int
630 rcu_torture_writer(void *arg)
632 int i;
633 long oldbatch = rcu_batches_completed();
634 struct rcu_torture *rp;
635 struct rcu_torture *old_rp;
636 static DEFINE_RCU_RANDOM(rand);
638 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
639 set_user_nice(current, 19);
641 do {
642 schedule_timeout_uninterruptible(1);
643 rp = rcu_torture_alloc();
644 if (rp == NULL)
645 continue;
646 rp->rtort_pipe_count = 0;
647 udelay(rcu_random(&rand) & 0x3ff);
648 old_rp = rcu_torture_current;
649 rp->rtort_mbtest = 1;
650 rcu_assign_pointer(rcu_torture_current, rp);
651 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
652 if (old_rp) {
653 i = old_rp->rtort_pipe_count;
654 if (i > RCU_TORTURE_PIPE_LEN)
655 i = RCU_TORTURE_PIPE_LEN;
656 atomic_inc(&rcu_torture_wcount[i]);
657 old_rp->rtort_pipe_count++;
658 cur_ops->deferred_free(old_rp);
660 rcu_torture_current_version++;
661 oldbatch = cur_ops->completed();
662 rcu_stutter_wait("rcu_torture_writer");
663 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
664 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
665 rcutorture_shutdown_absorb("rcu_torture_writer");
666 while (!kthread_should_stop())
667 schedule_timeout_uninterruptible(1);
668 return 0;
672 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
673 * delay between calls.
675 static int
676 rcu_torture_fakewriter(void *arg)
678 DEFINE_RCU_RANDOM(rand);
680 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
681 set_user_nice(current, 19);
683 do {
684 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
685 udelay(rcu_random(&rand) & 0x3ff);
686 cur_ops->sync();
687 rcu_stutter_wait("rcu_torture_fakewriter");
688 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
690 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
691 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
692 while (!kthread_should_stop())
693 schedule_timeout_uninterruptible(1);
694 return 0;
698 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
699 * incrementing the corresponding element of the pipeline array. The
700 * counter in the element should never be greater than 1, otherwise, the
701 * RCU implementation is broken.
703 static void rcu_torture_timer(unsigned long unused)
705 int idx;
706 int completed;
707 static DEFINE_RCU_RANDOM(rand);
708 static DEFINE_SPINLOCK(rand_lock);
709 struct rcu_torture *p;
710 int pipe_count;
712 idx = cur_ops->readlock();
713 completed = cur_ops->completed();
714 p = rcu_dereference(rcu_torture_current);
715 if (p == NULL) {
716 /* Leave because rcu_torture_writer is not yet underway */
717 cur_ops->readunlock(idx);
718 return;
720 if (p->rtort_mbtest == 0)
721 atomic_inc(&n_rcu_torture_mberror);
722 spin_lock(&rand_lock);
723 cur_ops->read_delay(&rand);
724 n_rcu_torture_timers++;
725 spin_unlock(&rand_lock);
726 preempt_disable();
727 pipe_count = p->rtort_pipe_count;
728 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
729 /* Should not happen, but... */
730 pipe_count = RCU_TORTURE_PIPE_LEN;
732 ++__get_cpu_var(rcu_torture_count)[pipe_count];
733 completed = cur_ops->completed() - completed;
734 if (completed > RCU_TORTURE_PIPE_LEN) {
735 /* Should not happen, but... */
736 completed = RCU_TORTURE_PIPE_LEN;
738 ++__get_cpu_var(rcu_torture_batch)[completed];
739 preempt_enable();
740 cur_ops->readunlock(idx);
744 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
745 * incrementing the corresponding element of the pipeline array. The
746 * counter in the element should never be greater than 1, otherwise, the
747 * RCU implementation is broken.
749 static int
750 rcu_torture_reader(void *arg)
752 int completed;
753 int idx;
754 DEFINE_RCU_RANDOM(rand);
755 struct rcu_torture *p;
756 int pipe_count;
757 struct timer_list t;
759 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
760 set_user_nice(current, 19);
761 if (irqreader && cur_ops->irq_capable)
762 setup_timer_on_stack(&t, rcu_torture_timer, 0);
764 do {
765 if (irqreader && cur_ops->irq_capable) {
766 if (!timer_pending(&t))
767 mod_timer(&t, 1);
769 idx = cur_ops->readlock();
770 completed = cur_ops->completed();
771 p = rcu_dereference(rcu_torture_current);
772 if (p == NULL) {
773 /* Wait for rcu_torture_writer to get underway */
774 cur_ops->readunlock(idx);
775 schedule_timeout_interruptible(HZ);
776 continue;
778 if (p->rtort_mbtest == 0)
779 atomic_inc(&n_rcu_torture_mberror);
780 cur_ops->read_delay(&rand);
781 preempt_disable();
782 pipe_count = p->rtort_pipe_count;
783 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
784 /* Should not happen, but... */
785 pipe_count = RCU_TORTURE_PIPE_LEN;
787 ++__get_cpu_var(rcu_torture_count)[pipe_count];
788 completed = cur_ops->completed() - completed;
789 if (completed > RCU_TORTURE_PIPE_LEN) {
790 /* Should not happen, but... */
791 completed = RCU_TORTURE_PIPE_LEN;
793 ++__get_cpu_var(rcu_torture_batch)[completed];
794 preempt_enable();
795 cur_ops->readunlock(idx);
796 schedule();
797 rcu_stutter_wait("rcu_torture_reader");
798 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
799 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
800 rcutorture_shutdown_absorb("rcu_torture_reader");
801 if (irqreader && cur_ops->irq_capable)
802 del_timer_sync(&t);
803 while (!kthread_should_stop())
804 schedule_timeout_uninterruptible(1);
805 return 0;
809 * Create an RCU-torture statistics message in the specified buffer.
811 static int
812 rcu_torture_printk(char *page)
814 int cnt = 0;
815 int cpu;
816 int i;
817 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
818 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
820 for_each_possible_cpu(cpu) {
821 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
822 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
823 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
826 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
827 if (pipesummary[i] != 0)
828 break;
830 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
831 cnt += sprintf(&page[cnt],
832 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
833 "rtmbe: %d nt: %ld",
834 rcu_torture_current,
835 rcu_torture_current_version,
836 list_empty(&rcu_torture_freelist),
837 atomic_read(&n_rcu_torture_alloc),
838 atomic_read(&n_rcu_torture_alloc_fail),
839 atomic_read(&n_rcu_torture_free),
840 atomic_read(&n_rcu_torture_mberror),
841 n_rcu_torture_timers);
842 if (atomic_read(&n_rcu_torture_mberror) != 0)
843 cnt += sprintf(&page[cnt], " !!!");
844 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
845 if (i > 1) {
846 cnt += sprintf(&page[cnt], "!!! ");
847 atomic_inc(&n_rcu_torture_error);
848 WARN_ON_ONCE(1);
850 cnt += sprintf(&page[cnt], "Reader Pipe: ");
851 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
852 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
853 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
854 cnt += sprintf(&page[cnt], "Reader Batch: ");
855 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
856 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
857 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
858 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
859 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
860 cnt += sprintf(&page[cnt], " %d",
861 atomic_read(&rcu_torture_wcount[i]));
863 cnt += sprintf(&page[cnt], "\n");
864 if (cur_ops->stats)
865 cnt += cur_ops->stats(&page[cnt]);
866 return cnt;
870 * Print torture statistics. Caller must ensure that there is only
871 * one call to this function at a given time!!! This is normally
872 * accomplished by relying on the module system to only have one copy
873 * of the module loaded, and then by giving the rcu_torture_stats
874 * kthread full control (or the init/cleanup functions when rcu_torture_stats
875 * thread is not running).
877 static void
878 rcu_torture_stats_print(void)
880 int cnt;
882 cnt = rcu_torture_printk(printk_buf);
883 printk(KERN_ALERT "%s", printk_buf);
887 * Periodically prints torture statistics, if periodic statistics printing
888 * was specified via the stat_interval module parameter.
890 * No need to worry about fullstop here, since this one doesn't reference
891 * volatile state or register callbacks.
893 static int
894 rcu_torture_stats(void *arg)
896 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
897 do {
898 schedule_timeout_interruptible(stat_interval * HZ);
899 rcu_torture_stats_print();
900 rcutorture_shutdown_absorb("rcu_torture_stats");
901 } while (!kthread_should_stop());
902 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
903 return 0;
906 static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
908 /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
909 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
911 static void rcu_torture_shuffle_tasks(void)
913 int i;
915 cpumask_setall(shuffle_tmp_mask);
916 get_online_cpus();
918 /* No point in shuffling if there is only one online CPU (ex: UP) */
919 if (num_online_cpus() == 1) {
920 put_online_cpus();
921 return;
924 if (rcu_idle_cpu != -1)
925 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
927 set_cpus_allowed_ptr(current, shuffle_tmp_mask);
929 if (reader_tasks) {
930 for (i = 0; i < nrealreaders; i++)
931 if (reader_tasks[i])
932 set_cpus_allowed_ptr(reader_tasks[i],
933 shuffle_tmp_mask);
936 if (fakewriter_tasks) {
937 for (i = 0; i < nfakewriters; i++)
938 if (fakewriter_tasks[i])
939 set_cpus_allowed_ptr(fakewriter_tasks[i],
940 shuffle_tmp_mask);
943 if (writer_task)
944 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
946 if (stats_task)
947 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
949 if (rcu_idle_cpu == -1)
950 rcu_idle_cpu = num_online_cpus() - 1;
951 else
952 rcu_idle_cpu--;
954 put_online_cpus();
957 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
958 * system to become idle at a time and cut off its timer ticks. This is meant
959 * to test the support for such tickless idle CPU in RCU.
961 static int
962 rcu_torture_shuffle(void *arg)
964 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
965 do {
966 schedule_timeout_interruptible(shuffle_interval * HZ);
967 rcu_torture_shuffle_tasks();
968 rcutorture_shutdown_absorb("rcu_torture_shuffle");
969 } while (!kthread_should_stop());
970 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
971 return 0;
974 /* Cause the rcutorture test to "stutter", starting and stopping all
975 * threads periodically.
977 static int
978 rcu_torture_stutter(void *arg)
980 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
981 do {
982 schedule_timeout_interruptible(stutter * HZ);
983 stutter_pause_test = 1;
984 if (!kthread_should_stop())
985 schedule_timeout_interruptible(stutter * HZ);
986 stutter_pause_test = 0;
987 rcutorture_shutdown_absorb("rcu_torture_stutter");
988 } while (!kthread_should_stop());
989 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
990 return 0;
993 static inline void
994 rcu_torture_print_module_parms(char *tag)
996 printk(KERN_ALERT "%s" TORTURE_FLAG
997 "--- %s: nreaders=%d nfakewriters=%d "
998 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
999 "shuffle_interval=%d stutter=%d irqreader=%d\n",
1000 torture_type, tag, nrealreaders, nfakewriters,
1001 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1002 stutter, irqreader);
1005 static struct notifier_block rcutorture_nb = {
1006 .notifier_call = rcutorture_shutdown_notify,
1009 static void
1010 rcu_torture_cleanup(void)
1012 int i;
1014 mutex_lock(&fullstop_mutex);
1015 if (fullstop == FULLSTOP_SHUTDOWN) {
1016 printk(KERN_WARNING /* but going down anyway, so... */
1017 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
1018 mutex_unlock(&fullstop_mutex);
1019 schedule_timeout_uninterruptible(10);
1020 if (cur_ops->cb_barrier != NULL)
1021 cur_ops->cb_barrier();
1022 return;
1024 fullstop = FULLSTOP_RMMOD;
1025 mutex_unlock(&fullstop_mutex);
1026 unregister_reboot_notifier(&rcutorture_nb);
1027 if (stutter_task) {
1028 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1029 kthread_stop(stutter_task);
1031 stutter_task = NULL;
1032 if (shuffler_task) {
1033 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1034 kthread_stop(shuffler_task);
1035 free_cpumask_var(shuffle_tmp_mask);
1037 shuffler_task = NULL;
1039 if (writer_task) {
1040 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1041 kthread_stop(writer_task);
1043 writer_task = NULL;
1045 if (reader_tasks) {
1046 for (i = 0; i < nrealreaders; i++) {
1047 if (reader_tasks[i]) {
1048 VERBOSE_PRINTK_STRING(
1049 "Stopping rcu_torture_reader task");
1050 kthread_stop(reader_tasks[i]);
1052 reader_tasks[i] = NULL;
1054 kfree(reader_tasks);
1055 reader_tasks = NULL;
1057 rcu_torture_current = NULL;
1059 if (fakewriter_tasks) {
1060 for (i = 0; i < nfakewriters; i++) {
1061 if (fakewriter_tasks[i]) {
1062 VERBOSE_PRINTK_STRING(
1063 "Stopping rcu_torture_fakewriter task");
1064 kthread_stop(fakewriter_tasks[i]);
1066 fakewriter_tasks[i] = NULL;
1068 kfree(fakewriter_tasks);
1069 fakewriter_tasks = NULL;
1072 if (stats_task) {
1073 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1074 kthread_stop(stats_task);
1076 stats_task = NULL;
1078 /* Wait for all RCU callbacks to fire. */
1080 if (cur_ops->cb_barrier != NULL)
1081 cur_ops->cb_barrier();
1083 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
1085 if (cur_ops->cleanup)
1086 cur_ops->cleanup();
1087 if (atomic_read(&n_rcu_torture_error))
1088 rcu_torture_print_module_parms("End of test: FAILURE");
1089 else
1090 rcu_torture_print_module_parms("End of test: SUCCESS");
1093 static int __init
1094 rcu_torture_init(void)
1096 int i;
1097 int cpu;
1098 int firsterr = 0;
1099 static struct rcu_torture_ops *torture_ops[] =
1100 { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops,
1101 &sched_expedited_ops,
1102 &srcu_ops, &sched_ops, &sched_ops_sync, };
1104 mutex_lock(&fullstop_mutex);
1106 /* Process args and tell the world that the torturer is on the job. */
1107 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1108 cur_ops = torture_ops[i];
1109 if (strcmp(torture_type, cur_ops->name) == 0)
1110 break;
1112 if (i == ARRAY_SIZE(torture_ops)) {
1113 printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
1114 torture_type);
1115 mutex_unlock(&fullstop_mutex);
1116 return -EINVAL;
1118 if (cur_ops->init)
1119 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1121 if (nreaders >= 0)
1122 nrealreaders = nreaders;
1123 else
1124 nrealreaders = 2 * num_online_cpus();
1125 rcu_torture_print_module_parms("Start of test");
1126 fullstop = FULLSTOP_DONTSTOP;
1128 /* Set up the freelist. */
1130 INIT_LIST_HEAD(&rcu_torture_freelist);
1131 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1132 rcu_tortures[i].rtort_mbtest = 0;
1133 list_add_tail(&rcu_tortures[i].rtort_free,
1134 &rcu_torture_freelist);
1137 /* Initialize the statistics so that each run gets its own numbers. */
1139 rcu_torture_current = NULL;
1140 rcu_torture_current_version = 0;
1141 atomic_set(&n_rcu_torture_alloc, 0);
1142 atomic_set(&n_rcu_torture_alloc_fail, 0);
1143 atomic_set(&n_rcu_torture_free, 0);
1144 atomic_set(&n_rcu_torture_mberror, 0);
1145 atomic_set(&n_rcu_torture_error, 0);
1146 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1147 atomic_set(&rcu_torture_wcount[i], 0);
1148 for_each_possible_cpu(cpu) {
1149 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1150 per_cpu(rcu_torture_count, cpu)[i] = 0;
1151 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1155 /* Start up the kthreads. */
1157 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1158 writer_task = kthread_run(rcu_torture_writer, NULL,
1159 "rcu_torture_writer");
1160 if (IS_ERR(writer_task)) {
1161 firsterr = PTR_ERR(writer_task);
1162 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1163 writer_task = NULL;
1164 goto unwind;
1166 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1167 GFP_KERNEL);
1168 if (fakewriter_tasks == NULL) {
1169 VERBOSE_PRINTK_ERRSTRING("out of memory");
1170 firsterr = -ENOMEM;
1171 goto unwind;
1173 for (i = 0; i < nfakewriters; i++) {
1174 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1175 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1176 "rcu_torture_fakewriter");
1177 if (IS_ERR(fakewriter_tasks[i])) {
1178 firsterr = PTR_ERR(fakewriter_tasks[i]);
1179 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1180 fakewriter_tasks[i] = NULL;
1181 goto unwind;
1184 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
1185 GFP_KERNEL);
1186 if (reader_tasks == NULL) {
1187 VERBOSE_PRINTK_ERRSTRING("out of memory");
1188 firsterr = -ENOMEM;
1189 goto unwind;
1191 for (i = 0; i < nrealreaders; i++) {
1192 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1193 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1194 "rcu_torture_reader");
1195 if (IS_ERR(reader_tasks[i])) {
1196 firsterr = PTR_ERR(reader_tasks[i]);
1197 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1198 reader_tasks[i] = NULL;
1199 goto unwind;
1202 if (stat_interval > 0) {
1203 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1204 stats_task = kthread_run(rcu_torture_stats, NULL,
1205 "rcu_torture_stats");
1206 if (IS_ERR(stats_task)) {
1207 firsterr = PTR_ERR(stats_task);
1208 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1209 stats_task = NULL;
1210 goto unwind;
1213 if (test_no_idle_hz) {
1214 rcu_idle_cpu = num_online_cpus() - 1;
1216 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
1217 firsterr = -ENOMEM;
1218 VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
1219 goto unwind;
1222 /* Create the shuffler thread */
1223 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1224 "rcu_torture_shuffle");
1225 if (IS_ERR(shuffler_task)) {
1226 free_cpumask_var(shuffle_tmp_mask);
1227 firsterr = PTR_ERR(shuffler_task);
1228 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1229 shuffler_task = NULL;
1230 goto unwind;
1233 if (stutter < 0)
1234 stutter = 0;
1235 if (stutter) {
1236 /* Create the stutter thread */
1237 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1238 "rcu_torture_stutter");
1239 if (IS_ERR(stutter_task)) {
1240 firsterr = PTR_ERR(stutter_task);
1241 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1242 stutter_task = NULL;
1243 goto unwind;
1246 register_reboot_notifier(&rcutorture_nb);
1247 mutex_unlock(&fullstop_mutex);
1248 return 0;
1250 unwind:
1251 mutex_unlock(&fullstop_mutex);
1252 rcu_torture_cleanup();
1253 return firsterr;
1256 module_init(rcu_torture_init);
1257 module_exit(rcu_torture_cleanup);