[PATCH 2/2] regmap: mmio: Free the context if regmap init fails

From: Arun Iyengar via B4 Relay

Date: Mon Nov 30 2026 - 15:21:33 EST


From: Arun Iyengar <aiyengar1990@xxxxxxxxx>

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