yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-virtiofsd-Convert-lo_destroy-to-take-the-lo-mutex-lo.patch

902636
From 24f91062f571ad2dd2ac22db3b7d456a2c8bd2cb Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Mon, 27 Jan 2020 19:02:23 +0100
902636
Subject: [PATCH 112/116] virtiofsd: Convert lo_destroy to take the lo->mutex
902636
 lock itself
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-109-dgilbert@redhat.com>
902636
Patchwork-id: 93563
902636
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 108/112] virtiofsd: Convert lo_destroy to take the lo->mutex lock itself
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
lo_destroy was relying on some implicit knowledge of the locking;
902636
we can avoid this if we create an unref_inode that doesn't take
902636
the lock and then grab it for the whole of the lo_destroy.
902636
902636
Suggested-by: Vivek Goyal <vgoyal@redhat.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
(cherry picked from commit fe4c15798a48143dd6b1f58d2d3cad12206ce211)
902636
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
902636
---
902636
 tools/virtiofsd/passthrough_ll.c | 31 +++++++++++++++++--------------
902636
 1 file changed, 17 insertions(+), 14 deletions(-)
902636
902636
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
902636
index eb001b9..fc15d61 100644
902636
--- a/tools/virtiofsd/passthrough_ll.c
902636
+++ b/tools/virtiofsd/passthrough_ll.c
902636
@@ -1344,14 +1344,13 @@ static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
902636
     lo_inode_put(lo, &inode;;
902636
 }
902636
 
902636
-static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
902636
-                                 uint64_t n)
902636
+/* To be called with lo->mutex held */
902636
+static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n)
902636
 {
902636
     if (!inode) {
902636
         return;
902636
     }
902636
 
902636
-    pthread_mutex_lock(&lo->mutex);
902636
     assert(inode->nlookup >= n);
902636
     inode->nlookup -= n;
902636
     if (!inode->nlookup) {
902636
@@ -1362,15 +1361,24 @@ static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
902636
         }
902636
         g_hash_table_destroy(inode->posix_locks);
902636
         pthread_mutex_destroy(&inode->plock_mutex);
902636
-        pthread_mutex_unlock(&lo->mutex);
902636
 
902636
         /* Drop our refcount from lo_do_lookup() */
902636
         lo_inode_put(lo, &inode;;
902636
-    } else {
902636
-        pthread_mutex_unlock(&lo->mutex);
902636
     }
902636
 }
902636
 
902636
+static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
902636
+                                 uint64_t n)
902636
+{
902636
+    if (!inode) {
902636
+        return;
902636
+    }
902636
+
902636
+    pthread_mutex_lock(&lo->mutex);
902636
+    unref_inode(lo, inode, n);
902636
+    pthread_mutex_unlock(&lo->mutex);
902636
+}
902636
+
902636
 static void lo_forget_one(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
902636
 {
902636
     struct lo_data *lo = lo_data(req);
902636
@@ -2458,13 +2466,7 @@ static void lo_destroy(void *userdata)
902636
 {
902636
     struct lo_data *lo = (struct lo_data *)userdata;
902636
 
902636
-    /*
902636
-     * Normally lo->mutex must be taken when traversing lo->inodes but
902636
-     * lo_destroy() is a serialized request so no races are possible here.
902636
-     *
902636
-     * In addition, we cannot acquire lo->mutex since unref_inode() takes it
902636
-     * too and this would result in a recursive lock.
902636
-     */
902636
+    pthread_mutex_lock(&lo->mutex);
902636
     while (true) {
902636
         GHashTableIter iter;
902636
         gpointer key, value;
902636
@@ -2475,8 +2477,9 @@ static void lo_destroy(void *userdata)
902636
         }
902636
 
902636
         struct lo_inode *inode = value;
902636
-        unref_inode_lolocked(lo, inode, inode->nlookup);
902636
+        unref_inode(lo, inode, inode->nlookup);
902636
     }
902636
+    pthread_mutex_unlock(&lo->mutex);
902636
 }
902636
 
902636
 static struct fuse_lowlevel_ops lo_oper = {
902636
-- 
902636
1.8.3.1
902636