linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/2] Fix two memory leaks in rproc_attach()
@ 2025-04-30  9:20 Xiaolei Wang
  2025-04-30  9:20 ` [PATCH v3 1/2] remoteproc: cleanup acquired resources when rproc_handle_resources() fails " Xiaolei Wang
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Xiaolei Wang @ 2025-04-30  9:20 UTC (permalink / raw)
  To: andersson, mathieu.poirier, shawnguo, s.hauer, kernel, festevam
  Cc: linux-remoteproc, imx, linux-arm-kernel, linux-kernel

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/20250426065348.1234391-2-xiaolei.wang@windriver.com/
    https://patchwork.kernel.org/project/linux-remoteproc/patch/20250426065348.1234391-3-xiaolei.wang@windriver.com/

V1: https://patchwork.kernel.org/project/linux-remoteproc/patch/20250424122252.2777363-1-xiaolei.wang@windriver.com/
    https://patchwork.kernel.org/project/linux-remoteproc/patch/20250424122252.2777363-2-xiaolei.wang@windriver.com/

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(-)

-- 
2.25.1



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v3 1/2] remoteproc: cleanup acquired resources when rproc_handle_resources() fails in rproc_attach()
  2025-04-30  9:20 [PATCH v3 0/2] Fix two memory leaks in rproc_attach() Xiaolei Wang
@ 2025-04-30  9:20 ` Xiaolei Wang
  2025-04-30  9:20 ` [PATCH v3 2/2] remoteproc: core: release rproc->clean_table after rproc_attach() fails Xiaolei Wang
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Xiaolei Wang @ 2025-04-30  9:20 UTC (permalink / raw)
  To: andersson, mathieu.poirier, shawnguo, s.hauer, kernel, festevam
  Cc: linux-remoteproc, imx, linux-arm-kernel, linux-kernel

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")
Suggested-by: Mathieu Poirier <mathieu.poirier@linaro.org>
Signed-off-by: Xiaolei Wang <xiaolei.wang@windriver.com>
---
 drivers/remoteproc/remoteproc_core.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/remoteproc/remoteproc_core.c b/drivers/remoteproc/remoteproc_core.c
index b21eedefff87..fb7515ef155f 100644
--- a/drivers/remoteproc/remoteproc_core.c
+++ b/drivers/remoteproc/remoteproc_core.c
@@ -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:
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 2/2] remoteproc: core: release rproc->clean_table after rproc_attach() fails
  2025-04-30  9:20 [PATCH v3 0/2] Fix two memory leaks in rproc_attach() Xiaolei Wang
  2025-04-30  9:20 ` [PATCH v3 1/2] remoteproc: cleanup acquired resources when rproc_handle_resources() fails " Xiaolei Wang
@ 2025-04-30  9:20 ` Xiaolei Wang
  2025-05-06  8:56 ` [PATCH v3 0/2] Fix two memory leaks in rproc_attach() Peng Fan
  2025-05-06 17:28 ` Mathieu Poirier
  3 siblings, 0 replies; 5+ messages in thread
From: Xiaolei Wang @ 2025-04-30  9:20 UTC (permalink / raw)
  To: andersson, mathieu.poirier, shawnguo, s.hauer, kernel, festevam
  Cc: linux-remoteproc, imx, linux-arm-kernel, linux-kernel

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")
Signed-off-by: Xiaolei Wang <xiaolei.wang@windriver.com>
---
 drivers/remoteproc/remoteproc_core.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/remoteproc/remoteproc_core.c b/drivers/remoteproc/remoteproc_core.c
index fb7515ef155f..48d146e1fa56 100644
--- a/drivers/remoteproc/remoteproc_core.c
+++ b/drivers/remoteproc/remoteproc_core.c
@@ -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;
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 0/2] Fix two memory leaks in rproc_attach()
  2025-04-30  9:20 [PATCH v3 0/2] Fix two memory leaks in rproc_attach() Xiaolei Wang
  2025-04-30  9:20 ` [PATCH v3 1/2] remoteproc: cleanup acquired resources when rproc_handle_resources() fails " Xiaolei Wang
  2025-04-30  9:20 ` [PATCH v3 2/2] remoteproc: core: release rproc->clean_table after rproc_attach() fails Xiaolei Wang
@ 2025-05-06  8:56 ` Peng Fan
  2025-05-06 17:28 ` Mathieu Poirier
  3 siblings, 0 replies; 5+ messages in thread
From: Peng Fan @ 2025-05-06  8:56 UTC (permalink / raw)
  To: Xiaolei Wang
  Cc: andersson, mathieu.poirier, shawnguo, s.hauer, kernel, festevam,
	linux-remoteproc, imx, linux-arm-kernel, linux-kernel

On Wed, Apr 30, 2025 at 05:20:41PM +0800, Xiaolei Wang wrote:
>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/20250426065348.1234391-2-xiaolei.wang@windriver.com/
>    https://patchwork.kernel.org/project/linux-remoteproc/patch/20250426065348.1234391-3-xiaolei.wang@windriver.com/
>
>V1: https://patchwork.kernel.org/project/linux-remoteproc/patch/20250424122252.2777363-1-xiaolei.wang@windriver.com/
>    https://patchwork.kernel.org/project/linux-remoteproc/patch/20250424122252.2777363-2-xiaolei.wang@windriver.com/
>
>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(-)

Reviewed-by: Peng Fan <peng.fan@nxp.com>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 0/2] Fix two memory leaks in rproc_attach()
  2025-04-30  9:20 [PATCH v3 0/2] Fix two memory leaks in rproc_attach() Xiaolei Wang
                   ` (2 preceding siblings ...)
  2025-05-06  8:56 ` [PATCH v3 0/2] Fix two memory leaks in rproc_attach() Peng Fan
@ 2025-05-06 17:28 ` Mathieu Poirier
  3 siblings, 0 replies; 5+ messages in thread
From: Mathieu Poirier @ 2025-05-06 17:28 UTC (permalink / raw)
  To: Xiaolei Wang
  Cc: andersson, shawnguo, s.hauer, kernel, festevam, linux-remoteproc,
	imx, linux-arm-kernel, linux-kernel

On Wed, Apr 30, 2025 at 05:20:41PM +0800, Xiaolei Wang wrote:
> 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/20250426065348.1234391-2-xiaolei.wang@windriver.com/
>     https://patchwork.kernel.org/project/linux-remoteproc/patch/20250426065348.1234391-3-xiaolei.wang@windriver.com/
> 
> V1: https://patchwork.kernel.org/project/linux-remoteproc/patch/20250424122252.2777363-1-xiaolei.wang@windriver.com/
>     https://patchwork.kernel.org/project/linux-remoteproc/patch/20250424122252.2777363-2-xiaolei.wang@windriver.com/
> 
> 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

> -- 
> 2.25.1
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-05-06 22:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-30  9:20 [PATCH v3 0/2] Fix two memory leaks in rproc_attach() Xiaolei Wang
2025-04-30  9:20 ` [PATCH v3 1/2] remoteproc: cleanup acquired resources when rproc_handle_resources() fails " Xiaolei Wang
2025-04-30  9:20 ` [PATCH v3 2/2] remoteproc: core: release rproc->clean_table after rproc_attach() fails Xiaolei Wang
2025-05-06  8:56 ` [PATCH v3 0/2] Fix two memory leaks in rproc_attach() Peng Fan
2025-05-06 17:28 ` Mathieu Poirier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).