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/freezer.h>
43 #include <linux/cpu.h>
44 #include <linux/delay.h>
45 #include <linux/byteorder/swabb.h>
46 #include <linux/stat.h>
47 #include <linux/srcu.h>
48 #include <linux/slab.h>
50 MODULE_LICENSE("GPL");
51 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
52 "Josh Triplett <josh@freedesktop.org>");
54 static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
55 static int nfakewriters = 4; /* # fake writer threads */
56 static int stat_interval; /* Interval between stats, in seconds. */
57 /* Defaults to "only at end of test". */
58 static int verbose; /* Print more debug info. */
59 static int test_no_idle_hz; /* Test RCU's support for tickless idle CPUs. */
60 static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
61 static int stutter = 5; /* Start/stop testing interval (in sec) */
62 static char *torture_type = "rcu"; /* What RCU implementation to torture. */
64 module_param(nreaders, int, 0444);
65 MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
66 module_param(nfakewriters, int, 0444);
67 MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
68 module_param(stat_interval, int, 0444);
69 MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
70 module_param(verbose, bool, 0444);
71 MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
72 module_param(test_no_idle_hz, bool, 0444);
73 MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
74 module_param(shuffle_interval, int, 0444);
75 MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
76 module_param(stutter, int, 0444);
77 MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
78 module_param(torture_type, charp, 0444);
79 MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
81 #define TORTURE_FLAG "-torture:"
82 #define PRINTK_STRING(s) \
83 do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
84 #define VERBOSE_PRINTK_STRING(s) \
85 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
86 #define VERBOSE_PRINTK_ERRSTRING(s) \
87 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
89 static char printk_buf[4096];
91 static int nrealreaders;
92 static struct task_struct *writer_task;
93 static struct task_struct **fakewriter_tasks;
94 static struct task_struct **reader_tasks;
95 static struct task_struct *stats_task;
96 static struct task_struct *shuffler_task;
97 static struct task_struct *stutter_task;
99 #define RCU_TORTURE_PIPE_LEN 10
102 struct rcu_head rtort_rcu;
103 int rtort_pipe_count;
104 struct list_head rtort_free;
108 static int fullstop = 0; /* stop generating callbacks at test end. */
109 static LIST_HEAD(rcu_torture_freelist);
110 static struct rcu_torture *rcu_torture_current = NULL;
111 static long rcu_torture_current_version = 0;
112 static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
113 static DEFINE_SPINLOCK(rcu_torture_lock);
114 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
116 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
118 static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
119 static atomic_t n_rcu_torture_alloc;
120 static atomic_t n_rcu_torture_alloc_fail;
121 static atomic_t n_rcu_torture_free;
122 static atomic_t n_rcu_torture_mberror;
123 static atomic_t n_rcu_torture_error;
124 static struct list_head rcu_torture_removed;
126 static int stutter_pause_test = 0;
128 #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
129 #define RCUTORTURE_RUNNABLE_INIT 1
131 #define RCUTORTURE_RUNNABLE_INIT 0
133 int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
136 * Allocate an element from the rcu_tortures pool.
138 static struct rcu_torture *
139 rcu_torture_alloc(void)
143 spin_lock_bh(&rcu_torture_lock);
144 if (list_empty(&rcu_torture_freelist)) {
145 atomic_inc(&n_rcu_torture_alloc_fail);
146 spin_unlock_bh(&rcu_torture_lock);
149 atomic_inc(&n_rcu_torture_alloc);
150 p = rcu_torture_freelist.next;
152 spin_unlock_bh(&rcu_torture_lock);
153 return container_of(p, struct rcu_torture, rtort_free);
157 * Free an element to the rcu_tortures pool.
160 rcu_torture_free(struct rcu_torture *p)
162 atomic_inc(&n_rcu_torture_free);
163 spin_lock_bh(&rcu_torture_lock);
164 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
165 spin_unlock_bh(&rcu_torture_lock);
168 struct rcu_random_state {
169 unsigned long rrs_state;
173 #define RCU_RANDOM_MULT 39916801 /* prime */
174 #define RCU_RANDOM_ADD 479001701 /* prime */
175 #define RCU_RANDOM_REFRESH 10000
177 #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
180 * Crude but fast random-number generator. Uses a linear congruential
181 * generator, with occasional help from cpu_clock().
184 rcu_random(struct rcu_random_state *rrsp)
186 if (--rrsp->rrs_count < 0) {
188 (unsigned long)cpu_clock(raw_smp_processor_id());
189 rrsp->rrs_count = RCU_RANDOM_REFRESH;
191 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
192 return swahw32(rrsp->rrs_state);
196 rcu_stutter_wait(void)
198 while (stutter_pause_test || !rcutorture_runnable)
199 if (rcutorture_runnable)
200 schedule_timeout_interruptible(1);
202 schedule_timeout_interruptible(round_jiffies_relative(HZ));
206 * Operations vector for selecting different types of tests.
209 struct rcu_torture_ops {
211 void (*cleanup)(void);
212 int (*readlock)(void);
213 void (*readdelay)(struct rcu_random_state *rrsp);
214 void (*readunlock)(int idx);
215 int (*completed)(void);
216 void (*deferredfree)(struct rcu_torture *p);
218 void (*cb_barrier)(void);
219 int (*stats)(char *page);
222 static struct rcu_torture_ops *cur_ops = NULL;
225 * Definitions for rcu torture testing.
228 static int rcu_torture_read_lock(void) __acquires(RCU)
234 static void rcu_read_delay(struct rcu_random_state *rrsp)
237 const long longdelay = 200;
239 /* We want there to be long-running readers, but not all the time. */
241 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay);
246 static void rcu_torture_read_unlock(int idx) __releases(RCU)
251 static int rcu_torture_completed(void)
253 return rcu_batches_completed();
257 rcu_torture_cb(struct rcu_head *p)
260 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
263 /* Test is ending, just drop callbacks on the floor. */
264 /* The next initialization will pick up the pieces. */
267 i = rp->rtort_pipe_count;
268 if (i > RCU_TORTURE_PIPE_LEN)
269 i = RCU_TORTURE_PIPE_LEN;
270 atomic_inc(&rcu_torture_wcount[i]);
271 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
272 rp->rtort_mbtest = 0;
273 rcu_torture_free(rp);
275 cur_ops->deferredfree(rp);
278 static void rcu_torture_deferred_free(struct rcu_torture *p)
280 call_rcu(&p->rtort_rcu, rcu_torture_cb);
283 static struct rcu_torture_ops rcu_ops = {
286 .readlock = rcu_torture_read_lock,
287 .readdelay = rcu_read_delay,
288 .readunlock = rcu_torture_read_unlock,
289 .completed = rcu_torture_completed,
290 .deferredfree = rcu_torture_deferred_free,
291 .sync = synchronize_rcu,
292 .cb_barrier = rcu_barrier,
297 static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
300 struct rcu_torture *rp;
301 struct rcu_torture *rp1;
304 list_add(&p->rtort_free, &rcu_torture_removed);
305 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
306 i = rp->rtort_pipe_count;
307 if (i > RCU_TORTURE_PIPE_LEN)
308 i = RCU_TORTURE_PIPE_LEN;
309 atomic_inc(&rcu_torture_wcount[i]);
310 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
311 rp->rtort_mbtest = 0;
312 list_del(&rp->rtort_free);
313 rcu_torture_free(rp);
318 static void rcu_sync_torture_init(void)
320 INIT_LIST_HEAD(&rcu_torture_removed);
323 static struct rcu_torture_ops rcu_sync_ops = {
324 .init = rcu_sync_torture_init,
326 .readlock = rcu_torture_read_lock,
327 .readdelay = rcu_read_delay,
328 .readunlock = rcu_torture_read_unlock,
329 .completed = rcu_torture_completed,
330 .deferredfree = rcu_sync_torture_deferred_free,
331 .sync = synchronize_rcu,
338 * Definitions for rcu_bh torture testing.
341 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
347 static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
349 rcu_read_unlock_bh();
352 static int rcu_bh_torture_completed(void)
354 return rcu_batches_completed_bh();
357 static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
359 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
362 struct rcu_bh_torture_synchronize {
363 struct rcu_head head;
364 struct completion completion;
367 static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
369 struct rcu_bh_torture_synchronize *rcu;
371 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
372 complete(&rcu->completion);
375 static void rcu_bh_torture_synchronize(void)
377 struct rcu_bh_torture_synchronize rcu;
379 init_completion(&rcu.completion);
380 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
381 wait_for_completion(&rcu.completion);
384 static struct rcu_torture_ops rcu_bh_ops = {
387 .readlock = rcu_bh_torture_read_lock,
388 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
389 .readunlock = rcu_bh_torture_read_unlock,
390 .completed = rcu_bh_torture_completed,
391 .deferredfree = rcu_bh_torture_deferred_free,
392 .sync = rcu_bh_torture_synchronize,
393 .cb_barrier = rcu_barrier_bh,
398 static struct rcu_torture_ops rcu_bh_sync_ops = {
399 .init = rcu_sync_torture_init,
401 .readlock = rcu_bh_torture_read_lock,
402 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
403 .readunlock = rcu_bh_torture_read_unlock,
404 .completed = rcu_bh_torture_completed,
405 .deferredfree = rcu_sync_torture_deferred_free,
406 .sync = rcu_bh_torture_synchronize,
409 .name = "rcu_bh_sync"
413 * Definitions for srcu torture testing.
416 static struct srcu_struct srcu_ctl;
418 static void srcu_torture_init(void)
420 init_srcu_struct(&srcu_ctl);
421 rcu_sync_torture_init();
424 static void srcu_torture_cleanup(void)
426 synchronize_srcu(&srcu_ctl);
427 cleanup_srcu_struct(&srcu_ctl);
430 static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
432 return srcu_read_lock(&srcu_ctl);
435 static void srcu_read_delay(struct rcu_random_state *rrsp)
438 const long uspertick = 1000000 / HZ;
439 const long longdelay = 10;
441 /* We want there to be long-running readers, but not all the time. */
443 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
445 schedule_timeout_interruptible(longdelay);
448 static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
450 srcu_read_unlock(&srcu_ctl, idx);
453 static int srcu_torture_completed(void)
455 return srcu_batches_completed(&srcu_ctl);
458 static void srcu_torture_synchronize(void)
460 synchronize_srcu(&srcu_ctl);
463 static int srcu_torture_stats(char *page)
467 int idx = srcu_ctl.completed & 0x1;
469 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
470 torture_type, TORTURE_FLAG, idx);
471 for_each_possible_cpu(cpu) {
472 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
473 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
474 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
476 cnt += sprintf(&page[cnt], "\n");
480 static struct rcu_torture_ops srcu_ops = {
481 .init = srcu_torture_init,
482 .cleanup = srcu_torture_cleanup,
483 .readlock = srcu_torture_read_lock,
484 .readdelay = srcu_read_delay,
485 .readunlock = srcu_torture_read_unlock,
486 .completed = srcu_torture_completed,
487 .deferredfree = rcu_sync_torture_deferred_free,
488 .sync = srcu_torture_synchronize,
490 .stats = srcu_torture_stats,
495 * Definitions for sched torture testing.
498 static int sched_torture_read_lock(void)
504 static void sched_torture_read_unlock(int idx)
509 static int sched_torture_completed(void)
514 static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
516 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
519 static void sched_torture_synchronize(void)
524 static struct rcu_torture_ops sched_ops = {
525 .init = rcu_sync_torture_init,
527 .readlock = sched_torture_read_lock,
528 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
529 .readunlock = sched_torture_read_unlock,
530 .completed = sched_torture_completed,
531 .deferredfree = rcu_sched_torture_deferred_free,
532 .sync = sched_torture_synchronize,
533 .cb_barrier = rcu_barrier_sched,
538 static struct rcu_torture_ops sched_ops_sync = {
539 .init = rcu_sync_torture_init,
541 .readlock = sched_torture_read_lock,
542 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
543 .readunlock = sched_torture_read_unlock,
544 .completed = sched_torture_completed,
545 .deferredfree = rcu_sync_torture_deferred_free,
546 .sync = sched_torture_synchronize,
553 * RCU torture writer kthread. Repeatedly substitutes a new structure
554 * for that pointed to by rcu_torture_current, freeing the old structure
555 * after a series of grace periods (the "pipeline").
558 rcu_torture_writer(void *arg)
561 long oldbatch = rcu_batches_completed();
562 struct rcu_torture *rp;
563 struct rcu_torture *old_rp;
564 static DEFINE_RCU_RANDOM(rand);
566 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
567 set_user_nice(current, 19);
570 schedule_timeout_uninterruptible(1);
571 if ((rp = rcu_torture_alloc()) == NULL)
573 rp->rtort_pipe_count = 0;
574 udelay(rcu_random(&rand) & 0x3ff);
575 old_rp = rcu_torture_current;
576 rp->rtort_mbtest = 1;
577 rcu_assign_pointer(rcu_torture_current, rp);
580 i = old_rp->rtort_pipe_count;
581 if (i > RCU_TORTURE_PIPE_LEN)
582 i = RCU_TORTURE_PIPE_LEN;
583 atomic_inc(&rcu_torture_wcount[i]);
584 old_rp->rtort_pipe_count++;
585 cur_ops->deferredfree(old_rp);
587 rcu_torture_current_version++;
588 oldbatch = cur_ops->completed();
590 } while (!kthread_should_stop() && !fullstop);
591 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
592 while (!kthread_should_stop())
593 schedule_timeout_uninterruptible(1);
598 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
599 * delay between calls.
602 rcu_torture_fakewriter(void *arg)
604 DEFINE_RCU_RANDOM(rand);
606 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
607 set_user_nice(current, 19);
610 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
611 udelay(rcu_random(&rand) & 0x3ff);
614 } while (!kthread_should_stop() && !fullstop);
616 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
617 while (!kthread_should_stop())
618 schedule_timeout_uninterruptible(1);
623 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
624 * incrementing the corresponding element of the pipeline array. The
625 * counter in the element should never be greater than 1, otherwise, the
626 * RCU implementation is broken.
629 rcu_torture_reader(void *arg)
633 DEFINE_RCU_RANDOM(rand);
634 struct rcu_torture *p;
637 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
638 set_user_nice(current, 19);
641 idx = cur_ops->readlock();
642 completed = cur_ops->completed();
643 p = rcu_dereference(rcu_torture_current);
645 /* Wait for rcu_torture_writer to get underway */
646 cur_ops->readunlock(idx);
647 schedule_timeout_interruptible(HZ);
650 if (p->rtort_mbtest == 0)
651 atomic_inc(&n_rcu_torture_mberror);
652 cur_ops->readdelay(&rand);
654 pipe_count = p->rtort_pipe_count;
655 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
656 /* Should not happen, but... */
657 pipe_count = RCU_TORTURE_PIPE_LEN;
659 ++__get_cpu_var(rcu_torture_count)[pipe_count];
660 completed = cur_ops->completed() - completed;
661 if (completed > RCU_TORTURE_PIPE_LEN) {
662 /* Should not happen, but... */
663 completed = RCU_TORTURE_PIPE_LEN;
665 ++__get_cpu_var(rcu_torture_batch)[completed];
667 cur_ops->readunlock(idx);
670 } while (!kthread_should_stop() && !fullstop);
671 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
672 while (!kthread_should_stop())
673 schedule_timeout_uninterruptible(1);
678 * Create an RCU-torture statistics message in the specified buffer.
681 rcu_torture_printk(char *page)
686 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
687 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
689 for_each_possible_cpu(cpu) {
690 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
691 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
692 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
695 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
696 if (pipesummary[i] != 0)
699 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
700 cnt += sprintf(&page[cnt],
701 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
704 rcu_torture_current_version,
705 list_empty(&rcu_torture_freelist),
706 atomic_read(&n_rcu_torture_alloc),
707 atomic_read(&n_rcu_torture_alloc_fail),
708 atomic_read(&n_rcu_torture_free),
709 atomic_read(&n_rcu_torture_mberror));
710 if (atomic_read(&n_rcu_torture_mberror) != 0)
711 cnt += sprintf(&page[cnt], " !!!");
712 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
714 cnt += sprintf(&page[cnt], "!!! ");
715 atomic_inc(&n_rcu_torture_error);
718 cnt += sprintf(&page[cnt], "Reader Pipe: ");
719 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
720 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
721 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
722 cnt += sprintf(&page[cnt], "Reader Batch: ");
723 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
724 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
725 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
726 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
727 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
728 cnt += sprintf(&page[cnt], " %d",
729 atomic_read(&rcu_torture_wcount[i]));
731 cnt += sprintf(&page[cnt], "\n");
733 cnt += cur_ops->stats(&page[cnt]);
738 * Print torture statistics. Caller must ensure that there is only
739 * one call to this function at a given time!!! This is normally
740 * accomplished by relying on the module system to only have one copy
741 * of the module loaded, and then by giving the rcu_torture_stats
742 * kthread full control (or the init/cleanup functions when rcu_torture_stats
743 * thread is not running).
746 rcu_torture_stats_print(void)
750 cnt = rcu_torture_printk(printk_buf);
751 printk(KERN_ALERT "%s", printk_buf);
755 * Periodically prints torture statistics, if periodic statistics printing
756 * was specified via the stat_interval module parameter.
758 * No need to worry about fullstop here, since this one doesn't reference
759 * volatile state or register callbacks.
762 rcu_torture_stats(void *arg)
764 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
766 schedule_timeout_interruptible(stat_interval * HZ);
767 rcu_torture_stats_print();
768 } while (!kthread_should_stop());
769 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
773 static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
775 /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
776 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
778 static void rcu_torture_shuffle_tasks(void)
783 cpus_setall(tmp_mask);
786 /* No point in shuffling if there is only one online CPU (ex: UP) */
787 if (num_online_cpus() == 1) {
792 if (rcu_idle_cpu != -1)
793 cpu_clear(rcu_idle_cpu, tmp_mask);
795 set_cpus_allowed_ptr(current, &tmp_mask);
798 for (i = 0; i < nrealreaders; i++)
800 set_cpus_allowed_ptr(reader_tasks[i],
804 if (fakewriter_tasks) {
805 for (i = 0; i < nfakewriters; i++)
806 if (fakewriter_tasks[i])
807 set_cpus_allowed_ptr(fakewriter_tasks[i],
812 set_cpus_allowed_ptr(writer_task, &tmp_mask);
815 set_cpus_allowed_ptr(stats_task, &tmp_mask);
817 if (rcu_idle_cpu == -1)
818 rcu_idle_cpu = num_online_cpus() - 1;
825 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
826 * system to become idle at a time and cut off its timer ticks. This is meant
827 * to test the support for such tickless idle CPU in RCU.
830 rcu_torture_shuffle(void *arg)
832 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
834 schedule_timeout_interruptible(shuffle_interval * HZ);
835 rcu_torture_shuffle_tasks();
836 } while (!kthread_should_stop());
837 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
841 /* Cause the rcutorture test to "stutter", starting and stopping all
842 * threads periodically.
845 rcu_torture_stutter(void *arg)
847 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
849 schedule_timeout_interruptible(stutter * HZ);
850 stutter_pause_test = 1;
851 if (!kthread_should_stop())
852 schedule_timeout_interruptible(stutter * HZ);
853 stutter_pause_test = 0;
854 } while (!kthread_should_stop());
855 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
860 rcu_torture_print_module_parms(char *tag)
862 printk(KERN_ALERT "%s" TORTURE_FLAG
863 "--- %s: nreaders=%d nfakewriters=%d "
864 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
865 "shuffle_interval=%d stutter=%d\n",
866 torture_type, tag, nrealreaders, nfakewriters,
867 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
872 rcu_torture_cleanup(void)
878 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
879 kthread_stop(stutter_task);
883 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
884 kthread_stop(shuffler_task);
886 shuffler_task = NULL;
889 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
890 kthread_stop(writer_task);
895 for (i = 0; i < nrealreaders; i++) {
896 if (reader_tasks[i]) {
897 VERBOSE_PRINTK_STRING(
898 "Stopping rcu_torture_reader task");
899 kthread_stop(reader_tasks[i]);
901 reader_tasks[i] = NULL;
906 rcu_torture_current = NULL;
908 if (fakewriter_tasks) {
909 for (i = 0; i < nfakewriters; i++) {
910 if (fakewriter_tasks[i]) {
911 VERBOSE_PRINTK_STRING(
912 "Stopping rcu_torture_fakewriter task");
913 kthread_stop(fakewriter_tasks[i]);
915 fakewriter_tasks[i] = NULL;
917 kfree(fakewriter_tasks);
918 fakewriter_tasks = NULL;
922 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
923 kthread_stop(stats_task);
927 /* Wait for all RCU callbacks to fire. */
929 if (cur_ops->cb_barrier != NULL)
930 cur_ops->cb_barrier();
932 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
934 if (cur_ops->cleanup)
936 if (atomic_read(&n_rcu_torture_error))
937 rcu_torture_print_module_parms("End of test: FAILURE");
939 rcu_torture_print_module_parms("End of test: SUCCESS");
943 rcu_torture_init(void)
948 static struct rcu_torture_ops *torture_ops[] =
949 { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops,
950 &srcu_ops, &sched_ops, &sched_ops_sync, };
952 /* Process args and tell the world that the torturer is on the job. */
953 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
954 cur_ops = torture_ops[i];
955 if (strcmp(torture_type, cur_ops->name) == 0)
958 if (i == ARRAY_SIZE(torture_ops)) {
959 printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
964 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
967 nrealreaders = nreaders;
969 nrealreaders = 2 * num_online_cpus();
970 rcu_torture_print_module_parms("Start of test");
973 /* Set up the freelist. */
975 INIT_LIST_HEAD(&rcu_torture_freelist);
976 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
977 rcu_tortures[i].rtort_mbtest = 0;
978 list_add_tail(&rcu_tortures[i].rtort_free,
979 &rcu_torture_freelist);
982 /* Initialize the statistics so that each run gets its own numbers. */
984 rcu_torture_current = NULL;
985 rcu_torture_current_version = 0;
986 atomic_set(&n_rcu_torture_alloc, 0);
987 atomic_set(&n_rcu_torture_alloc_fail, 0);
988 atomic_set(&n_rcu_torture_free, 0);
989 atomic_set(&n_rcu_torture_mberror, 0);
990 atomic_set(&n_rcu_torture_error, 0);
991 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
992 atomic_set(&rcu_torture_wcount[i], 0);
993 for_each_possible_cpu(cpu) {
994 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
995 per_cpu(rcu_torture_count, cpu)[i] = 0;
996 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1000 /* Start up the kthreads. */
1002 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1003 writer_task = kthread_run(rcu_torture_writer, NULL,
1004 "rcu_torture_writer");
1005 if (IS_ERR(writer_task)) {
1006 firsterr = PTR_ERR(writer_task);
1007 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1011 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1013 if (fakewriter_tasks == NULL) {
1014 VERBOSE_PRINTK_ERRSTRING("out of memory");
1018 for (i = 0; i < nfakewriters; i++) {
1019 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1020 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1021 "rcu_torture_fakewriter");
1022 if (IS_ERR(fakewriter_tasks[i])) {
1023 firsterr = PTR_ERR(fakewriter_tasks[i]);
1024 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1025 fakewriter_tasks[i] = NULL;
1029 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
1031 if (reader_tasks == NULL) {
1032 VERBOSE_PRINTK_ERRSTRING("out of memory");
1036 for (i = 0; i < nrealreaders; i++) {
1037 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1038 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1039 "rcu_torture_reader");
1040 if (IS_ERR(reader_tasks[i])) {
1041 firsterr = PTR_ERR(reader_tasks[i]);
1042 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1043 reader_tasks[i] = NULL;
1047 if (stat_interval > 0) {
1048 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1049 stats_task = kthread_run(rcu_torture_stats, NULL,
1050 "rcu_torture_stats");
1051 if (IS_ERR(stats_task)) {
1052 firsterr = PTR_ERR(stats_task);
1053 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1058 if (test_no_idle_hz) {
1059 rcu_idle_cpu = num_online_cpus() - 1;
1060 /* Create the shuffler thread */
1061 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1062 "rcu_torture_shuffle");
1063 if (IS_ERR(shuffler_task)) {
1064 firsterr = PTR_ERR(shuffler_task);
1065 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1066 shuffler_task = NULL;
1073 /* Create the stutter thread */
1074 stutter_task = kthread_run(rcu_torture_stutter, NULL,
1075 "rcu_torture_stutter");
1076 if (IS_ERR(stutter_task)) {
1077 firsterr = PTR_ERR(stutter_task);
1078 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1079 stutter_task = NULL;
1086 rcu_torture_cleanup();
1090 module_init(rcu_torture_init);
1091 module_exit(rcu_torture_cleanup);