[PATCH] drivers: hwmon: fix ioremap and memremap leak

Tianyi Jing posted 1 patch 1 year ago
There is a newer version of this series
drivers/hwmon/xgene-hwmon.c | 28 ++++++++++++++++++++++++----
1 file changed, 24 insertions(+), 4 deletions(-)
[PATCH] drivers: hwmon: fix ioremap and memremap leak
Posted by Tianyi Jing 1 year ago
Smatch reports:

drivers/hwmon/xgene-hwmon.c:757 xgene_hwmon_probe() warn:
'ctx->pcc_comm_addr' from ioremap() not released on line: 757.

This is because in drivers/hwmon/xgene-hwmon.c:701 xgene_hwmon_probe(),
ioremap and memremap is not released, which may cause a leak.

To fix this, iounmap and memunmap is added to line: 754. And the
declaration of 'version' is moved to xgene-hwmon.c:620 to help simplify
getting 'version' below.

Signed-off-by: Tianyi Jing <jingfelix@hust.edu.cn>
Reviewed-by: Dongliang Mu <dzm91@hust.edu.cn>
---
 drivers/hwmon/xgene-hwmon.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/drivers/hwmon/xgene-hwmon.c b/drivers/hwmon/xgene-hwmon.c
index 5cde837bfd09..44d1004ddacb 100644
--- a/drivers/hwmon/xgene-hwmon.c
+++ b/drivers/hwmon/xgene-hwmon.c
@@ -617,6 +617,7 @@ static int xgene_hwmon_probe(struct platform_device *pdev)
 	struct xgene_hwmon_dev *ctx;
 	struct mbox_client *cl;
 	int rc;
+	int version;
 
 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)
@@ -655,7 +656,6 @@ static int xgene_hwmon_probe(struct platform_device *pdev)
 	} else {
 		struct pcc_mbox_chan *pcc_chan;
 		const struct acpi_device_id *acpi_id;
-		int version;
 
 		acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
 					    &pdev->dev);
@@ -749,8 +749,15 @@ static int xgene_hwmon_probe(struct platform_device *pdev)
 out:
 	if (acpi_disabled)
 		mbox_free_channel(ctx->mbox_chan);
-	else
+	else {
+		if (ctx->comm_base_addr && ctx->pcc_comm_addr) {
+			if (version == XGENE_HWMON_V2)
+				iounmap(ctx->pcc_comm_addr);
+			else
+				memunmap(ctx->pcc_comm_addr);
+		}
 		pcc_mbox_free_channel(ctx->pcc_chan);
+	}
 out_mbox_free:
 	kfifo_free(&ctx->async_msg_fifo);
 
@@ -765,9 +772,22 @@ static int xgene_hwmon_remove(struct platform_device *pdev)
 	kfifo_free(&ctx->async_msg_fifo);
 	if (acpi_disabled)
 		mbox_free_channel(ctx->mbox_chan);
-	else
-		pcc_mbox_free_channel(ctx->pcc_chan);
+	else {
+		int version;
+		const struct acpi_device_id *acpi_id;
 
+		acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
+					    &pdev->dev);
+
+		version = (int)acpi_id->driver_data;
+
+		if (version == XGENE_HWMON_V2)
+			iounmap(ctx->pcc_comm_addr);
+		else
+			memunmap(ctx->pcc_comm_addr);
+
+		pcc_mbox_free_channel(ctx->pcc_chan);
+	}
 	return 0;
 }
 
-- 
2.34.1
Re: [PATCH] drivers: hwmon: fix ioremap and memremap leak
Posted by Guenter Roeck 1 year ago
On Sat, Mar 18, 2023 at 07:27:11PM +0800, Tianyi Jing wrote:
> Smatch reports:
> 
> drivers/hwmon/xgene-hwmon.c:757 xgene_hwmon_probe() warn:
> 'ctx->pcc_comm_addr' from ioremap() not released on line: 757.
> 
> This is because in drivers/hwmon/xgene-hwmon.c:701 xgene_hwmon_probe(),
> ioremap and memremap is not released, which may cause a leak.
> 
> To fix this, iounmap and memunmap is added to line: 754. And the
> declaration of 'version' is moved to xgene-hwmon.c:620 to help simplify
> getting 'version' below.
> 
> Signed-off-by: Tianyi Jing <jingfelix@hust.edu.cn>
> Reviewed-by: Dongliang Mu <dzm91@hust.edu.cn>

Please just use devm_ioremap() and devm_memremap().

Thanks,
Guenter