Skip to contentSkip to conversation

powercap/dtpm_cpu: Fix refcount leak in __dtpm_cpu_setup

PATCH In review Miaoqian Lin 2 participants 2 messages Dec 30, 2022 – Jan 20, 2023

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

PATCH 1/1 powercap/dtpm_cpu: Fix refcount leak in __dtpm_cpu_setup

The policy return by cpufreq_cpu_get() should be released with cpufreq_cpu_put() to balance the reference counter. Add missing cpufreq_cpu_put() to fix this.

Fixes 0e8f68d7f048 ("powercap/drivers/dtpm: Add CPU energy model based support")
---
 drivers/powercap/dtpm_cpu.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/powercap/dtpm_cpu.c +12 -5
index 2ff7717530bf..3083c6b45c90 100644
@@ -195,12 +195,16 @@ static int __dtpm_cpu_setup(int cpu, struct dtpm *parent)
 		return 0;
 
 	pd = em_cpu_get(cpu);
-	if (!pd || em_is_artificial(pd))
-		return -EINVAL;
+	if (!pd || em_is_artificial(pd)) {
+		ret = -EINVAL;
+		goto out_put_policy;
+	}
 
 	dtpm_cpu = kzalloc(sizeof(*dtpm_cpu), GFP_KERNEL);
-	if (!dtpm_cpu)
-		return -ENOMEM;
+	if (!dtpm_cpu) {
+		ret = -ENOMEM;
+		goto out_put_policy;
+	}
 
 	dtpm_init(&dtpm_cpu->dtpm, &dtpm_ops);
 	dtpm_cpu->cpu = cpu;
@@ -220,6 +224,8 @@ static int __dtpm_cpu_setup(int cpu, struct dtpm *parent)
 	if (ret)
 		goto out_dtpm_unregister;
 
+	cpufreq_cpu_put(policy);
+
 	return 0;
 
 out_dtpm_unregister:
@@ -230,7 +236,8 @@ static int __dtpm_cpu_setup(int cpu, struct dtpm *parent)
 	for_each_cpu(cpu, policy->related_cpus)
 		per_cpu(dtpm_per_cpu, cpu) = NULL;
 	kfree(dtpm_cpu);
-
+out_put_policy:
+	cpufreq_cpu_put(policy);
 	return ret;
 }
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 37 lines · Miaoqian Lin

The policy return by cpufreq_cpu_get() should be released with cpufreq_cpu_put() to balance the reference counter. Add missing cpufreq_cpu_put() to fix this.

Fixes 0e8f68d7f048 ("powercap/drivers/dtpm: Add CPU energy model based support")
---
 drivers/powercap/dtpm_cpu.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/powercap/dtpm_cpu.c +8 -4
index 2ff7717530bf..3083c6b45c90 100644
@@ -195,12 +195,16 @@ static int __dtpm_cpu_setup(int cpu, struct dtpm *parent)
                return 0;
 
        pd = em_cpu_get(cpu);
-       if (!pd || em_is_artificial(pd))
-               return -EINVAL;
+       if (!pd || em_is_artificial(pd)) {
+               ret = -EINVAL;
+               goto out_put_policy;
+       }
 
        dtpm_cpu = kzalloc(sizeof(*dtpm_cpu), GFP_KERNEL);
-       if (!dtpm_cpu)
-               return -ENOMEM;
+       if (!dtpm_cpu) {
+               ret = -ENOMEM;
+               goto out_put_policy;
+       }
 
        dtpm_init(&dtpm_cpu->dtpm, &dtpm_ops);
        dtpm_cpu->cpu = cpu;

drivers/powercap/dtpm_cpu.c static int __dtpm_cpu_setup(int cpu, struct dtpm *parent) · line 225

Quoted 2 lines
drivers/powercap/dtpm_cpu.c +0 -0
@@ -220,6 +224,8 @@ static int __dtpm_cpu_setup(int cpu, struct dtpm *parent)
        if (ret)
                goto out_dtpm_unregister;

The part of the patch above is valid, but I don't think that the policy reference counter can be dropped below, because that may allow the policy to go away and this driver will be still using it, won't it?

Quoted 19 lines
drivers/powercap/dtpm_cpu.c +4 -2
+       cpufreq_cpu_put(policy);
+
        return 0;
 
 out_dtpm_unregister:
@@ -230,7 +236,8 @@ static int __dtpm_cpu_setup(int cpu, struct dtpm *parent)
        for_each_cpu(cpu, policy->related_cpus)
                per_cpu(dtpm_per_cpu, cpu) = NULL;
        kfree(dtpm_cpu);
-
+out_put_policy:
+       cpufreq_cpu_put(policy);
        return ret;
 }
 
--

2.25.1