Skip to contentSkip to conversation

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

PATCH Superseded Jyoti Gupta 3 participants 6 messages Sep 24 – Sep 29 PowerPC

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

Initialise mpic to NULL and free the bitmap and the mpic in the common error path before dropping the device node reference. The register mappings are not released here.

---
 arch/powerpc/sysdev/mpic.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
arch/powerpc/sysdev/mpic.c +4 -5
index 6e8fa94985f3..e777b33f170d 100644
@@ -1221,7 +1221,7 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 				const char *name)
 {
 	int i, psize, intvec_top;
-	struct mpic *mpic;
+	struct mpic *mpic = NULL;
 	u32 greg_feature;
 	const char *vers;
 	const u32 *psrc;
@@ -1488,10 +1488,6 @@ 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)
 		goto err_of_node_put;
 
@@ -1527,6 +1523,9 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 	return mpic;
 
 err_of_node_put:
+	if (mpic)
+		bitmap_free(mpic->protected);
+	kfree(mpic);
 	of_node_put(node);
 	return NULL;
 }
Signature · 1 line
2.55.0
#1 lore raw
Quoted 9 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.

Initialise mpic to NULL and free the bitmap and the mpic in the common error path before dropping the device node reference. The register mappings are not released here.

I see a couple of problems with this patch:

1. The patch does not apply. Both "git apply" and "git am" fail on this patch. I think the context lines around the irq_domain_create_linear() call have tabs converted to spaces, which causes the context match to fail.

❯ git apply ./20260924_jyoti_powerpc_mpic_fix_memory_leak_of_mpic_on_error_paths.mbx --check --verbose Checking patch arch/powerpc/sysdev/mpic.c...
error: while searching for:
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)
goto err_of_node_put;

error: patch failed: arch/powerpc/sysdev/mpic.c:1488
error: arch/powerpc/sysdev/mpic.c: patch does not apply

Please regenerate with:

git format-patch -1

and verify with:

     git apply --check <patch>

before resending.

2. The mpic_setup_error_int() leak is not actually fixed. The commit message says both mpic_setup_error_int() and irq_domain_create_linear() failure paths are fixed, but the former still does a bare return NULL:

     if (fsl_version >= 0x401) {
         ret = mpic_setup_error_int(mpic, intvec_top - 13);
         if (ret)
             return NULL;   /* <-- still leaks mpic and mpic->protected */
     }

This needs to become:

         if (ret)
             goto err_of_node_put;

Thanks,
Amit

Quoted 42 lines
---
 arch/powerpc/sysdev/mpic.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
arch/powerpc/sysdev/mpic.c +4 -5
index 6e8fa94985f3..e777b33f170d 100644
@@ -1221,7 +1221,7 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 				const char *name)
 {
 	int i, psize, intvec_top;
-	struct mpic *mpic;
+	struct mpic *mpic = NULL;
 	u32 greg_feature;
 	const char *vers;
 	const u32 *psrc;
@@ -1488,10 +1488,6 @@ 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)
 		goto err_of_node_put;
 
@@ -1527,6 +1523,9 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 	return mpic;
 
 err_of_node_put:
+	if (mpic)
+		bitmap_free(mpic->protected);
+	kfree(mpic);
 	of_node_put(node);
 	return NULL;
 }
Signature · 1 line
2.55.0
#2 lore raw

Hi Jyoti,

For some reason, you response to this thread is not appearing on the list. I think you mail client is messing up the "In-Reply-To" information in the header. Fix look into fixing it.

For now, I'll qoute your reply here with my further response.

Quoted 93 lines · Amit Machhiwal
Quoted 9 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.

Initialise mpic to NULL and free the bitmap and the mpic in the common error path before dropping the device node reference. The register mappings are not released here.

I see a couple of problems with this patch:

1. The patch does not apply. Both "git apply" and "git am" fail on this patch. I think the context lines around the irq_domain_create_linear() call have tabs converted to spaces, which causes the context match to fail.

