29b115
From 893dffb820973361bcef33612a6b924554a856c1 Mon Sep 17 00:00:00 2001
29b115
From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= <eperezma@redhat.com>
29b115
Date: Thu, 21 Jul 2022 15:38:55 +0200
29b115
Subject: [PATCH 13/32] vhost: Check for queue full at vhost_svq_add
29b115
MIME-Version: 1.0
29b115
Content-Type: text/plain; charset=UTF-8
29b115
Content-Transfer-Encoding: 8bit
29b115
29b115
RH-Author: Eugenio Pérez <eperezma@redhat.com>
29b115
RH-MergeRequest: 108: Net Control Virtqueue shadow Support
29b115
RH-Commit: [13/27] d4bd8299fb7733a1e190618dfc92b4b53b7bbeb3 (eperezmartin/qemu-kvm)
29b115
RH-Bugzilla: 1939363
29b115
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
29b115
RH-Acked-by: Cindy Lu <lulu@redhat.com>
29b115
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
29b115
29b115
Bugzilla: https://bugzilla.redhat.com/1939363
29b115
29b115
Upstream Status: git://git.qemu.org/qemu.git
29b115
29b115
commit f20b70eb5a68cfd8fef74a13ccdd494ef1cb0221
29b115
Author: Eugenio Pérez <eperezma@redhat.com>
29b115
Date:   Wed Jul 20 08:59:32 2022 +0200
29b115
29b115
    vhost: Check for queue full at vhost_svq_add
29b115
29b115
    The series need to expose vhost_svq_add with full functionality,
29b115
    including checking for full queue.
29b115
29b115
    Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
29b115
    Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
29b115
    Signed-off-by: Jason Wang <jasowang@redhat.com>
29b115
29b115
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
29b115
---
29b115
 hw/virtio/vhost-shadow-virtqueue.c | 59 +++++++++++++++++-------------
29b115
 1 file changed, 33 insertions(+), 26 deletions(-)
29b115
29b115
diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
29b115
index e3fc3c2658..1d2bab287b 100644
29b115
--- a/hw/virtio/vhost-shadow-virtqueue.c
29b115
+++ b/hw/virtio/vhost-shadow-virtqueue.c
29b115
@@ -233,21 +233,29 @@ static void vhost_svq_kick(VhostShadowVirtqueue *svq)
29b115
  * Add an element to a SVQ.
29b115
  *
29b115
  * The caller must check that there is enough slots for the new element. It
29b115
- * takes ownership of the element: In case of failure, it is free and the SVQ
29b115
- * is considered broken.
29b115
+ * takes ownership of the element: In case of failure not ENOSPC, it is free.
29b115
+ *
29b115
+ * Return -EINVAL if element is invalid, -ENOSPC if dev queue is full
29b115
  */
29b115
-static bool vhost_svq_add(VhostShadowVirtqueue *svq, VirtQueueElement *elem)
29b115
+static int vhost_svq_add(VhostShadowVirtqueue *svq, VirtQueueElement *elem)
29b115
 {
29b115
     unsigned qemu_head;
29b115
-    bool ok = vhost_svq_add_split(svq, elem, &qemu_head);
29b115
+    unsigned ndescs = elem->in_num + elem->out_num;
29b115
+    bool ok;
29b115
+
29b115
+    if (unlikely(ndescs > vhost_svq_available_slots(svq))) {
29b115
+        return -ENOSPC;
29b115
+    }
29b115
+
29b115
+    ok = vhost_svq_add_split(svq, elem, &qemu_head);
29b115
     if (unlikely(!ok)) {
29b115
         g_free(elem);
29b115
-        return false;
29b115
+        return -EINVAL;
29b115
     }
29b115
 
29b115
     svq->ring_id_maps[qemu_head] = elem;
29b115
     vhost_svq_kick(svq);
29b115
-    return true;
29b115
+    return 0;
29b115
 }
29b115
 
29b115
 /**
29b115
@@ -274,7 +282,7 @@ static void vhost_handle_guest_kick(VhostShadowVirtqueue *svq)
29b115
 
29b115
         while (true) {
29b115
             VirtQueueElement *elem;
29b115
-            bool ok;
29b115
+            int r;
29b115
 
29b115
             if (svq->next_guest_avail_elem) {
29b115
                 elem = g_steal_pointer(&svq->next_guest_avail_elem);
29b115
@@ -286,25 +294,24 @@ static void vhost_handle_guest_kick(VhostShadowVirtqueue *svq)
29b115
                 break;
29b115
             }
29b115
 
29b115
-            if (elem->out_num + elem->in_num > vhost_svq_available_slots(svq)) {
29b115
-                /*
29b115
-                 * This condition is possible since a contiguous buffer in GPA
29b115
-                 * does not imply a contiguous buffer in qemu's VA
29b115
-                 * scatter-gather segments. If that happens, the buffer exposed
29b115
-                 * to the device needs to be a chain of descriptors at this
29b115
-                 * moment.
29b115
-                 *
29b115
-                 * SVQ cannot hold more available buffers if we are here:
29b115
-                 * queue the current guest descriptor and ignore further kicks
29b115
-                 * until some elements are used.
29b115
-                 */
29b115
-                svq->next_guest_avail_elem = elem;
29b115
-                return;
29b115
-            }
29b115
-
29b115
-            ok = vhost_svq_add(svq, elem);
29b115
-            if (unlikely(!ok)) {
29b115
-                /* VQ is broken, just return and ignore any other kicks */
29b115
+            r = vhost_svq_add(svq, elem);
29b115
+            if (unlikely(r != 0)) {
29b115
+                if (r == -ENOSPC) {
29b115
+                    /*
29b115
+                     * This condition is possible since a contiguous buffer in
29b115
+                     * GPA does not imply a contiguous buffer in qemu's VA
29b115
+                     * scatter-gather segments. If that happens, the buffer
29b115
+                     * exposed to the device needs to be a chain of descriptors
29b115
+                     * at this moment.
29b115
+                     *
29b115
+                     * SVQ cannot hold more available buffers if we are here:
29b115
+                     * queue the current guest descriptor and ignore kicks
29b115
+                     * until some elements are used.
29b115
+                     */
29b115
+                    svq->next_guest_avail_elem = elem;
29b115
+                }
29b115
+
29b115
+                /* VQ is full or broken, just return and ignore kicks */
29b115
                 return;
29b115
             }
29b115
         }
29b115
-- 
29b115
2.31.1
29b115