[PATCH v2 7/7] regmap: debugfs: Use guard to simplify code
From: Peng Fan (OSS)
Date: Fri Oct 02 2026 - 09:28:19 EST
From: Peng Fan <peng.fan@xxxxxxx>
Replace open-coded mutex_lock()/unlock() pairs with guard() and
scoped_guard() across the debugfs code.
- regmap_debugfs_get_dump_start(): guard(mutex) replaces three
unlock sites (kzalloc failure, cache hit, end-of-function).
- regmap_next_readable_reg(): early return for the common case
and scoped_guard for the cache fallback path.
- regmap_reg_ranges_read_file(): scoped_guard wraps the cache
iteration loop.
- regmap_debugfs_init(): scoped_guard for single list_add.
- regmap_debugfs_exit(): flatten if/else into early return and
scoped_guard for the early-list cleanup.
- regmap_debugfs_initcall(): guard(mutex) held to function end.
No functional change.
Signed-off-by: Peng Fan <peng.fan@xxxxxxx>
---
drivers/base/regmap/regmap-debugfs.c | 63 +++++++++++++++++-------------------
1 file changed, 29 insertions(+), 34 deletions(-)
diff --git a/drivers/base/regmap/regmap-debugfs.c b/drivers/base/regmap/regmap-debugfs.c
index 0a9c27426f15..b2ae52a21331 100644
--- a/drivers/base/regmap/regmap-debugfs.c
+++ b/drivers/base/regmap/regmap-debugfs.c
@@ -107,7 +107,7 @@ static unsigned int regmap_debugfs_get_dump_start(struct regmap *map,
* If we don't have a cache build one so we don't have to do a
* linear scan each time.
*/
- mutex_lock(&map->cache_lock);
+ guard(mutex)(&map->cache_lock);
i = base;
if (list_empty(&map->debugfs_off_cache)) {
for (; i <= map->max_register; i += map->reg_stride) {
@@ -129,7 +129,6 @@ static unsigned int regmap_debugfs_get_dump_start(struct regmap *map,
c = kzalloc_obj(*c);
if (!c) {
regmap_debugfs_free_dump_cache(map);
- mutex_unlock(&map->cache_lock);
return base;
}
c->min = p;
@@ -162,14 +161,12 @@ static unsigned int regmap_debugfs_get_dump_start(struct regmap *map,
fpos_offset = from - c->min;
reg_offset = fpos_offset / map->debugfs_tot_len;
*pos = c->min + (reg_offset * map->debugfs_tot_len);
- mutex_unlock(&map->cache_lock);
return c->base_reg + (reg_offset * map->reg_stride);
}
*pos = c->max;
ret = c->max_reg;
}
- mutex_unlock(&map->cache_lock);
return ret;
}
@@ -191,10 +188,10 @@ static int regmap_next_readable_reg(struct regmap *map, int reg)
struct regmap_debugfs_off_cache *c;
int ret = -EINVAL;
- if (regmap_printable(map, reg + map->reg_stride)) {
- ret = reg + map->reg_stride;
- } else {
- mutex_lock(&map->cache_lock);
+ if (regmap_printable(map, reg + map->reg_stride))
+ return reg + map->reg_stride;
+
+ scoped_guard(mutex, &map->cache_lock) {
list_for_each_entry(c, &map->debugfs_off_cache, list) {
if (reg > c->max_reg)
continue;
@@ -203,8 +200,8 @@ static int regmap_next_readable_reg(struct regmap *map, int reg)
break;
}
}
- mutex_unlock(&map->cache_lock);
}
+
return ret;
}
@@ -393,19 +390,19 @@ static ssize_t regmap_reg_ranges_read_file(struct file *file,
/* Reset file pointer as the fixed-format of the `registers'
* file is not compatible with the `range' file */
p = 0;
- mutex_lock(&map->cache_lock);
- list_for_each_entry(c, &map->debugfs_off_cache, list) {
- entry_len = snprintf(entry, PAGE_SIZE, "%x-%x\n",
- c->base_reg, c->max_reg);
- if (p >= *ppos) {
- if (buf_pos + entry_len > count)
- break;
- memcpy(buf + buf_pos, entry, entry_len);
- buf_pos += entry_len;
+ scoped_guard(mutex, &map->cache_lock) {
+ list_for_each_entry(c, &map->debugfs_off_cache, list) {
+ entry_len = snprintf(entry, PAGE_SIZE, "%x-%x\n",
+ c->base_reg, c->max_reg);
+ if (p >= *ppos) {
+ if (buf_pos + entry_len > count)
+ break;
+ memcpy(buf + buf_pos, entry, entry_len);
+ buf_pos += entry_len;
+ }
+ p += entry_len;
}
- p += entry_len;
}
- mutex_unlock(&map->cache_lock);
kfree(entry);
ret = buf_pos;
@@ -552,9 +549,8 @@ void regmap_debugfs_init(struct regmap *map)
if (!node)
return;
node->map = map;
- mutex_lock(®map_debugfs_early_lock);
- list_add(&node->link, ®map_debugfs_early_list);
- mutex_unlock(®map_debugfs_early_lock);
+ scoped_guard(mutex, ®map_debugfs_early_lock)
+ list_add(&node->link, ®map_debugfs_early_list);
return;
}
@@ -653,29 +649,29 @@ void regmap_debugfs_init(struct regmap *map)
void regmap_debugfs_exit(struct regmap *map)
{
+ struct regmap_debugfs_node *node, *tmp;
+
if (map->debugfs) {
debugfs_remove_recursive(map->debugfs);
- mutex_lock(&map->cache_lock);
- regmap_debugfs_free_dump_cache(map);
- mutex_unlock(&map->cache_lock);
+ scoped_guard(mutex, &map->cache_lock)
+ regmap_debugfs_free_dump_cache(map);
if (map->debugfs_dummy_id >= 0) {
ida_free(&dummy_ida, map->debugfs_dummy_id);
map->debugfs_dummy_id = -1;
}
kfree(map->debugfs_name);
map->debugfs_name = NULL;
- } else {
- struct regmap_debugfs_node *node, *tmp;
- mutex_lock(®map_debugfs_early_lock);
- list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list,
- link) {
+ return;
+ }
+
+ scoped_guard(mutex, ®map_debugfs_early_lock) {
+ list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list, link) {
if (node->map == map) {
list_del(&node->link);
kfree(node);
}
}
- mutex_unlock(®map_debugfs_early_lock);
}
}
@@ -685,11 +681,10 @@ void regmap_debugfs_initcall(void)
regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
- mutex_lock(®map_debugfs_early_lock);
+ guard(mutex)(®map_debugfs_early_lock);
list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list, link) {
regmap_debugfs_init(node->map);
list_del(&node->link);
kfree(node);
}
- mutex_unlock(®map_debugfs_early_lock);
}
--
2.51.0