Re: [PATCH] regcache: Sort the local copy of an unsorted reg_defaults array
From: Charles Keepax
Date: Wed Aug 05 2026 - 10:10:55 EST
On Wed, Aug 05, 2026 at 04:22:50PM +0300, Peter Ujfalusi wrote:
> regcache_lookup_reg() bsearch()es the reg_defaults array, which requires
> it to be sorted by ascending register address. Entries following a
> descending step are never found, so regcache_reg_needs_sync() reports
> that they need a sync and they are written to the device on every
> regcache_sync() even when they were never touched.
>
> Detect the misordering while reg_defaults is validated against the
> register stride and sort the local copy. The check needs no new loop
> and sort() only runs for the affected drivers, which are also warned
> about.
>
> Note that sort() is not stable, so for arrays with duplicated register
> addresses it remains unspecified which entry is found.
>
> Signed-off-by: Peter Ujfalusi <peter.ujfalusi@xxxxxxxxxxxxxxx>
> ---
> Hi,
>
> Fixes for affected in-tree codec drivers have been posted separately, so this
> is meant as a safety net for out-of-tree and future drivers rather than a
> replacement for fixing them, as discussed on the Cirrus codec patches.
>
> I'm not sure about the print level: dev_warn() makes the problem visible to
> whoever boots the affected machine, but the person who can act on it is the
> driver author. Should this be dev_dbg()?
Keep it at least as a warn, you need something annoying or no one
will take notice, it is quite likely people will apply debug on
their driver, much less likely they will apply it to the regmap
core. I still lean towards not sorting the defaults for people,
but not so strongly as to object if there is a rough concensus
forming around doing so.
Reviewed-by: Charles Keepax <ckeepax@xxxxxxxxxxxxxxxxxxxxx>
Tested-by: Charles Keepax <ckeepax@xxxxxxxxxxxxxxxxxxxxx>
Thanks,
Charles