Skip to contentSkip to conversation

vhost-vdpa: add support for configure interrupt

PATCH Superseded Cindy Lu 2 participants 26 messages Jun 2, 2021 – Jun 8, 2021 Networking

v7 of 21 Superseded Latest is v21 →

lore
Share & tools
lore

Cindy Lu

author
#0 lore raw

v7 cover vhost-vdpa: add support for configure interrupt

these patches are add the support for configure interrupt

These code are all tested in vp-vdpa (support configure interrupt) vdpa_sim (not support configure interrupt)

test in virtio-pci bus and virtio-mmio bus

Change in v2:
Add support for virtio-mmio bus
active the notifier while the backend support configure interrupt misc fixes form v1

Change in v3
fix the coding style problems

Change in v4
misc fixes form v3
merge the set_config_notifier to set_guest_notifier when vdpa start, check the feature by VIRTIO_NET_F_STATUS

Change in v5
misc fixes form v4
split the code for introduce configure interrupt type and callback function will init the configure interrupt in all virtio-pci and virtio-mmio bus, but will only active while using vhost-vdpa driver

Change in v6
misc fixes form v5
decouple virtqueue from interrupt setting and misc process
fix the bug in virtio_net_handle_rx
use -1 as the queue number to identify if the interrupt is configure interrupt

Change in v7
misc fixes form v6
decouple virtqueue from interrupt setting and misc process
decouple virtqueue from verctor use/release process
decouple virtqueue from set notifier fd handler process
move config_notifier and masked_config_notifier to VirtIODevice
fix the bug in virtio_net_handle_rx, add more information
add VIRTIO_CONFIG_IRQ_IDX as the queue number to identify if the interrupt is configure interrupt

Cindy Lu (10):
virtio: introduce macro IRTIO_CONFIG_IRQ_IDX virtio-pci:decouple virtqueue from interrupt setting process
virtio: decouple virtqueue from set notifier fd handler
vhost: add new call back function for config interrupt
vhost-vdpa: add support for config interrupt call back vhost:add support for configure interrupt
virtio-mmio: add support for configure interrupt
virtio-pci: decouple virtqueue from kvm_virtio_pci_vector_use
virtio-pci: add support for configure interrupt
virtio-net: add peer_deleted check in virtio_net_handle_rx

 hw/display/vhost-user-gpu.c       |   6 +
 hw/net/vhost_net.c                |   9 ++
 hw/net/virtio-net.c               |  20 ++-
 hw/s390x/virtio-ccw.c             |   6 +-
 hw/virtio/trace-events            |   2 +
 hw/virtio/vhost-user-fs.c         |   9 +-
 hw/virtio/vhost-vdpa.c            |   7 +
 hw/virtio/vhost-vsock-common.c    |   6 +
 hw/virtio/vhost.c                 |  68 +++++++++-
 hw/virtio/virtio-crypto.c         |   6 +
 hw/virtio/virtio-mmio.c           |  30 ++++-
 hw/virtio/virtio-pci.c            | 210 +++++++++++++++++++-----------
 hw/virtio/virtio.c                |  34 ++++-
 include/hw/virtio/vhost-backend.h |   3 +
 include/hw/virtio/vhost.h         |   2 +
 include/hw/virtio/virtio.h        |   7 +-
 include/net/vhost_net.h           |   3 +
 17 files changed, 335 insertions(+), 93 deletions(-)
Signature · 1 line
2.21.3
author
#1 lore raw

PATCH v7 1/10 virtio: introduce macro IRTIO_CONFIG_IRQ_IDX 聽

In order to support configure interrupt for vhost-vdpa introduce VIRTIO_CONFIG_IRQ_IDX -1as a queue index, then we can reuse the function guest_notifier_mask and guest_notifier_pending.
we add the check of queue index, if the driver
not support configure interrupt, the function will just return

---
 hw/display/vhost-user-gpu.c    |  6 ++++++
 hw/net/virtio-net.c            | 10 +++++++---
 hw/virtio/vhost-user-fs.c      |  9 +++++++--
 hw/virtio/vhost-vsock-common.c |  6 ++++++
 hw/virtio/virtio-crypto.c      |  6 ++++++
 include/hw/virtio/virtio.h     |  2 ++
 6 files changed, 34 insertions(+), 5 deletions(-)
hw/display/vhost-user-gpu.c +6 -0
index 6cdaa1c73b..c232d55986 100644
@@ -483,6 +483,9 @@ vhost_user_gpu_guest_notifier_pending(VirtIODevice *vdev, int idx)
 {
     VhostUserGPU *g = VHOST_USER_GPU(vdev);
 
+    if (idx == VIRTIO_CONFIG_IRQ_IDX) {
+        return false;
+    }
     return vhost_virtqueue_pending(&g->vhost->dev, idx);
 }
 
@@ -491,6 +494,9 @@ vhost_user_gpu_guest_notifier_mask(VirtIODevice *vdev, int idx, bool mask)
 {
     VhostUserGPU *g = VHOST_USER_GPU(vdev);
 
+    if (idx == VIRTIO_CONFIG_IRQ_IDX) {
+        return;
+    }
     vhost_virtqueue_mask(&g->vhost->dev, vdev, idx, mask);
 }
 
hw/net/virtio-net.c +7 -3
index 66b9ff4511..f50235b5d6 100644
@@ -3052,7 +3052,10 @@ static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
     VirtIONet *n = VIRTIO_NET(vdev);
     NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
     assert(n->vhost_started);
-    return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
+    if (idx != VIRTIO_CONFIG_IRQ_IDX) {
+        return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
+    }
+    return false;
 }
 
 static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
@@ -3061,8 +3064,9 @@ static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
     VirtIONet *n = VIRTIO_NET(vdev);
     NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
     assert(n->vhost_started);
-    vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
-                             vdev, idx, mask);
+    if (idx != VIRTIO_CONFIG_IRQ_IDX) {
+        vhost_net_virtqueue_mask(get_vhost_net(nc->peer), vdev, idx, mask);
+    }
 }
 
 static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
hw/virtio/vhost-user-fs.c +7 -2
index 6f7f91533d..7e8ca25f71 100644
@@ -156,11 +156,13 @@ static void vuf_handle_output(VirtIODevice *vdev, VirtQueue *vq)
      */
 }
 
-static void vuf_guest_notifier_mask(VirtIODevice *vdev, int idx,
-                                            bool mask)
+static void vuf_guest_notifier_mask(VirtIODevice *vdev, int idx, bool mask)
 {
     VHostUserFS *fs = VHOST_USER_FS(vdev);
 
+    if (idx == VIRTIO_CONFIG_IRQ_IDX) {
+        return;
+    }
     vhost_virtqueue_mask(&fs->vhost_dev, vdev, idx, mask);
 }
 
@@ -168,6 +170,9 @@ static bool vuf_guest_notifier_pending(VirtIODevice *vdev, int idx)
 {
     VHostUserFS *fs = VHOST_USER_FS(vdev);
 
+    if (idx == VIRTIO_CONFIG_IRQ_IDX) {
+        return false;
+    }
     return vhost_virtqueue_pending(&fs->vhost_dev, idx);
 }
 
hw/virtio/vhost-vsock-common.c +6 -0
index 4ad6e234ad..2112b44802 100644
@@ -101,6 +101,9 @@ static void vhost_vsock_common_guest_notifier_mask(VirtIODevice *vdev, int idx,
 {
     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
 
+    if (idx == VIRTIO_CONFIG_IRQ_IDX) {
+        return;
+    }
     vhost_virtqueue_mask(&vvc->vhost_dev, vdev, idx, mask);
 }
 
@@ -109,6 +112,9 @@ static bool vhost_vsock_common_guest_notifier_pending(VirtIODevice *vdev,
 {
     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
 
+    if (idx == VIRTIO_CONFIG_IRQ_IDX) {
+        return false;
+    }
     return vhost_virtqueue_pending(&vvc->vhost_dev, idx);
 }
 
hw/virtio/virtio-crypto.c +6 -0
index 54f9bbb789..1d5192f8b4 100644
@@ -948,6 +948,9 @@ static void virtio_crypto_guest_notifier_mask(VirtIODevice *vdev, int idx,
 
     assert(vcrypto->vhost_started);
 
+    if (idx == VIRTIO_CONFIG_IRQ_IDX) {
+        return;
+    }
     cryptodev_vhost_virtqueue_mask(vdev, queue, idx, mask);
 }
 
@@ -958,6 +961,9 @@ static bool virtio_crypto_guest_notifier_pending(VirtIODevice *vdev, int idx)
 
     assert(vcrypto->vhost_started);
 
+    if (idx == VIRTIO_CONFIG_IRQ_IDX) {
+        return false;
+    }
     return cryptodev_vhost_virtqueue_pending(vdev, queue, idx);
 }
 
include/hw/virtio/virtio.h +2 -0
index b7ece7a6a8..63cb9455ed 100644
@@ -67,6 +67,8 @@ typedef struct VirtQueueElement
 
 #define VIRTIO_NO_VECTOR 0xffff
 
+#define VIRTIO_CONFIG_IRQ_IDX -1
+
 #define TYPE_VIRTIO_DEVICE "virtio-device"
 OBJECT_DECLARE_TYPE(VirtIODevice, VirtioDeviceClass, VIRTIO_DEVICE)
 
Signature · 1 line
2.21.3
author
#2 lore raw

PATCH v7 2/10 virtio-pci:decouple virtqueue from interrupt setting process

In reply to Cindy Lu

Decouple virtqueue from interrupt setting process to support config interrupt Now the code for interrupt/vector are coupling
with the vq number, this patch will decouple the vritqueue numbers from these functions.

---
 hw/virtio/virtio-pci.c | 51 ++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 22 deletions(-)
hw/virtio/virtio-pci.c +29 -22
index b321604d9b..c5c080ec94 100644
@@ -693,23 +693,17 @@ static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
 }
 
 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
-                                 unsigned int queue_no,
+                                 EventNotifier *n,
                                  unsigned int vector)
 {
     VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
-    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
-    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
-    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
     return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq);
 }
 
 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
-                                      unsigned int queue_no,
+                                      EventNotifier *n ,
                                       unsigned int vector)
 {
-    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
-    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
-    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
     VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
     int ret;
 
@@ -724,7 +718,8 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
     unsigned int vector;
     int ret, queue_no;
-
+    VirtQueue *vq;
+    EventNotifier *n;
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
@@ -741,7 +736,9 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
          * Otherwise, delay until unmasked in the frontend.
          */
         if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
+            vq = virtio_get_queue(vdev, queue_no);
+            n = virtio_queue_get_guest_notifier(vq);
+            ret = kvm_virtio_pci_irqfd_use(proxy, n, vector);
             if (ret < 0) {
                 kvm_virtio_pci_vq_vector_release(proxy, vector);
                 goto undo;
@@ -757,7 +754,9 @@ undo:
             continue;
         }
         if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
+            vq = virtio_get_queue(vdev, queue_no);
+            n = virtio_queue_get_guest_notifier(vq);
+            kvm_virtio_pci_irqfd_release(proxy, n, vector);
         }
         kvm_virtio_pci_vq_vector_release(proxy, vector);
     }
@@ -771,7 +770,8 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
     unsigned int vector;
     int queue_no;
     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
-
+    VirtQueue *vq;
+    EventNotifier *n;
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
@@ -784,7 +784,9 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
          * Otherwise, it was cleaned when masked in the frontend.
          */
         if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
+            vq = virtio_get_queue(vdev, queue_no);
+            n = virtio_queue_get_guest_notifier(vq);
+            kvm_virtio_pci_irqfd_release(proxy, n, vector);
         }
         kvm_virtio_pci_vq_vector_release(proxy, vector);
     }
@@ -793,12 +795,11 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
                                        unsigned int queue_no,
                                        unsigned int vector,
-                                       MSIMessage msg)
+                                       MSIMessage msg,
+                                       EventNotifier *n)
 {
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
-    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
-    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
     VirtIOIRQFD *irqfd;
     int ret = 0;
 
@@ -825,14 +826,15 @@ static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
             event_notifier_set(n);
         }
     } else {
-        ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
+        ret = kvm_virtio_pci_irqfd_use(proxy, n, vector);
     }
     return ret;
 }
 
 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
                                              unsigned int queue_no,
-                                             unsigned int vector)
+                                             unsigned int vector,
+                                             EventNotifier *n)
 {
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
@@ -843,7 +845,7 @@ static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
     if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
         k->guest_notifier_mask(vdev, queue_no, true);
     } else {
-        kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
+        kvm_virtio_pci_irqfd_release(proxy, n, vector);
     }
 }
 
@@ -853,6 +855,7 @@ static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
     VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
     VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
+    EventNotifier *n;
     int ret, index, unmasked = 0;
 
     while (vq) {
@@ -861,7 +864,8 @@ static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
             break;
         }
         if (index < proxy->nvqs_with_notifiers) {
-            ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
+            n = virtio_queue_get_guest_notifier(vq);
+            ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg, n);
             if (ret < 0) {
                 goto undo;
             }
@@ -877,7 +881,8 @@ undo:
     while (vq && unmasked >= 0) {
         index = virtio_get_queue_index(vq);
         if (index < proxy->nvqs_with_notifiers) {
-            virtio_pci_vq_vector_mask(proxy, index, vector);
+            n = virtio_queue_get_guest_notifier(vq);
+            virtio_pci_vq_vector_mask(proxy, index, vector, n);
             --unmasked;
         }
         vq = virtio_vector_next_queue(vq);
@@ -890,15 +895,17 @@ static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
     VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
     VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
+    EventNotifier *n;
     int index;
 
     while (vq) {
         index = virtio_get_queue_index(vq);
+         n = virtio_queue_get_guest_notifier(vq);
         if (!virtio_queue_get_num(vdev, index)) {
             break;
         }
         if (index < proxy->nvqs_with_notifiers) {
-            virtio_pci_vq_vector_mask(proxy, index, vector);
+            virtio_pci_vq_vector_mask(proxy, index, vector, n);
         }
         vq = virtio_vector_next_queue(vq);
     }
Signature · 1 line
2.21.3
#3 lore raw

Re: [PATCH v7 02/10] virtio-pci:decouple virtqueue from interrupt setting process

Quoted 41 lines · Cindy Lu

Decouple virtqueue from interrupt setting process to support config interrupt Now the code for interrupt/vector are coupling
with the vq number, this patch will decouple the vritqueue numbers from these functions.

---
  hw/virtio/virtio-pci.c | 51 ++++++++++++++++++++++++------------------
  1 file changed, 29 insertions(+), 22 deletions(-)
hw/virtio/virtio-pci.c +2 -8
index b321604d9b..c5c080ec94 100644
@@ -693,23 +693,17 @@ static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
  }
 
  static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
-                                 unsigned int queue_no,
+                                 EventNotifier *n,
                                   unsigned int vector)
  {
      VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
-    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
-    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
-    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
      return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq);
  }
 
  static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
-                                      unsigned int queue_no,
+                                      EventNotifier *n ,
                                        unsigned int vector)
  {
-    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
-    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
-    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
      VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
      int ret;
 

hw/virtio/virtio-pci.c static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs) · line 722

Quoted 6 lines
hw/virtio/virtio-pci.c +2 -1
@@ -724,7 +718,8 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
      VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
      unsigned int vector;
      int ret, queue_no;
-
+    VirtQueue *vq;
+    EventNotifier *n;

Let's leave a newline here.

Quoted 26 lines
hw/virtio/virtio-pci.c +6 -2
      for (queue_no = 0; queue_no < nvqs; queue_no++) {
          if (!virtio_queue_get_num(vdev, queue_no)) {
              break;
@@ -741,7 +736,9 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
           * Otherwise, delay until unmasked in the frontend.
           */
          if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
+            vq = virtio_get_queue(vdev, queue_no);
+            n = virtio_queue_get_guest_notifier(vq);
+            ret = kvm_virtio_pci_irqfd_use(proxy, n, vector);
              if (ret < 0) {
                  kvm_virtio_pci_vq_vector_release(proxy, vector);
                  goto undo;
@@ -757,7 +754,9 @@ undo:
              continue;
          }
          if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
+            vq = virtio_get_queue(vdev, queue_no);
+            n = virtio_queue_get_guest_notifier(vq);
+            kvm_virtio_pci_irqfd_release(proxy, n, vector);
          }
          kvm_virtio_pci_vq_vector_release(proxy, vector);
      }

hw/virtio/virtio-pci.c static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs) · line 774

Quoted 6 lines
hw/virtio/virtio-pci.c +2 -1
@@ -771,7 +770,8 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
      unsigned int vector;
      int queue_no;
      VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
-
+    VirtQueue *vq;
+    EventNotifier *n;

Similar here.

Quoted 15 lines
hw/virtio/virtio-pci.c +3 -1
      for (queue_no = 0; queue_no < nvqs; queue_no++) {
          if (!virtio_queue_get_num(vdev, queue_no)) {
              break;
@@ -784,7 +784,9 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
           * Otherwise, it was cleaned when masked in the frontend.
           */
          if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
+            vq = virtio_get_queue(vdev, queue_no);
+            n = virtio_queue_get_guest_notifier(vq);
+            kvm_virtio_pci_irqfd_release(proxy, n, vector);
          }
          kvm_virtio_pci_vq_vector_release(proxy, vector);
      }

hw/virtio/virtio-pci.c static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs) · line 800

Quoted 7 lines
hw/virtio/virtio-pci.c +2 -1
@@ -793,12 +795,11 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
  static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
                                         unsigned int queue_no,
                                         unsigned int vector,
-                                       MSIMessage msg)
+                                       MSIMessage msg,
+                                       EventNotifier *n)
  {

A question: if this function needs to be used by configure interrupt, it needs rename. Otherwise we don't need to bother since it only deal with vq vector.

Quoted 63 lines
hw/virtio/virtio-pci.c +9 -7
      VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
      VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
-    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
-    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
      VirtIOIRQFD *irqfd;
      int ret = 0;
 
@@ -825,14 +826,15 @@ static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
              event_notifier_set(n);
          }
      } else {
-        ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
+        ret = kvm_virtio_pci_irqfd_use(proxy, n, vector);
      }
      return ret;
  }
 
  static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
                                               unsigned int queue_no,
-                                             unsigned int vector)
+                                             unsigned int vector,
+                                             EventNotifier *n)
  {
      VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
      VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
@@ -843,7 +845,7 @@ static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
      if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
          k->guest_notifier_mask(vdev, queue_no, true);
      } else {
-        kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
+        kvm_virtio_pci_irqfd_release(proxy, n, vector);
      }
  }
 
@@ -853,6 +855,7 @@ static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
      VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
      VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
      VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
+    EventNotifier *n;
      int ret, index, unmasked = 0;
 
      while (vq) {
@@ -861,7 +864,8 @@ static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
              break;
          }
          if (index < proxy->nvqs_with_notifiers) {
-            ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
+            n = virtio_queue_get_guest_notifier(vq);
+            ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg, n);
              if (ret < 0) {
                  goto undo;
              }
@@ -877,7 +881,8 @@ undo:
      while (vq && unmasked >= 0) {
          index = virtio_get_queue_index(vq);
          if (index < proxy->nvqs_with_notifiers) {
-            virtio_pci_vq_vector_mask(proxy, index, vector);
+            n = virtio_queue_get_guest_notifier(vq);
+            virtio_pci_vq_vector_mask(proxy, index, vector, n);
              --unmasked;
          }
          vq = virtio_vector_next_queue(vq);

hw/virtio/virtio-pci.c static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector) · line 903

Quoted 9 lines
hw/virtio/virtio-pci.c +2 -0
@@ -890,15 +895,17 @@ static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
      VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
      VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
      VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
+    EventNotifier *n;
      int index;
 
      while (vq) {
          index = virtio_get_queue_index(vq);
+         n = virtio_queue_get_guest_notifier(vq);

Indentation looks wrong.

Thanks

Quoted 9 lines
hw/virtio/virtio-pci.c +1 -1
          if (!virtio_queue_get_num(vdev, index)) {
              break;
          }
          if (index < proxy->nvqs_with_notifiers) {
-            virtio_pci_vq_vector_mask(proxy, index, vector);
+            virtio_pci_vq_vector_mask(proxy, index, vector, n);
          }
          vq = virtio_vector_next_queue(vq);
      }
author
#4 lore raw

PATCH v7 3/10 virtio: decouple virtqueue from set notifier fd handler

In reply to Cindy Lu

To support config interrupt.we need to decouple virtqueue number from virtio_queue_set_guest_notifier_fd_handler,

---
 hw/s390x/virtio-ccw.c      |  6 +++---
 hw/virtio/virtio-mmio.c    |  4 ++--
 hw/virtio/virtio-pci.c     |  4 ++--
 hw/virtio/virtio.c         | 17 +++++++++++------
 include/hw/virtio/virtio.h |  2 +-
 5 files changed, 19 insertions(+), 14 deletions(-)
hw/s390x/virtio-ccw.c +3 -3
index 8195f3546e..58bb5232fd 100644
@@ -1028,11 +1028,11 @@ static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
         if (r < 0) {
             return r;
         }
-        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
+        virtio_set_notifier_fd_handler(vdev, n, true, with_irqfd);
         if (with_irqfd) {
             r = virtio_ccw_add_irqfd(dev, n);
             if (r) {
-                virtio_queue_set_guest_notifier_fd_handler(vq, false,
+                virtio_set_notifier_fd_handler(vdev, n, false,
                                                            with_irqfd);
                 return r;
             }
@@ -1056,7 +1056,7 @@ static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
         if (with_irqfd) {
             virtio_ccw_remove_irqfd(dev, n);
         }
-        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
+        virtio_set_notifier_fd_handler(vdev, n, false, with_irqfd);
         event_notifier_cleanup(notifier);
     }
     return 0;
hw/virtio/virtio-mmio.c +2 -2
index 342c918ea7..13772d52bb 100644
@@ -658,9 +658,9 @@ static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign,
         if (r < 0) {
             return r;
         }
-        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
+        virtio_set_notifier_fd_handler(vdev, n, true, with_irqfd);
     } else {
-        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
+        virtio_set_notifier_fd_handler(vdev, n, false, with_irqfd);
         event_notifier_cleanup(notifier);
     }
 
hw/virtio/virtio-pci.c +2 -2
index c5c080ec94..6a4ef413a4 100644
@@ -958,9 +958,9 @@ static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
         if (r < 0) {
             return r;
         }
-        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
+        virtio_set_notifier_fd_handler(vdev, n, true, with_irqfd);
     } else {
-        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
+        virtio_set_notifier_fd_handler(vdev, n, false, with_irqfd);
         event_notifier_cleanup(notifier);
     }
 
hw/virtio/virtio.c +11 -6
index 07f4e60b30..c5d786bb5e 100644
@@ -3505,19 +3505,24 @@ static void virtio_queue_guest_notifier_read(EventNotifier *n)
     }
 }
 
-void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
-                                                bool with_irqfd)
+
+void virtio_set_notifier_fd_handler(VirtIODevice *vdev, int queue_no,
+                               bool assign, bool with_irqfd)
 {
+    EventNotifier *e ;
+    EventNotifierHandler *handler;
+    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
+    e = &vq->guest_notifier;
+    handler = virtio_queue_guest_notifier_read;
     if (assign && !with_irqfd) {
-        event_notifier_set_handler(&vq->guest_notifier,
-                                   virtio_queue_guest_notifier_read);
+        event_notifier_set_handler(e, handler);
     } else {
-        event_notifier_set_handler(&vq->guest_notifier, NULL);
+        event_notifier_set_handler(e, NULL);
     }
     if (!assign) {
         /* Test and clear notifier before closing it,
          * in case poll callback didn't have time to run. */
-        virtio_queue_guest_notifier_read(&vq->guest_notifier);
+        handler(e);
     }
 }
 
include/hw/virtio/virtio.h +1 -1
index 63cb9455ed..447899dea5 100644
@@ -310,7 +310,7 @@ void virtio_queue_update_used_idx(VirtIODevice *vdev, int n);
 VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
 uint16_t virtio_get_queue_index(VirtQueue *vq);
 EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
-void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
+void virtio_set_notifier_fd_handler(VirtIODevice *vdev, int n, bool assign,
                                                 bool with_irqfd);
 int virtio_device_start_ioeventfd(VirtIODevice *vdev);
 int virtio_device_grab_ioeventfd(VirtIODevice *vdev);
Signature · 1 line
2.21.3
#5 lore raw

Re: [PATCH v7 03/10] virtio: decouple virtqueue from set notifier fd handler

Quoted 1 line · Cindy Lu

To support config interrupt.we need to decouple virtqueue number

s/we/ We/

Quoted 75 lines

from virtio_queue_set_guest_notifier_fd_handler,

---
  hw/s390x/virtio-ccw.c      |  6 +++---
  hw/virtio/virtio-mmio.c    |  4 ++--
  hw/virtio/virtio-pci.c     |  4 ++--
  hw/virtio/virtio.c         | 17 +++++++++++------
  include/hw/virtio/virtio.h |  2 +-
  5 files changed, 19 insertions(+), 14 deletions(-)
hw/s390x/virtio-ccw.c +3 -3
index 8195f3546e..58bb5232fd 100644
@@ -1028,11 +1028,11 @@ static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
          if (r < 0) {
              return r;
          }
-        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
+        virtio_set_notifier_fd_handler(vdev, n, true, with_irqfd);
          if (with_irqfd) {
              r = virtio_ccw_add_irqfd(dev, n);
              if (r) {
-                virtio_queue_set_guest_notifier_fd_handler(vq, false,
+                virtio_set_notifier_fd_handler(vdev, n, false,
                                                             with_irqfd);
                  return r;
              }
@@ -1056,7 +1056,7 @@ static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
          if (with_irqfd) {
              virtio_ccw_remove_irqfd(dev, n);
          }
-        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
+        virtio_set_notifier_fd_handler(vdev, n, false, with_irqfd);
          event_notifier_cleanup(notifier);
      }
      return 0;
hw/virtio/virtio-mmio.c +2 -2
index 342c918ea7..13772d52bb 100644
@@ -658,9 +658,9 @@ static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign,
          if (r < 0) {
              return r;
          }
-        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
+        virtio_set_notifier_fd_handler(vdev, n, true, with_irqfd);
      } else {
-        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
+        virtio_set_notifier_fd_handler(vdev, n, false, with_irqfd);
          event_notifier_cleanup(notifier);
      }
 
hw/virtio/virtio-pci.c +2 -2
index c5c080ec94..6a4ef413a4 100644
@@ -958,9 +958,9 @@ static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
          if (r < 0) {
              return r;
          }
-        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
+        virtio_set_notifier_fd_handler(vdev, n, true, with_irqfd);
      } else {
-        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
+        virtio_set_notifier_fd_handler(vdev, n, false, with_irqfd);
          event_notifier_cleanup(notifier);
      }
 
hw/virtio/virtio.c +0 -0
index 07f4e60b30..c5d786bb5e 100644

hw/virtio/virtio.c static void virtio_queue_guest_notifier_read(EventNotifier *n) · line 3510

Quoted 8 lines
hw/virtio/virtio.c +3 -2
@@ -3505,19 +3505,24 @@ static void virtio_queue_guest_notifier_read(EventNotifier *n)
      }
  }
 
-void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
-                                                bool with_irqfd)
+
+void virtio_set_notifier_fd_handler(VirtIODevice *vdev, int queue_no,
+                               bool assign, bool with_irqfd)

Let's use "virtio_set_guest_notifier_fd_handler()".

hw/virtio/virtio.c static void virtio_queue_guest_notifier_read(EventNotifier *n) · line 3515

Quoted 5 lines
hw/virtio/virtio.c +4 -0
  {
+    EventNotifier *e ;
+    EventNotifierHandler *handler;
+    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
+    e = &vq->guest_notifier;

So you still depends on the vq implicitly.

How about keep virtio_queue_set_guest_notifier_fd_handler() and factor its core logic to

virtio_set_guest_notifier_fd_handler() by passing the EventNotifier to this new helper?

Quoted 4 lines
hw/virtio/virtio.c +1 -2
+    handler = virtio_queue_guest_notifier_read;
      if (assign && !with_irqfd) {
-        event_notifier_set_handler(&vq->guest_notifier,
-                                   virtio_queue_guest_notifier_read);

hw/virtio/virtio.c static void virtio_queue_guest_notifier_read(EventNotifier *n) · line 3525

Quoted 10 lines
hw/virtio/virtio.c +3 -2
+        event_notifier_set_handler(e, handler);
      } else {
-        event_notifier_set_handler(&vq->guest_notifier, NULL);
+        event_notifier_set_handler(e, NULL);
      }
      if (!assign) {
          /* Test and clear notifier before closing it,
           * in case poll callback didn't have time to run. */
-        virtio_queue_guest_notifier_read(&vq->guest_notifier);
+        handler(e);

Any reason for this change?

Thanks

Quoted 16 lines
hw/virtio/virtio.c +0 -0
      }
  }
 
include/hw/virtio/virtio.h +1 -1
index 63cb9455ed..447899dea5 100644
@@ -310,7 +310,7 @@ void virtio_queue_update_used_idx(VirtIODevice *vdev, int n);
  VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
  uint16_t virtio_get_queue_index(VirtQueue *vq);
  EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
-void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
+void virtio_set_notifier_fd_handler(VirtIODevice *vdev, int n, bool assign,
                                                  bool with_irqfd);
  int virtio_device_start_ioeventfd(VirtIODevice *vdev);
  int virtio_device_grab_ioeventfd(VirtIODevice *vdev);
author
#6 lore raw

PATCH v7 4/10 vhost: add new call back function for config interrupt

In reply to Cindy Lu

To support configure interrupt, we need to add a new call back function for config interrupt.

---
 include/hw/virtio/vhost-backend.h | 3 +++
 1 file changed, 3 insertions(+)
include/hw/virtio/vhost-backend.h +3 -0
index 8a6f8e2a7a..adaf6982d2 100644
@@ -125,6 +125,8 @@ typedef int (*vhost_get_device_id_op)(struct vhost_dev *dev, uint32_t *dev_id);
 
 typedef bool (*vhost_force_iommu_op)(struct vhost_dev *dev);
 
