[PATCH] genirq: cleanup: no_irq_type -> no_irq_chip rename
[safe/jmp/linux-2.6] / kernel / rcutorture.c
index 49fbbef..4d1c3d2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Read-Copy Update /proc-based torture test facility
+ * Read-Copy Update module-based torture test facility
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -39,7 +39,6 @@
 #include <linux/moduleparam.h>
 #include <linux/percpu.h>
 #include <linux/notifier.h>
-#include <linux/rcuref.h>
 #include <linux/cpu.h>
 #include <linux/random.h>
 #include <linux/delay.h>
 MODULE_LICENSE("GPL");
 
 static int nreaders = -1;      /* # reader threads, defaults to 4*ncpus */
-static int stat_interval = 0;  /* Interval between stats, in seconds. */
+static int stat_interval;      /* Interval between stats, in seconds. */
                                /*  Defaults to "only at end of test". */
-static int verbose = 0;                /* Print more debug info. */
+static int verbose;            /* Print more debug info. */
+static int test_no_idle_hz;    /* Test RCU's support for tickless idle CPUs. */
+static int shuffle_interval = 5; /* Interval between shuffles (in sec)*/
+static char *torture_type = "rcu"; /* What to torture. */
 
-MODULE_PARM(nreaders, "i");
+module_param(nreaders, int, 0);
 MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
-MODULE_PARM(stat_interval, "i");
+module_param(stat_interval, int, 0);
 MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
-MODULE_PARM(verbose, "i");
+module_param(verbose, bool, 0);
 MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
-#define TORTURE_FLAG "rcutorture: "
+module_param(test_no_idle_hz, bool, 0);
+MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
+module_param(shuffle_interval, int, 0);
+MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
+module_param(torture_type, charp, 0);
+MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh)");
+
+#define TORTURE_FLAG "-torture:"
 #define PRINTK_STRING(s) \
-       do { printk(KERN_ALERT TORTURE_FLAG s "\n"); } while (0)
+       do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
 #define VERBOSE_PRINTK_STRING(s) \
-       do { if (verbose) printk(KERN_ALERT TORTURE_FLAG s "\n"); } while (0)
+       do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
 #define VERBOSE_PRINTK_ERRSTRING(s) \
-       do { if (verbose) printk(KERN_ALERT TORTURE_FLAG "!!! " s "\n"); } while (0)
+       do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
 
 static char printk_buf[4096];
 
@@ -73,6 +82,7 @@ static int nrealreaders;
 static struct task_struct *writer_task;
 static struct task_struct **reader_tasks;
 static struct task_struct *stats_task;
+static struct task_struct *shuffler_task;
 
 #define RCU_TORTURE_PIPE_LEN 10
 
@@ -103,21 +113,21 @@ atomic_t n_rcu_torture_error;
 /*
  * Allocate an element from the rcu_tortures pool.
  */
-struct rcu_torture *
+static struct rcu_torture *
 rcu_torture_alloc(void)
 {
        struct list_head *p;
 
-       spin_lock(&rcu_torture_lock);
+       spin_lock_bh(&rcu_torture_lock);
        if (list_empty(&rcu_torture_freelist)) {
                atomic_inc(&n_rcu_torture_alloc_fail);
-               spin_unlock(&rcu_torture_lock);
+               spin_unlock_bh(&rcu_torture_lock);
                return NULL;
        }
        atomic_inc(&n_rcu_torture_alloc);
        p = rcu_torture_freelist.next;
        list_del_init(p);
-       spin_unlock(&rcu_torture_lock);
+       spin_unlock_bh(&rcu_torture_lock);
        return container_of(p, struct rcu_torture, rtort_free);
 }
 
