The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] regcache: Use a consistent sort for defaults table
@ 2026-08-05 17:51 Mark Brown
  2026-08-06  8:23 ` Péter Ujfalusi
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Mark Brown @ 2026-08-05 17:51 UTC (permalink / raw)
  To: Peter Ujfalusi
  Cc: Charles Keepax, Richard Fitzgerald, Pierre-Louis Bossart,
	linux-kernel, Mark Brown

When we look up registers in the defaults table we use a binary search,
and we have a regcache_sort_defaults() API to help drivers that constuct
their defaults tables on the fly.  Unfortunately the lookup and the sort
don't use the same comparison function, and to make matters worse the
comparison function used during lookups is written for signed register
numbers rather than the unsigned ones we actually have so can produce
suprising results when some of the addresses have the top bit set.

Standardise on the more explicitly coded function to ensure consistent
results.

Signed-off-by: Mark Brown <broonie@kernel.org>
---
 drivers/base/regmap/regcache.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

diff --git a/drivers/base/regmap/regcache.c b/drivers/base/regmap/regcache.c
index aa8f2efed779..480bc76f9a02 100644
--- a/drivers/base/regmap/regcache.c
+++ b/drivers/base/regmap/regcache.c
@@ -727,14 +727,6 @@ unsigned int regcache_get_val(struct regmap *map, const void *base,
 	return -1;
 }
 
-static int regcache_default_cmp(const void *a, const void *b)
-{
-	const struct reg_default *_a = a;
-	const struct reg_default *_b = b;
-
-	return _a->reg - _b->reg;
-}
-
 int regcache_lookup_reg(struct regmap *map, unsigned int reg)
 {
 	struct reg_default key;
@@ -744,7 +736,7 @@ int regcache_lookup_reg(struct regmap *map, unsigned int reg)
 	key.def = 0;
 
 	r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
-		    sizeof(struct reg_default), regcache_default_cmp);
+		    sizeof(struct reg_default), regcache_defaults_cmp);
 
 	if (r)
 		return r - map->reg_defaults;

---
base-commit: 075b74841bd0065a3bda3440873c747938e69b68
change-id: 20260805-regmap-regcache-sort-8d144a68bf8d

Best regards,
--  
Mark Brown <broonie@kernel.org>


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] regcache: Use a consistent sort for defaults table
  2026-08-05 17:51 [PATCH] regcache: Use a consistent sort for defaults table Mark Brown
@ 2026-08-06  8:23 ` Péter Ujfalusi
  2026-08-06  8:46 ` Charles Keepax
  2026-08-06 12:00 ` Mark Brown
  2 siblings, 0 replies; 4+ messages in thread
From: Péter Ujfalusi @ 2026-08-06  8:23 UTC (permalink / raw)
  To: Mark Brown
  Cc: Charles Keepax, Richard Fitzgerald, Pierre-Louis Bossart,
	linux-kernel



On 05/08/2026 20:51, Mark Brown wrote:
> When we look up registers in the defaults table we use a binary search,
> and we have a regcache_sort_defaults() API to help drivers that constuct
> their defaults tables on the fly.  Unfortunately the lookup and the sort
> don't use the same comparison function, and to make matters worse the
> comparison function used during lookups is written for signed register
> numbers rather than the unsigned ones we actually have so can produce
> suprising results when some of the addresses have the top bit set.
> 
> Standardise on the more explicitly coded function to ensure consistent
> results.

Reviewed-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>

> 
> Signed-off-by: Mark Brown <broonie@kernel.org>
> ---
>  drivers/base/regmap/regcache.c | 10 +---------
>  1 file changed, 1 insertion(+), 9 deletions(-)
> 
> diff --git a/drivers/base/regmap/regcache.c b/drivers/base/regmap/regcache.c
> index aa8f2efed779..480bc76f9a02 100644
> --- a/drivers/base/regmap/regcache.c
> +++ b/drivers/base/regmap/regcache.c
> @@ -727,14 +727,6 @@ unsigned int regcache_get_val(struct regmap *map, const void *base,
>  	return -1;
>  }
>  
> -static int regcache_default_cmp(const void *a, const void *b)
> -{
> -	const struct reg_default *_a = a;
> -	const struct reg_default *_b = b;
> -
> -	return _a->reg - _b->reg;
> -}
> -
>  int regcache_lookup_reg(struct regmap *map, unsigned int reg)
>  {
>  	struct reg_default key;
> @@ -744,7 +736,7 @@ int regcache_lookup_reg(struct regmap *map, unsigned int reg)
>  	key.def = 0;
>  
>  	r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
> -		    sizeof(struct reg_default), regcache_default_cmp);
> +		    sizeof(struct reg_default), regcache_defaults_cmp);
>  
>  	if (r)
>  		return r - map->reg_defaults;
> 
> ---
> base-commit: 075b74841bd0065a3bda3440873c747938e69b68
> change-id: 20260805-regmap-regcache-sort-8d144a68bf8d
> 
> Best regards,
> --  
> Mark Brown <broonie@kernel.org>
> 

-- 
Péter


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] regcache: Use a consistent sort for defaults table
  2026-08-05 17:51 [PATCH] regcache: Use a consistent sort for defaults table Mark Brown
  2026-08-06  8:23 ` Péter Ujfalusi
@ 2026-08-06  8:46 ` Charles Keepax
  2026-08-06 12:00 ` Mark Brown
  2 siblings, 0 replies; 4+ messages in thread
From: Charles Keepax @ 2026-08-06  8:46 UTC (permalink / raw)
  To: Mark Brown
  Cc: Peter Ujfalusi, Richard Fitzgerald, Pierre-Louis Bossart,
	linux-kernel

On Wed, Aug 05, 2026 at 06:51:00PM +0100, Mark Brown wrote:
> When we look up registers in the defaults table we use a binary search,
> and we have a regcache_sort_defaults() API to help drivers that constuct
> their defaults tables on the fly.  Unfortunately the lookup and the sort
> don't use the same comparison function, and to make matters worse the
> comparison function used during lookups is written for signed register
> numbers rather than the unsigned ones we actually have so can produce
> suprising results when some of the addresses have the top bit set.
> 
> Standardise on the more explicitly coded function to ensure consistent
> results.
> 
> Signed-off-by: Mark Brown <broonie@kernel.org>
> ---

Reviewed-by: Charles Keepax <ckeepax@opensource.cirrus.com>
Tested-by: Charles Keepax <ckeepax@opensource.cirrus.com>

Thanks,
Charles

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] regcache: Use a consistent sort for defaults table
  2026-08-05 17:51 [PATCH] regcache: Use a consistent sort for defaults table Mark Brown
  2026-08-06  8:23 ` Péter Ujfalusi
  2026-08-06  8:46 ` Charles Keepax
@ 2026-08-06 12:00 ` Mark Brown
  2 siblings, 0 replies; 4+ messages in thread
From: Mark Brown @ 2026-08-06 12:00 UTC (permalink / raw)
  To: Peter Ujfalusi, Mark Brown
  Cc: Charles Keepax, Richard Fitzgerald, Pierre-Louis Bossart,
	linux-kernel

On Wed, 05 Aug 2026 18:51:00 +0100, Mark Brown wrote:
> regcache: Use a consistent sort for defaults table

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git for-7.2

Thanks!

[1/1] regcache: Use a consistent sort for defaults table
      https://git.kernel.org/broonie/regmap/c/9ed3d974a266

All 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-08-06 13:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-05 17:51 [PATCH] regcache: Use a consistent sort for defaults table Mark Brown
2026-08-06  8:23 ` Péter Ujfalusi
2026-08-06  8:46 ` Charles Keepax
2026-08-06 12:00 ` Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox