mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Guanghui Feng <guanghuifeng@linux.alibaba.com>
To: guanghuifeng@linux.alibaba.com
Cc: alex@shazbot.org, alifm@linux.ibm.com, ankita@nvidia.com,
	dakr@kernel.org, fengchengwen@huawei.com, jgg@ziepe.ca,
	kevin.tian@intel.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, mattev@meta.com,
	ritesh.list@gmail.com, xinmengs@nvidia.com
Subject: [PATCH v2 0/2] vfio/pci: Provide kernel-side BAR alignment for huge page mapping
Date: Tue, 24 Nov 2026 20:40:02 +0800	[thread overview]
Message-ID: <20261124124004.457441-1-guanghuifeng@linux.alibaba.com> (raw)
In-Reply-To: <20261124073048.268988-1-guanghuifeng@linux.alibaba.com>

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


  parent reply	other threads:[~2026-11-24 12:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  7:30 [PATCH " 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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124124004.457441-1-guanghuifeng@linux.alibaba.com \
    --to=guanghuifeng@linux.alibaba.com \
    --cc=alex@shazbot.org \
    --cc=alifm@linux.ibm.com \
    --cc=ankita@nvidia.com \
    --cc=dakr@kernel.org \
    --cc=fengchengwen@huawei.com \
    --cc=jgg@ziepe.ca \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mattev@meta.com \
    --cc=ritesh.list@gmail.com \
    --cc=xinmengs@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®