[PATCH] IB/ipath: share more common code between RC and UC protocols
[powerpc.git] / drivers / cpufreq / cpufreq_ondemand.c
index 956d121..693e540 100644 (file)
@@ -21,6 +21,7 @@
 #include <linux/types.h>
 #include <linux/fs.h>
 #include <linux/sysfs.h>
+#include <linux/cpu.h>
 #include <linux/sched.h>
 #include <linux/kmod.h>
 #include <linux/workqueue.h>
@@ -71,9 +72,19 @@ static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
 
 static unsigned int dbs_enable;        /* number of CPUs using this policy */
 
+/*
+ * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
+ * lock and dbs_mutex. cpu_hotplug lock should always be held before
+ * dbs_mutex. If any function that can potentially take cpu_hotplug lock
+ * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
+ * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
+ * is recursive for the same process. -Venki
+ */
 static DEFINE_MUTEX (dbs_mutex);
 static DECLARE_WORK    (dbs_work, do_dbs_timer, NULL);
 
+static struct workqueue_struct *dbs_workq;
+
 struct dbs_tuners {
        unsigned int sampling_rate;
        unsigned int sampling_down_factor;
@@ -361,26 +372,34 @@ static void dbs_check_cpu(int cpu)
 static void do_dbs_timer(void *data)
 {
        int i;
+       lock_cpu_hotplug();
        mutex_lock(&dbs_mutex);
        for_each_online_cpu(i)
                dbs_check_cpu(i);
-       schedule_delayed_work(&dbs_work,
-                       usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
+       queue_delayed_work(dbs_workq, &dbs_work,
+                          usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
        mutex_unlock(&dbs_mutex);
+       unlock_cpu_hotplug();
 }
 
 static inline void dbs_timer_init(void)
 {
        INIT_WORK(&dbs_work, do_dbs_timer, NULL);
-       schedule_delayed_work(&dbs_work,
-                       usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
+       if (!dbs_workq)
+               dbs_workq = create_singlethread_workqueue("ondemand");
+       if (!dbs_workq) {
+               printk(KERN_ERR "ondemand: Cannot initialize kernel thread\n");
+               return;
+       }
+       queue_delayed_work(dbs_workq, &dbs_work,
+                          usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
        return;
 }
 
 static inline void dbs_timer_exit(void)
 {
-       cancel_delayed_work(&dbs_work);
-       return;
+       if (dbs_workq)
+               cancel_rearming_delayed_workqueue(dbs_workq, &dbs_work);
 }
 
 static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
@@ -461,6 +480,7 @@ static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
                break;
 
        case CPUFREQ_GOV_LIMITS:
+               lock_cpu_hotplug();
                mutex_lock(&dbs_mutex);
                if (policy->max < this_dbs_info->cur_policy->cur)
                        __cpufreq_driver_target(
@@ -471,6 +491,7 @@ static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
                                        this_dbs_info->cur_policy,
                                        policy->min, CPUFREQ_RELATION_L);
                mutex_unlock(&dbs_mutex);
+               unlock_cpu_hotplug();
                break;
        }
        return 0;
@@ -489,8 +510,12 @@ static int __init cpufreq_gov_dbs_init(void)
 
 static void __exit cpufreq_gov_dbs_exit(void)
 {
-       /* Make sure that the scheduled work is indeed not running */
-       flush_scheduled_work();
+       /* Make sure that the scheduled work is indeed not running.
+          Assumes the timer has been cancelled first. */
+       if (dbs_workq) {
+               flush_workqueue(dbs_workq);
+               destroy_workqueue(dbs_workq);
+       }
 
        cpufreq_unregister_governor(&cpufreq_gov_dbs);
 }