]> git.karo-electronics.de Git - mv-sheeva.git/blobdiff - kernel/cpu.c
[PATCH] delay accounting: temporarily enable by default
[mv-sheeva.git] / kernel / cpu.c
index d61ba88f34e57b90edae90342ed1db59ebfbb59c..f230f9ae01c239f21ea53e54087310ad4b2a7a0f 100644 (file)
 #include <linux/module.h>
 #include <linux/kthread.h>
 #include <linux/stop_machine.h>
-#include <asm/semaphore.h>
+#include <linux/mutex.h>
 
 /* This protects CPUs going up and down... */
-DECLARE_MUTEX(cpucontrol);
-EXPORT_SYMBOL_GPL(cpucontrol);
-
-static struct notifier_block *cpu_chain;
-
-/*
- * Used to check by callers if they need to acquire the cpucontrol
- * or not to protect a cpu from being removed. Its sometimes required to
- * call these functions both for normal operations, and in response to
- * a cpu being added/removed. If the context of the call is in the same
- * thread context as a CPU hotplug thread, we dont need to take the lock
- * since its already protected
- * check drivers/cpufreq/cpufreq.c for its usage - Ashok Raj
- */
+static DEFINE_MUTEX(cpu_add_remove_lock);
+static DEFINE_MUTEX(cpu_bitmask_lock);
+
+static __cpuinitdata BLOCKING_NOTIFIER_HEAD(cpu_chain);
+
+#ifdef CONFIG_HOTPLUG_CPU
+
+/* Crappy recursive lock-takers in cpufreq! Complain loudly about idiots */
+static struct task_struct *recursive;
+static int recursive_depth;
 
-int current_in_cpu_hotplug(void)
+void lock_cpu_hotplug(void)
 {
-       return (current->flags & PF_HOTPLUG_CPU);
+       struct task_struct *tsk = current;
+
+       if (tsk == recursive) {
+               static int warnings = 10;
+               if (warnings) {
+                       printk(KERN_ERR "Lukewarm IQ detected in hotplug locking\n");
+                       WARN_ON(1);
+                       warnings--;
+               }
+               recursive_depth++;
+               return;
+       }
+       mutex_lock(&cpu_bitmask_lock);
+       recursive = tsk;
 }
+EXPORT_SYMBOL_GPL(lock_cpu_hotplug);
 
-EXPORT_SYMBOL_GPL(current_in_cpu_hotplug);
+void unlock_cpu_hotplug(void)
+{
+       WARN_ON(recursive != current);
+       if (recursive_depth) {
+               recursive_depth--;
+               return;
+       }
+       mutex_unlock(&cpu_bitmask_lock);
+       recursive = NULL;
+}
+EXPORT_SYMBOL_GPL(unlock_cpu_hotplug);
 
+#endif /* CONFIG_HOTPLUG_CPU */
 
 /* Need to know about CPUs going up/down? */
-int register_cpu_notifier(struct notifier_block *nb)
+int __cpuinit register_cpu_notifier(struct notifier_block *nb)
 {
-       int ret;
-
-       if ((ret = down_interruptible(&cpucontrol)) != 0)
-               return ret;
-       ret = notifier_chain_register(&cpu_chain, nb);
-       up(&cpucontrol);
-       return ret;
+       return blocking_notifier_chain_register(&cpu_chain, nb);
 }
+
+#ifdef CONFIG_HOTPLUG_CPU
+
 EXPORT_SYMBOL(register_cpu_notifier);
 
 void unregister_cpu_notifier(struct notifier_block *nb)
 {
-       down(&cpucontrol);
-       notifier_chain_unregister(&cpu_chain, nb);
-       up(&cpucontrol);
+       blocking_notifier_chain_unregister(&cpu_chain, nb);
 }
 EXPORT_SYMBOL(unregister_cpu_notifier);
 
