Blame SOURCES/kvm-virtiofsd-stop-all-queue-threads-on-exit-in-virtio_l.patch

902636
From 06a24b54c94345b436d888a48b92fafa967c3d58 Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Mon, 27 Jan 2020 19:02:25 +0100
902636
Subject: [PATCH 114/116] virtiofsd: stop all queue threads on exit in
902636
 virtio_loop()
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-111-dgilbert@redhat.com>
902636
Patchwork-id: 93564
902636
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 110/112] virtiofsd: stop all queue threads on exit in virtio_loop()
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: Eryu Guan <eguan@linux.alibaba.com>
902636
902636
On guest graceful shutdown, virtiofsd receives VHOST_USER_GET_VRING_BASE
902636
request from VMM and shuts down virtqueues by calling fv_set_started(),
902636
which joins fv_queue_thread() threads. So when virtio_loop() returns,
902636
there should be no thread is still accessing data in fuse session and/or
902636
virtio dev.
902636
902636
But on abnormal exit, e.g. guest got killed for whatever reason,
902636
vhost-user socket is closed and virtio_loop() breaks out the main loop
902636
and returns to main(). But it's possible fv_queue_worker()s are still
902636
working and accessing fuse session and virtio dev, which results in
902636
crash or use-after-free.
902636
902636
Fix it by stopping fv_queue_thread()s before virtio_loop() returns,
902636
to make sure there's no-one could access fuse session and virtio dev.
902636
902636
Reported-by: Qingming Su <qingming.su@linux.alibaba.com>
902636
Signed-off-by: Eryu Guan <eguan@linux.alibaba.com>
902636
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
(cherry picked from commit 9883df8ccae6d744a0c8d9cbf9d62b1797d70ebd)
902636
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
902636
---
902636
 tools/virtiofsd/fuse_virtio.c | 13 +++++++++++++
902636
 1 file changed, 13 insertions(+)
902636
902636
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
902636
index 9f65823..80a6e92 100644
902636
--- a/tools/virtiofsd/fuse_virtio.c
902636
+++ b/tools/virtiofsd/fuse_virtio.c
902636
@@ -815,6 +815,19 @@ int virtio_loop(struct fuse_session *se)
902636
         }
902636
     }
902636
 
902636
+    /*
902636
+     * Make sure all fv_queue_thread()s quit on exit, as we're about to
902636
+     * free virtio dev and fuse session, no one should access them anymore.
902636
+     */
902636
+    for (int i = 0; i < se->virtio_dev->nqueues; i++) {
902636
+        if (!se->virtio_dev->qi[i]) {
902636
+            continue;
902636
+        }
902636
+
902636
+        fuse_log(FUSE_LOG_INFO, "%s: Stopping queue %d thread\n", __func__, i);
902636
+        fv_queue_cleanup_thread(se->virtio_dev, i);
902636
+    }
902636
+
902636
     fuse_log(FUSE_LOG_INFO, "%s: Exit\n", __func__);
902636
 
902636
     return 0;
902636
-- 
902636
1.8.3.1
902636