DM-Devel Archive on lore.kernel.org
 help / color / Atom feed
* [dm-devel] [PATCH] libmultipath: fix memory leaks in coalesce_paths
@ 2020-10-16  6:23 lixiaokeng
  2020-10-16 17:31 ` Benjamin Marzinski
  2020-10-16 19:51 ` Martin Wilck
  0 siblings, 2 replies; 4+ messages in thread
From: lixiaokeng @ 2020-10-16  6:23 UTC (permalink / raw)
  To: Christophe Varoqui, Martin Wilck, Benjamin Marzinski,
	dm-devel mailing list
  Cc: linfeilong, liuzhiqiang (I)

When multipath -F are executed firstly and multipath -v2 or
-d are executed later, asan will warn memory leaks. The
reason is that the mpp allocated in coalesce_paths isn't
freed. Here we add newmp in configure(multipath) to store
mpp and free it.

Signed-off-by: Lixiaokeng <lixiaokeng@huawei.com>
Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
Signed-off-by: Linfeilong <linfeilong@huawei.com>
---
 libmultipath/configure.c | 12 ++++++++++--
 multipath/main.c         |  7 +++++--
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/libmultipath/configure.c b/libmultipath/configure.c
index 6fb477fc..fb2c3f73 100644
--- a/libmultipath/configure.c
+++ b/libmultipath/configure.c
@@ -1270,8 +1270,14 @@ int coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid,
 				goto out;
 			}
 		}
-		if (r == DOMAP_DRY)
+		if (r == DOMAP_DRY) {
+			if (!vector_alloc_slot(newmp)) {
+				remove_map(mpp, vecs->pathvec, vecs->mpvec, KEEP_VEC);
+				goto out;
+			}
+			vector_set_slot(newmp, mpp);
 			continue;
+		}

 		if (r == DOMAP_EXIST && mpp->action == ACT_NOTHING &&
 		    force_reload == FORCE_RELOAD_WEAK)
