* [PATCH 0/2] vfio/pci: Provide kernel-side BAR alignment for huge page mapping
@ 2026-11-24 7:30 Guanghui Feng
2026-11-24 7:30 ` [PATCH 1/2] vfio: Add get_unmapped_area callback to vfio_device_ops Guanghui Feng
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Guanghui Feng @ 2026-11-24 7:30 UTC (permalink / raw)
To: alex, dakr, xinmengs, fengchengwen, jgg, kevin.tian, mattev,
alifm, ankita, ritesh.list
Cc: kvm, linux-kernel
VFIO PCI BAR regions support PMD/PUD huge page mapping via
vfio_pci_mmap_huge_fault() since commit f9e54c3a2f5b ("vfio/pci:
implement huge_fault support"). The huge_fault path requires both the
VA and the PFN to be aligned to the huge page boundary, checked by
is_aligned_for_order(). When alignment fails, huge_fault silently
returns VM_FAULT_FALLBACK and the mapping degrades to 4KB pages.
QEMU already handles this by pre-allocating an aligned VA region and
using MAP_FIXED, see QEMU commits 00b519c0bc ("vfio/helpers: Align
mmaps") and 3863e47828 ("hw/vfio: align mmap to power-of-2 of region
size for hugepfnmap"). Other userspace consumers (DPDK, custom VMMs,
test tools, and QEMU releases predating that change) typically call
mmap(fd, offset) with addr=0, relying on the kernel to select a suitable
VA. Without kernel-side alignment guidance, these consumers silently
get 4KB mappings for large BARs.
This series adds a .get_unmapped_area callback to the VFIO device
file_operations, modelled after drivers/dax/device.c, that provides
PMD/PUD-aligned VA selection for BAR mappings. It also sets
VM_HUGEPAGE on BAR VMAs, which keeps the huge mapping available to a
process that called prctl(PR_SET_THP_DISABLE,
PR_THP_DISABLE_EXCEPT_ADVISED).
Note: CONFIG_TRANSPARENT_HUGEPAGE=y is required at compile time for the
huge pfnmap infrastructure. The runtime THP sysfs policy (enabled=never)
does NOT block VFIO BAR huge pages -- vma_file_bypass_thp_tuneables()
already bypasses it for PFNMAP VMAs with a .huge_fault handler (commit
e384abeb559d).
Performance impact for a 256MB BAR, 4KB pages vs PMD huge pages:
- Page faults: 65536 -> 128 (512x reduction)
- Page table memory: ~516KB -> ~4KB (129x reduction)
- TLB pressure: 65536 entries (exceeds STLB) -> 128 entries (fits)
Patch 1 adds the vfio_device_ops callback infrastructure.
Patch 2 implements the PCI BAR alignment logic and VM_HUGEPAGE.
Compiled with W=1, checkpatch --strict clean (0 errors, 0 warnings).
Each patch compiles independently (bisect-safe).
Guanghui Feng (2):
vfio: Add get_unmapped_area callback to vfio_device_ops
vfio/pci: Align BAR mappings for huge page support
drivers/vfio/pci/vfio_pci.c | 3 +
drivers/vfio/pci/vfio_pci_core.c | 95 ++++++++++++++++++++++++++++++++
drivers/vfio/vfio_main.c | 34 ++++++++++++
include/linux/vfio.h | 12 ++++
include/linux/vfio_pci_core.h | 8 +++
5 files changed, 152 insertions(+)
--
2.43.7
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 1/2] vfio: Add get_unmapped_area callback to vfio_device_ops
2026-11-24 7:30 [PATCH 0/2] vfio/pci: Provide kernel-side BAR alignment for huge page mapping Guanghui Feng
@ 2026-11-24 7:30 ` Guanghui Feng
2026-11-24 7:30 ` [PATCH 2/2] vfio/pci: Align BAR mappings for huge page support Guanghui Feng
2026-11-24 12:40 ` [PATCH v2 0/2] vfio/pci: Provide kernel-side BAR alignment for huge page mapping Guanghui Feng
2 siblings, 0 replies; 8+ messages in thread
From: Guanghui Feng @ 2026-11-24 7:30 UTC (permalink / raw)
To: alex, dakr, xinmengs, fengchengwen, jgg, kevin.tian, mattev,
alifm, ankita, ritesh.list
Cc: kvm, linux-kernel
vfio exposes device regions through the device file descriptor and a
bus driver backs a region with pfnmaps over the device MMIO space when
the user calls mmap(2) on it. Unless the user asks for a specific
address, the virtual address of the resulting VMA is picked by the
generic address selection in mm_get_unmapped_area(), which only
guarantees PAGE_SIZE alignment.
That is enough for the base mapping, but it keeps a driver from ever
backing a region with huge pfnmaps. huge_fault() only installs a PMD
or PUD sized pfnmap when both the fault address and the pfn are aligned
to that page size, see is_aligned_for_order(). A driver can only
influence the first of those, and it cannot do so from its mmap()
callback because the address has already been fixed up by then.
Add a get_unmapped_area() callback to vfio_device_ops along with a
dispatcher in vfio_device_fops, so a bus driver can ask for an address
that is aligned to the largest page size its region can be mapped with
before the VMA is created. Drivers that do not provide the callback,
and configurations without huge pfnmap support, fall back to
mm_get_unmapped_area() and keep the existing behaviour.
The hook is only consulted when the kernel has to choose the address:
a MAP_FIXED mapping never reaches it, and an explicit address hint is
passed through to the driver, which is expected to honour it. The
callback therefore serves the userspace consumers that call
mmap(fd, offset) with addr=0 and rely on the kernel to pick a suitable
address for them, rather than trying to second guess userspace that
already arranges the alignment itself.
This mirrors what drivers/dax/device.c does with
dax_get_unmapped_area(), which has to satisfy the same alignment
requirement for its PMD sized pfnmaps.
Signed-off-by: Guanghui Feng <guanghuifeng@linux.alibaba.com>
---
drivers/vfio/vfio_main.c | 34 ++++++++++++++++++++++++++++++++++
include/linux/vfio.h | 12 ++++++++++++
2 files changed, 46 insertions(+)
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 4e5d738ccf4b..edf9eb4cd477 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -28,6 +28,7 @@
#include <linux/pseudo_fs.h>
#include <linux/rwsem.h>
#include <linux/sched.h>
+#include <linux/sched/mm.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/stat.h>
@@ -1431,6 +1432,36 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
return device->ops->mmap(device, vma);
}
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+/*
+ * Give the bus driver a chance to request a virtual address that allows the
+ * mapping to be backed by huge pages, fex. a PCI BAR mapping that could be
+ * faulted in with PMD or PUD sized pfnmaps. Anything the driver cannot
+ * handle, including devices without huge mapping support, falls back to the
+ * generic address selection, which is also what happens without this
+ * callback.
+ */
+static unsigned long vfio_device_fops_get_unmapped_area(struct file *filep,
+ unsigned long addr,
+ unsigned long len,
+ unsigned long pgoff,
+ unsigned long flags)
+{
+ struct vfio_device_file *df = filep->private_data;
+ struct vfio_device *device = df->device;
+
+ /* Paired with smp_store_release() following vfio_df_open() */
+ if (!smp_load_acquire(&df->access_granted))
+ goto out;
+
+ if (device->ops->get_unmapped_area)
+ return device->ops->get_unmapped_area(device, filep, addr, len,
+ pgoff, flags);
+out:
+ return mm_get_unmapped_area(filep, addr, len, pgoff, flags);
+}
+#endif
+
#ifdef CONFIG_PROC_FS
static void vfio_device_show_fdinfo(struct seq_file *m, struct file *filep)
{
@@ -1456,6 +1487,9 @@ const struct file_operations vfio_device_fops = {
.unlocked_ioctl = vfio_device_fops_unl_ioctl,
.compat_ioctl = compat_ptr_ioctl,
.mmap = vfio_device_fops_mmap,
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+ .get_unmapped_area = vfio_device_fops_get_unmapped_area,
+#endif
#ifdef CONFIG_PROC_FS
.show_fdinfo = vfio_device_show_fdinfo,
#endif
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 0cc91c6f96d2..68a84a1fd3fd 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -102,6 +102,12 @@ struct vfio_device {
* @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
* operations documented below
* @mmap: Perform mmap(2) on a region of the device file descriptor
+ * @get_unmapped_area: Optional, suggest a virtual address for mmap(2) of a
+ * device region such that the mapping can be backed by huge pages,
+ * ie. the address is aligned to the largest page size the region can
+ * be mapped with. Drivers that don't support huge mappings, and
+ * configurations without huge pfnmap support, leave this unset and
+ * the generic address selection is used.
* @request: Request for the bus driver to release the device
* @match: Optional device name match callback (return: 0 for no-match, >0 for
* match, -errno for abort (ex. match with insufficient or incorrect
@@ -143,6 +149,12 @@ struct vfio_device_ops {
void (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length);
int (*device_feature)(struct vfio_device *device, u32 flags,
void __user *arg, size_t argsz);
+ unsigned long (*get_unmapped_area)(struct vfio_device *vdev,
+ struct file *filp,
+ unsigned long addr,
+ unsigned long len,
+ unsigned long pgoff,
+ unsigned long flags);
};
#if IS_ENABLED(CONFIG_IOMMUFD)
--
2.43.7
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 2/2] vfio/pci: Align BAR mappings for huge page support
2026-11-24 7:30 [PATCH 0/2] vfio/pci: Provide kernel-side BAR alignment for huge page mapping Guanghui Feng
2026-11-24 7:30 ` [PATCH 1/2] vfio: Add get_unmapped_area callback to vfio_device_ops Guanghui Feng
@ 2026-11-24 7:30 ` Guanghui Feng
2026-11-24 12:40 ` [PATCH v2 0/2] vfio/pci: Provide kernel-side BAR alignment for huge page mapping Guanghui Feng
2 siblings, 0 replies; 8+ messages in thread
From: Guanghui Feng @ 2026-11-24 7:30 UTC (permalink / raw)
To: alex, dakr, xinmengs, fengchengwen, jgg, kevin.tian, mattev,
alifm, ankita, ritesh.list
Cc: kvm, linux-kernel
vfio_pci_mmap_huge_fault() is already wired up as the huge_fault handler
for BAR mappings and can install PMD or PUD sized pfnmaps when
CONFIG_TRANSPARENT_HUGEPAGE is enabled. Whether it ever gets to do so
depends on the address the BAR VMA ended up at: is_aligned_for_order()
requires both the fault address and the pfn to be aligned to the huge
page size, and the generic mm_get_unmapped_area() only guarantees
PAGE_SIZE alignment. When the address is unaligned the check fails,
huge_fault() returns VM_FAULT_FALLBACK without having inserted anything,
and the region silently degrades to one PAGE_SIZE fault per page.
A PCI BAR is required by the PCI bus specification to occupy a naturally
aligned, power of two sized region of physical address space, so the pfn
side of the check is already satisfied for any BAR that is at least 2MB
in size. What is missing is the virtual address side, and that is only
fixable before the VMA is created.
QEMU already works around this in userspace: it reserves an oversized
anonymous region, rounds its address up to an alignment of
MIN(pow2ceil(region->size), 1GiB), unmaps the slack at both ends and
remaps every sparse subregion with MAP_FIXED at the aligned address, see
QEMU commits 00b519c0bc ("vfio/helpers: Align mmaps") and 3863e47828
("hw/vfio: align mmap to power-of-2 of region size for hugepfnmap").
Other consumers do not. DPDK, custom VMMs, test tools and QEMU releases
predating that change typically call mmap(fd, offset) with addr=0 and
leave the address selection to the kernel, so they are the ones that
silently lose the huge mapping on a large BAR. Providing the alignment
in the kernel is defence in depth as well: the kernel should not quietly
give up on a huge mapping it is able to provide just because today's
userspace happens to compensate for its absence.
Implement vfio_pci_core_get_unmapped_area() on top of the new
vfio_device_ops callback, modelled after dax_get_unmapped_area() in
drivers/dax/device.c. It derives the largest usable page size from the
BAR length, capped at PUD_SIZE and at PMD_SIZE when the architecture
lacks CONFIG_ARCH_SUPPORTS_PUD_PFNMAP, asks for an area with enough
slack to shift the mapping up to the next aligned address, and applies
that shift. Shifting the address preserves the offset of the mapping
within the BAR, so aligning the address also aligns the pfn.
Everything this cannot improve keeps the current behaviour. A non-zero
address returns early, which makes the change transparent for userspace
such as QEMU that aligns the mapping itself: MAP_FIXED never reaches the
hook at all, and an explicit hint without MAP_FIXED is honoured as is.
Regions that are not mmap'able BARs, BARs smaller than PMD_SIZE, and
mappings too short to span one fully aligned huge page all take the same
fallback path.
Also set VM_HUGEPAGE on the BAR VMA. The THP sysfs policy does not need
it, since huge pfnmaps are uncompactable and so
vma_file_bypass_thp_tuneables() ignores the tuneables for a PFNMAP VMA
that has a huge_fault handler.
thp_vma_allowable_order() is still consulted before create_huge_pmd() and
create_huge_pud() are reached, though, and it calls vma_thp_disabled(),
which reports the VMA as disabled once the process has called
prctl(PR_SET_THP_DISABLE, PR_THP_DISABLE_EXCEPT_ADVISED) unless
VM_HUGEPAGE is set. Marking the VMA keeps the huge mapping available for
such a process instead of leaving it at the mercy of that single bypass,
and makes the intent explicit regardless of how the process or the
administrator configured THP.
Signed-off-by: Guanghui Feng <guanghuifeng@linux.alibaba.com>
---
drivers/vfio/pci/vfio_pci.c | 3 +
drivers/vfio/pci/vfio_pci_core.c | 95 ++++++++++++++++++++++++++++++++
include/linux/vfio_pci_core.h | 8 +++
3 files changed, 106 insertions(+)
diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index e7c012febe91..2757ff9075d2 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -158,6 +158,9 @@ static const struct vfio_device_ops vfio_pci_ops = {
.read = vfio_pci_core_read,
.write = vfio_pci_core_write,
.mmap = vfio_pci_core_mmap,
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+ .get_unmapped_area = vfio_pci_core_get_unmapped_area,
+#endif
.request = vfio_pci_core_request,
.match = vfio_pci_core_match,
.match_token_uuid = vfio_pci_core_match_token_uuid,
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index fcbb2da860e2..0f465cddd9f5 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -1874,12 +1874,107 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma
*/
vm_flags_set(vma, VM_ALLOW_ANY_UNCACHED | VM_IO | VM_PFNMAP |
VM_DONTEXPAND | VM_DONTDUMP);
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+ vm_flags_set(vma, VM_HUGEPAGE);
+#endif
vma->vm_ops = &vfio_pci_mmap_ops;
return 0;
}
EXPORT_SYMBOL_GPL(vfio_pci_core_mmap);
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+/*
+ * Return an mmap() address for a BAR that is aligned to the largest page
+ * size the BAR can be faulted in with, so that vfio_pci_mmap_huge_fault()
+ * can install PMD or PUD sized pfnmaps rather than silently falling back to
+ * PAGE_SIZE mappings.
+ *
+ * Both ends have to play together for a huge pfnmap: the address of the
+ * mapping has to be aligned such that the whole huge page fits within the
+ * VMA (see is_aligned_for_order() and insert_pmd()), and the pfn has to be
+ * aligned to the huge page size. The latter is a property of the BAR
+ * itself, the former is what we can influence here. The offset of the
+ * mapping within the file is preserved relative to the returned address,
+ * therefore aligning the address also aligns the pfn as long as the mapping
+ * starts at a BAR offset that is itself aligned, which is the common case.
+ *
+ * Anything we can't handle, including a user provided address hint and
+ * regions that are not mmap'able BARs, falls back to the generic address
+ * selection.
+ */
+unsigned long vfio_pci_core_get_unmapped_area(struct vfio_device *core_vdev,
+ struct file *filp,
+ unsigned long addr,
+ unsigned long len,
+ unsigned long pgoff,
+ unsigned long flags)
+{
+ struct vfio_pci_core_device *vdev =
+ container_of(core_vdev, struct vfio_pci_core_device, vdev);
+ struct pci_dev *pdev = vdev->pdev;
+ unsigned long align, off, off_end, off_align, len_align, addr_align;
+ unsigned int index;
+ u64 bar_len;
+
+ /* The user asked for a specific address, don't second guess it */
+ if (addr)
+ goto out;
+
+ index = pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
+ if (index >= VFIO_PCI_ROM_REGION_INDEX || !vdev->bar_mmap_supported[index])
+ goto out;
+
+ bar_len = pci_resource_len(pdev, index);
+ if (!bar_len)
+ goto out;
+
+ /* The BAR size caps the page size we can map it with */
+ align = min_t(unsigned long, PUD_SIZE,
+ rounddown_pow_of_two((unsigned long)bar_len));
+ if (align < PMD_SIZE)
+ goto out;
+ if (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_PUD_PFNMAP) && align > PMD_SIZE)
+ align = PMD_SIZE;
+
+ /* Only an alignment that fits within the mapping length is useful */
+ while (align > PMD_SIZE && len < align)
+ align >>= 1;
+ if (align < PMD_SIZE)
+ goto out;
+
+ off = pgoff << PAGE_SHIFT;
+ off_end = off + len;
+ if (off_end < off)
+ goto out;
+ off_align = round_up(off, align);
+
+ /* The mapping has to span at least one fully aligned huge page */
+ if (off_end <= off_align || off_end - off_align < align)
+ goto out;
+
+ len_align = len + align;
+ if (len_align < len)
+ goto out;
+
+ /*
+ * Find an area that can hold the mapping plus the slack needed to
+ * shift it up to the next aligned address, then apply that shift.
+ * The shift preserves the relative alignment of the file offset,
+ * which is what makes the pfn alignment work out.
+ */
+ addr_align = mm_get_unmapped_area(filp, addr, len_align, pgoff, flags);
+ if (!IS_ERR_VALUE(addr_align)) {
+ addr_align += (off - addr_align) & (align - 1);
+ return addr_align;
+ }
+
+out:
+ return mm_get_unmapped_area(filp, addr, len, pgoff, flags);
+}
+EXPORT_SYMBOL_GPL(vfio_pci_core_get_unmapped_area);
+#endif
+
void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count)
{
struct vfio_pci_core_device *vdev =
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index 2f3fa2787737..98350c87d2ea 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -187,6 +187,14 @@ vm_fault_t vfio_pci_vmf_insert_pfn(struct vfio_pci_core_device *vdev,
struct vm_fault *vmf, unsigned long pfn,
unsigned int order);
int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma);
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+unsigned long vfio_pci_core_get_unmapped_area(struct vfio_device *core_vdev,
+ struct file *filp,
+ unsigned long addr,
+ unsigned long len,
+ unsigned long pgoff,
+ unsigned long flags);
+#endif
void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count);
int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf);
int vfio_pci_core_match_token_uuid(struct vfio_device *core_vdev,
--
2.43.7
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v2 0/2] vfio/pci: Provide kernel-side BAR alignment for huge page mapping
2026-11-24 7:30 [PATCH 0/2] vfio/pci: Provide kernel-side BAR alignment for huge page mapping Guanghui Feng
2026-11-24 7:30 ` [PATCH 1/2] vfio: Add get_unmapped_area callback to vfio_device_ops Guanghui Feng
2026-11-24 7:30 ` [PATCH 2/2] vfio/pci: Align BAR mappings for huge page support Guanghui Feng
@ 2026-11-24 12:40 ` Guanghui Feng
2026-11-24 12:40 ` [PATCH v2 1/2] vfio: Add get_unmapped_area callback to vfio_device_ops Guanghui Feng
2026-11-24 12:40 ` [PATCH v2 2/2] vfio/pci: Align BAR mappings for huge page support Guanghui Feng
2 siblings, 2 replies; 8+ messages in thread
From: Guanghui Feng @ 2026-11-24 12:40 UTC (permalink / raw)
To: guanghuifeng
Cc: alex, alifm, ankita, dakr, fengchengwen, jgg, kevin.tian, kvm,
linux-kernel, mattev, ritesh.list, xinmengs
VFIO PCI BAR regions support PMD/PUD huge page mapping via
vfio_pci_mmap_huge_fault() since commit f9e54c3a2f5b ("vfio/pci:
implement huge_fault support"). The huge_fault path requires both the
VA and the PFN to be aligned to the huge page boundary, checked by
is_aligned_for_order(). When alignment fails, huge_fault silently
returns VM_FAULT_FALLBACK and the mapping degrades to 4KB pages.
QEMU already handles this by pre-allocating an aligned VA region and
using MAP_FIXED, see QEMU commits 00b519c0bc ("vfio/helpers: Align
mmaps") and 3863e47828 ("hw/vfio: align mmap to power-of-2 of region
size for hugepfnmap"). Other userspace consumers (DPDK, custom VMMs,
test tools, and QEMU releases predating that change) typically call
mmap(fd, offset) with addr=0, relying on the kernel to select a suitable
VA. Without kernel-side alignment guidance, these consumers silently
get 4KB mappings for large BARs.
This series adds a .get_unmapped_area callback to the VFIO device
file_operations, modelled after drivers/dax/device.c, that provides
PMD/PUD-aligned VA selection for BAR mappings. It also sets
VM_HUGEPAGE on BAR VMAs, which keeps the huge mapping available to a
process that called prctl(PR_SET_THP_DISABLE,
PR_THP_DISABLE_EXCEPT_ADVISED).
Note: CONFIG_TRANSPARENT_HUGEPAGE=y is required at compile time for the
huge pfnmap infrastructure. The runtime THP sysfs policy (enabled=never)
does NOT block VFIO BAR huge pages -- vma_file_bypass_thp_tuneables()
already bypasses it for PFNMAP VMAs with a .huge_fault handler (commit
e384abeb559d).
Performance impact for a 256MB BAR, 4KB pages vs PMD huge pages:
- Page faults: 65536 -> 128 (512x reduction)
- Page table memory: ~516KB -> ~4KB (129x reduction)
- TLB pressure: 65536 entries (exceeds STLB) -> 128 entries (fits)
Patch 1 adds the vfio_device_ops callback infrastructure.
Patch 2 implements the PCI BAR alignment logic and VM_HUGEPAGE.
Compiled with W=1, checkpatch --strict clean (0 errors, 0 warnings).
Each patch compiles independently (bisect-safe).
---
v2:
- Add MAP_FIXED/MAP_FIXED_NOREPLACE check to avoid interfering with
fixed-address mappings (Sashiko AI review)
- Simplify align selection to only PMD_SIZE or PUD_SIZE, eliminating
intermediate power-of-2 values that cannot be installed by
vfio_pci_vmf_insert_pfn() and could cause alignment failure for
sparse BAR regions (Sashiko AI review)
- Remove unreachable dead code (second align < PMD_SIZE check)
v1: https://lore.kernel.org/all/20261124073048.268988-1-guanghuifeng@linux.alibaba.com/
Guanghui Feng (2):
vfio: Add get_unmapped_area callback to vfio_device_ops
vfio/pci: Align BAR mappings for huge page support
drivers/vfio/pci/vfio_pci.c | 3 +
drivers/vfio/pci/vfio_pci_core.c | 95 ++++++++++++++++++++++++++++++++
drivers/vfio/vfio_main.c | 34 ++++++++++++
include/linux/vfio.h | 12 ++++
include/linux/vfio_pci_core.h | 8 +++
5 files changed, 152 insertions(+)
--
2.43.7
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v2 1/2] vfio: Add get_unmapped_area callback to vfio_device_ops
2026-11-24 12:40 ` [PATCH v2 0/2] vfio/pci: Provide kernel-side BAR alignment for huge page mapping Guanghui Feng
@ 2026-11-24 12:40 ` Guanghui Feng
2026-11-24 12:40 ` [PATCH v2 2/2] vfio/pci: Align BAR mappings for huge page support Guanghui Feng
1 sibling, 0 replies; 8+ messages in thread
From: Guanghui Feng @ 2026-11-24 12:40 UTC (permalink / raw)
To: guanghuifeng
Cc: alex, alifm, ankita, dakr, fengchengwen, jgg, kevin.tian, kvm,
linux-kernel, mattev, ritesh.list, xinmengs
vfio exposes device regions through the device file descriptor and a
bus driver backs a region with pfnmaps over the device MMIO space when
the user calls mmap(2) on it. Unless the user asks for a specific
address, the virtual address of the resulting VMA is picked by the
generic address selection in mm_get_unmapped_area(), which only
guarantees PAGE_SIZE alignment.
That is enough for the base mapping, but it keeps a driver from ever
backing a region with huge pfnmaps. huge_fault() only installs a PMD
or PUD sized pfnmap when both the fault address and the pfn are aligned
to that page size, see is_aligned_for_order(). A driver can only
influence the first of those, and it cannot do so from its mmap()
callback because the address has already been fixed up by then.
Add a get_unmapped_area() callback to vfio_device_ops along with a
dispatcher in vfio_device_fops, so a bus driver can ask for an address
that is aligned to the largest page size its region can be mapped with
before the VMA is created. Drivers that do not provide the callback,
and configurations without huge pfnmap support, fall back to
mm_get_unmapped_area() and keep the existing behaviour.
The hook is only consulted when the kernel has to choose the address:
a MAP_FIXED mapping never reaches it, and an explicit address hint is
passed through to the driver, which is expected to honour it. The
callback therefore serves the userspace consumers that call
mmap(fd, offset) with addr=0 and rely on the kernel to pick a suitable
address for them, rather than trying to second guess userspace that
already arranges the alignment itself.
This mirrors what drivers/dax/device.c does with
dax_get_unmapped_area(), which has to satisfy the same alignment
requirement for its PMD sized pfnmaps.
Signed-off-by: Guanghui Feng <guanghuifeng@linux.alibaba.com>
---
drivers/vfio/vfio_main.c | 34 ++++++++++++++++++++++++++++++++++
include/linux/vfio.h | 12 ++++++++++++
2 files changed, 46 insertions(+)
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 4e5d738ccf4b..edf9eb4cd477 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -28,6 +28,7 @@
#include <linux/pseudo_fs.h>
#include <linux/rwsem.h>
#include <linux/sched.h>
+#include <linux/sched/mm.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/stat.h>
@@ -1431,6 +1432,36 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
return device->ops->mmap(device, vma);
}
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+/*
+ * Give the bus driver a chance to request a virtual address that allows the
+ * mapping to be backed by huge pages, fex. a PCI BAR mapping that could be
+ * faulted in with PMD or PUD sized pfnmaps. Anything the driver cannot
+ * handle, including devices without huge mapping support, falls back to the
+ * generic address selection, which is also what happens without this
+ * callback.
+ */
+static unsigned long vfio_device_fops_get_unmapped_area(struct file *filep,
+ unsigned long addr,
+ unsigned long len,
+ unsigned long pgoff,
+ unsigned long flags)
+{
+ struct vfio_device_file *df = filep->private_data;
+ struct vfio_device *device = df->device;
+
+ /* Paired with smp_store_release() following vfio_df_open() */
+ if (!smp_load_acquire(&df->access_granted))
+ goto out;
+
+ if (device->ops->get_unmapped_area)
+ return device->ops->get_unmapped_area(device, filep, addr, len,
+ pgoff, flags);
+out:
+ return mm_get_unmapped_area(filep, addr, len, pgoff, flags);
+}
+#endif
+
#ifdef CONFIG_PROC_FS
static void vfio_device_show_fdinfo(struct seq_file *m, struct file *filep)
{
@@ -1456,6 +1487,9 @@ const struct file_operations vfio_device_fops = {
.unlocked_ioctl = vfio_device_fops_unl_ioctl,
.compat_ioctl = compat_ptr_ioctl,
.mmap = vfio_device_fops_mmap,
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+ .get_unmapped_area = vfio_device_fops_get_unmapped_area,
+#endif
#ifdef CONFIG_PROC_FS
.show_fdinfo = vfio_device_show_fdinfo,
#endif
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 0cc91c6f96d2..68a84a1fd3fd 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -102,6 +102,12 @@ struct vfio_device {
* @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
* operations documented below
* @mmap: Perform mmap(2) on a region of the device file descriptor
+ * @get_unmapped_area: Optional, suggest a virtual address for mmap(2) of a
+ * device region such that the mapping can be backed by huge pages,
+ * ie. the address is aligned to the largest page size the region can
+ * be mapped with. Drivers that don't support huge mappings, and
+ * configurations without huge pfnmap support, leave this unset and
+ * the generic address selection is used.
* @request: Request for the bus driver to release the device
* @match: Optional device name match callback (return: 0 for no-match, >0 for
* match, -errno for abort (ex. match with insufficient or incorrect
@@ -143,6 +149,12 @@ struct vfio_device_ops {
void (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length);
int (*device_feature)(struct vfio_device *device, u32 flags,
void __user *arg, size_t argsz);
+ unsigned long (*get_unmapped_area)(struct vfio_device *vdev,
+ struct file *filp,
+ unsigned long addr,
+ unsigned long len,
+ unsigned long pgoff,
+ unsigned long flags);
};
#if IS_ENABLED(CONFIG_IOMMUFD)
--
2.43.7
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v2 2/2] vfio/pci: Align BAR mappings for huge page support
2026-11-24 12:40 ` [PATCH v2 0/2] vfio/pci: Provide kernel-side BAR alignment for huge page mapping Guanghui Feng
2026-11-24 12:40 ` [PATCH v2 1/2] vfio: Add get_unmapped_area callback to vfio_device_ops Guanghui Feng
@ 2026-11-24 12:40 ` Guanghui Feng
2026-11-24 13:52 ` Jason Gunthorpe
1 sibling, 1 reply; 8+ messages in thread
From: Guanghui Feng @ 2026-11-24 12:40 UTC (permalink / raw)
To: guanghuifeng
Cc: alex, alifm, ankita, dakr, fengchengwen, jgg, kevin.tian, kvm,
linux-kernel, mattev, ritesh.list, xinmengs
vfio_pci_mmap_huge_fault() is already wired up as the huge_fault handler
for BAR mappings and can install PMD or PUD sized pfnmaps when
CONFIG_TRANSPARENT_HUGEPAGE is enabled. Whether it ever gets to do so
depends on the address the BAR VMA ended up at: is_aligned_for_order()
requires both the fault address and the pfn to be aligned to the huge
page size, and the generic mm_get_unmapped_area() only guarantees
PAGE_SIZE alignment. When the address is unaligned the check fails,
huge_fault() returns VM_FAULT_FALLBACK without having inserted anything,
and the region silently degrades to one PAGE_SIZE fault per page.
A PCI BAR is required by the PCI bus specification to occupy a naturally
aligned, power of two sized region of physical address space, so the pfn
side of the check is already satisfied for any BAR that is at least 2MB
in size. What is missing is the virtual address side, and that is only
fixable before the VMA is created.
QEMU already works around this in userspace: it reserves an oversized
anonymous region, rounds its address up to an alignment of
MIN(pow2ceil(region->size), 1GiB), unmaps the slack at both ends and
remaps every sparse subregion with MAP_FIXED at the aligned address, see
QEMU commits 00b519c0bc ("vfio/helpers: Align mmaps") and 3863e47828
("hw/vfio: align mmap to power-of-2 of region size for hugepfnmap").
Other consumers do not. DPDK, custom VMMs, test tools and QEMU releases
predating that change typically call mmap(fd, offset) with addr=0 and
leave the address selection to the kernel, so they are the ones that
silently lose the huge mapping on a large BAR. Providing the alignment
in the kernel is defence in depth as well: the kernel should not quietly
give up on a huge mapping it is able to provide just because today's
userspace happens to compensate for its absence.
Implement vfio_pci_core_get_unmapped_area() on top of the new
vfio_device_ops callback, modelled after dax_get_unmapped_area() in
drivers/dax/device.c. It derives the largest usable page size from the
BAR length, capped at PUD_SIZE and at PMD_SIZE when the architecture
lacks CONFIG_ARCH_SUPPORTS_PUD_PFNMAP, asks for an area with enough
slack to shift the mapping up to the next aligned address, and applies
that shift. Shifting the address preserves the offset of the mapping
within the BAR, so aligning the address also aligns the pfn.
Everything this cannot improve keeps the current behaviour. A non-zero
address returns early, which makes the change transparent for userspace
such as QEMU that aligns the mapping itself: MAP_FIXED never reaches the
hook at all, and an explicit hint without MAP_FIXED is honoured as is.
Regions that are not mmap'able BARs, BARs smaller than PMD_SIZE, and
mappings too short to span one fully aligned huge page all take the same
fallback path.
Also set VM_HUGEPAGE on the BAR VMA. The THP sysfs policy does not need
it, since huge pfnmaps are uncompactable and so
vma_file_bypass_thp_tuneables() ignores the tuneables for a PFNMAP VMA
that has a huge_fault handler.
thp_vma_allowable_order() is still consulted before create_huge_pmd() and
create_huge_pud() are reached, though, and it calls vma_thp_disabled(),
which reports the VMA as disabled once the process has called
prctl(PR_SET_THP_DISABLE, PR_THP_DISABLE_EXCEPT_ADVISED) unless
VM_HUGEPAGE is set. Marking the VMA keeps the huge mapping available for
such a process instead of leaving it at the mercy of that single bypass,
and makes the intent explicit regardless of how the process or the
administrator configured THP.
Signed-off-by: Guanghui Feng <guanghuifeng@linux.alibaba.com>
---
drivers/vfio/pci/vfio_pci.c | 3 +
drivers/vfio/pci/vfio_pci_core.c | 95 ++++++++++++++++++++++++++++++++
include/linux/vfio_pci_core.h | 8 +++
3 files changed, 106 insertions(+)
diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index e7c012febe91..2757ff9075d2 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -158,6 +158,9 @@ static const struct vfio_device_ops vfio_pci_ops = {
.read = vfio_pci_core_read,
.write = vfio_pci_core_write,
.mmap = vfio_pci_core_mmap,
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+ .get_unmapped_area = vfio_pci_core_get_unmapped_area,
+#endif
.request = vfio_pci_core_request,
.match = vfio_pci_core_match,
.match_token_uuid = vfio_pci_core_match_token_uuid,
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index fcbb2da860e2..0d116dc39f24 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -17,6 +17,7 @@
#include <linux/file.h>
#include <linux/interrupt.h>
#include <linux/iommu.h>
+#include <linux/mman.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/notifier.h>
@@ -1874,12 +1875,106 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma
*/
vm_flags_set(vma, VM_ALLOW_ANY_UNCACHED | VM_IO | VM_PFNMAP |
VM_DONTEXPAND | VM_DONTDUMP);
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+ vm_flags_set(vma, VM_HUGEPAGE);
+#endif
vma->vm_ops = &vfio_pci_mmap_ops;
return 0;
}
EXPORT_SYMBOL_GPL(vfio_pci_core_mmap);
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+/*
+ * Return an mmap() address for a BAR that is aligned to the largest page
+ * size the BAR can be faulted in with, so that vfio_pci_mmap_huge_fault()
+ * can install PMD or PUD sized pfnmaps rather than silently falling back to
+ * PAGE_SIZE mappings.
+ *
+ * Both ends have to play together for a huge pfnmap: the address of the
+ * mapping has to be aligned such that the whole huge page fits within the
+ * VMA (see is_aligned_for_order() and insert_pmd()), and the pfn has to be
+ * aligned to the huge page size. The latter is a property of the BAR
+ * itself, the former is what we can influence here. The offset of the
+ * mapping within the file is preserved relative to the returned address,
+ * therefore aligning the address also aligns the pfn as long as the mapping
+ * starts at a BAR offset that is itself aligned, which is the common case.
+ *
+ * Anything we can't handle, including a user provided address hint and
+ * regions that are not mmap'able BARs, falls back to the generic address
+ * selection.
+ */
+unsigned long vfio_pci_core_get_unmapped_area(struct vfio_device *core_vdev,
+ struct file *filp,
+ unsigned long addr,
+ unsigned long len,
+ unsigned long pgoff,
+ unsigned long flags)
+{
+ struct vfio_pci_core_device *vdev =
+ container_of(core_vdev, struct vfio_pci_core_device, vdev);
+ struct pci_dev *pdev = vdev->pdev;
+ unsigned long align, off, off_end, off_align, len_align, addr_align;
+ unsigned int index;
+ u64 bar_len;
+
+ /* Don't interfere with explicit address requests or fixed mappings */
+ if (addr || (flags & (MAP_FIXED | MAP_FIXED_NOREPLACE)))
+ goto out;
+
+ index = pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
+ if (index >= VFIO_PCI_ROM_REGION_INDEX || !vdev->bar_mmap_supported[index])
+ goto out;
+
+ bar_len = pci_resource_len(pdev, index);
+ if (!bar_len)
+ goto out;
+
+ /*
+ * Select the largest huge page size that the BAR physical alignment,
+ * mapping length, and arch support can all satisfy. Only PMD and PUD
+ * orders are supported by vfio_pci_vmf_insert_pfn().
+ */
+ if (IS_ENABLED(CONFIG_ARCH_SUPPORTS_PUD_PFNMAP) &&
+ bar_len >= PUD_SIZE && len >= PUD_SIZE)
+ align = PUD_SIZE;
+ else if (bar_len >= PMD_SIZE && len >= PMD_SIZE)
+ align = PMD_SIZE;
+ else
+ goto out;
+
+ off = pgoff << PAGE_SHIFT;
+ off_end = off + len;
+ if (off_end < off)
+ goto out;
+ off_align = round_up(off, align);
+
+ /* The mapping has to span at least one fully aligned huge page */
+ if (off_end <= off_align || off_end - off_align < align)
+ goto out;
+
+ len_align = len + align;
+ if (len_align < len)
+ goto out;
+
+ /*
+ * Find an area that can hold the mapping plus the slack needed to
+ * shift it up to the next aligned address, then apply that shift.
+ * The shift preserves the relative alignment of the file offset,
+ * which is what makes the pfn alignment work out.
+ */
+ addr_align = mm_get_unmapped_area(filp, addr, len_align, pgoff, flags);
+ if (!IS_ERR_VALUE(addr_align)) {
+ addr_align += (off - addr_align) & (align - 1);
+ return addr_align;
+ }
+
+out:
+ return mm_get_unmapped_area(filp, addr, len, pgoff, flags);
+}
+EXPORT_SYMBOL_GPL(vfio_pci_core_get_unmapped_area);
+#endif
+
void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count)
{
struct vfio_pci_core_device *vdev =
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index 2f3fa2787737..98350c87d2ea 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -187,6 +187,14 @@ vm_fault_t vfio_pci_vmf_insert_pfn(struct vfio_pci_core_device *vdev,
struct vm_fault *vmf, unsigned long pfn,
unsigned int order);
int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma);
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+unsigned long vfio_pci_core_get_unmapped_area(struct vfio_device *core_vdev,
+ struct file *filp,
+ unsigned long addr,
+ unsigned long len,
+ unsigned long pgoff,
+ unsigned long flags);
+#endif
void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count);
int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf);
int vfio_pci_core_match_token_uuid(struct vfio_device *core_vdev,
--
2.43.7
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 2/2] vfio/pci: Align BAR mappings for huge page support
2026-11-24 12:40 ` [PATCH v2 2/2] vfio/pci: Align BAR mappings for huge page support Guanghui Feng
@ 2026-11-24 13:52 ` Jason Gunthorpe
2026-11-24 17:25 ` Lorenzo Stoakes (ARM)
0 siblings, 1 reply; 8+ messages in thread
From: Jason Gunthorpe @ 2026-11-24 13:52 UTC (permalink / raw)
To: Guanghui Feng, linux-mm, Lorenzo Stoakes
Cc: alex, alifm, ankita, dakr, fengchengwen, kevin.tian, kvm,
linux-kernel, mattev, ritesh.list, xinmengs
On Tue, Nov 24, 2026 at 08:40:04PM +0800, Guanghui Feng wrote:
> +unsigned long vfio_pci_core_get_unmapped_area(struct vfio_device *core_vdev,
> + struct file *filp,
> + unsigned long addr,
> + unsigned long len,
> + unsigned long pgoff,
> + unsigned long flags)
> +{
> + struct vfio_pci_core_device *vdev =
> + container_of(core_vdev, struct vfio_pci_core_device, vdev);
> + struct pci_dev *pdev = vdev->pdev;
> + unsigned long align, off, off_end, off_align, len_align, addr_align;
> + unsigned int index;
> + u64 bar_len;
> +
> + /* Don't interfere with explicit address requests or fixed mappings */
> + if (addr || (flags & (MAP_FIXED | MAP_FIXED_NOREPLACE)))
> + goto out;
> +
> + index = pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
> + if (index >= VFIO_PCI_ROM_REGION_INDEX || !vdev->bar_mmap_supported[index])
> + goto out;
> +
> + bar_len = pci_resource_len(pdev, index);
> + if (!bar_len)
> + goto out;
> +
> + /*
> + * Select the largest huge page size that the BAR physical alignment,
> + * mapping length, and arch support can all satisfy. Only PMD and PUD
> + * orders are supported by vfio_pci_vmf_insert_pfn().
> + */
> + if (IS_ENABLED(CONFIG_ARCH_SUPPORTS_PUD_PFNMAP) &&
> + bar_len >= PUD_SIZE && len >= PUD_SIZE)
> + align = PUD_SIZE;
> + else if (bar_len >= PMD_SIZE && len >= PMD_SIZE)
> + align = PMD_SIZE;
> + else
> + goto out;
> +
> + off = pgoff << PAGE_SHIFT;
> + off_end = off + len;
> + if (off_end < off)
> + goto out;
> + off_align = round_up(off, align);
> +
> + /* The mapping has to span at least one fully aligned huge page */
> + if (off_end <= off_align || off_end - off_align < align)
> + goto out;
> +
> + len_align = len + align;
> + if (len_align < len)
> + goto out;
> +
> + /*
> + * Find an area that can hold the mapping plus the slack needed to
> + * shift it up to the next aligned address, then apply that shift.
> + * The shift preserves the relative alignment of the file offset,
> + * which is what makes the pfn alignment work out.
> + */
> + addr_align = mm_get_unmapped_area(filp, addr, len_align, pgoff, flags);
> + if (!IS_ERR_VALUE(addr_align)) {
> + addr_align += (off - addr_align) & (align - 1);
> + return addr_align;
PeterX was working on this previously and last time I was not excited
to see all this mm logic open coded in drivers.
I was hoping to see that the core mm could provide some helper scheme
for drivers that already know what they want to map to get a vma
alignment that is optimal for the architecutre.
Peter had a design that seemed like a good exploration at least..
Jason
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 2/2] vfio/pci: Align BAR mappings for huge page support
2026-11-24 13:52 ` Jason Gunthorpe
@ 2026-11-24 17:25 ` Lorenzo Stoakes (ARM)
0 siblings, 0 replies; 8+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-24 17:25 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: Guanghui Feng, linux-mm, alex, alifm, ankita, dakr, fengchengwen,
kevin.tian, kvm, linux-kernel, mattev, ritesh.list, xinmengs
On Tue, Nov 24, 2026 at 09:52:58AM -0400, Jason Gunthorpe wrote:
> On Tue, Nov 24, 2026 at 08:40:04PM +0800, Guanghui Feng wrote:
> > +unsigned long vfio_pci_core_get_unmapped_area(struct vfio_device *core_vdev,
> > + struct file *filp,
> > + unsigned long addr,
> > + unsigned long len,
> > + unsigned long pgoff,
> > + unsigned long flags)
> > +{
> > + struct vfio_pci_core_device *vdev =
> > + container_of(core_vdev, struct vfio_pci_core_device, vdev);
> > + struct pci_dev *pdev = vdev->pdev;
> > + unsigned long align, off, off_end, off_align, len_align, addr_align;
> > + unsigned int index;
> > + u64 bar_len;
> > +
> > + /* Don't interfere with explicit address requests or fixed mappings */
> > + if (addr || (flags & (MAP_FIXED | MAP_FIXED_NOREPLACE)))
> > + goto out;
> > +
> > + index = pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
> > + if (index >= VFIO_PCI_ROM_REGION_INDEX || !vdev->bar_mmap_supported[index])
> > + goto out;
> > +
> > + bar_len = pci_resource_len(pdev, index);
> > + if (!bar_len)
> > + goto out;
> > +
> > + /*
> > + * Select the largest huge page size that the BAR physical alignment,
> > + * mapping length, and arch support can all satisfy. Only PMD and PUD
> > + * orders are supported by vfio_pci_vmf_insert_pfn().
> > + */
> > + if (IS_ENABLED(CONFIG_ARCH_SUPPORTS_PUD_PFNMAP) &&
> > + bar_len >= PUD_SIZE && len >= PUD_SIZE)
> > + align = PUD_SIZE;
> > + else if (bar_len >= PMD_SIZE && len >= PMD_SIZE)
> > + align = PMD_SIZE;
> > + else
> > + goto out;
> > +
> > + off = pgoff << PAGE_SHIFT;
> > + off_end = off + len;
> > + if (off_end < off)
> > + goto out;
> > + off_align = round_up(off, align);
> > +
> > + /* The mapping has to span at least one fully aligned huge page */
> > + if (off_end <= off_align || off_end - off_align < align)
> > + goto out;
> > +
> > + len_align = len + align;
> > + if (len_align < len)
> > + goto out;
> > +
> > + /*
> > + * Find an area that can hold the mapping plus the slack needed to
> > + * shift it up to the next aligned address, then apply that shift.
> > + * The shift preserves the relative alignment of the file offset,
> > + * which is what makes the pfn alignment work out.
> > + */
> > + addr_align = mm_get_unmapped_area(filp, addr, len_align, pgoff, flags);
> > + if (!IS_ERR_VALUE(addr_align)) {
> > + addr_align += (off - addr_align) & (align - 1);
> > + return addr_align;
>
>
> PeterX was working on this previously and last time I was not excited
> to see all this mm logic open coded in drivers.
>
> I was hoping to see that the core mm could provide some helper scheme
> for drivers that already know what they want to map to get a vma
> alignment that is optimal for the architecutre.
>
> Peter had a design that seemed like a good exploration at least..
Well you and Matthew and I were discussing something similar previously also?
The AI fixgeddon is delaying any nice-to-haves of late, but in spirit yes, it's
appalling to have this mm code duplicated and it clearly shows a gap in the API.
>
> Jason
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-24 17:26 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 7:30 [PATCH 0/2] vfio/pci: Provide kernel-side BAR alignment for huge page mapping Guanghui Feng
2026-11-24 7:30 ` [PATCH 1/2] vfio: Add get_unmapped_area callback to vfio_device_ops Guanghui Feng
2026-11-24 7:30 ` [PATCH 2/2] vfio/pci: Align BAR mappings for huge page support Guanghui Feng
2026-11-24 12:40 ` [PATCH v2 0/2] vfio/pci: Provide kernel-side BAR alignment for huge page mapping Guanghui Feng
2026-11-24 12:40 ` [PATCH v2 1/2] vfio: Add get_unmapped_area callback to vfio_device_ops Guanghui Feng
2026-11-24 12:40 ` [PATCH v2 2/2] vfio/pci: Align BAR mappings for huge page support Guanghui Feng
2026-11-24 13:52 ` Jason Gunthorpe
2026-11-24 17:25 ` Lorenzo Stoakes (ARM)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®