Re: [PATCH v3 3/3] selftests/mm: verify ksm_zero_pages is not inherited on fork

From: David Hildenbrand (Arm)

Date: Mon Nov 23 2026 - 11:49:38 EST


On 11/23/26 04:45, 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@xxxxxxxxx>
> ---
> .../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..0e128c579d5c 100644
> --- a/tools/testing/selftests/mm/ksm_functional_tests.c
> +++ b/tools/testing/selftests/mm/ksm_functional_tests.c
> @@ -655,45 +655,71 @@ static void test_prot_none(void)
> munmap(map, size);
> }
>
> -static void test_fork_ksm_merging_page_count(void)
> +static void test_fork_ksm_statistic_count(void)

"statistic_count" sounds odd. did you mean "statistics" or "counters". Below you
use "stats".

> {
> const unsigned int size = 2 * MiB;
> + long merging_pages, zero_pages;
> char *map;
> + char *zero_map;

Can combine with previous line.

> 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("read KSM stats failed\n");

"reading"

> + goto unmap_zero;
> + }
> +
> + if (!merging_pages || !zero_pages) {
> + ksft_test_result_fail("KSM failed to merge pages before fork\n");
> + goto unmap_zero;
> + }
> +
> if (ksm_pause() < 0) {
> ksft_test_result_fail("Stop ksmd from running failed\n");
> - goto unmap;
> + goto unmap_zero;
> }
>
> 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;
> + goto unmap_zero;
> }
>
> if (waitpid(child_pid, &status, 0) < 0) {
> ksft_test_result_fail("waitpid() failed\n");
> - goto unmap;
> + goto unmap_zero;
> }
>
> status = WEXITSTATUS(status);
> if (status) {
> ksft_test_result_fail("ksm_merging_page in child: %d\n", status);
> - goto unmap;
> + goto unmap_zero;
> }
>
> - 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_zero:
> + munmap(zero_map, size);
> unmap:
> ksm_stop();
> munmap(map, size);

This ordering is odd.

Why stop in between?

Also, I suggest using a single label, like:

char *zero_map = MAP_FAILED, *map = MAP_FAILED;

...

if (zero_map != MAP_FAILED)
munmap(zero_map, size);
if (map != MAP_FAILED)
munmap(map, size);

--
Cheers,

David