The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] selftests/mm: Fix memleak in migration benchmark
@ 2026-07-08 10:37 Hongfu Li
  2026-07-08 13:15 ` David Hildenbrand (Arm)
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Hongfu Li @ 2026-07-08 10:37 UTC (permalink / raw)
  To: jgg, leon, akpm, david, ljs, liam, vbabka, rppt, surenb, mhocko,
	shuah
  Cc: linux-mm, linux-kselftest, linux-kernel, hongfu.li, Hongfu Li

Several early return paths in run_migration_benchmark() skip
hmm_buffer_free(), leaking the buffer. Replace with a single cleanup
label.

Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
---
 tools/testing/selftests/mm/hmm-tests.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/tools/testing/selftests/mm/hmm-tests.c b/tools/testing/selftests/mm/hmm-tests.c
index e4c49699f3f7..e8046675a0c3 100644
--- a/tools/testing/selftests/mm/hmm-tests.c
+++ b/tools/testing/selftests/mm/hmm-tests.c
@@ -2828,8 +2828,10 @@ static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_siz
 	buffer->ptr = mmap(NULL, buffer_size, PROT_READ | PROT_WRITE,
 			  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
 
-	if (buffer->ptr == MAP_FAILED)
-		return -1;
+	if (buffer->ptr == MAP_FAILED) {
+		ret = -1;
+		goto cleanup;
+	}
 
 	/* Apply THP hint if requested */
 	if (use_thp)
@@ -2838,7 +2840,7 @@ static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_siz
 		ret = madvise(buffer->ptr, buffer_size, MADV_NOHUGEPAGE);
 
 	if (ret)
-		return ret;
+		goto cleanup;
 
 	/* Initialize memory to make sure pages are allocated */
 	ptr = (int *)buffer->ptr;
@@ -2848,11 +2850,11 @@ static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_siz
 	/* Warmup iteration */
 	ret = hmm_migrate_sys_to_dev(fd, buffer, npages);
 	if (ret)
-		return ret;
+		goto cleanup;
 
 	ret = hmm_migrate_dev_to_sys(fd, buffer, npages);
 	if (ret)
-		return ret;
+		goto cleanup;
 
 	/* Benchmark iterations */
 	for (i = 0; i < iterations; i++) {
@@ -2861,7 +2863,7 @@ static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_siz
 
 		ret = hmm_migrate_sys_to_dev(fd, buffer, npages);
 		if (ret)
-			return ret;
+			goto cleanup;
 
 		end = get_time_ms();
 		s2d_total += (end - start);
@@ -2871,7 +2873,7 @@ static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_siz
 
 		ret = hmm_migrate_dev_to_sys(fd, buffer, npages);
 		if (ret)
-			return ret;
+			goto cleanup;
 
 		end = get_time_ms();
 		d2s_total += (end - start);
@@ -2886,8 +2888,9 @@ static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_siz
 				 (results->dev_to_sys_time / 1000.0);
 
 	/* Cleanup */
+cleanup:
 	hmm_buffer_free(buffer);
-	return 0;
+	return ret;
 }
 
 /*
-- 
2.25.1


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

* Re: [PATCH] selftests/mm: Fix memleak in migration benchmark
  2026-07-08 10:37 [PATCH] selftests/mm: Fix memleak in migration benchmark Hongfu Li
@ 2026-07-08 13:15 ` David Hildenbrand (Arm)
  2026-07-09  1:54   ` Hongfu Li
  2026-07-09  1:09 ` SJ Park
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: David Hildenbrand (Arm) @ 2026-07-08 13:15 UTC (permalink / raw)
  To: Hongfu Li, jgg, leon, akpm, ljs, liam, vbabka, rppt, surenb,
	mhocko, shuah
  Cc: linux-mm, linux-kselftest, linux-kernel, hongfu.li

On 7/8/26 12:37, Hongfu Li wrote:
> Several early return paths in run_migration_benchmark() skip
> hmm_buffer_free(), leaking the buffer. Replace with a single cleanup
> label.
> 

Probably best to add a Fixes: even when not really critical.

> Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
> ---

Nothing jumped at me

Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>

-- 
Cheers,

David

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

