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 2/2] vfio/pci: Align BAR mappings for huge page support
Date: Tue, 24 Nov 2026 20:40:04 +0800	[thread overview]
Message-ID: <20261124124004.457441-3-guanghuifeng@linux.alibaba.com> (raw)
In-Reply-To: <20261124124004.457441-1-guanghuifeng@linux.alibaba.com>

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


      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 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   ` Guanghui Feng [this message]

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-3-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®