cryptospore / rpms / qemu-kvm

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