Skip to content

Commit 31014ae

Browse files
yiliu1765awilliam
authored andcommitted
vfio: Record devid in vfio_device_file
.bind_iommufd() will generate an ID to represent this bond, which is needed by userspace for further usage. Store devid in vfio_device_file to avoid passing the pointer in multiple places. Reviewed-by: Kevin Tian <[email protected]> Reviewed-by: Jason Gunthorpe <[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 6f240ee commit 31014ae

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

drivers/vfio/iommufd.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ bool vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev,
1818
return !iommufd_vfio_compat_ioas_get_id(ictx, &ioas_id);
1919
}
2020

21-
int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx)
21+
int vfio_df_iommufd_bind(struct vfio_device_file *df)
2222
{
23-
u32 device_id;
23+
struct vfio_device *vdev = df->device;
24+
struct iommufd_ctx *ictx = df->iommufd;
2425

2526
lockdep_assert_held(&vdev->dev_set->lock);
2627

27-
/* The legacy path has no way to return the device id */
28-
return vdev->ops->bind_iommufd(vdev, ictx, &device_id);
28+
return vdev->ops->bind_iommufd(vdev, ictx, &df->devid);
2929
}
3030

3131
int vfio_iommufd_compat_attach_ioas(struct vfio_device *vdev,
@@ -48,8 +48,10 @@ int vfio_iommufd_compat_attach_ioas(struct vfio_device *vdev,
4848
return vdev->ops->attach_ioas(vdev, &ioas_id);
4949
}
5050

51-
void vfio_iommufd_unbind(struct vfio_device *vdev)
51+
void vfio_df_iommufd_unbind(struct vfio_device_file *df)
5252
{
53+
struct vfio_device *vdev = df->device;
54+
5355
lockdep_assert_held(&vdev->dev_set->lock);
5456

5557
if (vfio_device_is_noiommu(vdev))

drivers/vfio/vfio.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct vfio_device_file {
2121
struct vfio_group *group;
2222

2323
u8 access_granted;
24+
u32 devid; /* only valid when iommufd is valid */
2425
spinlock_t kvm_ref_lock; /* protect kvm field */
2526
struct kvm *kvm;
2627
struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
@@ -236,8 +237,8 @@ static inline void vfio_container_cleanup(void)
236237
#if IS_ENABLED(CONFIG_IOMMUFD)
237238
bool vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev,
238239
struct iommufd_ctx *ictx);
239-
int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx);
240-
void vfio_iommufd_unbind(struct vfio_device *device);
240+
int vfio_df_iommufd_bind(struct vfio_device_file *df);
241+
void vfio_df_iommufd_unbind(struct vfio_device_file *df);
241242
int vfio_iommufd_compat_attach_ioas(struct vfio_device *device,
242243
struct iommufd_ctx *ictx);
243244
#else
@@ -248,13 +249,12 @@ vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev,
248249
return false;
249250
}
250251

251-
static inline int vfio_iommufd_bind(struct vfio_device *device,
252-
struct iommufd_ctx *ictx)
252+
static inline int vfio_df_iommufd_bind(struct vfio_device_file *fd)
253253
{
254254
return -EOPNOTSUPP;
255255
}
256256

257-
static inline void vfio_iommufd_unbind(struct vfio_device *device)
257+
static inline void vfio_df_iommufd_unbind(struct vfio_device_file *df)
258258
{
259259
}
260260

drivers/vfio/vfio_main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ static int vfio_df_device_first_open(struct vfio_device_file *df)
446446
return -ENODEV;
447447

448448
if (iommufd)
449-
ret = vfio_iommufd_bind(device, iommufd);
449+
ret = vfio_df_iommufd_bind(df);
450450
else
451451
ret = vfio_device_group_use_iommu(device);
452452
if (ret)
@@ -461,7 +461,7 @@ static int vfio_df_device_first_open(struct vfio_device_file *df)
461461

462462
err_unuse_iommu:
463463
if (iommufd)
464-
vfio_iommufd_unbind(device);
464+
vfio_df_iommufd_unbind(df);
465465
else
466466
vfio_device_group_unuse_iommu(device);
467467
err_module_put:
@@ -479,7 +479,7 @@ static void vfio_df_device_last_close(struct vfio_device_file *df)
479479
if (device->ops->close_device)
480480
device->ops->close_device(device);
481481
if (iommufd)
482-
vfio_iommufd_unbind(device);
482+
vfio_df_iommufd_unbind(df);
483483
else
484484
vfio_device_group_unuse_iommu(device);
485485
module_put(device->dev->driver->owner);

0 commit comments

Comments
 (0)