Skip to content

Commit

Permalink
vfio: Pass struct vfio_device_file * to vfio_device_open/close()
Browse files Browse the repository at this point in the history
This avoids passing too much parameters in multiple functions. Per the
input parameter change, rename the function to be vfio_df_open/close().

Reviewed-by: Kevin Tian <[email protected]>
Reviewed-by: Jason Gunthorpe <[email protected]>
Reviewed-by: Eric Auger <[email protected]>
Tested-by: Terrence Xu <[email protected]>
Tested-by: Nicolin Chen <[email protected]>
Tested-by: Matthew Rosato <[email protected]>
Tested-by: Yanting Jiang <[email protected]>
Tested-by: Shameer Kolothum <[email protected]>
Tested-by: Zhenzhong Duan <[email protected]>
Signed-off-by: Yi Liu <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Alex Williamson <[email protected]>
  • Loading branch information
yiliu1765 authored and awilliam committed Jul 25, 2023
1 parent dcc31ea commit 05f37e1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
20 changes: 14 additions & 6 deletions drivers/vfio/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ static void vfio_device_group_get_kvm_safe(struct vfio_device *device)
spin_unlock(&device->group->kvm_ref_lock);
}

static int vfio_device_group_open(struct vfio_device *device)
static int vfio_df_group_open(struct vfio_device_file *df)
{
struct vfio_device *device = df->device;
int ret;

mutex_lock(&device->group->group_lock);
Expand All @@ -190,7 +191,11 @@ static int vfio_device_group_open(struct vfio_device *device)
if (device->open_count == 0)
vfio_device_group_get_kvm_safe(device);

ret = vfio_device_open(device, device->group->iommufd);
df->iommufd = device->group->iommufd;

ret = vfio_df_open(df);
if (ret)
df->iommufd = NULL;

if (device->open_count == 0)
vfio_device_put_kvm(device);
Expand All @@ -202,12 +207,15 @@ static int vfio_device_group_open(struct vfio_device *device)
return ret;
}

void vfio_device_group_close(struct vfio_device *device)
void vfio_df_group_close(struct vfio_device_file *df)
{
struct vfio_device *device = df->device;

mutex_lock(&device->group->group_lock);
mutex_lock(&device->dev_set->lock);

vfio_device_close(device, device->group->iommufd);
vfio_df_close(df);
df->iommufd = NULL;

if (device->open_count == 0)
vfio_device_put_kvm(device);
Expand All @@ -228,7 +236,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
goto err_out;
}

ret = vfio_device_group_open(device);
ret = vfio_df_group_open(df);
if (ret)
goto err_free;

Expand Down Expand Up @@ -260,7 +268,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
return filep;

err_close_device:
vfio_device_group_close(device);
vfio_df_group_close(df);
err_free:
kfree(df);
err_out:
Expand Down
8 changes: 4 additions & 4 deletions drivers/vfio/vfio.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ struct vfio_device_file {

spinlock_t kvm_ref_lock; /* protect kvm field */
struct kvm *kvm;
struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
};

void vfio_device_put_registration(struct vfio_device *device);
bool vfio_device_try_get_registration(struct vfio_device *device);
int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd);
void vfio_device_close(struct vfio_device *device,
struct iommufd_ctx *iommufd);
int vfio_df_open(struct vfio_device_file *df);
void vfio_df_close(struct vfio_device_file *df);
struct vfio_device_file *
vfio_allocate_device_file(struct vfio_device *device);

Expand Down Expand Up @@ -92,7 +92,7 @@ void vfio_device_group_register(struct vfio_device *device);
void vfio_device_group_unregister(struct vfio_device *device);
int vfio_device_group_use_iommu(struct vfio_device *device);
void vfio_device_group_unuse_iommu(struct vfio_device *device);
void vfio_device_group_close(struct vfio_device *device);
void vfio_df_group_close(struct vfio_device_file *df);
struct vfio_group *vfio_group_from_file(struct file *file);
bool vfio_group_enforced_coherent(struct vfio_group *group);
void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
Expand Down
25 changes: 15 additions & 10 deletions drivers/vfio/vfio_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,10 @@ vfio_allocate_device_file(struct vfio_device *device)
return df;
}

static int vfio_device_first_open(struct vfio_device *device,
struct iommufd_ctx *iommufd)
static int vfio_df_device_first_open(struct vfio_device_file *df)
{
struct vfio_device *device = df->device;
struct iommufd_ctx *iommufd = df->iommufd;
int ret;

lockdep_assert_held(&device->dev_set->lock);
Expand Down Expand Up @@ -468,9 +469,11 @@ static int vfio_device_first_open(struct vfio_device *device,
return ret;
}

static void vfio_device_last_close(struct vfio_device *device,
struct iommufd_ctx *iommufd)
static void vfio_df_device_last_close(struct vfio_device_file *df)
{
struct vfio_device *device = df->device;
struct iommufd_ctx *iommufd = df->iommufd;

lockdep_assert_held(&device->dev_set->lock);

if (device->ops->close_device)
Expand All @@ -482,30 +485,32 @@ static void vfio_device_last_close(struct vfio_device *device,
module_put(device->dev->driver->owner);
}

int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd)
int vfio_df_open(struct vfio_device_file *df)
{
struct vfio_device *device = df->device;
int ret = 0;

lockdep_assert_held(&device->dev_set->lock);

device->open_count++;
if (device->open_count == 1) {
ret = vfio_device_first_open(device, iommufd);
ret = vfio_df_device_first_open(df);
if (ret)
device->open_count--;
}

return ret;
}

void vfio_device_close(struct vfio_device *device,
struct iommufd_ctx *iommufd)
void vfio_df_close(struct vfio_device_file *df)
{
struct vfio_device *device = df->device;

lockdep_assert_held(&device->dev_set->lock);

vfio_assert_device_open(device);
if (device->open_count == 1)
vfio_device_last_close(device, iommufd);
vfio_df_device_last_close(df);
device->open_count--;
}

Expand Down Expand Up @@ -550,7 +555,7 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
struct vfio_device_file *df = filep->private_data;
struct vfio_device *device = df->device;

vfio_device_group_close(device);
vfio_df_group_close(df);

vfio_device_put_registration(device);

Expand Down

0 comments on commit 05f37e1

Please sign in to comment.