mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Liam Girdwood <lgirdwood@gmail.com>, Mark Brown <broonie@kernel.org>
Cc: Linus Walleij <linusw@kernel.org>, linux-kernel@vger.kernel.org
Subject: [PATCH] regulator: devres: release enable GPIO on devres_alloc() failure
Date: Mon, 30 Nov 2026 21:52:11 -0800	[thread overview]
Message-ID: <20261130-regulator-devm-gpiod-fix-v1-1-24f25d1486f3@gmail.com> (raw)

regulator_register() takes ownership of the enable GPIO descriptor
passed in the configuration and releases it on every failure path,
including out-of-memory errors. Because callers cannot distinguish an
-ENOMEM from devm_regulator_register()'s devres allocation from one
returned inside regulator_register(), they cannot safely free the GPIO
descriptor on error without risking a double put.

Release the enable GPIO descriptor when devres allocation fails in
devm_regulator_register() so the ownership contract matches
regulator_register() across all error paths.

Fixes: 0edb040d416a ("regulator: core: Track dangling GPIO descriptors")
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
Fix an enable GPIO descriptor leak in devm_regulator_register() when
devres_alloc() fails.
---
 drivers/regulator/devres.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/regulator/devres.c b/drivers/regulator/devres.c
index 62c67b9fd0cf..cab1d2005102 100644
--- a/drivers/regulator/devres.c
+++ b/drivers/regulator/devres.c
@@ -7,6 +7,7 @@
 
 #include <linux/kernel.h>
 #include <linux/err.h>
+#include <linux/gpio/consumer.h>
 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
 #include <linux/regulator/driver.h>
@@ -469,8 +470,11 @@ struct regulator_dev *devm_regulator_register(struct device *dev,
 
 	ptr = devres_alloc(devm_rdev_release, sizeof(*ptr),
 			   GFP_KERNEL);
-	if (!ptr)
+	if (!ptr) {
+		if (config && config->ena_gpiod)
+			gpiod_put(config->ena_gpiod);
 		return ERR_PTR(-ENOMEM);
+	}
 
 	rdev = regulator_register(dev, regulator_desc, config);
 	if (!IS_ERR(rdev)) {

---
base-commit: 253f86660c1f882fbf554db5eaa34bc5d66dc7e8
change-id: 20261130-regulator-devm-gpiod-fix-16e1bb62406b

Thanks.

-- 
Dmitry


             reply	other threads:[~2026-12-01  5:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  5:52 Dmitry Torokhov [this message]
2026-12-01 21:16 ` Linus Walleij

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=20261130-regulator-devm-gpiod-fix-v1-1-24f25d1486f3@gmail.com \
    --to=dmitry.torokhov@gmail.com \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linusw@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®