❯ git apply ./20260924_jyoti_powerpc_mpic_fix_memory_leak_of_mpic_on_error_paths.mbx --check --verbose Checking patch arch/powerpc/sysdev/mpic.c...
error: while searching for:
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)
goto err_of_node_put;

error: patch failed: arch/powerpc/sysdev/mpic.c:1488
error: arch/powerpc/sysdev/mpic.c: patch does not apply

Please regenerate with:

git format-patch -1

and verify with:

     git apply --check <patch>

before resending.

2. The mpic_setup_error_int() leak is not actually fixed. The commit message says both mpic_setup_error_int() and irq_domain_create_linear() failure paths are fixed, but the former still does a bare return NULL:

     if (fsl_version >= 0x401) {
         ret = mpic_setup_error_int(mpic, intvec_top - 13);
         if (ret)
             return NULL;   /* <-- still leaks mpic and mpic->protected */
     }

This needs to become:

         if (ret)
             goto err_of_node_put;

Thanks,
Amit

Quoted 31 lines
---
 arch/powerpc/sysdev/mpic.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
arch/powerpc/sysdev/mpic.c +1 -5
index 6e8fa94985f3..e777b33f170d 100644
@@ -1221,7 +1221,7 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 				const char *name)
 {
 	int i, psize, intvec_top;
-	struct mpic *mpic;
+	struct mpic *mpic = NULL;
 	u32 greg_feature;
 	const char *vers;
 	const u32 *psrc;
@@ -1488,10 +1488,6 @@ 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)
 		goto err_of_node_put;
 
@@ -1527,6 +1523,9 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 	return mpic;
 
Quoted 12 lines
Quoted 11 lines
arch/powerpc/sysdev/mpic.c +3 -0
 err_of_node_put:
+	if (mpic)
+		bitmap_free(mpic->protected);
+	kfree(mpic);
 	of_node_put(node);
 	return NULL;
 }
Signature · 1 line
2.55.0

Response from Jyoti:

Quoted 2 lines

The mpic_setup_error_int() leak is intentionally left to a separate patch already under

If that's the case, you shouldn't have mentioned about this change in the commit log of this patch. The commit log needs to be updated accordingly if that's what your intent is.

Quoted 4 lines

review:
"[PATCH] powerpc: Fix missing of_node_put() calls"
That patch converts the return NULL at line 1392 to goto err_of_node_put as part of a

I could only find below:

https://lore.kernel.org/all/[email protected]/

Sorry, but I don't think thats the right way to send a patch. You seem to have replied to the email which did not land into the mailing lists at all. Having said that, I think fixing this particular path in mpic_alloc() would make more sense in the current patch itself.

Please look into fixing your mail client and send the patches/responses in proper format.

Thanks,
Amit.

Quoted 6 lines

broader of_node_put() fix series. This patch focuses only on the irq_domain_create_linear()
failure path.

Thanks,
Jyoti

author
#3 lore raw
In reply to Amit Machhiwal
Quoted 39 lines · Amit Machhiwal
Quoted 9 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.

Initialise mpic to NULL and free the bitmap and the mpic in the common error path before dropping the device node reference. The register mappings are not released here.

I see a couple of problems with this patch:

1. The patch does not apply. Both "git apply" and "git am" fail on this patch. I think the context lines around the irq_domain_create_linear() call
have
tabs converted to spaces, which causes the context match to fail.

❯ git apply
./20260924_jyoti_powerpc_mpic_fix_memory_leak_of_mpic_on_error_paths.mbx --check --verbose
Checking patch arch/powerpc/sysdev/mpic.c...
error: while searching for:
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)
goto err_of_node_put;

error: patch failed: arch/powerpc/sysdev/mpic.c:1488
error: arch/powerpc/sysdev/mpic.c: patch does not apply

Please regenerate with:

Quoted 8 lines

git format-patch -1

and verify with:

     git apply --check <patch>

before resending.

Noted.I will regenerate with git format-patch -1 and verify with git apply --check before resending as v2.

Quoted 12 lines

