586cba
From 14200f493243f73152ea4a4b97274f0ec4fb36fa Mon Sep 17 00:00:00 2001
586cba
From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= <eperezma@redhat.com>
586cba
Date: Thu, 21 Jul 2022 15:38:55 +0200
586cba
Subject: [PATCH 15/32] vhost: Add SVQDescState
586cba
MIME-Version: 1.0
586cba
Content-Type: text/plain; charset=UTF-8
586cba
Content-Transfer-Encoding: 8bit
586cba
586cba
RH-Author: Eugenio Pérez <eperezma@redhat.com>
586cba
RH-MergeRequest: 108: Net Control Virtqueue shadow Support
586cba
RH-Commit: [15/27] 2e2866f22e37cace8598ff44dfcdc07fcc915d6d (eperezmartin/qemu-kvm)
586cba
RH-Bugzilla: 1939363
586cba
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
586cba
RH-Acked-by: Cindy Lu <lulu@redhat.com>
586cba
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
586cba
586cba
Bugzilla: https://bugzilla.redhat.com/1939363
586cba
586cba
Upstream Status: git://git.qemu.org/qemu.git
586cba
586cba
commit 9e87868fcaf5785c8e1490c290505fa32305ff91
586cba
Author: Eugenio Pérez <eperezma@redhat.com>
586cba
Date:   Wed Jul 20 08:59:34 2022 +0200
586cba
586cba
    vhost: Add SVQDescState
586cba
586cba
    This will allow SVQ to add context to the different queue elements.
586cba
586cba
    This patch only store the actual element, no functional change intended.
586cba
586cba
    Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
586cba
    Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
586cba
    Signed-off-by: Jason Wang <jasowang@redhat.com>
586cba
586cba
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
586cba
---
586cba
 hw/virtio/vhost-shadow-virtqueue.c | 16 ++++++++--------
586cba
 hw/virtio/vhost-shadow-virtqueue.h |  8 ++++++--
586cba
 2 files changed, 14 insertions(+), 10 deletions(-)
586cba
586cba
diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
586cba
index 3cec03d709..a08e3d4025 100644
586cba
--- a/hw/virtio/vhost-shadow-virtqueue.c
586cba
+++ b/hw/virtio/vhost-shadow-virtqueue.c
586cba
@@ -256,7 +256,7 @@ static int vhost_svq_add(VhostShadowVirtqueue *svq, const struct iovec *out_sg,
586cba
         return -EINVAL;
586cba
     }
586cba
 
586cba
-    svq->ring_id_maps[qemu_head] = elem;
586cba
+    svq->desc_state[qemu_head].elem = elem;
586cba
     vhost_svq_kick(svq);
586cba
     return 0;
586cba
 }
586cba
@@ -411,21 +411,21 @@ static VirtQueueElement *vhost_svq_get_buf(VhostShadowVirtqueue *svq,
586cba
         return NULL;
586cba
     }
586cba
 
586cba
-    if (unlikely(!svq->ring_id_maps[used_elem.id])) {
586cba
+    if (unlikely(!svq->desc_state[used_elem.id].elem)) {
586cba
         qemu_log_mask(LOG_GUEST_ERROR,
586cba
             "Device %s says index %u is used, but it was not available",
586cba
             svq->vdev->name, used_elem.id);
586cba
         return NULL;
586cba
     }
586cba
 
586cba
-    num = svq->ring_id_maps[used_elem.id]->in_num +
586cba
-          svq->ring_id_maps[used_elem.id]->out_num;
586cba
+    num = svq->desc_state[used_elem.id].elem->in_num +
586cba
+          svq->desc_state[used_elem.id].elem->out_num;
586cba
     last_used_chain = vhost_svq_last_desc_of_chain(svq, num, used_elem.id);
586cba
     svq->desc_next[last_used_chain] = svq->free_head;
586cba
     svq->free_head = used_elem.id;
586cba
 
586cba
     *len = used_elem.len;
586cba
-    return g_steal_pointer(&svq->ring_id_maps[used_elem.id]);
586cba
+    return g_steal_pointer(&svq->desc_state[used_elem.id].elem);
586cba
 }
586cba
 
586cba
 static void vhost_svq_flush(VhostShadowVirtqueue *svq,
586cba
@@ -595,7 +595,7 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
586cba
     memset(svq->vring.desc, 0, driver_size);
586cba
     svq->vring.used = qemu_memalign(qemu_real_host_page_size, device_size);
586cba
     memset(svq->vring.used, 0, device_size);
586cba
-    svq->ring_id_maps = g_new0(VirtQueueElement *, svq->vring.num);
586cba
+    svq->desc_state = g_new0(SVQDescState, svq->vring.num);
586cba
     svq->desc_next = g_new0(uint16_t, svq->vring.num);
586cba
     for (unsigned i = 0; i < svq->vring.num - 1; i++) {
586cba
         svq->desc_next[i] = cpu_to_le16(i + 1);
586cba
@@ -620,7 +620,7 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq)
586cba
 
586cba
     for (unsigned i = 0; i < svq->vring.num; ++i) {
586cba
         g_autofree VirtQueueElement *elem = NULL;
586cba
-        elem = g_steal_pointer(&svq->ring_id_maps[i]);
586cba
+        elem = g_steal_pointer(&svq->desc_state[i].elem);
586cba
         if (elem) {
586cba
             virtqueue_detach_element(svq->vq, elem, 0);
586cba
         }
586cba
@@ -632,7 +632,7 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq)
586cba
     }
586cba
     svq->vq = NULL;
586cba
     g_free(svq->desc_next);
586cba
-    g_free(svq->ring_id_maps);
586cba
+    g_free(svq->desc_state);
586cba
     qemu_vfree(svq->vring.desc);
586cba
     qemu_vfree(svq->vring.used);
586cba
 }
586cba
diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-virtqueue.h
586cba
index c132c994e9..d646c35054 100644
586cba
--- a/hw/virtio/vhost-shadow-virtqueue.h
586cba
+++ b/hw/virtio/vhost-shadow-virtqueue.h
586cba
@@ -15,6 +15,10 @@
586cba
 #include "standard-headers/linux/vhost_types.h"
586cba
 #include "hw/virtio/vhost-iova-tree.h"
586cba
 
586cba
+typedef struct SVQDescState {
586cba
+    VirtQueueElement *elem;
586cba
+} SVQDescState;
586cba
+
586cba
 /* Shadow virtqueue to relay notifications */
586cba
 typedef struct VhostShadowVirtqueue {
586cba
     /* Shadow vring */
586cba
@@ -47,8 +51,8 @@ typedef struct VhostShadowVirtqueue {
586cba
     /* IOVA mapping */
586cba
     VhostIOVATree *iova_tree;
586cba
 
586cba
-    /* Map for use the guest's descriptors */
586cba
-    VirtQueueElement **ring_id_maps;
586cba
+    /* SVQ vring descriptors state */
586cba
+    SVQDescState *desc_state;
586cba
 
586cba
     /* Next VirtQueue element that guest made available */
586cba
     VirtQueueElement *next_guest_avail_elem;
586cba
-- 
586cba
2.31.1
586cba