FAILED: patch "[PATCH] cpufreq: Fix updates of cached resolved frequency and index" failed to apply to 6.6-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.6-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.6.y
git checkout FETCH_HEAD
git cherry-pick -x 75120f31aed010c326b28d7c80d22314df34d9a8
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026112539-twine-magnify-87fa@gregkh' --subject-prefix 'PATCH 6.6.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 75120f31aed010c326b28d7c80d22314df34d9a8 Mon Sep 17 00:00:00 2001
From: "Rafael J. Wysocki" <[email protected]>
Date: Fri, 6 Nov 2026 16:05:06 +0100
Subject: [PATCH] cpufreq: Fix updates of cached resolved frequency and index
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().
drivers/cpufreq/cpufreq.c +20 -11
@@ -477,7 +477,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;
@@ -487,9 +487,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; }/**
@@ -520,7 +521,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);
@@ -2402,12 +2406,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 ("CPU %u: cur %u kHz -> target %u kHz (req %u kHz, rel %u)\n" , policy->cpu, policy->cur, target_freq, old_target_freq, relation);
@@ -2437,7 +2442,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);
@@ -2716,7 +2721,7 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy,* concurrently by cpufreq_driver_resolve_freq() during the update. */ freq =__resolve_freq (policy, new_data.max, new_data.min, new_data.max, -CPUFREQ_RELATION_H ); +CPUFREQ_RELATION_H ,NULL );if (freq != policy->max) {WRITE_ONCE (policy->max, freq);if (need_update_limits)
@@ -2724,7 +2729,7 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy, } freq =__resolve_freq (policy, new_data.min, new_data.min, new_data.max, -CPUFREQ_RELATION_L ); +CPUFREQ_RELATION_L ,NULL ); freq =min (freq, policy->max);if (freq != policy->min) {WRITE_ONCE (policy->min, freq);
@@ -2736,8 +2741,6 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy,cpufreq_update_pressure (policy); - policy->cached_target_freq =UINT_MAX ; -pr_debug ("new min and max freqs are %u - %u kHz\n" , policy->min, policy->max);
@@ -2763,6 +2766,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);
Sasha Levin
maintainerPATCH 1/3 cpufreq: Introduce policy->boost_supported flag
From: Viresh Kumar <[email protected]>
[ Upstream commit 1f7d1bab50e6ae517f8b6699e56d709d61ae13e5 ]
It is possible to have a scenario where not all cpufreq policies support boost frequencies. And letting sysfs (or other parts of the kernel) enable boost feature for that policy isn't correct.
Add a new flag, boost_supported, which will be set to true by the cpufreq core only if the freq table contains valid boost frequencies.
Some cpufreq drivers though don't have boost frequencies in the freq-table, they can set this flag from their ->init() callbacks.
Once all the drivers are updated to set the flag correctly, we can check it before enabling boost feature for a policy.
Stable-dep-of: 75120f31aed0 ("cpufreq: Fix updates of cached resolved frequency and index")
--- drivers/cpufreq/freq_table.c | 4 ++++ include/linux/cpufreq.h | 3 +++ 2 files changed, 7 insertions(+)
drivers/cpufreq/freq_table.c +4 -0
@@ -367,6 +367,10 @@ int cpufreq_table_validate_and_sort(struct cpufreq_policy *policy)if (ret)return ret; +/* Driver's may have set this field already */ +if (policy_has_boost_freq (policy)) + policy->boost_supported = true; +return set_freq_table_sorted (policy); }
include/linux/cpufreq.h +3 -0
@@ -147,6 +147,9 @@ struct cpufreq_policy {/* Per policy boost enabled flag. */ bool boost_enabled; +/* Per policy boost supported flag. */ +bool boost_supported; +/* Cached frequency lookup from cpufreq_driver_resolve_freq. */ unsigned int cached_target_freq;unsigned int cached_resolved_idx;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/3 cpufreq: Avoid redundant target() calls for unchanged limits
From: Viresh Kumar <[email protected]>
[ Upstream commit 9801be8bef65dbcbc40c1a9c2f03d70ed67a5d30 ]
Drivers setting CPUFREQ_NEED_UPDATE_LIMITS expect target() to be invoked even if the target frequency remains unchanged, so they can update their internal policy limits state.
Currently the core invokes target() unconditionally whenever the requested frequency matches policy->cur for such drivers, even if policy->min and policy->max haven't changed since the previous update.
Track pending policy limit updates explicitly and skip redundant target() invocations when neither the target frequency nor the effective limits changed.
Stable-dep-of: 75120f31aed0 ("cpufreq: Fix updates of cached resolved frequency and index")
--- drivers/cpufreq/cpufreq.c | 31 ++++++++++++++++++++++--------- include/linux/cpufreq.h | 3 +++ 2 files changed, 25 insertions(+), 9 deletions(-)
drivers/cpufreq/cpufreq.c +22 -9
@@ -2367,9 +2367,13 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,* exactly same freq is called again and so we can save on few function * calls. */ -if (target_freq == policy->cur && - !(cpufreq_driver->flags &CPUFREQ_NEED_UPDATE_LIMITS )) -return 0 ; +if (target_freq == policy->cur) { +if (!(cpufreq_driver->flags &CPUFREQ_NEED_UPDATE_LIMITS ) || + !policy->update_limits) +return 0 ; + + policy->update_limits = false; + }if (cpufreq_driver->target) {/*
@@ -2619,6 +2623,7 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy, {struct cpufreq_policy_data new_data;struct cpufreq_governor *old_gov; +unsigned int freq;int ret;memcpy (&new_data.cpuinfo, &policy->cpuinfo,sizeof (policy->cpuinfo));
@@ -2651,12 +2656,20 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy,* compiler optimizations around them because they may be accessed * concurrently by cpufreq_driver_resolve_freq() during the update. */ -WRITE_ONCE (policy->max,__resolve_freq (policy, new_data.max, - new_data.min, new_data.max, -CPUFREQ_RELATION_H )); - new_data.min =__resolve_freq (policy, new_data.min, new_data.min, - new_data.max,CPUFREQ_RELATION_L ); -WRITE_ONCE (policy->min, new_data.min > policy->max ? policy->max : new_data.min); + freq =__resolve_freq (policy, new_data.max, new_data.min, new_data.max, +CPUFREQ_RELATION_H ); +if (freq != policy->max) { +WRITE_ONCE (policy->max, freq); + policy->update_limits = true; + } + + freq =__resolve_freq (policy, new_data.min, new_data.min, new_data.max, +CPUFREQ_RELATION_L ); + freq =min (freq, policy->max); +if (freq != policy->min) { +WRITE_ONCE (policy->min, freq); + policy->update_limits = true; + }trace_cpu_frequency_limits (policy);
include/linux/cpufreq.h +3 -0
@@ -150,6 +150,9 @@ struct cpufreq_policy {/* Per policy boost supported flag. */ bool boost_supported; +/* Pending policy->min/max update for the driver */ +bool update_limits; +/* Cached frequency lookup from cpufreq_driver_resolve_freq. */ unsigned int cached_target_freq;unsigned int cached_resolved_idx;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/3 cpufreq: Fix updates of cached resolved frequency and index
In reply to Sasha LevinFrom: "Rafael J. Wysocki" <[email protected]>
[ 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().
--- drivers/cpufreq/cpufreq.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-)
drivers/cpufreq/cpufreq.c +20 -11
@@ -537,7 +537,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;
@@ -547,9 +547,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; }/**
@@ -580,7 +581,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);
@@ -2351,12 +2355,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);
@@ -2389,7 +2394,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);
@@ -2657,14 +2662,14 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy,* concurrently by cpufreq_driver_resolve_freq() during the update. */ freq =__resolve_freq (policy, new_data.max, new_data.min, new_data.max, -CPUFREQ_RELATION_H ); +CPUFREQ_RELATION_H ,NULL );if (freq != policy->max) {WRITE_ONCE (policy->max, freq); policy->update_limits = true; } freq =__resolve_freq (policy, new_data.min, new_data.min, new_data.max, -CPUFREQ_RELATION_L ); +CPUFREQ_RELATION_L ,NULL ); freq =min (freq, policy->max);if (freq != policy->min) {WRITE_ONCE (policy->min, freq);
@@ -2673,8 +2678,6 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy,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);
@@ -2700,6 +2703,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);
Signature · 1 line
2.53.0