Skip to contentSkip to conversation

powerpc/powernv: Fix refcount leak bugs

PATCH Applied Liang He 2 participants 2 messages Jun 20, 2022 – Sep 9, 2022 PowerPC

lore
Share & tools
lore

Liang He

author
#0 lore raw

PATCH v3 1/1 powerpc/powernv: Fix refcount leak bugs

In these driver init functions, there are two kinds of errors:

(1) missing of_put_node() for of_find_compatible_node()'s returned pointer (refcount incremented) in fail path or when it is not used anymore.
(2) missing of_put_node() for 'for_each_xxx' loop's break

These bugs are similar with the ones reported by commit-09700c504d.

---
changelog:

v3: merge more refcount bugs into one commit
v2: merge opal-*.c
v1: send mising of_node_put patch for each file

 arch/powerpc/platforms/powernv/idle.c               | 1 +
 arch/powerpc/platforms/powernv/opal-core.c          | 2 ++
 arch/powerpc/platforms/powernv/opal-powercap.c      | 6 +++++-
 arch/powerpc/platforms/powernv/opal-psr.c           | 6 +++++-
 arch/powerpc/platforms/powernv/opal-sensor-groups.c | 6 +++++-
 arch/powerpc/platforms/powernv/opal.c               | 1 +
 6 files changed, 19 insertions(+), 3 deletions(-)
arch/powerpc/platforms/powernv/idle.c +1 -0
index 6f94b808dd39..c1b369c7f507 100644
@@ -1419,6 +1419,7 @@ static int __init pnv_parse_cpuidle_dt(void)
 	kfree(temp_u32);
 	kfree(temp_u64);
 	kfree(temp_string);
+	of_node_put(np);
 	return rc;
 }
 
arch/powerpc/platforms/powernv/opal-core.c +2 -0
index adcb1a1a2bfe..bb7657115f1d 100644
@@ -348,6 +348,8 @@ static int __init create_opalcore(void)
 	if (!dn || ret)
 		pr_warn("WARNING: Failed to read OPAL base & entry values\n");
 
+	of_node_put(dn);
+
 	/* Use count to keep track of the program headers */
 	count = 0;
 
arch/powerpc/platforms/powernv/opal-powercap.c +5 -1
index 64506b46e77b..78c359c90093 100644
@@ -153,7 +153,7 @@ void __init opal_powercap_init(void)
 	pcaps = kcalloc(of_get_child_count(powercap), sizeof(*pcaps),
 			GFP_KERNEL);
 	if (!pcaps)
-		return;
+		goto out_powercap;
 
 	powercap_kobj = kobject_create_and_add("powercap", opal_kobj);
 	if (!powercap_kobj) {
@@ -226,6 +226,7 @@ void __init opal_powercap_init(void)
 		}
 		i++;
 	}
+	of_node_put(powercap);
 
 	return;
 
@@ -236,6 +237,9 @@ void __init opal_powercap_init(void)
 		kfree(pcaps[i].pg.name);
 	}
 	kobject_put(powercap_kobj);
+	of_node_put(node);
 out_pcaps:
 	kfree(pcaps);
+out_powercap:
+	of_node_put(powercap);
 }
arch/powerpc/platforms/powernv/opal-psr.c +5 -1
index 69d7e75950d1..ec32e0a93f08 100644
@@ -135,7 +135,7 @@ void __init opal_psr_init(void)
 	psr_attrs = kcalloc(of_get_child_count(psr), sizeof(*psr_attrs),
 			    GFP_KERNEL);
 	if (!psr_attrs)
-		return;
+		goto out_psr;
 
 	psr_kobj = kobject_create_and_add("psr", opal_kobj);
 	if (!psr_kobj) {
@@ -162,10 +162,14 @@ void __init opal_psr_init(void)
 		}
 		i++;
 	}
+	of_node_put(psr);
 
 	return;
 out_kobj:
+	of_node_put(node);
 	kobject_put(psr_kobj);
 out:
 	kfree(psr_attrs);
+out_psr:
+	of_node_put(psr);
 }
arch/powerpc/platforms/powernv/opal-sensor-groups.c +5 -1
index 8fba7d25ae56..9944376b115c 100644
@@ -170,7 +170,7 @@ void __init opal_sensor_groups_init(void)
 
 	sgs = kcalloc(of_get_child_count(sg), sizeof(*sgs), GFP_KERNEL);
 	if (!sgs)
-		return;
+		goto out_sg_put;
 
 	sg_kobj = kobject_create_and_add("sensor_groups", opal_kobj);
 	if (!sg_kobj) {
@@ -222,6 +222,7 @@ void __init opal_sensor_groups_init(void)
 		}
 		i++;
 	}
+	of_node_put(sg);
 
 	return;
 
@@ -231,6 +232,9 @@ void __init opal_sensor_groups_init(void)
 		kfree(sgs[i].sg.attrs);
 	}
 	kobject_put(sg_kobj);
+	of_node_put(node);
 out_sgs:
 	kfree(sgs);
+out_sg_put:
+	of_node_put(sg);
 }
arch/powerpc/platforms/powernv/opal.c +1 -0
index 55a8fbfdb5b2..d86cc48a10aa 100644
@@ -952,6 +952,7 @@ static void __init opal_imc_init_dev(void)
 	np = of_find_compatible_node(NULL, NULL, IMC_DTB_COMPAT);
 	if (np)
 		of_platform_device_create(np, NULL, NULL);
+	of_node_put(np);
 }
 
 static int kopald(void *unused)
Signature · 1 line
2.25.1
#1 lore raw
Quoted 8 lines · Liang He

In these driver init functions, there are two kinds of errors:

(1) missing of_put_node() for of_find_compatible_node()'s returned pointer (refcount incremented) in fail path or when it is not used anymore.
(2) missing of_put_node() for 'for_each_xxx' loop's break

[...]

Applied to powerpc/next.

[1/1] powerpc/powernv: Fix refcount leak bugs

      https://git.kernel.org/powerpc/c/605c27f3802038e4623b6fd1bbfa021e1f65b5c4

cheers