All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vladimir Murzin <vladimir.murzin@arm.com>
To: Brad Hawthorne <brad@sim.vu>,
	linux@armlinux.org.uk, linux-arm-kernel@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/5] ARM: NOMMU: pmsa-v7: Do not probe the region order on M-class
Date: Wed, 25 Nov 2026 13:12:16 +0000	[thread overview]
Message-ID: <2f02f0b1-73b0-48ef-995a-b844bacefac2@arm.com> (raw)
In-Reply-To: <20261125025757.1159931-2-brad@sim.vu>

On 11/25/26 02:57, Brad Hawthorne wrote:
> __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

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):

diff --git a/arch/arm/mm/pmsa-v7.c b/arch/arm/mm/pmsa-v7.c
index 59d916ccdf25..b746a4e6f9fa 100644
--- a/arch/arm/mm/pmsa-v7.c
+++ b/arch/arm/mm/pmsa-v7.c
@@ -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


  reply	other threads:[~2026-11-25 13:12 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 13:12   ` Vladimir Murzin [this message]
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 13:16   ` Vladimir Murzin
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 13:18   ` Vladimir Murzin
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 14:16   ` Vladimir Murzin
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 13:20   ` Vladimir Murzin
2026-11-25  7:46 ` [PATCH 0/5] ARM: NOMMU: fix PMSAv7/v8 MPU setup on M-class Arnd Bergmann
2026-11-25 14:36 ` Vladimir Murzin

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=2f02f0b1-73b0-48ef-995a-b844bacefac2@arm.com \
    --to=vladimir.murzin@arm.com \
    --cc=brad@sim.vu \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.