linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] cpufreq: amd_freq_sensitivity: Fix sensitivity clamping in amd_powersave_bias_target
@ 2025-12-02 12:44 Thorsten Blum
  2025-12-02 19:09 ` David Laight
  0 siblings, 1 reply; 4+ messages in thread
From: Thorsten Blum @ 2025-12-02 12:44 UTC (permalink / raw)
  To: Rafael J. Wysocki, Viresh Kumar, Thomas Renninger,
	Borislav Petkov, Jacob Shin
  Cc: Thorsten Blum, stable, Rafael J. Wysocki, linux-pm, linux-kernel

The local variable 'sensitivity' was never clamped to 0 or
POWERSAVE_BIAS_MAX because the return value of clamp() was not used. Fix
this by assigning the clamped value back to 'sensitivity'.

Cc: stable@vger.kernel.org
Fixes: 9c5320c8ea8b ("cpufreq: AMD "frequency sensitivity feedback" powersave bias for ondemand governor")
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 drivers/cpufreq/amd_freq_sensitivity.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/cpufreq/amd_freq_sensitivity.c b/drivers/cpufreq/amd_freq_sensitivity.c
index 13fed4b9e02b..713ccf24c97d 100644
--- a/drivers/cpufreq/amd_freq_sensitivity.c
+++ b/drivers/cpufreq/amd_freq_sensitivity.c
@@ -76,7 +76,7 @@ static unsigned int amd_powersave_bias_target(struct cpufreq_policy *policy,
 	sensitivity = POWERSAVE_BIAS_MAX -
 		(POWERSAVE_BIAS_MAX * (d_reference - d_actual) / d_reference);
 
-	clamp(sensitivity, 0, POWERSAVE_BIAS_MAX);
+	sensitivity = clamp(sensitivity, 0, POWERSAVE_BIAS_MAX);
 
 	/* this workload is not CPU bound, so choose a lower freq */
 	if (sensitivity < od_tuners->powersave_bias) {
-- 
Thorsten Blum <thorsten.blum@linux.dev>
GPG: 1D60 735E 8AEF 3BE4 73B6  9D84 7336 78FD 8DFE EAD4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] cpufreq: amd_freq_sensitivity: Fix sensitivity clamping in amd_powersave_bias_target
  2025-12-02 12:44 [PATCH] cpufreq: amd_freq_sensitivity: Fix sensitivity clamping in amd_powersave_bias_target Thorsten Blum
@ 2025-12-02 19:09 ` David Laight
  2025-12-19 15:55   ` Thorsten Blum
  0 siblings, 1 reply; 4+ messages in thread
From: David Laight @ 2025-12-02 19:09 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Rafael J. Wysocki, Viresh Kumar, Thomas Renninger,
	Borislav Petkov, Jacob Shin, stable, Rafael J. Wysocki, linux-pm,
	linux-kernel

On Tue,  2 Dec 2025 13:44:28 +0100
Thorsten Blum <thorsten.blum@linux.dev> wrote:

> The local variable 'sensitivity' was never clamped to 0 or
> POWERSAVE_BIAS_MAX because the return value of clamp() was not used. Fix
> this by assigning the clamped value back to 'sensitivity'.

This actually makes no difference
(assuming od_tuners->powersave_bias <= POWERSAVE_BIAS_MAX).
The only use of 'sensitivity' is the test at the end of the diff.

So I think you could just delete the line.

	David
 
> 
> Cc: stable@vger.kernel.org
> Fixes: 9c5320c8ea8b ("cpufreq: AMD "frequency sensitivity feedback" powersave bias for ondemand governor")
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
>  drivers/cpufreq/amd_freq_sensitivity.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/cpufreq/amd_freq_sensitivity.c b/drivers/cpufreq/amd_freq_sensitivity.c
> index 13fed4b9e02b..713ccf24c97d 100644
> --- a/drivers/cpufreq/amd_freq_sensitivity.c
> +++ b/drivers/cpufreq/amd_freq_sensitivity.c
> @@ -76,7 +76,7 @@ static unsigned int amd_powersave_bias_target(struct cpufreq_policy *policy,
>  	sensitivity = POWERSAVE_BIAS_MAX -
>  		(POWERSAVE_BIAS_MAX * (d_reference - d_actual) / d_reference);
>  
> -	clamp(sensitivity, 0, POWERSAVE_BIAS_MAX);
> +	sensitivity = clamp(sensitivity, 0, POWERSAVE_BIAS_MAX);
>  
>  	/* this workload is not CPU bound, so choose a lower freq */
>  	if (sensitivity < od_tuners->powersave_bias) {


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] cpufreq: amd_freq_sensitivity: Fix sensitivity clamping in amd_powersave_bias_target
  2025-12-02 19:09 ` David Laight
