Skip to contentSkip to conversation

regcache: Split out ->populate() and use it

PATCH In review Andy Shevchenko 3 participants 13 messages Oct 31, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover regcache: Split out ->populate() and use it

This is a refactoring series to decouple cache initialisation and population. On its own it has no functional impact but will be used in the further development. Besides that I found this split useful on its own (from the design perspective). That's why I decided to send it out as is separately from a bigger (and ongoing) work.

Changelog v3:
- added missing space (Mark)
- collected tags (Sander)

Changelog v2:
v2: https://lore.kernel.org/r/[email protected]
- rebased on top of the recent regcache changes (Mark)
- a new clean up patch induced by the previous

v1: https://lore.kernel.org/r/[email protected]

Andy Shevchenko (5):
regcache: Add ->populate() callback to separate from ->init()
regcache: rbtree: Split ->populate() from ->init()
regcache: flat: Remove unneeded check and error message for -ENOMEM
regcache: flat: Split ->populate() from ->init()
regcache: maple: Split ->populate() from ->init()

 drivers/base/regmap/internal.h        |  1 +
 drivers/base/regmap/regcache-flat.c   | 37 +++++++++++----------
 drivers/base/regmap/regcache-maple.c  | 47 ++++++++++++---------------
 drivers/base/regmap/regcache-rbtree.c | 31 ++++++++++--------
 drivers/base/regmap/regcache.c        | 16 +++++++++
 5 files changed, 74 insertions(+), 58 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH v3 1/5 regcache: Add ->populate() callback to separate from ->init()

In the future changes we would like to change the flow of the cache handling. Add ->populate() callback in order to prepare for that.

---
 drivers/base/regmap/internal.h |  1 +
 drivers/base/regmap/regcache.c | 16 ++++++++++++++++
 2 files changed, 17 insertions(+)
drivers/base/regmap/internal.h +1 -0
index 8d19a1414d5b..1477329410ec 100644
@@ -186,6 +186,7 @@ struct regcache_ops {
 	enum regcache_type type;
 	int (*init)(struct regmap *map);
 	int (*exit)(struct regmap *map);
+	int (*populate)(struct regmap *map);
 #ifdef CONFIG_DEBUG_FS
 	void (*debugfs_init)(struct regmap *map);
 #endif
drivers/base/regmap/regcache.c +16 -0
index 0392f5525cf3..319c342bf5a0 100644
@@ -222,8 +222,24 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
 		if (ret)
 			goto err_free;
 	}
+
+	if (map->num_reg_defaults && map->cache_ops->populate) {
+		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);
+		if (ret)
+			goto err_exit;
+	}
 	return 0;
 
+err_exit:
+	if (map->cache_ops->exit) {
+		dev_dbg(map->dev, "Destroying %s cache\n", map->cache_ops->name);
+		map->lock(map->lock_arg);
+		ret = map->cache_ops->exit(map);
+		map->unlock(map->lock_arg);
+	}
 err_free:
 	kfree(map->reg_defaults);
 	if (map->cache_free)
Signature · 1 line
2.50.1
#3 lore raw

Re: [PATCH v3 1/5] regcache: Add ->populate() callback to separate from ->init()

Quoted 5 lines · Charles Keepax
Quoted 2 lines · Andy Shevchenko

In the future changes we would like to change the flow of the cache handling. Add ->populate() callback in order to prepare for that.

Thank you!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

PATCH v3 2/5 regcache: rbtree: Split ->populate() from ->init()

In reply to Andy Shevchenko

Split ->populate() implementation from ->init() code. This decoupling will help for the further changes.

---
 drivers/base/regmap/regcache-rbtree.c | 31 +++++++++++++++------------
 1 file changed, 17 insertions(+), 14 deletions(-)
drivers/base/regmap/regcache-rbtree.c +17 -14
index a9d17f316e55..3344b82c3799 100644
@@ -184,8 +184,6 @@ static void rbtree_debugfs_init(struct regmap *map)
 static int regcache_rbtree_init(struct regmap *map)
 {
 	struct regcache_rbtree_ctx *rbtree_ctx;
-	int i;
-	int ret;
 
 	map->cache = kmalloc(sizeof *rbtree_ctx, map->alloc_flags);
 	if (!map->cache)
@@ -195,19 +193,7 @@ static int regcache_rbtree_init(struct regmap *map)
 	rbtree_ctx->root = RB_ROOT;
 	rbtree_ctx->cached_rbnode = NULL;
 
-	for (i = 0; i < map->num_reg_defaults; i++) {
-		ret = regcache_rbtree_write(map,
-					    map->reg_defaults[i].reg,
-					    map->reg_defaults[i].def);
-		if (ret)
-			goto err;
-	}
-
 	return 0;
-
-err:
-	regcache_rbtree_exit(map);
-	return ret;
 }
 
 static int regcache_rbtree_exit(struct regmap *map)
@@ -239,6 +225,22 @@ static int regcache_rbtree_exit(struct regmap *map)
 	return 0;
 }
 
