Merge branch 'proc-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/adobriyan...
[linux-2.6/mini2440.git] / kernel / rcutorture.c
blob1cff28db56b6a87cacc225a740e602c4603d2e50
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 = NULL;
114 static long rcu_torture_current_version = 0;
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 = 0;
128 static struct list_head rcu_torture_removed;
130 static int stutter_pause_test = 0;
132 #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
133 #define RCUTORTURE_RUNNABLE_INIT 1
134 #else
135 #define RCUTORTURE_RUNNABLE_INIT 0
136 #endif
137 int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
139 #define FULLSTOP_SHUTDOWN 1 /* Bail due to system shutdown/panic. */
140 #define FULLSTOP_CLEANUP 2 /* Orderly shutdown. */
141 static int fullstop; /* stop generating callbacks at test end. */
142 DEFINE_MUTEX(fullstop_mutex); /* protect fullstop transitions and */
143 /* spawning of kthreads. */
146 * Detect and respond to a signal-based shutdown.
148 static int
149 rcutorture_shutdown_notify(struct notifier_block *unused1,
150 unsigned long unused2, void *unused3)
152 if (fullstop)
153 return NOTIFY_DONE;
154 mutex_lock(&fullstop_mutex);
155 if (!fullstop)
156 fullstop = FULLSTOP_SHUTDOWN;
157 mutex_unlock(&fullstop_mutex);
158 return NOTIFY_DONE;
162 * Allocate an element from the rcu_tortures pool.
164 static struct rcu_torture *
165 rcu_torture_alloc(void)
167 struct list_head *p;
169 spin_lock_bh(&rcu_torture_lock);
170 if (list_empty(&rcu_torture_freelist)) {
171 atomic_inc(&n_rcu_torture_alloc_fail);
172 spin_unlock_bh(&rcu_torture_lock);
173 return NULL;
175 atomic_inc(&n_rcu_torture_alloc);
176 p = rcu_torture_freelist.next;
177 list_del_init(p);
178 spin_unlock_bh(&rcu_torture_lock);
179 return container_of(p, struct rcu_torture, rtort_free);
183 * Free an element to the rcu_tortures pool.
185 static void
186 rcu_torture_free(struct rcu_torture *p)
188 atomic_inc(&n_rcu_torture_free);
189 spin_lock_bh(&rcu_torture_lock);
190 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
191 spin_unlock_bh(&rcu_torture_lock);
194 struct rcu_random_state {
195 unsigned long rrs_state;
196 long rrs_count;
199 #define RCU_RANDOM_MULT 39916801 /* prime */
200 #define RCU_RANDOM_ADD 479001701 /* prime */
201 #define RCU_RANDOM_REFRESH 10000
203 #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
206 * Crude but fast random-number generator. Uses a linear congruential
207 * generator, with occasional help from cpu_clock().
209 static unsigned long
210 rcu_random(struct rcu_random_state *rrsp)
212 if (--rrsp->rrs_count < 0) {
213 rrsp->rrs_state +=
214 (unsigned long)cpu_clock(raw_smp_processor_id());
215 rrsp->rrs_count = RCU_RANDOM_REFRESH;
217 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
218 return swahw32(rrsp->rrs_state);
221 static void
222 rcu_stutter_wait(void)
224 while ((stutter_pause_test || !rcutorture_runnable) && !fullstop) {
225 if (rcutorture_runnable)
226 schedule_timeout_interruptible(1);
227 else
228 schedule_timeout_interruptible(round_jiffies_relative(HZ));
233 * Operations vector for selecting different types of tests.
236 struct rcu_torture_ops {
237 void (*init)(void);
238 void (*cleanup)(void);
239 int (*readlock)(void);
240 void (*readdelay)(struct rcu_random_state *rrsp);
241 void (*readunlock)(int idx);
242 int (*completed)(void);
243 void (*deferredfree)(struct rcu_torture *p);
244 void (*sync)(void);
245 void (*cb_barrier)(void);
246 int (*stats)(char *page);
247 int irqcapable;
248 char *name;
250 static struct rcu_torture_ops *cur_ops = NULL;
253 * Definitions for rcu torture testing.
256 static int rcu_torture_read_lock(void) __acquires(RCU)
258 rcu_read_lock();
259 return 0;
262 static void rcu_read_delay(struct rcu_random_state *rrsp)
264 long delay;
265 const long longdelay = 200;
267 /* We want there to be long-running readers, but not all the time. */
269 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay);
270 if (!delay)
271 udelay(longdelay);
274 static void rcu_torture_read_unlock(int idx) __releases(RCU)
276 rcu_read_unlock();
279 static int rcu_torture_completed(void)
281 return rcu_batches_completed();
284 static void
285 rcu_torture_cb(struct rcu_head *p)
287 int i;
288 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
290 if (fullstop) {
291 /* Test is ending, just drop callbacks on the floor. */
292 /* The next initialization will pick up the pieces. */
293 return;
295 i = rp->rtort_pipe_count;
296 if (i > RCU_TORTURE_PIPE_LEN)
297 i = RCU_TORTURE_PIPE_LEN;
298 atomic_inc(&rcu_torture_wcount[i]);
299 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
300 rp->rtort_mbtest = 0;
301 rcu_torture_free(rp);
302 } else
303 cur_ops->deferredfree(rp);
306 static void rcu_torture_deferred_free(struct rcu_torture *p)
308 call_rcu(&p->rtort_rcu, rcu_torture_cb);
311 static struct rcu_torture_ops rcu_ops = {
312 .init = NULL,
313 .cleanup = NULL,
314 .readlock = rcu_torture_read_lock,
315 .readdelay = rcu_read_delay,
316 .readunlock = rcu_torture_read_unlock,
317 .completed = rcu_torture_completed,
318 .deferredfree = rcu_torture_deferred_free,
319 .sync = synchronize_rcu,
320 .cb_barrier = rcu_barrier,
321 .stats = NULL,
322 .irqcapable = 1,
323 .name = "rcu"
326 static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
328 int i;
329 struct rcu_torture *rp;
330 struct rcu_torture *rp1;
332 cur_ops->sync();
333 list_add(&p->rtort_free, &rcu_torture_removed);
334 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
335 i = rp->rtort_pipe_count;
336 if (i > RCU_TORTURE_PIPE_LEN)
337 i = RCU_TORTURE_PIPE_LEN;
338 atomic_inc(&rcu_torture_wcount[i]);
339 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
340 rp->rtort_mbtest = 0;
341 list_del(&rp->rtort_free);
342 rcu_torture_free(rp);
347 static void rcu_sync_torture_init(void)
349 INIT_LIST_HEAD(&rcu_torture_removed);
352 static struct rcu_torture_ops rcu_sync_ops = {
353 .init = rcu_sync_torture_init,
354 .cleanup = NULL,
355 .readlock = rcu_torture_read_lock,
356 .readdelay = rcu_read_delay,
357 .readunlock = rcu_torture_read_unlock,
358 .completed = rcu_torture_completed,
359 .deferredfree = rcu_sync_torture_deferred_free,
360 .sync = synchronize_rcu,
361 .cb_barrier = NULL,
362 .stats = NULL,
363 .irqcapable = 1,
364 .name = "rcu_sync"
368 * Definitions for rcu_bh torture testing.
371 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
373 rcu_read_lock_bh();
374 return 0;
377 static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
379 rcu_read_unlock_bh();
382 static int rcu_bh_torture_completed(void)
384 return rcu_batches_completed_bh();
387 static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
389 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
392 struct rcu_bh_torture_synchronize {
393 struct rcu_head head;
394 struct completion completion;
397 static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
399 struct rcu_bh_torture_synchronize *rcu;
401 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
402 complete(&rcu->completion);
405 static void rcu_bh_torture_synchronize(void)
407 struct rcu_bh_torture_synchronize rcu;
409 init_completion(&rcu.completion);
410 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
411 wait_for_completion(&rcu.completion);
414 static struct rcu_torture_ops rcu_bh_ops = {
415 .init = NULL,
416 .cleanup = NULL,
417 .readlock = rcu_bh_torture_read_lock,
418 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
419 .readunlock = rcu_bh_torture_read_unlock,
420 .completed = rcu_bh_torture_completed,
421 .deferredfree = rcu_bh_torture_deferred_free,
422 .sync = rcu_bh_torture_synchronize,
423 .cb_barrier = rcu_barrier_bh,
424 .stats = NULL,
425 .irqcapable = 1,
426 .name = "rcu_bh"
429 static struct rcu_torture_ops rcu_bh_sync_ops = {
430 .init = rcu_sync_torture_init,
431 .cleanup = NULL,
432 .readlock = rcu_bh_torture_read_lock,
433 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
434 .readunlock = rcu_bh_torture_read_unlock,
435 .completed = rcu_bh_torture_completed,
436 .deferredfree = rcu_sync_torture_deferred_free,
437 .sync = rcu_bh_torture_synchronize,
438 .cb_barrier = NULL,
439 .stats = NULL,
440 .irqcapable = 1,
441 .name = "rcu_bh_sync"
445 * Definitions for srcu torture testing.
448 static struct srcu_struct srcu_ctl;
450 static void srcu_torture_init(void)
452 init_srcu_struct(&srcu_ctl);
453 rcu_sync_torture_init();
456 static void srcu_torture_cleanup(void)
458 synchronize_srcu(&srcu_ctl);
459 cleanup_srcu_struct(&srcu_ctl);
462 static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
464 return srcu_read_lock(&srcu_ctl);
467 static void srcu_read_delay(struct rcu_random_state *rrsp)
469 long delay;
470 const long uspertick = 1000000 / HZ;
471 const long longdelay = 10;
473 /* We want there to be long-running readers, but not all the time. */
475 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
476 if (!delay)
477 schedule_timeout_interruptible(longdelay);
480 static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
482 srcu_read_unlock(&srcu_ctl, idx);
485 static int srcu_torture_completed(void)
487 return srcu_batches_completed(&srcu_ctl);
490 static void srcu_torture_synchronize(void)
492 synchronize_srcu(&srcu_ctl);
495 static int srcu_torture_stats(char *page)
497 int cnt = 0;
498 int cpu;
499 int idx = srcu_ctl.completed & 0x1;
501 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
502 torture_type, TORTURE_FLAG, idx);
503 for_each_possible_cpu(cpu) {
504 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
505 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
506 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
508 cnt += sprintf(&page[cnt], "\n");
509 return cnt;
512 static struct rcu_torture_ops srcu_ops = {
513 .init = srcu_torture_init,
514 .cleanup = srcu_torture_cleanup,
515 .readlock = srcu_torture_read_lock,
516 .readdelay = srcu_read_delay,
517 .readunlock = srcu_torture_read_unlock,
518 .completed = srcu_torture_completed,
519 .deferredfree = rcu_sync_torture_deferred_free,
520 .sync = srcu_torture_synchronize,
521 .cb_barrier = NULL,
522 .stats = srcu_torture_stats,
523 .name = "srcu"
527 * Definitions for sched torture testing.
530 static int sched_torture_read_lock(void)
532 preempt_disable();
533 return 0;
536 static void sched_torture_read_unlock(int idx)
538 preempt_enable();
541 static int sched_torture_completed(void)
543 return 0;
546 static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
548 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
551 static void sched_torture_synchronize(void)
553 synchronize_sched();
556 static struct rcu_torture_ops sched_ops = {
557 .init = rcu_sync_torture_init,
558 .cleanup = NULL,
559 .readlock = sched_torture_read_lock,
560 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
561 .readunlock = sched_torture_read_unlock,
562 .completed = sched_torture_completed,
563 .deferredfree = rcu_sched_torture_deferred_free,
564 .sync = sched_torture_synchronize,
565 .cb_barrier = rcu_barrier_sched,
566 .stats = NULL,
567 .irqcapable = 1,
568 .name = "sched"
571 static struct rcu_torture_ops sched_ops_sync = {
572 .init = rcu_sync_torture_init,
573 .cleanup = NULL,
574 .readlock = sched_torture_read_lock,
575 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
576 .readunlock = sched_torture_read_unlock,
577 .completed = sched_torture_completed,
578 .deferredfree = rcu_sync_torture_deferred_free,
579 .sync = sched_torture_synchronize,
580 .cb_barrier = NULL,
581 .stats = NULL,
582 .name = "sched_sync"
586 * RCU torture writer kthread. Repeatedly substitutes a new structure
587 * for that pointed to by rcu_torture_current, freeing the old structure
588 * after a series of grace periods (the "pipeline").
590 static int
591 rcu_torture_writer(void *arg)
593 int i;
594 long oldbatch = rcu_batches_completed();
595 struct rcu_torture *rp;
596 struct rcu_torture *old_rp;
597 static DEFINE_RCU_RANDOM(rand);
599 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
600 set_user_nice(current, 19);
602 do {
603 schedule_timeout_uninterruptible(1);
604 if ((rp = rcu_torture_alloc()) == NULL)
605 continue;
606 rp->rtort_pipe_count = 0;
607 udelay(rcu_random(&rand) & 0x3ff);
608 old_rp = rcu_torture_current;
609 rp->rtort_mbtest = 1;
610 rcu_assign_pointer(rcu_torture_current, rp);
611 smp_wmb();
612 if (old_rp) {
613 i = old_rp->rtort_pipe_count;
614 if (i > RCU_TORTURE_PIPE_LEN)
615 i = RCU_TORTURE_PIPE_LEN;
616 atomic_inc(&rcu_torture_wcount[i]);
617 old_rp->rtort_pipe_count++;
618 cur_ops->deferredfree(old_rp);
620 rcu_torture_current_version++;
621 oldbatch = cur_ops->completed();
622 rcu_stutter_wait();
623 } while (!kthread_should_stop() && !fullstop);
624 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
625 while (!kthread_should_stop() && fullstop != FULLSTOP_SHUTDOWN)
626 schedule_timeout_uninterruptible(1);
627 return 0;
631 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
632 * delay between calls.
634 static int
635 rcu_torture_fakewriter(void *arg)
637 DEFINE_RCU_RANDOM(rand);
639 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
640 set_user_nice(current, 19);
642 do {
643 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
644 udelay(rcu_random(&rand) & 0x3ff);
645 cur_ops->sync();
646 rcu_stutter_wait();
647 } while (!kthread_should_stop() && !fullstop);
649 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
650 while (!kthread_should_stop() && fullstop != FULLSTOP_SHUTDOWN)
651 schedule_timeout_uninterruptible(1);
652 return 0;
656 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
657 * incrementing the corresponding element of the pipeline array. The
658 * counter in the element should never be greater than 1, otherwise, the
659 * RCU implementation is broken.
661 static void rcu_torture_timer(unsigned long unused)
663 int idx;
664 int completed;
665 static DEFINE_RCU_RANDOM(rand);
666 static DEFINE_SPINLOCK(rand_lock);
667 struct rcu_torture *p;
668 int pipe_count;
670 idx = cur_ops->readlock();
671 completed = cur_ops->completed();
672 p = rcu_dereference(rcu_torture_current);
673 if (p == NULL) {
674 /* Leave because rcu_torture_writer is not yet underway */
675 cur_ops->readunlock(idx);
676 return;
678 if (p->rtort_mbtest == 0)
679 atomic_inc(&n_rcu_torture_mberror);
680 spin_lock(&rand_lock);
681 cur_ops->readdelay(&rand);
682 n_rcu_torture_timers++;
683 spin_unlock(&rand_lock);
684 preempt_disable();
685 pipe_count = p->rtort_pipe_count;
686 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
687 /* Should not happen, but... */
688 pipe_count = RCU_TORTURE_PIPE_LEN;
690 ++__get_cpu_var(rcu_torture_count)[pipe_count];
691 completed = cur_ops->completed() - completed;
692 if (completed > RCU_TORTURE_PIPE_LEN) {
693 /* Should not happen, but... */
694 completed = RCU_TORTURE_PIPE_LEN;
696 ++__get_cpu_var(rcu_torture_batch)[completed];
697 preempt_enable();
698 cur_ops->readunlock(idx);
702 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
703 * incrementing the corresponding element of the pipeline array. The
704 * counter in the element should never be greater than 1, otherwise, the
705 * RCU implementation is broken.
707 static int
708 rcu_torture_reader(void *arg)
710 int completed;
711 int idx;
712 DEFINE_RCU_RANDOM(rand);
713 struct rcu_torture *p;
714 int pipe_count;
715 struct timer_list t;
717 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
718 set_user_nice(current, 19);
719 if (irqreader && cur_ops->irqcapable)
720 setup_timer_on_stack(&t, rcu_torture_timer, 0);
722 do {
723 if (irqreader && cur_ops->irqcapable) {
724 if (!timer_pending(&t))
725 mod_timer(&t, 1);
727 idx = cur_ops->readlock();
728 completed = cur_ops->completed();
729 p = rcu_dereference(rcu_torture_current);
730 if (p == NULL) {
731 /* Wait for rcu_torture_writer to get underway */
732 cur_ops->readunlock(idx);
733 schedule_timeout_interruptible(HZ);
734 continue;
736 if (p->rtort_mbtest == 0)
737 atomic_inc(&n_rcu_torture_mberror);
738 cur_ops->readdelay(&rand);
739 preempt_disable();
740 pipe_count = p->rtort_pipe_count;
741 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
742 /* Should not happen, but... */
743 pipe_count = RCU_TORTURE_PIPE_LEN;
745 ++__get_cpu_var(rcu_torture_count)[pipe_count];
746 completed = cur_ops->completed() - completed;
747 if (completed > RCU_TORTURE_PIPE_LEN) {
748 /* Should not happen, but... */
749 completed = RCU_TORTURE_PIPE_LEN;
751 ++__get_cpu_var(rcu_torture_batch)[completed];
752 preempt_enable();
753 cur_ops->readunlock(idx);
754 schedule();
755 rcu_stutter_wait();
756 } while (!kthread_should_stop() && !fullstop);
757 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
758 if (irqreader && cur_ops->irqcapable)
759 del_timer_sync(&t);
760 while (!kthread_should_stop() && fullstop != FULLSTOP_SHUTDOWN)
761 schedule_timeout_uninterruptible(1);
762 return 0;
766 * Create an RCU-torture statistics message in the specified buffer.
768 static int
769 rcu_torture_printk(char *page)
771 int cnt = 0;
772 int cpu;
773 int i;
774 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
775 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
777 for_each_possible_cpu(cpu) {
778 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
779 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
780 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
783 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
784 if (pipesummary[i] != 0)
785 break;
787 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
788 cnt += sprintf(&page[cnt],
789 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
790 "rtmbe: %d nt: %ld",
791 rcu_torture_current,
792 rcu_torture_current_version,
793 list_empty(&rcu_torture_freelist),
794 atomic_read(&n_rcu_torture_alloc),
795 atomic_read(&n_rcu_torture_alloc_fail),
796 atomic_read(&n_rcu_torture_free),
797 atomic_read(&n_rcu_torture_mberror),
798 n_rcu_torture_timers);
799 if (atomic_read(&n_rcu_torture_mberror) != 0)
800 cnt += sprintf(&page[cnt], " !!!");
801 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
802 if (i > 1) {
803 cnt += sprintf(&page[cnt], "!!! ");
804 atomic_inc(&n_rcu_torture_error);
805 WARN_ON_ONCE(1);
807 cnt += sprintf(&page[cnt], "Reader Pipe: ");
808 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
809 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
810 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
811 cnt += sprintf(&page[cnt], "Reader Batch: ");
812 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
813 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
814 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
815 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
816 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
817 cnt += sprintf(&page[cnt], " %d",
818 atomic_read(&rcu_torture_wcount[i]));
820 cnt += sprintf(&page[cnt], "\n");
821 if (cur_ops->stats)
822 cnt += cur_ops->stats(&page[cnt]);
823 return cnt;
827 * Print torture statistics. Caller must ensure that there is only
828 * one call to this function at a given time!!! This is normally
829 * accomplished by relying on the module system to only have one copy
830 * of the module loaded, and then by giving the rcu_torture_stats
831 * kthread full control (or the init/cleanup functions when rcu_torture_stats
832 * thread is not running).
834 static void
835 rcu_torture_stats_print(void)
837 int cnt;
839 cnt = rcu_torture_printk(printk_buf);
840 printk(KERN_ALERT "%s", printk_buf);
844 * Periodically prints torture statistics, if periodic statistics printing
845 * was specified via the stat_interval module parameter.
847 * No need to worry about fullstop here, since this one doesn't reference
848 * volatile state or register callbacks.
850 static int
851 rcu_torture_stats(void *arg)
853 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
854 do {
855 schedule_timeout_interruptible(stat_interval * HZ);
856 rcu_torture_stats_print();
857 } while (!kthread_should_stop() && !fullstop);
858 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
859 return 0;
862 static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
864 /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
865 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
867 static void rcu_torture_shuffle_tasks(void)
869 cpumask_var_t tmp_mask;
870 int i;
872 if (!alloc_cpumask_var(&tmp_mask, GFP_KERNEL))
873 BUG();
875 cpumask_setall(tmp_mask);
876 get_online_cpus();
878 /* No point in shuffling if there is only one online CPU (ex: UP) */
879 if (num_online_cpus() == 1)
880 goto out;
882 if (rcu_idle_cpu != -1)
883 cpumask_clear_cpu(rcu_idle_cpu, tmp_mask);
885 set_cpus_allowed_ptr(current, tmp_mask);
887 if (reader_tasks) {
888 for (i = 0; i < nrealreaders; i++)
889 if (reader_tasks[i])
890 set_cpus_allowed_ptr(reader_tasks[i],
891 tmp_mask);
894 if (fakewriter_tasks) {
895 for (i = 0; i < nfakewriters; i++)
896 if (fakewriter_tasks[i])
897 set_cpus_allowed_ptr(fakewriter_tasks[i],
898 tmp_mask);
901 if (writer_task)
902 set_cpus_allowed_ptr(writer_task, tmp_mask);
904 if (stats_task)
905 set_cpus_allowed_ptr(stats_task, tmp_mask);
907 if (rcu_idle_cpu == -1)
908 rcu_idle_cpu = num_online_cpus() - 1;
909 else
910 rcu_idle_cpu--;
912 out:
913 put_online_cpus();
914 free_cpumask_var(tmp_mask);
917 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
918 * system to become idle at a time and cut off its timer ticks. This is meant
919 * to test the support for such tickless idle CPU in RCU.
921 static int
922 rcu_torture_shuffle(void *arg)
924 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
925 do {
926 schedule_timeout_interruptible(shuffle_interval * HZ);
927 rcu_torture_shuffle_tasks();
928 } while (!kthread_should_stop() && !fullstop);
929 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
930 return 0;
933 /* Cause the rcutorture test to "stutter", starting and stopping all
934 * threads periodically.
936 static int
937 rcu_torture_stutter(void *arg)
939 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
940 do {
941 schedule_timeout_interruptible(stutter * HZ);
942 stutter_pause_test = 1;
943 if (!kthread_should_stop() && !fullstop)
944 schedule_timeout_interruptible(stutter * HZ);
945 stutter_pause_test = 0;
946 } while (!kthread_should_stop() && !fullstop);
947 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
948 return 0;
951 static inline void
952 rcu_torture_print_module_parms(char *tag)
954 printk(KERN_ALERT "%s" TORTURE_FLAG
955 "--- %s: nreaders=%d nfakewriters=%d "
956 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
957 "shuffle_interval=%d stutter=%d irqreader=%d\n",
958 torture_type, tag, nrealreaders, nfakewriters,
959 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
960 stutter, irqreader);
963 static struct notifier_block rcutorture_nb = {
964 .notifier_call = rcutorture_shutdown_notify,
967 static void
968 rcu_torture_cleanup(void)
970 int i;
972 mutex_lock(&fullstop_mutex);
973 if (!fullstop) {
974 /* If being signaled, let it happen, then exit. */
975 mutex_unlock(&fullstop_mutex);
976 schedule_timeout_interruptible(10 * HZ);
977 if (cur_ops->cb_barrier != NULL)
978 cur_ops->cb_barrier();
979 return;
981 fullstop = FULLSTOP_CLEANUP;
982 mutex_unlock(&fullstop_mutex);
983 unregister_reboot_notifier(&rcutorture_nb);
984 if (stutter_task) {
985 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
986 kthread_stop(stutter_task);
988 stutter_task = NULL;
989 if (shuffler_task) {
990 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
991 kthread_stop(shuffler_task);
993 shuffler_task = NULL;
995 if (writer_task) {
996 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
997 kthread_stop(writer_task);
999 writer_task = NULL;
1001 if (reader_tasks) {
1002 for (i = 0; i < nrealreaders; i++) {
1003 if (reader_tasks[i]) {
1004 VERBOSE_PRINTK_STRING(
1005 "Stopping rcu_torture_reader task");
1006 kthread_stop(reader_tasks[i]);
1008 reader_tasks[i] = NULL;
1010 kfree(reader_tasks);
1011 reader_tasks = NULL;
1013 rcu_torture_current = NULL;
1015 if (fakewriter_tasks) {
1016 for (i = 0; i < nfakewriters; i++) {
1017 if (fakewriter_tasks[i]) {
1018 VERBOSE_PRINTK_STRING(
1019 "Stopping rcu_torture_fakewriter task");
1020 kthread_stop(fakewriter_tasks[i]);
1022 fakewriter_tasks[i] = NULL;
1024 kfree(fakewriter_tasks);
1025 fakewriter_tasks = NULL;
1028 if (stats_task) {
1029 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1030 kthread_stop(stats_task);
1032 stats_task = NULL;
1034 /* Wait for all RCU callbacks to fire. */
1036 if (cur_ops->cb_barrier != NULL)
1037 cur_ops->cb_barrier();
1039 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
1041 if (cur_ops->cleanup)
1042 cur_ops->cleanup();
1043 if (atomic_read(&n_rcu_torture_error))
1044 rcu_torture_print_module_parms("End of test: FAILURE");
1045 else
1046 rcu_torture_print_module_parms("End of test: SUCCESS");
1049 static int __init
1050 rcu_torture_init(void)
1052 int i;
1053 int cpu;
1054 int firsterr = 0;
1055 static struct rcu_torture_ops *torture_ops[] =
1056 { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops,
1057 &srcu_ops, &sched_ops, &sched_ops_sync, };
1059 mutex_lock(&fullstop_mutex);
1061 /* Process args and tell the world that the torturer is on the job. */
1062 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1063 cur_ops = torture_ops[i];
1064 if (strcmp(torture_type, cur_ops->name) == 0)
1065 break;
1067 if (i == ARRAY_SIZE(torture_ops)) {
1068 printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
1069 torture_type);
1070 mutex_unlock(&fullstop_mutex);
1071 return (-EINVAL);
1073 if (cur_ops->init)
1074 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1076 if (nreaders >= 0)
1077 nrealreaders = nreaders;
1078 else
1079 nrealreaders = 2 * num_online_cpus();
1080 rcu_torture_print_module_parms("Start of test");
1081 fullstop = 0;
1083 /* Set up the freelist. */
1085 INIT_LIST_HEAD(&rcu_torture_freelist);
1086 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1087 rcu_tortures[i].rtort_mbtest = 0;
1088 list_add_tail(&rcu_tortures[i].rtort_free,
1089 &rcu_torture_freelist);
1092 /* Initialize the statistics so that each run gets its own numbers. */
1094 rcu_torture_current = NULL;
1095 rcu_torture_current_version = 0;
1096 atomic_set(&n_rcu_torture_alloc, 0);
1097 atomic_set(&n_rcu_torture_alloc_fail, 0);
1098 atomic_set(&n_rcu_torture_free, 0);
1099 atomic_set(&n_rcu_torture_mberror, 0);
1100 atomic_set(&n_rcu_torture_error, 0);
1101 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1102 atomic_set(&rcu_torture_wcount[i], 0);
1103 for_each_possible_cpu(cpu) {
1104 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1105 per_cpu(rcu_torture_count, cpu)[i] = 0;
1106 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1110 /* Start up the kthreads. */
1112 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1113 writer_task = kthread_run(rcu_torture_writer, NULL,
1114 "rcu_torture_writer");
1115 if (IS_ERR(writer_task)) {
1116 firsterr = PTR_ERR(writer_task);
1117 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1118 writer_task = NULL;
1119 goto unwind;
1121 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1122 GFP_KERNEL);
1123 if (fakewriter_tasks == NULL) {
1124 VERBOSE_PRINTK_ERRSTRING("out of memory");
1125 firsterr = -ENOMEM;
1126 goto unwind;
1128 for (i = 0; i < nfakewriters; i++) {
1129 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1130 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1131 "rcu_torture_fakewriter");
1132 if (IS_ERR(fakewriter_tasks[i])) {
1133 firsterr = PTR_ERR(fakewriter_tasks[i]);
1134 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1135 fakewriter_tasks[i] = NULL;
1136 goto unwind;
1139 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
1140 GFP_KERNEL);
1141 if (reader_tasks == NULL) {
1142 VERBOSE_PRINTK_ERRSTRING("out of memory");
1143 firsterr = -ENOMEM;
1144 goto unwind;
1146 for (i = 0; i < nrealreaders; i++) {
1147 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1148 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1149 "rcu_torture_reader");
1150 if (IS_ERR(reader_tasks[i])) {
1151 firsterr = PTR_ERR(reader_tasks[i]);
1152 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1153 reader_tasks[i] = NULL;
1154 goto unwind;
1157 if (stat_interval > 0) {
1158 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1159 stats_task = kthread_run(rcu_torture_stats, NULL,
1160 "rcu_torture_stats");
1161 if (IS_ERR(stats_task)) {
1162 firsterr = PTR_ERR(stats_task);
1163 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1164 stats_task = NULL;
1165 goto unwind;
1168 if (test_no_idle_hz) {
1169 rcu_idle_cpu = num_online_cpus() - 1;
1170 /* Create the shuffler thread */
1171 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1172 "rcu_torture_shuffle");
1173 if (IS_ERR(shuffler_task)) {
1174 firsterr = PTR_ERR(shuffler_task);
1175 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1176 shuffler_task = NULL;
1177 goto unwind;
1180 if (stutter < 0)
1181 stutter = 0;
1182 if (stutter) {
1183 /* Create the stutter thread */
1184 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1185 "rcu_torture_stutter");
1186 if (IS_ERR(stutter_task)) {
1187 firsterr = PTR_ERR(stutter_task);
1188 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1189 stutter_task = NULL;
1190 goto unwind;
1193 register_reboot_notifier(&rcutorture_nb);
1194 mutex_unlock(&fullstop_mutex);
1195 return 0;
1197 unwind:
1198 mutex_unlock(&fullstop_mutex);
1199 rcu_torture_cleanup();
1200 return firsterr;
1203 module_init(rcu_torture_init);
1204 module_exit(rcu_torture_cleanup);