Blame SOURCES/kvm-vdpa-Move-command-buffers-map-to-start-of-net-device.patch

586cba
From 70c72316c26e95cd18b4d46b83e78ba3a148212c Mon Sep 17 00:00:00 2001
586cba
From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= <eperezma@redhat.com>
586cba
Date: Tue, 23 Aug 2022 20:30:33 +0200
586cba
Subject: [PATCH 18/23] vdpa: Move command buffers map to start of net device
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: [17/21] 7a9824fa618f5c2904648b50e3078474cd3987aa (eperezmartin/qemu-kvm)
586cba
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2114060
586cba
Upstream status: git@github.com:jasowang/qemu.git net-next
586cba
586cba
As this series will reuse them to restore the device state at the end of
586cba
a migration (or a device start), let's allocate only once at the device
586cba
start so we don't duplicate their map and unmap.
586cba
586cba
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
586cba
Acked-by: Jason Wang <jasowang@redhat.com>
586cba
Signed-off-by: Jason Wang <jasowang@redhat.com>
586cba
(cherry picked from commit d7d73dec14cebcebd8de774424795aeb821236c1)
586cba
---
586cba
 net/vhost-vdpa.c | 123 ++++++++++++++++++++++-------------------------
586cba
 1 file changed, 58 insertions(+), 65 deletions(-)
586cba
586cba
diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c
586cba
index 03e4cf1abc..17626feb8d 100644
586cba
--- a/net/vhost-vdpa.c
586cba
+++ b/net/vhost-vdpa.c
586cba
@@ -263,29 +263,20 @@ static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
586cba
     return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size);
586cba
 }
586cba
 
586cba
-/** Copy and map a guest buffer. */
586cba
-static bool vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v,
586cba
-                                   const struct iovec *out_data,
586cba
-                                   size_t out_num, size_t data_len, void *buf,
586cba
-                                   size_t *written, bool write)
586cba
+/** Map CVQ buffer. */
586cba
+static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size,
586cba
+                                  bool write)
586cba
 {
586cba
     DMAMap map = {};
586cba
     int r;
586cba
 
586cba
-    if (unlikely(!data_len)) {
586cba
-        qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid legnth of %s buffer\n",
586cba
-                      __func__, write ? "in" : "out");
586cba
-        return false;
586cba
-    }
586cba
-
586cba
-    *written = iov_to_buf(out_data, out_num, 0, buf, data_len);
586cba
     map.translated_addr = (hwaddr)(uintptr_t)buf;
586cba
-    map.size = vhost_vdpa_net_cvq_cmd_page_len() - 1;
586cba
+    map.size = size - 1;
586cba
     map.perm = write ? IOMMU_RW : IOMMU_RO,
586cba
     r = vhost_iova_tree_map_alloc(v->iova_tree, &map);
586cba
     if (unlikely(r != IOVA_OK)) {
586cba
         error_report("Cannot map injected element");
586cba
-        return false;
586cba
+        return r;
586cba
     }
586cba
 
586cba
     r = vhost_vdpa_dma_map(v, map.iova, vhost_vdpa_net_cvq_cmd_page_len(), buf,
586cba
@@ -294,50 +285,58 @@ static bool vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v,
586cba
         goto dma_map_err;
586cba
     }
586cba
 
586cba
-    return true;
586cba
+    return 0;
586cba
 
586cba
 dma_map_err:
586cba
     vhost_iova_tree_remove(v->iova_tree, map);
586cba
-    return false;
586cba
+    return r;
586cba
 }
586cba
 
