]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/rcutorture.c
Merge branch 'linus' into core/rcu
[net-next-2.6.git] / kernel / rcutorture.c
CommitLineData
a241ec65 1/*
29766f1e 2 * Read-Copy Update module-based torture test facility
a241ec65
PM
3 *
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.
8 *
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.
13 *
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.
17 *
b772e1dd 18 * Copyright (C) IBM Corporation, 2005, 2006
a241ec65
PM
19 *
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
b772e1dd 21 * Josh Triplett <josh@freedesktop.org>
a241ec65
PM
22 *
23 * See also: Documentation/RCU/torture.txt
24 */
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>
a241ec65
PM
38#include <linux/completion.h>
39#include <linux/moduleparam.h>
40#include <linux/percpu.h>
41#include <linux/notifier.h>
83144186 42#include <linux/freezer.h>
a241ec65 43#include <linux/cpu.h>
a241ec65
PM
44#include <linux/delay.h>
45#include <linux/byteorder/swabb.h>
46#include <linux/stat.h>
b2896d2e 47#include <linux/srcu.h>
1aeb272c 48#include <linux/slab.h>
a241ec65
PM
49
50MODULE_LICENSE("GPL");
b772e1dd
JT
51MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
52 "Josh Triplett <josh@freedesktop.org>");
a241ec65 53
4802211c 54static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
b772e1dd 55static int nfakewriters = 4; /* # fake writer threads */
d84f5203 56static int stat_interval; /* Interval between stats, in seconds. */
a241ec65 57 /* Defaults to "only at end of test". */
d84f5203
SV
58static int verbose; /* Print more debug info. */
59static int test_no_idle_hz; /* Test RCU's support for tickless idle CPUs. */
60static int shuffle_interval = 5; /* Interval between shuffles (in sec)*/
20d2e428 61static char *torture_type = "rcu"; /* What RCU implementation to torture. */
a241ec65 62
d6ad6711 63module_param(nreaders, int, 0444);
a241ec65 64MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
d6ad6711 65module_param(nfakewriters, int, 0444);
b772e1dd 66MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
d6ad6711 67module_param(stat_interval, int, 0444);
a241ec65 68MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
d6ad6711 69module_param(verbose, bool, 0444);
a241ec65 70MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
d6ad6711 71module_param(test_no_idle_hz, bool, 0444);
d84f5203 72MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
d6ad6711 73module_param(shuffle_interval, int, 0444);
d84f5203 74MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
d6ad6711 75module_param(torture_type, charp, 0444);
b2896d2e 76MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
72e9bb54
PM
77
78#define TORTURE_FLAG "-torture:"
a241ec65 79#define PRINTK_STRING(s) \
72e9bb54 80 do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
a241ec65 81#define VERBOSE_PRINTK_STRING(s) \
72e9bb54 82 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
a241ec65 83#define VERBOSE_PRINTK_ERRSTRING(s) \
72e9bb54 84 do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
a241ec65
PM
85
86static char printk_buf[4096];
87
88static int nrealreaders;
89static struct task_struct *writer_task;
b772e1dd 90static struct task_struct **fakewriter_tasks;
a241ec65
PM
91static struct task_struct **reader_tasks;
92static struct task_struct *stats_task;
d84f5203 93static struct task_struct *shuffler_task;
a241ec65
PM
94
95#define RCU_TORTURE_PIPE_LEN 10
96
97struct rcu_torture {
98 struct rcu_head rtort_rcu;
99 int rtort_pipe_count;
100 struct list_head rtort_free;
996417d2 101 int rtort_mbtest;
a241ec65
PM
102};
103
104static int fullstop = 0; /* stop generating callbacks at test end. */
105static LIST_HEAD(rcu_torture_freelist);
106static struct rcu_torture *rcu_torture_current = NULL;
107static long rcu_torture_current_version = 0;
108static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
109static DEFINE_SPINLOCK(rcu_torture_lock);
110static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
111 { 0 };
112static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
113 { 0 };
114static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
b2896d2e
PM
115static atomic_t n_rcu_torture_alloc;
116static atomic_t n_rcu_torture_alloc_fail;
117static atomic_t n_rcu_torture_free;
118static atomic_t n_rcu_torture_mberror;
119static atomic_t n_rcu_torture_error;
e3033736 120static struct list_head rcu_torture_removed;
a241ec65
PM
121
122/*
123 * Allocate an element from the rcu_tortures pool.
124 */
97a41e26 125static struct rcu_torture *
a241ec65
PM
126rcu_torture_alloc(void)
127{
128 struct list_head *p;
129
adac1665 130 spin_lock_bh(&rcu_torture_lock);
a241ec65
PM
131 if (list_empty(&rcu_torture_freelist)) {
132 atomic_inc(&n_rcu_torture_alloc_fail);
adac1665 133 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
134 return NULL;
135 }
136 atomic_inc(&n_rcu_torture_alloc);
137 p = rcu_torture_freelist.next;
138 list_del_init(p);
adac1665 139 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
140 return container_of(p, struct rcu_torture, rtort_free);
141}
142
143/*
144 * Free an element to the rcu_tortures pool.
145 */
146static void
147rcu_torture_free(struct rcu_torture *p)
148{
149 atomic_inc(&n_rcu_torture_free);
adac1665 150 spin_lock_bh(&rcu_torture_lock);
a241ec65 151 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
adac1665 152 spin_unlock_bh(&rcu_torture_lock);
a241ec65
PM
153}
154
a241ec65
PM
155struct rcu_random_state {
156 unsigned long rrs_state;
75cfef32 157 long rrs_count;
a241ec65
PM
158};
159
160#define RCU_RANDOM_MULT 39916801 /* prime */
161#define RCU_RANDOM_ADD 479001701 /* prime */
162#define RCU_RANDOM_REFRESH 10000
163
164#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
165
166/*
167 * Crude but fast random-number generator. Uses a linear congruential
c17ac855 168 * generator, with occasional help from cpu_clock().
a241ec65 169 */
75cfef32 170static unsigned long
a241ec65
PM
171rcu_random(struct rcu_random_state *rrsp)
172{
a241ec65 173 if (--rrsp->rrs_count < 0) {
c17ac855
PM
174 rrsp->rrs_state +=
175 (unsigned long)cpu_clock(raw_smp_processor_id());
a241ec65
PM
176 rrsp->rrs_count = RCU_RANDOM_REFRESH;
177 }
178 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
179 return swahw32(rrsp->rrs_state);
180}
181
72e9bb54
PM
182/*
183 * Operations vector for selecting different types of tests.
184 */
185
186struct rcu_torture_ops {
187 void (*init)(void);
188 void (*cleanup)(void);
189 int (*readlock)(void);
b2896d2e 190 void (*readdelay)(struct rcu_random_state *rrsp);
72e9bb54
PM
191 void (*readunlock)(int idx);
192 int (*completed)(void);
193 void (*deferredfree)(struct rcu_torture *p);
b772e1dd 194 void (*sync)(void);
2326974d 195 void (*cb_barrier)(void);
72e9bb54
PM
196 int (*stats)(char *page);
197 char *name;
198};
199static struct rcu_torture_ops *cur_ops = NULL;
200
201/*
202 * Definitions for rcu torture testing.
203 */
204
a49a4af7 205static int rcu_torture_read_lock(void) __acquires(RCU)
72e9bb54
PM
206{
207 rcu_read_lock();
208 return 0;
209}
210
b2896d2e
PM
211static void rcu_read_delay(struct rcu_random_state *rrsp)
212{
213 long delay;
214 const long longdelay = 200;
215
216 /* We want there to be long-running readers, but not all the time. */
217
218 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay);
219 if (!delay)
220 udelay(longdelay);
221}
222
a49a4af7 223static void rcu_torture_read_unlock(int idx) __releases(RCU)
72e9bb54
PM
224{
225 rcu_read_unlock();
226}
227
228static int rcu_torture_completed(void)
229{
230 return rcu_batches_completed();
231}
232
233static void
234rcu_torture_cb(struct rcu_head *p)
235{
236 int i;
237 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
238
239 if (fullstop) {
240 /* Test is ending, just drop callbacks on the floor. */
241 /* The next initialization will pick up the pieces. */
242 return;
243 }
244 i = rp->rtort_pipe_count;
245 if (i > RCU_TORTURE_PIPE_LEN)
246 i = RCU_TORTURE_PIPE_LEN;
247 atomic_inc(&rcu_torture_wcount[i]);
248 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
249 rp->rtort_mbtest = 0;
250 rcu_torture_free(rp);
251 } else
252 cur_ops->deferredfree(rp);
253}
254
255static void rcu_torture_deferred_free(struct rcu_torture *p)
256{
257 call_rcu(&p->rtort_rcu, rcu_torture_cb);
258}
259
260static struct rcu_torture_ops rcu_ops = {
261 .init = NULL,
262 .cleanup = NULL,
263 .readlock = rcu_torture_read_lock,
b2896d2e 264 .readdelay = rcu_read_delay,
72e9bb54
PM
265 .readunlock = rcu_torture_read_unlock,
266 .completed = rcu_torture_completed,
267 .deferredfree = rcu_torture_deferred_free,
b772e1dd 268 .sync = synchronize_rcu,
2326974d 269 .cb_barrier = rcu_barrier,
72e9bb54
PM
270 .stats = NULL,
271 .name = "rcu"
272};
273
e3033736
JT
274static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
275{
276 int i;
277 struct rcu_torture *rp;
278 struct rcu_torture *rp1;
279
280 cur_ops->sync();
281 list_add(&p->rtort_free, &rcu_torture_removed);
282 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
283 i = rp->rtort_pipe_count;
284 if (i > RCU_TORTURE_PIPE_LEN)
285 i = RCU_TORTURE_PIPE_LEN;
286 atomic_inc(&rcu_torture_wcount[i]);
287 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
288 rp->rtort_mbtest = 0;
289 list_del(&rp->rtort_free);
290 rcu_torture_free(rp);
291 }
292 }
293}
294
295static void rcu_sync_torture_init(void)
296{
297 INIT_LIST_HEAD(&rcu_torture_removed);
298}
299
20d2e428
JT
300static struct rcu_torture_ops rcu_sync_ops = {
301 .init = rcu_sync_torture_init,
302 .cleanup = NULL,
303 .readlock = rcu_torture_read_lock,
304 .readdelay = rcu_read_delay,
305 .readunlock = rcu_torture_read_unlock,
306 .completed = rcu_torture_completed,
307 .deferredfree = rcu_sync_torture_deferred_free,
308 .sync = synchronize_rcu,
2326974d 309 .cb_barrier = NULL,
20d2e428
JT
310 .stats = NULL,
311 .name = "rcu_sync"
312};
313
c32e0660
PM
314/*
315 * Definitions for rcu_bh torture testing.
316 */
317
a49a4af7 318static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
c32e0660
PM
319{
320 rcu_read_lock_bh();
321 return 0;
322}
323
a49a4af7 324static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
c32e0660
PM
325{
326 rcu_read_unlock_bh();
327}
328
329static int rcu_bh_torture_completed(void)
330{
331 return rcu_batches_completed_bh();
332}
333
334static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
335{
336 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
337}
338
b772e1dd
JT
339struct rcu_bh_torture_synchronize {
340 struct rcu_head head;
341 struct completion completion;
342};
343
344static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
345{
346 struct rcu_bh_torture_synchronize *rcu;
347
348 rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
349 complete(&rcu->completion);
350}
351
352static void rcu_bh_torture_synchronize(void)
353{
354 struct rcu_bh_torture_synchronize rcu;
355
356 init_completion(&rcu.completion);
357 call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
358 wait_for_completion(&rcu.completion);
359}
360
c32e0660
PM
361static struct rcu_torture_ops rcu_bh_ops = {
362 .init = NULL,
363 .cleanup = NULL,
364 .readlock = rcu_bh_torture_read_lock,
b2896d2e 365 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
c32e0660
PM
366 .readunlock = rcu_bh_torture_read_unlock,
367 .completed = rcu_bh_torture_completed,
368 .deferredfree = rcu_bh_torture_deferred_free,
b772e1dd 369 .sync = rcu_bh_torture_synchronize,
2326974d 370 .cb_barrier = rcu_barrier_bh,
c32e0660
PM
371 .stats = NULL,
372 .name = "rcu_bh"
373};
374
11a14701
JT
375static struct rcu_torture_ops rcu_bh_sync_ops = {
376 .init = rcu_sync_torture_init,
377 .cleanup = NULL,
378 .readlock = rcu_bh_torture_read_lock,
379 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
380 .readunlock = rcu_bh_torture_read_unlock,
381 .completed = rcu_bh_torture_completed,
382 .deferredfree = rcu_sync_torture_deferred_free,
383 .sync = rcu_bh_torture_synchronize,
2326974d 384 .cb_barrier = NULL,
11a14701
JT
385 .stats = NULL,
386 .name = "rcu_bh_sync"
387};
388
b2896d2e
PM
389/*
390 * Definitions for srcu torture testing.
391 */
392
393static struct srcu_struct srcu_ctl;
b2896d2e
PM
394
395static void srcu_torture_init(void)
396{
397 init_srcu_struct(&srcu_ctl);
e3033736 398 rcu_sync_torture_init();
b2896d2e
PM
399}
400
401static void srcu_torture_cleanup(void)
402{
403 synchronize_srcu(&srcu_ctl);
404 cleanup_srcu_struct(&srcu_ctl);
405}
406
012d3ca8 407static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
b2896d2e
PM
408{
409 return srcu_read_lock(&srcu_ctl);
410}
411
412static void srcu_read_delay(struct rcu_random_state *rrsp)
413{
414 long delay;
415 const long uspertick = 1000000 / HZ;
416 const long longdelay = 10;
417
418 /* We want there to be long-running readers, but not all the time. */
419
420 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
421 if (!delay)
422 schedule_timeout_interruptible(longdelay);
423}
424
012d3ca8 425static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
b2896d2e
PM
426{
427 srcu_read_unlock(&srcu_ctl, idx);
428}
429
430static int srcu_torture_completed(void)
431{
432 return srcu_batches_completed(&srcu_ctl);
433}
434
b772e1dd
JT
435static void srcu_torture_synchronize(void)
436{
437 synchronize_srcu(&srcu_ctl);
438}
439
b2896d2e
PM
440static int srcu_torture_stats(char *page)
441{
442 int cnt = 0;
443 int cpu;
444 int idx = srcu_ctl.completed & 0x1;
445
446 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
447 torture_type, TORTURE_FLAG, idx);
448 for_each_possible_cpu(cpu) {
449 cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
450 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
451 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
452 }
453 cnt += sprintf(&page[cnt], "\n");
454 return cnt;
455}
456
457static struct rcu_torture_ops srcu_ops = {
458 .init = srcu_torture_init,
459 .cleanup = srcu_torture_cleanup,
460 .readlock = srcu_torture_read_lock,
461 .readdelay = srcu_read_delay,
462 .readunlock = srcu_torture_read_unlock,
463 .completed = srcu_torture_completed,
e3033736 464 .deferredfree = rcu_sync_torture_deferred_free,
b772e1dd 465 .sync = srcu_torture_synchronize,
2326974d 466 .cb_barrier = NULL,
b2896d2e
PM
467 .stats = srcu_torture_stats,
468 .name = "srcu"
469};
470
4b6c2cca
JT
471/*
472 * Definitions for sched torture testing.
473 */
474
475static int sched_torture_read_lock(void)
476{
477 preempt_disable();
478 return 0;
479}
480
481static void sched_torture_read_unlock(int idx)
482{
483 preempt_enable();
484}
485
486static int sched_torture_completed(void)
487{
488 return 0;
489}
490
2326974d
PM
491static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
492{
493 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
494}
495
4b6c2cca
JT
496static void sched_torture_synchronize(void)
497{
498 synchronize_sched();
499}
500
501static struct rcu_torture_ops sched_ops = {
502 .init = rcu_sync_torture_init,
503 .cleanup = NULL,
504 .readlock = sched_torture_read_lock,
505 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
506 .readunlock = sched_torture_read_unlock,
507 .completed = sched_torture_completed,
2326974d 508 .deferredfree = rcu_sched_torture_deferred_free,
4b6c2cca 509 .sync = sched_torture_synchronize,
2326974d 510 .cb_barrier = rcu_barrier_sched,
4b6c2cca
JT
511 .stats = NULL,
512 .name = "sched"
513};
514
2326974d
PM
515static struct rcu_torture_ops sched_ops_sync = {
516 .init = rcu_sync_torture_init,
517 .cleanup = NULL,
518 .readlock = sched_torture_read_lock,
519 .readdelay = rcu_read_delay, /* just reuse rcu's version. */
520 .readunlock = sched_torture_read_unlock,
521 .completed = sched_torture_completed,
522 .deferredfree = rcu_sync_torture_deferred_free,
523 .sync = sched_torture_synchronize,
524 .cb_barrier = NULL,
525 .stats = NULL,
526 .name = "sched_sync"
527};
528
a241ec65
PM
529/*
530 * RCU torture writer kthread. Repeatedly substitutes a new structure
531 * for that pointed to by rcu_torture_current, freeing the old structure
532 * after a series of grace periods (the "pipeline").
533 */
534static int
535rcu_torture_writer(void *arg)
536{
537 int i;
538 long oldbatch = rcu_batches_completed();
539 struct rcu_torture *rp;
540 struct rcu_torture *old_rp;
541 static DEFINE_RCU_RANDOM(rand);
542
543 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
dbdf65b1
IM
544 set_user_nice(current, 19);
545
a241ec65
PM
546 do {
547 schedule_timeout_uninterruptible(1);
a241ec65
PM
548 if ((rp = rcu_torture_alloc()) == NULL)
549 continue;
550 rp->rtort_pipe_count = 0;
551 udelay(rcu_random(&rand) & 0x3ff);
552 old_rp = rcu_torture_current;
996417d2 553 rp->rtort_mbtest = 1;
a241ec65
PM
554 rcu_assign_pointer(rcu_torture_current, rp);
555 smp_wmb();
c8e5b163 556 if (old_rp) {
a241ec65
PM
557 i = old_rp->rtort_pipe_count;
558 if (i > RCU_TORTURE_PIPE_LEN)
559 i = RCU_TORTURE_PIPE_LEN;
560 atomic_inc(&rcu_torture_wcount[i]);
561 old_rp->rtort_pipe_count++;
72e9bb54 562 cur_ops->deferredfree(old_rp);
a241ec65
PM
563 }
564 rcu_torture_current_version++;
72e9bb54 565 oldbatch = cur_ops->completed();
a241ec65
PM
566 } while (!kthread_should_stop() && !fullstop);
567 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
568 while (!kthread_should_stop())
569 schedule_timeout_uninterruptible(1);
570 return 0;
571}
572
b772e1dd
JT
573/*
574 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
575 * delay between calls.
576 */
577static int
578rcu_torture_fakewriter(void *arg)
579{
580 DEFINE_RCU_RANDOM(rand);
581
582 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
583 set_user_nice(current, 19);
584
585 do {
586 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
587 udelay(rcu_random(&rand) & 0x3ff);
588 cur_ops->sync();
589 } while (!kthread_should_stop() && !fullstop);
590
591 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
592 while (!kthread_should_stop())
593 schedule_timeout_uninterruptible(1);
594 return 0;
595}
596
a241ec65
PM
597/*
598 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
599 * incrementing the corresponding element of the pipeline array. The
600 * counter in the element should never be greater than 1, otherwise, the
601 * RCU implementation is broken.
602 */
603static int
604rcu_torture_reader(void *arg)
605{
606 int completed;
72e9bb54 607 int idx;
a241ec65
PM
608 DEFINE_RCU_RANDOM(rand);
609 struct rcu_torture *p;
610 int pipe_count;
611
612 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
dbdf65b1
IM
613 set_user_nice(current, 19);
614
a241ec65 615 do {
72e9bb54
PM
616 idx = cur_ops->readlock();
617 completed = cur_ops->completed();
a241ec65
PM
618 p = rcu_dereference(rcu_torture_current);
619 if (p == NULL) {
620 /* Wait for rcu_torture_writer to get underway */
72e9bb54 621 cur_ops->readunlock(idx);
a241ec65
PM
622 schedule_timeout_interruptible(HZ);
623 continue;
624 }
996417d2
PM
625 if (p->rtort_mbtest == 0)
626 atomic_inc(&n_rcu_torture_mberror);
b2896d2e 627 cur_ops->readdelay(&rand);
a241ec65
PM
628 preempt_disable();
629 pipe_count = p->rtort_pipe_count;
630 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
631 /* Should not happen, but... */
632 pipe_count = RCU_TORTURE_PIPE_LEN;
633 }
634 ++__get_cpu_var(rcu_torture_count)[pipe_count];
72e9bb54 635 completed = cur_ops->completed() - completed;
a241ec65
PM
636 if (completed > RCU_TORTURE_PIPE_LEN) {
637 /* Should not happen, but... */
638 completed = RCU_TORTURE_PIPE_LEN;
639 }
640 ++__get_cpu_var(rcu_torture_batch)[completed];
641 preempt_enable();
72e9bb54 642 cur_ops->readunlock(idx);
a241ec65
PM
643 schedule();
644 } while (!kthread_should_stop() && !fullstop);
645 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
646 while (!kthread_should_stop())
647 schedule_timeout_uninterruptible(1);
648 return 0;
649}
650
651/*
652 * Create an RCU-torture statistics message in the specified buffer.
653 */
654static int
655rcu_torture_printk(char *page)
656{
657 int cnt = 0;
658 int cpu;
659 int i;
660 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
661 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
662
0a945022 663 for_each_possible_cpu(cpu) {
a241ec65
PM
664 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
665 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
666 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
667 }
668 }
669 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
670 if (pipesummary[i] != 0)
671 break;
672 }
72e9bb54 673 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
a241ec65 674 cnt += sprintf(&page[cnt],
996417d2
PM
675 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
676 "rtmbe: %d",
a241ec65
PM
677 rcu_torture_current,
678 rcu_torture_current_version,
679 list_empty(&rcu_torture_freelist),
680 atomic_read(&n_rcu_torture_alloc),
681 atomic_read(&n_rcu_torture_alloc_fail),
996417d2
PM
682 atomic_read(&n_rcu_torture_free),
683 atomic_read(&n_rcu_torture_mberror));
684 if (atomic_read(&n_rcu_torture_mberror) != 0)
685 cnt += sprintf(&page[cnt], " !!!");
72e9bb54 686 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
996417d2 687 if (i > 1) {
a241ec65 688 cnt += sprintf(&page[cnt], "!!! ");
996417d2
PM
689 atomic_inc(&n_rcu_torture_error);
690 }
a241ec65
PM
691 cnt += sprintf(&page[cnt], "Reader Pipe: ");
692 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
693 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
72e9bb54 694 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
a241ec65 695 cnt += sprintf(&page[cnt], "Reader Batch: ");
72e9bb54 696 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
a241ec65 697 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
72e9bb54 698 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
a241ec65
PM
699 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
700 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
701 cnt += sprintf(&page[cnt], " %d",
702 atomic_read(&rcu_torture_wcount[i]));
703 }
704 cnt += sprintf(&page[cnt], "\n");
c8e5b163 705 if (cur_ops->stats)
72e9bb54 706 cnt += cur_ops->stats(&page[cnt]);
a241ec65
PM
707 return cnt;
708}
709
710/*
711 * Print torture statistics. Caller must ensure that there is only
712 * one call to this function at a given time!!! This is normally
713 * accomplished by relying on the module system to only have one copy
714 * of the module loaded, and then by giving the rcu_torture_stats
715 * kthread full control (or the init/cleanup functions when rcu_torture_stats
716 * thread is not running).
717 */
718static void
719rcu_torture_stats_print(void)
720{
721 int cnt;
722
723 cnt = rcu_torture_printk(printk_buf);
724 printk(KERN_ALERT "%s", printk_buf);
725}
726
727/*
728 * Periodically prints torture statistics, if periodic statistics printing
729 * was specified via the stat_interval module parameter.
730 *
731 * No need to worry about fullstop here, since this one doesn't reference
732 * volatile state or register callbacks.
733 */
734static int
735rcu_torture_stats(void *arg)
736{
737 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
738 do {
739 schedule_timeout_interruptible(stat_interval * HZ);
740 rcu_torture_stats_print();
741 } while (!kthread_should_stop());
742 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
743 return 0;
744}
745
d84f5203
SV
746static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
747
748/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
749 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
750 */
b2896d2e 751static void rcu_torture_shuffle_tasks(void)
d84f5203 752{
f70316da 753 cpumask_t tmp_mask;
d84f5203
SV
754 int i;
755
f70316da 756 cpus_setall(tmp_mask);
86ef5c9a 757 get_online_cpus();
d84f5203
SV
758
759 /* No point in shuffling if there is only one online CPU (ex: UP) */
760 if (num_online_cpus() == 1) {
86ef5c9a 761 put_online_cpus();
d84f5203
SV
762 return;
763 }
764
765 if (rcu_idle_cpu != -1)
766 cpu_clear(rcu_idle_cpu, tmp_mask);
767
f70316da 768 set_cpus_allowed_ptr(current, &tmp_mask);
d84f5203 769
c8e5b163 770 if (reader_tasks) {
d84f5203
SV
771 for (i = 0; i < nrealreaders; i++)
772 if (reader_tasks[i])
f70316da
MT
773 set_cpus_allowed_ptr(reader_tasks[i],
774 &tmp_mask);
d84f5203
SV
775 }
776
c8e5b163 777 if (fakewriter_tasks) {
b772e1dd
JT
778 for (i = 0; i < nfakewriters; i++)
779 if (fakewriter_tasks[i])
f70316da
MT
780 set_cpus_allowed_ptr(fakewriter_tasks[i],
781 &tmp_mask);
b772e1dd
JT
782 }
783
d84f5203 784 if (writer_task)
f70316da 785 set_cpus_allowed_ptr(writer_task, &tmp_mask);
d84f5203
SV
786
787 if (stats_task)
f70316da 788 set_cpus_allowed_ptr(stats_task, &tmp_mask);
d84f5203
SV
789
790 if (rcu_idle_cpu == -1)
791 rcu_idle_cpu = num_online_cpus() - 1;
792 else
793 rcu_idle_cpu--;
794
86ef5c9a 795 put_online_cpus();
d84f5203
SV
796}
797
798/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
799 * system to become idle at a time and cut off its timer ticks. This is meant
800 * to test the support for such tickless idle CPU in RCU.
801 */
802static int
803rcu_torture_shuffle(void *arg)
804{
805 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
806 do {
807 schedule_timeout_interruptible(shuffle_interval * HZ);
808 rcu_torture_shuffle_tasks();
809 } while (!kthread_should_stop());
810 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
811 return 0;
812}
813
95c38322
PM
814static inline void
815rcu_torture_print_module_parms(char *tag)
816{
b772e1dd
JT
817 printk(KERN_ALERT "%s" TORTURE_FLAG
818 "--- %s: nreaders=%d nfakewriters=%d "
95c38322
PM
819 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
820 "shuffle_interval = %d\n",
b772e1dd
JT
821 torture_type, tag, nrealreaders, nfakewriters,
822 stat_interval, verbose, test_no_idle_hz, shuffle_interval);
95c38322
PM
823}
824
a241ec65
PM
825static void
826rcu_torture_cleanup(void)
827{
828 int i;
829
830 fullstop = 1;
c8e5b163 831 if (shuffler_task) {
d84f5203
SV
832 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
833 kthread_stop(shuffler_task);
834 }
835 shuffler_task = NULL;
836
c8e5b163 837 if (writer_task) {
a241ec65
PM
838 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
839 kthread_stop(writer_task);
840 }
841 writer_task = NULL;
842
c8e5b163 843 if (reader_tasks) {
a241ec65 844 for (i = 0; i < nrealreaders; i++) {
c8e5b163 845 if (reader_tasks[i]) {
a241ec65
PM
846 VERBOSE_PRINTK_STRING(
847 "Stopping rcu_torture_reader task");
848 kthread_stop(reader_tasks[i]);
849 }
850 reader_tasks[i] = NULL;
851 }
852 kfree(reader_tasks);
853 reader_tasks = NULL;
854 }
855 rcu_torture_current = NULL;
856
c8e5b163 857 if (fakewriter_tasks) {
b772e1dd 858 for (i = 0; i < nfakewriters; i++) {
c8e5b163 859 if (fakewriter_tasks[i]) {
b772e1dd
JT
860 VERBOSE_PRINTK_STRING(
861 "Stopping rcu_torture_fakewriter task");
862 kthread_stop(fakewriter_tasks[i]);
863 }
864 fakewriter_tasks[i] = NULL;
865 }
866 kfree(fakewriter_tasks);
867 fakewriter_tasks = NULL;
868 }
869
c8e5b163 870 if (stats_task) {
a241ec65
PM
871 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
872 kthread_stop(stats_task);
873 }
874 stats_task = NULL;
875
876 /* Wait for all RCU callbacks to fire. */
2326974d
PM
877
878 if (cur_ops->cb_barrier != NULL)
879 cur_ops->cb_barrier();
a241ec65 880
a241ec65 881 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
72e9bb54 882
c8e5b163 883 if (cur_ops->cleanup)
72e9bb54 884 cur_ops->cleanup();
95c38322
PM
885 if (atomic_read(&n_rcu_torture_error))
886 rcu_torture_print_module_parms("End of test: FAILURE");
887 else
888 rcu_torture_print_module_parms("End of test: SUCCESS");
a241ec65
PM
889}
890
6f8bc500 891static int __init
a241ec65
PM
892rcu_torture_init(void)
893{
894 int i;
895 int cpu;
896 int firsterr = 0;
ade5fb81
JT
897 static struct rcu_torture_ops *torture_ops[] =
898 { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops,
2326974d 899 &srcu_ops, &sched_ops, &sched_ops_sync, };
a241ec65
PM
900
901 /* Process args and tell the world that the torturer is on the job. */
ade5fb81 902 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
72e9bb54 903 cur_ops = torture_ops[i];
ade5fb81 904 if (strcmp(torture_type, cur_ops->name) == 0)
72e9bb54 905 break;
72e9bb54 906 }
ade5fb81 907 if (i == ARRAY_SIZE(torture_ops)) {
72e9bb54
PM
908 printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
909 torture_type);
910 return (-EINVAL);
911 }
c8e5b163 912 if (cur_ops->init)
72e9bb54
PM
913 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
914
a241ec65
PM
915 if (nreaders >= 0)
916 nrealreaders = nreaders;
917 else
918 nrealreaders = 2 * num_online_cpus();
95c38322 919 rcu_torture_print_module_parms("Start of test");
a241ec65
PM
920 fullstop = 0;
921
922 /* Set up the freelist. */
923
924 INIT_LIST_HEAD(&rcu_torture_freelist);
788e770e 925 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
996417d2 926 rcu_tortures[i].rtort_mbtest = 0;
a241ec65
PM
927 list_add_tail(&rcu_tortures[i].rtort_free,
928 &rcu_torture_freelist);
929 }
930
931 /* Initialize the statistics so that each run gets its own numbers. */
932
933 rcu_torture_current = NULL;
934 rcu_torture_current_version = 0;
935 atomic_set(&n_rcu_torture_alloc, 0);
936 atomic_set(&n_rcu_torture_alloc_fail, 0);
937 atomic_set(&n_rcu_torture_free, 0);
996417d2
PM
938 atomic_set(&n_rcu_torture_mberror, 0);
939 atomic_set(&n_rcu_torture_error, 0);
a241ec65
PM
940 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
941 atomic_set(&rcu_torture_wcount[i], 0);
0a945022 942 for_each_possible_cpu(cpu) {
a241ec65
PM
943 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
944 per_cpu(rcu_torture_count, cpu)[i] = 0;
945 per_cpu(rcu_torture_batch, cpu)[i] = 0;
946 }
947 }
948
949 /* Start up the kthreads. */
950
951 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
952 writer_task = kthread_run(rcu_torture_writer, NULL,
953 "rcu_torture_writer");
954 if (IS_ERR(writer_task)) {
955 firsterr = PTR_ERR(writer_task);
956 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
957 writer_task = NULL;
958 goto unwind;
959 }
b772e1dd
JT
960 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
961 GFP_KERNEL);
962 if (fakewriter_tasks == NULL) {
963 VERBOSE_PRINTK_ERRSTRING("out of memory");
964 firsterr = -ENOMEM;
965 goto unwind;
966 }
967 for (i = 0; i < nfakewriters; i++) {
968 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
969 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
970 "rcu_torture_fakewriter");
971 if (IS_ERR(fakewriter_tasks[i])) {
972 firsterr = PTR_ERR(fakewriter_tasks[i]);
973 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
974 fakewriter_tasks[i] = NULL;
975 goto unwind;
976 }
977 }
2860aaba 978 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
a241ec65
PM
979 GFP_KERNEL);
980 if (reader_tasks == NULL) {
981 VERBOSE_PRINTK_ERRSTRING("out of memory");
982 firsterr = -ENOMEM;
983 goto unwind;
984 }
985 for (i = 0; i < nrealreaders; i++) {
986 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
987 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
988 "rcu_torture_reader");
989 if (IS_ERR(reader_tasks[i])) {
990 firsterr = PTR_ERR(reader_tasks[i]);
991 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
992 reader_tasks[i] = NULL;
993 goto unwind;
994 }
995 }
996 if (stat_interval > 0) {
997 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
998 stats_task = kthread_run(rcu_torture_stats, NULL,
999 "rcu_torture_stats");
1000 if (IS_ERR(stats_task)) {
1001 firsterr = PTR_ERR(stats_task);
1002 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1003 stats_task = NULL;
1004 goto unwind;
1005 }
1006 }
d84f5203
SV
1007 if (test_no_idle_hz) {
1008 rcu_idle_cpu = num_online_cpus() - 1;
1009 /* Create the shuffler thread */
1010 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1011 "rcu_torture_shuffle");
1012 if (IS_ERR(shuffler_task)) {
1013 firsterr = PTR_ERR(shuffler_task);
1014 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1015 shuffler_task = NULL;
1016 goto unwind;
1017 }
1018 }
a241ec65
PM
1019 return 0;
1020
1021unwind:
1022 rcu_torture_cleanup();
1023 return firsterr;
1024}
1025
1026module_init(rcu_torture_init);
1027module_exit(rcu_torture_cleanup);