[PATCH RFC 0/4] regmap: convert map->lock/unlock users to a scoped guard

From: Peng Fan (OSS)

Date: Mon Sep 21 2026 - 02:06:45 EST


The regmap lock is taken by calling the lock and unlock callbacks
directly at every call site:

map->lock(map->lock_arg);
...
map->unlock(map->lock_arg);

This open-coded pattern forces every error path to unlock by hand,
which spreads goto out_unlock chains and duplicated unlock statements
throughout regmap.c and regcache.c and makes it easy to leak the lock
on a newly added return path.

Define a scoped guard for the regmap lock (DEFINE_GUARD) in internal.h
and convert all users to it. The lock and unlock callbacks are chosen
at init time (mutex, spinlock, raw spinlock, hwspinlock or none) and
return void, so an unconditional guard is sufficient. Function-scope
critical sections use guard(regmap)(); sites that must run work after
the lock is dropped - for example regmap_register_patch() and the two
regcache sync helpers calling regmap_async_complete(), and the debugfs
cache_only handler calling regcache_sync() (which takes the lock
itself) - use scoped_guard(regmap, ...) so the trailing work stays
outside the guarded region.

No functional change intended.

Tested with the regmap KUnit suite (drivers/base/regmap/regmap-kunit.c)
under ARCH=um: 551/551 tests pass, and 551/551 again with lockdep
(PROVE_LOCKING, DEBUG_LOCK_ALLOC, DEBUG_ATOMIC_SLEEP) enabled with no
splats.

Signed-off-by: Peng Fan <peng.fan@xxxxxxx>
---
Peng Fan (4):
regmap: convert lock/unlock to a scoped guard
regcache: use the regmap scoped lock guard
regcache: rbtree: use the regmap scoped lock guard
regmap: debugfs: use the regmap scoped lock guard

drivers/base/regmap/internal.h | 11 ++
drivers/base/regmap/regcache-rbtree.c | 4 +-
drivers/base/regmap/regcache.c | 193 +++++++++++++++-------------------
drivers/base/regmap/regmap-debugfs.c | 24 ++---
drivers/base/regmap/regmap.c | 182 +++++++++++---------------------
5 files changed, 167 insertions(+), 247 deletions(-)
---
base-commit: 0d9d0dbf2fddcff5859d623e90ca73c4054276e1
change-id: 20260921-regmap-lock-guard-939bbf9dbeee

Best regards,
--
Peng Fan <peng.fan@xxxxxxx>