586cba
-/**
586cba
- * Copy the guest element into a dedicated buffer suitable to be sent to NIC
586cba
- *
586cba
- * @iov: [0] is the out buffer, [1] is the in one
586cba
- */
586cba
-static bool vhost_vdpa_net_cvq_map_elem(VhostVDPAState *s,
586cba
-                                        VirtQueueElement *elem,
586cba
-                                        struct iovec *iov)
586cba
+static int vhost_vdpa_net_cvq_start(NetClientState *nc)
586cba
 {
586cba
-    size_t in_copied;
586cba
-    bool ok;
586cba
+    VhostVDPAState *s;
586cba
+    int r;
586cba
 
586cba
-    iov[0].iov_base = s->cvq_cmd_out_buffer;
586cba
-    ok = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, elem->out_sg, elem->out_num,
586cba
-                                vhost_vdpa_net_cvq_cmd_len(), iov[0].iov_base,
586cba
-                                &iov[0].iov_len, false);
586cba
-    if (unlikely(!ok)) {
586cba
-        return false;
586cba
+    assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
586cba
+
586cba
+    s = DO_UPCAST(VhostVDPAState, nc, nc);
586cba
+    if (!s->vhost_vdpa.shadow_vqs_enabled) {
586cba
+        return 0;
586cba
     }
586cba
 
586cba
-    iov[1].iov_base = s->cvq_cmd_in_buffer;
586cba
-    ok = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, NULL, 0,
586cba
-                                sizeof(virtio_net_ctrl_ack), iov[1].iov_base,
586cba
-                                &in_copied, true);
586cba
-    if (unlikely(!ok)) {
586cba
+    r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer,
586cba
+                               vhost_vdpa_net_cvq_cmd_page_len(), false);
586cba
+    if (unlikely(r < 0)) {
586cba
+        return r;
586cba
+    }
586cba
+
586cba
+    r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->cvq_cmd_in_buffer,
586cba
+                               vhost_vdpa_net_cvq_cmd_page_len(), true);
586cba
+    if (unlikely(r < 0)) {
586cba
         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
586cba
-        return false;
586cba
     }
586cba
 
586cba
-    iov[1].iov_len = sizeof(virtio_net_ctrl_ack);
586cba
-    return true;
586cba
+    return r;
586cba
+}
586cba
+
586cba
+static void vhost_vdpa_net_cvq_stop(NetClientState *nc)
586cba
+{
586cba
+    VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
586cba
+
586cba
+    assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
586cba
+
586cba
+    if (s->vhost_vdpa.shadow_vqs_enabled) {
586cba
+        vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
586cba
+        vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_in_buffer);
586cba
+    }
586cba
 }
586cba
 
586cba
 static NetClientInfo net_vhost_vdpa_cvq_info = {
586cba
     .type = NET_CLIENT_DRIVER_VHOST_VDPA,
586cba
     .size = sizeof(VhostVDPAState),
586cba
     .receive = vhost_vdpa_receive,
586cba
+    .start = vhost_vdpa_net_cvq_start,
586cba
+    .stop = vhost_vdpa_net_cvq_stop,
586cba
     .cleanup = vhost_vdpa_cleanup,
586cba
     .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
586cba
     .has_ufo = vhost_vdpa_has_ufo,
586cba
@@ -348,19 +347,17 @@ static NetClientInfo net_vhost_vdpa_cvq_info = {
586cba
  * Do not forward commands not supported by SVQ. Otherwise, the device could
586cba
  * accept it and qemu would not know how to update the device model.
586cba
  */
586cba
-static bool vhost_vdpa_net_cvq_validate_cmd(const struct iovec *out,
586cba
-                                            size_t out_num)
586cba
+static bool vhost_vdpa_net_cvq_validate_cmd(const void *out_buf, size_t len)
586cba
 {
586cba
     struct virtio_net_ctrl_hdr ctrl;
586cba
-    size_t n;
586cba
 
586cba
-    n = iov_to_buf(out, out_num, 0, &ctrl, sizeof(ctrl));
586cba
-    if (unlikely(n < sizeof(ctrl))) {
586cba
+    if (unlikely(len < sizeof(ctrl))) {
586cba
         qemu_log_mask(LOG_GUEST_ERROR,
586cba
-                      "%s: invalid legnth of out buffer %zu\n", __func__, n);
586cba
+                      "%s: invalid legnth of out buffer %zu\n", __func__, len);
586cba
         return false;
586cba
     }
586cba
 
586cba
+    memcpy(&ctrl, out_buf, sizeof(ctrl));
586cba
     switch (ctrl.class) {
586cba
     case VIRTIO_NET_CTRL_MAC:
586cba
         switch (ctrl.cmd) {
586cba
@@ -392,10 +389,14 @@ static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq,
586cba
     VhostVDPAState *s = opaque;
586cba
     size_t in_len, dev_written;
586cba
     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
586cba
-    /* out and in buffers sent to the device */
586cba
-    struct iovec dev_buffers[2] = {
586cba
-        { .iov_base = s->cvq_cmd_out_buffer },
586cba
-        { .iov_base = s->cvq_cmd_in_buffer },
586cba
+    /* Out buffer sent to both the vdpa device and the device model */
586cba
+    struct iovec out = {
586cba
+        .iov_base = s->cvq_cmd_out_buffer,
586cba
+    };
586cba
+    /* In buffer sent to the device */
586cba
+    const struct iovec dev_in = {
586cba
+        .iov_base = s->cvq_cmd_in_buffer,
586cba
+        .iov_len = sizeof(virtio_net_ctrl_ack),
586cba
     };
586cba
     /* in buffer used for device model */
586cba
     const struct iovec in = {
586cba
@@ -405,17 +406,15 @@ static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq,
586cba
     int r = -EINVAL;
586cba
     bool ok;
586cba
 
586cba
-    ok = vhost_vdpa_net_cvq_map_elem(s, elem, dev_buffers);
586cba
-    if (unlikely(!ok)) {
586cba
-        goto out;
586cba
-    }
586cba
-
586cba
-    ok = vhost_vdpa_net_cvq_validate_cmd(&dev_buffers[0], 1);
586cba
+    out.iov_len = iov_to_buf(elem->out_sg, elem->out_num, 0,
586cba
+                             s->cvq_cmd_out_buffer,
586cba
+                             vhost_vdpa_net_cvq_cmd_len());
586cba
+    ok = vhost_vdpa_net_cvq_validate_cmd(s->cvq_cmd_out_buffer, out.iov_len);
586cba
     if (unlikely(!ok)) {
586cba
         goto out;
586cba
     }
586cba
 
586cba
-    r = vhost_svq_add(svq, &dev_buffers[0], 1, &dev_buffers[1], 1, elem);
586cba
+    r = vhost_svq_add(svq, &out, 1, &dev_in, 1, elem);
586cba
     if (unlikely(r != 0)) {
586cba
         if (unlikely(r == -ENOSPC)) {
586cba
             qemu_log_mask(LOG_GUEST_ERROR, "%s: No space on device queue\n",
586cba
@@ -435,13 +434,13 @@ static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq,
586cba
         goto out;
586cba
     }
586cba
 
586cba
-    memcpy(&status, dev_buffers[1].iov_base, sizeof(status));
586cba
+    memcpy(&status, s->cvq_cmd_in_buffer, sizeof(status));
586cba
     if (status != VIRTIO_NET_OK) {
586cba
         goto out;
586cba
     }
586cba
 
586cba
     status = VIRTIO_NET_ERR;
586cba
-    virtio_net_handle_ctrl_iov(svq->vdev, &in, 1, dev_buffers, 1);
586cba
+    virtio_net_handle_ctrl_iov(svq->vdev, &in, 1, &out, 1);
586cba
     if (status != VIRTIO_NET_OK) {
586cba
         error_report("Bad CVQ processing in model");
586cba
     }
586cba
@@ -454,12 +453,6 @@ out:
586cba
     }
586cba
     vhost_svq_push_elem(svq, elem, MIN(in_len, sizeof(status)));
586cba
     g_free(elem);
586cba
-    if (dev_buffers[0].iov_base) {
586cba
-        vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, dev_buffers[0].iov_base);
586cba
-    }
586cba
-    if (dev_buffers[1].iov_base) {
586cba
-        vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, dev_buffers[1].iov_base);
586cba
-    }
586cba
     return r;
586cba
 }
586cba
 
586cba
-- 
586cba
2.31.1
586cba