[PATCH v2] soc: qcom: cmd-db: Use devm_memremap() to fix memory leak in cmd_db_dev_probe

Haotian Zhang posted 1 patch 1 month, 3 weeks ago
drivers/soc/qcom/cmd-db.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
[PATCH v2] soc: qcom: cmd-db: Use devm_memremap() to fix memory leak in cmd_db_dev_probe
Posted by Haotian Zhang 1 month, 3 weeks ago
If 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.

Fixes: 312416d9171a ("drivers: qcom: add command DB driver")
Suggested-by: Dmitry Baryshkov <dmitry.baryshkov@oss.qualcomm.com>
Signed-off-by: Haotian Zhang <vulab@iscas.ac.cn>
---
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(-)

diff --git a/drivers/soc/qcom/cmd-db.c b/drivers/soc/qcom/cmd-db.c
index ae66c2623d25..84a75d8c4b70 100644
--- a/drivers/soc/qcom/cmd-db.c
+++ b/drivers/soc/qcom/cmd-db.c
@@ -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;
 	}
 
-- 
2.50.1.windows.1
Re: [PATCH v2] soc: qcom: cmd-db: Use devm_memremap() to fix memory leak in cmd_db_dev_probe
Posted by Bjorn Andersson 3 weeks, 6 days ago
On Tue, 16 Dec 2025 09:39:32 +0800, Haotian Zhang wrote:
> If 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.
> 
> 
> [...]

Applied, thanks!

[1/1] soc: qcom: cmd-db: Use devm_memremap() to fix memory leak in cmd_db_dev_probe
      commit: 0da7824734d8d83e6a844dd0207f071cb0c50cf4

Best regards,
-- 
Bjorn Andersson <andersson@kernel.org>