qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Cédric Le Goater" <clg@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Alex Williamson" <alex.williamson@redhat.com>,
	"Jing Liu" <jing2.liu@intel.com>,
	"Reinette Chatre" <reinette.chatre@intel.com>,
	"Cédric Le Goater" <clg@redhat.com>
Subject: [PULL 04/21] vfio/pci: enable vector on dynamic MSI-X allocation
Date: Fri,  6 Oct 2023 08:19:48 +0200	[thread overview]
Message-ID: <20231006062005.1040296-5-clg@redhat.com> (raw)
In-Reply-To: <20231006062005.1040296-1-clg@redhat.com>

From: Jing Liu <jing2.liu@intel.com>

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 <jing2.liu@intel.com>
Tested-by: Reinette Chatre <reinette.chatre@intel.com>
Reviewed-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
 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 86c92c51a46e2d1e9856c3cbcd7c47548daab422..eb7627c5f9fcd67f346595ebe73011cc3205b58e 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -470,6 +470,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
     VFIOPCIDevice *vdev = VFIO_PCI(pdev);
     VFIOMSIVector *vector;
     int ret;
+    bool resizing = !!(vdev->nr_vectors < nr + 1);
 
     trace_vfio_msix_vector_do_use(vdev->vbasedev.name, nr);
 
@@ -512,33 +513,42 @@ 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.
+     * 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 represents
+     * 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 = 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 = vfio_enable_vectors(vdev, true);
             if (ret) {
                 error_report("vfio: failed to enable vectors, %d", ret);
             }
-        }
-    } else {
-        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);
-        }
+            Error *err = NULL;
+            int32_t fd;
 
-        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 >= 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);
+            }
         }
     }
 
-- 
2.41.0



  parent reply	other threads:[~2023-10-06  6:21 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-06  6:19 [PULL 00/21] vfio queue Cédric Le Goater
2023-10-06  6:19 ` [PULL 01/21] vfio/display: Fix missing update to set backing fields Cédric Le Goater
2023-10-06  6:19 ` [PULL 02/21] vfio/pci: rename vfio_put_device to vfio_pci_put_device Cédric Le Goater
2023-10-06  6:19 ` [PULL 03/21] vfio/pci: detect the support of dynamic MSI-X allocation Cédric Le Goater
2023-10-06  6:19 ` Cédric Le Goater [this message]
2023-10-06  6:19 ` [PULL 05/21] vfio/pci: use an invalid fd to enable MSI-X Cédric Le Goater
2023-10-06  6:19 ` [PULL 06/21] vfio/pci: enable MSI-X in interrupt restoring on dynamic allocation Cédric Le Goater
2023-10-06  6:19 ` [PULL 07/21] scripts/update-linux-headers: Add iommufd.h Cédric Le Goater
2023-10-06  6:19 ` [PULL 08/21] linux-headers: " Cédric Le Goater
2023-10-06  6:19 ` [PULL 09/21] vfio/common: Move IOMMU agnostic helpers to a separate file Cédric Le Goater
2023-10-06  6:19 ` [PULL 10/21] vfio/common: Propagate KVM_SET_DEVICE_ATTR error if any Cédric Le Goater
2023-10-06  6:19 ` [PULL 11/21] vfio/common: Introduce vfio_container_add|del_section_window() Cédric Le Goater
2023-10-06  6:19 ` [PULL 12/21] vfio/common: Extract out vfio_kvm_device_[add/del]_fd Cédric Le Goater
2023-10-06  6:19 ` [PULL 13/21] vfio/pci: Introduce vfio_[attach/detach]_device Cédric Le Goater
2023-10-06  6:19 ` [PULL 14/21] vfio/platform: Use vfio_[attach/detach]_device Cédric Le Goater
2023-10-06  6:19 ` [PULL 15/21] vfio/ap: " Cédric Le Goater
2023-10-06  6:20 ` [PULL 16/21] vfio/ccw: " Cédric Le Goater
2023-10-06  6:20 ` [PULL 17/21] vfio/common: Move VFIO reset handler registration to a group agnostic function Cédric Le Goater
2023-10-06  6:20 ` [PULL 18/21] vfio/common: Introduce a per container device list Cédric Le Goater
2023-10-06  6:20 ` [PULL 19/21] vfio/common: Store the parent container in VFIODevice Cédric Le Goater
2023-10-06  6:20 ` [PULL 20/21] vfio/common: Introduce a global VFIODevice list Cédric Le Goater
2023-10-06  6:20 ` [PULL 21/21] vfio/common: Move legacy VFIO backend code into separate container.c Cédric Le Goater
2023-10-06 10:33 ` [PULL 00/21] vfio queue Cédric Le Goater
2023-10-06 11:42   ` Eric Auger
2023-10-06 11:46     ` Eric Auger
2023-10-06 12:25       ` Eric Auger
2023-10-06 17:08         ` Cédric Le Goater
2023-10-06 17:28           ` Eric Auger
2023-10-07 10:14             ` Cédric Le Goater
2023-10-07 15:33               ` Michael Tokarev
2023-10-09  6:47   ` Cédric Le Goater

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231006062005.1040296-5-clg@redhat.com \
    --to=clg@redhat.com \
    --cc=alex.williamson@redhat.com \
    --cc=jing2.liu@intel.com \
    --cc=qemu-devel@nongnu.org \
    --cc=reinette.chatre@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).