LKML Archive on
help / color / mirror / Atom feed
From: Eduardo Valentin <>
To: Guenter Roeck <>
Cc: Eduardo Valentin <>,
	Jean Delvare <>, <>,
Subject: [PATCH 2/2] hwmon: core: fix potential memory leak in *hwmon_device_register*
Date: Fri, 17 May 2019 16:13:37 -0700	[thread overview]
Message-ID: <> (raw)
In-Reply-To: <>

When registering a hwmon device with HWMON_C_REGISTER_TZ flag
in place, the hwmon subsystem will attempt to register the device
also with the thermal subsystem. When the of-thermal registration
fails, __hwmon_device_register jumps to ida_remove, leaving
the locally allocated hwdev pointer and also the hdev registered.

This patch fixes both issues by jumping to a new label that
will first unregister hdev and the fall into the kfree of hwdev
to finally remove the idas and propagate the error code.

Cc: Jean Delvare <>
Cc: Guenter Roeck <>
Signed-off-by: Eduardo Valentin <>
 drivers/hwmon/hwmon.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 6b3559f58b67..6f1194952189 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -637,7 +637,7 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
 								hwdev, j);
 					if (err) {
-						goto ida_remove;
+						goto device_unregister;
@@ -646,6 +646,8 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
 	return hdev;
+	device_unregister(hdev);

  parent reply	other threads:[~2019-05-17 23:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <>
2019-05-17 23:13 ` [PATCH 1/2] hwmon: core: add thermal sensors only if dev->of_node is present Eduardo Valentin
2019-05-28 15:08   ` Guenter Roeck
2019-05-29 23:12     ` Eduardo Valentin
2019-05-17 23:13 ` Eduardo Valentin [this message]
2019-05-28 15:06   ` [PATCH 2/2] hwmon: core: fix potential memory leak in *hwmon_device_register* Guenter Roeck
2019-05-29 23:14     ` Eduardo Valentin

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:

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

  git send-email \ \ \ \ \ \ \
    --subject='Re: [PATCH 2/2] hwmon: core: fix potential memory leak in *hwmon_device_register*' \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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