yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
902636
From 7d2efc3e4af15eff57b0c38cff7c81b371a98303 Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Mon, 27 Jan 2020 19:01:06 +0100
902636
Subject: [PATCH 035/116] virtiofsd: Fast path for virtio read
902636
MIME-Version: 1.0
902636
Content-Type: text/plain; charset=UTF-8
902636
Content-Transfer-Encoding: 8bit
902636
902636
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
Message-id: <20200127190227.40942-32-dgilbert@redhat.com>
902636
Patchwork-id: 93480
902636
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 031/112] virtiofsd: Fast path for virtio read
902636
Bugzilla: 1694164
902636
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
902636
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
902636
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
902636
Readv the data straight into the guests buffer.
902636
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
With fix by:
902636
Signed-off-by: Eryu Guan <eguan@linux.alibaba.com>
902636
Reviewed-by: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
(cherry picked from commit eb49d187ef5134483a34c970bbfece28aaa686a7)
902636
902636
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
902636
---
902636
 tools/virtiofsd/fuse_lowlevel.c |   5 ++
902636
 tools/virtiofsd/fuse_virtio.c   | 162 ++++++++++++++++++++++++++++++++++++++++
902636
 tools/virtiofsd/fuse_virtio.h   |   4 +
902636
 3 files changed, 171 insertions(+)
902636
902636
diff --git a/tools/virtiofsd/fuse_lowlevel.c b/tools/virtiofsd/fuse_lowlevel.c
902636
index 380d93b..4f4684d 100644
902636
--- a/tools/virtiofsd/fuse_lowlevel.c
902636
+++ b/tools/virtiofsd/fuse_lowlevel.c
902636
@@ -475,6 +475,11 @@ static int fuse_send_data_iov_fallback(struct fuse_session *se,
902636
         return fuse_send_msg(se, ch, iov, iov_count);
902636
     }
902636
 
902636
+    if (fuse_lowlevel_is_virtio(se) && buf->count == 1 &&
902636
+        buf->buf[0].flags == (FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK)) {
902636
+        return virtio_send_data_iov(se, ch, iov, iov_count, buf, len);
902636
+    }
902636
+
902636
     abort(); /* Will have taken vhost path */
902636
     return 0;
902636
 }
902636
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
902636
index f1adeb6..7e2711b 100644
902636
--- a/tools/virtiofsd/fuse_virtio.c
902636
+++ b/tools/virtiofsd/fuse_virtio.c
902636
@@ -230,6 +230,168 @@ err:
902636
     return ret;
902636
 }
902636
 
