586cba
From 3f2ba7cce6b272a8b5c8953e8923e799e4aa7b88 Mon Sep 17 00:00:00 2001
586cba
From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= <eperezma@redhat.com>
586cba
Date: Mon, 18 Jul 2022 14:05:45 +0200
586cba
Subject: [PATCH 02/23] vhost: Get vring base from vq, not svq
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: 116: vdpa: Restore device state on destination
586cba
RH-Bugzilla: 2114060
586cba
RH-Acked-by: Cindy Lu <lulu@redhat.com>
586cba
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
586cba
RH-Commit: [1/21] e7e0294bbc98f69ccdbc4af4715857e77b017f80 (eperezmartin/qemu-kvm)
586cba
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2114060
586cba
Upstream status: Merged
586cba
586cba
The SVQ vring used idx usually match with the guest visible one, as long
586cba
as all the guest buffers (GPA) maps to exactly one buffer within qemu's
586cba
VA. However, as we can see in virtqueue_map_desc, a single guest buffer
586cba
could map to many buffers in SVQ vring.
586cba
586cba
Also, its also a mistake to rewind them at the source of migration.
586cba
Since VirtQueue is able to migrate the inflight descriptors, its
586cba
responsability of the destination to perform the rewind just in case it
586cba
cannot report the inflight descriptors to the device.
586cba
586cba
This makes easier to migrate between backends or to recover them in
586cba
vhost devices that support set in flight descriptors.
586cba
586cba
Fixes: 6d0b22266633 ("vdpa: Adapt vhost_vdpa_get_vring_base to SVQ")
586cba
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
586cba
Signed-off-by: Jason Wang <jasowang@redhat.com>
586cba
(cherry picked from commit 2fdac348fd3d243bb964937236af3cc27ae7af2b)
586cba
---
586cba
 hw/virtio/vhost-vdpa.c | 24 ++++++++++++------------
586cba
 1 file changed, 12 insertions(+), 12 deletions(-)
586cba
586cba
diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
586cba
index 03dc6014b0..96334ab5b6 100644
586cba
--- a/hw/virtio/vhost-vdpa.c
586cba
+++ b/hw/virtio/vhost-vdpa.c
586cba
@@ -1177,7 +1177,18 @@ static int vhost_vdpa_set_vring_base(struct vhost_dev *dev,
586cba
                                        struct vhost_vring_state *ring)
586cba
 {
586cba
     struct vhost_vdpa *v = dev->opaque;
586cba
+    VirtQueue *vq = virtio_get_queue(dev->vdev, ring->index);
586cba
 
586cba
+    /*
586cba
+     * vhost-vdpa devices does not support in-flight requests. Set all of them
586cba
+     * as available.
586cba
+     *
586cba
+     * TODO: This is ok for networking, but other kinds of devices might
586cba
+     * have problems with these retransmissions.
586cba
+     */
586cba
+    while (virtqueue_rewind(vq, 1)) {
586cba
+        continue;
586cba
+    }
586cba
     if (v->shadow_vqs_enabled) {
586cba
         /*
586cba
          * Device vring base was set at device start. SVQ base is handled by
586cba
@@ -1193,21 +1204,10 @@ static int vhost_vdpa_get_vring_base(struct vhost_dev *dev,
586cba
                                        struct vhost_vring_state *ring)
586cba
 {
586cba
     struct vhost_vdpa *v = dev->opaque;
586cba
-    int vdpa_idx = ring->index - dev->vq_index;
586cba
     int ret;
586cba
 
586cba
     if (v->shadow_vqs_enabled) {
586cba
-        VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, vdpa_idx);
586cba
-
586cba
-        /*
586cba
-         * Setting base as last used idx, so destination will see as available
586cba
-         * all the entries that the device did not use, including the in-flight
586cba
-         * processing ones.
586cba
-         *
586cba
-         * TODO: This is ok for networking, but other kinds of devices might
586cba
-         * have problems with these retransmissions.
586cba
-         */
586cba
-        ring->num = svq->last_used_idx;
586cba
+        ring->num = virtio_queue_get_last_avail_idx(dev->vdev, ring->index);
586cba
         return 0;
586cba
     }
586cba
 
586cba
-- 
586cba
2.31.1
586cba