Skip to content

Commit 7adf267

Browse files
jpemartinsjgunthorpe
authored andcommitted
iommufd/selftest: Test IOMMU_HWPT_SET_DIRTY_TRACKING
Change mock_domain to supporting dirty tracking and add tests to exercise the new SET_DIRTY_TRACKING API in the iommufd_dirty_tracking selftest fixture. Link: https://lore.kernel.org/r/20231024135109.73787-16-joao.m.martins@oracle.com Signed-off-by: Joao Martins <joao.m.martins@oracle.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 266ce58 commit 7adf267

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

drivers/iommu/iommufd/selftest.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static struct platform_device *selftest_iommu_dev;
2424
size_t iommufd_test_memory_limit = 65536;
2525

2626
enum {
27+
MOCK_DIRTY_TRACK = 1,
2728
MOCK_IO_PAGE_SIZE = PAGE_SIZE / 2,
2829

2930
/*
@@ -86,6 +87,7 @@ void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
8687
}
8788

8889
struct mock_iommu_domain {
90+
unsigned long flags;
8991
struct iommu_domain domain;
9092
struct xarray pfns;
9193
};
@@ -155,6 +157,20 @@ static void *mock_domain_hw_info(struct device *dev, u32 *length, u32 *type)
155157
static int mock_domain_set_dirty_tracking(struct iommu_domain *domain,
156158
bool enable)
157159
{
160+
struct mock_iommu_domain *mock =
161+
container_of(domain, struct mock_iommu_domain, domain);
162+
unsigned long flags = mock->flags;
163+
164+
if (enable && !domain->dirty_ops)
165+
return -EINVAL;
166+
167+
/* No change? */
168+
if (!(enable ^ !!(flags & MOCK_DIRTY_TRACK)))
169+
return 0;
170+
171+
flags = (enable ? flags | MOCK_DIRTY_TRACK : flags & ~MOCK_DIRTY_TRACK);
172+
173+
mock->flags = flags;
158174
return 0;
159175
}
160176

tools/testing/selftests/iommu/iommufd.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,21 @@ TEST_F(iommufd_dirty_tracking, enforce_dirty)
14821482
test_ioctl_destroy(stddev_id);
14831483
}
14841484

1485+
TEST_F(iommufd_dirty_tracking, set_dirty_tracking)
1486+
{
1487+
uint32_t stddev_id;
1488+
uint32_t hwpt_id;
1489+
1490+
test_cmd_hwpt_alloc(self->idev_id, self->ioas_id,
1491+
IOMMU_HWPT_ALLOC_DIRTY_TRACKING, &hwpt_id);
1492+
test_cmd_mock_domain(hwpt_id, &stddev_id, NULL, NULL);
1493+
test_cmd_set_dirty_tracking(hwpt_id, true);
1494+
test_cmd_set_dirty_tracking(hwpt_id, false);
1495+
1496+
test_ioctl_destroy(stddev_id);
1497+
test_ioctl_destroy(hwpt_id);
1498+
}
1499+
14851500
/* VFIO compatibility IOCTLs */
14861501

14871502
TEST_F(iommufd, simple_ioctls)

tools/testing/selftests/iommu/iommufd_utils.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,23 @@ static int _test_cmd_access_replace_ioas(int fd, __u32 access_id,
179179
#define test_cmd_access_replace_ioas(access_id, ioas_id) \
180180
ASSERT_EQ(0, _test_cmd_access_replace_ioas(self->fd, access_id, ioas_id))
181181

182+
static int _test_cmd_set_dirty_tracking(int fd, __u32 hwpt_id, bool enabled)
183+
{
184+
struct iommu_hwpt_set_dirty_tracking cmd = {
185+
.size = sizeof(cmd),
186+
.flags = enabled ? IOMMU_HWPT_DIRTY_TRACKING_ENABLE : 0,
187+
.hwpt_id = hwpt_id,
188+
};
189+
int ret;
190+
191+
ret = ioctl(fd, IOMMU_HWPT_SET_DIRTY_TRACKING, &cmd);
192+
if (ret)
193+
return -errno;
194+
return 0;
195+
}
196+
#define test_cmd_set_dirty_tracking(hwpt_id, enabled) \
197+
ASSERT_EQ(0, _test_cmd_set_dirty_tracking(self->fd, hwpt_id, enabled))
198+
182199
static int _test_cmd_create_access(int fd, unsigned int ioas_id,
183200
__u32 *access_id, unsigned int flags)
184201
{

0 commit comments

Comments
 (0)