[PATCH RFC v2 2/4] regcache: use the regmap scoped lock guard
From: Peng Fan (OSS)
Date: Tue Sep 22 2026 - 07:23:12 EST
From: Peng Fan <peng.fan@xxxxxxx>
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_init() and regcache_exit(),
where the locked region is a subsection of the function, and
guard(regmap)() for the function-scope critical sections.
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.
regcache_init() keeps its goto err_* cleanup ladder alongside the
scoped_guard(). This does not conflict with the cleanup.h guidance
against mixing goto with cleanup helpers: that rule concerns a single
resource whose lifetime is managed by a goto. Here the goto ladder
only unwinds the allocation / hw-init path (kfree(map->reg_defaults),
regcache_hw_exit(), ->exit()), while the scoped_guard() covers a
different resource - the transient map lock around each callback - that
is acquired and released within its one-line scope and is never held
across a goto. The two mechanisms manage independent resources and no
goto ever crosses a live guard.
No functional change.
Assisted-by: Claude:claude-opus-4.8
Signed-off-by: Peng Fan <peng.fan@xxxxxxx>
---
drivers/base/regmap/regcache.c | 43 ++++++++++++++----------------------------
1 file changed, 14 insertions(+), 29 deletions(-)
diff --git a/drivers/base/regmap/regcache.c b/drivers/base/regmap/regcache.c
index 0d58d900a2ca..d030fcbe11d9 100644
--- a/drivers/base/regmap/regcache.c
+++ b/drivers/base/regmap/regcache.c
@@ -225,9 +225,8 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
if (map->cache_ops->init) {
dev_dbg(map->dev, "Initializing %s cache\n",
map->cache_ops->name);
- map->lock(map->lock_arg);
- ret = map->cache_ops->init(map);
- map->unlock(map->lock_arg);
+ scoped_guard(regmap, map)
+ ret = map->cache_ops->init(map);
if (ret)
goto err_free_reg_defaults;
}
@@ -246,9 +245,8 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
if (map->cache_ops->populate &&
(map->num_reg_defaults || map->reg_default_cb)) {
dev_dbg(map->dev, "Populating %s cache\n", map->cache_ops->name);
- map->lock(map->lock_arg);
- ret = map->cache_ops->populate(map);
- map->unlock(map->lock_arg);
+ scoped_guard(regmap, map)
+ ret = map->cache_ops->populate(map);
if (ret)
goto err_free;
}
@@ -259,9 +257,8 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
err_exit:
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);
}
err_free_reg_defaults:
kfree(map->reg_defaults);
@@ -281,9 +278,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 +580,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 +605,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 +628,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 +647,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 +667,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);
--
2.51.0