[PATCH] drivers/mfd: header included twice
[linux-2.6/linux-mips.git] / kernel / rcutorture.c
blob75174c81529a66a10be6b932222d8500d8c41625
1 /*
2 * Read-Copy Update /proc-based torture test facility
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.
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.
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.
18 * Copyright (C) IBM Corporation, 2005
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
22 * See also: Documentation/RCU/torture.txt
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/kthread.h>
29 #include <linux/err.h>
30 #include <linux/spinlock.h>
31 #include <linux/smp.h>
32 #include <linux/rcupdate.h>
33 #include <linux/interrupt.h>
34 #include <linux/sched.h>
35 #include <asm/atomic.h>
36 #include <linux/bitops.h>
37 #include <linux/module.h>
38 #include <linux/completion.h>
39 #include <linux/moduleparam.h>
40 #include <linux/percpu.h>
41 #include <linux/notifier.h>
42 #include <linux/cpu.h>
43 #include <linux/random.h>
44 #include <linux/delay.h>
45 #include <linux/byteorder/swabb.h>
46 #include <linux/stat.h>
48 MODULE_LICENSE("GPL");
50 static int nreaders = -1; /* # reader threads, defaults to 4*ncpus */
51 static int stat_interval = 0; /* Interval between stats, in seconds. */
52 /* Defaults to "only at end of test". */
53 static int verbose = 0; /* Print more debug info. */
55 MODULE_PARM(nreaders, "i");
56 MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
57 MODULE_PARM(stat_interval, "i");
58 MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
59 MODULE_PARM(verbose, "i");
60 MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
61 #define TORTURE_FLAG "rcutorture: "
62 #define PRINTK_STRING(s) \
63 do { printk(KERN_ALERT TORTURE_FLAG s "\n"); } while (0)
64 #define VERBOSE_PRINTK_STRING(s) \
65 do { if (verbose) printk(KERN_ALERT TORTURE_FLAG s "\n"); } while (0)
66 #define VERBOSE_PRINTK_ERRSTRING(s) \
67 do { if (verbose) printk(KERN_ALERT TORTURE_FLAG "!!! " s "\n"); } while (0)
69 static char printk_buf[4096];
71 static int nrealreaders;
72 static struct task_struct *writer_task;
73 static struct task_struct **reader_tasks;
74 static struct task_struct *stats_task;
76 #define RCU_TORTURE_PIPE_LEN 10
78 struct rcu_torture {
79 struct rcu_head rtort_rcu;
80 int rtort_pipe_count;
81 struct list_head rtort_free;
82 int rtort_mbtest;
85 static int fullstop = 0; /* stop generating callbacks at test end. */
86 static LIST_HEAD(rcu_torture_freelist);
87 static struct rcu_torture *rcu_torture_current = NULL;
88 static long rcu_torture_current_version = 0;
89 static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
90 static DEFINE_SPINLOCK(rcu_torture_lock);
91 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
92 { 0 };
93 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
94 { 0 };
95 static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
96 atomic_t n_rcu_torture_alloc;
97 atomic_t n_rcu_torture_alloc_fail;
98 atomic_t n_rcu_torture_free;
99 atomic_t n_rcu_torture_mberror;
100 atomic_t n_rcu_torture_error;
103 * Allocate an element from the rcu_tortures pool.
105 static struct rcu_torture *
106 rcu_torture_alloc(void)
108 struct list_head *p;
110 spin_lock(&rcu_torture_lock);
111 if (list_empty(&rcu_torture_freelist)) {
112 atomic_inc(&n_rcu_torture_alloc_fail);
113 spin_unlock(&rcu_torture_lock);
114 return NULL;
116 atomic_inc(&n_rcu_torture_alloc);
117 p = rcu_torture_freelist.next;
118 list_del_init(p);
119 spin_unlock(&rcu_torture_lock);
120 return container_of(p, struct rcu_torture, rtort_free);
124 * Free an element to the rcu_tortures pool.
126 static void
127 rcu_torture_free(struct rcu_torture *p)
129 atomic_inc(&n_rcu_torture_free);
130 spin_lock(&rcu_torture_lock);
131 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
132 spin_unlock(&rcu_torture_lock);
135 static void
136 rcu_torture_cb(struct rcu_head *p)
138 int i;
139 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
141 if (fullstop) {
142 /* Test is ending, just drop callbacks on the floor. */
143 /* The next initialization will pick up the pieces. */
144 return;
146 i = rp->rtort_pipe_count;
147 if (i > RCU_TORTURE_PIPE_LEN)
148 i = RCU_TORTURE_PIPE_LEN;
149 atomic_inc(&rcu_torture_wcount[i]);
150 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
151 rp->rtort_mbtest = 0;
152 rcu_torture_free(rp);
153 } else
154 call_rcu(p, rcu_torture_cb);
157 struct rcu_random_state {
158 unsigned long rrs_state;
159 unsigned long rrs_count;
162 #define RCU_RANDOM_MULT 39916801 /* prime */
163 #define RCU_RANDOM_ADD 479001701 /* prime */
164 #define RCU_RANDOM_REFRESH 10000
166 #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
169 * Crude but fast random-number generator. Uses a linear congruential
170 * generator, with occasional help from get_random_bytes().
172 static long
173 rcu_random(struct rcu_random_state *rrsp)
175 long refresh;
177 if (--rrsp->rrs_count < 0) {
178 get_random_bytes(&refresh, sizeof(refresh));
179 rrsp->rrs_state += refresh;
180 rrsp->rrs_count = RCU_RANDOM_REFRESH;
182 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
183 return swahw32(rrsp->rrs_state);
187 * RCU torture writer kthread. Repeatedly substitutes a new structure
188 * for that pointed to by rcu_torture_current, freeing the old structure
189 * after a series of grace periods (the "pipeline").
191 static int
192 rcu_torture_writer(void *arg)
194 int i;
195 long oldbatch = rcu_batches_completed();
196 struct rcu_torture *rp;
197 struct rcu_torture *old_rp;
198 static DEFINE_RCU_RANDOM(rand);
200 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
201 set_user_nice(current, 19);
203 do {
204 schedule_timeout_uninterruptible(1);
205 if (rcu_batches_completed() == oldbatch)
206 continue;
207 if ((rp = rcu_torture_alloc()) == NULL)
208 continue;
209 rp->rtort_pipe_count = 0;
210 udelay(rcu_random(&rand) & 0x3ff);
211 old_rp = rcu_torture_current;
212 rp->rtort_mbtest = 1;
213 rcu_assign_pointer(rcu_torture_current, rp);
214 smp_wmb();
215 if (old_rp != NULL) {
216 i = old_rp->rtort_pipe_count;
217 if (i > RCU_TORTURE_PIPE_LEN)
218 i = RCU_TORTURE_PIPE_LEN;
219 atomic_inc(&rcu_torture_wcount[i]);
220 old_rp->rtort_pipe_count++;
221 call_rcu(&old_rp->rtort_rcu, rcu_torture_cb);
223 rcu_torture_current_version++;
224 oldbatch = rcu_batches_completed();
225 } while (!kthread_should_stop() && !fullstop);
226 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
227 while (!kthread_should_stop())
228 schedule_timeout_uninterruptible(1);
229 return 0;
233 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
234 * incrementing the corresponding element of the pipeline array. The
235 * counter in the element should never be greater than 1, otherwise, the
236 * RCU implementation is broken.
238 static int
239 rcu_torture_reader(void *arg)
241 int completed;
242 DEFINE_RCU_RANDOM(rand);
243 struct rcu_torture *p;
244 int pipe_count;
246 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
247 set_user_nice(current, 19);
249 do {
250 rcu_read_lock();
251 completed = rcu_batches_completed();
252 p = rcu_dereference(rcu_torture_current);
253 if (p == NULL) {
254 /* Wait for rcu_torture_writer to get underway */
255 rcu_read_unlock();
256 schedule_timeout_interruptible(HZ);
257 continue;
259 if (p->rtort_mbtest == 0)
260 atomic_inc(&n_rcu_torture_mberror);
261 udelay(rcu_random(&rand) & 0x7f);
262 preempt_disable();
263 pipe_count = p->rtort_pipe_count;
264 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
265 /* Should not happen, but... */
266 pipe_count = RCU_TORTURE_PIPE_LEN;
268 ++__get_cpu_var(rcu_torture_count)[pipe_count];
269 completed = rcu_batches_completed() - completed;
270 if (completed > RCU_TORTURE_PIPE_LEN) {
271 /* Should not happen, but... */
272 completed = RCU_TORTURE_PIPE_LEN;
274 ++__get_cpu_var(rcu_torture_batch)[completed];
275 preempt_enable();
276 rcu_read_unlock();
277 schedule();
278 } while (!kthread_should_stop() && !fullstop);
279 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
280 while (!kthread_should_stop())
281 schedule_timeout_uninterruptible(1);
282 return 0;
286 * Create an RCU-torture statistics message in the specified buffer.
288 static int
289 rcu_torture_printk(char *page)
291 int cnt = 0;
292 int cpu;
293 int i;
294 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
295 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
297 for_each_cpu(cpu) {
298 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
299 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
300 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
303 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
304 if (pipesummary[i] != 0)
305 break;
307 cnt += sprintf(&page[cnt], "rcutorture: ");
308 cnt += sprintf(&page[cnt],
309 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
310 "rtmbe: %d",
311 rcu_torture_current,
312 rcu_torture_current_version,
313 list_empty(&rcu_torture_freelist),
314 atomic_read(&n_rcu_torture_alloc),
315 atomic_read(&n_rcu_torture_alloc_fail),
316 atomic_read(&n_rcu_torture_free),
317 atomic_read(&n_rcu_torture_mberror));
318 if (atomic_read(&n_rcu_torture_mberror) != 0)
319 cnt += sprintf(&page[cnt], " !!!");
320 cnt += sprintf(&page[cnt], "\nrcutorture: ");
321 if (i > 1) {
322 cnt += sprintf(&page[cnt], "!!! ");
323 atomic_inc(&n_rcu_torture_error);
325 cnt += sprintf(&page[cnt], "Reader Pipe: ");
326 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
327 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
328 cnt += sprintf(&page[cnt], "\nrcutorture: ");
329 cnt += sprintf(&page[cnt], "Reader Batch: ");
330 for (i = 0; i < RCU_TORTURE_PIPE_LEN; i++)
331 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
332 cnt += sprintf(&page[cnt], "\nrcutorture: ");
333 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
334 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
335 cnt += sprintf(&page[cnt], " %d",
336 atomic_read(&rcu_torture_wcount[i]));
338 cnt += sprintf(&page[cnt], "\n");
339 return cnt;
343 * Print torture statistics. Caller must ensure that there is only
344 * one call to this function at a given time!!! This is normally
345 * accomplished by relying on the module system to only have one copy
346 * of the module loaded, and then by giving the rcu_torture_stats
347 * kthread full control (or the init/cleanup functions when rcu_torture_stats
348 * thread is not running).
350 static void
351 rcu_torture_stats_print(void)
353 int cnt;
355 cnt = rcu_torture_printk(printk_buf);
356 printk(KERN_ALERT "%s", printk_buf);
360 * Periodically prints torture statistics, if periodic statistics printing
361 * was specified via the stat_interval module parameter.
363 * No need to worry about fullstop here, since this one doesn't reference
364 * volatile state or register callbacks.
366 static int
367 rcu_torture_stats(void *arg)
369 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
370 do {
371 schedule_timeout_interruptible(stat_interval * HZ);
372 rcu_torture_stats_print();
373 } while (!kthread_should_stop());
374 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
375 return 0;
378 static void
379 rcu_torture_cleanup(void)
381 int i;
383 fullstop = 1;
384 if (writer_task != NULL) {
385 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
386 kthread_stop(writer_task);
388 writer_task = NULL;
390 if (reader_tasks != NULL) {
391 for (i = 0; i < nrealreaders; i++) {
392 if (reader_tasks[i] != NULL) {
393 VERBOSE_PRINTK_STRING(
394 "Stopping rcu_torture_reader task");
395 kthread_stop(reader_tasks[i]);
397 reader_tasks[i] = NULL;
399 kfree(reader_tasks);
400 reader_tasks = NULL;
402 rcu_torture_current = NULL;
404 if (stats_task != NULL) {
405 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
406 kthread_stop(stats_task);
408 stats_task = NULL;
410 /* Wait for all RCU callbacks to fire. */
411 rcu_barrier();
413 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
414 printk(KERN_ALERT TORTURE_FLAG
415 "--- End of test: %s\n",
416 atomic_read(&n_rcu_torture_error) == 0 ? "SUCCESS" : "FAILURE");
419 static int
420 rcu_torture_init(void)
422 int i;
423 int cpu;
424 int firsterr = 0;
426 /* Process args and tell the world that the torturer is on the job. */
428 if (nreaders >= 0)
429 nrealreaders = nreaders;
430 else
431 nrealreaders = 2 * num_online_cpus();
432 printk(KERN_ALERT TORTURE_FLAG
433 "--- Start of test: nreaders=%d stat_interval=%d verbose=%d\n",
434 nrealreaders, stat_interval, verbose);
435 fullstop = 0;
437 /* Set up the freelist. */
439 INIT_LIST_HEAD(&rcu_torture_freelist);
440 for (i = 0; i < sizeof(rcu_tortures) / sizeof(rcu_tortures[0]); i++) {
441 rcu_tortures[i].rtort_mbtest = 0;
442 list_add_tail(&rcu_tortures[i].rtort_free,
443 &rcu_torture_freelist);
446 /* Initialize the statistics so that each run gets its own numbers. */
448 rcu_torture_current = NULL;
449 rcu_torture_current_version = 0;
450 atomic_set(&n_rcu_torture_alloc, 0);
451 atomic_set(&n_rcu_torture_alloc_fail, 0);
452 atomic_set(&n_rcu_torture_free, 0);
453 atomic_set(&n_rcu_torture_mberror, 0);
454 atomic_set(&n_rcu_torture_error, 0);
455 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
456 atomic_set(&rcu_torture_wcount[i], 0);
457 for_each_cpu(cpu) {
458 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
459 per_cpu(rcu_torture_count, cpu)[i] = 0;
460 per_cpu(rcu_torture_batch, cpu)[i] = 0;
464 /* Start up the kthreads. */
466 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
467 writer_task = kthread_run(rcu_torture_writer, NULL,
468 "rcu_torture_writer");
469 if (IS_ERR(writer_task)) {
470 firsterr = PTR_ERR(writer_task);
471 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
472 writer_task = NULL;
473 goto unwind;
475 reader_tasks = kmalloc(nrealreaders * sizeof(reader_tasks[0]),
476 GFP_KERNEL);
477 if (reader_tasks == NULL) {
478 VERBOSE_PRINTK_ERRSTRING("out of memory");
479 firsterr = -ENOMEM;
480 goto unwind;
482 for (i = 0; i < nrealreaders; i++) {
483 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
484 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
485 "rcu_torture_reader");
486 if (IS_ERR(reader_tasks[i])) {
487 firsterr = PTR_ERR(reader_tasks[i]);
488 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
489 reader_tasks[i] = NULL;
490 goto unwind;
493 if (stat_interval > 0) {
494 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
495 stats_task = kthread_run(rcu_torture_stats, NULL,
496 "rcu_torture_stats");
497 if (IS_ERR(stats_task)) {
498 firsterr = PTR_ERR(stats_task);
499 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
500 stats_task = NULL;
501 goto unwind;
504 return 0;
506 unwind:
507 rcu_torture_cleanup();
508 return firsterr;
511 module_init(rcu_torture_init);
512 module_exit(rcu_torture_cleanup);