2. The mpic_setup_error_int() leak is not actually fixed. The commit message says both mpic_setup_error_int() and irq_domain_create_linear()
failure paths
are fixed, but the former still does a bare return NULL:

     if (fsl_version >= 0x401) {
         ret = mpic_setup_error_int(mpic, intvec_top - 13);
         if (ret)
             return NULL;   /* <-- still leaks mpic and mpic->protected 
*/
     }
Quoted 8 lines

This needs to become:

         if (ret)
             goto err_of_node_put;

Thanks,
Amit

The mpic_setup_error_int() leak is intentionally left to a separate patch already under
review:
"[PATCH] powerpc: Fix missing of_node_put() calls"
That patch converts the return NULL at line 1392 to goto err_of_node_put as part of a broader of_node_put() fix series. This patch focuses only on the irq_domain_create_linear() failure path.

Thanks,
Jyoti

#4 lore raw
In reply to Jyoti Gupta
Quoted 18 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.

Initialise mpic to NULL and free the bitmap and the mpic in the common error path before dropping the device node reference. The register mappings are not released here.

---
 arch/powerpc/sysdev/mpic.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
arch/powerpc/sysdev/mpic.c +0 -0
index 6e8fa94985f3..e777b33f170d 100644

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

Quoted 5 lines
arch/powerpc/sysdev/mpic.c +1 -1
@@ -1221,7 +1221,7 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 				const char *name)
 {
 	int i, psize, intvec_top;
-	struct mpic *mpic;
+	struct mpic *mpic = NULL;

We don't need this initialization. We initialize it later again.

Quoted 28 lines
arch/powerpc/sysdev/mpic.c +3 -4
 	u32 greg_feature;
 	const char *vers;
 	const u32 *psrc;
@@ -1488,10 +1488,6 @@ 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)
 		goto err_of_node_put;
 
@@ -1527,6 +1523,9 @@ struct mpic * __init mpic_alloc(struct device_node *node,
 	return mpic;
 
 err_of_node_put:
+	if (mpic)
+		bitmap_free(mpic->protected);
+	kfree(mpic);
 	of_node_put(node);
 	return NULL;
 }
Signature · 1 line
2.55.0

Do we need a fixes tag for this?

Regards,
Mukesh

author
#5 lore raw
Quoted 14 lines · Mukesh Kumar Chaurasiya
Quoted 13 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.

Initialise mpic to NULL and free the bitmap and the mpic in the common error path before dropping the device node reference. The register mappings are not released here.

---
 arch/powerpc/sysdev/mpic.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
Quoted 12 lines

arch/powerpc/sysdev/mpic.c struct mpic * __init mpic_alloc(struct · line 1225

Quoted 11 lines
arch/powerpc/sysdev/mpic.c +1 -1
index 6e8fa94985f3..e777b33f170d 100644
@@ -1221,7 +1221,7 @@ struct mpic * __init mpic_alloc(struct 
 device_node *node,
 				const char *name)
 {
 	int i, psize, intvec_top;
-	struct mpic *mpic;
+	struct mpic *mpic = NULL;

We don't need this initialization. We initialize it later again.

Agree

Quoted 19 lines
Quoted 19 lines
arch/powerpc/sysdev/mpic.c +0 -0
 	u32 greg_feature;
 	const char *vers;
arch/powerpc/sysdev/mpic.c +0 -4
 	const u32 *psrc;
@@ -1488,10 +1488,6 @@ 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)
 		goto err_of_node_put;
@@ -1527,6 +1523,9 @@ struct mpic * __init mpic_alloc(struct 
 device_node *node,
 	return mpic;
 
Quoted 13 lines
Quoted 11 lines
arch/powerpc/sysdev/mpic.c +3 -0
 err_of_node_put:
+	if (mpic)
+		bitmap_free(mpic->protected);
+	kfree(mpic);
 	of_node_put(node);
 	return NULL;

}
--
2.55.0

Do we need a fixes tag for this?

Agree

Thanks,
Jyoti