[PATCH] regulator: devres: release enable GPIO on devres_alloc() failure

From: Dmitry Torokhov

Date: Tue Dec 01 2026 - 00:52:52 EST


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@xxxxxxxxx>
---
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