The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH 0/2] regmap: Fix bus context frees on init failure
@ 2026-11-30 20:21 Arun Iyengar via B4 Relay
  2026-11-30 20:21 ` [PATCH 1/2] regmap: Don't free the bus context when devm init fails Arun Iyengar via B4 Relay
  2026-11-30 20:21 ` [PATCH 2/2] regmap: mmio: Free the context if regmap " Arun Iyengar via B4 Relay
  0 siblings, 2 replies; 3+ messages in thread
From: Arun Iyengar via B4 Relay @ 2026-11-30 20:21 UTC (permalink / raw)
  To: Mark Brown
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Johan Hovold, Krzysztof Kozlowski, linux-kernel, driver-core,
	Guenter Roeck, linux-hwmon, Xu Yilun, Arun Iyengar

Since b1ef855c6260, a failing __devm_regmap_init() can free the bus
context, so spi-avmm and sch56xx free it twice. Patch 1 restores the
old behaviour. Patch 2 fixes the mmio context leak on init failure.

Tested with failslab on an Orange Pi 5 (devm mmio regmap): kmemleak
finds the mmio leak, and with patch 2 alone KASAN reports the double
free. With both patches, no reports. spi-avmm and sch56xx were not run.

Developed with Claude (Anthropic), which wrote the code and the commit
messages. I reviewed every change.

---
Arun Iyengar (2):
      regmap: Don't free the bus context when devm init fails
      regmap: mmio: Free the context if regmap init fails

 drivers/base/regmap/regmap-mmio.c | 18 ++++++++++++++----
 drivers/base/regmap/regmap.c      | 20 ++++++++++++--------
 2 files changed, 26 insertions(+), 12 deletions(-)
---
base-commit: 222b61e9c570dac6c7a786d039e4c226624b426d
change-id: 20261129-regmap-devm-ctx-2955bb2c1077

Best regards,
--  
Arun Iyengar <aiyengar1990@gmail.com>



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] regmap: Don't free the bus context when devm init fails
  2026-11-30 20:21 [PATCH 0/2] regmap: Fix bus context frees on init failure Arun Iyengar via B4 Relay
@ 2026-11-30 20:21 ` Arun Iyengar via B4 Relay
  2026-11-30 20:21 ` [PATCH 2/2] regmap: mmio: Free the context if regmap " Arun Iyengar via B4 Relay
  1 sibling, 0 replies; 3+ messages in thread
From: Arun Iyengar via B4 Relay @ 2026-11-30 20:21 UTC (permalink / raw)
  To: Mark Brown
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Johan Hovold, Krzysztof Kozlowski, linux-kernel, driver-core,
	Guenter Roeck, linux-hwmon, Xu Yilun, Arun Iyengar

From: Arun Iyengar <aiyengar1990@gmail.com>

Since the devres is added after __regmap_init(), a failure to add it
runs regmap_exit(), which frees the bus context. Callers that free
their context when __devm_regmap_init() fails (spi-avmm, sch56xx) then
free it twice.

Allocate the devres before __regmap_init() again.

Fixes: b1ef855c6260 ("regmap: Simplify devres handling")
Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
 drivers/base/regmap/regmap.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 0ba82fcc55bf..b4e07f8d6e91 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -1212,9 +1212,9 @@ struct regmap *__regmap_init(struct device *dev,
 }
 EXPORT_SYMBOL_GPL(__regmap_init);
 
-static void devm_regmap_release(void *regmap)
+static void devm_regmap_release(struct device *dev, void *res)
 {
-	regmap_exit(regmap);
+	regmap_exit(*(struct regmap **)res);
 }
 
 struct regmap *__devm_regmap_init(struct device *dev,
@@ -1224,17 +1224,21 @@ struct regmap *__devm_regmap_init(struct device *dev,
 				  struct lock_class_key *lock_key,
 				  const char *lock_name)
 {
-	struct regmap *regmap;
-	int ret;
+	struct regmap **ptr, *regmap;
+
+	ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
+	if (!ptr)
+		return ERR_PTR(-ENOMEM);
 
 	regmap = __regmap_init(dev, bus, bus_context, config,
 			       lock_key, lock_name);
-	if (IS_ERR(regmap))
+	if (IS_ERR(regmap)) {
+		devres_free(ptr);
 		return regmap;
+	}
 
-	ret = devm_add_action_or_reset(dev, devm_regmap_release, regmap);
-	if (ret)
-		return ERR_PTR(ret);
+	*ptr = regmap;
+	devres_add(dev, ptr);
 
 	return regmap;
 }

-- 
2.55.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] regmap: mmio: Free the context if regmap init fails
  2026-11-30 20:21 [PATCH 0/2] regmap: Fix bus context frees on init failure Arun Iyengar via B4 Relay
  2026-11-30 20:21 ` [PATCH 1/2] regmap: Don't free the bus context when devm init fails Arun Iyengar via B4 Relay
@ 2026-11-30 20:21 ` Arun Iyengar via B4 Relay
  1 sibling, 0 replies; 3+ messages in thread
From: Arun Iyengar via B4 Relay @ 2026-11-30 20:21 UTC (permalink / raw)
  To: Mark Brown
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Johan Hovold, Krzysztof Kozlowski, linux-kernel, driver-core,
	Guenter Roeck, linux-hwmon, Xu Yilun, Arun Iyengar

From: Arun Iyengar <aiyengar1990@gmail.com>

The MMIO context is freed only by regmap_exit(), so it leaks, together
with its prepared clock, when __regmap_init() or __devm_regmap_init()
fails. Free it on that error.

The devm part needs the previous patch: without it a failed devres
add has already freed the context.

Fixes: 45f5ff8107a8 ("regmap: add MMIO bus support")
Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
 drivers/base/regmap/regmap-mmio.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/drivers/base/regmap/regmap-mmio.c b/drivers/base/regmap/regmap-mmio.c
index 6adbc950877c..a7eeb9498cd2 100644
--- a/drivers/base/regmap/regmap-mmio.c
+++ b/drivers/base/regmap/regmap-mmio.c
@@ -559,13 +559,18 @@ struct regmap *__regmap_init_mmio_clk(struct device *dev, const char *clk_id,
 				      const char *lock_name)
 {
 	struct regmap_mmio_context *ctx;
+	struct regmap *map;
 
 	ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
 	if (IS_ERR(ctx))
 		return ERR_CAST(ctx);
 
-	return __regmap_init(dev, &regmap_mmio, ctx, config,
-			     lock_key, lock_name);
+	map = __regmap_init(dev, &regmap_mmio, ctx, config,
+			    lock_key, lock_name);
+	if (IS_ERR(map))
+		regmap_mmio_free_context(ctx);
+
+	return map;
 }
 EXPORT_SYMBOL_GPL(__regmap_init_mmio_clk);
 
@@ -577,13 +582,18 @@ struct regmap *__devm_regmap_init_mmio_clk(struct device *dev,
 					   const char *lock_name)
 {
 	struct regmap_mmio_context *ctx;
+	struct regmap *map;
 
 	ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
 	if (IS_ERR(ctx))
 		return ERR_CAST(ctx);
 
-	return __devm_regmap_init(dev, &regmap_mmio, ctx, config,
-				  lock_key, lock_name);
+	map = __devm_regmap_init(dev, &regmap_mmio, ctx, config,
+				 lock_key, lock_name);
+	if (IS_ERR(map))
+		regmap_mmio_free_context(ctx);
+
+	return map;
 }
 EXPORT_SYMBOL_GPL(__devm_regmap_init_mmio_clk);
 

-- 
2.55.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-30 20:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 20:21 [PATCH 0/2] regmap: Fix bus context frees on init failure Arun Iyengar via B4 Relay
2026-11-30 20:21 ` [PATCH 1/2] regmap: Don't free the bus context when devm init fails Arun Iyengar via B4 Relay
2026-11-30 20:21 ` [PATCH 2/2] regmap: mmio: Free the context if regmap " Arun Iyengar via B4 Relay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox