Skip to content

Commit

Permalink
vfio: Add vfio_file_is_group()
Browse files Browse the repository at this point in the history
This replaces uses of vfio_file_iommu_group() which were only detecting if
the file is a VFIO file with no interest in the actual group.

The only remaning user of vfio_file_iommu_group() is in KVM for the SPAPR
stuff. It passes the iommu_group into the arch code through kvm for some
reason.

Tested-by: Matthew Rosato <[email protected]>
Tested-by: Christian Borntraeger <[email protected]>
Tested-by: Eric Farman <[email protected]>
Signed-off-by: Jason Gunthorpe <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Alex Williamson <[email protected]>
  • Loading branch information
jgunthorpe authored and awilliam committed Oct 7, 2022
1 parent c82e81a commit 4b22ef0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion drivers/vfio/pci/vfio_pci_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ static int vfio_pci_ioctl_pci_hot_reset(struct vfio_pci_core_device *vdev,
}

/* Ensure the FD is a vfio group FD.*/
if (!vfio_file_iommu_group(file)) {
if (!vfio_file_is_group(file)) {
fput(file);
ret = -EINVAL;
break;
Expand Down
16 changes: 15 additions & 1 deletion drivers/vfio/vfio_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1553,17 +1553,31 @@ static const struct file_operations vfio_device_fops = {
* @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.
*/
struct iommu_group *vfio_file_iommu_group(struct file *file)
{
struct vfio_group *group = file->private_data;

if (file->f_op != &vfio_group_fops)
if (!IS_ENABLED(CONFIG_SPAPR_TCE_IOMMU))
return NULL;

if (!vfio_file_is_group(file))
return NULL;
return group->iommu_group;
}
EXPORT_SYMBOL_GPL(vfio_file_iommu_group);

/**
* vfio_file_is_group - True if the file is usable with VFIO aPIS
* @file: VFIO group file
*/
bool vfio_file_is_group(struct file *file)
{
return file->f_op == &vfio_group_fops;
}
EXPORT_SYMBOL_GPL(vfio_file_is_group);

/**
* vfio_file_enforced_coherent - True if the DMA associated with the VFIO file
* is always CPU cache coherent
Expand Down
1 change: 1 addition & 0 deletions include/linux/vfio.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ int vfio_mig_get_next_state(struct vfio_device *device,
* External user API
*/
struct iommu_group *vfio_file_iommu_group(struct file *file);
bool vfio_file_is_group(struct file *file);
bool vfio_file_enforced_coherent(struct file *file);
void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
Expand Down
20 changes: 18 additions & 2 deletions virt/kvm/vfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ static bool kvm_vfio_file_enforced_coherent(struct file *file)
return ret;
}

static bool kvm_vfio_file_is_group(struct file *file)
{
bool (*fn)(struct file *file);
bool ret;

fn = symbol_get(vfio_file_is_group);
if (!fn)
return false;

ret = fn(file);

symbol_put(vfio_file_is_group);

return ret;
}

#ifdef CONFIG_SPAPR_TCE_IOMMU
static struct iommu_group *kvm_vfio_file_iommu_group(struct file *file)
{
struct iommu_group *(*fn)(struct file *file);
Expand All @@ -77,7 +94,6 @@ static struct iommu_group *kvm_vfio_file_iommu_group(struct file *file)
return ret;
}

#ifdef CONFIG_SPAPR_TCE_IOMMU
static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm,
struct kvm_vfio_group *kvg)
{
Expand Down Expand Up @@ -136,7 +152,7 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
return -EBADF;

/* Ensure the FD is a vfio group FD.*/
if (!kvm_vfio_file_iommu_group(filp)) {
if (!kvm_vfio_file_is_group(filp)) {
ret = -EINVAL;
goto err_fput;
}
Expand Down

0 comments on commit 4b22ef0

Please sign in to comment.