Skip to content

Commit 94cc89e

Browse files
charleskeepaxbroonie
authored andcommitted
regmap: debugfs: Fix handling of name string for debugfs init delays
In regmap_debugfs_init the initialisation of the debugfs is delayed if the root node isn't ready yet. Most callers of regmap_debugfs_init pass the name from the regmap_config, which is considered temporary ie. may be unallocated after the regmap_init call returns. This leads to a potential use after free, where config->name has been freed by the time it is used in regmap_debugfs_initcall. This situation can be seen on Zynq, where the architecture init_irq callback registers a syscon device, using a local variable for the regmap_config. As init_irq is very early in the platform bring up the regmap debugfs root isn't ready yet. Although this doesn't crash it does result in the debugfs entry not having the correct name. Regmap already sets map->name from config->name on the regmap_init path and the fact that a separate field is used to pass the name to regmap_debugfs_init appears to be an artifact of the debugfs name being added before the map name. As such this patch updates regmap_debugfs_init to use map->name, which is already duplicated from the config avoiding the issue. This does however leave two lose ends, both regmap_attach_dev and regmap_reinit_cache can be called after a regmap is registered and would have had the effect of applying a new name to the debugfs entries. In both of these cases it was chosen to update the map name. In the case of regmap_attach_dev there are 3 users that currently use this function to update the name, thus doing so avoids changes for those users and it seems reasonable that attaching a device would want to set the name of the map. In the case of regmap_reinit_cache the primary use-case appears to be devices that need some register access to identify the device (for example devices in the same family) and then update the cache to match the exact hardware. Whilst no users do currently update the name here, given the use-case it seemed reasonable the name might want to be updated once the device is better identified. Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com> Link: https://lore.kernel.org/r/20200917120828.12987-1-ckeepax@opensource.cirrus.com Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent d012a71 commit 94cc89e

3 files changed

Lines changed: 38 additions & 17 deletions

File tree

‎drivers/base/regmap/internal.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ struct regmap_field {
217217

218218
#ifdef CONFIG_DEBUG_FS
219219
extern void regmap_debugfs_initcall(void);
220-
extern void regmap_debugfs_init(struct regmap *map, const char *name);
220+
extern void regmap_debugfs_init(struct regmap *map);
221221
extern void regmap_debugfs_exit(struct regmap *map);
222222

223223
static inline void regmap_debugfs_disable(struct regmap *map)
@@ -227,7 +227,7 @@ static inline void regmap_debugfs_disable(struct regmap *map)
227227

228228
#else
229229
static inline void regmap_debugfs_initcall(void) { }
230-
static inline void regmap_debugfs_init(struct regmap *map, const char *name) { }
230+
static inline void regmap_debugfs_init(struct regmap *map) { }
231231
static inline void regmap_debugfs_exit(struct regmap *map) { }
232232
static inline void regmap_debugfs_disable(struct regmap *map) { }
233233
#endif

‎drivers/base/regmap/regmap-debugfs.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
struct regmap_debugfs_node {
1919
struct regmap *map;
20-
const char *name;
2120
struct list_head link;
2221
};
2322

@@ -544,11 +543,12 @@ static const struct file_operations regmap_cache_bypass_fops = {
544543
.write = regmap_cache_bypass_write_file,
545544
};
546545

547-
void regmap_debugfs_init(struct regmap *map, const char *name)
546+
void regmap_debugfs_init(struct regmap *map)
548547
{
549548
struct rb_node *next;
550549
struct regmap_range_node *range_node;
551550
const char *devname = "dummy";
551+
const char *name = map->name;
552552

553553
/*
554554
* Userspace can initiate reads from the hardware over debugfs.
@@ -569,7 +569,6 @@ void regmap_debugfs_init(struct regmap *map, const char *name)
569569
if (!node)
570570
return;
571571
node->map = map;
572-
node->name = name;
573572
mutex_lock(&regmap_debugfs_early_lock);
574573
list_add(&node->link, &regmap_debugfs_early_list);
575574
mutex_unlock(&regmap_debugfs_early_lock);
@@ -679,7 +678,7 @@ void regmap_debugfs_initcall(void)
679678

680679
mutex_lock(&regmap_debugfs_early_lock);
681680
list_for_each_entry_safe(node, tmp, &regmap_debugfs_early_list, link) {
682-
regmap_debugfs_init(node->map, node->name);
681+
regmap_debugfs_init(node->map);
683682
list_del(&node->link);
684683
kfree(node);
685684
}

‎drivers/base/regmap/regmap.c‎

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -581,14 +581,34 @@ static void regmap_range_exit(struct regmap *map)
581581
kfree(map->selector_work_buf);
582582
}
583583

584+
static int regmap_set_name(struct regmap *map, const struct regmap_config *config)
585+
{
586+
if (config->name) {
587+
const char *name = kstrdup_const(config->name, GFP_KERNEL);
588+
589+
if (!name)
590+
return -ENOMEM;
591+
592+
kfree_const(map->name);
593+
map->name = name;
594+
}
595+
596+
return 0;
597+
}
598+
584599
int regmap_attach_dev(struct device *dev, struct regmap *map,
585600
const struct regmap_config *config)
586601
{
587602
struct regmap **m;
603+
int ret;
588604

589605
map->dev = dev;
590606

591-
regmap_debugfs_init(map, config->name);
607+
ret = regmap_set_name(map, config);
608+
if (ret)
609+
return ret;
610+
611+
regmap_debugfs_init(map);
592612

593613
/* Add a devres resource for dev_get_regmap() */
594614
m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
@@ -674,9 +694,9 @@ struct regmap *__regmap_init(struct device *dev,
674694
const char *lock_name)
675695
{
676696
struct regmap *map;
677-
int ret = -EINVAL;
678697
enum regmap_endian reg_endian, val_endian;
679698
int i, j;
699+
int ret;
680700

681701
if (!config)
682702
goto err;
@@ -687,13 +707,9 @@ struct regmap *__regmap_init(struct device *dev,
687707
goto err;
688708
}
689709

690-
if (config->name) {
691-
map->name = kstrdup_const(config->name, GFP_KERNEL);
692-
if (!map->name) {
693-
ret = -ENOMEM;
694-
goto err_map;
695-
}
696-
}
710+
ret = regmap_set_name(map, config);
711+
if (ret)
712+
goto err_map;
697713

698714
if (config->disable_locking) {
699715
map->lock = map->unlock = regmap_lock_unlock_none;
@@ -1137,7 +1153,7 @@ struct regmap *__regmap_init(struct device *dev,
11371153
if (ret != 0)
11381154
goto err_regcache;
11391155
} else {
1140-
regmap_debugfs_init(map, config->name);
1156+
regmap_debugfs_init(map);
11411157
}
11421158

11431159
return map;
@@ -1297,6 +1313,8 @@ EXPORT_SYMBOL_GPL(regmap_field_free);
12971313
*/
12981314
int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
12991315
{
1316+
int ret;
1317+
13001318
regcache_exit(map);
13011319
regmap_debugfs_exit(map);
13021320

@@ -1309,7 +1327,11 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
13091327
map->readable_noinc_reg = config->readable_noinc_reg;
13101328
map->cache_type = config->cache_type;
13111329

1312-
regmap_debugfs_init(map, config->name);
1330+
ret = regmap_set_name(map, config);
1331+
if (ret)
1332+
return ret;
1333+
1334+
regmap_debugfs_init(map);
13131335

13141336
map->cache_bypass = false;
13151337
map->cache_only = false;

0 commit comments

Comments
 (0)