[PATCH v3 2/5] mm/huge_memory: check the window and the pfn alignment before inserting
From: Eliav Farber
Date: Tue Nov 24 2026 - 08:15:54 EST
insert_pmd() and insert_pud() check that addr is inside the VMA, but not
that the whole PMD or PUD is. A VMA ending part-way through one gets
mapped past its end. Neither checks the pfn, and pfn_pmd() on x86 rounds
an unaligned pfn down, mapping the wrong memory.
No caller can reach either case today; each checks the geometry first.
The next patch adds vmf_insert_pfn_order(), whose callers do not.
Check both ends of the window, and the pfn alignment in
vmf_insert_pfn_pmd() and vmf_insert_pfn_pud(). Fall back rather than
SIGBUS: a VMA that does not span a whole PMD is ordinary.
Several callers open-code the same test; vfio-pci and nvgrace-gpu share
one as is_aligned_for_order() in a vfio-private header. The callers'
checks stay: some do more than mm can, as device-dax turns a window
failure into SIGBUS and the fsdax check also gates its PMD-order mapping
entry.
Signed-off-by: Eliav Farber <farbere@xxxxxxxxxx>
---
New in v3. In v2 these checks sat in vmf_insert_pfn_order(), where only
my own callers got them.
mm/huge_memory.c | 18 ++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 75e8641bf9ff..909db70aeba2 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1630,8 +1630,9 @@ static vm_fault_t insert_pmd(struct vm_area_struct *vma, unsigned long addr,
spinlock_t *ptl;
pmd_t entry;
- if (addr < vma->vm_start || addr >= vma->vm_end)
- return VM_FAULT_SIGBUS;
+ /* If the whole PMD doesn't fit in the VMA, fall back to 4KiB pages */
+ if (addr < vma->vm_start || addr + HPAGE_PMD_SIZE > vma->vm_end)
+ return VM_FAULT_FALLBACK;
if (arch_needs_pgtable_deposit()) {
pgtable = pte_alloc_one(vma->vm_mm);
@@ -1729,6 +1730,10 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, unsigned long pfn,
if (unlikely(is_huge_zero_pfn(pfn)))
return VM_FAULT_SIGBUS;
+ /* pfn_pmd() would round an unaligned pfn down and map the wrong memory */
+ if (!IS_ALIGNED(pfn, HPAGE_PMD_NR))
+ return VM_FAULT_FALLBACK;
+
pfnmap_setup_cachemode_pfn(pfn, &pgprot);
return insert_pmd(vma, addr, vmf->pmd, fop, pgprot, write);
@@ -1768,8 +1773,9 @@ static vm_fault_t insert_pud(struct vm_area_struct *vma, unsigned long addr,
spinlock_t *ptl;
pud_t entry;
- if (addr < vma->vm_start || addr >= vma->vm_end)
- return VM_FAULT_SIGBUS;
+ /* If the whole PUD doesn't fit in the VMA, fall back to smaller pages */
+ if (addr < vma->vm_start || addr + HPAGE_PUD_SIZE > vma->vm_end)
+ return VM_FAULT_FALLBACK;
ptl = pud_lock(mm, pud);
if (!pud_none(*pud)) {
@@ -1842,6 +1848,10 @@ vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, unsigned long pfn,
(VM_PFNMAP|VM_MIXEDMAP));
BUG_ON((vma->vm_flags & VM_PFNMAP) && vma_is_cow_mapping(vma));
+ /* pfn_pud() would round an unaligned pfn down and map the wrong memory */
+ if (!IS_ALIGNED(pfn, HPAGE_PUD_NR))
+ return VM_FAULT_FALLBACK;
+
pfnmap_setup_cachemode_pfn(pfn, &pgprot);
return insert_pud(vma, addr, vmf->pud, fop, pgprot, write);
--
2.47.3