yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-virtiofsd-prevent-fv_queue_thread-vs-virtio_loop-rac.patch

ddf19c
From 69c6a829f8136a8c95ccdf480f2fd0173d64b6ec Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Mon, 27 Jan 2020 19:02:05 +0100
ddf19c
Subject: [PATCH 094/116] virtiofsd: prevent fv_queue_thread() vs virtio_loop()
ddf19c
 races
ddf19c
MIME-Version: 1.0
ddf19c
Content-Type: text/plain; charset=UTF-8
ddf19c
Content-Transfer-Encoding: 8bit
ddf19c
ddf19c
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
Message-id: <20200127190227.40942-91-dgilbert@redhat.com>
ddf19c
Patchwork-id: 93544
ddf19c
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 090/112] virtiofsd: prevent fv_queue_thread() vs virtio_loop() races
ddf19c
Bugzilla: 1694164
ddf19c
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
ddf19c
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
ddf19c
ddf19c
From: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
ddf19c
We call into libvhost-user from the virtqueue handler thread and the
ddf19c
vhost-user message processing thread without a lock.  There is nothing
ddf19c
protecting the virtqueue handler thread if the vhost-user message
ddf19c
processing thread changes the virtqueue or memory table while it is
ddf19c
running.
ddf19c
ddf19c
This patch introduces a read-write lock.  Virtqueue handler threads are
ddf19c
readers.  The vhost-user message processing thread is a writer.  This
ddf19c
will allow concurrency for multiqueue in the future while protecting
ddf19c
against fv_queue_thread() vs virtio_loop() races.
ddf19c
ddf19c
Note that the critical sections could be made smaller but it would be
ddf19c
more invasive and require libvhost-user changes.  Let's start simple and
ddf19c
improve performance later, if necessary.  Another option would be an
ddf19c
RCU-style approach with lighter-weight primitives.
ddf19c
ddf19c
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit e7b337326d594b71b07cd6dbb332c49c122c80a4)
ddf19c
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/fuse_virtio.c | 34 +++++++++++++++++++++++++++++++++-
ddf19c
 1 file changed, 33 insertions(+), 1 deletion(-)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
ddf19c
index fb8d6d1..f6242f9 100644
ddf19c
--- a/tools/virtiofsd/fuse_virtio.c
ddf19c
+++ b/tools/virtiofsd/fuse_virtio.c
ddf19c
@@ -59,6 +59,18 @@ struct fv_VuDev {
ddf19c
     struct fuse_session *se;
ddf19c
 
ddf19c
     /*
ddf19c
+     * Either handle virtqueues or vhost-user protocol messages.  Don't do
ddf19c
+     * both at the same time since that could lead to race conditions if
ddf19c
+     * virtqueues or memory tables change while another thread is accessing
ddf19c
+     * them.
ddf19c
+     *
ddf19c
+     * The assumptions are:
ddf19c
+     * 1. fv_queue_thread() reads/writes to virtqueues and only reads VuDev.
ddf19c
+     * 2. virtio_loop() reads/writes virtqueues and VuDev.
ddf19c
+     */
ddf19c
+    pthread_rwlock_t vu_dispatch_rwlock;
ddf19c
+
ddf19c
+    /*
ddf19c
      * The following pair of fields are only accessed in the main
ddf19c
      * virtio_loop
ddf19c
      */
ddf19c
@@ -415,6 +427,8 @@ static void *fv_queue_thread(void *opaque)
ddf19c
              qi->qidx, qi->kick_fd);
ddf19c
     while (1) {
ddf19c
         struct pollfd pf[2];
ddf19c
+        int ret;
ddf19c
+
ddf19c
         pf[0].fd = qi->kick_fd;
ddf19c
         pf[0].events = POLLIN;
ddf19c
         pf[0].revents = 0;
ddf19c
@@ -461,6 +475,9 @@ static void *fv_queue_thread(void *opaque)
ddf19c
             fuse_log(FUSE_LOG_ERR, "Eventfd_read for queue: %m\n");
ddf19c
             break;
ddf19c
         }
