A vfio_group may be or may not be attached to a KVM instance, but if it is, the user of vfio_group might also want to know which KVM instance it is attached to, to perform some operations exclusively provided by KVM. In VFIO there are already external APIs for KVM to get/put the vfio_group, by extending these APIs KVM can set or clear itself to/from the vfio_group, for external users to get/put. Signed-off-by: Jike Song <jike.song@xxxxxxxxx> --- drivers/vfio/vfio.c | 30 ++++++++++++++++++++++++++++++ include/linux/vfio.h | 4 ++++ 2 files changed, 34 insertions(+) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index e3e58e3..a5c90d2 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -34,6 +34,7 @@ #include <linux/uaccess.h> #include <linux/vfio.h> #include <linux/wait.h> +#include <linux/kvm_host.h> #define DRIVER_VERSION "0.3" #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@xxxxxxxxxx>" @@ -86,6 +87,10 @@ struct vfio_group { struct mutex unbound_lock; atomic_t opened; bool noiommu; + struct { + struct kvm *kvm; + struct mutex lock; + } udata; }; struct vfio_device { @@ -333,6 +338,7 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group) mutex_init(&group->device_lock); INIT_LIST_HEAD(&group->unbound_list); mutex_init(&group->unbound_lock); + mutex_init(&group->udata.lock); atomic_set(&group->container_users, 0); atomic_set(&group->opened, 0); group->iommu_group = iommu_group; @@ -1739,6 +1745,30 @@ long vfio_external_check_extension(struct vfio_group *group, unsigned long arg) } EXPORT_SYMBOL_GPL(vfio_external_check_extension); +void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm) +{ + mutex_lock(&group->udata.lock); + group->udata.kvm = kvm; + mutex_unlock(&group->udata.lock); +} +EXPORT_SYMBOL_GPL(vfio_group_set_kvm); + +struct kvm *vfio_group_get_kvm(struct vfio_group *group) +{ + mutex_lock(&group->udata.lock); + + if (!group->udata.kvm) + goto out; + + kvm_get_kvm(group->udata.kvm); + mutex_unlock(&group->udata.lock); + return group->udata.kvm; +out: + mutex_unlock(&group->udata.lock); + return NULL; +} +EXPORT_SYMBOL_GPL(vfio_group_get_kvm); + /** * Sub-module support */ diff --git a/include/linux/vfio.h b/include/linux/vfio.h index ad9b857..3abd690 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -95,6 +95,10 @@ extern long vfio_external_check_extension(struct vfio_group *group, extern struct vfio_group *vfio_group_get_from_dev(struct device *dev); extern void vfio_group_put(struct vfio_group *group); +struct kvm; +extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm); +extern struct kvm *vfio_group_get_kvm(struct vfio_group *group); + /* * Sub-module helpers */ -- 1.9.1 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html