From: Sebastian Reichel <sebastian.reichel@collabora.com>
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>,
Heiko Stuebner <heiko@sntech.de>,
linux-rockchip@lists.infradead.org,
linux-kernel@vger.kernel.org, driver-core@lists.linux.dev,
kernel@collabora.com,
Sebastian Reichel <sebastian.reichel@collabora.com>
Subject: [PATCH] regmap: Support mutex-locked maps during atomic shutdown
Date: Fri, 27 Nov 2026 13:26:06 +0100 [thread overview]
Message-ID: <20261127-regmap-atomic-shutdown-v1-1-1a0122144df6@collabora.com> (raw)
Power-off and restart handlers run very late in the shutdown sequence,
when only one CPU is left and interrupts are disabled. I2C has long had
atomic transfer support for this, and SPI recently gained it as well,
so that PMICs can be told to move the system into poweroff mode.
Most PMIC drivers use a regmap with the default mutex locking though and
that may sleep. On a Radxa ROCK 5B+ (RK806 PMIC via SPI) with lockdep
enabled this results in:
[ BUG: Invalid wait context ]
systemd-shutdow/1 is trying to lock:
(rk8xx_spi:91:(&rk806_regmap_config_spi)->lock){+.+.}-{4:4},
at: regmap_lock_mutex+0x1c/0x30
...
Call trace:
mutex_lock_nested+0x44/0xb0
regmap_lock_mutex+0x1c/0x30
regmap_update_bits_base+0x40/0x90
rk808_power_off+0x70/0x110
sys_off_notify+0x48/0x80
atomic_notifier_call_chain+0x74/0x130
do_kernel_power_off+0x5c/0x80
Detect this stage using the same check as the I2C and SPI cores and
just skip the locking as the regmap cannot be accessed by anything
else at this point.
Note that writing a cached register, which is not yet present in the
cache, may allocate memory in regcache_write() resulting in a potential
sleep. But this is not a problem in practice, as registers used for
power-off are usually volatile.
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
---
drivers/base/regmap/regmap.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 0ba82fcc55bf..e73c4df194e5 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -440,15 +440,29 @@ static void regmap_lock_unlock_none(void *__map)
}
+static inline bool regmap_in_atomic_shutdown(void)
+{
+ return system_state > SYSTEM_RUNNING &&
+ (IS_ENABLED(CONFIG_PREEMPT_COUNT) ? !preemptible() : irqs_disabled());
+}
+
static void regmap_lock_mutex(void *__map)
{
struct regmap *map = __map;
+
+ if (regmap_in_atomic_shutdown())
+ return;
+
mutex_lock(&map->mutex);
}
static void regmap_unlock_mutex(void *__map)
{
struct regmap *map = __map;
+
+ if (regmap_in_atomic_shutdown())
+ return;
+
mutex_unlock(&map->mutex);
}
---
base-commit: 222b61e9c570dac6c7a786d039e4c226624b426d
change-id: 20261127-regmap-atomic-shutdown-ba5a1c988107
Best regards,
--
Sebastian Reichel <sebastian.reichel@collabora.com>
reply other threads:[~2026-11-27 12:27 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=20261127-regmap-atomic-shutdown-v1-1-1a0122144df6@collabora.com \
--to=sebastian.reichel@collabora.com \
--cc=broonie@kernel.org \
--cc=dakr@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=heiko@sntech.de \
--cc=johan@kernel.org \
--cc=kernel@collabora.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rockchip@lists.infradead.org \
--cc=rafael@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®