thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
ed5979
From 9a234f849273d3480e4a88042cb1ea06a37a626b Mon Sep 17 00:00:00 2001
ed5979
From: Cindy Lu <lulu@redhat.com>
ed5979
Date: Thu, 22 Dec 2022 15:04:43 +0800
ed5979
Subject: [PATCH 02/31] virtio-pci: decouple notifier from interrupt process
ed5979
MIME-Version: 1.0
ed5979
Content-Type: text/plain; charset=UTF-8
ed5979
Content-Transfer-Encoding: 8bit
ed5979
ed5979
RH-Author: Cindy Lu <lulu@redhat.com>
ed5979
RH-MergeRequest: 132: vhost-vdpa: support config interrupt in vhost-vdpa
ed5979
RH-Bugzilla: 1905805
ed5979
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
ed5979
RH-Acked-by: Eugenio PĂ©rez <eperezma@redhat.com>
ed5979
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
ed5979
RH-Commit: [2/10] a20f4c9ff38b239531d12cbcc7deaa649c86abc3 (lulu6/qemu-kvm3)
ed5979
ed5979
https://bugzilla.redhat.com/show_bug.cgi?id=1905805
ed5979
To reuse the notifier process. We add the virtio_pci_get_notifier
ed5979
to get the notifier and vector. The INPUT for this function is IDX,
ed5979
The OUTPUT is the notifier and the vector
ed5979
ed5979
Signed-off-by: Cindy Lu <lulu@redhat.com>
ed5979
Message-Id: <20221222070451.936503-3-lulu@redhat.com>
ed5979
Acked-by: Jason Wang <jasowang@redhat.com>
ed5979
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
ed5979
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
ed5979
(cherry picked from commit 2e07f69d0c828e21515b63dc22884d548540b382)
ed5979
Signed-off-by: Cindy Lu <lulu@redhat.com>
ed5979
---
ed5979
 hw/virtio/virtio-pci.c | 88 +++++++++++++++++++++++++++---------------
ed5979
 1 file changed, 57 insertions(+), 31 deletions(-)
ed5979
ed5979
diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
ed5979
index a1c9dfa7bb..52c7692fff 100644
ed5979
--- a/hw/virtio/virtio-pci.c
ed5979
+++ b/hw/virtio/virtio-pci.c
ed5979
@@ -728,29 +728,41 @@ static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
ed5979
 }
ed5979
 
ed5979
 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
ed5979
-                                 unsigned int queue_no,
ed5979
+                                 EventNotifier *n,
ed5979
                                  unsigned int vector)
ed5979
 {
ed5979
     VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
ed5979
-    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
ed5979
-    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
ed5979
-    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
ed5979
     return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq);
ed5979
 }
ed5979
 
ed5979
 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
ed5979
-                                      unsigned int queue_no,
ed5979
+                                      EventNotifier *n ,
ed5979
                                       unsigned int vector)
ed5979
 {
ed5979
-    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
ed5979
-    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
ed5979
-    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
ed5979
     VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
ed5979
     int ret;
ed5979
 
ed5979
     ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
ed5979
     assert(ret == 0);
ed5979
 }
ed5979
+static int virtio_pci_get_notifier(VirtIOPCIProxy *proxy, int queue_no,
ed5979
+                                      EventNotifier **n, unsigned int *vector)
ed5979
+{
ed5979
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
ed5979
+    VirtQueue *vq;
ed5979
+
ed5979
+    if (queue_no == VIRTIO_CONFIG_IRQ_IDX) {
ed5979
+        return -1;
ed5979
+    } else {
ed5979
+        if (!virtio_queue_get_num(vdev, queue_no)) {
ed5979
+            return -1;
ed5979
+        }
ed5979
+        *vector = virtio_queue_vector(vdev, queue_no);
ed5979
+        vq = virtio_get_queue(vdev, queue_no);
ed5979
+        *n = virtio_queue_get_guest_notifier(vq);
ed5979
+    }
ed5979
+    return 0;
ed5979
+}
ed5979
 
ed5979
 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
