@@ -7,6 +7,7 @@
#include "../iommu-priv.h"
#include "iommufd_private.h"
+#include "iommufd_test.h"
void iommufd_hw_pagetable_destroy(struct iommufd_object *obj)
{
@@ -115,4 +115,20 @@ struct iommu_test_hw_info {
__u32 test_reg;
};
+/* Should not be equal to any defined value in enum iommu_hwpt_type */
+#define IOMMU_HWPT_TYPE_SELFTTEST 0xdead
+
+/**
+ * struct iommu_hwpt_selftest
+ *
+ * @flags: page table entry attributes
+ * @test_config: default iotlb setup (value IOMMU_TEST_IOTLB_DEFAULT)
+ */
+struct iommu_hwpt_selftest {
+#define IOMMU_TEST_FLAG_NESTED (1ULL << 0)
+ __u64 flags;
+#define IOMMU_TEST_IOTLB_DEFAULT 0xbadbeef
+ __u64 test_config;
+};
+
#endif
@@ -84,7 +84,9 @@ void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
struct mock_iommu_domain {
struct iommu_domain domain;
+ struct mock_iommu_domain *parent;
struct xarray pfns;
+ u32 iotlb;
};
enum selftest_obj_type {
@@ -144,26 +146,81 @@ static void *mock_domain_hw_info(struct device *dev, u32 *length)
return info;
}
-static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type)
+static const struct iommu_ops mock_ops;
+static struct iommu_domain_ops domain_nested_ops;
+
+static struct iommu_domain *
+__mock_domain_alloc(unsigned int iommu_domain_type,
+ struct mock_iommu_domain *mock_parent,
+ const struct iommu_hwpt_selftest *user_cfg)
{
struct mock_iommu_domain *mock;
if (iommu_domain_type == IOMMU_DOMAIN_BLOCKED)
return &mock_blocking_domain;
- if (WARN_ON(iommu_domain_type != IOMMU_DOMAIN_UNMANAGED))
- return NULL;
-
mock = kzalloc(sizeof(*mock), GFP_KERNEL);
if (!mock)
- return NULL;
- mock->domain.geometry.aperture_start = MOCK_APERTURE_START;
- mock->domain.geometry.aperture_end = MOCK_APERTURE_LAST;
+ return ERR_PTR(-ENOMEM);
+ mock->parent = mock_parent;
+ mock->domain.type = iommu_domain_type;
mock->domain.pgsize_bitmap = MOCK_IO_PAGE_SIZE;
- xa_init(&mock->pfns);
+ if (mock_parent) {
+ mock->iotlb = user_cfg->test_config;
+ mock->domain.ops = &domain_nested_ops;
+ } else {
+ mock->domain.geometry.aperture_start = MOCK_APERTURE_START;
+ mock->domain.geometry.aperture_end = MOCK_APERTURE_LAST;
+ mock->domain.ops = mock_ops.default_domain_ops;
+ xa_init(&mock->pfns);
+ }
return &mock->domain;
}
+static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type)
+{
+ struct iommu_domain *domain;
+
+ if (WARN_ON(iommu_domain_type != IOMMU_DOMAIN_BLOCKED &&
+ iommu_domain_type != IOMMU_DOMAIN_UNMANAGED))
+ return NULL;
+ domain = __mock_domain_alloc(IOMMU_DOMAIN_UNMANAGED, NULL, NULL);
+ if (IS_ERR(domain))
+ domain = NULL;
+ return domain;
+}
+
+static struct iommu_domain *mock_domain_alloc_user(struct device *dev,
+ enum iommu_hwpt_type hwpt_type,
+ struct iommu_domain *parent,
+ const union iommu_domain_user_data *user_data)
+{
+ const struct iommu_hwpt_selftest *user_cfg = (struct iommu_hwpt_selftest *)user_data;
+ unsigned int iommu_domain_type = IOMMU_DOMAIN_UNMANAGED;
+ struct mock_iommu_domain *mock_parent = NULL;
+
+ /* Check union iommu_domain_user_data in include/linux/iommu.h */
+ static_assert(sizeof(struct iommu_hwpt_selftest) <= 16);
+
+ if (hwpt_type != IOMMU_HWPT_TYPE_DEFAULT &&
+ hwpt_type != IOMMU_HWPT_TYPE_SELFTTEST)
+ return ERR_PTR(-EINVAL);
+
+ if (parent) {
+ if (hwpt_type != IOMMU_HWPT_TYPE_SELFTTEST)
+ return ERR_PTR(-EINVAL);
+ if (parent->ops != mock_ops.default_domain_ops)
+ return ERR_PTR(-EINVAL);
+ if (!user_cfg || !(user_cfg->flags & IOMMU_TEST_FLAG_NESTED))
+ return ERR_PTR(-EINVAL);
+ iommu_domain_type = IOMMU_DOMAIN_NESTED;
+ mock_parent = container_of(parent,
+ struct mock_iommu_domain, domain);
+ }
+
+ return __mock_domain_alloc(iommu_domain_type, mock_parent, user_cfg);
+}
+
static void mock_domain_free(struct iommu_domain *domain)
{
struct mock_iommu_domain *mock =
@@ -324,6 +381,7 @@ static const struct iommu_ops mock_ops = {
.hw_info_type = IOMMU_HW_INFO_TYPE_SELFTEST,
.hw_info = mock_domain_hw_info,
.domain_alloc = mock_domain_alloc,
+ .domain_alloc_user = mock_domain_alloc_user,
.capable = mock_domain_capable,
.get_resv_regions = iommufd_test_get_resv_regions,
.set_platform_dma_ops = mock_domain_set_plaform_dma_ops,
@@ -337,6 +395,11 @@ static const struct iommu_ops mock_ops = {
},
};
+static struct iommu_domain_ops domain_nested_ops = {
+ .free = mock_domain_free,
+ .attach_dev = mock_domain_nop_attach,
+};
+
static struct iommu_device mock_iommu_device = {
.ops = &mock_ops,
};
@@ -353,7 +416,10 @@ get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id,
if (IS_ERR(obj))
return ERR_CAST(obj);
hwpt = container_of(obj, struct iommufd_hw_pagetable, obj);
- if (hwpt->domain->ops != mock_ops.default_domain_ops) {
+ if ((hwpt->domain->type == IOMMU_DOMAIN_UNMANAGED &&
+ hwpt->domain->ops != mock_ops.default_domain_ops) ||
+ (hwpt->domain->type == IOMMU_DOMAIN_NESTED &&
+ hwpt->domain->ops != &domain_nested_ops)) {
iommufd_put_object(&hwpt->obj);
return ERR_PTR(-EINVAL);
}
@@ -237,6 +237,9 @@ struct iommu_iotlb_gather {
* include/uapi/linux/iommufd.h.
*/
union iommu_domain_user_data {
+#ifdef CONFIG_IOMMUFD_TEST
+ __u64 test[2];
+#endif
};
/**