From patchwork Thu Feb 11 12:01:12 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 61731 Delivered-To: patch@linaro.org Received: by 10.112.43.199 with SMTP id y7csp145960lbl; Thu, 11 Feb 2016 04:01:47 -0800 (PST) X-Received: by 10.66.102.70 with SMTP id fm6mr66149341pab.80.1455192106044; Thu, 11 Feb 2016 04:01:46 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id eg4si12304826pac.40.2016.02.11.04.01.45; Thu, 11 Feb 2016 04:01:46 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dkim=pass header.i=@linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752418AbcBKMBj (ORCPT + 30 others); Thu, 11 Feb 2016 07:01:39 -0500 Received: from mail-pa0-f54.google.com ([209.85.220.54]:35660 "EHLO mail-pa0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752146AbcBKMBd (ORCPT ); Thu, 11 Feb 2016 07:01:33 -0500 Received: by mail-pa0-f54.google.com with SMTP id ho8so28078803pac.2 for ; Thu, 11 Feb 2016 04:01:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=pmxngDQ8pjE9DYRMP5zS6IkqLVhJUaapfXbtco/S+Vk=; b=EjENiYNGm8Xa1koHzwyeAGc48uC22cvkozfvwO54D2hDHP4cQqDtWuCdcivNgnDxuG 1HLczUBx60Vi9EcAie5mSCZuh/EJGAJBZ3HRy59O6MIoWd+RREvsu3OdYkjVQAMBXZ7j 5H9q/7KmRRWU2r694bO133/jUKs9pHDA7QWHw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=pmxngDQ8pjE9DYRMP5zS6IkqLVhJUaapfXbtco/S+Vk=; b=j8jMowBhqmsf4azP+Hy4unSel1eHdR7dHSGgX0xfDlpaImgk87x/FYQYocGEDMlBrI 3eJ3P5swij2cNc4OXAn8PpUnwxW8dXa9yx1ovAkiCDPkPbF4VvkgEY0FMIDrjR2e8L+M ELK/oZv/5Eqb+wlc5tXZlpb0xNZhMXuuKTGyWjxz1g4nXeOCcqIEWAw/LoiBsVvnzIN2 A9VMPgPQ7xcyIg8xH1hxyAb3fszW2pJLiviBzkDcDFX/NAYRjsCEL33JVcdeW5jKQ5/4 SkSeapDpxB/fin9LPYIcdZcffmbnM+sfHvs2PNhXJpQ6Qb5sDDxZX2sUOeunQ4TbUL+3 sAbg== X-Gm-Message-State: AG10YORzYmE31CMk3MFMAUswp+CHET8RjqJXh0IsfqnMLP2jkLxeyK9ssWcEH18sOVgM4FLz X-Received: by 10.66.158.129 with SMTP id wu1mr66329487pab.146.1455192092953; Thu, 11 Feb 2016 04:01:32 -0800 (PST) Received: from localhost ([122.172.89.184]) by smtp.gmail.com with ESMTPSA id dg1sm11963826pad.18.2016.02.11.04.01.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Feb 2016 04:01:32 -0800 (PST) From: Viresh Kumar To: Rafael Wysocki , juri.lelli@arm.com Cc: linaro-kernel@lists.linaro.org, linux-pm@vger.kernel.org, shilpa.bhat@linux.vnet.ibm.com, linux-kernel@vger.kernel.org, Viresh Kumar Subject: [PATCH V5 2/6] cpufreq: Call __cpufreq_governor() with policy->rwsem held Date: Thu, 11 Feb 2016 17:31:12 +0530 Message-Id: <3878a871586ae97a9f28fed77b4e5e790f19ee8a.1455191663.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.7.1.370.gb2aa7f8 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The cpufreq core code is not consistent with respect to invoking __cpufreq_governor() under policy->rwsem. Changing all code to always hold policy->rwsem around __cpufreq_governor() invocations will allow us to remove cpufreq_governor_lock, that is used today because we can't guarantee that __cpufreq_governor() isn't executed in parallel. We should also ensure that the lock is held across state changes to the governors. For example, while adding a CPU to the policy on cpu-online path, we need to stop the governor, change policy->cpus, start the governor and then refresh its limits. The complete sequence must be guaranteed to execute without any concurrent races. And that can be achieved using policy->rwsem around these use cases. Also note that, after this patch cpufreq_driver->stop_cpu() and ->exit() will get called while policy->rwsem is held, which wasn't the case earlier. That shouldn't have any side effects though. Signed-off-by: Viresh Kumar Tested-by: Juri Lelli Tested-by: Shilpasri G Bhat --- drivers/cpufreq/cpufreq.c | 49 +++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) -- 2.7.1.370.gb2aa7f8 diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index 863ac26c4ecf..51fb47cd38a0 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -1048,30 +1048,29 @@ static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cp if (cpumask_test_cpu(cpu, policy->cpus)) return 0; + down_write(&policy->rwsem); if (has_target()) { ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP); if (ret) { pr_err("%s: Failed to stop governor\n", __func__); - return ret; + goto unlock; } } - down_write(&policy->rwsem); cpumask_set_cpu(cpu, policy->cpus); - up_write(&policy->rwsem); if (has_target()) { ret = __cpufreq_governor(policy, CPUFREQ_GOV_START); if (!ret) ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); - if (ret) { + if (ret) pr_err("%s: Failed to start governor\n", __func__); - return ret; - } } - return 0; +unlock: + up_write(&policy->rwsem); + return ret; } static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) @@ -1374,13 +1373,13 @@ static void cpufreq_offline(unsigned int cpu) return; } + down_write(&policy->rwsem); if (has_target()) { ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP); if (ret) pr_err("%s: Failed to stop governor\n", __func__); } - down_write(&policy->rwsem); cpumask_clear_cpu(cpu, policy->cpus); if (policy_is_inactive(policy)) { @@ -1393,7 +1392,6 @@ static void cpufreq_offline(unsigned int cpu) /* Nominate new CPU */ policy->cpu = cpumask_any(policy->cpus); } - up_write(&policy->rwsem); /* Start governor again for active policy */ if (!policy_is_inactive(policy)) { @@ -1406,7 +1404,7 @@ static void cpufreq_offline(unsigned int cpu) pr_err("%s: Failed to start governor\n", __func__); } - return; + goto unlock; } if (cpufreq_driver->stop_cpu) @@ -1428,6 +1426,9 @@ static void cpufreq_offline(unsigned int cpu) cpufreq_driver->exit(policy); policy->freq_table = NULL; } + +unlock: + up_write(&policy->rwsem); } /** @@ -1624,6 +1625,7 @@ EXPORT_SYMBOL(cpufreq_generic_suspend); void cpufreq_suspend(void) { struct cpufreq_policy *policy; + int ret; if (!cpufreq_driver) return; @@ -1634,7 +1636,11 @@ void cpufreq_suspend(void) pr_debug("%s: Suspending Governors\n", __func__); for_each_active_policy(policy) { - if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP)) + down_write(&policy->rwsem); + ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP); + up_write(&policy->rwsem); + + if (ret) pr_err("%s: Failed to stop governor for policy: %p\n", __func__, policy); else if (cpufreq_driver->suspend @@ -1656,6 +1662,7 @@ void cpufreq_suspend(void) void cpufreq_resume(void) { struct cpufreq_policy *policy; + int ret; if (!cpufreq_driver) return; @@ -1668,13 +1675,20 @@ void cpufreq_resume(void) pr_debug("%s: Resuming Governors\n", __func__); for_each_active_policy(policy) { - if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) + if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) { pr_err("%s: Failed to resume driver: %p\n", __func__, policy); - else if (__cpufreq_governor(policy, CPUFREQ_GOV_START) - || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS)) - pr_err("%s: Failed to start governor for policy: %p\n", - __func__, policy); + } else { + down_write(&policy->rwsem); + ret = __cpufreq_governor(policy, CPUFREQ_GOV_START); + if (!ret) + __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); + up_write(&policy->rwsem); + + if (ret) + pr_err("%s: Failed to start governor for policy: %p\n", + __func__, policy); + } } /* @@ -2325,8 +2339,11 @@ static int cpufreq_boost_set_sw(int state) __func__); break; } + + down_write(&policy->rwsem); policy->user_policy.max = policy->max; __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); + up_write(&policy->rwsem); } }