thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
ed5979
From 84c203faa570b85eec006215768c83371c9f0399 Mon Sep 17 00:00:00 2001
ed5979
From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= <eperezma@redhat.com>
ed5979
Date: Thu, 15 Dec 2022 12:31:44 +0100
ed5979
Subject: [PATCH 12/14] vdpa: always start CVQ in SVQ mode if possible
ed5979
MIME-Version: 1.0
ed5979
Content-Type: text/plain; charset=UTF-8
ed5979
Content-Transfer-Encoding: 8bit
ed5979
ed5979
RH-Author: Eugenio Pérez <eperezma@redhat.com>
ed5979
RH-MergeRequest: 136: vDPA ASID support in Qemu
ed5979
RH-Bugzilla: 2104412
ed5979
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
ed5979
RH-Acked-by: Cindy Lu <lulu@redhat.com>
ed5979
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
ed5979
RH-Commit: [12/13] 83f94b3e163ca38d08dbf7c111a4cfa7a44e3dc2 (eperezmartin/qemu-kvm)
ed5979
ed5979
Isolate control virtqueue in its own group, allowing to intercept control
ed5979
commands but letting dataplane run totally passthrough to the guest.
ed5979
ed5979
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
ed5979
Message-Id: <20221215113144.322011-13-eperezma@redhat.com>
ed5979
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
ed5979
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
ed5979
Acked-by: Jason Wang <jasowang@redhat.com>
ed5979
(cherry picked from commit c1a1008685af0327d9d03f03d43bdb77e7af5bea)
ed5979
---
ed5979
 hw/virtio/vhost-vdpa.c |   3 +-
ed5979
 net/vhost-vdpa.c       | 110 ++++++++++++++++++++++++++++++++++++++++-
ed5979
 2 files changed, 111 insertions(+), 2 deletions(-)
