Skip to contentSkip to conversation

Fix two memory leaks in rproc_attach()

PATCH In review Xiaolei Wang 3 participants 5 messages Apr 30, 2025 – May 6, 2025

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

v3 cover Fix two memory leaks in rproc_attach()

In the rproc_attach() function, if rproc_handle_resources() returns failure, the resources requested in imx_rproc_prepare() should be released, since almost the same thing is done in imx_rproc_prepare() and rproc_resource_cleanup(), Function rproc_resource_cleanup() is able to deal with empty lists so it is better to fix the "goto" statements in rproc_attach(). replace the "unprepare_device" goto statement with "clean_up_resources" and get rid of the "unprepare_device" label. and rproc->clean_table should also be released

Changes in v3:
Update patch1, replace the "unprepare_device" goto statement with "clean_up_resources" and get rid of the "unprepare_device" label.

V2:

    Updated the commit log of these two patches
    https://patchwork.kernel.org/project/linux-remoteproc/patch/[email protected]/
    https://patchwork.kernel.org/project/linux-remoteproc/patch/[email protected]/

V1: https://patchwork.kernel.org/project/linux-remoteproc/patch/[email protected]/

    https://patchwork.kernel.org/project/linux-remoteproc/patch/[email protected]/

Xiaolei Wang (2):
remoteproc: cleanup acquired resources when rproc_handle_resources()
fails in rproc_attach()
remoteproc: core: release rproc->clean_table after rproc_attach()
fails

 drivers/remoteproc/remoteproc_core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 1/2 remoteproc: cleanup acquired resources when rproc_handle_resources() fails in rproc_attach()

When rproc->state = RPROC_DETACHED and rproc_attach() is used to attach to the remote processor, if rproc_handle_resources() returns a failure, the resources allocated by imx_rproc_prepare() should be released, otherwise the following memory leak will occur.

Since almost the same thing is done in imx_rproc_prepare() and rproc_resource_cleanup(), Function rproc_resource_cleanup() is able to deal with empty lists so it is better to fix the "goto" statements in rproc_attach(). replace the "unprepare_device" goto statement with "clean_up_resources" and get rid of the "unprepare_device" label.

unreferenced object 0xffff0000861c5d00 (size 128):
comm "kworker/u12:3", pid 59, jiffies 4294893509 (age 149.220s) hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 02 88 00 00 00 00 00 00 10 00 00 00 00 00 ............ backtrace:

 [<00000000f949fe18>] slab_post_alloc_hook+0x98/0x37c
 [<00000000adbfb3e7>] __kmem_cache_alloc_node+0x138/0x2e0
 [<00000000521c0345>] kmalloc_trace+0x40/0x158
 [<000000004e330a49>] rproc_mem_entry_init+0x60/0xf8
 [<000000002815755e>] imx_rproc_prepare+0xe0/0x180
 [<0000000003f61b4e>] rproc_boot+0x2ec/0x528
 [<00000000e7e994ac>] rproc_add+0x124/0x17c
 [<0000000048594076>] imx_rproc_probe+0x4ec/0x5d4
 [<00000000efc298a1>] platform_probe+0x68/0xd8
 [<00000000110be6fe>] really_probe+0x110/0x27c
 [<00000000e245c0ae>] __driver_probe_device+0x78/0x12c
 [<00000000f61f6f5e>] driver_probe_device+0x3c/0x118
 [<00000000a7874938>] __device_attach_driver+0xb8/0xf8
 [<0000000065319e69>] bus_for_each_drv+0x84/0xe4
 [<00000000db3eb243>] __device_attach+0xfc/0x18c
 [<0000000072e4e1a4>] device_initial_probe+0x14/0x20
Fixes 10a3d4079eae ("remoteproc: imx_rproc: move memory parsing to rproc_ops")
---
 drivers/remoteproc/remoteproc_core.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/remoteproc/remoteproc_core.c +2 -3
index b21eedefff87..fb7515ef155f 100644
@@ -1617,7 +1617,7 @@ static int rproc_attach(struct rproc *rproc)
 	ret = rproc_set_rsc_table(rproc);
 	if (ret) {
 		dev_err(dev, "can't load resource table: %d\n", ret);
-		goto unprepare_device;
+		goto clean_up_resources;
 	}
 
 	/* reset max_notifyid */
@@ -1634,7 +1634,7 @@ static int rproc_attach(struct rproc *rproc)
 	ret = rproc_handle_resources(rproc, rproc_loading_handlers);
 	if (ret) {
 		dev_err(dev, "Failed to process resources: %d\n", ret);
-		goto unprepare_device;
+		goto clean_up_resources;
 	}
 
 	/* Allocate carveout resources associated to rproc */
@@ -1653,7 +1653,6 @@ static int rproc_attach(struct rproc *rproc)
 
 clean_up_resources:
 	rproc_resource_cleanup(rproc);
-unprepare_device:
 	/* release HW resources if needed */
 	rproc_unprepare_device(rproc);
 disable_iommu:
Signature · 1 line
2.25.1
#2 lore raw

PATCH v3 2/2 remoteproc: core: release rproc->clean_table after rproc_attach() fails

In reply to Xiaolei Wang

When rproc->state = RPROC_DETACHED is attached to remote processor through rproc_attach(), if rproc_handle_resources() returns failure, then the clean table should be released, otherwise the following memory leak will occur.

