Skip to content

Commit

Permalink
vfio: Common function to increment container_users
Browse files Browse the repository at this point in the history
This change rearrange functions to have common function to increment
container_users

Signed-off-by: Kirti Wankhede <kwankhede@nvidia.com>
Signed-off-by: Neo Jia <cjia@nvidia.com>
Reviewed-by: Jike Song <jike.song@intel.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
  • Loading branch information
Kirti Wankhede authored and Alex Williamson committed Nov 17, 2016
1 parent 7ed3ea8 commit 32f55d8
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions drivers/vfio/vfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,23 @@ static bool vfio_group_viable(struct vfio_group *group)
group, vfio_dev_viable) == 0);
}

static int vfio_group_add_container_user(struct vfio_group *group)
{
if (!atomic_inc_not_zero(&group->container_users))
return -EINVAL;

if (group->noiommu) {
atomic_dec(&group->container_users);
return -EPERM;
}
if (!group->container->iommu_driver || !vfio_group_viable(group)) {
atomic_dec(&group->container_users);
return -EINVAL;
}

return 0;
}

static const struct file_operations vfio_device_fops;

static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
Expand Down Expand Up @@ -1694,23 +1711,14 @@ static const struct file_operations vfio_device_fops = {
struct vfio_group *vfio_group_get_external_user(struct file *filep)
{
struct vfio_group *group = filep->private_data;
int ret;

if (filep->f_op != &vfio_group_fops)
return ERR_PTR(-EINVAL);

if (!atomic_inc_not_zero(&group->container_users))
return ERR_PTR(-EINVAL);

if (group->noiommu) {
atomic_dec(&group->container_users);
return ERR_PTR(-EPERM);
}

if (!group->container->iommu_driver ||
!vfio_group_viable(group)) {
atomic_dec(&group->container_users);
return ERR_PTR(-EINVAL);
}
ret = vfio_group_add_container_user(group);
if (ret)
return ERR_PTR(ret);

vfio_group_get(group);

Expand Down

0 comments on commit 32f55d8

Please sign in to comment.