aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/cpufreq/cpufreq.c
diff options
context:
space:
mode:
authorAaron Plattner <aplattner@nvidia.com>2014-03-04 15:42:15 -0500
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>2014-03-06 07:25:16 -0500
commit999976e0f6233322a878b0b7148c810544d6c8a8 (patch)
treedf07413e3d9018e7ee7c439bc697c93cb52a61b3 /drivers/cpufreq/cpufreq.c
parent0414855fdc4a40da05221fc6062cccbc0c30f169 (diff)
cpufreq: use cpufreq_cpu_get() to avoid cpufreq_get() race conditions
If a module calls cpufreq_get while cpufreq is initializing, it's possible for it to be called after cpufreq_driver is set but before cpufreq_cpu_data is written during subsys_interface_register. This happens because cpufreq_get doesn't take the cpufreq_driver_lock around its use of cpufreq_cpu_data. Fix this by using cpufreq_cpu_get(cpu) to look up the policy rather than reading it out of cpufreq_cpu_data directly. cpufreq_cpu_get() takes the appropriate locks to prevent this race from happening. Since it's possible for policy to be NULL if the caller passes in an invalid CPU number or calls the function before cpufreq is initialized, delete the BUG_ON(!policy) and simply return 0. Don't try to return -ENOENT because that's negative and the function returns an unsigned integer. References: https://bbs.archlinux.org/viewtopic.php?id=177934 Signed-off-by: Aaron Plattner <aplattner@nvidia.com> Cc: 3.13+ <stable@vger.kernel.org> # 3.13+ Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Diffstat (limited to 'drivers/cpufreq/cpufreq.c')
-rw-r--r--drivers/cpufreq/cpufreq.c21
1 files changed, 7 insertions, 14 deletions
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index cb003a6b72c8..c4cacabbbb55 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -1546,23 +1546,16 @@ static unsigned int __cpufreq_get(unsigned int cpu)
1546 */ 1546 */
1547unsigned int cpufreq_get(unsigned int cpu) 1547unsigned int cpufreq_get(unsigned int cpu)
1548{ 1548{
1549 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 1549 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1550 unsigned int ret_freq = 0; 1550 unsigned int ret_freq = 0;
1551 1551
1552 if (cpufreq_disabled() || !cpufreq_driver) 1552 if (policy) {
1553 return -ENOENT; 1553 down_read(&policy->rwsem);
1554 1554 ret_freq = __cpufreq_get(cpu);
1555 BUG_ON(!policy); 1555 up_read(&policy->rwsem);
1556
1557 if (!down_read_trylock(&cpufreq_rwsem))
1558 return 0;
1559
1560 down_read(&policy->rwsem);
1561
1562 ret_freq = __cpufreq_get(cpu);
1563 1556
1564 up_read(&policy->rwsem); 1557 cpufreq_cpu_put(policy);
1565 up_read(&cpufreq_rwsem); 1558 }
1566 1559
1567 return ret_freq; 1560 return ret_freq;
1568} 1561}