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 1/2] vfio: Add get_unmapped_area callback to vfio_device_ops
Date: Tue, 24 Nov 2026 20:40:03 +0800	[thread overview]
Message-ID: <20261124124004.457441-2-guanghuifeng@linux.alibaba.com> (raw)
In-Reply-To: <20261124124004.457441-1-guanghuifeng@linux.alibaba.com>

vfio exposes device regions through the device file descriptor and a
bus driver backs a region with pfnmaps over the device MMIO space when
the user calls mmap(2) on it.  Unless the user asks for a specific
address, the virtual address of the resulting VMA is picked by the
generic address selection in mm_get_unmapped_area(), which only
guarantees PAGE_SIZE alignment.

That is enough for the base mapping, but it keeps a driver from ever
backing a region with huge pfnmaps.  huge_fault() only installs a PMD
or PUD sized pfnmap when both the fault address and the pfn are aligned
to that page size, see is_aligned_for_order().  A driver can only
influence the first of those, and it cannot do so from its mmap()
callback because the address has already been fixed up by then.

Add a get_unmapped_area() callback to vfio_device_ops along with a
dispatcher in vfio_device_fops, so a bus driver can ask for an address
that is aligned to the largest page size its region can be mapped with
before the VMA is created.  Drivers that do not provide the callback,
and configurations without huge pfnmap support, fall back to
mm_get_unmapped_area() and keep the existing behaviour.

The hook is only consulted when the kernel has to choose the address:
a MAP_FIXED mapping never reaches it, and an explicit address hint is
passed through to the driver, which is expected to honour it.  The
callback therefore serves the userspace consumers that call
mmap(fd, offset) with addr=0 and rely on the kernel to pick a suitable
address for them, rather than trying to second guess userspace that
already arranges the alignment itself.

This mirrors what drivers/dax/device.c does with
dax_get_unmapped_area(), which has to satisfy the same alignment
requirement for its PMD sized pfnmaps.

Signed-off-by: Guanghui Feng <guanghuifeng@linux.alibaba.com>
---
 drivers/vfio/vfio_main.c | 34 ++++++++++++++++++++++++++++++++++
 include/linux/vfio.h     | 12 ++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 4e5d738ccf4b..edf9eb4cd477 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -28,6 +28,7 @@
 #include <linux/pseudo_fs.h>
 #include <linux/rwsem.h>
 #include <linux/sched.h>
+#include <linux/sched/mm.h>
 #include <linux/seq_file.h>
 #include <linux/slab.h>
 #include <linux/stat.h>
@@ -1431,6 +1432,36 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
 	return device->ops->mmap(device, vma);
 }
 
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+/*
+ * Give the bus driver a chance to request a virtual address that allows the
+ * mapping to be backed by huge pages, fex. a PCI BAR mapping that could be
+ * faulted in with PMD or PUD sized pfnmaps.  Anything the driver cannot
+ * handle, including devices without huge mapping support, falls back to the
+ * generic address selection, which is also what happens without this
+ * callback.
+ */
+static unsigned long vfio_device_fops_get_unmapped_area(struct file *filep,
+							unsigned long addr,
+							unsigned long len,
+							unsigned long pgoff,
+							unsigned long flags)
+{
+	struct vfio_device_file *df = filep->private_data;
+	struct vfio_device *device = df->device;
+
+	/* Paired with smp_store_release() following vfio_df_open() */
+	if (!smp_load_acquire(&df->access_granted))
+		goto out;
+
+	if (device->ops->get_unmapped_area)
+		return device->ops->get_unmapped_area(device, filep, addr, len,
+						      pgoff, flags);
+out:
+	return mm_get_unmapped_area(filep, addr, len, pgoff, flags);
+}
+#endif
+
 #ifdef CONFIG_PROC_FS
 static void vfio_device_show_fdinfo(struct seq_file *m, struct file *filep)
 {
@@ -1456,6 +1487,9 @@ const struct file_operations vfio_device_fops = {
 	.unlocked_ioctl	= vfio_device_fops_unl_ioctl,
 	.compat_ioctl	= compat_ptr_ioctl,
 	.mmap		= vfio_device_fops_mmap,
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+	.get_unmapped_area = vfio_device_fops_get_unmapped_area,
+#endif
 #ifdef CONFIG_PROC_FS
 	.show_fdinfo	= vfio_device_show_fdinfo,
 #endif
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 0cc91c6f96d2..68a84a1fd3fd 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -102,6 +102,12 @@ struct vfio_device {
  * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
  *         operations documented below
  * @mmap: Perform mmap(2) on a region of the device file descriptor
+ * @get_unmapped_area: Optional, suggest a virtual address for mmap(2) of a
+ *         device region such that the mapping can be backed by huge pages,
+ *         ie. the address is aligned to the largest page size the region can
+ *         be mapped with.  Drivers that don't support huge mappings, and
+ *         configurations without huge pfnmap support, leave this unset and
+ *         the generic address selection is used.
  * @request: Request for the bus driver to release the device
  * @match: Optional device name match callback (return: 0 for no-match, >0 for
  *         match, -errno for abort (ex. match with insufficient or incorrect
@@ -143,6 +149,12 @@ struct vfio_device_ops {
 	void	(*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length);
 	int	(*device_feature)(struct vfio_device *device, u32 flags,
 				  void __user *arg, size_t argsz);
+	unsigned long (*get_unmapped_area)(struct vfio_device *vdev,
+					   struct file *filp,
+					   unsigned long addr,
+					   unsigned long len,
+					   unsigned long pgoff,
+					   unsigned long flags);
 };
 
 #if IS_ENABLED(CONFIG_IOMMUFD)
-- 
2.43.7


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