Commit 65fe32f7 authored by Nicolin Chen's avatar Nicolin Chen Committed by Jason Gunthorpe

iommufd/selftest: Add nested domain allocation for mock domain

Add nested domain support in the ->domain_alloc_user op with some proper
sanity checks. Then, add a domain_nested_ops for all nested domains and
split the get_md_pagetable helper into paging and nested helpers.

Also, add an iotlb as a testing property of a nested domain.

Link: https://lore.kernel.org/r/20231026043938.63898-10-yi.l.liu@intel.comSigned-off-by: default avatarNicolin Chen <nicolinc@nvidia.com>
Signed-off-by: default avatarYi Liu <yi.l.liu@intel.com>
Reviewed-by: default avatarKevin Tian <kevin.tian@intel.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
parent e9d36c07
...@@ -46,6 +46,11 @@ enum { ...@@ -46,6 +46,11 @@ enum {
MOCK_FLAGS_DEVICE_NO_DIRTY = 1 << 0, MOCK_FLAGS_DEVICE_NO_DIRTY = 1 << 0,
}; };
enum {
MOCK_NESTED_DOMAIN_IOTLB_ID_MAX = 3,
MOCK_NESTED_DOMAIN_IOTLB_NUM = 4,
};
struct iommu_test_cmd { struct iommu_test_cmd {
__u32 size; __u32 size;
__u32 op; __u32 op;
...@@ -130,4 +135,17 @@ struct iommu_test_hw_info { ...@@ -130,4 +135,17 @@ struct iommu_test_hw_info {
__u32 test_reg; __u32 test_reg;
}; };
/* Should not be equal to any defined value in enum iommu_hwpt_data_type */
#define IOMMU_HWPT_DATA_SELFTEST 0xdead
#define IOMMU_TEST_IOTLB_DEFAULT 0xbadbeef
/**
* struct iommu_hwpt_selftest
*
* @iotlb: default mock iotlb value, IOMMU_TEST_IOTLB_DEFAULT
*/
struct iommu_hwpt_selftest {
__u32 iotlb;
};
#endif #endif
...@@ -93,6 +93,12 @@ struct mock_iommu_domain { ...@@ -93,6 +93,12 @@ struct mock_iommu_domain {
struct xarray pfns; struct xarray pfns;
}; };
struct mock_iommu_domain_nested {
struct iommu_domain domain;
struct mock_iommu_domain *parent;
u32 iotlb[MOCK_NESTED_DOMAIN_IOTLB_NUM];
};
enum selftest_obj_type { enum selftest_obj_type {
TYPE_IDEV, TYPE_IDEV,
}; };
...@@ -217,54 +223,99 @@ const struct iommu_dirty_ops dirty_ops = { ...@@ -217,54 +223,99 @@ const struct iommu_dirty_ops dirty_ops = {
}; };
static const struct iommu_ops mock_ops; 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) static struct iommu_domain *
__mock_domain_alloc_paging(unsigned int iommu_domain_type, bool needs_dirty_ops)
{ {
struct mock_iommu_domain *mock; struct mock_iommu_domain *mock;
if (iommu_domain_type == IOMMU_DOMAIN_BLOCKED)
return &mock_blocking_domain;
if (iommu_domain_type != IOMMU_DOMAIN_UNMANAGED)
return NULL;
mock = kzalloc(sizeof(*mock), GFP_KERNEL); mock = kzalloc(sizeof(*mock), GFP_KERNEL);
if (!mock) if (!mock)
return NULL; return ERR_PTR(-ENOMEM);
mock->domain.geometry.aperture_start = MOCK_APERTURE_START; mock->domain.geometry.aperture_start = MOCK_APERTURE_START;
mock->domain.geometry.aperture_end = MOCK_APERTURE_LAST; mock->domain.geometry.aperture_end = MOCK_APERTURE_LAST;
mock->domain.pgsize_bitmap = MOCK_IO_PAGE_SIZE; mock->domain.pgsize_bitmap = MOCK_IO_PAGE_SIZE;
mock->domain.ops = mock_ops.default_domain_ops; mock->domain.ops = mock_ops.default_domain_ops;
if (needs_dirty_ops)
mock->domain.dirty_ops = &dirty_ops;
mock->domain.type = iommu_domain_type; mock->domain.type = iommu_domain_type;
xa_init(&mock->pfns); xa_init(&mock->pfns);
return &mock->domain; return &mock->domain;
} }
static struct iommu_domain *
__mock_domain_alloc_nested(struct mock_iommu_domain *mock_parent,
const struct iommu_hwpt_selftest *user_cfg)
{
struct mock_iommu_domain_nested *mock_nested;
int i;
mock_nested = kzalloc(sizeof(*mock_nested), GFP_KERNEL);
if (!mock_nested)
return ERR_PTR(-ENOMEM);
mock_nested->parent = mock_parent;
mock_nested->domain.ops = &domain_nested_ops;
mock_nested->domain.type = IOMMU_DOMAIN_NESTED;
for (i = 0; i < MOCK_NESTED_DOMAIN_IOTLB_NUM; i++)
mock_nested->iotlb[i] = user_cfg->iotlb;
return &mock_nested->domain;
}
static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type)
{
struct iommu_domain *domain;
if (iommu_domain_type == IOMMU_DOMAIN_BLOCKED)
return &mock_blocking_domain;
if (iommu_domain_type != IOMMU_DOMAIN_UNMANAGED)
return NULL;
domain = __mock_domain_alloc_paging(iommu_domain_type, false);
if (IS_ERR(domain))
domain = NULL;
return domain;
}
static struct iommu_domain * static struct iommu_domain *
mock_domain_alloc_user(struct device *dev, u32 flags, mock_domain_alloc_user(struct device *dev, u32 flags,
struct iommu_domain *parent, struct iommu_domain *parent,
const struct iommu_user_data *user_data) const struct iommu_user_data *user_data)
{ {
struct mock_iommu_domain *mock_parent;
struct iommu_hwpt_selftest user_cfg;
int rc;
/* must be mock_domain */
if (!parent) {
struct mock_dev *mdev = container_of(dev, struct mock_dev, dev); struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);
struct iommu_domain *domain; bool has_dirty_flag = flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
bool no_dirty_ops = mdev->flags & MOCK_FLAGS_DEVICE_NO_DIRTY;
if (flags & if (flags & (~(IOMMU_HWPT_ALLOC_NEST_PARENT |
(~(IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING))) IOMMU_HWPT_ALLOC_DIRTY_TRACKING)))
return ERR_PTR(-EOPNOTSUPP); return ERR_PTR(-EOPNOTSUPP);
if (user_data || (has_dirty_flag && no_dirty_ops))
if (parent || user_data)
return ERR_PTR(-EOPNOTSUPP); return ERR_PTR(-EOPNOTSUPP);
return __mock_domain_alloc_paging(IOMMU_DOMAIN_UNMANAGED,
has_dirty_flag);
}
if ((flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING) && /* must be mock_domain_nested */
(mdev->flags & MOCK_FLAGS_DEVICE_NO_DIRTY)) if (user_data->type != IOMMU_HWPT_DATA_SELFTEST || flags)
return ERR_PTR(-EOPNOTSUPP); return ERR_PTR(-EOPNOTSUPP);
if (!parent || parent->ops != mock_ops.default_domain_ops)
return ERR_PTR(-EINVAL);
domain = mock_domain_alloc(IOMMU_DOMAIN_UNMANAGED); mock_parent = container_of(parent, struct mock_iommu_domain, domain);
if (domain && !(mdev->flags & MOCK_FLAGS_DEVICE_NO_DIRTY)) if (!mock_parent)
domain->dirty_ops = &dirty_ops; return ERR_PTR(-EINVAL);
if (!domain)
domain = ERR_PTR(-ENOMEM); rc = iommu_copy_struct_from_user(&user_cfg, user_data,
return domain; IOMMU_HWPT_DATA_SELFTEST, iotlb);
if (rc)
return ERR_PTR(rc);
return __mock_domain_alloc_nested(mock_parent, &user_cfg);
} }
static void mock_domain_free(struct iommu_domain *domain) static void mock_domain_free(struct iommu_domain *domain)
...@@ -434,19 +485,41 @@ static const struct iommu_ops mock_ops = { ...@@ -434,19 +485,41 @@ static const struct iommu_ops mock_ops = {
}, },
}; };
static void mock_domain_free_nested(struct iommu_domain *domain)
{
struct mock_iommu_domain_nested *mock_nested =
container_of(domain, struct mock_iommu_domain_nested, domain);
kfree(mock_nested);
}
static struct iommu_domain_ops domain_nested_ops = {
.free = mock_domain_free_nested,
.attach_dev = mock_domain_nop_attach,
};
static inline struct iommufd_hw_pagetable * static inline struct iommufd_hw_pagetable *
get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id, __get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id, u32 hwpt_type)
struct mock_iommu_domain **mock)
{ {
struct iommufd_hw_pagetable *hwpt;
struct iommufd_object *obj; struct iommufd_object *obj;
obj = iommufd_get_object(ucmd->ictx, mockpt_id, obj = iommufd_get_object(ucmd->ictx, mockpt_id, hwpt_type);
IOMMUFD_OBJ_HWPT_PAGING);
if (IS_ERR(obj)) if (IS_ERR(obj))
return ERR_CAST(obj); return ERR_CAST(obj);
hwpt = container_of(obj, struct iommufd_hw_pagetable, obj); return container_of(obj, struct iommufd_hw_pagetable, obj);
if (hwpt->domain->ops != mock_ops.default_domain_ops) { }
static inline struct iommufd_hw_pagetable *
get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id,
struct mock_iommu_domain **mock)
{
struct iommufd_hw_pagetable *hwpt;
hwpt = __get_md_pagetable(ucmd, mockpt_id, IOMMUFD_OBJ_HWPT_PAGING);
if (IS_ERR(hwpt))
return hwpt;
if (hwpt->domain->type != IOMMU_DOMAIN_UNMANAGED ||
hwpt->domain->ops != mock_ops.default_domain_ops) {
iommufd_put_object(&hwpt->obj); iommufd_put_object(&hwpt->obj);
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
} }
...@@ -454,6 +527,25 @@ get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id, ...@@ -454,6 +527,25 @@ get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id,
return hwpt; return hwpt;
} }
static inline struct iommufd_hw_pagetable *
get_md_pagetable_nested(struct iommufd_ucmd *ucmd, u32 mockpt_id,
struct mock_iommu_domain_nested **mock_nested)
{
struct iommufd_hw_pagetable *hwpt;
hwpt = __get_md_pagetable(ucmd, mockpt_id, IOMMUFD_OBJ_HWPT_NESTED);
if (IS_ERR(hwpt))
return hwpt;
if (hwpt->domain->type != IOMMU_DOMAIN_NESTED ||
hwpt->domain->ops != &domain_nested_ops) {
iommufd_put_object(&hwpt->obj);
return ERR_PTR(-EINVAL);
}
*mock_nested = container_of(hwpt->domain,
struct mock_iommu_domain_nested, domain);
return hwpt;
}
struct mock_bus_type { struct mock_bus_type {
struct bus_type bus; struct bus_type bus;
struct notifier_block nb; struct notifier_block nb;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment