The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] selftests/cgroup: check malloc return value in alloc_anon functions
@ 2026-05-11  2:16 Hongfu Li
  2026-05-11  2:46 ` Muchun Song
  2026-05-11 11:59 ` Vishal Moola
  0 siblings, 2 replies; 4+ messages in thread
From: Hongfu Li @ 2026-05-11  2:16 UTC (permalink / raw)
  To: hannes, mhocko, roman.gushchin, shakeel.butt, muchun.song, tj,
	mkoutny, shuah
  Cc: cgroups, linux-mm, linux-kselftest, linux-kernel, Hongfu Li

The alloc_anon() function calls malloc() without checking for a NULL
return. If memory allocation fails, a NULL pointer dereference will
occur when accessing the buffer.

Add proper error handling to return -1 when malloc() fails in all
four alloc_anon variants:
- alloc_anon()
- alloc_anon_50M_check()
- alloc_anon_noexit()
- alloc_anon_50M_check_swap()

Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
---
 tools/testing/selftests/cgroup/test_memcontrol.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c
index b43da9bc20c4..8ef9c99a82eb 100644
--- a/tools/testing/selftests/cgroup/test_memcontrol.c
+++ b/tools/testing/selftests/cgroup/test_memcontrol.c
@@ -61,6 +61,11 @@ int alloc_anon(const char *cgroup, void *arg)
 	char *buf, *ptr;
 
 	buf = malloc(size);
+	if (buf == NULL) {
+		fprintf(stderr, "malloc() failed\n");
+		return -1;
+	}
+
 	for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
 		*ptr = 0;
 
-- 
2.25.1


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

* Re: [PATCH] selftests/cgroup: check malloc return value in alloc_anon functions
  2026-05-11  2:16 [PATCH] selftests/cgroup: check malloc return value in alloc_anon functions Hongfu Li
@ 2026-05-11  2:46 ` Muchun Song
  2026-05-11 11:59 ` Vishal Moola
  1 sibling, 0 replies; 4+ messages in thread
From: Muchun Song @ 2026-05-11  2:46 UTC (permalink / raw)
  To: Hongfu Li
  Cc: hannes, mhocko, roman.gushchin, shakeel.butt, tj, mkoutny, shuah,
	cgroups, linux-mm, linux-kselftest, linux-kernel



> On May 11, 2026, at 10:16, Hongfu Li <lihongfu@kylinos.cn> wrote:
> 
> The alloc_anon() function calls malloc() without checking for a NULL
> return. If memory allocation fails, a NULL pointer dereference will
> occur when accessing the buffer.
> 
> Add proper error handling to return -1 when malloc() fails in all
> four alloc_anon variants:
> - alloc_anon()
> - alloc_anon_50M_check()
> - alloc_anon_noexit()
> - alloc_anon_50M_check_swap()
> 
> Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>

Reviewed-by: Muchun Song <muchun.song@linux.dev>

Thanks.


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

* Re: [PATCH] selftests/cgroup: check malloc return value in alloc_anon functions
  2026-05-11  2:16 [PATCH] selftests/cgroup: check malloc return value in alloc_anon functions Hongfu Li
  2026-05-11  2:46 ` Muchun Song
@ 2026-05-11 11:59 ` Vishal Moola
  2026-05-12  3:06   ` Hongfu Li
  1 sibling, 1 reply; 4+ messages in thread
From: Vishal Moola @ 2026-05-11 11:59 UTC (permalink / raw)
  To: Hongfu Li
  Cc: hannes, mhocko, roman.gushchin, shakeel.butt, muchun.song, tj,
	mkoutny, shuah, cgroups, linux-mm, linux-kselftest, linux-kernel

On Mon, May 11, 2026 at 10:16:15AM +0800, Hongfu Li wrote:
> The alloc_anon() function calls malloc() without checking for a NULL
> return. If memory allocation fails, a NULL pointer dereference will
> occur when accessing the buffer.
> 
> Add proper error handling to return -1 when malloc() fails in all
> four alloc_anon variants:
> - alloc_anon()

Just a nit, It looks like the below already have proper error handling.

> - alloc_anon_50M_check()
> - alloc_anon_noexit()
> - alloc_anon_50M_check_swap()
> 
> Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
> ---
>  tools/testing/selftests/cgroup/test_memcontrol.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c
> index b43da9bc20c4..8ef9c99a82eb 100644
> --- a/tools/testing/selftests/cgroup/test_memcontrol.c
> +++ b/tools/testing/selftests/cgroup/test_memcontrol.c
> @@ -61,6 +61,11 @@ int alloc_anon(const char *cgroup, void *arg)
>  	char *buf, *ptr;
>  
>  	buf = malloc(size);
> +	if (buf == NULL) {
> +		fprintf(stderr, "malloc() failed\n");
> +		return -1;
> +	}
> +
>  	for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
>  		*ptr = 0;

Every malloc() call in this file has this same pattern. Maybe we'd be
better off making it a helper function?

Either way:
Reviewed-by: Vishal Moola <vishal.moola@gmail.com>

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

* Re: [PATCH] selftests/cgroup: check malloc return value in alloc_anon functions
  2026-05-11 11:59 ` Vishal Moola
@ 2026-05-12  3:06   ` Hongfu Li
  0 siblings, 0 replies; 4+ messages in thread
From: Hongfu Li @ 2026-05-12  3:06 UTC (permalink / raw)
  To: vishal.moola
  Cc: cgroups, hannes, lihongfu, linux-kernel, linux-kselftest,
	linux-mm, mhocko, mkoutny, muchun.song, roman.gushchin,
	shakeel.butt, shuah, tj

Hi Vishal,

> > diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c
> > index b43da9bc20c4..8ef9c99a82eb 100644
> > --- a/tools/testing/selftests/cgroup/test_memcontrol.c
> > +++ b/tools/testing/selftests/cgroup/test_memcontrol.c
> > @@ -61,6 +61,11 @@ int alloc_anon(const char *cgroup, void *arg)
> >  	char *buf, *ptr;
> >  
> >  	buf = malloc(size);
> > +	if (buf == NULL) {
> > +		fprintf(stderr, "malloc() failed\n");
> > +		return -1;
> > +	}
> > +
> >  	for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
> >  		*ptr = 0;
> 
> Every malloc() call in this file has this same pattern. Maybe we'd be
> better off making it a helper function?
> 
> Either way:
> Reviewed-by: Vishal Moola <vishal.moola@gmail.com>

Thanks for your review and valuable suggestion.
I agree with you, will refactor all malloc() checks into a common helper
function, and send out a v2 patch soon.

Best regards,
Hongfu Li

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

end of thread, other threads:[~2026-05-12  3:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-11  2:16 [PATCH] selftests/cgroup: check malloc return value in alloc_anon functions Hongfu Li
2026-05-11  2:46 ` Muchun Song
2026-05-11 11:59 ` Vishal Moola
2026-05-12  3:06   ` Hongfu Li

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