Add a new iommufd_viommu FIXTURE and setup it up with a vIOMMU object.
Any new vIOMMU feature will be added as a TEST_F under that.
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
tools/testing/selftests/iommu/iommufd_utils.h | 28 ++++
tools/testing/selftests/iommu/iommufd.c | 128 ++++++++++++++++++
.../selftests/iommu/iommufd_fail_nth.c | 11 ++
3 files changed, 167 insertions(+)
diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h
index 6a11c26370f3..7dabc261fae2 100644
--- a/tools/testing/selftests/iommu/iommufd_utils.h
+++ b/tools/testing/selftests/iommu/iommufd_utils.h
@@ -819,3 +819,31 @@ static int _test_cmd_trigger_iopf(int fd, __u32 device_id, __u32 fault_fd)
#define test_cmd_trigger_iopf(device_id, fault_fd) \
ASSERT_EQ(0, _test_cmd_trigger_iopf(self->fd, device_id, fault_fd))
+
+static int _test_cmd_viommu_alloc(int fd, __u32 device_id, __u32 hwpt_id,
+ __u32 type, __u32 flags, __u32 *viommu_id)
+{
+ struct iommu_viommu_alloc cmd = {
+ .size = sizeof(cmd),
+ .flags = flags,
+ .type = type,
+ .dev_id = device_id,
+ .hwpt_id = hwpt_id,
+ };
+ int ret;
+
+ ret = ioctl(fd, IOMMU_VIOMMU_ALLOC, &cmd);
+ if (ret)
+ return ret;
+ if (viommu_id)
+ *viommu_id = cmd.out_viommu_id;
+ return 0;
+}
+
+#define test_cmd_viommu_alloc(device_id, hwpt_id, type, viommu_id) \
+ ASSERT_EQ(0, _test_cmd_viommu_alloc(self->fd, device_id, hwpt_id, \
+ type, 0, viommu_id))
+#define test_err_viommu_alloc(_errno, device_id, hwpt_id, type, viommu_id) \
+ EXPECT_ERRNO(_errno, \
+ _test_cmd_viommu_alloc(self->fd, device_id, hwpt_id, \
+ type, 0, viommu_id))
diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c
index 88b92bb69756..3142819cc26d 100644
--- a/tools/testing/selftests/iommu/iommufd.c
+++ b/tools/testing/selftests/iommu/iommufd.c
@@ -133,6 +133,7 @@ TEST_F(iommufd, cmd_length)
TEST_LENGTH(iommu_option, IOMMU_OPTION, val64);
TEST_LENGTH(iommu_vfio_ioas, IOMMU_VFIO_IOAS, __reserved);
TEST_LENGTH(iommu_ioas_map_file, IOMMU_IOAS_MAP_FILE, iova);
+ TEST_LENGTH(iommu_viommu_alloc, IOMMU_VIOMMU_ALLOC, out_viommu_id);
#undef TEST_LENGTH
}
@@ -2480,4 +2481,131 @@ TEST_F(vfio_compat_mock_domain, huge_map)
}
}
+FIXTURE(iommufd_viommu)
+{
+ int fd;
+ uint32_t ioas_id;
+ uint32_t stdev_id;
+ uint32_t hwpt_id;
+ uint32_t nested_hwpt_id;
+ uint32_t device_id;
+ uint32_t viommu_id;
+};
+
+FIXTURE_VARIANT(iommufd_viommu)
+{
+ unsigned int viommu;
+};
+
+FIXTURE_SETUP(iommufd_viommu)
+{
+ self->fd = open("/dev/iommu", O_RDWR);
+ ASSERT_NE(-1, self->fd);
+ test_ioctl_ioas_alloc(&self->ioas_id);
+ test_ioctl_set_default_memory_limit();
+
+ if (variant->viommu) {
+ struct iommu_hwpt_selftest data = {
+ .iotlb = IOMMU_TEST_IOTLB_DEFAULT,
+ };
+
+ test_cmd_mock_domain(self->ioas_id, &self->stdev_id, NULL,
+ &self->device_id);
+
+ /* Negative test -- invalid hwpt */
+ test_err_viommu_alloc(ENOENT, self->device_id, self->hwpt_id,
+ IOMMU_VIOMMU_TYPE_SELFTEST, NULL);
+
+ /* Negative test -- not a nesting parent hwpt */
+ test_cmd_hwpt_alloc(self->device_id, self->ioas_id, 0,
+ &self->hwpt_id);
+ test_err_viommu_alloc(EINVAL, self->device_id, self->hwpt_id,
+ IOMMU_VIOMMU_TYPE_SELFTEST, NULL);
+ test_ioctl_destroy(self->hwpt_id);
+
+ /* Allocate a nesting parent hwpt */
+ test_cmd_hwpt_alloc(self->device_id, self->ioas_id,
+ IOMMU_HWPT_ALLOC_NEST_PARENT,
+ &self->hwpt_id);
+ /* Negative test -- unsupported viommu type */
+ test_err_viommu_alloc(EOPNOTSUPP, self->device_id,
+ self->hwpt_id, 0xdead, NULL);
+
+ /* Allocate a vIOMMU taking refcount of the parent hwpt */
+ test_cmd_viommu_alloc(self->device_id, self->hwpt_id,
+ IOMMU_VIOMMU_TYPE_SELFTEST,
+ &self->viommu_id);
+ EXPECT_ERRNO(EBUSY,
+ _test_ioctl_destroy(self->fd, self->hwpt_id));
+
+ /* Allocate a regular nested hwpt */
+ test_cmd_hwpt_alloc_nested(self->device_id, self->viommu_id, 0,
+ &self->nested_hwpt_id,
+ IOMMU_HWPT_DATA_SELFTEST, &data,
+ sizeof(data));
+ EXPECT_ERRNO(EBUSY,
+ _test_ioctl_destroy(self->fd, self->viommu_id));
+ } else {
+ test_err_viommu_alloc(ENOENT, self->device_id, self->hwpt_id,
+ IOMMU_VIOMMU_TYPE_SELFTEST, NULL);
+ }
+}
+
+FIXTURE_TEARDOWN(iommufd_viommu)
+{
+ teardown_iommufd(self->fd, _metadata);
+}
+
+FIXTURE_VARIANT_ADD(iommufd_viommu, no_viommu)
+{
+ .viommu = 0,
+};
+
+FIXTURE_VARIANT_ADD(iommufd_viommu, mock_viommu)
+{
+ .viommu = 1,
+};
+
+TEST_F(iommufd_viommu, viommu_auto_destroy)
+{
+}
+
+TEST_F(iommufd_viommu, viommu_alloc_nested_iopf)
+{
+ struct iommu_hwpt_selftest data = {
+ .iotlb = IOMMU_TEST_IOTLB_DEFAULT,
+ };
+ uint32_t viommu_id = self->viommu_id;
+ uint32_t dev_id = self->device_id;
+ uint32_t iopf_hwpt_id;
+ uint32_t fault_id;
+ uint32_t fault_fd;
+
+ if (self->device_id) {
+ test_ioctl_fault_alloc(&fault_id, &fault_fd);
+ test_err_hwpt_alloc_iopf(
+ ENOENT, dev_id, viommu_id, UINT32_MAX,
+ IOMMU_HWPT_FAULT_ID_VALID, &iopf_hwpt_id,
+ IOMMU_HWPT_DATA_SELFTEST, &data, sizeof(data));
+ test_err_hwpt_alloc_iopf(
+ EOPNOTSUPP, dev_id, viommu_id, fault_id,
+ IOMMU_HWPT_FAULT_ID_VALID | (1 << 31), &iopf_hwpt_id,
+ IOMMU_HWPT_DATA_SELFTEST, &data, sizeof(data));
+ test_cmd_hwpt_alloc_iopf(
+ dev_id, viommu_id, fault_id, IOMMU_HWPT_FAULT_ID_VALID,
+ &iopf_hwpt_id, IOMMU_HWPT_DATA_SELFTEST, &data,
+ sizeof(data));
+
+ test_cmd_mock_domain_replace(self->stdev_id, iopf_hwpt_id);
+ EXPECT_ERRNO(EBUSY,
+ _test_ioctl_destroy(self->fd, iopf_hwpt_id));
+ test_cmd_trigger_iopf(dev_id, fault_fd);
+
+ test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id);
+ test_ioctl_destroy(iopf_hwpt_id);
+ close(fault_fd);
+ test_ioctl_destroy(fault_id);
+ }
+}
+
TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c
index 2d7d01638be8..fb618485d7ca 100644
--- a/tools/testing/selftests/iommu/iommufd_fail_nth.c
+++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c
@@ -621,6 +621,7 @@ TEST_FAIL_NTH(basic_fail_nth, device)
uint32_t stdev_id;
uint32_t idev_id;
uint32_t hwpt_id;
+ uint32_t viommu_id;
__u64 iova;
self->fd = open("/dev/iommu", O_RDWR);
@@ -663,6 +664,16 @@ TEST_FAIL_NTH(basic_fail_nth, device)
if (_test_cmd_mock_domain_replace(self->fd, stdev_id, hwpt_id, NULL))
return -1;
+
+ if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, 0,
+ IOMMU_HWPT_ALLOC_NEST_PARENT, &hwpt_id,
+ IOMMU_HWPT_DATA_NONE, 0, 0))
+ return -1;
+
+ if (_test_cmd_viommu_alloc(self->fd, idev_id, hwpt_id,
+ IOMMU_VIOMMU_TYPE_SELFTEST, 0, &viommu_id))
+ return -1;
+
return 0;
}
--
2.43.0
On Wed, Oct 30, 2024 at 02:34:38PM -0700, Nicolin Chen wrote: > +FIXTURE_SETUP(iommufd_viommu) > +{ > + self->fd = open("/dev/iommu", O_RDWR); > + ASSERT_NE(-1, self->fd); > + test_ioctl_ioas_alloc(&self->ioas_id); > + test_ioctl_set_default_memory_limit(); > + > + if (variant->viommu) { > + struct iommu_hwpt_selftest data = { > + .iotlb = IOMMU_TEST_IOTLB_DEFAULT, > + }; > + > + test_cmd_mock_domain(self->ioas_id, &self->stdev_id, NULL, > + &self->device_id); > + > + /* Negative test -- invalid hwpt */ > + test_err_viommu_alloc(ENOENT, self->device_id, self->hwpt_id, > + IOMMU_VIOMMU_TYPE_SELFTEST, NULL); > + > + /* Negative test -- not a nesting parent hwpt */ > + test_cmd_hwpt_alloc(self->device_id, self->ioas_id, 0, > + &self->hwpt_id); > + test_err_viommu_alloc(EINVAL, self->device_id, self->hwpt_id, > + IOMMU_VIOMMU_TYPE_SELFTEST, NULL); > + test_ioctl_destroy(self->hwpt_id); > + > + /* Allocate a nesting parent hwpt */ > + test_cmd_hwpt_alloc(self->device_id, self->ioas_id, > + IOMMU_HWPT_ALLOC_NEST_PARENT, > + &self->hwpt_id); > + /* Negative test -- unsupported viommu type */ > + test_err_viommu_alloc(EOPNOTSUPP, self->device_id, > + self->hwpt_id, 0xdead, NULL); > + > + /* Allocate a vIOMMU taking refcount of the parent hwpt */ > + test_cmd_viommu_alloc(self->device_id, self->hwpt_id, > + IOMMU_VIOMMU_TYPE_SELFTEST, > + &self->viommu_id); > + EXPECT_ERRNO(EBUSY, > + _test_ioctl_destroy(self->fd, self->hwpt_id)); There shouldn't be testing in the FIXTURE_SETUP, it should just do enough to setup the fixture. negative tests/etc should be in their own function Jason
On Thu, Oct 31, 2024 at 10:16:37AM -0300, Jason Gunthorpe wrote: > On Wed, Oct 30, 2024 at 02:34:38PM -0700, Nicolin Chen wrote: > > +FIXTURE_SETUP(iommufd_viommu) > > +{ > > + self->fd = open("/dev/iommu", O_RDWR); > > + ASSERT_NE(-1, self->fd); > > + test_ioctl_ioas_alloc(&self->ioas_id); > > + test_ioctl_set_default_memory_limit(); > > + > > + if (variant->viommu) { > > + struct iommu_hwpt_selftest data = { > > + .iotlb = IOMMU_TEST_IOTLB_DEFAULT, > > + }; > > + > > + test_cmd_mock_domain(self->ioas_id, &self->stdev_id, NULL, > > + &self->device_id); > > + > > + /* Negative test -- invalid hwpt */ > > + test_err_viommu_alloc(ENOENT, self->device_id, self->hwpt_id, > > + IOMMU_VIOMMU_TYPE_SELFTEST, NULL); > > + > > + /* Negative test -- not a nesting parent hwpt */ > > + test_cmd_hwpt_alloc(self->device_id, self->ioas_id, 0, > > + &self->hwpt_id); > > + test_err_viommu_alloc(EINVAL, self->device_id, self->hwpt_id, > > + IOMMU_VIOMMU_TYPE_SELFTEST, NULL); > > + test_ioctl_destroy(self->hwpt_id); > > + > > + /* Allocate a nesting parent hwpt */ > > + test_cmd_hwpt_alloc(self->device_id, self->ioas_id, > > + IOMMU_HWPT_ALLOC_NEST_PARENT, > > + &self->hwpt_id); > > + /* Negative test -- unsupported viommu type */ > > + test_err_viommu_alloc(EOPNOTSUPP, self->device_id, > > + self->hwpt_id, 0xdead, NULL); > > + > > + /* Allocate a vIOMMU taking refcount of the parent hwpt */ > > + test_cmd_viommu_alloc(self->device_id, self->hwpt_id, > > + IOMMU_VIOMMU_TYPE_SELFTEST, > > + &self->viommu_id); > > + EXPECT_ERRNO(EBUSY, > > + _test_ioctl_destroy(self->fd, self->hwpt_id)); > > There shouldn't be testing in the FIXTURE_SETUP, it should just do > enough to setup the fixture. negative tests/etc should be in their own > function OK. I'll make a change by moving all EXPECT_ERRNOs to a TEST_F. Thanks Nicolin
On Thu, Oct 31, 2024 at 09:38:17AM -0700, Nicolin Chen wrote: > On Thu, Oct 31, 2024 at 10:16:37AM -0300, Jason Gunthorpe wrote: > > On Wed, Oct 30, 2024 at 02:34:38PM -0700, Nicolin Chen wrote: > > > +FIXTURE_SETUP(iommufd_viommu) ... > > > + EXPECT_ERRNO(EBUSY, > > > + _test_ioctl_destroy(self->fd, self->hwpt_id)); > > > > There shouldn't be testing in the FIXTURE_SETUP, it should just do > > enough to setup the fixture. negative tests/etc should be in their own > > function > > OK. I'll make a change by moving all EXPECT_ERRNOs to a TEST_F. Will squash the followings into this patch in v7: diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 4fc5185fca0dc..bad11f65b63dd 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -2424,42 +2424,21 @@ FIXTURE_SETUP(iommufd_viommu) test_cmd_mock_domain(self->ioas_id, &self->stdev_id, NULL, &self->device_id); - /* Negative test -- invalid hwpt */ - test_err_viommu_alloc(ENOENT, self->device_id, self->hwpt_id, - IOMMU_VIOMMU_TYPE_SELFTEST, NULL); - - /* Negative test -- not a nesting parent hwpt */ - test_cmd_hwpt_alloc(self->device_id, self->ioas_id, 0, - &self->hwpt_id); - test_err_viommu_alloc(EINVAL, self->device_id, self->hwpt_id, - IOMMU_VIOMMU_TYPE_SELFTEST, NULL); - test_ioctl_destroy(self->hwpt_id); - /* Allocate a nesting parent hwpt */ test_cmd_hwpt_alloc(self->device_id, self->ioas_id, IOMMU_HWPT_ALLOC_NEST_PARENT, &self->hwpt_id); - /* Negative test -- unsupported viommu type */ - test_err_viommu_alloc(EOPNOTSUPP, self->device_id, - self->hwpt_id, 0xdead, NULL); /* Allocate a vIOMMU taking refcount of the parent hwpt */ test_cmd_viommu_alloc(self->device_id, self->hwpt_id, IOMMU_VIOMMU_TYPE_SELFTEST, &self->viommu_id); - EXPECT_ERRNO(EBUSY, - _test_ioctl_destroy(self->fd, self->hwpt_id)); /* Allocate a regular nested hwpt */ test_cmd_hwpt_alloc_nested(self->device_id, self->viommu_id, 0, &self->nested_hwpt_id, IOMMU_HWPT_DATA_SELFTEST, &data, sizeof(data)); - EXPECT_ERRNO(EBUSY, - _test_ioctl_destroy(self->fd, self->viommu_id)); - } else { - test_err_viommu_alloc(ENOENT, self->device_id, self->hwpt_id, - IOMMU_VIOMMU_TYPE_SELFTEST, NULL); } } @@ -2482,6 +2461,36 @@ TEST_F(iommufd_viommu, viommu_auto_destroy) { } +TEST_F(iommufd_viommu, viommu_negative_tests) +{ + uint32_t device_id = self->device_id; + uint32_t ioas_id = self->ioas_id; + uint32_t hwpt_id; + + if (self->device_id) { + /* Negative test -- invalid hwpt (hwpt_id=0) */ + test_err_viommu_alloc(ENOENT, device_id, 0, + IOMMU_VIOMMU_TYPE_SELFTEST, NULL); + + /* Negative test -- not a nesting parent hwpt */ + test_cmd_hwpt_alloc(device_id, ioas_id, 0, &hwpt_id); + test_err_viommu_alloc(EINVAL, device_id, hwpt_id, + IOMMU_VIOMMU_TYPE_SELFTEST, NULL); + test_ioctl_destroy(hwpt_id); + + /* Negative test -- unsupported viommu type */ + test_err_viommu_alloc(EOPNOTSUPP, device_id, self->hwpt_id, + 0xdead, NULL); + EXPECT_ERRNO(EBUSY, + _test_ioctl_destroy(self->fd, self->hwpt_id)); + EXPECT_ERRNO(EBUSY, + _test_ioctl_destroy(self->fd, self->viommu_id)); + } else { + test_err_viommu_alloc(ENOENT, self->device_id, self->hwpt_id, + IOMMU_VIOMMU_TYPE_SELFTEST, NULL); + } +} + TEST_F(iommufd_viommu, viommu_alloc_nested_iopf) { struct iommu_hwpt_selftest data = {
© 2016 - 2024 Red Hat, Inc.