Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935392AbcCKInX (ORCPT ); Fri, 11 Mar 2016 03:43:23 -0500 Received: from www.linutronix.de ([62.245.132.108]:36058 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935365AbcCKInO (ORCPT ); Fri, 11 Mar 2016 03:43:14 -0500 From: Richard Cochran To: Cc: , Dirk Brandewie , "Rafael J. Wysocki" , Viresh Kumar , linux-pm@vger.kernel.org Subject: [PATCH v2] cpufreq: Make cpufreq_quick_get() safe to call. Date: Fri, 11 Mar 2016 09:43:07 +0100 Message-Id: <1457685787-6046-1-git-send-email-rcochran@linutronix.de> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1450 Lines: 42 The function, cpufreq_quick_get, accesses the global 'cpufreq_driver' and its fields without taking the associated lock, cpufreq_driver_lock. Without the locking, nothing guarantees that 'cpufreq_driver' remains consistent during the call. This patch fixes the issue by taking the lock before accessing the data structure. Cc: Dirk Brandewie Cc: Rafael J. Wysocki Cc: Viresh Kumar Cc: linux-pm@vger.kernel.org Signed-off-by: Richard Cochran --- drivers/cpufreq/cpufreq.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index e979ec7..053aa1f 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -1457,9 +1457,17 @@ unsigned int cpufreq_quick_get(unsigned int cpu) { struct cpufreq_policy *policy; unsigned int ret_freq = 0; + unsigned long flags; - if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) - return cpufreq_driver->get(cpu); + read_lock_irqsave(&cpufreq_driver_lock, flags); + + if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) { + ret_freq = cpufreq_driver->get(cpu); + read_unlock_irqrestore(&cpufreq_driver_lock, flags); + return ret_freq; + } + + read_unlock_irqrestore(&cpufreq_driver_lock, flags); policy = cpufreq_cpu_get(cpu); if (policy) { -- 2.1.4