From nobody Sun Nov 16 01:14:28 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=1745393590; cv=none; d=zohomail.com; s=zohoarc; b=QXXqE6o3XSary7y+rjsD0Y+vtepBw7tQRWOwSnaBGsK4npw0RZ0TiGTBhcC6LJnVUL4m0hh4tTOz35Tn/LOcWNRRhawFYG81jWwiLnWD8o1beLdEMS5dz9vBreaCKGNjnH73R+dMoIEsPeiwfHK4HgNDA8qo9HrO3os7RUV2EZQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1745393590; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Z/sKeB+VfiwRxc657w4vusZn0EOamc7GAF6HATJjz8o=; b=c9hSYAmeO+15uKua/g9erKzYjjA1KduQ9sHF0AoeP5o5Jp4YArWDf5JKyATvBcTCnEFzuUIFxUy8fBVgG2CcrLHcrtEUwmMyyYOrly9fcI/tw17t6bCVAoxpkf4moRgWuOUPr9rFB2AbMIWoJwtzNRIQAracUITCNXN5v4mcTks= 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 1745393590855479.0634372653982; Wed, 23 Apr 2025 00:33:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u7Uas-0006wl-KN; Wed, 23 Apr 2025 03:32:30 -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 1u7Uaj-0006jM-GN for qemu-devel@nongnu.org; Wed, 23 Apr 2025 03:32:22 -0400 Received: from mgamail.intel.com ([198.175.65.13]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u7Uah-0002Yx-BQ for qemu-devel@nongnu.org; Wed, 23 Apr 2025 03:32:21 -0400 Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Apr 2025 00:32:18 -0700 Received: from spr-s2600bt.bj.intel.com ([10.240.192.127]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Apr 2025 00:32:16 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1745393539; x=1776929539; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4aYU2aYrGZv6Ifhv0cwF9ZzIZVaej/rWoKf+nIOTJao=; b=U4eZxYrrNZF99p55XV7NBz0jysihY1cm/4B10NlW1mi984rWAJEAX9Cm Osu+cAbXhypf7w7flv2LVDCaa4ekKR6WUp1yr20TvyqtgQmmhiw0SZ1Dn FNqMl9960eE6sT8UiOQHHmZQ0x5o0kMh9UaC1Ke12qrw3XSp/T3LK4ati WkhUfXS3dc4fYjtjQhM2l/UxazTCSzhbEPL3uB2gm7jXfmpG154C4BXdf qCGHuQlGD+Vkf5Aio4+8nWHdcgGrtg4ETGaPmFsbCxHK8BOVRTz6tzXUu Dp1OOnLQuSF8Pw/lBSd8VWna1zIJ//RWLwqV9GMCza2hzDO5JzW66qhmT g==; X-CSE-ConnectionGUID: ixl9HH4SR+yAgM5wPQFcHg== X-CSE-MsgGUID: RJ58yXmMQNG+oZkuPI5dsg== X-IronPort-AV: E=McAfee;i="6700,10204,11411"; a="57959270" X-IronPort-AV: E=Sophos;i="6.15,233,1739865600"; d="scan'208";a="57959270" X-CSE-ConnectionGUID: EEGeXqPYR1KTGTUsIKWUQg== X-CSE-MsgGUID: sze0xJy/Qeew13k5jgDuYw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,233,1739865600"; d="scan'208";a="137025390" From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, yi.l.liu@intel.com, chao.p.peng@intel.com, Zhenzhong Duan Subject: [PATCH v2 4/5] vfio: Cleanup host IOMMU device creation Date: Wed, 23 Apr 2025 15:28:23 +0800 Message-Id: <20250423072824.3647952-5-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250423072824.3647952-1-zhenzhong.duan@intel.com> References: <20250423072824.3647952-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=198.175.65.13; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -47 X-Spam_score: -4.8 X-Spam_bar: ---- X-Spam_report: (-4.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.411, 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_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: 1745393594172019100 realize() is now moved after attachment, do the same for hiod creation. Introduce a new function vfio_device_hiod_create_and_realize() to do them all in one go. Suggested-by: C=C3=A9dric Le Goater Signed-off-by: Zhenzhong Duan --- include/hw/vfio/vfio-device.h | 3 ++- hw/vfio/container.c | 5 ++++- hw/vfio/device.c | 33 ++++++++++++++------------------- hw/vfio/iommufd.c | 4 +++- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index 66797b4c92..65fa67e65a 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -123,7 +123,8 @@ bool vfio_device_irq_set_signaling(VFIODevice *vbasedev= , int index, int subindex =20 void vfio_device_reset_handler(void *opaque); bool vfio_device_is_mdev(VFIODevice *vbasedev); -bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp); +bool vfio_device_hiod_create_and_realize(VFIODevice *vbasedev, + const char *typename, Error **err= p); bool vfio_device_attach(char *name, VFIODevice *vbasedev, AddressSpace *as, Error **errp); void vfio_device_detach(VFIODevice *vbasedev); diff --git a/hw/vfio/container.c b/hw/vfio/container.c index 1bb8e2de6c..2de20692d1 100644 --- a/hw/vfio/container.c +++ b/hw/vfio/container.c @@ -898,7 +898,9 @@ static bool vfio_legacy_attach_device(const char *name,= VFIODevice *vbasedev, goto group_put_exit; } =20 - if (!vfio_device_hiod_realize(vbasedev, errp)) { + if (!vfio_device_hiod_create_and_realize(vbasedev, + TYPE_HOST_IOMMU_DEVICE_LEGACY= _VFIO, + errp)) { goto device_put_exit; } =20 @@ -924,6 +926,7 @@ static void vfio_legacy_detach_device(VFIODevice *vbase= dev) QLIST_REMOVE(vbasedev, container_next); vbasedev->bcontainer =3D NULL; trace_vfio_device_detach(vbasedev->name, group->groupid); + object_unref(vbasedev->hiod); vfio_device_put(vbasedev); vfio_group_put(group); } diff --git a/hw/vfio/device.c b/hw/vfio/device.c index 4de6948cf4..d625a7c4db 100644 --- a/hw/vfio/device.c +++ b/hw/vfio/device.c @@ -347,15 +347,24 @@ bool vfio_device_is_mdev(VFIODevice *vbasedev) return subsys && (strcmp(subsys, "/sys/bus/mdev") =3D=3D 0); } =20 -bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp) +bool vfio_device_hiod_create_and_realize(VFIODevice *vbasedev, + const char *typename, Error **err= p) { - HostIOMMUDevice *hiod =3D vbasedev->hiod; + HostIOMMUDevice *hiod; =20 - if (!hiod) { + if (vbasedev->mdev) { return true; } =20 - return HOST_IOMMU_DEVICE_GET_CLASS(hiod)->realize(hiod, vbasedev, errp= ); + hiod =3D HOST_IOMMU_DEVICE(object_new(typename)); + + if (!HOST_IOMMU_DEVICE_GET_CLASS(hiod)->realize(hiod, vbasedev, errp))= { + object_unref(hiod); + return false; + } + + vbasedev->hiod =3D hiod; + return true; } =20 VFIODevice *vfio_get_vfio_device(Object *obj) @@ -372,7 +381,6 @@ bool vfio_device_attach(char *name, VFIODevice *vbasede= v, { const VFIOIOMMUClass *ops =3D VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_LEGACY)); - HostIOMMUDevice *hiod =3D NULL; =20 if (vbasedev->iommufd) { ops =3D VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMM= UFD)); @@ -380,19 +388,7 @@ bool vfio_device_attach(char *name, VFIODevice *vbased= ev, =20 assert(ops); =20 - - if (!vbasedev->mdev) { - hiod =3D HOST_IOMMU_DEVICE(object_new(ops->hiod_typename)); - vbasedev->hiod =3D hiod; - } - - if (!ops->attach_device(name, vbasedev, as, errp)) { - object_unref(hiod); - vbasedev->hiod =3D NULL; - return false; - } - - return true; + return ops->attach_device(name, vbasedev, as, errp); } =20 void vfio_device_detach(VFIODevice *vbasedev) @@ -400,6 +396,5 @@ void vfio_device_detach(VFIODevice *vbasedev) if (!vbasedev->bcontainer) { return; } - object_unref(vbasedev->hiod); VFIO_IOMMU_GET_CLASS(vbasedev->bcontainer)->detach_device(vbasedev); } diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c index f273dc8712..8a010a51ea 100644 --- a/hw/vfio/iommufd.c +++ b/hw/vfio/iommufd.c @@ -574,7 +574,8 @@ found_container: goto err_listener_register; } =20 - if (!vfio_device_hiod_realize(vbasedev, errp)) { + if (!vfio_device_hiod_create_and_realize(vbasedev, + TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO, errp)) { goto err_hiod_realize; } =20 @@ -630,6 +631,7 @@ static void iommufd_cdev_detach(VFIODevice *vbasedev) iommufd_cdev_ram_block_discard_disable(false); } =20 + object_unref(vbasedev->hiod); vfio_cpr_unregister_container(bcontainer); iommufd_cdev_detach_container(vbasedev, container); iommufd_cdev_container_destroy(container); --=20 2.34.1