+typedef int (*vhost_set_config_call_op)(struct vhost_dev *dev,
+                                       int *fd);
 typedef struct VhostOps {
     VhostBackendType backend_type;
     vhost_backend_init vhost_backend_init;
@@ -170,6 +172,7 @@ typedef struct VhostOps {
     vhost_vq_get_addr_op  vhost_vq_get_addr;
     vhost_get_device_id_op vhost_get_device_id;
     vhost_force_iommu_op vhost_force_iommu;
+    vhost_set_config_call_op vhost_set_config_call;
 } VhostOps;
 
 extern const VhostOps user_ops;
Signature · 1 line
2.21.3
Acked-by on v7 4/10
#7 lore raw

Re: [PATCH v7 04/10] vhost: add new call back function for config interrupt

Quoted 4 lines · Cindy Lu

To support configure interrupt, we need to add a new call back function for config interrupt.

Quoted 25 lines
---
  include/hw/virtio/vhost-backend.h | 3 +++
  1 file changed, 3 insertions(+)
include/hw/virtio/vhost-backend.h +3 -0
index 8a6f8e2a7a..adaf6982d2 100644
@@ -125,6 +125,8 @@ typedef int (*vhost_get_device_id_op)(struct vhost_dev *dev, uint32_t *dev_id);
 
  typedef bool (*vhost_force_iommu_op)(struct vhost_dev *dev);
 
+typedef int (*vhost_set_config_call_op)(struct vhost_dev *dev,
+                                       int *fd);
  typedef struct VhostOps {
      VhostBackendType backend_type;
      vhost_backend_init vhost_backend_init;
@@ -170,6 +172,7 @@ typedef struct VhostOps {
      vhost_vq_get_addr_op  vhost_vq_get_addr;
      vhost_get_device_id_op vhost_get_device_id;
      vhost_force_iommu_op vhost_force_iommu;
+    vhost_set_config_call_op vhost_set_config_call;
  } VhostOps;
 
  extern const VhostOps user_ops;
author
#8 lore raw

PATCH v7 5/10 vhost-vdpa: add support for config interrupt call back

In reply to Cindy Lu

Add new call back function in vhost-vdpa, this call back function only supported in vhost-vdpa backend

---
 hw/virtio/trace-events | 2 ++
 hw/virtio/vhost-vdpa.c | 7 +++++++
 2 files changed, 9 insertions(+)
hw/virtio/trace-events +2 -0
index c62727f879..60a15f0186 100644
@@ -52,6 +52,8 @@ vhost_vdpa_set_vring_call(void *dev, unsigned int index, int fd) "dev: %p index:
 vhost_vdpa_get_features(void *dev, uint64_t features) "dev: %p features: 0x%"PRIx64
 vhost_vdpa_set_owner(void *dev) "dev: %p"
 vhost_vdpa_vq_get_addr(void *dev, void *vq, uint64_t desc_user_addr, uint64_t avail_user_addr, uint64_t used_user_addr) "dev: %p vq: %p desc_user_addr: 0x%"PRIx64" avail_user_addr: 0x%"PRIx64" used_user_addr: 0x%"PRIx64
+vhost_vdpa_set_config_call(void *dev, int *fd)"dev: %p fd: %p"
+
 
 # virtio.c
 virtqueue_alloc_element(void *elem, size_t sz, unsigned in_num, unsigned out_num) "elem %p size %zd in_num %u out_num %u"
hw/virtio/vhost-vdpa.c +7 -0
index 01d2101d09..9ba2a2bed4 100644
@@ -545,6 +545,12 @@ static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,
     trace_vhost_vdpa_set_vring_call(dev, file->index, file->fd);
     return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file);
 }
+static int vhost_vdpa_set_config_call(struct vhost_dev *dev,
+                                       int *fd)
+{
+    trace_vhost_vdpa_set_config_call(dev, fd);
+    return vhost_vdpa_call(dev, VHOST_VDPA_SET_CONFIG_CALL, fd);
+}
 
 static int vhost_vdpa_get_features(struct vhost_dev *dev,
                                      uint64_t *features)
@@ -611,4 +617,5 @@ const VhostOps vdpa_ops = {
         .vhost_get_device_id = vhost_vdpa_get_device_id,
         .vhost_vq_get_addr = vhost_vdpa_vq_get_addr,
         .vhost_force_iommu = vhost_vdpa_force_iommu,
+        .vhost_set_config_call = vhost_vdpa_set_config_call,
 };
Signature · 1 line
2.21.3
#9 lore raw

Re: [PATCH v7 05/10] vhost-vdpa: add support for config interrupt call back

Quoted 14 lines · Cindy Lu

Add new call back function in vhost-vdpa, this call back function only supported in vhost-vdpa backend

---
  hw/virtio/trace-events | 2 ++
  hw/virtio/vhost-vdpa.c | 7 +++++++
  2 files changed, 9 insertions(+)
hw/virtio/trace-events +0 -0
index c62727f879..60a15f0186 100644

hw/virtio/trace-events vhost_vdpa_set_vring_call(void *dev, unsigned int index, int fd) "dev: %p index: · line 55

Quoted 4 lines
hw/virtio/trace-events +1 -0
@@ -52,6 +52,8 @@ vhost_vdpa_set_vring_call(void *dev, unsigned int index, int fd) "dev: %p index:
  vhost_vdpa_get_features(void *dev, uint64_t features) "dev: %p features: 0x%"PRIx64
  vhost_vdpa_set_owner(void *dev) "dev: %p"
  vhost_vdpa_vq_get_addr(void *dev, void *vq, uint64_t desc_user_addr, uint64_t avail_user_addr, uint64_t used_user_addr) "dev: %p vq: %p desc_user_addr: 0x%"PRIx64" avail_user_addr: 0x%"PRIx64" used_user_addr: 0x%"PRIx64
+vhost_vdpa_set_config_call(void *dev, int *fd)"dev: %p fd: %p"

This tracing information is sub-optimal, I think we should show the fd instead of its address here.

Other looks good.

Thanks

Quoted 27 lines
hw/virtio/trace-events +1 -0
+
 
  # virtio.c
  virtqueue_alloc_element(void *elem, size_t sz, unsigned in_num, unsigned out_num) "elem %p size %zd in_num %u out_num %u"
hw/virtio/vhost-vdpa.c +7 -0
index 01d2101d09..9ba2a2bed4 100644
@@ -545,6 +545,12 @@ static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,
      trace_vhost_vdpa_set_vring_call(dev, file->index, file->fd);
      return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file);
  }
+static int vhost_vdpa_set_config_call(struct vhost_dev *dev,
+                                       int *fd)
+{
+    trace_vhost_vdpa_set_config_call(dev, fd);
+    return vhost_vdpa_call(dev, VHOST_VDPA_SET_CONFIG_CALL, fd);
+}
 
  static int vhost_vdpa_get_features(struct vhost_dev *dev,
                                       uint64_t *features)
@@ -611,4 +617,5 @@ const VhostOps vdpa_ops = {
          .vhost_get_device_id = vhost_vdpa_get_device_id,
          .vhost_vq_get_addr = vhost_vdpa_vq_get_addr,
          .vhost_force_iommu = vhost_vdpa_force_iommu,
+        .vhost_set_config_call = vhost_vdpa_set_config_call,
  };
author
#10 lore raw

Re: [PATCH v7 05/10] vhost-vdpa: add support for config interrupt call back

Quoted 26 lines · Jason Wang
Quoted 14 lines · Cindy Lu

Add new call back function in vhost-vdpa, this call back function only supported in vhost-vdpa backend

---
  hw/virtio/trace-events | 2 ++
  hw/virtio/vhost-vdpa.c | 7 +++++++
  2 files changed, 9 insertions(+)
hw/virtio/trace-events +0 -0
index c62727f879..60a15f0186 100644

hw/virtio/trace-events vhost_vdpa_set_vring_call(void *dev, unsigned int index, int fd) "dev: %p index: · line 55

Quoted 4 lines
hw/virtio/trace-events +1 -0
@@ -52,6 +52,8 @@ vhost_vdpa_set_vring_call(void *dev, unsigned int index, int fd) "dev: %p index:
  vhost_vdpa_get_features(void *dev, uint64_t features) "dev: %p features: 0x%"PRIx64
  vhost_vdpa_set_owner(void *dev) "dev: %p"
  vhost_vdpa_vq_get_addr(void *dev, void *vq, uint64_t desc_user_addr, uint64_t avail_user_addr, uint64_t used_user_addr) "dev: %p vq: %p desc_user_addr: 0x%"PRIx64" avail_user_addr: 0x%"PRIx64" used_user_addr: 0x%"PRIx64
+vhost_vdpa_set_config_call(void *dev, int *fd)"dev: %p fd: %p"

This tracing information is sub-optimal, I think we should show the fd instead of its address here.

Quoted 5 lines

Other looks good.

Thanks

sure, I will fix this

Quoted 28 lines
Quoted 27 lines
hw/virtio/trace-events +1 -0
+
 
  # virtio.c
  virtqueue_alloc_element(void *elem, size_t sz, unsigned in_num, unsigned out_num) "elem %p size %zd in_num %u out_num %u"
hw/virtio/vhost-vdpa.c +7 -0
index 01d2101d09..9ba2a2bed4 100644
@@ -545,6 +545,12 @@ static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,
      trace_vhost_vdpa_set_vring_call(dev, file->index, file->fd);
      return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file);
  }
+static int vhost_vdpa_set_config_call(struct vhost_dev *dev,
+                                       int *fd)
+{
+    trace_vhost_vdpa_set_config_call(dev, fd);
+    return vhost_vdpa_call(dev, VHOST_VDPA_SET_CONFIG_CALL, fd);
+}
 
  static int vhost_vdpa_get_features(struct vhost_dev *dev,
                                       uint64_t *features)
@@ -611,4 +617,5 @@ const VhostOps vdpa_ops = {
          .vhost_get_device_id = vhost_vdpa_get_device_id,
          .vhost_vq_get_addr = vhost_vdpa_vq_get_addr,
          .vhost_force_iommu = vhost_vdpa_force_iommu,
+        .vhost_set_config_call = vhost_vdpa_set_config_call,
  };
author
#11 lore raw

PATCH v7 6/10 vhost:add support for configure interrupt

In reply to Cindy Lu

Add configure notifier support in vhost and virtio driver When backend support VIRTIO_NET_F_STATUS,setup the configure interrupt function in vhost_dev_start and release the related resource when vhost_dev_stop

---
 hw/net/vhost_net.c         |  9 +++++
 hw/net/virtio-net.c        |  6 ++++
 hw/virtio/vhost.c          | 68 ++++++++++++++++++++++++++++++++++++--
 hw/virtio/virtio.c         | 23 +++++++++++--
 include/hw/virtio/vhost.h  |  2 ++
 include/hw/virtio/virtio.h |  3 ++
 include/net/vhost_net.h    |  3 ++
 7 files changed, 109 insertions(+), 5 deletions(-)
hw/net/vhost_net.c +9 -0
index 24d555e764..5d0c35f18d 100644
@@ -426,6 +426,15 @@ void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
     vhost_virtqueue_mask(&net->dev, dev, idx, mask);
 }
 
+bool vhost_net_config_pending(VHostNetState *net, int idx)
+{
+    return vhost_config_pending(&net->dev, idx);
+}
+void vhost_net_config_mask(VHostNetState *net, VirtIODevice *dev,
+                              bool mask)
+{
+    vhost_config_mask(&net->dev, dev, mask);
+}
 VHostNetState *get_vhost_net(NetClientState *nc)
 {
     VHostNetState *vhost_net = 0;
hw/net/virtio-net.c +6 -0
index f50235b5d6..02033be748 100644
@@ -3055,6 +3055,9 @@ static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
     if (idx != VIRTIO_CONFIG_IRQ_IDX) {
         return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
     }
+    if (idx == VIRTIO_CONFIG_IRQ_IDX) {
+        return vhost_net_config_pending(get_vhost_net(nc->peer), idx);
+   }
     return false;
 }
 
@@ -3067,6 +3070,9 @@ static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
     if (idx != VIRTIO_CONFIG_IRQ_IDX) {
         vhost_net_virtqueue_mask(get_vhost_net(nc->peer), vdev, idx, mask);
     }
+    if (idx == VIRTIO_CONFIG_IRQ_IDX) {
+        vhost_net_config_mask(get_vhost_net(nc->peer), vdev, mask);
+     }
 }
 
 static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
hw/virtio/vhost.c +66 -2

113 lines not on this page · raw message

hw/virtio/virtio.c +20 -3
index c5d786bb5e..09ed3f67d9 100644
@@ -3506,14 +3506,27 @@ static void virtio_queue_guest_notifier_read(EventNotifier *n)
 }
 
 