* Re: [PATCH] selftests/mm: Fix memleak in migration benchmark
  2026-07-08 10:37 [PATCH] selftests/mm: Fix memleak in migration benchmark Hongfu Li
  2026-07-08 13:15 ` David Hildenbrand (Arm)
@ 2026-07-09  1:09 ` SJ Park
  2026-07-09  2:02   ` Hongfu Li
  2026-07-09  2:37 ` Andrew Morton
  2026-07-11  7:31 ` Lorenzo Stoakes
  3 siblings, 1 reply; 8+ messages in thread
From: SJ Park @ 2026-07-09  1:09 UTC (permalink / raw)
  To: Hongfu Li
  Cc: SJ Park, jgg, leon, akpm, david, ljs, liam, vbabka, rppt, surenb,
	mhocko, shuah, linux-mm, linux-kselftest, linux-kernel, hongfu.li

On Wed,  8 Jul 2026 18:37:05 +0800 Hongfu Li <lihongfu@kylinos.cn> wrote:

> Several early return paths in run_migration_benchmark() skip
> hmm_buffer_free(), leaking the buffer. Replace with a single cleanup
> label.

Looks good to me.

> 
> Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>

As David suggested, having Fixes: would be nice.  Seems 271a7b2e3c13
("selftests/mm/hmm-tests: new throughput tests including THP") could be the
one?

Reviewed-by: SJ Park <sj@kernel.org>

[...]
>  
>  	/* Cleanup */
> +cleanup:
>  	hmm_buffer_free(buffer);
> -	return 0;
> +	return ret;

A trivial nit.  I think '/* Cleanup */' doesn't really need to be stayed there,
as the 'cleanup:' label self-explains what it is doing here.


Thanks,
SJ

[...]

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

* Re: [PATCH] selftests/mm: Fix memleak in migration benchmark
  2026-07-08 13:15 ` David Hildenbrand (Arm)
@ 2026-07-09  1:54   ` Hongfu Li
  0 siblings, 0 replies; 8+ messages in thread
From: Hongfu Li @ 2026-07-09  1:54 UTC (permalink / raw)
  To: david
  Cc: akpm, hongfu.li, jgg, leon, liam, lihongfu, linux-kernel,
	linux-kselftest, linux-mm, ljs, mhocko, rppt, shuah, surenb,
	vbabka

Hi,

> > Several early return paths in run_migration_benchmark() skip
> > hmm_buffer_free(), leaking the buffer. Replace with a single cleanup
> > label.
> > 
> 
> Probably best to add a Fixes: even when not really critical.
> 
> > Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
> > ---
> 
> Nothing jumped at me
> 
> Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>

Thanks for reviewing. Will add the Fixes tag and post an updated patch.

Best regards,
Hongfu

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

* Re: [PATCH] selftests/mm: Fix memleak in migration benchmark
  2026-07-09  1:09 ` SJ Park
@ 2026-07-09  2:02   ` Hongfu Li
  0 siblings, 0 replies; 8+ messages in thread
From: Hongfu Li @ 2026-07-09  2:02 UTC (permalink / raw)
  To: sj
  Cc: akpm, david, hongfu.li, jgg, leon, liam, lihongfu, linux-kernel,
	linux-kselftest, linux-mm, ljs, mhocko, rppt, shuah, surenb,
	vbabka

Hi,

> > Several early return paths in run_migration_benchmark() skip
> > hmm_buffer_free(), leaking the buffer. Replace with a single cleanup
> > label.
> 
> Looks good to me.
> 
> > 
> > Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
> 
> As David suggested, having Fixes: would be nice.  Seems 271a7b2e3c13
> ("selftests/mm/hmm-tests: new throughput tests including THP") could be the
> one?
> 
> Reviewed-by: SJ Park <sj@kernel.org>
> 
> [...]
> >  
> >  	/* Cleanup */
> > +cleanup:
> >  	hmm_buffer_free(buffer);
> > -	return 0;
> > +	return ret;
> 
> A trivial nit.  I think '/* Cleanup */' doesn't really need to be stayed there,
> as the 'cleanup:' label self-explains what it is doing here.

Thanks a lot for your review and suggestions.

I'll add the Fixes tag referencing commit 271a7b2e3c13 and drop the
redundant comment in the next version.

Best regards,
Hongfu

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

* Re: [PATCH] selftests/mm: Fix memleak in migration benchmark
  2026-07-08 10:37 [PATCH] selftests/mm: Fix memleak in migration benchmark Hongfu Li
  2026-07-08 13:15 ` David Hildenbrand (Arm)
  2026-07-09  1:09 ` SJ Park
@ 2026-07-09  2:37 ` Andrew Morton
  2026-07-09  3:03   ` Hongfu Li
  2026-07-11  7:31 ` Lorenzo Stoakes
  3 siblings, 1 reply; 8+ messages in thread
From: Andrew Morton @ 2026-07-09  2:37 UTC (permalink / raw)
  To: Hongfu Li
  Cc: jgg, leon, david, ljs, liam, vbabka, rppt, surenb, mhocko, shuah,
	linux-mm, linux-kselftest, linux-kernel, hongfu.li

On Wed,  8 Jul 2026 18:37:05 +0800 Hongfu Li <lihongfu@kylinos.cn> wrote:

> Several early return paths in run_migration_benchmark() skip
> hmm_buffer_free(), leaking the buffer. Replace with a single cleanup
> label.

Thanks.  Sashiko found a bug:
	https://sashiko.dev/#/patchset/20260708103705.840679-1-lihongfu@kylinos.cn

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

* Re: [PATCH] selftests/mm: Fix memleak in migration benchmark
  2026-07-09  2:37 ` Andrew Morton
@ 2026-07-09  3:03   ` Hongfu Li
  0 siblings, 0 replies; 8+ messages in thread
From: Hongfu Li @ 2026-07-09  3:03 UTC (permalink / raw)
  To: Andrew Morton, Hongfu Li
  Cc: jgg, leon, david, ljs, liam, vbabka, rppt, surenb, mhocko, shuah,
	linux-mm, linux-kselftest, linux-kernel


