]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
iommufd/selftest: Add coverage for IOPF test
authorLu Baolu <baolu.lu@linux.intel.com>
Tue, 2 Jul 2024 06:34:44 +0000 (14:34 +0800)
committerJason Gunthorpe <jgg@nvidia.com>
Tue, 9 Jul 2024 16:54:32 +0000 (13:54 -0300)
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/20240702063444.105814-11-baolu.lu@linux.intel.com
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
tools/testing/selftests/iommu/iommufd.c
tools/testing/selftests/iommu/iommufd_fail_nth.c
tools/testing/selftests/iommu/iommufd_utils.h

index edf1c99c9936c8549e8a2938a2ff11875197b3d4..93634e53e95e7e2545961291dd65f161d6d2832f 100644 (file)
@@ -279,6 +279,9 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested)
        uint32_t parent_hwpt_id = 0;
        uint32_t parent_hwpt_id_not_work = 0;
        uint32_t test_hwpt_id = 0;
+       uint32_t iopf_hwpt_id;
+       uint32_t fault_id;
+       uint32_t fault_fd;
 
        if (self->device_id) {
                /* Negative tests */
@@ -326,6 +329,7 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested)
                                           sizeof(data));
 
                /* Allocate two nested hwpts sharing one common parent hwpt */
+               test_ioctl_fault_alloc(&fault_id, &fault_fd);
                test_cmd_hwpt_alloc_nested(self->device_id, parent_hwpt_id, 0,
                                           &nested_hwpt_id[0],
                                           IOMMU_HWPT_DATA_SELFTEST, &data,
@@ -334,6 +338,14 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested)
                                           &nested_hwpt_id[1],
                                           IOMMU_HWPT_DATA_SELFTEST, &data,
                                           sizeof(data));
+               test_err_hwpt_alloc_iopf(ENOENT, self->device_id, parent_hwpt_id,
+                                        UINT32_MAX, IOMMU_HWPT_FAULT_ID_VALID,
+                                        &iopf_hwpt_id, IOMMU_HWPT_DATA_SELFTEST,
+                                        &data, sizeof(data));
+               test_cmd_hwpt_alloc_iopf(self->device_id, parent_hwpt_id, fault_id,
+                                        IOMMU_HWPT_FAULT_ID_VALID, &iopf_hwpt_id,
+                                        IOMMU_HWPT_DATA_SELFTEST, &data,
+                                        sizeof(data));
                test_cmd_hwpt_check_iotlb_all(nested_hwpt_id[0],
                                              IOMMU_TEST_IOTLB_DEFAULT);
                test_cmd_hwpt_check_iotlb_all(nested_hwpt_id[1],
@@ -504,14 +516,24 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested)
                             _test_ioctl_destroy(self->fd, nested_hwpt_id[1]));
                test_ioctl_destroy(nested_hwpt_id[0]);
 
+               /* Switch from nested_hwpt_id[1] to iopf_hwpt_id */
+               test_cmd_mock_domain_replace(self->stdev_id, iopf_hwpt_id);
+               EXPECT_ERRNO(EBUSY,
+                            _test_ioctl_destroy(self->fd, iopf_hwpt_id));
+               /* Trigger an IOPF on the device */
+               test_cmd_trigger_iopf(self->device_id, fault_fd);
+
                /* Detach from nested_hwpt_id[1] and destroy it */
                test_cmd_mock_domain_replace(self->stdev_id, parent_hwpt_id);
                test_ioctl_destroy(nested_hwpt_id[1]);
+               test_ioctl_destroy(iopf_hwpt_id);
 
                /* Detach from the parent hw_pagetable and destroy it */
                test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id);
                test_ioctl_destroy(parent_hwpt_id);
                test_ioctl_destroy(parent_hwpt_id_not_work);
+               close(fault_fd);
+               test_ioctl_destroy(fault_id);
        } else {
                test_err_hwpt_alloc(ENOENT, self->device_id, self->ioas_id, 0,
                                    &parent_hwpt_id);
index f590417cd67a95bf31065f9b7682bc7c627ebf3a..c5d5e69452b010946dc42e2f0dc1a71c0a0480c7 100644 (file)
@@ -615,7 +615,7 @@ TEST_FAIL_NTH(basic_fail_nth, device)
        if (_test_cmd_get_hw_info(self->fd, idev_id, &info, sizeof(info), NULL))
                return -1;
 
-       if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, 0, &hwpt_id,
+       if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, 0, 0, &hwpt_id,
                                 IOMMU_HWPT_DATA_NONE, 0, 0))
                return -1;
 
index 8d2b46b2114da814f75740992c0dc4b1be14d33b..a70e35a7858408815984b65c84eb3415afe183ba 100644 (file)
@@ -153,7 +153,7 @@ static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id,
        EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace(self->fd, stdev_id, \
                                                           pt_id, NULL))
 
-static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id,
+static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, __u32 ft_id,
                                __u32 flags, __u32 *hwpt_id, __u32 data_type,
                                void *data, size_t data_len)
 {
@@ -165,6 +165,7 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id,
                .data_type = data_type,
                .data_len = data_len,
                .data_uptr = (uint64_t)data,
+               .fault_id = ft_id,
        };
        int ret;
 
