Skip to content

Commit d121176

Browse files
LuBaolujgunthorpe
authored andcommitted
iommufd/selftest: Add coverage for IOPF test
Extend the selftest tool to add coverage of testing IOPF handling. This would include the following tests: - Allocating and destroying an iommufd fault object. - Allocating and destroying an IOPF-capable HWPT. - Attaching/detaching/replacing an IOPF-capable HWPT on a device. - Triggering an IOPF on the mock device. - Retrieving and responding to the IOPF through the file interface. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Lu Baolu <[email protected]> Reviewed-by: Kevin Tian <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent ddee199 commit d121176

File tree

3 files changed

+104
-6
lines changed

3 files changed

+104
-6
lines changed

tools/testing/selftests/iommu/iommufd.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested)
279279
uint32_t parent_hwpt_id = 0;
280280
uint32_t parent_hwpt_id_not_work = 0;
281281
uint32_t test_hwpt_id = 0;
282+
uint32_t iopf_hwpt_id;
283+
uint32_t fault_id;
284+
uint32_t fault_fd;
282285

283286
if (self->device_id) {
284287
/* Negative tests */
@@ -326,6 +329,7 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested)
326329
sizeof(data));
327330

328331
/* Allocate two nested hwpts sharing one common parent hwpt */
332+
test_ioctl_fault_alloc(&fault_id, &fault_fd);
329333
test_cmd_hwpt_alloc_nested(self->device_id, parent_hwpt_id, 0,
330334
&nested_hwpt_id[0],
331335
IOMMU_HWPT_DATA_SELFTEST, &data,
@@ -334,6 +338,14 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested)
334338
&nested_hwpt_id[1],
335339
IOMMU_HWPT_DATA_SELFTEST, &data,
336340
sizeof(data));
341+
test_err_hwpt_alloc_iopf(ENOENT, self->device_id, parent_hwpt_id,
342+
UINT32_MAX, IOMMU_HWPT_FAULT_ID_VALID,
343+
&iopf_hwpt_id, IOMMU_HWPT_DATA_SELFTEST,
344+
&data, sizeof(data));
345+
test_cmd_hwpt_alloc_iopf(self->device_id, parent_hwpt_id, fault_id,
346+
IOMMU_HWPT_FAULT_ID_VALID, &iopf_hwpt_id,
347+
IOMMU_HWPT_DATA_SELFTEST, &data,
348+
sizeof(data));
337349
test_cmd_hwpt_check_iotlb_all(nested_hwpt_id[0],
338350
IOMMU_TEST_IOTLB_DEFAULT);
339351
test_cmd_hwpt_check_iotlb_all(nested_hwpt_id[1],
@@ -504,14 +516,24 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested)
504516
_test_ioctl_destroy(self->fd, nested_hwpt_id[1]));
505517
test_ioctl_destroy(nested_hwpt_id[0]);
506518

519+
/* Switch from nested_hwpt_id[1] to iopf_hwpt_id */
520+
test_cmd_mock_domain_replace(self->stdev_id, iopf_hwpt_id);
521+
EXPECT_ERRNO(EBUSY,
522+
_test_ioctl_destroy(self->fd, iopf_hwpt_id));
523+
/* Trigger an IOPF on the device */
524+
test_cmd_trigger_iopf(self->device_id, fault_fd);
525+
507526
/* Detach from nested_hwpt_id[1] and destroy it */
508527
test_cmd_mock_domain_replace(self->stdev_id, parent_hwpt_id);
509528
test_ioctl_destroy(nested_hwpt_id[1]);
529+
test_ioctl_destroy(iopf_hwpt_id);
510530

511531
/* Detach from the parent hw_pagetable and destroy it */
512532
test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id);
513533
test_ioctl_destroy(parent_hwpt_id);
514534
test_ioctl_destroy(parent_hwpt_id_not_work);
535+
close(fault_fd);
536+
test_ioctl_destroy(fault_id);
515537
} else {
516538
test_err_hwpt_alloc(ENOENT, self->device_id, self->ioas_id, 0,
517539
&parent_hwpt_id);

tools/testing/selftests/iommu/iommufd_fail_nth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ TEST_FAIL_NTH(basic_fail_nth, device)
615615
if (_test_cmd_get_hw_info(self->fd, idev_id, &info, sizeof(info), NULL))
616616
return -1;
617617

618-
if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, 0, &hwpt_id,
618+
if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, 0, 0, &hwpt_id,
619619
IOMMU_HWPT_DATA_NONE, 0, 0))
620620
return -1;
621621

tools/testing/selftests/iommu/iommufd_utils.h

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id,
153153
EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace(self->fd, stdev_id, \
154154
pt_id, NULL))
155155

156-
static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id,
156+
static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, __u32 ft_id,
157157
__u32 flags, __u32 *hwpt_id, __u32 data_type,
158158
void *data, size_t data_len)
159159
{
@@ -165,6 +165,7 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id,
165165
.data_type = data_type,
166166
.data_len = data_len,
167167
.data_uptr = (uint64_t)data,
168+
.fault_id = ft_id,
168169
};
169170
int ret;
170171

@@ -177,24 +178,36 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id,
177178
}
178179

