[PATCH] VM: fix zone list restart in page allocatate
[linux-2.6.22.y-op.git] / kernel / rcutorture.c
blobeb6719c50b4e0deb4546e7479a42bb82a9db90e4
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/rcuref.h>
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>
49 MODULE_LICENSE("GPL");
51 static int nreaders = -1; /* # reader threads, defaults to 4*ncpus */
52 static int stat_interval = 0; /* Interval between stats, in seconds. */
53 /* Defaults to "only at end of test". */
54 static int verbose = 0; /* Print more debug info. */
56 MODULE_PARM(nreaders, "i");
57 MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
58 MODULE_PARM(stat_interval, "i");
59 MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
60 MODULE_PARM(verbose, "i");
61 MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
62 #define TORTURE_FLAG "rcutorture: "
63 #define PRINTK_STRING(s) \
64 do { printk(KERN_ALERT TORTURE_FLAG s "\n"); } while (0)
65 #define VERBOSE_PRINTK_STRING(s) \
66 do { if (verbose) printk(KERN_ALERT TORTURE_FLAG s "\n"); } while (0)
67 #define VERBOSE_PRINTK_ERRSTRING(s) \
68 do { if (verbose) printk(KERN_ALERT TORTURE_FLAG "!!! " s "\n"); } while (0)
70 static char printk_buf[4096];
72 static int nrealreaders;
73 static struct task_struct *writer_task;
74 static struct task_struct **reader_tasks;
75 static struct task_struct *stats_task;
77 #define RCU_TORTURE_PIPE_LEN 10
79 struct rcu_torture {
80 struct rcu_head rtort_rcu;
81 int rtort_pipe_count;
82 struct list_head rtort_free;
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;
101 * Allocate an element from the rcu_tortures pool.
103 struct rcu_torture *
104 rcu_torture_alloc(void)
106 struct list_head *p;
108 spin_lock(&rcu_torture_lock);
109 if (list_empty(&rcu_torture_freelist)) {
110 atomic_inc(&n_rcu_torture_alloc_fail);
111 spin_unlock(&rcu_torture_lock);
112 return NULL;
114 atomic_inc(&n_rcu_torture_alloc);
115 p = rcu_torture_freelist.next;
116 list_del_init(p);
117 spin_unlock(&rcu_torture_lock);
118 return container_of(p, struct rcu_torture, rtort_free);
122 * Free an element to the rcu_tortures pool.
124 static void
125 rcu_torture_free(struct rcu_torture *p)
127 atomic_inc(&n_rcu_torture_free);
128 spin_lock(&rcu_torture_lock);
129 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
130 spin_unlock(&rcu_torture_lock);
133 static void
134 rcu_torture_cb(struct rcu_head *p)
136 int i;
137 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
139 if (fullstop) {
140 /* Test is ending, just drop callbacks on the floor. */
141 /* The next initialization will pick up the pieces. */
142 return;
144 i = rp->rtort_pipe_count;
145 if (i > RCU_TORTURE_PIPE_LEN)
146 i = RCU_TORTURE_PIPE_LEN;
147 atomic_inc(&rcu_torture_wcount[i]);
148 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN)
149 rcu_torture_free(rp);
150 else
151 call_rcu(p, rcu_torture_cb);
154 struct rcu_random_state {
155 unsigned long rrs_state;
156 unsigned long rrs_count;
159 #define RCU_RANDOM_MULT 39916801 /* prime */
160 #define RCU_RANDOM_ADD 479001701 /* prime */
161 #define RCU_RANDOM_REFRESH 10000
163 #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
166 * Crude but fast random-number generator. Uses a linear congruential
167 * generator, with occasional help from get_random_bytes().
169 static long
170 rcu_random(struct rcu_random_state *rrsp)
172 long refresh;
174 if (--rrsp->rrs_count < 0) {
175 get_random_bytes(&refresh, sizeof(refresh));
176 rrsp->rrs_state += refresh;
177 rrsp->rrs_count = RCU_RANDOM_REFRESH;
179 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
180 return swahw32(rrsp->rrs_state);
184 * RCU torture writer kthread. Repeatedly substitutes a new structure
185 * for that pointed to by rcu_torture_current, freeing the old structure
186 * after a series of grace periods (the "pipeline").
188 static int
189 rcu_torture_writer(void *arg)
191 int i;
192 long oldbatch = rcu_batches_completed();
193 struct rcu_torture *rp;
194 struct rcu_torture *old_rp;
195 static DEFINE_RCU_RANDOM(rand);
197 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
198 set_user_nice(current, 19);
200 do {
201 schedule_timeout_uninterruptible(1);
202 if (rcu_batches_completed() == oldbatch)
203 continue;
204 if ((rp = rcu_torture_alloc()) == NULL)
205 continue;
206 rp->rtort_pipe_count = 0;
207 udelay(rcu_random(&rand) & 0x3ff);
208 old_rp = rcu_torture_current;
209 rcu_assign_pointer(rcu_torture_current, rp);
210 smp_wmb();
211 if (old_rp != NULL) {
212 i = old_rp->rtort_pipe_count;
213 if (i > RCU_TORTURE_PIPE_LEN)
214 i = RCU_TORTURE_PIPE_LEN;
215 atomic_inc(&rcu_torture_wcount[i]);
216 old_rp->rtort_pipe_count++;
217 call_rcu(&old_rp->rtort_rcu, rcu_torture_cb);
219 rcu_torture_current_version++;
220 oldbatch = rcu_batches_completed();
221 } while (!kthread_should_stop() && !fullstop);
222 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
223 while (!kthread_should_stop())
224 schedule_timeout_uninterruptible(1);
225 return 0;
229 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
230 * incrementing the corresponding element of the pipeline array. The
231 * counter in the element should never be greater than 1, otherwise, the
232 * RCU implementation is broken.
234 static int
235 rcu_torture_reader(void *arg)
237 int completed;
238 DEFINE_RCU_RANDOM(rand);
239 struct rcu_torture *p;
240 int pipe_count;
242 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
243 set_user_nice(current, 19);
245 do {
246 rcu_read_lock();
247 completed = rcu_batches_completed();
248 p = rcu_dereference(rcu_torture_current);
249 if (p == NULL) {
250 /* Wait for rcu_torture_writer to get underway */
251 rcu_read_unlock();
252 schedule_timeout_interruptible(HZ);
253 continue;
255 udelay(rcu_random(&rand) & 0x7f);
256 preempt_disable();
257 pipe_count = p->rtort_pipe_count;
258 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
259 /* Should not happen, but... */
260 pipe_count = RCU_TORTURE_PIPE_LEN;
262 ++__get_cpu_var(rcu_torture_count)[pipe_count];
263 completed = rcu_batches_completed() - completed;
264 if (completed > RCU_TORTURE_PIPE_LEN) {
265 /* Should not happen, but... */
266 completed = RCU_TORTURE_PIPE_LEN;
268 ++__get_cpu_var(rcu_torture_batch)[completed];
269 preempt_enable();
270 rcu_read_unlock();
271 schedule();
272 } while (!kthread_should_stop() && !fullstop);
273 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
274 while (!kthread_should_stop())
275 schedule_timeout_uninterruptible(1);
276 return 0;
280 * Create an RCU-torture statistics message in the specified buffer.
282 static int
283 rcu_torture_printk(char *page)
285 int cnt = 0;
286 int cpu;
287 int i;
288 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
289 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
291 for_each_cpu(cpu) {
292 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
293 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
294 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
297 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
298 if (pipesummary[i] != 0)
299 break;
301 cnt += sprintf(&page[cnt], "rcutorture: ");
302 cnt += sprintf(&page[cnt],
303 "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d",
304 rcu_torture_current,
305 rcu_torture_current_version,
306 list_empty(&rcu_torture_freelist),
307 atomic_read(&n_rcu_torture_alloc),
308 atomic_read(&n_rcu_torture_alloc_fail),
309 atomic_read(&n_rcu_torture_free));
310 cnt += sprintf(&page[cnt], "\nrcutorture: ");
311 if (i > 1)
312 cnt += sprintf(&page[cnt], "!!! ");
313 cnt += sprintf(&page[cnt], "Reader Pipe: ");
314 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
315 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
316 cnt += sprintf(&page[cnt], "\nrcutorture: ");
317 cnt += sprintf(&page[cnt], "Reader Batch: ");
318 for (i = 0; i < RCU_TORTURE_PIPE_LEN; i++)
319 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
320 cnt += sprintf(&page[cnt], "\nrcutorture: ");
321 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
322 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
323 cnt += sprintf(&page[cnt], " %d",
324 atomic_read(&rcu_torture_wcount[i]));
326 cnt += sprintf(&page[cnt], "\n");
327 return cnt;
331 * Print torture statistics. Caller must ensure that there is only
332 * one call to this function at a given time!!! This is normally
333 * accomplished by relying on the module system to only have one copy
334 * of the module loaded, and then by giving the rcu_torture_stats
335 * kthread full control (or the init/cleanup functions when rcu_torture_stats
336 * thread is not running).
338 static void
339 rcu_torture_stats_print(void)
341 int cnt;
343 cnt = rcu_torture_printk(printk_buf);
344 printk(KERN_ALERT "%s", printk_buf);
348 * Periodically prints torture statistics, if periodic statistics printing
349 * was specified via the stat_interval module parameter.
351 * No need to worry about fullstop here, since this one doesn't reference
352 * volatile state or register callbacks.
354 static int
355 rcu_torture_stats(void *arg)
357 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
358 do {
359 schedule_timeout_interruptible(stat_interval * HZ);
360 rcu_torture_stats_print();
361 } while (!kthread_should_stop());
362 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
363 return 0;
366 static void
367 rcu_torture_cleanup(void)
369 int i;
371 fullstop = 1;
372 if (writer_task != NULL) {
373 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
374 kthread_stop(writer_task);
376 writer_task = NULL;
378 if (reader_tasks != NULL) {
379 for (i = 0; i < nrealreaders; i++) {
380 if (reader_tasks[i] != NULL) {
381 VERBOSE_PRINTK_STRING(
382 "Stopping rcu_torture_reader task");
383 kthread_stop(reader_tasks[i]);
385 reader_tasks[i] = NULL;
387 kfree(reader_tasks);
388 reader_tasks = NULL;
390 rcu_torture_current = NULL;
392 if (stats_task != NULL) {
393 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
394 kthread_stop(stats_task);
396 stats_task = NULL;
398 /* Wait for all RCU callbacks to fire. */
400 for (i = 0; i < RCU_TORTURE_PIPE_LEN; i++)
401 synchronize_rcu();
402 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
403 PRINTK_STRING("--- End of test");
406 static int
407 rcu_torture_init(void)
409 int i;
410 int cpu;
411 int firsterr = 0;
413 /* Process args and tell the world that the torturer is on the job. */
415 if (nreaders >= 0)
416 nrealreaders = nreaders;
417 else
418 nrealreaders = 2 * num_online_cpus();
419 printk(KERN_ALERT TORTURE_FLAG
420 "--- Start of test: nreaders=%d stat_interval=%d verbose=%d\n",
421 nrealreaders, stat_interval, verbose);
422 fullstop = 0;
424 /* Set up the freelist. */
426 INIT_LIST_HEAD(&rcu_torture_freelist);
427 for (i = 0; i < sizeof(rcu_tortures) / sizeof(rcu_tortures[0]); i++) {
428 list_add_tail(&rcu_tortures[i].rtort_free,
429 &rcu_torture_freelist);
432 /* Initialize the statistics so that each run gets its own numbers. */
434 rcu_torture_current = NULL;
435 rcu_torture_current_version = 0;
436 atomic_set(&n_rcu_torture_alloc, 0);
437 atomic_set(&n_rcu_torture_alloc_fail, 0);
438 atomic_set(&n_rcu_torture_free, 0);
439 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
440 atomic_set(&rcu_torture_wcount[i], 0);
441 for_each_cpu(cpu) {
442 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
443 per_cpu(rcu_torture_count, cpu)[i] = 0;
444 per_cpu(rcu_torture_batch, cpu)[i] = 0;
448 /* Start up the kthreads. */
450 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
451 writer_task = kthread_run(rcu_torture_writer, NULL,
452 "rcu_torture_writer");
453 if (IS_ERR(writer_task)) {
454 firsterr = PTR_ERR(writer_task);
455 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
456 writer_task = NULL;
457 goto unwind;
459 reader_tasks = kmalloc(nrealreaders * sizeof(reader_tasks[0]),
460 GFP_KERNEL);
461 if (reader_tasks == NULL) {
462 VERBOSE_PRINTK_ERRSTRING("out of memory");
463 firsterr = -ENOMEM;
464 goto unwind;
466 for (i = 0; i < nrealreaders; i++) {
467 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
468 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
469 "rcu_torture_reader");
470 if (IS_ERR(reader_tasks[i])) {
471 firsterr = PTR_ERR(reader_tasks[i]);
472 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
473 reader_tasks[i] = NULL;
474 goto unwind;
477 if (stat_interval > 0) {
478 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
479 stats_task = kthread_run(rcu_torture_stats, NULL,
480 "rcu_torture_stats");
481 if (IS_ERR(stats_task)) {
482 firsterr = PTR_ERR(stats_task);
483 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
484 stats_task = NULL;
485 goto unwind;
488 return 0;
490 unwind:
491 rcu_torture_cleanup();
492 return firsterr;
495 module_init(rcu_torture_init);
496 module_exit(rcu_torture_cleanup);