linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tianyi Jing <jingfelix@hust.edu.cn>
To: Jean Delvare <jdelvare@suse.com>, Guenter Roeck <linux@roeck-us.net>
Cc: Tianyi Jing <jingfelix@hust.edu.cn>,
	Dongliang Mu <dzm91@hust.edu.cn>,
	linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] drivers: hwmon: fix ioremap and memremap leak
Date: Sat, 18 Mar 2023 19:27:11 +0800	[thread overview]
Message-ID: <20230318112711.1803167-1-jingfelix@hust.edu.cn> (raw)

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


             reply	other threads:[~2023-03-18 11:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-18 11:27 Tianyi Jing [this message]
2023-03-18 12:31 ` [PATCH] drivers: hwmon: fix ioremap and memremap leak Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230318112711.1803167-1-jingfelix@hust.edu.cn \
    --to=jingfelix@hust.edu.cn \
    --cc=dzm91@hust.edu.cn \
    --cc=jdelvare@suse.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).