[PATCH] regmap: mmio: Don't attach the clock if clk_prepare() fails

From: Arun Iyengar via B4 Relay

Date: Thu Nov 19 2026 - 11:51:08 EST


From: Arun Iyengar <aiyengar1990@xxxxxxxxx>

regmap_mmio_attach_clk() stores the clock before preparing it. If
clk_prepare() fails, regmap_exit() later unprepares a clock that was
never prepared, and the caller may already have put it.

Only attach the clock once it is prepared.

Fixes: 31895662f9ba ("regmap: mmio: Add function to attach a clock")
Cc: stable@xxxxxxxxxxxxxxx
Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Closes: https://lore.kernel.org/r/179493691305.3136.16311404541139130213@xxxxxxxxxx
Signed-off-by: Arun Iyengar <aiyengar1990@xxxxxxxxx>
---
Found while fixing the error path of of_syscon_register(); that mfd patch
is sent separately and neither depends on the other.

Tested on an Orange Pi 5 (RK3588S), 7.3 with KASAN, together with the mfd
patch: with a forced clk_prepare() failure in of_syscon_register(), a
use-after-free in clk_unprepare() is reported without the two patches and
nothing with both. This patch was not run on its own.

Developed with Claude (Anthropic), which wrote the code and the commit
message. I reviewed every change.
---
drivers/base/regmap/regmap-mmio.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/base/regmap/regmap-mmio.c b/drivers/base/regmap/regmap-mmio.c
index 1de2278fa572..6adbc950877c 100644
--- a/drivers/base/regmap/regmap-mmio.c
+++ b/drivers/base/regmap/regmap-mmio.c
@@ -590,11 +590,16 @@ EXPORT_SYMBOL_GPL(__devm_regmap_init_mmio_clk);
int regmap_mmio_attach_clk(struct regmap *map, struct clk *clk)
{
struct regmap_mmio_context *ctx = map->bus_context;
+ int ret;
+
+ ret = clk_prepare(clk);
+ if (ret)
+ return ret;

ctx->clk = clk;
ctx->attached_clk = true;

- return clk_prepare(ctx->clk);
+ return 0;
}
EXPORT_SYMBOL_GPL(regmap_mmio_attach_clk);


---
base-commit: 50b3d8bb527b11384153c4ca854c8610f4a55649
change-id: 20261119-regmap-mmio-attach-clk-f95ad7fd33a3

Best regards,
--
Arun Iyengar <aiyengar1990@xxxxxxxxx>