ddf19c
+        /* Mutual exclusion with virtio_loop() */
ddf19c
+        ret = pthread_rwlock_rdlock(&qi->virtio_dev->vu_dispatch_rwlock);
ddf19c
+        assert(ret == 0); /* there is no possible error case */
ddf19c
         /* out is from guest, in is too guest */
ddf19c
         unsigned int in_bytes, out_bytes;
ddf19c
         vu_queue_get_avail_bytes(dev, q, &in_bytes, &out_bytes, ~0, ~0);
ddf19c
@@ -469,6 +486,7 @@ static void *fv_queue_thread(void *opaque)
ddf19c
                  "%s: Queue %d gave evalue: %zx available: in: %u out: %u\n",
ddf19c
                  __func__, qi->qidx, (size_t)evalue, in_bytes, out_bytes);
ddf19c
 
ddf19c
+
ddf19c
         while (1) {
ddf19c
             bool allocated_bufv = false;
ddf19c
             struct fuse_bufvec bufv;
ddf19c
@@ -597,6 +615,8 @@ static void *fv_queue_thread(void *opaque)
ddf19c
             free(elem);
ddf19c
             elem = NULL;
ddf19c
         }
ddf19c
+
ddf19c
+        pthread_rwlock_unlock(&qi->virtio_dev->vu_dispatch_rwlock);
ddf19c
     }
ddf19c
 out:
ddf19c
     pthread_mutex_destroy(&ch.lock);
ddf19c
@@ -711,6 +731,8 @@ int virtio_loop(struct fuse_session *se)
ddf19c
 
ddf19c
     while (!fuse_session_exited(se)) {
ddf19c
         struct pollfd pf[1];
ddf19c
+        bool ok;
ddf19c
+        int ret;
ddf19c
         pf[0].fd = se->vu_socketfd;
ddf19c
         pf[0].events = POLLIN;
ddf19c
         pf[0].revents = 0;
ddf19c
@@ -735,7 +757,15 @@ int virtio_loop(struct fuse_session *se)
ddf19c
         }
ddf19c
         assert(pf[0].revents & POLLIN);
ddf19c
         fuse_log(FUSE_LOG_DEBUG, "%s: Got VU event\n", __func__);
ddf19c
-        if (!vu_dispatch(&se->virtio_dev->dev)) {
ddf19c
+        /* Mutual exclusion with fv_queue_thread() */
ddf19c
+        ret = pthread_rwlock_wrlock(&se->virtio_dev->vu_dispatch_rwlock);
ddf19c
+        assert(ret == 0); /* there is no possible error case */
ddf19c
+
ddf19c
+        ok = vu_dispatch(&se->virtio_dev->dev);
ddf19c
+
ddf19c
+        pthread_rwlock_unlock(&se->virtio_dev->vu_dispatch_rwlock);
ddf19c
+
ddf19c
+        if (!ok) {
ddf19c
             fuse_log(FUSE_LOG_ERR, "%s: vu_dispatch failed\n", __func__);
ddf19c
             break;
ddf19c
         }
ddf19c
@@ -877,6 +907,7 @@ int virtio_session_mount(struct fuse_session *se)
ddf19c
 
ddf19c
     se->vu_socketfd = data_sock;
ddf19c
     se->virtio_dev->se = se;
ddf19c
+    pthread_rwlock_init(&se->virtio_dev->vu_dispatch_rwlock, NULL);
ddf19c
     vu_init(&se->virtio_dev->dev, 2, se->vu_socketfd, fv_panic, fv_set_watch,
ddf19c
             fv_remove_watch, &fv_iface);
ddf19c
 
ddf19c
@@ -892,6 +923,7 @@ void virtio_session_close(struct fuse_session *se)
ddf19c
     }
ddf19c
 
ddf19c
     free(se->virtio_dev->qi);
ddf19c
+    pthread_rwlock_destroy(&se->virtio_dev->vu_dispatch_rwlock);
ddf19c
     free(se->virtio_dev);
ddf19c
     se->virtio_dev = NULL;
ddf19c
 }
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c