mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] regulator: devres: release enable GPIO on devres_alloc() failure
@ 2026-12-01  5:52 Dmitry Torokhov
  0 siblings, 0 replies; only message in thread
From: Dmitry Torokhov @ 2026-12-01  5:52 UTC (permalink / raw)
  To: Liam Girdwood, Mark Brown; +Cc: Linus Walleij, linux-kernel

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-01  5:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01  5:52 [PATCH] regulator: devres: release enable GPIO on devres_alloc() failure Dmitry Torokhov

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®