902636
From 9a44d78f5019280b006bb5b3de7164336289d639 Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Mon, 27 Jan 2020 19:02:21 +0100
902636
Subject: [PATCH 110/116] virtiofsd: fix lo_destroy() resource leaks
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-107-dgilbert@redhat.com>
902636
Patchwork-id: 93560
902636
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 106/112] virtiofsd: fix lo_destroy() resource leaks
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: Stefan Hajnoczi <stefanha@redhat.com>
902636
902636
Now that lo_destroy() is serialized we can call unref_inode() so that
902636
all inode resources are freed.
902636
902636
Signed-off-by: Stefan Hajnoczi <stefanha@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 28f7a3b026f231bfe8de5fed6a18a8d27b1dfcee)
902636
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
902636
---
902636
 tools/virtiofsd/passthrough_ll.c | 41 ++++++++++++++++++++--------------------
902636
 1 file changed, 20 insertions(+), 21 deletions(-)
902636
902636
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
902636
index 79b8b71..eb001b9 100644
902636
--- a/tools/virtiofsd/passthrough_ll.c
902636
+++ b/tools/virtiofsd/passthrough_ll.c
902636
@@ -1371,26 +1371,6 @@ static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
902636
     }
902636
 }
902636
 
902636
-static int unref_all_inodes_cb(gpointer key, gpointer value, gpointer user_data)
902636
-{
902636
-    struct lo_inode *inode = value;
902636
-    struct lo_data *lo = user_data;
902636
-
902636
-    inode->nlookup = 0;
902636
-    lo_map_remove(&lo->ino_map, inode->fuse_ino);
902636
-    close(inode->fd);
902636
-    lo_inode_put(lo, &inode;; /* Drop our refcount from lo_do_lookup() */
902636
-
902636
-    return TRUE;
902636
-}
902636
-
902636
-static void unref_all_inodes(struct lo_data *lo)
902636
-{
902636
-    pthread_mutex_lock(&lo->mutex);
902636
-    g_hash_table_foreach_remove(lo->inodes, unref_all_inodes_cb, lo);
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
@@ -2477,7 +2457,26 @@ static void lo_lseek(fuse_req_t req, fuse_ino_t ino, off_t off, int whence,
902636
 static void lo_destroy(void *userdata)
902636
 {
902636
     struct lo_data *lo = (struct lo_data *)userdata;
902636
-    unref_all_inodes(lo);
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
+    while (true) {
902636
+        GHashTableIter iter;
902636
+        gpointer key, value;
902636
+
902636
+        g_hash_table_iter_init(&iter, lo->inodes);
902636
+        if (!g_hash_table_iter_next(&iter, &key, &value)) {
902636
+            break;
902636
+        }
902636
+
902636
+        struct lo_inode *inode = value;
902636
+        unref_inode_lolocked(lo, inode, inode->nlookup);
902636
+    }
902636
 }
902636
 
902636
 static struct fuse_lowlevel_ops lo_oper = {
902636
-- 
902636
1.8.3.1
902636