Skip to contentSkip to conversation

powerpc/mpic: Fix memory leak of mpic on error paths

PATCH Superseded Jyoti Gupta 4 participants 4 messages Oct 6 PowerPC

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 powerpc/mpic: Fix memory leak of mpic on error paths

mpic_alloc() allocates struct mpic (and optionally the protected sources bitmap) but does not free them when mpic_setup_error_int() or irq_domain_create_linear() fails, leaking the memory.

Free the bitmap and the mpic in the common error path before dropping the device node reference.

Fixes 996983b75ceb ("powerpc/mpic: Search for open-pic device-tree node if NULL")

---

Changelog
===========
v2->v3:
+ Introduce 'err_free_mpic' unwind label to cleanly free mpic and mpic->protected
+ Link to v2: https://lore.kernel.org/all/[email protected]/

---
---
arch/powerpc/sysdev/mpic.c | 11 +++++------

 1 file changed, 5 insertions(+), 6 deletions(-)
arch/powerpc/sysdev/mpic.c +5 -6
index 257886ec3f70..d799cf4da7bd 100644
@@ -1389,7 +1389,7 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 		if (fsl_version >= 0x401) {
 			ret = mpic_setup_error_int(mpic, intvec_top - 13);
 			if (ret)
-				return NULL;
+				goto err_free_mpic;
 		}
 
 	}
@@ -1488,12 +1488,8 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 						 intvec_top,
 						 &mpic_host_ops, mpic);
 
-	/*
-	 * FIXME: The code leaks the MPIC object and mappings here; this
-	 * is very unlikely to fail but it ought to be fixed anyways.
-	 */
 	if (mpic->irqhost == NULL)
-		return NULL;
+		goto err_free_mpic;
 
 	/* Display version */
 	switch (greg_feature & MPIC_GREG_FEATURE_VERSION_MASK) {
@@ -1526,6 +1522,9 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 
 	return mpic;
 
+err_free_mpic:
+	bitmap_free(mpic->protected);
+	kfree(mpic);
 err_of_node_put:
 	of_node_put(node);
 	return NULL;
Signature · 1 line
2.55.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 10 lines · Jyoti Gupta

mpic_alloc() allocates struct mpic (and optionally the protected sources bitmap) but does not free them when mpic_setup_error_int() or irq_domain_create_linear() fails, leaking the memory.

Free the bitmap and the mpic in the common error path before dropping the device node reference.

Fixes 996983b75ceb ("powerpc/mpic: Search for open-pic device-tree node if NULL")

---

Thanks for updating this to use layered error labels. The patch looks clean and good to me now.

Thanks,
Amit

Reviewed-by on v3 1/1
#2 lore raw
In reply to Jyoti Gupta
Quoted 62 lines · Jyoti Gupta

mpic_alloc() allocates struct mpic (and optionally the protected sources bitmap) but does not free them when mpic_setup_error_int() or irq_domain_create_linear() fails, leaking the memory.

Free the bitmap and the mpic in the common error path before dropping the device node reference.

Fixes 996983b75ceb ("powerpc/mpic: Search for open-pic device-tree node if NULL")

---

Changelog
===========
v2->v3:
+ Introduce 'err_free_mpic' unwind label to cleanly free mpic and mpic->protected
+ Link to v2: https://lore.kernel.org/all/[email protected]/

---
---
arch/powerpc/sysdev/mpic.c | 11 +++++------

 1 file changed, 5 insertions(+), 6 deletions(-)
arch/powerpc/sysdev/mpic.c +5 -6
index 257886ec3f70..d799cf4da7bd 100644
@@ -1389,7 +1389,7 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 		if (fsl_version >= 0x401) {
 			ret = mpic_setup_error_int(mpic, intvec_top - 13);
 			if (ret)
-				return NULL;
+				goto err_free_mpic;
 		}
 
 	}
@@ -1488,12 +1488,8 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 						 intvec_top,
 						 &mpic_host_ops, mpic);
 
-	/*
-	 * FIXME: The code leaks the MPIC object and mappings here; this
-	 * is very unlikely to fail but it ought to be fixed anyways.
-	 */
 	if (mpic->irqhost == NULL)
-		return NULL;
+		goto err_free_mpic;
 
 	/* Display version */
 	switch (greg_feature & MPIC_GREG_FEATURE_VERSION_MASK) {
@@ -1526,6 +1522,9 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 
 	return mpic;
 
+err_free_mpic:
+	bitmap_free(mpic->protected);
+	kfree(mpic);
 err_of_node_put:
 	of_node_put(node);
 	return NULL;
Signature · 1 line
2.55.0

Yeah, this looks clean.

reviewer Reviewed-by on v3 1/1
#3 lore raw
In reply to Jyoti Gupta
Quoted 36 lines · Jyoti Gupta

mpic_alloc() allocates struct mpic (and optionally the protected sources bitmap) but does not free them when mpic_setup_error_int() or irq_domain_create_linear() fails, leaking the memory.

Free the bitmap and the mpic in the common error path before dropping the device node reference.

Fixes 996983b75ceb ("powerpc/mpic: Search for open-pic device-tree node if NULL")

---

Changelog
===========
v2->v3:
+ Introduce 'err_free_mpic' unwind label to cleanly free mpic and mpic->protected
+ Link to v2: https://lore.kernel.org/all/[email protected]/

---
---
arch/powerpc/sysdev/mpic.c | 11 +++++------

 1 file changed, 5 insertions(+), 6 deletions(-)
arch/powerpc/sysdev/mpic.c +1 -1
index 257886ec3f70..d799cf4da7bd 100644
@@ -1389,7 +1389,7 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 		if (fsl_version >= 0x401) {
 			ret = mpic_setup_error_int(mpic, intvec_top - 13);
 			if (ret)
-				return NULL;
+				goto err_free_mpic;
 		}
 
 	}

arch/powerpc/sysdev/mpic.c struct mpic * __init mpic_alloc(struct device_node *node, · line 1494

Quoted 7 lines
arch/powerpc/sysdev/mpic.c +0 -4
@@ -1488,12 +1488,8 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 						 intvec_top,
 						 &mpic_host_ops, mpic);
 
-	/*
-	 * FIXME: The code leaks the MPIC object and mappings here; this
-	 * is very unlikely to fail but it ought to be fixed anyways.
-	 */

I don't think the current change fixes leaked mappings for !CONFIG_PPC_DCR or !MPIC_USES_DCR. So removing this comment completely doesn't look correct.
And maybe unless we have the HW or a way to really validate this - not sure whether it is worth working on a full error handling routine to unmap all the mappings from ioremap().

In that case, in this patch at-least, I think we should still keep this comment i.e.:

/*
* FIXME: The code leaks the MPIC mappings here; this is very unlikely
* to fail but it ought to be fixed anyways.
*/

With that comment fixed - the change looks good to me, as it still fixes the memory leak in error handling path. So feel free to add:

Quoted 18 lines
arch/powerpc/sysdev/mpic.c +4 -1
 	if (mpic->irqhost == NULL)
-		return NULL;
+		goto err_free_mpic;
 
 	/* Display version */
 	switch (greg_feature & MPIC_GREG_FEATURE_VERSION_MASK) {
@@ -1526,6 +1522,9 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 
 	return mpic;
 
+err_free_mpic:
+	bitmap_free(mpic->protected);
+	kfree(mpic);
 err_of_node_put:
 	of_node_put(node);
 	return NULL;
Signature · 1 line
2.55.0