[PATCH v1 11/16] iommufd/selftest: Add coverage for the new mmap interface

Nicolin Chen
2025-04-11T06:38:51Z

Extend the loopback test to a new mmap page.

Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
drivers/iommu/iommufd/iommufd_test.h | 4 +++
drivers/iommu/iommufd/selftest.c | 47 ++++++++++++++++++++-----
tools/testing/selftests/iommu/iommufd.c | 5 +++
3 files changed, 48 insertions(+), 8 deletions(-)

diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h
index fbf9ecb35a13..4e4be0b73391 100644
--- a/drivers/iommu/iommufd/iommufd_test.h
+++ b/drivers/iommu/iommufd/iommufd_test.h
@@ -232,12 +232,16 @@ struct iommu_hwpt_invalidate_selftest {
* (IOMMU_VIOMMU_TYPE_SELFTEST)
* @in_data: Input random data from user space
* @out_data: Output data (matching @in_data) to user space
+ * @out_mmap_pgoff: The offset argument for mmap syscall
+ * @out_mmap_pgsz: Maximum page size for mmap syscall
*
* Simply set @out_data=@in_data for a loopback test
*/
struct iommu_viommu_selftest {
__u32 in_data;
__u32 out_data;
+ __aligned_u64 out_mmap_pgoff;
+ __aligned_u64 out_mmap_pgsz;
};

/* Should not be equal to any defined value in enum iommu_viommu_invalidate_data_type */
diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index b04bd2fbc53d..4e6374f4fad2 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -148,6 +148,9 @@ to_mock_nested(struct iommu_domain *domain)
struct mock_viommu {
struct iommufd_viommu core;
struct mock_iommu_domain *s2_parent;
+
+ unsigned long mmap_pgoff;
+ u32 *page; /* Mmap page to test u32 type of in_data */
};

static inline struct mock_viommu *to_mock_viommu(struct iommufd_viommu *viommu)
@@ -632,9 +635,12 @@ static void mock_viommu_destroy(struct iommufd_viommu *viommu)
{
struct mock_iommu_device *mock_iommu = container_of(
viommu->iommu_dev, struct mock_iommu_device, iommu_dev);
+ struct mock_viommu *mock_viommu = to_mock_viommu(viommu);

if (refcount_dec_and_test(&mock_iommu->users))
complete(&mock_iommu->complete);
+ iommufd_ctx_free_mmap(viommu->ictx, mock_viommu->mmap_pgoff);
+ free_page((unsigned long)mock_viommu->page);

/* iommufd core frees mock_viommu and viommu */
}
@@ -748,8 +754,9 @@ mock_viommu_alloc(struct device *dev, struct iommu_domain *domain,
return ERR_PTR(-EOPNOTSUPP);

if (user_data) {
- rc = iommu_copy_struct_from_user(
- &data, user_data, IOMMU_VIOMMU_TYPE_SELFTEST, out_data);
+ rc = iommu_copy_struct_from_user(&data, user_data,
+ IOMMU_VIOMMU_TYPE_SELFTEST,
+ out_mmap_pgsz);
if (rc)
return ERR_PTR(rc);
}
@@ -760,17 +767,41 @@ mock_viommu_alloc(struct device *dev, struct iommu_domain *domain,
return ERR_CAST(mock_viommu);

if (user_data) {
- data.out_data = data.in_data;
- rc = iommu_copy_struct_to_user(
- user_data, &data, IOMMU_VIOMMU_TYPE_SELFTEST, out_data);
- if (rc) {
- iommufd_struct_destroy(ictx, mock_viommu, core);
- return ERR_PTR(rc);
+ mock_viommu->page =
+ (u32 *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
+ if (!mock_viommu->page) {
+ rc = -ENOMEM;
+ goto err_destroy_struct;
}
+
+ rc = iommufd_ctx_alloc_mmap(ictx, __pa(mock_viommu->page),
+ PAGE_SIZE, false,
+ &mock_viommu->mmap_pgoff);
+ if (rc)
+ goto err_free_page;
+
+ /* For loopback tests on both the page and out_data */
+ *mock_viommu->page = data.in_data;
+ data.out_data = data.in_data;
+ data.out_mmap_pgsz = PAGE_SIZE;
+ data.out_mmap_pgoff = mock_viommu->mmap_pgoff;
+ rc = iommu_copy_struct_to_user(user_data, &data,
+ IOMMU_VIOMMU_TYPE_SELFTEST,
+ out_mmap_pgsz);
+ if (rc)
+ goto err_free_mmap;
}

refcount_inc(&mock_iommu->users);
return &mock_viommu->core;
+
+err_free_mmap:
+ iommufd_ctx_free_mmap(ictx, mock_viommu->mmap_pgoff);
+err_free_page:
+ free_page((unsigned long)mock_viommu->page);
+err_destroy_struct:
+ iommufd_struct_destroy(ictx, mock_viommu, core);
+ return ERR_PTR(rc);
}

static const struct iommu_ops mock_ops = {
diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c
index 8ebbb7fda02d..4aa6411e9a76 100644
--- a/tools/testing/selftests/iommu/iommufd.c
+++ b/tools/testing/selftests/iommu/iommufd.c
@@ -2799,12 +2799,17 @@ TEST_F(iommufd_viommu, viommu_alloc_with_data)
struct iommu_viommu_selftest data = {
.in_data = 0xbeef,
};
+ uint32_t *test;

if (self->device_id) {
test_cmd_viommu_alloc(self->device_id, self->hwpt_id,
IOMMU_VIOMMU_TYPE_SELFTEST, &data,
sizeof(data), &self->viommu_id);
assert(data.out_data == data.in_data);
+ test = mmap(NULL, data.out_mmap_pgsz, PROT_READ | PROT_WRITE,
+ MAP_SHARED, self->fd, data.out_mmap_pgoff);
+ assert(test && *test == data.in_data);
+ munmap(test, data.out_mmap_pgsz);
}
}

--
2.43.0