On 5/17/22 2:08 PM, Matthew Rosato wrote:
Rather than relying on a notifier for associating the KVM with
the group, let's assume that the association has already been
made prior to device_open. The first time a device is opened
associate the group KVM with the device.
Suggested-by: Jason Gunthorpe <jgg@xxxxxxxxxx>
Signed-off-by: Matthew Rosato <mjrosato@xxxxxxxxxxxxx>
...
diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index cfcff7764403..c5d421eda275 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -10,6 +10,7 @@
* Author: Tom Lyon, pugs@xxxxxxxxx
*/
+#include "linux/kvm_host.h"
#include <linux/cdev.h>
#include <linux/compat.h>
#include <linux/device.h>
@@ -1083,6 +1084,13 @@ static struct file *vfio_device_open(struct vfio_device *device)
mutex_lock(&device->dev_set->lock);
device->open_count++;
+ down_write(&device->group->group_rwsem);
+ if (device->open_count == 1 && device->group->kvm) {
+ device->kvm = device->group->kvm;
+ kvm_get_kvm(device->kvm);
Did some more compile testing, since vfio has no hard kvm dependency,
kvm_get_kvm and kvm_put_kvm are an issue if KVM is a module while vfio
is built-in...