[dpdk-dev] vhost: clean up per-socket mutex

Message ID 20170612212904.8787-1-daniel.verkamp@intel.com (mailing list archive)
State Accepted, archived
Delegated to: Yuanhan Liu
Headers

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Daniel Verkamp June 12, 2017, 9:29 p.m. UTC
  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.

Signed-off-by: Daniel Verkamp <daniel.verkamp@intel.com>
---
 lib/librte_vhost/socket.c | 3 +++
 1 file changed, 3 insertions(+)
  

Comments

Jens Freimann June 14, 2017, 8:19 a.m. UTC | #1
On Mon, Jun 12, 2017 at 02:29:04PM -0700, Daniel Verkamp wrote:
> 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.
> 
> Signed-off-by: Daniel Verkamp <daniel.verkamp@intel.com>
> ---
>  lib/librte_vhost/socket.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/lib/librte_vhost/socket.c b/lib/librte_vhost/socket.c
> index c7f99b0..9720773 100644
> --- a/lib/librte_vhost/socket.c
> +++ b/lib/librte_vhost/socket.c
> @@ -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);

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

regards,
Jens
  
Yuanhan Liu July 1, 2017, 11:25 p.m. UTC | #2
On Wed, Jun 14, 2017 at 10:19:53AM +0200, Jens Freimann wrote:
> On Mon, Jun 12, 2017 at 02:29:04PM -0700, Daniel Verkamp wrote:
> > 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.
> > 
> > Signed-off-by: Daniel Verkamp <daniel.verkamp@intel.com>
> > ---
> >  lib/librte_vhost/socket.c | 3 +++
> >  1 file changed, 3 insertions(+)
> > 
> > diff --git a/lib/librte_vhost/socket.c b/lib/librte_vhost/socket.c
> > index c7f99b0..9720773 100644
> > --- a/lib/librte_vhost/socket.c
> > +++ b/lib/librte_vhost/socket.c
> > @@ -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);
> 
> 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
  

Patch

diff --git a/lib/librte_vhost/socket.c b/lib/librte_vhost/socket.c
index c7f99b0..9720773 100644
--- a/lib/librte_vhost/socket.c
+++ b/lib/librte_vhost/socket.c
@@ -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);