[PATCH v10 16/18] iommufd/viommu: Allow vIOMMUs without a parent HWPT

Baolu Lu baolu.lu at linux.intel.com
Wed Dec 2 23:00:48 PST 2026


On 11/25/2026 4:11 PM, Aneesh Kumar K.V (Arm) wrote:
> VIOMMU_ALLOC currently requires a nesting parent HWPT_PAGING even when
> the selected vIOMMU implementation has no use for one. This would force
> implementations such as the following Arm Realm vIOMMU to create an
> unused parent.
> 
> Add IOMMUFD_VIOMMU_NO_HWPT so an implementation can opt out. Require
> hwpt_id to be zero in that case and pass a NULL parent domain to its
> initialization callback. For implementations requiring a parent, verify
> that it supports nesting and belongs to the selected physical IOMMU
> driver before invoking viommu_init().
> 
> Reject nested domain and hardware queue allocation for a vIOMMU without
> a parent. Nested domain allocation also requires physical IOMMU backing.
> Document the two allocation modes in the UAPI.

[...]

> diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c
> index 859eadf05f1d..deb8a94d8b75 100644
> --- a/drivers/iommu/iommufd/hw_pagetable.c
> +++ b/drivers/iommu/iommufd/hw_pagetable.c
> @@ -306,6 +306,13 @@ iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags,
>   		return ERR_PTR(-EOPNOTSUPP);
>   	if (!user_data->len)
>   		return ERR_PTR(-EOPNOTSUPP);
> +
> +	if (!viommu->hwpt)
> +		return ERR_PTR(-EOPNOTSUPP);
> +
> +	if (viommu->backing != IOMMUFD_VIOMMU_BACKING_IOMMU)
> +		return ERR_PTR(-EOPNOTSUPP);

It's better to move this check to patch 14/18.

