Re: [RFC PATCH v1 07/38] iommufd/viommu: Add support to associate viommu with kvm instance

Jonathan Cameron
2025-07-29T16:46:34Z

On Mon, 28 Jul 2025 19:21:44 +0530
"Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org> wrote:

> The associated kvm instance will be used in later patch by iommufd to
> bind a tdi to kvm.
>
> Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
> ---
> drivers/iommu/iommufd/viommu.c | 45 +++++++++++++++++++++++++++++++++-
> include/linux/iommufd.h | 3 +++
> include/uapi/linux/iommufd.h | 12 +++++++++
> 3 files changed, 59 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c
> index 2ca5809b238b..59f1e1176f7f 100644
> --- a/drivers/iommu/iommufd/viommu.c
> +++ b/drivers/iommu/iommufd/viommu.c
> @@ -2,6 +2,36 @@
> /* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES
> */
> #include "iommufd_private.h"
> +#include "linux/tsm.h"
> +
> +#if IS_ENABLED(CONFIG_KVM)
> +#include <linux/kvm_host.h>
> +
> +static int viommu_get_kvm(struct iommufd_viommu *viommu, int kvm_vm_fd)
> +{
> + int rc = -EBADF;
> + struct file *filp;
> +
> + filp = fget(kvm_vm_fd);
> +
> + if (!file_is_kvm(filp))
> + goto err_out;
> +
> + /* hold the kvm reference via file descriptor */
> + viommu->kvm_filp = filp;
> + return 0;
> +err_out:
> + viommu->kvm_filp = NULL;

Is this to undo side effects from this function on error?

kvm_filp is only set after all error paths so maybe this isn't
needed?

If this isn't needed then use __free(fput) and no_free_ptr() to
deal with filp more simply and in teh erorr path can just return -EBADF
directly rather than the goto.

Or are we avoiding that stuff in iommufd?

> + fput(filp);
> + return rc;
> +}
> +
> +static void viommu_put_kvm(struct iommufd_viommu *viommu)
> +{
> + fput(viommu->kvm_filp);
> + viommu->kvm_filp = NULL;
> +}
> +#endif