* [PATCH 1/5] ARM: NOMMU: pmsa-v7: Do not probe the region order on M-class
2026-11-25 2:57 [PATCH 0/5] ARM: NOMMU: fix PMSAv7/v8 MPU setup on M-class Brad Hawthorne
@ 2026-11-25 2:57 ` Brad Hawthorne
2026-11-25 2:57 ` [PATCH 2/5] ARM: NOMMU: pmsa-v7: Do not allocate more RAM regions than the limit Brad Hawthorne
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Brad Hawthorne @ 2026-11-25 2:57 UTC (permalink / raw)
To: linux, linux-arm-kernel; +Cc: vladimir.murzin, linux-kernel
__mpu_min_region_order() finds the minimum region size by writing
0xFFFFFFFC to the region base register and reading it back. That
works on R-class, where the low bits are reserved. On M-class,
MPU_RBAR bits [4:0] are VALID and REGION, so the write selects region
12, which an MPU with fewer than 13 regions does not have. QEMU
(mps2-an385, 8 regions) logs "MPU region out of range 12/8", the
read-back is 0, every RAM region fails, and the kernel panics:
Kernel panic - not syncing: MPU region initialization failure! -12
Region 12 is outside the permitted values on hardware too. If the write
lands, the read-back returns the current region number in bits [3:0]
(MPU_RBAR, Cortex-M3 Devices Generic User Guide), so the probe returns
order 2 instead of 5. Nothing checks against that, so the bug can go
unseen there.
On M-class the minimum region size is fixed at 32 bytes, so return
that order without probing.
Tested under QEMU (mps2-an385): the kernel now sets up the MPU and
reaches 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: 9fcb01a9f54c ("ARM: 8711/1: V7M: Add support for MPU to M-class")
Cc: stable@vger.kernel.org
Signed-off-by: Brad Hawthorne <brad@sim.vu>
---
arch/arm/mm/pmsa-v7.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/arch/arm/mm/pmsa-v7.c b/arch/arm/mm/pmsa-v7.c
index 59d916ccdf25..a20a43867210 100644
--- a/arch/arm/mm/pmsa-v7.c
+++ b/arch/arm/mm/pmsa-v7.c
@@ -341,6 +341,10 @@ static int __init __mpu_min_region_order(void)
{
u32 drbar_result, irbar_result;
+ /* On M-class, writing MPU_RBAR selects a region; the minimum is 32 bytes */
+ if (IS_ENABLED(CONFIG_CPU_V7M))
+ return 5;
+
/* We've kept a region free for this probing */
rgnr_write(PMSAv7_PROBE_REGION);
isb();
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 2/5] ARM: NOMMU: pmsa-v7: Do not allocate more RAM regions than the limit
2026-11-25 2:57 [PATCH 0/5] ARM: NOMMU: fix PMSAv7/v8 MPU setup on M-class Brad Hawthorne
2026-11-25 2:57 ` [PATCH 1/5] ARM: NOMMU: pmsa-v7: Do not probe the region order " Brad Hawthorne
@ 2026-11-25 2:57 ` Brad Hawthorne
2026-11-25 2:57 ` [PATCH 3/5] ARM: NOMMU: pmsa-v7: Fix off-by-one in mpu_setup_region() Brad Hawthorne
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Brad Hawthorne @ 2026-11-25 2:57 UTC (permalink / raw)
To: linux, linux-arm-kernel; +Cc: vladimir.murzin, linux-kernel
allocate_region() checks "count > limit" only after it has filled
regions[count], so it can return one region more than the limit. The
caller passes the number of MPU regions left for RAM, so the setup
then programs a region the MPU does not have. Under QEMU (mps2-an385,
8 regions) with a memory node of 0x21000000 + 0x9f9000, which needs
more regions than are left, QEMU logs "MPU region out of range 8/8"
and the kernel reports:
Using ARMv7 PMSA Compliant MPU. Region independence: No, Used 9 of 8 regions
Stop allocating once the limit is reached, so the memory that does not
fit is truncated, as intended.
Tested under QEMU (mps2-an385): the same case now truncates memory and
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@vger.kernel.org
Signed-off-by: Brad Hawthorne <brad@sim.vu>
---
arch/arm/mm/pmsa-v7.c | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/arch/arm/mm/pmsa-v7.c b/arch/arm/mm/pmsa-v7.c
index a20a43867210..f2ad0f3ff480 100644
--- a/arch/arm/mm/pmsa-v7.c
+++ b/arch/arm/mm/pmsa-v7.c
@@ -193,7 +193,7 @@ static int __init allocate_region(phys_addr_t base, phys_addr_t size,
phys_addr_t diff = size;
int attempts = MPU_MAX_REGIONS;
- while (diff) {
+ while (diff && count < limit) {
/* Try cover region as is (maybe with help of subregions) */
if (try_split_region(base, size, ®ions[count])) {
count++;
@@ -215,9 +215,6 @@ static int __init allocate_region(phys_addr_t base, phys_addr_t size,
size = asize < p2size ? asize + 1 : p2size + 1;
}
- if (count > limit)
- break;
-
if (!attempts)
break;
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 3/5] ARM: NOMMU: pmsa-v7: Fix off-by-one in mpu_setup_region()
2026-11-25 2:57 [PATCH 0/5] ARM: NOMMU: fix PMSAv7/v8 MPU setup on M-class Brad Hawthorne
2026-11-25 2:57 ` [PATCH 1/5] ARM: NOMMU: pmsa-v7: Do not probe the region order " Brad Hawthorne
2026-11-25 2:57 ` [PATCH 2/5] ARM: NOMMU: pmsa-v7: Do not allocate more RAM regions than the limit Brad Hawthorne
@ 2026-11-25 2:57 ` Brad Hawthorne
2026-11-25 2:57 ` [PATCH 4/5] ARM: NOMMU: pmsa-v7: Align a split region's base to its size Brad Hawthorne
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Brad Hawthorne @ 2026-11-25 2:57 UTC (permalink / raw)
To: linux, linux-arm-kernel; +Cc: vladimir.murzin, linux-kernel
mpu_setup_region() rejects "number > mpu_max_regions", which lets
number == mpu_max_regions through. Regions are numbered from 0, so
that number names a region the MPU does not have. Reject it too.
Now that allocate_region() respects its limit, no caller passes that
number. Tested under QEMU (mps2-an385): no change in behaviour.
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: 5ad7dcbe40ae ("ARM: mpu: add MPU probe and initialisation functions in C")
Signed-off-by: Brad Hawthorne <brad@sim.vu>
---
arch/arm/mm/pmsa-v7.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/arm/mm/pmsa-v7.c b/arch/arm/mm/pmsa-v7.c
index f2ad0f3ff480..af0c71135a6e 100644
--- a/arch/arm/mm/pmsa-v7.c
+++ b/arch/arm/mm/pmsa-v7.c
@@ -371,7 +371,7 @@ static int __init mpu_setup_region(unsigned int number, phys_addr_t start,
u32 size_data;
/* We kept a region free for probing resolution of MPU regions*/
- if (number > mpu_max_regions
+ if (number >= mpu_max_regions
|| number >= MPU_MAX_REGIONS)
return -ENOENT;
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 4/5] ARM: NOMMU: pmsa-v7: Align a split region's base to its size
2026-11-25 2:57 [PATCH 0/5] ARM: NOMMU: fix PMSAv7/v8 MPU setup on M-class Brad Hawthorne
` (2 preceding siblings ...)
2026-11-25 2:57 ` [PATCH 3/5] ARM: NOMMU: pmsa-v7: Fix off-by-one in mpu_setup_region() Brad Hawthorne
@ 2026-11-25 2:57 ` Brad Hawthorne
2026-11-25 2:57 ` [PATCH 5/5] ARM: NOMMU: pmsa-v8: Fix off-by-one in __pmsav8_setup_region() Brad Hawthorne
2026-11-25 7:46 ` [PATCH 0/5] ARM: NOMMU: fix PMSAv7/v8 MPU setup on M-class Arnd Bergmann
5 siblings, 0 replies; 7+ messages in thread
From: Brad Hawthorne @ 2026-11-25 2:57 UTC (permalink / raw)
To: linux, linux-arm-kernel; +Cc: vladimir.murzin, linux-kernel
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@vger.kernel.org
Signed-off-by: Brad Hawthorne <brad@sim.vu>
---
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
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 5/5] ARM: NOMMU: pmsa-v8: Fix off-by-one in __pmsav8_setup_region()
2026-11-25 2:57 [PATCH 0/5] ARM: NOMMU: fix PMSAv7/v8 MPU setup on M-class Brad Hawthorne
` (3 preceding siblings ...)
2026-11-25 2:57 ` [PATCH 4/5] ARM: NOMMU: pmsa-v7: Align a split region's base to its size Brad Hawthorne
@ 2026-11-25 2:57 ` Brad Hawthorne
2026-11-25 7:46 ` [PATCH 0/5] ARM: NOMMU: fix PMSAv7/v8 MPU setup on M-class Arnd Bergmann
5 siblings, 0 replies; 7+ messages in thread
From: Brad Hawthorne @ 2026-11-25 2:57 UTC (permalink / raw)
To: linux, linux-arm-kernel; +Cc: vladimir.murzin, linux-kernel
__pmsav8_setup_region() rejects "number > mpu_max_regions", which lets
number == mpu_max_regions through. Regions are numbered from 0, so
that number names a region the MPU does not have. Reject it.
A Cortex-M33 can be built with 4 regions. Under QEMU (mps2-an505) with
a 4-region MPU, QEMU logs "MPU region out of range 4/4" and the kernel
takes an unhandled exception during the setup. With this patch the
setup warns "MPU region initialization failure! -2" and the kernel
faults later, in memblock_alloc_try_nid(), because RAM that region
would have mapped has none. On an RP2350 board (Cortex-M33, 8 regions)
the boot is unchanged.
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,
the board test and this changelog, and ran them; I reviewed the
reasoning and the test results.
Fixes: 046835b4aa22 ("ARM: 8757/1: NOMMU: Support PMSAv8 MPU")
Signed-off-by: Brad Hawthorne <brad@sim.vu>
---
arch/arm/mm/pmsa-v8.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/arm/mm/pmsa-v8.c b/arch/arm/mm/pmsa-v8.c
index 28cdc5468406..e5353a030731 100644
--- a/arch/arm/mm/pmsa-v8.c
+++ b/arch/arm/mm/pmsa-v8.c
@@ -140,7 +140,7 @@ static int __init __mpu_max_regions(void)
static int __init __pmsav8_setup_region(unsigned int number, u32 bar, u32 lar)
{
- if (number > mpu_max_regions
+ if (number >= mpu_max_regions
|| number >= MPU_MAX_REGIONS)
return -ENOENT;
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH 0/5] ARM: NOMMU: fix PMSAv7/v8 MPU setup on M-class
2026-11-25 2:57 [PATCH 0/5] ARM: NOMMU: fix PMSAv7/v8 MPU setup on M-class Brad Hawthorne
` (4 preceding siblings ...)
2026-11-25 2:57 ` [PATCH 5/5] ARM: NOMMU: pmsa-v8: Fix off-by-one in __pmsav8_setup_region() Brad Hawthorne
@ 2026-11-25 7:46 ` Arnd Bergmann
5 siblings, 0 replies; 7+ messages in thread
From: Arnd Bergmann @ 2026-11-25 7:46 UTC (permalink / raw)
To: Brad Hawthorne, Russell King, linux-arm-kernel
Cc: Vladimir Murzin, linux-kernel
On Wed, Nov 25, 2026, at 03:57, Brad Hawthorne wrote:
> With CONFIG_ARM_MPU=y, a Cortex-M3 kernel under QEMU (mps2-an385)
> panics before its console comes up. Patch 1 fixes that: the M-class
> region base register has VALID and REGION in its low bits, so the
> R-class probe selects a region that does not exist. After that,
> allocate_region() can return one region too many (2),
> mpu_setup_region() accepts a number one past the end (3), and
> try_split_region() can return a base not aligned to its size (4).
> Patch 5 fixes the same off-by-one as patch 3 in pmsa-v8.c.
I had a look at these and they all make sense to me, thanks for
addressing these issues!
Arnd
^ permalink raw reply [flat|nested] 7+ messages in thread