cpufreq: Properly handle errors from cpufreq_init_policy()
authorViresh Kumar <viresh.kumar@linaro.org>
Wed, 8 Jul 2015 09:42:16 +0000 (15:12 +0530)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Thu, 16 Jul 2015 21:51:26 +0000 (23:51 +0200)
cpufreq_init_policy() can fail, and we don't do anything except a call
to ->exit() on that. The policy should be freed if this happens.

Do it properly.

Reported-and-tested-by: "Jon Medhurst (Tixy)" <tixy@linaro.org>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
drivers/cpufreq/cpufreq.c

index 702777b1d645c482319ab1ef6ad9c21caa23a80a..a7b6ac6e048e9b3a52f684dc1ee086f4cd1c8300 100644 (file)
@@ -1060,11 +1060,10 @@ static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
        return cpufreq_add_dev_symlink(policy);
 }
 
-static void cpufreq_init_policy(struct cpufreq_policy *policy)
+static int cpufreq_init_policy(struct cpufreq_policy *policy)
 {
        struct cpufreq_governor *gov = NULL;
        struct cpufreq_policy new_policy;
-       int ret = 0;
 
        memcpy(&new_policy, policy, sizeof(*policy));
 
@@ -1083,12 +1082,7 @@ static void cpufreq_init_policy(struct cpufreq_policy *policy)
                cpufreq_parse_governor(gov->name, &new_policy.policy, NULL);
 
        /* set default policy */
-       ret = cpufreq_set_policy(policy, &new_policy);
-       if (ret) {
-               pr_debug("setting policy failed\n");
-               if (cpufreq_driver->exit)
-                       cpufreq_driver->exit(policy);
-       }
+       return cpufreq_set_policy(policy, &new_policy);
 }
 
 static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
@@ -1386,7 +1380,12 @@ static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
                write_unlock_irqrestore(&cpufreq_driver_lock, flags);
        }
 
-       cpufreq_init_policy(policy);
+       ret = cpufreq_init_policy(policy);
+       if (ret) {
+               pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
+                      __func__, cpu, ret);
+               goto out_remove_policy_notify;
+       }
 
        if (!recover_policy) {
                policy->user_policy.policy = policy->policy;
@@ -1406,6 +1405,9 @@ static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
 
        return 0;
 
+out_remove_policy_notify:
+       /* cpufreq_policy_free() will notify based on this */
+       recover_policy = true;
 out_exit_policy:
        up_write(&policy->rwsem);