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