+static void virtio_config_read(EventNotifier *n)
+{
+    VirtIODevice *vdev = container_of(n, VirtIODevice, config_notifier);
+
+    if (event_notifier_test_and_clear(n)) {
+        virtio_notify_config(vdev);
+    }
+}
 void virtio_set_notifier_fd_handler(VirtIODevice *vdev, int queue_no,
                                bool assign, bool with_irqfd)
 {
     EventNotifier *e ;
     EventNotifierHandler *handler;
-    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
-    e = &vq->guest_notifier;
-    handler = virtio_queue_guest_notifier_read;
+    if (queue_no != VIRTIO_CONFIG_IRQ_IDX) {
+        VirtQueue *vq = virtio_get_queue(vdev, queue_no);
+        e = &vq->guest_notifier;
+        handler = virtio_queue_guest_notifier_read;
+    } else {
+        e = &vdev->config_notifier;
+        handler = virtio_config_read;
+    }
     if (assign && !with_irqfd) {
         event_notifier_set_handler(e, handler);
     } else {
@@ -3599,6 +3612,10 @@ EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
     return &vq->host_notifier;
 }
 
+EventNotifier *virtio_get_config_notifier(VirtIODevice *vdev)
+{
+    return &vdev->config_notifier;
+}
 void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled)
 {
     vq->host_notifier_enabled = enabled;
include/hw/virtio/vhost.h +2 -0
index 4a8bc75415..b8814ece32 100644
@@ -108,6 +108,8 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
+bool vhost_config_pending(struct vhost_dev *hdev, int n);
+void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev, bool mask);
 
 /* Test and clear masked event pending status.
  * Should be called after unmask to avoid losing events.
include/hw/virtio/virtio.h +3 -0
index 447899dea5..5856517d43 100644
@@ -110,6 +110,8 @@ struct VirtIODevice
     bool use_guest_notifier_mask;
     AddressSpace *dma_as;
     QLIST_HEAD(, VirtQueue) *vector_queues;
+    EventNotifier config_notifier;
+    EventNotifier masked_config_notifier;
 };
 
 struct VirtioDeviceClass {
@@ -317,6 +319,7 @@ int virtio_device_grab_ioeventfd(VirtIODevice *vdev);
 void virtio_device_release_ioeventfd(VirtIODevice *vdev);
 bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev);
 EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
+EventNotifier *virtio_get_config_notifier(VirtIODevice *vdev);
 void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled);
 void virtio_queue_host_notifier_read(EventNotifier *n);
 void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
include/net/vhost_net.h +3 -0
index 172b0051d8..0d38c97c94 100644
@@ -36,6 +36,9 @@ int vhost_net_set_config(struct vhost_net *net, const uint8_t *data,
 bool vhost_net_virtqueue_pending(VHostNetState *net, int n);
 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
                               int idx, bool mask);
+bool vhost_net_config_pending(VHostNetState *net, int n);
+void vhost_net_config_mask(VHostNetState *net, VirtIODevice *dev,
+                              bool mask);
 int vhost_net_notify_migration_done(VHostNetState *net, char* mac_addr);
 VHostNetState *get_vhost_net(NetClientState *nc);
 
Signature · 1 line
2.21.3
#12 lore raw

Re: [PATCH v7 06/10] vhost:add support for configure interrupt

Quoted 2 lines · Cindy Lu

Add configure notifier support in vhost and virtio driver When backend support VIRTIO_NET_F_STATUS,

So config interrupt is the basic facility of the virtio device. We need to make the code not specific to this feature.

But we can leave the specific device (like virtio-net, or vhost-net) to decide whether or not it can be used.

For net, it is also used by guest announcement.

Quoted 64 lines

setup the configure
interrupt function in vhost_dev_start and release the related resource when vhost_dev_stop

---
  hw/net/vhost_net.c         |  9 +++++
  hw/net/virtio-net.c        |  6 ++++
  hw/virtio/vhost.c          | 68 ++++++++++++++++++++++++++++++++++++--
  hw/virtio/virtio.c         | 23 +++++++++++--
  include/hw/virtio/vhost.h  |  2 ++
  include/hw/virtio/virtio.h |  3 ++
  include/net/vhost_net.h    |  3 ++
  7 files changed, 109 insertions(+), 5 deletions(-)
hw/net/vhost_net.c +9 -0
index 24d555e764..5d0c35f18d 100644
@@ -426,6 +426,15 @@ void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
      vhost_virtqueue_mask(&net->dev, dev, idx, mask);
  }
 
+bool vhost_net_config_pending(VHostNetState *net, int idx)
+{
+    return vhost_config_pending(&net->dev, idx);
+}
+void vhost_net_config_mask(VHostNetState *net, VirtIODevice *dev,
+                              bool mask)
+{
+    vhost_config_mask(&net->dev, dev, mask);
+}
  VHostNetState *get_vhost_net(NetClientState *nc)
  {
      VHostNetState *vhost_net = 0;
hw/net/virtio-net.c +6 -0
index f50235b5d6..02033be748 100644
@@ -3055,6 +3055,9 @@ static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
      if (idx != VIRTIO_CONFIG_IRQ_IDX) {
          return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
      }
+    if (idx == VIRTIO_CONFIG_IRQ_IDX) {
+        return vhost_net_config_pending(get_vhost_net(nc->peer), idx);
+   }
      return false;
  }
 
@@ -3067,6 +3070,9 @@ static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
      if (idx != VIRTIO_CONFIG_IRQ_IDX) {
          vhost_net_virtqueue_mask(get_vhost_net(nc->peer), vdev, idx, mask);
      }
+    if (idx == VIRTIO_CONFIG_IRQ_IDX) {
+        vhost_net_config_mask(get_vhost_net(nc->peer), vdev, mask);
+     }
  }
 
  static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
hw/virtio/vhost.c +0 -0
index e2163a0d63..3b05f09d98 100644

hw/virtio/vhost.c line 24

Quoted 4 lines
hw/virtio/vhost.c +1 -0
@@ -21,6 +21,7 @@
  #include "qemu/error-report.h"
  #include "qemu/memfd.h"
  #include "standard-headers/linux/vhost_types.h"
+#include "standard-headers/linux/virtio_net.h"

Inclusion of device specific header in the general vhost code looks like a layer violation.

hw/virtio/vhost.c bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n) · line 1511

Quoted 10 lines
hw/virtio/vhost.c +3 -0
  #include "exec/address-spaces.h"
  #include "hw/virtio/virtio-bus.h"
  #include "hw/virtio/virtio-access.h"
@@ -1505,6 +1506,16 @@ bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n)
      return event_notifier_test_and_clear(&vq->masked_notifier);
  }
 
+bool vhost_config_pending(struct vhost_dev *hdev, int n)
+{
+    assert(hdev->vhost_ops);

I think we can remove this.

hw/virtio/vhost.c bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n) · line 1513

Quoted 2 lines
hw/virtio/vhost.c +2 -0
+    VirtIODevice *vdev = hdev->vdev;
+    if ((hdev->started == false) ||

We don't check this in vhost_virtqueue_pending(), any reason to do this?

hw/virtio/vhost.c bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n) · line 1514

Quoted 1 line
hw/virtio/vhost.c +1 -0
+        (hdev->vhost_ops->vhost_set_config_call == NULL)) {

I think we need first check what happens if we try to down the link for vhost-vdpa network backend in the monitor. (Or should we block that from the monitor?)

Then we can decided whether or not we need this.

Quoted 23 lines
hw/virtio/vhost.c +16 -0
+        return false;
+    }
+    return event_notifier_test_and_clear(&vdev->masked_config_notifier);
+}
  /* Mask/unmask events from this vq. */
  void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
                           bool mask)
@@ -1529,6 +1540,30 @@ void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
          VHOST_OPS_DEBUG("vhost_set_vring_call failed");
      }
  }
+void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev,
+                         bool mask)
+{
+   int fd;
+   int r;
+   EventNotifier *masked_config_notifier = &vdev->masked_config_notifier;
+   EventNotifier *config_notifier = &vdev->config_notifier;
+   assert(hdev->vhost_ops);
+
+   if ((hdev->started == false) ||
+        (hdev->vhost_ops->vhost_set_config_call == NULL)) {
+        return ;

hw/virtio/vhost.c void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n, · line 1564

Quoted 10 lines
hw/virtio/vhost.c +10 -0
+    }
+    if (mask) {
+        assert(vdev->use_guest_notifier_mask);
+        fd = event_notifier_get_fd(masked_config_notifier);
+    } else {
+        fd = event_notifier_get_fd(config_notifier);
+    }
+   r = hdev->vhost_ops->vhost_set_config_call(hdev, &fd);
+   if (r < 0) {
+        error_report("vhost_set_config_call failed");

VHOST_OPS_DEBUG() please.

Quoted 16 lines
hw/virtio/vhost.c +3 -0
+    }
+}
 
  uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
                              uint64_t features)
@@ -1708,6 +1743,7 @@ int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
  int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
  {
      int i, r;
+    int fd = 0;
 
      /* should only be called after backend is connected */
      assert(hdev->vhost_ops);
@@ -1739,7 +1775,14 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
              goto fail_vq;
          }

hw/virtio/vhost.c int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) · line 1785

Quoted 10 lines
hw/virtio/vhost.c +8 -1
      }
-
+    r = event_notifier_init(&vdev->masked_config_notifier, 0);
+    if (r < 0) {
+        return r;
+    }
+    event_notifier_test_and_clear(&vdev->masked_config_notifier);
+    if (!vdev->use_guest_notifier_mask) {
+        vhost_config_mask(hdev, vdev, true);
+    }

Let's leave a new line for separating the logical blocks.

hw/virtio/vhost.c int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) · line 1821

Quoted 10 lines
hw/virtio/vhost.c +3 -0
      if (hdev->log_enabled) {
          uint64_t log_base;
 
@@ -1773,6 +1816,19 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
              vhost_device_iotlb_miss(hdev, vq->used_phys, true);
          }
      }
+   if (!(hdev->features & (0x1ULL << VIRTIO_NET_F_STATUS))) {
+        return 0;
+    }

Let's move this to vhost-net instead of doing it in the general vhost code.

Or we can even remove this, otherwise it will introduce some burden if we need to implement other features that depends on the config interrupt for vhost-net/vDPA.

hw/virtio/vhost.c int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) · line 1826

Quoted 5 lines
hw/virtio/vhost.c +5 -0
+    if (hdev->vhost_ops->vhost_set_config_call) {
+        fd = event_notifier_get_fd(&vdev->config_notifier);
+        r = hdev->vhost_ops->vhost_set_config_call(hdev, &fd);
+        if (!r) {
+            event_notifier_set(&vdev->config_notifier);

Any reason you need to trigger the config interrupt here?

Quoted 9 lines
hw/virtio/vhost.c +5 -0
+        }
+        if (r) {
+            goto fail_log;
+        }
+    }
      return 0;
  fail_log:
      vhost_log_put(hdev, false);

hw/virtio/vhost.c fail_features: · line 1859

Quoted 10 lines
hw/virtio/vhost.c +3 -1
@@ -1795,10 +1851,18 @@ fail_features:
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
  {
      int i;
+    int fd;
 
      /* should only be called after backend is connected */
      assert(hdev->vhost_ops);
-
+    event_notifier_test_and_clear(&vdev->masked_config_notifier);
+    event_notifier_test_and_clear(&vdev->config_notifier);

If we do this in the start, there's no need for such cleanup here.

hw/virtio/vhost.c fail_features: · line 1860

Quoted 1 line
hw/virtio/vhost.c +1 -0
+    if ((hdev->features & (0x1ULL << VIRTIO_NET_F_STATUS))) {

Let's move this to vhost-net or I'd rather remove this.

Thanks

Quoted 102 lines
hw/virtio/vhost.c +5 -0
+        if (hdev->vhost_ops->vhost_set_config_call) {
+            fd = -1;
+            hdev->vhost_ops->vhost_set_config_call(hdev, &fd);
+        }
+    }
      if (hdev->vhost_ops->vhost_dev_start) {
          hdev->vhost_ops->vhost_dev_start(hdev, false);
      }
hw/virtio/virtio.c +20 -3
index c5d786bb5e..09ed3f67d9 100644
@@ -3506,14 +3506,27 @@ static void virtio_queue_guest_notifier_read(EventNotifier *n)
  }
 
 
+static void virtio_config_read(EventNotifier *n)
+{
+    VirtIODevice *vdev = container_of(n, VirtIODevice, config_notifier);
+
+    if (event_notifier_test_and_clear(n)) {
+        virtio_notify_config(vdev);
+    }
+}
  void virtio_set_notifier_fd_handler(VirtIODevice *vdev, int queue_no,
                                 bool assign, bool with_irqfd)
  {
      EventNotifier *e ;
      EventNotifierHandler *handler;
-    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
-    e = &vq->guest_notifier;
-    handler = virtio_queue_guest_notifier_read;
+    if (queue_no != VIRTIO_CONFIG_IRQ_IDX) {
+        VirtQueue *vq = virtio_get_queue(vdev, queue_no);
+        e = &vq->guest_notifier;
+        handler = virtio_queue_guest_notifier_read;
+    } else {
+        e = &vdev->config_notifier;
+        handler = virtio_config_read;
+    }
      if (assign && !with_irqfd) {
          event_notifier_set_handler(e, handler);
      } else {
@@ -3599,6 +3612,10 @@ EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
      return &vq->host_notifier;
  }
 
+EventNotifier *virtio_get_config_notifier(VirtIODevice *vdev)
+{
+    return &vdev->config_notifier;
+}
  void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled)
  {
      vq->host_notifier_enabled = enabled;
include/hw/virtio/vhost.h +2 -0
index 4a8bc75415..b8814ece32 100644
@@ -108,6 +108,8 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
  int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
+bool vhost_config_pending(struct vhost_dev *hdev, int n);
+void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev, bool mask);
 
  /* Test and clear masked event pending status.
   * Should be called after unmask to avoid losing events.
include/hw/virtio/virtio.h +3 -0
index 447899dea5..5856517d43 100644
@@ -110,6 +110,8 @@ struct VirtIODevice
      bool use_guest_notifier_mask;
      AddressSpace *dma_as;
      QLIST_HEAD(, VirtQueue) *vector_queues;
+    EventNotifier config_notifier;
+    EventNotifier masked_config_notifier;
  };
 
  struct VirtioDeviceClass {
@@ -317,6 +319,7 @@ int virtio_device_grab_ioeventfd(VirtIODevice *vdev);
  void virtio_device_release_ioeventfd(VirtIODevice *vdev);
  bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev);
  EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
+EventNotifier *virtio_get_config_notifier(VirtIODevice *vdev);
  void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled);
  void virtio_queue_host_notifier_read(EventNotifier *n);
  void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
include/net/vhost_net.h +3 -0
index 172b0051d8..0d38c97c94 100644
@@ -36,6 +36,9 @@ int vhost_net_set_config(struct vhost_net *net, const uint8_t *data,
  bool vhost_net_virtqueue_pending(VHostNetState *net, int n);
  void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
                                int idx, bool mask);
+bool vhost_net_config_pending(VHostNetState *net, int n);
+void vhost_net_config_mask(VHostNetState *net, VirtIODevice *dev,
+                              bool mask);
  int vhost_net_notify_migration_done(VHostNetState *net, char* mac_addr);
  VHostNetState *get_vhost_net(NetClientState *nc);
 
author
#13 lore raw

Re: [PATCH v7 06/10] vhost:add support for configure interrupt

Quoted 7 lines · Jason Wang
Quoted 2 lines · Cindy Lu

Add configure notifier support in vhost and virtio driver When backend support VIRTIO_NET_F_STATUS,

Quoted 8 lines

So config interrupt is the basic facility of the virtio device. We need to make the code not specific to this feature.

But we can leave the specific device (like virtio-net, or vhost-net) to decide whether or not it can be used.

For net, it is also used by guest announcement.

sure, I will rewrite this part

Quoted 97 lines
Quoted 65 lines

setup the configure
interrupt function in vhost_dev_start and release the related resource when vhost_dev_stop

---
  hw/net/vhost_net.c         |  9 +++++
  hw/net/virtio-net.c        |  6 ++++
  hw/virtio/vhost.c          | 68 ++++++++++++++++++++++++++++++++++++--
  hw/virtio/virtio.c         | 23 +++++++++++--
  include/hw/virtio/vhost.h  |  2 ++
  include/hw/virtio/virtio.h |  3 ++
  include/net/vhost_net.h    |  3 ++
  7 files changed, 109 insertions(+), 5 deletions(-)
hw/net/vhost_net.c +9 -0
index 24d555e764..5d0c35f18d 100644
@@ -426,6 +426,15 @@ void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
      vhost_virtqueue_mask(&net->dev, dev, idx, mask);
  }
 
+bool vhost_net_config_pending(VHostNetState *net, int idx)
+{
+    return vhost_config_pending(&net->dev, idx);
+}
+void vhost_net_config_mask(VHostNetState *net, VirtIODevice *dev,
+                              bool mask)
+{
+    vhost_config_mask(&net->dev, dev, mask);
+}
  VHostNetState *get_vhost_net(NetClientState *nc)
  {
      VHostNetState *vhost_net = 0;
hw/net/virtio-net.c +6 -0
index f50235b5d6..02033be748 100644
@@ -3055,6 +3055,9 @@ static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
      if (idx != VIRTIO_CONFIG_IRQ_IDX) {
          return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
      }
+    if (idx == VIRTIO_CONFIG_IRQ_IDX) {
+        return vhost_net_config_pending(get_vhost_net(nc->peer), idx);
+   }
      return false;
  }
 
@@ -3067,6 +3070,9 @@ static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
      if (idx != VIRTIO_CONFIG_IRQ_IDX) {
          vhost_net_virtqueue_mask(get_vhost_net(nc->peer), vdev, idx, mask);
      }
+    if (idx == VIRTIO_CONFIG_IRQ_IDX) {
+        vhost_net_config_mask(get_vhost_net(nc->peer), vdev, mask);
+     }
  }
 
  static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
hw/virtio/vhost.c +0 -0
index e2163a0d63..3b05f09d98 100644

hw/virtio/vhost.c line 24

Quoted 4 lines
hw/virtio/vhost.c +1 -0
@@ -21,6 +21,7 @@
  #include "qemu/error-report.h"
  #include "qemu/memfd.h"
  #include "standard-headers/linux/vhost_types.h"
+#include "standard-headers/linux/virtio_net.h"

Inclusion of device specific header in the general vhost code looks like a layer violation.

hw/virtio/vhost.c bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n) · line 1511

Quoted 10 lines
hw/virtio/vhost.c +3 -0
  #include "exec/address-spaces.h"
  #include "hw/virtio/virtio-bus.h"
  #include "hw/virtio/virtio-access.h"
@@ -1505,6 +1506,16 @@ bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n)
      return event_notifier_test_and_clear(&vq->masked_notifier);
  }
 
+bool vhost_config_pending(struct vhost_dev *hdev, int n)
+{
+    assert(hdev->vhost_ops);

I think we can remove this.

hw/virtio/vhost.c bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n) · line 1513

Quoted 2 lines
hw/virtio/vhost.c +2 -0
+    VirtIODevice *vdev = hdev->vdev;
+    if ((hdev->started == false) ||

We don't check this in vhost_virtqueue_pending(), any reason to do this?

Quoted 10 lines

hw/virtio/vhost.c bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n) · line 1514

Quoted 1 line
hw/virtio/vhost.c +1 -0
+        (hdev->vhost_ops->vhost_set_config_call == NULL)) {

I think we need first check what happens if we try to down the link for vhost-vdpa network backend in the monitor. (Or should we block that from the monitor?)

Then we can decided whether or not we need this.

I will remove this part

Quoted 28 lines
Quoted 28 lines
hw/virtio/vhost.c +21 -0
+        return false;
+    }
+    return event_notifier_test_and_clear(&vdev->masked_config_notifier);
+}
  /* Mask/unmask events from this vq. */
  void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
                           bool mask)
@@ -1529,6 +1540,30 @@ void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
          VHOST_OPS_DEBUG("vhost_set_vring_call failed");
      }
  }
+void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev,
+                         bool mask)
+{
+   int fd;
+   int r;
+   EventNotifier *masked_config_notifier = &vdev->masked_config_notifier;
+   EventNotifier *config_notifier = &vdev->config_notifier;
+   assert(hdev->vhost_ops);
+
+   if ((hdev->started == false) ||
+        (hdev->vhost_ops->vhost_set_config_call == NULL)) {
+        return ;
+    }
+    if (mask) {
+        assert(vdev->use_guest_notifier_mask);
+        fd = event_notifier_get_fd(masked_config_notifier);
+    } else {
Quoted 9 lines

hw/virtio/vhost.c void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n, · line 1564

Quoted 5 lines
hw/virtio/vhost.c +5 -0
+        fd = event_notifier_get_fd(config_notifier);
+    }
+   r = hdev->vhost_ops->vhost_set_config_call(hdev, &fd);
+   if (r < 0) {
+        error_report("vhost_set_config_call failed");

VHOST_OPS_DEBUG() please.

will fix this

Quoted 22 lines
Quoted 22 lines
hw/virtio/vhost.c +6 -1
+    }
+}
 
  uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
                              uint64_t features)
@@ -1708,6 +1743,7 @@ int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
  int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
  {
      int i, r;
+    int fd = 0;
 
      /* should only be called after backend is connected */
      assert(hdev->vhost_ops);
@@ -1739,7 +1775,14 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
              goto fail_vq;
          }
      }
-
+    r = event_notifier_init(&vdev->masked_config_notifier, 0);
+    if (r < 0) {
+        return r;
Quoted 10 lines

hw/virtio/vhost.c int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) · line 1785

Quoted 5 lines
hw/virtio/vhost.c +5 -0
+    }
+    event_notifier_test_and_clear(&vdev->masked_config_notifier);
+    if (!vdev->use_guest_notifier_mask) {
+        vhost_config_mask(hdev, vdev, true);
+    }

Let's leave a new line for separating the logical blocks.

will fix this

Quoted 14 lines

hw/virtio/vhost.c int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) · line 1821

Quoted 10 lines
hw/virtio/vhost.c +3 -0
      if (hdev->log_enabled) {
          uint64_t log_base;
 
@@ -1773,6 +1816,19 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
              vhost_device_iotlb_miss(hdev, vq->used_phys, true);
          }
      }
+   if (!(hdev->features & (0x1ULL << VIRTIO_NET_F_STATUS))) {
+        return 0;
+    }

Let's move this to vhost-net instead of doing it in the general vhost code.

Quoted 5 lines

Or we can even remove this, otherwise it will introduce some burden if we need to implement other features that depends on the config interrupt for vhost-net/vDPA.

will fix this

Quoted 34 lines

hw/virtio/vhost.c int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) · line 1826

Quoted 5 lines
hw/virtio/vhost.c +5 -0
+    if (hdev->vhost_ops->vhost_set_config_call) {
+        fd = event_notifier_get_fd(&vdev->config_notifier);
+        r = hdev->vhost_ops->vhost_set_config_call(hdev, &fd);
+        if (!r) {
+            event_notifier_set(&vdev->config_notifier);

Any reason you need to trigger the config interrupt here?

Quoted 9 lines
hw/virtio/vhost.c +5 -0
+        }
+        if (r) {
+            goto fail_log;
+        }
+    }
      return 0;
  fail_log:
      vhost_log_put(hdev, false);

hw/virtio/vhost.c fail_features: · line 1859

Quoted 10 lines
hw/virtio/vhost.c +3 -1
@@ -1795,10 +1851,18 @@ fail_features:
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
  {
      int i;
+    int fd;
 
      /* should only be called after backend is connected */
      assert(hdev->vhost_ops);
-
+    event_notifier_test_and_clear(&vdev->masked_config_notifier);
+    event_notifier_test_and_clear(&vdev->config_notifier);

If we do this in the start, there's no need for such cleanup here.

Quoted 8 lines

hw/virtio/vhost.c fail_features: · line 1860

Quoted 1 line
hw/virtio/vhost.c +1 -0
+    if ((hdev->features & (0x1ULL << VIRTIO_NET_F_STATUS))) {

Let's move this to vhost-net or I'd rather remove this.

Thanks

will fix this

Quoted 103 lines
Quoted 102 lines
hw/virtio/vhost.c +5 -0
+        if (hdev->vhost_ops->vhost_set_config_call) {
+            fd = -1;
+            hdev->vhost_ops->vhost_set_config_call(hdev, &fd);
+        }
+    }
      if (hdev->vhost_ops->vhost_dev_start) {
          hdev->vhost_ops->vhost_dev_start(hdev, false);
      }
hw/virtio/virtio.c +20 -3
index c5d786bb5e..09ed3f67d9 100644
@@ -3506,14 +3506,27 @@ static void virtio_queue_guest_notifier_read(EventNotifier *n)
  }
 
 
+static void virtio_config_read(EventNotifier *n)
+{
+    VirtIODevice *vdev = container_of(n, VirtIODevice, config_notifier);
+
+    if (event_notifier_test_and_clear(n)) {
+        virtio_notify_config(vdev);
+    }
+}
  void virtio_set_notifier_fd_handler(VirtIODevice *vdev, int queue_no,
                                 bool assign, bool with_irqfd)
  {
      EventNotifier *e ;
      EventNotifierHandler *handler;
-    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
-    e = &vq->guest_notifier;
-    handler = virtio_queue_guest_notifier_read;
+    if (queue_no != VIRTIO_CONFIG_IRQ_IDX) {
+        VirtQueue *vq = virtio_get_queue(vdev, queue_no);
+        e = &vq->guest_notifier;
+        handler = virtio_queue_guest_notifier_read;
+    } else {
+        e = &vdev->config_notifier;
+        handler = virtio_config_read;
+    }
      if (assign && !with_irqfd) {
          event_notifier_set_handler(e, handler);
      } else {
@@ -3599,6 +3612,10 @@ EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
      return &vq->host_notifier;
  }
 
+EventNotifier *virtio_get_config_notifier(VirtIODevice *vdev)
+{
+    return &vdev->config_notifier;
+}
  void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled)
  {
      vq->host_notifier_enabled = enabled;
include/hw/virtio/vhost.h +2 -0
index 4a8bc75415..b8814ece32 100644
@@ -108,6 +108,8 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
  int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
+bool vhost_config_pending(struct vhost_dev *hdev, int n);
+void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev, bool mask);
 
  /* Test and clear masked event pending status.
   * Should be called after unmask to avoid losing events.
include/hw/virtio/virtio.h +3 -0
index 447899dea5..5856517d43 100644
@@ -110,6 +110,8 @@ struct VirtIODevice
      bool use_guest_notifier_mask;
      AddressSpace *dma_as;
      QLIST_HEAD(, VirtQueue) *vector_queues;
+    EventNotifier config_notifier;
+    EventNotifier masked_config_notifier;
  };
 
  struct VirtioDeviceClass {
@@ -317,6 +319,7 @@ int virtio_device_grab_ioeventfd(VirtIODevice *vdev);
  void virtio_device_release_ioeventfd(VirtIODevice *vdev);
  bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev);
  EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
+EventNotifier *virtio_get_config_notifier(VirtIODevice *vdev);
  void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled);
  void virtio_queue_host_notifier_read(EventNotifier *n);
  void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
include/net/vhost_net.h +3 -0
index 172b0051d8..0d38c97c94 100644
@@ -36,6 +36,9 @@ int vhost_net_set_config(struct vhost_net *net, const uint8_t *data,
  bool vhost_net_virtqueue_pending(VHostNetState *net, int n);
  void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
                                int idx, bool mask);
+bool vhost_net_config_pending(VHostNetState *net, int n);
+void vhost_net_config_mask(VHostNetState *net, VirtIODevice *dev,
+                              bool mask);
  int vhost_net_notify_migration_done(VHostNetState *net, char* mac_addr);
  VHostNetState *get_vhost_net(NetClientState *nc);
 
author
#14 lore raw

PATCH v7 7/10 virtio-mmio: add support for configure interrupt

In reply to Cindy Lu

Add configure interrupt support for virtio-mmio bus. This interrupt will working while backend is vhost-vdpa

---
 hw/virtio/virtio-mmio.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
hw/virtio/virtio-mmio.c +26 -0
index 13772d52bb..423267d51c 100644
@@ -670,7 +670,26 @@ static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign,
 
     return 0;
 }
+static int virtio_mmio_set_config_notifier(DeviceState *d, bool assign)
+{
+    VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
 
+    EventNotifier *notifier = virtio_get_config_notifier(vdev);
+    int r = 0;
+    if (assign) {
+        r = event_notifier_init(notifier, 0);
+        virtio_set_notifier_fd_handler(vdev, -1, true, false);
+    } else {
+        virtio_set_notifier_fd_handler(vdev, -1, false, false);
+        event_notifier_cleanup(notifier);
+    }
+    if (vdc->guest_notifier_mask && vdev->use_guest_notifier_mask) {
+        vdc->guest_notifier_mask(vdev, -1, !assign);
+    }
+    return r;
+}
 static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs,
                                            bool assign)
 {
@@ -692,8 +711,15 @@ static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs,
             goto assign_error;
         }
     }
+    r = virtio_mmio_set_config_notifier(d, assign);
+    if (r < 0) {
+        goto config_assign_error;
+    }
 
     return 0;
+config_assign_error:
+    assert(assign);
+    r = virtio_mmio_set_config_notifier(d, false);
 
 assign_error:
     /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
Signature · 1 line
2.21.3
#15 lore raw

Re: [PATCH v7 07/10] virtio-mmio: add support for configure interrupt

Quoted 16 lines · Cindy Lu

Add configure interrupt support for virtio-mmio bus. This interrupt will working while backend is vhost-vdpa

---
  hw/virtio/virtio-mmio.c | 26 ++++++++++++++++++++++++++
  1 file changed, 26 insertions(+)
hw/virtio/virtio-mmio.c +0 -0
index 13772d52bb..423267d51c 100644
@@ -670,7 +670,26 @@ static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign,
 
      return 0;
  }

hw/virtio/virtio-mmio.c static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign, · line 682

Quoted 10 lines
hw/virtio/virtio-mmio.c +9 -0
+static int virtio_mmio_set_config_notifier(DeviceState *d, bool assign)
+{
+    VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
 
+    EventNotifier *notifier = virtio_get_config_notifier(vdev);
+    int r = 0;
+    if (assign) {
+        r = event_notifier_init(notifier, 0);

The return value is ignored.

hw/virtio/virtio-mmio.c static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign, · line 683

Quoted 1 line
hw/virtio/virtio-mmio.c +1 -0
+        virtio_set_notifier_fd_handler(vdev, -1, true, false);

You'd better use the macro you introduced in patch 1 here?

Quoted 15 lines
hw/virtio/virtio-mmio.c +9 -0
+    } else {
+        virtio_set_notifier_fd_handler(vdev, -1, false, false);
+        event_notifier_cleanup(notifier);
+    }
+    if (vdc->guest_notifier_mask && vdev->use_guest_notifier_mask) {
+        vdc->guest_notifier_mask(vdev, -1, !assign);
+    }
+    return r;
+}
  static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs,
                                             bool assign)
  {
@@ -692,8 +711,15 @@ static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs,
              goto assign_error;
          }

hw/virtio/virtio-mmio.c static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs, · line 722

Quoted 10 lines
hw/virtio/virtio-mmio.c +7 -0
      }
+    r = virtio_mmio_set_config_notifier(d, assign);
+    if (r < 0) {
+        goto config_assign_error;
+    }
 
      return 0;
+config_assign_error:
+    assert(assign);
+    r = virtio_mmio_set_config_notifier(d, false);

This looks wired. We only have a single configure interrupt, so assign fails should mean unassigned?

Thanks

Quoted 3 lines
hw/virtio/virtio-mmio.c +0 -0
  assign_error:
      /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
author
#16 lore raw

Re: [PATCH v7 07/10] virtio-mmio: add support for configure interrupt

Quoted 25 lines · Jason Wang
Quoted 22 lines · Cindy Lu

Add configure interrupt support for virtio-mmio bus. This interrupt will working while backend is vhost-vdpa

---
  hw/virtio/virtio-mmio.c | 26 ++++++++++++++++++++++++++
  1 file changed, 26 insertions(+)
hw/virtio/virtio-mmio.c +5 -0
index 13772d52bb..423267d51c 100644
@@ -670,7 +670,26 @@ static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign,
 
      return 0;
  }
+static int virtio_mmio_set_config_notifier(DeviceState *d, bool assign)
+{
+    VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
 
Quoted 8 lines

hw/virtio/virtio-mmio.c static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign, · line 682

Quoted 4 lines
hw/virtio/virtio-mmio.c +4 -0
+    EventNotifier *notifier = virtio_get_config_notifier(vdev);
+    int r = 0;
+    if (assign) {
+        r = event_notifier_init(notifier, 0);

The return value is ignored.

will add the check here

Quoted 6 lines

hw/virtio/virtio-mmio.c static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign, · line 683

Quoted 2 lines
hw/virtio/virtio-mmio.c +1 -0
+        virtio_set_notifier_fd_handler(vdev, -1, true, false);

You'd better use the macro you introduced in patch 1 here?

sorry, Seems I missed this, I will correct this

Quoted 31 lines
Quoted 16 lines
hw/virtio/virtio-mmio.c +9 -0
+    } else {
+        virtio_set_notifier_fd_handler(vdev, -1, false, false);
+        event_notifier_cleanup(notifier);
+    }
+    if (vdc->guest_notifier_mask && vdev->use_guest_notifier_mask) {
+        vdc->guest_notifier_mask(vdev, -1, !assign);
+    }
+    return r;
+}
  static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs,
                                             bool assign)
  {
@@ -692,8 +711,15 @@ static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs,
              goto assign_error;
          }

hw/virtio/virtio-mmio.c static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs, · line 722

Quoted 10 lines
hw/virtio/virtio-mmio.c +7 -0
      }
+    r = virtio_mmio_set_config_notifier(d, assign);
+    if (r < 0) {
+        goto config_assign_error;
+    }
 
      return 0;
+config_assign_error:
+    assert(assign);
+    r = virtio_mmio_set_config_notifier(d, false);

This looks wired. We only have a single configure interrupt, so assign fails should mean unassigned?

Quoted 2 lines

Thanks

sure Will correct this

Quoted 4 lines
Quoted 3 lines
hw/virtio/virtio-mmio.c +0 -0
  assign_error:
      /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
author
#17 lore raw

PATCH v7 8/10 virtio-pci: decouple virtqueue from kvm_virtio_pci_vector_use

In reply to Cindy Lu

inorder to support configure interrupt, we need to decouple virtqueue from vector use and vector release function this patch introduce vector_release_one and vector_use_one to support one vector.

---
 hw/virtio/virtio-pci.c | 122 ++++++++++++++++++++---------------------
 1 file changed, 61 insertions(+), 61 deletions(-)
hw/virtio/virtio-pci.c +61 -61
index 6a4ef413a4..f863c89de6 100644
@@ -666,7 +666,6 @@ static uint32_t virtio_read_config(PCIDevice *pci_dev,
 }
 
 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
-                                        unsigned int queue_no,
                                         unsigned int vector)
 {
     VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
@@ -710,85 +709,86 @@ static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
     ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
     assert(ret == 0);
 }
-
-static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
+static int virtio_pci_get_notifier(VirtIOPCIProxy *proxy, int queue_no,
+                                      EventNotifier **n, unsigned int *vector)
 {
     PCIDevice *dev = &proxy->pci_dev;
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
-    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
-    unsigned int vector;
-    int ret, queue_no;
     VirtQueue *vq;
-    EventNotifier *n;
-    for (queue_no = 0; queue_no < nvqs; queue_no++) {
+
+    if (queue_no == VIRTIO_CONFIG_IRQ_IDX) {
+        return -1;
+    } else {
         if (!virtio_queue_get_num(vdev, queue_no)) {
-            break;
-        }
-        vector = virtio_queue_vector(vdev, queue_no);
-        if (vector >= msix_nr_vectors_allocated(dev)) {
-            continue;
-        }
-        ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector);
-        if (ret < 0) {
-            goto undo;
-        }
-        /* If guest supports masking, set up irqfd now.
-         * Otherwise, delay until unmasked in the frontend.
-         */
-        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            vq = virtio_get_queue(vdev, queue_no);
-            n = virtio_queue_get_guest_notifier(vq);
-            ret = kvm_virtio_pci_irqfd_use(proxy, n, vector);
-            if (ret < 0) {
-                kvm_virtio_pci_vq_vector_release(proxy, vector);
-                goto undo;
-            }
+            return -1;
         }
