diff mbox series

cpufreq: amd_pstate: Fix invalid write to MSR_AMD_CPPC_REQ

Message ID 20230214075811.23644-1-wyes.karny@amd.com (mailing list archive)
State Mainlined, archived
Headers show
Series cpufreq: amd_pstate: Fix invalid write to MSR_AMD_CPPC_REQ | expand

Commit Message

Wyes Karny Feb. 14, 2023, 7:58 a.m. UTC
`amd_pstate_set_epp` function uses `cppc_req_cached` and `epp` variable
to update the MSR_AMD_CPPC_REQ register for AMD MSR systems. The recent
commit 7cca9a9851a5 ("cpufreq: amd-pstate: avoid uninitialized variable
use") changed the sequence of updating cppc_req_cached and writing the
MSR_AMD_CPPC_REQ. Therefore while switching from powersave to
performance governor and vice-versa in active mode MSR_AMD_CPPC_REQ is
set with the previous cached value. To fix this: first update the
`cppc_req_cached` variable and then call `amd_pstate_set_epp` function.

- Before commit 7cca9a9851a5 ("cpufreq: amd-pstate: avoid uninitialized
variable use"):

With powersave governor:
[    1.652743] amd_pstate_epp_init: writing to cppc_req_cached = 0x1eff
[    1.652744] amd_pstate_set_epp: writing cppc_req_cached = 0x1eff
[    1.652746] amd_pstate_set_epp: writing min_perf = 30, des_perf = 0, max_perf = 255, epp = 0

Changing to performance governor:
[  300.493842] amd_pstate_epp_init: writing to cppc_req_cached = 0xffff
[  300.493846] amd_pstate_set_epp: writing cppc_req_cached = 0xffff
[  300.493847] amd_pstate_set_epp: writing min_perf = 255, des_perf = 0, max_perf = 255, epp = 0

- After commit 7cca9a9851a5 ("cpufreq: amd-pstate: avoid uninitialized
variable use"):

With powersave governor:
[    1.646037] amd_pstate_set_epp: writing cppc_req_cached = 0xffff
[    1.646038] amd_pstate_set_epp: writing min_perf = 255, des_perf = 0, max_perf = 255, epp = 0
[    1.646042] amd_pstate_epp_init: writing to cppc_req_cached = 0x1eff

Changing to performance governor:
[  687.117401] amd_pstate_set_epp: writing cppc_req_cached = 0x1eff
[  687.117405] amd_pstate_set_epp: writing min_perf = 30, des_perf = 0, max_perf = 255, epp = 0
[  687.117419] amd_pstate_epp_init: writing to cppc_req_cached = 0xffff

- After this fix:

With powersave governor:
[    2.525717] amd_pstate_epp_init: writing to cppc_req_cached = 0x1eff
[    2.525720] amd_pstate_set_epp: writing cppc_req_cached = 0x1eff
[    2.525722] amd_pstate_set_epp: writing min_perf = 30, des_perf = 0, max_perf = 255, epp = 0

Changing to performance governor:
[ 3440.152468] amd_pstate_epp_init: writing to cppc_req_cached = 0xffff
[ 3440.152473] amd_pstate_set_epp: writing cppc_req_cached = 0xffff
[ 3440.152474] amd_pstate_set_epp: writing min_perf = 255, des_perf = 0, max_perf = 255, epp = 0

Fixes: 7cca9a9851a5 ("cpufreq: amd-pstate: avoid uninitialized variable use")
Signed-off-by: Wyes Karny <wyes.karny@amd.com>
---
 drivers/cpufreq/amd-pstate.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

Comments

Huang Rui Feb. 15, 2023, 12:34 p.m. UTC | #1
On Tue, Feb 14, 2023 at 03:58:11PM +0800, Karny, Wyes wrote:
> `amd_pstate_set_epp` function uses `cppc_req_cached` and `epp` variable
> to update the MSR_AMD_CPPC_REQ register for AMD MSR systems. The recent
> commit 7cca9a9851a5 ("cpufreq: amd-pstate: avoid uninitialized variable
> use") changed the sequence of updating cppc_req_cached and writing the
> MSR_AMD_CPPC_REQ. Therefore while switching from powersave to
> performance governor and vice-versa in active mode MSR_AMD_CPPC_REQ is
> set with the previous cached value. To fix this: first update the
> `cppc_req_cached` variable and then call `amd_pstate_set_epp` function.
> 
> - Before commit 7cca9a9851a5 ("cpufreq: amd-pstate: avoid uninitialized
> variable use"):
> 
> With powersave governor:
> [    1.652743] amd_pstate_epp_init: writing to cppc_req_cached = 0x1eff
> [    1.652744] amd_pstate_set_epp: writing cppc_req_cached = 0x1eff
> [    1.652746] amd_pstate_set_epp: writing min_perf = 30, des_perf = 0, max_perf = 255, epp = 0
> 
> Changing to performance governor:
> [  300.493842] amd_pstate_epp_init: writing to cppc_req_cached = 0xffff
> [  300.493846] amd_pstate_set_epp: writing cppc_req_cached = 0xffff
> [  300.493847] amd_pstate_set_epp: writing min_perf = 255, des_perf = 0, max_perf = 255, epp = 0
> 
> - After commit 7cca9a9851a5 ("cpufreq: amd-pstate: avoid uninitialized
> variable use"):
> 
> With powersave governor:
> [    1.646037] amd_pstate_set_epp: writing cppc_req_cached = 0xffff
> [    1.646038] amd_pstate_set_epp: writing min_perf = 255, des_perf = 0, max_perf = 255, epp = 0
> [    1.646042] amd_pstate_epp_init: writing to cppc_req_cached = 0x1eff
> 
> Changing to performance governor:
> [  687.117401] amd_pstate_set_epp: writing cppc_req_cached = 0x1eff
> [  687.117405] amd_pstate_set_epp: writing min_perf = 30, des_perf = 0, max_perf = 255, epp = 0
> [  687.117419] amd_pstate_epp_init: writing to cppc_req_cached = 0xffff
> 
> - After this fix:
> 
> With powersave governor:
> [    2.525717] amd_pstate_epp_init: writing to cppc_req_cached = 0x1eff
> [    2.525720] amd_pstate_set_epp: writing cppc_req_cached = 0x1eff
> [    2.525722] amd_pstate_set_epp: writing min_perf = 30, des_perf = 0, max_perf = 255, epp = 0
> 
> Changing to performance governor:
> [ 3440.152468] amd_pstate_epp_init: writing to cppc_req_cached = 0xffff
> [ 3440.152473] amd_pstate_set_epp: writing cppc_req_cached = 0xffff
> [ 3440.152474] amd_pstate_set_epp: writing min_perf = 255, des_perf = 0, max_perf = 255, epp = 0
> 
> Fixes: 7cca9a9851a5 ("cpufreq: amd-pstate: avoid uninitialized variable use")
> Signed-off-by: Wyes Karny <wyes.karny@amd.com>

That's really nice catch! Thanks Wyes.

Acked-by: Huang Rui <ray.huang@amd.com>

> ---
>  drivers/cpufreq/amd-pstate.c | 25 +++++++++++++------------
>  1 file changed, 13 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
> index b8862afef4e4..45c88894fd8e 100644
> --- a/drivers/cpufreq/amd-pstate.c
> +++ b/drivers/cpufreq/amd-pstate.c
> @@ -1057,27 +1057,28 @@ static void amd_pstate_epp_init(unsigned int cpu)
>  
>  	cpudata->epp_policy = cpudata->policy;
>  
> -	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
> -		epp = amd_pstate_get_epp(cpudata, value);
> -		if (epp < 0)
> -			goto skip_epp;
> -		/* force the epp value to be zero for performance policy */
> -		epp = 0;
> -	} else {
> -		/* Get BIOS pre-defined epp value */
> -		epp = amd_pstate_get_epp(cpudata, value);
> -		if (epp)
> -			goto skip_epp;
> +	/* Get BIOS pre-defined epp value */
> +	epp = amd_pstate_get_epp(cpudata, value);
> +	if (epp < 0) {
> +		/**
> +		 * This return value can only be negative for shared_memory
> +		 * systems where EPP register read/write not supported.
> +		 */
> +		goto skip_epp;
>  	}
> +
> +	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
> +		epp = 0;
> +
>  	/* Set initial EPP value */
>  	if (boot_cpu_has(X86_FEATURE_CPPC)) {
>  		value &= ~GENMASK_ULL(31, 24);
>  		value |= (u64)epp << 24;
>  	}
>  
> +	WRITE_ONCE(cpudata->cppc_req_cached, value);
>  	amd_pstate_set_epp(cpudata, epp);
>  skip_epp:
> -	WRITE_ONCE(cpudata->cppc_req_cached, value);
>  	cpufreq_cpu_put(policy);
>  }
>  
> -- 
> 2.34.1
>
Rafael J. Wysocki Feb. 15, 2023, 2:55 p.m. UTC | #2
On Wed, Feb 15, 2023 at 1:35 PM Huang Rui <ray.huang@amd.com> wrote:
>
> On Tue, Feb 14, 2023 at 03:58:11PM +0800, Karny, Wyes wrote:
> > `amd_pstate_set_epp` function uses `cppc_req_cached` and `epp` variable
> > to update the MSR_AMD_CPPC_REQ register for AMD MSR systems. The recent
> > commit 7cca9a9851a5 ("cpufreq: amd-pstate: avoid uninitialized variable
> > use") changed the sequence of updating cppc_req_cached and writing the
> > MSR_AMD_CPPC_REQ. Therefore while switching from powersave to
> > performance governor and vice-versa in active mode MSR_AMD_CPPC_REQ is
> > set with the previous cached value. To fix this: first update the
> > `cppc_req_cached` variable and then call `amd_pstate_set_epp` function.
> >
> > - Before commit 7cca9a9851a5 ("cpufreq: amd-pstate: avoid uninitialized
> > variable use"):
> >
> > With powersave governor:
> > [    1.652743] amd_pstate_epp_init: writing to cppc_req_cached = 0x1eff
> > [    1.652744] amd_pstate_set_epp: writing cppc_req_cached = 0x1eff
> > [    1.652746] amd_pstate_set_epp: writing min_perf = 30, des_perf = 0, max_perf = 255, epp = 0
> >
> > Changing to performance governor:
> > [  300.493842] amd_pstate_epp_init: writing to cppc_req_cached = 0xffff
> > [  300.493846] amd_pstate_set_epp: writing cppc_req_cached = 0xffff
> > [  300.493847] amd_pstate_set_epp: writing min_perf = 255, des_perf = 0, max_perf = 255, epp = 0
> >
> > - After commit 7cca9a9851a5 ("cpufreq: amd-pstate: avoid uninitialized
> > variable use"):
> >
> > With powersave governor:
> > [    1.646037] amd_pstate_set_epp: writing cppc_req_cached = 0xffff
> > [    1.646038] amd_pstate_set_epp: writing min_perf = 255, des_perf = 0, max_perf = 255, epp = 0
> > [    1.646042] amd_pstate_epp_init: writing to cppc_req_cached = 0x1eff
> >
> > Changing to performance governor:
> > [  687.117401] amd_pstate_set_epp: writing cppc_req_cached = 0x1eff
> > [  687.117405] amd_pstate_set_epp: writing min_perf = 30, des_perf = 0, max_perf = 255, epp = 0
> > [  687.117419] amd_pstate_epp_init: writing to cppc_req_cached = 0xffff
> >
> > - After this fix:
> >
> > With powersave governor:
> > [    2.525717] amd_pstate_epp_init: writing to cppc_req_cached = 0x1eff
> > [    2.525720] amd_pstate_set_epp: writing cppc_req_cached = 0x1eff
> > [    2.525722] amd_pstate_set_epp: writing min_perf = 30, des_perf = 0, max_perf = 255, epp = 0
> >
> > Changing to performance governor:
> > [ 3440.152468] amd_pstate_epp_init: writing to cppc_req_cached = 0xffff
> > [ 3440.152473] amd_pstate_set_epp: writing cppc_req_cached = 0xffff
> > [ 3440.152474] amd_pstate_set_epp: writing min_perf = 255, des_perf = 0, max_perf = 255, epp = 0
> >
> > Fixes: 7cca9a9851a5 ("cpufreq: amd-pstate: avoid uninitialized variable use")
> > Signed-off-by: Wyes Karny <wyes.karny@amd.com>
>
> That's really nice catch! Thanks Wyes.
>
> Acked-by: Huang Rui <ray.huang@amd.com>

Applied, thanks!
diff mbox series

Patch

diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
index b8862afef4e4..45c88894fd8e 100644
--- a/drivers/cpufreq/amd-pstate.c
+++ b/drivers/cpufreq/amd-pstate.c
@@ -1057,27 +1057,28 @@  static void amd_pstate_epp_init(unsigned int cpu)
 
 	cpudata->epp_policy = cpudata->policy;
 
-	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
-		epp = amd_pstate_get_epp(cpudata, value);
-		if (epp < 0)
-			goto skip_epp;
-		/* force the epp value to be zero for performance policy */
-		epp = 0;
-	} else {
-		/* Get BIOS pre-defined epp value */
-		epp = amd_pstate_get_epp(cpudata, value);
-		if (epp)
-			goto skip_epp;
+	/* Get BIOS pre-defined epp value */
+	epp = amd_pstate_get_epp(cpudata, value);
+	if (epp < 0) {
+		/**
+		 * This return value can only be negative for shared_memory
+		 * systems where EPP register read/write not supported.
+		 */
+		goto skip_epp;
 	}
+
+	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
+		epp = 0;
+
 	/* Set initial EPP value */
 	if (boot_cpu_has(X86_FEATURE_CPPC)) {
 		value &= ~GENMASK_ULL(31, 24);
 		value |= (u64)epp << 24;
 	}
 
+	WRITE_ONCE(cpudata->cppc_req_cached, value);
 	amd_pstate_set_epp(cpudata, epp);
 skip_epp:
-	WRITE_ONCE(cpudata->cppc_req_cached, value);
 	cpufreq_cpu_put(policy);
 }