qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: peili.dev@gmail.com
To: qemu-devel@nongnu.org
Cc: eperezma@redhat.com, Pei Li <peili.dev@gmail.com>
Subject: [PATCH 2/2] Reduce vdpa initialization / startup overhead
Date: Tue, 18 Apr 2023 18:56:38 -0400	[thread overview]
Message-ID: <20230418225638.1467969-2-peili.dev@gmail.com> (raw)
In-Reply-To: <20230418225638.1467969-1-peili.dev@gmail.com>

From: Pei Li <peili.dev@gmail.com>

Currently, part of the vdpa initialization / startup process
needs to trigger many ioctls per vq, which is very inefficient
and causing unnecessary context switch between user mode and
kernel mode.

This patch creates an additional ioctl() command, namely
VHOST_VDPA_SET_VRING_ENABLE_BATCH, that will batching
commands of VHOST_VDPA_SET_VRING_ENABLE_BATCH into a single
ioctl() call.

Signed-off-by: Pei Li <peili.dev@gmail.com>
---
 linux-headers/linux/vhost.h | 10 ++++++
 net/vhost-vdpa.c            | 70 +++++++++++++++++++++++++++++++------
 2 files changed, 70 insertions(+), 10 deletions(-)

diff --git a/linux-headers/linux/vhost.h b/linux-headers/linux/vhost.h
index 4c9ddd0a0e..f7cfa324c4 100644
--- a/linux-headers/linux/vhost.h
+++ b/linux-headers/linux/vhost.h
@@ -187,4 +187,14 @@
 #define VHOST_VDPA_SET_VRING_ENABLE_BATCH	_IOW(VHOST_VIRTIO, 0x7F, \
 					     struct vhost_vring_state)
 
+/* Batch version of VHOST_VDPA_GET_VRING_GROUP
+ *
+ * Get the group for a virtqueue: read index, write group in num,
+ * The virtqueue index is stored in the index field of
+ * vhost_vring_state. The group for this specific virtqueue is
+ * returned via num field of vhost_vring_state while batching commands.
+ */
+#define VHOST_VDPA_GET_VRING_GROUP_BATCH	_IOWR(VHOST_VIRTIO, 0x82, \
+					      struct vhost_vring_state)
+
 #endif
diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c
index 99904a0da7..ed4f2d5c49 100644
--- a/net/vhost-vdpa.c
+++ b/net/vhost-vdpa.c
@@ -377,6 +377,47 @@ static int64_t vhost_vdpa_get_vring_group(int device_fd, unsigned vq_index)
     return state.num;
 }
 
+static int64_t vhost_vdpa_get_vring_group_batch(int device_fd, unsigned vq_index)
+{
+    int r;
+    struct vhost_vring_state states[vq_index + 1];
+    int64_t cvq_group;
+
+    states[0].num = vq_index;
+
+    for (int i = 1; i <= vq_index; ++i) {
+        states[i].index = i - 1;
+    }
+
+    r = ioctl(device_fd, VHOST_VDPA_GET_VRING_GROUP_BATCH, &states[0]);
+
+    if (unlikely(r < 0)) {
+        error_report("Cannot get VQ %d group: %s", vq_index - 1,
+                     g_strerror(errno));
+        return r;
+    }
+
+    cvq_group = states[vq_index].num;
+
+    if (unlikely(cvq_group < 0)) {
+        return cvq_group;
+    }
+
+    for (int i = 1; i < vq_index; ++i) {
+        int64_t group = states[i].num;
+
+        if (unlikely(group < 0)) {
+            return group;
+        }
+
+        if (group == cvq_group) {
+            return 0;
+        }
+    }
+
+    return vq_index;
+}
+
 static int vhost_vdpa_set_address_space_id(struct vhost_vdpa *v,
                                            unsigned vq_group,
                                            unsigned asid_num)
@@ -512,19 +553,28 @@ static int vhost_vdpa_net_cvq_start(NetClientState *nc)
      * than the last vq. VQ group of last group passed in cvq_group.
      */
     cvq_index = v->dev->vq_index_end - 1;
-    cvq_group = vhost_vdpa_get_vring_group(v->device_fd, cvq_index);
-    if (unlikely(cvq_group < 0)) {
-        return cvq_group;
-    }
-    for (int i = 0; i < cvq_index; ++i) {
-        int64_t group = vhost_vdpa_get_vring_group(v->device_fd, i);
 
-        if (unlikely(group < 0)) {
-            return group;
+    if (! (backend_features & BIT_ULL(VHOST_BACKEND_F_IOCTL_BATCH))) {
+        cvq_group = vhost_vdpa_get_vring_group(v->device_fd, cvq_index);
+        if (unlikely(cvq_group < 0)) {
+            return cvq_group;
         }
+        for (int i = 0; i < cvq_index; ++i) {
+            int64_t group = vhost_vdpa_get_vring_group(v->device_fd, i);
 
-        if (group == cvq_group) {
-            return 0;
+            if (unlikely(group < 0)) {
+                return group;
+            }
+
+            if (group == cvq_group) {
+                return 0;
+            }
+        }
+    } else {
+        cvq_group = vhost_vdpa_get_vring_group_batch(v->device_fd, cvq_index + 1);
+
+        if (unlikely(cvq_group <= 0)) {
+            return cvq_group;
         }
     }
 
-- 
2.25.1



  reply	other threads:[~2023-04-19  0:40 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-18 22:56 [PATCH 1/2] Reduce vdpa initialization / startup overhead peili.dev
2023-04-18 22:56 ` peili.dev [this message]
2023-04-19 15:33 ` Eugenio Perez Martin
2023-04-20  4:18   ` Jason Wang
2023-04-20  5:25     ` Pei Li
2023-04-20  8:14       ` Jason Wang
2023-04-20  8:59       ` Eugenio Perez Martin
2023-07-18 10:55         ` Michael S. Tsirkin
2023-07-21 10:39           ` Eugenio Perez Martin
2023-07-21 20:57             ` Si-Wei Liu
2023-07-18 10:32     ` vdpa: use io_uring passthrough command for IOCTLs [was Re: [PATCH 1/2] Reduce vdpa initialization / startup overhead] Stefano Garzarella
2023-07-26  8:10       ` Jason Wang
2023-07-18 10:53 ` [PATCH 1/2] Reduce vdpa initialization / startup overhead Michael S. Tsirkin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230418225638.1467969-2-peili.dev@gmail.com \
    --to=peili.dev@gmail.com \
    --cc=eperezma@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).