From nobody Fri May 17 10:13:27 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=1695694543; cv=none; d=zohomail.com; s=zohoarc; b=mj0t2zmIJeRlqjKIKoRQjAX4oja9pWzyk9NIKu4yEAui7h+yhWMQ0H1EWdsKfiKEPK49IM7KGRuX4S7TqX0UZVaWdMxxvjmWJW4c3E+w3BzVbTuIlk7n9GHnt61onIN+VbBHO5hXZ4CbrTisOBIYodNiyX/vrfHNDN7yh2yHA2Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695694543; 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=p9IEA3DYxwmA7Ld1uuxDIpTwvs5FvabNA+JHuxf9W2c=; b=NhTAxdIyC0SUfS9nvlPQaJFpJHagK35VCwiD4so8BcxGbr3r4wLZF4xXZB9/+JjlJiV4G4s5VTGJNwPOdFayUu9LYD/PiClfZywUoqOKEidfQnS3jQ0Ga/n5MlNGM6V8EfGhekHW5FdhSs42LMTASAzwlfGe2o+Io2vwRtyrGoQ= 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 1695694543643232.17159964255825; Mon, 25 Sep 2023 19:15:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qkxaj-0004eR-Cb; Mon, 25 Sep 2023 22:14:25 -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 1qkxah-0004dh-Rw for qemu-devel@nongnu.org; Mon, 25 Sep 2023 22:14:23 -0400 Received: from mgamail.intel.com ([192.55.52.88]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qkxag-0002e0-4M for qemu-devel@nongnu.org; Mon, 25 Sep 2023 22:14:23 -0400 Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Sep 2023 19:14:17 -0700 Received: from vmmteam.bj.intel.com ([10.240.193.84]) by orsmga003.jf.intel.com with ESMTP; 25 Sep 2023 19:14:14 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695694462; x=1727230462; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6lQYAHPuI/TlrcW63zBcoubCLGW7adiaATdLkZPOXQc=; b=HaJajX4rWZMiv1AJAN1/5p02I8G+f4RuiNy3totuo+kAsj0VOomjrF/5 3IWtt2EKDpXO2MBGsyfvQnki3jxHF3c39JfZE05fmXe2XapT+r1o085/h NQxKsw7Y5Kzb05oSI4dxemv9d5MaGBoJ+XIY308m3dF6e0gtVsX4/40mf 3UhDY2PyKThLrOpCrzx9jc07qnYw9uVVBYysypgkgDrN84gcY2kkEXdON UO1h/WOpnd6DShXNFZZp9+aAp09mKhQPGFv/4iEPqCZl+4StV7vcRMRLh o5b61uF0D5APzQK8wdR2ABHG6OvwKV/sh1cDQ2mk0kg3duaPxRowhBT/X g==; X-IronPort-AV: E=McAfee;i="6600,9927,10843"; a="412380121" X-IronPort-AV: E=Sophos;i="6.03,176,1694761200"; d="scan'208";a="412380121" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10843"; a="698283011" X-IronPort-AV: E=Sophos;i="6.03,176,1694761200"; d="scan'208";a="698283011" 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 v3 1/4] vfio/pci: detect the support of dynamic MSI-X allocation Date: Mon, 25 Sep 2023 22:14:04 -0400 Message-Id: <20230926021407.580305-2-jing2.liu@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230926021407.580305-1-jing2.liu@intel.com> References: <20230926021407.580305-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=192.55.52.88; 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, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=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: 1695694545015100003 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 v2: - Apply Alex's Reviewed-by. 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 3b2ca3c24ca2..a94eef50e41e 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 2d836093a83d..0d89eb761ece 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 e64ca4a01961..0ba3c5a0e26b 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:13:27 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=1695694516; cv=none; d=zohomail.com; s=zohoarc; b=ih3Kxg9OpYuzJNxm1W2lXWiN/rO3x6jGTG7e0YMuu42KeOKbZsskUhnzz6xqceS3XR7Irqm0w+ko8KZ1BBFk+7sOagDD8tY6LUi5gEVIEhg482l+22Wt7fOeHFrfrnxry3QNChd1z87LpO/Ux2SmjidjZbjWvURHaQuQUMLAndE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695694516; 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=a6Jren6q6ME/GIb4HDStIfKyKNeKtskszcT6CE2577Q=; b=jFXJ/YlayWB50VFp+slRVF3jzATOpJqzrbRbxxB6/WntHdthBseavXMQy9nGU3vtDAi26yDICdpHJlG0MzN2ebaDFeIrDn7HG/9V5SGbAbqisvSaDFwy/jqtvRK6kTitgcmYSgDi3uECSS5txDTfgRfoz4i7be7ApK4muGqFMZg= 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 16956945167160.5860208118415358; Mon, 25 Sep 2023 19:15:16 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qkxak-0004ev-Sy; Mon, 25 Sep 2023 22:14:26 -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 1qkxai-0004e3-A9 for qemu-devel@nongnu.org; Mon, 25 Sep 2023 22:14:24 -0400 Received: from mgamail.intel.com ([192.55.52.88]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qkxag-0002dW-LH for qemu-devel@nongnu.org; Mon, 25 Sep 2023 22:14:24 -0400 Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Sep 2023 19:14:19 -0700 Received: from vmmteam.bj.intel.com ([10.240.193.84]) by orsmga003.jf.intel.com with ESMTP; 25 Sep 2023 19:14:17 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695694462; x=1727230462; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=H6jSarW2lCotbNiDl5NvQoomn5YR3Bbd4nBXLPh3NX8=; b=KJfl7lYFpri4AlSU2EDt5E9s8rku81x1B5UpSgYJ8FGc0wX5m0vrTU0o 4bjTVyjMtBMqUlsb3QIT1AlgCxsdN4gLhiL3MY3oxYTMfGDMFzokm8N1X 8yzXPDixqjl6LAYILUKqVmH+LoEINu9nbzYhTx8EC5kfuCLEXu6WFfs2Z dRIT3Y0MXfwNjkE2gYWTMXTlqkDMEdhjHfTl6f+C8KqbAqqeRwwjiPR6e aT+y3UKM4/lS7oQEdQBM7gVKkYZZsYvH7ZuERYBz0WXPmbUCUjxXyupuA fcChHVaMnmDbWlyzd74e8U0DPFrik5Y0ugyVrTMrfsgSCxXDDOTVttlg4 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10843"; a="412380127" X-IronPort-AV: E=Sophos;i="6.03,176,1694761200"; d="scan'208";a="412380127" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10843"; a="698283018" X-IronPort-AV: E=Sophos;i="6.03,176,1694761200"; d="scan'208";a="698283018" 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 v3 2/4] vfio/pci: enable vector on dynamic MSI-X allocation Date: Mon, 25 Sep 2023 22:14:05 -0400 Message-Id: <20230926021407.580305-3-jing2.liu@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230926021407.580305-1-jing2.liu@intel.com> References: <20230926021407.580305-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=192.55.52.88; 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, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=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: 1695694517930100001 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 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 v2: - Use a bool type to test (vdev->nr_vectors < nr + 1). (Alex) - Revise the comments. (Alex) - Apply Alex's Reviewed-by. 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 | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index a94eef50e41e..27a65302ea69 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; + bool resizing =3D !!(vdev->nr_vectors < nr + 1); =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) { + if (resizing) { vdev->nr_vectors =3D nr + 1; - if (!vdev->defer_kvm_irq_routing) { + } + + if (!vdev->defer_kvm_irq_routing) { + if (vdev->msix->noresize && resizing) { 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:13:27 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=1695694516; cv=none; d=zohomail.com; s=zohoarc; b=PFY0nfsDFw9o5wiXJx6LpYORuMhA786yrma9QiCL3HWs5QX/4U7FtV9qvhGAVKrLuEEjmcNimwPGahRpAWRv/RtaKKMYzL2qM9r5hdb2v50Tqs5tnlkF7qQj8RdO2oEAYPiPd/Piv0D2vjg/clPZhP++Lw/q5FDDvumPzxFSUlQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695694516; 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=vNzyw2Ihd9q6E3uDXIG4JhLW3J4H7PG6WczRXQFPhKo=; b=cqlBE6pfiAHsR83JhkJlcgb1HEmcogeZL+xC7OZjymILf5qnqZgLSLXS3C1FOimhRlgSc6M59G42WQD1naxnrBnDgKRKQhEg+sFjSOvJzvTz8tKFTmR1iZI3T3SF3WXlAcFt36aOKcFhSeZUoeyNlmNAkIZiOHV5t1pOOUwGgx8= 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 1695694516681625.6071868755702; Mon, 25 Sep 2023 19:15:16 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qkxal-0004f0-FE; Mon, 25 Sep 2023 22:14:27 -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 1qkxaj-0004eX-U8 for qemu-devel@nongnu.org; Mon, 25 Sep 2023 22:14:25 -0400 Received: from mgamail.intel.com ([192.55.52.88]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qkxai-0002e0-7A for qemu-devel@nongnu.org; Mon, 25 Sep 2023 22:14:25 -0400 Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Sep 2023 19:14:22 -0700 Received: from vmmteam.bj.intel.com ([10.240.193.84]) by orsmga003.jf.intel.com with ESMTP; 25 Sep 2023 19:14:20 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695694464; x=1727230464; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=A8ZgWFvEi8WnDuk+p23nGXm3+oxdsWcW4BkN58DCS9k=; b=AtLq3XSOLG7PEpIzhCFCpzpHEzfTcoVQc1xTPqCwYQEt1ueJAEy/b+wW WkASyuT5LlUruYJGnltjzuw6b+u+YtTr20w5XuS5K9Fnq9diMb3iHIpNj zdS2hZcMBb+NLXDcpgpVTh2MJRZLlfQHdaKuYjqqUYfNq5iJYUrICIxyi zEz27GoDJV0xhoxIu6uAsszM3Z78teDStN7qW5PwAsbTkDz1n/i8wSTDH YMmf3xXgZegnJWDPeB34uFQR1qIcmAbHcsULSrdexHJj79W2HgFhVHzSr cU19tqTObbk14tmKoLVGyqUP2SJOg0BoUKnoCHAnNpGMZy1iPYXTAuVES A==; X-IronPort-AV: E=McAfee;i="6600,9927,10843"; a="412380132" X-IronPort-AV: E=Sophos;i="6.03,176,1694761200"; d="scan'208";a="412380132" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10843"; a="698283041" X-IronPort-AV: E=Sophos;i="6.03,176,1694761200"; d="scan'208";a="698283041" 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 v3 3/4] vfio/pci: use an invalid fd to enable MSI-X Date: Mon, 25 Sep 2023 22:14:06 -0400 Message-Id: <20230926021407.580305-4-jing2.liu@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230926021407.580305-1-jing2.liu@intel.com> References: <20230926021407.580305-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=192.55.52.88; 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, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=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: 1695694518852100011 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: C=C3=A9dric Le Goater Reviewed-by: Alex Williamson --- Changes since v2: - Apply C=C3=A9dric's Reviewed-by. - Apply Alex's Reviewed-by. 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 27a65302ea69..bf676a49ae77 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:13:27 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=1695694516; cv=none; d=zohomail.com; s=zohoarc; b=RBpwvbvuip+ZhcC319v/iJ+JoJXETRuJTQD4CLJgm6GOutP2go4Fq/Kcey+s4ofs7wOIAUPcLWBJpwSybWpO1/BDDpONDSjHko4Ibiey5MRj7c0YQ0WcTuNWFMpOlTG93BObskKBDZvU3KxhEskVr6VqGks2JUup2D0V5okyeDo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695694516; 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=qhMpIJjLVSKDVx1Hl95FsL5wVbGdyrFHK/t1iYzMJ5g=; b=jRe+ETVbjg7rwLbRnTYCf+3Q0WTckgBN2j1OKFSX+v6BqUYDPhVof8eBQT6W0YpmP1wzXE1yRgMT+/vIqByToLwOft0y4F51sFGfQT3z5HxCm04PjRh8wrSxElPYHwjlCbtZ4IYdXX4OS+cLkZ8+Vs72coXKH4grfrWdef5aiqw= 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 169569451675785.90181676209909; Mon, 25 Sep 2023 19:15:16 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qkxao-0004fL-0F; Mon, 25 Sep 2023 22:14: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 1qkxam-0004fD-Qj for qemu-devel@nongnu.org; Mon, 25 Sep 2023 22:14:28 -0400 Received: from mgamail.intel.com ([192.55.52.88]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qkxal-0002f4-9N for qemu-devel@nongnu.org; Mon, 25 Sep 2023 22:14:28 -0400 Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Sep 2023 19:14:25 -0700 Received: from vmmteam.bj.intel.com ([10.240.193.84]) by orsmga003.jf.intel.com with ESMTP; 25 Sep 2023 19:14:22 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695694467; x=1727230467; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=uKZCmRpqgWSTZeMlHWN0D2p8zG/tAmYsBuyTmvb8Xz4=; b=LATf+MCefbKfcmI37n9DIIHDiMZ4dDwS40HxSXANfsXVMSUTeOf8hztd 3pw4bgpMJli3ZMp4ubWm8+wEl1scrnhqFDXWw8QY+TMsU/lK8tEKFxbhC e3UfbxuirWfnFACvwE9s1LlN52SqOUxiTKfmf9rltwhF+/7GSyjyfKZXF JLRlYWQ+qFVCh/VcxM7mxZtfXp8ohJSYToW2wHhl30Lspd0qlpNr/E5RA o4rCblEwvFHGWBJSeiJKHKLKj39SACl/YEb8CZk/8rwCi56VyjDxkIrTW YsLOZmhtBq1q2VLIBB6ZySbgNgqS8J6SGBI/9Q1Lc09xq8ViUSJg1mFix g==; X-IronPort-AV: E=McAfee;i="6600,9927,10843"; a="412380143" X-IronPort-AV: E=Sophos;i="6.03,176,1694761200"; d="scan'208";a="412380143" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10843"; a="698283058" X-IronPort-AV: E=Sophos;i="6.03,176,1694761200"; d="scan'208";a="698283058" 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 v3 4/4] vfio/pci: enable MSI-X in interrupt restoring on dynamic allocation Date: Mon, 25 Sep 2023 22:14:07 -0400 Message-Id: <20230926021407.580305-5-jing2.liu@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230926021407.580305-1-jing2.liu@intel.com> References: <20230926021407.580305-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=192.55.52.88; 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, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=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: 1695694518037100003 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: C=C3=A9dric Le Goater Reviewed-by: Alex Williamson --- Changes since v2: - Apply C=C3=A9dric's Reviewed-by. - Apply Alex's Reviewed-by. 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 bf676a49ae77..8a082af39e77 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