Skip to contentSkip to conversation

regmap: Support mutex-locked maps during atomic shutdown

PATCH New Sebastian Reichel 1 participant 1 message Nov 27

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 regmap: Support mutex-locked maps during atomic shutdown

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.

---
 drivers/base/regmap/regmap.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/base/regmap/regmap.c +14 -0
index 0ba82fcc55bf..e73c4df194e5 100644
@@ -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,

Signature · 1 line
Sebastian Reichel <[email protected]>