Skip to contentSkip to conversation

cpufreq: amd-pstate: avoid uninitialized variable use

PATCH Applied Arnd Bergmann 5 participants 6 messages Feb 7, 2023 – Feb 9, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpufreq: amd-pstate: avoid uninitialized variable use

From: Arnd Bergmann <[email protected]>

The new epp support causes warnings about three separate but related bugs:

1) failing before allocation should just return an error:

drivers/cpufreq/amd-pstate.c:951:6: error: variable 'ret' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

        if (!dev)
            ^~~~
drivers/cpufreq/amd-pstate.c:1018:9: note: uninitialized use occurs here
        return ret;
               ^~~

2) wrong variable to store return code:

drivers/cpufreq/amd-pstate.c:963:6: error: variable 'ret' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

        if (rc)
            ^~
drivers/cpufreq/amd-pstate.c:1019:9: note: uninitialized use occurs here
        return ret;
               ^~~
drivers/cpufreq/amd-pstate.c:963:2: note: remove the 'if' if its condition is always false
        if (rc)
        ^~~~~~~

3) calling amd_pstate_set_epp() in cleanup path after determining that it should not be called:

drivers/cpufreq/amd-pstate.c:1055:6: error: variable 'epp' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

        if (cpudata->epp_policy == cpudata->policy)
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/cpufreq/amd-pstate.c:1080:30: note: uninitialized use occurs here
        amd_pstate_set_epp(cpudata, epp);
                                    ^~~

All three are trivial to fix, but most likely there are additional bugs in this function when the error handling was not really tested.

Fixes ffa5096a7c33 ("cpufreq: amd-pstate: implement Pstate EPP support for the AMD processors")
---
 drivers/cpufreq/amd-pstate.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/cpufreq/amd-pstate.c +4 -5
index 168a28bed6ee..847f5f31396d 100644
@@ -940,7 +940,6 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
 	int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
 	struct amd_cpudata *cpudata;
 	struct device *dev;
