Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Guanghui Feng" <guanghuifeng@linux.alibaba.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH 2/2] vfio/pci: Align BAR mappings for huge page support
Date: Tue, 24 Nov 2026 07:52:51 +0000	[thread overview]
Message-ID: <sashiko-outbox-230684@kernel.org> (raw)
In-Reply-To: <20261124073048.268988-3-guanghuifeng@linux.alibaba.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] vfio/pci: wrong align and no PMD fallback in
  vfio_pci_core_get_unmapped_area()
- [Medium] vfio/pci: missing MAP_FIXED check in
  vfio_pci_core_get_unmapped_area()
--

commit 22651b52e2b64249734636f112ebe4aa3bd8ca86
Author: Guanghui Feng <guanghuifeng@linux.alibaba.com>

vfio/pci: Align BAR mappings for huge page support

This commit implements vfio_pci_core_get_unmapped_area() to align BAR
virtual addresses for PMD and PUD pfnmaps and sets VM_HUGEPAGE on BAR
VMAs.

> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
> index f2d5b6857925f..dd437f23b9384 100644
> --- a/drivers/vfio/pci/vfio_pci_core.c
> +++ b/drivers/vfio/pci/vfio_pci_core.c
> @@ -1878,12 +1878,107 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma
[ ... ]
> +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;

[Severity: Medium]
Can this shift a fixed mapping at address 0 to a non-zero address when
flags has MAP_FIXED or MAP_FIXED_NOREPLACE set?

do_mmap() and vrm_set_new_addr() still invoke __get_unmapped_area() when
MAP_FIXED is set.  With addr == 0 and MAP_FIXED, mm_get_unmapped_area()
returns 0 immediately, and the shift below changes the returned address to
off & (align - 1).  When (off & (align - 1)) >= mmap_min_addr, do_mmap()
with MAP_FIXED will then unmap and overwrite existing VMAs at
[off & (align - 1), (off & (align - 1)) + len) instead of failing on
address 0.

Checking if (addr || (flags & MAP_FIXED)) before aligning avoids mutating
a fixed address.

> +
> +	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;

[Severity: Medium]
When CONFIG_ARCH_SUPPORTS_PUD_PFNMAP is enabled, doesn't this pick
intermediate power-of-two alignments between PMD_SIZE and PUD_SIZE, and
give up on PMD_SIZE alignment whenever the range does not span a full
block at that larger alignment?

vfio_pci_vmf_insert_pfn() only installs huge mappings at PUD_ORDER and
PMD_ORDER.  For a BAR or mapping length between PMD_SIZE and PUD_SIZE
(for example, a 16 MiB BAR with a 4 KiB MSI-X hole at the start and a
4 KiB PBA hole at the end), align becomes 8 MiB, off_align rounds up to
8 MiB, and off_end - off_align < align jumps to out with PAGE_SIZE
alignment instead of aligning to PMD_SIZE.  Even when off is 0, a BAR
between 4 MiB and 512 MiB reserves up to 512 MiB of extra virtual address
slack when only PMD_SIZE pages can be faulted in.  Also, the second
if (align < PMD_SIZE) check after the while loop cannot be reached because
align >= PMD_SIZE before the loop and the loop stops at PMD_SIZE.

Trying PUD_SIZE only when CONFIG_ARCH_SUPPORTS_PUD_PFNMAP is enabled and
the mapping spans a PUD_SIZE-aligned block, and falling back to PMD_SIZE
otherwise, would handle both full and sparse BAR mappings.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124073048.268988-1-guanghuifeng@linux.alibaba.com?part=2

      reply	other threads:[~2026-11-24  7:52 UTC|newest]

Thread overview: 5+ 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:48   ` sashiko-bot
2026-11-24  7:30 ` [PATCH 2/2] vfio/pci: Align BAR mappings for huge page support Guanghui Feng
2026-11-24  7:52   ` sashiko-bot [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=sashiko-outbox-230684@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=guanghuifeng@linux.alibaba.com \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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