+static int regcache_rbtree_populate(struct regmap *map)
+{
+	unsigned int i;
+	int ret;
+
+	for (i = 0; i < map->num_reg_defaults; i++) {
+		ret = regcache_rbtree_write(map,
+					    map->reg_defaults[i].reg,
+					    map->reg_defaults[i].def);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static int regcache_rbtree_read(struct regmap *map,
 				unsigned int reg, unsigned int *value)
 {
@@ -546,6 +548,7 @@ struct regcache_ops regcache_rbtree_ops = {
 	.name = "rbtree",
 	.init = regcache_rbtree_init,
 	.exit = regcache_rbtree_exit,
+	.populate = regcache_rbtree_populate,
 #ifdef CONFIG_DEBUG_FS
 	.debugfs_init = rbtree_debugfs_init,
 #endif
Signature · 1 line
2.50.1
#6 lore raw

PATCH v3 3/5 regcache: flat: Remove unneeded check and error message for -ENOMEM

In reply to Andy Shevchenko

There is a convention in the kernel to avoid error messages in the cases of -ENOMEM errors. Besides that, the idea behind using struct_size() and other macros from overflow.h is to saturate the size that the following allocation call will definitely fail, hence the check and the error messaging added in regcache_flat_init() are redundant. Remove them.

---
 drivers/base/regmap/regcache-flat.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
drivers/base/regmap/regcache-flat.c +1 -9
index 3b9235bb8313..bacb7137092f 100644
@@ -30,7 +30,6 @@ struct regcache_flat_data {
 static int regcache_flat_init(struct regmap *map)
 {
 	int i;
-	size_t cache_data_size;
 	unsigned int cache_size;
 	struct regcache_flat_data *cache;
 
@@ -38,14 +37,7 @@ static int regcache_flat_init(struct regmap *map)
 		return -EINVAL;
 
 	cache_size = regcache_flat_get_index(map, map->max_register) + 1;
-	cache_data_size = struct_size(cache, data, cache_size);
-
-	if (cache_data_size == SIZE_MAX) {
-		dev_err(map->dev, "cannot allocate regmap cache");
-		return -ENOMEM;
-	}
-
-	cache = kzalloc(cache_data_size, map->alloc_flags);
+	cache = kzalloc(struct_size(cache, data, cache_size), map->alloc_flags);
 	if (!cache)
 		return -ENOMEM;
 
Signature · 1 line
2.50.1
Reviewed-by on v3 3/5
#7 lore raw

Re: [PATCH v3 3/5] regcache: flat: Remove unneeded check and error message for -ENOMEM

Quoted 10 lines · Andy Shevchenko

There is a convention in the kernel to avoid error messages in the cases of -ENOMEM errors. Besides that, the idea behind using struct_size() and other macros from overflow.h is to saturate the size that the following allocation call will definitely fail, hence the check and the error messaging added in regcache_flat_init() are redundant. Remove them.

---

Thanks,
Charles

#8 lore raw

PATCH v3 4/5 regcache: flat: Split ->populate() from ->init()

In reply to Andy Shevchenko

Split ->populate() implementation from ->init() code. This decoupling will help for the further changes.

---
 drivers/base/regmap/regcache-flat.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
drivers/base/regmap/regcache-flat.c +18 -9
index bacb7137092f..53cc59c84e2f 100644
@@ -29,7 +29,6 @@ struct regcache_flat_data {
 
 static int regcache_flat_init(struct regmap *map)
 {
-	int i;
 	unsigned int cache_size;
 	struct regcache_flat_data *cache;
 
@@ -47,14 +46,6 @@ static int regcache_flat_init(struct regmap *map)
 
 	map->cache = cache;
 
-	for (i = 0; i < map->num_reg_defaults; i++) {
-		unsigned int reg = map->reg_defaults[i].reg;
-		unsigned int index = regcache_flat_get_index(map, reg);
-
-		cache->data[index] = map->reg_defaults[i].def;
-		__set_bit(index, cache->valid);
-	}
-
 	return 0;
 
 err_free:
@@ -75,6 +66,22 @@ static int regcache_flat_exit(struct regmap *map)
 	return 0;
 }
 
+static int regcache_flat_populate(struct regmap *map)
+{
+	struct regcache_flat_data *cache = map->cache;
+	unsigned int i;
+
+	for (i = 0; i < map->num_reg_defaults; i++) {
+		unsigned int reg = map->reg_defaults[i].reg;
+		unsigned int index = regcache_flat_get_index(map, reg);
+
+		cache->data[index] = map->reg_defaults[i].def;
+		__set_bit(index, cache->valid);
+	}
+
+	return 0;
+}
+
 static int regcache_flat_read(struct regmap *map,
 			      unsigned int reg, unsigned int *value)
 {
@@ -134,6 +141,7 @@ struct regcache_ops regcache_flat_ops = {
 	.name = "flat",
 	.init = regcache_flat_init,
 	.exit = regcache_flat_exit,
+	.populate = regcache_flat_populate,
 	.read = regcache_flat_read,
 	.write = regcache_flat_write,
 };
@@ -143,6 +151,7 @@ struct regcache_ops regcache_flat_sparse_ops = {
 	.name = "flat-sparse",
 	.init = regcache_flat_init,
 	.exit = regcache_flat_exit,
+	.populate = regcache_flat_populate,
 	.read = regcache_flat_sparse_read,
 	.write = regcache_flat_write,
 	.drop = regcache_flat_drop,
Signature · 1 line
2.50.1
#10 lore raw

PATCH v3 5/5 regcache: maple: Split ->populate() from ->init()

In reply to Andy Shevchenko

Split ->populate() implementation from ->init() code. This decoupling will help for the further changes.

---
 drivers/base/regmap/regcache-maple.c | 47 +++++++++++++---------------
 1 file changed, 21 insertions(+), 26 deletions(-)
drivers/base/regmap/regcache-maple.c +21 -26
index 2319c30283a6..ca1c72b68f31 100644
@@ -289,6 +289,23 @@ static int regcache_maple_sync(struct regmap *map, unsigned int min,
 	return ret;
 }
 
+static int regcache_maple_init(struct regmap *map)
+{
+	struct maple_tree *mt;
+
+	mt = kmalloc(sizeof(*mt), map->alloc_flags);
+	if (!mt)
+		return -ENOMEM;
+	map->cache = mt;
+
+	mt_init(mt);
+
+	if (!mt_external_lock(mt) && map->lock_key)
+		lockdep_set_class_and_subclass(&mt->ma_lock, map->lock_key, 1);
+
+	return 0;
+}
+
 static int regcache_maple_exit(struct regmap *map)
 {
 	struct maple_tree *mt = map->cache;
@@ -340,26 +357,12 @@ static int regcache_maple_insert_block(struct regmap *map, int first,
 	return ret;
 }
 
-static int regcache_maple_init(struct regmap *map)
+static int regcache_maple_populate(struct regmap *map)
 {
-	struct maple_tree *mt;
 	int i;
 	int ret;
 	int range_start;
 
-	mt = kmalloc(sizeof(*mt), map->alloc_flags);
-	if (!mt)
-		return -ENOMEM;
-	map->cache = mt;
-
-	mt_init(mt);
-
-	if (!mt_external_lock(mt) && map->lock_key)
-		lockdep_set_class_and_subclass(&mt->ma_lock, map->lock_key, 1);
-
-	if (!map->num_reg_defaults)
-		return 0;
-
 	range_start = 0;
 
 	/* Scan for ranges of contiguous registers */
@@ -369,23 +372,14 @@ static int regcache_maple_init(struct regmap *map)
 			ret = regcache_maple_insert_block(map, range_start,
 							  i - 1);
 			if (ret != 0)
-				goto err;
+				return ret;
 
 			range_start = i;
 		}
 	}
 
 	/* Add the last block */
-	ret = regcache_maple_insert_block(map, range_start,
-					  map->num_reg_defaults - 1);
-	if (ret != 0)
-		goto err;
-
-	return 0;
-
-err:
-	regcache_maple_exit(map);
-	return ret;
+	return regcache_maple_insert_block(map, range_start, map->num_reg_defaults - 1);
 }
 
 struct regcache_ops regcache_maple_ops = {
@@ -393,6 +387,7 @@ struct regcache_ops regcache_maple_ops = {
 	.name = "maple",
 	.init = regcache_maple_init,
 	.exit = regcache_maple_exit,
+	.populate = regcache_maple_populate,
 	.read = regcache_maple_read,
 	.write = regcache_maple_write,
 	.drop = regcache_maple_drop,
Signature · 1 line
2.50.1
maintainer
#12 lore raw
In reply to Andy Shevchenko
Quoted 11 lines · Andy Shevchenko

This is a refactoring series to decouple cache initialisation and population. On its own it has no functional impact but will be used in the further development. Besides that I found this split useful on its own (from the design perspective). That's why I decided to send it out as is separately from a bigger (and ongoing) work.

Changelog v3:
- added missing space (Mark)
- collected tags (Sander)

[...]

Applied to

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

Thanks!

[1/5] regcache: Add ->populate() callback to separate from ->init()

      commit: 94a3a95f03154d8d4c6206950a7f6ef9a30baec6
[2/5] regcache: rbtree: Split ->populate() from ->init()
      commit: bda6f8749c8e0b10f083dc7a1edf169f349fb776
[3/5] regcache: flat: Remove unneeded check and error message for -ENOMEM
      commit: 27fef3048fe95934f6f2f87341eb33ef6581a075
[4/5] regcache: flat: Split ->populate() from ->init()
      commit: 44c1a444b030647803d900e60f5a8af31a782f0e
[5/5] regcache: maple: Split ->populate() from ->init()
      commit: ed5d499b5c9cc11dd3edae1a7a55db7dfa4f1bdc

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