cpufreq/sched: Improve synchronization of policy limits updates with schedutil
Subsystems (2)
cover cpufreq/sched: Improve synchronization of policy limits updates with schedutil
Hi Everyone,
This series of patches has been inspired by the discussion following a bug report regarding the patch at
https://lore.kernel.org/lkml/[email protected]/
and its attempted unsuccessful resolution:
https://lore.kernel.org/linux-pm/[email protected]/
which basically leads to the conclusion that cpufreq policy limits updates are not sufficiently synchronized with the scheditil governor, especially in the fast switching case in which running the driver callback is the only way to make the new policy limits take effect.
The purpose of this series is to address this concern.
Patch [1/5] is a fix for the issue introduced by the patch linked above (please see the patch changelog for details), for 6.15-rc. The remaining patches are for 6.16.
Patch [2/5] adds memory barriers in two places in schedutil along with some WRITE_ONCE()/READ_ONCE() annotations to ensure that policy limits updates will not be missed due to reordering of instructions.
Patch [3/5] is a preparatory function rename with no functional impact.
Patch [4/5] updates the cpufreq core to avoid situations in which cpufreq_driver_resolve_freq(), called by schedutil, may see intermediate values of policy->min and policy->max and makes that function address the unlikely case in which it may see policy->min > policy->max.
Patch [5/5] cleans up the code after the previous changes.
Please see individual patch changelogs for details.
Thanks!
PATCH 1/5 cpufreq/sched: Check fast_switch_enabled when setting need_freq_update
From: Rafael J. Wysocki <[email protected]>
Commit 8e461a1cb43d ("cpufreq: schedutil: Fix superfluous updates caused by need_freq_update") overlooked the fact that when fast swtching is enabled, it is the only way to pick up new policy limits and so need_freq_update needs to be set in that case when limits_changed is set.
This causes policy limits updates to be missed in some cases, so make sugov_should_update_freq() also set need_freq_update when the fast_switch_enabled policy flag is set.
--- kernel/sched/cpufreq_schedutil.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/sched/cpufreq_schedutil.c +3 -1
@@ -83,7 +83,9 @@if (unlikely (sg_policy->limits_changed)) { sg_policy->limits_changed = false; - sg_policy->need_freq_update =cpufreq_driver_test_flags (CPUFREQ_NEED_UPDATE_LIMITS ); + sg_policy->need_freq_update = + sg_policy->policy->fast_switch_enabled || +cpufreq_driver_test_flags (CPUFREQ_NEED_UPDATE_LIMITS );return true; }
Rafael J. Wysocki
maintainerRe: [PATCH v1 1/5] cpufreq/sched: Check fast_switch_enabled when setting need_freq_update
Quoted 9 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Commit
8e461a1cb43d("cpufreq: schedutil: Fix superfluous updates caused byneed_freq_update") overlooked the fact that when fast swtching is enabled, it is the only way to pick up new policy limits and soneed_freq_updateneeds to be set in that case whenlimits_changedis set.
Quoted 3 lines
This causes policy limits updates to be missed in some cases, so make
sugov_should_update_freq()also setneed_freq_updatewhen thefast_switch_enabledpolicy flag is set.
Earlier today I realized that this patch would not be sufficient because if the policy limits change, schedutil needed to invoke the driver callback for the new limits to take effect regardless of whether or not fast switching had been enabled.
After making this observation I've realized that there's a better fix that covers all of the relevant cases, but it requires patch [2/5] to be rebased and one more can be made between the new fix and patch [2/5].
So there will be a v2.
Quoted 25 lines
Fixes 8e461a1cb43d ("cpufreq: schedutil: Fix superfluous updates caused by need_freq_update")Reported-by Stephan Gerhold <[email protected]>Signed-off-by Rafael J. Wysocki <[email protected]>--- kernel/sched/cpufreq_schedutil.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)kernel/sched/cpufreq_schedutil.c +3
-1@@ -83,7 +83,9 @@if (unlikely (sg_policy->limits_changed)) { sg_policy->limits_changed = false; - sg_policy->need_freq_update =cpufreq_driver_test_flags (CPUFREQ_NEED_UPDATE_LIMITS ); + sg_policy->need_freq_update = + sg_policy->policy->fast_switch_enabled || +cpufreq_driver_test_flags (CPUFREQ_NEED_UPDATE_LIMITS );return true; }
PATCH 2/5 cpufreq/sched: Explicitly synchronize limits_changed flag handling
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
The handling of the limits_changed flag in struct sugov_policy needs to be explicitly synchronized to ensure that cpufreq policy limits updates will not be missed in some cases.
Without that synchronization it is theoretically possible that the limits_changed update in sugov_should_update_freq() will be reordered with respect to the reads of the policy limits in cpufreq_driver_resolve_freq() and in that case, if the limits_changed update in sugov_limits() clobbers the one in sugov_should_update_freq(), the new policy limits may not take effect for a long time.
Likewise, the limits_changed update in sugov_limits() may theoretically get reordered with respect to the updates of the policy limits in cpufreq_set_policy() and if sugov_should_update_freq() runs between them, the policy limits change may be missed.
To ensure that the above situations will not take place, add memory barriers preventing the reordering in question from taking place and add READ_ONCE() and WRITE_ONCE() annotations around all of the limits_changed flag updates to prevent the compiler from messing up with that code.
--- kernel/sched/cpufreq_schedutil.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-)
kernel/sched/cpufreq_schedutil.c +24 -4
@@ -81,11 +81,22 @@if (!cpufreq_this_cpu_can_update (sg_policy->policy))return false; -if (unlikely (sg_policy->limits_changed)) { - sg_policy->limits_changed = false; +if (unlikely (READ_ONCE (sg_policy->limits_changed))) { +WRITE_ONCE (sg_policy->limits_changed, false); sg_policy->need_freq_update = sg_policy->policy->fast_switch_enabled ||cpufreq_driver_test_flags (CPUFREQ_NEED_UPDATE_LIMITS ); + +/* +* The above limits_changed update must occur before the reads +* of policy limits in cpufreq_driver_resolve_freq() or a policy +* limits update might be missed, so use a memory barrier to +* ensure it. +* +* This pairs with the write memory barrier in sugov_limits(). +*/ +smp_mb (); +return true; }
@@ -367,7 +378,7 @@static inline void ignore_dl_rate_limit (struct sugov_cpu *sg_cpu) {if (cpu_bw_dl (cpu_rq (sg_cpu->cpu)) > sg_cpu->bw_min) - sg_cpu->sg_policy->limits_changed = true; +WRITE_ONCE (sg_cpu->sg_policy->limits_changed, true); } static inline bool sugov_update_single_common(struct sugov_cpu *sg_cpu,
@@ -873,7 +884,16 @@mutex_unlock (&sg_policy->work_lock); } - sg_policy->limits_changed = true; +/* +* The limits_changed update below must take place before the updates +* of policy limits in cpufreq_set_policy() or a policy limits update +* might be missed, so use a memory barrier to ensure it. +* +* This pairs with the memory barrier in sugov_should_update_freq(). +*/ +smp_wmb (); + +WRITE_ONCE (sg_policy->limits_changed, true); }struct cpufreq_governor schedutil_gov = {
PATCH 3/5 cpufreq: Rename __resolve_freq() to clamp_and_resolve_freq()
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
In preparation for subsequent changes rename a function in the cpufreq core as per the subject and while at it, clean up some white space around the declaration for that function.
No functional impact.
--- drivers/cpufreq/cpufreq.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/cpufreq/cpufreq.c +7 -6
@@ -490,8 +490,9 @@ }EXPORT_SYMBOL_GPL (cpufreq_disable_fast_switch); -static unsigned int __resolve_freq (struct cpufreq_policy *policy, -unsigned int target_freq,unsigned int relation) +static unsigned int clamp_and_resolve_freq (struct cpufreq_policy *policy, +unsigned int target_freq, +unsigned int relation) {unsigned int idx;
@@ -520,7 +521,7 @@unsigned int cpufreq_driver_resolve_freq (struct cpufreq_policy *policy,unsigned int target_freq) { -return __resolve_freq (policy, target_freq,CPUFREQ_RELATION_LE ); +return clamp_and_resolve_freq (policy, target_freq,CPUFREQ_RELATION_LE ); }EXPORT_SYMBOL_GPL (cpufreq_driver_resolve_freq);
@@ -2338,7 +2339,7 @@if (cpufreq_disabled ())return -ENODEV ; - target_freq =__resolve_freq (policy, target_freq, relation); + target_freq =clamp_and_resolve_freq (policy, target_freq, relation);pr_debug ("target for CPU %u: %u kHz, relation %u, requested %u kHz\n" , policy->cpu, target_freq, relation, old_target_freq);
@@ -2634,8 +2635,8 @@*/ policy->min = new_data.min; policy->max = new_data.max; - policy->min =__resolve_freq (policy, policy->min,CPUFREQ_RELATION_L ); - policy->max =__resolve_freq (policy, policy->max,CPUFREQ_RELATION_H ); + policy->min =clamp_and_resolve_freq (policy, policy->min,CPUFREQ_RELATION_L ); + policy->max =clamp_and_resolve_freq (policy, policy->max,CPUFREQ_RELATION_H );trace_cpu_frequency_limits (policy);cpufreq_update_pressure (policy);
PATCH 4/5 cpufreq: Avoid inconsistent policy->min and policy->max
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Since cpufreq_driver_resolve_freq() can run in parallel with cpufreq_set_policy() and there is no synchronization between them, the former may access policy->min and policy->max while the latter is updating them and it may see intermediate values of them due to the way the update is carried out. Also the compiler is free to apply any optimizations it wants both to the stores in cpufreq_set_policy() and to the loads in cpufreq_driver_resolve_freq() which may result in additional inconsistencies.
To address this, use WRITE_ONCE() when updating policy->min and policy->max in cpufreq_set_policy() and use READ_ONCE() for reading them in cpufreq_driver_resolve_freq(). Moreover, rearrange the update in cpufreq_set_policy() to avoid storing intermediate values in policy->min and policy->max with the help of the observation that their new values are expected to be properly ordered upfront.
Also modify cpufreq_driver_resolve_freq() to take the possible reverse ordering of policy->min and policy->max, which may happen depending on the ordering of operations when this function and cpufreq_set_policy() run concurrently, into account by always honoring the max when it turns out to be less than the min (in case it comes from thermal throttling or similar).
--- drivers/cpufreq/cpufreq.c | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-)
drivers/cpufreq/cpufreq.c +36 -10
@@ -490,14 +490,12 @@ }EXPORT_SYMBOL_GPL (cpufreq_disable_fast_switch); -static unsigned int clamp_and_resolve_freq (struct cpufreq_policy *policy, -unsigned int target_freq, -unsigned int relation) +static unsigned int __resolve_freq (struct cpufreq_policy *policy, +unsigned int target_freq, +unsigned int relation) {unsigned int idx; - target_freq =clamp_val (target_freq, policy->min, policy->max); -if (!policy->freq_table)return target_freq;
@@ -507,6 +505,15 @@return policy->freq_table[idx].frequency; } +static unsigned int clamp_and_resolve_freq (struct cpufreq_policy *policy, +unsigned int target_freq, +unsigned int relation) +{ + target_freq =clamp_val (target_freq, policy->min, policy->max); + +return __resolve_freq (policy, target_freq, relation); +} +/** * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported * one.
@@ -521,7 +528,22 @@unsigned int cpufreq_driver_resolve_freq (struct cpufreq_policy *policy,unsigned int target_freq) { -return clamp_and_resolve_freq (policy, target_freq,CPUFREQ_RELATION_LE ); +unsigned int min =READ_ONCE (policy->min); +unsigned int max =READ_ONCE (policy->max); + +/* +* If this function runs in parallel with cpufreq_set_policy(), it may +* read policy->min before the update and policy->max after the update +* or the other way around, so there is no ordering guarantee. +* +* Resolve this by always honoring the max (in case it comes from +* thermal throttling or similar). +*/ +if (unlikely (min > max)) + min = max; + +return __resolve_freq (policy,clamp_val (target_freq, min, max), +CPUFREQ_RELATION_LE ); }EXPORT_SYMBOL_GPL (cpufreq_driver_resolve_freq);
@@ -2632,11 +2654,15 @@* Resolve policy min/max to available frequencies. It ensures * no frequency resolution will neither overshoot the requested maximum * nor undershoot the requested minimum. +* +* Avoid storing intermediate values in policy->max or policy->min and +* compiler optimizations around them because them may be accessed +* concurrently by cpufreq_driver_resolve_freq() during the update. */ - policy->min = new_data.min; - policy->max = new_data.max; - policy->min =clamp_and_resolve_freq (policy, policy->min,CPUFREQ_RELATION_L ); - policy->max =clamp_and_resolve_freq (policy, policy->max,CPUFREQ_RELATION_H ); +WRITE_ONCE (policy->max,__resolve_freq (policy, new_data.max,CPUFREQ_RELATION_H )); + new_data.min =__resolve_freq (policy, new_data.min,CPUFREQ_RELATION_L ); +WRITE_ONCE (policy->min, new_data.min > policy->max ? policy->max : new_data.min); +trace_cpu_frequency_limits (policy);cpufreq_update_pressure (policy);
PATCH 5/5 cpufreq: Eliminate clamp_and_resolve_freq()
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Fold clamp_and_resolve_freq() into __cpufreq_driver_target() which is its only remaining caller.
No intentional functional impact.
--- drivers/cpufreq/cpufreq.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-)
drivers/cpufreq/cpufreq.c +2 -10
@@ -505,15 +505,6 @@return policy->freq_table[idx].frequency; } -static unsigned int clamp_and_resolve_freq (struct cpufreq_policy *policy, -unsigned int target_freq, -unsigned int relation) -{ - target_freq =clamp_val (target_freq, policy->min, policy->max); - -return __resolve_freq (policy, target_freq, relation); -} -/** * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported * one.
@@ -2361,7 +2352,8 @@if (cpufreq_disabled ())return -ENODEV ; - target_freq =clamp_and_resolve_freq (policy, target_freq, relation); + target_freq =clamp_val (target_freq, policy->min, policy->max); + target_freq =__resolve_freq (policy, target_freq, relation);pr_debug ("target for CPU %u: %u kHz, relation %u, requested %u kHz\n" , policy->cpu, target_freq, relation, old_target_freq);