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

Andy Shevchenko
2026-02-27T05:44:03Z

On Thu, Feb 26, 2026 at 10:14:55PM +0000, Mark Brown wrote:
> 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...

Actually the check in the regmap_hw_init() should be done against count,
indeed.

> > + /*
> > + * 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.

Is it possible to allocate something, then count, then reallocate
in the existing caches and in the potential future ones?

I think it's the patter that we first count the size of the allocation,
do the allocation, and only then fill up the allocated area with data.

Otherwise we came back to the chicken-egg issue this patch tries to solve.

--
With Best Regards,
Andy Shevchenko