Skip to contentSkip to conversation

virtiofsd: leak fixes

PATCH In review Liu Bo 2 participants 9 messages Jun 6, 2019 – Jun 7, 2019 Arm/actions semi architecture

lore
Share & tools
lore

Liu Bo

author
#0 lore raw

v2 cover virtiofsd: leak fixes

Patch 1-4 fixes memory/fd/hashmap leakage.
Patch 5 is a cleanup.

Liu Bo (5):
virtiofsd: cleanup allocated resource in se
virtiofsd: fix memory leak on lo.source
virtiofsd: fix memory leak on lo.inodes hashmap
virtiofsd: fix error handling in main()
virtiofsd: add helper for lo_data cleanup

 contrib/virtiofsd/fuse_lowlevel.c  |  7 +++++++
 contrib/virtiofsd/fuse_virtio.c    |  7 +++++++
 contrib/virtiofsd/fuse_virtio.h    |  2 +-
 contrib/virtiofsd/passthrough_ll.c | 36 ++++++++++++++++++++++--------------
 4 files changed, 37 insertions(+), 15 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH v2 1/5 virtiofsd: cleanup allocated resource in se

This cleans up unfreed resources in se on quiting, including se->virtio_dev, se->vu_socket_path, se->vu_socketfd.

---
 contrib/virtiofsd/fuse_lowlevel.c | 7 +++++++
 contrib/virtiofsd/fuse_virtio.c   | 7 +++++++
 contrib/virtiofsd/fuse_virtio.h   | 2 +-
 3 files changed, 15 insertions(+), 1 deletion(-)
contrib/virtiofsd/fuse_lowlevel.c +7 -0
index 0fc2880..c460c4c 100644
@@ -2557,6 +2557,13 @@ void fuse_session_destroy(struct fuse_session *se)
 	free(se->cuse_data);
 	if (se->fd != -1)
 		close(se->fd);
+
+        if (se->vu_socket_path) {
+                virtio_session_close(se);
+                free(se->vu_socket_path);
+                se->vu_socket_path = NULL;
+        }
+
 	free(se);
 }
 
contrib/virtiofsd/fuse_virtio.c +7 -0
index cb46558..ed6ba57 100644
@@ -886,6 +886,13 @@ int virtio_session_mount(struct fuse_session *se)
         return 0;
 }
 
+void virtio_session_close(struct fuse_session *se)
+{
+        close(se->vu_socketfd);
+        free(se->virtio_dev);
+        se->virtio_dev = NULL;
+}
+
 int64_t fuse_virtio_map(fuse_req_t req, VhostUserFSSlaveMsg *msg, int fd)
 {
         if (!req->se->virtio_dev) return -ENODEV;
contrib/virtiofsd/fuse_virtio.h +1 -1
index 19c468e..e602298 100644
@@ -19,7 +19,7 @@
 struct fuse_session;
 
 int virtio_session_mount(struct fuse_session *se);
-
+void virtio_session_close(struct fuse_session *se);
 int virtio_loop(struct fuse_session *se);
 
 
Signature · 1 line
1.8.3.1
Reviewed-by on v2 1/5
#2 lore raw

Re: [Virtio-fs] [PATCH v2 1/5] virtiofsd: cleanup allocated resource in se

Quoted 4 lines · * Liu Bo ([email protected]) wrote:

This cleans up unfreed resources in se on quiting, including se->virtio_dev, se->vu_socket_path, se->vu_socketfd.

Quoted 58 lines
---
 contrib/virtiofsd/fuse_lowlevel.c | 7 +++++++
 contrib/virtiofsd/fuse_virtio.c   | 7 +++++++
 contrib/virtiofsd/fuse_virtio.h   | 2 +-
 3 files changed, 15 insertions(+), 1 deletion(-)
contrib/virtiofsd/fuse_lowlevel.c +7 -0
index 0fc2880..c460c4c 100644
@@ -2557,6 +2557,13 @@ void fuse_session_destroy(struct fuse_session *se)
 	free(se->cuse_data);
 	if (se->fd != -1)
 		close(se->fd);
+
+        if (se->vu_socket_path) {
+                virtio_session_close(se);
+                free(se->vu_socket_path);
+                se->vu_socket_path = NULL;
+        }
+
 	free(se);
 }
 