179180
#define test_cmd_hwpt_alloc(device_id, pt_id, flags, hwpt_id) \
180-
ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, flags, \
181+
ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, 0, flags, \
181182
hwpt_id, IOMMU_HWPT_DATA_NONE, NULL, \
182183
0))
183184
#define test_err_hwpt_alloc(_errno, device_id, pt_id, flags, hwpt_id) \
184185
EXPECT_ERRNO(_errno, _test_cmd_hwpt_alloc( \
185-
self->fd, device_id, pt_id, flags, \
186+
self->fd, device_id, pt_id, 0, flags, \
186187
hwpt_id, IOMMU_HWPT_DATA_NONE, NULL, 0))
187188

188189
#define test_cmd_hwpt_alloc_nested(device_id, pt_id, flags, hwpt_id, \
189190
data_type, data, data_len) \
190-
ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, flags, \
191+
ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, 0, flags, \
191192
hwpt_id, data_type, data, data_len))
192193
#define test_err_hwpt_alloc_nested(_errno, device_id, pt_id, flags, hwpt_id, \
193194
data_type, data, data_len) \
194195
EXPECT_ERRNO(_errno, \
195-
_test_cmd_hwpt_alloc(self->fd, device_id, pt_id, flags, \
196+
_test_cmd_hwpt_alloc(self->fd, device_id, pt_id, 0, flags, \
196197
hwpt_id, data_type, data, data_len))
197198

199+
#define test_cmd_hwpt_alloc_iopf(device_id, pt_id, fault_id, flags, hwpt_id, \
200+
data_type, data, data_len) \
201+
ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, fault_id, \
202+
flags, hwpt_id, data_type, data, \
203+
data_len))
204+
#define test_err_hwpt_alloc_iopf(_errno, device_id, pt_id, fault_id, flags, \
205+
hwpt_id, data_type, data, data_len) \
206+
EXPECT_ERRNO(_errno, \
207+
_test_cmd_hwpt_alloc(self->fd, device_id, pt_id, fault_id, \
208+
flags, hwpt_id, data_type, data, \
209+
data_len))
210+
198211
#define test_cmd_hwpt_check_iotlb(hwpt_id, iotlb_id, expected) \
199212
({ \
200213
struct iommu_test_cmd test_cmd = { \
@@ -684,3 +697,66 @@ static int _test_cmd_get_hw_info(int fd, __u32 device_id, void *data,
684697

685698
#define test_cmd_get_hw_capabilities(device_id, caps, mask) \
686699
ASSERT_EQ(0, _test_cmd_get_hw_info(self->fd, device_id, NULL, 0, &caps))
700+
701+
static int _test_ioctl_fault_alloc(int fd, __u32 *fault_id, __u32 *fault_fd)
702+
{
703+
struct iommu_fault_alloc cmd = {
704+
.size = sizeof(cmd),
705+
};
706+
int ret;
707+
708+
ret = ioctl(fd, IOMMU_FAULT_QUEUE_ALLOC, &cmd);
709+
if (ret)
710+
return ret;
711+
*fault_id = cmd.out_fault_id;
712+
*fault_fd = cmd.out_fault_fd;
713+
return 0;
714+
}
715+
716+
#define test_ioctl_fault_alloc(fault_id, fault_fd) \
717+
({ \
718+
ASSERT_EQ(0, _test_ioctl_fault_alloc(self->fd, fault_id, \
719+
fault_fd)); \
720+
ASSERT_NE(0, *(fault_id)); \
721+
ASSERT_NE(0, *(fault_fd)); \
722+
})
723+
724+
static int _test_cmd_trigger_iopf(int fd, __u32 device_id, __u32 fault_fd)
725+
{
726+
struct iommu_test_cmd trigger_iopf_cmd = {
727+
.size = sizeof(trigger_iopf_cmd),
728+
.op = IOMMU_TEST_OP_TRIGGER_IOPF,
729+
.trigger_iopf = {
730+
.dev_id = device_id,
731+
.pasid = 0x1,
732+
.grpid = 0x2,
733+
.perm = IOMMU_PGFAULT_PERM_READ | IOMMU_PGFAULT_PERM_WRITE,
734+
.addr = 0xdeadbeaf,
735+
},
736+
};
737+
struct iommu_hwpt_page_response response = {
738+
.code = IOMMUFD_PAGE_RESP_SUCCESS,
739+
};
740+
struct iommu_hwpt_pgfault fault = {};
741+
ssize_t bytes;
742+
int ret;
743+
744+
ret = ioctl(fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_TRIGGER_IOPF), &trigger_iopf_cmd);
745+
if (ret)
746+
return ret;
747+
748+
bytes = read(fault_fd, &fault, sizeof(fault));
749+
if (bytes <= 0)
750+
return -EIO;
751+
752+
response.cookie = fault.cookie;
753+
754+
bytes = write(fault_fd, &response, sizeof(response));
755+
if (bytes <= 0)
756+
return -EIO;
757+
758+
return 0;
759+
}
760+
761+
#define test_cmd_trigger_iopf(device_id, fault_fd) \
762+
ASSERT_EQ(0, _test_cmd_trigger_iopf(self->fd, device_id, fault_fd))

0 commit comments

Comments
 (0)