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 /* 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 */
149 * Detect and respond to a system shutdown.
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
;
159 printk(KERN_WARNING
/* but going down anyway, so... */
160 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
161 mutex_unlock(&fullstop_mutex
);
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
) {
173 "rcutorture thread %s parking due to system shutdown\n",
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)
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
);
193 atomic_inc(&n_rcu_torture_alloc
);
194 p
= rcu_torture_freelist
.next
;
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.
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
;
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().
228 rcu_random(struct rcu_random_state
*rrsp
)
230 if (--rrsp
->rrs_count
< 0) {
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
);
240 rcu_stutter_wait(char *title
)
242 while (stutter_pause_test
|| !rcutorture_runnable
) {
243 if (rcutorture_runnable
)
244 schedule_timeout_interruptible(1);
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
{
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
);
264 void (*cb_barrier
)(void);
265 int (*stats
)(char *page
);
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
)
281 static void rcu_read_delay(struct rcu_random_state
*rrsp
)
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
);
293 static void rcu_torture_read_unlock(int idx
) __releases(RCU
)
298 static int rcu_torture_completed(void)
300 return rcu_batches_completed();
304 rcu_torture_cb(struct rcu_head
*p
)
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. */
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
);
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
= {
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
,
345 static void rcu_sync_torture_deferred_free(struct rcu_torture
*p
)
348 struct rcu_torture
*rp
;
349 struct rcu_torture
*rp1
;
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
,
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
,
387 * Definitions for rcu_bh torture testing.
390 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH
)
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
= {
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
,
448 static struct rcu_torture_ops rcu_bh_sync_ops
= {
449 .init
= rcu_sync_torture_init
,
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
,
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
)
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
);
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
)
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");
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
,
541 .stats
= srcu_torture_stats
,
546 * Definitions for sched torture testing.
549 static int sched_torture_read_lock(void)
555 static void sched_torture_read_unlock(int idx
)
560 static int sched_torture_completed(void)
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)
575 static struct rcu_torture_ops sched_ops
= {
576 .init
= rcu_sync_torture_init
,
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
,
590 static struct rcu_torture_ops sched_ops_sync
= {
591 .init
= rcu_sync_torture_init
,
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
,
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").
610 rcu_torture_writer(void *arg
)
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);
622 schedule_timeout_uninterruptible(1);
623 if ((rp
= rcu_torture_alloc()) == NULL
)
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
);
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);
651 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
652 * delay between calls.
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);
663 schedule_timeout_uninterruptible(1 + rcu_random(&rand
)%10);
664 udelay(rcu_random(&rand
) & 0x3ff);
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);
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
)
686 static DEFINE_RCU_RANDOM(rand
);
687 static DEFINE_SPINLOCK(rand_lock
);
688 struct rcu_torture
*p
;
691 idx
= cur_ops
->readlock();
692 completed
= cur_ops
->completed();
693 p
= rcu_dereference(rcu_torture_current
);
695 /* Leave because rcu_torture_writer is not yet underway */
696 cur_ops
->readunlock(idx
);
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
);
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
];
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.
729 rcu_torture_reader(void *arg
)
733 DEFINE_RCU_RANDOM(rand
);
734 struct rcu_torture
*p
;
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);
744 if (irqreader
&& cur_ops
->irqcapable
) {
745 if (!timer_pending(&t
))
748 idx
= cur_ops
->readlock();
749 completed
= cur_ops
->completed();
750 p
= rcu_dereference(rcu_torture_current
);
752 /* Wait for rcu_torture_writer to get underway */
753 cur_ops
->readunlock(idx
);
754 schedule_timeout_interruptible(HZ
);
757 if (p
->rtort_mbtest
== 0)
758 atomic_inc(&n_rcu_torture_mberror
);
759 cur_ops
->readdelay(&rand
);
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
];
774 cur_ops
->readunlock(idx
);
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
)
782 while (!kthread_should_stop())
783 schedule_timeout_uninterruptible(1);
788 * Create an RCU-torture statistics message in the specified buffer.
791 rcu_torture_printk(char *page
)
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)
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 "
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
);
825 cnt
+= sprintf(&page
[cnt
], "!!! ");
826 atomic_inc(&n_rcu_torture_error
);
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");
844 cnt
+= cur_ops
->stats(&page
[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).
857 rcu_torture_stats_print(void)
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.
873 rcu_torture_stats(void *arg
)
875 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
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");
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)
895 cpus_setall(tmp_mask
);
898 /* No point in shuffling if there is only one online CPU (ex: UP) */
899 if (num_online_cpus() == 1) {
904 if (rcu_idle_cpu
!= -1)
905 cpu_clear(rcu_idle_cpu
, tmp_mask
);
907 set_cpus_allowed_ptr(current
, &tmp_mask
);
910 for (i
= 0; i
< nrealreaders
; i
++)
912 set_cpus_allowed_ptr(reader_tasks
[i
],
916 if (fakewriter_tasks
) {
917 for (i
= 0; i
< nfakewriters
; i
++)
918 if (fakewriter_tasks
[i
])
919 set_cpus_allowed_ptr(fakewriter_tasks
[i
],
924 set_cpus_allowed_ptr(writer_task
, &tmp_mask
);
927 set_cpus_allowed_ptr(stats_task
, &tmp_mask
);
929 if (rcu_idle_cpu
== -1)
930 rcu_idle_cpu
= num_online_cpus() - 1;
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.
942 rcu_torture_shuffle(void *arg
)
944 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
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");
954 /* Cause the rcutorture test to "stutter", starting and stopping all
955 * threads periodically.
958 rcu_torture_stutter(void *arg
)
960 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
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");
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
,
985 static struct notifier_block rcutorture_nb
= {
986 .notifier_call
= rcutorture_shutdown_notify
,
990 rcu_torture_cleanup(void)
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();
1004 fullstop
= FULLSTOP_RMMOD
;
1005 mutex_unlock(&fullstop_mutex
);
1006 unregister_reboot_notifier(&rcutorture_nb
);
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
;
1019 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1020 kthread_stop(writer_task
);
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
;
1052 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1053 kthread_stop(stats_task
);
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
)
1066 if (atomic_read(&n_rcu_torture_error
))
1067 rcu_torture_print_module_parms("End of test: FAILURE");
1069 rcu_torture_print_module_parms("End of test: SUCCESS");
1073 rcu_torture_init(void)
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)
1090 if (i
== ARRAY_SIZE(torture_ops
)) {
1091 printk(KERN_ALERT
"rcutorture: invalid torture type: \"%s\"\n",
1093 mutex_unlock(&fullstop_mutex
);
1097 cur_ops
->init(); /* no "goto unwind" prior to this point!!! */
1100 nrealreaders
= nreaders
;
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");
1144 fakewriter_tasks
= kzalloc(nfakewriters
* sizeof(fakewriter_tasks
[0]),
1146 if (fakewriter_tasks
== NULL
) {
1147 VERBOSE_PRINTK_ERRSTRING("out of memory");
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
;
1162 reader_tasks
= kzalloc(nrealreaders
* sizeof(reader_tasks
[0]),
1164 if (reader_tasks
== NULL
) {
1165 VERBOSE_PRINTK_ERRSTRING("out of memory");
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
;
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");
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
;
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
;
1216 register_reboot_notifier(&rcutorture_nb
);
1217 mutex_unlock(&fullstop_mutex
);
1221 mutex_unlock(&fullstop_mutex
);
1222 rcu_torture_cleanup();
1226 module_init(rcu_torture_init
);
1227 module_exit(rcu_torture_cleanup
);