@@ -1309,8 +1315,10 @@ int coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid,

 		if (newmp) {
 			if (mpp->action != ACT_REJECT) {
-				if (!vector_alloc_slot(newmp))
+				if (!vector_alloc_slot(newmp)) {
+					remove_map(mpp, vecs->pathvec, vecs->mpvec, KEEP_VEC);
 					goto out;
+				}
 				vector_set_slot(newmp, mpp);
 			}
 			else
diff --git a/multipath/main.c b/multipath/main.c
index 9e920d89..5f5b435a 100644
--- a/multipath/main.c
+++ b/multipath/main.c
@@ -472,6 +472,7 @@ configure (struct config *conf, enum mpath_cmds cmd,
 {
 	vector curmp = NULL;
 	vector pathvec = NULL;
+	vector newmp = NULL;
 	struct vectors vecs;
 	int r = RTVL_FAIL, rc;
 	int di_flag = 0;
@@ -483,8 +484,9 @@ configure (struct config *conf, enum mpath_cmds cmd,
 	 */
 	curmp = vector_alloc();
 	pathvec = vector_alloc();
+	newmp = vector_alloc();

-	if (!curmp || !pathvec) {
+	if (!curmp || !pathvec || !newmp) {
 		condlog(0, "can not allocate memory");
 		goto out;
 	}
@@ -586,7 +588,7 @@ configure (struct config *conf, enum mpath_cmds cmd,
 	/*
 	 * core logic entry point
 	 */
-	rc = coalesce_paths(&vecs, NULL, refwwid,
+	rc = coalesce_paths(&vecs, newmp, refwwid,
 			   conf->force_reload, cmd);
 	r = rc == CP_RETRY ? RTVL_RETRY : rc == CP_OK ? RTVL_OK : RTVL_FAIL;

@@ -595,6 +597,7 @@ out:
 		FREE(refwwid);

 	free_multipathvec(curmp, KEEP_PATHS);
+	free_multipathvec(newmp, KEEP_PATHS);
 	free_pathvec(pathvec, FREE_PATHS);

 	return r;
-- 

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [dm-devel] [PATCH] libmultipath: fix memory leaks in coalesce_paths
  2020-10-16  6:23 [dm-devel] [PATCH] libmultipath: fix memory leaks in coalesce_paths lixiaokeng
@ 2020-10-16 17:31 ` Benjamin Marzinski
  2020-10-16 19:51 ` Martin Wilck
  1 sibling, 0 replies; 4+ messages in thread
From: Benjamin Marzinski @ 2020-10-16 17:31 UTC (permalink / raw)
  To: lixiaokeng
  Cc: linfeilong, dm-devel mailing list, Martin Wilck, liuzhiqiang (I)

On Fri, Oct 16, 2020 at 02:23:58PM +0800, lixiaokeng wrote:
> When multipath -F are executed firstly and multipath -v2 or
> -d are executed later, asan will warn memory leaks. The
> reason is that the mpp allocated in coalesce_paths isn't
> freed. Here we add newmp in configure(multipath) to store
> mpp and free it.
> 
> Signed-off-by: Lixiaokeng <lixiaokeng@huawei.com>
> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> Signed-off-by: Linfeilong <linfeilong@huawei.com>
> ---
>  libmultipath/configure.c | 12 ++++++++++--
>  multipath/main.c         |  7 +++++--
>  2 files changed, 15 insertions(+), 4 deletions(-)
> 
> diff --git a/libmultipath/configure.c b/libmultipath/configure.c
> index 6fb477fc..fb2c3f73 100644
> --- a/libmultipath/configure.c
> +++ b/libmultipath/configure.c
> @@ -1270,8 +1270,14 @@ int coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid,
>  				goto out;
>  			}
>  		}
> -		if (r == DOMAP_DRY)
> +		if (r == DOMAP_DRY) {
> +			if (!vector_alloc_slot(newmp)) {
> +				remove_map(mpp, vecs->pathvec, vecs->mpvec, KEEP_VEC);
> +				goto out;
> +			}
> +			vector_set_slot(newmp, mpp);
>  			continue;
> +		}
> 
>  		if (r == DOMAP_EXIST && mpp->action == ACT_NOTHING &&
>  		    force_reload == FORCE_RELOAD_WEAK)
> @@ -1309,8 +1315,10 @@ int coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid,
> 
>  		if (newmp) {
>  			if (mpp->action != ACT_REJECT) {
> -				if (!vector_alloc_slot(newmp))
> +				if (!vector_alloc_slot(newmp)) {
> +					remove_map(mpp, vecs->pathvec, vecs->mpvec, KEEP_VEC);
>  					goto out;
> +				}
>  				vector_set_slot(newmp, mpp);
>  			}
>  			else
> diff --git a/multipath/main.c b/multipath/main.c
> index 9e920d89..5f5b435a 100644
> --- a/multipath/main.c
> +++ b/multipath/main.c

You should rebase this on top of Martin's "libmultipath: improve cleanup
on exit" patchset, since it conflicts with those patches. He's just
posted another version of them.

-Ben

> @@ -472,6 +472,7 @@ configure (struct config *conf, enum mpath_cmds cmd,
>  {
>  	vector curmp = NULL;
>  	vector pathvec = NULL;
> +	vector newmp = NULL;
>  	struct vectors vecs;
>  	int r = RTVL_FAIL, rc;
>  	int di_flag = 0;
> @@ -483,8 +484,9 @@ configure (struct config *conf, enum mpath_cmds cmd,
>  	 */
>  	curmp = vector_alloc();
>  	pathvec = vector_alloc();
> +	newmp = vector_alloc();
> 
> -	if (!curmp || !pathvec) {
> +	if (!curmp || !pathvec || !newmp) {
>  		condlog(0, "can not allocate memory");
>  		goto out;
>  	}
> @@ -586,7 +588,7 @@ configure (struct config *conf, enum mpath_cmds cmd,
>  	/*
>  	 * core logic entry point
>  	 */
> -	rc = coalesce_paths(&vecs, NULL, refwwid,
> +	rc = coalesce_paths(&vecs, newmp, refwwid,
>  			   conf->force_reload, cmd);
>  	r = rc == CP_RETRY ? RTVL_RETRY : rc == CP_OK ? RTVL_OK : RTVL_FAIL;
> 
> @@ -595,6 +597,7 @@ out:
>  		FREE(refwwid);
> 
>  	free_multipathvec(curmp, KEEP_PATHS);
> +	free_multipathvec(newmp, KEEP_PATHS);
>  	free_pathvec(pathvec, FREE_PATHS);
> 
>  	return r;
> -- 

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [dm-devel] [PATCH] libmultipath: fix memory leaks in coalesce_paths
  2020-10-16  6:23 [dm-devel] [PATCH] libmultipath: fix memory leaks in coalesce_paths lixiaokeng
  2020-10-16 17:31 ` Benjamin Marzinski
@ 2020-10-16 19:51 ` Martin Wilck
  2020-10-19 10:17   ` lixiaokeng
  1 sibling, 1 reply; 4+ messages in thread
From: Martin Wilck @ 2020-10-16 19:51 UTC (permalink / raw)
  To: lixiaokeng, Christophe Varoqui, Benjamin Marzinski,
	dm-devel mailing list
  Cc: linfeilong, liuzhiqiang (I)

On Fri, 2020-10-16 at 14:23 +0800, lixiaokeng wrote:
> When multipath -F are executed firstly and multipath -v2 or
> -d are executed later, asan will warn memory leaks. The
> reason is that the mpp allocated in coalesce_paths isn't
> freed. Here we add newmp in configure(multipath) to store
> mpp and free it.
> 
> Signed-off-by: Lixiaokeng <lixiaokeng@huawei.com>
> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> Signed-off-by: Linfeilong <linfeilong@huawei.com>

Besides what Ben noted already, I think you shouldn't force callers to
pass a non-NULL "newmp". The tricky part is to make sure that paths
aren't handled repeatedly in the CMD_DRY_RUN case. Currently pp->mpp !=
NULL is the condition used by coalesce_paths() to check if a path has
already been dealt with; if you simply call remove_map(), that won't
work any more. I suppose you realized that, and that's why you
introduced the non-NULL newmp in multipath (you should have mentioned
that in the changelog message).

I suggest to have callers pass a "vector *pnewmp" instead of "vector
newmp", always allocate newmp in coalesce_paths(), and upon return,
either free newmp, or assign it to the pointer passed by the caller:

     if (pnewmp)
	    *pnewmp = newmp;
     else
            free_multipathvec(newmp, KEEP_PATHS);

Regards,
Martin


--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [dm-devel] [PATCH] libmultipath: fix memory leaks in coalesce_paths
  2020-10-16 19:51 ` Martin Wilck
@ 2020-10-19 10:17   ` lixiaokeng
  0 siblings, 0 replies; 4+ messages in thread
From: lixiaokeng @ 2020-10-19 10:17 UTC (permalink / raw)
  To: Martin Wilck, Christophe Varoqui, Benjamin Marzinski,
	dm-devel mailing list
  Cc: linfeilong, liuzhiqiang (I)



On 2020/10/17 3:51, Martin Wilck wrote:
> On Fri, 2020-10-16 at 14:23 +0800, lixiaokeng wrote:
>> When multipath -F are executed firstly and multipath -v2 or
>> -d are executed later, asan will warn memory leaks. The
>> reason is that the mpp allocated in coalesce_paths isn't
>> freed. Here we add newmp in configure(multipath) to store
>> mpp and free it.
>>
>> Signed-off-by: Lixiaokeng <lixiaokeng@huawei.com>
>> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
>> Signed-off-by: Linfeilong <linfeilong@huawei.com>
> 
> Besides what Ben noted already, I think you shouldn't force callers to
> pass a non-NULL "newmp". The tricky part is to make sure that paths
> aren't handled repeatedly in the CMD_DRY_RUN case. Currently pp->mpp !=
> NULL is the condition used by coalesce_paths() to check if a path has
> already been dealt with; if you simply call remove_map(), that won't
> work any more. I suppose you realized that, and that's why you
> introduced the non-NULL newmp in multipath (you should have mentioned
> that in the changelog message).
> 
> I suggest to have callers pass a "vector *pnewmp" instead of "vector
> newmp", always allocate newmp in coalesce_paths(), and upon return,
> either free newmp, or assign it to the pointer passed by the caller:
> 
>      if (pnewmp)
> 	    *pnewmp = newmp;
>      else
>             free_multipathvec(newmp, KEEP_PATHS);
> 
> Regards,
> Martin
>
Hi Martin,
   Thanks for your review. It is a great idea with your suggestion.
I think it's better that callers pass a "vector mpvec" instead of
"vector newmp", either copy mpvec to newmp or allocate newmp at the
beginning of coalesce_paths, and free newmp or not at the end:

int coalesce_paths (struct vectors *vecs, vector mpvec, char *refwwid,
		    int force_reload, enum mpath_cmds cmd)
	......

 	if (mpvec)
		newmp = mpvec;
	else
		newmp = vector_alloc();
	if (!newmp) {
		condlog(0, "can not allocate newmp");
		return ret;
	}
	......
	
 	if (!mpvec)
		free_multipathvec(newmp, KEEP_PATHS);

About conflict, can you give me the url of the code with your
patchset? If I just change coalease_paths, will it have some
conflicts?

Regards,
Lixiaokeng

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-16  6:23 [dm-devel] [PATCH] libmultipath: fix memory leaks in coalesce_paths lixiaokeng
2020-10-16 17:31 ` Benjamin Marzinski
2020-10-16 19:51 ` Martin Wilck
2020-10-19 10:17   ` lixiaokeng

DM-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/dm-devel/0 dm-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 dm-devel dm-devel/ https://lore.kernel.org/dm-devel \
		dm-devel@redhat.com
	public-inbox-index dm-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/com.redhat.dm-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git