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