Blame 0114-virtiofsd-Convert-lo_destroy-to-take-the-lo-mutex-lo.patch

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