Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] selftests/mm: mlock2-tests: fix fclose(NULL) in is_vmflag_set()
@ 2026-11-13  2:52 Hongfu Li
  2026-11-13 10:31 ` David Hildenbrand (Arm)
  2026-11-14  2:53 ` sashiko-bot
  0 siblings, 2 replies; 5+ messages in thread
From: Hongfu Li @ 2026-11-13  2:52 UTC (permalink / raw)
  To: Andrew Morton, David Hildenbrand, Lorenzo Stoakes,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Shuah Khan
  Cc: hongfu.li, linux-mm, linux-kselftest, linux-kernel,
	Liam R.Howlett, Hongfu Li

is_vmflag_set() jumps to the shared 'out' label when
seek_to_smaps_entry() fails. smaps is NULL on that path, and the
code at the 'out' label calls fclose(smaps) unconditionally.
fclose(NULL) is undefined behaviour, so the test crashes before
it can report a test result.

Return early on that error path instead, so smaps is always valid
at the 'out' label.

get_value_for_name() follows the same pattern. Return early on the
seek_to_smaps_entry() error path there as well, and drop the now
redundant 'if (smaps)' guard on the fclose().

Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
---
Changes in v2:
- Convert get_value_for_name() in the same way, as suggested by Liam R.Howlett.
- Update the commit message.
---
 tools/testing/selftests/mm/mlock2-tests.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/mm/mlock2-tests.c b/tools/testing/selftests/mm/mlock2-tests.c
index 144b550813a6..455f1c841dec 100644
--- a/tools/testing/selftests/mm/mlock2-tests.c
+++ b/tools/testing/selftests/mm/mlock2-tests.c
@@ -66,7 +66,7 @@ static bool is_vmflag_set(unsigned long addr, const char *vmflag)
 	smaps = seek_to_smaps_entry(addr);
 	if (!smaps) {
 		ksft_print_msg("Unable to parse /proc/self/smaps\n");
-		goto out;
+		return false;
 	}
 
 	while (getline(&line, &size, smaps) > 0) {
@@ -103,7 +103,7 @@ static unsigned long get_value_for_name(unsigned long addr, const char *name)
 	smaps = seek_to_smaps_entry(addr);
 	if (!smaps) {
 		ksft_print_msg("Unable to parse /proc/self/smaps\n");
-		goto out;
+		return value;
 	}
 
 	while (getline(&line, &size, smaps) > 0) {
@@ -123,9 +123,8 @@ static unsigned long get_value_for_name(unsigned long addr, const char *name)
 	}
 
 out:
-	if (smaps)
-		fclose(smaps);
 	free(line);
+	fclose(smaps);
 	return value;
 }
 

---
base-commit: 74d73ffcffe03804f97e9fb82301dadf75671282
change-id: 20261104-for-mlock2-a67f86dad498

Best regards,
--  
Hongfu Li <lihongfu@kylinos.cn>



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

* Re: [PATCH v2] selftests/mm: mlock2-tests: fix fclose(NULL) in is_vmflag_set()
  2026-11-13  2:52 [PATCH v2] selftests/mm: mlock2-tests: fix fclose(NULL) in is_vmflag_set() Hongfu Li
@ 2026-11-13 10:31 ` David Hildenbrand (Arm)
  2026-11-13 13:03   ` Lorenzo Stoakes (ARM)
  2026-11-14  2:53 ` sashiko-bot
  1 sibling, 1 reply; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-13 10:31 UTC (permalink / raw)
  To: Hongfu Li, Andrew Morton, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Shuah Khan
  Cc: hongfu.li, linux-mm, linux-kselftest, linux-kernel

On 11/13/26 03:52, Hongfu Li wrote:
> is_vmflag_set() jumps to the shared 'out' label when
> seek_to_smaps_entry() fails. smaps is NULL on that path, and the
> code at the 'out' label calls fclose(smaps) unconditionally.
> fclose(NULL) is undefined behaviour, so the test crashes before
> it can report a test result.

I was briefly confuses staring at get_value_for_name() diff below. WHich is
clarified below.

> 
> Return early on that error path instead, so smaps is always valid
> at the 'out' label.
> 
> get_value_for_name() follows the same pattern. Return early on the
> seek_to_smaps_entry() error path there as well, and drop the now
> redundant 'if (smaps)' guard on the fclose().
> 
> Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>

Fixes: tag? :)

Apart form that LGTM

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

-- 
Cheers,

David


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

* Re: [PATCH v2] selftests/mm: mlock2-tests: fix fclose(NULL) in is_vmflag_set()
  2026-11-13 10:31 ` David Hildenbrand (Arm)
