[PATCH RFC v3 66/88] vfio: Dispatch guest_memfd lookup_pfn_range via vfio_device_ops
From: Ackerley Tng via B4 Relay
Date: Fri Nov 06 2026 - 19:58:57 EST
From: Ackerley Tng <ackerleytng@xxxxxxxxxx>
When scanning guest_memfd memory ranges for confidential architecture
reclaim or conversion, guest_memfd invokes the .lookup_pfn_range
provider operation to locate backed PFN extents or skip unbacked holes
in bulk.
Implement .lookup_pfn_range on vfio_device_gmem_provider_ops to
dispatch to the bus driver's .gmem_lookup_pfn_range callback on struct
vfio_device_ops. Return -EOPNOTSUPP if the bus driver does not define
the callback.
Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>
---
drivers/vfio/vfio_main.c | 15 +++++++++++++++
include/linux/vfio.h | 2 ++
2 files changed, 17 insertions(+)
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index fd5f71dfc44b7..1c2811848c775 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -1636,10 +1636,25 @@ static int vfio_device_gmem_check_set_attributes(void *provider, pgoff_t index,
attributes);
}
+static int vfio_device_gmem_lookup_pfn_range(void *provider, pgoff_t index,
+ unsigned long *pfn,
+ size_t *nr_pages)
+{
+ struct file *file = provider;
+ struct vfio_device_file *df = file->private_data;
+ struct vfio_device *device = df->device;
+
+ if (!device->ops->gmem_lookup_pfn_range)
+ return -EOPNOTSUPP;
+
+ return device->ops->gmem_lookup_pfn_range(device, index, pfn, nr_pages);
+}
+
static const struct guest_memfd_provider_operations vfio_device_gmem_provider_ops = {
.attach = vfio_device_gmem_attach,
.release = vfio_device_gmem_release,
.get_pfn = vfio_device_gmem_get_pfn,
+ .lookup_pfn_range = vfio_device_gmem_lookup_pfn_range,
.mmap = vfio_device_gmem_mmap,
.check_set_attributes = vfio_device_gmem_check_set_attributes,
};
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index aade091a91fe8..8022adedc26a6 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -128,6 +128,8 @@ struct vfio_device_ops {
int (*gmem_check_set_attributes)(struct vfio_device *device,
pgoff_t index, size_t nr_pages,
u64 attributes);
+ int (*gmem_lookup_pfn_range)(struct vfio_device *device, pgoff_t index,
+ unsigned long *pfn, size_t *nr_pages);
char *name;
int (*init)(struct vfio_device *vdev);
void (*release)(struct vfio_device *vdev);
--
2.56.0.455.g6c5a503b38-goog