902636
From 38282d996cde61261211160577b366b83cad8012 Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Mon, 27 Jan 2020 19:01:00 +0100
902636
Subject: [PATCH 029/116] virtiofsd: Start queue threads
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-26-dgilbert@redhat.com>
902636
Patchwork-id: 93479
902636
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 025/112] virtiofsd: Start queue threads
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
Start a thread for each queue when we get notified it's been started.
902636
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
fix by:
902636
Signed-off-by: Jun Piao <piaojun@huawei.com>
902636
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
(cherry picked from commit e4c55a3c144493b436e40031e2eed61a84eca47b)
902636
902636
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
902636
---
902636
 tools/virtiofsd/fuse_virtio.c | 89 +++++++++++++++++++++++++++++++++++++++++++
902636
 1 file changed, 89 insertions(+)
902636
902636
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
902636
index 4819e56..2a94bb3 100644
902636
--- a/tools/virtiofsd/fuse_virtio.c
902636
+++ b/tools/virtiofsd/fuse_virtio.c
902636
@@ -11,6 +11,7 @@
902636
  * See the file COPYING.LIB
902636
  */
902636
 
902636
+#include "qemu/osdep.h"
902636
 #include "fuse_virtio.h"
902636
 #include "fuse_i.h"
902636
 #include "standard-headers/linux/fuse.h"
902636
@@ -30,6 +31,15 @@
902636
 
902636
 #include "contrib/libvhost-user/libvhost-user.h"
902636
 
902636
+struct fv_QueueInfo {
902636
+    pthread_t thread;
902636
+    struct fv_VuDev *virtio_dev;
902636
+
902636
+    /* Our queue index, corresponds to array position */
902636
+    int qidx;
902636
+    int kick_fd;
902636
+};
902636
+
902636
 /*
902636
  * We pass the dev element into libvhost-user
902636
  * and then use it to get back to the outer
902636
@@ -38,6 +48,13 @@
902636
 struct fv_VuDev {
902636
     VuDev dev;
902636
     struct fuse_session *se;
902636
+
902636
+    /*
902636
+     * The following pair of fields are only accessed in the main
902636
+     * virtio_loop
902636
+     */
902636
+    size_t nqueues;
902636
+    struct fv_QueueInfo **qi;
902636
 };
902636
 
902636
 /* From spec */
902636
@@ -83,6 +100,75 @@ static void fv_panic(VuDev *dev, const char *err)
902636
     exit(EXIT_FAILURE);
902636
 }
902636
 
902636
+static void *fv_queue_thread(void *opaque)
902636
+{
902636
+    struct fv_QueueInfo *qi = opaque;
902636
+    fuse_log(FUSE_LOG_INFO, "%s: Start for queue %d kick_fd %d\n", __func__,
902636
+             qi->qidx, qi->kick_fd);
902636
+    while (1) {
902636
+        /* TODO */
902636
+    }
902636
+
902636
+    return NULL;
902636
+}
902636
+
902636
+/* Callback from libvhost-user on start or stop of a queue */
902636
+static void fv_queue_set_started(VuDev *dev, int qidx, bool started)
902636
+{
902636
+    struct fv_VuDev *vud = container_of(dev, struct fv_VuDev, dev);
902636
+    struct fv_QueueInfo *ourqi;
902636
+
902636
+    fuse_log(FUSE_LOG_INFO, "%s: qidx=%d started=%d\n", __func__, qidx,
902636
+             started);
902636
+    assert(qidx >= 0);
902636
+
902636
+    /*
902636
+     * Ignore additional request queues for now.  passthrough_ll.c must be
902636
+     * audited for thread-safety issues first.  It was written with a
902636
+     * well-behaved client in mind and may not protect against all types of
902636
+     * races yet.
902636
+     */
902636
+    if (qidx > 1) {
902636
+        fuse_log(FUSE_LOG_ERR,
902636
+                 "%s: multiple request queues not yet implemented, please only "
902636
+                 "configure 1 request queue\n",
902636
+                 __func__);
902636
+        exit(EXIT_FAILURE);
902636
+    }
902636
+
902636
+    if (started) {
902636
+        /* Fire up a thread to watch this queue */
902636
+        if (qidx >= vud->nqueues) {
902636
+            vud->qi = realloc(vud->qi, (qidx + 1) * sizeof(vud->qi[0]));
902636
+            assert(vud->qi);
902636
+            memset(vud->qi + vud->nqueues, 0,
902636
+                   sizeof(vud->qi[0]) * (1 + (qidx - vud->nqueues)));
902636
+            vud->nqueues = qidx + 1;
902636
+        }
902636
+        if (!vud->qi[qidx]) {
902636
+            vud->qi[qidx] = calloc(sizeof(struct fv_QueueInfo), 1);
902636
+            assert(vud->qi[qidx]);
902636
+            vud->qi[qidx]->virtio_dev = vud;
902636
+            vud->qi[qidx]->qidx = qidx;
902636
+        } else {
902636
+            /* Shouldn't have been started */
902636
+            assert(vud->qi[qidx]->kick_fd == -1);
902636
+        }
902636
+        ourqi = vud->qi[qidx];
902636
+        ourqi->kick_fd = dev->vq[qidx].kick_fd;
902636
+        if (pthread_create(&ourqi->thread, NULL, fv_queue_thread, ourqi)) {
902636
+            fuse_log(FUSE_LOG_ERR, "%s: Failed to create thread for queue %d\n",
902636
+                     __func__, qidx);
902636
+            assert(0);
902636
+        }
902636
+    } else {
902636
+        /* TODO: Kill the thread */
902636
+        assert(qidx < vud->nqueues);
902636
+        ourqi = vud->qi[qidx];
902636
+        ourqi->kick_fd = -1;
902636
+    }
902636
+}
902636
+
902636
 static bool fv_queue_order(VuDev *dev, int qidx)
902636
 {
902636
     return false;
902636
@@ -92,6 +178,9 @@ static const VuDevIface fv_iface = {
902636
     .get_features = fv_get_features,
902636
     .set_features = fv_set_features,
902636
 
902636
+    /* Don't need process message, we've not got any at vhost-user level */
902636
+    .queue_set_started = fv_queue_set_started,
902636
+
902636
     .queue_is_processed_in_order = fv_queue_order,
902636
 };
902636
 
902636
-- 
902636
1.8.3.1
902636