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