thebeanogamer / rpms / qemu-kvm

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