soc: qcom: cmd-db: Fix memory leak in cmd_db_dev_probe error path
Subsystems (2)
PATCH 1/1 soc: qcom: cmd-db: Fix memory leak in cmd_db_dev_probe error path
If cmd_db_magic_matches() fails after memremap() succeeds, the function returns -EINVAL without unmapping the memory region, causing a potential resource leak.
Add memunmap() call and set cmd_db_header to NULL before returning error to properly clean up the mapped memory region.
--- drivers/soc/qcom/cmd-db.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/soc/qcom/cmd-db.c +2 -0
@@ -358,6 +358,8 @@ static int cmd_db_dev_probe(struct platform_device *pdev)if (!cmd_db_magic_matches (cmd_db_header)) {dev_err (&pdev->dev,"Invalid Command DB Magic\n" ); +memunmap (cmd_db_header); + cmd_db_header =NULL ;return -EINVAL ; }
Signature · 1 line
2.50.1.windows.1
Quoted 4 lines · Haotian Zhang
If
cmd_db_magic_matches()fails aftermemremap()succeeds, the function returns-EINVALwithout unmapping the memory region, causing a potential resource leak.
Quoted 9 lines
Add
memunmap()call and setcmd_db_headerto NULL before returning error to properly clean up the mapped memory region.Fixes 312416d9171a ("drivers: qcom: add command DB driver")Signed-off-by Haotian Zhang <[email protected]>--- drivers/soc/qcom/cmd-db.c | 2 ++ 1 file changed, 2 insertions(+)
Also there is no _remove() function, so the mapped memory will never be cleaned up. Please consider using devm_memremap() instead.
Signature · 2 lines
With best wishes Dmitry
PATCH v2 1/1 soc: qcom: cmd-db: Use devm_memremap() to fix memory leak in cmd_db_dev_probe
In reply to Haotian ZhangIf cmd_db_magic_matches() fails after memremap() succeeds, the function returns -EINVAL without unmapping the memory region, causing a potential resource leak.
Switch to devm_memremap to automatically manage the map resource.
---
Changes in v2:
- Switch to devm_memremap instead of adding memunmap to also do the cleaning when cmd_db_dev_driver is unregistered, since there is no _remove() function.
- Updated subject to reflect the usage of devm_memremap().
--- drivers/soc/qcom/cmd-db.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/soc/qcom/cmd-db.c +4 -3
@@ -349,15 +349,16 @@ static int cmd_db_dev_probe(struct platform_device *pdev)return -EINVAL ; } - cmd_db_header =memremap (rmem->base, rmem->size,MEMREMAP_WC ); -if (!cmd_db_header) { - ret = -ENOMEM ; + cmd_db_header =devm_memremap (&pdev->dev, rmem->base, rmem->size,MEMREMAP_WC ); +if (IS_ERR (cmd_db_header)) { + ret =PTR_ERR (cmd_db_header); cmd_db_header =NULL ;return ret; }if (!cmd_db_magic_matches (cmd_db_header)) {dev_err (&pdev->dev,"Invalid Command DB Magic\n" ); + cmd_db_header =NULL ;return -EINVAL ; }
Signature · 1 line
2.50.1.windows.1
Re: [PATCH v2] soc: qcom: cmd-db: Use devm_memremap() to fix memory leak in cmd_db_dev_probe
Quoted 8 lines · Haotian Zhang
If
cmd_db_magic_matches()fails aftermemremap()succeeds, the function returns-EINVALwithout unmapping the memory region, causing a potential resource leak.Switch to
devm_memremapto automatically manage the map resource.[...]
Applied, thanks!
[1/1] soc: qcom: cmd-db: Use devm_memremap() to fix memory leak in cmd_db_dev_probe
commit: 0da7824734d8d83e6a844dd0207f071cb0c50cf4
Best regards,
Signature · 1 line
Bjorn Andersson <[email protected]>