From: Eliav Farber <farbere@amazon.com>
To: Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Zi Yan <ziy@nvidia.com>
Cc: Lorenzo Stoakes <ljs@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>,
"Suren Baghdasaryan" <surenb@google.com>,
Michal Hocko <mhocko@suse.com>, Jann Horn <jannh@google.com>,
Pedro Falcato <pfalcato@suse.de>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Nico Pache <nico.pache@linux.dev>,
"Ryan Roberts" <ryan.roberts@arm.com>,
Dev Jain <dev.jain@arm.com>, Barry Song <baohua@kernel.org>,
Lance Yang <lance.yang@linux.dev>,
Usama Arif <usama.arif@linux.dev>,
Kiryl Shutsemau <kas@kernel.org>, <linux-mm@kvack.org>,
<linux-kernel@vger.kernel.org>, Eliav Farber <farbere@amazon.com>
Subject: [PATCH v3 1/5] mm: add MMAP_TRACK_PFN action and track_pfn_range() for lazy PFN maps
Date: Tue, 24 Nov 2026 13:15:06 +0000 [thread overview]
Message-ID: <20261124131510.38745-2-farbere@amazon.com> (raw)
In-Reply-To: <20261124131510.38745-1-farbere@amazon.com>
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
next prev parent reply other threads:[~2026-11-24 13:15 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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 ` [PATCH v3 3/5] mm: add vmf_insert_pfn_order() Eliav Farber
2026-11-24 13:15 ` [PATCH v3 4/5] mm/char-mem: implement huge_fault support for /dev/mem Eliav Farber
2026-11-25 13:16 ` sashiko-bot
2026-11-24 13:15 ` [PATCH v3 5/5] uio: implement huge_fault support for physical mappings Eliav Farber
2026-11-24 14:12 ` Greg Kroah-Hartman
2026-11-24 13:28 ` [PATCH v3 0/5] mm, uio: huge PFN mappings for /dev/mem and UIO Lorenzo Stoakes (ARM)
2026-11-24 18:15 ` Farber, Eliav
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124131510.38745-2-farbere@amazon.com \
--to=farbere@amazon.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=gregkh@linuxfoundation.org \
--cc=jannh@google.com \
--cc=kas@kernel.org \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=nico.pache@linux.dev \
--cc=pfalcato@suse.de \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=surenb@google.com \
--cc=usama.arif@linux.dev \
--cc=vbabka@kernel.org \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.