ed5979
 {
ed5979
@@ -759,12 +771,15 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
ed5979
     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
ed5979
     unsigned int vector;
ed5979
     int ret, queue_no;
ed5979
-
ed5979
+    EventNotifier *n;
ed5979
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
ed5979
         if (!virtio_queue_get_num(vdev, queue_no)) {
ed5979
             break;
ed5979
         }
ed5979
-        vector = virtio_queue_vector(vdev, queue_no);
ed5979
+        ret = virtio_pci_get_notifier(proxy, queue_no, &n, &vector);
ed5979
+        if (ret < 0) {
ed5979
+            break;
ed5979
+        }
ed5979
         if (vector >= msix_nr_vectors_allocated(dev)) {
ed5979
             continue;
ed5979
         }
ed5979
@@ -776,7 +791,7 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
ed5979
          * Otherwise, delay until unmasked in the frontend.
ed5979
          */
ed5979
         if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
ed5979
-            ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
ed5979
+            ret = kvm_virtio_pci_irqfd_use(proxy, n, vector);
ed5979
             if (ret < 0) {
ed5979
                 kvm_virtio_pci_vq_vector_release(proxy, vector);
ed5979
                 goto undo;
ed5979
@@ -792,7 +807,11 @@ undo:
ed5979
             continue;
ed5979
         }
ed5979
         if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
ed5979
-            kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
ed5979
+            ret = virtio_pci_get_notifier(proxy, queue_no, &n, &vector);
ed5979
+            if (ret < 0) {
ed5979
+                break;
ed5979
+            }
ed5979
+            kvm_virtio_pci_irqfd_release(proxy, n, vector);
ed5979
         }
ed5979
         kvm_virtio_pci_vq_vector_release(proxy, vector);
ed5979
     }
ed5979
@@ -806,12 +825,16 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
ed5979
     unsigned int vector;
ed5979
     int queue_no;
ed5979
     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
ed5979
-
ed5979
+    EventNotifier *n;
ed5979
+    int ret ;
ed5979
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
ed5979
         if (!virtio_queue_get_num(vdev, queue_no)) {
ed5979
             break;
ed5979
         }
ed5979
-        vector = virtio_queue_vector(vdev, queue_no);
ed5979
+        ret = virtio_pci_get_notifier(proxy, queue_no, &n, &vector);
ed5979
+        if (ret < 0) {
ed5979
+            break;
ed5979
+        }
ed5979
         if (vector >= msix_nr_vectors_allocated(dev)) {
ed5979
             continue;
ed5979
         }
ed5979
@@ -819,21 +842,20 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
ed5979
          * Otherwise, it was cleaned when masked in the frontend.
ed5979
          */
ed5979
         if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
ed5979
-            kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
ed5979
+            kvm_virtio_pci_irqfd_release(proxy, n, vector);
ed5979
         }
ed5979
         kvm_virtio_pci_vq_vector_release(proxy, vector);
ed5979
     }
ed5979
 }
ed5979
 
ed5979
-static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
ed5979
+static int virtio_pci_one_vector_unmask(VirtIOPCIProxy *proxy,
ed5979
                                        unsigned int queue_no,
ed5979
                                        unsigned int vector,
ed5979
-                                       MSIMessage msg)
ed5979
+                                       MSIMessage msg,
ed5979
+                                       EventNotifier *n)
ed5979
 {
ed5979
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
ed5979
     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
ed5979
-    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
ed5979
-    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
ed5979
     VirtIOIRQFD *irqfd;
ed5979
     int ret = 0;
ed5979
 
ed5979
@@ -860,14 +882,15 @@ static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
ed5979
             event_notifier_set(n);
ed5979
         }
ed5979
     } else {
ed5979
-        ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
ed5979
+        ret = kvm_virtio_pci_irqfd_use(proxy, n, vector);
ed5979
     }
ed5979
     return ret;
ed5979
 }
ed5979
 
ed5979
-static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
ed5979
+static void virtio_pci_one_vector_mask(VirtIOPCIProxy *proxy,
ed5979
                                              unsigned int queue_no,
ed5979
-                                             unsigned int vector)
ed5979
+                                             unsigned int vector,
ed5979
+                                             EventNotifier *n)
ed5979
 {
ed5979
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
ed5979
     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
ed5979
@@ -878,7 +901,7 @@ static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
ed5979
     if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
ed5979
         k->guest_notifier_mask(vdev, queue_no, true);
ed5979
     } else {
ed5979
-        kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
ed5979
+        kvm_virtio_pci_irqfd_release(proxy, n, vector);
ed5979
     }
ed5979
 }
ed5979
 
ed5979
@@ -888,6 +911,7 @@ static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
ed5979
     VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
ed5979
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
ed5979
     VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
ed5979
+    EventNotifier *n;
ed5979
     int ret, index, unmasked = 0;
ed5979
 
ed5979
     while (vq) {
ed5979
@@ -896,7 +920,8 @@ static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
ed5979
             break;
ed5979
         }
ed5979
         if (index < proxy->nvqs_with_notifiers) {
ed5979
-            ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
ed5979
+            n = virtio_queue_get_guest_notifier(vq);
ed5979
+            ret = virtio_pci_one_vector_unmask(proxy, index, vector, msg, n);
ed5979
             if (ret < 0) {
ed5979
                 goto undo;
ed5979
             }
ed5979
@@ -912,7 +937,8 @@ undo:
ed5979
     while (vq && unmasked >= 0) {
ed5979
         index = virtio_get_queue_index(vq);
ed5979
         if (index < proxy->nvqs_with_notifiers) {
ed5979
-            virtio_pci_vq_vector_mask(proxy, index, vector);
ed5979
+            n = virtio_queue_get_guest_notifier(vq);
ed5979
+            virtio_pci_one_vector_mask(proxy, index, vector, n);
ed5979
             --unmasked;
ed5979
         }
ed5979
         vq = virtio_vector_next_queue(vq);
ed5979
@@ -925,15 +951,17 @@ static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
ed5979
     VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
ed5979
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
ed5979
     VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
ed5979
+    EventNotifier *n;
ed5979
     int index;
ed5979
 
ed5979
     while (vq) {
ed5979
         index = virtio_get_queue_index(vq);
ed5979
+        n = virtio_queue_get_guest_notifier(vq);
ed5979
         if (!virtio_queue_get_num(vdev, index)) {
ed5979
             break;
ed5979
         }
ed5979
         if (index < proxy->nvqs_with_notifiers) {
ed5979
-            virtio_pci_vq_vector_mask(proxy, index, vector);
ed5979
+            virtio_pci_one_vector_mask(proxy, index, vector, n);
ed5979
         }
ed5979
         vq = virtio_vector_next_queue(vq);
ed5979
     }
ed5979
@@ -949,19 +977,17 @@ static void virtio_pci_vector_poll(PCIDevice *dev,
ed5979
     int queue_no;
ed5979
     unsigned int vector;
ed5979
     EventNotifier *notifier;
ed5979
-    VirtQueue *vq;
ed5979
+    int ret;
ed5979
 
ed5979
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
ed5979
-        if (!virtio_queue_get_num(vdev, queue_no)) {
ed5979
+        ret = virtio_pci_get_notifier(proxy, queue_no, &notifier, &vector);
ed5979
+        if (ret < 0) {
ed5979
             break;
ed5979
         }
ed5979
-        vector = virtio_queue_vector(vdev, queue_no);
ed5979
         if (vector < vector_start || vector >= vector_end ||
ed5979
             !msix_is_masked(dev, vector)) {
ed5979
             continue;
ed5979
         }
ed5979
-        vq = virtio_get_queue(vdev, queue_no);
ed5979
-        notifier = virtio_queue_get_guest_notifier(vq);
ed5979
         if (k->guest_notifier_pending) {
ed5979
             if (k->guest_notifier_pending(vdev, queue_no)) {
ed5979
                 msix_set_pending(dev, vector);
ed5979
-- 
ed5979
2.31.1
ed5979