[RFC,v4,4/4] iommufd/selftests: Add coverage for IOMMU_DEVICE_SET/UNSET_DATA

Message ID 688dc847fd15e92c9fadd72d1dc92c2f15161606.1683593949.git.nicolinc@nvidia.com
State New
Headers
Series Add set_dev_data and unset_dev_data support |

Commit Message

Nicolin Chen May 9, 2023, 4:51 a.m. UTC
  Add a new IOMMU_TEST_OP_DEV_CHECK_DATA to verify whether the dev_data
is properly set/unset by the IOMMU_DEVICE_SET/UNSET_DATA.

Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
 tools/testing/selftests/iommu/iommufd.c       | 20 +++++++
 tools/testing/selftests/iommu/iommufd_utils.h | 59 +++++++++++++++++++
 2 files changed, 79 insertions(+)
  

Patch

diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c
index 031e222cebf9..2987e8603418 100644
--- a/tools/testing/selftests/iommu/iommufd.c
+++ b/tools/testing/selftests/iommu/iommufd.c
@@ -1517,6 +1517,26 @@  TEST_F(iommufd_mock_domain, alloc_hwpt)
 	}
 }
 
+TEST_F(iommufd_mock_domain, set_dev_data)
+{
+	struct iommu_test_device_data dev_data = {
+		.val = IOMMU_DEVICE_DATA_SELFTEST,
+	};
+	int i;
+
+	for (i = 0; i != variant->mock_domains; i++) {
+		test_err_device_set_data(ENOENT, 0, &dev_data);
+		test_err_device_set_data(EINVAL, self->idev_ids[i], NULL);
+		test_cmd_device_set_data(self->idev_ids[i], &dev_data);
+		test_err_device_set_data(EEXIST, self->idev_ids[i], &dev_data);
+		test_cmd_dev_check_data(self->idev_ids[i], dev_data.val);
+		test_err_device_unset_data(ENOENT, 0);
+		test_cmd_device_unset_data(self->idev_ids[i]);
+		test_err_device_unset_data(ENOENT, self->idev_ids[i]);
+		test_cmd_dev_check_data(self->idev_ids[i], 0);
+	}
+}
+
 /* VFIO compatibility IOCTLs */
 
 TEST_F(iommufd, simple_ioctls)
diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h
index fef17fb46160..487d45c29c6d 100644
--- a/tools/testing/selftests/iommu/iommufd_utils.h
+++ b/tools/testing/selftests/iommu/iommufd_utils.h
@@ -444,3 +444,62 @@  static int _test_cmd_device_get_hw_info(int fd, __u32 device_id,
 	EXPECT_ERRNO(_errno,                                           \
 		     _test_cmd_device_get_hw_info(self->fd, device_id, \
 						  data_len, data))
+
+#define test_cmd_dev_check_data(device_id, expected)                           \
+	({                                                                     \
+		struct iommu_test_cmd test_cmd = {                             \
+			.size = sizeof(test_cmd),                              \
+			.op = IOMMU_TEST_OP_DEV_CHECK_DATA,                    \
+			.id = device_id,                                       \
+			.check_dev_data = { .val = expected },                 \
+		};                                                             \
+		ASSERT_EQ(0,                                                   \
+			  ioctl(self->fd,                                      \
+				_IOMMU_TEST_CMD(IOMMU_TEST_OP_DEV_CHECK_DATA), \
+				&test_cmd));                                   \
+	})
+
+static int _test_cmd_device_set_data(int fd, __u32 device_id,
+				     struct iommu_test_device_data *dev_data)
+{
+	struct iommu_device_set_data cmd = {
+		.size = sizeof(cmd),
+		.dev_id = device_id,
+		.data_uptr = (uint64_t)dev_data,
+		.data_len = sizeof(*dev_data),
+	};
+	int ret;
+
+	ret = ioctl(fd, IOMMU_DEVICE_SET_DATA, &cmd);
+	if (ret)
+		return ret;
+	return 0;
+}
+
+#define test_cmd_device_set_data(device_id, dev_data) \
+	ASSERT_EQ(0, _test_cmd_device_set_data(self->fd, device_id, dev_data))
+
+#define test_err_device_set_data(_errno, device_id, dev_data) \
+	EXPECT_ERRNO(_errno,                                  \
+		     _test_cmd_device_set_data(self->fd, device_id, dev_data))
+
+static int _test_cmd_device_unset_data(int fd, __u32 device_id)
+{
+	struct iommu_device_unset_data cmd = {
+		.size = sizeof(cmd),
+		.dev_id = device_id,
+	};
+	int ret;
+
+	ret = ioctl(fd, IOMMU_DEVICE_UNSET_DATA, &cmd);
+	if (ret)
+		return ret;
+	return 0;
+}
+
+#define test_cmd_device_unset_data(device_id) \
+	ASSERT_EQ(0, _test_cmd_device_unset_data(self->fd, device_id))
+
+#define test_err_device_unset_data(_errno, device_id) \
+	EXPECT_ERRNO(_errno,                          \
+		     _test_cmd_device_unset_data(self->fd, device_id))