> +
>   	if (!viommu->ops || !viommu->ops->alloc_domain_nested)
>   		return ERR_PTR(-EOPNOTSUPP);
>   
> diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c
> index 909ef86f39a9..e176bc2f5235 100644
> --- a/drivers/iommu/iommufd/viommu.c
> +++ b/drivers/iommu/iommufd/viommu.c
> @@ -19,7 +19,9 @@ void iommufd_viommu_destroy(struct iommufd_object *obj)
>   	if (viommu->backing == IOMMUFD_VIOMMU_BACKING_TSM)
>   		tsm_put_device(viommu->tsm_dev);
>   
> -	refcount_dec(&viommu->hwpt->common.obj.users);
> +	if (viommu->hwpt)
> +		refcount_dec(&viommu->hwpt->common.obj.users);
> +
>   	if (viommu->kvm_file)
>   		fput(viommu->kvm_file);
>   	xa_destroy(&viommu->vdevs);
> @@ -33,7 +35,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
>   		.uptr = u64_to_user_ptr(cmd->data_uptr),
>   		.len = cmd->data_len,
>   	};
> -	struct iommufd_hwpt_paging *hwpt_paging;
> +	struct iommufd_hwpt_paging *hwpt_paging = NULL;
> +	struct iommu_domain *parent_domain = NULL;
>   	struct iommufd_viommu *viommu;
>   	struct iommufd_device *idev;
>   	struct iommu_device *iommu_dev = NULL;
> @@ -95,15 +98,27 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
>   		goto out_put_idev;
>   	}
>   
> -	hwpt_paging = iommufd_get_hwpt_paging(ucmd, cmd->hwpt_id);
> -	if (IS_ERR(hwpt_paging)) {
> -		rc = PTR_ERR(hwpt_paging);
> -		goto out_put_idev;
> -	}
> +	if (ops->flags & IOMMUFD_VIOMMU_NO_HWPT) {
> +		if (cmd->hwpt_id) {
> +			rc = -EINVAL;
> +			goto out_put_idev;
> +		}
> +	} else {
> +		hwpt_paging = iommufd_get_hwpt_paging(ucmd, cmd->hwpt_id);
> +		if (IS_ERR(hwpt_paging)) {
> +			rc = PTR_ERR(hwpt_paging);
> +			goto out_put_idev;
> +		}
>   
> -	if (!hwpt_paging->nest_parent) {
> -		rc = -EINVAL;
> -		goto out_put_hwpt;
> +		/*
> +		 * A parent HWPT requires physical IOMMU backing. The parent must
> +		 * support nesting and belong to that IOMMU's driver.
> +		 */
> +		if (!hwpt_paging->nest_parent || !iommu_dev ||
> +		    hwpt_paging->common.domain->owner != iommu_dev->ops) {

The owner check is new and unrelated to NO_HWPT? If so, put it in its
own patch with the necessary rationale.

> +			rc = -EINVAL;
> +			goto out_put_hwpt;
> +		}
>   	}
>   
>   	viommu = (struct iommufd_viommu *)_iommufd_object_alloc_ucmd(
> @@ -119,7 +134,9 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
>   	viommu->type = cmd->type;
>   	viommu->ictx = ucmd->ictx;
>   	viommu->hwpt = hwpt_paging;
> -	refcount_inc(&viommu->hwpt->common.obj.users);
> +	if (viommu->hwpt)
> +		refcount_inc(&viommu->hwpt->common.obj.users);
> +
>   	INIT_LIST_HEAD(&viommu->veventqs);
>   	init_rwsem(&viommu->veventqs_rwsem);
>   
> @@ -136,8 +153,10 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
>   		viommu->iommu_dev = iommu_dev;
>   	}
>   
> -	rc = ops->viommu_init(viommu, idev->dev,
> -			      hwpt_paging->common.domain,
> +	if (hwpt_paging)
> +		parent_domain = hwpt_paging->common.domain;
> +
> +	rc = ops->viommu_init(viommu, idev->dev, parent_domain,
>   			      user_data.len ? &user_data : NULL);
>   	if (rc)
>   		goto out_put_hwpt;
> @@ -148,7 +167,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
>   	rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
>   
>   out_put_hwpt:
> -	iommufd_put_object(ucmd->ictx, &hwpt_paging->common.obj);
> +	if (hwpt_paging)
> +		iommufd_put_object(ucmd->ictx, &hwpt_paging->common.obj);
>   out_put_idev:
>   	iommufd_put_object(ucmd->ictx, &idev->obj);
>   	return rc;
> @@ -472,7 +492,7 @@ int iommufd_hw_queue_alloc_ioctl(struct iommufd_ucmd *ucmd)
>   	if (IS_ERR(viommu))
>   		return PTR_ERR(viommu);
>   
> -	if (!viommu->ops || !viommu->ops->get_hw_queue_size ||
> +	if (!viommu->hwpt || !viommu->ops || !viommu->ops->get_hw_queue_size ||
>   	    !viommu->ops->hw_queue_init_phys) {
>   		rc = -EOPNOTSUPP;
>   		goto out_put_viommu;

[...]

> diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h
> index b64f6330468e..31371ccd1ef2 100644
> --- a/include/uapi/linux/iommufd.h
> +++ b/include/uapi/linux/iommufd.h
> @@ -1138,7 +1138,8 @@ struct iommu_viommu_tegra241_cmdqv {
>    * @flags: Must be 0
>    * @type: Type of the virtual IOMMU. Must be defined in enum iommu_viommu_type
>    * @dev_id: The device's physical IOMMU will be used to back the virtual IOMMU

The comment for @dev_id is stale now: for a tsm-backed viommu it selects
the device's tsm device, not its physical iommu device.

Please update it.

> - * @hwpt_id: ID of a nesting parent HWPT to associate to
> + * @hwpt_id: ID of a nesting parent HWPT to associate to. Must be zero when the
> + *           selected vIOMMU implementation does not use a parent HWPT

This reads a bit odd. Whether a type needs a parent is now decided by
the kernel implementation, but userspace has to know it up front to pass
the right hwpt_id?

>    * @out_viommu_id: Output virtual IOMMU ID for the allocated object
>    * @data_len: Length of the type specific data
>    * @__reserved: Must be 0

Thanks,
baolu



More information about the linux-arm-kernel mailing list