From 69ea9011bd7d65190890f7c9dc36b757ebbcc28d Mon Sep 17 00:00:00 2001 From: Mostafa Saleh Date: Thu, 16 Nov 2023 11:03:05 +0000 Subject: [PATCH] ANDROID: vfio: Add vfio_file_get_device Add a function to get struct device from a VFIO file, this is would be used from kvm-vfio bridge for protected device assignment. Bug: 357781595 Bug: 348382247 Change-Id: I60026744e2bd65b290cee8eb439a542946255755 Signed-off-by: Mostafa Saleh --- drivers/vfio/vfio_main.c | 13 +++++++++++++ include/linux/vfio.h | 1 + 2 files changed, 14 insertions(+) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index a5a62d9d963f..2167452c2499 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -479,6 +479,19 @@ void vfio_device_put_kvm(struct vfio_device *device) clear: device->kvm = NULL; } + +static struct vfio_device *vfio_device_from_file(struct file *file); +/** + * vfio_file_get_device - Return struct device from vfio device fd + * @file: VFIO device file + */ +struct device *vfio_file_get_device(struct file *file) +{ + struct vfio_device *device = vfio_device_from_file(file); + + return device ? device->dev : NULL; +} +EXPORT_SYMBOL_GPL(vfio_file_get_device); #endif /* true if the vfio_device has open_device() called but not close_device() */ diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 000a6cab2d31..861bbeaa3d86 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -316,6 +316,7 @@ static inline bool vfio_file_has_dev(struct file *file, struct vfio_device *devi bool vfio_file_is_valid(struct file *file); bool vfio_file_enforced_coherent(struct file *file); void vfio_file_set_kvm(struct file *file, struct kvm *kvm); +struct device *vfio_file_get_device(struct file *file); #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))