[PATCH] riscv: mm: make pud_leaf() only care about the leaf bits
From: Jingyi Sun
Date: Tue Nov 24 2026 - 03:06:27 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Jingyi Sun <1045704958@xxxxxx>
---
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