|
@@ -86,6 +86,8 @@ struct vfio_group {
|
|
struct mutex unbound_lock;
|
|
struct mutex unbound_lock;
|
|
atomic_t opened;
|
|
atomic_t opened;
|
|
bool noiommu;
|
|
bool noiommu;
|
|
|
|
+ struct kvm *kvm;
|
|
|
|
+ struct blocking_notifier_head notifier;
|
|
};
|
|
};
|
|
|
|
|
|
struct vfio_device {
|
|
struct vfio_device {
|
|
@@ -339,6 +341,7 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
|
|
#ifdef CONFIG_VFIO_NOIOMMU
|
|
#ifdef CONFIG_VFIO_NOIOMMU
|
|
group->noiommu = (iommu_group_get_iommudata(iommu_group) == &noiommu);
|
|
group->noiommu = (iommu_group_get_iommudata(iommu_group) == &noiommu);
|
|
#endif
|
|
#endif
|
|
|
|
+ BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
|
|
|
|
|
|
group->nb.notifier_call = vfio_iommu_group_notifier;
|
|
group->nb.notifier_call = vfio_iommu_group_notifier;
|
|
|
|
|
|
@@ -1581,6 +1584,9 @@ static int vfio_group_fops_release(struct inode *inode, struct file *filep)
|
|
|
|
|
|
filep->private_data = NULL;
|
|
filep->private_data = NULL;
|
|
|
|
|
|
|
|
+ /* Any user didn't unregister? */
|
|
|
|
+ WARN_ON(group->notifier.head);
|
|
|
|
+
|
|
vfio_group_try_dissolve_container(group);
|
|
vfio_group_try_dissolve_container(group);
|
|
|
|
|
|
atomic_dec(&group->opened);
|
|
atomic_dec(&group->opened);
|
|
@@ -2063,6 +2069,76 @@ static int vfio_unregister_iommu_notifier(struct vfio_group *group,
|
|
return ret;
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
|
|
|
|
+{
|
|
|
|
+ group->kvm = kvm;
|
|
|
|
+ blocking_notifier_call_chain(&group->notifier,
|
|
|
|
+ VFIO_GROUP_NOTIFY_SET_KVM, kvm);
|
|
|
|
+}
|
|
|
|
+EXPORT_SYMBOL_GPL(vfio_group_set_kvm);
|
|
|
|
+
|
|
|
|
+static int vfio_register_group_notifier(struct vfio_group *group,
|
|
|
|
+ unsigned long *events,
|
|
|
|
+ struct notifier_block *nb)
|
|
|
|
+{
|
|
|
|
+ struct vfio_container *container;
|
|
|
|
+ int ret;
|
|
|
|
+ bool set_kvm = false;
|
|
|
|
+
|
|
|
|
+ if (*events & VFIO_GROUP_NOTIFY_SET_KVM)
|
|
|
|
+ set_kvm = true;
|
|
|
|
+
|
|
|
|
+ /* clear known events */
|
|
|
|
+ *events &= ~VFIO_GROUP_NOTIFY_SET_KVM;
|
|
|
|
+
|
|
|
|
+ /* refuse to continue if still events remaining */
|
|
|
|
+ if (*events)
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ ret = vfio_group_add_container_user(group);
|
|
|
|
+ if (ret)
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ container = group->container;
|
|
|
|
+ down_read(&container->group_lock);
|
|
|
|
+
|
|
|
|
+ ret = blocking_notifier_chain_register(&group->notifier, nb);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * The attaching of kvm and vfio_group might already happen, so
|
|
|
|
+ * here we replay once upon registration.
|
|
|
|
+ */
|
|
|
|
+ if (!ret && set_kvm && group->kvm)
|
|
|
|
+ blocking_notifier_call_chain(&group->notifier,
|
|
|
|
+ VFIO_GROUP_NOTIFY_SET_KVM, group->kvm);
|
|
|
|
+
|
|
|
|
+ up_read(&container->group_lock);
|
|
|
|
+ vfio_group_try_dissolve_container(group);
|
|
|
|
+
|
|
|
|
+ return ret;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int vfio_unregister_group_notifier(struct vfio_group *group,
|
|
|
|
+ struct notifier_block *nb)
|
|
|
|
+{
|
|
|
|
+ struct vfio_container *container;
|
|
|
|
+ int ret;
|
|
|
|
+
|
|
|
|
+ ret = vfio_group_add_container_user(group);
|
|
|
|
+ if (ret)
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ container = group->container;
|
|
|
|
+ down_read(&container->group_lock);
|
|
|
|
+
|
|
|
|
+ ret = blocking_notifier_chain_unregister(&group->notifier, nb);
|
|
|
|
+
|
|
|
|
+ up_read(&container->group_lock);
|
|
|
|
+ vfio_group_try_dissolve_container(group);
|
|
|
|
+
|
|
|
|
+ return ret;
|
|
|
|
+}
|
|
|
|
+
|
|
int vfio_register_notifier(struct device *dev, enum vfio_notify_type type,
|
|
int vfio_register_notifier(struct device *dev, enum vfio_notify_type type,
|
|
unsigned long *events, struct notifier_block *nb)
|
|
unsigned long *events, struct notifier_block *nb)
|
|
{
|
|
{
|
|
@@ -2080,6 +2156,9 @@ int vfio_register_notifier(struct device *dev, enum vfio_notify_type type,
|
|
case VFIO_IOMMU_NOTIFY:
|
|
case VFIO_IOMMU_NOTIFY:
|
|
ret = vfio_register_iommu_notifier(group, events, nb);
|
|
ret = vfio_register_iommu_notifier(group, events, nb);
|
|
break;
|
|
break;
|
|
|
|
+ case VFIO_GROUP_NOTIFY:
|
|
|
|
+ ret = vfio_register_group_notifier(group, events, nb);
|
|
|
|
+ break;
|
|
default:
|
|
default:
|
|
ret = -EINVAL;
|
|
ret = -EINVAL;
|
|
}
|
|
}
|
|
@@ -2106,6 +2185,9 @@ int vfio_unregister_notifier(struct device *dev, enum vfio_notify_type type,
|
|
case VFIO_IOMMU_NOTIFY:
|
|
case VFIO_IOMMU_NOTIFY:
|
|
ret = vfio_unregister_iommu_notifier(group, nb);
|
|
ret = vfio_unregister_iommu_notifier(group, nb);
|
|
break;
|
|
break;
|
|
|
|
+ case VFIO_GROUP_NOTIFY:
|
|
|
|
+ ret = vfio_unregister_group_notifier(group, nb);
|
|
|
|
+ break;
|
|
default:
|
|
default:
|
|
ret = -EINVAL;
|
|
ret = -EINVAL;
|
|
}
|
|
}
|