@@ -128,31 +138,9 @@ static void
 rcu_torture_free(struct rcu_torture *p)
 {
        atomic_inc(&n_rcu_torture_free);
-       spin_lock(&rcu_torture_lock);
+       spin_lock_bh(&rcu_torture_lock);
        list_add_tail(&p->rtort_free, &rcu_torture_freelist);
-       spin_unlock(&rcu_torture_lock);
-}
-
-static void
-rcu_torture_cb(struct rcu_head *p)
-{
-       int i;
-       struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
-
-       if (fullstop) {
-               /* Test is ending, just drop callbacks on the floor. */
-               /* The next initialization will pick up the pieces. */
-               return;
-       }
-       i = rp->rtort_pipe_count;
-       if (i > RCU_TORTURE_PIPE_LEN)
-               i = RCU_TORTURE_PIPE_LEN;
-       atomic_inc(&rcu_torture_wcount[i]);
-       if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
-               rp->rtort_mbtest = 0;
-               rcu_torture_free(rp);
-       } else
-               call_rcu(p, rcu_torture_cb);
+       spin_unlock_bh(&rcu_torture_lock);
 }
 
 struct rcu_random_state {
@@ -185,6 +173,119 @@ rcu_random(struct rcu_random_state *rrsp)
 }
 
 /*
+ * Operations vector for selecting different types of tests.
+ */
+
+struct rcu_torture_ops {
+       void (*init)(void);
+       void (*cleanup)(void);
+       int (*readlock)(void);
+       void (*readunlock)(int idx);
+       int (*completed)(void);
+       void (*deferredfree)(struct rcu_torture *p);
+       int (*stats)(char *page);
+       char *name;
+};
+static struct rcu_torture_ops *cur_ops = NULL;
+
+/*
+ * Definitions for rcu torture testing.
+ */
+
+static int rcu_torture_read_lock(void)
+{
+       rcu_read_lock();
+       return 0;
+}
+
+static void rcu_torture_read_unlock(int idx)
+{
+       rcu_read_unlock();
+}
+
+static int rcu_torture_completed(void)
+{
+       return rcu_batches_completed();
+}
+
+static void
+rcu_torture_cb(struct rcu_head *p)
+{
+       int i;
+       struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
+
+       if (fullstop) {
+               /* Test is ending, just drop callbacks on the floor. */
+               /* The next initialization will pick up the pieces. */
+               return;
+       }
+       i = rp->rtort_pipe_count;
+       if (i > RCU_TORTURE_PIPE_LEN)
+               i = RCU_TORTURE_PIPE_LEN;
+       atomic_inc(&rcu_torture_wcount[i]);
+       if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
+               rp->rtort_mbtest = 0;
+               rcu_torture_free(rp);
+       } else
+               cur_ops->deferredfree(rp);
+}
+
+static void rcu_torture_deferred_free(struct rcu_torture *p)
+{
+       call_rcu(&p->rtort_rcu, rcu_torture_cb);
+}
+
+static struct rcu_torture_ops rcu_ops = {
+       .init = NULL,
+       .cleanup = NULL,
+       .readlock = rcu_torture_read_lock,
+       .readunlock = rcu_torture_read_unlock,
+       .completed = rcu_torture_completed,
+       .deferredfree = rcu_torture_deferred_free,
+       .stats = NULL,
+       .name = "rcu"
+};
+
+/*
+ * Definitions for rcu_bh torture testing.
+ */
+
+static int rcu_bh_torture_read_lock(void)
+{
+       rcu_read_lock_bh();
+       return 0;
+}
+
+static void rcu_bh_torture_read_unlock(int idx)
+{
+       rcu_read_unlock_bh();
+}
+
+static int rcu_bh_torture_completed(void)
+{
+       return rcu_batches_completed_bh();
+}
+
+static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
+{
+       call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
+}
+
+static struct rcu_torture_ops rcu_bh_ops = {
+       .init = NULL,
+       .cleanup = NULL,
+       .readlock = rcu_bh_torture_read_lock,
+       .readunlock = rcu_bh_torture_read_unlock,
+       .completed = rcu_bh_torture_completed,
+       .deferredfree = rcu_bh_torture_deferred_free,
+       .stats = NULL,
+       .name = "rcu_bh"
+};
+
+static struct rcu_torture_ops *torture_ops[] =
+       { &rcu_ops, &rcu_bh_ops, NULL };
+
+/*
  * RCU torture writer kthread.  Repeatedly substitutes a new structure
  * for that pointed to by rcu_torture_current, freeing the old structure
  * after a series of grace periods (the "pipeline").
@@ -203,8 +304,6 @@ rcu_torture_writer(void *arg)
 
        do {
                schedule_timeout_uninterruptible(1);
-               if (rcu_batches_completed() == oldbatch)
-                       continue;
                if ((rp = rcu_torture_alloc()) == NULL)
                        continue;
                rp->rtort_pipe_count = 0;
@@ -219,10 +318,10 @@ rcu_torture_writer(void *arg)
                                i = RCU_TORTURE_PIPE_LEN;
                        atomic_inc(&rcu_torture_wcount[i]);
                        old_rp->rtort_pipe_count++;
-                       call_rcu(&old_rp->rtort_rcu, rcu_torture_cb);
+                       cur_ops->deferredfree(old_rp);
                }
                rcu_torture_current_version++;
-               oldbatch = rcu_batches_completed();
+               oldbatch = cur_ops->completed();
        } while (!kthread_should_stop() && !fullstop);
        VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
        while (!kthread_should_stop())
@@ -240,6 +339,7 @@ static int
 rcu_torture_reader(void *arg)
 {
        int completed;
+       int idx;
        DEFINE_RCU_RANDOM(rand);
        struct rcu_torture *p;
        int pipe_count;
@@ -248,12 +348,12 @@ rcu_torture_reader(void *arg)
        set_user_nice(current, 19);
 
        do {
-               rcu_read_lock();
-               completed = rcu_batches_completed();
+               idx = cur_ops->readlock();
+               completed = cur_ops->completed();
                p = rcu_dereference(rcu_torture_current);
                if (p == NULL) {
                        /* Wait for rcu_torture_writer to get underway */
