Skip to contentSkip to conversation

leak fixes

PATCH Superseded Liu Bo 2 participants 12 messages Jun 5, 2019 – Jun 6, 2019 Arm/actions semi architecture

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Liu Bo

author
#0 lore raw

cover 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  |  6 ++++++
 contrib/virtiofsd/passthrough_ll.c | 38 +++++++++++++++++++++++++-------------
 2 files changed, 31 insertions(+), 13 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 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 | 6 ++++++
 1 file changed, 6 insertions(+)
contrib/virtiofsd/fuse_lowlevel.c +6 -0
index 0fc2880..de700ad 100644
@@ -2557,6 +2557,12 @@ void fuse_session_destroy(struct fuse_session *se)
 	free(se->cuse_data);
 	if (se->fd != -1)
 		close(se->fd);
+
+        free(se->virtio_dev);
+        se->virtio_dev = NULL;
+        free(se->vu_socket_path);
+        close(se->vu_socketfd);
+
 	free(se);
 }
 
Signature · 1 line
1.8.3.1
#2 lore raw

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

Quoted 13 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.

---
 contrib/virtiofsd/fuse_lowlevel.c | 6 ++++++
 1 file changed, 6 insertions(+)
contrib/virtiofsd/fuse_lowlevel.c +0 -0
index 0fc2880..de700ad 100644

contrib/virtiofsd/fuse_lowlevel.c void fuse_session_destroy(struct fuse_session *se) · line 2562

Quoted 6 lines
contrib/virtiofsd/fuse_lowlevel.c +3 -0
@@ -2557,6 +2557,12 @@ void fuse_session_destroy(struct fuse_session *se)
 	free(se->cuse_data);
 	if (se->fd != -1)
 		close(se->fd);
+
+        free(se->virtio_dev);
+        se->virtio_dev = NULL;

We don't touch virtio_dev or vu_socketfd anywhere else in fuse_lowlevel.c - so we should probably do this cleanup in fuse_virtio.c, and then have something like:

   if (se->vu_socket_path) {
     virtio_session_close(se);
     free(se->vu_socket_path);
     se->vu_socket_path = NULL;
   }
Quoted 13 lines
contrib/virtiofsd/fuse_lowlevel.c +3 -0
+        free(se->vu_socket_path);
+        close(se->vu_socketfd);
+
 	free(se);
 }
 
Signature · 6 lines
1.8.3.1

_______________________________________________
Virtio-fs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virtio-fs

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

author
#3 lore raw

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

Quoted 25 lines · Dr. David Alan Gilbert
Quoted 13 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.

---
 contrib/virtiofsd/fuse_lowlevel.c | 6 ++++++
 1 file changed, 6 insertions(+)
contrib/virtiofsd/fuse_lowlevel.c +0 -0
index 0fc2880..de700ad 100644

contrib/virtiofsd/fuse_lowlevel.c void fuse_session_destroy(struct fuse_session *se) · line 2562

Quoted 6 lines
contrib/virtiofsd/fuse_lowlevel.c +3 -0
@@ -2557,6 +2557,12 @@ void fuse_session_destroy(struct fuse_session *se)
 	free(se->cuse_data);
 	if (se->fd != -1)
 		close(se->fd);
+
+        free(se->virtio_dev);
+        se->virtio_dev = NULL;

We don't touch virtio_dev or vu_socketfd anywhere else in fuse_lowlevel.c - so we should probably do this cleanup in fuse_virtio.c, and then have something like:

Quoted 6 lines
   if (se->vu_socket_path) {
     virtio_session_close(se);
     free(se->vu_socket_path);
     se->vu_socket_path = NULL;
   }

I see, will fix it. Thanks for the comments.

thanks,
-liubo

Quoted 16 lines
Quoted 14 lines
contrib/virtiofsd/fuse_lowlevel.c +3 -0
+        free(se->vu_socket_path);
+        close(se->vu_socketfd);
+
 	free(se);
 }
 
Signature · 6 lines
1.8.3.1

_______________________________________________
Virtio-fs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virtio-fs

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

author
#4 lore raw

PATCH 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 "/".

This adds a check to free the allocated memory only.

---
 contrib/virtiofsd/passthrough_ll.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
contrib/virtiofsd/passthrough_ll.c +6 -1
index b58708f..f348b16 100644
@@ -2217,6 +2217,7 @@ int main(int argc, char *argv[])
 	};
 	struct lo_map_elem *root_elem;
 	int ret = -1;
+        bool free_source = false;
 
 	/* Don't mask creation mode, kernel already did that */
 	umask(0);
@@ -2269,9 +2270,10 @@ 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");
-
+                free_source = true;
 	} else {
 		lo.source = "/";
+                free_source = false;
 	}
 	lo.root.is_symlink = false;
 	if (!lo.timeout_set) {
@@ -2333,5 +2335,8 @@ err_out1:
 	if (lo.root.fd >= 0)
 		close(lo.root.fd);
 
+        if (free_source)
+                free((char *)lo.source);
+
 	return ret ? 1 : 0;
 }
