Skip to content

Commit 6f240ee

Browse files
yiliu1765awilliam
authored andcommitted
vfio-iommufd: Split bind/attach into two steps
This aligns the bind/attach logic with the coming vfio device cdev support. 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 6086efe commit 6f240ee

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

drivers/vfio/group.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,13 @@ static int vfio_df_group_open(struct vfio_device_file *df)
207207
}
208208

209209
ret = vfio_df_open(df);
210-
if (ret) {
211-
df->iommufd = NULL;
210+
if (ret)
212211
goto out_put_kvm;
212+
213+
if (df->iommufd && device->open_count == 1) {
214+
ret = vfio_iommufd_compat_attach_ioas(device, df->iommufd);
215+
if (ret)
216+
goto out_close_device;
213217
}
214218

215219
/*
@@ -218,12 +222,17 @@ static int vfio_df_group_open(struct vfio_device_file *df)
218222
*/
219223
smp_store_release(&df->access_granted, true);
220224

225+
mutex_unlock(&device->dev_set->lock);
226+
mutex_unlock(&device->group->group_lock);
227+
return 0;
228+
229+
out_close_device:
230+
vfio_df_close(df);
221231
out_put_kvm:
232+
df->iommufd = NULL;
222233
if (device->open_count == 0)
223234
vfio_device_put_kvm(device);
224-
225235
mutex_unlock(&device->dev_set->lock);
226-
227236
out_unlock:
228237
mutex_unlock(&device->group->group_lock);
229238
return ret;

drivers/vfio/iommufd.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,32 @@ bool vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev,
2020

2121
int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx)
2222
{
23-
u32 ioas_id;
2423
u32 device_id;
24+
25+
lockdep_assert_held(&vdev->dev_set->lock);
26+
27+
/* The legacy path has no way to return the device id */
28+
return vdev->ops->bind_iommufd(vdev, ictx, &device_id);
29+
}
30+
31+
int vfio_iommufd_compat_attach_ioas(struct vfio_device *vdev,
32+
struct iommufd_ctx *ictx)
33+
{
34+
u32 ioas_id;
2535
int ret;
2636

2737
lockdep_assert_held(&vdev->dev_set->lock);
2838

29-
ret = vdev->ops->bind_iommufd(vdev, ictx, &device_id);
30-
if (ret)
31-
return ret;
39+
/* compat noiommu does not need to do ioas attach */
40+
if (vfio_device_is_noiommu(vdev))
41+
return 0;
3242

3343
ret = iommufd_vfio_compat_ioas_get_id(ictx, &ioas_id);
3444
if (ret)
35-
goto err_unbind;
36-
ret = vdev->ops->attach_ioas(vdev, &ioas_id);
37-
if (ret)
38-
goto err_unbind;
39-
40-
/*
41-
* The legacy path has no way to return the device id or the selected
42-
* pt_id
43-
*/
44-
return 0;
45+
return ret;
4546

46-
err_unbind:
47-
if (vdev->ops->unbind_iommufd)
48-
vdev->ops->unbind_iommufd(vdev);
49-
return ret;
47+
/* The legacy path has no way to return the selected pt_id */
48+
return vdev->ops->attach_ioas(vdev, &ioas_id);
5049
}
5150

5251
void vfio_iommufd_unbind(struct vfio_device *vdev)

drivers/vfio/vfio.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ bool vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev,
238238
struct iommufd_ctx *ictx);
239239
int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx);
240240
void vfio_iommufd_unbind(struct vfio_device *device);
241+
int vfio_iommufd_compat_attach_ioas(struct vfio_device *device,
242+
struct iommufd_ctx *ictx);
241243
#else
242244
static inline bool
243245
vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev,
@@ -255,6 +257,13 @@ static inline int vfio_iommufd_bind(struct vfio_device *device,
255257
static inline void vfio_iommufd_unbind(struct vfio_device *device)
256258
{
257259
}
260+
261+
static inline int
262+
vfio_iommufd_compat_attach_ioas(struct vfio_device *device,
263+
struct iommufd_ctx *ictx)
264+
{
265+
return -EOPNOTSUPP;
266+
}
258267
#endif
259268

260269
#if IS_ENABLED(CONFIG_VFIO_VIRQFD)

0 commit comments

Comments
 (0)