[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <d4988c21-1e1a-4de2-a1c0-7d78525d2939@redhat.com>
Date: Wed, 17 Sep 2025 15:19:24 +0200
From: David Hildenbrand <david@...hat.com>
To: Donet Tom <donettom@...ux.ibm.com>,
Andrew Morton <akpm@...ux-foundation.org>
Cc: Ritesh Harjani <ritesh.list@...il.com>, Xu Xin <xu.xin16@....com.cn>,
Chengming Zhou <chengming.zhou@...ux.dev>,
Wei Yang <richard.weiyang@...il.com>,
Aboorva Devarajan <aboorvad@...ux.ibm.com>, linux-mm@...ck.org,
linux-kernel@...r.kernel.org,
Giorgi Tchankvetadze <giorgitchankvetadze1997@...il.com>
Subject: Re: [PATCH v2 3/3] selftests/mm: Added fork test to verify global
ksm_zero_pages counter behavior
On 15.09.25 17:03, Donet Tom wrote:
> Added a selftest to verify the behavior of the global KSM zero-page
> counter during fork. When a process forks, the per-process zero-page
> counter is inherited by the child, and the global counter should be
> updated with this inherited value. This test ensures that the global
> counter is correctly updated after fork.
>
> Signed-off-by: Donet Tom <donettom@...ux.ibm.com>
> ---
> .../selftests/mm/ksm_functional_tests.c | 74 ++++++++++++++++++-
> 1 file changed, 73 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
> index 645cefba2126..f23597ac8066 100644
> --- a/tools/testing/selftests/mm/ksm_functional_tests.c
> +++ b/tools/testing/selftests/mm/ksm_functional_tests.c
> @@ -602,6 +602,77 @@ static void test_prot_none(void)
> munmap(map, size);
> }
>
> +long ksm_get_global_ksm_zero_pages(void)
> +{
> + int global_ksm_zero_pages_fd;
> + char buf[10];
> + ssize_t ret;
> +
> + global_ksm_zero_pages_fd = open("/sys/kernel/mm/ksm/ksm_zero_pages",
> + O_RDONLY);
> + if (global_ksm_zero_pages_fd < 0)
> + return -errno;
> +
> + ret = pread(global_ksm_zero_pages_fd, buf, sizeof(buf) - 1, 0);
> + close(global_ksm_zero_pages_fd);
> + if (ret <= 0)
> + return -errno;
> + buf[ret] = 0;
> +
> + return strtol(buf, NULL, 10);
> +}
> +
> +static void test_fork_global_ksm_zero_pages_count(void)
> +{
> + const unsigned int size = 2 * MiB;
> + char *map;
> + pid_t child_pid;
> + int status;
> + long g_zpages_before = 0, g_zpages_after = 0;
> +
> + ksft_print_msg("[RUN] %s\n", __func__);
> +
> + /* Unmerge all pages before test */
> + if (ksm_stop() < 0) {
> + ksft_test_result_fail("KSM unmerging failed\n");
> + return;
> + }
> + /* Get the global zero page count before test */
That only works when "use_zero_pages" is enabled, no?
> + g_zpages_before = ksm_get_global_ksm_zero_pages();
> + /* Let KSM deduplicate zero pages. */
> + map = mmap_and_merge_range(0x00, size, PROT_READ | PROT_WRITE, KSM_MERGE_MADVISE);
> + if (map == MAP_FAILED)
> + return;
> +
> + child_pid = fork();
> + if (!child_pid) {
> + exit(ksm_stop());
> + } else if (child_pid < 0) {
> + ksft_test_result_fail("fork() failed\n");
> + return;
Cleanup missing as for patch #2.
> + }
> + if (waitpid(child_pid, &status, 0) < 0) {
> + ksft_test_result_fail("waitpid() failed\n");
> + return;
> + }
> + status = WEXITSTATUS(status);
> + if (status < 0) {
> + ksft_test_result_fail("KSM unmerging failed in child\n");
> + return;
> + }
> +
> + /* Verify global zero-page count remains unchanged */
> + g_zpages_after = ksm_get_global_ksm_zero_pages();
> + if (g_zpages_before != g_zpages_after) {
> + ksft_test_result_fail("Incorrect global ksm zero page count after fork\n");
> + return;
> + }
> +
> + ksft_test_result_pass("Global ksm zero page count is correct after fork\n");
> + ksm_stop();
>
What stops KSM from merging pages in other processes by accident
concurrently and giving false failures?
Likely you would want to stop ksm before the fork.
--
Cheers
David / dhildenb
Powered by blists - more mailing lists