[PATCH -next] cpufreq: CPPC: Fix wrong return value in cppc_get_cpu_cost()

Jinjie Ruan posted 1 patch 2 weeks, 4 days ago
drivers/cpufreq/cppc_cpufreq.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH -next] cpufreq: CPPC: Fix wrong return value in cppc_get_cpu_cost()
Posted by Jinjie Ruan 2 weeks, 4 days ago
cppc_get_cpu_cost() return 0 if the policy is NULL. Then in
em_compute_costs(), the later zero check for cost is not valid
as cost is uninitialized. As Quentin pointed out, kernel energy model
core check the return value of get_cost() first, so if the callback
failed it should tell the core. Return -EINVAL to fix it.

Fixes: 1a1374bb8c59 ("cpufreq: CPPC: Fix possible null-ptr-deref for cppc_get_cpu_cost()")
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Closes: https://lore.kernel.org/all/c4765377-7830-44c2-84fa-706b6e304e10@stanley.mountain/
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
Suggested-by: Quentin Perret <qperret@google.com>
---
 drivers/cpufreq/cppc_cpufreq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/cpufreq/cppc_cpufreq.c b/drivers/cpufreq/cppc_cpufreq.c
index aa5dd194fc5b..cdc569cf7743 100644
--- a/drivers/cpufreq/cppc_cpufreq.c
+++ b/drivers/cpufreq/cppc_cpufreq.c
@@ -475,7 +475,7 @@ static int cppc_get_cpu_cost(struct device *cpu_dev, unsigned long KHz,
 
 	policy = cpufreq_cpu_get_raw(cpu_dev->id);
 	if (!policy)
-		return 0;
+		return -EINVAL;
 
 	cpu_data = policy->driver_data;
 	perf_caps = &cpu_data->perf_caps;
-- 
2.34.1
Re: [PATCH -next] cpufreq: CPPC: Fix wrong return value in cppc_get_cpu_cost()
Posted by Viresh Kumar 1 week, 6 days ago
On 06-11-24, 09:12, Jinjie Ruan wrote:
> cppc_get_cpu_cost() return 0 if the policy is NULL. Then in
> em_compute_costs(), the later zero check for cost is not valid
> as cost is uninitialized. As Quentin pointed out, kernel energy model
> core check the return value of get_cost() first, so if the callback
> failed it should tell the core. Return -EINVAL to fix it.
> 
> Fixes: 1a1374bb8c59 ("cpufreq: CPPC: Fix possible null-ptr-deref for cppc_get_cpu_cost()")
> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> Closes: https://lore.kernel.org/all/c4765377-7830-44c2-84fa-706b6e304e10@stanley.mountain/
> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
> Suggested-by: Quentin Perret <qperret@google.com>
> ---
>  drivers/cpufreq/cppc_cpufreq.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/cpufreq/cppc_cpufreq.c b/drivers/cpufreq/cppc_cpufreq.c
> index aa5dd194fc5b..cdc569cf7743 100644
> --- a/drivers/cpufreq/cppc_cpufreq.c
> +++ b/drivers/cpufreq/cppc_cpufreq.c
> @@ -475,7 +475,7 @@ static int cppc_get_cpu_cost(struct device *cpu_dev, unsigned long KHz,
>  
>  	policy = cpufreq_cpu_get_raw(cpu_dev->id);
>  	if (!policy)
> -		return 0;
> +		return -EINVAL;
>  
>  	cpu_data = policy->driver_data;
>  	perf_caps = &cpu_data->perf_caps;

Applied. Thanks.

-- 
viresh