mm, uio: huge PFN mappings for /dev/mem and UIO
Subsystems (6)
v3 cover mm, uio: huge PFN mappings for /dev/mem and UIO
/dev/mem and UIO map large physical regions with 4KiB PTEs, even where a 2MiB or 1GiB entry would do. vfio-pci fixed that in commit f9e54c3a2f5b ("vfio/pci: implement huge_fault support") by populating on fault. This series does the same for /dev/mem and UIO.
Patches 1 and 3 add the two things a driver needs outside vfio-pci: a way to reserve a range's memory type without populating it, and the order dispatch vfio-pci open-codes. Patch 2 fixes a window and pfn check in huge_memory that the new callers reach. Patches 4 and 5 are the users.
Applies on top of Lorenzo Stoakes' fixes for races between huge and non-huge faults in PFN mappings, queued in mm for 7.5:
https://lore.kernel.org/all/[email protected]/
Changes in v3:
- Patch 2 is new: the window and pfn checks moved into mm, where every caller gets them.
- /dev/mem keeps the eager mapping under CONFIG_IO_STRICT_DEVMEM.
- Renamed pfn_range_track() to track_pfn_range(), and dropped its addr and size arguments.
- Shorter commit messages and comments.
Changes in v2:
- vmf_insert_pfn_order() maps a write fault writable at every order. (Sashiko)
- COW mappings get their own vm_operations_struct, instead of a COW test inside the fault handler. (Sashiko)
- Corrected the L1TF claim in the /dev/mem and UIO patches, and dropped the claim that uffd-wp can write-protect such a mapping.
An LLM helped me find corner cases and fix them in the code, write tests and run them under QEMU. The runs on real hardware are mine.
v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
Eliav Farber (5):
mm: add MMAP_TRACK_PFN action and track_pfn_range() for lazy PFN maps mm/huge_memory: check the window and the pfn alignment before
inserting
mm: add vmf_insert_pfn_order()
mm/char-mem: implement huge_fault support for /dev/mem
uio: implement huge_fault support for physical mappings
drivers/uio/uio.c | 99 +++++++++++++++-- include/linux/mm.h | 18 ++++ include/linux/mm_types.h | 1 + mm/char-mem.c | 81 +++++++++++++- mm/huge_memory.c | 18 +++- mm/internal.h | 3 + mm/memory.c | 182 +++++++++++++++++++++++++++----- mm/util.c | 7 ++ tools/testing/vma/include/dup.h | 1 + 9 files changed, 369 insertions(+), 41 deletions(-)
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
Signature · 1 line
2.47.3
PATCH v3 1/5 mm: add MMAP_TRACK_PFN action and track_pfn_range() for lazy PFN maps
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.
---
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(+)
include/linux/mm.h +16 -0
@@ -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,
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_TRACK_PFN, /* Track PFN range, map on fault. */ }; enum discontig_kernel_page_action {
mm/internal.h +3 -0
@@ -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,
mm/memory.c +80 -0
86 lines not on this page · raw message
mm/util.c +7 -0
@@ -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 ;
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_TRACK_PFN, /* Track PFN range, map on fault. */ };/*
Signature · 1 line
2.47.3
PATCH v3 2/5 mm/huge_memory: check the window and the pfn alignment before inserting
In reply to Eliav Farberinsert_pmd() and insert_pud() check that addr is inside the VMA, but not that the whole PMD or PUD is. A VMA ending part-way through one gets mapped past its end. Neither checks the pfn, and pfn_pmd() on x86 rounds an unaligned pfn down, mapping the wrong memory.
No caller can reach either case today; each checks the geometry first. The next patch adds vmf_insert_pfn_order(), whose callers do not.
Check both ends of the window, and the pfn alignment in vmf_insert_pfn_pmd() and vmf_insert_pfn_pud(). Fall back rather than
SIGBUS: a VMA that does not span a whole PMD is ordinary.
Several callers open-code the same test; vfio-pci and nvgrace-gpu share one as is_aligned_for_order() in a vfio-private header. The callers' checks stay: some do more than mm can, as device-dax turns a window failure into SIGBUS and the fsdax check also gates its PMD-order mapping entry.
---
New in v3. In v2 these checks sat in vmf_insert_pfn_order(), where only my own callers got them.
mm/huge_memory.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
mm/huge_memory.c +14 -4
@@ -1630,8 +1630,9 @@ static vm_fault_t insert_pmd(struct vm_area_struct *vma, unsigned long addr,spinlock_t *ptl;pmd_t entry; -if (addr < vma->vm_start || addr >= vma->vm_end) -return VM_FAULT_SIGBUS ; +/* If the whole PMD doesn't fit in the VMA, fall back to 4KiB pages */ +if (addr < vma->vm_start || addr +HPAGE_PMD_SIZE > vma->vm_end) +return VM_FAULT_FALLBACK ;if (arch_needs_pgtable_deposit ()) { pgtable =pte_alloc_one (vma->vm_mm);
@@ -1729,6 +1730,10 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, unsigned long pfn,if (unlikely (is_huge_zero_pfn (pfn)))return VM_FAULT_SIGBUS ; +/* pfn_pmd() would round an unaligned pfn down and map the wrong memory */ +if (!IS_ALIGNED (pfn,HPAGE_PMD_NR )) +return VM_FAULT_FALLBACK ; +pfnmap_setup_cachemode_pfn (pfn, &pgprot);return insert_pmd (vma, addr, vmf->pmd, fop, pgprot, write);
@@ -1768,8 +1773,9 @@ static vm_fault_t insert_pud(struct vm_area_struct *vma, unsigned long addr,spinlock_t *ptl;pud_t entry; -if (addr < vma->vm_start || addr >= vma->vm_end) -return VM_FAULT_SIGBUS ; +/* If the whole PUD doesn't fit in the VMA, fall back to smaller pages */ +if (addr < vma->vm_start || addr +HPAGE_PUD_SIZE > vma->vm_end) +return VM_FAULT_FALLBACK ; ptl =pud_lock (mm, pud);if (!pud_none (*pud)) {
@@ -1842,6 +1848,10 @@ vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, unsigned long pfn, (VM_PFNMAP|VM_MIXEDMAP));BUG_ON ((vma->vm_flags &VM_PFNMAP ) &&vma_is_cow_mapping (vma)); +/* pfn_pud() would round an unaligned pfn down and map the wrong memory */ +if (!IS_ALIGNED (pfn,HPAGE_PUD_NR )) +return VM_FAULT_FALLBACK ; +pfnmap_setup_cachemode_pfn (pfn, &pgprot);return insert_pud (vma, addr, vmf->pud, fop, pgprot, write);
Signature · 1 line
2.47.3
PATCH v3 4/5 mm/char-mem: implement huge_fault support for /dev/mem
In reply to Eliav Farbermmap_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.
---
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(-)
mm/char-mem.c +77 -4
@@ -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 ; }
Signature · 1 line
2.47.3
PATCH v3 3/5 mm: add vmf_insert_pfn_order()
In reply to Eliav Farbermm offers a ->huge_fault handler a PUD first, then a PMD, then ->fault for a PTE. A VM_PFNMAP handler wanting huge mappings therefore dispatches on the order itself, guarding the huge two with CONFIG_ARCH_SUPPORTS_{PMD,PUD}_PFNMAP. vfio-pci open-codes that dispatch.
Add vmf_insert_pfn_order() to do the dispatch instead.
It maps a write fault writable at every order, as the huge DAX and DRM handlers do; vfio-pci passes false. No order-0 PFNMAP insert took a write flag before. vmf_insert_pfn_prot() keeps its signature, with its body moved to a static helper that takes the write flag.
/dev/mem and UIO, later in this series, are the first users.
---
Changes in v3:
- Dropped vmf_pfn_order_ok(). Patch 2 checks the window and the pfn now, so this helper only dispatches on the order.
Changes in v2:
- Pass the write flag at order 0 too. An ->access caller that does not retry the fault was left with a read-only pte.
- vmf_insert_pfn_prot() keeps its signature; its body moved to a static helper that takes the flag.
- Dropped the claim that uffd-wp can write-protect such a mapping.
include/linux/mm.h | 2 + mm/memory.c | 102 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 80 insertions(+), 24 deletions(-)
include/linux/mm.h +2 -0
@@ -5069,6 +5069,8 @@ vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn);vm_fault_t vmf_insert_mixed_mkwrite (struct vm_area_struct *vma,unsigned long addr,unsigned long pfn); +vm_fault_t vmf_insert_pfn_order (struct vm_fault *vmf,unsigned long pfn, +unsigned int order);int vm_iomap_memory (struct vm_area_struct *vma,phys_addr_t start,unsigned long len); static inline vm_fault_t vmf_insert_page(struct vm_area_struct *vma,
mm/memory.c +78 -24
120 lines not on this page · raw message
Signature · 1 line
2.47.3
PATCH v3 5/5 uio: implement huge_fault support for physical mappings
In reply to Eliav Farberuio_mmap_physical() maps UIO_MEM_PHYS and UIO_MEM_IOVA regions at mmap() time with 4KiB PTEs, however large and aligned they are. Populate on fault instead, as vfio-pci does since commit f9e54c3a2f5b ("vfio/pci:
implement huge_fault support"): call track_pfn_range() rather than remap_pfn_range(), and add ->fault and ->huge_fault handlers using vmf_insert_pfn_order(). A large enough aligned region is then mapped with PMD or PUD entries.
Like uio_vma_fault(), both handlers take idev->info_lock and fail with SIGBUS once the device is gone. COW mappings keep the eager remap.
The fault handler needs the base pfn, so vm_pgoff now holds that instead of the map index, as remap_pfn_range() does for COW mappings. /proc/pid/maps reports it as the offset of these mappings.
Once the device is unregistered, an untouched page gets SIGBUS. The eager remap used to keep it mapped. A remote read through ptrace or /proc/pid/mem of a mapping with no VM_READ now fails too, where the eager remap used to leave a present pte.
---
Changes in v3:
- Follow track_pfn_range()'s new signature.
- Return fixup_user_fault()'s error from .access instead of ignoring it.
Changes in v2:
- Give COW mappings their own vm_operations_struct, rather than test for COW inside the fault handler.
- Corrected the L1TF claim in the commit message.
drivers/uio/uio.c | 99 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 90 insertions(+), 9 deletions(-)
drivers/uio/uio.c +90 -9
@@ -733,12 +733,78 @@ static int uio_mmap_logical(struct vm_area_struct *vma)return 0 ; } -static const struct vm_operations_struct uio_physical_vm_ops = { +/* Populate UIO maps on fault, huge where the arch and alignment allow */ +static vm_fault_t uio_physical_huge_fault (struct vm_fault *vmf, +unsigned int order) +{ +unsigned long addr = vmf->address & ~((PAGE_SIZE << order) -1 ); +struct vm_area_struct *vma = vmf->vma; +struct uio_device *idev = vma->vm_private_data; +vm_fault_t ret =VM_FAULT_SIGBUS ; +unsigned long pfn; + +/* An order-aligned addr below the VMA would wrap the subtraction */ +if (addr < vma->vm_start) +return VM_FAULT_FALLBACK ; + +/* vm_pgoff is the base pfn, not the map index (uio_mmap_physical()) */ + pfn = vma->vm_pgoff + ((addr - vma->vm_start) >>PAGE_SHIFT ); + +mutex_lock (&idev->info_lock); + +if (idev->info) + ret =vmf_insert_pfn_order (vmf, pfn, order); + +mutex_unlock (&idev->info_lock); + +return ret; +} + +static vm_fault_t uio_physical_fault (struct vm_fault *vmf) +{ +return uio_physical_huge_fault (vmf,0 ); +} + +#ifdef CONFIG_HAVE_IOREMAP_PROT +/* generic_access_phys() needs a present PTE; a lazy mapping may have none */ +static int uio_physical_access (struct vm_area_struct *vma,unsigned long addr, +void *buf,int len,int write) +{ +unsigned int fault_flags =FAULT_FLAG_REMOTE ; +int ret; + +if (write) + fault_flags |=FAULT_FLAG_WRITE ; + +/* unlocked == NULL keeps the mmap lock held and the vma valid */ + ret =fixup_user_fault (vma->vm_mm, addr, fault_flags,NULL ); +if (ret) +return ret; + +return generic_access_phys (vma, addr, buf, len, write); +} +#endif + +/* +* vm_ops for a COW mapping, which vmf_insert_pfn*() cannot populate. Must +* exist even when empty: a NULL vm_ops would make the VMA anonymous. +*/ +static const struct vm_operations_struct uio_physical_cow_vm_ops = { #ifdef CONFIG_HAVE_IOREMAP_PROT .access = generic_access_phys, #endif }; +static const struct vm_operations_struct uio_physical_vm_ops = { + .fault = uio_physical_fault, +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP + .huge_fault = uio_physical_huge_fault, +#endif +#ifdef CONFIG_HAVE_IOREMAP_PROT + .access = uio_physical_access, +#endif +}; +static int uio_mmap_physical (struct vm_area_struct *vma) {struct uio_device *idev = vma->vm_private_data;
@@ -754,7 +820,6 @@ static int uio_mmap_physical(struct vm_area_struct *vma)if (vma->vm_end - vma->vm_start > mem->size)return -EINVAL ; - vma->vm_ops = &uio_physical_vm_ops;if (idev->info->mem[mi].memtype ==UIO_MEM_PHYS ) vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot);
@@ -764,14 +829,30 @@ static int uio_mmap_physical(struct vm_area_struct *vma)* up above in uio_find_mem_index(), rather than an * actual page offset into the mmap. * -* So we just do the physical mmap without a page -* offset. +* So a COW mapping is remapped below without a page +* offset, and the lazy path overwrites vm_pgoff. */ -return remap_pfn_range (vma, - vma->vm_start, - mem->addr >>PAGE_SHIFT , - vma->vm_end - vma->vm_start, - vma->vm_page_prot); +if (vma_is_cow_mapping (vma)) { + vma->vm_ops = &uio_physical_cow_vm_ops; + +return remap_pfn_range (vma, + vma->vm_start, + mem->addr >>PAGE_SHIFT , + vma->vm_end - vma->vm_start, + vma->vm_page_prot); + } + + vma->vm_ops = &uio_physical_vm_ops; + +/* +* A VMA split adds the split offset to vm_pgoff, so the map index +* stored there would no longer find the map. Store the base pfn +* instead, as remap_pfn_range() does for COW mappings. +*/ + vma->vm_pgoff = mem->addr >>PAGE_SHIFT ; + +/* Tracking reserves the memory type, for PAT conflict detection */ +return track_pfn_range (vma, mem->addr >>PAGE_SHIFT ); }static int uio_mmap_dma_coherent (struct vm_area_struct *vma)
Signature · 1 line
2.47.3
Lorenzo Stoakes (ARM)
maintainerWhat on earth are you doing, Eliav?
There was outstanding review on v2, you replied to it with what looks like an AI-generated response [0], then instantly sent this respin out?...!
Don't send respins unless it is ENTIRELY CLEAR that the feedback is addressed, and that goes double if a maintainer is CLEARLY not happy with your responses, as is patently obvious in this case.
I am inclined to consider your patches as bug reports only moving forwards, honestly. Maybe David has more patience than me on this :)
--
Cheers, Lorenzo
Greg Kroah-Hartman
maintainerRe: [PATCH v3 5/5] uio: implement huge_fault support for physical mappings
In reply to Eliav FarberQuoted 11 lines · Eliav Farber
uio_mmap_physical()mapsUIO_MEM_PHYSandUIO_MEM_IOVAregions atmmap()time with 4KiB PTEs, however large and aligned they are. Populate on fault instead, as vfio-pci does since commitf9e54c3a2f5b("vfio/pci:
implementhuge_faultsupport"): calltrack_pfn_range()rather thanremap_pfn_range(), and add ->fault and ->huge_fault handlers usingvmf_insert_pfn_order(). A large enough aligned region is then mapped with PMD or PUD entries.Like
uio_vma_fault(), both handlers takeidev->info_lockand fail with SIGBUS once the device is gone. COW mappings keep the eager remap.
Quoted 8 lines
The fault handler needs the base pfn, so
vm_pgoffnow holds that instead of the map index, asremap_pfn_range()does for COW mappings./proc/pid/mapsreports it as the offset of these mappings.Once the device is unregistered, an untouched page gets SIGBUS. The eager remap used to keep it mapped. A remote read through ptrace or
/proc/pid/memof a mapping with noVM_READnow fails too, where the eager remap used to leave a present pte.
Why in the world are you using UIO for this? Why not do the right thing and put your driver in the kernel in the kernel itself if it needs to access large mappings like this instead of hacking around the "limitations" of /dev/mem and uio?
Do it right please.
thanks,
greg k-h
Quoted 4 lines · Lorenzo Stoakes (ARM)
What on earth are you doing, Eliav?
There was outstanding review on v2, you replied to it with what looks like an AI-generated response [0], then instantly sent this respin out?...!
I did not mean to annoy you, and I am sorry.
Sending that reply was my normal practice. I answer the open review on the old version first, and send the new version after that.
I sent v3 because I misread what David was asking. On 11/13/26 11:39 he
Quoted 2 lines · wrote:
Before I reply to anything in here: did an LLM compose most of these walls of text? :)
I took that as being about the whole series, not about my replies. He was right about the series: much of the cover letter, the commit messages and the code comments in v1 and v2 was LLM text. I spent the last days going back over all of it and rewriting it, and I thought that was what he was waiting for.
Sorry again for the noise.
Thanks,
Eliav