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