unreferenced object 0xffff000086a99800 (size 1024):
comm "kworker/u12:3", pid 59, jiffies 4294893670 (age 121.140s) hex dump (first 32 bytes):
00 00 00 00 00 80 00 00 00 00 00 00 00 00 10 00 ............ 00 00 00 00 00 00 08 00 00 00 00 00 00 00 00 00 ............ backtrace:

 [<000000008bbe4ca8>] slab_post_alloc_hook+0x98/0x3fc
 [<000000003b8a272b>] __kmem_cache_alloc_node+0x13c/0x230
 [<000000007a507c51>] __kmalloc_node_track_caller+0x5c/0x260
 [<0000000037818dae>] kmemdup+0x34/0x60
 [<00000000610f7f57>] rproc_boot+0x35c/0x56c
 [<0000000065f8871a>] rproc_add+0x124/0x17c
 [<00000000497416ee>] imx_rproc_probe+0x4ec/0x5d4
 [<000000003bcaa37d>] platform_probe+0x68/0xd8
 [<00000000771577f9>] really_probe+0x110/0x27c
 [<00000000531fea59>] __driver_probe_device+0x78/0x12c
 [<0000000080036a04>] driver_probe_device+0x3c/0x118
 [<000000007e0bddcb>] __device_attach_driver+0xb8/0xf8
 [<000000000cf1fa33>] bus_for_each_drv+0x84/0xe4
 [<000000001a53b53e>] __device_attach+0xfc/0x18c
 [<00000000d1a2a32c>] device_initial_probe+0x14/0x20
 [<00000000d8f8b7ae>] bus_probe_device+0xb0/0xb4
 unreferenced object 0xffff0000864c9690 (size 16):
Fixes 9dc9507f1880 ("remoteproc: Properly deal with the resource table when detaching")
---
 drivers/remoteproc/remoteproc_core.c | 1 +
 1 file changed, 1 insertion(+)
drivers/remoteproc/remoteproc_core.c +1 -0
index fb7515ef155f..48d146e1fa56 100644
@@ -1655,6 +1655,7 @@ static int rproc_attach(struct rproc *rproc)
 	rproc_resource_cleanup(rproc);
 	/* release HW resources if needed */
 	rproc_unprepare_device(rproc);
+	kfree(rproc->clean_table);
 disable_iommu:
 	rproc_disable_iommu(rproc);
 	return ret;
Signature · 1 line
2.25.1
Reviewed-by on the v3 series
#3 lore raw
In reply to Xiaolei Wang
Quoted 21 lines · Xiaolei Wang

In the rproc_attach() function, if rproc_handle_resources() returns failure, the resources requested in imx_rproc_prepare() should be released, since almost the same thing is done in imx_rproc_prepare() and rproc_resource_cleanup(), Function rproc_resource_cleanup() is able to deal with empty lists so it is better to fix the "goto" statements in rproc_attach(). replace the "unprepare_device" goto statement with "clean_up_resources" and get rid of the "unprepare_device" label. and rproc->clean_table should also be released

Changes in v3:
Update patch1, replace the "unprepare_device" goto statement with "clean_up_resources" and get rid of the "unprepare_device" label.

V2:

   Updated the commit log of these two patches
   https://patchwork.kernel.org/project/linux-remoteproc/patch/[email protected]/
   https://patchwork.kernel.org/project/linux-remoteproc/patch/[email protected]/

V1: https://patchwork.kernel.org/project/linux-remoteproc/patch/[email protected]/
https://patchwork.kernel.org/project/linux-remoteproc/patch/[email protected]/

Quoted 8 lines

Xiaolei Wang (2):
remoteproc: cleanup acquired resources when rproc_handle_resources()
fails in rproc_attach()
remoteproc: core: release rproc->clean_table after rproc_attach()
fails

drivers/remoteproc/remoteproc_core.c | 6 +++---

1 file changed, 3 insertions(+), 3 deletions(-)
maintainer
#4 lore raw
In reply to Xiaolei Wang
Quoted 21 lines · Xiaolei Wang

In the rproc_attach() function, if rproc_handle_resources() returns failure, the resources requested in imx_rproc_prepare() should be released, since almost the same thing is done in imx_rproc_prepare() and rproc_resource_cleanup(), Function rproc_resource_cleanup() is able to deal with empty lists so it is better to fix the "goto" statements in rproc_attach(). replace the "unprepare_device" goto statement with "clean_up_resources" and get rid of the "unprepare_device" label. and rproc->clean_table should also be released

Changes in v3:
Update patch1, replace the "unprepare_device" goto statement with "clean_up_resources" and get rid of the "unprepare_device" label.

V2:

    Updated the commit log of these two patches
    https://patchwork.kernel.org/project/linux-remoteproc/patch/[email protected]/
    https://patchwork.kernel.org/project/linux-remoteproc/patch/[email protected]/

V1: https://patchwork.kernel.org/project/linux-remoteproc/patch/[email protected]/

    https://patchwork.kernel.org/project/linux-remoteproc/patch/[email protected]/
Quoted 9 lines

Xiaolei Wang (2):
remoteproc: cleanup acquired resources when rproc_handle_resources()
fails in rproc_attach()
remoteproc: core: release rproc->clean_table after rproc_attach()
fails

 drivers/remoteproc/remoteproc_core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

I have applied this patchset.

Thanks,
Mathieu

Quoted 3 lines
Signature · 1 line
2.25.1