Blame SOURCES/kvm-contrib-libvhost-user-Protect-slave-fd-with-mutex.patch

902636
From 548de8acbf0137b6e49a14b63682badfff037d23 Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Mon, 27 Jan 2020 19:01:44 +0100
902636
Subject: [PATCH 073/116] contrib/libvhost-user: Protect slave fd with mutex
902636
MIME-Version: 1.0
902636
Content-Type: text/plain; charset=UTF-8
902636
Content-Transfer-Encoding: 8bit
902636
902636
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
Message-id: <20200127190227.40942-70-dgilbert@redhat.com>
902636
Patchwork-id: 93523
902636
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 069/112] contrib/libvhost-user: Protect slave fd with mutex
902636
Bugzilla: 1694164
902636
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
902636
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
902636
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
902636
In future patches we'll be performing commands on the slave-fd driven
902636
by commands on queues, since those queues will be driven by individual
902636
threads we need to make sure they don't attempt to use the slave-fd
902636
for multiple commands in parallel.
902636
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
(cherry picked from commit c25c02b9e6a196be87a818f459c426556b24770d)
902636
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
902636
---
902636
 contrib/libvhost-user/libvhost-user.c | 24 ++++++++++++++++++++----
902636
 contrib/libvhost-user/libvhost-user.h |  3 +++
902636
 2 files changed, 23 insertions(+), 4 deletions(-)
902636
902636
diff --git a/contrib/libvhost-user/libvhost-user.c b/contrib/libvhost-user/libvhost-user.c
902636
index ec27b78..63e4106 100644
902636
--- a/contrib/libvhost-user/libvhost-user.c
902636
+++ b/contrib/libvhost-user/libvhost-user.c
902636
@@ -392,26 +392,37 @@ vu_send_reply(VuDev *dev, int conn_fd, VhostUserMsg *vmsg)
902636
     return vu_message_write(dev, conn_fd, vmsg);
902636
 }
902636
 
902636
+/*
902636
+ * Processes a reply on the slave channel.
902636
+ * Entered with slave_mutex held and releases it before exit.
902636
+ * Returns true on success.
902636
+ */
902636
 static bool
902636
 vu_process_message_reply(VuDev *dev, const VhostUserMsg *vmsg)
902636
 {
902636
     VhostUserMsg msg_reply;
902636
+    bool result = false;
902636
 
902636
     if ((vmsg->flags & VHOST_USER_NEED_REPLY_MASK) == 0) {
902636
-        return true;
902636
+        result = true;
902636
+        goto out;
902636
     }
902636
 
902636
     if (!vu_message_read(dev, dev->slave_fd, &msg_reply)) {
902636
-        return false;
902636
+        goto out;
902636
     }
902636
 
902636
     if (msg_reply.request != vmsg->request) {
902636
         DPRINT("Received unexpected msg type. Expected %d received %d",
902636
                vmsg->request, msg_reply.request);
902636
-        return false;
902636
+        goto out;
902636
     }
902636
 
902636
-    return msg_reply.payload.u64 == 0;
902636
+    result = msg_reply.payload.u64 == 0;
902636
+
902636
+out:
902636
+    pthread_mutex_unlock(&dev->slave_mutex);
902636
+    return result;
902636
 }
902636
 
902636
 /* Kick the log_call_fd if required. */
902636
@@ -1105,10 +1116,13 @@ bool vu_set_queue_host_notifier(VuDev *dev, VuVirtq *vq, int fd,
902636
         return false;
902636
     }
902636
 
902636
+    pthread_mutex_lock(&dev->slave_mutex);
902636
     if (!vu_message_write(dev, dev->slave_fd, &vmsg)) {
902636
+        pthread_mutex_unlock(&dev->slave_mutex);
902636
         return false;
902636
     }
902636
 
902636
+    /* Also unlocks the slave_mutex */
902636
     return vu_process_message_reply(dev, &vmsg);
902636
 }
902636
 
902636
@@ -1628,6 +1642,7 @@ vu_deinit(VuDev *dev)
902636
         close(dev->slave_fd);
902636
         dev->slave_fd = -1;
902636
     }
902636
+    pthread_mutex_destroy(&dev->slave_mutex);
902636
 
902636
     if (dev->sock != -1) {
902636
         close(dev->sock);
902636
@@ -1663,6 +1678,7 @@ vu_init(VuDev *dev,
902636
     dev->remove_watch = remove_watch;
902636
     dev->iface = iface;
902636
     dev->log_call_fd = -1;
902636
+    pthread_mutex_init(&dev->slave_mutex, NULL);
902636
     dev->slave_fd = -1;
902636
     dev->max_queues = max_queues;
902636
 
902636
diff --git a/contrib/libvhost-user/libvhost-user.h b/contrib/libvhost-user/libvhost-user.h
902636
index 46b6007..1844b6f 100644
902636
--- a/contrib/libvhost-user/libvhost-user.h
902636
+++ b/contrib/libvhost-user/libvhost-user.h
902636
@@ -19,6 +19,7 @@
902636
 #include <stddef.h>
902636
 #include <sys/poll.h>
902636
 #include <linux/vhost.h>
902636
+#include <pthread.h>
902636
 #include "standard-headers/linux/virtio_ring.h"
902636
 
902636
 /* Based on qemu/hw/virtio/vhost-user.c */
902636
@@ -355,6 +356,8 @@ struct VuDev {
902636
     VuVirtq *vq;
902636
     VuDevInflightInfo inflight_info;
902636
     int log_call_fd;
902636
+    /* Must be held while using slave_fd */
902636
+    pthread_mutex_t slave_mutex;
902636
     int slave_fd;
902636
     uint64_t log_size;
902636
     uint8_t *log_table;
902636
-- 
902636
1.8.3.1
902636