[PATCH 08/10] vfio: Move vfio_group_set_kvm() into vfio_file_ops

Jason Gunthorpe
2022-04-14T18:46:28Z

Just move the exported function into the op and have it accept the struct
file instead of vfio_group.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/vfio/vfio.c | 26 ++++++++++++++++++--------
include/linux/vfio.h | 5 +++--
virt/kvm/vfio.c | 19 +++----------------
3 files changed, 24 insertions(+), 26 deletions(-)

diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index c08093fb6d28d5..2eb63d9dded8fb 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -2029,9 +2029,27 @@ static bool vfio_file_enforced_coherent(struct file *filep)
return ret;
}

+/**
+ * vfio_file_set_kvm - Link a kvm with VFIO drivers
+ * @filep: VFIO file
+ * @kvm: KVM to link
+ *
+ * The kvm pointer will be forwarded to all the vfio_device's attached to the
+ * VFIO file via the VFIO_GROUP_NOTIFY_SET_KVM notifier.
+ */
+static void vfio_file_set_kvm(struct file *filep, struct kvm *kvm)
+{
+ struct vfio_group *group = filep->private_data;
+
+ group->kvm = kvm;
+ blocking_notifier_call_chain(&group->notifier,
+ VFIO_GROUP_NOTIFY_SET_KVM, kvm);
+}
+
static const struct vfio_file_ops vfio_file_group_ops = {
.get_iommu_group = vfio_file_iommu_group,
.is_enforced_coherent = vfio_file_enforced_coherent,
+ .set_kvm = vfio_file_set_kvm,
};

/**
@@ -2461,14 +2479,6 @@ static int vfio_unregister_iommu_notifier(struct vfio_group *group,
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)
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index b1583eb80f12e6..bc6e47f3f26560 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -15,6 +15,8 @@
#include <linux/poll.h>
#include <uapi/linux/vfio.h>

+struct kvm;
+
/*
* VFIO devices can be placed in a set, this allows all devices to share this
* structure and the VFIO core will provide a lock that is held around
@@ -141,6 +143,7 @@ int vfio_mig_get_next_state(struct vfio_device *device,
struct vfio_file_ops {
struct iommu_group *(*get_iommu_group)(struct file *filep);
bool (*is_enforced_coherent)(struct file *filep);
+ void (*set_kvm)(struct file *filep, struct kvm *kvm);
};
extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
extern void vfio_group_put_external_user(struct vfio_group *group);
@@ -186,8 +189,6 @@ extern int vfio_unregister_notifier(struct device *dev,
enum vfio_notify_type type,
struct notifier_block *nb);

-struct kvm;
-extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);

/*
* Sub-module helpers
diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c
index f5ef78192a97ab..9baf04c5b0cc3d 100644
--- a/virt/kvm/vfio.c
+++ b/virt/kvm/vfio.c
@@ -73,19 +73,6 @@ static void kvm_vfio_group_put_external_user(struct vfio_group *vfio_group)
symbol_put(vfio_group_put_external_user);
}

-static void kvm_vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
-{
- void (*fn)(struct vfio_group *, struct kvm *);
-
- fn = symbol_get(vfio_group_set_kvm);
- if (!fn)
- return;
-
- fn(group, kvm);
-
- symbol_put(vfio_group_set_kvm);
-}
-
static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm,
struct kvm_vfio_group *kvg)
{
@@ -184,7 +171,7 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)

mutex_unlock(&kv->lock);

- kvm_vfio_group_set_kvm(vfio_group, dev->kvm);
+ kvg->ops->set_kvm(kvg->filp, dev->kvm);
kvm_vfio_update_coherency(dev);

return 0;
@@ -218,7 +205,7 @@ static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd)
list_del(&kvg->node);
kvm_arch_end_assignment(dev->kvm);
kvm_spapr_tce_release_vfio_group(dev->kvm, kvg);
- kvm_vfio_group_set_kvm(kvg->vfio_group, NULL);
+ kvg->ops->set_kvm(kvg->filp, NULL);
kvm_vfio_group_put_external_user(kvg->vfio_group);
fput(kvg->filp);
kfree(kvg);
@@ -334,7 +321,7 @@ static void kvm_vfio_destroy(struct kvm_device *dev)

list_for_each_entry_safe(kvg, tmp, &kv->group_list, node) {
kvm_spapr_tce_release_vfio_group(dev->kvm, kvg);
- kvm_vfio_group_set_kvm(kvg->vfio_group, NULL);
+ kvg->ops->set_kvm(kvg->filp, NULL);
kvm_vfio_group_put_external_user(kvg->vfio_group);
fput(kvg->filp);
list_del(&kvg->node);
--
2.35.1