Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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