From: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
To: Mark Brown <broonie@kernel.org>
Cc: Charles Keepax <ckeepax@opensource.cirrus.com>,
Richard Fitzgerald <rf@opensource.cirrus.com>,
Pierre-Louis Bossart <pierre-louis.bossart@linux.dev>,
Peter Ujfalusi <peter.ujfalusi@linux.intel.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH] regcache: Sort the local copy of an unsorted reg_defaults array
Date: Wed, 5 Aug 2026 16:22:50 +0300 [thread overview]
Message-ID: <20260805132250.2637-1-peter.ujfalusi@linux.intel.com> (raw)
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@linux.intel.com>
---
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()?
Regards,
Peter
drivers/base/regmap/regcache.c | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/drivers/base/regmap/regcache.c b/drivers/base/regmap/regcache.c
index be167ee6f57c..aa7f6c30f232 100644
--- a/drivers/base/regmap/regcache.c
+++ b/drivers/base/regmap/regcache.c
@@ -123,6 +123,8 @@ static void regcache_hw_exit(struct regmap *map)
int regcache_init(struct regmap *map, const struct regmap_config *config)
{
+ bool sort_defaults = false;
+ unsigned int reg_prev = 0;
int count = 0;
int ret;
int i;
@@ -149,10 +151,16 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
return -EINVAL;
}
- for (i = 0; i < config->num_reg_defaults; i++)
+ for (i = 0; i < config->num_reg_defaults; i++) {
if (config->reg_defaults[i].reg % map->reg_stride)
return -EINVAL;
+ if (reg_prev > config->reg_defaults[i].reg)
+ sort_defaults = true;
+
+ reg_prev = config->reg_defaults[i].reg;
+ }
+
for (i = 0; i < ARRAY_SIZE(cache_types); i++)
if (cache_types[i]->type == map->cache_type)
break;
@@ -186,6 +194,13 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
sizeof(*map->reg_defaults), GFP_KERNEL);
if (!tmp_buf)
return -ENOMEM;
+
+ /* regcache_lookup_reg() bsearch()es this array */
+ if (sort_defaults) {
+ dev_warn(map->dev,
+ "Driver needs fixing: Unsorted reg_defaults, sorting the copy\n");
+ regcache_sort_defaults(tmp_buf, map->num_reg_defaults);
+ }
map->reg_defaults = tmp_buf;
} else if (map->num_reg_defaults_raw) {
count = regcache_count_cacheable_registers(map);
--
2.55.0
next reply other threads:[~2026-08-05 13:22 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-05 13:22 Peter Ujfalusi [this message]
2026-08-05 14:07 ` [PATCH] regcache: Sort the local copy of an unsorted reg_defaults array Charles Keepax
2026-08-05 14:34 ` Mark Brown
2026-08-06 12:01 ` Mark Brown
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260805132250.2637-1-peter.ujfalusi@linux.intel.com \
--to=peter.ujfalusi@linux.intel.com \
--cc=broonie@kernel.org \
--cc=ckeepax@opensource.cirrus.com \
--cc=linux-kernel@vger.kernel.org \
--cc=pierre-louis.bossart@linux.dev \
--cc=rf@opensource.cirrus.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox