Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp2472399imj; Mon, 18 Feb 2019 06:37:09 -0800 (PST) X-Google-Smtp-Source: AHgI3IbF/edoTBzrb5aZzWuAfFJYGx0A4HpY/o1VD7TPHoOkPyAIpSqd45PDX7IPkCK5uFZKX18Q X-Received: by 2002:a62:6383:: with SMTP id x125mr6418618pfb.239.1550500629489; Mon, 18 Feb 2019 06:37:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550500629; cv=none; d=google.com; s=arc-20160816; b=VCSuWTeXlTwCVa46bZkN/hKAXXlXDpbUkc1ykB0v+VlBWwURNgkzYoo4z3s47UJ9Cy /t25ounyFq3dzrCAsdk3ciJmPwf68d1OKzDfOscEfpx5W5TrlQsL4mwRXm3Z+xw1LZyL kUGc/H61vBsvXtWeY+mL9L0ZYTn8SHRAxnx4T25R2jPtdQ2QgTNjRYr7ChkHAKQp044F ClC5y0vjmvlNiL/tn3b27Qt7Rh03CtclRM5UDeUJzjjJ2R2JkZRiTRky4zd/x9uQMQ06 G5hxQUbVC3PS3dzAFFGRzl/SE/XLJUidRNPHrGcX/dO2pP0duGvo0NLbq0JQaMC/OaeF cYCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=IRW2fEjzT5eYJKB0Qslcfik4ysM5M5SG+hFj2dqISMo=; b=IxrJHb+zmNTYvY95qJqv9eNnIKIgSeh/w1BGTdCW9N/0PUdXx599uWzju7MVBk4XNn b7WKcTooUO9nwTu8yqG0hdytRQlnmpdPbCmgVbbl5YvqDGai4XwWYn2AzY8bJeAx0Ite uOj7pSJMJtfguJD7KKn/Bjpgehvbp8iBxn4+mJCYgyKZghMIwGd/r4H+VHB7wxHvm1Te t/Fb4Ql08FxBzeWdRPf18sqys9/twoZ/ITizzazXSuy7W0j4rhfexIe2PnsmT3HU7G7D r4p1IUJ2l2c94AWsFty8FTGm9Avt9+NbwvrZwXRJopZ728zrsHZe9e1lW/QcGoQ1GwUd 1EaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=JI5vtKAO; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z23si12717636pgu.151.2019.02.18.06.36.53; Mon, 18 Feb 2019 06:37:09 -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; dkim=pass header.i=@kernel.org header.s=default header.b=JI5vtKAO; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389127AbfBROfi (ORCPT + 99 others); Mon, 18 Feb 2019 09:35:38 -0500 Received: from mail.kernel.org ([198.145.29.99]:36420 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387711AbfBRN4O (ORCPT ); Mon, 18 Feb 2019 08:56:14 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id EE5032077B; Mon, 18 Feb 2019 13:56:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1550498173; bh=AHj2qwe4oPT3tGAoTtwE+7JEtaSitJN8WqB141BCrkg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JI5vtKAOiH0HPjt3BnGsDw77KzMZh5gtzpm4RCgnKLjS7h0WIdwLrcrL2xcuumy0R gIu3JAv3vNU0zaSoO1CDHyqkEYMAEUGHIOsGQaZF6/23GGtDTRh85suTakaknm13sJ 6KqJxmPsyNGx2jl0/nt97kjMaHXP8knk2H6CG/ig= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sudeep Holla , Viresh Kumar , "Rafael J. Wysocki" , Sasha Levin Subject: [PATCH 4.14 26/62] cpufreq: check if policy is inactive early in __cpufreq_get() Date: Mon, 18 Feb 2019 14:43:32 +0100 Message-Id: <20190218133508.232660089@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190218133505.801423074@linuxfoundation.org> References: <20190218133505.801423074@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 2f66196208c98b3d1b4294edffb2c5a8197be899 ] cpuinfo_cur_freq gets current CPU frequency as detected by hardware while scaling_cur_freq last known CPU frequency. Some platforms may not allow checking the CPU frequency of an offline CPU or the associated resources may have been released via cpufreq_exit when the CPU gets offlined, in which case the policy would have been invalidated already. If we attempt to get current frequency from the hardware, it may result in hang or crash. For example on Juno, I see: Unable to handle kernel NULL pointer dereference at virtual address 0000000000000188 [0000000000000188] pgd=0000000000000000 Internal error: Oops: 96000004 [#1] PREEMPT SMP Modules linked in: CPU: 5 PID: 4202 Comm: cat Not tainted 4.20.0-08251-ga0f2c0318a15-dirty #87 Hardware name: ARM LTD ARM Juno Development Platform/ARM Juno Development Platform pstate: 40000005 (nZcv daif -PAN -UAO) pc : scmi_cpufreq_get_rate+0x34/0xb0 lr : scmi_cpufreq_get_rate+0x34/0xb0 Call trace: scmi_cpufreq_get_rate+0x34/0xb0 __cpufreq_get+0x34/0xc0 show_cpuinfo_cur_freq+0x24/0x78 show+0x40/0x60 sysfs_kf_seq_show+0xc0/0x148 kernfs_seq_show+0x44/0x50 seq_read+0xd4/0x480 kernfs_fop_read+0x15c/0x208 __vfs_read+0x60/0x188 vfs_read+0x94/0x150 ksys_read+0x6c/0xd8 __arm64_sys_read+0x24/0x30 el0_svc_common+0x78/0x100 el0_svc_handler+0x38/0x78 el0_svc+0x8/0xc ---[ end trace 3d1024e58f77f6b2 ]--- So fix the issue by checking if the policy is invalid early in __cpufreq_get before attempting to get the current frequency. Signed-off-by: Sudeep Holla Acked-by: Viresh Kumar Signed-off-by: Rafael J. Wysocki Signed-off-by: Sasha Levin --- drivers/cpufreq/cpufreq.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index 93754300cb57..66c2790dcc5f 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -1523,17 +1523,16 @@ static unsigned int __cpufreq_get(struct cpufreq_policy *policy) { unsigned int ret_freq = 0; - if (!cpufreq_driver->get) + if (unlikely(policy_is_inactive(policy)) || !cpufreq_driver->get) return ret_freq; ret_freq = cpufreq_driver->get(policy->cpu); /* - * Updating inactive policies is invalid, so avoid doing that. Also - * if fast frequency switching is used with the given policy, the check + * If fast frequency switching is used with the given policy, the check * against policy->cur is pointless, so skip it in that case too. */ - if (unlikely(policy_is_inactive(policy)) || policy->fast_switch_enabled) + if (policy->fast_switch_enabled) return ret_freq; if (ret_freq && policy->cur && @@ -1562,10 +1561,7 @@ unsigned int cpufreq_get(unsigned int cpu) if (policy) { down_read(&policy->rwsem); - - if (!policy_is_inactive(policy)) - ret_freq = __cpufreq_get(policy); - + ret_freq = __cpufreq_get(policy); up_read(&policy->rwsem); cpufreq_cpu_put(policy); -- 2.19.1