@ 2025-12-19 15:55   ` Thorsten Blum
  2025-12-23 13:23     ` Thorsten Blum
  0 siblings, 1 reply; 4+ messages in thread
From: Thorsten Blum @ 2025-12-19 15:55 UTC (permalink / raw)
  To: David Laight
  Cc: Rafael J. Wysocki, Viresh Kumar, Thomas Renninger,
	Borislav Petkov, Jacob Shin, stable, Rafael J. Wysocki, linux-pm,
	linux-kernel

On 2. Dec 2025, at 20:09, David Laight wrote:
> On Tue,  2 Dec 2025 13:44:28 +0100
> Thorsten Blum <thorsten.blum@linux.dev> wrote:
> 
>> The local variable 'sensitivity' was never clamped to 0 or
>> POWERSAVE_BIAS_MAX because the return value of clamp() was not used. Fix
>> this by assigning the clamped value back to 'sensitivity'.
> 
> This actually makes no difference
> (assuming od_tuners->powersave_bias <= POWERSAVE_BIAS_MAX).
> The only use of 'sensitivity' is the test at the end of the diff.
> 
> So I think you could just delete the line.

The local variable 'sensitivity' is an 'int', while '->powersave_bias'
is an 'unsigned int'. If 'sensitivity' were ever negative, it would be
converted to an 'unsigned int', producing an incorrect result. That's
probably what the clamping was meant to prevent.

However, calculating 'sensitivity' can be simplified from:

	sensitivity = POWERSAVE_BIAS_MAX -
		(POWERSAVE_BIAS_MAX * (d_reference - d_actual) / d_reference);

to:

	sensitivity = POWERSAVE_BIAS_MAX * d_actual / d_reference;

which makes it clearer that, in practice, 'sensitivity' is never
negative. How about simplifying the formula as above, changing
'sensitivity' to 'unsigned int', and removing the clamping?

Thanks,
Thorsten


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] cpufreq: amd_freq_sensitivity: Fix sensitivity clamping in amd_powersave_bias_target
  2025-12-19 15:55   ` Thorsten Blum
@ 2025-12-23 13:23     ` Thorsten Blum
  0 siblings, 0 replies; 4+ messages in thread
From: Thorsten Blum @ 2025-12-23 13:23 UTC (permalink / raw)
  To: David Laight
  Cc: Rafael J. Wysocki, Viresh Kumar, Thomas Renninger,
	Borislav Petkov, Jacob Shin, stable, Rafael J. Wysocki, linux-pm,
	linux-kernel

On 19. Dec 2025, at 16:55, Thorsten Blum wrote:
> On 2. Dec 2025, at 20:09, David Laight wrote:
>> On Tue,  2 Dec 2025 13:44:28 +0100
>> Thorsten Blum <thorsten.blum@linux.dev> wrote:
>> 
>>> The local variable 'sensitivity' was never clamped to 0 or
>>> POWERSAVE_BIAS_MAX because the return value of clamp() was not used. Fix
>>> this by assigning the clamped value back to 'sensitivity'.
>> 
>> This actually makes no difference
>> (assuming od_tuners->powersave_bias <= POWERSAVE_BIAS_MAX).
>> The only use of 'sensitivity' is the test at the end of the diff.
>> 
>> So I think you could just delete the line.
> 
> The local variable 'sensitivity' is an 'int', while '->powersave_bias'
> is an 'unsigned int'. If 'sensitivity' were ever negative, it would be
> converted to an 'unsigned int', producing an incorrect result. That's
> probably what the clamping was meant to prevent.
> 
> However, calculating 'sensitivity' can be simplified from:
> 
> 	sensitivity = POWERSAVE_BIAS_MAX -
> 		(POWERSAVE_BIAS_MAX * (d_reference - d_actual) / d_reference);
> 
> to:
> 
> 	sensitivity = POWERSAVE_BIAS_MAX * d_actual / d_reference;
> 
> which makes it clearer that, in practice, 'sensitivity' is never
> negative. How about simplifying the formula as above, changing
> 'sensitivity' to 'unsigned int', and removing the clamping?

Hm, changing the formula could alter the integer arithmetic, potentially
producing slightly different results, and might even overflow.

I guess we should keep the formula as is and either defensively clamp
'sensitivity', as originally intended, or just remove the line (since
this seems to have been working since 2013 and as suggested by David).

I'm slightly in favor of clamping the value because the assumptions
aren't obvious to me.

Any preferences or other suggestions?

Thanks,
Thorsten


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-12-23 13:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-02 12:44 [PATCH] cpufreq: amd_freq_sensitivity: Fix sensitivity clamping in amd_powersave_bias_target Thorsten Blum
2025-12-02 19:09 ` David Laight
2025-12-19 15:55   ` Thorsten Blum
2025-12-23 13:23     ` Thorsten Blum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).