-#ifdef CONFIG_HOTPLUG_CPU
 static inline void check_for_tasks(int cpu)
 {
        struct task_struct *p;
@@ -99,9 +114,7 @@ int cpu_down(unsigned int cpu)
        struct task_struct *p;
        cpumask_t old_allowed, tmp;
 
-       if ((err = lock_cpu_hotplug_interruptible()) != 0)
-               return err;
-
+       mutex_lock(&cpu_add_remove_lock);
        if (num_online_cpus() == 1) {
                err = -EBUSY;
                goto out;
@@ -112,14 +125,7 @@ int cpu_down(unsigned int cpu)
                goto out;
        }
 
-       /*
-        * Leave a trace in current->flags indicating we are already in
-        * process of performing CPU hotplug. Callers can check if cpucontrol
-        * is already acquired by current thread, and if so not cause
-        * a dead lock by not acquiring the lock
-        */
-       current->flags |= PF_HOTPLUG_CPU;
-       err = notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE,
+       err = blocking_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE,
                                                (void *)(long)cpu);
        if (err == NOTIFY_BAD) {
                printk("%s: attempt to take down CPU %u failed\n",
@@ -134,10 +140,13 @@ int cpu_down(unsigned int cpu)
        cpu_clear(cpu, tmp);
        set_cpus_allowed(current, tmp);
 
+       mutex_lock(&cpu_bitmask_lock);
        p = __stop_machine_run(take_cpu_down, NULL, cpu);
+       mutex_unlock(&cpu_bitmask_lock);
+
        if (IS_ERR(p)) {
                /* CPU didn't die: tell everyone.  Can't complain. */
-               if (notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED,
+               if (blocking_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED,
                                (void *)(long)cpu) == NOTIFY_BAD)
                        BUG();
 
@@ -160,8 +169,8 @@ int cpu_down(unsigned int cpu)
        put_cpu();
 
        /* CPU is completely dead: tell everyone.  Too late to complain. */
-       if (notifier_call_chain(&cpu_chain, CPU_DEAD, (void *)(long)cpu)
-           == NOTIFY_BAD)
+       if (blocking_notifier_call_chain(&cpu_chain, CPU_DEAD,
+                       (void *)(long)cpu) == NOTIFY_BAD)
                BUG();
 
        check_for_tasks(cpu);
@@ -171,8 +180,7 @@ out_thread:
 out_allowed:
        set_cpus_allowed(current, old_allowed);
 out:
-       current->flags &= ~PF_HOTPLUG_CPU;
-       unlock_cpu_hotplug();
+       mutex_unlock(&cpu_add_remove_lock);
        return err;
 }
 #endif /*CONFIG_HOTPLUG_CPU*/
@@ -182,20 +190,13 @@ int __devinit cpu_up(unsigned int cpu)
        int ret;
        void *hcpu = (void *)(long)cpu;
 
-       if ((ret = down_interruptible(&cpucontrol)) != 0)
-               return ret;
-
+       mutex_lock(&cpu_add_remove_lock);
        if (cpu_online(cpu) || !cpu_present(cpu)) {
                ret = -EINVAL;
                goto out;
        }
 
-       /*
-        * Leave a trace in current->flags indicating we are already in
-        * process of performing CPU hotplug.
-        */
-       current->flags |= PF_HOTPLUG_CPU;
-       ret = notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
+       ret = blocking_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
        if (ret == NOTIFY_BAD) {
                printk("%s: attempt to bring up CPU %u failed\n",
                                __FUNCTION__, cpu);
@@ -204,20 +205,21 @@ int __devinit cpu_up(unsigned int cpu)
        }
 
        /* Arch-specific enabling code. */
+       mutex_lock(&cpu_bitmask_lock);
        ret = __cpu_up(cpu);
+       mutex_unlock(&cpu_bitmask_lock);
        if (ret != 0)
                goto out_notify;
-       if (!cpu_online(cpu))
-               BUG();
+       BUG_ON(!cpu_online(cpu));
 
        /* Now call notifier in preparation. */
-       notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
+       blocking_notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
 
 out_notify:
        if (ret != 0)
-               notifier_call_chain(&cpu_chain, CPU_UP_CANCELED, hcpu);
+               blocking_notifier_call_chain(&cpu_chain,
+                               CPU_UP_CANCELED, hcpu);
 out:
-       current->flags &= ~PF_HOTPLUG_CPU;
-       up(&cpucontrol);
+       mutex_unlock(&cpu_add_remove_lock);
        return ret;
 }