Skip to content

Commit 6086efe

Browse files
yiliu1765awilliam
authored andcommitted
vfio-iommufd: Move noiommu compat validation out of vfio_iommufd_bind()
This moves the noiommu compat validation logic into vfio_df_group_open(). This is more consistent with what will be done in vfio device cdev path. 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 839e692 commit 6086efe

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

drivers/vfio/group.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,19 @@ static int vfio_df_group_open(struct vfio_device_file *df)
192192
vfio_device_group_get_kvm_safe(device);
193193

194194
df->iommufd = device->group->iommufd;
195+
if (df->iommufd && vfio_device_is_noiommu(device) && device->open_count == 0) {
196+
/*
197+
* Require no compat ioas to be assigned to proceed. The basic
198+
* statement is that the user cannot have done something that
199+
* implies they expected translation to exist
200+
*/
201+
if (!capable(CAP_SYS_RAWIO) ||
202+
vfio_iommufd_device_has_compat_ioas(device, df->iommufd))
203+
ret = -EPERM;
204+
else
205+
ret = 0;
206+
goto out_put_kvm;
207+
}
195208

196209
ret = vfio_df_open(df);
197210
if (ret) {

drivers/vfio/iommufd.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
MODULE_IMPORT_NS(IOMMUFD);
1111
MODULE_IMPORT_NS(IOMMUFD_VFIO);
1212

13+
bool vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev,
14+
struct iommufd_ctx *ictx)
15+
{
16+
u32 ioas_id;
17+
18+
return !iommufd_vfio_compat_ioas_get_id(ictx, &ioas_id);
19+
}
20+
1321
int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx)
1422
{
1523
u32 ioas_id;
@@ -18,20 +26,6 @@ int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx)
1826

1927
lockdep_assert_held(&vdev->dev_set->lock);
2028

21-
if (vfio_device_is_noiommu(vdev)) {
22-
if (!capable(CAP_SYS_RAWIO))
23-
return -EPERM;
24-
25-
/*
26-
* Require no compat ioas to be assigned to proceed. The basic
27-
* statement is that the user cannot have done something that
28-
* implies they expected translation to exist
29-
*/
30-
if (!iommufd_vfio_compat_ioas_get_id(ictx, &ioas_id))
31-
return -EPERM;
32-
return 0;
33-
}
34-
3529
ret = vdev->ops->bind_iommufd(vdev, ictx, &device_id);
3630
if (ret)
3731
return ret;

drivers/vfio/vfio.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,18 @@ static inline void vfio_container_cleanup(void)
234234
#endif
235235

236236
#if IS_ENABLED(CONFIG_IOMMUFD)
237+
bool vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev,
238+
struct iommufd_ctx *ictx);
237239
int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx);
238240
void vfio_iommufd_unbind(struct vfio_device *device);
239241
#else
242+
static inline bool
243+
vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev,
244+
struct iommufd_ctx *ictx)
245+
{
246+
return false;
247+
}
248+
240249
static inline int vfio_iommufd_bind(struct vfio_device *device,
241250
struct iommufd_ctx *ictx)
242251
{

0 commit comments

Comments
 (0)