From nobody Wed Feb 11 02:09:12 2026 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=1693392917; cv=none; d=zohomail.com; s=zohoarc; b=Q5fh8vBXqtHhAbm/C8TiD+GFpJbkr4zsnnZJGAmVRsufQilCiCeHRmvkeW0xCgCPO0kxzGau6g7vrtdnfS5snuhnNcmX19h7KHXy3dyoS9zpxRRENVBDUO1i2LCrg2yiuWluj1oNmP2TQy3Xiyab1TjTahFSrJGe5YmnqMwx5j4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693392917; 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=oz1zT5HvIcNhNlD8V+S8K/ZakZGZFYpsK3fQHKbFc1g=; b=F6wfFECw3Pu3q5o5WkXSuPsd4nV70S86kUOhkc0SnON8I8NpQ6/efpywXzrgs6GStk3QEts83q77mSbJqlUN4Te6s1FLFSPR8BrO2iGZ947LilaCM5yWAcA6otP5BF2+Q5Lo9jmUyClov73mfFIzbB1pMa7sTkhn+t4dB0rWaYs= 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 1693392917658462.10594103758524; Wed, 30 Aug 2023 03:55:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qbIou-0004Di-9A; Wed, 30 Aug 2023 06:53:08 -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 1qbIos-0004DL-9K for qemu-devel@nongnu.org; Wed, 30 Aug 2023 06:53:06 -0400 Received: from mgamail.intel.com ([192.55.52.93]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qbIop-0007eq-7q for qemu-devel@nongnu.org; Wed, 30 Aug 2023 06:53:06 -0400 Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2023 03:52:52 -0700 Received: from duan-server-s2600bt.bj.intel.com ([10.240.192.147]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2023 03:52:47 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1693392783; x=1724928783; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=D9aTIKHB/HvRfwXvd1jYJrNHVmQZAyYwCbrHCKAztgM=; b=ZP7XwARG085Iimh5Lezh2D784VLtHwq12G3TbwEbQWKXFh4UDO/XLn9s B/vtl01yX6dzj2cYIchROkbbOG2MKk4qPDwdKHtFSg5gu1Uxc60e9WLDl uO2u0nnnNvqLM9KEjaTBQtofyA1bi4NWXhnqW1JPHHor80XpXK70+F4SI o2tdHg909kEsU8r2PwYta98QjwqyCKAsVkcnwys3RKm2S8yOMziQxNyAu RclNfqaRSAXt0boB5uHXqtR/8wczR+bOQJTdoVRFxRM/a65yZzTCUWJUo rvkcVkGVfhT3GxjeHVJWYyvoQvR+WOacrvp7VxpkzHFWUG03Fw0XMYXOZ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10817"; a="373016421" X-IronPort-AV: E=Sophos;i="6.02,213,1688454000"; d="scan'208";a="373016421" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10817"; a="715866064" X-IronPort-AV: E=Sophos;i="6.02,213,1688454000"; d="scan'208";a="715866064" From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, eric.auger@redhat.com, peterx@redhat.com, jasowang@redhat.com, kevin.tian@intel.com, yi.l.liu@intel.com, yi.y.sun@intel.com, chao.p.peng@intel.com, Zhenzhong Duan Subject: [PATCH v1 05/22] vfio/common: Extract out vfio_kvm_device_[add/del]_fd Date: Wed, 30 Aug 2023 18:37:37 +0800 Message-Id: <20230830103754.36461-6-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230830103754.36461-1-zhenzhong.duan@intel.com> References: <20230830103754.36461-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=192.55.52.93; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.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, 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: 1693392919319100003 Content-Type: text/plain; charset="utf-8" ...which will be used by both legacy and iommufd backend. Signed-off-by: Yi Liu Signed-off-by: Zhenzhong Duan --- hw/vfio/common.c | 44 +++++++++++++++++++++++------------ include/hw/vfio/vfio-common.h | 3 +++ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 67150e4575..949ad6714a 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1759,17 +1759,17 @@ void vfio_reset_handler(void *opaque) } } =20 -static void vfio_kvm_device_add_group(VFIOGroup *group) +int vfio_kvm_device_add_fd(int fd) { #ifdef CONFIG_KVM struct kvm_device_attr attr =3D { - .group =3D KVM_DEV_VFIO_GROUP, - .attr =3D KVM_DEV_VFIO_GROUP_ADD, - .addr =3D (uint64_t)(unsigned long)&group->fd, + .group =3D KVM_DEV_VFIO_FILE, + .attr =3D KVM_DEV_VFIO_FILE_ADD, + .addr =3D (uint64_t)(unsigned long)&fd, }; =20 if (!kvm_enabled()) { - return; + return 0; } =20 if (vfio_kvm_device_fd < 0) { @@ -1779,37 +1779,51 @@ static void vfio_kvm_device_add_group(VFIOGroup *gr= oup) =20 if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) { error_report("Failed to create KVM VFIO device: %m"); - return; + return -ENODEV; } =20 vfio_kvm_device_fd =3D cd.fd; } =20 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { - error_report("Failed to add group %d to KVM VFIO device: %m", - group->groupid); + error_report("Failed to add fd %d to KVM VFIO device: %m", + fd); + return -errno; } #endif + return 0; } =20 -static void vfio_kvm_device_del_group(VFIOGroup *group) +static void vfio_kvm_device_add_group(VFIOGroup *group) +{ + vfio_kvm_device_add_fd(group->fd); +} + +int vfio_kvm_device_del_fd(int fd) { #ifdef CONFIG_KVM struct kvm_device_attr attr =3D { - .group =3D KVM_DEV_VFIO_GROUP, - .attr =3D KVM_DEV_VFIO_GROUP_DEL, - .addr =3D (uint64_t)(unsigned long)&group->fd, + .group =3D KVM_DEV_VFIO_FILE, + .attr =3D KVM_DEV_VFIO_FILE_DEL, + .addr =3D (uint64_t)(unsigned long)&fd, }; =20 if (vfio_kvm_device_fd < 0) { - return; + return -EINVAL; } =20 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { - error_report("Failed to remove group %d from KVM VFIO device: %m", - group->groupid); + error_report("Failed to remove fd %d from KVM VFIO device: %m", + fd); + return -EBADF; } #endif + return 0; +} + +static void vfio_kvm_device_del_group(VFIOGroup *group) +{ + vfio_kvm_device_del_fd(group->fd); } =20 static VFIOAddressSpace *vfio_get_address_space(AddressSpace *as) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 5e376c436e..598c3ce079 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -220,6 +220,9 @@ struct vfio_device_info *vfio_get_device_info(int fd); int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vbasedev, Error **errp); =20 +int vfio_kvm_device_add_fd(int fd); +int vfio_kvm_device_del_fd(int fd); + extern const MemoryRegionOps vfio_region_ops; typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList; extern VFIOGroupList vfio_group_list; --=20 2.34.1