[PATCH v3 1/5] mm: add MMAP_TRACK_PFN action and track_pfn_range() for lazy PFN maps

From: Eliav Farber

Date: Tue Nov 24 2026 - 08:15:45 EST


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@xxxxxxxxxx>
---

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