Skip to content

Commit 266ce58

Browse files
jpemartinsjgunthorpe
authored andcommitted
iommufd/selftest: Test IOMMU_HWPT_ALLOC_DIRTY_TRACKING
In order to selftest the iommu domain dirty enforcing implement the mock_domain necessary support and add a new dev_flags to test that the hwpt_alloc/attach_device fails as expected. Expand the existing mock_domain fixture with a enforce_dirty test that exercises the hwpt_alloc and device attachment. Link: https://lore.kernel.org/r/20231024135109.73787-15-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 e04b23c commit 266ce58

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

drivers/iommu/iommufd/selftest.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ static void mock_domain_blocking_free(struct iommu_domain *domain)
119119
static int mock_domain_nop_attach(struct iommu_domain *domain,
120120
struct device *dev)
121121
{
122+
struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);
123+
124+
if (domain->dirty_ops && (mdev->flags & MOCK_FLAGS_DEVICE_NO_DIRTY))
125+
return -EINVAL;
126+
122127
return 0;
123128
}
124129

@@ -147,6 +152,25 @@ static void *mock_domain_hw_info(struct device *dev, u32 *length, u32 *type)
147152
return info;
148153
}
149154

155+
static int mock_domain_set_dirty_tracking(struct iommu_domain *domain,
156+
bool enable)
157+
{
158+
return 0;
159+
}
160+
161+
static int mock_domain_read_and_clear_dirty(struct iommu_domain *domain,
162+
unsigned long iova, size_t size,
163+
unsigned long flags,
164+
struct iommu_dirty_bitmap *dirty)
165+
{
166+
return 0;
167+
}
168+
169+
const struct iommu_dirty_ops dirty_ops = {
170+
.set_dirty_tracking = mock_domain_set_dirty_tracking,
171+
.read_and_clear_dirty = mock_domain_read_and_clear_dirty,
172+
};
173+
150174
static const struct iommu_ops mock_ops;
151175

152176
static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type)
@@ -174,12 +198,20 @@ static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type)
174198
static struct iommu_domain *
175199
mock_domain_alloc_user(struct device *dev, u32 flags)
176200
{
201+
struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);
177202
struct iommu_domain *domain;
178203

179-
if (flags & (~IOMMU_HWPT_ALLOC_NEST_PARENT))
204+
if (flags &
205+
(~(IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING)))
206+
return ERR_PTR(-EOPNOTSUPP);
207+
208+
if ((flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING) &&
209+
(mdev->flags & MOCK_FLAGS_DEVICE_NO_DIRTY))
180210
return ERR_PTR(-EOPNOTSUPP);
181211

182212
domain = mock_domain_alloc(IOMMU_DOMAIN_UNMANAGED);
213+
if (domain && !(mdev->flags & MOCK_FLAGS_DEVICE_NO_DIRTY))
214+
domain->dirty_ops = &dirty_ops;
183215
if (!domain)
184216
domain = ERR_PTR(-ENOMEM);
185217
return domain;
@@ -387,6 +419,9 @@ static struct mock_dev *mock_dev_create(unsigned long dev_flags)
387419
struct mock_dev *mdev;
388420
int rc;
389421

422+
if (dev_flags & ~(MOCK_FLAGS_DEVICE_NO_DIRTY))
423+
return ERR_PTR(-EINVAL);
424+
390425
mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
391426
if (!mdev)
392427
return ERR_PTR(-ENOMEM);

tools/testing/selftests/iommu/iommufd.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,55 @@ TEST_F(iommufd_mock_domain, alloc_hwpt)
14331433
}
14341434
}
14351435

1436+
FIXTURE(iommufd_dirty_tracking)
1437+
{
1438+
int fd;
1439+
uint32_t ioas_id;
1440+
uint32_t hwpt_id;
1441+
uint32_t stdev_id;
1442+
uint32_t idev_id;
1443+
};
1444+
1445+
FIXTURE_SETUP(iommufd_dirty_tracking)
1446+
{
1447+
self->fd = open("/dev/iommu", O_RDWR);
1448+
ASSERT_NE(-1, self->fd);
1449+
1450+
test_ioctl_ioas_alloc(&self->ioas_id);
1451+
test_cmd_mock_domain(self->ioas_id, &self->stdev_id, &self->hwpt_id,
1452+
&self->idev_id);
1453+
}
1454+
1455+
FIXTURE_TEARDOWN(iommufd_dirty_tracking)
1456+
{
1457+
teardown_iommufd(self->fd, _metadata);
1458+
}
1459+
1460+
TEST_F(iommufd_dirty_tracking, enforce_dirty)
1461+
{
1462+
uint32_t ioas_id, stddev_id, idev_id;
1463+
uint32_t hwpt_id, _hwpt_id;
1464+
uint32_t dev_flags;
1465+
1466+
/* Regular case */
1467+
dev_flags = MOCK_FLAGS_DEVICE_NO_DIRTY;
1468+
test_cmd_hwpt_alloc(self->idev_id, self->ioas_id,
1469+
IOMMU_HWPT_ALLOC_DIRTY_TRACKING, &hwpt_id);
1470+
test_cmd_mock_domain(hwpt_id, &stddev_id, NULL, NULL);
1471+
test_err_mock_domain_flags(EINVAL, hwpt_id, dev_flags, &stddev_id,
1472+
NULL);
1473+
test_ioctl_destroy(stddev_id);
1474+
test_ioctl_destroy(hwpt_id);
1475+
1476+
/* IOMMU device does not support dirty tracking */
1477+
test_ioctl_ioas_alloc(&ioas_id);
1478+
test_cmd_mock_domain_flags(ioas_id, dev_flags, &stddev_id, &_hwpt_id,
1479+
&idev_id);
1480+
test_err_hwpt_alloc(EOPNOTSUPP, idev_id, ioas_id,
1481+
IOMMU_HWPT_ALLOC_DIRTY_TRACKING, &hwpt_id);
1482+
test_ioctl_destroy(stddev_id);
1483+
}
1484+
14361485
/* VFIO compatibility IOCTLs */
14371486

14381487
TEST_F(iommufd, simple_ioctls)

tools/testing/selftests/iommu/iommufd_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ static int _test_cmd_mock_domain_flags(int fd, unsigned int ioas_id,
9898
*idev_id = cmd.mock_domain_flags.out_idev_id;
9999
return 0;
100100
}
101+
#define test_cmd_mock_domain_flags(ioas_id, flags, stdev_id, hwpt_id, idev_id) \
102+
ASSERT_EQ(0, _test_cmd_mock_domain_flags(self->fd, ioas_id, flags, \
103+
stdev_id, hwpt_id, idev_id))
101104
#define test_err_mock_domain_flags(_errno, ioas_id, flags, stdev_id, hwpt_id) \
102105
EXPECT_ERRNO(_errno, \
103106
_test_cmd_mock_domain_flags(self->fd, ioas_id, flags, \

0 commit comments

Comments
 (0)