-	int rc;
 	u64 value;
 
 	/*
@@ -950,7 +949,7 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
 	amd_perf_ctl_reset(policy->cpu);
 	dev = get_cpu_device(policy->cpu);
 	if (!dev)
-		goto free_cpudata1;
+		return -ENODEV;
 
 	cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
 	if (!cpudata)
@@ -959,8 +958,8 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
 	cpudata->cpu = policy->cpu;
 	cpudata->epp_policy = 0;
 
-	rc = amd_pstate_init_perf(cpudata);
-	if (rc)
+	ret = amd_pstate_init_perf(cpudata);
+	if (ret)
 		goto free_cpudata1;
 
 	min_freq = amd_get_min_freq(cpudata);
@@ -1076,9 +1075,9 @@ static void amd_pstate_epp_init(unsigned int cpu)
 		value |= (u64)epp << 24;
 	}
 
+	amd_pstate_set_epp(cpudata, epp);
 skip_epp:
 	WRITE_ONCE(cpudata->cppc_req_cached, value);
-	amd_pstate_set_epp(cpudata, epp);
 	cpufreq_cpu_put(policy);
 }
 
Signature · 1 line
2.39.1
Tested-by on v1 1/1
#1 lore raw
Quoted 78 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The new epp support causes warnings about three separate but related bugs:

1) failing before allocation should just return an error:

drivers/cpufreq/amd-pstate.c:951:6: error: variable 'ret' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

        if (!dev)
            ^~~~
drivers/cpufreq/amd-pstate.c:1018:9: note: uninitialized use occurs here
        return ret;
               ^~~

2) wrong variable to store return code:

drivers/cpufreq/amd-pstate.c:963:6: error: variable 'ret' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

        if (rc)
            ^~
drivers/cpufreq/amd-pstate.c:1019:9: note: uninitialized use occurs here
        return ret;
               ^~~
drivers/cpufreq/amd-pstate.c:963:2: note: remove the 'if' if its condition is always false
        if (rc)
        ^~~~~~~

3) calling amd_pstate_set_epp() in cleanup path after determining that it should not be called:

drivers/cpufreq/amd-pstate.c:1055:6: error: variable 'epp' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

        if (cpudata->epp_policy == cpudata->policy)
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/cpufreq/amd-pstate.c:1080:30: note: uninitialized use occurs here
        amd_pstate_set_epp(cpudata, epp);
                                    ^~~

All three are trivial to fix, but most likely there are additional bugs in this function when the error handling was not really tested.

Fixes ffa5096a7c33 ("cpufreq: amd-pstate: implement Pstate EPP support for the AMD processors")
---
 drivers/cpufreq/amd-pstate.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/cpufreq/amd-pstate.c +3 -4
index 168a28bed6ee..847f5f31396d 100644
@@ -940,7 +940,6 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
 	int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
 	struct amd_cpudata *cpudata;
 	struct device *dev;
-	int rc;
 	u64 value;
 
 	/*
@@ -950,7 +949,7 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
 	amd_perf_ctl_reset(policy->cpu);
 	dev = get_cpu_device(policy->cpu);
 	if (!dev)
-		goto free_cpudata1;
+		return -ENODEV;
 
 	cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
 	if (!cpudata)
@@ -959,8 +958,8 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
 	cpudata->cpu = policy->cpu;
 	cpudata->epp_policy = 0;
 
-	rc = amd_pstate_init_perf(cpudata);
-	if (rc)
+	ret = amd_pstate_init_perf(cpudata);
+	if (ret)
 		goto free_cpudata1;
 
 	min_freq = amd_get_min_freq(cpudata);

drivers/cpufreq/amd-pstate.c static void amd_pstate_epp_init(unsigned int cpu) · line 1083

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +1 -1
@@ -1076,9 +1075,9 @@ static void amd_pstate_epp_init(unsigned int cpu)
 		value |= (u64)epp << 24;
 	}
 
+	amd_pstate_set_epp(cpudata, epp);
 skip_epp:
 	WRITE_ONCE(cpudata->cppc_req_cached, value);
-	amd_pstate_set_epp(cpudata, epp);
 	cpufreq_cpu_put(policy);
 }
 

Tested this on AMD Milan (Zen 3) system.

Quoted 3 lines
Signature · 1 line
2.39.1
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Arnd Bergmann

[AMD Official Use Only - General]

Hi Arnd,

Quoted 110 lines

-----Original Message-----
From: Arnd Bergmann <[email protected]>
Sent: Wednesday, February 8, 2023 12:13 AM
To: Huang, Ray <[email protected]>; Rafael J. Wysocki <[email protected]>; Viresh Kumar <[email protected]>; Karny, Wyes <[email protected]>; Yuan, Perry <[email protected]>; Limonciello, Mario <[email protected]>
Cc: Arnd Bergmann <[email protected]>; Yuan, Perry <[email protected]>; Limonciello, Mario <[email protected]>; Jinzhou Su <[email protected]>; Meng, Li (Jassmine) <[email protected]>; linux- [email protected]; [email protected]
Subject: [PATCH] cpufreq: amd-pstate: avoid uninitialized variable use

From: Arnd Bergmann <[email protected]>

The new epp support causes warnings about three separate but related bugs:

1) failing before allocation should just return an error:

drivers/cpufreq/amd-pstate.c:951:6: error: variable 'ret' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

        if (!dev)
            ^~~~
drivers/cpufreq/amd-pstate.c:1018:9: note: uninitialized use occurs here
        return ret;
               ^~~

2) wrong variable to store return code:

drivers/cpufreq/amd-pstate.c:963:6: error: variable 'ret' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

        if (rc)
            ^~
drivers/cpufreq/amd-pstate.c:1019:9: note: uninitialized use occurs here
        return ret;
               ^~~
drivers/cpufreq/amd-pstate.c:963:2: note: remove the 'if' if its condition is
always false
        if (rc)
        ^~~~~~~

3) calling amd_pstate_set_epp() in cleanup path after determining that it should not be called:

drivers/cpufreq/amd-pstate.c:1055:6: error: variable 'epp' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes- uninitialized]

        if (cpudata->epp_policy == cpudata->policy)
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/cpufreq/amd-pstate.c:1080:30: note: uninitialized use occurs here
        amd_pstate_set_epp(cpudata, epp);
                                    ^~~

All three are trivial to fix, but most likely there are additional bugs in this function when the error handling was not really tested.

Fixes ffa5096a7c33 ("cpufreq: amd-pstate: implement Pstate EPP support

for the AMD processors")

---
 drivers/cpufreq/amd-pstate.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/cpufreq/amd-pstate.c +0 -1
index 168a28bed6ee..847f5f31396d 100644
@@ -940,7 +940,6 @@ static int amd_pstate_epp_cpu_init(struct
 cpufreq_policy *policy)
 	int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
 	struct amd_cpudata *cpudata;
 	struct device *dev;
-	int rc;
 	u64 value;
 
drivers/cpufreq/amd-pstate.c +1 -1
 	/*
@@ -950,7 +949,7 @@ static int amd_pstate_epp_cpu_init(struct
 cpufreq_policy *policy)
 	amd_perf_ctl_reset(policy->cpu);
 	dev = get_cpu_device(policy->cpu);
 	if (!dev)
-		goto free_cpudata1;
+		return -ENODEV;
 
 	cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
drivers/cpufreq/amd-pstate.c +2 -2
 	if (!cpudata)
@@ -959,8 +958,8 @@ static int amd_pstate_epp_cpu_init(struct
 cpufreq_policy *policy)
 	cpudata->cpu = policy->cpu;
 	cpudata->epp_policy = 0;
 
-	rc = amd_pstate_init_perf(cpudata);
-	if (rc)
+	ret = amd_pstate_init_perf(cpudata);
+	if (ret)
 		goto free_cpudata1;
 
drivers/cpufreq/amd-pstate.c +1 -2
 	min_freq = amd_get_min_freq(cpudata);
@@ -1076,9 +1075,9 @@ static void amd_pstate_epp_init(unsigned int cpu)
 		value |= (u64)epp << 24;
 	}
 
+	amd_pstate_set_epp(cpudata, epp);
 skip_epp:
 	WRITE_ONCE(cpudata->cppc_req_cached, value);
-	amd_pstate_set_epp(cpudata, epp);
 	cpufreq_cpu_put(policy);
 }
 
--
Quoted 1 line

2.39.1

Thanks for your quick fix patch.

maintainer Acked-by on v1 1/1
#3 lore raw
In reply to Arnd Bergmann
Quoted 36 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The new epp support causes warnings about three separate but related bugs:

1) failing before allocation should just return an error:

drivers/cpufreq/amd-pstate.c:951:6: error: variable 'ret' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

        if (!dev)
            ^~~~
drivers/cpufreq/amd-pstate.c:1018:9: note: uninitialized use occurs here
        return ret;
               ^~~

2) wrong variable to store return code:

drivers/cpufreq/amd-pstate.c:963:6: error: variable 'ret' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

        if (rc)
            ^~
drivers/cpufreq/amd-pstate.c:1019:9: note: uninitialized use occurs here
        return ret;
               ^~~
drivers/cpufreq/amd-pstate.c:963:2: note: remove the 'if' if its condition is always false
        if (rc)
        ^~~~~~~

3) calling amd_pstate_set_epp() in cleanup path after determining that it should not be called:

drivers/cpufreq/amd-pstate.c:1055:6: error: variable 'epp' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

        if (cpudata->epp_policy == cpudata->policy)
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/cpufreq/amd-pstate.c:1080:30: note: uninitialized use occurs here
        amd_pstate_set_epp(cpudata, epp);
                                    ^~~
Quoted 5 lines

All three are trivial to fix, but most likely there are additional bugs in this function when the error handling was not really tested.

Fixes ffa5096a7c33 ("cpufreq: amd-pstate: implement Pstate EPP support for the AMD processors")

Thanks!

Quoted 50 lines
---
 drivers/cpufreq/amd-pstate.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/cpufreq/amd-pstate.c +4 -5
index 168a28bed6ee..847f5f31396d 100644
@@ -940,7 +940,6 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
 	int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
 	struct amd_cpudata *cpudata;
 	struct device *dev;
-	int rc;
 	u64 value;
 
 	/*
@@ -950,7 +949,7 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
 	amd_perf_ctl_reset(policy->cpu);
 	dev = get_cpu_device(policy->cpu);
 	if (!dev)
-		goto free_cpudata1;
+		return -ENODEV;
 
 	cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
 	if (!cpudata)
@@ -959,8 +958,8 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
 	cpudata->cpu = policy->cpu;
 	cpudata->epp_policy = 0;
 
-	rc = amd_pstate_init_perf(cpudata);
-	if (rc)
+	ret = amd_pstate_init_perf(cpudata);
+	if (ret)
 		goto free_cpudata1;
 
 	min_freq = amd_get_min_freq(cpudata);
@@ -1076,9 +1075,9 @@ static void amd_pstate_epp_init(unsigned int cpu)
 		value |= (u64)epp << 24;
 	}
 
+	amd_pstate_set_epp(cpudata, epp);
 skip_epp:
 	WRITE_ONCE(cpudata->cppc_req_cached, value);
-	amd_pstate_set_epp(cpudata, epp);
 	cpufreq_cpu_put(policy);
 }
 
Signature · 1 line
2.39.1
maintainer Applied
#4 lore raw
Quoted 88 lines · Huang Rui
Quoted 36 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The new epp support causes warnings about three separate but related bugs:

1) failing before allocation should just return an error:

drivers/cpufreq/amd-pstate.c:951:6: error: variable 'ret' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

        if (!dev)
            ^~~~
drivers/cpufreq/amd-pstate.c:1018:9: note: uninitialized use occurs here
        return ret;
               ^~~

2) wrong variable to store return code:

drivers/cpufreq/amd-pstate.c:963:6: error: variable 'ret' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

        if (rc)
            ^~
drivers/cpufreq/amd-pstate.c:1019:9: note: uninitialized use occurs here
        return ret;
               ^~~
drivers/cpufreq/amd-pstate.c:963:2: note: remove the 'if' if its condition is always false
        if (rc)
        ^~~~~~~

3) calling amd_pstate_set_epp() in cleanup path after determining that it should not be called:

drivers/cpufreq/amd-pstate.c:1055:6: error: variable 'epp' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

        if (cpudata->epp_policy == cpudata->policy)
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/cpufreq/amd-pstate.c:1080:30: note: uninitialized use occurs here
        amd_pstate_set_epp(cpudata, epp);
                                    ^~~
Quoted 5 lines

All three are trivial to fix, but most likely there are additional bugs in this function when the error handling was not really tested.

Fixes ffa5096a7c33 ("cpufreq: amd-pstate: implement Pstate EPP support for the AMD processors")

Thanks!

Quoted 40 lines
---
 drivers/cpufreq/amd-pstate.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/cpufreq/amd-pstate.c +3 -4
index 168a28bed6ee..847f5f31396d 100644
@@ -940,7 +940,6 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
      int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
      struct amd_cpudata *cpudata;
      struct device *dev;
-     int rc;
      u64 value;
 
      /*
@@ -950,7 +949,7 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
      amd_perf_ctl_reset(policy->cpu);
      dev = get_cpu_device(policy->cpu);
      if (!dev)
-             goto free_cpudata1;
+             return -ENODEV;
 
      cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
      if (!cpudata)
@@ -959,8 +958,8 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
      cpudata->cpu = policy->cpu;
      cpudata->epp_policy = 0;
 
-     rc = amd_pstate_init_perf(cpudata);
-     if (rc)
+     ret = amd_pstate_init_perf(cpudata);
+     if (ret)
              goto free_cpudata1;
 
      min_freq = amd_get_min_freq(cpudata);
@@ -1076,9 +1075,9 @@ static void amd_pstate_epp_init(unsigned int cpu)
              value |= (u64)epp << 24;
      }
 
Quoted 8 lines

drivers/cpufreq/amd-pstate.c static void amd_pstate_epp_init(unsigned int cpu)

Quoted 8 lines
drivers/cpufreq/amd-pstate.c +1 -2
+     amd_pstate_set_epp(cpudata, epp);
 skip_epp:
      WRITE_ONCE(cpudata->cppc_req_cached, value);
-     amd_pstate_set_epp(cpudata, epp);
      cpufreq_cpu_put(policy);
 }
 
--

Applied, thanks!

#5 lore raw
In reply to Arnd Bergmann

Hi Arnd, Ray, Perry,

drivers/cpufreq/amd-pstate.c static void amd_pstate_epp_init(unsigned int cpu) · line 1082

Quoted 10 lines · Arnd Bergmann
drivers/cpufreq/amd-pstate.c +1 -1
@@ -1076,9 +1075,9 @@ static void amd_pstate_epp_init(unsigned int cpu)
 		value |= (u64)epp << 24;
 	}
 
+	amd_pstate_set_epp(cpudata, epp);
 skip_epp:
 	WRITE_ONCE(cpudata->cppc_req_cached, value);
-	amd_pstate_set_epp(cpudata, epp);
 	cpufreq_cpu_put(policy);
 }

I see an issue here with MSR-based systems.
Here EPP is being set before updating cppc_req_cached and for MSR based systems while updating EPP value, MIN_PERF and MAX_PERF also updated. So, here whatever value MSR_AMD_CPPC_REQ had for min and max perf that will be rewritten instead of highest and lowest perf from MSR_AMD_CPPC_CAP1.

Can we do something like this:

@@ -1053,32 +1052,34 @@ static void amd_pstate_epp_init(unsigned int cpu)

        value &= ~AMD_CPPC_DES_PERF(~0L);
        value |= AMD_CPPC_DES_PERF(0);

+ /* No need to set epp again if previous and current policy is same */

        if (cpudata->epp_policy == cpudata->policy)
                goto skip_epp;

        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;
        }

-skip_epp:

        WRITE_ONCE(cpudata->cppc_req_cached, value);
        amd_pstate_set_epp(cpudata, epp);
+skip_epp:
        cpufreq_cpu_put(policy);

}

Quoted 4 lines
Signature · 2 lines
-- 
2.39.1