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