[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 104/109] virtiofsd: fix lo_destroy() resource leaks
From: |
Dr. David Alan Gilbert (git) |
Subject: |
[PATCH v2 104/109] virtiofsd: fix lo_destroy() resource leaks |
Date: |
Tue, 21 Jan 2020 12:24:28 +0000 |
From: Stefan Hajnoczi <address@hidden>
Now that lo_destroy() is serialized we can call unref_inode() so that
all inode resources are freed.
Signed-off-by: Stefan Hajnoczi <address@hidden>
Signed-off-by: Dr. David Alan Gilbert <address@hidden>
Reviewed-by: Philippe Mathieu-Daudé <address@hidden>
---
tools/virtiofsd/passthrough_ll.c | 41 ++++++++++++++++----------------
1 file changed, 20 insertions(+), 21 deletions(-)
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
index 4482138904..4c656b4a60 100644
--- a/tools/virtiofsd/passthrough_ll.c
+++ b/tools/virtiofsd/passthrough_ll.c
@@ -1371,26 +1371,6 @@ static void unref_inode_lolocked(struct lo_data *lo,
struct lo_inode *inode,
}
}
-static int unref_all_inodes_cb(gpointer key, gpointer value, gpointer
user_data)
-{
- struct lo_inode *inode = value;
- struct lo_data *lo = user_data;
-
- inode->nlookup = 0;
- lo_map_remove(&lo->ino_map, inode->fuse_ino);
- close(inode->fd);
- lo_inode_put(lo, &inode); /* Drop our refcount from lo_do_lookup() */
-
- return TRUE;
-}
-
-static void unref_all_inodes(struct lo_data *lo)
-{
- pthread_mutex_lock(&lo->mutex);
- g_hash_table_foreach_remove(lo->inodes, unref_all_inodes_cb, lo);
- pthread_mutex_unlock(&lo->mutex);
-}
-
static void lo_forget_one(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
{
struct lo_data *lo = lo_data(req);
@@ -2477,7 +2457,26 @@ static void lo_lseek(fuse_req_t req, fuse_ino_t ino,
off_t off, int whence,
static void lo_destroy(void *userdata)
{
struct lo_data *lo = (struct lo_data *)userdata;
- unref_all_inodes(lo);
+
+ /*
+ * Normally lo->mutex must be taken when traversing lo->inodes but
+ * lo_destroy() is a serialized request so no races are possible here.
+ *
+ * In addition, we cannot acquire lo->mutex since unref_inode() takes it
+ * too and this would result in a recursive lock.
+ */
+ while (true) {
+ GHashTableIter iter;
+ gpointer key, value;
+
+ g_hash_table_iter_init(&iter, lo->inodes);
+ if (!g_hash_table_iter_next(&iter, &key, &value)) {
+ break;
+ }
+
+ struct lo_inode *inode = value;
+ unref_inode_lolocked(lo, inode, inode->nlookup);
+ }
}
static struct fuse_lowlevel_ops lo_oper = {
--
2.24.1
- [PATCH v2 095/109] virtiofsd: introduce inode refcount to prevent use-after-free, (continued)
- [PATCH v2 095/109] virtiofsd: introduce inode refcount to prevent use-after-free, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 096/109] virtiofsd: do not always set FUSE_FLOCK_LOCKS, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 098/109] virtiofsd: Reset O_DIRECT flag during file open, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 097/109] virtiofsd: convert more fprintf and perror to use fuse log infra, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 100/109] virtiofsd: add definition of fuse_buf_writev(), Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 099/109] virtiofsd: Fix data corruption with O_APPEND write in writeback mode, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 101/109] virtiofsd: use fuse_buf_writev to replace fuse_buf_write for better performance, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 102/109] virtiofsd: process requests in a thread pool, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 103/109] virtiofsd: prevent FUSE_INIT/FUSE_DESTROY races, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 104/109] virtiofsd: fix lo_destroy() resource leaks,
Dr. David Alan Gilbert (git) <=
- [PATCH v2 105/109] virtiofsd: add --thread-pool-size=NUM option, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 106/109] virtiofsd: Convert lo_destroy to take the lo->mutex lock itself, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 107/109] virtiofsd/passthrough_ll: Pass errno to fuse_reply_err(), Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 108/109] virtiofsd: stop all queue threads on exit in virtio_loop(), Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 109/109] virtiofsd: add some options to the help message, Dr. David Alan Gilbert (git), 2020/01/21
- Re: [PATCH v2 000/109] virtiofs daemon [all], no-reply, 2020/01/21
- Re: [PATCH v2 000/109] virtiofs daemon [all], Philippe Mathieu-Daudé, 2020/01/21