Signature · 1 line
1.8.3.1
#5 lore raw

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

Quoted 23 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 "/".

This adds a check to free the allocated memory only.

---
 contrib/virtiofsd/passthrough_ll.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
contrib/virtiofsd/passthrough_ll.c +1 -0
index b58708f..f348b16 100644
@@ -2217,6 +2217,7 @@ int main(int argc, char *argv[])
 	};
 	struct lo_map_elem *root_elem;
 	int ret = -1;
+        bool free_source = false;
 
 	/* Don't mask creation mode, kernel already did that */
 	umask(0);

contrib/virtiofsd/passthrough_ll.c int main(int argc, char *argv[]) · line 2276

Quoted 8 lines
contrib/virtiofsd/passthrough_ll.c +2 -1
@@ -2269,9 +2270,10 @@ 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");
-
+                free_source = true;
 	} else {
 		lo.source = "/";
+                free_source = false;

An easier way is just to do lo.source = strdup("/")
and then we don't need the flag.

Quoted 19 lines
contrib/virtiofsd/passthrough_ll.c +3 -0
 	}
 	lo.root.is_symlink = false;
 	if (!lo.timeout_set) {
@@ -2333,5 +2335,8 @@ err_out1:
 	if (lo.root.fd >= 0)
 		close(lo.root.fd);
 
+        if (free_source)
+                free((char *)lo.source);
+
 	return ret ? 1 : 0;
 }
Signature · 6 lines
1.8.3.1

_______________________________________________
Virtio-fs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virtio-fs

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

author
#6 lore raw

PATCH 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 f348b16..e7e19dc 100644
@@ -2324,6 +2324,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
Reviewed-by on v1 3/5
#7 lore raw

Re: [Virtio-fs] [PATCH 3/5] virtiofsd: fix memory leak on lo.inodes hashmap

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

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 +0 -0
index f348b16..e7e19dc 100644

contrib/virtiofsd/passthrough_ll.c err_out2: · line 2331

Quoted 8 lines
contrib/virtiofsd/passthrough_ll.c +2 -0
@@ -2324,6 +2324,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);
Quoted 7 lines
Signature · 6 lines
1.8.3.1

_______________________________________________
Virtio-fs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virtio-fs

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

author
#8 lore raw

PATCH 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 e7e19dc..649991e 100644
@@ -2243,7 +2243,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();
@@ -2258,7 +2259,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
Reviewed-by on v1 4/5
#9 lore raw

Re: [Virtio-fs] [PATCH 4/5] virtiofsd: fix error handling in main()

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

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

Quoted 34 lines
---
 contrib/virtiofsd/passthrough_ll.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
contrib/virtiofsd/passthrough_ll.c +3 -2
index e7e19dc..649991e 100644
@@ -2243,7 +2243,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();
@@ -2258,7 +2259,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 · 6 lines
1.8.3.1

_______________________________________________
Virtio-fs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virtio-fs

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

author
#10 lore raw

PATCH 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 | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
contrib/virtiofsd/passthrough_ll.c +19 -15
index 649991e..73ca09f 100644
@@ -2205,6 +2205,24 @@ static gboolean lo_key_equal(gconstpointer a, gconstpointer b)
                la->dev == lb->dev;
 }
 
+static void fuse_lo_data_cleanup(struct lo_data *lo, bool free_source)
+{
+        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);
+
+        if (free_source)
+                free((char *)lo->source);
+}
 
 int main(int argc, char *argv[])
 {
@@ -2325,21 +2343,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);
-
-        if (free_source)
-                free((char *)lo.source);
+        fuse_lo_data_cleanup(&lo, free_source);
 
 	return ret ? 1 : 0;
 }
Signature · 1 line
1.8.3.1
#11 lore raw

Re: [Virtio-fs] [PATCH 5/5] virtiofsd: add helper for lo_data cleanup

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

This offers an helper function for lo_data's cleanup.

That's ok, but will need updating when you do a new version of the lo.source.

Dave

Quoted 63 lines
---
 contrib/virtiofsd/passthrough_ll.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
contrib/virtiofsd/passthrough_ll.c +19 -15
index 649991e..73ca09f 100644
@@ -2205,6 +2205,24 @@ static gboolean lo_key_equal(gconstpointer a, gconstpointer b)
                la->dev == lb->dev;
 }
 
+static void fuse_lo_data_cleanup(struct lo_data *lo, bool free_source)
+{
+        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);
+
+        if (free_source)
+                free((char *)lo->source);
+}
 
 int main(int argc, char *argv[])
 {
@@ -2325,21 +2343,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);
-
-        if (free_source)
-                free((char *)lo.source);
+        fuse_lo_data_cleanup(&lo, free_source);
 
 	return ret ? 1 : 0;
 }
Signature · 6 lines
1.8.3.1

_______________________________________________
Virtio-fs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virtio-fs

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