contrib/virtiofsd/fuse_virtio.c +7 -0
index cb46558..ed6ba57 100644
@@ -886,6 +886,13 @@ int virtio_session_mount(struct fuse_session *se)
         return 0;
 }
 
+void virtio_session_close(struct fuse_session *se)
+{
+        close(se->vu_socketfd);
+        free(se->virtio_dev);
+        se->virtio_dev = NULL;
+}
+
 int64_t fuse_virtio_map(fuse_req_t req, VhostUserFSSlaveMsg *msg, int fd)
 {
         if (!req->se->virtio_dev) return -ENODEV;
contrib/virtiofsd/fuse_virtio.h +1 -1
index 19c468e..e602298 100644
@@ -19,7 +19,7 @@
 struct fuse_session;
 
 int virtio_session_mount(struct fuse_session *se);
-
+void virtio_session_close(struct fuse_session *se);
 int virtio_loop(struct fuse_session *se);
 
 
Signature · 1 line
1.8.3.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

author
#3 lore raw

PATCH v2 2/5 virtiofsd: fix memory leak on lo.source

In reply to Liu Bo

valgrind reported that lo.source is leaked on quiting, but it was defined as (const char*) as it may point to a const string "/".

---
 contrib/virtiofsd/passthrough_ll.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
contrib/virtiofsd/passthrough_ll.c +3 -2
index b58708f..959d74d 100644
@@ -2269,9 +2269,8 @@ int main(int argc, char *argv[])
 			err(1, "failed to stat source (\"%s\")", lo.source);
 		if (!S_ISDIR(stat.st_mode))
 			errx(1, "source is not a directory");
-
 	} else {
-		lo.source = "/";
+		lo.source = strdup("/");
 	}
 	lo.root.is_symlink = false;
 	if (!lo.timeout_set) {
@@ -2333,5 +2332,7 @@ err_out1:
 	if (lo.root.fd >= 0)
 		close(lo.root.fd);
 
+        free((char *)lo.source);
+
 	return ret ? 1 : 0;
 }
Signature · 1 line
1.8.3.1
Reviewed-by on v2 2/5
#4 lore raw

Re: [Virtio-fs] [PATCH v2 2/5] virtiofsd: fix memory leak on lo.source

Quoted 4 lines · * Liu Bo ([email protected]) wrote:

valgrind reported that lo.source is leaked on quiting, but it was defined as (const char*) as it may point to a const string "/".

Quoted 30 lines
---
 contrib/virtiofsd/passthrough_ll.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
contrib/virtiofsd/passthrough_ll.c +3 -2
index b58708f..959d74d 100644
@@ -2269,9 +2269,8 @@ int main(int argc, char *argv[])
 			err(1, "failed to stat source (\"%s\")", lo.source);
 		if (!S_ISDIR(stat.st_mode))
 			errx(1, "source is not a directory");
-
 	} else {
-		lo.source = "/";
+		lo.source = strdup("/");
 	}
 	lo.root.is_symlink = false;
 	if (!lo.timeout_set) {
@@ -2333,5 +2332,7 @@ err_out1:
 	if (lo.root.fd >= 0)
 		close(lo.root.fd);
 
+        free((char *)lo.source);
+
 	return ret ? 1 : 0;
 }
Signature · 1 line
1.8.3.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

author
#5 lore raw

PATCH v2 3/5 virtiofsd: fix memory leak on lo.inodes hashmap

In reply to Liu Bo

lo.inodes hashmap was not unref/destroy 'd on quiting, which was caught by valgrind.

---
 contrib/virtiofsd/passthrough_ll.c | 2 ++
 1 file changed, 2 insertions(+)
contrib/virtiofsd/passthrough_ll.c +2 -0
index 959d74d..57508df 100644
@@ -2321,6 +2321,8 @@ err_out2:
 err_out1:
 	fuse_opt_free_args(&args);
 
+        if (lo.inodes)
+                g_hash_table_destroy(lo.inodes);
 	lo_map_destroy(&lo.fd_map);
 	lo_map_destroy(&lo.dirp_map);
 	lo_map_destroy(&lo.ino_map);
