mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Arun Iyengar via B4 Relay <devnull+aiyengar1990.gmail.com@kernel.org>
To: Mark Brown <broonie@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 "Rafael J. Wysocki" <rafael@kernel.org>,
	Danilo Krummrich <dakr@kernel.org>,
	 Johan Hovold <johan@kernel.org>,
	Maxime Ripard <mripard@kernel.org>,
	 linux-kernel@vger.kernel.org, driver-core@lists.linux.dev,
	 stable@vger.kernel.org, Arun Iyengar <aiyengar1990@gmail.com>
Subject: [PATCH] regmap: mmio: Don't attach the clock if clk_prepare() fails
Date: Thu, 19 Nov 2026 16:50:01 +0000	[thread overview]
Message-ID: <20261119-regmap-mmio-attach-clk-v1-1-91291802ac53@gmail.com> (raw)

From: Arun Iyengar <aiyengar1990@gmail.com>

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@vger.kernel.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/r/179493691305.3136.16311404541139130213@kernel.org
Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
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@gmail.com>



                 reply	other threads:[~2026-11-19 16:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119-regmap-mmio-attach-clk-v1-1-91291802ac53@gmail.com \
    --to=devnull+aiyengar1990.gmail.com@kernel.org \
    --cc=aiyengar1990@gmail.com \
    --cc=broonie@kernel.org \
    --cc=dakr@kernel.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=johan@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mripard@kernel.org \
    --cc=rafael@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®