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

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