[PATCH 4/5] ARM: NOMMU: pmsa-v7: Align a split region's base to its size

From: Brad Hawthorne

Date: Tue Nov 24 2026 - 21:59:00 EST


try_split_region() masks the base with size - 1, which aligns it only
when size is a power of two, then rounds the region size up without
aligning the base again. An MPU region's base must be aligned to its
size. For memory at 0x21400000 + 0xa00000, the kernel asks for a 16MB
region at 0x21400000. QEMU ignores such a region. Hardware that
ignores the low bits would map 0x21000000-0x219fffff and leave
0x21a00000-0x21dfffff, which the kernel uses, unmapped.

Start from the size rounded down to a power of two, align the base to
it, and double it until the aligned region covers the request.

Tested under QEMU (mps2-an385) with that memory layout: QEMU no longer
reports a misaligned region, and the kernel boots to user space. Not
tested on PMSAv7 hardware.

This patch was written with an AI coding assistant (Claude Opus 5.5,
Anthropic): it found the bug in review, wrote the fix, the QEMU tests
and this changelog, and ran them; I reviewed the reasoning and the test
results.

Fixes: 5c9d9a1b3a54 ("ARM: 8712/1: NOMMU: Use more MPU regions to cover memory")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Brad Hawthorne <brad@xxxxxx>
---
arch/arm/mm/pmsa-v7.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/arch/arm/mm/pmsa-v7.c b/arch/arm/mm/pmsa-v7.c
index af0c71135a6e..bd776ea90934 100644
--- a/arch/arm/mm/pmsa-v7.c
+++ b/arch/arm/mm/pmsa-v7.c
@@ -146,13 +146,19 @@ static inline unsigned long irbar_read(void) {return 0;}
static bool __init try_split_region(phys_addr_t base, phys_addr_t size, struct region *region)
{
unsigned long subreg, bslots, sslots;
- phys_addr_t abase = base & ~(size - 1);
- phys_addr_t asize = base + size - abase;
- phys_addr_t p2size = 1 << __fls(asize);
- phys_addr_t bdiff, sdiff;
-
- if (p2size != asize)
+ phys_addr_t p2size = 1 << __fls(size);
+ phys_addr_t abase, asize, bdiff, sdiff;
+
+ /* The base must be aligned to the region size */
+ for (;;) {
+ abase = base & ~(p2size - 1);
+ asize = base + size - abase;
+ if (asize <= p2size)
+ break;
p2size *= 2;
+ if (!p2size)
+ return false; /* would need a 4GB region */
+ }

bdiff = base - abase;
sdiff = p2size - asize;
--
2.43.0