+        *vector = virtio_queue_vector(vdev, queue_no);
+        vq = virtio_get_queue(vdev, queue_no);
+        *n = virtio_queue_get_guest_notifier(vq);
+    }
+    if (*vector >= msix_nr_vectors_allocated(dev)) {
+        return -1;
     }
     return 0;
+}
 
+static int kvm_virtio_pci_vector_use_one(VirtIOPCIProxy *proxy, int queue_no)
+{
+    unsigned int vector;
+    int ret;
+    EventNotifier *n;
+    ret = virtio_pci_get_notifier(proxy, queue_no, &n, &vector);
+    if (ret < 0) {
+        return ret;
+    }
+    ret = kvm_virtio_pci_vq_vector_use(proxy, vector);
+    if (ret < 0) {
+        goto undo;
+    }
+    ret = kvm_virtio_pci_irqfd_use(proxy, n, vector);
+    if (ret < 0) {
+        goto undo;
+    }
+    return 0;
 undo:
-    while (--queue_no >= 0) {
-        vector = virtio_queue_vector(vdev, queue_no);
-        if (vector >= msix_nr_vectors_allocated(dev)) {
-            continue;
-        }
-        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            vq = virtio_get_queue(vdev, queue_no);
-            n = virtio_queue_get_guest_notifier(vq);
-            kvm_virtio_pci_irqfd_release(proxy, n, vector);
-        }
-        kvm_virtio_pci_vq_vector_release(proxy, vector);
+    kvm_virtio_pci_irqfd_release(proxy, n, vector);
+    return ret;
+}
+static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
+{
+    int queue_no;
+    int ret = 0;
+    for (queue_no = 0; queue_no < nvqs; queue_no++) {
+        ret = kvm_virtio_pci_vector_use_one(proxy, queue_no);
     }
     return ret;
 }
 
-static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
+
+static void kvm_virtio_pci_vector_release_one(VirtIOPCIProxy *proxy,
+                        int queue_no)
 {
-    PCIDevice *dev = &proxy->pci_dev;
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
     unsigned int vector;
-    int queue_no;
-    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
-    VirtQueue *vq;
     EventNotifier *n;
+    int ret;
+    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
+    ret = virtio_pci_get_notifier(proxy, queue_no, &n, &vector);
+    if (ret < 0) {
+        return;
+    }
+
+    if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
+        kvm_virtio_pci_irqfd_release(proxy, n, vector);
+    }
+    kvm_virtio_pci_vq_vector_release(proxy, vector);
+}
+static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
+{
+    int queue_no;
+
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
-        if (!virtio_queue_get_num(vdev, queue_no)) {
-            break;
-        }
-        vector = virtio_queue_vector(vdev, queue_no);
-        if (vector >= msix_nr_vectors_allocated(dev)) {
-            continue;
-        }
-        /* If guest supports masking, clean up irqfd now.
-         * Otherwise, it was cleaned when masked in the frontend.
-         */
-        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            vq = virtio_get_queue(vdev, queue_no);
-            n = virtio_queue_get_guest_notifier(vq);
-            kvm_virtio_pci_irqfd_release(proxy, n, vector);
-        }
-        kvm_virtio_pci_vq_vector_release(proxy, vector);
+        kvm_virtio_pci_vector_release_one(proxy, queue_no);
     }
 }
 
Signature · 1 line
2.21.3
#18 lore raw

Re: [PATCH v7 08/10] virtio-pci: decouple virtqueue from kvm_virtio_pci_vector_use

Quoted 1 line · Cindy Lu

inorder

s/inorder/In order/

Quoted 6 lines

to support configure interrupt, we need to decouple virtqueue from vector use and vector release function this patch introduce vector_release_one and vector_use_one to support one vector.

I think we need to reorder the patches to let such decoupling comes first in this series.

Quoted 63 lines
---
  hw/virtio/virtio-pci.c | 122 ++++++++++++++++++++---------------------
  1 file changed, 61 insertions(+), 61 deletions(-)
hw/virtio/virtio-pci.c +9 -29
index 6a4ef413a4..f863c89de6 100644
@@ -666,7 +666,6 @@ static uint32_t virtio_read_config(PCIDevice *pci_dev,
  }
 
  static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
-                                        unsigned int queue_no,
                                          unsigned int vector)
  {
      VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
@@ -710,85 +709,86 @@ static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
      ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
      assert(ret == 0);
  }
-
-static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
+static int virtio_pci_get_notifier(VirtIOPCIProxy *proxy, int queue_no,
+                                      EventNotifier **n, unsigned int *vector)
  {
      PCIDevice *dev = &proxy->pci_dev;
      VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
-    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
-    unsigned int vector;
-    int ret, queue_no;
      VirtQueue *vq;
-    EventNotifier *n;
-    for (queue_no = 0; queue_no < nvqs; queue_no++) {
+
+    if (queue_no == VIRTIO_CONFIG_IRQ_IDX) {
+        return -1;
+    } else {
          if (!virtio_queue_get_num(vdev, queue_no)) {
-            break;
-        }
-        vector = virtio_queue_vector(vdev, queue_no);
-        if (vector >= msix_nr_vectors_allocated(dev)) {
-            continue;
-        }
-        ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector);
-        if (ret < 0) {
-            goto undo;
-        }
-        /* If guest supports masking, set up irqfd now.
-         * Otherwise, delay until unmasked in the frontend.
-         */
-        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            vq = virtio_get_queue(vdev, queue_no);
-            n = virtio_queue_get_guest_notifier(vq);
-            ret = kvm_virtio_pci_irqfd_use(proxy, n, vector);
-            if (ret < 0) {
-                kvm_virtio_pci_vq_vector_release(proxy, vector);
-                goto undo;
-            }
+            return -1;
          }
+        *vector = virtio_queue_vector(vdev, queue_no);
+        vq = virtio_get_queue(vdev, queue_no);

hw/virtio/virtio-pci.c static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy, · line 736

Quoted 10 lines
hw/virtio/virtio-pci.c +7 -0
+        *n = virtio_queue_get_guest_notifier(vq);
+    }
+    if (*vector >= msix_nr_vectors_allocated(dev)) {
+        return -1;
      }
      return 0;
+}
 
+static int kvm_virtio_pci_vector_use_one(VirtIOPCIProxy *proxy, int queue_no)
+{

To ease the reviewer, let's separate this patch into two.

1) factoring out the core logic
2) decouple the vq

Thanks

Quoted 90 lines
hw/virtio/virtio-pci.c +45 -32
+    unsigned int vector;
+    int ret;
+    EventNotifier *n;
+    ret = virtio_pci_get_notifier(proxy, queue_no, &n, &vector);
+    if (ret < 0) {
+        return ret;
+    }
+    ret = kvm_virtio_pci_vq_vector_use(proxy, vector);
+    if (ret < 0) {
+        goto undo;
+    }
+    ret = kvm_virtio_pci_irqfd_use(proxy, n, vector);
+    if (ret < 0) {
+        goto undo;
+    }
+    return 0;
  undo:
-    while (--queue_no >= 0) {
-        vector = virtio_queue_vector(vdev, queue_no);
-        if (vector >= msix_nr_vectors_allocated(dev)) {
-            continue;
-        }
-        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            vq = virtio_get_queue(vdev, queue_no);
-            n = virtio_queue_get_guest_notifier(vq);
-            kvm_virtio_pci_irqfd_release(proxy, n, vector);
-        }
-        kvm_virtio_pci_vq_vector_release(proxy, vector);
+    kvm_virtio_pci_irqfd_release(proxy, n, vector);
+    return ret;
+}
+static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
+{
+    int queue_no;
+    int ret = 0;
+    for (queue_no = 0; queue_no < nvqs; queue_no++) {
+        ret = kvm_virtio_pci_vector_use_one(proxy, queue_no);
      }
      return ret;
  }
 
-static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
+
+static void kvm_virtio_pci_vector_release_one(VirtIOPCIProxy *proxy,
+                        int queue_no)
  {
-    PCIDevice *dev = &proxy->pci_dev;
      VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
      unsigned int vector;
-    int queue_no;
-    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
-    VirtQueue *vq;
      EventNotifier *n;
+    int ret;
+    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
+    ret = virtio_pci_get_notifier(proxy, queue_no, &n, &vector);
+    if (ret < 0) {
+        return;
+    }
+
+    if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
+        kvm_virtio_pci_irqfd_release(proxy, n, vector);
+    }
+    kvm_virtio_pci_vq_vector_release(proxy, vector);
+}
+static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
+{
+    int queue_no;
+
      for (queue_no = 0; queue_no < nvqs; queue_no++) {
-        if (!virtio_queue_get_num(vdev, queue_no)) {
-            break;
-        }
-        vector = virtio_queue_vector(vdev, queue_no);
-        if (vector >= msix_nr_vectors_allocated(dev)) {
-            continue;
-        }
-        /* If guest supports masking, clean up irqfd now.
-         * Otherwise, it was cleaned when masked in the frontend.
-         */
-        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            vq = virtio_get_queue(vdev, queue_no);
-            n = virtio_queue_get_guest_notifier(vq);
-            kvm_virtio_pci_irqfd_release(proxy, n, vector);
-        }
-        kvm_virtio_pci_vq_vector_release(proxy, vector);
+        kvm_virtio_pci_vector_release_one(proxy, queue_no);
      }
  }
 
author
#19 lore raw

Re: [PATCH v7 08/10] virtio-pci: decouple virtqueue from kvm_virtio_pci_vector_use

Quoted 98 lines · Jason Wang
Quoted 1 line · Cindy Lu

inorder

s/inorder/In order/

Quoted 6 lines

to support configure interrupt, we need to decouple virtqueue from vector use and vector release function this patch introduce vector_release_one and vector_use_one to support one vector.

I think we need to reorder the patches to let such decoupling comes first in this series.

Quoted 63 lines
---
  hw/virtio/virtio-pci.c | 122 ++++++++++++++++++++---------------------
  1 file changed, 61 insertions(+), 61 deletions(-)
hw/virtio/virtio-pci.c +9 -29
index 6a4ef413a4..f863c89de6 100644
@@ -666,7 +666,6 @@ static uint32_t virtio_read_config(PCIDevice *pci_dev,
  }
 
  static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
-                                        unsigned int queue_no,
                                          unsigned int vector)
  {
      VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
@@ -710,85 +709,86 @@ static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
      ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
      assert(ret == 0);
  }
-
-static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
+static int virtio_pci_get_notifier(VirtIOPCIProxy *proxy, int queue_no,
+                                      EventNotifier **n, unsigned int *vector)
  {
      PCIDevice *dev = &proxy->pci_dev;
      VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
-    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
-    unsigned int vector;
-    int ret, queue_no;
      VirtQueue *vq;
-    EventNotifier *n;
-    for (queue_no = 0; queue_no < nvqs; queue_no++) {
+
+    if (queue_no == VIRTIO_CONFIG_IRQ_IDX) {
+        return -1;
+    } else {
          if (!virtio_queue_get_num(vdev, queue_no)) {
-            break;
-        }
-        vector = virtio_queue_vector(vdev, queue_no);
-        if (vector >= msix_nr_vectors_allocated(dev)) {
-            continue;
-        }
-        ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector);
-        if (ret < 0) {
-            goto undo;
-        }
-        /* If guest supports masking, set up irqfd now.
-         * Otherwise, delay until unmasked in the frontend.
-         */
-        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            vq = virtio_get_queue(vdev, queue_no);
-            n = virtio_queue_get_guest_notifier(vq);
-            ret = kvm_virtio_pci_irqfd_use(proxy, n, vector);
-            if (ret < 0) {
-                kvm_virtio_pci_vq_vector_release(proxy, vector);
-                goto undo;
-            }
+            return -1;
          }
+        *vector = virtio_queue_vector(vdev, queue_no);
+        vq = virtio_get_queue(vdev, queue_no);

hw/virtio/virtio-pci.c static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy, · line 736

Quoted 10 lines
hw/virtio/virtio-pci.c +7 -0
+        *n = virtio_queue_get_guest_notifier(vq);
+    }
+    if (*vector >= msix_nr_vectors_allocated(dev)) {
+        return -1;
      }
      return 0;
+}
 
+static int kvm_virtio_pci_vector_use_one(VirtIOPCIProxy *proxy, int queue_no)
+{

To ease the reviewer, let's separate this patch into two.

Quoted 5 lines

1) factoring out the core logic
2) decouple the vq

Thanks

Thanks Jason, I will change this part