-                       rcu_read_unlock();
+                       cur_ops->readunlock(idx);
                        schedule_timeout_interruptible(HZ);
                        continue;
                }
@@ -267,14 +367,14 @@ rcu_torture_reader(void *arg)
                        pipe_count = RCU_TORTURE_PIPE_LEN;
                }
                ++__get_cpu_var(rcu_torture_count)[pipe_count];
-               completed = rcu_batches_completed() - completed;
+               completed = cur_ops->completed() - completed;
                if (completed > RCU_TORTURE_PIPE_LEN) {
                        /* Should not happen, but... */
                        completed = RCU_TORTURE_PIPE_LEN;
                }
                ++__get_cpu_var(rcu_torture_batch)[completed];
                preempt_enable();
-               rcu_read_unlock();
+               cur_ops->readunlock(idx);
                schedule();
        } while (!kthread_should_stop() && !fullstop);
        VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
@@ -295,7 +395,7 @@ rcu_torture_printk(char *page)
        long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
        long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
 
-       for_each_cpu(cpu) {
+       for_each_possible_cpu(cpu) {
                for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
                        pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
                        batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
@@ -305,7 +405,7 @@ rcu_torture_printk(char *page)
                if (pipesummary[i] != 0)
                        break;
        }
-       cnt += sprintf(&page[cnt], "rcutorture: ");
+       cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
        cnt += sprintf(&page[cnt],
                       "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
                       "rtmbe: %d",
@@ -318,7 +418,7 @@ rcu_torture_printk(char *page)
                       atomic_read(&n_rcu_torture_mberror));
        if (atomic_read(&n_rcu_torture_mberror) != 0)
                cnt += sprintf(&page[cnt], " !!!");
-       cnt += sprintf(&page[cnt], "\nrcutorture: ");
+       cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
        if (i > 1) {
                cnt += sprintf(&page[cnt], "!!! ");
                atomic_inc(&n_rcu_torture_error);
@@ -326,17 +426,19 @@ rcu_torture_printk(char *page)
        cnt += sprintf(&page[cnt], "Reader Pipe: ");
        for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
                cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
-       cnt += sprintf(&page[cnt], "\nrcutorture: ");
+       cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
        cnt += sprintf(&page[cnt], "Reader Batch: ");
-       for (i = 0; i < RCU_TORTURE_PIPE_LEN; i++)
+       for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
                cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
-       cnt += sprintf(&page[cnt], "\nrcutorture: ");
+       cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
        cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
        for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
                cnt += sprintf(&page[cnt], " %d",
                               atomic_read(&rcu_torture_wcount[i]));
        }
        cnt += sprintf(&page[cnt], "\n");
+       if (cur_ops->stats != NULL)
+               cnt += cur_ops->stats(&page[cnt]);
        return cnt;
 }
 
@@ -376,12 +478,87 @@ rcu_torture_stats(void *arg)
        return 0;
 }
 
