From: Jike Song <jike.song@intel.com>
To: alex.williamson@redhat.com, pbonzini@redhat.com,
guangrong.xiao@linux.intel.com
Cc: kwankhede@nvidia.com, cjia@nvidia.com, kevin.tian@intel.com,
kvm@vger.kernel.org, Jike Song <jike.song@intel.com>
Subject: [v5 2/3] vfio: support notifier chain in vfio_group
Date: Thu, 17 Nov 2016 20:57:10 +0800 [thread overview]
Message-ID: <1479387431-1266-3-git-send-email-jike.song@intel.com> (raw)
In-Reply-To: <1479387431-1266-1-git-send-email-jike.song@intel.com>
Beyond vfio_iommu events, users might also be interested in
vfio_group events. For example, if a vfio_group is used along
with Qemu/KVM, whenever kvm pointer is set to/cleared from the
vfio_group, users could be notified.
Currently only VFIO_GROUP_NOTIFY_SET_KVM supported.
Cc: Alex Williamson <alex.williamson@redhat.com>
Cc: Kirti Wankhede <kwankhede@nvidia.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Xiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: Jike Song <jike.song@intel.com>
---
drivers/vfio/vfio.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/vfio.h | 6 ++++++
2 files changed, 63 insertions(+)
diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index 819c7f9..85a14ee 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -86,6 +86,8 @@ struct vfio_group {
struct mutex unbound_lock;
atomic_t opened;
bool noiommu;
+ struct kvm *kvm;
+ struct blocking_notifier_head notifier;
};
struct vfio_device {
@@ -312,6 +314,10 @@ static void vfio_group_unlock_and_free(struct vfio_group *group)
* that the group is no longer in vfio.group_list.
*/
iommu_group_unregister_notifier(group->iommu_group, &group->nb);
+
+ /* Any user didn't unregister? */
+ WARN_ON(group->notifier.head);
+
kfree(group);
}
@@ -339,6 +345,7 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
#ifdef CONFIG_VFIO_NOIOMMU
group->noiommu = (iommu_group_get_iommudata(iommu_group) == &noiommu);
#endif
+ BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
group->nb.notifier_call = vfio_iommu_group_notifier;
@@ -1015,6 +1022,50 @@ static long vfio_ioctl_check_extension(struct vfio_container *container,
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)
+{
+ int ret;
+ bool replay = false;
+
+ /* clear known events */
+ if (*events & VFIO_GROUP_NOTIFY_SET_KVM) {
+ replay = true;
+ *events &= ~VFIO_GROUP_NOTIFY_SET_KVM;
+ }
+
+ /* refuse to continue if still events remaining */
+ if (*events)
+ return -EINVAL;
+
+ 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 && replay && group->kvm)
+ blocking_notifier_call_chain(&group->notifier,
+ VFIO_GROUP_NOTIFY_SET_KVM, group->kvm);
+
+ return ret;
+}
+
+static int vfio_unregister_group_notifier(struct vfio_group *group,
+ struct notifier_block *nb)
+{
+ return blocking_notifier_chain_unregister(&group->notifier, nb);
+}
+
/* hold write lock on container->group_lock */
static int __vfio_container_attach_groups(struct vfio_container *container,
struct vfio_iommu_driver *driver,
@@ -2089,6 +2140,9 @@ int vfio_register_notifier(struct device *dev, vfio_notify_type_t type,
case VFIO_IOMMU_NOTIFY:
ret = vfio_register_iommu_notifier(group, events, nb);
break;
+ case VFIO_GROUP_NOTIFY:
+ ret = vfio_register_group_notifier(group, events, nb);
+ break;
default:
ret = EINVAL;
}
@@ -2115,6 +2169,9 @@ int vfio_unregister_notifier(struct device *dev, vfio_notify_type_t type,
case VFIO_IOMMU_NOTIFY:
ret = vfio_unregister_iommu_notifier(group, nb);
break;
+ case VFIO_GROUP_NOTIFY:
+ ret = vfio_unregister_group_notifier(group, nb);
+ break;
default:
ret = -EINVAL;
}
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 6f3ff31..5d46e3c 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -119,11 +119,17 @@ extern int vfio_unregister_notifier(struct device *dev,
/* each type has independent events */
enum vfio_notify_type {
VFIO_IOMMU_NOTIFY = (__force vfio_notify_type_t)0,
+ VFIO_GROUP_NOTIFY = (__force vfio_notify_type_t)1,
};
/* events for VFIO_IOMMU_NOTIFY */
#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
+/* events for VFIO_GROUP_NOTIFY */
+#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
+
+struct kvm;
+extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
/*
* Sub-module helpers
--
1.9.1
next prev parent reply other threads:[~2016-11-17 14:28 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-11-17 12:57 [v5 0/3] plumb kvm/vfio to notify kvm:group attach/detach Jike Song
2016-11-17 12:57 ` [v5 1/3] vfio: vfio_register_notifier: classify iommu notfier Jike Song
2016-11-17 22:10 ` Alex Williamson
2016-11-17 12:57 ` Jike Song [this message]
2016-11-17 22:10 ` [v5 2/3] vfio: support notifier chain in vfio_group Alex Williamson
2016-11-17 12:57 ` [v5 3/3] kvm: set/clear kvm to/from vfio_group when group add/delete Jike Song
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1479387431-1266-3-git-send-email-jike.song@intel.com \
--to=jike.song@intel.com \
--cc=alex.williamson@redhat.com \
--cc=cjia@nvidia.com \
--cc=guangrong.xiao@linux.intel.com \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=kwankhede@nvidia.com \
--cc=pbonzini@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).