cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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