[PATCH v3 3/5] mm: add vmf_insert_pfn_order()
From: Eliav Farber
Date: Tue Nov 24 2026 - 08:15:41 EST
mm offers a ->huge_fault handler a PUD first, then a PMD, then ->fault
for a PTE. A VM_PFNMAP handler wanting huge mappings therefore dispatches
on the order itself, guarding the huge two with
CONFIG_ARCH_SUPPORTS_{PMD,PUD}_PFNMAP. vfio-pci open-codes that dispatch.
Add vmf_insert_pfn_order() to do the dispatch instead.
It maps a write fault writable at every order, as the huge DAX and DRM
handlers do; vfio-pci passes false. No order-0 PFNMAP insert took a write
flag before. vmf_insert_pfn_prot() keeps its signature, with its body
moved to a static helper that takes the write flag.
/dev/mem and UIO, later in this series, are the first users.
Signed-off-by: Eliav Farber <farbere@xxxxxxxxxx>
---
Changes in v3:
- Dropped vmf_pfn_order_ok(). Patch 2 checks the window and the pfn now,
so this helper only dispatches on the order.
Changes in v2:
- Pass the write flag at order 0 too. An ->access caller that does not
retry the fault was left with a read-only pte.
- vmf_insert_pfn_prot() keeps its signature; its body moved to a static
helper that takes the flag.
- Dropped the claim that uffd-wp can write-protect such a mapping.
include/linux/mm.h | 2 +
mm/memory.c | 102 ++++++++++++++++++++++++++++++++++-----------
2 files changed, 80 insertions(+), 24 deletions(-)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index cab58a403a3d..263689ded155 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -5069,6 +5069,8 @@ vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
unsigned long pfn);
vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma,
unsigned long addr, unsigned long pfn);
+vm_fault_t vmf_insert_pfn_order(struct vm_fault *vmf, unsigned long pfn,
+ unsigned int order);
int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len);
static inline vm_fault_t vmf_insert_page(struct vm_area_struct *vma,
diff --git a/mm/memory.c b/mm/memory.c
index 3d5706f8a7bf..7deff9c18acb 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2931,6 +2931,36 @@ static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr,
return VM_FAULT_NOPAGE;
}
+static vm_fault_t __vmf_insert_pfn_prot(struct vm_area_struct *vma,
+ unsigned long addr, unsigned long pfn,
+ pgprot_t pgprot, bool mkwrite)
+{
+ /*
+ * Technically, architectures with pte_special can avoid all these
+ * restrictions (same for remap_pfn_range). However we would like
+ * consistency in testing and feature parity among all, so we should
+ * try to keep these invariants in place for everybody.
+ */
+ BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
+ BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
+ (VM_PFNMAP|VM_MIXEDMAP));
+ BUG_ON((vma->vm_flags & VM_PFNMAP) && vma_is_cow_mapping(vma));
+ BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
+
+ if (unlikely(is_zero_pfn(pfn)))
+ return VM_FAULT_SIGBUS;
+
+ if (addr < vma->vm_start || addr >= vma->vm_end)
+ return VM_FAULT_SIGBUS;
+
+ if (!pfn_modify_allowed(pfn, pgprot))
+ return VM_FAULT_SIGBUS;
+
+ pfnmap_setup_cachemode_pfn(pfn, &pgprot);
+
+ return insert_pfn(vma, addr, pfn, pgprot, mkwrite);
+}
+
/**
* vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot
* @vma: user vma to map to
@@ -2967,30 +2997,7 @@ static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr,
vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
unsigned long pfn, pgprot_t pgprot)
{
- /*
- * Technically, architectures with pte_special can avoid all these
- * restrictions (same for remap_pfn_range). However we would like
- * consistency in testing and feature parity among all, so we should
- * try to keep these invariants in place for everybody.
- */
- BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
- BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
- (VM_PFNMAP|VM_MIXEDMAP));
- BUG_ON((vma->vm_flags & VM_PFNMAP) && vma_is_cow_mapping(vma));
- BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
-
- if (unlikely(is_zero_pfn(pfn)))
- return VM_FAULT_SIGBUS;
-
- if (addr < vma->vm_start || addr >= vma->vm_end)
- return VM_FAULT_SIGBUS;
-
- if (!pfn_modify_allowed(pfn, pgprot))
- return VM_FAULT_SIGBUS;
-
- pfnmap_setup_cachemode_pfn(pfn, &pgprot);
-
- return insert_pfn(vma, addr, pfn, pgprot, false);
+ return __vmf_insert_pfn_prot(vma, addr, pfn, pgprot, false);
}
EXPORT_SYMBOL(vmf_insert_pfn_prot);
@@ -3021,6 +3028,53 @@ vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
}
EXPORT_SYMBOL(vmf_insert_pfn);
+/**
+ * vmf_insert_pfn_order - insert a pfn mapping at a given fault order
+ * @vmf: The fault being handled.
+ * @pfn: The pfn to map at the start of the order-aligned fault window.
+ * @order: The fault order, as passed to ->huge_fault (0 for ->fault).
+ *
+ * For ->fault and ->huge_fault handlers of VM_PFNMAP mappings, which must not
+ * be COW. Maps @pfn with a PTE, PMD or PUD as @order and the architecture
+ * allow. A write fault is mapped writable, so the first write does not call
+ * ->pfn_mkwrite.
+ *
+ * Return: vm_fault_t value; %VM_FAULT_FALLBACK if the fault cannot be mapped
+ * at @order, so that it is retried at a smaller order. @order 0 never returns
+ * %VM_FAULT_FALLBACK.
+ */
+vm_fault_t vmf_insert_pfn_order(struct vm_fault *vmf, unsigned long pfn,
+ unsigned int order)
+{
+ struct vm_area_struct *vma = vmf->vma;
+ bool write = vmf->flags & FAULT_FLAG_WRITE;
+
+ /*
+ * Pass write at order 0 too: vm_page_prot is read-only while soft-dirty
+ * is armed, and .access has no second fault to upgrade the pte.
+ */
+ if (!order)
+ return __vmf_insert_pfn_prot(vma, vmf->address, pfn,
+ vma->vm_page_prot, write);
+
+ /* vmf_insert_pfn_pmd() rejects the huge zero pfn; order 0 does not */
+ if (is_huge_zero_pfn(pfn))
+ return VM_FAULT_FALLBACK;
+
+ /* vmf_insert_pfn_pmd() and vmf_insert_pfn_pud() do not check L1TF */
+ if (!pfn_modify_allowed(pfn, vma->vm_page_prot))
+ return VM_FAULT_FALLBACK;
+
+ if (IS_ENABLED(CONFIG_ARCH_SUPPORTS_PMD_PFNMAP) && order == PMD_ORDER)
+ return vmf_insert_pfn_pmd(vmf, pfn, write);
+
+ if (IS_ENABLED(CONFIG_ARCH_SUPPORTS_PUD_PFNMAP) && order == PUD_ORDER)
+ return vmf_insert_pfn_pud(vmf, pfn, write);
+
+ return VM_FAULT_FALLBACK;
+}
+EXPORT_SYMBOL_GPL(vmf_insert_pfn_order);
+
static bool vm_mixed_ok(struct vm_area_struct *vma, unsigned long pfn,
bool mkwrite)
{
--
2.47.3