mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mark Brown <broonie@kernel.org>
To: Mark Brown <broonie@kernel.org>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] regcache: maple: Don't overflow the register number when inserting
Date: Mon, 26 Oct 2026 16:57:00 +0000	[thread overview]
Message-ID: <20261026-regmap-maple-bounds-v1-1-446f3eb8d406@kernel.org> (raw)

When inserting a previously uncached register into the maple cache we
check for adjacent blocks of registers above and below the register
number we are inserting and merge there.  Currently we check for reg - 1
and reg + 1 without worrying about if reg is 0 or UINT_MAX, meaning that
the register number will overflow and we could consider a block at the
other extremity of possible register numbers adjacent.  Avoid doing this
by clamping to 0 and UINT_MAX, if we do this then our search for an
adjacent block will fail as we will be looking for the register number
we are trying to insert.

The current behaviour means that in the event that we do attempt to
add 0 and merge it with a block at UINT_MAX kmalloc_array() rejects the
allocation for being absurdly large, this results in us failing the
write.  Such register maps are very unlikely due to the normal patterns
of register design and the fact that register numbers usually have less
bits than an int.

Signed-off-by: Mark Brown <broonie@kernel.org>
---
 drivers/base/regmap/regcache-maple.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/drivers/base/regmap/regcache-maple.c b/drivers/base/regmap/regcache-maple.c
index 16e488da5f23..9bc217b1e6f9 100644
--- a/drivers/base/regmap/regcache-maple.c
+++ b/drivers/base/regmap/regcache-maple.c
@@ -41,7 +41,7 @@ static int regcache_maple_write(struct regmap *map, unsigned int reg,
 	struct maple_tree *mt = map->cache;
 	MA_STATE(mas, mt, reg, reg);
 	unsigned long *entry, *upper, *lower;
-	unsigned long index, last;
+	unsigned long index, last, lower_reg, upper_reg;
 	size_t lower_sz, upper_sz;
 	int ret;
 
@@ -54,18 +54,33 @@ static int regcache_maple_write(struct regmap *map, unsigned int reg,
 		return 0;
 	}
 
-	/* Any adjacent entries to extend/merge? */
-	mas_set_range(&mas, reg - 1, reg + 1);
+	/* Avoid overflowing and wrapping */
+	if (reg != 0)
+		lower_reg = reg - 1;
+	else
+		lower_reg = reg;
+
+	if (reg != UINT_MAX)
+		upper_reg = reg + 1;
+	else
+		upper_reg = reg;
+
+	/*
+	 * Any adjacent entries to extend/merge?  If we are here then
+	 * reg won't exist so if we clamped lower_reg or upper_reg we
+	 * won't get anything from mas_find().
+	 */
+	mas_set_range(&mas, lower_reg, upper_reg);
 	index = reg;
 	last = reg;
 
-	lower = mas_find(&mas, reg - 1);
+	lower = mas_find(&mas, lower_reg);
 	if (lower) {
 		index = mas.index;
 		lower_sz = (mas.last - mas.index + 1) * sizeof(unsigned long);
 	}
 
-	upper = mas_find(&mas, reg + 1);
+	upper = mas_find(&mas, upper_reg);
 	if (upper) {
 		last = mas.last;
 		upper_sz = (mas.last - mas.index + 1) * sizeof(unsigned long);

---
base-commit: 2dee91ee94eec85d09c13c7ca83e3005906d3732
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git for-7.4
change-id: 20261026-regmap-maple-bounds-bc3075fb7a41

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


                 reply	other threads:[~2026-10-26 16:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261026-regmap-maple-bounds-v1-1-446f3eb8d406@kernel.org \
    --to=broonie@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®