cpufreq: governor: Close dbs_data update race condition
authorRafael J. Wysocki <rafael.j.wysocki@intel.com>
Thu, 18 Feb 2016 01:19:00 +0000 (02:19 +0100)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Wed, 9 Mar 2016 13:41:06 +0000 (14:41 +0100)
It is possible for a dbs_data object to be updated after its
usage counter has become 0.  That may happen if governor_store()
runs (via a govenor tunable sysfs attribute write) in parallel
with cpufreq_governor_exit() called for the last cpufreq policy
associated with the dbs_data in question.  In that case, if
governor_store() acquires dbs_data->mutex right after
cpufreq_governor_exit() has released it, the ->store() callback
invoked by it may operate on dbs_data with no users.  Although
sysfs will cause the kobject_put() in cpufreq_governor_exit() to
block until governor_store() has returned, that situation may
lead to some unexpected results, depending on the implementation
of the ->store callback, and therefore it should be avoided.

To that end, modify governor_store() to check the dbs_data's
usage count before invoking the ->store() callback and return
an error if it is 0 at that point.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
drivers/cpufreq/cpufreq_governor.c

index 56dba71d1788f7ec89ee0115eb273e19e856db8c..65ed859030badde065a2147254f05c02e839f270 100644 (file)
@@ -112,7 +112,7 @@ static ssize_t governor_store(struct kobject *kobj, struct attribute *attr,
 
        mutex_lock(&dbs_data->mutex);
 
-       if (gattr->store)
+       if (dbs_data->usage_count && gattr->store)
                ret = gattr->store(dbs_data, buf, count);
 
        mutex_unlock(&dbs_data->mutex);