From nobody Thu Apr 3 11:27:44 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1742976032; cv=none; d=zohomail.com; s=zohoarc; b=ef7lPwIEfIsTfu2fbQhkcpx888b/5y3Y6nwaIPP6/whxyUjj8rth3ALwqLneU6vWo+hbPVUekRmKIgJAl0XDcleViPd9SW2Z3qZSiIwfu3cJsgIYBlvbDpJNdveUWvQw1g6DBiqI+zxaBQInVyC1s0s6xjkH+gOyjyp5buIilBQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1742976032; 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=CR5onqLi/nwk9XV1BwqHLz4Pmvnr+XtpFIJ+MyDpn3Y=; b=Idm8vz7xZfIZY8V1bwZlhI2Stt1/xiO9kqm7kafLW1nIgfPhFbMrz55ptblSC++1LvP0f5PkC4I3BFT6K8kT9Sd9cUrpaYb42rDMAX85j/hCFXzCwi8pIIg+D9pMHaxpFi00cT95KadlhI7cH9z58TRTrUTNRBWhYYjZ4XS+TGA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1742976032442698.7398875840371; Wed, 26 Mar 2025 01:00:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1txLZM-0000ah-DB; Wed, 26 Mar 2025 03:53:00 -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 1txLZ9-0008W1-IS for qemu-devel@nongnu.org; Wed, 26 Mar 2025 03:52:47 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1txLZ7-0006J1-FO for qemu-devel@nongnu.org; Wed, 26 Mar 2025 03:52:47 -0400 Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-329-oB9fO4sIP2uqeMSqEl8TUw-1; Wed, 26 Mar 2025 03:52:43 -0400 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E99931933B48; Wed, 26 Mar 2025 07:52:41 +0000 (UTC) Received: from corto.redhat.com (unknown [10.45.226.180]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3769B19560AB; Wed, 26 Mar 2025 07:52:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1742975564; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CR5onqLi/nwk9XV1BwqHLz4Pmvnr+XtpFIJ+MyDpn3Y=; b=fe/0An+PaqZm9wwjMTpdBPzG++HvxtQTmCYEL5wI8PWdirSJ/sxuJrOEoM9wloeregLHmw Rghx9V/69BviDz1MB4uneFZOF2ftEI+jS23xEsIeiQWCoQRGDjBrFI+tmH3tV7+HGYc/OL yLFthNxKm6hOL4FBivnSaOu6YUGUNi0= X-MC-Unique: oB9fO4sIP2uqeMSqEl8TUw-1 X-Mimecast-MFC-AGG-ID: oB9fO4sIP2uqeMSqEl8TUw_1742975562 From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= To: qemu-devel@nongnu.org, Alex Williamson Cc: Avihai Horon , Eric Auger , Zhenzhong Duan , John Levon , Joao Martins , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [PATCH for-10.1 v2 18/37] vfio: Move vfio_kvm_device_add/del_fd() to helpers.c Date: Wed, 26 Mar 2025 08:51:03 +0100 Message-ID: <20250326075122.1299361-19-clg@redhat.com> In-Reply-To: <20250326075122.1299361-1-clg@redhat.com> References: <20250326075122.1299361-1-clg@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 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=170.10.129.124; envelope-from=clg@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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 @redhat.com) X-ZM-MESSAGEID: 1742976033508019100 vfio_kvm_device_add/del_fd() are low level routines. Move them with the other helpers. Reviewed-by: Zhenzhong Duan Reviewed-by: John Levon Link: https://lore.kernel.org/qemu-devel/20250318095415.670319-18-clg@redha= t.com Signed-off-by: C=C3=A9dric Le Goater --- hw/vfio/vfio-helpers.h | 3 ++ include/hw/vfio/vfio-common.h | 3 -- hw/vfio/common.c | 58 ---------------------------------- hw/vfio/helpers.c | 59 +++++++++++++++++++++++++++++++++++ hw/vfio/iommufd.c | 1 + hw/vfio/meson.build | 2 +- 6 files changed, 64 insertions(+), 62 deletions(-) diff --git a/hw/vfio/vfio-helpers.h b/hw/vfio/vfio-helpers.h index 9af43878b8b90c0247221543423a5aca8be2eef5..5d91e33d27be1bdd8eb9b3ce74c= 39a4ac1991adc 100644 --- a/hw/vfio/vfio-helpers.h +++ b/hw/vfio/vfio-helpers.h @@ -26,4 +26,7 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info= *info, =20 int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size); =20 +int vfio_kvm_device_add_fd(int fd, Error **errp); +int vfio_kvm_device_del_fd(int fd, Error **errp); + #endif /* HW_VFIO_VFIO_HELPERS_H */ diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index f5ec9fb5ba4616ece30e43ce38b3a4e46e2c9c3c..5cf7ae617371a2f8a09e52b1b43= 8fa23703c6dd8 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -130,9 +130,6 @@ bool vfio_attach_device(char *name, VFIODevice *vbasede= v, void vfio_detach_device(VFIODevice *vbasedev); VFIODevice *vfio_get_vfio_device(Object *obj); =20 -int vfio_kvm_device_add_fd(int fd, Error **errp); -int vfio_kvm_device_del_fd(int fd, Error **errp); - bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **er= rp); void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer); =20 diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 4b3f26d4c0ced17c93f6a1fab02c85181d55f6be..cbe69fb3225f7d30fbded348ace= a597b26f98651 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1334,64 +1334,6 @@ void vfio_reset_handler(void *opaque) } } =20 -int vfio_kvm_device_add_fd(int fd, Error **errp) -{ -#ifdef CONFIG_KVM - struct kvm_device_attr attr =3D { - .group =3D KVM_DEV_VFIO_FILE, - .attr =3D KVM_DEV_VFIO_FILE_ADD, - .addr =3D (uint64_t)(unsigned long)&fd, - }; - - if (!kvm_enabled()) { - return 0; - } - - if (vfio_kvm_device_fd < 0) { - struct kvm_create_device cd =3D { - .type =3D KVM_DEV_TYPE_VFIO, - }; - - if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) { - error_setg_errno(errp, errno, "Failed to create KVM VFIO devic= e"); - return -errno; - } - - vfio_kvm_device_fd =3D cd.fd; - } - - if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { - error_setg_errno(errp, errno, "Failed to add fd %d to KVM VFIO dev= ice", - fd); - return -errno; - } -#endif - return 0; -} - -int vfio_kvm_device_del_fd(int fd, Error **errp) -{ -#ifdef CONFIG_KVM - struct kvm_device_attr attr =3D { - .group =3D KVM_DEV_VFIO_FILE, - .attr =3D KVM_DEV_VFIO_FILE_DEL, - .addr =3D (uint64_t)(unsigned long)&fd, - }; - - if (vfio_kvm_device_fd < 0) { - error_setg(errp, "KVM VFIO device isn't created yet"); - return -EINVAL; - } - - if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { - error_setg_errno(errp, errno, - "Failed to remove fd %d from KVM VFIO device", fd= ); - return -errno; - } -#endif - return 0; -} - struct vfio_device_info *vfio_get_device_info(int fd) { struct vfio_device_info *info; diff --git a/hw/vfio/helpers.c b/hw/vfio/helpers.c index 1a584ba5f007995a16d2364faeb72cb2b22de4e9..e6b75baa805241b4df28769338b= 15a6fe9b99501 100644 --- a/hw/vfio/helpers.c +++ b/hw/vfio/helpers.c @@ -22,6 +22,7 @@ #include "qemu/osdep.h" #include =20 +#include "system/kvm.h" #include "hw/vfio/vfio-common.h" #include "hw/vfio/pci.h" #include "hw/hw.h" @@ -253,6 +254,64 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1_i= nfo *info, return true; } =20 +int vfio_kvm_device_add_fd(int fd, Error **errp) +{ +#ifdef CONFIG_KVM + struct kvm_device_attr attr =3D { + .group =3D KVM_DEV_VFIO_FILE, + .attr =3D KVM_DEV_VFIO_FILE_ADD, + .addr =3D (uint64_t)(unsigned long)&fd, + }; + + if (!kvm_enabled()) { + return 0; + } + + if (vfio_kvm_device_fd < 0) { + struct kvm_create_device cd =3D { + .type =3D KVM_DEV_TYPE_VFIO, + }; + + if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) { + error_setg_errno(errp, errno, "Failed to create KVM VFIO devic= e"); + return -errno; + } + + vfio_kvm_device_fd =3D cd.fd; + } + + if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { + error_setg_errno(errp, errno, "Failed to add fd %d to KVM VFIO dev= ice", + fd); + return -errno; + } +#endif + return 0; +} + +int vfio_kvm_device_del_fd(int fd, Error **errp) +{ +#ifdef CONFIG_KVM + struct kvm_device_attr attr =3D { + .group =3D KVM_DEV_VFIO_FILE, + .attr =3D KVM_DEV_VFIO_FILE_DEL, + .addr =3D (uint64_t)(unsigned long)&fd, + }; + + if (vfio_kvm_device_fd < 0) { + error_setg(errp, "KVM VFIO device isn't created yet"); + return -EINVAL; + } + + if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { + error_setg_errno(errp, errno, + "Failed to remove fd %d from KVM VFIO device", fd= ); + return -errno; + } +#endif + return 0; +} + int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type, uint32_t subtype, struct vfio_region_info **i= nfo) { diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c index 2ec15bc2692e2eb90299e5a1c09ab6b738c69cf0..85b5a8146ac401af6be8109c28e= a4c7a39e84521 100644 --- a/hw/vfio/iommufd.c +++ b/hw/vfio/iommufd.c @@ -26,6 +26,7 @@ #include "qemu/chardev_open.h" #include "pci.h" #include "vfio-iommufd.h" +#include "vfio-helpers.h" =20 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO \ TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio" diff --git a/hw/vfio/meson.build b/hw/vfio/meson.build index 07010c7c9e01a39ae3449c54d2027a2cdd0a7a4d..21795b3d19e5db0c93993c0cb4a= 951a70d260f10 100644 --- a/hw/vfio/meson.build +++ b/hw/vfio/meson.build @@ -2,6 +2,7 @@ vfio_ss =3D ss.source_set() vfio_ss.add(files( 'common.c', 'container.c', + 'helpers.c', )) vfio_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr.c')) vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files( @@ -18,7 +19,6 @@ specific_ss.add_all(when: 'CONFIG_VFIO', if_true: vfio_ss) system_ss.add(when: 'CONFIG_VFIO_XGMAC', if_true: files('calxeda-xgmac.c')) system_ss.add(when: 'CONFIG_VFIO_AMD_XGBE', if_true: files('amd-xgbe.c')) system_ss.add(when: 'CONFIG_VFIO', if_true: files( - 'helpers.c', 'container-base.c', 'migration.c', 'migration-multifd.c', --=20 2.49.0