586cba
From 893dffb820973361bcef33612a6b924554a856c1 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 13/32] vhost: Check for queue full at vhost_svq_add
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: [13/27] d4bd8299fb7733a1e190618dfc92b4b53b7bbeb3 (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 f20b70eb5a68cfd8fef74a13ccdd494ef1cb0221
586cba
Author: Eugenio Pérez <eperezma@redhat.com>
586cba
Date:   Wed Jul 20 08:59:32 2022 +0200
586cba
586cba
    vhost: Check for queue full at vhost_svq_add
586cba
586cba
    The series need to expose vhost_svq_add with full functionality,
586cba
    including checking for full queue.
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 | 59 +++++++++++++++++-------------
586cba
 1 file changed, 33 insertions(+), 26 deletions(-)
586cba
586cba
diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
586cba
index e3fc3c2658..1d2bab287b 100644
586cba
--- a/hw/virtio/vhost-shadow-virtqueue.c
586cba
+++ b/hw/virtio/vhost-shadow-virtqueue.c
586cba
@@ -233,21 +233,29 @@ static void vhost_svq_kick(VhostShadowVirtqueue *svq)
586cba
  * Add an element to a SVQ.
586cba
  *
586cba
  * The caller must check that there is enough slots for the new element. It
586cba
- * takes ownership of the element: In case of failure, it is free and the SVQ
586cba
- * is considered broken.
586cba
+ * takes ownership of the element: In case of failure not ENOSPC, it is free.
586cba
+ *
586cba
+ * Return -EINVAL if element is invalid, -ENOSPC if dev queue is full
586cba
  */
586cba
-static bool vhost_svq_add(VhostShadowVirtqueue *svq, VirtQueueElement *elem)
586cba
+static int vhost_svq_add(VhostShadowVirtqueue *svq, VirtQueueElement *elem)
586cba
 {
586cba
     unsigned qemu_head;
586cba
-    bool ok = vhost_svq_add_split(svq, elem, &qemu_head);
586cba
+    unsigned ndescs = elem->in_num + elem->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
     if (unlikely(!ok)) {
586cba
         g_free(elem);
586cba
-        return false;
586cba
+        return -EINVAL;
586cba
     }
586cba
 
586cba
     svq->ring_id_maps[qemu_head] = elem;
586cba
     vhost_svq_kick(svq);
586cba
-    return true;
586cba
+    return 0;
586cba
 }
586cba
 
586cba
 /**
586cba
@@ -274,7 +282,7 @@ static void vhost_handle_guest_kick(VhostShadowVirtqueue *svq)
586cba
 
586cba
         while (true) {
586cba
             VirtQueueElement *elem;
586cba
-            bool ok;
586cba
+            int r;
586cba
 
586cba
             if (svq->next_guest_avail_elem) {
586cba
                 elem = g_steal_pointer(&svq->next_guest_avail_elem);
586cba
@@ -286,25 +294,24 @@ static void vhost_handle_guest_kick(VhostShadowVirtqueue *svq)
586cba
                 break;
586cba
             }
586cba
 
586cba
-            if (elem->out_num + elem->in_num > vhost_svq_available_slots(svq)) {
586cba
-                /*
586cba
-                 * This condition is possible since a contiguous buffer in GPA
586cba
-                 * does not imply a contiguous buffer in qemu's VA
586cba
-                 * scatter-gather segments. If that happens, the buffer exposed
586cba
-                 * to the device needs to be a chain of descriptors at this
586cba
-                 * moment.
586cba
-                 *
586cba
-                 * SVQ cannot hold more available buffers if we are here:
586cba
-                 * queue the current guest descriptor and ignore further kicks
586cba
-                 * until some elements are used.
586cba
-                 */
586cba
-                svq->next_guest_avail_elem = elem;
586cba
-                return;
586cba
-            }
586cba
-
586cba
-            ok = vhost_svq_add(svq, elem);
586cba
-            if (unlikely(!ok)) {
586cba
-                /* VQ is broken, just return and ignore any other kicks */
586cba
+            r = vhost_svq_add(svq, elem);
586cba
+            if (unlikely(r != 0)) {
586cba
+                if (r == -ENOSPC) {
586cba
+                    /*
586cba
+                     * This condition is possible since a contiguous buffer in
586cba
+                     * GPA does not imply a contiguous buffer in qemu's VA
586cba
+                     * scatter-gather segments. If that happens, the buffer
586cba
+                     * exposed to the device needs to be a chain of descriptors
586cba
+                     * at this moment.
586cba
+                     *
586cba
+                     * SVQ cannot hold more available buffers if we are here:
586cba
+                     * queue the current guest descriptor and ignore kicks
586cba
+                     * until some elements are used.
586cba
+                     */
586cba
+                    svq->next_guest_avail_elem = elem;
586cba
+                }
586cba
+
586cba
+                /* VQ is full or broken, just return and ignore kicks */
586cba
                 return;
586cba
             }
586cba
         }
586cba
-- 
586cba
2.31.1
586cba