smp: have smp_call_function_single() detect invalid CPUs
[safe/jmp/linux-2.6] / kernel / smp.c
index f77b75c..f362a85 100644 (file)
@@ -8,6 +8,7 @@
 #include <linux/module.h>
 #include <linux/percpu.h>
 #include <linux/rcupdate.h>
+#include <linux/rculist.h>
 #include <linux/smp.h>
 
 static DEFINE_PER_CPU(struct call_single_queue, call_single_queue);
@@ -32,7 +33,7 @@ struct call_single_queue {
        spinlock_t lock;
 };
 
-void __cpuinit init_call_single_data(void)
+static int __cpuinit init_call_single_data(void)
 {
        int i;
 
@@ -42,7 +43,9 @@ void __cpuinit init_call_single_data(void)
                spin_lock_init(&q->lock);
                INIT_LIST_HEAD(&q->list);
        }
+       return 0;
 }
+early_initcall(init_call_single_data);
 
 static void csd_flag_wait(struct call_single_data *data)
 {
@@ -132,7 +135,8 @@ void generic_smp_call_function_interrupt(void)
                         */
                        smp_wmb();
                        data->csd.flags &= ~CSD_FLAG_WAIT;
-               } else
+               }
+               if (data->csd.flags & CSD_FLAG_ALLOC)
                        call_rcu(&data->rcu_head, rcu_free_call_data);
        }
        rcu_read_unlock();
@@ -195,7 +199,6 @@ void generic_smp_call_function_single_interrupt(void)
  * smp_call_function_single - Run a function on a specific CPU
  * @func: The function to run. This must be fast and non-blocking.
  * @info: An arbitrary pointer to pass to the function.
- * @retry: Unused
  * @wait: If true, wait until function has completed on other CPUs.
  *
  * Returns 0 on success, else a negative status code. Note that @wait
@@ -203,12 +206,14 @@ void generic_smp_call_function_single_interrupt(void)
  * we fall back to on-stack allocation.
  */
 int smp_call_function_single(int cpu, void (*func) (void *info), void *info,
-                            int retry, int wait)
+                            int wait)
 {
        struct call_single_data d;
        unsigned long flags;
-       /* prevent preemption and reschedule on another processor */
+       /* prevent preemption and reschedule on another processor,
+          as well as CPU removal */
        int me = get_cpu();
+       int err = 0;
 
        /* Can deadlock when called with interrupts disabled */
        WARN_ON(irqs_disabled());
@@ -217,7 +222,7 @@ int smp_call_function_single(int cpu, void (*func) (void *info), void *info,
                local_irq_save(flags);
                func(info);
                local_irq_restore(flags);
-       } else {
+       } else if ((unsigned)cpu < NR_CPUS && cpu_online(cpu)) {
                struct call_single_data *data = NULL;
 
                if (!wait) {
@@ -233,10 +238,12 @@ int smp_call_function_single(int cpu, void (*func) (void *info), void *info,
                data->func = func;
                data->info = info;
                generic_exec_single(cpu, data);
+       } else {
+               err = -ENXIO;   /* CPU not online */
        }
 
        put_cpu();
-       return 0;
+       return err;
 }
 EXPORT_SYMBOL(smp_call_function_single);
 
@@ -258,6 +265,42 @@ void __smp_call_function_single(int cpu, struct call_single_data *data)
        generic_exec_single(cpu, data);
 }
 
+/* Dummy function */
+static void quiesce_dummy(void *unused)
+{
+}
+
+/*
+ * Ensure stack based data used in call function mask is safe to free.
+ *
+ * This is needed by smp_call_function_mask when using on-stack data, because
+ * a single call function queue is shared by all CPUs, and any CPU may pick up
+ * the data item on the queue at any time before it is deleted. So we need to
+ * ensure that all CPUs have transitioned through a quiescent state after
+ * this call.
+ *
+ * This is a very slow function, implemented by sending synchronous IPIs to
+ * all possible CPUs. For this reason, we have to alloc data rather than use
+ * stack based data even in the case of synchronous calls. The stack based
+ * data is then just used for deadlock/oom fallback which will be very rare.
+ *
+ * If a faster scheme can be made, we could go back to preferring stack based
+ * data -- the data allocation/free is non-zero cost.
+ */
+static void smp_call_function_mask_quiesce_stack(cpumask_t mask)
+{
+       struct call_single_data data;
+       int cpu;
+
+       data.func = quiesce_dummy;
+       data.info = NULL;
+
+       for_each_cpu_mask(cpu, mask) {
+               data.flags = CSD_FLAG_WAIT;
+               generic_exec_single(cpu, &data);
+       }
+}
+
 /**
  * smp_call_function_mask(): Run a function on a set of other CPUs.
  * @mask: The set of cpus to run on.
@@ -283,6 +326,7 @@ int smp_call_function_mask(cpumask_t mask, void (*func)(void *), void *info,
        cpumask_t allbutself;
        unsigned long flags;
        int cpu, num_cpus;
+       int slowpath = 0;
 
        /* Can deadlock when called with interrupts disabled */
        WARN_ON(irqs_disabled());
@@ -301,17 +345,19 @@ int smp_call_function_mask(cpumask_t mask, void (*func)(void *), void *info,
                return 0;
        else if (num_cpus == 1) {
                cpu = first_cpu(mask);
-               return smp_call_function_single(cpu, func, info, 0, wait);
+               return smp_call_function_single(cpu, func, info, wait);
        }
 
-       if (!wait) {
-               data = kmalloc(sizeof(*data), GFP_ATOMIC);
-               if (data)
-                       data->csd.flags = CSD_FLAG_ALLOC;
-       }
-       if (!data) {
+       data = kmalloc(sizeof(*data), GFP_ATOMIC);
+       if (data) {
+               data->csd.flags = CSD_FLAG_ALLOC;
+               if (wait)
+                       data->csd.flags |= CSD_FLAG_WAIT;
+       } else {
                data = &d;
                data->csd.flags = CSD_FLAG_WAIT;
+               wait = 1;
+               slowpath = 1;
        }
 
        spin_lock_init(&data->lock);
@@ -328,8 +374,11 @@ int smp_call_function_mask(cpumask_t mask, void (*func)(void *), void *info,
        arch_send_call_function_ipi(mask);
 
        /* optionally wait for the CPUs to complete */
-       if (wait)
+       if (wait) {
                csd_flag_wait(&data->csd);
+               if (unlikely(slowpath))
+                       smp_call_function_mask_quiesce_stack(mask);
+       }
 
        return 0;
 }
@@ -339,7 +388,6 @@ EXPORT_SYMBOL(smp_call_function_mask);
  * smp_call_function(): Run a function on all other CPUs.
  * @func: The function to run. This must be fast and non-blocking.
  * @info: An arbitrary pointer to pass to the function.
- * @natomic: Unused
  * @wait: If true, wait (atomically) until function has completed on other CPUs.
  *
  * Returns 0 on success, else a negative status code.
@@ -351,7 +399,7 @@ EXPORT_SYMBOL(smp_call_function_mask);
  * You must not call this function with disabled interrupts or from a
  * hardware interrupt handler or from a bottom half handler.
  */
-int smp_call_function(void (*func)(void *), void *info, int natomic, int wait)
+int smp_call_function(void (*func)(void *), void *info, int wait)
 {
        int ret;