From patchwork Thu Jul 27 07:24:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Liu X-Patchwork-Id: 1813789 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=mqeCJvk3; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RBXcQ4KCmz1ybX for ; Fri, 28 Jul 2023 00:06:14 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qP1H5-00061j-Um; Thu, 27 Jul 2023 09:43: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 1qOvVv-0005f9-7C for qemu-devel@nongnu.org; Thu, 27 Jul 2023 03:34:23 -0400 Received: from [192.55.52.136] (helo=mgamail.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qOvVt-0004N7-GO for qemu-devel@nongnu.org; Thu, 27 Jul 2023 03:34:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690443261; x=1721979261; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WABoFA/wWazeez7zhBvvVv7o7DbqHVzXK2WHrjvGk54=; b=mqeCJvk3fU0Rlz0yfHpMRYTsF8sDNpxB+dBu04r5c0GhenSYvYDjsXxw y4z2Kj9EizYWcF8VZ9bpMBfwL9QJd4mrukdJtBbcbn8XKn7MuEPiOE3hj CKP6Ug+dZVH6vTAMSddVBx/HlAn5kuIyHfZhq4ihqDADI872p9qzNVoXt Iqx1I8ph/gEQyTDnreCj/1afBL+vS6iCEejhHyvHbJb8DxkRxcaQtDo8t T2p99loWjbEycfKy33HUCpn3DJPTfJUpomJaa+tQn1mrHCCZ79myH6sRt fuqgJuesctc0XvKFM44A0pA/I10GAgXZ5yNqBgZtKTL1r9KFv4nN+9M8N g==; X-IronPort-AV: E=McAfee;i="6600,9927,10783"; a="347842868" X-IronPort-AV: E=Sophos;i="6.01,234,1684825200"; d="scan'208";a="347842868" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jul 2023 00:24:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10783"; a="756547369" X-IronPort-AV: E=Sophos;i="6.01,234,1684825200"; d="scan'208";a="756547369" Received: from vmmteam.bj.intel.com ([10.240.192.110]) by orsmga008.jf.intel.com with ESMTP; 27 Jul 2023 00:24:15 -0700 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 Subject: [PATCH RFC v1 1/3] vfio/pci: detect the support of dynamic MSI-X allocation Date: Thu, 27 Jul 2023 03:24:08 -0400 Message-Id: <20230727072410.135743-2-jing2.liu@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230727072410.135743-1-jing2.liu@intel.com> References: <20230727072410.135743-1-jing2.liu@intel.com> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 192.55.52.136 (failed) Received-SPF: pass client-ip=192.55.52.136; envelope-from=jing2.liu@intel.com; helo=mgamail.intel.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 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, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Thu, 27 Jul 2023 09:43:26 -0400 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Reinette Chatre 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 and store the flags from host for later use to determine if specific flags are set. Signed-off-by: Reinette Chatre Signed-off-by: Jing Liu --- hw/vfio/pci.c | 12 ++++++++++++ hw/vfio/pci.h | 1 + hw/vfio/trace-events | 2 ++ 3 files changed, 15 insertions(+) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index a205c6b1130f..0c4ac0873d40 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -1572,6 +1572,7 @@ static void vfio_msix_early_setup(VFIOPCIDevice *vdev, Error **errp) static int vfio_msix_setup(VFIOPCIDevice *vdev, int pos, Error **errp) { + struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info) }; int ret; Error *err = NULL; @@ -1624,6 +1625,17 @@ static int vfio_msix_setup(VFIOPCIDevice *vdev, int pos, Error **errp) memory_region_set_enabled(&vdev->pdev.msix_table_mmio, false); } + irq_info.index = VFIO_PCI_MSIX_IRQ_INDEX; + ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info); + if (ret) { + /* This can fail for an old kernel or legacy PCI dev */ + trace_vfio_msix_setup_get_irq_info_failure(strerror(errno)); + } else { + vdev->msix->irq_info_flags = irq_info.flags; + } + trace_vfio_msix_setup_irq_info_flags(vdev->vbasedev.name, + vdev->msix->irq_info_flags); + return 0; } diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h index a2771b9ff3cc..ad34ec56d0ae 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; + uint32_t irq_info_flags; } VFIOMSIXInfo; #define TYPE_VFIO_PCI "vfio-pci" diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index ee7509e68e4f..7d4a398f044d 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -28,6 +28,8 @@ vfio_pci_read_config(const char *name, int addr, int len, int val) " (%s, @0x%x, vfio_pci_write_config(const char *name, int addr, int val, int len) " (%s, @0x%x, 0x%x, len=0x%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_setup_get_irq_info_failure(const char *errstr) "VFIO_DEVICE_GET_IRQ_INFO failure: %s" +vfio_msix_setup_irq_info_flags(const char *name, uint32_t flags) " (%s) MSI-X irq info flags 0x%x" 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" From patchwork Thu Jul 27 07:24:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Liu X-Patchwork-Id: 1813788 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=TKCDCWI7; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RBXRb2sSwz1yYl for ; Thu, 27 Jul 2023 23:58:35 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qP1H6-00062E-EF; Thu, 27 Jul 2023 09:43:28 -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 1qOvVt-0005ew-1y for qemu-devel@nongnu.org; Thu, 27 Jul 2023 03:34:21 -0400 Received: from [192.55.52.136] (helo=mgamail.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qOvVr-0004N7-CH for qemu-devel@nongnu.org; Thu, 27 Jul 2023 03:34:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690443259; x=1721979259; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=loxFiijST0IQty0YeY0ldu2O+ixiW/kkhN5LYkEB+U0=; b=TKCDCWI7MMVs3rIuqS861z1NIQN6zxmd/1bKz1IFFegSSit63oLihP+H wX3utb+wFcjJ05hoXQu5O5BiRtvx9m/U9JHCMNNFDz1kSsB3SOlpH/Gwq LHQQfK87PNVSQX0nVm/bgGZG1LnEj5F3As1hYlYWoyX0KK3tUiH8ssYn+ Wi4yKpRmU61AiMbPqE44C6uxZ7Xv17/odl9F3u1S5LQ+EeleYYBHBfUAW gw91O/nnbxjSPMJ/ze9bRlpqOzb5xkA7c5Pxz18gFEmno5WakCoKaruVS zLH6U4HtSLRN4B8q0W4Wb+pnS+uJJDSvkbZJPtz8/WMb4h+G6MNgeEDV+ g==; X-IronPort-AV: E=McAfee;i="6600,9927,10783"; a="347842874" X-IronPort-AV: E=Sophos;i="6.01,234,1684825200"; d="scan'208";a="347842874" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jul 2023 00:24:20 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10783"; a="756547372" X-IronPort-AV: E=Sophos;i="6.01,234,1684825200"; d="scan'208";a="756547372" Received: from vmmteam.bj.intel.com ([10.240.192.110]) by orsmga008.jf.intel.com with ESMTP; 27 Jul 2023 00:24:17 -0700 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 Subject: [PATCH RFC v1 2/3] vfio/pci: enable vector on dynamic MSI-X allocation Date: Thu, 27 Jul 2023 03:24:09 -0400 Message-Id: <20230727072410.135743-3-jing2.liu@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230727072410.135743-1-jing2.liu@intel.com> References: <20230727072410.135743-1-jing2.liu@intel.com> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 192.55.52.136 (failed) Received-SPF: pass client-ip=192.55.52.136; envelope-from=jing2.liu@intel.com; helo=mgamail.intel.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 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, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Thu, 27 Jul 2023 09:43:26 -0400 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- hw/vfio/pci.c | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 0c4ac0873d40..8c485636445c 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -512,12 +512,20 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr, } /* - * 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. + * And nr_vectors stands for the number of vectors being allocated. + * + * When dynamic allocation is supported, let the host only allocate + * and enable a vector when it is in use in guest. nr_vectors stands + * for the upper bound of vectors being enabled (but not all of the + * ranges is allocated or enabled). */ - if (vdev->nr_vectors < nr + 1) { + if ((vdev->msix->irq_info_flags & VFIO_IRQ_INFO_NORESIZE) && + (vdev->nr_vectors < nr + 1)) { vdev->nr_vectors = nr + 1; + if (!vdev->defer_kvm_irq_routing) { vfio_disable_irqindex(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX); ret = vfio_enable_vectors(vdev, true); @@ -529,16 +537,22 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr, Error *err = NULL; int32_t fd; - if (vector->virq >= 0) { - fd = event_notifier_get_fd(&vector->kvm_interrupt); - } else { - fd = event_notifier_get_fd(&vector->interrupt); - } + if (!vdev->defer_kvm_irq_routing) { + if (vector->virq >= 0) { + fd = event_notifier_get_fd(&vector->kvm_interrupt); + } else { + fd = 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, &err)) { - error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); + 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); + } + } + /* Increase for dynamic allocation case. */ + if (vdev->nr_vectors < nr + 1) { + vdev->nr_vectors = nr + 1; } } From patchwork Thu Jul 27 07:24:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Liu X-Patchwork-Id: 1813786 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=E8V7UkQa; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RBXP060lbz1yYl for ; Thu, 27 Jul 2023 23:56:20 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qP1H6-00061h-6G; Thu, 27 Jul 2023 09:43:28 -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 1qOvVv-0005fI-R0 for qemu-devel@nongnu.org; Thu, 27 Jul 2023 03:34:23 -0400 Received: from [192.55.52.136] (helo=mgamail.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qOvVu-0004Nk-78 for qemu-devel@nongnu.org; Thu, 27 Jul 2023 03:34:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690443262; x=1721979262; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PDY6PXgBHsLC8cUf42XTMOY4sPIs93ocKgpDuj7Te6A=; b=E8V7UkQauWE6NxD3lEq7hN91mUUiqASGeR/S26ivYwvEFawx9NPxn/Op oIBHB5q6o7ScLT4HQWtaF7SaMC820GCPrlx5S7u/dXeOP9xoizNRRFwKu g2/VRiGisQfjZymScLh7/crZvjacZs5n+Vrw3a8f/PrSl5WKARZoOlOXJ k1Tg1r25OygI8Jc33BMWuiEEuSXR0Flq9d/9eOL10//JxOIod7KUOr8/W /nvxIBMXDzR80j+5gC4ew+dRaCO2nRWkDv6tUu6Mnp62Shx74mtfhobgt iNctoPtESSmhDt9FG3YKRP6WmVmuVa4j1cixkxvfUv5AHPQnShJyPH1KU Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10783"; a="347842880" X-IronPort-AV: E=Sophos;i="6.01,234,1684825200"; d="scan'208";a="347842880" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jul 2023 00:24:22 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10783"; a="756547376" X-IronPort-AV: E=Sophos;i="6.01,234,1684825200"; d="scan'208";a="756547376" Received: from vmmteam.bj.intel.com ([10.240.192.110]) by orsmga008.jf.intel.com with ESMTP; 27 Jul 2023 00:24:20 -0700 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 Subject: [PATCH RFC v1 3/3] vfio/pci: dynamic MSI-X allocation in interrupt restoring Date: Thu, 27 Jul 2023 03:24:10 -0400 Message-Id: <20230727072410.135743-4-jing2.liu@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230727072410.135743-1-jing2.liu@intel.com> References: <20230727072410.135743-1-jing2.liu@intel.com> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 192.55.52.136 (failed) Received-SPF: pass client-ip=192.55.52.136; envelope-from=jing2.liu@intel.com; helo=mgamail.intel.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 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, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Thu, 27 Jul 2023 09:43:26 -0400 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 ioctl(). When dynamic MSI-X allocation is supported, we only want the guest unmasked vectors being allocated and enabled. Therefore, Qemu can first set vector 0 to enable MSI-X and after that, all the vectors can be allocated in need. Signed-off-by: Jing Liu --- hw/vfio/pci.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 8c485636445c..43ffacd5b36a 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -375,6 +375,38 @@ static int vfio_enable_vectors(VFIOPCIDevice *vdev, bool msix) int ret = 0, i, argsz; int32_t *fds; + /* + * If dynamic MSI-X allocation is supported, the vectors to be allocated + * and enabled can be scattered. Before kernel enabling MSI-X, setting + * nr_vectors causes all these vectors being allocated on host. + * + * To keep allocation as needed, first setup vector 0 with an invalid + * fd to make MSI-X enabled, then enable vectors by setting all so that + * kernel allocates and enables interrupts only when enabled in guest. + */ + if (msix && !(vdev->msix->irq_info_flags & VFIO_IRQ_INFO_NORESIZE)) { + argsz = sizeof(*irq_set) + sizeof(*fds); + + irq_set = g_malloc0(argsz); + irq_set->argsz = argsz; + irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | + VFIO_IRQ_SET_ACTION_TRIGGER; + irq_set->index = msix ? VFIO_PCI_MSIX_IRQ_INDEX : + VFIO_PCI_MSI_IRQ_INDEX; + irq_set->start = 0; + irq_set->count = 1; + fds = (int32_t *)&irq_set->data; + fds[0] = -1; + + ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); + + g_free(irq_set); + + if (ret) { + return ret; + } + } + argsz = sizeof(*irq_set) + (vdev->nr_vectors * sizeof(*fds)); irq_set = g_malloc0(argsz);