yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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