Skip to content

Commit 05f37e1

Browse files
yiliu1765awilliam
authored andcommitted
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: 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]>
1 parent dcc31ea commit 05f37e1

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

drivers/vfio/group.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@ static void vfio_device_group_get_kvm_safe(struct vfio_device *device)
169169
spin_unlock(&device->group->kvm_ref_lock);
170170
}
171171

172-
static int vfio_device_group_open(struct vfio_device *device)
172+
static int vfio_df_group_open(struct vfio_device_file *df)
173173
{
174+
struct vfio_device *device = df->device;
174175
int ret;
175176

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

193-
ret = vfio_device_open(device, device->group->iommufd);
194+
df->iommufd = device->group->iommufd;
195+
196+
ret = vfio_df_open(df);
197+
if (ret)
198+
df->iommufd = NULL;
194199

195200
if (device->open_count == 0)
196201
vfio_device_put_kvm(device);
@@ -202,12 +207,15 @@ static int vfio_device_group_open(struct vfio_device *device)
202207
return ret;
203208
}
204209

205-
void vfio_device_group_close(struct vfio_device *device)
210+
void vfio_df_group_close(struct vfio_device_file *df)
206211
{
212+
struct vfio_device *device = df->device;
213+
207214
mutex_lock(&device->group->group_lock);
208215
mutex_lock(&device->dev_set->lock);
209216

210-
vfio_device_close(device, device->group->iommufd);
217+
vfio_df_close(df);
218+
df->iommufd = NULL;
211219

212220
if (device->open_count == 0)
213221
vfio_device_put_kvm(device);
@@ -228,7 +236,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
228236
goto err_out;
229237
}
230238

231-
ret = vfio_device_group_open(device);
239+
ret = vfio_df_group_open(df);
232240
if (ret)
233241
goto err_free;
234242

@@ -260,7 +268,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
260268
return filep;
261269

262270
err_close_device:
263-
vfio_device_group_close(device);
271+
vfio_df_group_close(df);
264272
err_free:
265273
kfree(df);
266274
err_out:

drivers/vfio/vfio.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ struct vfio_device_file {
2121

2222
spinlock_t kvm_ref_lock; /* protect kvm field */
2323
struct kvm *kvm;
24+
struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
2425
};
2526

2627
void vfio_device_put_registration(struct vfio_device *device);
2728
bool vfio_device_try_get_registration(struct vfio_device *device);
28-
int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd);
29-
void vfio_device_close(struct vfio_device *device,
30-
struct iommufd_ctx *iommufd);
29+
int vfio_df_open(struct vfio_device_file *df);
30+
void vfio_df_close(struct vfio_device_file *df);
3131
struct vfio_device_file *
3232
vfio_allocate_device_file(struct vfio_device *device);
3333

@@ -92,7 +92,7 @@ void vfio_device_group_register(struct vfio_device *device);
9292
void vfio_device_group_unregister(struct vfio_device *device);
9393
int vfio_device_group_use_iommu(struct vfio_device *device);
9494
void vfio_device_group_unuse_iommu(struct vfio_device *device);
95-
void vfio_device_group_close(struct vfio_device *device);
95+
void vfio_df_group_close(struct vfio_device_file *df);
9696
struct vfio_group *vfio_group_from_file(struct file *file);
9797
bool vfio_group_enforced_coherent(struct vfio_group *group);
9898
void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);

drivers/vfio/vfio_main.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,10 @@ vfio_allocate_device_file(struct vfio_device *device)
434434
return df;
435435
}
436436

437-
static int vfio_device_first_open(struct vfio_device *device,
438-
struct iommufd_ctx *iommufd)
437+
static int vfio_df_device_first_open(struct vfio_device_file *df)
439438
{
439+
struct vfio_device *device = df->device;
440+
struct iommufd_ctx *iommufd = df->iommufd;
440441
int ret;
441442

442443
lockdep_assert_held(&device->dev_set->lock);
@@ -468,9 +469,11 @@ static int vfio_device_first_open(struct vfio_device *device,
468469
return ret;
469470
}
470471

471-
static void vfio_device_last_close(struct vfio_device *device,
472-
struct iommufd_ctx *iommufd)
472+
static void vfio_df_device_last_close(struct vfio_device_file *df)
473473
{
474+
struct vfio_device *device = df->device;
475+
struct iommufd_ctx *iommufd = df->iommufd;
476+
474477
lockdep_assert_held(&device->dev_set->lock);
475478

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

485-
int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd)
488+
int vfio_df_open(struct vfio_device_file *df)
486489
{
490+
struct vfio_device *device = df->device;
487491
int ret = 0;
488492

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

491495
device->open_count++;
492496
if (device->open_count == 1) {
493-
ret = vfio_device_first_open(device, iommufd);
497+
ret = vfio_df_device_first_open(df);
494498
if (ret)
495499
device->open_count--;
496500
}
497501

498502
return ret;
499503
}
500504

501-
void vfio_device_close(struct vfio_device *device,
502-
struct iommufd_ctx *iommufd)
505+
void vfio_df_close(struct vfio_device_file *df)
503506
{
507+
struct vfio_device *device = df->device;
508+
504509
lockdep_assert_held(&device->dev_set->lock);
505510

506511
vfio_assert_device_open(device);
507512
if (device->open_count == 1)
508-
vfio_device_last_close(device, iommufd);
513+
vfio_df_device_last_close(df);
509514
device->open_count--;
510515
}
511516

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

553-
vfio_device_group_close(device);
558+
vfio_df_group_close(df);
554559

555560
vfio_device_put_registration(device);
556561

0 commit comments

Comments
 (0)