mm, uio: huge PFN mappings for /dev/mem and UIO
Subsystems (5)
cover mm, uio: huge PFN mappings for /dev/mem and UIO
/dev/mem and UIO both hand large, well-aligned physical regions to userspace, and both populate them eagerly at mmap() time with remap_pfn_range(), that is with 4K PTEs however large and well aligned the region is. vfio-pci moved to fault-driven population in commit f9e54c3a2f5b ("vfio/pci: implement huge_fault support") and gets PMD and PUD mappings where alignment allows. This series does the same for /dev/mem and UIO, and factors out the logic vfio-pci open-codes so that a third driver does not have to repeat it.
Two things have to be solved to do this outside vfio-pci:
1. remap_pfn_range() reserves the range's memory type in the PAT memtype tree, via pfnmap_track() -> reserve_pfn_range(). The vmf_insert_pfn*() helpers only look the memory type up, they never reserve. A driver that populates lazily therefore loses the conflict detection that rejects a second mapping of the same physical range with a conflicting cache attribute. Patch 1 adds pfn_range_track(), which sets the VMA up as remap_pfn_range() would and reserves the range for the VMA's lifetime, but leaves population to the fault handler. For ->mmap_prepare drivers, where no VMA exists yet, it adds an MMAP_PFN_TRACK action.
2. The order dispatch itself: the window and alignment checks, and the pfn_modify_allowed() check that vmf_insert_pfn_pmd() and vmf_insert_pfn_pud() skip but the order-0 path performs. Patch 2 adds vmf_pfn_order_ok() and vmf_insert_pfn_order(), split in two so a driver can apply its own policy between the check and the insert.
Patches 3 and 4 convert /dev/mem and UIO to use them.
Patch 2 also passes the fault's write flag to vmf_insert_pfn_pmd() and vmf_insert_pfn_pud(), as the DAX and DRM ->huge_fault handlers do, rather than the literal false that vfio-pci passes. With write=false, insert_pmd() leaves a present read-only entry in place and returns VM_FAULT_NOPAGE, and wp_huge_pmd() splits the entry only on VM_FAULT_FALLBACK, so a store to a mapping that mm write-protected for tracking (soft-dirty, uffd-wp, fs writeback) faults forever. vfio-pci has the same problem today and has no ->pfn_mkwrite either; that needs a separate patch, which I have not written.
Testing
=======
Tested on x86_64 at 7.4-rc2, and on arm64 at 7.3.4, which is what the test hardware runs. The four patches are identical in both trees.
On arm64 Cortex-A75: a local selftest over UIO map0, UIO map1 and /dev/mem reports 64 passed, 0 failed and 3 skipped of 67, the skips being the soft-dirty case, which needs CONFIG_MEM_SOFT_DIRTY. tools/testing/selftests/mm/pfnmap.c passes 6/6 on /dev/mem and 6/6 on /dev/uio0. dmesg is free of warnings and call traces. The fault counts confirm the huge path: 4 faults for four PMD windows where a 4K mapping needs 2048, 2 faults for a two-window PMD-offset mapping, 1024 for a 4K-offset mapping of 1024 pages, 2048 for a deliberately misaligned VA, and 0 on a second pass.
x86_64 under QEMU, four kernel configs, with a 4G ivshmem BAR as the test region and a small lab module exposing it through UIO:
THP always, DEBUG_VM=y 30/30 the same plus lockdep 30/30 THP=n 30/30 THP always, MEM_SOFT_DIRTY=y 31/31
The checks cover the PMD and PUD paths and their fallbacks, PAT reservation and the rejection of a conflicting cache attribute over a tracked range, iomem revoke and re-population after release, COW mappings, fork, remote access through /proc/pid/mem, and the UIO entry point. The soft-dirty config adds the write-protected store case; the others report it as a single skip.
On x86, tools/testing/selftests/mm/pfnmap.c passes 6/6 pointed at the UIO device. Reaching the /dev/mem entry point needs STRICT_DEVMEM=n, as the test's System RAM target is otherwise refused, so a fifth config was built with it disabled; it passes 6/6 there too. The suite above is not a useful instrument in that config, four of its checks being STRICT_DEVMEM behaviour that inverts when it is off.
The write flag in patch 2 was confirmed by building the same tree twice and changing nothing else: with the fault's write flag passed, a store to a soft-dirty write-protected PUD and PMD mapping completes; with the literal false restored, both stores time out in a fault loop under a 10s alarm.
Build-only checks, all with KCFLAGS=-Werror W=1:
m68k ColdFire, CONFIG_MMU=n vmlinux links riscv64, THP and UIO enabled mm/ and drivers/uio/ build tools/testing/vma 29/29
riscv64 is the only other architecture selecting HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD, and its defconfig leaves THP and UIO off, so both were turned on for that build.
Two things none of this reaches: 1G PUD mappings anywhere but x86, as the test hardware has no suitable 1G-aligned region, and the huge zero folio fallback in patch 2, which needs STRICT_DEVMEM=n and has no check targeting it.
Performance
===========
Measured on arm64 Cortex-A75 at 7.3.4, 64-byte stride, medians of 5 runs. Three kernels: the base commit, which populates eagerly at mmap() as today; this series; and this series with CONFIG_TRANSPARENT_HUGEPAGE off, which disables ARCH_SUPPORTS_HUGE_PFNMAP and so makes vmf_insert_pfn_order() fall back to order 0.
A 256MB /dev/mem region:
eager 4K series series, 4K mmap() 421,536 ns 4,456 ns 3,912 ns first touch 2.60 ms 2.66 ms 41.42 ms faults 0 128 65536 setup total 3.03 ms 2.66 ms 41.42 ms dTLB read misses 658,700 8,247 660,730 sweep per access 39.01 ns 39.49 ns 39.28 ns
An 8MB UIO region:
eager 4K series series, 4K mmap() 16,048 ns 2,888 ns 2,536 ns first touch 81 us 82 us 1.34 ms faults 0 4 2048 setup total 97 us 85 us 1.34 ms dTLB read misses 20,503 70 20,499 sweep per access 39.12 ns 38.77 ns 39.80 ns
Against today's eager mapping, the series cuts mmap() by 95x on the 256MB region and 5.6x on the 8MB one, as populating the range no longer happens inside the syscall. Total setup, for a consumer that goes on to touch every page, improves by about 12% on both targets: the work moves out of mmap() into the fault handler rather than disappearing. A consumer that touches only part of the range pays only for that part, which this benchmark cannot show because it touches everything.
The dTLB footprint falls about 80x on the 256MB region, and from 20,503 to 70 on the UIO one. That does not become throughput here: the per-access sweep cost is the same in all three kernels, because at 39 ns per 64-byte access the sweep is bound by the device memory path and page-table walks hide underneath it.
The third column is the series with huge entries disabled. Nobody would run it; it is there to separate what huge entries contribute from what fault-driven population contributes, and it is the worst of the three, paying 65536 faults at about 600 ns each.
Touching the same 256MB region in random page order leaves the dTLB counts at 552,176 for the series against 662,577 eager. I do not have an explanation for that: 128 PMD entries should cover the region, so random order was expected to benefit as well.
Eliav Farber (4):
mm: add MMAP_PFN_TRACK action and pfn_range_track() for lazy PFN maps
mm: add vmf_insert_pfn_order() and vmf_pfn_order_ok() helpers mm/char-mem: implement huge_fault support for /dev/mem
uio: implement huge_fault support for physical mappings
drivers/uio/uio.c | 102 +++++++++++++++++++++-- include/linux/mm.h | 47 +++++++++++ include/linux/mm_types.h | 1 + mm/char-mem.c | 103 ++++++++++++++++++++++- mm/internal.h | 3 + mm/memory.c | 142 ++++++++++++++++++++++++++++++++ mm/util.c | 7 ++ tools/testing/vma/include/dup.h | 1 + 8 files changed, 396 insertions(+), 10 deletions(-)
base-commit: 067c5c24cef23d913b67eaaf71ae67e7169a6620
Signature · 1 line
2.47.3
PATCH 1/4 mm: add MMAP_PFN_TRACK action and pfn_range_track() for lazy PFN maps
Drivers that expose a physical PFN range to userspace have traditionally used remap_pfn_range(), which both populates the page tables eagerly and, on x86, reserves the range's memory type in the PAT memtype tree via pfnmap_track() -> reserve_pfn_range() -> memtype_reserve(). That reservation is what lets the kernel detect and reject a second mapping of the same physical range with a conflicting cache attribute (e.g. WB vs UC/WC), which can otherwise cause data corruption or inconsistent cache state.
Drivers that instead populate a PFN mapping lazily from a fault handler (via vmf_insert_pfn()/vmf_insert_pfn_pmd()/vmf_insert_pfn_pud(), e.g. to obtain huge PFN mappings on fault) get no such reservation: those helpers only call pfnmap_setup_cachemode_pfn(), a lookup-only wrapper that never reserves. Such mappings are therefore never registered in the PAT memtype tree and conflicting mappings go undetected.
Add pfn_range_track(): it sets up the VMA exactly as remap_pfn_range() would (VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP flags), reserves and tracks the whole VMA's PFN range for its lifetime by attaching a pfnmap_track_ctx to the VMA (released automatically on VMA teardown), but does not populate any page tables - population is left to the caller's fault handler. COW mappings are rejected, as the fault-driven vmf_insert_pfn*() helpers cannot populate them.
On architectures without PFN tracking (!__HAVE_PFNMAP_TRACKING) the reservation is a no-op, matching remap_pfn_range()'s notrack path; the VMA flag setup still happens.
For drivers that use ->mmap_prepare, where no VMA exists yet, add an MMAP_PFN_TRACK mmap action and a mmap_action_pfn_track() helper. It sets up the VMA like MMAP_REMAP_PFN, and on completion tracks the range like pfn_range_track() instead of populating it. Mirror the new action type in the tools/testing/vma copy of the enum.
--- include/linux/mm.h | 19 +++++++ include/linux/mm_types.h | 1 + mm/internal.h | 3 ++ mm/memory.c | 93 +++++++++++++++++++++++++++++++++ mm/util.c | 7 +++ tools/testing/vma/include/dup.h | 1 + 6 files changed, 124 insertions(+)
include/linux/mm.h +19 -0
@@ -4752,6 +4752,23 @@ 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_pfn_track - 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. +* +* The VMA is set up as for mmap_action_remap_full(), but no page tables are +* populated: the mapping is populated on demand from the driver's fault +* handler. Must not be used for a COW mapping. See pfn_range_track(). +*/ +static inline void mmap_action_pfn_track (struct vm_area_desc *desc, +unsigned long start_pfn) +{ +mmap_action_remap_full (desc, start_pfn); + desc->action.type =MMAP_PFN_TRACK ; +} +/** * 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
@@ -4973,6 +4990,8 @@ 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 pfn_range_track (struct vm_area_struct *vma,unsigned long addr, +unsigned long pfn,unsigned long size);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,
include/linux/mm_types.h +1 -0
@@ -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_PFN_TRACK, /* Track PFN range, map on fault. */ }; enum discontig_kernel_page_action {
mm/internal.h +3 -0
@@ -1546,6 +1546,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 pfn_range_track_prepare (struct vm_area_desc *desc); +int pfn_range_track_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,
mm/memory.c +93 -0
99 lines not on this page · raw message
mm/util.c +7 -0
@@ -1546,6 +1546,8 @@ int mmap_action_prepare(struct vm_area_desc *desc)return 0 ;case MMAP_REMAP_PFN :return remap_pfn_range_prepare (desc); +case MMAP_PFN_TRACK : +return pfn_range_track_prepare (desc);case MMAP_IO_REMAP_PFN :return io_remap_pfn_range_prepare (desc);case MMAP_SIMPLE_IO_REMAP :
@@ -1583,6 +1585,9 @@ int mmap_action_complete(struct vm_area_struct *vma,case MMAP_REMAP_PFN : err =remap_pfn_range_complete (vma, action);break ; +case MMAP_PFN_TRACK : + err =pfn_range_track_complete (vma, action); +break ;case MMAP_KERNEL_PAGES : err =map_kernel_pages_complete (vma, action);break ;
@@ -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_PFN_TRACK :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_PFN_TRACK :WARN_ON_ONCE (1 );/* nommu cannot handle this. */ err = -EINVAL ;
tools/testing/vma/include/dup.h +1 -0
@@ -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_PFN_TRACK, /* Track PFN range, map on fault. */ };/*
Signature · 1 line
2.47.3
PATCH 2/4 mm: add vmf_insert_pfn_order() and vmf_pfn_order_ok() helpers
In reply to Eliav Farber->fault/->huge_fault handlers of VM_PFNMAP mappings that want huge PFN mappings open-code the same steps: check that the order-aligned fault window lies within the VMA and that the pfn is aligned to the order, then call vmf_insert_pfn(), vmf_insert_pfn_pmd() or vmf_insert_pfn_pud() depending on the order, guarded by CONFIG_ARCH_SUPPORTS_{PMD,PUD}_PFNMAP, and fall back to a smaller order otherwise. vfio-pci has its own is_aligned_for_order() and vfio_pci_vmf_insert_pfn() for this.
Add vmf_pfn_order_ok() for the window and alignment check, and vmf_insert_pfn_order() for the order dispatch. The latter also checks pfn_modify_allowed() for huge orders: the order-0 vmf_insert_pfn() does this, but vmf_insert_pfn_pmd() and vmf_insert_pfn_pud() do not. The two are separate so that a driver can apply its own policy (access checks, device locking) between them.
vmf_insert_pfn_order() passes the fault's write flag to vmf_insert_pfn_pmd() and vmf_insert_pfn_pud(), as the DAX and DRM ->huge_fault handlers do. With write=false, insert_pmd() leaves a read-only entry in place and returns VM_FAULT_NOPAGE, so a store to a mapping that mm write-protected for tracking (soft-dirty, uffd-wp) faults forever; wp_huge_pmd() splits the entry only when the handler returns VM_FAULT_FALLBACK. Passing it marks the entry young, dirty and, where the VMA allows, writable, as wp_page_reuse() does at PTE level. The order-0 path needs nothing: wp_pfn_shared() resolves that fault.
The helper also declines the huge zero folio's pfn, which vmf_insert_pfn_pmd() refuses with SIGBUS, so that the fault is retried at PTE order where it maps.
They will be used by the /dev/mem and UIO huge_fault handlers added later in this series.
--- include/linux/mm.h | 28 ++++++++++++++++++++++++++ mm/memory.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+)
include/linux/mm.h +28 -0
@@ -5010,6 +5010,34 @@ 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); + +/** +* vmf_pfn_order_ok - check that a pfn can be mapped 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 (0 for a single page). +* +* For a huge (order > 0) fault, the order-aligned window around @vmf->address +* must lie within the VMA and @pfn must be aligned to @order. @pfn is only +* looked at once the window is known to lie within the VMA, so a caller may +* compute it before calling, even when the window starts below vm_start. +* +* Return: true if vmf_insert_pfn_order() may map @pfn at @order, false if the +* fault should fall back to a smaller order. +*/ +static inline bool vmf_pfn_order_ok (const struct vm_fault *vmf, +unsigned long pfn,unsigned int order) +{ +const struct vm_area_struct *vma = vmf->vma; +unsigned long addr = vmf->address & ~((PAGE_SIZE << order) -1 ); + +return !order || (addr >= vma->vm_start && + addr + (PAGE_SIZE << order) <= vma->vm_end && +IS_ALIGNED (pfn,1UL << 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,
mm/memory.c +49 -0
55 lines not on this page · raw message
Signature · 1 line
2.47.3
PATCH 3/4 mm/char-mem: implement huge_fault support for /dev/mem
In reply to Eliav Farber/dev/mem is commonly used to map large physical regions (reserved memory, device memory) into userspace. mmap_mem_prepare() maps the whole range at mmap() time with 4K PTEs, however large and well aligned it is, which costs many page-table entries and TLB misses.
Map non-COW mappings on fault instead, as vfio-pci does since commit f9e54c3a2f5b ("vfio/pci: implement huge_fault support"): request MMAP_PFN_TRACK rather than an eager remap, so the VMA is set up and its memory type tracked as before, and add ->fault/->huge_fault handlers that map the faulting window with vmf_insert_pfn_order(), using a PMD or PUD where alignment allows. COW mappings cannot be populated by vmf_insert_pfn*() and stay eager; if revoke_iomem() zaps them, the fault handler refuses them with SIGBUS, as before.
The handler re-checks range_is_allowed(): with CONFIG_IO_STRICT_DEVMEM, revoke_iomem() zaps a range once a driver claims it, and a fault must not map it back in. With CONFIG_STRICT_DEVMEM this costs one devmem_is_allowed() call per page of the faulting window. Unlike the eager mapping, a revoke is not permanent: once the driver releases the range, a fault repopulates the mapping, as a fresh mmap() of it would.
generic_access_phys() only follows present PTEs, so .access now faults the page in first; otherwise remote access (ptrace, /proc/pid/mem) to an untouched page would fail. A mapping that does not permit the fault, PROT_NONE for example, keeps that gap: fixup_user_fault() refuses it, where the eager mapping left a present PTE.
Validation at mmap() time is unchanged, except for the L1TF pfn check:
the eager mapping rejected a disallowed pfn with -EAGAIN from mmap(), where the fault handler now raises SIGBUS on first touch.
vmf_insert_pfn() refuses the zero pfn, where remap_pfn_range() mapped it, so a page of RAM that happens to be the zero page now SIGBUSes. That needs /dev/mem access to RAM, i.e. a kernel without CONFIG_STRICT_DEVMEM.
Without huge PFN mappings, or for small or unaligned mappings, the range is populated one page per fault, paid only for pages that are actually touched.
--- mm/char-mem.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 4 deletions(-)
mm/char-mem.c +99 -4
@@ -318,9 +318,92 @@ static inline int private_mapping_ok(struct vm_area_desc *desc) }#endif +#ifdef CONFIG_MMU +/* +* Populate /dev/mem mappings on fault, using a PMD or PUD mapping where the +* architecture, the fault address and the pfn allow it. +*/ +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; + +/* +* COW mappings are mapped eagerly at mmap() time and only lose their +* pages to revoke_iomem(); keep them unmapped, as without a fault +* handler. vmf_insert_pfn*() would also BUG_ON them. +*/ +if (vma_is_cow_mapping (vma)) +return VM_FAULT_SIGBUS ; + +/* +* vm_pgoff is the base pfn of the mapping. vmf_pfn_order_ok() ignores +* pfn if the window starts below vm_start. +*/ + pfn = vma->vm_pgoff + ((addr - vma->vm_start) >>PAGE_SHIFT ); +if (!vmf_pfn_order_ok (vmf, pfn, order)) +return VM_FAULT_FALLBACK ; + +/* +* Access was checked at mmap() time, but with CONFIG_IO_STRICT_DEVMEM +* revoke_iomem() zaps the mapping once a driver claims part of the +* range. Re-check here so a revoked page is not faulted back in. Like +* the mmap()-time check, it is not serialised against revoke_iomem(), +* so a fault that passed it can still install a page revoked right +* after. +*/ +if (!range_is_allowed (pfn,PAGE_SIZE << order)) +return order ?VM_FAULT_FALLBACK :VM_FAULT_SIGBUS ; + +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() only follows present PTEs, and a lazily populated +* mapping has none for pages the target has not touched yet. Fault the page in +* first, so that remote access (ptrace, /proc/pid/mem) keeps working. +* +* fixup_user_fault() refuses a fault the vma does not permit, so an untouched +* page of a mapping without VM_READ stays unreadable, where the eager mapping +* left a present PTE. +*/ +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 ; + +if (write) + fault_flags |=FAULT_FLAG_WRITE ; + +/* +* COW mappings are populated at mmap() time, and a write fault would +* copy the page instead. With unlocked == NULL, fixup_user_fault() +* keeps the mmap lock held and the vma valid. +*/ +if (!vma_is_cow_mapping (vma)) +fixup_user_fault (vma->vm_mm, addr, fault_flags,NULL ); + +return generic_access_phys (vma, addr, buf, len, write); +} +#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 };
@@ -356,11 +439,23 @@ static int mmap_mem_prepare(struct vm_area_desc *desc) desc->page_prot); 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 ; +if (vma_desc_is_cow_mapping (desc)) { +/* +* vmf_insert_pfn*() cannot populate COW mappings: map them now. +* Remap-pfn-range will mark the range with the I/O flag. +*/ +mmap_action_remap_full (desc, desc->pgoff); +return 0 ; + } + +/* +* Populate on fault (see mmap_mem_huge_fault()). Tracking the PFN range +* keeps its memory type reserved, for PAT conflict detection. +*/ +mmap_action_pfn_track (desc, desc->pgoff); +return 0 ; }
Signature · 1 line
2.47.3
PATCH 4/4 uio: implement huge_fault support for physical mappings
In reply to Eliav FarberUIO physical mappings (UIO_MEM_PHYS, UIO_MEM_IOVA) often expose large device memory regions to userspace. uio_mmap_physical() maps them at mmap() time with remap_pfn_range(), i.e. with 4K PTEs, even when the region and the user address are aligned for huge mappings.
Map non-COW mappings on fault instead, as vfio-pci does since commit f9e54c3a2f5b ("vfio/pci: implement huge_fault support"): call pfn_range_track() rather than remap_pfn_range(), so the VMA is set up and its memory type tracked as before, and add ->fault/->huge_fault handlers that map the faulting window with vmf_insert_pfn_order(), using a PMD or PUD where alignment allows. Like uio_vma_fault(), the handlers take idev->info_lock and fail with SIGBUS once the device is gone. COW mappings cannot be populated by vmf_insert_pfn*() and stay eager.
vm_pgoff holds the UIO map index, which a fault handler cannot use: a VMA split (partial munmap(), mprotect(), ...) adds the split offset to vm_pgoff of the upper part, so the index no longer identifies the map. Store the base pfn in vm_pgoff instead, as remap_pfn_range() does for COW mappings; splits then keep it correct. As a side effect, /proc/<pid>/maps shows the physical address as the offset of such a mapping, rather than N * PAGE_SIZE.
generic_access_phys() only follows present PTEs, so .access now faults the page in first; otherwise remote access (ptrace, /proc/pid/mem) to an untouched page would fail. A mapping that does not permit the fault, PROT_NONE for example, keeps that gap: fixup_user_fault() refuses it, where the eager mapping left a present PTE.
Validation at mmap() time is unchanged, except for the L1TF pfn check:remap_pfn_range() rejected a disallowed pfn with -EACCES from mmap(), where the fault handler now raises SIGBUS on first touch.
Once the device is unregistered, a page with no present entry gets SIGBUS, where the eager mapping kept it mapped. Not only untouched
pages: splitting a huge entry with a partial munmap() or mprotect() zaps it, since __split_huge_pmd_locked() clears a PFN-mapped PMD rather than converting it to PTEs.
Without huge PFN mappings, or for small or unaligned mappings, the region is populated one page per fault, paid only for pages that are actually touched.
--- drivers/uio/uio.c | 102 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 96 insertions(+), 6 deletions(-)
drivers/uio/uio.c +96 -6
@@ -733,9 +733,80 @@ static int uio_mmap_logical(struct vm_area_struct *vma)return 0 ; } +/* +* Populate UIO physical mappings on fault, using a PMD or PUD mapping where the +* architecture, the fault address and the pfn allow it. +*/ +static vm_fault_t uio_physical_huge_fault (struct vm_fault *vmf, +unsigned int order) +{ +struct vm_area_struct *vma = vmf->vma; +unsigned long addr = vmf->address & ~((PAGE_SIZE << order) -1 ); +struct uio_device *idev = vma->vm_private_data; +vm_fault_t ret =VM_FAULT_SIGBUS ; +unsigned long pfn; + +/* +* vm_pgoff is the base pfn of the mapping (see uio_mmap_physical()). +* vmf_pfn_order_ok() ignores pfn if the window starts below vm_start. +*/ + pfn = vma->vm_pgoff + ((addr - vma->vm_start) >>PAGE_SHIFT ); +if (!vmf_pfn_order_ok (vmf, pfn, order)) +return VM_FAULT_FALLBACK ; + +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() only follows present PTEs, and a lazily populated +* mapping has none for pages the target has not touched yet. Fault the page in +* first (through the handlers above, so info_lock is honoured), so that remote +* access (ptrace, /proc/pid/mem) keeps working. +* +* fixup_user_fault() refuses a fault the vma does not permit, so an untouched +* page of a mapping without VM_READ stays unreadable, where the eager mapping +* left a present PTE. +*/ +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 ; + +if (write) + fault_flags |=FAULT_FLAG_WRITE ; + +/* +* COW mappings are populated at mmap() time, and a write fault would +* copy the page instead. With unlocked == NULL, fixup_user_fault() +* keeps the mmap lock held and the vma valid. +*/ +if (!vma_is_cow_mapping (vma)) +fixup_user_fault (vma->vm_mm, addr, fault_flags,NULL ); + +return generic_access_phys (vma, addr, buf, len, write); +} +#endif +static const struct vm_operations_struct uio_physical_vm_ops = { #ifdef CONFIG_HAVE_IOREMAP_PROT - .access = generic_access_phys, + .access = uio_physical_access, +#endif + .fault = uio_physical_fault, +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP + .huge_fault = uio_physical_huge_fault, #endif };
@@ -767,11 +838,30 @@ static int uio_mmap_physical(struct vm_area_struct *vma)* So we just do the physical mmap without a page * offset. */ -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)) { +/* COW mappings cannot be populated on fault: map them now. */ +return remap_pfn_range (vma, + vma->vm_start, + mem->addr >>PAGE_SHIFT , + vma->vm_end - vma->vm_start, + vma->vm_page_prot); + } + +/* +* Populate on fault (see uio_physical_huge_fault()). Tracking the PFN +* range keeps its memory type reserved, for PAT conflict detection. +* +* vm_pgoff holds the map index, which the fault handler cannot use: +* splitting the VMA adds the page offset of the split to vm_pgoff of +* the upper part, so uio_find_mem_index() then returns the wrong map, +* or none, and the start of that part would be mapped to mem->addr. +* Store the base pfn instead, as remap_pfn_range() does for COW +* mappings; VMA splits then keep vm_pgoff correct for each part. +*/ + vma->vm_pgoff = mem->addr >>PAGE_SHIFT ; + +return pfn_range_track (vma, vma->vm_start, mem->addr >>PAGE_SHIFT , + vma->vm_end - vma->vm_start); }static int uio_mmap_dma_coherent (struct vm_area_struct *vma)
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 2/4] mm: add vmf_insert_pfn_order() and vmf_pfn_order_ok() helpers
In reply to Eliav FarberThank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] mm: missing cross-granularity…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] mm: missing cross-granularity zeropage checks in vmf_insert_pfn_prot() [1]
--
commit 4d09867f23905da16cf7a825dd8a13e152a14d1e
Author: Eliav Farber <[email protected]>
mm: add vmf_insert_pfn_order() and vmf_pfn_order_ok() helpers
This commit introduces vmf_pfn_order_ok() and vmf_insert_pfn_order() to validate fault windows and PFN alignment and to dispatch PFN insertions across PTE, PMD, and PUD orders for VM_PFNMAP fault handlers.
Quoted 29 lines
mm/memory.c +21
-0@@ -3002,6 +3002,55 @@ 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 of 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 (0 for a single page). +* +* Helper for ->fault and ->huge_fault handlers of VM_PFNMAP mappings: maps @pfn +* with a PTE for order 0, or with a PMD/PUD for PMD_ORDER/PUD_ORDER where the +* architecture supports huge PFN mappings. The caller must have checked the +* window and the pfn alignment with vmf_pfn_order_ok(). +* +* vma cannot be a COW mapping. +* +* Return: vm_fault_t value; %VM_FAULT_FALLBACK if @order cannot be mapped, so +* that the fault is retried at a smaller order. +*/ +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 ;
mm/memory.c vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, · line 3035
Quoted 10 lines
mm/memory.c +10
-0+ +if (!order) +return vmf_insert_pfn (vma, vmf->address, pfn); + +/* +* vmf_insert_pfn_pmd() refuses the huge zero folio's pfn with SIGBUS, +* while a PTE mapping of it is fine, so fall back to a smaller order. +*/ +if (is_huge_zero_pfn (pfn)) +return VM_FAULT_FALLBACK ;
[Severity: Critical]
This problem wasn't introduced by this patch, but can falling back to order-0 here or dispatching to vmf_insert_pfn_pmd() and vmf_insert_pfn_pud() below bypass zeropage checks across granularities [1]?
Because is_huge_zero_pfn() and is_zero_pfn() match mutually exclusive PFNs, when a huge zeropage PFN falls back to vmf_insert_pfn() -> vmf_insert_pfn_prot() (or reaches vm_mixed_ok()), it only checks is_zero_pfn(pfn), allowing the shared huge zeropage to be mapped writable via PTEs and corrupted system-wide. Conversely, vmf_insert_pfn_pmd() and vmf_insert_pfn_pud() do not check is_zero_pfn(pfn) (and vmf_insert_pfn_pud() does not check is_huge_zero_pfn(pfn) either), so mapping the order-0 shared zeropage at PMD or PUD granularity exposes the surrounding physical memory within that PMD/PUD range to userspace.
[1] https://sashiko.dev/#/bug/linux-c77c75ca-4c72-4dae-8d19-e971631bb97c
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH 3/4] mm/char-mem: implement huge_fault support for /dev/mem
In reply to Eliav FarberThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/char-mem: read-only PTE after fixup_user_fault() in mmap_mem_access() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/char-mem: read-only PTE after fixup_user_fault() in mmap_mem_access()
--
commit 2398e62524e61afe6489b128b93a5b0922afcd83
Author: Eliav Farber <[email protected]>
mm/char-mem: implement huge_fault support for /dev/mem
This commit switches non-COW /dev/mem mappings from eager remap at mmap() time to fault-time population using ->fault and ->huge_fault handlers, and adds mmap_mem_access() to fault in untouched pages before calling generic_access_phys().
Quoted 5 lines
mm/char-mem.c +0
-0@@ -318,9 +318,92 @@ static inline int private_mapping_ok(struct vm_area_desc *desc)
[ ... ]
Quoted 19 lines
mm/char-mem.c +19
-0+#ifdef CONFIG_HAVE_IOREMAP_PROT +/* +* generic_access_phys() only follows present PTEs, and a lazily populated +* mapping has none for pages the target has not touched yet. Fault the page in +* first, so that remote access (ptrace, /proc/pid/mem) keeps working. +* +* fixup_user_fault() refuses a fault the vma does not permit, so an untouched +* page of a mapping without VM_READ stays unreadable, where the eager mapping +* left a present PTE. +*/ +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 ; + +if (write) + fault_flags |=FAULT_FLAG_WRITE ; + +/*
mm/char-mem.c static inline int private_mapping_ok(struct vm_area_desc *desc) · line 396
Quoted 10 lines
mm/char-mem.c +10
-0+* COW mappings are populated at mmap() time, and a write fault would +* copy the page instead. With unlocked == NULL, fixup_user_fault() +* keeps the mmap lock held and the vma valid. +*/ +if (!vma_is_cow_mapping (vma)) +fixup_user_fault (vma->vm_mm, addr, fault_flags,NULL ); + + return generic_access_phys(vma, addr, buf, len, write); +} +#endif
[Severity: Medium]
Can a remote write to an untouched order-0 page fail with -EINVAL when soft-dirty tracking is armed on the VMA?
Writing 4 to /proc/pid/clear_refs clears VM_SOFTDIRTY on the VMA and strips write permission from vma->vm_page_prot via vma_set_page_prot(). On an order-0 fault, vmf_insert_pfn_order() calls vmf_insert_pfn(), which calls insert_pfn() with mkwrite == false and installs a read-only PTE:
mm/memory.c:vmf_insert_pfn_order() {
struct vm_area_struct *vma = vmf->vma;
bool write = vmf->flags & FAULT_FLAG_WRITE;
if (!order)
return vmf_insert_pfn(vma, vmf->address, pfn);fixup_user_fault() only invokes handle_mm_fault() once, so it does not take the second write-protection fault (do_wp_page() -> wp_pfn_shared()) that a CPU store would take to make the PTE writable. generic_access_phys() then sees !args.writable with FOLL_WRITE set and returns -EINVAL on the first write attempt.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH 4/4] uio: implement huge_fault support for physical mappings
In reply to Eliav FarberThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] uio: BUG_ON in uio_physical_huge_fault() on COW physical mappings - [Medium] uio: read-only PTE…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] uio: BUG_ON in uio_physical_huge_fault() on COW physical mappings
- [Medium] uio: read-only PTE after fixup_user_fault() in uio_physical_access()
--
commit 64a7c2da3be318c1019224cd530d155344992a33
Author: Eliav Farber <[email protected]>
uio: implement huge_fault support for physical mappings
This commit populates non-COW UIO physical mappings on fault via .fault and .huge_fault handlers using vmf_insert_pfn_order(), while tracking the PFN range with pfn_range_track() and faulting pages in from .access.
Quoted 64 lines
drivers/uio/uio.c +56
-0@@ -733,9 +733,80 @@ static int uio_mmap_logical(struct vm_area_struct *vma)return 0 ; } +/* +* Populate UIO physical mappings on fault, using a PMD or PUD mapping where the +* architecture, the fault address and the pfn allow it. +*/ +static vm_fault_t uio_physical_huge_fault (struct vm_fault *vmf, +unsigned int order) +{ +struct vm_area_struct *vma = vmf->vma; +unsigned long addr = vmf->address & ~((PAGE_SIZE << order) -1 ); +struct uio_device *idev = vma->vm_private_data; +vm_fault_t ret =VM_FAULT_SIGBUS ; +unsigned long pfn; + +/* +* vm_pgoff is the base pfn of the mapping (see uio_mmap_physical()). +* vmf_pfn_order_ok() ignores pfn if the window starts below vm_start. +*/ + pfn = vma->vm_pgoff + ((addr - vma->vm_start) >>PAGE_SHIFT ); +if (!vmf_pfn_order_ok (vmf, pfn, order)) +return VM_FAULT_FALLBACK ; + +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() only follows present PTEs, and a lazily populated +* mapping has none for pages the target has not touched yet. Fault the page in +* first (through the handlers above, so info_lock is honoured), so that remote +* access (ptrace, /proc/pid/mem) keeps working. +* +* fixup_user_fault() refuses a fault the vma does not permit, so an untouched +* page of a mapping without VM_READ stays unreadable, where the eager mapping +* left a present PTE. +*/ +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 ; + +if (write) + fault_flags |=FAULT_FLAG_WRITE ; + +/*
drivers/uio/uio.c static int uio_mmap_logical(struct vm_area_struct *vma) · line 801
Quoted 10 lines
drivers/uio/uio.c +10
-0+* COW mappings are populated at mmap() time, and a write fault would +* copy the page instead. With unlocked == NULL, fixup_user_fault() +* keeps the mmap lock held and the vma valid. +*/ +if (!vma_is_cow_mapping (vma)) +fixup_user_fault (vma->vm_mm, addr, fault_flags,NULL ); + + return generic_access_phys(vma, addr, buf, len, write); +} +#endif
[Severity: Medium]
Can a remote write to an untouched page fail with -EINVAL after soft-dirty bits are cleared via /proc/<pid>/clear_refs?
Writing CLEAR_REFS_SOFT_DIRTY to /proc/<pid>/clear_refs clears VM_SOFTDIRTY on the VMA and calls vma_set_page_prot(), which strips write permission from vma->vm_page_prot so soft-dirty tracking can catch writes. When fixup_user_fault() then faults an unmapped page at order 0, vmf_insert_pfn_order() calls vmf_insert_pfn(), which passes mkwrite = false to insert_pfn() and installs a read-only PTE from vma->vm_page_prot, returning VM_FAULT_NOPAGE without running wp_pfn_shared(). generic_access_phys() then sees !args.writable and rejects the FOLL_WRITE access with -EINVAL.
Quoted 19 lines
drivers/uio/uio.c +6
-4+static const struct vm_operations_struct uio_physical_vm_ops = { #ifdef CONFIG_HAVE_IOREMAP_PROT - .access = generic_access_phys, + .access = uio_physical_access, +#endif + .fault = uio_physical_fault, +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP + .huge_fault = uio_physical_huge_fault, #endif };@@ -767,11 +838,30 @@ static int uio_mmap_physical(struct vm_area_struct *vma)* So we just do the physical mmap without a page * offset. */ - return remap_pfn_range(vma, - vma->vm_start, - mem->addr >> PAGE_SHIFT,
drivers/uio/uio.c static int uio_mmap_physical(struct vm_area_struct *vma) · line 848
Quoted 10 lines
drivers/uio/uio.c +8
-2- vma->vm_end - vma->vm_start, - vma->vm_page_prot); +if (vma_is_cow_mapping (vma)) { +/* COW mappings cannot be populated on fault: map them now. */ +return remap_pfn_range (vma, + vma->vm_start, + mem->addr >>PAGE_SHIFT , + vma->vm_end - vma->vm_start, + vma->vm_page_prot); + }
[Severity: High]
Can installing uio_physical_vm_ops on a COW mapping trigger a kernel BUG_ON() when a concurrent fault observes a transiently cleared PTE?
uio_mmap_physical() sets vma->vm_ops = &uio_physical_vm_ops before checking vma_is_cow_mapping(vma), so a MAP_PRIVATE writable VMA gets .fault and .huge_fault installed even though it is mapped eagerly by remap_pfn_range():
CPU0 CPU1handle_pte_fault()
do_wp_page()
wp_page_copy()
ptep_clear_flush(...)
handle_pte_fault()
orig_pte = ptep_get_lockless(...)
/* sees pte_none(orig_pte) */
do_pte_missing()
do_fault()
do_read_fault()
__do_fault()
uio_physical_fault()
vmf_insert_pfn_order()
vmf_insert_pfn_prot()
BUG_ON(...)
set_pte_at(...)Previously .fault was NULL, so do_fault() took the !vma->vm_ops->fault branch, rechecked the PTE under the page table lock, and returned VM_FAULT_NOPAGE. With .fault set and no vma_is_cow_mapping() check in uio_physical_huge_fault(), do_fault() calls into vmf_insert_pfn_prot() (or the PMD/PUD equivalents), which hits BUG_ON((vma->vm_flags & VM_PFNMAP) && vma_is_cow_mapping(vma)).
Even if uio_physical_huge_fault() checked vma_is_cow_mapping() and returned VM_FAULT_SIGBUS, having .fault set on a COW VMA would still bypass do_fault()'s page table lock recheck and deliver a spurious SIGBUS on transient PTE clearings, and having .huge_fault set makes vma_can_map_huge_file() report
THPeligible: 1 in /proc/<pid>/smaps for PMD-aligned COW mappings. Should COW mappings keep using a vm_ops structure that only defines
.access = generic_access_phys?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
RE: [PATCH 2/4] mm: add vmf_insert_pfn_order() and vmf_pfn_order_ok() helpers
In reply to [email protected]Quoted 5 lines · Sashiko AI review
[Severity: Critical]
This problem wasn't introduced by this patch, but can falling back to order-0 here or dispatching tovmf_insert_pfn_pmd()andvmf_insert_pfn_pud()below bypass zeropage checks across granularities [1]?
Yes, the asymmetry is real: __vmf_insert_pfn_prot() checks is_zero_pfn() but not is_huge_zero_pfn(), vmf_insert_pfn_pmd() the reverse, and vmf_insert_pfn_pud() neither.
I left it alone because this series does not reach it. UIO takes the pfn from mem->addr, so userspace picks a map index rather than an address. /dev/mem does let userspace name the address, but both zero pages are System RAM, which range_is_allowed() refuses under CONFIG_STRICT_DEVMEM. And remap_pfn_range(), which these patches replace, has no zeropage check at all, so the new order-0 path is the stricter of the two.
The vmf_insert_pfn_pud() gap looks worth a patch of its own. If you would rather vmf_insert_pfn_order() declined both at huge order, adding is_zero_pfn() beside the is_huge_zero_pfn() test is a one-liner.
Thanks,
Eliav
RE: [PATCH 3/4] mm/char-mem: implement huge_fault support for /dev/mem
In reply to [email protected]Quoted 3 lines · Sashiko AI review
[Severity: Medium]
Can a remote write to an untouched order-0 page fail with-EINVALwhen soft-dirty tracking is armed on the VMA?
Fixed in v2: vmf_insert_pfn_order() now passes the fault's write flag on the order-0 path too, so the first remote write installs a writable pte rather than relying on a second fault that fixup_user_fault() never takes. vmf_insert_pfn_prot() keeps its signature; its body moved into a static helper taking mkwrite.
I could not reproduce the -EINVAL itself, on /dev/mem or on either UIO memtype, with a test that arms clear_refs 4 and then writes an untouched page through /proc/pid/mem. The commit message therefore presents the change as consistency between the orders rather than a fix for a failure I observed.
Thanks,
Eliav
RE: [PATCH 4/4] uio: implement huge_fault support for physical mappings
In reply to [email protected]Quoted 3 lines · Sashiko AI review
[Severity: High]
Can installinguio_physical_vm_opson a COW mapping trigger a kernelBUG_ON()when a concurrent fault observes a transiently cleared PTE?
[...]
Quoted 2 lines
Should COW mappings keep using a
vm_opsstructure that only defines .access =generic_access_phys?
Yes, and v2 does that. I reproduced the interleaving you describe with eight threads racing a store and a read over 2000 rounds:
kernel BUG at mm/memory.c:2989!
RIP: 0010:vmf_insert_pfn_prot+0xfb/0x100 Comm: pfnmap_cow
after which the guest wedges, since BUG() in a fault path leaves mmap_lock held. /dev/mem had the same bug, so patch 3 gets the same treatment.
Your middle paragraph saved me a wrong fix. A COW test returning VM_FAULT_SIGBUS was my first attempt, and it produces exactly the spurious SIGBUS you predict - on /dev/mem it fired at 200 rounds with four threads, more readily than the BUG_ON. Both drivers now give COW mappings a vm_operations_struct defining only .access, so .fault and .huge_fault are NULL and do_fault() keeps its recheck under the page table lock. THPeligible: 1 on a PMD-aligned COW mapping is gone with it.
Quoted 3 lines
[Severity: Medium]
Can a remote write to an untouched page fail with-EINVALafter soft-dirty bits are cleared via /proc/<pid>/clear_refs?
Fixed in patch 2 for both drivers: vmf_insert_pfn_order() now passes the fault's write flag on the order-0 path too. I could not reproduce the -EINVAL itself - see my reply on 3/4.
Thanks,
Eliav
David Hildenbrand (Arm)
maintainerRe: [PATCH 2/4] mm: add vmf_insert_pfn_order() and vmf_pfn_order_ok() helpers
In reply to Eliav FarberQuoted 63 lines · Eliav Farber
->fault/->huge_fault handlers of
VM_PFNMAPmappings that want huge PFN mappings open-code the same steps: check that the order-aligned fault window lies within the VMA and that the pfn is aligned to the order, then callvmf_insert_pfn(),vmf_insert_pfn_pmd()orvmf_insert_pfn_pud()depending on the order, guarded by CONFIG_ARCH_SUPPORTS_{PMD,PUD}_PFNMAP, and fall back to a smaller order otherwise. vfio-pci has its ownis_aligned_for_order()andvfio_pci_vmf_insert_pfn()for this.Add
vmf_pfn_order_ok()for the window and alignment check, andvmf_insert_pfn_order()for the order dispatch. The latter also checkspfn_modify_allowed()for huge orders: the order-0vmf_insert_pfn()does this, butvmf_insert_pfn_pmd()andvmf_insert_pfn_pud()do not. The two are separate so that a driver can apply its own policy (access checks, device locking) between them.
vmf_insert_pfn_order()passes the fault's write flag tovmf_insert_pfn_pmd()andvmf_insert_pfn_pud(), as the DAX and DRM ->huge_fault handlers do. With write=false,insert_pmd()leaves a read-only entry in place and returnsVM_FAULT_NOPAGE, so a store to a mapping that mm write-protected for tracking (soft-dirty, uffd-wp) faults forever;wp_huge_pmd()splits the entry only when the handler returnsVM_FAULT_FALLBACK. Passing it marks the entry young, dirty and, where the VMA allows, writable, aswp_page_reuse()does at PTE level. The order-0 path needs nothing:wp_pfn_shared()resolves that fault.The helper also declines the huge zero folio's pfn, which
vmf_insert_pfn_pmd()refuses with SIGBUS, so that the fault is retried at PTE order where it maps.They will be used by the
/dev/memand UIOhuge_faulthandlers added later in this series.Signed-off-by Eliav Farber <[email protected]>--- include/linux/mm.h | 28 ++++++++++++++++++++++++++ mm/memory.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+)include/linux/mm.h +16
-0@@ -5010,6 +5010,34 @@ 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); + +/** +* vmf_pfn_order_ok - check that a pfn can be mapped 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 (0 for a single page). +* +* For a huge (order > 0) fault, the order-aligned window around @vmf->address +* must lie within the VMA and @pfn must be aligned to @order. @pfn is only +* looked at once the window is known to lie within the VMA, so a caller may +* compute it before calling, even when the window starts below vm_start. +* +* Return: true if vmf_insert_pfn_order() may map @pfn at @order, false if the +* fault should fall back to a smaller order.
include/linux/mm.h vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, · line 5038
Quoted 10 lines
include/linux/mm.h +10
-0+*/ +static inline bool vmf_pfn_order_ok (const struct vm_fault *vmf, +unsigned long pfn,unsigned int order) +{ +const struct vm_area_struct *vma = vmf->vma; +unsigned long addr = vmf->address & ~((PAGE_SIZE << order) -1 ); + +return !order || (addr >= vma->vm_start && + addr + (PAGE_SIZE << order) <= vma->vm_end && +IS_ALIGNED (pfn,1UL << order));
To make this somewhat consumable for a human,
if (!order)
return true;
return start >= vma->vm_start && end <= vma->vm_end;
And maybe rejecting IS_ALIGNED(pfn, 1UL << order) with WARN_ONCE() as this sounds like the user is doing something stupid?
But looking again, this all looks like sanity checks where the user is just doing something stupid?
Why is that required at all? I'd get rid of this entirely.
Quoted 55 lines
include/linux/mm.h +2
-0+} +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,mm/memory.c +42
-0@@ -3002,6 +3002,55 @@ 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 of 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 (0 for a single page). +* +* Helper for ->fault and ->huge_fault handlers of VM_PFNMAP mappings: maps @pfn +* with a PTE for order 0, or with a PMD/PUD for PMD_ORDER/PUD_ORDER where the +* architecture supports huge PFN mappings. The caller must have checked the +* window and the pfn alignment with vmf_pfn_order_ok(). +* +* vma cannot be a COW mapping. +* +* Return: vm_fault_t value; %VM_FAULT_FALLBACK if @order cannot be mapped, so +* that the fault is retried at a smaller order. +*/ +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 ; + +if (!order) +return vmf_insert_pfn (vma, vmf->address, pfn); + +/* +* vmf_insert_pfn_pmd() refuses the huge zero folio's pfn with SIGBUS, +* while a PTE mapping of it is fine, so fall back to a smaller order. +*/ +if (is_huge_zero_pfn (pfn)) +return VM_FAULT_FALLBACK ; + +/* +* vmf_insert_pfn_pmd()/_pud() don't call pfn_modify_allowed(), unlike +* the order-0 path, so check it explicitly for the L1TF mitigation. +*/ +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); +
mm/memory.c vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, · line 3056
Quoted 10 lines
mm/memory.c +7
-0+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) {
Why do we even care about the order? Why isn't it sufficient that we get called with "insert this pfn range" and we see how to best map it (pfn/pmd/pud)?
Why does the caller even have to care about *how* something is mapped?
Signature · 3 lines
Cheers, David
RE: [PATCH 2/4] mm: add vmf_insert_pfn_order() and vmf_pfn_order_ok() helpers
Quoted 9 lines · David Hildenbrand (Arm)
include/linux/mm.h vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, · line 5038
Quoted 3 lines
include/linux/mm.h +3
-0+return !order || (addr >= vma->vm_start && + addr + (PAGE_SIZE << order) <= vma->vm_end && +IS_ALIGNED (pfn,1UL << order));To make this somewhat consumable for a human,
if (!order)
return true;return start >=
vma->vm_start&& end <=vma->vm_end;
We crossed in flight: v2 went out about six minutes before your mail, so you were reading v1. I posted it to address sashiko-bot's review of v1, which found a write-flag asymmetry in this patch and a COW problem in patches 3 and 4. vmf_pfn_order_ok() is unchanged there, so all three of your comments apply to v2 as posted.
Agreed on the shape. Your version drops the pfn term, which is your next question, so let me answer that and then propose deleting the helper.
Quoted 2 lines
And maybe rejecting
IS_ALIGNED(pfn, 1UL << order)withWARN_ONCE()as this sounds like the user is doing something stupid?
A misaligned pfn is not a caller bug, it is what userspace asked for. For /dev/mem, vm_pgoff is the base pfn, so an mmap() at a page-aligned but not PMD-aligned physical offset gives a pfn that cannot be mapped at PMD order. Falling back to PTE order is the correct answer. The selftest covers this
directly: it maps /dev/mem at offset +4K onto a PMD-aligned VA, so the window term passes and only the pfn term fails, and it asserts both the 4K fallback and that the data read back is the data at that physical address. A WARN there would fire on an ordinary mmap().
Dropping the term is worse than a WARN, because nothing below catches it. x86 pfn_pmd() masks the pfn:
static inline pmd_t pfn_pmd(unsigned long page_nr, pgprot_t pgprot)
{
phys_addr_t pfn = (phys_addr_t)page_nr << PAGE_SHIFT;
pfn ^= protnone_mask(pgprot_val(pgprot));
pfn &= PHYSICAL_PMD_PAGE_MASK;so a misaligned pfn is silently rounded down and the mapping points at the wrong physical memory. pfn_pud() does the same with PHYSICAL_PUD_PAGE_MASK.
Quoted 4 lines
But looking again, this all looks like sanity checks where the user is just doing something stupid?
Why is that required at all? I'd get rid of this entirely.
The check is required on the fault path because mm asks the handler to do it. From __thp_vma_allowable_orders():
/*
* Check alignment for file vma and size for both file and anon vma by
* filtering out the unsuitable orders.
*
* Skip the check for page fault. Huge fault does the check in fault
* handlers.
*/
if (!in_pf) {
thp_vma_suitable_order() is the same check: the order window within the VMA, plus, for a non-anonymous VMA, IS_ALIGNED(vm_start >> PAGE_SHIFT - vm_pgoff, ...), which for a PFNMAP whose vm_pgoff is the base pfn is the pfn alignment. mm's own huge handler honours that contract:do_huge_pmd_anonymous_page() opens with
if (!thp_vma_suitable_order(vma, haddr, PMD_ORDER))
return VM_FAULT_FALLBACK;
and the pfn-mapping handlers each carry a private copy:is_aligned_for_order() in include/linux/vfio_pci_core.h for vfio-pci and nvgrace-gpu, device-dax re-derives it from dev_dax->align, and fs/dax.c has dax_fault_check_fallback(), which tests the PMD colour and both ends of the window.
So I agree with where you are going, just not that it can be deleted. I would rather move both terms into mm than add a seventh copy. insert_pmd() and insert_pud() are the right place: they already reject
addr < vm_start || addr >= vm_end, and they already hold the pfn in fop.pfn. What is missing there is the end of the window and, in the non-folio branch, the alignment. Concretely:
- Add to insert_pmd(), beside the start test:
addr + HPAGE_PMD_SIZE > vma->vm_end, and, in the !fop.is_folio case, !IS_ALIGNED(fop.pfn, HPAGE_PMD_NR), both returning VM_FAULT_FALLBACK. insert_pud() the same with HPAGE_PUD_SIZE and HPAGE_PUD_NR. The alignment test is gated on !fop.is_folio, and the window test is a no-op for the folio callers, since dax_fault_check_fallback() already rejects (pmd_addr + PMD_SIZE) > vm_end before calling.
- Drop vmf_pfn_order_ok() entirely. No caller needs it and no new driver does geometry.
That removes the helper you object to, fixes an unchecked window end that exists today for every implementor, and leaves vmf_insert_pfn_order() as the order dispatch plus the huge-zero and pfn_modify_allowed() cases.
Quoted 4 lines
Why do we even care about the order? Why isn't it sufficient that we get called with "insert this pfn range" and we see how to best map it (pfn/pmd/pud)?
Why does the caller even have to care about *how* something is mapped?
It does not, and it does not choose. mm picks the order and tells the handler which one: __handle_mm_fault() tries create_huge_pud() -> ->huge_fault(vmf, PUD_ORDER), descends to create_huge_pmd() -> ->huge_fault(vmf, PMD_ORDER) on VM_FAULT_FALLBACK, then to handle_pte_fault(). vmf_insert_pfn_order() only dispatches on the level mm reached, which is what vfio_pci_vmf_insert_pfn() already open-codes with the same !order / PMD_ORDER / PUD_ORDER ladder. The patch de-duplicates that; it does not introduce the order-based shape.
Changing the interface to "here is a pfn range, you pick" means changing ->huge_fault itself, for all of its implementors. The folio-backed ones need the order, because they look up a folio of that size. That seems like a bigger discussion than this series, but I am happy to have it.
For VM_PFNMAP specifically there is a narrower version of your idea that does work, and patch 1 is most of it: pfn_range_track() already gives mm the pfn range at mmap() time, so the fault path could insert without calling the driver at all. What stops it is per-fault policy, not page table walking: /dev/mem must re-check range_is_allowed() because revoke_iomem() can zap the mapping after mmap(), UIO must test idev->info under info_lock so a fault racing unbind gets SIGBUS, and vfio-pci holds memory_lock and checks __vfio_pci_memory_enabled(). That is why ->huge_fault exists for these mappings.
Does the insert_pmd()/insert_pud() move look right to you? The missing window-end test is pre-existing, so it could equally go on the huge-pfnmap mm fixes series rather than inside v3. Say which you prefer and I will send it that way.
What changed in this patch between v1 and v2:
https://lore.kernel.org/all/[email protected]/
Only the order-0 path. The sashiko-bot review on this thread observed that vmf_insert_pfn_order() honoured the fault's write flag for PMD and PUD but not for order 0, where the pte comes from vma->vm_page_prot. v2 passes it at order 0 too, through an extracted __vmf_insert_pfn_prot(..., mkwrite), so the v1 sentence you quoted above the hunk, "The order-0 path needs
nothing: wp_pfn_shared() resolves that fault", is gone. I could not reproduce a user-visible failure on either new target: /dev/mem and UIO both modify vm_page_prot, so vma_wants_writenotify() takes its escape. That change therefore rests on consistency with the huge paths, not on a demonstrated bug. It does mean a first write at order 0 no longer calls ->pfn_mkwrite, which the kernel-doc now states. v2's cover letter has the rest of the changelog.
--
Thanks,
Eliav
David Hildenbrand (Arm)
maintainerRe: [PATCH 2/4] mm: add vmf_insert_pfn_order() and vmf_pfn_order_ok() helpers
Quoted 5 lines · Eliav
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 3 lines
include/linux/mm.h +3
-0+return !order || (addr >= vma->vm_start && + addr + (PAGE_SIZE << order) <= vma->vm_end && +IS_ALIGNED (pfn,1UL << order));
Quoted 8 lines
Quoted 5 lines
To make this somewhat consumable for a human,
if (!order)
return true;return start >=
vma->vm_start&& end <=vma->vm_end;We crossed in flight: v2 went out about six minutes before your mail, so you were reading v1. I posted it to address sashiko-bot's review of v1,
First, wait for human review. Always.
Second, read Documentation/process/submitting-patches.rst
"Wait for a minimum of one week before resubmitting or pinging reviewers
- possibly longer during busy times like merge windows."
Because we have much other priorities right now :)
Quoted 122 lines
which found a write-flag asymmetry in this patch and a COW problem in patches 3 and 4.
vmf_pfn_order_ok()is unchanged there, so all three of your comments apply to v2 as posted.Agreed on the shape. Your version drops the pfn term, which is your next question, so let me answer that and then propose deleting the helper.
Quoted 2 lines
And maybe rejecting
IS_ALIGNED(pfn, 1UL << order)withWARN_ONCE()as this sounds like the user is doing something stupid?A misaligned pfn is not a caller bug, it is what userspace asked for. For
/dev/mem,vm_pgoffis the base pfn, so anmmap()at a page-aligned but not PMD-aligned physical offset gives a pfn that cannot be mapped at PMD order. Falling back to PTE order is the correct answer. The selftest covers this
directly: it maps/dev/memat offset +4K onto a PMD-aligned VA, so the window term passes and only the pfn term fails, and it asserts both the 4K fallback and that the data read back is the data at that physical address. A WARN there would fire on an ordinarymmap().Dropping the term is worse than a WARN, because nothing below catches it. x86
pfn_pmd()masks the pfn:static inline pmd_t pfn_pmd(unsigned long page_nr, pgprot_t pgprot) { phys_addr_t pfn = (phys_addr_t)page_nr << PAGE_SHIFT; pfn ^= protnone_mask(pgprot_val(pgprot)); pfn &= PHYSICAL_PMD_PAGE_MASK;so a misaligned pfn is silently rounded down and the mapping points at the wrong physical memory.
pfn_pud()does the same withPHYSICAL_PUD_PAGE_MASK.Quoted 4 lines
But looking again, this all looks like sanity checks where the user is just doing something stupid?
Why is that required at all? I'd get rid of this entirely.
The check is required on the fault path because mm asks the handler to do it. From
__thp_vma_allowable_orders():/*
* Check alignment for file vma and size for both file and anon vma by
* filtering out the unsuitable orders.
*
* Skip the check for page fault. Huge fault does the check in fault
* handlers.
*/
if (!in_pf) {
thp_vma_suitable_order()is the same check: the order window within the VMA, plus, for a non-anonymous VMA,IS_ALIGNED(vm_start >> PAGE_SHIFT - vm_pgoff, ...), which for a PFNMAP whosevm_pgoffis the base pfn is the pfn alignment. mm's own huge handler honours that contract:do_huge_pmd_anonymous_page()opens withif (!thp_vma_suitable_order(vma, haddr,
PMD_ORDER))
returnVM_FAULT_FALLBACK;and the pfn-mapping handlers each carry a private copy:
is_aligned_for_order()ininclude/linux/vfio_pci_core.hfor vfio-pci and nvgrace-gpu, device-dax re-derives it fromdev_dax->align, andfs/dax.chasdax_fault_check_fallback(), which tests the PMD colour and both ends of the window.So I agree with where you are going, just not that it can be deleted. I would rather move both terms into mm than add a seventh copy.
insert_pmd()andinsert_pud()are the right place: they already reject
addr <vm_start|| addr >=vm_end, and they already hold the pfn in fop.pfn. What is missing there is the end of the window and, in the non-folio branch, the alignment. Concretely:- Add to
insert_pmd(), beside the start test:
addr +HPAGE_PMD_SIZE>vma->vm_end, and, in the !fop.is_folio case, !IS_ALIGNED(fop.pfn,HPAGE_PMD_NR), both returningVM_FAULT_FALLBACK.insert_pud()the same withHPAGE_PUD_SIZEandHPAGE_PUD_NR. The alignment test is gated on !fop.is_folio, and the window test is a no-op for the folio callers, sincedax_fault_check_fallback()already rejects (pmd_addr+PMD_SIZE) >vm_endbefore calling.
- Dropvmf_pfn_order_ok()entirely. No caller needs it and no new driver does geometry.That removes the helper you object to, fixes an unchecked window end that exists today for every implementor, and leaves
vmf_insert_pfn_order()as the order dispatch plus the huge-zero andpfn_modify_allowed()cases.Quoted 4 lines
Why do we even care about the order? Why isn't it sufficient that we get called with "insert this pfn range" and we see how to best map it (pfn/pmd/pud)?
Why does the caller even have to care about *how* something is mapped?
It does not, and it does not choose. mm picks the order and tells the handler which one:
__handle_mm_fault()triescreate_huge_pud()-> ->huge_fault(vmf,PUD_ORDER), descends tocreate_huge_pmd()-> ->huge_fault(vmf,PMD_ORDER) onVM_FAULT_FALLBACK, then tohandle_pte_fault().vmf_insert_pfn_order()only dispatches on the level mm reached, which is whatvfio_pci_vmf_insert_pfn()already open-codes with the same !order /PMD_ORDER/PUD_ORDERladder. The patch de-duplicates that; it does not introduce the order-based shape.Changing the interface to "here is a pfn range, you pick" means changing ->huge_fault itself, for all of its implementors. The folio-backed ones need the order, because they look up a folio of that size. That seems like a bigger discussion than this series, but I am happy to have it.
For
VM_PFNMAPspecifically there is a narrower version of your idea that does work, and patch 1 is most of it:pfn_range_track()already gives mm the pfn range atmmap()time, so the fault path could insert without calling the driver at all. What stops it is per-fault policy, not page table walking:/dev/memmust re-checkrange_is_allowed()becauserevoke_iomem()can zap the mapping aftermmap(), UIO must testidev->infounderinfo_lockso a fault racing unbind gets SIGBUS, and vfio-pci holdsmemory_lockand checks__vfio_pci_memory_enabled(). That is why ->huge_fault exists for these mappings.Does the insert_pmd()/insert_pud() move look right to you? The missing window-end test is pre-existing, so it could equally go on the huge-pfnmap mm fixes series rather than inside v3. Say which you prefer and I will send it that way.
What changed in this patch between v1 and v2:
Quoted 12 lines
Only the order-0 path. The sashiko-bot review on this thread observed that
vmf_insert_pfn_order()honoured the fault's write flag for PMD and PUD but not for order 0, where the pte comes fromvma->vm_page_prot. v2 passes it at order 0 too, through an extracted__vmf_insert_pfn_prot(..., mkwrite), so the v1 sentence you quoted above the hunk, "The order-0 path needs
nothing:wp_pfn_shared()resolves that fault", is gone. I could not reproduce a user-visible failure on either new target:/dev/memand UIO both modifyvm_page_prot, sovma_wants_writenotify()takes its escape. That change therefore rests on consistency with the huge paths, not on a demonstrated bug. It does mean a first write at order 0 no longer calls ->pfn_mkwrite, which the kernel-doc now states. v2's cover letter has the rest of the changelog.
Before I reply to anything in here: did an LLM compose most of these walls of text? :)
Signature · 3 lines
Cheers, David
RE: [PATCH 2/4] mm: add vmf_insert_pfn_order() and vmf_pfn_order_ok() helpers
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 2 lines · Eliav
We crossed in flight: v2 went out about six minutes before your mail, so you were reading v1. I posted it to address sashiko-bot's review of v1,
Quoted 8 lines
First, wait for human review. Always.
Second, read
Documentation/process/submitting-patches.rst"Wait for a minimum of one week before resubmitting or pinging reviewers
- possibly longer during busy times like merge windows."Because we have much other priorities right now :)
Understood, sorry about that. I won't post a new version before I get human review, and not before a week has passed.
Quoted 2 lines
Before I reply to anything in here: did an LLM compose most of these walls of text? :)
Yes, partly. I used an LLM while working on this series, to help with the code and to find bugs, mostly for testing. I also used it to write the text, and that is why my replies came out so long. I should have kept them short, sorry.
Thanks,
Eliav
David Hildenbrand (Arm)
maintainerRe: [PATCH 2/4] mm: add vmf_insert_pfn_order() and vmf_pfn_order_ok() helpers
Quoted 17 lines · Eliav
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 2 lines · Eliav
We crossed in flight: v2 went out about six minutes before your mail, so you were reading v1. I posted it to address sashiko-bot's review of v1,
Quoted 8 lines
First, wait for human review. Always.
Second, read
Documentation/process/submitting-patches.rst"Wait for a minimum of one week before resubmitting or pinging reviewers
- possibly longer during busy times like merge windows."Because we have much other priorities right now :)
Understood, sorry about that. I won't post a new version before I get human review, and not before a week has passed.
Quoted 7 lines
Quoted 2 lines
Before I reply to anything in here: did an LLM compose most of these walls of text? :)
Yes, partly. I used an LLM while working on this series, to help with the code and to find bugs, mostly for testing. I also used it to write the text, and that is why my replies came out so long. I should have kept them short, sorry.
I don't want to be angry on a Sunday (I'm not the type of poison), but this grinds my gears. Your commit description is schloppy, your code is schloppy, and -- even worse -- your replies are schloppy.
I'm not interested in getting work-schlopped by people that believe their time is more valuable than mine by feeding me AI schlop on the mailing list.
Please rewrite your answer in a way that doesn't make me hate kernel development.
Signature · 3 lines
Cheers, David
RE: [PATCH 2/4] mm: add vmf_insert_pfn_order() and vmf_pfn_order_ok() helpers
Quoted 2 lines · David Hildenbrand (Arm)
Please rewrite your answer in a way that doesn't make me hate kernel development.
Sorry, I should have written it myself.
Quoted 4 lines
But looking again, this all looks like sanity checks where the user is just doing something stupid?
Why is that required at all? I'd get rid of this entirely.
You were right, the helper is gone.
The checks are not a bug. They are incomplete. insert_pmd() and insert_pud() test the start of the window but not its end. I extended that to both ends, and made it fall back instead of SIGBUS. A VMA that does not span a whole PMD is ordinary. The pfn is not checked at all, so I added that to vmf_insert_pfn_pmd() and vmf_insert_pfn_pud().
No caller can reach either case today, because each one checks the geometry first. The callers of vmf_insert_pfn_order() will not.
Quoted 2 lines
And maybe rejecting
IS_ALIGNED(pfn, 1UL << order)withWARN_ONCE()as this sounds like the user is doing something stupid?
No WARN. An unaligned pfn comes from an ordinary mmap(), not from a bug. For /dev/mem, vm_pgoff is the base pfn. An mmap() at offset 0x1000 gives pfn 1, which no PMD entry can map. Falling back to PTE order is correct, and a WARN would fire on that mmap().
Dropping the check is not safe either. x86 pfn_pmd() masks the pfn with PHYSICAL_PMD_PAGE_MASK. An unaligned pfn is rounded down, and the mapping points at the wrong memory.
This is in v3, which I will post soon.
Thanks,
Eliav