Re: [PATCH v7 08/16] iommufd/viommu: Keep a reference to the KVM file
From: Jacob Pan
Date: Wed Oct 14 2026 - 14:04:08 EST
Hi Aneesh,
On Thu, 8 Oct 2026 11:29:47 +0530
"Aneesh Kumar K.V (Arm)" <aneesh.kumar@xxxxxxxxxx> wrote:
> From: Nicolin Chen <nicolinc@xxxxxxxxxx>
>
> The TSM vDevice operations need access to the KVM associated with the
> device's vIOMMU. Save the device's KVM file in the iommufd_viommu
> when the vIOMMU is allocated, and take a file reference so it remains
> valid for the lifetime of the vIOMMU.
>
> Release the reference when the vIOMMU is destroyed.
>
> Based on an original patch by
> Shameer Kolothum <shameerali.kolothum.thodi@xxxxxxxxxx>
>
> [nicolinc: hold kvm's users_count]
> Signed-off-by: Nicolin Chen <nicolinc@xxxxxxxxxx>
> [aneesh.kumar: Switch to use kvm_file]
> Cc: jgg@xxxxxxxx
> Cc: kevin.tian@xxxxxxxxx
> Cc: joro@xxxxxxxxxx
> Cc: will@xxxxxxxxxx
> Cc: robin.murphy@xxxxxxx
> Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@xxxxxxxxxx>
> ---
> drivers/iommu/iommufd/viommu.c | 5 +++++
> include/linux/iommufd.h | 1 +
> 2 files changed, 6 insertions(+)
>
> diff --git a/drivers/iommu/iommufd/viommu.c
> b/drivers/iommu/iommufd/viommu.c index f7951057a1e5..b7489ed259bd
> 100644 --- a/drivers/iommu/iommufd/viommu.c
> +++ b/drivers/iommu/iommufd/viommu.c
> @@ -1,6 +1,7 @@
> // SPDX-License-Identifier: GPL-2.0-only
> /* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES
> */
> +#include <linux/file.h>
> #include "iommufd_private.h"
>
> void iommufd_viommu_destroy(struct iommufd_object *obj)
> @@ -11,6 +12,8 @@ void iommufd_viommu_destroy(struct iommufd_object
> *obj) if (viommu->ops && viommu->ops->destroy)
> viommu->ops->destroy(viommu);
> refcount_dec(&viommu->hwpt->common.obj.users);
> + if (viommu->kvm_file)
> + fput(viommu->kvm_file);
> xa_destroy(&viommu->vdevs);
> }
>
> @@ -82,6 +85,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd
> *ucmd) }
>
> xa_init(&viommu->vdevs);
> + if (idev->kvm_file)
> + viommu->kvm_file = get_file(idev->kvm_file);
So kvm_file will persist inside viommu even when no more vdevice exists
under the viommu. In that case, don't we need to check idev->kvm_file
matches viommu->kvm_file during vdevice allocation?
Otherwise, it appears possible to associate a device belonging to a
different KVM VM with the existing vIOMMU, no?
> viommu->type = cmd->type;
> viommu->ictx = ucmd->ictx;
> viommu->hwpt = hwpt_paging;
> diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
> index 0a0bb4abfbd2..3267717f676d 100644
> --- a/include/linux/iommufd.h
> +++ b/include/linux/iommufd.h
> @@ -103,6 +103,7 @@ struct iommufd_viommu {
> struct iommufd_ctx *ictx;
> struct iommu_device *iommu_dev;
> struct iommufd_hwpt_paging *hwpt;
> + struct file *kvm_file;
>
> const struct iommufd_viommu_ops *ops;
>