vfio: New external user group/file match

commit 5d6dee80a1 upstream.

At the point where the kvm-vfio pseudo device wants to release its
vfio group reference, we can't always acquire a new reference to make
that happen.  The group can be in a state where we wouldn't allow a
new reference to be added.  This new helper function allows a caller
to match a file to a group to facilitate this.  Given a file and
group, report if they match.  Thus the caller needs to already have a
group reference to match to the file.  This allows the deletion of a
group without acquiring a new reference.

Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Tested-by: Eric Auger <eric.auger@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Alex Williamson 2017-06-28 13:50:05 -06:00 committed by Greg Kroah-Hartman
parent e91a55790d
commit 8f9dec0c2d
3 changed files with 30 additions and 8 deletions

View File

@ -1751,6 +1751,15 @@ void vfio_group_put_external_user(struct vfio_group *group)
bool vfio_external_group_match_file(struct vfio_group *test_group,
struct file *filep)
struct vfio_group *group = filep->private_data;
return (filep->f_op == &vfio_group_fops) && (group == test_group);
int vfio_external_user_iommu_id(struct vfio_group *group)
return iommu_group_id(group->iommu_group);

View File

@ -88,6 +88,8 @@ extern void vfio_unregister_iommu_driver(
extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
extern void vfio_group_put_external_user(struct vfio_group *group);
extern bool vfio_external_group_match_file(struct vfio_group *group,
struct file *filep);
extern int vfio_external_user_iommu_id(struct vfio_group *group);
extern long vfio_external_check_extension(struct vfio_group *group,
unsigned long arg);

View File

@ -47,6 +47,22 @@ static struct vfio_group *kvm_vfio_group_get_external_user(struct file *filep)
return vfio_group;
static bool kvm_vfio_external_group_match_file(struct vfio_group *group,
struct file *filep)
bool ret, (*fn)(struct vfio_group *, struct file *);
fn = symbol_get(vfio_external_group_match_file);
if (!fn)
return false;
ret = fn(group, filep);
return ret;
static void kvm_vfio_group_put_external_user(struct vfio_group *vfio_group)
void (*fn)(struct vfio_group *);
@ -171,18 +187,13 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)
if (!f.file)
return -EBADF;
vfio_group = kvm_vfio_group_get_external_user(f.file);
if (IS_ERR(vfio_group))
return PTR_ERR(vfio_group);
ret = -ENOENT;
list_for_each_entry(kvg, &kv->group_list, node) {
if (kvg->vfio_group != vfio_group)
if (!kvm_vfio_external_group_match_file(kvg->vfio_group,
@ -196,7 +207,7 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)