Skip to contentSkip to conversation

regmap: fix reinit cache failure handling

PATCH In review Peng Fan 2 participants 2 messages Nov 17 – Nov 18

Subsystems (2)
lore
Share & tools
lore

Peng Fan

author
#0 lore raw

PATCH 1/1 regmap: fix reinit cache failure handling

From: Peng Fan <[email protected]>

regmap_reinit_cache() tears down the existing cache and then updates the map configuration. regcache_exit() frees reg_defaults_raw when cache_free is set but does not clear cache_free or the pointer, and neither regmap_set_name() nor regcache_init() resets map->cache_type on failure.

If the reinitialisation then fails, a later regmap_exit() calls regcache_exit() again. It frees reg_defaults_raw a second time and runs cache_ops->exit() on the already torn-down cache. A stale cache_ops can also make regmap_volatile() treat registers as cacheable on a map that has no cache.

Clear cache_free and reg_defaults_raw in regcache_hw_exit(), and mark the map as having no cache when regmap_reinit_cache() fails after tearing down the old one. Assign the new cache type only after regmap_set_name() has succeeded so the failure paths cannot leave a type without cache ops.

Fixes bf315173359b ("regmap: Allow drivers to reinitialise the register cache at runtime")

---

V1:
Separate from https://lore.kernel.org/all/[email protected]/

 drivers/base/regmap/regcache.c | 10 ++++++++--
 drivers/base/regmap/regmap.c   | 14 +++++++++++---
 2 files changed, 19 insertions(+), 5 deletions(-)
drivers/base/regmap/regcache.c +8 -2
index 1447967ff00b..5b36ac48e394 100644
@@ -122,8 +122,11 @@ static int regcache_hw_init(struct regmap *map, int count)
 
 static void regcache_hw_exit(struct regmap *map)
 {
-	if (map->cache_free)
+	if (map->cache_free) {
 		kfree(map->reg_defaults_raw);
+		map->reg_defaults_raw = NULL;
+		map->cache_free = false;
+	}
 }
 
 static int regcache_locked_op(struct regmap *map,
@@ -276,18 +279,20 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
 	}
 err_free_reg_defaults:
 	kfree(map->reg_defaults);
+	map->reg_defaults = NULL;
 
 	return ret;
 }
 
 void regcache_exit(struct regmap *map)
 {
+	regcache_hw_exit(map);
+
 	if (map->cache_type == REGCACHE_NONE)
 		return;
 
 	BUG_ON(!map->cache_ops);
 
-	regcache_hw_exit(map);
 	if (map->cache_ops->exit) {
 		dev_dbg(map->dev, "Destroying %s cache\n",
 			map->cache_ops->name);
@@ -296,6 +301,7 @@ void regcache_exit(struct regmap *map)
 
 	kfree(map->reg_defaults);
 	map->reg_defaults = NULL;
+	map->cache_dirty = false;
 }
 
 /**
drivers/base/regmap/regmap.c +11 -3
index 7bb90d8aed3d..57de64241eb7 100644
@@ -1452,6 +1452,7 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
 
 	regmap_debugfs_exit(map);
 	regcache_exit(map);
+	map->cache_ops = NULL;
 
 	map->max_register = config->max_register;
 	map->max_register_is_set = map->max_register ?: config->max_register_is_0;
@@ -1462,22 +1463,29 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
 	map->writeable_noinc_reg = config->writeable_noinc_reg;
 	map->readable_noinc_reg = config->readable_noinc_reg;
 	map->reg_default_cb = config->reg_default_cb;
-	map->cache_type = config->cache_type;
 
 	ret = regmap_set_name(map, config);
 	if (ret)
-		return ret;
+		goto err_none;
 
 	map->cache_bypass = false;
 	map->cache_only = false;
 
+	map->cache_type = config->cache_type;
 	ret = regcache_init(map, config);
 	if (ret)
-		return ret;
+		goto err_none;
 
 	regmap_debugfs_init(map);
 
 	return 0;
+
+err_none:
+	map->cache_ops = NULL;
+	map->cache_type = REGCACHE_NONE;
+	map->cache_bypass = true;
+	map->cache_only = false;
+	return ret;
 }
 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
 
Signature · 1 line
2.56.0
maintainer
#1 lore raw
Quoted 1 line · Peng Fan

regmap: fix reinit cache failure handling

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git for-7.5

Thanks!

[1/1] regmap: fix reinit cache failure handling

      https://git.kernel.org/broonie/regmap/c/0a94ff3d3cc9

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