peterdelevoryas / rpms / qemu

Forked from rpms/qemu 2 years ago
Clone

Blame 0116-virtiofsd-stop-all-queue-threads-on-exit-in-virtio_l.patch

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