yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
ddf19c
From 9a44d78f5019280b006bb5b3de7164336289d639 Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Mon, 27 Jan 2020 19:02:21 +0100
ddf19c
Subject: [PATCH 110/116] virtiofsd: fix lo_destroy() resource leaks
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-107-dgilbert@redhat.com>
ddf19c
Patchwork-id: 93560
ddf19c
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 106/112] virtiofsd: fix lo_destroy() resource leaks
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: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
ddf19c
Now that lo_destroy() is serialized we can call unref_inode() so that
ddf19c
all inode resources are freed.
ddf19c
ddf19c
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit 28f7a3b026f231bfe8de5fed6a18a8d27b1dfcee)
ddf19c
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/passthrough_ll.c | 41 ++++++++++++++++++++--------------------
ddf19c
 1 file changed, 20 insertions(+), 21 deletions(-)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
ddf19c
index 79b8b71..eb001b9 100644
ddf19c
--- a/tools/virtiofsd/passthrough_ll.c
ddf19c
+++ b/tools/virtiofsd/passthrough_ll.c
ddf19c
@@ -1371,26 +1371,6 @@ static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
ddf19c
     }
ddf19c
 }
ddf19c
 
ddf19c
-static int unref_all_inodes_cb(gpointer key, gpointer value, gpointer user_data)
ddf19c
-{
ddf19c
-    struct lo_inode *inode = value;
ddf19c
-    struct lo_data *lo = user_data;
ddf19c
-
ddf19c
-    inode->nlookup = 0;
ddf19c
-    lo_map_remove(&lo->ino_map, inode->fuse_ino);
ddf19c
-    close(inode->fd);
ddf19c
-    lo_inode_put(lo, &inode;; /* Drop our refcount from lo_do_lookup() */
ddf19c
-
ddf19c
-    return TRUE;
ddf19c
-}
ddf19c
-
ddf19c
-static void unref_all_inodes(struct lo_data *lo)
ddf19c
-{
ddf19c
-    pthread_mutex_lock(&lo->mutex);
ddf19c
-    g_hash_table_foreach_remove(lo->inodes, unref_all_inodes_cb, lo);
ddf19c
-    pthread_mutex_unlock(&lo->mutex);
ddf19c
-}
ddf19c
-
ddf19c
 static void lo_forget_one(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
ddf19c
 {
ddf19c
     struct lo_data *lo = lo_data(req);
ddf19c
@@ -2477,7 +2457,26 @@ static void lo_lseek(fuse_req_t req, fuse_ino_t ino, off_t off, int whence,
ddf19c
 static void lo_destroy(void *userdata)
ddf19c
 {
ddf19c
     struct lo_data *lo = (struct lo_data *)userdata;
ddf19c
-    unref_all_inodes(lo);
ddf19c
+
ddf19c
+    /*
ddf19c
+     * Normally lo->mutex must be taken when traversing lo->inodes but
ddf19c
+     * lo_destroy() is a serialized request so no races are possible here.
ddf19c
+     *
ddf19c
+     * In addition, we cannot acquire lo->mutex since unref_inode() takes it
ddf19c
+     * too and this would result in a recursive lock.
ddf19c
+     */
ddf19c
+    while (true) {
ddf19c
+        GHashTableIter iter;
ddf19c
+        gpointer key, value;
ddf19c
+
ddf19c
+        g_hash_table_iter_init(&iter, lo->inodes);
ddf19c
+        if (!g_hash_table_iter_next(&iter, &key, &value)) {
ddf19c
+            break;
ddf19c
+        }
ddf19c
+
ddf19c
+        struct lo_inode *inode = value;
ddf19c
+        unref_inode_lolocked(lo, inode, inode->nlookup);
ddf19c
+    }
ddf19c
 }
ddf19c
 
ddf19c
 static struct fuse_lowlevel_ops lo_oper = {
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c