Skip to content

Commit 4086636

Browse files
yiliu1765jgunthorpe
authored andcommitted
iommufd/selftest: Add domain_alloc_user() support in iommu mock
Add mock_domain_alloc_user() and a new test case for IOMMU_HWPT_ALLOC_NEST_PARENT. Link: https://lore.kernel.org/r/20230928071528.26258-6-yi.l.liu@intel.com Co-developed-by: Nicolin Chen <nicolinc@nvidia.com> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> Signed-off-by: Yi Liu <yi.l.liu@intel.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 4ff5421 commit 4086636

4 files changed

Lines changed: 48 additions & 8 deletions

File tree

drivers/iommu/iommufd/selftest.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ static void *mock_domain_hw_info(struct device *dev, u32 *length, u32 *type)
146146
return info;
147147
}
148148

149+
static const struct iommu_ops mock_ops;
150+
149151
static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type)
150152
{
151153
struct mock_iommu_domain *mock;
@@ -162,10 +164,26 @@ static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type)
162164
mock->domain.geometry.aperture_start = MOCK_APERTURE_START;
163165
mock->domain.geometry.aperture_end = MOCK_APERTURE_LAST;
164166
mock->domain.pgsize_bitmap = MOCK_IO_PAGE_SIZE;
167+
mock->domain.ops = mock_ops.default_domain_ops;
168+
mock->domain.type = iommu_domain_type;
165169
xa_init(&mock->pfns);
166170
return &mock->domain;
167171
}
168172

173+
static struct iommu_domain *
174+
mock_domain_alloc_user(struct device *dev, u32 flags)
175+
{
176+
struct iommu_domain *domain;
177+
178+
if (flags & (~IOMMU_HWPT_ALLOC_NEST_PARENT))
179+
return ERR_PTR(-EOPNOTSUPP);
180+
181+
domain = mock_domain_alloc(IOMMU_DOMAIN_UNMANAGED);
182+
if (!domain)
183+
domain = ERR_PTR(-ENOMEM);
184+
return domain;
185+
}
186+
169187
static void mock_domain_free(struct iommu_domain *domain)
170188
{
171189
struct mock_iommu_domain *mock =
@@ -307,6 +325,7 @@ static const struct iommu_ops mock_ops = {
307325
.pgsize_bitmap = MOCK_IO_PAGE_SIZE,
308326
.hw_info = mock_domain_hw_info,
309327
.domain_alloc = mock_domain_alloc,
328+
.domain_alloc_user = mock_domain_alloc_user,
310329
.capable = mock_domain_capable,
311330
.set_platform_dma_ops = mock_domain_set_plaform_dma_ops,
312331
.device_group = generic_device_group,

tools/testing/selftests/iommu/iommufd.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ TEST_F(iommufd, cmd_length)
114114

115115
TEST_LENGTH(iommu_destroy, IOMMU_DESTROY);
116116
TEST_LENGTH(iommu_hw_info, IOMMU_GET_HW_INFO);
117+
TEST_LENGTH(iommu_hwpt_alloc, IOMMU_HWPT_ALLOC);
117118
TEST_LENGTH(iommu_ioas_alloc, IOMMU_IOAS_ALLOC);
118119
TEST_LENGTH(iommu_ioas_iova_ranges, IOMMU_IOAS_IOVA_RANGES);
119120
TEST_LENGTH(iommu_ioas_allow_iovas, IOMMU_IOAS_ALLOW_IOVAS);
@@ -1404,13 +1405,28 @@ TEST_F(iommufd_mock_domain, alloc_hwpt)
14041405
int i;
14051406

14061407
for (i = 0; i != variant->mock_domains; i++) {
1408+
uint32_t hwpt_id[2];
14071409
uint32_t stddev_id;
1408-
uint32_t hwpt_id;
14091410

1410-
test_cmd_hwpt_alloc(self->idev_ids[i], self->ioas_id, &hwpt_id);
1411-
test_cmd_mock_domain(hwpt_id, &stddev_id, NULL, NULL);
1411+
test_err_hwpt_alloc(EOPNOTSUPP,
1412+
self->idev_ids[i], self->ioas_id,
1413+
~IOMMU_HWPT_ALLOC_NEST_PARENT, &hwpt_id[0]);
1414+
test_cmd_hwpt_alloc(self->idev_ids[i], self->ioas_id,
1415+
0, &hwpt_id[0]);
1416+
test_cmd_hwpt_alloc(self->idev_ids[i], self->ioas_id,
1417+
IOMMU_HWPT_ALLOC_NEST_PARENT, &hwpt_id[1]);
1418+
1419+
/* Do a hw_pagetable rotation test */
1420+
test_cmd_mock_domain_replace(self->stdev_ids[i], hwpt_id[0]);
1421+
EXPECT_ERRNO(EBUSY, _test_ioctl_destroy(self->fd, hwpt_id[0]));
1422+
test_cmd_mock_domain_replace(self->stdev_ids[i], hwpt_id[1]);
1423+
EXPECT_ERRNO(EBUSY, _test_ioctl_destroy(self->fd, hwpt_id[1]));
1424+
test_cmd_mock_domain_replace(self->stdev_ids[i], self->ioas_id);
1425+
test_ioctl_destroy(hwpt_id[1]);
1426+
1427+
test_cmd_mock_domain(hwpt_id[0], &stddev_id, NULL, NULL);
14121428
test_ioctl_destroy(stddev_id);
1413-
test_ioctl_destroy(hwpt_id);
1429+
test_ioctl_destroy(hwpt_id[0]);
14141430
}
14151431
}
14161432

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)))
616616
return -1;
617617

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

621621
if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL))

tools/testing/selftests/iommu/iommufd_utils.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,11 @@ static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id,
103103
pt_id, NULL))
104104

105105
static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id,
106-
__u32 *hwpt_id)
106+
__u32 flags, __u32 *hwpt_id)
107107
{
108108
struct iommu_hwpt_alloc cmd = {
109109
.size = sizeof(cmd),
110+
.flags = flags,
110111
.dev_id = device_id,
111112
.pt_id = pt_id,
112113
};
@@ -120,8 +121,12 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id,
120121
return 0;
121122
}
122123

123-
#define test_cmd_hwpt_alloc(device_id, pt_id, hwpt_id) \
124-
ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, hwpt_id))
124+
#define test_cmd_hwpt_alloc(device_id, pt_id, flags, hwpt_id) \
125+
ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, \
126+
pt_id, flags, hwpt_id))
127+
#define test_err_hwpt_alloc(_errno, device_id, pt_id, flags, hwpt_id) \
128+
EXPECT_ERRNO(_errno, _test_cmd_hwpt_alloc(self->fd, device_id, \
129+
pt_id, flags, hwpt_id))
125130

126131
static int _test_cmd_access_replace_ioas(int fd, __u32 access_id,
127132
unsigned int ioas_id)

0 commit comments

Comments
 (0)