On 7/9/26 10:37 AM, Andrew Morton wrote:
>> Several early return paths in run_migration_benchmark() skip
>> hmm_buffer_free(), leaking the buffer. Replace with a single cleanup
>> label.
> Thanks.  Sashiko found a bug:
> 	https://sashiko.dev/#/patchset/20260708103705.840679-1-lihongfu@kylinos.cn

Thanks a lot for the reminder. I'll address it in the next revision of this patch.

Best regards,
Hongfu


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

* Re: [PATCH] selftests/mm: Fix memleak in migration benchmark
  2026-07-08 10:37 [PATCH] selftests/mm: Fix memleak in migration benchmark Hongfu Li
                   ` (2 preceding siblings ...)
  2026-07-09  2:37 ` Andrew Morton
@ 2026-07-11  7:31 ` Lorenzo Stoakes
  3 siblings, 0 replies; 8+ messages in thread
From: Lorenzo Stoakes @ 2026-07-11  7:31 UTC (permalink / raw)
  To: Hongfu Li
  Cc: jgg, leon, akpm, david, liam, vbabka, rppt, surenb, mhocko, shuah,
	linux-mm, linux-kselftest, linux-kernel, hongfu.li

On Wed, Jul 08, 2026 at 06:37:05PM +0800, Hongfu Li wrote:
> Several early return paths in run_migration_benchmark() skip
> hmm_buffer_free(), leaking the buffer. Replace with a single cleanup
> label.
>
> Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>

LGTM. With the Fixes tag added as requested by David, feel free to add:

Reviewed-by: Lorenzo Stoakes <ljs@kernel.org>

> ---
>  tools/testing/selftests/mm/hmm-tests.c | 19 +++++++++++--------
>  1 file changed, 11 insertions(+), 8 deletions(-)
>
> diff --git a/tools/testing/selftests/mm/hmm-tests.c b/tools/testing/selftests/mm/hmm-tests.c
> index e4c49699f3f7..e8046675a0c3 100644
> --- a/tools/testing/selftests/mm/hmm-tests.c
> +++ b/tools/testing/selftests/mm/hmm-tests.c
> @@ -2828,8 +2828,10 @@ static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_siz
>  	buffer->ptr = mmap(NULL, buffer_size, PROT_READ | PROT_WRITE,
>  			  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
>
> -	if (buffer->ptr == MAP_FAILED)
> -		return -1;
> +	if (buffer->ptr == MAP_FAILED) {
> +		ret = -1;
> +		goto cleanup;
> +	}
>
>  	/* Apply THP hint if requested */
>  	if (use_thp)
> @@ -2838,7 +2840,7 @@ static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_siz
>  		ret = madvise(buffer->ptr, buffer_size, MADV_NOHUGEPAGE);
>
>  	if (ret)
> -		return ret;
> +		goto cleanup;
>
>  	/* Initialize memory to make sure pages are allocated */
>  	ptr = (int *)buffer->ptr;
> @@ -2848,11 +2850,11 @@ static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_siz
>  	/* Warmup iteration */
>  	ret = hmm_migrate_sys_to_dev(fd, buffer, npages);
>  	if (ret)
> -		return ret;
> +		goto cleanup;
>
>  	ret = hmm_migrate_dev_to_sys(fd, buffer, npages);
>  	if (ret)
> -		return ret;
> +		goto cleanup;
>
>  	/* Benchmark iterations */
>  	for (i = 0; i < iterations; i++) {
> @@ -2861,7 +2863,7 @@ static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_siz
>
>  		ret = hmm_migrate_sys_to_dev(fd, buffer, npages);
>  		if (ret)
> -			return ret;
> +			goto cleanup;
>
>  		end = get_time_ms();
>  		s2d_total += (end - start);
> @@ -2871,7 +2873,7 @@ static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_siz
>
>  		ret = hmm_migrate_dev_to_sys(fd, buffer, npages);
>  		if (ret)
> -			return ret;
> +			goto cleanup;
>
>  		end = get_time_ms();
>  		d2s_total += (end - start);
> @@ -2886,8 +2888,9 @@ static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_siz
>  				 (results->dev_to_sys_time / 1000.0);
>
>  	/* Cleanup */
> +cleanup:
>  	hmm_buffer_free(buffer);
> -	return 0;
> +	return ret;
>  }
>
>  /*
> --
> 2.25.1
>

Cheers, Lorenzo

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

end of thread, other threads:[~2026-07-11  7:31 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-08 10:37 [PATCH] selftests/mm: Fix memleak in migration benchmark Hongfu Li
2026-07-08 13:15 ` David Hildenbrand (Arm)
2026-07-09  1:54   ` Hongfu Li
2026-07-09  1:09 ` SJ Park
2026-07-09  2:02   ` Hongfu Li
2026-07-09  2:37 ` Andrew Morton
2026-07-09  3:03   ` Hongfu Li
2026-07-11  7:31 ` Lorenzo Stoakes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox