From nobody Fri Dec 19 13:24:55 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1689147606; cv=none; d=zohomail.com; s=zohoarc; b=Rj83v4eEOQFXY+FvevT+CRc1O01ql3T7cWIt5qpFN5sv0eqheiKoQsGjGM4bsnYCAFRybAd3fRhdF5qR59z0DxRr8LvN48n9UrGDoNbArRTVk2w3+qV45n/39mETdzC/8a+mO42VMTudNl8gg4L8UPojElKF9n5e6Llc1Kp24w0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1689147606; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=zUkMrD0z3FRIjtBli1SV/KsnH9bg+ycfhTa/zafcdDQ=; b=JmDc3zBglvUBBDK0rayNYIUr9z19M1+gg0eQoOKnlXqKnXqJa4WFbjx9K8TPQ5PpZASzQh4zEig5m8H0VKY1iO/rGtA4tTBo4JIA/ULw54mfKYd/MNWFoahxU45qDy8+OVLx2YUv3dzBpHxjYCy7O6jIbxg0roMwqojhg1LC36U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1689147606962336.33531691014514; Wed, 12 Jul 2023 00:40:06 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qJURv-0004Gu-Sj; Wed, 12 Jul 2023 03:39:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qJURu-0004GQ-Ih for qemu-devel@nongnu.org; Wed, 12 Jul 2023 03:39:46 -0400 Received: from mga07.intel.com ([134.134.136.100]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qJURs-0006oM-K9 for qemu-devel@nongnu.org; Wed, 12 Jul 2023 03:39:46 -0400 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jul 2023 00:39:38 -0700 Received: from duan-server-s2600bt.bj.intel.com ([10.240.192.147]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jul 2023 00:39:32 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689147584; x=1720683584; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RZR2Ekd1xY9mtZIVTyh0arSpbRzSxXZX16ZAwZ/xk4g=; b=MCXHRlM+m9fVPDVAgfr3q/wY7IVboZLS1uW1oBqYTyNKZwjoYd6xGGvP ZGwzlkrHhiMDWSRicLRF8edO6UUOdnR4up0vPErszosy+Oqo3PkOFtwVW eR12zfKgZtvxRGnfB8sHITy1R7H9/8jrA9fZ74QxwtLPS/nSjecKKgrd0 nlgwIjZBcEwkoait5+BUsHlhixpJSplEJlXAsEuVxDAhhDzkTFfXpG0gA fk7PiAP9ED9OD8IoCP8jY7sASWUIsRtvmyUFkTFr0FRhdgGmkRWIMeOWS wC+/tr7pjoDXnT/rHMb/B0/R+KA8qM/RYHTPr2EZYf4dQqFXaxILYhxuC g==; X-IronPort-AV: E=McAfee;i="6600,9927,10768"; a="430953697" X-IronPort-AV: E=Sophos;i="6.01,199,1684825200"; d="scan'208";a="430953697" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10768"; a="835024042" X-IronPort-AV: E=Sophos;i="6.01,199,1684825200"; d="scan'208";a="835024042" From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, eric.auger@redhat.com, peterx@redhat.com, jasonwang@redhat.com, kevin.tian@intel.com, yi.l.liu@intel.com, yi.y.sun@intel.com, chao.p.peng@intel.com, Zhenzhong Duan Subject: [RFC PATCH v4 11/24] vfio/container: Introduce vfio_[attach/detach]_device Date: Wed, 12 Jul 2023 15:25:15 +0800 Message-Id: <20230712072528.275577-12-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230712072528.275577-1-zhenzhong.duan@intel.com> References: <20230712072528.275577-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=134.134.136.100; envelope-from=zhenzhong.duan@intel.com; helo=mga07.intel.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1689147607682100001 Content-Type: text/plain; charset="utf-8" From: Eric Auger We want the VFIO devices to be able to use two different IOMMU callbacks, the legacy VFIO one and the new iommufd one. Introduce vfio_[attach/detach]_device which aim at hiding the underlying IOMMU backend (IOCTLs, datatypes, ...). Once vfio_attach_device completes, the device is attached to a security context and its fd can be used. Conversely When vfio_detach_device completes, the device has been detached to the security context. In this patch, only the vfio-pci device gets converted to use the new API. Subsequent patches will handle other devices. Signed-off-by: Eric Auger Signed-off-by: Yi Liu Signed-off-by: Zhenzhong Duan --- hw/vfio/container.c | 70 +++++++++++++++++++++++++++++++++++ hw/vfio/pci.c | 50 +++---------------------- include/hw/vfio/vfio-common.h | 3 ++ 3 files changed, 78 insertions(+), 45 deletions(-) diff --git a/hw/vfio/container.c b/hw/vfio/container.c index f45a2f6e66..23b9d5a901 100644 --- a/hw/vfio/container.c +++ b/hw/vfio/container.c @@ -1008,6 +1008,8 @@ int vfio_get_device(VFIOGroup *group, const char *nam= e, =20 void vfio_put_base_device(VFIODevice *vbasedev) { + g_free(vbasedev->name); + if (!vbasedev->group) { return; } @@ -1116,6 +1118,74 @@ int vfio_eeh_as_op(AddressSpace *as, uint32_t op) return vfio_eeh_container_op(container, op); } =20 +static int vfio_device_groupid(VFIODevice *vbasedev, Error **errp) +{ + char *tmp, group_path[PATH_MAX], *group_name; + int ret, groupid; + ssize_t len; + + tmp =3D g_strdup_printf("%s/iommu_group", vbasedev->sysfsdev); + len =3D readlink(tmp, group_path, sizeof(group_path)); + g_free(tmp); + + if (len <=3D 0 || len >=3D sizeof(group_path)) { + ret =3D len < 0 ? -errno : -ENAMETOOLONG; + error_setg_errno(errp, -ret, "no iommu_group found"); + return ret; + } + + group_path[len] =3D 0; + + group_name =3D basename(group_path); + if (sscanf(group_name, "%d", &groupid) !=3D 1) { + error_setg_errno(errp, errno, "failed to read %s", group_path); + return -errno; + } + return groupid; +} + +int vfio_attach_device(char *name, VFIODevice *vbasedev, + AddressSpace *as, Error **errp) +{ + int groupid =3D vfio_device_groupid(vbasedev, errp); + VFIODevice *vbasedev_iter; + VFIOGroup *group; + int ret; + + if (groupid < 0) { + return groupid; + } + + trace_vfio_realize(vbasedev->name, groupid); + group =3D vfio_get_group(groupid, as, errp); + if (!group) { + return -1; + } + + QLIST_FOREACH(vbasedev_iter, &group->device_list, next) { + if (strcmp(vbasedev_iter->name, vbasedev->name) =3D=3D 0) { + error_setg(errp, "device is already attached"); + vfio_put_group(group); + return -1; + } + } + ret =3D vfio_get_device(group, name, vbasedev, errp); + if (ret) { + vfio_put_group(group); + return -1; + } + + return 0; +} + +void vfio_detach_device(VFIODevice *vbasedev) +{ + VFIOGroup *group =3D vbasedev->group; + + vfio_put_base_device(vbasedev); + vfio_put_group(group); +} + static void vfio_iommu_backend_legacy_ops_class_init(ObjectClass *oc, void *data) { VFIOIOMMUBackendOpsClass *ops =3D VFIO_IOMMU_BACKEND_OPS_CLASS(oc); diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index a205c6b113..43e4da9bff 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -2828,10 +2828,9 @@ static void vfio_populate_device(VFIOPCIDevice *vdev= , Error **errp) =20 static void vfio_put_device(VFIOPCIDevice *vdev) { - g_free(vdev->vbasedev.name); g_free(vdev->msix); =20 - vfio_put_base_device(&vdev->vbasedev); + vfio_detach_device(&vdev->vbasedev); } =20 static void vfio_err_notifier_handler(void *opaque) @@ -2978,13 +2977,9 @@ static void vfio_realize(PCIDevice *pdev, Error **er= rp) { VFIOPCIDevice *vdev =3D VFIO_PCI(pdev); VFIODevice *vbasedev =3D &vdev->vbasedev; - VFIODevice *vbasedev_iter; - VFIOGroup *group; - char *tmp, *subsys, group_path[PATH_MAX], *group_name; + char *tmp, *subsys; Error *err =3D NULL; - ssize_t len; struct stat st; - int groupid; int i, ret; bool is_mdev; char uuid[UUID_FMT_LEN]; @@ -3015,39 +3010,6 @@ static void vfio_realize(PCIDevice *pdev, Error **er= rp) vbasedev->type =3D VFIO_DEVICE_TYPE_PCI; vbasedev->dev =3D DEVICE(vdev); =20 - tmp =3D g_strdup_printf("%s/iommu_group", vbasedev->sysfsdev); - len =3D readlink(tmp, group_path, sizeof(group_path)); - g_free(tmp); - - if (len <=3D 0 || len >=3D sizeof(group_path)) { - error_setg_errno(errp, len < 0 ? errno : ENAMETOOLONG, - "no iommu_group found"); - goto error; - } - - group_path[len] =3D 0; - - group_name =3D basename(group_path); - if (sscanf(group_name, "%d", &groupid) !=3D 1) { - error_setg_errno(errp, errno, "failed to read %s", group_path); - goto error; - } - - trace_vfio_realize(vbasedev->name, groupid); - - group =3D vfio_get_group(groupid, pci_device_iommu_address_space(pdev)= , errp); - if (!group) { - goto error; - } - - QLIST_FOREACH(vbasedev_iter, &group->device_list, next) { - if (strcmp(vbasedev_iter->name, vbasedev->name) =3D=3D 0) { - error_setg(errp, "device is already attached"); - vfio_put_group(group); - goto error; - } - } - /* * Mediated devices *might* operate compatibly with discarding of RAM,= but * we cannot know for certain, it depends on whether the mdev vendor d= river @@ -3065,7 +3027,6 @@ static void vfio_realize(PCIDevice *pdev, Error **err= p) if (vbasedev->ram_block_discard_allowed && !is_mdev) { error_setg(errp, "x-balloon-allowed only potentially compatible " "with mdev devices"); - vfio_put_group(group); goto error; } =20 @@ -3076,10 +3037,10 @@ static void vfio_realize(PCIDevice *pdev, Error **e= rrp) name =3D g_strdup(vbasedev->name); } =20 - ret =3D vfio_get_device(group, name, vbasedev, errp); + ret =3D vfio_attach_device(name, vbasedev, + pci_device_iommu_address_space(pdev), errp); g_free(name); if (ret) { - vfio_put_group(group); goto error; } =20 @@ -3313,12 +3274,12 @@ out_teardown: vfio_bars_exit(vdev); error: error_prepend(errp, VFIO_MSG_PREFIX, vbasedev->name); + vfio_detach_device(vbasedev); } =20 static void vfio_instance_finalize(Object *obj) { VFIOPCIDevice *vdev =3D VFIO_PCI(obj); - VFIOGroup *group =3D vdev->vbasedev.group; =20 vfio_display_finalize(vdev); vfio_bars_finalize(vdev); @@ -3332,7 +3293,6 @@ static void vfio_instance_finalize(Object *obj) * g_free(vdev->igd_opregion); */ vfio_put_device(vdev); - vfio_put_group(group); } =20 static void vfio_exitfn(PCIDevice *pdev) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index cd11ffa01b..f42aef2515 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -187,6 +187,9 @@ void vfio_put_group(VFIOGroup *group); struct vfio_device_info *vfio_get_device_info(int fd); int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vbasedev, Error **errp); +int vfio_attach_device(char *name, VFIODevice *vbasedev, + AddressSpace *as, Error **errp); +void vfio_detach_device(VFIODevice *vbasedev); =20 extern int vfio_kvm_device_fd; =20 --=20 2.34.1