* [PATCH v3 1/5] mm: add MMAP_TRACK_PFN action and track_pfn_range() for lazy PFN maps
2026-11-24 13:15 [PATCH v3 0/5] mm, uio: huge PFN mappings for /dev/mem and UIO Eliav Farber
@ 2026-11-24 13:15 ` Eliav Farber
2026-11-24 13:15 ` [PATCH v3 2/5] mm/huge_memory: check the window and the pfn alignment before inserting Eliav Farber
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Eliav Farber @ 2026-11-24 13:15 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Greg Kroah-Hartman, Zi Yan
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Jann Horn, Pedro Falcato,
Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
Lance Yang, Usama Arif, Kiryl Shutsemau, linux-mm, linux-kernel,
Eliav Farber
remap_pfn_range() populates a PFN mapping and, on x86, reserves the
range's memory type, so a later conflicting mapping of the same range is
rejected or coerced. A driver that populates from a fault handler gets no
such reservation: vmf_insert_pfn() and friends only look the memory type
up, so a conflict goes undetected.
Add track_pfn_range(). It sets the VMA flags remap_pfn_range() would set
and reserves the whole VMA's PFN range for its lifetime, but populates
nothing: that is left to the caller's fault handler. COW mappings are
rejected, as vmf_insert_pfn*() cannot populate them.
For drivers that use ->mmap_prepare, where no VMA exists yet, add an
MMAP_TRACK_PFN action that describes the range as MMAP_REMAP_PFN does and
tracks it on completion. It must cover the whole VMA, as that is what
gets reserved.
The /dev/mem and UIO handlers later in this series are the first users.
Signed-off-by: Eliav Farber <farbere@amazon.com>
---
Changes in v3:
- Renamed pfn_range_track() to track_pfn_range(), and MMAP_PFN_TRACK to
MMAP_TRACK_PFN, to read like remap_pfn_range() and MMAP_REMAP_PFN.
- Dropped the addr and size arguments. The range is always the whole VMA.
include/linux/mm.h | 16 +++++++
include/linux/mm_types.h | 1 +
mm/internal.h | 3 ++
mm/memory.c | 80 +++++++++++++++++++++++++++++++++
mm/util.c | 7 +++
tools/testing/vma/include/dup.h | 1 +
6 files changed, 108 insertions(+)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 040cff64381b..cab58a403a3d 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -4814,6 +4814,21 @@ static inline void mmap_action_ioremap_full(struct vm_area_desc *desc,
mmap_action_ioremap(desc, desc->start, start_pfn, vma_desc_size(desc));
}
+/**
+ * mmap_action_track_pfn - helper for mmap_prepare hook to specify that the PFN
+ * range backing the entirety of a VMA should be tracked, but not mapped.
+ * @desc: The VMA descriptor for the VMA.
+ * @start_pfn: The first PFN in the range.
+ *
+ * Population is left to the driver's fault handler. Not for COW mappings.
+ */
+static inline void mmap_action_track_pfn(struct vm_area_desc *desc,
+ unsigned long start_pfn)
+{
+ mmap_action_remap_full(desc, start_pfn);
+ desc->action.type = MMAP_TRACK_PFN;
+}
+
/**
* mmap_action_simple_ioremap - helper for mmap_prepare hook to specify that the
* physical range in [start_phys_addr, start_phys_addr + size) should be I/O
@@ -5035,6 +5050,7 @@ struct vm_area_struct *find_extend_vma_locked(struct mm_struct *,
unsigned long addr);
int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
unsigned long pfn, unsigned long size, pgprot_t pgprot);
+int track_pfn_range(struct vm_area_struct *vma, unsigned long pfn);
int vm_insert_page(struct vm_area_struct *, unsigned long addr, struct page *);
int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr,
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 1ed35d258d2e..f9f1866ad772 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -821,6 +821,7 @@ enum mmap_action_type {
MMAP_SIMPLE_IO_REMAP, /* I/O remap with guardrails. */
MMAP_KERNEL_PAGES, /* Map kernel page range from array. */
MMAP_DISCONTIG_KERNEL_PAGES, /* Map kernel discontig page range. */
+ MMAP_TRACK_PFN, /* Track PFN range, map on fault. */
};
enum discontig_kernel_page_action {
diff --git a/mm/internal.h b/mm/internal.h
index 85cf7becf858..5a599402c57d 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -1548,6 +1548,9 @@ int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm);
int remap_pfn_range_prepare(struct vm_area_desc *desc);
int remap_pfn_range_complete(struct vm_area_struct *vma,
struct mmap_action *action);
+int track_pfn_range_prepare(struct vm_area_desc *desc);
+int track_pfn_range_complete(struct vm_area_struct *vma,
+ struct mmap_action *action);
int simple_ioremap_prepare(struct vm_area_desc *desc);
int map_kernel_pages_prepare(struct vm_area_desc *desc);
int map_kernel_pages_complete(struct vm_area_struct *vma,
diff --git a/mm/memory.c b/mm/memory.c
index 7f019afc35ca..3d5706f8a7bf 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3439,6 +3439,86 @@ int remap_pfn_range_complete(struct vm_area_struct *vma,
return do_remap_pfn_range(vma, start, pfn, size, prot);
}
+#ifdef __HAVE_PFNMAP_TRACKING
+static int do_track_pfn_range(struct vm_area_struct *vma, unsigned long pfn)
+{
+ const unsigned long size = vma->vm_end - vma->vm_start;
+ struct pfnmap_track_ctx *ctx;
+
+ if (vma->pfnmap_track_ctx)
+ return -EINVAL;
+
+ ctx = pfnmap_track_ctx_alloc(pfn, size, &vma->vm_page_prot);
+ if (IS_ERR(ctx))
+ return PTR_ERR(ctx);
+
+ vma->pfnmap_track_ctx = ctx;
+ return 0;
+}
+#else
+static int do_track_pfn_range(struct vm_area_struct *vma, unsigned long pfn)
+{
+ return 0;
+}
+#endif
+
+/**
+ * track_pfn_range - reserve a VMA's PFN range without populating it
+ * @vma: user vma to track the range for
+ * @pfn: page frame number at vma->vm_start
+ *
+ * Sets the VMA flags remap_pfn_range() would set and reserves the memory type
+ * of the whole VMA's PFN range for its lifetime. Creates no page table
+ * entries: the caller's fault handler populates the range, and must not map
+ * outside it.
+ *
+ * vma cannot be a COW mapping.
+ *
+ * Note: this is only safe if the mmap lock is held for writing when called.
+ *
+ * Return: %0 on success, negative error code otherwise.
+ */
+int track_pfn_range(struct vm_area_struct *vma, unsigned long pfn)
+{
+ int err;
+
+ if (vma_is_cow_mapping(vma))
+ return -EINVAL;
+
+ err = remap_pfn_range_prepare_vma(vma, vma->vm_start, pfn,
+ vma->vm_end - vma->vm_start);
+ if (err)
+ return err;
+
+ return do_track_pfn_range(vma, pfn);
+}
+EXPORT_SYMBOL_GPL(track_pfn_range);
+
+int track_pfn_range_prepare(struct vm_area_desc *desc)
+{
+ const struct mmap_action *action = &desc->action;
+
+ /* COW mappings cannot be populated on fault */
+ if (vma_desc_is_cow_mapping(desc))
+ return -EINVAL;
+
+ /* do_track_pfn_range() anchors start_pfn at the VMA start */
+ if (action->remap.start != desc->start)
+ return -EINVAL;
+
+ /* do_track_pfn_range() reserves the whole VMA, so the action must */
+ if (action->remap.size != vma_desc_size(desc))
+ return -EINVAL;
+
+ return remap_pfn_range_prepare(desc);
+}
+
+int track_pfn_range_complete(struct vm_area_struct *vma,
+ struct mmap_action *action)
+{
+ return do_track_pfn_range(vma, action->remap.start_pfn);
+}
+
static int __simple_ioremap_prep(unsigned long vm_len, pgoff_t vm_pgoff,
phys_addr_t start_phys, unsigned long size,
unsigned long *pfnp)
diff --git a/mm/util.c b/mm/util.c
index 80f6c714844f..5fad246b7a58 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -1554,6 +1554,8 @@ int mmap_action_prepare(struct vm_area_desc *desc)
return map_kernel_pages_prepare(desc);
case MMAP_DISCONTIG_KERNEL_PAGES:
return map_discontig_kernel_pages_prepare(desc);
+ case MMAP_TRACK_PFN:
+ return track_pfn_range_prepare(desc);
}
WARN_ON_ONCE(1);
@@ -1589,6 +1591,9 @@ int mmap_action_complete(struct vm_area_struct *vma,
case MMAP_DISCONTIG_KERNEL_PAGES:
err = map_discontig_kernel_pages_complete(vma, action);
break;
+ case MMAP_TRACK_PFN:
+ err = track_pfn_range_complete(vma, action);
+ break;
case MMAP_IO_REMAP_PFN:
case MMAP_SIMPLE_IO_REMAP:
/* Should have been delegated. */
@@ -1611,6 +1616,7 @@ int mmap_action_prepare(struct vm_area_desc *desc)
case MMAP_SIMPLE_IO_REMAP:
case MMAP_KERNEL_PAGES:
case MMAP_DISCONTIG_KERNEL_PAGES:
+ case MMAP_TRACK_PFN:
WARN_ON_ONCE(1); /* nommu cannot handle these. */
break;
}
@@ -1633,6 +1639,7 @@ int mmap_action_complete(struct vm_area_struct *vma,
case MMAP_SIMPLE_IO_REMAP:
case MMAP_KERNEL_PAGES:
case MMAP_DISCONTIG_KERNEL_PAGES:
+ case MMAP_TRACK_PFN:
WARN_ON_ONCE(1); /* nommu cannot handle this. */
err = -EINVAL;
diff --git a/tools/testing/vma/include/dup.h b/tools/testing/vma/include/dup.h
index dc4927d26296..1bbcb92ab5a6 100644
--- a/tools/testing/vma/include/dup.h
+++ b/tools/testing/vma/include/dup.h
@@ -455,6 +455,7 @@ enum mmap_action_type {
MMAP_SIMPLE_IO_REMAP, /* I/O remap with guardrails. */
MMAP_KERNEL_PAGES, /* Map kernel page range from array. */
MMAP_DISCONTIG_KERNEL_PAGES, /* Map kernel discontig page range. */
+ MMAP_TRACK_PFN, /* Track PFN range, map on fault. */
};
/*
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v3 2/5] mm/huge_memory: check the window and the pfn alignment before inserting
2026-11-24 13:15 [PATCH v3 0/5] mm, uio: huge PFN mappings for /dev/mem and UIO Eliav Farber
2026-11-24 13:15 ` [PATCH v3 1/5] mm: add MMAP_TRACK_PFN action and track_pfn_range() for lazy PFN maps Eliav Farber
@ 2026-11-24 13:15 ` Eliav Farber
2026-11-24 13:15 ` [PATCH v3 3/5] mm: add vmf_insert_pfn_order() Eliav Farber
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Eliav Farber @ 2026-11-24 13:15 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Greg Kroah-Hartman, Zi Yan
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Jann Horn, Pedro Falcato,
Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
Lance Yang, Usama Arif, Kiryl Shutsemau, linux-mm, linux-kernel,
Eliav Farber
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@amazon.com>
---
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
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v3 3/5] mm: add vmf_insert_pfn_order()
2026-11-24 13:15 [PATCH v3 0/5] mm, uio: huge PFN mappings for /dev/mem and UIO Eliav Farber
2026-11-24 13:15 ` [PATCH v3 1/5] mm: add MMAP_TRACK_PFN action and track_pfn_range() for lazy PFN maps Eliav Farber
2026-11-24 13:15 ` [PATCH v3 2/5] mm/huge_memory: check the window and the pfn alignment before inserting Eliav Farber
@ 2026-11-24 13:15 ` Eliav Farber
2026-11-24 13:15 ` [PATCH v3 4/5] mm/char-mem: implement huge_fault support for /dev/mem Eliav Farber
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Eliav Farber @ 2026-11-24 13:15 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Greg Kroah-Hartman, Zi Yan
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Jann Horn, Pedro Falcato,
Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
Lance Yang, Usama Arif, Kiryl Shutsemau, linux-mm, linux-kernel,
Eliav Farber
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@amazon.com>
---
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
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v3 4/5] mm/char-mem: implement huge_fault support for /dev/mem
2026-11-24 13:15 [PATCH v3 0/5] mm, uio: huge PFN mappings for /dev/mem and UIO Eliav Farber
` (2 preceding siblings ...)
2026-11-24 13:15 ` [PATCH v3 3/5] mm: add vmf_insert_pfn_order() Eliav Farber
@ 2026-11-24 13:15 ` Eliav Farber
2026-11-24 13:15 ` [PATCH v3 5/5] uio: implement huge_fault support for physical mappings Eliav Farber
2026-11-24 13:28 ` [PATCH v3 0/5] mm, uio: huge PFN mappings for /dev/mem and UIO Lorenzo Stoakes (ARM)
5 siblings, 0 replies; 7+ messages in thread
From: Eliav Farber @ 2026-11-24 13:15 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Greg Kroah-Hartman, Zi Yan
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Jann Horn, Pedro Falcato,
Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
Lance Yang, Usama Arif, Kiryl Shutsemau, linux-mm, linux-kernel,
Eliav Farber
mmap_mem_prepare() maps /dev/mem at mmap() time with 4KiB PTEs, however
large and aligned the region is. Populate on fault instead, as vfio-pci
does since commit f9e54c3a2f5b ("vfio/pci: implement huge_fault
support"): request MMAP_TRACK_PFN rather than an eager remap, and add
->fault and ->huge_fault handlers using vmf_insert_pfn_order(). A large
enough aligned region is then mapped with PMD or PUD entries.
Two cases keep the eager remap: COW, which vmf_insert_pfn*() cannot
populate, and CONFIG_IO_STRICT_DEVMEM, where a fault can race
revoke_iomem() and leave a claimed range mapped.
ptrace and /proc/pid/mem keep working, because .access faults the page
in first. They now fail on a mapping with no VM_READ. The eager remap
used to leave a present pte for them to follow.
The order-0 insert refuses the zero pfn, which remap_pfn_range() mapped.
With CONFIG_STRICT_DEVMEM on, mmap() of that pfn fails with -EPERM.
Signed-off-by: Eliav Farber <farbere@amazon.com>
---
Changes in v3:
- Keep the eager mapping when CONFIG_IO_STRICT_DEVMEM is set, instead of
re-checking range_is_allowed() on every fault.
- Return fixup_user_fault()'s error from .access instead of ignoring it.
Changes in v2:
- Give COW mappings their own vm_operations_struct, rather than test for
COW inside the fault handler.
- Corrected the L1TF claim in the commit message.
mm/char-mem.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 77 insertions(+), 4 deletions(-)
diff --git a/mm/char-mem.c b/mm/char-mem.c
index c5761ddedbd7..283531645f18 100644
--- a/mm/char-mem.c
+++ b/mm/char-mem.c
@@ -318,9 +318,68 @@ static inline int private_mapping_ok(struct vm_area_desc *desc)
}
#endif
+#ifdef CONFIG_MMU
+/* Populate /dev/mem on fault, huge where the arch and alignment allow */
+static vm_fault_t mmap_mem_huge_fault(struct vm_fault *vmf, unsigned int order)
+{
+ unsigned long addr = vmf->address & ~((PAGE_SIZE << order) - 1);
+ struct vm_area_struct *vma = vmf->vma;
+ unsigned long pfn;
+
+ /* An order-aligned addr below the VMA would wrap the subtraction */
+ if (addr < vma->vm_start)
+ return VM_FAULT_FALLBACK;
+
+ pfn = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT);
+
+ return vmf_insert_pfn_order(vmf, pfn, order);
+}
+
+static vm_fault_t mmap_mem_fault(struct vm_fault *vmf)
+{
+ return mmap_mem_huge_fault(vmf, 0);
+}
+#endif /* CONFIG_MMU */
+
+#ifdef CONFIG_HAVE_IOREMAP_PROT
+/* generic_access_phys() needs a present PTE; a lazy mapping may have none */
+static int mmap_mem_access(struct vm_area_struct *vma, unsigned long addr,
+ void *buf, int len, int write)
+{
+ unsigned int fault_flags = FAULT_FLAG_REMOTE;
+ int ret;
+
+ if (write)
+ fault_flags |= FAULT_FLAG_WRITE;
+
+ /* unlocked == NULL keeps the mmap lock held and the vma valid */
+ ret = fixup_user_fault(vma->vm_mm, addr, fault_flags, NULL);
+ if (ret)
+ return ret;
+
+ return generic_access_phys(vma, addr, buf, len, write);
+}
+#endif
+
+/*
+ * vm_ops for an eagerly populated mapping. Must exist even when empty: a NULL
+ * vm_ops would make the VMA anonymous.
+ */
+static const struct vm_operations_struct mmap_mem_eager_vm_ops = {
+#ifdef CONFIG_HAVE_IOREMAP_PROT
+ .access = generic_access_phys,
+#endif
+};
+
static const struct vm_operations_struct mmap_mem_ops = {
+#ifdef CONFIG_MMU
+ .fault = mmap_mem_fault,
+#endif
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+ .huge_fault = mmap_mem_huge_fault,
+#endif
#ifdef CONFIG_HAVE_IOREMAP_PROT
- .access = generic_access_phys
+ .access = mmap_mem_access,
#endif
};
@@ -355,11 +414,25 @@ static int mmap_mem_prepare(struct vm_area_desc *desc)
size,
desc->page_prot);
+ desc->action.error_override = -EAGAIN;
+
+ /*
+ * COW can't be populated on fault: vmf_insert_pfn*() BUG_ON() it. And
+ * with IO_STRICT_DEVMEM a fault can install an entry after
+ * revoke_iomem() has zapped it, so keep the revoke final.
+ */
+ if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM) ||
+ vma_desc_is_cow_mapping(desc)) {
+ /* Remap-pfn-range will mark the range with the I/O flag. */
+ desc->vm_ops = &mmap_mem_eager_vm_ops;
+ mmap_action_remap_full(desc, desc->pgoff);
+ return 0;
+ }
+
desc->vm_ops = &mmap_mem_ops;
- /* Remap-pfn-range will mark the range with the I/O flag. */
- mmap_action_remap_full(desc, desc->pgoff);
- desc->action.error_override = -EAGAIN;
+ /* Tracking reserves the memory type, for PAT conflict detection */
+ mmap_action_track_pfn(desc, desc->pgoff);
return 0;
}
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v3 5/5] uio: implement huge_fault support for physical mappings
2026-11-24 13:15 [PATCH v3 0/5] mm, uio: huge PFN mappings for /dev/mem and UIO Eliav Farber
` (3 preceding siblings ...)
2026-11-24 13:15 ` [PATCH v3 4/5] mm/char-mem: implement huge_fault support for /dev/mem Eliav Farber
@ 2026-11-24 13:15 ` Eliav Farber
2026-11-24 13:28 ` [PATCH v3 0/5] mm, uio: huge PFN mappings for /dev/mem and UIO Lorenzo Stoakes (ARM)
5 siblings, 0 replies; 7+ messages in thread
From: Eliav Farber @ 2026-11-24 13:15 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Greg Kroah-Hartman, Zi Yan
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Jann Horn, Pedro Falcato,
Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
Lance Yang, Usama Arif, Kiryl Shutsemau, linux-mm, linux-kernel,
Eliav Farber
uio_mmap_physical() maps UIO_MEM_PHYS and UIO_MEM_IOVA regions at mmap()
time with 4KiB PTEs, however large and aligned they are. Populate on
fault instead, as vfio-pci does since commit f9e54c3a2f5b ("vfio/pci:
implement huge_fault support"): call track_pfn_range() rather than
remap_pfn_range(), and add ->fault and ->huge_fault handlers using
vmf_insert_pfn_order(). A large enough aligned region is then mapped with
PMD or PUD entries.
Like uio_vma_fault(), both handlers take idev->info_lock and fail with
SIGBUS once the device is gone. COW mappings keep the eager remap.
The fault handler needs the base pfn, so vm_pgoff now holds that instead
of the map index, as remap_pfn_range() does for COW mappings.
/proc/pid/maps reports it as the offset of these mappings.
Once the device is unregistered, an untouched page gets SIGBUS. The
eager remap used to keep it mapped. A remote read through ptrace or
/proc/pid/mem of a mapping with no VM_READ now fails too, where the
eager remap used to leave a present pte.
Signed-off-by: Eliav Farber <farbere@amazon.com>
---
Changes in v3:
- Follow track_pfn_range()'s new signature.
- Return fixup_user_fault()'s error from .access instead of ignoring it.
Changes in v2:
- Give COW mappings their own vm_operations_struct, rather than test for
COW inside the fault handler.
- Corrected the L1TF claim in the commit message.
drivers/uio/uio.c | 99 ++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 90 insertions(+), 9 deletions(-)
diff --git a/drivers/uio/uio.c b/drivers/uio/uio.c
index f8fa20522660..00f58b68d504 100644
--- a/drivers/uio/uio.c
+++ b/drivers/uio/uio.c
@@ -733,12 +733,78 @@ static int uio_mmap_logical(struct vm_area_struct *vma)
return 0;
}
-static const struct vm_operations_struct uio_physical_vm_ops = {
+/* Populate UIO maps on fault, huge where the arch and alignment allow */
+static vm_fault_t uio_physical_huge_fault(struct vm_fault *vmf,
+ unsigned int order)
+{
+ unsigned long addr = vmf->address & ~((PAGE_SIZE << order) - 1);
+ struct vm_area_struct *vma = vmf->vma;
+ struct uio_device *idev = vma->vm_private_data;
+ vm_fault_t ret = VM_FAULT_SIGBUS;
+ unsigned long pfn;
+
+ /* An order-aligned addr below the VMA would wrap the subtraction */
+ if (addr < vma->vm_start)
+ return VM_FAULT_FALLBACK;
+
+ /* vm_pgoff is the base pfn, not the map index (uio_mmap_physical()) */
+ pfn = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT);
+
+ mutex_lock(&idev->info_lock);
+
+ if (idev->info)
+ ret = vmf_insert_pfn_order(vmf, pfn, order);
+
+ mutex_unlock(&idev->info_lock);
+
+ return ret;
+}
+
+static vm_fault_t uio_physical_fault(struct vm_fault *vmf)
+{
+ return uio_physical_huge_fault(vmf, 0);
+}
+
+#ifdef CONFIG_HAVE_IOREMAP_PROT
+/* generic_access_phys() needs a present PTE; a lazy mapping may have none */
+static int uio_physical_access(struct vm_area_struct *vma, unsigned long addr,
+ void *buf, int len, int write)
+{
+ unsigned int fault_flags = FAULT_FLAG_REMOTE;
+ int ret;
+
+ if (write)
+ fault_flags |= FAULT_FLAG_WRITE;
+
+ /* unlocked == NULL keeps the mmap lock held and the vma valid */
+ ret = fixup_user_fault(vma->vm_mm, addr, fault_flags, NULL);
+ if (ret)
+ return ret;
+
+ return generic_access_phys(vma, addr, buf, len, write);
+}
+#endif
+
+/*
+ * vm_ops for a COW mapping, which vmf_insert_pfn*() cannot populate. Must
+ * exist even when empty: a NULL vm_ops would make the VMA anonymous.
+ */
+static const struct vm_operations_struct uio_physical_cow_vm_ops = {
#ifdef CONFIG_HAVE_IOREMAP_PROT
.access = generic_access_phys,
#endif
};
+static const struct vm_operations_struct uio_physical_vm_ops = {
+ .fault = uio_physical_fault,
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+ .huge_fault = uio_physical_huge_fault,
+#endif
+#ifdef CONFIG_HAVE_IOREMAP_PROT
+ .access = uio_physical_access,
+#endif
+};
+
static int uio_mmap_physical(struct vm_area_struct *vma)
{
struct uio_device *idev = vma->vm_private_data;
@@ -754,7 +820,6 @@ static int uio_mmap_physical(struct vm_area_struct *vma)
if (vma->vm_end - vma->vm_start > mem->size)
return -EINVAL;
- vma->vm_ops = &uio_physical_vm_ops;
if (idev->info->mem[mi].memtype == UIO_MEM_PHYS)
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
@@ -764,14 +829,30 @@ static int uio_mmap_physical(struct vm_area_struct *vma)
* up above in uio_find_mem_index(), rather than an
* actual page offset into the mmap.
*
- * So we just do the physical mmap without a page
- * offset.
+ * So a COW mapping is remapped below without a page
+ * offset, and the lazy path overwrites vm_pgoff.
*/
- return remap_pfn_range(vma,
- vma->vm_start,
- mem->addr >> PAGE_SHIFT,
- vma->vm_end - vma->vm_start,
- vma->vm_page_prot);
+ if (vma_is_cow_mapping(vma)) {
+ vma->vm_ops = &uio_physical_cow_vm_ops;
+
+ return remap_pfn_range(vma,
+ vma->vm_start,
+ mem->addr >> PAGE_SHIFT,
+ vma->vm_end - vma->vm_start,
+ vma->vm_page_prot);
+ }
+
+ vma->vm_ops = &uio_physical_vm_ops;
+
+ /*
+ * A VMA split adds the split offset to vm_pgoff, so the map index
+ * stored there would no longer find the map. Store the base pfn
+ * instead, as remap_pfn_range() does for COW mappings.
+ */
+ vma->vm_pgoff = mem->addr >> PAGE_SHIFT;
+
+ /* Tracking reserves the memory type, for PAT conflict detection */
+ return track_pfn_range(vma, mem->addr >> PAGE_SHIFT);
}
static int uio_mmap_dma_coherent(struct vm_area_struct *vma)
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH v3 0/5] mm, uio: huge PFN mappings for /dev/mem and UIO
2026-11-24 13:15 [PATCH v3 0/5] mm, uio: huge PFN mappings for /dev/mem and UIO Eliav Farber
` (4 preceding siblings ...)
2026-11-24 13:15 ` [PATCH v3 5/5] uio: implement huge_fault support for physical mappings Eliav Farber
@ 2026-11-24 13:28 ` Lorenzo Stoakes (ARM)
5 siblings, 0 replies; 7+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-24 13:28 UTC (permalink / raw)
To: Eliav Farber
Cc: Andrew Morton, David Hildenbrand, Greg Kroah-Hartman, Zi Yan,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Jann Horn, Pedro Falcato,
Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
Lance Yang, Usama Arif, Kiryl Shutsemau, linux-mm, linux-kernel
What on earth are you doing, Eliav?
There was outstanding review on v2, you replied to it with what looks like an
AI-generated response [0], then instantly sent this respin out?...!
Don't send respins unless it is ENTIRELY CLEAR that the feedback is addressed,
and that goes double if a maintainer is CLEARLY not happy with your responses,
as is patently obvious in this case.
I am inclined to consider your patches as bug reports only moving forwards,
honestly. Maybe David has more patience than me on this :)
--
Cheers, Lorenzo
[0]:https://lore.kernel.org/all/BY3PR18MB4722314909E880B1D77EED62C6442@BY3PR18MB4722.namprd18.prod.outlook.com/
^ permalink raw reply [flat|nested] 7+ messages in thread