Skip to content

Commit d6563aa

Browse files
nicolincjgunthorpe
authored andcommitted
iommufd/selftest: Add mock_viommu_cache_invalidate
Similar to the coverage of cache_invalidate_user for iotlb invalidation, add a device cache and a viommu_cache_invalidate function to test it out. Link: https://patch.msgid.link/r/a29c7c23d7cd143fb26ab68b3618e0957f485fdb.1730836308.git.nicolinc@nvidia.com Reviewed-by: Kevin Tian <[email protected]> Signed-off-by: Nicolin Chen <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent c747e67 commit d6563aa

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

drivers/iommu/iommufd/iommufd_test.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ enum {
5454
MOCK_NESTED_DOMAIN_IOTLB_NUM = 4,
5555
};
5656

57+
enum {
58+
MOCK_DEV_CACHE_ID_MAX = 3,
59+
MOCK_DEV_CACHE_NUM = 4,
60+
};
61+
5762
struct iommu_test_cmd {
5863
__u32 size;
5964
__u32 op;
@@ -152,6 +157,7 @@ struct iommu_test_hw_info {
152157
/* Should not be equal to any defined value in enum iommu_hwpt_data_type */
153158
#define IOMMU_HWPT_DATA_SELFTEST 0xdead
154159
#define IOMMU_TEST_IOTLB_DEFAULT 0xbadbeef
160+
#define IOMMU_TEST_DEV_CACHE_DEFAULT 0xbaddad
155161

156162
/**
157163
* struct iommu_hwpt_selftest
@@ -182,4 +188,23 @@ struct iommu_hwpt_invalidate_selftest {
182188

183189
#define IOMMU_VIOMMU_TYPE_SELFTEST 0xdeadbeef
184190

191+
/* Should not be equal to any defined value in enum iommu_viommu_invalidate_data_type */
192+
#define IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST 0xdeadbeef
193+
#define IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST_INVALID 0xdadbeef
194+
195+
/**
196+
* struct iommu_viommu_invalidate_selftest - Invalidation data for Mock VIOMMU
197+
* (IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST)
198+
* @flags: Invalidate flags
199+
* @cache_id: Invalidate cache entry index
200+
*
201+
* If IOMMU_TEST_INVALIDATE_ALL is set in @flags, @cache_id will be ignored
202+
*/
203+
struct iommu_viommu_invalidate_selftest {
204+
#define IOMMU_TEST_INVALIDATE_FLAG_ALL (1 << 0)
205+
__u32 flags;
206+
__u32 vdev_id;
207+
__u32 cache_id;
208+
};
209+
185210
#endif

drivers/iommu/iommufd/selftest.c

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ struct mock_dev {
163163
struct device dev;
164164
unsigned long flags;
165165
int id;
166+
u32 cache[MOCK_DEV_CACHE_NUM];
166167
};
167168

168169
static inline struct mock_dev *to_mock_dev(struct device *dev)
@@ -609,9 +610,80 @@ mock_viommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
609610
return &mock_nested->domain;
610611
}
611612

613+
static int mock_viommu_cache_invalidate(struct iommufd_viommu *viommu,
614+
struct iommu_user_data_array *array)
615+
{
616+
struct iommu_viommu_invalidate_selftest *cmds;
617+
struct iommu_viommu_invalidate_selftest *cur;
618+
struct iommu_viommu_invalidate_selftest *end;
619+
int rc;
620+
621+
/* A zero-length array is allowed to validate the array type */
622+
if (array->entry_num == 0 &&
623+
array->type == IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST) {
624+
array->entry_num = 0;
625+
return 0;
626+
}
627+
628+
cmds = kcalloc(array->entry_num, sizeof(*cmds), GFP_KERNEL);
629+
if (!cmds)
630+
return -ENOMEM;
631+
cur = cmds;
632+
end = cmds + array->entry_num;
633+
634+
static_assert(sizeof(*cmds) == 3 * sizeof(u32));
635+
rc = iommu_copy_struct_from_full_user_array(
636+
cmds, sizeof(*cmds), array,
637+
IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST);
638+
if (rc)
639+
goto out;
640+
641+
while (cur != end) {
642+
struct mock_dev *mdev;
643+
struct device *dev;
644+
int i;
645+
646+
if (cur->flags & ~IOMMU_TEST_INVALIDATE_FLAG_ALL) {
647+
rc = -EOPNOTSUPP;
648+
goto out;
649+
}
650+
651+
if (cur->cache_id > MOCK_DEV_CACHE_ID_MAX) {
652+
rc = -EINVAL;
653+
goto out;
654+
}
655+
656+
xa_lock(&viommu->vdevs);
657+
dev = iommufd_viommu_find_dev(viommu,
658+
(unsigned long)cur->vdev_id);
659+
if (!dev) {
660+
xa_unlock(&viommu->vdevs);
661+
rc = -EINVAL;
662+
goto out;
663+
}
664+
mdev = container_of(dev, struct mock_dev, dev);
665+
666+
if (cur->flags & IOMMU_TEST_INVALIDATE_FLAG_ALL) {
667+
/* Invalidate all cache entries and ignore cache_id */
668+
for (i = 0; i < MOCK_DEV_CACHE_NUM; i++)
669+
mdev->cache[i] = 0;
670+
} else {
671+
mdev->cache[cur->cache_id] = 0;
672+
}
673+
xa_unlock(&viommu->vdevs);
674+
675+
cur++;
676+
}
677+
out:
678+
array->entry_num = cur - cmds;
679+
kfree(cmds);
680+
return rc;
681+
}
682+
612683
static struct iommufd_viommu_ops mock_viommu_ops = {
613684
.destroy = mock_viommu_destroy,
614685
.alloc_domain_nested = mock_viommu_alloc_domain_nested,
686+
.cache_invalidate = mock_viommu_cache_invalidate,
615687
};
616688

617689
static struct iommufd_viommu *mock_viommu_alloc(struct device *dev,
@@ -782,7 +854,7 @@ static void mock_dev_release(struct device *dev)
782854
static struct mock_dev *mock_dev_create(unsigned long dev_flags)
783855
{
784856
struct mock_dev *mdev;
785-
int rc;
857+
int rc, i;
786858

787859
if (dev_flags &
788860
~(MOCK_FLAGS_DEVICE_NO_DIRTY | MOCK_FLAGS_DEVICE_HUGE_IOVA))
@@ -796,6 +868,8 @@ static struct mock_dev *mock_dev_create(unsigned long dev_flags)
796868
mdev->flags = dev_flags;
797869
mdev->dev.release = mock_dev_release;
798870
mdev->dev.bus = &iommufd_mock_bus_type.bus;
871+
for (i = 0; i < MOCK_DEV_CACHE_NUM; i++)
872+
mdev->cache[i] = IOMMU_TEST_DEV_CACHE_DEFAULT;
799873

800874
rc = ida_alloc(&mock_dev_ida, GFP_KERNEL);
801875
if (rc < 0)

0 commit comments

Comments
 (0)