Quoted 92 lines
Quoted 91 lines
hw/virtio/virtio-pci.c +45 -32
+    unsigned int vector;
+    int ret;
+    EventNotifier *n;
+    ret = virtio_pci_get_notifier(proxy, queue_no, &n, &vector);
+    if (ret < 0) {
+        return ret;
+    }
+    ret = kvm_virtio_pci_vq_vector_use(proxy, vector);
+    if (ret < 0) {
+        goto undo;
+    }
+    ret = kvm_virtio_pci_irqfd_use(proxy, n, vector);
+    if (ret < 0) {
+        goto undo;
+    }
+    return 0;
  undo:
-    while (--queue_no >= 0) {
-        vector = virtio_queue_vector(vdev, queue_no);
-        if (vector >= msix_nr_vectors_allocated(dev)) {
-            continue;
-        }
-        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            vq = virtio_get_queue(vdev, queue_no);
-            n = virtio_queue_get_guest_notifier(vq);
-            kvm_virtio_pci_irqfd_release(proxy, n, vector);
-        }
-        kvm_virtio_pci_vq_vector_release(proxy, vector);
+    kvm_virtio_pci_irqfd_release(proxy, n, vector);
+    return ret;
+}
+static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
+{
+    int queue_no;
+    int ret = 0;
+    for (queue_no = 0; queue_no < nvqs; queue_no++) {
+        ret = kvm_virtio_pci_vector_use_one(proxy, queue_no);
      }
      return ret;
  }
 
-static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
+
+static void kvm_virtio_pci_vector_release_one(VirtIOPCIProxy *proxy,
+                        int queue_no)
  {
-    PCIDevice *dev = &proxy->pci_dev;
      VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
      unsigned int vector;
-    int queue_no;
-    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
-    VirtQueue *vq;
      EventNotifier *n;
+    int ret;
+    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
+    ret = virtio_pci_get_notifier(proxy, queue_no, &n, &vector);
+    if (ret < 0) {
+        return;
+    }
+
+    if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
+        kvm_virtio_pci_irqfd_release(proxy, n, vector);
+    }
+    kvm_virtio_pci_vq_vector_release(proxy, vector);
+}
+static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
+{
+    int queue_no;
+
      for (queue_no = 0; queue_no < nvqs; queue_no++) {
-        if (!virtio_queue_get_num(vdev, queue_no)) {
-            break;
-        }
-        vector = virtio_queue_vector(vdev, queue_no);
-        if (vector >= msix_nr_vectors_allocated(dev)) {
-            continue;
-        }
-        /* If guest supports masking, clean up irqfd now.
-         * Otherwise, it was cleaned when masked in the frontend.
-         */
-        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
-            vq = virtio_get_queue(vdev, queue_no);
-            n = virtio_queue_get_guest_notifier(vq);
-            kvm_virtio_pci_irqfd_release(proxy, n, vector);
-        }
-        kvm_virtio_pci_vq_vector_release(proxy, vector);
+        kvm_virtio_pci_vector_release_one(proxy, queue_no);
      }
  }
 
author
#20 lore raw

PATCH v7 9/10 virtio-pci: add support for configure interrupt

In reply to Cindy Lu

Add support for configure interrupt, use kvm_irqfd_assign and set the gsi to kernel. When the configure notifier was eventfd_signal by host kernel, this will finally inject an msix interrupt to guest

---
 hw/virtio/virtio-pci.c | 63 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 60 insertions(+), 3 deletions(-)
hw/virtio/virtio-pci.c +60 -3
index f863c89de6..1e03f11a85 100644
@@ -717,7 +717,8 @@ static int virtio_pci_get_notifier(VirtIOPCIProxy *proxy, int queue_no,
     VirtQueue *vq;
 
     if (queue_no == VIRTIO_CONFIG_IRQ_IDX) {
-        return -1;
+        *n = virtio_get_config_notifier(vdev);
+        *vector = vdev->config_vector;
     } else {
         if (!virtio_queue_get_num(vdev, queue_no)) {
             return -1;
@@ -764,6 +765,10 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
     return ret;
 }
 
+static int kvm_virtio_pci_vector_config_use(VirtIOPCIProxy *proxy)
+{
+    return kvm_virtio_pci_vector_use_one(proxy, VIRTIO_CONFIG_IRQ_IDX);
+}
 
 static void kvm_virtio_pci_vector_release_one(VirtIOPCIProxy *proxy,
                         int queue_no)
@@ -792,6 +797,28 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
     }
 }
 
+static void kvm_virtio_pci_vector_config_release(VirtIOPCIProxy *proxy)
+{
+    kvm_virtio_pci_vector_release_one(proxy, VIRTIO_CONFIG_IRQ_IDX);
+}
+static int virtio_pci_set_config_notifier(DeviceState *d, bool assign)
+{
+    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    EventNotifier *notifier = virtio_get_config_notifier(vdev);
+    int r = 0;
+    if (assign) {
+        r = event_notifier_init(notifier, 0);
+        virtio_set_notifier_fd_handler(vdev, VIRTIO_CONFIG_IRQ_IDX, true, true);
+        kvm_virtio_pci_vector_config_use(proxy);
+    } else {
+        virtio_set_notifier_fd_handler(vdev, VIRTIO_CONFIG_IRQ_IDX,
+                                             false, true);
+        kvm_virtio_pci_vector_config_release(proxy);
+        event_notifier_cleanup(notifier);
+    }
+    return r;
+}
 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
                                        unsigned int queue_no,
                                        unsigned int vector,
@@ -873,9 +900,17 @@ static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
         }
         vq = virtio_vector_next_queue(vq);
     }
-
+    n = virtio_get_config_notifier(vdev);
+    ret = virtio_pci_vq_vector_unmask(proxy, VIRTIO_CONFIG_IRQ_IDX,
+                        vector, msg, n);
+    if (ret < 0) {
+        goto config_undo;
+    }
     return 0;
 
+config_undo:
+    n = virtio_get_config_notifier(vdev);
+    virtio_pci_vq_vector_mask(proxy, VIRTIO_CONFIG_IRQ_IDX, vector, n);
 undo:
     vq = virtio_vector_first_queue(vdev, vector);
     while (vq && unmasked >= 0) {
@@ -909,6 +944,8 @@ static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
         }
         vq = virtio_vector_next_queue(vq);
     }
+    n = virtio_get_config_notifier(vdev);
+    virtio_pci_vq_vector_mask(proxy, VIRTIO_CONFIG_IRQ_IDX, vector, n);
 }
 
 static void virtio_pci_vector_poll(PCIDevice *dev,
@@ -942,6 +979,20 @@ static void virtio_pci_vector_poll(PCIDevice *dev,
             msix_set_pending(dev, vector);
         }
     }
+   /*check for config interrupt*/
+   vector = vdev->config_vector;
+   notifier = virtio_get_config_notifier(vdev);
+   if (vector < vector_start || vector >= vector_end ||
+            !msix_is_masked(dev, vector)) {
+        return;
+   }
+   if (k->guest_notifier_pending) {
+        if (k->guest_notifier_pending(vdev, VIRTIO_CONFIG_IRQ_IDX)) {
+            msix_set_pending(dev, vector);
+        }
+   } else if (event_notifier_test_and_clear(notifier)) {
+        msix_set_pending(dev, vector);
+   }
 }
 
 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
@@ -1002,6 +1053,7 @@ static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
         msix_unset_vector_notifiers(&proxy->pci_dev);
         if (proxy->vector_irqfd) {
             kvm_virtio_pci_vector_release(proxy, nvqs);
+            kvm_virtio_pci_vector_config_release(proxy);
             g_free(proxy->vector_irqfd);
             proxy->vector_irqfd = NULL;
         }
@@ -1029,6 +1081,10 @@ static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
                 goto assign_error;
             }
         }
+        r = virtio_pci_set_config_notifier(d, assign);
+        if (r < 0) {
+            goto config_error;
+        }
         r = msix_set_vector_notifiers(&proxy->pci_dev,
                                       virtio_pci_vector_unmask,
                                       virtio_pci_vector_mask,
@@ -1045,7 +1101,8 @@ notifiers_error:
         assert(assign);
         kvm_virtio_pci_vector_release(proxy, nvqs);
     }
-
+config_error:
+    kvm_virtio_pci_vector_config_release(proxy);
 assign_error:
     /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
     assert(assign);
Signature · 1 line
2.21.3
#21 lore raw

Re: [PATCH v7 09/10] virtio-pci: add support for configure interrupt

Quoted 35 lines · Cindy Lu

Add support for configure interrupt, use kvm_irqfd_assign and set the gsi to kernel. When the configure notifier was eventfd_signal by host kernel, this will finally inject an msix interrupt to guest

---
  hw/virtio/virtio-pci.c | 63 ++++++++++++++++++++++++++++++++++++++++--
  1 file changed, 60 insertions(+), 3 deletions(-)
hw/virtio/virtio-pci.c +6 -1
index f863c89de6..1e03f11a85 100644
@@ -717,7 +717,8 @@ static int virtio_pci_get_notifier(VirtIOPCIProxy *proxy, int queue_no,
      VirtQueue *vq;
 
      if (queue_no == VIRTIO_CONFIG_IRQ_IDX) {
-        return -1;
+        *n = virtio_get_config_notifier(vdev);
+        *vector = vdev->config_vector;
      } else {
          if (!virtio_queue_get_num(vdev, queue_no)) {
              return -1;
@@ -764,6 +765,10 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
      return ret;
  }
 
+static int kvm_virtio_pci_vector_config_use(VirtIOPCIProxy *proxy)
+{
+    return kvm_virtio_pci_vector_use_one(proxy, VIRTIO_CONFIG_IRQ_IDX);
+}
 
  static void kvm_virtio_pci_vector_release_one(VirtIOPCIProxy *proxy,
                          int queue_no)

hw/virtio/virtio-pci.c static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs) · line 803

Quoted 7 lines
hw/virtio/virtio-pci.c +4 -0
@@ -792,6 +797,28 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
      }
  }
 
+static void kvm_virtio_pci_vector_config_release(VirtIOPCIProxy *proxy)
+{
+    kvm_virtio_pci_vector_release_one(proxy, VIRTIO_CONFIG_IRQ_IDX);
+}

Newline please, can this survive from checkpatch.pl?

Quoted 22 lines
hw/virtio/virtio-pci.c +18 -0
+static int virtio_pci_set_config_notifier(DeviceState *d, bool assign)
+{
+    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    EventNotifier *notifier = virtio_get_config_notifier(vdev);
+    int r = 0;
+    if (assign) {
+        r = event_notifier_init(notifier, 0);
+        virtio_set_notifier_fd_handler(vdev, VIRTIO_CONFIG_IRQ_IDX, true, true);
+        kvm_virtio_pci_vector_config_use(proxy);
+    } else {
+        virtio_set_notifier_fd_handler(vdev, VIRTIO_CONFIG_IRQ_IDX,
+                                             false, true);
+        kvm_virtio_pci_vector_config_release(proxy);
+        event_notifier_cleanup(notifier);
+    }
+    return r;
+}
  static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
                                         unsigned int queue_no,
                                         unsigned int vector,

hw/virtio/virtio-pci.c static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector, · line 908

Quoted 10 lines
hw/virtio/virtio-pci.c +6 -1
@@ -873,9 +900,17 @@ static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
          }
          vq = virtio_vector_next_queue(vq);
      }
-
+    n = virtio_get_config_notifier(vdev);
+    ret = virtio_pci_vq_vector_unmask(proxy, VIRTIO_CONFIG_IRQ_IDX,
+                        vector, msg, n);
+    if (ret < 0) {
+        goto config_undo;
+    }

I'd do this before the loop, but need to validate whether the vector is the one that is used by the config interrupt.

hw/virtio/virtio-pci.c static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector, · line 913

Quoted 5 lines
hw/virtio/virtio-pci.c +3 -0
      return 0;
 
+config_undo:
+    n = virtio_get_config_notifier(vdev);
+    virtio_pci_vq_vector_mask(proxy, VIRTIO_CONFIG_IRQ_IDX, vector, n);

So unmask fail means it is still masked, we don't need to mask it again.

Quoted 20 lines
hw/virtio/virtio-pci.c +6 -0
  undo:
      vq = virtio_vector_first_queue(vdev, vector);
      while (vq && unmasked >= 0) {
@@ -909,6 +944,8 @@ static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
          }
          vq = virtio_vector_next_queue(vq);
      }
+    n = virtio_get_config_notifier(vdev);
+    virtio_pci_vq_vector_mask(proxy, VIRTIO_CONFIG_IRQ_IDX, vector, n);
  }
 
  static void virtio_pci_vector_poll(PCIDevice *dev,
@@ -942,6 +979,20 @@ static void virtio_pci_vector_poll(PCIDevice *dev,
              msix_set_pending(dev, vector);
          }
      }
+   /*check for config interrupt*/
+   vector = vdev->config_vector;
+   notifier = virtio_get_config_notifier(vdev);
+   if (vector < vector_start || vector >= vector_end ||

hw/virtio/virtio-pci.c static void virtio_pci_vector_poll(PCIDevice *dev, · line 995

Quoted 10 lines
hw/virtio/virtio-pci.c +10 -0
+            !msix_is_masked(dev, vector)) {
+        return;
+   }
+   if (k->guest_notifier_pending) {
+        if (k->guest_notifier_pending(vdev, VIRTIO_CONFIG_IRQ_IDX)) {
+            msix_set_pending(dev, vector);
+        }
+   } else if (event_notifier_test_and_clear(notifier)) {
+        msix_set_pending(dev, vector);
+   }

Let's consider to unify the codes with vq vector here.

Quoted 23 lines
hw/virtio/virtio-pci.c +5 -0
  }
 
  static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
@@ -1002,6 +1053,7 @@ static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
          msix_unset_vector_notifiers(&proxy->pci_dev);
          if (proxy->vector_irqfd) {
              kvm_virtio_pci_vector_release(proxy, nvqs);
+            kvm_virtio_pci_vector_config_release(proxy);
              g_free(proxy->vector_irqfd);
              proxy->vector_irqfd = NULL;
          }
@@ -1029,6 +1081,10 @@ static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
                  goto assign_error;
              }
          }
+        r = virtio_pci_set_config_notifier(d, assign);
+        if (r < 0) {
+            goto config_error;
+        }
          r = msix_set_vector_notifiers(&proxy->pci_dev,
                                        virtio_pci_vector_unmask,
                                        virtio_pci_vector_mask,

hw/virtio/virtio-pci.c notifiers_error: · line 1048

Quoted 4 lines
hw/virtio/virtio-pci.c +0 -1
@@ -1045,7 +1101,8 @@ notifiers_error:
          assert(assign);
          kvm_virtio_pci_vector_release(proxy, nvqs);
      }
-

Newline should be kept here.

Thanks

Quoted 5 lines
hw/virtio/virtio-pci.c +2 -0
+config_error:
+    kvm_virtio_pci_vector_config_release(proxy);
  assign_error:
      /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
      assert(assign);
author
#22 lore raw

Re: [PATCH v7 09/10] virtio-pci: add support for configure interrupt

Quoted 41 lines · Jason Wang
Quoted 38 lines · Cindy Lu

Add support for configure interrupt, use kvm_irqfd_assign and set the gsi to kernel. When the configure notifier was eventfd_signal by host kernel, this will finally inject an msix interrupt to guest

---
  hw/virtio/virtio-pci.c | 63 ++++++++++++++++++++++++++++++++++++++++--
  1 file changed, 60 insertions(+), 3 deletions(-)
