From patchwork Wed Mar 8 13:28:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 13165813 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CEF7DC64EC4 for ; Wed, 8 Mar 2023 13:29:43 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4006910E630; Wed, 8 Mar 2023 13:29:43 +0000 (UTC) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by gabe.freedesktop.org (Postfix) with ESMTPS id 5A25D10E624; Wed, 8 Mar 2023 13:29:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678282176; x=1709818176; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VuqafYByKK/jWkKRq463YXQFQwOvpfEb1z0GI9PVpxY=; b=Dh5NfmHFutfwxWeEwtLnvQ/95VB75apgQtO1vnWaoW7RyzVm7YbORkvp zh/mtOYWkh4ZSTM0fpEoZVEoU/iZx3x/HHwxdwXKfgyqaD+ZecsyVLPYQ BNrQSgwj9cbGZLWMr9lJIrDyzl6Po+5kdJkJ5gCvfjvDy4Xnc1Z01sQIv njpOJwm3aHnTuJa+GesVLpwAb7rAihZsmrVtUoHqRI48Mz/vFM6oSyITK Jok2BUA8/7C5ubh+CH9Bam2D89EuTbbLDew0RlcgqwawclbBXnvqxV2tT WK/3ydefx8P7fFfly1lA5QMFIEEhfWXSpstJoWa1qvmTxHQydIY3UVL7w w==; X-IronPort-AV: E=McAfee;i="6500,9779,10642"; a="336165295" X-IronPort-AV: E=Sophos;i="5.98,244,1673942400"; d="scan'208";a="336165295" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Mar 2023 05:29:36 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10642"; a="922789423" X-IronPort-AV: E=Sophos;i="5.98,244,1673942400"; d="scan'208";a="922789423" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 08 Mar 2023 05:29:35 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Date: Wed, 8 Mar 2023 05:28:56 -0800 Message-Id: <20230308132903.465159-18-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230308132903.465159-1-yi.l.liu@intel.com> References: <20230308132903.465159-1-yi.l.liu@intel.com> MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH v6 17/24] vfio-iommufd: Make vfio_iommufd_bind() selectively return devid X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-s390@vger.kernel.org, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, mjrosato@linux.ibm.com, kvm@vger.kernel.org, intel-gvt-dev@lists.freedesktop.org, joro@8bytes.org, cohuck@redhat.com, xudong.hao@intel.com, peterx@redhat.com, yan.y.zhao@intel.com, eric.auger@redhat.com, terrence.xu@intel.com, nicolinc@nvidia.com, shameerali.kolothum.thodi@huawei.com, suravee.suthikulpanit@amd.com, intel-gfx@lists.freedesktop.org, chao.p.peng@linux.intel.com, lulu@redhat.com, robin.murphy@arm.com, jasowang@redhat.com Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" bind_iommufd() will generate an ID to represent this bond, it is needed by userspace for further usage. devid is stored in vfio_device_file to avoid passing devid pointer in multiple places. Signed-off-by: Yi Liu --- drivers/vfio/iommufd.c | 13 ++++++++++--- drivers/vfio/vfio.h | 6 ++++-- drivers/vfio/vfio_main.c | 8 +++++--- include/linux/iommufd.h | 2 ++ 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c index 8c518f8bd39a..b2cdb6b2b37f 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -28,7 +28,8 @@ static int vfio_iommufd_device_probe_comapt_noiommu(struct vfio_device *vdev, return 0; } -int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx) +int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx, + u32 *devid) { u32 device_id; int ret; @@ -44,8 +45,14 @@ int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx) if (WARN_ON(!vdev->ops->bind_iommufd)) return -ENODEV; - /* The legacy path has no way to return the device id */ - return vdev->ops->bind_iommufd(vdev, ictx, &device_id); + ret = vdev->ops->bind_iommufd(vdev, ictx, &device_id); + if (ret) + return ret; + + if (devid) + *devid = device_id; + + return 0; } int vfio_iommufd_attach_compat_ioas(struct vfio_device *vdev, diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 615ffd58562b..98cee2f765e9 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -24,6 +24,7 @@ struct vfio_device_file { spinlock_t kvm_ref_lock; /* protect kvm field */ struct kvm *kvm; struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */ + u32 devid; /* only valid when iommufd is valid */ }; void vfio_device_put_registration(struct vfio_device *device); @@ -236,13 +237,14 @@ static inline void vfio_container_cleanup(void) #endif #if IS_ENABLED(CONFIG_IOMMUFD) -int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx); +int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx, + u32 *devid); void vfio_iommufd_unbind(struct vfio_device *device); int vfio_iommufd_attach_compat_ioas(struct vfio_device *vdev, struct iommufd_ctx *ictx); #else static inline int vfio_iommufd_bind(struct vfio_device *device, - struct iommufd_ctx *ictx) + struct iommufd_ctx *ictx, u32 *devid) { return -EOPNOTSUPP; } diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 8c73df1a400e..a66ca138059b 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -444,7 +444,7 @@ static int vfio_device_first_open(struct vfio_device_file *df) * to be done here just go ahead to open device. */ if (iommufd) - ret = vfio_iommufd_bind(device, iommufd); + ret = vfio_iommufd_bind(device, iommufd, &df->devid); else if (vfio_device_group_uses_container(df)) ret = vfio_device_group_use_iommu(device); if (ret) @@ -476,10 +476,12 @@ static void vfio_device_last_close(struct vfio_device_file *df) if (device->ops->close_device) device->ops->close_device(device); - if (iommufd) + if (iommufd) { vfio_iommufd_unbind(device); - else if (vfio_device_group_uses_container(df)) + df->devid = IOMMUFD_INVALID_ID; + } else if (vfio_device_group_uses_container(df)) { vfio_device_group_unuse_iommu(device); + } module_put(device->dev->driver->owner); } diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 7a0d7f2c4237..48b9bfab9891 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -10,6 +10,8 @@ #include #include +#define IOMMUFD_INVALID_ID 0 + struct device; struct iommufd_device; struct page;