@@ -301,6 +301,9 @@ static int iommufd_device_attach_ioas(struct iommufd_device *idev,
struct io_pagetable *iopt;
int rc;
+ if (hwpt->parent)
+ hwpt = hwpt->parent;
+
iopt = &hwpt->ioas->iopt;
rc = iopt_table_enforce_group_resv_regions(iopt, idev->dev,
@@ -321,6 +324,8 @@ static int iommufd_device_attach_ioas(struct iommufd_device *idev,
static void iommufd_device_detach_ioas(struct iommufd_device *idev,
struct iommufd_hw_pagetable *hwpt)
{
+ if (hwpt->parent)
+ hwpt = hwpt->parent;
iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev);
}
@@ -15,8 +15,12 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj)
WARN_ON(!list_empty(&hwpt->devices));
- iopt_table_remove_domain(&hwpt->ioas->iopt, hwpt->domain);
- list_del(&hwpt->hwpt_item);
+ if (!hwpt->parent) {
+ iopt_table_remove_domain(&hwpt->ioas->iopt, hwpt->domain);
+ list_del(&hwpt->hwpt_item);
+ } else {
+ refcount_dec(&hwpt->parent->obj.users);
+ }
iommu_domain_free(hwpt->domain);
refcount_dec(&hwpt->ioas->obj.users);
mutex_destroy(&hwpt->devices_lock);
@@ -58,13 +62,18 @@ __iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx,
goto out_abort;
}
+ hwpt->parent = parent;
INIT_LIST_HEAD(&hwpt->devices);
INIT_LIST_HEAD(&hwpt->hwpt_item);
mutex_init(&hwpt->devices_lock);
- rc = iopt_table_add_domain(&ioas->iopt, hwpt->domain);
- if (rc)
- goto out_free_domain;
- list_add_tail(&hwpt->hwpt_item, &ioas->hwpt_list);
+ if (!parent) {
+ rc = iopt_table_add_domain(&ioas->iopt, hwpt->domain);
+ if (rc)
+ goto out_free_domain;
+ list_add_tail(&hwpt->hwpt_item, &ioas->hwpt_list);
+ } else {
+ refcount_inc(&parent->obj.users);
+ }
/* Pairs with iommufd_hw_pagetable_destroy() */
refcount_inc(&ioas->obj.users);
@@ -241,6 +241,7 @@ int iommufd_vfio_ioas(struct iommufd_ucmd *ucmd);
*/
struct iommufd_hw_pagetable {
struct iommufd_object obj;
+ struct iommufd_hw_pagetable *parent;
struct iommufd_ioas *ioas;
struct iommu_domain *domain;
bool auto_domain : 1;