Skip to contentSkip to conversation

ARM: NOMMU: fix PMSAv7/v8 MPU setup on M-class

PATCH In review Brad Hawthorne 3 participants 13 messages Nov 25 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

cover ARM: NOMMU: fix PMSAv7/v8 MPU setup on M-class

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.

Patches 2 to 4 also run on R-class.

I know the Cortex-M platforms are going in 7.5. These are for the stable kernels that still carry them, and for the v8-M core while its future is discussed.

Not fixed here: on M-class, pmsav7_setup() rewrites regions with the MPU on, so for a moment a region has its new base and old size, and QEMU faults when it covers the running code (an385, 0x3f9000 of RAM). Turning the MPU off around the rewrite works only where the default memory map matches the kernel's. Vladimir, how would you want this handled?

Testing: I have no PMSAv7 hardware, so patches 1 to 4 are tested under QEMU only (mps2-an385, several memory layouts; every prefix builds with W=1). Patch 5 is tested under QEMU (mps2-an505) and on an RP2350 board (Cortex-M33). A test on any Cortex-M3/M4/M7 or Cortex-R board with CONFIG_ARM_MPU=y would help; the line to check is "Using ARMv7 PMSA Compliant MPU ... Used N of M regions", with N no larger than M.

This series was written with an AI coding assistant (Claude Opus 5.5, Anthropic), as each patch says; I reviewed the reasoning and the test results.

Brad Hawthorne (5):
ARM: NOMMU: pmsa-v7: Do not probe the region order on M-class
ARM: NOMMU: pmsa-v7: Do not allocate more RAM regions than the limit
ARM: NOMMU: pmsa-v7: Fix off-by-one in mpu_setup_region()
ARM: NOMMU: pmsa-v7: Align a split region's base to its size
ARM: NOMMU: pmsa-v8: Fix off-by-one in __pmsav8_setup_region()

 arch/arm/mm/pmsa-v7.c | 29 ++++++++++++++++++-----------
 arch/arm/mm/pmsa-v8.c |  2 +-
 2 files changed, 19 insertions(+), 12 deletions(-)

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/5 ARM: NOMMU: pmsa-v7: Do not probe the region order on M-class

__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")
---
 arch/arm/mm/pmsa-v7.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm/mm/pmsa-v7.c +4 -0
index 59d916ccdf25..a20a43867210 100644
@@ -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();
Signature · 1 line
2.43.0
#3 lore raw

PATCH 2/5 ARM: NOMMU: pmsa-v7: Do not allocate more RAM regions than the limit

In reply to Brad Hawthorne

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")
---
 arch/arm/mm/pmsa-v7.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
arch/arm/mm/pmsa-v7.c +1 -4
index a20a43867210..f2ad0f3ff480 100644
@@ -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, &regions[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;
 
Signature · 1 line
2.43.0
#5 lore raw

PATCH 3/5 ARM: NOMMU: pmsa-v7: Fix off-by-one in mpu_setup_region()

In reply to Brad Hawthorne

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")
---
 arch/arm/mm/pmsa-v7.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/mm/pmsa-v7.c +1 -1
index f2ad0f3ff480..af0c71135a6e 100644
@@ -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;
 
Signature · 1 line
2.43.0
#7 lore raw

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

In reply to Brad Hawthorne

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")
---
 arch/arm/mm/pmsa-v7.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
arch/arm/mm/pmsa-v7.c +12 -6
index af0c71135a6e..bd776ea90934 100644
@@ -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;
Signature · 1 line
2.43.0
#9 lore raw

PATCH 5/5 ARM: NOMMU: pmsa-v8: Fix off-by-one in __pmsav8_setup_region()

In reply to Brad Hawthorne

__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")
---
 arch/arm/mm/pmsa-v8.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/mm/pmsa-v8.c +1 -1
index 28cdc5468406..e5353a030731 100644
@@ -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;
 
Signature · 1 line
2.43.0
#11 lore raw
In reply to Brad Hawthorne
Quoted 8 lines · Brad Hawthorne

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

#2 lore raw

Re: [PATCH 1/5] ARM: NOMMU: pmsa-v7: Do not probe the region order on M-class

In reply to Brad Hawthorne
Quoted 40 lines · Brad Hawthorne

__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")
---
 arch/arm/mm/pmsa-v7.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm/mm/pmsa-v7.c +0 -0
index 59d916ccdf25..a20a43867210 100644
@@ -341,6 +341,10 @@ static int __init __mpu_min_region_order(void)
 {

arch/arm/mm/pmsa-v7.c static int __init __mpu_min_region_order(void)

Quoted 10 lines
arch/arm/mm/pmsa-v7.c +4 -1
 	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

Well, minimum is not the same as implemented. I still prefer architected discovery:

| Software can find the minimum size of region supported by an
| MPU region by writing all ones to MPU_RBAR[31:5] for that
| region, and then reading the register to find the value saved
| to bits[31:5]. The number of trailing zeros in this bit field
| indicates the minimum supported alignment and therefore the
| supported region size.

Which can be done with something like below (untested):

arch/arm/mm/pmsa-v7.c +4 -3
index 59d916ccdf25..b746a4e6f9fa 100644
@@ -340,20 +340,21 @@ static int __init mpu_iside_independent(void)
 static int __init __mpu_min_region_order(void)
 {
        u32 drbar_result, irbar_result;
+       u32 mask = IS_ENABLED(CONFIG_CPU_V7M) ? GENMASK(31, 5) : GENMASK(31, 2);
 
        /* We've kept a region free for this probing */
        rgnr_write(PMSAv7_PROBE_REGION);
        isb();
        /*
-        * As per ARM ARM, write 0xFFFFFFFC to DRBAR to find the minimum
+        * As per ARM ARM, write 'mask' to DRBAR to find the minimum
         * region order
        */
-       drbar_write(0xFFFFFFFC);
+       drbar_write(mask);
        drbar_result = irbar_result = drbar_read();
        drbar_write(0x0);
        /* If the MPU is non-unified, we use the larger of the two minima*/
        if (mpu_iside_independent()) {
-               irbar_write(0xFFFFFFFC);
+               irbar_write(mask);
                irbar_result = irbar_read();
                irbar_write(0x0);
        }

Cheers
Vladimir

Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] ARM: NOMMU: pmsa-v7: Do not allocate more RAM regions than the limit

In reply to Brad Hawthorne
Quoted 43 lines · Brad Hawthorne

deviation from their usual behavior, this may indicate that their account has been compromised. Be extra cautious before opening links or attachments. * 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")
---
 arch/arm/mm/pmsa-v7.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
arch/arm/mm/pmsa-v7.c +1 -1
index a20a43867210..f2ad0f3ff480 100644
@@ -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, &regions[count])) {
 			count++;

arch/arm/mm/pmsa-v7.c static int __init allocate_region(phys_addr_t base, phys_addr_t size,

Quoted 10 lines
arch/arm/mm/pmsa-v7.c +0 -4
@@ -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
Reviewed-by on v1 3/5
#6 lore raw

Re: [PATCH 3/5] ARM: NOMMU: pmsa-v7: Fix off-by-one in mpu_setup_region()

In reply to Brad Hawthorne
Quoted 23 lines · Brad Hawthorne

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")
---
 arch/arm/mm/pmsa-v7.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/mm/pmsa-v7.c +0 -0
index f2ad0f3ff480..af0c71135a6e 100644

arch/arm/mm/pmsa-v7.c static int __init mpu_setup_region(unsigned int number, phys_addr_t start,

Quoted 9 lines
arch/arm/mm/pmsa-v7.c +1 -2
@@ -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
Reviewed-by on v1 5/5
#10 lore raw

Re: [PATCH 5/5] ARM: NOMMU: pmsa-v8: Fix off-by-one in __pmsav8_setup_region()

In reply to Brad Hawthorne
Quoted 28 lines · Brad Hawthorne

__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")
---
 arch/arm/mm/pmsa-v8.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/mm/pmsa-v8.c +0 -0
index 28cdc5468406..e5353a030731 100644

arch/arm/mm/pmsa-v8.c static int __init __mpu_max_regions(void)

Quoted 9 lines
arch/arm/mm/pmsa-v8.c +1 -2
@@ -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
Reviewed-by on v1 4/5
#8 lore raw

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

In reply to Brad Hawthorne
Quoted 49 lines · Brad Hawthorne

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")
---
 arch/arm/mm/pmsa-v7.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
arch/arm/mm/pmsa-v7.c +7 -6
index af0c71135a6e..bd776ea90934 100644
@@ -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;

arch/arm/mm/pmsa-v7.c static inline unsigned long irbar_read(void) {return 0;}

Quoted 10 lines
arch/arm/mm/pmsa-v7.c +5 -1
+		if (asize <= p2size)
+			break;
 		p2size *= 2;
+		if (!p2size)
+			return false;	/* would need a 4GB region */
+	}
 
 	bdiff = base - abase;
 	sdiff = p2size - asize;
-- 2.43.0
#12 lore raw
In reply to Brad Hawthorne
Quoted 6 lines · Brad Hawthorne

Not fixed here: on M-class, pmsav7_setup() rewrites regions with the MPU on, so for a moment a region has its new base and old size, and QEMU faults when it covers the running code (an385, 0x3f9000 of RAM). Turning the MPU off around the rewrite works only where the default memory map matches the kernel's. Vladimir, how would you want this handled?

I'm afraid I need more details on what exactly fails :(

Cheers
Vladimir