Blame SOURCES/kvm-vhost-Decouple-vhost_svq_add-from-VirtQueueElement.patch

586cba
From 5c8de23e185a1a1f0b19eac3c9fa03411c9f545c 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 14/32] vhost: Decouple vhost_svq_add from VirtQueueElement
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: [14/27] 463087dd316adc91b9c7a4e6634c6fc1745c1849 (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 1f46ae65d85f677b660bda46685dd3e94885a7cb
586cba
Author: Eugenio Pérez <eperezma@redhat.com>
586cba
Date:   Wed Jul 20 08:59:33 2022 +0200
586cba
586cba
    vhost: Decouple vhost_svq_add from VirtQueueElement
586cba
586cba
    VirtQueueElement comes from the guest, but we're heading SVQ to be able
586cba
    to modify the element presented to the device without the guest's
586cba
    knowledge.
586cba
586cba
    To do so, make SVQ accept sg buffers directly, instead of using
586cba
    VirtQueueElement.
586cba
586cba
    Add vhost_svq_add_element to maintain element convenience.
586cba
586cba
    Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
586cba
    Acked-by: Jason Wang <jasowang@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 | 33 ++++++++++++++++++++----------
586cba
 1 file changed, 22 insertions(+), 11 deletions(-)
586cba
586cba
diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
586cba
index 1d2bab287b..3cec03d709 100644
586cba
--- a/hw/virtio/vhost-shadow-virtqueue.c
586cba
+++ b/hw/virtio/vhost-shadow-virtqueue.c
586cba
@@ -172,30 +172,31 @@ static bool vhost_svq_vring_write_descs(VhostShadowVirtqueue *svq, hwaddr *sg,
586cba
 }
586cba
 
586cba
 static bool vhost_svq_add_split(VhostShadowVirtqueue *svq,
586cba
-                                VirtQueueElement *elem, unsigned *head)
586cba
+                                const struct iovec *out_sg, size_t out_num,
586cba
+                                const struct iovec *in_sg, size_t in_num,
586cba
+                                unsigned *head)
586cba
 {
586cba
     unsigned avail_idx;
586cba
     vring_avail_t *avail = svq->vring.avail;
586cba
     bool ok;
586cba
-    g_autofree hwaddr *sgs = g_new(hwaddr, MAX(elem->out_num, elem->in_num));
586cba
+    g_autofree hwaddr *sgs = g_new(hwaddr, MAX(out_num, in_num));
586cba
 
586cba
     *head = svq->free_head;
586cba
 
586cba
     /* We need some descriptors here */
586cba
-    if (unlikely(!elem->out_num && !elem->in_num)) {
586cba
+    if (unlikely(!out_num && !in_num)) {
586cba
         qemu_log_mask(LOG_GUEST_ERROR,
586cba
                       "Guest provided element with no descriptors");
586cba
         return false;
586cba
     }
586cba
 
586cba
-    ok = vhost_svq_vring_write_descs(svq, sgs, elem->out_sg, elem->out_num,
586cba
-                                     elem->in_num > 0, false);
586cba
+    ok = vhost_svq_vring_write_descs(svq, sgs, out_sg, out_num, in_num > 0,
586cba
+                                     false);
586cba
     if (unlikely(!ok)) {
586cba
         return false;
586cba
     }
586cba
 
586cba
-    ok = vhost_svq_vring_write_descs(svq, sgs, elem->in_sg, elem->in_num, false,
586cba
-                                     true);
586cba
+    ok = vhost_svq_vring_write_descs(svq, sgs, in_sg, in_num, false, true);
586cba
     if (unlikely(!ok)) {
586cba
         return false;
586cba
     }
586cba
@@ -237,17 +238,19 @@ static void vhost_svq_kick(VhostShadowVirtqueue *svq)
586cba
  *
586cba
  * Return -EINVAL if element is invalid, -ENOSPC if dev queue is full
586cba
  */
586cba
-static int vhost_svq_add(VhostShadowVirtqueue *svq, VirtQueueElement *elem)
586cba
+static int vhost_svq_add(VhostShadowVirtqueue *svq, const struct iovec *out_sg,
586cba
+                          size_t out_num, const struct iovec *in_sg,
586cba
+                          size_t in_num, VirtQueueElement *elem)
586cba
 {
586cba
     unsigned qemu_head;
586cba
-    unsigned ndescs = elem->in_num + elem->out_num;
586cba
+    unsigned ndescs = in_num + out_num;
586cba
     bool ok;
586cba
 
586cba
     if (unlikely(ndescs > vhost_svq_available_slots(svq))) {
586cba
         return -ENOSPC;
586cba
     }
586cba
 
586cba
-    ok = vhost_svq_add_split(svq, elem, &qemu_head);
586cba
+    ok = vhost_svq_add_split(svq, out_sg, out_num, in_sg, in_num, &qemu_head);
586cba
     if (unlikely(!ok)) {
586cba
         g_free(elem);
586cba
         return -EINVAL;
586cba
@@ -258,6 +261,14 @@ static int vhost_svq_add(VhostShadowVirtqueue *svq, VirtQueueElement *elem)
586cba
     return 0;
586cba
 }
586cba
 
586cba
+/* Convenience wrapper to add a guest's element to SVQ */
586cba
+static int vhost_svq_add_element(VhostShadowVirtqueue *svq,
586cba
+                                 VirtQueueElement *elem)
586cba
+{
586cba
+    return vhost_svq_add(svq, elem->out_sg, elem->out_num, elem->in_sg,
586cba
+                         elem->in_num, elem);
586cba
+}
586cba
+
586cba
 /**
586cba
  * Forward available buffers.
586cba
  *
586cba
@@ -294,7 +305,7 @@ static void vhost_handle_guest_kick(VhostShadowVirtqueue *svq)
586cba
                 break;
586cba
             }
586cba
 
586cba
-            r = vhost_svq_add(svq, elem);
586cba
+            r = vhost_svq_add_element(svq, elem);
586cba
             if (unlikely(r != 0)) {
586cba
                 if (r == -ENOSPC) {
586cba
                     /*
586cba
-- 
586cba
2.31.1
586cba