ed5979
ed5979
diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
ed5979
index dc3498e995..72ff06673c 100644
ed5979
--- a/hw/virtio/vhost-vdpa.c
ed5979
+++ b/hw/virtio/vhost-vdpa.c
ed5979
@@ -638,7 +638,8 @@ static int vhost_vdpa_set_backend_cap(struct vhost_dev *dev)
ed5979
 {
ed5979
     uint64_t features;
ed5979
     uint64_t f = 0x1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2 |
ed5979
-        0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH;
ed5979
+        0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH |
ed5979
+        0x1ULL << VHOST_BACKEND_F_IOTLB_ASID;
ed5979
     int r;
ed5979
 
ed5979
     if (vhost_vdpa_call(dev, VHOST_GET_BACKEND_FEATURES, &features)) {
ed5979
diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c
ed5979
index eea7a0df12..07d33dae26 100644
ed5979
--- a/net/vhost-vdpa.c
ed5979
+++ b/net/vhost-vdpa.c
ed5979
@@ -101,6 +101,8 @@ static const uint64_t vdpa_svq_device_features =
ed5979
     BIT_ULL(VIRTIO_NET_F_RSC_EXT) |
ed5979
     BIT_ULL(VIRTIO_NET_F_STANDBY);
ed5979
 
ed5979
+#define VHOST_VDPA_NET_CVQ_ASID 1
ed5979
+
ed5979
 VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc)
ed5979
 {
ed5979
     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
ed5979
@@ -242,6 +244,40 @@ static NetClientInfo net_vhost_vdpa_info = {
ed5979
         .check_peer_type = vhost_vdpa_check_peer_type,
ed5979
 };
ed5979
 
ed5979
+static int64_t vhost_vdpa_get_vring_group(int device_fd, unsigned vq_index)
ed5979
+{
ed5979
+    struct vhost_vring_state state = {
ed5979
+        .index = vq_index,
ed5979
+    };
ed5979
+    int r = ioctl(device_fd, VHOST_VDPA_GET_VRING_GROUP, &state);
ed5979
+
ed5979
+    if (unlikely(r < 0)) {
ed5979
+        error_report("Cannot get VQ %u group: %s", vq_index,
ed5979
+                     g_strerror(errno));
ed5979
+        return r;
ed5979
+    }
ed5979
+
ed5979
+    return state.num;
ed5979
+}
ed5979
+
ed5979
+static int vhost_vdpa_set_address_space_id(struct vhost_vdpa *v,
ed5979
+                                           unsigned vq_group,
ed5979
+                                           unsigned asid_num)
ed5979
+{
ed5979
+    struct vhost_vring_state asid = {
ed5979
+        .index = vq_group,
ed5979
+        .num = asid_num,
ed5979
+    };
ed5979
+    int r;
ed5979
+
ed5979
+    r = ioctl(v->device_fd, VHOST_VDPA_SET_GROUP_ASID, &asid);
ed5979
+    if (unlikely(r < 0)) {
ed5979
+        error_report("Can't set vq group %u asid %u, errno=%d (%s)",
ed5979
+                     asid.index, asid.num, errno, g_strerror(errno));
ed5979
+    }
ed5979
+    return r;
ed5979
+}
ed5979
+
ed5979
 static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa *v, void *addr)
ed5979
 {
ed5979
     VhostIOVATree *tree = v->iova_tree;
ed5979
@@ -316,11 +352,75 @@ dma_map_err:
ed5979
 static int vhost_vdpa_net_cvq_start(NetClientState *nc)
ed5979
 {
ed5979
     VhostVDPAState *s;
ed5979
-    int r;
ed5979
+    struct vhost_vdpa *v;
ed5979
+    uint64_t backend_features;
ed5979
+    int64_t cvq_group;
ed5979
+    int cvq_index, r;
ed5979
 
ed5979
     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
ed5979
 
ed5979
     s = DO_UPCAST(VhostVDPAState, nc, nc);
ed5979
+    v = &s->vhost_vdpa;
ed5979
+
ed5979
+    v->shadow_data = s->always_svq;
ed5979
+    v->shadow_vqs_enabled = s->always_svq;
ed5979
+    s->vhost_vdpa.address_space_id = VHOST_VDPA_GUEST_PA_ASID;
ed5979
+
ed5979
+    if (s->always_svq) {
ed5979
+        /* SVQ is already configured for all virtqueues */
ed5979
+        goto out;
ed5979
+    }
ed5979
+
ed5979
+    /*
ed5979
+     * If we early return in these cases SVQ will not be enabled. The migration
ed5979
+     * will be blocked as long as vhost-vdpa backends will not offer _F_LOG.
ed5979
+     *
ed5979
+     * Calling VHOST_GET_BACKEND_FEATURES as they are not available in v->dev
ed5979
+     * yet.
ed5979
+     */
ed5979
+    r = ioctl(v->device_fd, VHOST_GET_BACKEND_FEATURES, &backend_features);
ed5979
+    if (unlikely(r < 0)) {
ed5979
+        error_report("Cannot get vdpa backend_features: %s(%d)",
ed5979
+            g_strerror(errno), errno);
ed5979
+        return -1;
ed5979
+    }
ed5979
+    if (!(backend_features & VHOST_BACKEND_F_IOTLB_ASID) ||
ed5979
+        !vhost_vdpa_net_valid_svq_features(v->dev->features, NULL)) {
ed5979
+        return 0;
ed5979
+    }
ed5979
+
ed5979
+    /*
ed5979
+     * Check if all the virtqueues of the virtio device are in a different vq
ed5979
+     * than the last vq. VQ group of last group passed in cvq_group.
ed5979
+     */
ed5979
+    cvq_index = v->dev->vq_index_end - 1;
ed5979
+    cvq_group = vhost_vdpa_get_vring_group(v->device_fd, cvq_index);
ed5979
+    if (unlikely(cvq_group < 0)) {
ed5979
+        return cvq_group;
ed5979
+    }
ed5979
+    for (int i = 0; i < cvq_index; ++i) {
ed5979
+        int64_t group = vhost_vdpa_get_vring_group(v->device_fd, i);
ed5979
+
ed5979
+        if (unlikely(group < 0)) {
ed5979
+            return group;
ed5979
+        }
ed5979
+
ed5979
+        if (group == cvq_group) {
ed5979
+            return 0;
ed5979
+        }
ed5979
+    }
ed5979
+
ed5979
+    r = vhost_vdpa_set_address_space_id(v, cvq_group, VHOST_VDPA_NET_CVQ_ASID);
ed5979
+    if (unlikely(r < 0)) {
ed5979
+        return r;
ed5979
+    }
ed5979
+
ed5979
+    v->iova_tree = vhost_iova_tree_new(v->iova_range.first,
ed5979
+                                       v->iova_range.last);
ed5979
+    v->shadow_vqs_enabled = true;
ed5979
+    s->vhost_vdpa.address_space_id = VHOST_VDPA_NET_CVQ_ASID;
ed5979
+
ed5979
+out:
ed5979
     if (!s->vhost_vdpa.shadow_vqs_enabled) {
ed5979
         return 0;
ed5979
     }
ed5979
@@ -349,6 +449,14 @@ static void vhost_vdpa_net_cvq_stop(NetClientState *nc)
ed5979
     if (s->vhost_vdpa.shadow_vqs_enabled) {
ed5979
         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
ed5979
         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->status);
ed5979
+        if (!s->always_svq) {
ed5979
+            /*
ed5979
+             * If only the CVQ is shadowed we can delete this safely.
ed5979
+             * If all the VQs are shadows this will be needed by the time the
ed5979
+             * device is started again to register SVQ vrings and similar.
ed5979
+             */
ed5979
+            g_clear_pointer(&s->vhost_vdpa.iova_tree, vhost_iova_tree_delete);
ed5979
+        }
ed5979
     }
ed5979
 }
ed5979
 
ed5979
-- 
ed5979
2.31.1
ed5979