Skip to content

Commit fe2535a

Browse files
committed
cpufreq: CPPC: Fix potential memleak in cppc_cpufreq_cpu_init
It's a classic example of memleak, we allocate something, we fail and never free the resources. Make sure we free all resources on policy ->init() failures. Fixes: a28b2bf ("cppc_cpufreq: replace per-cpu data array with a list") Tested-by: Vincent Guittot <[email protected]> Reviewed-by: Ionela Voinescu <[email protected]> Tested-by: Qian Cai <[email protected]> Signed-off-by: Viresh Kumar <[email protected]>
1 parent 9357a38 commit fe2535a

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

drivers/cpufreq/cppc_cpufreq.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,16 @@ static struct cppc_cpudata *cppc_cpufreq_get_cpu_data(unsigned int cpu)
256256
return NULL;
257257
}
258258

259+
static void cppc_cpufreq_put_cpu_data(struct cpufreq_policy *policy)
260+
{
261+
struct cppc_cpudata *cpu_data = policy->driver_data;
262+
263+
list_del(&cpu_data->node);
264+
free_cpumask_var(cpu_data->shared_cpu_map);
265+
kfree(cpu_data);
266+
policy->driver_data = NULL;
267+
}
268+
259269
static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
260270
{
261271
unsigned int cpu = policy->cpu;
@@ -309,7 +319,8 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
309319
default:
310320
pr_debug("Unsupported CPU co-ord type: %d\n",
311321
policy->shared_type);
312-
return -EFAULT;
322+
ret = -EFAULT;
323+
goto out;
313324
}
314325

315326
/*
@@ -324,10 +335,16 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
324335
cpu_data->perf_ctrls.desired_perf = caps->highest_perf;
325336

326337
ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
327-
if (ret)
338+
if (ret) {
328339
pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
329340
caps->highest_perf, cpu, ret);
341+
goto out;
342+
}
343+
344+
return 0;
330345

346+
out:
347+
cppc_cpufreq_put_cpu_data(policy);
331348
return ret;
332349
}
333350

@@ -345,12 +362,7 @@ static int cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
345362
pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
346363
caps->lowest_perf, cpu, ret);
347364

348-
/* Remove CPU node from list and free driver data for policy */
349-
free_cpumask_var(cpu_data->shared_cpu_map);
350-
list_del(&cpu_data->node);
351-
kfree(policy->driver_data);
352-
policy->driver_data = NULL;
353-
365+
cppc_cpufreq_put_cpu_data(policy);
354366
return 0;
355367
}
356368

0 commit comments

Comments
 (0)