dcavalca / rpms / qemu

Forked from rpms/qemu a year ago
Clone

Blame 0096-virtiofsd-prevent-fv_queue_thread-vs-virtio_loop-rac.patch

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