Re: [PATCH v3 1/3] regcache: Move HW readback after cache initialisation

From: Mark Brown

Date: Thu Feb 26 2026 - 17:15:06 EST


On Thu, Feb 26, 2026 at 02:57:09PM +0100, Andy Shevchenko wrote:

> Make sure that cache is initialised before calling any IO
> using regmap, this makes sure that we won't access NULL or
> invalid pointers in the cache which hasn't been initialised.

> @@ -202,14 +210,6 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
> count = regcache_count_cacheable_registers(map);
> if (map->cache_bypass)
> return 0;

This is in the case where num_reg_defaults_raw != 0 (and we didn't have
any explicit defaults!), it's the only place where count gets set...

>
> + /*
> + * Some devices such as PMICs don't have cache defaults,
> + * we cope with this by reading back the HW registers and
> + * crafting the cache defaults by hand.
> + */
> + ret = regcache_hw_init(map, count);
> + if (ret)
> + goto err_exit;
> +

...and we now pass count off to regcache_hw_init() which will attempt to
allocate a zero length array and presumably faceplant if that happens.
I don't *think* we should ever hit that case (at least not for a
sensible regmap), but I'm having to think far too hard about the whole
thing to convince myself it's safe. I think we should keep the counting
of registers to allocate and the decision to call regcache_hw_init()
more joined up.

Attachment: signature.asc
Description: PGP signature