regmap: Introduce validity info for flat cache
Subsystems (2)
v5 cover regmap: Introduce validity info for flat cache
The flat cache behaves differently from the other caches, in that it has no way of knowing if a cache entry is valid. Initialization has to happen either by providing defaults, by loading defaults from hardware (via num_reg_defaults_raw), or by performing the required register writes. This difference in behavior may be unexpected to the user [1].
To provide feature parity between the different cache types, a new variant of the flat cache is proposed. This allows user to migrate to the sparse flat cache (or a different cache type) when possible.
In order to encourage migration, the second patch adds a new warning to inform the user of their suspicious cache.
[1] https://lore.kernel.org/linux-gpio/[email protected]/
---
Changes sinve v4:
- Improve formatting of log message
- Reduce logging to not confuse KUnit test result parser
- Add sparse flat cache to all KUnit config lists
Changes since v3:
- Split changes into two patches to provide a migration path
Changes since v2:
- Complete renaming of index variables so regcache-flat.c compiles again
Changes since v1:
- Fix off-by-one in length for bitmap_clear()
- Add REGCACHE_FLAT to the list of sparse cache tests
Sander Vanheule (2):
regmap: add flat cache with sparse validity
regmap: warn users about uninitialized flat cache
drivers/base/regmap/internal.h | 1 + drivers/base/regmap/regcache-flat.c | 98 +++++++++++++++++++++++++---- drivers/base/regmap/regcache.c | 1 + drivers/base/regmap/regmap-kunit.c | 22 +++++++ include/linux/regmap.h | 1 + 5 files changed, 112 insertions(+), 11 deletions(-)
Signature · 1 line
2.51.0
PATCH v5 1/2 regmap: add flat cache with sparse validity
The flat regcache will always assume the data in the cache is valid. Since the cache is preferred over hardware access, this may shadow the actual state of the device.
Add a new containing cache structure with the flat data table and a bitmap indicating cache validity. REGCACHE_FLAT will still behave as before, as the validity is ignored.
Define new cache type REGCACHE_FLAT_S: a flat cache with sparse validity. The sparse validity is used to determine if a hardware access should occur to initialize the cache on the fly, vs. at regmap init for REGCACHE_FLAT. Contrary to REGCACHE_FLAT, this allows us to implement regcache_ops.drop.
---
Changes since v4:
- Add REGCACHE_FLAT_S to remaining KUnit test config tables
- Shuffle code to make the patches cleaner
Changes since v3:
- Define new flat-sparse ops instead of modifying existing ones
- Update KUnit tests
- Allocate cache as flexible array to limit performance impact
--- drivers/base/regmap/internal.h | 1 + drivers/base/regmap/regcache-flat.c | 104 +++++++++++++++++++++++++--- drivers/base/regmap/regcache.c | 1 + drivers/base/regmap/regmap-kunit.c | 22 ++++++ include/linux/regmap.h | 1 + 5 files changed, 118 insertions(+), 11 deletions(-)
drivers/base/regmap/internal.h +1 -0
@@ -288,6 +288,7 @@ enum regmap_endian regmap_get_val_endian(struct device *dev,const struct regmap_bus *bus, const struct regmap_config *config); +extern struct regcache_ops regcache_flat_sparse_ops;extern struct regcache_ops regcache_rbtree_ops;extern struct regcache_ops regcache_maple_ops;extern struct regcache_ops regcache_flat_ops;
drivers/base/regmap/regcache-flat.c +93 -11
157 lines not on this page · raw message
drivers/base/regmap/regcache.c +1 -0
@@ -16,6 +16,7 @@#include "internal.h" static const struct regcache_ops *cache_types[] = { + ®cache_flat_sparse_ops, ®cache_rbtree_ops, ®cache_maple_ops, ®cache_flat_ops,
drivers/base/regmap/regmap-kunit.c +22 -0
@@ -54,6 +54,8 @@ static const char *regcache_type_name(enum regcache_type type)return "none" ;case REGCACHE_FLAT :return "flat" ; +case REGCACHE_FLAT_S : +return "flat-sparse" ;case REGCACHE_RBTREE :return "rbtree" ;case REGCACHE_MAPLE :
@@ -93,6 +95,8 @@ static const struct regmap_test_param regcache_types_list[] = { { .cache = REGCACHE_NONE, .fast_io = true }, { .cache = REGCACHE_FLAT }, { .cache = REGCACHE_FLAT, .fast_io = true }, + { .cache = REGCACHE_FLAT_S }, + { .cache = REGCACHE_FLAT_S, .fast_io = true }, { .cache = REGCACHE_RBTREE }, { .cache = REGCACHE_RBTREE, .fast_io = true }, { .cache = REGCACHE_MAPLE },
@@ -104,6 +108,8 @@ KUNIT_ARRAY_PARAM(regcache_types, regcache_types_list, param_to_desc);static const struct regmap_test_param real_cache_types_only_list[] = { { .cache =REGCACHE_FLAT }, { .cache =REGCACHE_FLAT , .fast_io = true }, + { .cache =REGCACHE_FLAT_S }, + { .cache =REGCACHE_FLAT_S , .fast_io = true }, { .cache =REGCACHE_RBTREE }, { .cache =REGCACHE_RBTREE , .fast_io = true }, { .cache =REGCACHE_MAPLE },
@@ -119,6 +125,12 @@ static const struct regmap_test_param real_cache_types_list[] = { { .cache = REGCACHE_FLAT, .from_reg = 0x2002 }, { .cache = REGCACHE_FLAT, .from_reg = 0x2003 }, { .cache = REGCACHE_FLAT, .from_reg = 0x2004 }, + { .cache = REGCACHE_FLAT_S, .from_reg = 0 }, + { .cache = REGCACHE_FLAT_S, .from_reg = 0, .fast_io = true }, + { .cache = REGCACHE_FLAT_S, .from_reg = 0x2001 }, + { .cache = REGCACHE_FLAT_S, .from_reg = 0x2002 }, + { .cache = REGCACHE_FLAT_S, .from_reg = 0x2003 }, + { .cache = REGCACHE_FLAT_S, .from_reg = 0x2004 }, { .cache = REGCACHE_RBTREE, .from_reg = 0 }, { .cache = REGCACHE_RBTREE, .from_reg = 0, .fast_io = true }, { .cache = REGCACHE_RBTREE, .from_reg = 0x2001 },
@@ -136,6 +148,12 @@ static const struct regmap_test_param real_cache_types_list[] = {KUNIT_ARRAY_PARAM (real_cache_types, real_cache_types_list, param_to_desc);static const struct regmap_test_param sparse_cache_types_list[] = { + { .cache =REGCACHE_FLAT_S , .from_reg =0 }, + { .cache =REGCACHE_FLAT_S , .from_reg =0 , .fast_io = true }, + { .cache =REGCACHE_FLAT_S , .from_reg =0x2001 }, + { .cache =REGCACHE_FLAT_S , .from_reg =0x2002 }, + { .cache =REGCACHE_FLAT_S , .from_reg =0x2003 }, + { .cache =REGCACHE_FLAT_S , .from_reg =0x2004 }, { .cache =REGCACHE_RBTREE , .from_reg =0 }, { .cache =REGCACHE_RBTREE , .from_reg =0 , .fast_io = true }, { .cache =REGCACHE_RBTREE , .from_reg =0x2001 },
@@ -1597,6 +1615,8 @@ static const struct regmap_test_param raw_types_list[] = { { .cache = REGCACHE_NONE, .val_endian = REGMAP_ENDIAN_BIG }, { .cache = REGCACHE_FLAT, .val_endian = REGMAP_ENDIAN_LITTLE }, { .cache = REGCACHE_FLAT, .val_endian = REGMAP_ENDIAN_BIG }, + { .cache = REGCACHE_FLAT_S, .val_endian = REGMAP_ENDIAN_LITTLE }, + { .cache = REGCACHE_FLAT_S, .val_endian = REGMAP_ENDIAN_BIG }, { .cache = REGCACHE_RBTREE, .val_endian = REGMAP_ENDIAN_LITTLE }, { .cache = REGCACHE_RBTREE, .val_endian = REGMAP_ENDIAN_BIG }, { .cache = REGCACHE_MAPLE, .val_endian = REGMAP_ENDIAN_LITTLE },
@@ -1608,6 +1628,8 @@ KUNIT_ARRAY_PARAM(raw_test_types, raw_types_list, param_to_desc);static const struct regmap_test_param raw_cache_types_list[] = { { .cache =REGCACHE_FLAT , .val_endian =REGMAP_ENDIAN_LITTLE }, { .cache =REGCACHE_FLAT , .val_endian =REGMAP_ENDIAN_BIG }, + { .cache =REGCACHE_FLAT_S , .val_endian =REGMAP_ENDIAN_LITTLE }, + { .cache =REGCACHE_FLAT_S , .val_endian =REGMAP_ENDIAN_BIG }, { .cache =REGCACHE_RBTREE , .val_endian =REGMAP_ENDIAN_LITTLE }, { .cache =REGCACHE_RBTREE , .val_endian =REGMAP_ENDIAN_BIG }, { .cache =REGCACHE_MAPLE , .val_endian =REGMAP_ENDIAN_LITTLE },
include/linux/regmap.h +1 -0
@@ -67,6 +67,7 @@ enum regcache_type {REGCACHE_RBTREE ,REGCACHE_FLAT ,REGCACHE_MAPLE , + REGCACHE_FLAT_S, };/**
Signature · 1 line
2.51.0
Mark Brown
maintainerRe: [PATCH v5 1/2] regmap: add flat cache with sparse validity
drivers/base/regmap/regcache-flat.c static inline unsigned int regcache_flat_get_index(const struct regmap *map, · line 49
Quoted 4 lines · Sander Vanheule
drivers/base/regmap/regcache-flat.c +1
-2- map->cache =kcalloc (regcache_flat_get_index (map, map->max_register) - +1 ,sizeof (unsigned int ), map->alloc_flags); + cache =kmalloc (cache_data_size, map->alloc_flags);
kcalloc() does a zero initialisation so should be replace by kzalloc() rather than kmalloc(), otherwise we break existing users who rely on the zero initialisation.
Re: [PATCH v5 1/2] regmap: add flat cache with sparse validity
Hi Mark,
Quoted 10 lines · Mark Brown
drivers/base/regmap/regcache-flat.c static inline unsigned int regcache_flat_get_index(const struct regmap *map, · line 49
Quoted 4 lines · Sander Vanheule
drivers/base/regmap/regcache-flat.c +1
-2- map->cache =kcalloc (regcache_flat_get_index (map, map->max_register) - +1 ,sizeof (unsigned int ), map->alloc_flags); + cache =kmalloc (cache_data_size, map->alloc_flags);
kcalloc()does a zero initialisation so should be replace bykzalloc()rather thankmalloc(), otherwise we break existing users who rely on the zero initialisation.
Of course. The intention is to maintain backward compatibility with the current flat cache, so I'll send a respin amending this.
Best,
Sander
PATCH v5 2/2 regmap: warn users about uninitialized flat cache
In reply to Sander VanheuleThe standard flat cache did not contain any validity info, so the cache was always considered to be entirely valid. Multiple mechanisms exist to initialize the cache on regmap init (defaults, raw defaults, HW init), but not all drivers are using one of these. As a result, their implementation might currently depend on the zero-initialized cache or contain other workarounds.
When reading an uninitialized value from the flat cache, warn the user, but maintain the current behavior. This will allow developers to switch to a sparse (flat) cache independently.
---
Changes since v4:
- Use one line for the log message
- Use dev_warn_once() with test_bit() to avoid log flooding with offset register spaces
Changes since v3:
- New patch: emit a warning for the flat cache on suspicious registers
--- drivers/base/regmap/regcache-flat.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-)
drivers/base/regmap/regcache-flat.c +6 -12
@@ -91,6 +91,11 @@ static int regcache_flat_read(struct regmap *map,struct regcache_flat_data *cache = map->cache;unsigned int index =regcache_flat_get_index (map, reg); +/* legacy behavior: ignore validity, but warn the user */ +if (unlikely (!test_bit (index, cache->valid))) +dev_warn_once (map->dev, +"using zero-initialized flat cache, this may cause unexpected behavior" ); + *value = cache->data[index];return 0 ;
@@ -116,17 +121,6 @@ static int regcache_flat_write(struct regmap *map, unsigned int reg,struct regcache_flat_data *cache = map->cache;unsigned int index =regcache_flat_get_index (map, reg); - cache->data[index] = value; - -return 0 ; -} - -static int regcache_flat_sparse_write (struct regmap *map,unsigned int reg, -unsigned int value) -{ -struct regcache_flat_data *cache = map->cache; -unsigned int index =regcache_flat_get_index (map, reg); - cache->data[index] = value;__set_bit (index, cache->valid);
@@ -160,6 +154,6 @@ struct regcache_ops regcache_flat_sparse_ops = { .init = regcache_flat_init, .exit = regcache_flat_exit, .read = regcache_flat_sparse_read, - .write = regcache_flat_sparse_write, + .write = regcache_flat_write, .drop = regcache_flat_drop, };
Signature · 1 line
2.51.0
Mark Brown
maintainerQuoted 11 lines · Sander Vanheule
The flat cache behaves differently from the other caches, in that it has no way of knowing if a cache entry is valid. Initialization has to happen either by providing defaults, by loading defaults from hardware (via
num_reg_defaults_raw), or by performing the required register writes. This difference in behavior may be unexpected to the user [1].To provide feature parity between the different cache types, a new variant of the flat cache is proposed. This allows user to migrate to the sparse flat cache (or a different cache type) when possible.
[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git for-next
Thanks!
[1/2] regmap: add flat cache with sparse validity
commit: 9c7f7262bc1affb9b9acd2ec2fb1f6314d5d474c
[2/2] regmap: warn users about uninitialized flat cache
commit: e062bdfdd6adbb2dee7751d054c1d8df63ddb8b8All 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