mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: "Xu Xin (vivo)" <xu.xin@linux.dev>
Cc: akpm@linux-foundation.org, chengming.zhou@linux.dev,
	donettom@linux.ibm.com, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org, stable@vger.kernel.org
Subject: Re: [PATCH v4 3/3] selftests/mm: verify ksm_zero_pages is not inherited on fork
Date: Wed, 25 Nov 2026 11:34:24 +0100	[thread overview]
Message-ID: <4d9417a1-eb75-4507-be86-a544437e3abd@kernel.org> (raw)
In-Reply-To: <20261124130343.131760-1-xu.xin@linux.dev>

On 11/24/26 14:03, Xu Xin (vivo) wrote:
> Extend test_fork_ksm_merging_page_count() to enable use_zero_pages and
> merge zero pages alongside non-zero pages before fork(), verifying that
> neither ksm_merging_pages nor ksm_zero_pages is inherited by the child.
> 
> Signed-off-by: Xu Xin (vivo) <xu.xin@linux.dev>
> ---
> Changes in v4:
> - Rename test_fork_ksm_statistic_count() to test_fork_ksm_counters() (David)
> - Unify cleanup into a single label and munmap before ksm_stop() (David)
> - Fix phrasing in log messages ("Reading KSM counters failed", etc.)
> 
>  .../selftests/mm/ksm_functional_tests.c       | 44 +++++++++++++++----
>  1 file changed, 35 insertions(+), 9 deletions(-)
> 
> diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
> index b871d5656688..7aadce09e6c7 100644
> --- a/tools/testing/selftests/mm/ksm_functional_tests.c
> +++ b/tools/testing/selftests/mm/ksm_functional_tests.c
> @@ -655,27 +655,51 @@ static void test_prot_none(void)
>  	munmap(map, size);
>  }
>  
> -static void test_fork_ksm_merging_page_count(void)
> +static void test_fork_ksm_counters(void)
>  {
> +	char *zero_map = MAP_FAILED, *map = MAP_FAILED;
>  	const unsigned int size = 2 * MiB;
> -	char *map;
> +	long merging_pages, zero_pages;
>  	pid_t child_pid;
>  	int status;
>  
>  	ksft_print_msg("[RUN] %s\n", __func__);
>  
> +	if (ksm_use_zero_pages() < 0) {
> +		ksft_test_result_skip("Enabling use_zero_pages failed\n");
> +		return;
> +	}
> +
>  	map = mmap_and_merge_range(0xcf, size, PROT_READ | PROT_WRITE, KSM_MERGE_MADVISE);
>  	if (map == MAP_FAILED)
>  		return;
>  
> +	zero_map = mmap_and_merge_range(0x00, size, PROT_READ | PROT_WRITE, KSM_MERGE_MADVISE);
> +	if (zero_map == MAP_FAILED)
> +		goto unmap;
> +
> +	merging_pages = ksm_get_self_merging_pages();
> +	zero_pages = ksm_get_self_zero_pages();
> +	if (merging_pages < 0 || zero_pages < 0) {
> +		ksft_test_result_skip("Reading KSM counters failed\n");
> +		goto unmap;
> +	}
> +
> +	if (!merging_pages || !zero_pages) {
> +		ksft_test_result_fail("KSM failed to merge pages before fork\n");
> +		goto unmap;
> +	}
> +
>  	if (ksm_pause() < 0) {
> -		ksft_test_result_fail("Stop ksmd from running failed\n");
> +		ksft_test_result_fail("Stopping ksmd from running failed\n");
>  		goto unmap;
>  	}
>  
>  	child_pid = fork();
>  	if (!child_pid) {
> -		exit(!!ksm_get_self_merging_pages());
> +		merging_pages = ksm_get_self_merging_pages();
> +		zero_pages = ksm_get_self_zero_pages();
> +		exit(merging_pages != 0 || zero_pages != 0);
>  	} else if (child_pid < 0) {
>  		ksft_test_result_fail("fork() failed\n");
>  		goto unmap;
> @@ -688,15 +712,17 @@ static void test_fork_ksm_merging_page_count(void)
>  
>  	status = WEXITSTATUS(status);
>  	if (status) {
> -		ksft_test_result_fail("ksm_merging_page in child: %d\n", status);
> +		ksft_test_result_fail("KSM counters are not zero in child\n");
>  		goto unmap;
>  	}
>  
> -	ksft_test_result_pass("ksm_merging_pages is not inherited after fork\n");
> -
> +	ksft_test_result_pass("ksm_merging_pages and ksm_zero_pages are reset after fork\n");
>  unmap:
> +	if (zero_map != MAP_FAILED)
> +		munmap(zero_map, size);
> +	if (map != MAP_FAILED)
> +		munmap(map, size);

For that to work you have to initialize both to MAP_FAILED above.

With that

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

-- 
Cheers,

David

      reply	other threads:[~2026-11-25 10:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 13:01 [PATCH v4 0/3] mm/ksm: fix ksm_zero_pages accounting " Xu Xin (vivo)
2026-11-24 13:03 ` [PATCH v4 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers " Xu Xin (vivo)
2026-11-25 10:32   ` David Hildenbrand (Arm)
2026-11-24 13:03 ` [PATCH v4 2/3] selftests/mm: fix false-positive and racy KSM fork test Xu Xin (vivo)
2026-11-24 13:03 ` [PATCH v4 3/3] selftests/mm: verify ksm_zero_pages is not inherited on fork Xu Xin (vivo)
2026-11-25 10:34   ` David Hildenbrand (Arm) [this message]

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=4d9417a1-eb75-4507-be86-a544437e3abd@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=chengming.zhou@linux.dev \
    --cc=donettom@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=stable@vger.kernel.org \
    --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

all inboxes | Powered by JetHome®