prism54: convert to internal net_device_stats
[linux-2.6/mini2440.git] / kernel / rcutorture.c
blob7c4142a79f0ab5a2ef80300b3d5be3188bdec8c7
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 /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
141 #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
142 #define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
143 #define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
144 static int fullstop = FULLSTOP_RMMOD;
145 DEFINE_MUTEX(fullstop_mutex); /* Protect fullstop transitions and spawning */
146 /* of kthreads. */
149 * Detect and respond to a system shutdown.
151 static int
152 rcutorture_shutdown_notify(struct notifier_block *unused1,
153 unsigned long unused2, void *unused3)
155 mutex_lock(&fullstop_mutex);
156 if (fullstop == FULLSTOP_DONTSTOP)
157 fullstop = FULLSTOP_SHUTDOWN;
158 else
159 printk(KERN_WARNING /* but going down anyway, so... */
160 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
161 mutex_unlock(&fullstop_mutex);
162 return NOTIFY_DONE;
166 * Absorb kthreads into a kernel function that won't return, so that
167 * they won't ever access module text or data again.
169 static void rcutorture_shutdown_absorb(char *title)
171 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
172 printk(KERN_NOTICE
173 "rcutorture thread %s parking due to system shutdown\n",
174 title);
175 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
180 * Allocate an element from the rcu_tortures pool.
182 static struct rcu_torture *
183 rcu_torture_alloc(void)
185 struct list_head *p;
187 spin_lock_bh(&rcu_torture_lock);
188 if (list_empty(&rcu_torture_freelist)) {
189 atomic_inc(&n_rcu_torture_alloc_fail);
190 spin_unlock_bh(&rcu_torture_lock);
191 return NULL;
193 atomic_inc(&n_rcu_torture_alloc);
194 p = rcu_torture_freelist.next;
195 list_del_init(p);
196 spin_unlock_bh(&rcu_torture_lock);
197 return container_of(p, struct rcu_torture, rtort_free);
201 * Free an element to the rcu_tortures pool.
203 static void
204 rcu_torture_free(struct rcu_torture *p)
206 atomic_inc(&n_rcu_torture_free);
207 spin_lock_bh(&rcu_torture_lock);
208 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
209 spin_unlock_bh(&rcu_torture_lock);
212 struct rcu_random_state {
213 unsigned long rrs_state;
214 long rrs_count;
217 #define RCU_RANDOM_MULT 39916801 /* prime */
218 #define RCU_RANDOM_ADD 479001701 /* prime */
219 #define RCU_RANDOM_REFRESH 10000
221 #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
224 * Crude but fast random-number generator. Uses a linear congruential
225 * generator, with occasional help from cpu_clock().
227 static unsigned long
228 rcu_random(struct rcu_random_state *rrsp)
230 if (--rrsp->rrs_count < 0) {
231 rrsp->rrs_state +=
232 (unsigned long)cpu_clock(raw_smp_processor_id());
233 rrsp->rrs_count = RCU_RANDOM_REFRESH;
235 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
236 return swahw32(rrsp->rrs_state);
239 static void
240 rcu_stutter_wait(char *title)
242 while (stutter_pause_test || !rcutorture_runnable) {
243 if (rcutorture_runnable)
244 schedule_timeout_interruptible(1);
245 else
246 schedule_timeout_interruptible(round_jiffies_relative(HZ));
247 rcutorture_shutdown_absorb(title);
252 * Operations vector for selecting different types of tests.
255 struct rcu_torture_ops {
256 void (*init)(void);
257 void (*cleanup)(void);
258 int (*readlock)(void);
259 void (*readdelay)(struct rcu_random_state *rrsp);
260 void (*readunlock)(int idx);
261 int (*completed)(void);
262 void (*deferredfree)(struct rcu_torture *p);
263 void (*sync)(void);
264 void (*cb_barrier)(void);
265 int (*stats)(char *page);
266 int irqcapable;
267 char *name;
269 static struct rcu_torture_ops *cur_ops = NULL;
272 * Definitions for rcu torture testing.
275 static int rcu_torture_read_lock(void) __acquires(RCU)
277 rcu_read_lock();
278 return 0;
281 static void rcu_read_delay(struct rcu_random_state *rrsp)
283 long delay;
284 const long longdelay = 200;
286 /* We want there to be long-running readers, but not all the time. */
288 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay);
289 if (!delay)
290 udelay(longdelay);
293 static void rcu_torture_read_unlock(int idx) __releases(RCU)
295 rcu_read_unlock();
298 static int rcu_torture_completed(void)
300 return rcu_batches_completed();
303 static void
304 rcu_torture_cb(struct rcu_head *p)
306 int i;
307 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
309 if (fullstop != FULLSTOP_DONTSTOP) {
310 /* Test is ending, just drop callbacks on the floor. */
311 /* The next initialization will pick up the pieces. */
312 return;
314 i = rp->rtort_pipe_count;
315 if (i > RCU_TORTURE_PIPE_LEN)
316 i = RCU_TORTURE_PIPE_LEN;
317 atomic_inc(&rcu_torture_wcount[i]);
318 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
319 rp->rtort_mbtest = 0;
320 rcu_torture_free(rp);
321 } else
322 cur_ops->deferredfree(rp);
325 static void rcu_torture_deferred_free(struct rcu_torture *p)
327 call_rcu(&p->rtort_rcu, rcu_torture_cb);
330 static struct rcu_torture_ops rcu_ops = {
331 .init = NULL,
332 .cleanup = NULL,
333 .readlock = rcu_torture_read_lock,
334 .readdelay = rcu_read_delay,
335 .readunlock = rcu_torture_read_unlock,
336 .completed = rcu_torture_completed,
337 .deferredfree = rcu_torture_deferred_free,
338 .sync = synchronize_rcu,
339 .cb_barrier = rcu_barrier,
340 .stats = NULL,
341 .irqcapable = 1,
342 .name = "rcu"
345 static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
347 int i;
348 struct rcu_torture *rp;
349 struct rcu_torture *rp1;
351 cur_ops->sync();
352 list_add(&p->rtort_free, &rcu_torture_removed);
353 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
354 i = rp->rtort_pipe_count;
355 if (i > RCU_TORTURE_PIPE_LEN)
356 i = RCU_TORTURE_PIPE_LEN;
357 atomic_inc(&rcu_torture_wcount[i]);
358 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
359 rp->rtort_mbtest = 0;
360 list_del(&rp->rtort_free);
361 rcu_torture_free(rp);
366 static void rcu_sync_torture_init(void)
368 INIT_LIST_HEAD(&rcu_torture_removed);
371 static struct rcu_torture_ops rcu_sync_ops = {
372 .init = rcu_sync_torture_init,
373 .cleanup = NULL,
374 .readlock = rcu_torture_read_lock,
375 .readdelay = rcu_read_delay,
376 .readunlock = rcu_torture_read_unlock,
377 .completed = rcu_torture_completed,
378 .deferredfree = rcu_sync_torture_deferred_free,
379 .sync = synchronize_rcu,
380 .cb_barrier = NULL,
381 .stats = NULL,
382 .irqcapable = 1,
383 .name = "rcu_sync"
387 * Definitions for rcu_bh torture testing.
390 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
392 rcu_read_lock_bh();
393 return 0;
396 static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
398 rcu_read_unlock_bh();
401 static int rcu_bh_torture_completed(void)
403 return rcu_batches_completed_bh();
406 static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
408 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
411 struct rcu_bh_torture_synchronize {
412 struct rcu_head head;
413 struct completion completion;
416 static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
418 struct rcu_bh_torture_synchronize *rcu;
420 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
421 complete(&rcu->completion);
424 static void rcu_bh_torture_synchronize(void)
426 struct rcu_bh_torture_synchronize rcu;
428 init_completion(&rcu.completion);
429 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
430 wait_for_completion(&rcu.completion);
433 static struct rcu_torture_ops rcu_bh_ops = {
434 .init = NULL,
435 .cleanup = NULL,
436 .readlock = rcu_bh_torture_read_lock,
437 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
438 .readunlock = rcu_bh_torture_read_unlock,
439 .completed = rcu_bh_torture_completed,
440 .deferredfree = rcu_bh_torture_deferred_free,
441 .sync = rcu_bh_torture_synchronize,
442 .cb_barrier = rcu_barrier_bh,
443 .stats = NULL,
444 .irqcapable = 1,
445 .name = "rcu_bh"
448 static struct rcu_torture_ops rcu_bh_sync_ops = {
449 .init = rcu_sync_torture_init,
450 .cleanup = NULL,
451 .readlock = rcu_bh_torture_read_lock,
452 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
453 .readunlock = rcu_bh_torture_read_unlock,
454 .completed = rcu_bh_torture_completed,
455 .deferredfree = rcu_sync_torture_deferred_free,
456 .sync = rcu_bh_torture_synchronize,
457 .cb_barrier = NULL,
458 .stats = NULL,
459 .irqcapable = 1,
460 .name = "rcu_bh_sync"
464 * Definitions for srcu torture testing.
467 static struct srcu_struct srcu_ctl;
469 static void srcu_torture_init(void)
471 init_srcu_struct(&srcu_ctl);
472 rcu_sync_torture_init();
475 static void srcu_torture_cleanup(void)
477 synchronize_srcu(&srcu_ctl);
478 cleanup_srcu_struct(&srcu_ctl);
481 static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
483 return srcu_read_lock(&srcu_ctl);
486 static void srcu_read_delay(struct rcu_random_state *rrsp)
488 long delay;
489 const long uspertick = 1000000 / HZ;
490 const long longdelay = 10;
492 /* We want there to be long-running readers, but not all the time. */
494 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
495 if (!delay)
496 schedule_timeout_interruptible(longdelay);
499 static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
501 srcu_read_unlock(&srcu_ctl, idx);
504 static int srcu_torture_completed(void)
506 return srcu_batches_completed(&srcu_ctl);
509 static void srcu_torture_synchronize(void)
511 synchronize_srcu(&srcu_ctl);
514 static int srcu_torture_stats(char *page)
516 int cnt = 0;
517 int cpu;
518 int idx = srcu_ctl.completed & 0x1;
520 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
521 torture_type, TORTURE_FLAG, idx);
522 for_each_possible_cpu(cpu) {
523 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
524 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
525 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
527 cnt += sprintf(&page[cnt], "\n");
528 return cnt;
531 static struct rcu_torture_ops srcu_ops = {
532 .init = srcu_torture_init,
533 .cleanup = srcu_torture_cleanup,
534 .readlock = srcu_torture_read_lock,
535 .readdelay = srcu_read_delay,
536 .readunlock = srcu_torture_read_unlock,
537 .completed = srcu_torture_completed,
538 .deferredfree = rcu_sync_torture_deferred_free,
539 .sync = srcu_torture_synchronize,
540 .cb_barrier = NULL,
541 .stats = srcu_torture_stats,
542 .name = "srcu"
546 * Definitions for sched torture testing.
549 static int sched_torture_read_lock(void)
551 preempt_disable();
552 return 0;
555 static void sched_torture_read_unlock(int idx)
557 preempt_enable();
560 static int sched_torture_completed(void)
562 return 0;
565 static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
567 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
570 static void sched_torture_synchronize(void)
572 synchronize_sched();
575 static struct rcu_torture_ops sched_ops = {
576 .init = rcu_sync_torture_init,
577 .cleanup = NULL,
578 .readlock = sched_torture_read_lock,
579 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
580 .readunlock = sched_torture_read_unlock,
581 .completed = sched_torture_completed,
582 .deferredfree = rcu_sched_torture_deferred_free,
583 .sync = sched_torture_synchronize,
584 .cb_barrier = rcu_barrier_sched,
585 .stats = NULL,
586 .irqcapable = 1,
587 .name = "sched"
590 static struct rcu_torture_ops sched_ops_sync = {
591 .init = rcu_sync_torture_init,
592 .cleanup = NULL,
593 .readlock = sched_torture_read_lock,
594 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
595 .readunlock = sched_torture_read_unlock,
596 .completed = sched_torture_completed,
597 .deferredfree = rcu_sync_torture_deferred_free,
598 .sync = sched_torture_synchronize,
599 .cb_barrier = NULL,
600 .stats = NULL,
601 .name = "sched_sync"
605 * RCU torture writer kthread. Repeatedly substitutes a new structure
606 * for that pointed to by rcu_torture_current, freeing the old structure
607 * after a series of grace periods (the "pipeline").
609 static int
610 rcu_torture_writer(void *arg)
612 int i;
613 long oldbatch = rcu_batches_completed();
614 struct rcu_torture *rp;
615 struct rcu_torture *old_rp;
616 static DEFINE_RCU_RANDOM(rand);
618 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
619 set_user_nice(current, 19);
621 do {
622 schedule_timeout_uninterruptible(1);
623 if ((rp = rcu_torture_alloc()) == NULL)
624 continue;
625 rp->rtort_pipe_count = 0;
626 udelay(rcu_random(&rand) & 0x3ff);
627 old_rp = rcu_torture_current;
628 rp->rtort_mbtest = 1;
629 rcu_assign_pointer(rcu_torture_current, rp);
630 smp_wmb();
631 if (old_rp) {
632 i = old_rp->rtort_pipe_count;
633 if (i > RCU_TORTURE_PIPE_LEN)
634 i = RCU_TORTURE_PIPE_LEN;
635 atomic_inc(&rcu_torture_wcount[i]);
636 old_rp->rtort_pipe_count++;
637 cur_ops->deferredfree(old_rp);
639 rcu_torture_current_version++;
640 oldbatch = cur_ops->completed();
641 rcu_stutter_wait("rcu_torture_writer");
642 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
643 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
644 rcutorture_shutdown_absorb("rcu_torture_writer");
645 while (!kthread_should_stop())
646 schedule_timeout_uninterruptible(1);
647 return 0;
651 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
652 * delay between calls.
654 static int
655 rcu_torture_fakewriter(void *arg)
657 DEFINE_RCU_RANDOM(rand);
659 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
660 set_user_nice(current, 19);
662 do {
663 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
664 udelay(rcu_random(&rand) & 0x3ff);
665 cur_ops->sync();
666 rcu_stutter_wait("rcu_torture_fakewriter");
667 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
669 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
670 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
671 while (!kthread_should_stop())
672 schedule_timeout_uninterruptible(1);
673 return 0;
677 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
678 * incrementing the corresponding element of the pipeline array. The
679 * counter in the element should never be greater than 1, otherwise, the
680 * RCU implementation is broken.
682 static void rcu_torture_timer(unsigned long unused)
684 int idx;
685 int completed;
686 static DEFINE_RCU_RANDOM(rand);
687 static DEFINE_SPINLOCK(rand_lock);
688 struct rcu_torture *p;
689 int pipe_count;
691 idx = cur_ops->readlock();
692 completed = cur_ops->completed();
693 p = rcu_dereference(rcu_torture_current);
694 if (p == NULL) {
695 /* Leave because rcu_torture_writer is not yet underway */
696 cur_ops->readunlock(idx);
697 return;
699 if (p->rtort_mbtest == 0)
700 atomic_inc(&n_rcu_torture_mberror);
701 spin_lock(&rand_lock);
702 cur_ops->readdelay(&rand);
703 n_rcu_torture_timers++;
704 spin_unlock(&rand_lock);
705 preempt_disable();
706 pipe_count = p->rtort_pipe_count;
707 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
708 /* Should not happen, but... */
709 pipe_count = RCU_TORTURE_PIPE_LEN;
711 ++__get_cpu_var(rcu_torture_count)[pipe_count];
712 completed = cur_ops->completed() - completed;
713 if (completed > RCU_TORTURE_PIPE_LEN) {
714 /* Should not happen, but... */
715 completed = RCU_TORTURE_PIPE_LEN;
717 ++__get_cpu_var(rcu_torture_batch)[completed];
718 preempt_enable();
719 cur_ops->readunlock(idx);
723 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
724 * incrementing the corresponding element of the pipeline array. The
725 * counter in the element should never be greater than 1, otherwise, the
726 * RCU implementation is broken.
728 static int
729 rcu_torture_reader(void *arg)
731 int completed;
732 int idx;
733 DEFINE_RCU_RANDOM(rand);
734 struct rcu_torture *p;
735 int pipe_count;
736 struct timer_list t;
738 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
739 set_user_nice(current, 19);
740 if (irqreader && cur_ops->irqcapable)
741 setup_timer_on_stack(&t, rcu_torture_timer, 0);
743 do {
744 if (irqreader && cur_ops->irqcapable) {
745 if (!timer_pending(&t))
746 mod_timer(&t, 1);
748 idx = cur_ops->readlock();
749 completed = cur_ops->completed();
750 p = rcu_dereference(rcu_torture_current);
751 if (p == NULL) {
752 /* Wait for rcu_torture_writer to get underway */
753 cur_ops->readunlock(idx);
754 schedule_timeout_interruptible(HZ);
755 continue;
757 if (p->rtort_mbtest == 0)
758 atomic_inc(&n_rcu_torture_mberror);
759 cur_ops->readdelay(&rand);
760 preempt_disable();
761 pipe_count = p->rtort_pipe_count;
762 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
763 /* Should not happen, but... */
764 pipe_count = RCU_TORTURE_PIPE_LEN;
766 ++__get_cpu_var(rcu_torture_count)[pipe_count];
767 completed = cur_ops->completed() - completed;
768 if (completed > RCU_TORTURE_PIPE_LEN) {
769 /* Should not happen, but... */
770 completed = RCU_TORTURE_PIPE_LEN;
772 ++__get_cpu_var(rcu_torture_batch)[completed];
773 preempt_enable();
774 cur_ops->readunlock(idx);
775 schedule();
776 rcu_stutter_wait("rcu_torture_reader");
777 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
778 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
779 rcutorture_shutdown_absorb("rcu_torture_reader");
780 if (irqreader && cur_ops->irqcapable)
781 del_timer_sync(&t);
782 while (!kthread_should_stop())
783 schedule_timeout_uninterruptible(1);
784 return 0;
788 * Create an RCU-torture statistics message in the specified buffer.
790 static int
791 rcu_torture_printk(char *page)
793 int cnt = 0;
794 int cpu;
795 int i;
796 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
797 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
799 for_each_possible_cpu(cpu) {
800 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
801 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
802 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
805 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
806 if (pipesummary[i] != 0)
807 break;
809 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
810 cnt += sprintf(&page[cnt],
811 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
812 "rtmbe: %d nt: %ld",
813 rcu_torture_current,
814 rcu_torture_current_version,
815 list_empty(&rcu_torture_freelist),
816 atomic_read(&n_rcu_torture_alloc),
817 atomic_read(&n_rcu_torture_alloc_fail),
818 atomic_read(&n_rcu_torture_free),
819 atomic_read(&n_rcu_torture_mberror),
820 n_rcu_torture_timers);
821 if (atomic_read(&n_rcu_torture_mberror) != 0)
822 cnt += sprintf(&page[cnt], " !!!");
823 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
824 if (i > 1) {
825 cnt += sprintf(&page[cnt], "!!! ");
826 atomic_inc(&n_rcu_torture_error);
827 WARN_ON_ONCE(1);
829 cnt += sprintf(&page[cnt], "Reader Pipe: ");
830 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
831 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
832 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
833 cnt += sprintf(&page[cnt], "Reader Batch: ");
834 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
835 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
836 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
837 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
838 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
839 cnt += sprintf(&page[cnt], " %d",
840 atomic_read(&rcu_torture_wcount[i]));
842 cnt += sprintf(&page[cnt], "\n");
843 if (cur_ops->stats)
844 cnt += cur_ops->stats(&page[cnt]);
845 return cnt;
849 * Print torture statistics. Caller must ensure that there is only
850 * one call to this function at a given time!!! This is normally
851 * accomplished by relying on the module system to only have one copy
852 * of the module loaded, and then by giving the rcu_torture_stats
853 * kthread full control (or the init/cleanup functions when rcu_torture_stats
854 * thread is not running).
856 static void
857 rcu_torture_stats_print(void)
859 int cnt;
861 cnt = rcu_torture_printk(printk_buf);
862 printk(KERN_ALERT "%s", printk_buf);
866 * Periodically prints torture statistics, if periodic statistics printing
867 * was specified via the stat_interval module parameter.
869 * No need to worry about fullstop here, since this one doesn't reference
870 * volatile state or register callbacks.
872 static int
873 rcu_torture_stats(void *arg)
875 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
876 do {
877 schedule_timeout_interruptible(stat_interval * HZ);
878 rcu_torture_stats_print();
879 rcutorture_shutdown_absorb("rcu_torture_stats");
880 } while (!kthread_should_stop());
881 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
882 return 0;
885 static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
887 /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
888 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
890 static void rcu_torture_shuffle_tasks(void)
892 cpumask_t tmp_mask;
893 int i;
895 cpus_setall(tmp_mask);
896 get_online_cpus();
898 /* No point in shuffling if there is only one online CPU (ex: UP) */
899 if (num_online_cpus() == 1) {
900 put_online_cpus();
901 return;
904 if (rcu_idle_cpu != -1)
905 cpu_clear(rcu_idle_cpu, tmp_mask);
907 set_cpus_allowed_ptr(current, &tmp_mask);
909 if (reader_tasks) {
910 for (i = 0; i < nrealreaders; i++)
911 if (reader_tasks[i])
912 set_cpus_allowed_ptr(reader_tasks[i],
913 &tmp_mask);
916 if (fakewriter_tasks) {
917 for (i = 0; i < nfakewriters; i++)
918 if (fakewriter_tasks[i])
919 set_cpus_allowed_ptr(fakewriter_tasks[i],
920 &tmp_mask);
923 if (writer_task)
924 set_cpus_allowed_ptr(writer_task, &tmp_mask);
926 if (stats_task)
927 set_cpus_allowed_ptr(stats_task, &tmp_mask);
929 if (rcu_idle_cpu == -1)
930 rcu_idle_cpu = num_online_cpus() - 1;
931 else
932 rcu_idle_cpu--;
934 put_online_cpus();
937 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
938 * system to become idle at a time and cut off its timer ticks. This is meant
939 * to test the support for such tickless idle CPU in RCU.
941 static int
942 rcu_torture_shuffle(void *arg)
944 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
945 do {
946 schedule_timeout_interruptible(shuffle_interval * HZ);
947 rcu_torture_shuffle_tasks();
948 rcutorture_shutdown_absorb("rcu_torture_shuffle");
949 } while (!kthread_should_stop());
950 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
951 return 0;
954 /* Cause the rcutorture test to "stutter", starting and stopping all
955 * threads periodically.
957 static int
958 rcu_torture_stutter(void *arg)
960 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
961 do {
962 schedule_timeout_interruptible(stutter * HZ);
963 stutter_pause_test = 1;
964 if (!kthread_should_stop())
965 schedule_timeout_interruptible(stutter * HZ);
966 stutter_pause_test = 0;
967 rcutorture_shutdown_absorb("rcu_torture_stutter");
968 } while (!kthread_should_stop());
969 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
970 return 0;
973 static inline void
974 rcu_torture_print_module_parms(char *tag)
976 printk(KERN_ALERT "%s" TORTURE_FLAG
977 "--- %s: nreaders=%d nfakewriters=%d "
978 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
979 "shuffle_interval=%d stutter=%d irqreader=%d\n",
980 torture_type, tag, nrealreaders, nfakewriters,
981 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
982 stutter, irqreader);
985 static struct notifier_block rcutorture_nb = {
986 .notifier_call = rcutorture_shutdown_notify,
989 static void
990 rcu_torture_cleanup(void)
992 int i;
994 mutex_lock(&fullstop_mutex);
995 if (fullstop == FULLSTOP_SHUTDOWN) {
996 printk(KERN_WARNING /* but going down anyway, so... */
997 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
998 mutex_unlock(&fullstop_mutex);
999 schedule_timeout_uninterruptible(10);
1000 if (cur_ops->cb_barrier != NULL)
1001 cur_ops->cb_barrier();
1002 return;
1004 fullstop = FULLSTOP_RMMOD;
1005 mutex_unlock(&fullstop_mutex);
1006 unregister_reboot_notifier(&rcutorture_nb);
1007 if (stutter_task) {
1008 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1009 kthread_stop(stutter_task);
1011 stutter_task = NULL;
1012 if (shuffler_task) {
1013 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1014 kthread_stop(shuffler_task);
1016 shuffler_task = NULL;
1018 if (writer_task) {
1019 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1020 kthread_stop(writer_task);
1022 writer_task = NULL;
1024 if (reader_tasks) {
1025 for (i = 0; i < nrealreaders; i++) {
1026 if (reader_tasks[i]) {
1027 VERBOSE_PRINTK_STRING(
1028 "Stopping rcu_torture_reader task");
1029 kthread_stop(reader_tasks[i]);
1031 reader_tasks[i] = NULL;
1033 kfree(reader_tasks);
1034 reader_tasks = NULL;
1036 rcu_torture_current = NULL;
1038 if (fakewriter_tasks) {
1039 for (i = 0; i < nfakewriters; i++) {
1040 if (fakewriter_tasks[i]) {
1041 VERBOSE_PRINTK_STRING(
1042 "Stopping rcu_torture_fakewriter task");
1043 kthread_stop(fakewriter_tasks[i]);
1045 fakewriter_tasks[i] = NULL;
1047 kfree(fakewriter_tasks);
1048 fakewriter_tasks = NULL;
1051 if (stats_task) {
1052 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1053 kthread_stop(stats_task);
1055 stats_task = NULL;
1057 /* Wait for all RCU callbacks to fire. */
1059 if (cur_ops->cb_barrier != NULL)
1060 cur_ops->cb_barrier();
1062 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
1064 if (cur_ops->cleanup)
1065 cur_ops->cleanup();
1066 if (atomic_read(&n_rcu_torture_error))
1067 rcu_torture_print_module_parms("End of test: FAILURE");
1068 else
1069 rcu_torture_print_module_parms("End of test: SUCCESS");
1072 static int __init
1073 rcu_torture_init(void)
1075 int i;
1076 int cpu;
1077 int firsterr = 0;
1078 static struct rcu_torture_ops *torture_ops[] =
1079 { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops,
1080 &srcu_ops, &sched_ops, &sched_ops_sync, };
1082 mutex_lock(&fullstop_mutex);
1084 /* Process args and tell the world that the torturer is on the job. */
1085 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1086 cur_ops = torture_ops[i];
1087 if (strcmp(torture_type, cur_ops->name) == 0)
1088 break;
1090 if (i == ARRAY_SIZE(torture_ops)) {
1091 printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
1092 torture_type);
1093 mutex_unlock(&fullstop_mutex);
1094 return (-EINVAL);
1096 if (cur_ops->init)
1097 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1099 if (nreaders >= 0)
1100 nrealreaders = nreaders;
1101 else
1102 nrealreaders = 2 * num_online_cpus();
1103 rcu_torture_print_module_parms("Start of test");
1104 fullstop = FULLSTOP_DONTSTOP;
1106 /* Set up the freelist. */
1108 INIT_LIST_HEAD(&rcu_torture_freelist);
1109 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1110 rcu_tortures[i].rtort_mbtest = 0;
1111 list_add_tail(&rcu_tortures[i].rtort_free,
1112 &rcu_torture_freelist);
1115 /* Initialize the statistics so that each run gets its own numbers. */
1117 rcu_torture_current = NULL;
1118 rcu_torture_current_version = 0;
1119 atomic_set(&n_rcu_torture_alloc, 0);
1120 atomic_set(&n_rcu_torture_alloc_fail, 0);
1121 atomic_set(&n_rcu_torture_free, 0);
1122 atomic_set(&n_rcu_torture_mberror, 0);
1123 atomic_set(&n_rcu_torture_error, 0);
1124 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1125 atomic_set(&rcu_torture_wcount[i], 0);
1126 for_each_possible_cpu(cpu) {
1127 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1128 per_cpu(rcu_torture_count, cpu)[i] = 0;
1129 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1133 /* Start up the kthreads. */
1135 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1136 writer_task = kthread_run(rcu_torture_writer, NULL,
1137 "rcu_torture_writer");
1138 if (IS_ERR(writer_task)) {
1139 firsterr = PTR_ERR(writer_task);
1140 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1141 writer_task = NULL;
1142 goto unwind;
1144 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1145 GFP_KERNEL);
1146 if (fakewriter_tasks == NULL) {
1147 VERBOSE_PRINTK_ERRSTRING("out of memory");
1148 firsterr = -ENOMEM;
1149 goto unwind;
1151 for (i = 0; i < nfakewriters; i++) {
1152 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1153 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1154 "rcu_torture_fakewriter");
1155 if (IS_ERR(fakewriter_tasks[i])) {
1156 firsterr = PTR_ERR(fakewriter_tasks[i]);
1157 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1158 fakewriter_tasks[i] = NULL;
1159 goto unwind;
1162 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
1163 GFP_KERNEL);
1164 if (reader_tasks == NULL) {
1165 VERBOSE_PRINTK_ERRSTRING("out of memory");
1166 firsterr = -ENOMEM;
1167 goto unwind;
1169 for (i = 0; i < nrealreaders; i++) {
1170 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1171 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1172 "rcu_torture_reader");
1173 if (IS_ERR(reader_tasks[i])) {
1174 firsterr = PTR_ERR(reader_tasks[i]);
1175 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1176 reader_tasks[i] = NULL;
1177 goto unwind;
1180 if (stat_interval > 0) {
1181 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1182 stats_task = kthread_run(rcu_torture_stats, NULL,
1183 "rcu_torture_stats");
1184 if (IS_ERR(stats_task)) {
1185 firsterr = PTR_ERR(stats_task);
1186 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1187 stats_task = NULL;
1188 goto unwind;
1191 if (test_no_idle_hz) {
1192 rcu_idle_cpu = num_online_cpus() - 1;
1193 /* Create the shuffler thread */
1194 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1195 "rcu_torture_shuffle");
1196 if (IS_ERR(shuffler_task)) {
1197 firsterr = PTR_ERR(shuffler_task);
1198 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1199 shuffler_task = NULL;
1200 goto unwind;
1203 if (stutter < 0)
1204 stutter = 0;
1205 if (stutter) {
1206 /* Create the stutter thread */
1207 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1208 "rcu_torture_stutter");
1209 if (IS_ERR(stutter_task)) {
1210 firsterr = PTR_ERR(stutter_task);
1211 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1212 stutter_task = NULL;
1213 goto unwind;
1216 register_reboot_notifier(&rcutorture_nb);
1217 mutex_unlock(&fullstop_mutex);
1218 return 0;
1220 unwind:
1221 mutex_unlock(&fullstop_mutex);
1222 rcu_torture_cleanup();
1223 return firsterr;
1226 module_init(rcu_torture_init);
1227 module_exit(rcu_torture_cleanup);