cpufreq/schedutil: Fix null pointer dereference in sugov_update_single_freq
Subsystems (5)
v2 cover cpufreq/schedutil: Fix null pointer dereference in sugov_update_single_freq
Currently, amd_pstate only uses adjust_perf and target callback functions to get the frequency/effective utilization data from scaling governors. Schedutil generally avoids calling target and fast_switch functions if adjust_perf function pointer is set for the driver. But in some rare cases, schedutil tries to call fast_switch function even the function pointer is not set. This happens when frequency invariance is turned off. When frequency invariance is turned off schedutil falls back to sugov_update_single_freq which currently relies on the fast_switch callback.
Currently, frequency invariance is turned off when any anomaly is detected with aperf/mperf readings. Which triggers this problem.
Fix this by disabling fast_switch_enabled flag if fast_switch callback is not set and removing fast_switch_enabled flag dependency for adjust_perf callback. But this will force schedutil to take a slower path to update frequency. Therefore to fix this add fast_switch function on amd_pstate to take advantage of fast frequency update.
Changelog:
v1 -> v2
- Remove fast_switch_enabled flag dependency for adjust_perf callback
v1: https://lore.kernel.org/linux-pm/[email protected]/
Gautham R. Shenoy (1):
amd_pstate: Add ->fast_switch() callback
Wyes Karny (1):
cpufreq/schedutil: Remove fast_switch_possible flag if driver doesn't
set fast_switch
drivers/cpufreq/amd-pstate.c | 46 +++++++++++++++++++++++++------- drivers/cpufreq/cpufreq.c | 20 +++++++++++++- drivers/cpufreq/intel_pstate.c | 3 +-- include/linux/cpufreq.h | 1 + kernel/sched/cpufreq_schedutil.c | 2 +- 5 files changed, 59 insertions(+), 13 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/2 cpufreq/schedutil: Remove fast_switch_possible flag if driver doesn't set fast_switch
The set value of fast_switch_enabled indicates that fast_switch callback is set. For some drivers such as amd_pstate and intel_pstate, the adjust_perf callback is used but it still sets fast_switch_possible flag. This is because this flag also decides whether schedutil governor selects adjust_perf function for frequency update. This condition in the schedutil governor forces the scaling driver to set the fast_switch_possible flag.
Remove fast_switch_enabled check when schedutil decides to select adjust_perf function for frequency update. Thus removing this drivers are now free to remove fast_switch_possible flag if they don't use fast_switch callback.
This issue becomes apparent when aperf/mperf overflow occurs. When this happens, kernel disables frequency invariance calculation which causes schedutil to fallback to sugov_update_single_freq which currently relies on the fast_switch callback.
Normal flow:
sugov_update_single_perf
cpufreq_driver_adjust_perf
cpufreq_driver->adjust_perfError case flow:
sugov_update_single_perf
sugov_update_single_freq <-- This is chosen because the freq invariant is disabled due to aperf/mperf overflow
cpufreq_driver_fast_switch
cpufreq_driver->fast_switch <-- Here NULL pointer dereference is happening, because fast_switch is not set
This change fixes this NULL pointer dereference issue.
--- drivers/cpufreq/amd-pstate.c | 10 +++++++--- drivers/cpufreq/cpufreq.c | 20 +++++++++++++++++++- drivers/cpufreq/intel_pstate.c | 3 +-- include/linux/cpufreq.h | 1 + kernel/sched/cpufreq_schedutil.c | 2 +- 5 files changed, 29 insertions(+), 7 deletions(-)
drivers/cpufreq/amd-pstate.c +7 -3
@@ -671,8 +671,14 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)/* It will be updated by governor */ policy->cur = policy->cpuinfo.min_freq; +/** +* For shared memory system frequency update takes time that's why +* do this in deferred kthread context. +*/ if (boot_cpu_has (X86_FEATURE_CPPC )) - policy->fast_switch_possible = true; + current_pstate_driver->adjust_perf = amd_pstate_adjust_perf; +else + current_pstate_driver->adjust_perf =NULL ; ret =freq_qos_add_request (&policy->constraints, &cpudata->req[0 ],FREQ_QOS_MIN , policy->cpuinfo.min_freq);
@@ -697,8 +703,6 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy) policy->driver_data = cpudata;amd_pstate_boost_init (cpudata); -if (!current_pstate_driver->adjust_perf) - current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;return 0 ;
drivers/cpufreq/cpufreq.c +19 -1
@@ -501,6 +501,13 @@ void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)if (!policy->fast_switch_possible)return ; +/** +* It's not expected driver's fast_switch callback is not set +* even fast_switch_possible is true. +*/ +if (WARN_ON (!cpufreq_driver_has_fast_switch ())) +return ; +mutex_lock (&cpufreq_fast_switch_lock);if (cpufreq_fast_switch_count >=0 ) { cpufreq_fast_switch_count++;
@@ -2143,6 +2150,17 @@ unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, }EXPORT_SYMBOL_GPL (cpufreq_driver_fast_switch); +/** +* cpufreq_driver_has_fast_switch - Check "fast switch" callback. +* +* Return 'true' if the ->fast_switch callback is present for the +* current driver or 'false' otherwise. +*/ +bool cpufreq_driver_has_fast_switch (void ) +{ +return !!cpufreq_driver->fast_switch; +} +/** * cpufreq_driver_adjust_perf - Adjust CPU performance level in one go. * @cpu: Target CPU.
@@ -2157,7 +2175,7 @@ EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);* and it is expected to select a suitable performance level equal to or above * @min_perf and preferably equal to or below @target_perf. * -* This function must not be called if policy->fast_switch_enabled is unset. +* By default this function takes the fast frequency update path. * * Governors calling this function must guarantee that it will never be invoked * twice in parallel for the same CPU and that it will never be called in
drivers/cpufreq/intel_pstate.c +1 -2
@@ -2698,8 +2698,6 @@ static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)intel_pstate_init_acpi_perf_limits (policy); - policy->fast_switch_possible = true; -return 0 ; }
@@ -2955,6 +2953,7 @@ static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)if (ret)return ret; + policy->fast_switch_possible = true; policy->cpuinfo.transition_latency =INTEL_CPUFREQ_TRANSITION_LATENCY ;/* This reflects the intel_pstate_get_cpu_pstates() setting. */ policy->cur = policy->cpuinfo.min_freq;
include/linux/cpufreq.h +1 -0
@@ -604,6 +604,7 @@ struct cpufreq_governor {/* Pass a target to the cpufreq driver */ unsigned int cpufreq_driver_fast_switch (struct cpufreq_policy *policy,unsigned int target_freq); +bool cpufreq_driver_has_fast_switch (void ); void cpufreq_driver_adjust_perf(unsigned int cpu, unsigned long min_perf, unsigned long target_perf,
kernel/sched/cpufreq_schedutil.c +1 -1
@@ -776,7 +776,7 @@ static int sugov_start(struct cpufreq_policy *policy)if (policy_is_shared (policy)) uu = sugov_update_shared; -else if (policy->fast_switch_enabled &&cpufreq_driver_has_adjust_perf ()) +else if (cpufreq_driver_has_adjust_perf ()) uu = sugov_update_single_perf;else uu = sugov_update_single_freq;
Signature · 1 line
2.34.1
Rafael J. Wysocki
maintainerRe: [PATCH v2 1/2] cpufreq/schedutil: Remove fast_switch_possible flag if driver doesn't set fast_switch
Quoted 21 lines · Wyes Karny
The set value of
fast_switch_enabledindicates thatfast_switchcallback is set. For some drivers such asamd_pstateandintel_pstate, theadjust_perfcallback is used but it still setsfast_switch_possibleflag. This is because this flag also decides whether schedutil governor selectsadjust_perffunction for frequency update. This condition in the schedutil governor forces the scaling driver to set thefast_switch_possibleflag.Remove
fast_switch_enabledcheck when schedutil decides to selectadjust_perffunction for frequency update. Thus removing this drivers are now free to removefast_switch_possibleflag if they don't usefast_switchcallback.This issue becomes apparent when aperf/mperf overflow occurs. When this happens, kernel disables frequency invariance calculation which causes schedutil to fallback to
sugov_update_single_freqwhich currently relies on thefast_switchcallback.Normal flow:
sugov_update_single_perf
Quoted 8 lines
cpufreq_driver_adjust_perf cpufreq_driver->adjust_perfError case flow:
sugov_update_single_perf
sugov_update_single_freq<-- This is chosen because the freq invariant is disabled due to aperf/mperf overflow
cpufreq_driver_fast_switch
cpufreq_driver->fast_switch<-- Here NULL pointer dereference is happening, becausefast_switchis not set
So you need to set fast_switch.
Please read the comment in sugov_update_single_perf(). It explains why adjust_perf is not used when scale invariance is not enabled: the mapping between the performance levels and frequency are not generally defined in that case and it is up to the driver to figure out what perf level to use to get the given frequency. And this is exactly why fast_switch is not optional: because scale invariance may be disabled.
Please feel free to update the documentation to clarify this, but the way to fix the issue is to implement fast_switch in the driver.
Quoted 136 lines
Fixes a61dec744745 ("cpufreq: schedutil: Avoid missing updates for one-CPU policies")Signed-off-by Wyes Karny <[email protected]>Cc "Rafael J. Wysocki" <[email protected]>--- drivers/cpufreq/amd-pstate.c | 10 +++++++--- drivers/cpufreq/cpufreq.c | 20 +++++++++++++++++++- drivers/cpufreq/intel_pstate.c | 3 +-- include/linux/cpufreq.h | 1 + kernel/sched/cpufreq_schedutil.c | 2 +- 5 files changed, 29 insertions(+), 7 deletions(-)drivers/cpufreq/amd-pstate.c +7
-3@@ -671,8 +671,14 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)/* It will be updated by governor */ policy->cur = policy->cpuinfo.min_freq; +/** +* For shared memory system frequency update takes time that's why +* do this in deferred kthread context. +*/ if (boot_cpu_has (X86_FEATURE_CPPC )) - policy->fast_switch_possible = true; + current_pstate_driver->adjust_perf = amd_pstate_adjust_perf; +else + current_pstate_driver->adjust_perf =NULL ; ret =freq_qos_add_request (&policy->constraints, &cpudata->req[0 ],FREQ_QOS_MIN , policy->cpuinfo.min_freq);@@ -697,8 +703,6 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy) policy->driver_data = cpudata;amd_pstate_boost_init (cpudata); -if (!current_pstate_driver->adjust_perf) - current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;return 0 ;drivers/cpufreq/cpufreq.c +19
-1@@ -501,6 +501,13 @@ void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)if (!policy->fast_switch_possible)return ; +/** +* It's not expected driver's fast_switch callback is not set +* even fast_switch_possible is true. +*/ +if (WARN_ON (!cpufreq_driver_has_fast_switch ())) +return ; +mutex_lock (&cpufreq_fast_switch_lock);if (cpufreq_fast_switch_count >=0 ) { cpufreq_fast_switch_count++;@@ -2143,6 +2150,17 @@ unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, }EXPORT_SYMBOL_GPL (cpufreq_driver_fast_switch); +/** +* cpufreq_driver_has_fast_switch - Check "fast switch" callback. +* +* Return 'true' if the ->fast_switch callback is present for the +* current driver or 'false' otherwise. +*/ +bool cpufreq_driver_has_fast_switch (void ) +{ +return !!cpufreq_driver->fast_switch; +} +/** * cpufreq_driver_adjust_perf - Adjust CPU performance level in one go. * @cpu: Target CPU. @@ -2157,7 +2175,7 @@ EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);* and it is expected to select a suitable performance level equal to or above * @min_perf and preferably equal to or below @target_perf. * -* This function must not be called if policy->fast_switch_enabled is unset. +* By default this function takes the fast frequency update path. * * Governors calling this function must guarantee that it will never be invoked * twice in parallel for the same CPU and that it will never be called in drivers/cpufreq/intel_pstate.c +1
-2@@ -2698,8 +2698,6 @@ static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)intel_pstate_init_acpi_perf_limits (policy); - policy->fast_switch_possible = true; -return 0 ; }@@ -2955,6 +2953,7 @@ static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)if (ret)return ret; + policy->fast_switch_possible = true; policy->cpuinfo.transition_latency =INTEL_CPUFREQ_TRANSITION_LATENCY ;/* This reflects the intel_pstate_get_cpu_pstates() setting. */ policy->cur = policy->cpuinfo.min_freq;include/linux/cpufreq.h +1
-0@@ -604,6 +604,7 @@ struct cpufreq_governor {/* Pass a target to the cpufreq driver */ unsigned int cpufreq_driver_fast_switch (struct cpufreq_policy *policy,unsigned int target_freq); +bool cpufreq_driver_has_fast_switch (void ); void cpufreq_driver_adjust_perf(unsigned int cpu, unsigned long min_perf, unsigned long target_perf,kernel/sched/cpufreq_schedutil.c +1
-2@@ -776,7 +776,7 @@ static int sugov_start(struct cpufreq_policy *policy)if (policy_is_shared (policy)) uu = sugov_update_shared; -else if (policy->fast_switch_enabled &&cpufreq_driver_has_adjust_perf ()) +else if (cpufreq_driver_has_adjust_perf ()) uu = sugov_update_single_perf;else uu = sugov_update_single_freq; --2.34.1
Rafael J. Wysocki
maintainerRe: [PATCH v2 1/2] cpufreq/schedutil: Remove fast_switch_possible flag if driver doesn't set fast_switch
Quoted 5 lines · Rafael J. Wysocki
Quoted 3 lines · Wyes Karny
The set value of
fast_switch_enabledindicates thatfast_switchcallback is set.
BTW, this is not true. The ACPI cpufreq driver has fast_switch, but it doesn't always use it.
Quoted 6 lines
Quoted 6 lines
For some drivers such as
amd_pstateandintel_pstate,
theadjust_perfcallback is used but it still setsfast_switch_possibleflag. This is because this flag also decides whether schedutil governor selectsadjust_perffunction for frequency update. This condition in the schedutil governor forces the scaling driver to set thefast_switch_possibleflag.
Yes, it does, but setting this flag is not sufficient.
Quoted 133 lines
Quoted 12 lines
Remove
fast_switch_enabledcheck when schedutil decides to selectadjust_perffunction for frequency update. Thus removing this drivers are now free to removefast_switch_possibleflag if they don't usefast_switchcallback.This issue becomes apparent when aperf/mperf overflow occurs. When this happens, kernel disables frequency invariance calculation which causes schedutil to fallback to
sugov_update_single_freqwhich currently relies on thefast_switchcallback.Normal flow:
sugov_update_single_perfQuoted 8 lines
cpufreq_driver_adjust_perf cpufreq_driver->adjust_perfError case flow:
sugov_update_single_perf
sugov_update_single_freq<-- This is chosen because the freq invariant is disabled due to aperf/mperf overflow
cpufreq_driver_fast_switch
cpufreq_driver->fast_switch<-- Here NULL pointer dereference is happening, becausefast_switchis not setSo you need to set
fast_switch.Please read the comment in
sugov_update_single_perf(). It explains whyadjust_perfis not used when scale invariance is not enabled: the mapping between the performance levels and frequency are not generally defined in that case and it is up to the driver to figure out what perf level to use to get the given frequency. And this is exactly whyfast_switchis not optional: because scale invariance may be disabled.Please feel free to update the documentation to clarify this, but the way to fix the issue is to implement
fast_switchin the driver.Quoted 100 lines
Fixes a61dec744745 ("cpufreq: schedutil: Avoid missing updates for one-CPU policies")Signed-off-by Wyes Karny <[email protected]>Cc "Rafael J. Wysocki" <[email protected]>--- drivers/cpufreq/amd-pstate.c | 10 +++++++--- drivers/cpufreq/cpufreq.c | 20 +++++++++++++++++++- drivers/cpufreq/intel_pstate.c | 3 +-- include/linux/cpufreq.h | 1 + kernel/sched/cpufreq_schedutil.c | 2 +- 5 files changed, 29 insertions(+), 7 deletions(-)drivers/cpufreq/amd-pstate.c +7
-3@@ -671,8 +671,14 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)/* It will be updated by governor */ policy->cur = policy->cpuinfo.min_freq; +/** +* For shared memory system frequency update takes time that's why +* do this in deferred kthread context. +*/ if (boot_cpu_has (X86_FEATURE_CPPC )) - policy->fast_switch_possible = true; + current_pstate_driver->adjust_perf = amd_pstate_adjust_perf; +else + current_pstate_driver->adjust_perf =NULL ; ret =freq_qos_add_request (&policy->constraints, &cpudata->req[0 ],FREQ_QOS_MIN , policy->cpuinfo.min_freq);@@ -697,8 +703,6 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy) policy->driver_data = cpudata;amd_pstate_boost_init (cpudata); -if (!current_pstate_driver->adjust_perf) - current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;return 0 ;drivers/cpufreq/cpufreq.c +19
-1@@ -501,6 +501,13 @@ void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)if (!policy->fast_switch_possible)return ; +/** +* It's not expected driver's fast_switch callback is not set +* even fast_switch_possible is true. +*/ +if (WARN_ON (!cpufreq_driver_has_fast_switch ())) +return ; +mutex_lock (&cpufreq_fast_switch_lock);if (cpufreq_fast_switch_count >=0 ) { cpufreq_fast_switch_count++;@@ -2143,6 +2150,17 @@ unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, }EXPORT_SYMBOL_GPL (cpufreq_driver_fast_switch); +/** +* cpufreq_driver_has_fast_switch - Check "fast switch" callback. +* +* Return 'true' if the ->fast_switch callback is present for the +* current driver or 'false' otherwise. +*/ +bool cpufreq_driver_has_fast_switch (void ) +{ +return !!cpufreq_driver->fast_switch; +} +/** * cpufreq_driver_adjust_perf - Adjust CPU performance level in one go. * @cpu: Target CPU. @@ -2157,7 +2175,7 @@ EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);* and it is expected to select a suitable performance level equal to or above * @min_perf and preferably equal to or below @target_perf. * -* This function must not be called if policy->fast_switch_enabled is unset. +* By default this function takes the fast frequency update path. * * Governors calling this function must guarantee that it will never be invoked * twice in parallel for the same CPU and that it will never be called in drivers/cpufreq/intel_pstate.c +0
-2@@ -2698,8 +2698,6 @@ static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)intel_pstate_init_acpi_perf_limits (policy); - policy->fast_switch_possible = true; -return 0 ; }
Quoted 5 lines
drivers/cpufreq/intel_pstate.c static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 2956
Quoted 5 lines
drivers/cpufreq/intel_pstate.c +1
-0@@ -2955,6 +2953,7 @@ static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)if (ret)return ret; + policy->fast_switch_possible = true;
I'm not sure what this is about. Is it a cleanup of intel_pstate?
Quoted 29 lines
Quoted 29 lines
drivers/cpufreq/intel_pstate.c +0
-0policy->cpuinfo.transition_latency =INTEL_CPUFREQ_TRANSITION_LATENCY ;/* This reflects the intel_pstate_get_cpu_pstates() setting. */ policy->cur = policy->cpuinfo.min_freq;include/linux/cpufreq.h +1
-0@@ -604,6 +604,7 @@ struct cpufreq_governor {/* Pass a target to the cpufreq driver */ unsigned int cpufreq_driver_fast_switch (struct cpufreq_policy *policy,unsigned int target_freq); +bool cpufreq_driver_has_fast_switch (void ); void cpufreq_driver_adjust_perf(unsigned int cpu, unsigned long min_perf, unsigned long target_perf,kernel/sched/cpufreq_schedutil.c +1
-2@@ -776,7 +776,7 @@ static int sugov_start(struct cpufreq_policy *policy)if (policy_is_shared (policy)) uu = sugov_update_shared; -else if (policy->fast_switch_enabled &&cpufreq_driver_has_adjust_perf ()) +else if (cpufreq_driver_has_adjust_perf ()) uu = sugov_update_single_perf;else uu = sugov_update_single_freq; --
Re: [PATCH v2 1/2] cpufreq/schedutil: Remove fast_switch_possible flag if driver doesn't set fast_switch
Hi Rafael,
Thanks for reviewing the patch.
On 09 May 20:39, Rafael J. Wysocki wrote:
------------------------------------------>8--------------------------------------
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
drivers/cpufreq/intel_pstate.c +0
-2@@ -2698,8 +2698,6 @@ static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)intel_pstate_init_acpi_perf_limits (policy); - policy->fast_switch_possible = true; -return 0 ; }
Quoted 7 lines
Quoted 5 lines
drivers/cpufreq/intel_pstate.c static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 2956
Quoted 5 lines
drivers/cpufreq/intel_pstate.c +1
-0@@ -2955,6 +2953,7 @@ static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)if (ret)return ret; + policy->fast_switch_possible = true;I'm not sure what this is about. Is it a cleanup of
intel_pstate?
This patch intends to remove fast_switch_possible flag dependency from drivers which only use adjust_perf as frequency/pref update callback. As intel_pstate and amd_pstate driver has only adjust_perf and not fast_switch, therefore I'm removing that flag from these drivers. But intel_cpufreq has fast_switch therefore, only adding that flag for intel_cpufreq driver.
Thanks & Regards,
Wyes
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
drivers/cpufreq/intel_pstate.c +0
-0policy->cpuinfo.transition_latency =INTEL_CPUFREQ_TRANSITION_LATENCY ;/* This reflects the intel_pstate_get_cpu_pstates() setting. */ policy->cur = policy->cpuinfo.min_freq;include/linux/cpufreq.h +1
-0@@ -604,6 +604,7 @@ struct cpufreq_governor {/* Pass a target to the cpufreq driver */ unsigned int cpufreq_driver_fast_switch (struct cpufreq_policy *policy,unsigned int target_freq); +bool cpufreq_driver_has_fast_switch (void ); void cpufreq_driver_adjust_perf(unsigned int cpu, unsigned long min_perf, unsigned long target_perf,kernel/sched/cpufreq_schedutil.c +1
-2@@ -776,7 +776,7 @@ static int sugov_start(struct cpufreq_policy *policy)if (policy_is_shared (policy)) uu = sugov_update_shared; -else if (policy->fast_switch_enabled &&cpufreq_driver_has_adjust_perf ()) +else if (cpufreq_driver_has_adjust_perf ()) uu = sugov_update_single_perf;else uu = sugov_update_single_freq; --
Rafael J. Wysocki
maintainerRe: [PATCH v2 1/2] cpufreq/schedutil: Remove fast_switch_possible flag if driver doesn't set fast_switch
Quoted 28 lines · Wyes Karny
Hi Rafael,
Thanks for reviewing the patch.
On 09 May 20:39, Rafael J. Wysocki wrote:
------------------------------------------>8--------------------------------------Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
drivers/cpufreq/intel_pstate.c +0
-2@@ -2698,8 +2698,6 @@ static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)intel_pstate_init_acpi_perf_limits (policy); - policy->fast_switch_possible = true; -return 0 ; }Quoted 7 lines
Quoted 5 lines
drivers/cpufreq/intel_pstate.c static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 2956
Quoted 5 lines
drivers/cpufreq/intel_pstate.c +1
-0@@ -2955,6 +2953,7 @@ static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)if (ret)return ret; + policy->fast_switch_possible = true;I'm not sure what this is about. Is it a cleanup of
intel_pstate?
Quoted 6 lines
This patch intends to remove
fast_switch_possibleflag dependency from drivers which only useadjust_perfas frequency/pref update callback. Asintel_pstateandamd_pstatedriver has onlyadjust_perfand notfast_switch, therefore I'm removing that flag from these drivers. Butintel_cpufreqhasfast_switchtherefore, only adding that flag forintel_cpufreqdriver.
But is it really better to change it? It works correctly as-is AFAICS.
In any case, the intel_pstate change should be a separate patch, because it is not directly related to the other changes in the $subject one IMV.
Re: [PATCH v2 1/2] cpufreq/schedutil: Remove fast_switch_possible flag if driver doesn't set fast_switch
In reply to Rafael J. WysockiHi Rafael,
Quoted 40 lines · Rafael J. Wysocki
Quoted 21 lines · Wyes Karny
The set value of
fast_switch_enabledindicates thatfast_switchcallback is set. For some drivers such asamd_pstateandintel_pstate, theadjust_perfcallback is used but it still setsfast_switch_possibleflag. This is because this flag also decides whether schedutil governor selectsadjust_perffunction for frequency update. This condition in the schedutil governor forces the scaling driver to set thefast_switch_possibleflag.Remove
fast_switch_enabledcheck when schedutil decides to selectadjust_perffunction for frequency update. Thus removing this drivers are now free to removefast_switch_possibleflag if they don't usefast_switchcallback.This issue becomes apparent when aperf/mperf overflow occurs. When this happens, kernel disables frequency invariance calculation which causes schedutil to fallback to
sugov_update_single_freqwhich currently relies on thefast_switchcallback.Normal flow:
sugov_update_single_perfQuoted 8 lines
cpufreq_driver_adjust_perf cpufreq_driver->adjust_perfError case flow:
sugov_update_single_perf
sugov_update_single_freq<-- This is chosen because the freq invariant is disabled due to aperf/mperf overflow
cpufreq_driver_fast_switch
cpufreq_driver->fast_switch<-- Here NULL pointer dereference is happening, becausefast_switchis not setSo you need to set
fast_switch.Please read the comment in
sugov_update_single_perf(). It explains whyadjust_perfis not used when scale invariance is not enabled: the mapping between the performance levels and frequency are not generally defined in that case and it is up to the driver to figure out what perf level to use to get the given frequency. And this is exactly whyfast_switchis not optional: because scale invariance may be disabled.
Quoted 2 lines
Please feel free to update the documentation to clarify this, but the way to fix the issue is to implement
fast_switchin the driver.
Thanks for clarifying that fast_swich is not optional as frequency invariance could be disabled in runtime and this would cause to select fast_switch. I'll make those changes.
Thanks,
Wyes
Quoted 137 lines
Quoted 137 lines
Fixes a61dec744745 ("cpufreq: schedutil: Avoid missing updates for one-CPU policies")Signed-off-by Wyes Karny <[email protected]>Cc "Rafael J. Wysocki" <[email protected]>--- drivers/cpufreq/amd-pstate.c | 10 +++++++--- drivers/cpufreq/cpufreq.c | 20 +++++++++++++++++++- drivers/cpufreq/intel_pstate.c | 3 +-- include/linux/cpufreq.h | 1 + kernel/sched/cpufreq_schedutil.c | 2 +- 5 files changed, 29 insertions(+), 7 deletions(-)drivers/cpufreq/amd-pstate.c +7
-3@@ -671,8 +671,14 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)/* It will be updated by governor */ policy->cur = policy->cpuinfo.min_freq; +/** +* For shared memory system frequency update takes time that's why +* do this in deferred kthread context. +*/ if (boot_cpu_has (X86_FEATURE_CPPC )) - policy->fast_switch_possible = true; + current_pstate_driver->adjust_perf = amd_pstate_adjust_perf; +else + current_pstate_driver->adjust_perf =NULL ; ret =freq_qos_add_request (&policy->constraints, &cpudata->req[0 ],FREQ_QOS_MIN , policy->cpuinfo.min_freq);@@ -697,8 +703,6 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy) policy->driver_data = cpudata;amd_pstate_boost_init (cpudata); -if (!current_pstate_driver->adjust_perf) - current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;return 0 ;drivers/cpufreq/cpufreq.c +19
-1@@ -501,6 +501,13 @@ void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)if (!policy->fast_switch_possible)return ; +/** +* It's not expected driver's fast_switch callback is not set +* even fast_switch_possible is true. +*/ +if (WARN_ON (!cpufreq_driver_has_fast_switch ())) +return ; +mutex_lock (&cpufreq_fast_switch_lock);if (cpufreq_fast_switch_count >=0 ) { cpufreq_fast_switch_count++;@@ -2143,6 +2150,17 @@ unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, }EXPORT_SYMBOL_GPL (cpufreq_driver_fast_switch); +/** +* cpufreq_driver_has_fast_switch - Check "fast switch" callback. +* +* Return 'true' if the ->fast_switch callback is present for the +* current driver or 'false' otherwise. +*/ +bool cpufreq_driver_has_fast_switch (void ) +{ +return !!cpufreq_driver->fast_switch; +} +/** * cpufreq_driver_adjust_perf - Adjust CPU performance level in one go. * @cpu: Target CPU. @@ -2157,7 +2175,7 @@ EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);* and it is expected to select a suitable performance level equal to or above * @min_perf and preferably equal to or below @target_perf. * -* This function must not be called if policy->fast_switch_enabled is unset. +* By default this function takes the fast frequency update path. * * Governors calling this function must guarantee that it will never be invoked * twice in parallel for the same CPU and that it will never be called in drivers/cpufreq/intel_pstate.c +1
-2@@ -2698,8 +2698,6 @@ static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)intel_pstate_init_acpi_perf_limits (policy); - policy->fast_switch_possible = true; -return 0 ; }@@ -2955,6 +2953,7 @@ static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)if (ret)return ret; + policy->fast_switch_possible = true; policy->cpuinfo.transition_latency =INTEL_CPUFREQ_TRANSITION_LATENCY ;/* This reflects the intel_pstate_get_cpu_pstates() setting. */ policy->cur = policy->cpuinfo.min_freq;include/linux/cpufreq.h +1
-0@@ -604,6 +604,7 @@ struct cpufreq_governor {/* Pass a target to the cpufreq driver */ unsigned int cpufreq_driver_fast_switch (struct cpufreq_policy *policy,unsigned int target_freq); +bool cpufreq_driver_has_fast_switch (void ); void cpufreq_driver_adjust_perf(unsigned int cpu, unsigned long min_perf, unsigned long target_perf,kernel/sched/cpufreq_schedutil.c +1
-2@@ -776,7 +776,7 @@ static int sugov_start(struct cpufreq_policy *policy)if (policy_is_shared (policy)) uu = sugov_update_shared; -else if (policy->fast_switch_enabled &&cpufreq_driver_has_adjust_perf ()) +else if (cpufreq_driver_has_adjust_perf ()) uu = sugov_update_single_perf;else uu = sugov_update_single_freq; --2.34.1
PATCH v2 2/2 amd_pstate: Add ->fast_switch() callback
In reply to Wyes KarnyFrom: "Gautham R. Shenoy" <[email protected]>
Schedutil normally calls the adjust_perf callback for drivers with adjust_perf callback available and fast_switch_possible flag set. However, when frequency invariance is disabled and schedutil tries to invoke fast_switch, and that callback is NULL, it schedules a kthread to invoke the target() callback, which could slow down the frequency update.
Prevent the frequency update slow down by implementing the fast_switch callback for amd_pstate.
--- drivers/cpufreq/amd-pstate.c | 40 ++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-)
drivers/cpufreq/amd-pstate.c +32 -8
@@ -444,9 +444,8 @@ static int amd_pstate_verify(struct cpufreq_policy_data *policy)return 0 ; } -static int amd_pstate_target (struct cpufreq_policy *policy, -unsigned int target_freq, -unsigned int relation) +static int amd_pstate_update_freq (struct cpufreq_policy *policy, +unsigned int target_freq,bool fast_switch) {struct cpufreq_freqs freqs;struct amd_cpudata *cpudata = policy->driver_data;
@@ -465,14 +464,37 @@ static int amd_pstate_target(struct cpufreq_policy *policy, des_perf =DIV_ROUND_CLOSEST (target_freq * cap_perf, cpudata->max_freq); -cpufreq_freq_transition_begin (policy, &freqs); +WARN_ON (fast_switch && !policy->fast_switch_enabled); +/* +* If fast_switch is desired, then there aren't any registered +* transition notifiers. See comment for +* cpufreq_enable_fast_switch(). +*/ +if (!fast_switch) +cpufreq_freq_transition_begin (policy, &freqs); +amd_pstate_update (cpudata, min_perf, des_perf, - max_perf, false, policy->governor->flags); -cpufreq_freq_transition_end (policy, &freqs, false); + max_perf, fast_switch, policy->governor->flags); + +if (!fast_switch) +cpufreq_freq_transition_end (policy, &freqs, false);return 0 ; } +static int amd_pstate_target (struct cpufreq_policy *policy, +unsigned int target_freq, +unsigned int relation) +{ +return amd_pstate_update_freq (policy, target_freq, false); +} + +static unsigned int amd_pstate_fast_switch (struct cpufreq_policy *policy, +unsigned int target_freq) +{ +return amd_pstate_update_freq (policy, target_freq, true); +} + static void amd_pstate_adjust_perf(unsigned int cpu, unsigned long _min_perf, unsigned long target_perf,
@@ -675,9 +697,10 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)* For shared memory system frequency update takes time that's why * do this in deferred kthread context. */ -if (boot_cpu_has (X86_FEATURE_CPPC )) +if (boot_cpu_has (X86_FEATURE_CPPC )) { current_pstate_driver->adjust_perf = amd_pstate_adjust_perf; -else + policy->fast_switch_possible = true; + }else current_pstate_driver->adjust_perf =NULL ; ret = freq_qos_add_request(&policy->constraints, &cpudata->req[0],
@@ -1313,6 +1336,7 @@ static struct cpufreq_driver amd_pstate_driver = { .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS, .verify = amd_pstate_verify, .target = amd_pstate_target, + .fast_switch = amd_pstate_fast_switch, .init = amd_pstate_cpu_init, .exit = amd_pstate_cpu_exit, .suspend = amd_pstate_cpu_suspend,
Signature · 1 line
2.34.1