From nobody Fri May 17 10:34:16 2024 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=1695030383; cv=none; d=zohomail.com; s=zohoarc; b=h+zTOlY7T8RB7Ddn5YUZG03iPwXaT3IDznvLhQlxT6Snc8LnNrt3Peia/ZI3TfjTKAdkHZqeSc1/O5Hc/LM3XagllZTpMVC6DZEFi/eC+qpw4r0HQlecgyxpgp7HsGPrQIy03ILbq/esUoH63uMFRU0pa3zbaBbYP+IN7ktjOnw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695030383; h=Content-Type: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=vhjR0H+cM7U5Z0BdtNLXXloonqQRE3z1vtLz7uKoc9g=; b=l0IK+ej3G7eqSD3CLqawROIuyRpHUGl7OkA+3aY4C7xxMwC0uFXOAFZFa3UzinJivCCittN8y8vr4HB24yiftE91GZADiT4WpyfQInOxFQQii5kk5yhOSdmVFoVO3W3KgbgjA8vSpIUS4uEiKyluAFifkb8BT0FNKTA+LhI48zE= 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 16950303830831018.3606339847587; Mon, 18 Sep 2023 02:46:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qiAor-0005oD-20; Mon, 18 Sep 2023 05:45:29 -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 1qiAoo-0005nd-25 for qemu-devel@nongnu.org; Mon, 18 Sep 2023 05:45:26 -0400 Received: from mgamail.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 1qiAoe-0000xj-7u for qemu-devel@nongnu.org; Mon, 18 Sep 2023 05:45:25 -0400 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Sep 2023 02:45:13 -0700 Received: from vmmteam.bj.intel.com ([10.240.193.84]) by FMSMGA003.fm.intel.com with ESMTP; 18 Sep 2023 02:45:11 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695030316; x=1726566316; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZnuW1WYEcEDGILKnDibSvGbY0oSLRt/k2KWdG1CgWKo=; b=K+YeTPpeQtgG489rE67U3iPMmLTXMBoGeg7+ZfvbWVzdxSa8d4yMaymb m+eSFM1IjYiZSpwwxGRcrozz72U9TaAzVoMQpOrVYQ7ciNks1Y4fYwcWY IRB8KOjcKpo1+L8yZWIvPqrx1AHbjXdwYNiVGBRDSsCSPaYGDuySgHwNM jS/flOMu+vEg4hibv48th3KirVBSrN4gNLcjwzJkFn1Fm6xNz1tJEFkqb 3+Msfcpq5/496yytxUTNxB871+nSkQAw0YtdU/gJahyvBQl57yC5uGLnY FW506LUdqok5XEHAtb3TxQirtei+LsTsIw4FSl22OMemUNg6umBdY9MPR g==; X-IronPort-AV: E=McAfee;i="6600,9927,10836"; a="446072269" X-IronPort-AV: E=Sophos;i="6.02,156,1688454000"; d="scan'208";a="446072269" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10836"; a="835955945" X-IronPort-AV: E=Sophos;i="6.02,156,1688454000"; d="scan'208";a="835955945" From: Jing Liu To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, pbonzini@redhat.com, kevin.tian@intel.com, reinette.chatre@intel.com, jing2.liu@intel.com, jing2.liu@linux.intel.com Subject: [PATCH v2 1/4] vfio/pci: detect the support of dynamic MSI-X allocation Date: Mon, 18 Sep 2023 05:45:04 -0400 Message-Id: <20230918094507.409050-2-jing2.liu@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230918094507.409050-1-jing2.liu@intel.com> References: <20230918094507.409050-1-jing2.liu@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=134.134.136.100; envelope-from=jing2.liu@intel.com; helo=mgamail.intel.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, 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: 1695030384037100002 Kernel provides the guidance of dynamic MSI-X allocation support of passthrough device, by clearing the VFIO_IRQ_INFO_NORESIZE flag to guide user space. Fetch the flags from host to determine if dynamic MSI-X allocation is supported. Originally-by: Reinette Chatre Signed-off-by: Jing Liu Reviewed-by: C=C3=A9dric Le Goater Reviewed-by: Alex Williamson --- Changes since v1: - Free msix when failed to get MSI-X irq info. (C=C3=A9dric) - Apply C=C3=A9dric's Reviewed-by. Changes since RFC v1: - Filter the dynamic MSI-X allocation flag and store as a bool type. (Alex) - Move the detection to vfio_msix_early_setup(). (Alex) - Report error of getting irq info and remove the trace of failure case. (Alex, C=C3=A9dric) --- hw/vfio/pci.c | 16 ++++++++++++++-- hw/vfio/pci.h | 1 + hw/vfio/trace-events | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index a205c6b1130f..60654ca28ab8 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -1493,7 +1493,9 @@ static void vfio_msix_early_setup(VFIOPCIDevice *vdev= , Error **errp) uint8_t pos; uint16_t ctrl; uint32_t table, pba; - int fd =3D vdev->vbasedev.fd; + int ret, fd =3D vdev->vbasedev.fd; + struct vfio_irq_info irq_info =3D { .argsz =3D sizeof(irq_info), + .index =3D VFIO_PCI_MSIX_IRQ_INDEX }; VFIOMSIXInfo *msix; =20 pos =3D pci_find_capability(&vdev->pdev, PCI_CAP_ID_MSIX); @@ -1530,6 +1532,15 @@ static void vfio_msix_early_setup(VFIOPCIDevice *vde= v, Error **errp) msix->pba_offset =3D pba & ~PCI_MSIX_FLAGS_BIRMASK; msix->entries =3D (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1; =20 + ret =3D ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info); + if (ret < 0) { + error_setg_errno(errp, -ret, "failed to get MSI-X irq info"); + g_free(msix); + return; + } + + msix->noresize =3D !!(irq_info.flags & VFIO_IRQ_INFO_NORESIZE); + /* * Test the size of the pba_offset variable and catch if it extends ou= tside * of the specified BAR. If it is the case, we need to apply a hardware @@ -1562,7 +1573,8 @@ static void vfio_msix_early_setup(VFIOPCIDevice *vdev= , Error **errp) } =20 trace_vfio_msix_early_setup(vdev->vbasedev.name, pos, msix->table_bar, - msix->table_offset, msix->entries); + msix->table_offset, msix->entries, + msix->noresize); vdev->msix =3D msix; =20 vfio_pci_fixup_msix_region(vdev); diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h index a2771b9ff3cc..0717574d79e9 100644 --- a/hw/vfio/pci.h +++ b/hw/vfio/pci.h @@ -113,6 +113,7 @@ typedef struct VFIOMSIXInfo { uint32_t table_offset; uint32_t pba_offset; unsigned long *pending; + bool noresize; } VFIOMSIXInfo; =20 #define TYPE_VFIO_PCI "vfio-pci" diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index 81ec7c7a958b..cc7c21365c92 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -27,7 +27,7 @@ vfio_vga_read(uint64_t addr, int size, uint64_t data) " (= 0x%"PRIx64", %d) =3D 0x%" vfio_pci_read_config(const char *name, int addr, int len, int val) " (%s, = @0x%x, len=3D0x%x) 0x%x" vfio_pci_write_config(const char *name, int addr, int val, int len) " (%s,= @0x%x, 0x%x, len=3D0x%x)" vfio_msi_setup(const char *name, int pos) "%s PCI MSI CAP @0x%x" -vfio_msix_early_setup(const char *name, int pos, int table_bar, int offset= , int entries) "%s PCI MSI-X CAP @0x%x, BAR %d, offset 0x%x, entries %d" +vfio_msix_early_setup(const char *name, int pos, int table_bar, int offset= , int entries, bool noresize) "%s PCI MSI-X CAP @0x%x, BAR %d, offset 0x%x,= entries %d, noresize %d" vfio_check_pcie_flr(const char *name) "%s Supports FLR via PCIe cap" vfio_check_pm_reset(const char *name) "%s Supports PM reset" vfio_check_af_flr(const char *name) "%s Supports FLR via AF cap" --=20 2.27.0 From nobody Fri May 17 10:34:16 2024 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=1695030409; cv=none; d=zohomail.com; s=zohoarc; b=lXRM7+pVTFPn1XSMBPu8Xzs+EHaAOmWqktM7UwBsEdaQLfIQxCAbjxMiiDi08/UAuFHc/tkV+xirX4vS/dGIu8FuUiXz6+6RBj4PMvxf8itQgVVXPFd0MHe1I4xlxLBeFSGDHBHiQWL1G7cG6e45wDW1+25YdKp1eQZDzrN82H4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695030409; 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=B/SVpbVwWVP+GQbeP/23XLLGyoafNJpau73bD7Yr2QU=; b=JuKoGOx7fHKLK2ofN/e9QBejyWPnjCp5ZFZ2sdOm082q8n8Q8PT6DsekipgRbXvJZj1nJR0p5azUybrISPplnK/L3alx3jN43BYYGDcCOgWfdxOw8izTBBeDSyszmK1Lz26rUAooIUteo1/Qzux9fro0FsDWF+rKpjfrWNA0ndk= 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 1695030409355286.38897907912724; Mon, 18 Sep 2023 02:46:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qiAos-0005oi-Ey; Mon, 18 Sep 2023 05:45: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 1qiAop-0005nt-HM for qemu-devel@nongnu.org; Mon, 18 Sep 2023 05:45:28 -0400 Received: from mgamail.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 1qiAof-0000zF-H5 for qemu-devel@nongnu.org; Mon, 18 Sep 2023 05:45:27 -0400 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Sep 2023 02:45:16 -0700 Received: from vmmteam.bj.intel.com ([10.240.193.84]) by FMSMGA003.fm.intel.com with ESMTP; 18 Sep 2023 02:45:13 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695030317; x=1726566317; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sYenLO8qBFQaQjUPMfoV6cxi4vZBlBpvvEUXyjxOwYk=; b=HzfkbFFJtNRozrDD0jEU5hOjs+yJ41FTKBIJDUq/c/A47kLIzhNKPfQq bm+lMW70Qh4Rr9QPTJotVaE01jketAHFCA5zFY9xm4KFAdWssdFGo+cP2 P5+Ci4fM2uezY+bJ9t904DLx9m8IyvAPIU153pl+UPWdJ1Jd/CfeEYb3a VcDaCSAfh3Lpdo78rMuFECQPS8/5M8xItsH57J9JlYaWTAxLbHlc+ZqBk 3v/fleea2ue7Xpq+eGI2Bq3LUSLUMZGMv92HhICFELjpE5EMdVTLAJSN4 QABFRQPf2WeS8Un3rFW2TW9EHa5p+yUTuH8lf20tCF6jBm3qjNVsubBBQ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10836"; a="446072275" X-IronPort-AV: E=Sophos;i="6.02,156,1688454000"; d="scan'208";a="446072275" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10836"; a="835955952" X-IronPort-AV: E=Sophos;i="6.02,156,1688454000"; d="scan'208";a="835955952" From: Jing Liu To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, pbonzini@redhat.com, kevin.tian@intel.com, reinette.chatre@intel.com, jing2.liu@intel.com, jing2.liu@linux.intel.com Subject: [PATCH v2 2/4] vfio/pci: enable vector on dynamic MSI-X allocation Date: Mon, 18 Sep 2023 05:45:05 -0400 Message-Id: <20230918094507.409050-3-jing2.liu@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230918094507.409050-1-jing2.liu@intel.com> References: <20230918094507.409050-1-jing2.liu@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=jing2.liu@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: 1695030411071100005 Content-Type: text/plain; charset="utf-8" The vector_use callback is used to enable vector that is unmasked in guest. The kernel used to only support static MSI-X allocation. When allocating a new interrupt using "static MSI-X allocation" kernels, QEMU first disables all previously allocated vectors and then re-allocates all including the new one. The nr_vectors of VFIOPCIDevice indicates that all vectors from 0 to nr_vectors are allocated (and may be enabled), which is used to to loop all the possibly used vectors When, e.g., disabling MSI-X interrupts. Extend the vector_use function to support dynamic MSI-X allocation when host supports the capability. QEMU therefore can individually allocate and enable a new interrupt without affecting others or causing interrupts lost during runtime. Utilize nr_vectors to calculate the upper bound of enabled vectors in dynamic MSI-X allocation mode since looping all msix_entries_nr is not efficient and unnecessary. Signed-off-by: Jing Liu Tested-by: Reinette Chatre Reviewed-by: Alex Williamson --- Changes since v1: - Revise Qemu to QEMU. Changes since RFC v1: - Test vdev->msix->noresize to identify the allocation mode. (Alex) - Move defer_kvm_irq_routing test out and update nr_vectors in a common place before vfio_enable_vectors(). (Alex) - Revise the comments. (Alex) --- hw/vfio/pci.c | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 60654ca28ab8..84987e46fd7a 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -470,6 +470,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, uns= igned int nr, VFIOPCIDevice *vdev =3D VFIO_PCI(pdev); VFIOMSIVector *vector; int ret; + int old_nr_vecs =3D vdev->nr_vectors; =20 trace_vfio_msix_vector_do_use(vdev->vbasedev.name, nr); =20 @@ -512,33 +513,42 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, u= nsigned int nr, } =20 /* - * We don't want to have the host allocate all possible MSI vectors - * for a device if they're not in use, so we shutdown and incrementally - * increase them as needed. + * When dynamic allocation is not supported, we don't want to have the + * host allocate all possible MSI vectors for a device if they're not + * in use, so we shutdown and incrementally increase them as needed. + * nr_vectors represents the total number of vectors allocated. + * + * When dynamic allocation is supported, let the host only allocate + * and enable a vector when it is in use in guest. nr_vectors represen= ts + * the upper bound of vectors being enabled (but not all of the ranges + * is allocated or enabled). */ if (vdev->nr_vectors < nr + 1) { vdev->nr_vectors =3D nr + 1; - if (!vdev->defer_kvm_irq_routing) { + } + + if (!vdev->defer_kvm_irq_routing) { + if (vdev->msix->noresize && (old_nr_vecs < nr + 1)) { vfio_disable_irqindex(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX= ); ret =3D vfio_enable_vectors(vdev, true); if (ret) { error_report("vfio: failed to enable vectors, %d", ret); } - } - } else { - Error *err =3D NULL; - int32_t fd; - - if (vector->virq >=3D 0) { - fd =3D event_notifier_get_fd(&vector->kvm_interrupt); } else { - fd =3D event_notifier_get_fd(&vector->interrupt); - } + Error *err =3D NULL; + int32_t fd; =20 - if (vfio_set_irq_signaling(&vdev->vbasedev, - VFIO_PCI_MSIX_IRQ_INDEX, nr, - VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err= )) { - error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); + if (vector->virq >=3D 0) { + fd =3D event_notifier_get_fd(&vector->kvm_interrupt); + } else { + fd =3D event_notifier_get_fd(&vector->interrupt); + } + + if (vfio_set_irq_signaling(&vdev->vbasedev, + VFIO_PCI_MSIX_IRQ_INDEX, nr, + VFIO_IRQ_SET_ACTION_TRIGGER, fd, &e= rr)) { + error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.nam= e); + } } } =20 --=20 2.27.0 From nobody Fri May 17 10:34:16 2024 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=1695030407; cv=none; d=zohomail.com; s=zohoarc; b=aeh/YD51Y8wm4vH12dpwJHypYe5t1UXhFSeAcIR+7w60vljkWuIaAqWtijH7h9qOdDgJTbMjdvtOS1kqmD02IsP6Y9pfAe3CENA2yLlr1YhzmodwbEV5u6ofCpW+8sOGVH6BT3QBbfFX//l8L4kK7Pm+3enSw8o+rNm/BS5VQz4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695030407; h=Content-Type: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=V0Eyfj3fegQaLGLgu9hFJhgW/oAuM9nJDeHjMbSpYQQ=; b=U41w+sMONJ33mG6Kl/w/O73z2fmjtVL0ekxcU8bhXV3eL+qmVgL1n/CYNX7TagFHFGEPlxMGQZz46qpYLRVbDgbZkkL9nED50ROAhHyEA4EWBjjEJGX9IMizE9TJq0myB0ju6IJ+jWSoxsTgv59Fq7ZrQ45JhTCzOfrt8g1KfxE= 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 1695030407597228.73825898456573; Mon, 18 Sep 2023 02:46:47 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qiAot-0005os-JR; Mon, 18 Sep 2023 05:45:31 -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 1qiAor-0005oH-AM for qemu-devel@nongnu.org; Mon, 18 Sep 2023 05:45:29 -0400 Received: from mgamail.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 1qiAop-0000xj-Gj for qemu-devel@nongnu.org; Mon, 18 Sep 2023 05:45:29 -0400 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Sep 2023 02:45:18 -0700 Received: from vmmteam.bj.intel.com ([10.240.193.84]) by FMSMGA003.fm.intel.com with ESMTP; 18 Sep 2023 02:45:16 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695030327; x=1726566327; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=K9qjTjP69wy9/oxYf5DYvTpAECxOt0osGbsO6aI872Y=; b=ITf4tbG5X115TDrDbWrvBCfX5X8OPSfpms3DQ1NaPd0QADOBYkUay8bJ 5zYVAuNT4yAIM8Tv/pH39nTD9CGPAB6a+wjd12oAx6IVqMtiHkRv1ngH6 90uvnwe0z6ZBobqCmTh5NnrEPvL1DLOAEkHKGc+HjpkEwxjd+QrAoUJO5 IWySfSLdfpJAC3SIW8Wv5AESNkaxvg97K1QfAlbZe8EedBkYe2E3zUJMP nd1CmRqtOPzaI6Z+kyys92eOJya8+eETrytDXwxO1EgxRLxDFlnuwueNG B8VKgtP7u9HvJPSpSJW9ar7BmqjMal7Wn2fs9fgZ0gFysZ1x1NFNelZCz Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10836"; a="446072279" X-IronPort-AV: E=Sophos;i="6.02,156,1688454000"; d="scan'208";a="446072279" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10836"; a="835955956" X-IronPort-AV: E=Sophos;i="6.02,156,1688454000"; d="scan'208";a="835955956" From: Jing Liu To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, pbonzini@redhat.com, kevin.tian@intel.com, reinette.chatre@intel.com, jing2.liu@intel.com, jing2.liu@linux.intel.com Subject: [PATCH v2 3/4] vfio/pci: use an invalid fd to enable MSI-X Date: Mon, 18 Sep 2023 05:45:06 -0400 Message-Id: <20230918094507.409050-4-jing2.liu@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230918094507.409050-1-jing2.liu@intel.com> References: <20230918094507.409050-1-jing2.liu@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=134.134.136.100; envelope-from=jing2.liu@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: 1695030409137100003 Guests typically enable MSI-X with all of the vectors masked in the MSI-X vector table. To match the guest state of device, QEMU enables MSI-X by enabling vector 0 with userspace triggering and immediately release. However the release function actually does not release it due to already using userspace mode. It is no need to enable triggering on host and rely on the mask bit to avoid spurious interrupts. Use an invalid fd (i.e. fd =3D -1) is enough to get MSI-X enabled. After dynamic MSI-X allocation is supported, the interrupt restoring also need use such way to enable MSI-X, therefore, create a function for that. Suggested-by: Alex Williamson Signed-off-by: Jing Liu Reviewed-by: Alex Williamson Reviewed-by: C=C3=A9dric Le Goater --- Changes since v1: - Revise Qemu to QEMU. (C=C3=A9dric) - Use g_autofree to automatically release. (C=C3=A9dric) - Just return 'ret' and let the caller of vfio_enable_msix_no_vec() report the error. (C=C3=A9dric) Changes since RFC v1: - A new patch. Use an invalid fd to get MSI-X enabled instead of using userspace triggering. (Alex) --- hw/vfio/pci.c | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 84987e46fd7a..0117f230e934 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -369,6 +369,33 @@ static void vfio_msi_interrupt(void *opaque) notify(&vdev->pdev, nr); } =20 +/* + * Get MSI-X enabled, but no vector enabled, by setting vector 0 with an i= nvalid + * fd to kernel. + */ +static int vfio_enable_msix_no_vec(VFIOPCIDevice *vdev) +{ + g_autofree struct vfio_irq_set *irq_set =3D NULL; + int ret =3D 0, argsz; + int32_t *fd; + + argsz =3D sizeof(*irq_set) + sizeof(*fd); + + irq_set =3D g_malloc0(argsz); + irq_set->argsz =3D argsz; + irq_set->flags =3D VFIO_IRQ_SET_DATA_EVENTFD | + VFIO_IRQ_SET_ACTION_TRIGGER; + irq_set->index =3D VFIO_PCI_MSIX_IRQ_INDEX; + irq_set->start =3D 0; + irq_set->count =3D 1; + fd =3D (int32_t *)&irq_set->data; + *fd =3D -1; + + ret =3D ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); + + return ret; +} + static int vfio_enable_vectors(VFIOPCIDevice *vdev, bool msix) { struct vfio_irq_set *irq_set; @@ -618,6 +645,8 @@ static void vfio_commit_kvm_msi_virq_batch(VFIOPCIDevic= e *vdev) =20 static void vfio_msix_enable(VFIOPCIDevice *vdev) { + int ret; + vfio_disable_interrupts(vdev); =20 vdev->msi_vectors =3D g_new0(VFIOMSIVector, vdev->msix->entries); @@ -640,8 +669,6 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev) vfio_commit_kvm_msi_virq_batch(vdev); =20 if (vdev->nr_vectors) { - int ret; - ret =3D vfio_enable_vectors(vdev, true); if (ret) { error_report("vfio: failed to enable vectors, %d", ret); @@ -655,13 +682,14 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev) * MSI-X capability, but leaves the vector table masked. We there= fore * can't rely on a vector_use callback (from request_irq() in the = guest) * to switch the physical device into MSI-X mode because that may = come a - * long time after pci_enable_msix(). This code enables vector 0 = with - * triggering to userspace, then immediately release the vector, l= eaving - * the physical device with no vectors enabled, but MSI-X enabled,= just - * like the guest view. + * long time after pci_enable_msix(). This code sets vector 0 wit= h an + * invalid fd to make the physical device MSI-X enabled, but with = no + * vectors enabled, just like the guest view. */ - vfio_msix_vector_do_use(&vdev->pdev, 0, NULL, NULL); - vfio_msix_vector_release(&vdev->pdev, 0); + ret =3D vfio_enable_msix_no_vec(vdev); + if (ret) { + error_report("vfio: failed to enable MSI-X, %d", ret); + } } =20 trace_vfio_msix_enable(vdev->vbasedev.name); --=20 2.27.0 From nobody Fri May 17 10:34:16 2024 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=1695030383; cv=none; d=zohomail.com; s=zohoarc; b=Zebnc8XI/76QtufFtuTUhmMjYdjVAK7NzfrgHhjeVS/9pKIqVsaaV6OlvKBLigE+hHDuU1ESLL08p3BbRr60IQ2nG2eZ0CLlH+kXaEBK/TcBp1GlqhenPJfZkwLZJRfFBscOEa7eQKOd/BEoeVdcONGVkUeFynGZfesidRE9m4o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695030383; 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=GaA/M7P5Qe9ZK9zsjmSsZ5ZKGULYB42DUUlLcKeSipI=; b=PTFMAq7Zrsbg/Xd266V2ntJ+b6Yq77aKGwKmcWETcJRVQb2FKh7Y0KhcRkm3PIh4Gl/Rs3uKgECzxAjQ3hfrCZc+m23KARv5tTJGHxm2QWa0yjHuEDgcKcmz79dTQyTBk9FPnjVCh8W9SkkL+k1SFJ3TFAjdTCP6hnUEvVflztM= 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 169503038307333.7778860367913; Mon, 18 Sep 2023 02:46:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qiAou-0005p0-28; Mon, 18 Sep 2023 05:45:32 -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 1qiAos-0005oU-2P for qemu-devel@nongnu.org; Mon, 18 Sep 2023 05:45:30 -0400 Received: from mgamail.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 1qiAop-0000zF-US for qemu-devel@nongnu.org; Mon, 18 Sep 2023 05:45:29 -0400 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Sep 2023 02:45:20 -0700 Received: from vmmteam.bj.intel.com ([10.240.193.84]) by FMSMGA003.fm.intel.com with ESMTP; 18 Sep 2023 02:45:18 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695030327; x=1726566327; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KqrlI41bT36v2JNI8frGPTvfWv6nYWbfQq51fwpgcGI=; b=fIsb/RJplL8cSG9ePCTqpWGbjE4v/xvM15wwvA5U4uPjnJn0FS+YgmaH RYHrG6DwlNKgUtWo5VeHOQigONWK81vmG+exD03BPq44rMBbwTPASDi3L rZUXxdtzMan2WVJksYH+h1gvpAxx9IoO0kiwBKAFtiHod/ehEPoWTAt0P L3f6mNGCo4yD0mhr72ByhMJMd6nOrw4IpzFGTaqd+0vbgEvLhmU576kEv IKx+kli3RV8f8iirVsHz3+9S10iiomkOyjDIZqjcd9ckp0NgcpDDU/cPf H5XJItYHdATbr/Id1QJRZgFEblvRaINyiBSYa/2UZymj8OG3HOR5oKBF0 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10836"; a="446072281" X-IronPort-AV: E=Sophos;i="6.02,156,1688454000"; d="scan'208";a="446072281" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10836"; a="835955962" X-IronPort-AV: E=Sophos;i="6.02,156,1688454000"; d="scan'208";a="835955962" From: Jing Liu To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, pbonzini@redhat.com, kevin.tian@intel.com, reinette.chatre@intel.com, jing2.liu@intel.com, jing2.liu@linux.intel.com Subject: [PATCH v2 4/4] vfio/pci: enable MSI-X in interrupt restoring on dynamic allocation Date: Mon, 18 Sep 2023 05:45:07 -0400 Message-Id: <20230918094507.409050-5-jing2.liu@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230918094507.409050-1-jing2.liu@intel.com> References: <20230918094507.409050-1-jing2.liu@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=jing2.liu@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: 1695030384019100001 Content-Type: text/plain; charset="utf-8" During migration restoring, vfio_enable_vectors() is called to restore enabling MSI-X interrupts for assigned devices. It sets the range from 0 to nr_vectors to kernel to enable MSI-X and the vectors unmasked in guest. During the MSI-X enabling, all the vectors within the range are allocated according to the VFIO_DEVICE_SET_IRQS ioctl. When dynamic MSI-X allocation is supported, we only want the guest unmasked vectors being allocated and enabled. Use vector 0 with an invalid fd to get MSI-X enabled, after that, all the vectors can be allocated in need. Signed-off-by: Jing Liu Reviewed-by: Alex Williamson Reviewed-by: C=C3=A9dric Le Goater --- Changes since v1: - No change. Changes since RFC v1: - Revise the comments. (Alex) - Call the new helper function in previous patch to enable MSI-X. (Alex) --- hw/vfio/pci.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 0117f230e934..f5f891dc0792 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -402,6 +402,23 @@ static int vfio_enable_vectors(VFIOPCIDevice *vdev, bo= ol msix) int ret =3D 0, i, argsz; int32_t *fds; =20 + /* + * If dynamic MSI-X allocation is supported, the vectors to be allocat= ed + * and enabled can be scattered. Before kernel enabling MSI-X, setting + * nr_vectors causes all these vectors to be allocated on host. + * + * To keep allocation as needed, use vector 0 with an invalid fd to get + * MSI-X enabled first, then set vectors with a potentially sparse set= of + * eventfds to enable interrupts only when enabled in guest. + */ + if (msix && !vdev->msix->noresize) { + ret =3D vfio_enable_msix_no_vec(vdev); + + if (ret) { + return ret; + } + } + argsz =3D sizeof(*irq_set) + (vdev->nr_vectors * sizeof(*fds)); =20 irq_set =3D g_malloc0(argsz); --=20 2.27.0