+static int rcu_idle_cpu;       /* Force all torture tasks off this CPU */
+
+/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
+ * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
+ */
+void rcu_torture_shuffle_tasks(void)
+{
+       cpumask_t tmp_mask = CPU_MASK_ALL;
+       int i;
+
+       lock_cpu_hotplug();
+
+       /* No point in shuffling if there is only one online CPU (ex: UP) */
+       if (num_online_cpus() == 1) {
+               unlock_cpu_hotplug();
+               return;
+       }
+
+       if (rcu_idle_cpu != -1)
+               cpu_clear(rcu_idle_cpu, tmp_mask);
+
+       set_cpus_allowed(current, tmp_mask);
+
+       if (reader_tasks != NULL) {
+               for (i = 0; i < nrealreaders; i++)
+                       if (reader_tasks[i])
+                               set_cpus_allowed(reader_tasks[i], tmp_mask);
+       }
+
+       if (writer_task)
+               set_cpus_allowed(writer_task, tmp_mask);
+
+       if (stats_task)
+               set_cpus_allowed(stats_task, tmp_mask);
+
+       if (rcu_idle_cpu == -1)
+               rcu_idle_cpu = num_online_cpus() - 1;
+       else
+               rcu_idle_cpu--;
+
+       unlock_cpu_hotplug();
+}
+
+/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
+ * system to become idle at a time and cut off its timer ticks. This is meant
+ * to test the support for such tickless idle CPU in RCU.
+ */
+static int
+rcu_torture_shuffle(void *arg)
+{
+       VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
+       do {
+               schedule_timeout_interruptible(shuffle_interval * HZ);
+               rcu_torture_shuffle_tasks();
+       } while (!kthread_should_stop());
+       VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
+       return 0;
+}
+
+static inline void
+rcu_torture_print_module_parms(char *tag)
+{
+       printk(KERN_ALERT "%s" TORTURE_FLAG "--- %s: nreaders=%d "
+               "stat_interval=%d verbose=%d test_no_idle_hz=%d "
+               "shuffle_interval = %d\n",
+               torture_type, tag, nrealreaders, stat_interval, verbose,
+               test_no_idle_hz, shuffle_interval);
+}
+
 static void
 rcu_torture_cleanup(void)
 {
        int i;
 
        fullstop = 1;
+       if (shuffler_task != NULL) {
+               VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
+               kthread_stop(shuffler_task);
+       }
+       shuffler_task = NULL;
+
        if (writer_task != NULL) {
                VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
                kthread_stop(writer_task);
@@ -412,9 +589,13 @@ rcu_torture_cleanup(void)
        rcu_barrier();
 
        rcu_torture_stats_print();  /* -After- the stats thread is stopped! */
-       printk(KERN_ALERT TORTURE_FLAG
-              "--- End of test: %s\n",
-              atomic_read(&n_rcu_torture_error) == 0 ? "SUCCESS" : "FAILURE");
+
+       if (cur_ops->cleanup != NULL)
+               cur_ops->cleanup();
+       if (atomic_read(&n_rcu_torture_error))
+               rcu_torture_print_module_parms("End of test: FAILURE");
+       else
+               rcu_torture_print_module_parms("End of test: SUCCESS");
 }
 
 static int
@@ -426,13 +607,25 @@ rcu_torture_init(void)
 
        /* Process args and tell the world that the torturer is on the job. */
 
+       for (i = 0; cur_ops = torture_ops[i], cur_ops != NULL; i++) {
+               cur_ops = torture_ops[i];
+               if (strcmp(torture_type, cur_ops->name) == 0) {
+                       break;
+               }
+       }
+       if (cur_ops == NULL) {
+               printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
+                      torture_type);
+               return (-EINVAL);
+       }
+       if (cur_ops->init != NULL)
+               cur_ops->init(); /* no "goto unwind" prior to this point!!! */
+
        if (nreaders >= 0)
                nrealreaders = nreaders;
        else
                nrealreaders = 2 * num_online_cpus();
-       printk(KERN_ALERT TORTURE_FLAG
-              "--- Start of test: nreaders=%d stat_interval=%d verbose=%d\n",
-              nrealreaders, stat_interval, verbose);
+       rcu_torture_print_module_parms("Start of test");
        fullstop = 0;
 
        /* Set up the freelist. */
@@ -455,7 +648,7 @@ rcu_torture_init(void)
        atomic_set(&n_rcu_torture_error, 0);
        for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
                atomic_set(&rcu_torture_wcount[i], 0);
-       for_each_cpu(cpu) {
+       for_each_possible_cpu(cpu) {
                for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
                        per_cpu(rcu_torture_count, cpu)[i] = 0;
                        per_cpu(rcu_torture_batch, cpu)[i] = 0;
@@ -502,6 +695,18 @@ rcu_torture_init(void)
                        goto unwind;
                }
        }
+       if (test_no_idle_hz) {
+               rcu_idle_cpu = num_online_cpus() - 1;
+               /* Create the shuffler thread */
+               shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
+                                         "rcu_torture_shuffle");
+               if (IS_ERR(shuffler_task)) {
+                       firsterr = PTR_ERR(shuffler_task);
+                       VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
+                       shuffler_task = NULL;
+                       goto unwind;
+               }
+       }
        return 0;
 
 unwind: