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 4/5] mm/char-mem: implement huge_fault support for /dev/mem
Date: Tue, 24 Nov 2026 13:15:09 +0000 [thread overview]
Message-ID: <20261124131510.38745-5-farbere@amazon.com> (raw)
In-Reply-To: <20261124131510.38745-1-farbere@amazon.com>
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
next prev parent reply other threads:[~2026-11-24 13:15 UTC|newest]
Thread overview: 6+ 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 ` [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 ` [PATCH v3 3/5] mm: add vmf_insert_pfn_order() Eliav Farber
2026-11-24 13:15 ` Eliav Farber [this message]
2026-11-24 13:15 ` [PATCH v3 5/5] uio: implement huge_fault support for physical mappings Eliav Farber
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-5-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox