From nobody Fri Mar 29 12:28:57 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.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=fail(p=none dis=none) header.from=huawei.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1634172715358934.0547947287131; Wed, 13 Oct 2021 17:51:55 -0700 (PDT) Received: from localhost ([::1]:51402 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1maoyQ-0001Xt-44 for importer@patchew.org; Wed, 13 Oct 2021 20:51:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50116) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maow2-000752-S0 for qemu-devel@nongnu.org; Wed, 13 Oct 2021 20:49:26 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3484) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maovz-0003FS-0z for qemu-devel@nongnu.org; Wed, 13 Oct 2021 20:49:26 -0400 Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.54]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4HV9jP6WyYzWlZ2; Thu, 14 Oct 2021 08:47:37 +0800 (CST) Received: from dggpeml100016.china.huawei.com (7.185.36.216) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Thu, 14 Oct 2021 08:49:16 +0800 Received: from DESKTOP-27KDQMV.china.huawei.com (10.174.148.223) by dggpeml100016.china.huawei.com (7.185.36.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Thu, 14 Oct 2021 08:49:15 +0800 From: "Longpeng(Mike)" To: , Subject: [PATCH v4 1/6] vfio: simplify the conditional statements in vfio_msi_enable Date: Thu, 14 Oct 2021 08:48:47 +0800 Message-ID: <20211014004852.1293-2-longpeng2@huawei.com> X-Mailer: git-send-email 2.25.0.windows.1 In-Reply-To: <20211014004852.1293-1-longpeng2@huawei.com> References: <20211014004852.1293-1-longpeng2@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.148.223] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml100016.china.huawei.com (7.185.36.216) X-CFilter-Loop: Reflected 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=45.249.212.187; envelope-from=longpeng2@huawei.com; helo=szxga01-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H2=-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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Longpeng\(Mike\)" , arei.gonglei@huawei.com, qemu-devel@nongnu.org, kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1634172716486100005 Content-Type: text/plain; charset="utf-8" It's unnecessary to test against the specific return value of VFIO_DEVICE_SET_IRQS, since any positive return is an error indicating the number of vectors we should retry with. Signed-off-by: Longpeng(Mike) --- hw/vfio/pci.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 4feaa1c..c1fba40 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -650,7 +650,7 @@ retry: if (ret) { if (ret < 0) { error_report("vfio: Error: Failed to setup MSI fds: %m"); - } else if (ret !=3D vdev->nr_vectors) { + } else { error_report("vfio: Error: Failed to enable %d " "MSI vectors, retry with %d", vdev->nr_vectors, r= et); } @@ -668,7 +668,7 @@ retry: g_free(vdev->msi_vectors); vdev->msi_vectors =3D NULL; =20 - if (ret > 0 && ret !=3D vdev->nr_vectors) { + if (ret > 0) { vdev->nr_vectors =3D ret; goto retry; } --=20 1.8.3.1 From nobody Fri Mar 29 12:28:57 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.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=fail(p=none dis=none) header.from=huawei.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1634172712889993.7768870618715; Wed, 13 Oct 2021 17:51:52 -0700 (PDT) Received: from localhost ([::1]:51188 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1maoyN-0001PA-Li for importer@patchew.org; Wed, 13 Oct 2021 20:51:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50118) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maow3-00075D-1H for qemu-devel@nongnu.org; Wed, 13 Oct 2021 20:49:28 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:3153) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maovz-0003J4-AG for qemu-devel@nongnu.org; Wed, 13 Oct 2021 20:49:26 -0400 Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4HV9dn08pQz906s; Thu, 14 Oct 2021 08:44:29 +0800 (CST) Received: from dggpeml100016.china.huawei.com (7.185.36.216) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Thu, 14 Oct 2021 08:49:19 +0800 Received: from DESKTOP-27KDQMV.china.huawei.com (10.174.148.223) by dggpeml100016.china.huawei.com (7.185.36.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Thu, 14 Oct 2021 08:49:19 +0800 From: "Longpeng(Mike)" To: , Subject: [PATCH v4 2/6] vfio: move re-enabling INTX out of the common helper Date: Thu, 14 Oct 2021 08:48:48 +0800 Message-ID: <20211014004852.1293-3-longpeng2@huawei.com> X-Mailer: git-send-email 2.25.0.windows.1 In-Reply-To: <20211014004852.1293-1-longpeng2@huawei.com> References: <20211014004852.1293-1-longpeng2@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.148.223] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml100016.china.huawei.com (7.185.36.216) X-CFilter-Loop: Reflected 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=45.249.212.188; envelope-from=longpeng2@huawei.com; helo=szxga02-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H2=-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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Longpeng\(Mike\)" , arei.gonglei@huawei.com, qemu-devel@nongnu.org, kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1634172714577100001 Content-Type: text/plain; charset="utf-8" Move re-enabling INTX out, and the callers should decide to re-enable it or not. Signed-off-by: Longpeng(Mike) --- hw/vfio/pci.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index c1fba40..a4985c0 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -690,7 +690,6 @@ retry: =20 static void vfio_msi_disable_common(VFIOPCIDevice *vdev) { - Error *err =3D NULL; int i; =20 for (i =3D 0; i < vdev->nr_vectors; i++) { @@ -709,15 +708,11 @@ static void vfio_msi_disable_common(VFIOPCIDevice *vd= ev) vdev->msi_vectors =3D NULL; vdev->nr_vectors =3D 0; vdev->interrupt =3D VFIO_INT_NONE; - - vfio_intx_enable(vdev, &err); - if (err) { - error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); - } } =20 static void vfio_msix_disable(VFIOPCIDevice *vdev) { + Error *err =3D NULL; int i; =20 msix_unset_vector_notifiers(&vdev->pdev); @@ -738,6 +733,10 @@ static void vfio_msix_disable(VFIOPCIDevice *vdev) } =20 vfio_msi_disable_common(vdev); + vfio_intx_enable(vdev, &err); + if (err) { + error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); + } =20 memset(vdev->msix->pending, 0, BITS_TO_LONGS(vdev->msix->entries) * sizeof(unsigned long)); @@ -747,8 +746,14 @@ static void vfio_msix_disable(VFIOPCIDevice *vdev) =20 static void vfio_msi_disable(VFIOPCIDevice *vdev) { + Error *err =3D NULL; + vfio_disable_irqindex(&vdev->vbasedev, VFIO_PCI_MSI_IRQ_INDEX); vfio_msi_disable_common(vdev); + vfio_intx_enable(vdev, &err); + if (err) { + error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); + } =20 trace_vfio_msi_disable(vdev->vbasedev.name); } --=20 1.8.3.1 From nobody Fri Mar 29 12:28:57 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.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=fail(p=none dis=none) header.from=huawei.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1634173051947245.6952231407805; Wed, 13 Oct 2021 17:57:31 -0700 (PDT) Received: from localhost ([::1]:36684 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1map3q-0002Mn-V9 for importer@patchew.org; Wed, 13 Oct 2021 20:57:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50160) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maow5-00076I-GG for qemu-devel@nongnu.org; Wed, 13 Oct 2021 20:49:30 -0400 Received: from szxga08-in.huawei.com ([45.249.212.255]:2880) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maow0-0003Jq-Mp for qemu-devel@nongnu.org; Wed, 13 Oct 2021 20:49:29 -0400 Received: from dggemv704-chm.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4HV9jW14Ryz1DHZC; Thu, 14 Oct 2021 08:47:43 +0800 (CST) Received: from dggpeml100016.china.huawei.com (7.185.36.216) by dggemv704-chm.china.huawei.com (10.3.19.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Thu, 14 Oct 2021 08:49:21 +0800 Received: from DESKTOP-27KDQMV.china.huawei.com (10.174.148.223) by dggpeml100016.china.huawei.com (7.185.36.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Thu, 14 Oct 2021 08:49:20 +0800 From: "Longpeng(Mike)" To: , Subject: [PATCH v4 3/6] vfio: simplify the failure path in vfio_msi_enable Date: Thu, 14 Oct 2021 08:48:49 +0800 Message-ID: <20211014004852.1293-4-longpeng2@huawei.com> X-Mailer: git-send-email 2.25.0.windows.1 In-Reply-To: <20211014004852.1293-1-longpeng2@huawei.com> References: <20211014004852.1293-1-longpeng2@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.148.223] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml100016.china.huawei.com (7.185.36.216) X-CFilter-Loop: Reflected 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=45.249.212.255; envelope-from=longpeng2@huawei.com; helo=szxga08-in.huawei.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Longpeng\(Mike\)" , arei.gonglei@huawei.com, qemu-devel@nongnu.org, kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1634173052747100001 Content-Type: text/plain; charset="utf-8" Use vfio_msi_disable_common to simplify the error handling in vfio_msi_enable. Signed-off-by: Longpeng(Mike) --- hw/vfio/pci.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index a4985c0..aeeb6cd 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -47,6 +47,7 @@ =20 static void vfio_disable_interrupts(VFIOPCIDevice *vdev); static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled); +static void vfio_msi_disable_common(VFIOPCIDevice *vdev); =20 /* * Disabling BAR mmaping can be slow, but toggling it around INTx can @@ -655,24 +656,12 @@ retry: "MSI vectors, retry with %d", vdev->nr_vectors, r= et); } =20 - for (i =3D 0; i < vdev->nr_vectors; i++) { - VFIOMSIVector *vector =3D &vdev->msi_vectors[i]; - if (vector->virq >=3D 0) { - vfio_remove_kvm_msi_virq(vector); - } - qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt), - NULL, NULL, NULL); - event_notifier_cleanup(&vector->interrupt); - } - - g_free(vdev->msi_vectors); - vdev->msi_vectors =3D NULL; + vfio_msi_disable_common(vdev); =20 if (ret > 0) { vdev->nr_vectors =3D ret; goto retry; } - vdev->nr_vectors =3D 0; =20 /* * Failing to setup MSI doesn't really fall within any specificati= on. @@ -680,7 +669,6 @@ retry: * out to fall back to INTx for this device. */ error_report("vfio: Error: Failed to enable MSI"); - vdev->interrupt =3D VFIO_INT_NONE; =20 return; } --=20 1.8.3.1 From nobody Fri Mar 29 12:28:57 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.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=fail(p=none dis=none) header.from=huawei.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1634172805993220.6834821851179; Wed, 13 Oct 2021 17:53:25 -0700 (PDT) Received: from localhost ([::1]:56046 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1maozs-0004cJ-UF for importer@patchew.org; Wed, 13 Oct 2021 20:53:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50162) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maow5-00076K-Qj for qemu-devel@nongnu.org; Wed, 13 Oct 2021 20:49:30 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:3154) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maow0-0003Kw-IW for qemu-devel@nongnu.org; Wed, 13 Oct 2021 20:49:29 -0400 Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4HV9dq3bZ4z908L; Thu, 14 Oct 2021 08:44:31 +0800 (CST) Received: from dggpeml100016.china.huawei.com (7.185.36.216) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Thu, 14 Oct 2021 08:49:22 +0800 Received: from DESKTOP-27KDQMV.china.huawei.com (10.174.148.223) by dggpeml100016.china.huawei.com (7.185.36.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Thu, 14 Oct 2021 08:49:21 +0800 From: "Longpeng(Mike)" To: , Subject: [PATCH v4 4/6] kvm: irqchip: extract kvm_irqchip_add_deferred_msi_route Date: Thu, 14 Oct 2021 08:48:50 +0800 Message-ID: <20211014004852.1293-5-longpeng2@huawei.com> X-Mailer: git-send-email 2.25.0.windows.1 In-Reply-To: <20211014004852.1293-1-longpeng2@huawei.com> References: <20211014004852.1293-1-longpeng2@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.148.223] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml100016.china.huawei.com (7.185.36.216) X-CFilter-Loop: Reflected 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=45.249.212.188; envelope-from=longpeng2@huawei.com; helo=szxga02-in.huawei.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H2=-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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Longpeng\(Mike\)" , arei.gonglei@huawei.com, qemu-devel@nongnu.org, kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1634172806306100001 Content-Type: text/plain; charset="utf-8" Extract a common helper that add MSI route for specific vector but does not commit immediately. Signed-off-by: Longpeng(Mike) --- accel/kvm/kvm-all.c | 15 +++++++++++++-- include/sysemu/kvm.h | 6 ++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index db8d83b..8627f7c 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -1953,7 +1953,7 @@ int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg) return kvm_set_irq(s, route->kroute.gsi, 1); } =20 -int kvm_irqchip_add_msi_route(KVMState *s, int vector, PCIDevice *dev) +int kvm_irqchip_add_deferred_msi_route(KVMState *s, int vector, PCIDevice = *dev) { struct kvm_irq_routing_entry kroute =3D {}; int virq; @@ -1996,7 +1996,18 @@ int kvm_irqchip_add_msi_route(KVMState *s, int vecto= r, PCIDevice *dev) =20 kvm_add_routing_entry(s, &kroute); kvm_arch_add_msi_route_post(&kroute, vector, dev); - kvm_irqchip_commit_routes(s); + + return virq; +} + +int kvm_irqchip_add_msi_route(KVMState *s, int vector, PCIDevice *dev) +{ + int virq; + + virq =3D kvm_irqchip_add_deferred_msi_route(s, vector, dev); + if (virq >=3D 0) { + kvm_irqchip_commit_routes(s); + } =20 return virq; } diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index a1ab1ee..8de0d9a 100644 --- a/include/sysemu/kvm.h +++ b/include/sysemu/kvm.h @@ -476,6 +476,12 @@ void kvm_init_cpu_signals(CPUState *cpu); * @return: virq (>=3D0) when success, errno (<0) when failed. */ int kvm_irqchip_add_msi_route(KVMState *s, int vector, PCIDevice *dev); +/** + * Add MSI route for specific vector but does not commit to KVM + * immediately + */ +int kvm_irqchip_add_deferred_msi_route(KVMState *s, int vector, + PCIDevice *dev); int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg, PCIDevice *dev); void kvm_irqchip_commit_routes(KVMState *s); --=20 1.8.3.1 From nobody Fri Mar 29 12:28:57 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.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=fail(p=none dis=none) header.from=huawei.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1634172714352604.8802155849548; Wed, 13 Oct 2021 17:51:54 -0700 (PDT) Received: from localhost ([::1]:51290 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1maoyP-0001Su-5z for importer@patchew.org; Wed, 13 Oct 2021 20:51:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50156) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maow5-000768-6B for qemu-devel@nongnu.org; Wed, 13 Oct 2021 20:49:30 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:2825) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maow2-0003NB-Is for qemu-devel@nongnu.org; Wed, 13 Oct 2021 20:49:28 -0400 Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4HV9fG5K1wzYd2n; Thu, 14 Oct 2021 08:44:54 +0800 (CST) Received: from dggpeml100016.china.huawei.com (7.185.36.216) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Thu, 14 Oct 2021 08:49:23 +0800 Received: from DESKTOP-27KDQMV.china.huawei.com (10.174.148.223) by dggpeml100016.china.huawei.com (7.185.36.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Thu, 14 Oct 2021 08:49:22 +0800 From: "Longpeng(Mike)" To: , Subject: [PATCH v4 5/6] Revert "vfio: Avoid disabling and enabling vectors repeatedly in VFIO migration" Date: Thu, 14 Oct 2021 08:48:51 +0800 Message-ID: <20211014004852.1293-6-longpeng2@huawei.com> X-Mailer: git-send-email 2.25.0.windows.1 In-Reply-To: <20211014004852.1293-1-longpeng2@huawei.com> References: <20211014004852.1293-1-longpeng2@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.148.223] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml100016.china.huawei.com (7.185.36.216) X-CFilter-Loop: Reflected 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=45.249.212.188; envelope-from=longpeng2@huawei.com; helo=szxga02-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H2=-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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Longpeng\(Mike\)" , arei.gonglei@huawei.com, qemu-devel@nongnu.org, kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1634172716395100003 Content-Type: text/plain; charset="utf-8" Commit ecebe53fe993 ("vfio: Avoid disabling and enabling vectors repeatedly in VFIO migration") avoids inefficiently disabling and enabling vectors repeatedly and lets the unmasked vectors be enabled one by one. But we want to batch multiple routes and defer the commit, and only commit once outside the loop of setting vector notifiers, so we cannot enable the vectors one by one in the loop now. Revert that commit and we will take another way in the next patch, it can not only avoid disabling/enabling vectors repeatedly, but also satisfy our requirement of defer to commit. Signed-off-by: Longpeng(Mike) --- hw/vfio/pci.c | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index aeeb6cd..0bd832b 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -569,9 +569,6 @@ static void vfio_msix_vector_release(PCIDevice *pdev, u= nsigned int nr) =20 static void vfio_msix_enable(VFIOPCIDevice *vdev) { - PCIDevice *pdev =3D &vdev->pdev; - unsigned int nr, max_vec =3D 0; - vfio_disable_interrupts(vdev); =20 vdev->msi_vectors =3D g_new0(VFIOMSIVector, vdev->msix->entries); @@ -590,22 +587,11 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev) * triggering to userspace, then immediately release the vector, leavi= ng * the physical device with no vectors enabled, but MSI-X enabled, just * like the guest view. - * If there are already unmasked vectors (in migration resume phase and - * some guest startups) which will be enabled soon, we can allocate all - * of them here to avoid inefficiently disabling and enabling vectors - * repeatedly later. */ - if (!pdev->msix_function_masked) { - for (nr =3D 0; nr < msix_nr_vectors_allocated(pdev); nr++) { - if (!msix_is_masked(pdev, nr)) { - max_vec =3D nr; - } - } - } - vfio_msix_vector_do_use(pdev, max_vec, NULL, NULL); - vfio_msix_vector_release(pdev, max_vec); + vfio_msix_vector_do_use(&vdev->pdev, 0, NULL, NULL); + vfio_msix_vector_release(&vdev->pdev, 0); =20 - if (msix_set_vector_notifiers(pdev, vfio_msix_vector_use, + if (msix_set_vector_notifiers(&vdev->pdev, vfio_msix_vector_use, vfio_msix_vector_release, NULL)) { error_report("vfio: msix_set_vector_notifiers failed"); } --=20 1.8.3.1 From nobody Fri Mar 29 12:28:57 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.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=fail(p=none dis=none) header.from=huawei.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1634172845804267.44263383606267; Wed, 13 Oct 2021 17:54:05 -0700 (PDT) Received: from localhost ([::1]:59014 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1map0V-0006aH-D0 for importer@patchew.org; Wed, 13 Oct 2021 20:54:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50158) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maow5-00076E-90 for qemu-devel@nongnu.org; Wed, 13 Oct 2021 20:49:30 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:2940) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maow2-0003TL-TA for qemu-devel@nongnu.org; Wed, 13 Oct 2021 20:49:29 -0400 Received: from dggemv704-chm.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4HV9fJ046jzbn4k; Thu, 14 Oct 2021 08:44:56 +0800 (CST) Received: from dggpeml100016.china.huawei.com (7.185.36.216) by dggemv704-chm.china.huawei.com (10.3.19.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Thu, 14 Oct 2021 08:49:24 +0800 Received: from DESKTOP-27KDQMV.china.huawei.com (10.174.148.223) by dggpeml100016.china.huawei.com (7.185.36.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Thu, 14 Oct 2021 08:49:24 +0800 From: "Longpeng(Mike)" To: , Subject: [PATCH v4 6/6] vfio: defer to commit kvm irq routing when enable msi/msix Date: Thu, 14 Oct 2021 08:48:52 +0800 Message-ID: <20211014004852.1293-7-longpeng2@huawei.com> X-Mailer: git-send-email 2.25.0.windows.1 In-Reply-To: <20211014004852.1293-1-longpeng2@huawei.com> References: <20211014004852.1293-1-longpeng2@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.148.223] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml100016.china.huawei.com (7.185.36.216) X-CFilter-Loop: Reflected 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=45.249.212.187; envelope-from=longpeng2@huawei.com; helo=szxga01-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H2=-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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Longpeng\(Mike\)" , arei.gonglei@huawei.com, qemu-devel@nongnu.org, kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1634172847793100001 Content-Type: text/plain; charset="utf-8" In migration resume phase, all unmasked msix vectors need to be setup when loading the VF state. However, the setup operation would take longer if the VM has more VFs and each VF has more unmasked vectors. The hot spot is kvm_irqchip_commit_routes, it'll scan and update all irqfds that are already assigned each invocation, so more vectors means need more time to process them. vfio_pci_load_config vfio_msix_enable msix_set_vector_notifiers for (vector =3D 0; vector < dev->msix_entries_nr; vector++) { vfio_msix_vector_do_use vfio_add_kvm_msi_virq kvm_irqchip_commit_routes <-- expensive } We can reduce the cost by only committing once outside the loop. The routes are cached in kvm_state, we commit them first and then bind irqfd for each vector. The test VM has 128 vcpus and 8 VF (each one has 65 vectors), we measure the cost of the vfio_msix_enable for each VF, and we can see 90+% costs can be reduce. VF Count of irqfds[*] Original With this patch 1st 65 8 2 2nd 130 15 2 3rd 195 22 2 4th 260 24 3 5th 325 36 2 6th 390 44 3 7th 455 51 3 8th 520 58 4 Total 258ms 21ms [*] Count of irqfds How many irqfds that already assigned and need to process in this round. The optimization can be applied to msi type too. Signed-off-by: Longpeng(Mike) --- hw/vfio/pci.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++--------= ---- hw/vfio/pci.h | 1 + 2 files changed, 105 insertions(+), 25 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 0bd832b..dca2d0c 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -413,8 +413,6 @@ static int vfio_enable_vectors(VFIOPCIDevice *vdev, boo= l msix) static void vfio_add_kvm_msi_virq(VFIOPCIDevice *vdev, VFIOMSIVector *vect= or, int vector_n, bool msix) { - int virq; - if ((msix && vdev->no_kvm_msix) || (!msix && vdev->no_kvm_msi)) { return; } @@ -423,20 +421,31 @@ static void vfio_add_kvm_msi_virq(VFIOPCIDevice *vdev= , VFIOMSIVector *vector, return; } =20 - virq =3D kvm_irqchip_add_msi_route(kvm_state, vector_n, &vdev->pdev); - if (virq < 0) { + vector->virq =3D kvm_irqchip_add_deferred_msi_route(kvm_state, vector_= n, + &vdev->pdev); + if (vector->virq < 0) { event_notifier_cleanup(&vector->kvm_interrupt); + vector->virq =3D -1; + return; + } + + if (vdev->defer_kvm_irq_routing) { + /* + * The vector->virq will be reset to -1 if we fail to add the + * corresponding irqfd in vfio_commit_kvm_msi_virq_batch(). + */ return; } =20 + kvm_irqchip_commit_routes(kvm_state); + if (kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, &vector->kvm_interru= pt, - NULL, virq) < 0) { - kvm_irqchip_release_virq(kvm_state, virq); + NULL, vector->virq) < 0) { + kvm_irqchip_release_virq(kvm_state, vector->virq); event_notifier_cleanup(&vector->kvm_interrupt); + vector->virq =3D -1; return; } - - vector->virq =3D virq; } =20 static void vfio_remove_kvm_msi_virq(VFIOMSIVector *vector) @@ -501,11 +510,13 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, u= nsigned int nr, * increase them as needed. */ if (vdev->nr_vectors < nr + 1) { - vfio_disable_irqindex(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX); vdev->nr_vectors =3D nr + 1; - ret =3D vfio_enable_vectors(vdev, true); - if (ret) { - error_report("vfio: failed to enable vectors, %d", ret); + if (!vdev->defer_kvm_irq_routing) { + 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; @@ -567,6 +578,46 @@ static void vfio_msix_vector_release(PCIDevice *pdev, = unsigned int nr) } } =20 +static void vfio_prepare_kvm_msi_virq_batch(VFIOPCIDevice *vdev) +{ + assert(!vdev->defer_kvm_irq_routing); + vdev->defer_kvm_irq_routing =3D true; +} + +static void vfio_commit_kvm_msi_virq_batch(VFIOPCIDevice *vdev) +{ + VFIOMSIVector *vector; + int i; + + if (!vdev->defer_kvm_irq_routing) { + return; + } + + vdev->defer_kvm_irq_routing =3D false; + + if (!vdev->nr_vectors) { + return; + } + + kvm_irqchip_commit_routes(kvm_state); + + for (i =3D 0; i < vdev->nr_vectors; i++) { + vector =3D &vdev->msi_vectors[i]; + + if (!vector->use || vector->virq < 0) { + continue; + } + + if (kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, + &vector->kvm_interrupt, + NULL, vector->virq) < 0) { + kvm_irqchip_release_virq(kvm_state, vector->virq); + event_notifier_cleanup(&vector->kvm_interrupt); + vector->virq =3D -1; + } + } +} + static void vfio_msix_enable(VFIOPCIDevice *vdev) { vfio_disable_interrupts(vdev); @@ -576,26 +627,45 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev) vdev->interrupt =3D VFIO_INT_MSIX; =20 /* - * Some communication channels between VF & PF or PF & fw rely on the - * physical state of the device and expect that enabling MSI-X from the - * guest enables the same on the host. When our guest is Linux, the - * guest driver call to pci_enable_msix() sets the enabling bit in the - * MSI-X capability, but leaves the vector table masked. We therefore - * can't rely on a vector_use callback (from request_irq() in the gues= t) - * 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, leavi= ng - * the physical device with no vectors enabled, but MSI-X enabled, just - * like the guest view. + * Setting vector notifiers triggers synchronous vector-use + * callbacks for each active vector. Deferring to commit the KVM + * routes once rather than per vector provides a substantial + * performance improvement. */ - vfio_msix_vector_do_use(&vdev->pdev, 0, NULL, NULL); - vfio_msix_vector_release(&vdev->pdev, 0); + vfio_prepare_kvm_msi_virq_batch(vdev); =20 if (msix_set_vector_notifiers(&vdev->pdev, vfio_msix_vector_use, vfio_msix_vector_release, NULL)) { error_report("vfio: msix_set_vector_notifiers failed"); } =20 + vfio_commit_kvm_msi_virq_batch(vdev); + + if (vdev->nr_vectors) { + int ret; + + ret =3D vfio_enable_vectors(vdev, true); + if (ret) { + error_report("vfio: failed to enable vectors, %d", ret); + } + } else { + /* + * Some communication channels between VF & PF or PF & fw rely on = the + * physical state of the device and expect that enabling MSI-X fro= m the + * guest enables the same on the host. When our guest is Linux, t= he + * guest driver call to pci_enable_msix() sets the enabling bit in= the + * 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. + */ + vfio_msix_vector_do_use(&vdev->pdev, 0, NULL, NULL); + vfio_msix_vector_release(&vdev->pdev, 0); + } + trace_vfio_msix_enable(vdev->vbasedev.name); } =20 @@ -605,6 +675,13 @@ static void vfio_msi_enable(VFIOPCIDevice *vdev) =20 vfio_disable_interrupts(vdev); =20 + /* + * Setting vector notifiers needs to enable route for each vector. + * Deferring to commit the KVM routes once rather than per vector + * provides a substantial performance improvement. + */ + vfio_prepare_kvm_msi_virq_batch(vdev); + vdev->nr_vectors =3D msi_nr_vectors_allocated(&vdev->pdev); retry: vdev->msi_vectors =3D g_new0(VFIOMSIVector, vdev->nr_vectors); @@ -630,6 +707,8 @@ retry: vfio_add_kvm_msi_virq(vdev, vector, i, false); } =20 + vfio_commit_kvm_msi_virq_batch(vdev); + /* Set interrupt type prior to possible interrupts */ vdev->interrupt =3D VFIO_INT_MSI; =20 diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h index 6477751..d3c5177 100644 --- a/hw/vfio/pci.h +++ b/hw/vfio/pci.h @@ -171,6 +171,7 @@ struct VFIOPCIDevice { bool no_kvm_ioeventfd; bool no_vfio_ioeventfd; bool enable_ramfb; + bool defer_kvm_irq_routing; VFIODisplay *dpy; Notifier irqchip_change_notifier; }; --=20 1.8.3.1