Skip to contentSkip to conversation

vhost: clean up per-socket mutex

PATCH In review Daniel Verkamp 3 participants 3 messages Jun 12, 2017 – Jul 1, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vhost: clean up per-socket mutex

vsocket->conn_mutex was allocated with pthread_mutex_init() but never freed with pthread_mutex_destroy(). This is a potential memory leak, depending on how pthread_mutex_t is implemented.

---
 lib/librte_vhost/socket.c | 3 +++
 1 file changed, 3 insertions(+)
lib/librte_vhost/socket.c +3 -0
index c7f99b0..9720773 100644
@@ -636,6 +636,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)
 		vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
 		if (vsocket->reconnect && reconn_tid == 0) {
 			if (vhost_user_reconnect_init() < 0) {
+				pthread_mutex_destroy(&vsocket->conn_mutex);
 				free(vsocket->path);
 				free(vsocket);
 				goto out;
@@ -646,6 +647,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)
 	}
 	ret = create_unix_socket(vsocket);
 	if (ret < 0) {
+		pthread_mutex_destroy(&vsocket->conn_mutex);
 		free(vsocket->path);
 		free(vsocket);
 		goto out;
@@ -724,6 +726,7 @@ rte_vhost_driver_unregister(const char *path)
 			}
 			pthread_mutex_unlock(&vsocket->conn_mutex);
 
+			pthread_mutex_destroy(&vsocket->conn_mutex);
 			free(vsocket->path);
 			free(vsocket);
 
Signature · 1 line
2.9.4
#1 lore raw
Quoted 30 lines · Daniel Verkamp

vsocket->conn_mutex was allocated with pthread_mutex_init() but never freed with pthread_mutex_destroy(). This is a potential memory leak, depending on how pthread_mutex_t is implemented.

---
 lib/librte_vhost/socket.c | 3 +++
 1 file changed, 3 insertions(+)
lib/librte_vhost/socket.c +2 -0
index c7f99b0..9720773 100644
@@ -636,6 +636,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)
 		vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
 		if (vsocket->reconnect && reconn_tid == 0) {
 			if (vhost_user_reconnect_init() < 0) {
+				pthread_mutex_destroy(&vsocket->conn_mutex);
 				free(vsocket->path);
 				free(vsocket);
 				goto out;
@@ -646,6 +647,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)
 	}
 	ret = create_unix_socket(vsocket);
 	if (ret < 0) {
+		pthread_mutex_destroy(&vsocket->conn_mutex);
 		free(vsocket->path);
 		free(vsocket);
 		goto out;

lib/librte_vhost/socket.c rte_vhost_driver_unregister(const char *path) · line 729

Quoted 4 lines
lib/librte_vhost/socket.c +1 -0
@@ -724,6 +726,7 @@ rte_vhost_driver_unregister(const char *path)
 			}
 			pthread_mutex_unlock(&vsocket->conn_mutex);
 
+			pthread_mutex_destroy(&vsocket->conn_mutex);

Seems like we never do it, but shouldn't we check the return value here?

regards,
Jens

#2 lore raw
Quoted 34 lines · Jens Freimann
Quoted 33 lines · Daniel Verkamp

vsocket->conn_mutex was allocated with pthread_mutex_init() but never freed with pthread_mutex_destroy(). This is a potential memory leak, depending on how pthread_mutex_t is implemented.

---
 lib/librte_vhost/socket.c | 3 +++
 1 file changed, 3 insertions(+)
lib/librte_vhost/socket.c +2 -0
index c7f99b0..9720773 100644
@@ -636,6 +636,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)
 		vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
 		if (vsocket->reconnect && reconn_tid == 0) {
 			if (vhost_user_reconnect_init() < 0) {
+				pthread_mutex_destroy(&vsocket->conn_mutex);
 				free(vsocket->path);
 				free(vsocket);
 				goto out;
@@ -646,6 +647,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)
 	}
 	ret = create_unix_socket(vsocket);
 	if (ret < 0) {
+		pthread_mutex_destroy(&vsocket->conn_mutex);
 		free(vsocket->path);
 		free(vsocket);
 		goto out;
@@ -724,6 +726,7 @@ rte_vhost_driver_unregister(const char *path)
 			}
 			pthread_mutex_unlock(&vsocket->conn_mutex);
 
Quoted 4 lines

lib/librte_vhost/socket.c rte_vhost_driver_unregister(const char *path) · line 729

Quoted 1 line
lib/librte_vhost/socket.c +1 -0
+			pthread_mutex_destroy(&vsocket->conn_mutex);

Seems like we never do it, but shouldn't we check the return value here?

I think so. There are more: pthread_mutex_init/lock, etc. We probably should make another patch to fix it. For this patch, I'd like to apply as it is.

So applied to dpdk-next-virtio.

Thanks.

	--yliu