Commit 05f37e1c authored by Yi Liu's avatar Yi Liu Committed by Alex Williamson

vfio: Pass struct vfio_device_file * to vfio_device_open/close()

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: default avatarKevin Tian <kevin.tian@intel.com>
Reviewed-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Reviewed-by: default avatarEric Auger <eric.auger@redhat.com>
Tested-by: default avatarTerrence Xu <terrence.xu@intel.com>
Tested-by: default avatarNicolin Chen <nicolinc@nvidia.com>
Tested-by: default avatarMatthew Rosato <mjrosato@linux.ibm.com>
Tested-by: default avatarYanting Jiang <yanting.jiang@intel.com>
Tested-by: default avatarShameer Kolothum <shameerali.kolothum.thodi@huawei.com>
Tested-by: default avatarZhenzhong Duan <zhenzhong.duan@intel.com>
Signed-off-by: default avatarYi Liu <yi.l.liu@intel.com>
Link: https://lore.kernel.org/r/20230718135551.6592-7-yi.l.liu@intel.comSigned-off-by: default avatarAlex Williamson <alex.williamson@redhat.com>
parent dcc31ea6
...@@ -169,8 +169,9 @@ static void vfio_device_group_get_kvm_safe(struct vfio_device *device) ...@@ -169,8 +169,9 @@ static void vfio_device_group_get_kvm_safe(struct vfio_device *device)
spin_unlock(&device->group->kvm_ref_lock); 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; int ret;
mutex_lock(&device->group->group_lock); mutex_lock(&device->group->group_lock);
...@@ -190,7 +191,11 @@ static int vfio_device_group_open(struct vfio_device *device) ...@@ -190,7 +191,11 @@ static int vfio_device_group_open(struct vfio_device *device)
if (device->open_count == 0) if (device->open_count == 0)
vfio_device_group_get_kvm_safe(device); 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) if (device->open_count == 0)
vfio_device_put_kvm(device); vfio_device_put_kvm(device);
...@@ -202,12 +207,15 @@ static int vfio_device_group_open(struct vfio_device *device) ...@@ -202,12 +207,15 @@ static int vfio_device_group_open(struct vfio_device *device)
return ret; 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->group->group_lock);
mutex_lock(&device->dev_set->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) if (device->open_count == 0)
vfio_device_put_kvm(device); vfio_device_put_kvm(device);
...@@ -228,7 +236,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device) ...@@ -228,7 +236,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
goto err_out; goto err_out;
} }
ret = vfio_device_group_open(device); ret = vfio_df_group_open(df);
if (ret) if (ret)
goto err_free; goto err_free;
...@@ -260,7 +268,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device) ...@@ -260,7 +268,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
return filep; return filep;
err_close_device: err_close_device:
vfio_device_group_close(device); vfio_df_group_close(df);
err_free: err_free:
kfree(df); kfree(df);
err_out: err_out:
......
...@@ -21,13 +21,13 @@ struct vfio_device_file { ...@@ -21,13 +21,13 @@ struct vfio_device_file {
spinlock_t kvm_ref_lock; /* protect kvm field */ spinlock_t kvm_ref_lock; /* protect kvm field */
struct kvm *kvm; struct kvm *kvm;
struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
}; };
void vfio_device_put_registration(struct vfio_device *device); void vfio_device_put_registration(struct vfio_device *device);
bool vfio_device_try_get_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); int vfio_df_open(struct vfio_device_file *df);
void vfio_device_close(struct vfio_device *device, void vfio_df_close(struct vfio_device_file *df);
struct iommufd_ctx *iommufd);
struct vfio_device_file * struct vfio_device_file *
vfio_allocate_device_file(struct vfio_device *device); vfio_allocate_device_file(struct vfio_device *device);
...@@ -92,7 +92,7 @@ void vfio_device_group_register(struct vfio_device *device); ...@@ -92,7 +92,7 @@ void vfio_device_group_register(struct vfio_device *device);
void vfio_device_group_unregister(struct vfio_device *device); void vfio_device_group_unregister(struct vfio_device *device);
int vfio_device_group_use_iommu(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_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); struct vfio_group *vfio_group_from_file(struct file *file);
bool vfio_group_enforced_coherent(struct vfio_group *group); bool vfio_group_enforced_coherent(struct vfio_group *group);
void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm); void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
......
...@@ -434,9 +434,10 @@ vfio_allocate_device_file(struct vfio_device *device) ...@@ -434,9 +434,10 @@ vfio_allocate_device_file(struct vfio_device *device)
return df; return df;
} }
static int vfio_device_first_open(struct vfio_device *device, static int vfio_df_device_first_open(struct vfio_device_file *df)
struct iommufd_ctx *iommufd)
{ {
struct vfio_device *device = df->device;
struct iommufd_ctx *iommufd = df->iommufd;
int ret; int ret;
lockdep_assert_held(&device->dev_set->lock); lockdep_assert_held(&device->dev_set->lock);
...@@ -468,9 +469,11 @@ static int vfio_device_first_open(struct vfio_device *device, ...@@ -468,9 +469,11 @@ static int vfio_device_first_open(struct vfio_device *device,
return ret; return ret;
} }
static void vfio_device_last_close(struct vfio_device *device, static void vfio_df_device_last_close(struct vfio_device_file *df)
struct iommufd_ctx *iommufd)
{ {
struct vfio_device *device = df->device;
struct iommufd_ctx *iommufd = df->iommufd;
lockdep_assert_held(&device->dev_set->lock); lockdep_assert_held(&device->dev_set->lock);
if (device->ops->close_device) if (device->ops->close_device)
...@@ -482,15 +485,16 @@ static void vfio_device_last_close(struct vfio_device *device, ...@@ -482,15 +485,16 @@ static void vfio_device_last_close(struct vfio_device *device,
module_put(device->dev->driver->owner); 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; int ret = 0;
lockdep_assert_held(&device->dev_set->lock); lockdep_assert_held(&device->dev_set->lock);
device->open_count++; device->open_count++;
if (device->open_count == 1) { if (device->open_count == 1) {
ret = vfio_device_first_open(device, iommufd); ret = vfio_df_device_first_open(df);
if (ret) if (ret)
device->open_count--; device->open_count--;
} }
...@@ -498,14 +502,15 @@ int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd) ...@@ -498,14 +502,15 @@ int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd)
return ret; return ret;
} }
void vfio_device_close(struct vfio_device *device, void vfio_df_close(struct vfio_device_file *df)
struct iommufd_ctx *iommufd)
{ {
struct vfio_device *device = df->device;
lockdep_assert_held(&device->dev_set->lock); lockdep_assert_held(&device->dev_set->lock);
vfio_assert_device_open(device); vfio_assert_device_open(device);
if (device->open_count == 1) if (device->open_count == 1)
vfio_device_last_close(device, iommufd); vfio_df_device_last_close(df);
device->open_count--; device->open_count--;
} }
...@@ -550,7 +555,7 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep) ...@@ -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_file *df = filep->private_data;
struct vfio_device *device = df->device; struct vfio_device *device = df->device;
vfio_device_group_close(device); vfio_df_group_close(df);
vfio_device_put_registration(device); vfio_device_put_registration(device);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment