Blame 0075-contrib-libvhost-user-Protect-slave-fd-with-mutex.patch

1d442b
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
1d442b
Date: Mon, 27 Jan 2020 19:01:44 +0000
1d442b
Subject: [PATCH] contrib/libvhost-user: Protect slave fd with mutex
1d442b
MIME-Version: 1.0
1d442b
Content-Type: text/plain; charset=UTF-8
1d442b
Content-Transfer-Encoding: 8bit
1d442b
1d442b
In future patches we'll be performing commands on the slave-fd driven
1d442b
by commands on queues, since those queues will be driven by individual
1d442b
threads we need to make sure they don't attempt to use the slave-fd
1d442b
for multiple commands in parallel.
1d442b
1d442b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@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 c25c02b9e6a196be87a818f459c426556b24770d)
1d442b
---
1d442b
 contrib/libvhost-user/libvhost-user.c | 24 ++++++++++++++++++++----
1d442b
 contrib/libvhost-user/libvhost-user.h |  3 +++
1d442b
 2 files changed, 23 insertions(+), 4 deletions(-)
1d442b
1d442b
diff --git a/contrib/libvhost-user/libvhost-user.c b/contrib/libvhost-user/libvhost-user.c
1d442b
index ec27b78ff1..63e41062a4 100644
1d442b
--- a/contrib/libvhost-user/libvhost-user.c
1d442b
+++ b/contrib/libvhost-user/libvhost-user.c
1d442b
@@ -392,26 +392,37 @@ vu_send_reply(VuDev *dev, int conn_fd, VhostUserMsg *vmsg)
1d442b
     return vu_message_write(dev, conn_fd, vmsg);
1d442b
 }
1d442b
 
1d442b
+/*
1d442b
+ * Processes a reply on the slave channel.
1d442b
+ * Entered with slave_mutex held and releases it before exit.
1d442b
+ * Returns true on success.
1d442b
+ */
1d442b
 static bool
1d442b
 vu_process_message_reply(VuDev *dev, const VhostUserMsg *vmsg)
1d442b
 {
1d442b
     VhostUserMsg msg_reply;
1d442b
+    bool result = false;
1d442b
 
1d442b
     if ((vmsg->flags & VHOST_USER_NEED_REPLY_MASK) == 0) {
1d442b
-        return true;
1d442b
+        result = true;
1d442b
+        goto out;
1d442b
     }
1d442b
 
1d442b
     if (!vu_message_read(dev, dev->slave_fd, &msg_reply)) {
1d442b
-        return false;
1d442b
+        goto out;
1d442b
     }
1d442b
 
1d442b
     if (msg_reply.request != vmsg->request) {
1d442b
         DPRINT("Received unexpected msg type. Expected %d received %d",
1d442b
                vmsg->request, msg_reply.request);
1d442b
-        return false;
1d442b
+        goto out;
1d442b
     }
1d442b
 
1d442b
-    return msg_reply.payload.u64 == 0;
1d442b
+    result = msg_reply.payload.u64 == 0;
1d442b
+
1d442b
+out:
1d442b
+    pthread_mutex_unlock(&dev->slave_mutex);
1d442b
+    return result;
1d442b
 }
1d442b
 
1d442b
 /* Kick the log_call_fd if required. */
1d442b
@@ -1105,10 +1116,13 @@ bool vu_set_queue_host_notifier(VuDev *dev, VuVirtq *vq, int fd,
1d442b
         return false;
1d442b
     }
1d442b
 
1d442b
+    pthread_mutex_lock(&dev->slave_mutex);
1d442b
     if (!vu_message_write(dev, dev->slave_fd, &vmsg)) {
1d442b
+        pthread_mutex_unlock(&dev->slave_mutex);
1d442b
         return false;
1d442b
     }
1d442b
 
1d442b
+    /* Also unlocks the slave_mutex */
1d442b
     return vu_process_message_reply(dev, &vmsg);
1d442b
 }
1d442b
 
1d442b
@@ -1628,6 +1642,7 @@ vu_deinit(VuDev *dev)
1d442b
         close(dev->slave_fd);
1d442b
         dev->slave_fd = -1;
1d442b
     }
1d442b
+    pthread_mutex_destroy(&dev->slave_mutex);
1d442b
 
1d442b
     if (dev->sock != -1) {
1d442b
         close(dev->sock);
1d442b
@@ -1663,6 +1678,7 @@ vu_init(VuDev *dev,
1d442b
     dev->remove_watch = remove_watch;
1d442b
     dev->iface = iface;
1d442b
     dev->log_call_fd = -1;
1d442b
+    pthread_mutex_init(&dev->slave_mutex, NULL);
1d442b
     dev->slave_fd = -1;
1d442b
     dev->max_queues = max_queues;
1d442b
 
1d442b
diff --git a/contrib/libvhost-user/libvhost-user.h b/contrib/libvhost-user/libvhost-user.h
1d442b
index 46b600799b..1844b6f8d4 100644
1d442b
--- a/contrib/libvhost-user/libvhost-user.h
1d442b
+++ b/contrib/libvhost-user/libvhost-user.h
1d442b
@@ -19,6 +19,7 @@
1d442b
 #include <stddef.h>
1d442b
 #include <sys/poll.h>
1d442b
 #include <linux/vhost.h>
1d442b
+#include <pthread.h>
1d442b
 #include "standard-headers/linux/virtio_ring.h"
1d442b
 
1d442b
 /* Based on qemu/hw/virtio/vhost-user.c */
1d442b
@@ -355,6 +356,8 @@ struct VuDev {
1d442b
     VuVirtq *vq;
1d442b
     VuDevInflightInfo inflight_info;
1d442b
     int log_call_fd;
1d442b
+    /* Must be held while using slave_fd */
1d442b
+    pthread_mutex_t slave_mutex;
1d442b
     int slave_fd;
1d442b
     uint64_t log_size;
1d442b
     uint8_t *log_table;