regmap: convert map->lock/unlock users to a scoped guard
Subsystems (2)
v3 cover 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]>
RFC PATCH v3 1/4 regmap: convert lock/unlock to a scoped guard
From: Peng Fan <[email protected]>
The regmap lock and unlock callbacks are invoked directly as map->lock(map->lock_arg) / map->unlock(map->lock_arg) at every call site. This open-coded pattern requires manual unlocking on every return path, which spreads goto out_unlock chains and duplicated unlock statements throughout the code and is an easy place to leak the lock on an error path.
Define a scoped guard for the regmap lock in internal.h. The lock and unlock callbacks are chosen at init time (mutex, spinlock, raw spinlock, hwspinlock or none) and return void, so an unconditional DEFINE_GUARD() is sufficient.
Convert all lock/unlock users in regmap.c to guard(regmap)() for function-scope critical sections and scoped_guard(regmap, ...) where work must run outside the lock (e.g. regmap_register_patch() calling regmap_async_complete()). This drops every manual unlock and the associated goto out_unlock labels with no functional change.
--- drivers/base/regmap/internal.h | 11 +++ drivers/base/regmap/regmap.c | 182 +++++++++++++---------------------------- 2 files changed, 70 insertions(+), 123 deletions(-)
drivers/base/regmap/internal.h +11 -0
@@ -10,6 +10,7 @@#ifndef _REGMAP_INTERNAL_H#define _REGMAP_INTERNAL_H +#include <linux/cleanup.h> #include <linux/device.h> #include <linux/regmap.h> #include <linux/fs.h>
@@ -185,6 +186,16 @@ struct regmap {struct hwspinlock *hwlock; }; +/* +* Scoped guard for the regmap lock. The lock/unlock callbacks are selected +* at init time (mutex, spinlock, raw spinlock, hwspinlock or none) and never +* fail, so an unconditional guard is sufficient. Use with guard(regmap)(map) +* or scoped_guard(regmap, map) { ... }. +*/ +DEFINE_GUARD(regmap, struct regmap *, + _T->lock (_T->lock_arg), + _T->unlock(_T->lock_arg)) +struct regcache_ops {const char *name;enum regcache_type type;
drivers/base/regmap/regmap.c +59 -123
422 lines not on this page · raw message
Signature · 1 line
2.51.0
RFC PATCH v3 2/4 regcache: use the regmap scoped lock guard
In reply to Peng Fan (OSS)From: Peng Fan <[email protected]>
Convert the open-coded map->lock()/map->unlock() users in regcache.c to the regmap scoped guard introduced for regmap.c. Use scoped_guard(regmap, ...) in regcache_exit(), where the locked region is a subsection of the function, and guard(regmap)() for the function-scope critical sections.
regcache_init() keeps explicit map->lock()/map->unlock() calls: it has a goto err_* cleanup ladder and mixing goto with cleanup helpers in the same function is not allowed by cleanup.h.
regcache_sync() and regcache_sync_region() are left as-is: they already use a single goto out unlock path, so converting them would require either mixing a goto with a scoped_guard scope or restructuring their control flow, neither of which is an improvement.
No functional change.
--- drivers/base/regmap/regcache.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-)
drivers/base/regmap/regcache.c +8 -20
@@ -281,9 +281,8 @@ void regcache_exit(struct regmap *map)if (map->cache_ops->exit) {dev_dbg (map->dev,"Destroying %s cache\n" , map->cache_ops->name); - map->lock (map->lock_arg); - map->cache_ops->exit (map); - map->unlock (map->lock_arg); +scoped_guard (regmap, map) + map->cache_ops->exit(map); }kfree (map->reg_defaults);
@@ -584,20 +583,14 @@ EXPORT_SYMBOL_GPL(regcache_sync_region);int regcache_drop_region (struct regmap *map,unsigned int min,unsigned int max) { -int ret =0 ; -if (!map->cache_ops || !map->cache_ops->drop)return -EINVAL ; - map->lock (map->lock_arg); +guard (regmap)(map);trace_regcache_drop_region (map, min, max); - ret = map->cache_ops->drop (map, min, max); - - map->unlock (map->lock_arg); - -return ret; +return map->cache_ops->drop (map, min, max); }EXPORT_SYMBOL_GPL (regcache_drop_region);
@@ -615,12 +608,11 @@ EXPORT_SYMBOL_GPL(regcache_drop_region);*/ void regcache_cache_only (struct regmap *map,bool enable) { - map->lock (map->lock_arg); +guard (regmap)(map);WARN_ON (map->cache_type !=REGCACHE_NONE && map->cache_bypass && enable); map->cache_only = enable;trace_regmap_cache_only (map, enable); - map->unlock (map->lock_arg); }EXPORT_SYMBOL_GPL (regcache_cache_only);
@@ -639,10 +631,9 @@ EXPORT_SYMBOL_GPL(regcache_cache_only);*/ void regcache_mark_dirty (struct regmap *map) { - map->lock (map->lock_arg); +guard (regmap)(map); map->cache_dirty = true; map->no_sync_defaults = true; - map->unlock (map->lock_arg); }EXPORT_SYMBOL_GPL (regcache_mark_dirty);
@@ -659,11 +650,10 @@ EXPORT_SYMBOL_GPL(regcache_mark_dirty);*/ void regcache_cache_bypass (struct regmap *map,bool enable) { - map->lock (map->lock_arg); +guard (regmap)(map);WARN_ON (map->cache_only && enable); map->cache_bypass = enable;trace_regmap_cache_bypass (map, enable); - map->unlock (map->lock_arg); }EXPORT_SYMBOL_GPL (regcache_cache_bypass);
@@ -680,12 +670,10 @@ bool regcache_reg_cached(struct regmap *map, unsigned int reg)unsigned int val;int ret; - map->lock (map->lock_arg); +guard (regmap)(map); ret =regcache_read (map, reg, &val); - map->unlock (map->lock_arg); -return ret ==0 ; }EXPORT_SYMBOL_GPL (regcache_reg_cached);
Signature · 1 line
2.51.0
RFC PATCH v3 3/4 regcache: rbtree: use the regmap scoped lock guard
In reply to Peng Fan (OSS)From: Peng Fan <[email protected]>
Convert the open-coded map->lock()/map->unlock() pair in rbtree_show() to guard(regmap)(). The locked region spans the whole function body up to the single return, so a function-scope guard drops the manual unlock with no functional change.
--- drivers/base/regmap/regcache-rbtree.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/base/regmap/regcache-rbtree.c +1 -3
@@ -141,7 +141,7 @@ static int rbtree_show(struct seq_file *s, void *ignored)int registers =0 ;int this_registers, average; - map->lock (map->lock_arg); +guard (regmap)(map); mem_size =sizeof (*rbtree_ctx);
@@ -168,8 +168,6 @@ static int rbtree_show(struct seq_file *s, void *ignored)seq_printf (s,"%d nodes, %d registers, average %d registers, used %zu bytes\n" , nodes, registers, average, mem_size); - map->unlock (map->lock_arg); -return 0 ; }
Signature · 1 line
2.51.0
RFC PATCH v3 4/4 regmap: debugfs: use the regmap scoped lock guard
In reply to Peng Fan (OSS)From: Peng Fan <[email protected]>
Convert the open-coded map->lock()/map->unlock() users in the debugfs cache_only and cache_bypass write handlers to the regmap scoped guard.
regmap_cache_bypass_write_file() uses guard(regmap)() since the locked region runs to the return. regmap_cache_only_write_file() uses scoped_guard(regmap, ...) because the subsequent regcache_sync() must run with the lock released - it takes the regmap lock itself - so it stays outside the guarded scope exactly as before. No functional change.
--- drivers/base/regmap/regmap-debugfs.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-)
drivers/base/regmap/regmap-debugfs.c +10 -14
@@ -471,18 +471,16 @@ static ssize_t regmap_cache_only_write_file(struct file *file,if (err)return count; - map->lock (map->lock_arg); - -if (new_val && !map->cache_only) { -dev_warn (map->dev,"debugfs cache_only=Y forced\n" ); -add_taint (TAINT_USER ,LOCKDEP_STILL_OK ); - }else if (!new_val && map->cache_only) { -dev_warn (map->dev,"debugfs cache_only=N forced: syncing cache\n" ); - require_sync = true; + scoped_guard(regmap, map) { +if (new_val && !map->cache_only) { +dev_warn (map->dev,"debugfs cache_only=Y forced\n" ); +add_taint (TAINT_USER ,LOCKDEP_STILL_OK ); + }else if (!new_val && map->cache_only) { +dev_warn (map->dev,"debugfs cache_only=N forced: syncing cache\n" ); + require_sync = true; + } + map->cache_only = new_val; } - map->cache_only = new_val; - - map->unlock (map->lock_arg);if (require_sync) { err =regcache_sync (map);
@@ -513,7 +511,7 @@ static ssize_t regmap_cache_bypass_write_file(struct file *file,if (err)return count; - map->lock (map->lock_arg); +guard (regmap)(map);if (new_val && !map->cache_bypass) {dev_warn (map->dev,"debugfs cache_bypass=Y forced\n" );
@@ -523,8 +521,6 @@ static ssize_t regmap_cache_bypass_write_file(struct file *file, } map->cache_bypass = new_val; - map->unlock (map->lock_arg); -return count; }
Signature · 1 line
2.51.0
Mark Brown
maintainerQuoted 10 lines · Peng Fan (OSS)
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);[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git for-7.4
Thanks!
[1/4] regmap: convert lock/unlock to a scoped guard
https://git.kernel.org/broonie/regmap/c/80328b41ea28 [2/4] regcache: use the regmap scoped lock guard https://git.kernel.org/broonie/regmap/c/23f1120dcbea [3/4] regcache: rbtree: use the regmap scoped lock guard https://git.kernel.org/broonie/regmap/c/555a1bd78bbc [4/4] regmap: debugfs: use the regmap scoped lock guard https://git.kernel.org/broonie/regmap/c/9999d5a07ee0
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark