yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-virtiofsd-Kill-threads-when-queues-are-stopped.patch

22c213
From b37344c38b866c7e7fb773b4a3172a39306bac7e Mon Sep 17 00:00:00 2001
22c213
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
22c213
Date: Mon, 27 Jan 2020 19:01:42 +0100
22c213
Subject: [PATCH 071/116] virtiofsd: Kill threads when queues are stopped
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-68-dgilbert@redhat.com>
22c213
Patchwork-id: 93522
22c213
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 067/112] virtiofsd: Kill threads when queues are stopped
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
Kill the threads we've started when the queues get stopped.
22c213
22c213
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
With improvements by:
22c213
Signed-off-by: Eryu Guan <eguan@linux.alibaba.com>
22c213
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
22c213
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
(cherry picked from commit 10477ac47fc57d00a84802ff97c15450cd8021c1)
22c213
22c213
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
22c213
---
22c213
 tools/virtiofsd/fuse_virtio.c | 51 +++++++++++++++++++++++++++++++++++++------
22c213
 1 file changed, 44 insertions(+), 7 deletions(-)
22c213
22c213
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
22c213
index 872968f..7a8774a 100644
22c213
--- a/tools/virtiofsd/fuse_virtio.c
22c213
+++ b/tools/virtiofsd/fuse_virtio.c
22c213
@@ -41,6 +41,7 @@ struct fv_QueueInfo {
22c213
     /* Our queue index, corresponds to array position */
22c213
     int qidx;
22c213
     int kick_fd;
22c213
+    int kill_fd; /* For killing the thread */
22c213
 
22c213
     /* The element for the command currently being processed */
22c213
     VuVirtqElement *qe;
22c213
@@ -412,14 +413,17 @@ static void *fv_queue_thread(void *opaque)
22c213
     fuse_log(FUSE_LOG_INFO, "%s: Start for queue %d kick_fd %d\n", __func__,
22c213
              qi->qidx, qi->kick_fd);
22c213
     while (1) {
22c213
-        struct pollfd pf[1];
22c213
+        struct pollfd pf[2];
22c213
         pf[0].fd = qi->kick_fd;
22c213
         pf[0].events = POLLIN;
22c213
         pf[0].revents = 0;
22c213
+        pf[1].fd = qi->kill_fd;
22c213
+        pf[1].events = POLLIN;
22c213
+        pf[1].revents = 0;
22c213
 
22c213
         fuse_log(FUSE_LOG_DEBUG, "%s: Waiting for Queue %d event\n", __func__,
22c213
                  qi->qidx);
22c213
-        int poll_res = ppoll(pf, 1, NULL, NULL);
22c213
+        int poll_res = ppoll(pf, 2, NULL, NULL);
22c213
 
22c213
         if (poll_res == -1) {
22c213
             if (errno == EINTR) {
22c213
@@ -430,12 +434,23 @@ static void *fv_queue_thread(void *opaque)
22c213
             fuse_log(FUSE_LOG_ERR, "fv_queue_thread ppoll: %m\n");
22c213
             break;
22c213
         }
22c213
-        assert(poll_res == 1);
22c213
+        assert(poll_res >= 1);
22c213
         if (pf[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
22c213
             fuse_log(FUSE_LOG_ERR, "%s: Unexpected poll revents %x Queue %d\n",
22c213
                      __func__, pf[0].revents, qi->qidx);
22c213
             break;
22c213
         }
22c213
+        if (pf[1].revents & (POLLERR | POLLHUP | POLLNVAL)) {
22c213
+            fuse_log(FUSE_LOG_ERR,
22c213
+                     "%s: Unexpected poll revents %x Queue %d killfd\n",
22c213
+                     __func__, pf[1].revents, qi->qidx);
22c213
+            break;
22c213
+        }
22c213
+        if (pf[1].revents) {
22c213
+            fuse_log(FUSE_LOG_INFO, "%s: kill event on queue %d - quitting\n",
22c213
+                     __func__, qi->qidx);
22c213
+            break;
22c213
+        }
22c213
         assert(pf[0].revents & POLLIN);
22c213
         fuse_log(FUSE_LOG_DEBUG, "%s: Got queue event on Queue %d\n", __func__,
22c213
                  qi->qidx);
22c213
@@ -589,6 +604,28 @@ out:
22c213
     return NULL;
22c213
 }
22c213
 
22c213
+static void fv_queue_cleanup_thread(struct fv_VuDev *vud, int qidx)
22c213
+{
22c213
+    int ret;
22c213
+    struct fv_QueueInfo *ourqi;
22c213
+
22c213
+    assert(qidx < vud->nqueues);
22c213
+    ourqi = vud->qi[qidx];
22c213
+
22c213
+    /* Kill the thread */
22c213
+    if (eventfd_write(ourqi->kill_fd, 1)) {
22c213
+        fuse_log(FUSE_LOG_ERR, "Eventfd_write for queue %d: %s\n",
22c213
+                 qidx, strerror(errno));
22c213
+    }
22c213
+    ret = pthread_join(ourqi->thread, NULL);
22c213
+    if (ret) {
22c213
+        fuse_log(FUSE_LOG_ERR, "%s: Failed to join thread idx %d err %d\n",
22c213
+                 __func__, qidx, ret);
22c213
+    }
22c213
+    close(ourqi->kill_fd);
22c213
+    ourqi->kick_fd = -1;
22c213
+}
22c213
+
22c213
 /* Callback from libvhost-user on start or stop of a queue */
22c213
 static void fv_queue_set_started(VuDev *dev, int qidx, bool started)
22c213
 {
22c213
@@ -633,16 +670,16 @@ static void fv_queue_set_started(VuDev *dev, int qidx, bool started)
22c213
         }
22c213
         ourqi = vud->qi[qidx];
22c213
         ourqi->kick_fd = dev->vq[qidx].kick_fd;
22c213
+
22c213
+        ourqi->kill_fd = eventfd(0, EFD_CLOEXEC | EFD_SEMAPHORE);
22c213
+        assert(ourqi->kill_fd != -1);
22c213
         if (pthread_create(&ourqi->thread, NULL, fv_queue_thread, ourqi)) {
22c213
             fuse_log(FUSE_LOG_ERR, "%s: Failed to create thread for queue %d\n",
22c213
                      __func__, qidx);
22c213
             assert(0);
22c213
         }
22c213
     } else {
22c213
-        /* TODO: Kill the thread */
22c213
-        assert(qidx < vud->nqueues);
22c213
-        ourqi = vud->qi[qidx];
22c213
-        ourqi->kick_fd = -1;
22c213
+        fv_queue_cleanup_thread(vud, qidx);
22c213
     }
22c213
 }
22c213
 
22c213
-- 
22c213
1.8.3.1
22c213