cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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