[PATCH RFC v3 60/88] vfio/pci: Implement get_pfn provider op for guest_memfd

From: Ackerley Tng via B4 Relay

Date: Fri Nov 06 2026 - 19:50:53 EST


From: Ackerley Tng <ackerleytng@xxxxxxxxxx>

Implement the .get_pfn provider operation in the generic PCI VFIO bus
driver to translate guest_memfd page offsets to physical BAR frames.

Check the BAR availability flag locklessly via smp_load_acquire() to
avoid acquiring the device memory lock on the fault path, returning
-EFAULT if memory decoding is disabled or the device is in low-power D3.

Decode the 40-bit VFIO region offset to identify the targeted BAR and
offset within the BAR, validating that the BAR is a standard memory BAR,
supports memory mapping, and the offset falls within the resource size.

Compute the physical frame number from the PCI resource start address
and report GUEST_MEMFD_PFN_ATTRS_CACHE_UC_ALLOW_WC as the frame's
caching attribute so stage-2 mappings permit guest write-combining while
host userspace mappings remain uncached.

Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>
---
drivers/vfio/pci/vfio_pci.c | 1 +
drivers/vfio/pci/vfio_pci_core.c | 38 ++++++++++++++++++++++++++++++++++++++
include/linux/vfio_pci_core.h | 3 +++
3 files changed, 42 insertions(+)

diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index 96f438671db29..9bc8009a9e766 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -171,6 +171,7 @@ static const struct vfio_device_ops vfio_pci_ops = {
#ifdef CONFIG_KVM_GUEST_MEMFD
.gmem_attach = vfio_pci_core_gmem_attach,
.gmem_release = vfio_pci_core_gmem_release,
+ .gmem_get_pfn = vfio_pci_core_gmem_get_pfn,
#endif
};

diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index c7b67fbce4cae..9551a067b5689 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -2731,6 +2731,44 @@ void vfio_pci_core_gmem_release(struct vfio_device *core_vdev)
up_write(&vdev->memory_lock);
}
EXPORT_SYMBOL_GPL(vfio_pci_core_gmem_release);
+
+int vfio_pci_core_gmem_get_pfn(struct vfio_device *core_vdev, pgoff_t index,
+ unsigned long *pfn, int *max_order,
+ u64 *pfn_attrs)
+{
+ struct vfio_pci_core_device *vdev =
+ container_of(core_vdev, struct vfio_pci_core_device, vdev);
+ struct pci_dev *pdev = vdev->pdev;
+ unsigned int bar;
+ u64 pgoff;
+
+ /*
+ * Lockless check: if memory decoding is disabled or the device is in
+ * low-power D3, fail immediately without blocking on memory_lock.
+ */
+ if (!smp_load_acquire(&vdev->bars_available))
+ return -EFAULT;
+
+ bar = index >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
+ pgoff = index & ((1ULL << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
+
+ if (bar >= PCI_STD_NUM_BARS || !vdev->bar_mmap_supported[bar])
+ return -EINVAL;
+
+ if (pgoff >= (pci_resource_len(pdev, bar) >> PAGE_SHIFT))
+ return -EINVAL;
+
+ *pfn = (pci_resource_start(pdev, bar) >> PAGE_SHIFT) + pgoff;
+
+ if (max_order)
+ *max_order = 0;
+
+ if (pfn_attrs)
+ *pfn_attrs = GUEST_MEMFD_PFN_ATTRS_CACHE_UC_ALLOW_WC;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(vfio_pci_core_gmem_get_pfn);
#endif

module_init(vfio_pci_core_init);
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index c3c16459e14f4..3a6d039454472 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -172,6 +172,9 @@ void vfio_pci_core_unregister_device(struct vfio_pci_core_device *vdev);
#ifdef CONFIG_KVM_GUEST_MEMFD
int vfio_pci_core_gmem_attach(struct vfio_device *core_vdev);
void vfio_pci_core_gmem_release(struct vfio_device *core_vdev);
+int vfio_pci_core_gmem_get_pfn(struct vfio_device *core_vdev, pgoff_t index,
+ unsigned long *pfn, int *max_order,
+ u64 *pfn_attrs);
#endif
extern const struct pci_error_handlers vfio_pci_core_err_handlers;
int vfio_pci_core_sriov_configure(struct vfio_pci_core_device *vdev,

--
2.56.0.455.g6c5a503b38-goog