@ 2026-11-13 13:03   ` Lorenzo Stoakes (ARM)
  0 siblings, 0 replies; 5+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-13 13:03 UTC (permalink / raw)
  To: David Hildenbrand (Arm)
  Cc: Hongfu Li, Andrew Morton, Liam R. Howlett, Vlastimil Babka,
	Mike Rapoport, Suren Baghdasaryan, Michal Hocko, Shuah Khan,
	hongfu.li, linux-mm, linux-kselftest, linux-kernel

On Fri, Nov 13, 2026 at 11:31:55AM +0100, David Hildenbrand (Arm) wrote:
> On 11/13/26 03:52, Hongfu Li wrote:
> > is_vmflag_set() jumps to the shared 'out' label when
> > seek_to_smaps_entry() fails. smaps is NULL on that path, and the
> > code at the 'out' label calls fclose(smaps) unconditionally.
> > fclose(NULL) is undefined behaviour, so the test crashes before
> > it can report a test result.
>
> I was briefly confuses staring at get_value_for_name() diff below. WHich is
> clarified below.
>
> >
> > Return early on that error path instead, so smaps is always valid
> > at the 'out' label.
> >
> > get_value_for_name() follows the same pattern. Return early on the
> > seek_to_smaps_entry() error path there as well, and drop the now
> > redundant 'if (smaps)' guard on the fclose().
> >
> > Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
>
> Fixes: tag? :)
>
> Apart form that LGTM
>
> Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>

Similarly,

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

>
> --
> Cheers,
>
> David

--
Cheers, Lorenzo


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

* Re: [PATCH v2] selftests/mm: mlock2-tests: fix fclose(NULL) in is_vmflag_set()
  2026-11-13  2:52 [PATCH v2] selftests/mm: mlock2-tests: fix fclose(NULL) in is_vmflag_set() Hongfu Li
  2026-11-13 10:31 ` David Hildenbrand (Arm)
@ 2026-11-14  2:53 ` sashiko-bot
  2026-11-14  3:10   ` Andrew Morton
  1 sibling, 1 reply; 5+ messages in thread
From: sashiko-bot @ 2026-11-14  2:53 UTC (permalink / raw)
  To: Hongfu Li, Andrew Morton, David Hildenbrand, Lorenzo Stoakes,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Shuah Khan
  Cc: hongfu.li, linux-mm

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] selftests/mm: missing Fixes trailer for fclose(NULL) fix in
  is_vmflag_set()
--

commit 38a7fefcf9bd0b1ed77197eeaab78f5845d74dfd
Author: Hongfu Li <lihongfu@kylinos.cn>

> selftests/mm: mlock2-tests: fix fclose(NULL) in is_vmflag_set()
>
> is_vmflag_set() jumps to the shared 'out' label when
> seek_to_smaps_entry() fails. smaps is NULL on that path, and the
> code at the 'out' label calls fclose(smaps) unconditionally.
> fclose(NULL) is undefined behaviour, so the test crashes before
> it can report a test result.
>
> Return early on that error path instead, so smaps is always valid
> at the 'out' label.
>
> get_value_for_name() follows the same pattern. Return early on the
> seek_to_smaps_entry() error path there as well, and drop the now
> redundant 'if (smaps)' guard on the fclose().
>
> Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>

[Severity: Low]
Since this fixes the fclose(NULL) crash in is_vmflag_set(), should this
include:

  Fixes: b3b0d09c7a23 ("selftests: vm: add tests for lock on fault")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261113-for-mlock2-v2-1-f62195fc8394@kylinos.cn?part=1


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

* Re: [PATCH v2] selftests/mm: mlock2-tests: fix fclose(NULL) in is_vmflag_set()
  2026-11-14  2:53 ` sashiko-bot
@ 2026-11-14  3:10   ` Andrew Morton
  0 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2026-11-14  3:10 UTC (permalink / raw)
  To: sashiko-reviews
  Cc: sashiko-bot, Hongfu Li, David Hildenbrand, Lorenzo Stoakes,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Shuah Khan, hongfu.li, linux-mm

On Sat, 14 Nov 2026 02:53:30 +0000 sashiko-bot@kernel.org wrote:

> Since this fixes the fclose(NULL) crash in is_vmflag_set(), should this
> include:
> 
>   Fixes: b3b0d09c7a23 ("selftests: vm: add tests for lock on fault")

Yes.  Added.


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

end of thread, other threads:[~2026-11-14  3:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-13  2:52 [PATCH v2] selftests/mm: mlock2-tests: fix fclose(NULL) in is_vmflag_set() Hongfu Li
2026-11-13 10:31 ` David Hildenbrand (Arm)
2026-11-13 13:03   ` Lorenzo Stoakes (ARM)
2026-11-14  2:53 ` sashiko-bot
2026-11-14  3:10   ` Andrew Morton

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