hw/virtio/virtio-pci.c +6 -1
index f863c89de6..1e03f11a85 100644
@@ -717,7 +717,8 @@ static int virtio_pci_get_notifier(VirtIOPCIProxy *proxy, int queue_no,
      VirtQueue *vq;
 
      if (queue_no == VIRTIO_CONFIG_IRQ_IDX) {
-        return -1;
+        *n = virtio_get_config_notifier(vdev);
+        *vector = vdev->config_vector;
      } else {
          if (!virtio_queue_get_num(vdev, queue_no)) {
              return -1;
@@ -764,6 +765,10 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
      return ret;
  }
 
+static int kvm_virtio_pci_vector_config_use(VirtIOPCIProxy *proxy)
+{
+    return kvm_virtio_pci_vector_use_one(proxy, VIRTIO_CONFIG_IRQ_IDX);
+}
 
  static void kvm_virtio_pci_vector_release_one(VirtIOPCIProxy *proxy,
                          int queue_no)
@@ -792,6 +797,28 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
      }
  }
 
Quoted 8 lines

hw/virtio/virtio-pci.c static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs) · line 803

Quoted 4 lines
hw/virtio/virtio-pci.c +4 -0
+static void kvm_virtio_pci_vector_config_release(VirtIOPCIProxy *proxy)
+{
+    kvm_virtio_pci_vector_release_one(proxy, VIRTIO_CONFIG_IRQ_IDX);
+}

Newline please, can this survive from checkpatch.pl?

sure, will fix this , I have tried checkpatch.pl?, but seems it not working correctly in my environment

Quoted 29 lines
Quoted 29 lines
hw/virtio/virtio-pci.c +20 -1
+static int virtio_pci_set_config_notifier(DeviceState *d, bool assign)
+{
+    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    EventNotifier *notifier = virtio_get_config_notifier(vdev);
+    int r = 0;
+    if (assign) {
+        r = event_notifier_init(notifier, 0);
+        virtio_set_notifier_fd_handler(vdev, VIRTIO_CONFIG_IRQ_IDX, true, true);
+        kvm_virtio_pci_vector_config_use(proxy);
+    } else {
+        virtio_set_notifier_fd_handler(vdev, VIRTIO_CONFIG_IRQ_IDX,
+                                             false, true);
+        kvm_virtio_pci_vector_config_release(proxy);
+        event_notifier_cleanup(notifier);
+    }
+    return r;
+}
  static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
                                         unsigned int queue_no,
                                         unsigned int vector,
@@ -873,9 +900,17 @@ static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
          }
          vq = virtio_vector_next_queue(vq);
      }
-
+    n = virtio_get_config_notifier(vdev);
+    ret = virtio_pci_vq_vector_unmask(proxy, VIRTIO_CONFIG_IRQ_IDX,
Quoted 9 lines

hw/virtio/virtio-pci.c static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector, · line 908

Quoted 4 lines
hw/virtio/virtio-pci.c +4 -0
+                        vector, msg, n);
+    if (ret < 0) {
+        goto config_undo;
+    }

I'd do this before the loop, but need to validate whether the vector is the one that is used by the config interrupt.

sure ,I will move this section

Quoted 36 lines

hw/virtio/virtio-pci.c static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector, · line 913

Quoted 6 lines
hw/virtio/virtio-pci.c +3 -0
      return 0;
 
+config_undo:
+    n = virtio_get_config_notifier(vdev);
+    virtio_pci_vq_vector_mask(proxy, VIRTIO_CONFIG_IRQ_IDX, vector, n);

So unmask fail means it is still masked, we don't need to mask it again.

Quoted 25 lines
hw/virtio/virtio-pci.c +11 -0
  undo:
      vq = virtio_vector_first_queue(vdev, vector);
      while (vq && unmasked >= 0) {
@@ -909,6 +944,8 @@ static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
          }
          vq = virtio_vector_next_queue(vq);
      }
+    n = virtio_get_config_notifier(vdev);
+    virtio_pci_vq_vector_mask(proxy, VIRTIO_CONFIG_IRQ_IDX, vector, n);
  }
 
  static void virtio_pci_vector_poll(PCIDevice *dev,
@@ -942,6 +979,20 @@ static void virtio_pci_vector_poll(PCIDevice *dev,
              msix_set_pending(dev, vector);
          }
      }
+   /*check for config interrupt*/
+   vector = vdev->config_vector;
+   notifier = virtio_get_config_notifier(vdev);
+   if (vector < vector_start || vector >= vector_end ||
+            !msix_is_masked(dev, vector)) {
+        return;
+   }
+   if (k->guest_notifier_pending) {
+        if (k->guest_notifier_pending(vdev, VIRTIO_CONFIG_IRQ_IDX)) {
Quoted 10 lines

hw/virtio/virtio-pci.c static void virtio_pci_vector_poll(PCIDevice *dev, · line 995

Quoted 5 lines
hw/virtio/virtio-pci.c +5 -0
+            msix_set_pending(dev, vector);
+        }
+   } else if (event_notifier_test_and_clear(notifier)) {
+        msix_set_pending(dev, vector);
+   }

Let's consider to unify the codes with vq vector here.

Will do this

Quoted 31 lines
Quoted 23 lines
hw/virtio/virtio-pci.c +5 -0
  }
 
  static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
@@ -1002,6 +1053,7 @@ static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
          msix_unset_vector_notifiers(&proxy->pci_dev);
          if (proxy->vector_irqfd) {
              kvm_virtio_pci_vector_release(proxy, nvqs);
+            kvm_virtio_pci_vector_config_release(proxy);
              g_free(proxy->vector_irqfd);
              proxy->vector_irqfd = NULL;
          }
@@ -1029,6 +1081,10 @@ static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
                  goto assign_error;
              }
          }
+        r = virtio_pci_set_config_notifier(d, assign);
+        if (r < 0) {
+            goto config_error;
+        }
          r = msix_set_vector_notifiers(&proxy->pci_dev,
                                        virtio_pci_vector_unmask,
                                        virtio_pci_vector_mask,

hw/virtio/virtio-pci.c notifiers_error: · line 1048

Quoted 4 lines
hw/virtio/virtio-pci.c +0 -1
@@ -1045,7 +1101,8 @@ notifiers_error:
          assert(assign);
          kvm_virtio_pci_vector_release(proxy, nvqs);
      }
-

Newline should be kept here.

Quoted 3 lines

Thanks

will correct this

Quoted 6 lines
Quoted 5 lines
hw/virtio/virtio-pci.c +2 -0
+config_error:
+    kvm_virtio_pci_vector_config_release(proxy);
  assign_error:
      /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
      assert(assign);
author
#23 lore raw

PATCH v7 10/10 virtio-net: add peer_deleted check in virtio_net_handle_rx

In reply to Cindy Lu

During the test, We found this function will continue running while the peer is deleted, this will cause the crash. so add check for this. this only exist in machines type microvm

reproduce step :
load the VM with
qemu-system-x86_64 -M microvm
...

    -netdev tap,id=tap0,vhost=on,script=no,downscript=no \
    -device virtio-net-device,netdev=tap0 \

..
enter the VM's console
shutdown the VM
(gdb) bt

0 0x000055555595b926 in qemu_net_queue_flush (queue=0x0) at ../net/queue.c:275 1 0x0000555555a046ea in qemu_flush_or_purge_queued_packets (nc=0x555556ccb920, purge=false)

    at ../net/net.c:624
2  0x0000555555a04736 in qemu_flush_queued_packets (nc=0x555556ccb920) at ../net/net.c:637
3  0x0000555555ccc01a in virtio_net_handle_rx (vdev=0x555557360ed0, vq=0x7ffff40d6010)
    at ../hw/net/virtio-net.c:1401
4  0x0000555555ce907a in virtio_queue_notify_vq (vq=0x7ffff40d6010) at ../hw/virtio/virtio.c:2346
5  0x0000555555cec07c in virtio_queue_host_notifier_read (n=0x7ffff40d608c)
    at ../hw/virtio/virtio.c:3606
6  0x00005555560376ac in aio_dispatch_handler (ctx=0x555556a857e0, node=0x555556f013d0)
    at ../util/aio-posix.c:329
7  0x00005555560377a4 in aio_dispatch_ready_handlers (ctx=0x555556a857e0,
    ready_list=0x7fffffffdfe0) at ../util/aio-posix.c:359

8 0x0000555556038209 in aio_poll (ctx=0x555556a857e0, blocking=false) at ../util/aio-posix.c:662 9 0x0000555555e51c6f in monitor_cleanup () at ../monitor/monitor.c:637
10 0x0000555555d2d626 in qemu_cleanup () at ../softmmu/runstate.c:821
11 0x000055555585b19b in main (argc=21, argv=0x7fffffffe1c8, envp=0x7fffffffe278)

---
 hw/net/virtio-net.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +3 -1
index 02033be748..927a808654 100644
@@ -1397,7 +1397,9 @@ static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
 {
     VirtIONet *n = VIRTIO_NET(vdev);
     int queue_index = vq2q(virtio_get_queue_index(vq));
-
+    if (n->nic->peer_deleted) {
+        return;
+    }
     qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
 }
 
Signature · 1 line
2.21.3
#24 lore raw

Re: [PATCH v7 10/10] virtio-net: add peer_deleted check in virtio_net_handle_rx

Quoted 3 lines · Cindy Lu

During the test, We found this function will continue running while the peer is deleted, this will cause the crash. so add check for this. this only exist in machines type microvm

Any idea why it only happens on microvm?

Quoted 5 lines

reproduce step :
load the VM with
qemu-system-x86_64 -M microvm
...

Quoted 8 lines
     -netdev tap,id=tap0,vhost=on,script=no,downscript=no \
     -device virtio-net-device,netdev=tap0 \

..
enter the VM's console
shutdown the VM
(gdb) bt

0 0x000055555595b926 in qemu_net_queue_flush (queue=0x0) at ../net/queue.c:275

So which piece of code trigger this? When the nc has a NIC peer we don't free it until the NIC is freed.

Quoted 27 lines

1 0x0000555555a046ea in qemu_flush_or_purge_queued_packets (nc=0x555556ccb920, purge=false)

     at ../net/net.c:624
2  0x0000555555a04736 in qemu_flush_queued_packets (nc=0x555556ccb920) at ../net/net.c:637
3  0x0000555555ccc01a in virtio_net_handle_rx (vdev=0x555557360ed0, vq=0x7ffff40d6010)
     at ../hw/net/virtio-net.c:1401
4  0x0000555555ce907a in virtio_queue_notify_vq (vq=0x7ffff40d6010) at ../hw/virtio/virtio.c:2346
5  0x0000555555cec07c in virtio_queue_host_notifier_read (n=0x7ffff40d608c)
     at ../hw/virtio/virtio.c:3606
6  0x00005555560376ac in aio_dispatch_handler (ctx=0x555556a857e0, node=0x555556f013d0)
     at ../util/aio-posix.c:329
7  0x00005555560377a4 in aio_dispatch_ready_handlers (ctx=0x555556a857e0,
     ready_list=0x7fffffffdfe0) at ../util/aio-posix.c:359

8 0x0000555556038209 in aio_poll (ctx=0x555556a857e0, blocking=false) at ../util/aio-posix.c:662 9 0x0000555555e51c6f in monitor_cleanup () at ../monitor/monitor.c:637
10 0x0000555555d2d626 in qemu_cleanup () at ../softmmu/runstate.c:821
11 0x000055555585b19b in main (argc=21, argv=0x7fffffffe1c8, envp=0x7fffffffe278)

---
  hw/net/virtio-net.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +0 -0
index 02033be748..927a808654 100644

hw/net/virtio-net.c static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) · line 1401

Quoted 6 lines
hw/net/virtio-net.c +2 -1
@@ -1397,7 +1397,9 @@ static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
  {
      VirtIONet *n = VIRTIO_NET(vdev);
      int queue_index = vq2q(virtio_get_queue_index(vq));
-
+    if (n->nic->peer_deleted) {
+        return;

This needs to be fixed in the network core instead of virtio-net.

Thanks

Quoted 4 lines
hw/net/virtio-net.c +1 -0
+    }
      qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
  }
 
author
#25 lore raw

Re: [PATCH v7 10/10] virtio-net: add peer_deleted check in virtio_net_handle_rx

Quoted 87 lines · Jason Wang
Quoted 3 lines · Cindy Lu

During the test, We found this function will continue running while the peer is deleted, this will cause the crash. so add check for this. this only exist in machines type microvm

Any idea why it only happens on microvm?

Quoted 5 lines

reproduce step :
load the VM with
qemu-system-x86_64 -M microvm
...

Quoted 8 lines
     -netdev tap,id=tap0,vhost=on,script=no,downscript=no \
     -device virtio-net-device,netdev=tap0 \

..
enter the VM's console
shutdown the VM
(gdb) bt

0 0x000055555595b926 in qemu_net_queue_flush (queue=0x0) at

../net/queue.c:275

So which piece of code trigger this? When the nc has a NIC peer we don't free it until the NIC is freed.

Quoted 1 line

1 0x0000555555a046ea in qemu_flush_or_purge_queued_packets

(nc=0x555556ccb920, purge=false)

Quoted 2 lines
     at ../net/net.c:624

2 0x0000555555a04736 in qemu_flush_queued_packets (nc=0x555556ccb920)

at ../net/net.c:637

Quoted 1 line

3 0x0000555555ccc01a in virtio_net_handle_rx (vdev=0x555557360ed0,

vq=0x7ffff40d6010)

Quoted 2 lines
     at ../hw/net/virtio-net.c:1401

4 0x0000555555ce907a in virtio_queue_notify_vq (vq=0x7ffff40d6010) at

../hw/virtio/virtio.c:2346

Quoted 1 line

5 0x0000555555cec07c in virtio_queue_host_notifier_read

(n=0x7ffff40d608c)

Quoted 2 lines
     at ../hw/virtio/virtio.c:3606

6 0x00005555560376ac in aio_dispatch_handler (ctx=0x555556a857e0,

node=0x555556f013d0)

Quoted 4 lines
     at ../util/aio-posix.c:329
7  0x00005555560377a4 in aio_dispatch_ready_handlers (ctx=0x555556a857e0,
     ready_list=0x7fffffffdfe0) at ../util/aio-posix.c:359

8 0x0000555556038209 in aio_poll (ctx=0x555556a857e0, blocking=false)

at ../util/aio-posix.c:662

Quoted 3 lines

9 0x0000555555e51c6f in monitor_cleanup () at ../monitor/monitor.c:637 10 0x0000555555d2d626 in qemu_cleanup () at ../softmmu/runstate.c:821 11 0x000055555585b19b in main (argc=21, argv=0x7fffffffe1c8,

envp=0x7fffffffe278)

hw/net/virtio-net.c static void virtio_net_handle_rx(VirtIODevice

Quoted 11 lines
---
  hw/net/virtio-net.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +0 -0
index 02033be748..927a808654 100644
@@ -1397,7 +1397,9 @@ static void virtio_net_handle_rx(VirtIODevice

*vdev, VirtQueue *vq)

hw/net/virtio-net.c static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) · line 1401

Quoted 6 lines
hw/net/virtio-net.c +2 -1
  {
      VirtIONet *n = VIRTIO_NET(vdev);
      int queue_index = vq2q(virtio_get_queue_index(vq));
-
+    if (n->nic->peer_deleted) {
+        return;

This needs to be fixed in the network core instead of virtio-net.

Thanks

sure I will fix this problem

Quoted 4 lines
hw/net/virtio-net.c +1 -0
+    }
      qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
  }