From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796388799; b=v/uQZRZC6S4ar0fEgRDBtGRQ1nSGBQ3+04TXYGSBA0oKNMaUlcp0FcGT0YalV8TMWv9g vOcDgsORGNpG2Yw5reUi324dh6ZQEKZuPFw/5EIgVmz2zRehbq7I6lx24B59E3oQBAuLX 5a51BthmG+V8yj7lkCd0eD4I3XhC4X4/coY7TeUsJTovYfj3pXb3HiTTMLu4RSvELv2uj lVz+PCp6is2IIbjVDwuGWFXERaIS9g4nHBZkoazoATOpRuxC8P10+/hCMVRDct0dvB4G0 PkZkIXP6giv3GQa7dxot+tBptHsnOnFblMD7muagHt5et8yuwg6I/bgIR1qVt+ueEyg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388799; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=AfHkx+n/8NmGTr69S93sBsOnblNsVacplE8U/gRaVis=; b=QOHLPdgtxo4pFLyTSw1OsAdbXyNtGEfJOQQ5HMxHHOgNLWLplM3ctI7f52p28fgOPx3m TBzc8DunKgrTRaJwenveHPDLWIH7YMzkzT0nWaltrchUxFaRGb1ZTyiZpDUXzAGTDqlPr Rvv/SlFgD5lBjuzbY/ngfAVVoblEGpfzuBqvH1SY6P805JF5udirFoC9JcFW32Tv9Q8pv H9pTTogt2ZdNrB/VgJRS1dj0XkV4nx2cvmcCYJfwOY3aOJCpoQ+w8tXCaNQHs6kb0CbRv jVlDhDFC95PCYE3re4j46PHmDONgqxkzReL4JTjqatvci//J0xn195x3k6323IxR9sQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b="lJu/rTnc"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B0816606A32C; Fri, 4 Dec 2026 12:53:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=lJu/rTnc Received: by smtp.kernel.org (Postfix) with ESMTPSA id BD96A1F00897; Fri, 4 Dec 2026 12:53:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796388799; bh=AfHkx+n/8NmGTr69S93sBsOnblNsVacplE8U/gRaVis=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=lJu/rTnca5CvqWUO53Izfm4woD5wF6e9AoVrBER2KJ76vMdgTodSuxhU+42YjArC7 EffPRt6qM/mMfsRZXqDzLSEtWzGzzFzfQEXJ2s5bF4UOnqe1QUS0bN+0gIUUfi/IQM z897xlWVj6I1SPGvPiLTerieU9F+1Hucfmhb9PVU= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Zhongqiu Han , "Rafael J. Wysocki" , Christian Loehle , Sasha Levin Subject: [PATCH 6.1 0716/1059] cpufreq: Fix updates of cached resolved frequency and index Date: Fri, 4 Dec 2026 19:04:07 +0900 Message-ID: <20261204100702.521694622@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Rafael J. Wysocki [ Upstream commit 75120f31aed010c326b28d7c80d22314df34d9a8 ] The cached resolved frequency and index, cached_target_freq and cached_resolved_idx, respectively, may be updated concurrently if cpufreq_driver_resolve_freq() is called by the schedutil governor while cpufreq_set_policy() is running for the same policy. Those concurrent updates are not synchronized in any way and the values written by cpufreq_set_policy(), if it wins the race, may confuse drivers using those policy fields [1]. To address this, use the observation that the only place where cached_target_freq and cached_resolved_idx need to be updated is cpufreq_driver_resolve_freq() because the only legitimate users of them are .fast_switch() driver callbacks. Although cached_resolved_idx is also used in __cpufreq_driver_target(), that is in fact unnecessary and it is better to use a local variable for storing the frequency table index in that function. Accordingly, rearrange __resolve_freq() and its callers so that cached_target_freq and cached_resolved_idx are only updated in cpufreq_driver_resolve_freq(). The cached_target_freq initialization in cpufreq_set_policy() is still needed, but move it after stopping the old governor and before starting the new one to avoid racing with cpufreq_driver_resolve_freq(). Fixes: f9ccdec24d91 ("cpufreq: Reuse cpufreq_driver_resolve_freq() in __cpufreq_driver_target()") Fixes: 151717690694 ("cpufreq: Make policy min/max hard requirements") Reported-by: Zhongqiu Han Closes: https://lore.kernel.org/linux-pm/20261105140357.1331894-1-zhongqiu.han@oss.qualcomm.com/ [1] Co-developed-by: Zhongqiu Han Signed-off-by: Zhongqiu Han Signed-off-by: Rafael J. Wysocki Reviewed-by: Christian Loehle Tested-by: Christian Loehle Cc: All applicable # 342d3a73c5db cpufreq: Force passing target frequency to driver during resume Cc: All applicable # 166207cac451 cpufreq: Cache post-resolution target frequency in __resolve_freq() Link: https://patch.msgid.link/3472677.aeNJFYEL58@rafael.j.wysocki Signed-off-by: Rafael J. Wysocki [ sashal: adjusted cpufreq_set_policy() to keep the older WRITE_ONCE min/max resolution, adding only the NULL idx argument and removing the early cached_target_freq = UINT_MAX, since force_target/pressure code is absent. ] Signed-off-by: Sasha Levin --- drivers/cpufreq/cpufreq.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index 9956415aec00a..931ab927316bd 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -533,7 +533,7 @@ EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch); static unsigned int __resolve_freq(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int min, unsigned int max, - unsigned int relation) + unsigned int relation, unsigned int *idx_p) { unsigned int idx; @@ -543,9 +543,10 @@ static unsigned int __resolve_freq(struct cpufreq_policy *policy, return target_freq; idx = cpufreq_frequency_table_target(policy, target_freq, min, max, relation); - policy->cached_resolved_idx = idx; - policy->cached_target_freq = policy->freq_table[idx].frequency; - return policy->cached_target_freq; + if (idx_p) + *idx_p = idx; + + return policy->freq_table[idx].frequency; } /** @@ -576,7 +577,10 @@ unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy, if (unlikely(min > max)) min = max; - return __resolve_freq(policy, target_freq, min, max, CPUFREQ_RELATION_LE); + policy->cached_target_freq = __resolve_freq(policy, target_freq, min, max, + CPUFREQ_RELATION_LE, + &policy->cached_resolved_idx); + return policy->cached_target_freq; } EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq); @@ -2340,12 +2344,13 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int relation) { unsigned int old_target_freq = target_freq; + unsigned int idx = 0; if (cpufreq_disabled()) return -ENODEV; target_freq = __resolve_freq(policy, target_freq, policy->min, - policy->max, relation); + policy->max, relation, &idx); pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", policy->cpu, target_freq, relation, old_target_freq); @@ -2374,7 +2379,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy, if (!cpufreq_driver->target_index) return -EINVAL; - return __target_index(policy, policy->cached_resolved_idx); + return __target_index(policy, idx); } EXPORT_SYMBOL_GPL(__cpufreq_driver_target); @@ -2647,15 +2652,13 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy, */ WRITE_ONCE(policy->max, __resolve_freq(policy, new_data.max, new_data.min, new_data.max, - CPUFREQ_RELATION_H)); + CPUFREQ_RELATION_H, NULL)); new_data.min = __resolve_freq(policy, new_data.min, new_data.min, - new_data.max, CPUFREQ_RELATION_L); + new_data.max, CPUFREQ_RELATION_L, NULL); WRITE_ONCE(policy->min, new_data.min > policy->max ? policy->max : new_data.min); trace_cpu_frequency_limits(policy); - policy->cached_target_freq = UINT_MAX; - pr_debug("new min and max freqs are %u - %u kHz\n", policy->min, policy->max); @@ -2681,6 +2684,12 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy, cpufreq_exit_governor(policy); } + /* + * Initialize cached_target_freq while no governor is running to avoid + * racing with cpufreq_driver_resolve_freq(), which updates it. + */ + policy->cached_target_freq = UINT_MAX; + /* start new governor */ policy->governor = new_gov; ret = cpufreq_init_governor(policy); -- 2.53.0