[PATCH v2] regmap: debugfs: Fix name collision without atomic operations

From: Zxyan Zhu
Date: Tue Mar 04 2025 - 09:35:15 EST


The `dummy_index` global variable caused debugfs file name conflicts
during re-entry, leading to creation failures. Use atomic operations
to ensure safe and unique debugfs `dummy%d` naming.

Changes since v1:
- Replaced atomic_read + atomic_inc with atomic_inc_return.
- Added atomic_dec in the error path to maintain index consistency.
- Updated the commit message to clarify the fix.

Signed-off-by: Zxyan Zhu <zxyan20@xxxxxxx>
---
drivers/base/regmap/regmap-debugfs.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/drivers/base/regmap/regmap-debugfs.c b/drivers/base/regmap/regmap-debugfs.c
index fb84cda92a75..60c8d9a673b8 100644
--- a/drivers/base/regmap/regmap-debugfs.c
+++ b/drivers/base/regmap/regmap-debugfs.c
@@ -20,7 +20,7 @@ struct regmap_debugfs_node {
struct list_head link;
};

-static unsigned int dummy_index;
+static atomic_t dummy_index = ATOMIC_INIT(0);
static struct dentry *regmap_debugfs_root;
static LIST_HEAD(regmap_debugfs_early_list);
static DEFINE_MUTEX(regmap_debugfs_early_lock);
@@ -549,6 +549,7 @@ void regmap_debugfs_init(struct regmap *map)
struct regmap_range_node *range_node;
const char *devname = "dummy";
const char *name = map->name;
+ int index;

/*
* Userspace can initiate reads from the hardware over debugfs.
@@ -595,12 +596,13 @@ void regmap_debugfs_init(struct regmap *map)

if (!strcmp(name, "dummy")) {
kfree(map->debugfs_name);
- map->debugfs_name = kasprintf(GFP_KERNEL, "dummy%d",
- dummy_index);
- if (!map->debugfs_name)
+ index = atomic_inc_return(&dummy_index);
+ map->debugfs_name = kasprintf(GFP_KERNEL, "dummy%d", index);
+ if (!map->debugfs_name) {
+ atomic_dec(&dummy_index);
return;
+ }
name = map->debugfs_name;
- dummy_index++;
}

map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
--
2.34.1