Skip to content

Commit

Permalink
vfio: Hold a reference to the iommu_group in kvm for SPAPR
Browse files Browse the repository at this point in the history
SPAPR exists completely outside the normal iommu driver framework, the
groups it creates are fake and are only created to enable VFIO's uAPI.

Thus, it does not need to follow the iommu core rule that the iommu_group
will only be touched while a driver is attached.

Carry a group reference into KVM and have KVM directly manage the lifetime
of this object independently of VFIO. This means KVM no longer relies on
the vfio group file being valid to maintain the group reference.

Tested-by: Matthew Rosato <mjrosato@linux.ibm.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/2-v2-15417f29324e+1c-vfio_group_disassociate_jgg@nvidia.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
  • Loading branch information
Jason Gunthorpe authored and Alex Williamson committed Oct 7, 2022
1 parent 4b22ef0 commit 819da99
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
6 changes: 4 additions & 2 deletions drivers/vfio/vfio_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1552,8 +1552,9 @@ static const struct file_operations vfio_device_fops = {
* vfio_file_iommu_group - Return the struct iommu_group for the vfio group file
* @file: VFIO group file
*
* The returned iommu_group is valid as long as a ref is held on the file.
* This function is deprecated, only the SPAPR path in kvm should call it.
* The returned iommu_group is valid as long as a ref is held on the file. This
* returns a reference on the group. This function is deprecated, only the SPAPR
* path in kvm should call it.
*/
struct iommu_group *vfio_file_iommu_group(struct file *file)
{
Expand All @@ -1564,6 +1565,7 @@ struct iommu_group *vfio_file_iommu_group(struct file *file)

if (!vfio_file_is_group(file))
return NULL;
iommu_group_ref_get(group->iommu_group);
return group->iommu_group;
}
EXPORT_SYMBOL_GPL(vfio_file_iommu_group);
Expand Down
25 changes: 14 additions & 11 deletions virt/kvm/vfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
struct kvm_vfio_group {
struct list_head node;
struct file *file;
#ifdef CONFIG_SPAPR_TCE_IOMMU
struct iommu_group *iommu_group;
#endif
};

struct kvm_vfio {
Expand Down Expand Up @@ -97,12 +100,12 @@ static struct iommu_group *kvm_vfio_file_iommu_group(struct file *file)
static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm,
struct kvm_vfio_group *kvg)
{
struct iommu_group *grp = kvm_vfio_file_iommu_group(kvg->file);

if (WARN_ON_ONCE(!grp))
if (WARN_ON_ONCE(!kvg->iommu_group))
return;

kvm_spapr_tce_release_iommu_group(kvm, grp);
kvm_spapr_tce_release_iommu_group(kvm, kvg->iommu_group);
iommu_group_put(kvg->iommu_group);
kvg->iommu_group = NULL;
}
#endif

Expand Down Expand Up @@ -252,19 +255,19 @@ static int kvm_vfio_group_set_spapr_tce(struct kvm_device *dev,
mutex_lock(&kv->lock);

list_for_each_entry(kvg, &kv->group_list, node) {
struct iommu_group *grp;

if (kvg->file != f.file)
continue;

grp = kvm_vfio_file_iommu_group(kvg->file);
if (WARN_ON_ONCE(!grp)) {
ret = -EIO;
goto err_fdput;
if (!kvg->iommu_group) {
kvg->iommu_group = kvm_vfio_file_iommu_group(kvg->file);
if (WARN_ON_ONCE(!kvg->iommu_group)) {
ret = -EIO;
goto err_fdput;
}
}

ret = kvm_spapr_tce_attach_iommu_group(dev->kvm, param.tablefd,
grp);
kvg->iommu_group);
break;
}

Expand Down

0 comments on commit 819da99

Please sign in to comment.