[PATCH v2 0/2] vfio/pci: Provide kernel-side BAR alignment for huge page mapping
From: Guanghui Feng
Date: Tue Nov 24 2026 - 07:40:42 EST
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@xxxxxxxxxxxxxxxxx/
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