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
106 struct rcu_head rtort_rcu
;
107 int rtort_pipe_count
;
108 struct list_head rtort_free
;
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
) =
119 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN
+ 1], rcu_torture_batch
) =
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
135 #define RCUTORTURE_RUNNABLE_INIT 0
137 int rcutorture_runnable
= RCUTORTURE_RUNNABLE_INIT
;
139 #define FULLSTOP_SIGNALED 1 /* Bail due to signal. */
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.
149 rcutorture_shutdown_notify(struct notifier_block
*unused1
,
150 unsigned long unused2
, void *unused3
)
154 if (signal_pending(current
)) {
155 mutex_lock(&fullstop_mutex
);
156 if (!ACCESS_ONCE(fullstop
))
157 fullstop
= FULLSTOP_SIGNALED
;
158 mutex_unlock(&fullstop_mutex
);
164 * Allocate an element from the rcu_tortures pool.
166 static struct rcu_torture
*
167 rcu_torture_alloc(void)
171 spin_lock_bh(&rcu_torture_lock
);
172 if (list_empty(&rcu_torture_freelist
)) {
173 atomic_inc(&n_rcu_torture_alloc_fail
);
174 spin_unlock_bh(&rcu_torture_lock
);
177 atomic_inc(&n_rcu_torture_alloc
);
178 p
= rcu_torture_freelist
.next
;
180 spin_unlock_bh(&rcu_torture_lock
);
181 return container_of(p
, struct rcu_torture
, rtort_free
);
185 * Free an element to the rcu_tortures pool.
188 rcu_torture_free(struct rcu_torture
*p
)
190 atomic_inc(&n_rcu_torture_free
);
191 spin_lock_bh(&rcu_torture_lock
);
192 list_add_tail(&p
->rtort_free
, &rcu_torture_freelist
);
193 spin_unlock_bh(&rcu_torture_lock
);
196 struct rcu_random_state
{
197 unsigned long rrs_state
;
201 #define RCU_RANDOM_MULT 39916801 /* prime */
202 #define RCU_RANDOM_ADD 479001701 /* prime */
203 #define RCU_RANDOM_REFRESH 10000
205 #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
208 * Crude but fast random-number generator. Uses a linear congruential
209 * generator, with occasional help from cpu_clock().
212 rcu_random(struct rcu_random_state
*rrsp
)
214 if (--rrsp
->rrs_count
< 0) {
216 (unsigned long)cpu_clock(raw_smp_processor_id());
217 rrsp
->rrs_count
= RCU_RANDOM_REFRESH
;
219 rrsp
->rrs_state
= rrsp
->rrs_state
* RCU_RANDOM_MULT
+ RCU_RANDOM_ADD
;
220 return swahw32(rrsp
->rrs_state
);
224 rcu_stutter_wait(void)
226 while ((stutter_pause_test
|| !rcutorture_runnable
) && !fullstop
) {
227 if (rcutorture_runnable
)
228 schedule_timeout_interruptible(1);
230 schedule_timeout_interruptible(round_jiffies_relative(HZ
));
235 * Operations vector for selecting different types of tests.
238 struct rcu_torture_ops
{
240 void (*cleanup
)(void);
241 int (*readlock
)(void);
242 void (*readdelay
)(struct rcu_random_state
*rrsp
);
243 void (*readunlock
)(int idx
);
244 int (*completed
)(void);
245 void (*deferredfree
)(struct rcu_torture
*p
);
247 void (*cb_barrier
)(void);
248 int (*stats
)(char *page
);
252 static struct rcu_torture_ops
*cur_ops
= NULL
;
255 * Definitions for rcu torture testing.
258 static int rcu_torture_read_lock(void) __acquires(RCU
)
264 static void rcu_read_delay(struct rcu_random_state
*rrsp
)
267 const long longdelay
= 200;
269 /* We want there to be long-running readers, but not all the time. */
271 delay
= rcu_random(rrsp
) % (nrealreaders
* 2 * longdelay
);
276 static void rcu_torture_read_unlock(int idx
) __releases(RCU
)
281 static int rcu_torture_completed(void)
283 return rcu_batches_completed();
287 rcu_torture_cb(struct rcu_head
*p
)
290 struct rcu_torture
*rp
= container_of(p
, struct rcu_torture
, rtort_rcu
);
293 /* Test is ending, just drop callbacks on the floor. */
294 /* The next initialization will pick up the pieces. */
297 i
= rp
->rtort_pipe_count
;
298 if (i
> RCU_TORTURE_PIPE_LEN
)
299 i
= RCU_TORTURE_PIPE_LEN
;
300 atomic_inc(&rcu_torture_wcount
[i
]);
301 if (++rp
->rtort_pipe_count
>= RCU_TORTURE_PIPE_LEN
) {
302 rp
->rtort_mbtest
= 0;
303 rcu_torture_free(rp
);
305 cur_ops
->deferredfree(rp
);
308 static void rcu_torture_deferred_free(struct rcu_torture
*p
)
310 call_rcu(&p
->rtort_rcu
, rcu_torture_cb
);
313 static struct rcu_torture_ops rcu_ops
= {
316 .readlock
= rcu_torture_read_lock
,
317 .readdelay
= rcu_read_delay
,
318 .readunlock
= rcu_torture_read_unlock
,
319 .completed
= rcu_torture_completed
,
320 .deferredfree
= rcu_torture_deferred_free
,
321 .sync
= synchronize_rcu
,
322 .cb_barrier
= rcu_barrier
,
328 static void rcu_sync_torture_deferred_free(struct rcu_torture
*p
)
331 struct rcu_torture
*rp
;
332 struct rcu_torture
*rp1
;
335 list_add(&p
->rtort_free
, &rcu_torture_removed
);
336 list_for_each_entry_safe(rp
, rp1
, &rcu_torture_removed
, rtort_free
) {
337 i
= rp
->rtort_pipe_count
;
338 if (i
> RCU_TORTURE_PIPE_LEN
)
339 i
= RCU_TORTURE_PIPE_LEN
;
340 atomic_inc(&rcu_torture_wcount
[i
]);
341 if (++rp
->rtort_pipe_count
>= RCU_TORTURE_PIPE_LEN
) {
342 rp
->rtort_mbtest
= 0;
343 list_del(&rp
->rtort_free
);
344 rcu_torture_free(rp
);
349 static void rcu_sync_torture_init(void)
351 INIT_LIST_HEAD(&rcu_torture_removed
);
354 static struct rcu_torture_ops rcu_sync_ops
= {
355 .init
= rcu_sync_torture_init
,
357 .readlock
= rcu_torture_read_lock
,
358 .readdelay
= rcu_read_delay
,
359 .readunlock
= rcu_torture_read_unlock
,
360 .completed
= rcu_torture_completed
,
361 .deferredfree
= rcu_sync_torture_deferred_free
,
362 .sync
= synchronize_rcu
,
370 * Definitions for rcu_bh torture testing.
373 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH
)
379 static void rcu_bh_torture_read_unlock(int idx
) __releases(RCU_BH
)
381 rcu_read_unlock_bh();
384 static int rcu_bh_torture_completed(void)
386 return rcu_batches_completed_bh();
389 static void rcu_bh_torture_deferred_free(struct rcu_torture
*p
)
391 call_rcu_bh(&p
->rtort_rcu
, rcu_torture_cb
);
394 struct rcu_bh_torture_synchronize
{
395 struct rcu_head head
;
396 struct completion completion
;
399 static void rcu_bh_torture_wakeme_after_cb(struct rcu_head
*head
)
401 struct rcu_bh_torture_synchronize
*rcu
;
403 rcu
= container_of(head
, struct rcu_bh_torture_synchronize
, head
);
404 complete(&rcu
->completion
);
407 static void rcu_bh_torture_synchronize(void)
409 struct rcu_bh_torture_synchronize rcu
;
411 init_completion(&rcu
.completion
);
412 call_rcu_bh(&rcu
.head
, rcu_bh_torture_wakeme_after_cb
);
413 wait_for_completion(&rcu
.completion
);
416 static struct rcu_torture_ops rcu_bh_ops
= {
419 .readlock
= rcu_bh_torture_read_lock
,
420 .readdelay
= rcu_read_delay
, /* just reuse rcu's version. */
421 .readunlock
= rcu_bh_torture_read_unlock
,
422 .completed
= rcu_bh_torture_completed
,
423 .deferredfree
= rcu_bh_torture_deferred_free
,
424 .sync
= rcu_bh_torture_synchronize
,
425 .cb_barrier
= rcu_barrier_bh
,
431 static struct rcu_torture_ops rcu_bh_sync_ops
= {
432 .init
= rcu_sync_torture_init
,
434 .readlock
= rcu_bh_torture_read_lock
,
435 .readdelay
= rcu_read_delay
, /* just reuse rcu's version. */
436 .readunlock
= rcu_bh_torture_read_unlock
,
437 .completed
= rcu_bh_torture_completed
,
438 .deferredfree
= rcu_sync_torture_deferred_free
,
439 .sync
= rcu_bh_torture_synchronize
,
443 .name
= "rcu_bh_sync"
447 * Definitions for srcu torture testing.
450 static struct srcu_struct srcu_ctl
;
452 static void srcu_torture_init(void)
454 init_srcu_struct(&srcu_ctl
);
455 rcu_sync_torture_init();
458 static void srcu_torture_cleanup(void)
460 synchronize_srcu(&srcu_ctl
);
461 cleanup_srcu_struct(&srcu_ctl
);
464 static int srcu_torture_read_lock(void) __acquires(&srcu_ctl
)
466 return srcu_read_lock(&srcu_ctl
);
469 static void srcu_read_delay(struct rcu_random_state
*rrsp
)
472 const long uspertick
= 1000000 / HZ
;
473 const long longdelay
= 10;
475 /* We want there to be long-running readers, but not all the time. */
477 delay
= rcu_random(rrsp
) % (nrealreaders
* 2 * longdelay
* uspertick
);
479 schedule_timeout_interruptible(longdelay
);
482 static void srcu_torture_read_unlock(int idx
) __releases(&srcu_ctl
)
484 srcu_read_unlock(&srcu_ctl
, idx
);
487 static int srcu_torture_completed(void)
489 return srcu_batches_completed(&srcu_ctl
);
492 static void srcu_torture_synchronize(void)
494 synchronize_srcu(&srcu_ctl
);
497 static int srcu_torture_stats(char *page
)
501 int idx
= srcu_ctl
.completed
& 0x1;
503 cnt
+= sprintf(&page
[cnt
], "%s%s per-CPU(idx=%d):",
504 torture_type
, TORTURE_FLAG
, idx
);
505 for_each_possible_cpu(cpu
) {
506 cnt
+= sprintf(&page
[cnt
], " %d(%d,%d)", cpu
,
507 per_cpu_ptr(srcu_ctl
.per_cpu_ref
, cpu
)->c
[!idx
],
508 per_cpu_ptr(srcu_ctl
.per_cpu_ref
, cpu
)->c
[idx
]);
510 cnt
+= sprintf(&page
[cnt
], "\n");
514 static struct rcu_torture_ops srcu_ops
= {
515 .init
= srcu_torture_init
,
516 .cleanup
= srcu_torture_cleanup
,
517 .readlock
= srcu_torture_read_lock
,
518 .readdelay
= srcu_read_delay
,
519 .readunlock
= srcu_torture_read_unlock
,
520 .completed
= srcu_torture_completed
,
521 .deferredfree
= rcu_sync_torture_deferred_free
,
522 .sync
= srcu_torture_synchronize
,
524 .stats
= srcu_torture_stats
,
529 * Definitions for sched torture testing.
532 static int sched_torture_read_lock(void)
538 static void sched_torture_read_unlock(int idx
)
543 static int sched_torture_completed(void)
548 static void rcu_sched_torture_deferred_free(struct rcu_torture
*p
)
550 call_rcu_sched(&p
->rtort_rcu
, rcu_torture_cb
);
553 static void sched_torture_synchronize(void)
558 static struct rcu_torture_ops sched_ops
= {
559 .init
= rcu_sync_torture_init
,
561 .readlock
= sched_torture_read_lock
,
562 .readdelay
= rcu_read_delay
, /* just reuse rcu's version. */
563 .readunlock
= sched_torture_read_unlock
,
564 .completed
= sched_torture_completed
,
565 .deferredfree
= rcu_sched_torture_deferred_free
,
566 .sync
= sched_torture_synchronize
,
567 .cb_barrier
= rcu_barrier_sched
,
573 static struct rcu_torture_ops sched_ops_sync
= {
574 .init
= rcu_sync_torture_init
,
576 .readlock
= sched_torture_read_lock
,
577 .readdelay
= rcu_read_delay
, /* just reuse rcu's version. */
578 .readunlock
= sched_torture_read_unlock
,
579 .completed
= sched_torture_completed
,
580 .deferredfree
= rcu_sync_torture_deferred_free
,
581 .sync
= sched_torture_synchronize
,
588 * RCU torture writer kthread. Repeatedly substitutes a new structure
589 * for that pointed to by rcu_torture_current, freeing the old structure
590 * after a series of grace periods (the "pipeline").
593 rcu_torture_writer(void *arg
)
596 long oldbatch
= rcu_batches_completed();
597 struct rcu_torture
*rp
;
598 struct rcu_torture
*old_rp
;
599 static DEFINE_RCU_RANDOM(rand
);
601 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
602 set_user_nice(current
, 19);
605 schedule_timeout_uninterruptible(1);
606 if ((rp
= rcu_torture_alloc()) == NULL
)
608 rp
->rtort_pipe_count
= 0;
609 udelay(rcu_random(&rand
) & 0x3ff);
610 old_rp
= rcu_torture_current
;
611 rp
->rtort_mbtest
= 1;
612 rcu_assign_pointer(rcu_torture_current
, rp
);
615 i
= old_rp
->rtort_pipe_count
;
616 if (i
> RCU_TORTURE_PIPE_LEN
)
617 i
= RCU_TORTURE_PIPE_LEN
;
618 atomic_inc(&rcu_torture_wcount
[i
]);
619 old_rp
->rtort_pipe_count
++;
620 cur_ops
->deferredfree(old_rp
);
622 rcu_torture_current_version
++;
623 oldbatch
= cur_ops
->completed();
625 } while (!kthread_should_stop() && !fullstop
);
626 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
627 while (!kthread_should_stop() && fullstop
!= FULLSTOP_SIGNALED
)
628 schedule_timeout_uninterruptible(1);
633 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
634 * delay between calls.
637 rcu_torture_fakewriter(void *arg
)
639 DEFINE_RCU_RANDOM(rand
);
641 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
642 set_user_nice(current
, 19);
645 schedule_timeout_uninterruptible(1 + rcu_random(&rand
)%10);
646 udelay(rcu_random(&rand
) & 0x3ff);
649 } while (!kthread_should_stop() && !fullstop
);
651 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
652 while (!kthread_should_stop() && fullstop
!= FULLSTOP_SIGNALED
)
653 schedule_timeout_uninterruptible(1);
658 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
659 * incrementing the corresponding element of the pipeline array. The
660 * counter in the element should never be greater than 1, otherwise, the
661 * RCU implementation is broken.
663 static void rcu_torture_timer(unsigned long unused
)
667 static DEFINE_RCU_RANDOM(rand
);
668 static DEFINE_SPINLOCK(rand_lock
);
669 struct rcu_torture
*p
;
672 idx
= cur_ops
->readlock();
673 completed
= cur_ops
->completed();
674 p
= rcu_dereference(rcu_torture_current
);
676 /* Leave because rcu_torture_writer is not yet underway */
677 cur_ops
->readunlock(idx
);
680 if (p
->rtort_mbtest
== 0)
681 atomic_inc(&n_rcu_torture_mberror
);
682 spin_lock(&rand_lock
);
683 cur_ops
->readdelay(&rand
);
684 n_rcu_torture_timers
++;
685 spin_unlock(&rand_lock
);
687 pipe_count
= p
->rtort_pipe_count
;
688 if (pipe_count
> RCU_TORTURE_PIPE_LEN
) {
689 /* Should not happen, but... */
690 pipe_count
= RCU_TORTURE_PIPE_LEN
;
692 ++__get_cpu_var(rcu_torture_count
)[pipe_count
];
693 completed
= cur_ops
->completed() - completed
;
694 if (completed
> RCU_TORTURE_PIPE_LEN
) {
695 /* Should not happen, but... */
696 completed
= RCU_TORTURE_PIPE_LEN
;
698 ++__get_cpu_var(rcu_torture_batch
)[completed
];
700 cur_ops
->readunlock(idx
);
704 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
705 * incrementing the corresponding element of the pipeline array. The
706 * counter in the element should never be greater than 1, otherwise, the
707 * RCU implementation is broken.
710 rcu_torture_reader(void *arg
)
714 DEFINE_RCU_RANDOM(rand
);
715 struct rcu_torture
*p
;
719 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
720 set_user_nice(current
, 19);
721 if (irqreader
&& cur_ops
->irqcapable
)
722 setup_timer_on_stack(&t
, rcu_torture_timer
, 0);
725 if (irqreader
&& cur_ops
->irqcapable
) {
726 if (!timer_pending(&t
))
729 idx
= cur_ops
->readlock();
730 completed
= cur_ops
->completed();
731 p
= rcu_dereference(rcu_torture_current
);
733 /* Wait for rcu_torture_writer to get underway */
734 cur_ops
->readunlock(idx
);
735 schedule_timeout_interruptible(HZ
);
738 if (p
->rtort_mbtest
== 0)
739 atomic_inc(&n_rcu_torture_mberror
);
740 cur_ops
->readdelay(&rand
);
742 pipe_count
= p
->rtort_pipe_count
;
743 if (pipe_count
> RCU_TORTURE_PIPE_LEN
) {
744 /* Should not happen, but... */
745 pipe_count
= RCU_TORTURE_PIPE_LEN
;
747 ++__get_cpu_var(rcu_torture_count
)[pipe_count
];
748 completed
= cur_ops
->completed() - completed
;
749 if (completed
> RCU_TORTURE_PIPE_LEN
) {
750 /* Should not happen, but... */
751 completed
= RCU_TORTURE_PIPE_LEN
;
753 ++__get_cpu_var(rcu_torture_batch
)[completed
];
755 cur_ops
->readunlock(idx
);
758 } while (!kthread_should_stop() && !fullstop
);
759 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
760 if (irqreader
&& cur_ops
->irqcapable
)
762 while (!kthread_should_stop() && fullstop
!= FULLSTOP_SIGNALED
)
763 schedule_timeout_uninterruptible(1);
768 * Create an RCU-torture statistics message in the specified buffer.
771 rcu_torture_printk(char *page
)
776 long pipesummary
[RCU_TORTURE_PIPE_LEN
+ 1] = { 0 };
777 long batchsummary
[RCU_TORTURE_PIPE_LEN
+ 1] = { 0 };
779 for_each_possible_cpu(cpu
) {
780 for (i
= 0; i
< RCU_TORTURE_PIPE_LEN
+ 1; i
++) {
781 pipesummary
[i
] += per_cpu(rcu_torture_count
, cpu
)[i
];
782 batchsummary
[i
] += per_cpu(rcu_torture_batch
, cpu
)[i
];
785 for (i
= RCU_TORTURE_PIPE_LEN
- 1; i
>= 0; i
--) {
786 if (pipesummary
[i
] != 0)
789 cnt
+= sprintf(&page
[cnt
], "%s%s ", torture_type
, TORTURE_FLAG
);
790 cnt
+= sprintf(&page
[cnt
],
791 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
794 rcu_torture_current_version
,
795 list_empty(&rcu_torture_freelist
),
796 atomic_read(&n_rcu_torture_alloc
),
797 atomic_read(&n_rcu_torture_alloc_fail
),
798 atomic_read(&n_rcu_torture_free
),
799 atomic_read(&n_rcu_torture_mberror
),
800 n_rcu_torture_timers
);
801 if (atomic_read(&n_rcu_torture_mberror
) != 0)
802 cnt
+= sprintf(&page
[cnt
], " !!!");
803 cnt
+= sprintf(&page
[cnt
], "\n%s%s ", torture_type
, TORTURE_FLAG
);
805 cnt
+= sprintf(&page
[cnt
], "!!! ");
806 atomic_inc(&n_rcu_torture_error
);
809 cnt
+= sprintf(&page
[cnt
], "Reader Pipe: ");
810 for (i
= 0; i
< RCU_TORTURE_PIPE_LEN
+ 1; i
++)
811 cnt
+= sprintf(&page
[cnt
], " %ld", pipesummary
[i
]);
812 cnt
+= sprintf(&page
[cnt
], "\n%s%s ", torture_type
, TORTURE_FLAG
);
813 cnt
+= sprintf(&page
[cnt
], "Reader Batch: ");
814 for (i
= 0; i
< RCU_TORTURE_PIPE_LEN
+ 1; i
++)
815 cnt
+= sprintf(&page
[cnt
], " %ld", batchsummary
[i
]);
816 cnt
+= sprintf(&page
[cnt
], "\n%s%s ", torture_type
, TORTURE_FLAG
);
817 cnt
+= sprintf(&page
[cnt
], "Free-Block Circulation: ");
818 for (i
= 0; i
< RCU_TORTURE_PIPE_LEN
+ 1; i
++) {
819 cnt
+= sprintf(&page
[cnt
], " %d",
820 atomic_read(&rcu_torture_wcount
[i
]));
822 cnt
+= sprintf(&page
[cnt
], "\n");
824 cnt
+= cur_ops
->stats(&page
[cnt
]);
829 * Print torture statistics. Caller must ensure that there is only
830 * one call to this function at a given time!!! This is normally
831 * accomplished by relying on the module system to only have one copy
832 * of the module loaded, and then by giving the rcu_torture_stats
833 * kthread full control (or the init/cleanup functions when rcu_torture_stats
834 * thread is not running).
837 rcu_torture_stats_print(void)
841 cnt
= rcu_torture_printk(printk_buf
);
842 printk(KERN_ALERT
"%s", printk_buf
);
846 * Periodically prints torture statistics, if periodic statistics printing
847 * was specified via the stat_interval module parameter.
849 * No need to worry about fullstop here, since this one doesn't reference
850 * volatile state or register callbacks.
853 rcu_torture_stats(void *arg
)
855 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
857 schedule_timeout_interruptible(stat_interval
* HZ
);
858 rcu_torture_stats_print();
859 } while (!kthread_should_stop() && !fullstop
);
860 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
864 static int rcu_idle_cpu
; /* Force all torture tasks off this CPU */
866 /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
867 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
869 static void rcu_torture_shuffle_tasks(void)
871 cpumask_var_t tmp_mask
;
874 if (!alloc_cpumask_var(&tmp_mask
, GFP_KERNEL
))
877 cpumask_setall(tmp_mask
);
880 /* No point in shuffling if there is only one online CPU (ex: UP) */
881 if (num_online_cpus() == 1)
884 if (rcu_idle_cpu
!= -1)
885 cpumask_clear_cpu(rcu_idle_cpu
, tmp_mask
);
887 set_cpus_allowed_ptr(current
, tmp_mask
);
890 for (i
= 0; i
< nrealreaders
; i
++)
892 set_cpus_allowed_ptr(reader_tasks
[i
],
896 if (fakewriter_tasks
) {
897 for (i
= 0; i
< nfakewriters
; i
++)
898 if (fakewriter_tasks
[i
])
899 set_cpus_allowed_ptr(fakewriter_tasks
[i
],
904 set_cpus_allowed_ptr(writer_task
, tmp_mask
);
907 set_cpus_allowed_ptr(stats_task
, tmp_mask
);
909 if (rcu_idle_cpu
== -1)
910 rcu_idle_cpu
= num_online_cpus() - 1;
916 free_cpumask_var(tmp_mask
);
919 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
920 * system to become idle at a time and cut off its timer ticks. This is meant
921 * to test the support for such tickless idle CPU in RCU.
924 rcu_torture_shuffle(void *arg
)
926 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
928 schedule_timeout_interruptible(shuffle_interval
* HZ
);
929 rcu_torture_shuffle_tasks();
930 } while (!kthread_should_stop() && !fullstop
);
931 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
935 /* Cause the rcutorture test to "stutter", starting and stopping all
936 * threads periodically.
939 rcu_torture_stutter(void *arg
)
941 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
943 schedule_timeout_interruptible(stutter
* HZ
);
944 stutter_pause_test
= 1;
945 if (!kthread_should_stop() && !fullstop
)
946 schedule_timeout_interruptible(stutter
* HZ
);
947 stutter_pause_test
= 0;
948 } while (!kthread_should_stop() && !fullstop
);
949 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
954 rcu_torture_print_module_parms(char *tag
)
956 printk(KERN_ALERT
"%s" TORTURE_FLAG
957 "--- %s: nreaders=%d nfakewriters=%d "
958 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
959 "shuffle_interval=%d stutter=%d irqreader=%d\n",
960 torture_type
, tag
, nrealreaders
, nfakewriters
,
961 stat_interval
, verbose
, test_no_idle_hz
, shuffle_interval
,
965 static struct notifier_block rcutorture_nb
= {
966 .notifier_call
= rcutorture_shutdown_notify
,
970 rcu_torture_cleanup(void)
974 mutex_lock(&fullstop_mutex
);
976 /* If being signaled, let it happen, then exit. */
977 mutex_unlock(&fullstop_mutex
);
978 schedule_timeout_interruptible(10 * HZ
);
979 if (cur_ops
->cb_barrier
!= NULL
)
980 cur_ops
->cb_barrier();
983 fullstop
= FULLSTOP_CLEANUP
;
984 mutex_unlock(&fullstop_mutex
);
985 unregister_reboot_notifier(&rcutorture_nb
);
987 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
988 kthread_stop(stutter_task
);
992 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
993 kthread_stop(shuffler_task
);
995 shuffler_task
= NULL
;
998 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
999 kthread_stop(writer_task
);
1004 for (i
= 0; i
< nrealreaders
; i
++) {
1005 if (reader_tasks
[i
]) {
1006 VERBOSE_PRINTK_STRING(
1007 "Stopping rcu_torture_reader task");
1008 kthread_stop(reader_tasks
[i
]);
1010 reader_tasks
[i
] = NULL
;
1012 kfree(reader_tasks
);
1013 reader_tasks
= NULL
;
1015 rcu_torture_current
= NULL
;
1017 if (fakewriter_tasks
) {
1018 for (i
= 0; i
< nfakewriters
; i
++) {
1019 if (fakewriter_tasks
[i
]) {
1020 VERBOSE_PRINTK_STRING(
1021 "Stopping rcu_torture_fakewriter task");
1022 kthread_stop(fakewriter_tasks
[i
]);
1024 fakewriter_tasks
[i
] = NULL
;
1026 kfree(fakewriter_tasks
);
1027 fakewriter_tasks
= NULL
;
1031 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1032 kthread_stop(stats_task
);
1036 /* Wait for all RCU callbacks to fire. */
1038 if (cur_ops
->cb_barrier
!= NULL
)
1039 cur_ops
->cb_barrier();
1041 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
1043 if (cur_ops
->cleanup
)
1045 if (atomic_read(&n_rcu_torture_error
))
1046 rcu_torture_print_module_parms("End of test: FAILURE");
1048 rcu_torture_print_module_parms("End of test: SUCCESS");
1052 rcu_torture_init(void)
1057 static struct rcu_torture_ops
*torture_ops
[] =
1058 { &rcu_ops
, &rcu_sync_ops
, &rcu_bh_ops
, &rcu_bh_sync_ops
,
1059 &srcu_ops
, &sched_ops
, &sched_ops_sync
, };
1061 mutex_lock(&fullstop_mutex
);
1063 /* Process args and tell the world that the torturer is on the job. */
1064 for (i
= 0; i
< ARRAY_SIZE(torture_ops
); i
++) {
1065 cur_ops
= torture_ops
[i
];
1066 if (strcmp(torture_type
, cur_ops
->name
) == 0)
1069 if (i
== ARRAY_SIZE(torture_ops
)) {
1070 printk(KERN_ALERT
"rcutorture: invalid torture type: \"%s\"\n",
1072 mutex_unlock(&fullstop_mutex
);
1076 cur_ops
->init(); /* no "goto unwind" prior to this point!!! */
1079 nrealreaders
= nreaders
;
1081 nrealreaders
= 2 * num_online_cpus();
1082 rcu_torture_print_module_parms("Start of test");
1085 /* Set up the freelist. */
1087 INIT_LIST_HEAD(&rcu_torture_freelist
);
1088 for (i
= 0; i
< ARRAY_SIZE(rcu_tortures
); i
++) {
1089 rcu_tortures
[i
].rtort_mbtest
= 0;
1090 list_add_tail(&rcu_tortures
[i
].rtort_free
,
1091 &rcu_torture_freelist
);
1094 /* Initialize the statistics so that each run gets its own numbers. */
1096 rcu_torture_current
= NULL
;
1097 rcu_torture_current_version
= 0;
1098 atomic_set(&n_rcu_torture_alloc
, 0);
1099 atomic_set(&n_rcu_torture_alloc_fail
, 0);
1100 atomic_set(&n_rcu_torture_free
, 0);
1101 atomic_set(&n_rcu_torture_mberror
, 0);
1102 atomic_set(&n_rcu_torture_error
, 0);
1103 for (i
= 0; i
< RCU_TORTURE_PIPE_LEN
+ 1; i
++)
1104 atomic_set(&rcu_torture_wcount
[i
], 0);
1105 for_each_possible_cpu(cpu
) {
1106 for (i
= 0; i
< RCU_TORTURE_PIPE_LEN
+ 1; i
++) {
1107 per_cpu(rcu_torture_count
, cpu
)[i
] = 0;
1108 per_cpu(rcu_torture_batch
, cpu
)[i
] = 0;
1112 /* Start up the kthreads. */
1114 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1115 writer_task
= kthread_run(rcu_torture_writer
, NULL
,
1116 "rcu_torture_writer");
1117 if (IS_ERR(writer_task
)) {
1118 firsterr
= PTR_ERR(writer_task
);
1119 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1123 fakewriter_tasks
= kzalloc(nfakewriters
* sizeof(fakewriter_tasks
[0]),
1125 if (fakewriter_tasks
== NULL
) {
1126 VERBOSE_PRINTK_ERRSTRING("out of memory");
1130 for (i
= 0; i
< nfakewriters
; i
++) {
1131 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1132 fakewriter_tasks
[i
] = kthread_run(rcu_torture_fakewriter
, NULL
,
1133 "rcu_torture_fakewriter");
1134 if (IS_ERR(fakewriter_tasks
[i
])) {
1135 firsterr
= PTR_ERR(fakewriter_tasks
[i
]);
1136 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1137 fakewriter_tasks
[i
] = NULL
;
1141 reader_tasks
= kzalloc(nrealreaders
* sizeof(reader_tasks
[0]),
1143 if (reader_tasks
== NULL
) {
1144 VERBOSE_PRINTK_ERRSTRING("out of memory");
1148 for (i
= 0; i
< nrealreaders
; i
++) {
1149 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1150 reader_tasks
[i
] = kthread_run(rcu_torture_reader
, NULL
,
1151 "rcu_torture_reader");
1152 if (IS_ERR(reader_tasks
[i
])) {
1153 firsterr
= PTR_ERR(reader_tasks
[i
]);
1154 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1155 reader_tasks
[i
] = NULL
;
1159 if (stat_interval
> 0) {
1160 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1161 stats_task
= kthread_run(rcu_torture_stats
, NULL
,
1162 "rcu_torture_stats");
1163 if (IS_ERR(stats_task
)) {
1164 firsterr
= PTR_ERR(stats_task
);
1165 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1170 if (test_no_idle_hz
) {
1171 rcu_idle_cpu
= num_online_cpus() - 1;
1172 /* Create the shuffler thread */
1173 shuffler_task
= kthread_run(rcu_torture_shuffle
, NULL
,
1174 "rcu_torture_shuffle");
1175 if (IS_ERR(shuffler_task
)) {
1176 firsterr
= PTR_ERR(shuffler_task
);
1177 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1178 shuffler_task
= NULL
;
1185 /* Create the stutter thread */
1186 stutter_task
= kthread_run(rcu_torture_stutter
, NULL
,
1187 "rcu_torture_stutter");
1188 if (IS_ERR(stutter_task
)) {
1189 firsterr
= PTR_ERR(stutter_task
);
1190 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1191 stutter_task
= NULL
;
1195 register_reboot_notifier(&rcutorture_nb
);
1196 mutex_unlock(&fullstop_mutex
);
1200 mutex_unlock(&fullstop_mutex
);
1201 rcu_torture_cleanup();
1205 module_init(rcu_torture_init
);
1206 module_exit(rcu_torture_cleanup
);