Skip to content

Commit 5778c75

Browse files
nicolincjgunthorpe
authored andcommitted
iommufd/selftest: Add IOMMU_VDEVICE_ALLOC test coverage
Add a vdevice_alloc op to the viommu mock_viommu_ops for the coverage of IOMMU_VIOMMU_TYPE_SELFTEST allocations. Then, add a vdevice_alloc TEST_F to cover the IOMMU_VDEVICE_ALLOC ioctl. Link: https://patch.msgid.link/r/4b9607e5b86726c8baa7b89bd48123fb44104a23.1730836308.git.nicolinc@nvidia.com Reviewed-by: Kevin Tian <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Signed-off-by: Nicolin Chen <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 0ce5c24 commit 5778c75

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

tools/testing/selftests/iommu/iommufd.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ TEST_F(iommufd, cmd_length)
134134
TEST_LENGTH(iommu_vfio_ioas, IOMMU_VFIO_IOAS, __reserved);
135135
TEST_LENGTH(iommu_ioas_map_file, IOMMU_IOAS_MAP_FILE, iova);
136136
TEST_LENGTH(iommu_viommu_alloc, IOMMU_VIOMMU_ALLOC, out_viommu_id);
137+
TEST_LENGTH(iommu_vdevice_alloc, IOMMU_VDEVICE_ALLOC, virt_id);
137138
#undef TEST_LENGTH
138139
}
139140

@@ -2617,4 +2618,23 @@ TEST_F(iommufd_viommu, viommu_alloc_nested_iopf)
26172618
}
26182619
}
26192620

2621+
TEST_F(iommufd_viommu, vdevice_alloc)
2622+
{
2623+
uint32_t viommu_id = self->viommu_id;
2624+
uint32_t dev_id = self->device_id;
2625+
uint32_t vdev_id = 0;
2626+
2627+
if (dev_id) {
2628+
/* Set vdev_id to 0x99, unset it, and set to 0x88 */
2629+
test_cmd_vdevice_alloc(viommu_id, dev_id, 0x99, &vdev_id);
2630+
test_err_vdevice_alloc(EEXIST, viommu_id, dev_id, 0x99,
2631+
&vdev_id);
2632+
test_ioctl_destroy(vdev_id);
2633+
test_cmd_vdevice_alloc(viommu_id, dev_id, 0x88, &vdev_id);
2634+
test_ioctl_destroy(vdev_id);
2635+
} else {
2636+
test_err_vdevice_alloc(ENOENT, viommu_id, dev_id, 0x99, NULL);
2637+
}
2638+
}
2639+
26202640
TEST_HARNESS_MAIN

tools/testing/selftests/iommu/iommufd_fail_nth.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ TEST_FAIL_NTH(basic_fail_nth, device)
622622
uint32_t idev_id;
623623
uint32_t hwpt_id;
624624
uint32_t viommu_id;
625+
uint32_t vdev_id;
625626
__u64 iova;
626627

627628
self->fd = open("/dev/iommu", O_RDWR);
@@ -674,6 +675,9 @@ TEST_FAIL_NTH(basic_fail_nth, device)
674675
IOMMU_VIOMMU_TYPE_SELFTEST, 0, &viommu_id))
675676
return -1;
676677

678+
if (_test_cmd_vdevice_alloc(self->fd, viommu_id, idev_id, 0, &vdev_id))
679+
return -1;
680+
677681
return 0;
678682
}
679683

tools/testing/selftests/iommu/iommufd_utils.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,3 +847,30 @@ static int _test_cmd_viommu_alloc(int fd, __u32 device_id, __u32 hwpt_id,
847847
EXPECT_ERRNO(_errno, \
848848
_test_cmd_viommu_alloc(self->fd, device_id, hwpt_id, \
849849
type, 0, viommu_id))
850+
851+
static int _test_cmd_vdevice_alloc(int fd, __u32 viommu_id, __u32 idev_id,
852+
__u64 virt_id, __u32 *vdev_id)
853+
{
854+
struct iommu_vdevice_alloc cmd = {
855+
.size = sizeof(cmd),
856+
.dev_id = idev_id,
857+
.viommu_id = viommu_id,
858+
.virt_id = virt_id,
859+
};
860+
int ret;
861+
862+
ret = ioctl(fd, IOMMU_VDEVICE_ALLOC, &cmd);
863+
if (ret)
864+
return ret;
865+
if (vdev_id)
866+
*vdev_id = cmd.out_vdevice_id;
867+
return 0;
868+
}
869+
870+
#define test_cmd_vdevice_alloc(viommu_id, idev_id, virt_id, vdev_id) \
871+
ASSERT_EQ(0, _test_cmd_vdevice_alloc(self->fd, viommu_id, idev_id, \
872+
virt_id, vdev_id))
873+
#define test_err_vdevice_alloc(_errno, viommu_id, idev_id, virt_id, vdev_id) \
874+
EXPECT_ERRNO(_errno, \
875+
_test_cmd_vdevice_alloc(self->fd, viommu_id, idev_id, \
876+
virt_id, vdev_id))

0 commit comments

Comments
 (0)