Skip to content

Commit 6583c86

Browse files
committed
iommufd/selftest: Add a selftest for IOMMU_HWPT_ALLOC
Test the basic flow. Link: https://lore.kernel.org/r/19-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com Reviewed-by: Kevin Tian <kevin.tian@intel.com> Tested-by: Nicolin Chen <nicolinc@nvidia.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 7a467e0 commit 6583c86

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

drivers/iommu/iommufd/io_pagetable.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,9 @@ int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt,
11801180
unsigned int num_sw_msi = 0;
11811181
int rc;
11821182

1183+
if (iommufd_should_fail())
1184+
return -EINVAL;
1185+
11831186
down_write(&iopt->iova_rwsem);
11841187
/* FIXME: drivers allocate memory but there is no failure propogated */
11851188
iommu_get_resv_regions(dev, &resv_regions);

tools/testing/selftests/iommu/iommufd.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,21 @@ TEST_F(iommufd_mock_domain, replace)
13381338
test_ioctl_destroy(ioas_id);
13391339
}
13401340

1341+
TEST_F(iommufd_mock_domain, alloc_hwpt)
1342+
{
1343+
int i;
1344+
1345+
for (i = 0; i != variant->mock_domains; i++) {
1346+
uint32_t stddev_id;
1347+
uint32_t hwpt_id;
1348+
1349+
test_cmd_hwpt_alloc(self->idev_ids[0], self->ioas_id, &hwpt_id);
1350+
test_cmd_mock_domain(hwpt_id, &stddev_id, NULL, NULL);
1351+
test_ioctl_destroy(stddev_id);
1352+
test_ioctl_destroy(hwpt_id);
1353+
}
1354+
}
1355+
13411356
/* VFIO compatibility IOCTLs */
13421357

13431358
TEST_F(iommufd, simple_ioctls)

tools/testing/selftests/iommu/iommufd_fail_nth.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,8 @@ TEST_FAIL_NTH(basic_fail_nth, device)
579579
uint32_t ioas_id;
580580
uint32_t ioas_id2;
581581
uint32_t stdev_id;
582+
uint32_t idev_id;
583+
uint32_t hwpt_id;
582584
__u64 iova;
583585

584586
self->fd = open("/dev/iommu", O_RDWR);
@@ -605,11 +607,18 @@ TEST_FAIL_NTH(basic_fail_nth, device)
605607

606608
fail_nth_enable();
607609

608-
if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, NULL))
610+
if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL,
611+
&idev_id))
612+
return -1;
613+
614+
if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, &hwpt_id))
609615
return -1;
610616

611617
if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL))
612618
return -1;
619+
620+
if (_test_cmd_mock_domain_replace(self->fd, stdev_id, hwpt_id, NULL))
621+
return -1;
613622
return 0;
614623
}
615624

tools/testing/selftests/iommu/iommufd_utils.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,27 @@ static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id,
9898
EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace(self->fd, stdev_id, \
9999
pt_id, NULL))
100100

101+
static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id,
102+
__u32 *hwpt_id)
103+
{
104+
struct iommu_hwpt_alloc cmd = {
105+
.size = sizeof(cmd),
106+
.dev_id = device_id,
107+
.pt_id = pt_id,
108+
};
109+
int ret;
110+
111+
ret = ioctl(fd, IOMMU_HWPT_ALLOC, &cmd);
112+
if (ret)
113+
return ret;
114+
if (hwpt_id)
115+
*hwpt_id = cmd.out_hwpt_id;
116+
return 0;
117+
}
118+
119+
#define test_cmd_hwpt_alloc(device_id, pt_id, hwpt_id) \
120+
ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, hwpt_id))
121+
101122
static int _test_cmd_create_access(int fd, unsigned int ioas_id,
102123
__u32 *access_id, unsigned int flags)
103124
{

0 commit comments

Comments
 (0)