[PATCH v10 14/18] iommufd/viommu: Identify physical IOMMU backing with a tag
Baolu Lu
baolu.lu at linux.intel.com
Wed Dec 2 21:24:25 PST 2026
On 11/25/2026 4:11 PM, Aneesh Kumar K.V (Arm) wrote:
> When allocating a vDEVICE or nested HWPT, IOMMUFD checks that the device
> and its vIOMMU use the same physical IOMMU.
>
> The next patch adds vIOMMUs implemented by a TSM. These store a TSM
> pointer instead of a physical IOMMU pointer, so the same comparison
> cannot be used.
>
> Add a backing field to identify vIOMMUs that use a physical IOMMU, and
> perform the comparison only for those vIOMMUs. All existing vIOMMUs
> select that value, so their behavior is unchanged.
>
> Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar at kernel.org>
> ---
> drivers/iommu/iommufd/hw_pagetable.c | 10 ++++++----
> drivers/iommu/iommufd/viommu.c | 11 +++++++----
> include/linux/iommufd.h | 5 +++++
> 3 files changed, 18 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c
> index ae62db50885c..859eadf05f1d 100644
> --- a/drivers/iommu/iommufd/hw_pagetable.c
> +++ b/drivers/iommu/iommufd/hw_pagetable.c
> @@ -404,10 +404,12 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd)
> struct iommufd_viommu *viommu;
>
> viommu = container_of(pt_obj, struct iommufd_viommu, obj);
> - iommu_dev = iommufd_device_get_iommu_dev(idev);
> - if (!iommu_dev || viommu->iommu_dev != iommu_dev) {
> - rc = -EINVAL;
> - goto out_unlock;
> + if (viommu->backing == IOMMUFD_VIOMMU_BACKING_IOMMU) {
> + iommu_dev = iommufd_device_get_iommu_dev(idev);
> + if (!iommu_dev || viommu->iommu_dev != iommu_dev) {
> + rc = -EINVAL;
> + goto out_unlock;
> + }
What happens when viommu->backing != IOMMUFD_VIOMMU_BACKING_IOMMU? A
nested hwpt is not valid for such a viommu, but this code skips the
check and still calls iommufd_viommu_alloc_hwpt_nested(). That function
dereferences viommu->iommu_dev->ops, which after the next patch aliases
tsm_dev. How about rejecting it explicitly:
if (viommu->backing != IOMMUFD_VIOMMU_BACKING_IOMMU) {
rc = -EOPNOTSUPP;
goto out_unlock;
}
iommu_dev = iommufd_device_get_iommu_dev(idev);
if (!iommu_dev || viommu->iommu_dev != iommu_dev) {
rc = -EINVAL;
goto out_unlock;
}
> }
> hwpt_nested = iommufd_viommu_alloc_hwpt_nested(
> viommu, cmd->flags, &user_data);
> diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c
> index 472381b96a8e..dab499d24c45 100644
> --- a/drivers/iommu/iommufd/viommu.c
> +++ b/drivers/iommu/iommufd/viommu.c
> @@ -105,6 +105,7 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
> * pluggable IOMMU instance (if exists) is responsible for refcounting
> * on its own.
> */
> + viommu->backing = IOMMUFD_VIOMMU_BACKING_IOMMU;
> viommu->iommu_dev = iommu_dev;
>
> rc = ops->viommu_init(viommu, idev->dev,
> @@ -218,10 +219,12 @@ int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)
> goto out_put_idev;
> }
>
> - iommu_dev = iommufd_device_get_iommu_dev(idev);
> - if (!iommu_dev || viommu->iommu_dev != iommu_dev) {
> - rc = -EINVAL;
> - goto out_put_idev;
> + if (viommu->backing == IOMMUFD_VIOMMU_BACKING_IOMMU) {
> + iommu_dev = iommufd_device_get_iommu_dev(idev);
> + if (!iommu_dev || viommu->iommu_dev != iommu_dev) {
> + rc = -EINVAL;
> + goto out_put_idev;
> + }
Similar concern here, although a tsm-backed viommu does need vdevices,
so this path can't simply be rejected. Skipping the check means the core
accepts any idev for a tsm viommu, without verifying that the device is
managed by viommu->tsm_dev.
> }
>
> mutex_lock(&idev->igroup->lock);
> diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
> index 5f5c67b24134..192d46b8ccfd 100644
> --- a/include/linux/iommufd.h
> +++ b/include/linux/iommufd.h
> @@ -98,9 +98,14 @@ void iommufd_access_detach(struct iommufd_access *access);
>
> void iommufd_ctx_get(struct iommufd_ctx *ictx);
>
> +enum iommufd_viommu_backing {
> + IOMMUFD_VIOMMU_BACKING_IOMMU,
> +};
> +
> struct iommufd_viommu {
> struct iommufd_object obj;
> struct iommufd_ctx *ictx;
> + enum iommufd_viommu_backing backing;
> struct iommu_device *iommu_dev;
> struct iommufd_hwpt_paging *hwpt;
> struct file *kvm_file;
Thanks,
baolu
More information about the linux-arm-kernel
mailing list