Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Xu Xin (vivo)" <xu.xin@linux.dev>
Cc: akpm@linux-foundation.org, chengming.zhou@linux.dev,
	david@kernel.org, donettom@linux.ibm.com, linux-mm@kvack.org
Subject: Re: [PATCH v2 2/3] selftests/mm: fix false-positive KSM fork test by dropping init_global_file_handles()
Date: Sat, 21 Nov 2026 07:45:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-226795@kernel.org> (raw)
In-Reply-To: <20261120074445.41446-1-xu.xin@linux.dev>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] selftests/mm: exit() truncates page count in
  test_fork_ksm_merging_page_count()
- [Medium] selftests/mm: ksmd race across fork() in
  test_fork_ksm_merging_page_count()
--

commit 320ca42efa1f04c6ac42a52e155a4cd5bc860e0e
Author: Xu Xin (vivo) <xu.xin@linux.dev>

selftests/mm: fix false-positive KSM fork test by dropping init_global_file_handles()

This commit removes the init_global_file_handles() call from the child
process in test_fork_ksm_merging_page_count() so it does not invoke
ksm_stop() before reading ksm_get_self_merging_pages().

> diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
> index 135191f99962d..c20982b0aff0e 100644
> --- a/tools/testing/selftests/mm/ksm_functional_tests.c
> +++ b/tools/testing/selftests/mm/ksm_functional_tests.c
> @@ -670,7 +670,6 @@ static void test_fork_ksm_merging_page_count(void)
>  
>  	child_pid = fork();
>  	if (!child_pid) {
> -		init_global_file_handles();
>  		exit(ksm_get_self_merging_pages());

[Severity: Medium]
Does exit(ksm_get_self_merging_pages()) truncate the page count to 0 on
4 KiB page systems?

test_fork_ksm_merging_page_count() merges a 2 MiB region, which is 512
(0x200) pages on 4 KiB page systems (or 256 pages on 8 KiB page
systems).  Because exit() and WEXITSTATUS() only preserve the low 8 bits
of the exit code, an inherited count of 512 is truncated to 0 and the
test still falsely passes when fork() fails to reset
mm->ksm_merging_pages.

Also, does ksm_stop() actually reset an inherited ksm_merging_pages
counter in the child as described in the commit message?
unmerge_and_remove_all_rmap_items() only decrements
rmap_item->mm->ksm_merging_pages when removing rmap_items from
mm_slot->rmap_list, which is empty in a newly forked child.

A later commit in this series, commit "selftests/mm: verify
ksm_zero_pages is not inherited on fork", converts the exit argument to
a boolean via !!, but doing exit(!!ksm_get_self_merging_pages()) here is
needed so this stable-tagged fix works on its own.

[Severity: Medium]
Can ksmd race with the child process across fork() and cause a false
test failure?

mmap_and_merge_range() leaves ksmd running in KSM_RUN_MERGE mode, and
ksm_fork() adds the child mm to ksmd's scan list:

Parent                            ksmd
test_fork_ksm_merging_page_count()
  mmap_and_merge_range()
    ksm_start()
  fork()
    dup_mmap()
      ksm_fork()
        child_mm->ksm_merging_pages = 0
        __ksm_enter(child_mm)
                                  ksm_do_scan()
                                    cmp_and_merge_page()
                                      try_to_merge_with_ksm_page()
                                      stable_tree_append()
                                        child_mm->ksm_merging_pages++
  Child:
  ksm_get_self_merging_pages()  <-- reads non-zero count

Because the child's forked PTEs already map the merged KSM folio,
try_to_merge_one_page() succeeds on the first scan pass ("ksm page
forked") and stable_tree_append() increments
child_mm->ksm_merging_pages before the child reads
/proc/self/ksm_merging_pages.  Pausing ksmd by writing 0 (KSM_RUN_STOP)
to /sys/kernel/mm/ksm/run after mmap_and_merge_range() and before fork()
would avoid this race without unmerging pages.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120074230.40269-1-xu.xin@linux.dev?part=2


  reply	other threads:[~2026-11-21  7:45 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  7:42 [PATCH v2 0/3] mm/ksm: fix ksm_zero_pages accounting on fork Xu Xin (vivo)
2026-11-20  7:43 ` [PATCH v2 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers " Xu Xin (vivo)
2026-11-20  7:44 ` [PATCH v2 2/3] selftests/mm: fix false-positive KSM fork test by dropping init_global_file_handles() Xu Xin (vivo)
2026-11-21  7:45   ` sashiko-bot [this message]
2026-11-20  7:44 ` [PATCH v2 3/3] selftests/mm: verify ksm_zero_pages is not inherited on fork Xu Xin (vivo)
2026-11-21  7:45   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-226795@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=chengming.zhou@linux.dev \
    --cc=david@kernel.org \
    --cc=donettom@linux.ibm.com \
    --cc=linux-mm@kvack.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=xu.xin@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox