[RFC PATCH 10/21] vfio: Export helper to get vfio_device from fd

Alexey Kardashevskiy
2024-08-23T13:30:22Z

The SEV TIO Bind operation is going to be handled in the KVM
and requires the BDFn of the device being bound, and the only
supplied information is VFIO device fd.

Add helper to convert vfio devfd to a device.

Note that vfio_put_device() is already public (it is "static
inline" wrapper for put_device()).

Signed-off-by: Alexey Kardashevskiy <aik@amd.com>
---
include/linux/vfio.h | 1 +
drivers/vfio/vfio_main.c | 13 +++++++++++++
2 files changed, 14 insertions(+)

diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 000a6cab2d31..91fd376ad13e 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -293,6 +293,7 @@ int vfio_mig_get_next_state(struct vfio_device *device,

void vfio_combine_iova_ranges(struct rb_root_cached *root, u32 cur_nodes,
u32 req_nodes);
+struct vfio_device *vfio_file_device(struct file *file);

/*
* External user API
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index a5a62d9d963f..5aa804ff918b 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -1447,6 +1447,19 @@ void vfio_file_set_kvm(struct file *file, struct kvm *kvm)
}
EXPORT_SYMBOL_GPL(vfio_file_set_kvm);

+struct vfio_device *vfio_file_device(struct file *filep)
+{
+ struct vfio_device_file *df = filep->private_data;
+
+ if (filep->f_op != &vfio_device_fops)
+ return NULL;
+
+ get_device(&df->device->device);
+
+ return df->device;
+}
+EXPORT_SYMBOL_GPL(vfio_file_device);
+
/*
* Sub-module support
*/
--
2.45.2