902636
+/*
902636
+ * Callback from fuse_send_data_iov_* when it's virtio and the buffer
902636
+ * is a single FD with FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK
902636
+ * We need send the iov and then the buffer.
902636
+ * Return 0 on success
902636
+ */
902636
+int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
902636
+                         struct iovec *iov, int count, struct fuse_bufvec *buf,
902636
+                         size_t len)
902636
+{
902636
+    int ret = 0;
902636
+    VuVirtqElement *elem;
902636
+    VuVirtq *q;
902636
+
902636
+    assert(count >= 1);
902636
+    assert(iov[0].iov_len >= sizeof(struct fuse_out_header));
902636
+
902636
+    struct fuse_out_header *out = iov[0].iov_base;
902636
+    /* TODO: Endianness! */
902636
+
902636
+    size_t iov_len = iov_size(iov, count);
902636
+    size_t tosend_len = iov_len + len;
902636
+
902636
+    out->len = tosend_len;
902636
+
902636
+    fuse_log(FUSE_LOG_DEBUG, "%s: count=%d len=%zd iov_len=%zd\n", __func__,
902636
+             count, len, iov_len);
902636
+
902636
+    /* unique == 0 is notification which we don't support */
902636
+    assert(out->unique);
902636
+
902636
+    /* For virtio we always have ch */
902636
+    assert(ch);
902636
+    assert(!ch->qi->reply_sent);
902636
+    elem = ch->qi->qe;
902636
+    q = &ch->qi->virtio_dev->dev.vq[ch->qi->qidx];
902636
+
902636
+    /* The 'in' part of the elem is to qemu */
902636
+    unsigned int in_num = elem->in_num;
902636
+    struct iovec *in_sg = elem->in_sg;
902636
+    size_t in_len = iov_size(in_sg, in_num);
902636
+    fuse_log(FUSE_LOG_DEBUG, "%s: elem %d: with %d in desc of length %zd\n",
902636
+             __func__, elem->index, in_num, in_len);
902636
+
902636
+    /*
902636
+     * The elem should have room for a 'fuse_out_header' (out from fuse)
902636
+     * plus the data based on the len in the header.
902636
+     */
902636
+    if (in_len < sizeof(struct fuse_out_header)) {
902636
+        fuse_log(FUSE_LOG_ERR, "%s: elem %d too short for out_header\n",
902636
+                 __func__, elem->index);
902636
+        ret = E2BIG;
902636
+        goto err;
902636
+    }
902636
+    if (in_len < tosend_len) {
902636
+        fuse_log(FUSE_LOG_ERR, "%s: elem %d too small for data len %zd\n",
902636
+                 __func__, elem->index, tosend_len);
902636
+        ret = E2BIG;
902636
+        goto err;
902636
+    }
902636
+
902636
+    /* TODO: Limit to 'len' */
902636
+
902636
+    /* First copy the header data from iov->in_sg */
902636
+    copy_iov(iov, count, in_sg, in_num, iov_len);
902636
+
902636
+    /*
902636
+     * Build a copy of the the in_sg iov so we can skip bits in it,
902636
+     * including changing the offsets
902636
+     */
902636
+    struct iovec *in_sg_cpy = calloc(sizeof(struct iovec), in_num);
902636
+    assert(in_sg_cpy);
902636
+    memcpy(in_sg_cpy, in_sg, sizeof(struct iovec) * in_num);
902636
+    /* These get updated as we skip */
902636
+    struct iovec *in_sg_ptr = in_sg_cpy;
902636
+    int in_sg_cpy_count = in_num;
902636
+
902636
+    /* skip over parts of in_sg that contained the header iov */
902636
+    size_t skip_size = iov_len;
902636
+
902636
+    size_t in_sg_left = 0;
902636
+    do {
902636
+        while (skip_size != 0 && in_sg_cpy_count) {
902636
+            if (skip_size >= in_sg_ptr[0].iov_len) {
902636
+                skip_size -= in_sg_ptr[0].iov_len;
902636
+                in_sg_ptr++;
902636
+                in_sg_cpy_count--;
902636
+            } else {
902636
+                in_sg_ptr[0].iov_len -= skip_size;
902636
+                in_sg_ptr[0].iov_base += skip_size;
902636
+                break;
902636
+            }
902636
+        }
902636
+
902636
+        int i;
902636
+        for (i = 0, in_sg_left = 0; i < in_sg_cpy_count; i++) {
902636
+            in_sg_left += in_sg_ptr[i].iov_len;
902636
+        }
902636
+        fuse_log(FUSE_LOG_DEBUG,
902636
+                 "%s: after skip skip_size=%zd in_sg_cpy_count=%d "
902636
+                 "in_sg_left=%zd\n",
902636
+                 __func__, skip_size, in_sg_cpy_count, in_sg_left);
902636
+        ret = preadv(buf->buf[0].fd, in_sg_ptr, in_sg_cpy_count,
902636
+                     buf->buf[0].pos);
902636
+
902636
+        if (ret == -1) {
902636
+            ret = errno;
902636
+            fuse_log(FUSE_LOG_DEBUG, "%s: preadv failed (%m) len=%zd\n",
902636
+                     __func__, len);
902636
+            free(in_sg_cpy);
902636
+            goto err;
902636
+        }
902636
+        fuse_log(FUSE_LOG_DEBUG, "%s: preadv ret=%d len=%zd\n", __func__,
902636
+                 ret, len);
902636
+        if (ret < len && ret) {
902636
+            fuse_log(FUSE_LOG_DEBUG, "%s: ret < len\n", __func__);
902636
+            /* Skip over this much next time around */
902636
+            skip_size = ret;
902636
+            buf->buf[0].pos += ret;
902636
+            len -= ret;
902636
+
902636
+            /* Lets do another read */
902636
+            continue;
902636
+        }
902636
+        if (!ret) {
902636
+            /* EOF case? */
902636
+            fuse_log(FUSE_LOG_DEBUG, "%s: !ret in_sg_left=%zd\n", __func__,
902636
+                     in_sg_left);
902636
+            break;
902636
+        }
902636
+        if (ret != len) {
902636
+            fuse_log(FUSE_LOG_DEBUG, "%s: ret!=len\n", __func__);
902636
+            ret = EIO;
902636
+            free(in_sg_cpy);
902636
+            goto err;
902636
+        }
902636
+        in_sg_left -= ret;
902636
+        len -= ret;
902636
+    } while (in_sg_left);
902636
+    free(in_sg_cpy);
902636
+
902636
+    /* Need to fix out->len on EOF */
902636
+    if (len) {
902636
+        struct fuse_out_header *out_sg = in_sg[0].iov_base;
902636
+
902636
+        tosend_len -= len;
902636
+        out_sg->len = tosend_len;
902636
+    }
902636
+
902636
+    ret = 0;
902636
+
902636
+    vu_queue_push(&se->virtio_dev->dev, q, elem, tosend_len);
902636
+    vu_queue_notify(&se->virtio_dev->dev, q);
902636
+
902636
+err:
902636
+    if (ret == 0) {
902636
+        ch->qi->reply_sent = true;
902636
+    }
902636
+
902636
+    return ret;
902636
+}
902636
+
902636
 /* Thread function for individual queues, created when a queue is 'started' */
902636
 static void *fv_queue_thread(void *opaque)
902636
 {
902636
diff --git a/tools/virtiofsd/fuse_virtio.h b/tools/virtiofsd/fuse_virtio.h
902636
index 135a148..cc676b9 100644
902636
--- a/tools/virtiofsd/fuse_virtio.h
902636
+++ b/tools/virtiofsd/fuse_virtio.h
902636
@@ -26,4 +26,8 @@ int virtio_loop(struct fuse_session *se);
902636
 int virtio_send_msg(struct fuse_session *se, struct fuse_chan *ch,
902636
                     struct iovec *iov, int count);
902636
 
902636
+int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
902636
+                         struct iovec *iov, int count,
902636
+                         struct fuse_bufvec *buf, size_t len);
902636
+
902636
 #endif
902636
-- 
902636
1.8.3.1
902636