Skip to content

RFC regmap: convert map->lock/unlock users to a scoped guard

Peng Fan (OSS) driver-core lkml 3 versions – In review

Get series with b4
b4 am [email protected] change-id 20260921-regmap-lock-guard-939bbf9dbeee

Versions newest first

  1. v3 RFC Latest4 patches 1 replyIn review
  2. v2 RFC4 patches 1 replySuperseded
  3. v1 RFC4 patches 2 repliesSuperseded

Changes in v3 from the cover letter

- Drop the conversions regcache_init to avoid mix goto and scoped_guard
in one function.
- Link to v2: https://patch.msgid.link/[email protected]

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4regmap: convert lock/unlock to a scoped guarddrivers/base/regmap/internal.h drivers/base/regmap/regmap.c+70 -1230 replies
2/4regcache: use the regmap scoped lock guarddrivers/base/regmap/regcache.c+8 -200 replies
3/4regcache: rbtree: use the regmap scoped lock guarddrivers/base/regmap/regcache-rbtree.c+1 -30 replies
4/4regmap: debugfs: use the regmap scoped lock guarddrivers/base/regmap/regmap-debugfs.c+10 -140 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v3 yet.

Read the v3 thread

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

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);

The 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 the 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() 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.

regcache_sync() and regcache_sync_region() are deliberately left
unconverted: they already use a single goto out unlock path, so a guard would save nothing while forcing either a goto inside a scoped_guard scope or a control-flow rewrite, neither of which is an improvement.

This patchset removes many manual unlock statements together with the associated goto out_unlock labels.

The series is split to keep the regcache: and regmap: changes on their own commits and to preserve independent revertibility:

1. define the guard and convert regmap.c
2. convert regcache.c (except the sync helpers, see above)
3. convert the regcache rbtree debugfs dump
4. convert the regmap debugfs write handlers

No functional change.

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. Built clean with sparse (C=1) showing no lock-context imbalance warnings.

---
Changes in v3:
- Drop the conversions regcache_init to avoid mix goto and scoped_guard
in one function.
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Drop the regcache_sync() and regcache_sync_region() conversions.
They used a goto out unlock path that a scoped_guard cannot express cleanly; leaving them as map->lock()/unlock() avoids mixing goto with
a guard scope (per Mark's review). No other functional change.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
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        |  28 ++----
 drivers/base/regmap/regmap-debugfs.c  |  24 ++---
 drivers/base/regmap/regmap.c          | 182 +++++++++++-----------------------
 5 files changed, 89 insertions(+), 160 deletions(-)

---
base-commit: 0d9d0dbf2fddcff5859d623e90ca73c4054276e1
change-id: 20260921-regmap-lock-guard-939bbf9dbeee

Best regards,

Signature · 1 line
Peng Fan <[email protected]>