@@ -177,24 +178,36 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id,
 }
 
 #define test_cmd_hwpt_alloc(device_id, pt_id, flags, hwpt_id)                  \
-       ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, flags,   \
+       ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, 0, flags,   \
                                          hwpt_id, IOMMU_HWPT_DATA_NONE, NULL, \
                                          0))
 #define test_err_hwpt_alloc(_errno, device_id, pt_id, flags, hwpt_id)   \
        EXPECT_ERRNO(_errno, _test_cmd_hwpt_alloc(                      \
-                                    self->fd, device_id, pt_id, flags, \
+                                    self->fd, device_id, pt_id, 0, flags, \
                                     hwpt_id, IOMMU_HWPT_DATA_NONE, NULL, 0))
 
 #define test_cmd_hwpt_alloc_nested(device_id, pt_id, flags, hwpt_id,         \
                                   data_type, data, data_len)                \
-       ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, flags, \
+       ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, 0, flags, \
                                          hwpt_id, data_type, data, data_len))
 #define test_err_hwpt_alloc_nested(_errno, device_id, pt_id, flags, hwpt_id, \
                                   data_type, data, data_len)                \
        EXPECT_ERRNO(_errno,                                                 \
-                    _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, flags, \
+                    _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, 0, flags, \
                                          hwpt_id, data_type, data, data_len))
 
+#define test_cmd_hwpt_alloc_iopf(device_id, pt_id, fault_id, flags, hwpt_id,    \
+                                  data_type, data, data_len)                   \
+       ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, fault_id, \
+                                         flags, hwpt_id, data_type, data,      \
+                                         data_len))
+#define test_err_hwpt_alloc_iopf(_errno, device_id, pt_id, fault_id, flags,     \
+                                hwpt_id, data_type, data, data_len)            \
+       EXPECT_ERRNO(_errno,                                                    \
+                    _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, fault_id, \
+                                         flags, hwpt_id, data_type, data,      \
+                                         data_len))
+
 #define test_cmd_hwpt_check_iotlb(hwpt_id, iotlb_id, expected)                 \
        ({                                                                     \
                struct iommu_test_cmd test_cmd = {                             \
@@ -684,3 +697,66 @@ static int _test_cmd_get_hw_info(int fd, __u32 device_id, void *data,
 
 #define test_cmd_get_hw_capabilities(device_id, caps, mask) \
        ASSERT_EQ(0, _test_cmd_get_hw_info(self->fd, device_id, NULL, 0, &caps))
+
+static int _test_ioctl_fault_alloc(int fd, __u32 *fault_id, __u32 *fault_fd)
+{
+       struct iommu_fault_alloc cmd = {
+               .size = sizeof(cmd),
+       };
+       int ret;
+
+       ret = ioctl(fd, IOMMU_FAULT_QUEUE_ALLOC, &cmd);
+       if (ret)
+               return ret;
+       *fault_id = cmd.out_fault_id;
+       *fault_fd = cmd.out_fault_fd;
+       return 0;
+}
+
+#define test_ioctl_fault_alloc(fault_id, fault_fd)                       \
+       ({                                                               \
+               ASSERT_EQ(0, _test_ioctl_fault_alloc(self->fd, fault_id, \
+                                                    fault_fd));         \
+               ASSERT_NE(0, *(fault_id));                               \
+               ASSERT_NE(0, *(fault_fd));                               \
+       })
+
+static int _test_cmd_trigger_iopf(int fd, __u32 device_id, __u32 fault_fd)
+{
+       struct iommu_test_cmd trigger_iopf_cmd = {
+               .size = sizeof(trigger_iopf_cmd),
+               .op = IOMMU_TEST_OP_TRIGGER_IOPF,
+               .trigger_iopf = {
+                       .dev_id = device_id,
+                       .pasid = 0x1,
+                       .grpid = 0x2,
+                       .perm = IOMMU_PGFAULT_PERM_READ | IOMMU_PGFAULT_PERM_WRITE,
+                       .addr = 0xdeadbeaf,
+               },
+       };
+       struct iommu_hwpt_page_response response = {
+               .code = IOMMUFD_PAGE_RESP_SUCCESS,
+       };
+       struct iommu_hwpt_pgfault fault = {};
+       ssize_t bytes;
+       int ret;
+
+       ret = ioctl(fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_TRIGGER_IOPF), &trigger_iopf_cmd);
+       if (ret)
+               return ret;
+
+       bytes = read(fault_fd, &fault, sizeof(fault));
+       if (bytes <= 0)
+               return -EIO;
+
+       response.cookie = fault.cookie;
+
+       bytes = write(fault_fd, &response, sizeof(response));
+       if (bytes <= 0)
+               return -EIO;
+
+       return 0;
+}
+
+#define test_cmd_trigger_iopf(device_id, fault_fd) \
+       ASSERT_EQ(0, _test_cmd_trigger_iopf(self->fd, device_id, fault_fd))