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

Jason Gunthorpe
2025-07-28T14:10:29Z

On Mon, Jul 28, 2025 at 07:21:44PM +0530, Aneesh Kumar K.V (Arm) wrote:

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

Missing stub functions for !CONFIG_KVM?

Looks like an OK design otherwise

> @@ -1057,6 +1068,7 @@ struct iommu_viommu_alloc {
> __u32 data_len;
> __u32 __reserved;
> __aligned_u64 data_uptr;
> + __u32 kvm_vm_fd;

fds are __s32, they are signed numbers.

Jason