Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* Re: [PATCH v2 2/2] vfio/pci: Align BAR mappings for huge page support
       [not found]   ` <20261124124004.457441-3-guanghuifeng@linux.alibaba.com>
@ 2026-11-24 13:52     ` Jason Gunthorpe
  2026-11-24 17:25       ` Lorenzo Stoakes (ARM)
  0 siblings, 1 reply; 2+ 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] 2+ messages in thread

* Re: [PATCH v2 2/2] vfio/pci: Align BAR mappings for huge page support
  2026-11-24 13:52     ` [PATCH v2 2/2] vfio/pci: Align BAR mappings for huge page support Jason Gunthorpe
@ 2026-11-24 17:25       ` Lorenzo Stoakes (ARM)
  0 siblings, 0 replies; 2+ 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] 2+ messages in thread

end of thread, other threads:[~2026-11-24 17:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261124073048.268988-1-guanghuifeng@linux.alibaba.com>
     [not found] ` <20261124124004.457441-1-guanghuifeng@linux.alibaba.com>
     [not found]   ` <20261124124004.457441-3-guanghuifeng@linux.alibaba.com>
2026-11-24 13:52     ` [PATCH v2 2/2] vfio/pci: Align BAR mappings for huge page support 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