[PATCH v9 09/13] iommufd/viommu: Select vIOMMU operations before allocation

Baolu Lu baolu.lu at linux.intel.com
Thu Nov 19 20:44:10 PST 2026


On 11/16/2026 4:19 PM, Aneesh Kumar K.V (Arm) wrote:
> Physical IOMMU drivers currently provide the vIOMMU size and
> initialization callbacks through iommu_ops. The initialization callback
> then selects and installs the corresponding iommufd_viommu_ops.
> 
> Add iommu_ops::get_viommu_ops() to select the operations from the
> physical device and requested vIOMMU type before allocation. Move the
> size and initialization callbacks into iommufd_viommu_ops. The core can
> then validate the selected operations, allocate the driver structure,
> initialize it, and install the operations.
> 
> Convert AMD, Arm SMMUv3, Tegra CMDQV, and the selftest backend to the
> new interface.
> 
> The current code requires a valid parent HWPT before calling
> viommu_init(), which selects the vIOMMU operations. A later patch adds
> an operations flag indicating that a vIOMMU type does not require a
> parent HWPT. Selecting the operations earlier allows the core to check
> that flag before deciding whether to look up and validate a parent HWPT.
> 
> Temporarily disable AMD vIOMMU allocation by returning NULL from
> amd_iommufd_get_viommu_ops() while the vIOMMU interfaces are being
> updated.
> 
> Cc: Joerg Roedel <joro at 8bytes.org>
> Cc: Suravee Suthikulpanit <suravee.suthikulpanit at amd.com>
> Cc: Vasant Hegde <vasant.hegde at amd.com>
> Cc: Will Deacon <will at kernel.org>
> Cc: Robin Murphy <robin.murphy at arm.com>
> Cc: Thierry Reding <thierry.reding at kernel.org>
> Cc: Krishna Reddy <vdumpa at nvidia.com>
> Cc: Jonathan Hunter <jonathanh at nvidia.com>
> Cc: Jason Gunthorpe <jgg at ziepe.ca>
> Cc: Kevin Tian <kevin.tian at intel.com>
> Cc: linux-arm-kernel at lists.infradead.org
> Cc: linux-tegra at vger.kernel.org
> Cc: iommu at lists.linux.dev
> Based on original patch by Jason Gunthorpe <jgg at nvidia.com>
> Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar at kernel.org>
> ---
>   drivers/iommu/amd/iommu.c                     |  3 +-
>   drivers/iommu/amd/iommufd.c                   | 18 ++++++---
>   drivers/iommu/amd/iommufd.h                   | 11 ++++--
>   drivers/iommu/amd/nested.c                    |  4 +-
>   .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c     | 37 +++++++++++--------
>   drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   |  7 ++--
>   drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   | 16 ++++----
>   .../iommu/arm/arm-smmu-v3/tegra241-cmdqv.c    | 24 ++++++++----
>   drivers/iommu/iommufd/selftest.c              | 31 +++++++++++-----
>   drivers/iommu/iommufd/viommu.c                | 32 ++++++++--------
>   include/linux/iommu.h                         | 20 +++-------
>   include/linux/iommufd.h                       | 14 +++++++
>   12 files changed, 129 insertions(+), 88 deletions(-)
> 

[...]

> diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c
> index 0d162a3fdc98..2eba1562a704 100644
> --- a/drivers/iommu/iommufd/viommu.c
> +++ b/drivers/iommu/iommufd/viommu.c
> @@ -33,7 +33,7 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
>   	struct iommufd_viommu *viommu;
>   	struct iommufd_device *idev;
>   	struct iommu_device *iommu_dev;
> -	const struct iommu_ops *ops;
> +	const struct iommufd_viommu_ops *ops;
>   	size_t viommu_size;
>   	int rc;
>   
> @@ -49,23 +49,25 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
>   		rc = -EOPNOTSUPP;
>   		goto out_put_idev;
>   	}
> -	ops = iommu_dev->ops;
> -	if (!ops->get_viommu_size || !ops->viommu_init) {
> +	if (!iommu_dev->ops->get_viommu_ops) {
>   		rc = -EOPNOTSUPP;
>   		goto out_put_idev;
>   	}
> -
> -	viommu_size = ops->get_viommu_size(idev->dev, cmd->type);
> -	if (!viommu_size) {
> +	ops = iommu_dev->ops->get_viommu_ops(idev->dev, cmd->type);
> +	if (!ops) {
>   		rc = -EOPNOTSUPP;
>   		goto out_put_idev;
>   	}
> -
>   	/*
> -	 * It is a driver bug for providing a viommu_size smaller than the core
> -	 * vIOMMU structure size
> +	 * It is a driver bug to omit the required operations or provide a size
> +	 * smaller than the core vIOMMU structure.
>   	 */
> -	if (WARN_ON_ONCE(viommu_size < sizeof(*viommu))) {
> +	if (WARN_ON_ONCE(!ops->get_viommu_size || !ops->viommu_init)) {
> +		rc = -EOPNOTSUPP;
> +		goto out_put_idev;
> +	}
> +	viommu_size = ops->get_viommu_size(idev->dev, cmd->type);
> +	if (!viommu_size || WARN_ON_ONCE(viommu_size < sizeof(*viommu))) {
>   		rc = -EOPNOTSUPP;
>   		goto out_put_idev;
>   	}
> @@ -104,16 +106,12 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
>   	 */
>   	viommu->iommu_dev = iommu_dev;
>   
> -	rc = ops->viommu_init(viommu, hwpt_paging->common.domain,
> +	rc = ops->viommu_init(viommu, idev->dev,
> +			      hwpt_paging->common.domain,
>   			      user_data.len ? &user_data : NULL);
>   	if (rc)
>   		goto out_put_hwpt;
> -
> -	/* It is a driver bug that viommu->ops isn't filled */
> -	if (WARN_ON_ONCE(!viommu->ops)) {
> -		rc = -EOPNOTSUPP;
> -		goto out_put_hwpt;
> -	}
> +	viommu->ops = ops;
>   
>   	cmd->out_viommu_id = viommu->obj.id;
>   	rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
> diff --git a/include/linux/iommu.h b/include/linux/iommu.h
> index ef5043752a36..f27064917df7 100644
> --- a/include/linux/iommu.h
> +++ b/include/linux/iommu.h
> @@ -46,6 +46,7 @@ struct iommu_dma_msi_cookie;
>   struct iommu_fault_param;
>   struct iommufd_ctx;
>   struct iommufd_viommu;
> +struct iommufd_viommu_ops;
>   struct msi_desc;
>   struct msi_msg;
>   
> @@ -669,16 +670,8 @@ __iommu_copy_struct_to_user(const struct iommu_user_data *dst_data,
>    *		- IOMMU_DOMAIN_DMA: must use a dma domain
>    *		- 0: use the default setting
>    * @default_domain_ops: the default ops for domains
> - * @get_viommu_size: Get the size of a driver-level vIOMMU structure for a given
> - *                   @dev corresponding to @viommu_type. Driver should return 0
> - *                   if vIOMMU isn't supported accordingly. It is required for
> - *                   driver to use the VIOMMU_STRUCT_SIZE macro to sanitize the
> - *                   driver-level vIOMMU structure related to the core one
> - * @viommu_init: Init the driver-level struct of an iommufd_viommu on a physical
> - *               IOMMU instance @viommu->iommu_dev, as the set of virtualization
> - *               resources shared/passed to user space IOMMU instance. Associate
> - *               it with a nesting @parent_domain. It is required for driver to
> - *               set @viommu->ops pointing to its own viommu_ops
> + * @get_viommu_ops: Return the vIOMMU operations supported by @dev for a type.
> + *                  Return NULL if the type is unsupported.
>    * @owner: Driver module providing these ops
>    * @identity_domain: An always available, always attachable identity
>    *                   translation.
> @@ -729,11 +722,8 @@ struct iommu_ops {
>   
>   	int (*def_domain_type)(struct device *dev);
>   
> -	size_t (*get_viommu_size)(struct device *dev,
> -				  enum iommu_viommu_type viommu_type);
> -	int (*viommu_init)(struct iommufd_viommu *viommu,
> -			   struct iommu_domain *parent_domain,
> -			   const struct iommu_user_data *user_data);
> +	const struct iommufd_viommu_ops *(*get_viommu_ops)(
> +		struct device *dev, enum iommu_viommu_type viommu_type);
>   
>   	const struct iommu_domain_ops *default_domain_ops;
>   	struct module *owner;
> diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
> index d1fd7888fc91..5f5c67b24134 100644
> --- a/include/linux/iommufd.h
> +++ b/include/linux/iommufd.h
> @@ -148,6 +148,15 @@ struct iommufd_hw_queue {
>   
>   /**
>    * struct iommufd_viommu_ops - vIOMMU specific operations
> + * @get_viommu_size: Get the size of a driver-level vIOMMU structure for a given
> + *                   @dev corresponding to @viommu_type. Driver should return 0
> + *                   if vIOMMU isn't supported accordingly. It is required for
> + *                   driver to use the VIOMMU_STRUCT_SIZE macro to sanitize the
> + *                   driver-level vIOMMU structure related to the core one
> + * @viommu_init: Init the driver-level struct of an iommufd_viommu on a physical
> + *               IOMMU instance @viommu->iommu_dev, as the set of virtualization
> + *               resources shared/passed to user space IOMMU instance. Associate
> + *               it with a nesting @parent_domain.
>    * @destroy: Clean up all driver-specific parts of an iommufd_viommu. The memory
>    *           of the vIOMMU will be free-ed by iommufd core after calling this op
>    * @alloc_domain_nested: Allocate a IOMMU_DOMAIN_NESTED on a vIOMMU that holds a
> @@ -190,6 +199,11 @@ struct iommufd_hw_queue {
>    *                      does, it should set it to the @hw_queue->destroy pointer
>    */
>   struct iommufd_viommu_ops {
> +	size_t (*get_viommu_size)(struct device *dev,
> +		enum iommu_viommu_type type);
> +	int (*viommu_init)(struct iommufd_viommu *viommu, struct device *dev,
> +		struct iommu_domain *parent_domain,
> +		const struct iommu_user_data *user_data);
>   	void (*destroy)(struct iommufd_viommu *viommu);
>   	struct iommu_domain *(*alloc_domain_nested)(
>   		struct iommufd_viommu *viommu, u32 flags,

For changes in iommufd/viommu common code,

Reviewed-by: Lu Baolu <baolu.lu at linux.intel.com>



More information about the linux-arm-kernel mailing list