Signature · 1 line
1.8.3.1
author
#6 lore raw

PATCH v2 4/5 virtiofsd: fix error handling in main()

In reply to Liu Bo

Neither fuse_parse_cmdline() nor fuse_opt_parse() goes to the right place to do cleanup.

---
 contrib/virtiofsd/passthrough_ll.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
contrib/virtiofsd/passthrough_ll.c +3 -2
index 57508df..73484d1 100644
@@ -2242,7 +2242,8 @@ int main(int argc, char *argv[])
 	lo_map_init(&lo.fd_map);
 
 	if (fuse_parse_cmdline(&args, &opts) != 0)
-		return 1;
+		goto err_out1;
+
 	if (opts.show_help) {
 		printf("usage: %s [options]\n\n", argv[0]);
 		fuse_cmdline_help();
@@ -2257,7 +2258,7 @@ int main(int argc, char *argv[])
 	}
 
 	if (fuse_opt_parse(&args, &lo, lo_opts, NULL)== -1)
-		return 1;
+		goto err_out1;
 
 	lo.debug = opts.debug;
 	if (lo.source) {
Signature · 1 line
1.8.3.1
author
#7 lore raw

PATCH v2 5/5 virtiofsd: add helper for lo_data cleanup

In reply to Liu Bo

This offers an helper function for lo_data's cleanup.

---
 contrib/virtiofsd/passthrough_ll.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
contrib/virtiofsd/passthrough_ll.c +18 -14
index 73484d1..067a110 100644
@@ -2205,6 +2205,23 @@ static gboolean lo_key_equal(gconstpointer a, gconstpointer b)
                la->dev == lb->dev;
 }
 
+static void fuse_lo_data_cleanup(struct lo_data *lo)
+{
+        if (lo->inodes)
+                g_hash_table_destroy(lo->inodes);
+	lo_map_destroy(&lo->fd_map);
+	lo_map_destroy(&lo->dirp_map);
+	lo_map_destroy(&lo->ino_map);
+
+	if (lo->proc_self_fd >= 0) {
+		close(lo->proc_self_fd);
+	}
+
+	if (lo->root.fd >= 0)
+		close(lo->root.fd);
+
+        free((char *)lo->source);
+}
 
 int main(int argc, char *argv[])
 {
@@ -2322,20 +2339,7 @@ err_out2:
 err_out1:
 	fuse_opt_free_args(&args);
 
-        if (lo.inodes)
-                g_hash_table_destroy(lo.inodes);
-	lo_map_destroy(&lo.fd_map);
-	lo_map_destroy(&lo.dirp_map);
-	lo_map_destroy(&lo.ino_map);
-
-	if (lo.proc_self_fd >= 0) {
-		close(lo.proc_self_fd);
-	}
-
-	if (lo.root.fd >= 0)
-		close(lo.root.fd);
-
-        free((char *)lo.source);
+        fuse_lo_data_cleanup(&lo);
 
 	return ret ? 1 : 0;
 }
Signature · 1 line
1.8.3.1
#8 lore raw
In reply to Liu Bo
Quoted 2 lines · * Liu Bo ([email protected]) wrote:

Patch 1-4 fixes memory/fd/hashmap leakage.
Patch 5 is a cleanup.

I've just pushed a new -dev branch that includes this and a bunch of other fixes.

Dave

Quoted 7 lines

Liu Bo (5):
virtiofsd: cleanup allocated resource in se
virtiofsd: fix memory leak on lo.source
virtiofsd: fix memory leak on lo.inodes hashmap
virtiofsd: fix error handling in main()
virtiofsd: add helper for lo_data cleanup

Quoted 9 lines
 contrib/virtiofsd/fuse_lowlevel.c  |  7 +++++++
 contrib/virtiofsd/fuse_virtio.c    |  7 +++++++
 contrib/virtiofsd/fuse_virtio.h    |  2 +-
 contrib/virtiofsd/passthrough_ll.c | 36 ++++++++++++++++++++++--------------
 4 files changed, 37 insertions(+), 15 deletions(-)
Signature · 1 line
1.8.3.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK