Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] riscv: mm: make pud_leaf() only care about the leaf bits
@ 2026-11-24  8:05 Jingyi Sun
  2026-11-25  8:06 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Jingyi Sun @ 2026-11-24  8:05 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: Alexandre Ghiti, linux-riscv, linux-kernel, David Hildenbrand,
	linux-mm, Jingyi Sun, stable

Since commit c3cc2a4a3a23 ("riscv: Add support for PUD THP"), riscv can
map huge PUDs, e.g. 1G pfnmaps of VFIO BARs. mprotect(PROT_NONE) on such
a mapping goes through change_huge_pud(), which writes back
pud_modify(pud, PAGE_NONE). PAGE_NONE does not have _PAGE_PRESENT, and
pud_leaf() requires pud_present(), so afterwards pud_leaf() and
pud_trans_huge() return false for an entry that still maps 1G.

Core mm then treats the leaf entry as a pointer to a PMD table:

- munmap(): zap_pud_range() skips zap_huge_pud(), and
  pud_none_or_clear_bad() reports "bad pud 00000001000001e2." and clears
  the entry.

- mprotect() back to PROT_READ|PROT_WRITE: change_pud_range() calls
  change_pmd_range(), which uses the physical address of the 1G mapping
  as a PMD table. For a BAR that address is not in the linear map, so
  the kernel takes a paging fault at 0xff60000380000000 (the linear map
  alias of the BAR) and oopses:

    epc : change_protection+0x498/0xf74
     ra : change_protection+0x2e4/0xf74
    [<ffffffff80263f4e>] change_protection+0x498/0xf74
    [<ffffffff80264c18>] mprotect_fixup+0x1ee/0x32c
    [<ffffffff80264f96>] __riscv_sys_mprotect+0x240/0x344

pmd_present() already treats _PAGE_PROT_NONE as present, so PMD-sized
mappings are fine. x86 had the same problem with PUDs and fixed it in
commit 144bb0aee33a ("mm/x86: make pud_leaf() only care about PSE bit"):
PROT_NONE entries must still be reported as leaves. Do the same for
riscv and only check _PAGE_LEAF in pud_leaf(). pud_present() is left
alone, so pud_access_permitted() and GUP-fast still reject PROT_NONE
PUDs.

Reproduced in QEMU (riscv64 virt, Sv57) on v7.3-rc2 and on mainline as
of 2026-11-19 (v7.4-rc3+), with an ivshmem-plain device (2G BAR) bound
to vfio-pci in no-IOMMU mode. The reproducer mmaps the first 1G of BAR2
at a 1G-aligned address and touches it; vfio-pci dynamic debug shows
vfio_pci_mmap_huge_fault() with order 18 returning VM_FAULT_NOPAGE.
Then:

- mprotect(PROT_NONE) + munmap(): "bad pud" in 3/3 runs, 0/3 with this
  patch;
- mprotect(PROT_NONE) + mprotect(PROT_READ|PROT_WRITE) + access: oops in
  3/3 runs, 0/3 with this patch (the data is read back and written
  normally);
- mprotect(PROT_READ) + munmap(): no "bad pud" (control).

x86_64 with the same reproducer shows neither problem.

The issue was found with a research static analyzer for
cross-architecture inconsistencies and confirmed with the QEMU
reproducer described above.

Fixes: c3cc2a4a3a23 ("riscv: Add support for PUD THP")
Cc: stable@vger.kernel.org
Signed-off-by: Jingyi Sun <1045704958@qq.com>
---
Notes:

- The tested mainline kernels (2026-11-19) had DEBUG_VM,
  DEBUG_VM_PGTABLE and PAGE_TABLE_CHECK(_ENFORCED) enabled. With this
  patch the debug_vm_pgtable self-test and page_table_check stay quiet.

- With this patch, the PROT_NONE -> PROT_READ|PROT_WRITE case reaches
  change_huge_pud() -> pudp_invalidate() on a PROT_NONE PUD, and
  VM_WARN_ON_ONCE(!pud_present(*pudp)) fires once:

    WARNING: arch/riscv/mm/pgtable.c:160 at pudp_invalidate+0x42/0x64

  x86_64 (same mainline, DEBUG_VM) shows the same warning with the same
  reproducer:

    WARNING: arch/x86/mm/pgtable.c:527 at pudp_invalidate+0xb6/0x100

  so this looks like a separate issue: pmdp_invalidate() has the same
  check, but pmd_present() is true for PROT_NONE on both architectures,
  while pud_present() is not. I left it alone here; I am not sure
  whether the check or pud_present() should change.

- The reproducer (a static /init for QEMU) is available on request.

 arch/riscv/include/asm/pgtable-64.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/riscv/include/asm/pgtable-64.h b/arch/riscv/include/asm/pgtable-64.h
index eb7708e35cf9..1f45ebce8a75 100644
--- a/arch/riscv/include/asm/pgtable-64.h
+++ b/arch/riscv/include/asm/pgtable-64.h
@@ -226,7 +226,7 @@ static inline int pud_bad(pud_t pud)
 #define pud_leaf	pud_leaf
 static inline bool pud_leaf(pud_t pud)
 {
-	return pud_present(pud) && (pud_val(pud) & _PAGE_LEAF);
+	return pud_val(pud) & _PAGE_LEAF;
 }
 
 static inline int pud_user(pud_t pud)

base-commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] riscv: mm: make pud_leaf() only care about the leaf bits
  2026-11-24  8:05 [PATCH] riscv: mm: make pud_leaf() only care about the leaf bits Jingyi Sun
@ 2026-11-25  8:06 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-25  8:06 UTC (permalink / raw)
  To: Jingyi Sun, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: Alexandre Ghiti, David Hildenbrand, linux-mm

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] riscv: mm: missing present check in pud_user_accessible_page()
--

commit d68f5d3a880f9ce9b41f596dae6d5013013582b2
Author: Jingyi Sun <1045704958@qq.com>

riscv: mm: make pud_leaf() only care about the leaf bits

This commit drops the pud_present() check from pud_leaf() so that
PROT_NONE huge PUD mappings are still recognized as leaf entries.

> diff --git a/arch/riscv/include/asm/pgtable-64.h b/arch/riscv/include/asm/pgtable-64.h
> index eb7708e35cf92..1f45ebce8a750 100644
> --- a/arch/riscv/include/asm/pgtable-64.h
> +++ b/arch/riscv/include/asm/pgtable-64.h
> @@ -226,7 +226,7 @@ static inline int pud_bad(pud_t pud)
>  #define pud_leaf	pud_leaf
>  static inline bool pud_leaf(pud_t pud)
>  {
> -	return pud_present(pud) && (pud_val(pud) & _PAGE_LEAF);
> +	return pud_val(pud) & _PAGE_LEAF;
>  }
>  
>  static inline int pud_user(pud_t pud)

[Severity: Low]
Does dropping pud_present() here cause pud_user_accessible_page() to
return true for invalidated PUDs created by pud_mkinvalid()?

arch/riscv/include/asm/pgtable.h:pud_user_accessible_page() {
	return pud_leaf(pud) && pud_user(pud);
}

pud_mkinvalid() clears _PAGE_PRESENT and _PAGE_PROT_NONE while leaving
_PAGE_LEAF and _PAGE_USER set.  During change_huge_pud(),
pudp_invalidate() installs pud_mkinvalid(*pudp) via pudp_establish() ->
page_table_check_pud_set().  Because pud_user_accessible_page() no longer
checks whether the PUD is present, page_table_check accounts the
invalidated intermediate PUD as a live user mapping and performs
262,144 redundant page_ext counter increments and decrements across the
1G range before set_pud_at() installs the modified PUD.

Should pud_user_accessible_page() check for _PAGE_PRESENT (or
_PAGE_PRESENT | _PAGE_PROT_NONE), matching pmd_user_accessible_page() from
commit bdda2887cbf3 ("riscv: mm: exclude invalid THP PMDs from page table
check")?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_9D774595101D7C0F5A46CBCE69817B8A6E06@qq.com?part=1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-25  8:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  8:05 [PATCH] riscv: mm: make pud_leaf() only care about the leaf bits Jingyi Sun
2026-11-25  8:06 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox