selftests/mm: Fix false positives and skip unsupported tests
Subsystems (4)
v3 cover selftests/mm: Fix false positives and skip unsupported tests
Hi all,
This patch series addresses false positives in the generic mm selftests and skips tests that cannot run correctly due to missing features or system limitations.
v2: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- Rebased onto the latest mm-new branch, top commit of the base is commit 0709ddf8951f ("mm: add zblock allocator").
- Minor refactor based on the review comments.
- Included the tags from the previous version.
---
v1: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Rebased onto the mm-new branch, top commit of the base is commit 3b4a8ad89f7e ("mm: add zblock allocator").
- Split some patches for clarity.
- Updated virtual_address_range test to support testing 4PB VA on PPC64.
- Added proper Fixes: tags.
- Included a patch to skip a failing userfaultfd test when unsupported,
instead of reporting a failure.
---
Please let us know if you have any further comments.
Thanks,
Aboorva
Aboorva Devarajan (3):
selftests/mm: Fix child process exit codes in ksm_functional_tests selftests/mm: Skip thuge-gen test if system is not setup properly selftests/mm: Skip hugepage-mremap test if userfaultfd unavailable
Donet Tom (4):
mm/selftests: Fix incorrect pointer being passed to mark_range() selftests/mm: Add support to test 4PB VA on PPC64 selftest/mm: Fix ksm_funtional_test failures mm/selftests: Fix split_huge_page_test failure on systems with 64KB
page size
tools/testing/selftests/mm/hugepage-mremap.c | 16 +++++++++-- .../selftests/mm/ksm_functional_tests.c | 28 +++++++++++++------ .../selftests/mm/split_huge_page_test.c | 23 +++++++++------ tools/testing/selftests/mm/thuge-gen.c | 11 +++++--- .../selftests/mm/virtual_address_range.c | 13 ++++++++- 5 files changed, 67 insertions(+), 24 deletions(-)
Signature · 1 line
2.47.1
PATCH v3 1/7 mm/selftests: Fix incorrect pointer being passed to mark_range()
From: Donet Tom <[email protected]>
In main(), the high address is stored in hptr, but for mark_range(), the address passed is ptr, not hptr. Fixed this by changing ptr[i] to hptr[i] in mark_range() function call.
--- tools/testing/selftests/mm/virtual_address_range.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/mm/virtual_address_range.c +1 -1
@@ -227,7 +227,7 @@ int main(int argc, char *argv[])if (hptr[i] ==MAP_FAILED )break ; -mark_range (ptr[i],MAP_CHUNK_SIZE ); +mark_range (hptr[i],MAP_CHUNK_SIZE );validate_addr (hptr[i],1 ); } hchunks = i;
Signature · 1 line
2.47.1
PATCH v3 2/7 selftests/mm: Add support to test 4PB VA on PPC64
In reply to Aboorva DevarajanFrom: Donet Tom <[email protected]>
PowerPC64 supports a 4PB virtual address space, but this test was previously limited to 512TB. This patch extends the coverage up to the full 4PB VA range on PowerPC64.
Memory from 0 to 128TB is allocated without an address hint, while allocations from 128TB to 4PB use a hint address.
--- tools/testing/selftests/mm/virtual_address_range.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
tools/testing/selftests/mm/virtual_address_range.c +11 -0
@@ -44,12 +44,18 @@* On Arm64 the address space is 256TB and support for * high mappings up to 4PB virtual address space has * been added. +* +* On PowerPC64, the address space up to 128TB can be +* mapped without a hint. Addresses beyond 128TB, up to +* 4PB, can be mapped with a hint. +* */ #define NR_CHUNKS_128TB ((128 * SZ_1TB) / MAP_CHUNK_SIZE)/* Number of chunks for 128TB */ #define NR_CHUNKS_256TB (NR_CHUNKS_128TB * 2UL)#define NR_CHUNKS_384TB (NR_CHUNKS_128TB * 3UL)#define NR_CHUNKS_3840TB (NR_CHUNKS_128TB * 30UL) +#define NR_CHUNKS_3968TB (NR_CHUNKS_128TB * 31UL)#define ADDR_MARK_128TB (1UL << 47)/* First address beyond 128TB */ #define ADDR_MARK_256TB (1UL << 48)/* First address beyond 256TB */
@@ -59,6 +65,11 @@#define HIGH_ADDR_SHIFT 49#define NR_CHUNKS_LOW NR_CHUNKS_256TB#define NR_CHUNKS_HIGH NR_CHUNKS_3840TB +#elif defined(__PPC64__) +#define HIGH_ADDR_MARK ADDR_MARK_128TB +#define HIGH_ADDR_SHIFT 48 +#define NR_CHUNKS_LOW NR_CHUNKS_128TB +#define NR_CHUNKS_HIGH NR_CHUNKS_3968TB#else #define HIGH_ADDR_MARK ADDR_MARK_128TB#define HIGH_ADDR_SHIFT 48
Signature · 1 line
2.47.1
PATCH v3 3/7 selftest/mm: Fix ksm_funtional_test failures
In reply to Aboorva DevarajanFrom: Donet Tom <[email protected]>
This patch fixed 2 issues.
1) After fork() in test_prctl_fork, the child process uses the file descriptors from the parent process to read ksm_stat and ksm_merging_pages. This results in incorrect values being read (parent process ksm_stat and ksm_merging_pages will be read in child), causing the test to fail.
This patch calls init_global_file_handles() in the child process to ensure that the current process's file descriptors are used to read ksm_stat and ksm_merging_pages.
2) All tests currently call ksm_merge to trigger page merging. To ensure the system remains in a consistent state for subsequent tests, it is better to call ksm_unmerge during the test cleanup phase.
In the test_prctl_fork test, after a fork(), reading ksm_merging_pages in the child process returns a non-zero value because a previous test performed a merge, and the child's memory state is inherited from the parent.
Although the child process calls ksm_unmerge, the ksm_merging_pages counter in the parent is reset to zero, while the child's counter remains unchanged. This discrepancy causes the test to fail.
To avoid this issue, each test should call ksm_unmerge during cleanup to ensure the counter is reset and the system is in a clean state for subsequent tests.
execv argument is an array of pointers to null-terminated strings. In this patch we also added NULL in the execv argument.
--- tools/testing/selftests/mm/ksm_functional_tests.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
tools/testing/selftests/mm/ksm_functional_tests.c +11 -1
@@ -46,6 +46,8 @@ static int ksm_use_zero_pages_fd;static int pagemap_fd;static size_t pagesize; +static void init_global_file_handles (void ); +static bool range_maps_duplicates (char *addr,unsigned long size) {unsigned long offs_a, offs_b, pfn_a, pfn_b;
@@ -274,6 +276,7 @@ static void test_unmerge(void)ksft_test_result (!range_maps_duplicates (map, size),"Pages were unmerged\n" ); unmap: +ksm_unmerge ();munmap (map, size); }
@@ -338,6 +341,7 @@ static void test_unmerge_zero_pages(void)ksft_test_result (!range_maps_duplicates (map, size),"KSM zero pages were unmerged\n" ); unmap: +ksm_unmerge ();munmap (map, size); }
@@ -366,6 +370,7 @@ static void test_unmerge_discarded(void)ksft_test_result (!range_maps_duplicates (map, size),"Pages were unmerged\n" ); unmap: +ksm_unmerge ();munmap (map, size); }
@@ -452,6 +457,7 @@ static void test_unmerge_uffd_wp(void) close_uffd:close (uffd); unmap: +ksm_unmerge (); munmap(map, size); }#endif
@@ -515,6 +521,7 @@ static int test_child_ksm(void)else if (map ==MAP_MERGE_SKIP )return -3 ; +ksm_unmerge ();munmap (map, size);return 0 ; }
@@ -548,6 +555,7 @@ static void test_prctl_fork(void) child_pid =fork ();if (!child_pid) { +init_global_file_handles ();exit (test_child_ksm ()); }else if (child_pid <0 ) {ksft_test_result_fail ("fork() failed\n" );
@@ -595,7 +603,7 @@ static void test_prctl_fork_exec(void)return ; }else if (child_pid ==0 ) {char *prg_name ="./ksm_functional_tests" ; -char *argv_for_program[] = { prg_name,FORK_EXEC_CHILD_PRG_NAME }; +char *argv_for_program[] = { prg_name,FORK_EXEC_CHILD_PRG_NAME ,NULL };execv (prg_name, argv_for_program);return ;
@@ -644,6 +652,7 @@ static void test_prctl_unmerge(void)ksft_test_result (!range_maps_duplicates (map, size),"Pages were unmerged\n" ); unmap: +ksm_unmerge ();munmap (map, size); }
@@ -677,6 +686,7 @@ static void test_prot_none(void)ksft_test_result (!range_maps_duplicates (map, size),"Pages were unmerged\n" ); unmap: +ksm_unmerge ();munmap (map, size); }
Signature · 1 line
2.47.1
Re: [PATCH v3 3/7] selftest/mm: Fix ksm_funtional_test failures
Quoted 59 lines · Aboorva Devarajan
From: Donet Tom <[email protected]>
This patch fixed 2 issues.
1) After
fork()intest_prctl_fork, the child process uses the file descriptors from the parent process to readksm_statandksm_merging_pages. This results in incorrect values being read (parent processksm_statandksm_merging_pageswill be read in child), causing the test to fail.This patch calls
init_global_file_handles()in the child process to ensure that the current process's file descriptors are used to readksm_statandksm_merging_pages.2) All tests currently call
ksm_mergeto trigger page merging. To ensure the system remains in a consistent state for subsequent tests, it is better to callksm_unmergeduring the test cleanup phase.In the
test_prctl_forktest, after afork(), readingksm_merging_pagesin the child process returns a non-zero value because a previous test performed a merge, and the child's memory state is inherited from the parent.Although the child process calls
ksm_unmerge, theksm_merging_pagescounter in the parent is reset to zero, while the child's counter remains unchanged. This discrepancy causes the test to fail.To avoid this issue, each test should call
ksm_unmergeduring cleanup to ensure the counter is reset and the system is in a clean state for subsequent tests.execv argument is an array of pointers to null-terminated strings. In this patch we also added NULL in the execv argument.
Fixes 6c47de3be3a0 ("selftest/mm: ksm_functional_tests: extend test case for ksm fork/exec")Co-developed-by Aboorva Devarajan <[email protected]>Signed-off-by Aboorva Devarajan <[email protected]>Signed-off-by Donet Tom <[email protected]>---
tools/testing/selftests/mm/ksm_functional_tests.c| 12 +++++++++++-1 file changed, 11 insertions(+), 1 deletion(-)tools/testing/selftests/mm/ksm_functional_tests.c +2
-0@@ -46,6 +46,8 @@ static int ksm_use_zero_pages_fd;static int pagemap_fd;static size_t pagesize; +static void init_global_file_handles (void ); +static bool range_maps_duplicates (char *addr,unsigned long size) {unsigned long offs_a, offs_b, pfn_a, pfn_b;@@ -274,6 +276,7 @@ static void test_unmerge(void)ksft_test_result (!range_maps_duplicates (map, size),"Pages were unmerged\n" );unmap:
tools/testing/selftests/mm/ksm_functional_tests.c
Quoted 1 line
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0+ksm_unmerge ();
In __mmap_and_merge_range(), we call ksm_unmerge(). Why this one not help?
Not very familiar with ksm stuff. Would you mind giving more on how this fix the failure you see?
Quoted 36 lines
munmap(map, size); }tools/testing/selftests/mm/ksm_functional_tests.c +0
-0@@ -338,6 +341,7 @@ static void test_unmerge_zero_pages(void)ksft_test_result (!range_maps_duplicates (map, size),"KSM zero pages were unmerged\n" );unmap:
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0+ksm_unmerge ();munmap(map, size); }tools/testing/selftests/mm/ksm_functional_tests.c +0
-0@@ -366,6 +370,7 @@ static void test_unmerge_discarded(void)ksft_test_result (!range_maps_duplicates (map, size),"Pages were unmerged\n" );unmap:
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0+ksm_unmerge ();munmap(map, size); }tools/testing/selftests/mm/ksm_functional_tests.c +0
-0@@ -452,6 +457,7 @@ static void test_unmerge_uffd_wp(void)
close_uffd:close(uffd);unmap:
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0+ksm_unmerge ();munmap(map, size); }#endif
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0@@ -515,6 +521,7 @@ static int test_child_ksm(void)else if (map ==MAP_MERGE_SKIP )return -3 ; +ksm_unmerge ();munmap (map, size);return 0 ; }
tools/testing/selftests/mm/ksm_functional_tests.c static void test_prctl_fork(void) · line 558
Quoted 4 lines
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0@@ -548,6 +555,7 @@ static void test_prctl_fork(void) child_pid =fork ();if (!child_pid) { +init_global_file_handles ();
Would this leave fd in parent as orphan?
Quoted 31 lines
tools/testing/selftests/mm/ksm_functional_tests.c +1
-1exit (test_child_ksm ()); }else if (child_pid <0 ) {ksft_test_result_fail ("fork() failed\n" );@@ -595,7 +603,7 @@ static void test_prctl_fork_exec(void)return ; }else if (child_pid ==0 ) {char *prg_name ="./ksm_functional_tests" ; -char *argv_for_program[] = { prg_name,FORK_EXEC_CHILD_PRG_NAME }; +char *argv_for_program[] = { prg_name,FORK_EXEC_CHILD_PRG_NAME ,NULL };execv (prg_name, argv_for_program);return ;@@ -644,6 +652,7 @@ static void test_prctl_unmerge(void)ksft_test_result (!range_maps_duplicates (map, size),"Pages were unmerged\n" );unmap:
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0+ksm_unmerge ();munmap(map, size); }tools/testing/selftests/mm/ksm_functional_tests.c +0
-0@@ -677,6 +686,7 @@ static void test_prot_none(void)ksft_test_result (!range_maps_duplicates (map, size),"Pages were unmerged\n" );unmap:
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0+ksm_unmerge ();munmap(map, size); }Signature · 1 line
2.47.1
Signature · 2 lines
Wei Yang Help you, Help me
Re: [PATCH v3 3/7] selftest/mm: Fix ksm_funtional_test failures
Quoted 37 lines
Quoted 37 lines
}
tools/testing/selftests/mm/ksm_functional_tests.c +0
-0@@ -338,6 +341,7 @@ static void test_unmerge_zero_pages(void)ksft_test_result (!range_maps_duplicates (map, size),"KSM zero pages were unmerged\n" );unmap:
(quoted diff) +1
-0+ ksm_unmerge();munmap(map, size); }(quoted diff) +0
-0@@ -366,6 +370,7 @@ static void test_unmerge_discarded(void) ksft_test_result(!range_maps_duplicates(map, size), "Pages were unmerged\n");unmap:
(quoted diff) +1
-0+ ksm_unmerge();munmap(map, size); }(quoted diff) +0
-0@@ -452,6 +457,7 @@ static void test_unmerge_uffd_wp(void)
close_uffd:close(uffd);unmap:
(quoted diff) +1
-0+ ksm_unmerge();munmap(map, size); }#endif
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0@@ -515,6 +521,7 @@ static int test_child_ksm(void)else if (map ==MAP_MERGE_SKIP )return -3 ; +ksm_unmerge ();munmap (map, size);return 0 ; }@@ -548,6 +555,7 @@ static void test_prctl_fork(void)
Quoted 5 lines
tools/testing/selftests/mm/ksm_functional_tests.c static void test_prctl_fork(void) · line 558
Quoted 3 lines
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0child_pid =fork ();if (!child_pid) { +init_global_file_handles ();Would this leave fd in parent as orphan?
Probably yes, but only until the child quits, so likely we don't care.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v3 3/7] selftest/mm: Fix ksm_funtional_test failures
In reply to Wei YangQuoted 63 lines · Wei Yang
Quoted 59 lines · Aboorva Devarajan
From: Donet Tom <[email protected]>
This patch fixed 2 issues.
1) After
fork()intest_prctl_fork, the child process uses the file descriptors from the parent process to readksm_statandksm_merging_pages. This results in incorrect values being read (parent processksm_statandksm_merging_pageswill be read in child), causing the test to fail.This patch calls
init_global_file_handles()in the child process to ensure that the current process's file descriptors are used to readksm_statandksm_merging_pages.2) All tests currently call
ksm_mergeto trigger page merging. To ensure the system remains in a consistent state for subsequent tests, it is better to callksm_unmergeduring the test cleanup phase.In the
test_prctl_forktest, after afork(), readingksm_merging_pagesin the child process returns a non-zero value because a previous test performed a merge, and the child's memory state is inherited from the parent.Although the child process calls
ksm_unmerge, theksm_merging_pagescounter in the parent is reset to zero, while the child's counter remains unchanged. This discrepancy causes the test to fail.To avoid this issue, each test should call
ksm_unmergeduring cleanup to ensure the counter is reset and the system is in a clean state for subsequent tests.execv argument is an array of pointers to null-terminated strings. In this patch we also added NULL in the execv argument.
Fixes 6c47de3be3a0 ("selftest/mm: ksm_functional_tests: extend test case for ksm fork/exec")Co-developed-by Aboorva Devarajan <[email protected]>Signed-off-by Aboorva Devarajan <[email protected]>Signed-off-by Donet Tom <[email protected]>---
tools/testing/selftests/mm/ksm_functional_tests.c| 12 +++++++++++-1 file changed, 11 insertions(+), 1 deletion(-)tools/testing/selftests/mm/ksm_functional_tests.c +2
-0@@ -46,6 +46,8 @@ static int ksm_use_zero_pages_fd;static int pagemap_fd;static size_t pagesize; +static void init_global_file_handles (void ); +static bool range_maps_duplicates (char *addr,unsigned long size) {unsigned long offs_a, offs_b, pfn_a, pfn_b;@@ -274,6 +276,7 @@ static void test_unmerge(void)ksft_test_result (!range_maps_duplicates (map, size),"Pages were unmerged\n" );unmap:
tools/testing/selftests/mm/ksm_functional_tests.c
Quoted 1 line
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0+ksm_unmerge ();In
__mmap_and_merge_range(), we callksm_unmerge(). Why this one not help?
Quoted 2 lines
Not very familiar with ksm stuff. Would you mind giving more on how this fix the failure you see?
The issue I was facing here was test_prctl_fork was failing.
# [RUN] test_prctl_fork
# Still pages merged
#
This issue occurred because the previous test performed a merge, causing the value of /proc/self/ksm_merging_pages to reflect the number of deduplicated pages. After that, a fork() was called. Post-fork, the child process inherited the parent's ksm_merging_pages value.
Then, the child process invoked __mmap_and_merge_range(), which resulted in unmerging the pages and resetting the value. However, since the parent process had performed the merge, its ksm_merging_pages value also got reset to 0. Meanwhile, the child process had not performed any merge itself, so the inherited value remained unchanged. That’s why get_my_merging_page() in the child was returning a non-zero value.
Initially, I fixed the issue by calling ksm_unmerge() before the fork(), and that resolved the problem. Later, I decided it would be cleaner to move the ksm_unmerge() call to the test cleanup phase.
Quoted 74 lines
Quoted 37 lines
munmap(map, size); }tools/testing/selftests/mm/ksm_functional_tests.c +0
-0@@ -338,6 +341,7 @@ static void test_unmerge_zero_pages(void)ksft_test_result (!range_maps_duplicates (map, size),"KSM zero pages were unmerged\n" );unmap:
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0+ksm_unmerge ();munmap(map, size); }tools/testing/selftests/mm/ksm_functional_tests.c +0
-0@@ -366,6 +370,7 @@ static void test_unmerge_discarded(void)ksft_test_result (!range_maps_duplicates (map, size),"Pages were unmerged\n" );unmap:
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0+ksm_unmerge ();munmap(map, size); }tools/testing/selftests/mm/ksm_functional_tests.c +0
-0@@ -452,6 +457,7 @@ static void test_unmerge_uffd_wp(void)
close_uffd:close(uffd);unmap:
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0+ksm_unmerge ();munmap(map, size); }#endif
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0@@ -515,6 +521,7 @@ static int test_child_ksm(void)else if (map ==MAP_MERGE_SKIP )return -3 ; +ksm_unmerge ();munmap (map, size);return 0 ; }tools/testing/selftests/mm/ksm_functional_tests.c static void test_prctl_fork(void) · line 558
Quoted 4 lines
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0@@ -548,6 +555,7 @@ static void test_prctl_fork(void) child_pid =fork ();if (!child_pid) { +init_global_file_handles ();Would this leave fd in parent as orphan?
Quoted 31 lines
tools/testing/selftests/mm/ksm_functional_tests.c +1
-1exit (test_child_ksm ()); }else if (child_pid <0 ) {ksft_test_result_fail ("fork() failed\n" );@@ -595,7 +603,7 @@ static void test_prctl_fork_exec(void)return ; }else if (child_pid ==0 ) {char *prg_name ="./ksm_functional_tests" ; -char *argv_for_program[] = { prg_name,FORK_EXEC_CHILD_PRG_NAME }; +char *argv_for_program[] = { prg_name,FORK_EXEC_CHILD_PRG_NAME ,NULL };execv (prg_name, argv_for_program);return ;@@ -644,6 +652,7 @@ static void test_prctl_unmerge(void)ksft_test_result (!range_maps_duplicates (map, size),"Pages were unmerged\n" );unmap:
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0+ksm_unmerge ();munmap(map, size); }tools/testing/selftests/mm/ksm_functional_tests.c +0
-0@@ -677,6 +686,7 @@ static void test_prot_none(void)ksft_test_result (!range_maps_duplicates (map, size),"Pages were unmerged\n" );unmap:
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0+ksm_unmerge ();munmap(map, size); }Signature · 1 line
2.47.1
Re: [PATCH v3 3/7] selftest/mm: Fix ksm_funtional_test failures
Quoted 5 lines · Donet Tom
Quoted 3 lines · Wei Yang
Quoted 2 lines · Aboorva Devarajan
From: Donet Tom <[email protected]>
[...]
Quoted 30 lines
Quoted 20 lines
Quoted 17 lines
tools/testing/selftests/mm/ksm_functional_tests.c +2
-0@@ -46,6 +46,8 @@ static int ksm_use_zero_pages_fd;static int pagemap_fd;static size_t pagesize; +static void init_global_file_handles (void ); +static bool range_maps_duplicates (char *addr,unsigned long size) {unsigned long offs_a, offs_b, pfn_a, pfn_b;@@ -274,6 +276,7 @@ static void test_unmerge(void)ksft_test_result (!range_maps_duplicates (map, size),"Pages were unmerged\n" );unmap:
tools/testing/selftests/mm/ksm_functional_tests.c
Quoted 1 line
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0+ksm_unmerge ();In
__mmap_and_merge_range(), we callksm_unmerge(). Why this one not help?Quoted 2 lines
Not very familiar with ksm stuff. Would you mind giving more on how this fix the failure you see?
The issue I was facing here was
test_prctl_forkwas failing.# [RUN]
test_prctl_fork
# Still pages merged
#
Quoted 6 lines
This issue occurred because the previous test performed a merge, causing the value of
/proc/self/ksm_merging_pagesto reflect the number of deduplicated pages. After that, afork()was called. Post-fork, the child process
inherited the parent'sksm_merging_pagesvalue.
Yes, this one is fixed by calling init_global_file_handles() in child.
Quoted 6 lines
Then, the child process invoked
__mmap_and_merge_range(), which resulted in unmerging the pages and resetting the value. However, since the parent process
had performed the merge, itsksm_merging_pagesvalue also got reset to 0. Meanwhile, the child process had not performed any merge itself, so the inherited
I assume the behavior described here is after the change to call init_global_file_handles() in child.
Child process inherit the ksm_merging_pages from parent, which is reasonable to me. But I am confused why ksm_unmerge() would just reset ksm_merging_pages for parent and leave ksm_merging_pages in child process unchanged.
ksm_unmerge() writes to /sys/kernel/mm/ksm/run, which is a system wide sysfs interface. I expect it applies to both parent and child.
Quoted 3 lines
value remained unchanged. That’s why
get_my_merging_page()in the child was returning a non-zero value.
I guess you mean the get_my_merging_page() in __mmap_and_merge_range() return a non-zero value. But there is ksm_unmerge() before it. Why this ksm_unmerge() couldn't reset the value, but a ksm_unmerge() in parent could.
Quoted 5 lines
Initially, I fixed the issue by calling
ksm_unmerge()before thefork(), and that
resolved the problem. Later, I decided it would be cleaner to move theksm_unmerge()call to the test cleanup phase.
Also all the tests before test_prctl_fork(), except test_prctl(), calls
ksft_test_result(!range_maps_duplicates());
If the previous tests succeed, it means there is no duplicate pages. This means ksm_merging_pages should be 0 before test_prctl_fork() if other tests pass. And the child process would inherit a 0 ksm_merging_pages. (A quick test proves it.)
So which part of the story I missed?
Signature · 2 lines
Wei Yang Help you, Help me
Re: [PATCH v3 3/7] selftest/mm: Fix ksm_funtional_test failures
Quoted 43 lines · Wei Yang
Quoted 4 lines · Donet Tom
Quoted 3 lines · Wei Yang
Quoted 2 lines · Aboorva Devarajan
From: Donet Tom <[email protected]>
[...]
Quoted 29 lines
Quoted 20 lines
Quoted 17 lines
tools/testing/selftests/mm/ksm_functional_tests.c +2
-0@@ -46,6 +46,8 @@ static int ksm_use_zero_pages_fd;static int pagemap_fd;static size_t pagesize; +static void init_global_file_handles (void ); +static bool range_maps_duplicates (char *addr,unsigned long size) {unsigned long offs_a, offs_b, pfn_a, pfn_b;@@ -274,6 +276,7 @@ static void test_unmerge(void)ksft_test_result (!range_maps_duplicates (map, size),"Pages were unmerged\n" );unmap:
tools/testing/selftests/mm/ksm_functional_tests.c
Quoted 1 line
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0+ksm_unmerge ();In
__mmap_and_merge_range(), we callksm_unmerge(). Why this one not help?Quoted 2 lines
Not very familiar with ksm stuff. Would you mind giving more on how this fix the failure you see?
The issue I was facing here was
test_prctl_forkwas failing.# [RUN]
test_prctl_fork
# Still pages merged
#Quoted 6 lines
This issue occurred because the previous test performed a merge, causing the value of
/proc/self/ksm_merging_pagesto reflect the number of deduplicated pages. After that, afork()was called. Post-fork, the child process
inherited the parent'sksm_merging_pagesvalue.Yes, this one is fixed by calling
init_global_file_handles()in child.
Quoted 8 lines
Quoted 6 lines
Then, the child process invoked
__mmap_and_merge_range(), which resulted in unmerging the pages and resetting the value. However, since the parent process
had performed the merge, itsksm_merging_pagesvalue also got reset to 0. Meanwhile, the child process had not performed any merge itself, so the inheritedI assume the behavior described here is after the change to call
init_global_file_handles()in child.
Yes
Quoted 7 lines
Child process inherit the
ksm_merging_pagesfrom parent, which is reasonable to me. But I am confused whyksm_unmerge()would just resetksm_merging_pagesfor parent and leaveksm_merging_pagesin child process unchanged.
ksm_unmerge()writes to/sys/kernel/mm/ksm/run, which is a system wide sysfs interface. I expect it applies to both parent and child.
I am not very familiar with the KSM code, but from what I understand:
The ksm_merging_pages counter is maintained per mm_struct. When we write to /sys/kernel/mm/ksm/run, unmerging is triggered, and the counters are updated for all mm_structs present in the ksm_mm_slot list.
A mm_struct gets added to this list when MADV_MERGEABLE is called. In the case of the child process, since MADV_MERGEABLE has not been invoked yet, its mm_struct is not part of the list. As a result, its ksm_merging_pages counter is not reset.
Quoted 12 lines
Quoted 3 lines
value remained unchanged. That’s why
get_my_merging_page()in the child was returning a non-zero value.I guess you mean the
get_my_merging_page()in__mmap_and_merge_range()return a non-zero value. But there isksm_unmerge()before it. Why thisksm_unmerge()couldn't reset the value, but aksm_unmerge()in parent could.Quoted 5 lines
Initially, I fixed the issue by calling
ksm_unmerge()before thefork(), and that
resolved the problem. Later, I decided it would be cleaner to move theksm_unmerge()call to the test cleanup phase.
Quoted 8 lines
Also all the tests before
test_prctl_fork(), excepttest_prctl(), callsksft_test_result(!range_maps_duplicates());If the previous tests succeed, it means there is no duplicate pages. This means
ksm_merging_pagesshould be 0 beforetest_prctl_fork()if other tests pass. And the child process would inherit a 0ksm_merging_pages. (A quick test proves it.)
If I understand correctly, all the tests are calling MADV_UNMERGEABLE, which internally calls break_ksm() in the kernel. This function replaces the KSM page with an exclusive anonymous page. However, the ksm_merging_pages counters are not updated at this point.
The function range_maps_duplicates(map, size) checks whether the pages have been unmerged. Since break_ksm() does perform the unmerge, this function returns false, and the test passes.
The ksm_merging_pages update happens later via the ksm_scan_thread(). That’s why we observe that ksm_merging_pages values are not reset immediately after the test finishes.
If we add a sleep(1) after the MADV_UNMERGEABLE call, we can see that the ksm_merging_pages values are reset after the sleep.
Once the test completes successfully, we can call ksm_unmerge(), which will immediately reset the ksm_merging_pages value. This way, in the fork test, the child process will also see the correct value.
Quoted 3 lines
So which part of the story I missed?
So, during the cleanup phase after a successful test, we can call ksm_unmerge() to reset the counter. Do you see any issue with this approach?
Quoted 1 line
Re: [PATCH v3 3/7] selftest/mm: Fix ksm_funtional_test failures
On Wed, Aug 06, 2025 at 06:30:37PM +0530, Donet Tom wrote:
[...]
Quoted 9 lines
Quoted 6 lines
Child process inherit the
ksm_merging_pagesfrom parent, which is reasonable to me. But I am confused whyksm_unmerge()would just resetksm_merging_pagesfor parent and leaveksm_merging_pagesin child process unchanged.
ksm_unmerge()writes to/sys/kernel/mm/ksm/run, which is a system wide sysfs interface. I expect it applies to both parent and child.I am not very familiar with the KSM code, but from what I understand:
Quoted 9 lines
The
ksm_merging_pagescounter is maintained permm_struct. When we write to/sys/kernel/mm/ksm/run, unmerging is triggered, and the counters are updated for allmm_structspresent in theksm_mm_slotlist.A
mm_structgets added to this list whenMADV_MERGEABLEis called. In the case of the child process, sinceMADV_MERGEABLEhas not been invoked yet, itsmm_structis not part of the list. As a result, itsksm_merging_pagescounter is not reset.
Would this flag be inherited during fork? VM_MERGEABLE is saved in related vma I don't see it would be dropped during fork. Maybe missed.
Quoted 28 lines
Quoted 13 lines
Quoted 4 lines
value remained unchanged. That’s why
get_my_merging_page()in the child was returning a non-zero value.I guess you mean the
get_my_merging_page()in__mmap_and_merge_range()return a non-zero value. But there isksm_unmerge()before it. Why thisksm_unmerge()couldn't reset the value, but aksm_unmerge()in parent could.Quoted 5 lines
Initially, I fixed the issue by calling
ksm_unmerge()before thefork(), and that
resolved the problem. Later, I decided it would be cleaner to move theksm_unmerge()call to the test cleanup phase.Quoted 8 lines
Also all the tests before
test_prctl_fork(), excepttest_prctl(), callsksft_test_result(!range_maps_duplicates());If the previous tests succeed, it means there is no duplicate pages. This means
ksm_merging_pagesshould be 0 beforetest_prctl_fork()if other tests pass. And the child process would inherit a 0ksm_merging_pages. (A quick test proves it.)If I understand correctly, all the tests are calling
MADV_UNMERGEABLE, which internally callsbreak_ksm()in the kernel. This function replaces the KSM page with an exclusive anonymous page. However, theksm_merging_pagescounters are not updated at this point.
Quoted 8 lines
The function
range_maps_duplicates(map, size)checks whether the pages have been unmerged. Sincebreak_ksm()does perform the unmerge, this function returns false, and the test passes.The
ksm_merging_pagesupdate happens later via theksm_scan_thread(). That’s why we observe thatksm_merging_pagesvalues are not reset immediately after the test finishes.
Not familiar with ksm internal. But the ksm_merging_pages counter still has non-zero value when all merged pages are unmerged makes me feel odd.
Quoted 7 lines
If we add a sleep(1) after the
MADV_UNMERGEABLEcall, we can see that theksm_merging_pagesvalues are reset after the sleep.Once the test completes successfully, we can call
ksm_unmerge(), which will immediately reset theksm_merging_pagesvalue. This way, in the fork test, the child process will also see the correct value.Quoted 1 line
Quoted 7 lines
Quoted 2 lines
So which part of the story I missed?
So, during the cleanup phase after a successful test, we can call
ksm_unmerge()to reset the counter. Do you see any issue with this approach?
It looks there is no issue with an extra ksm_unmerge().
But one more question. Why an extra ksm_unmerge() could help.
Here is what we have during test:
test_prot_none()
!range_maps_duplicates()
ksm_unmerge() 1) <--- newly add
test_prctl_fork()
>--- in child
__mmap_and_merge_range()
ksm_unmerge() 2) <--- already haveAs you mentioned above ksm_unmerge() would immediately reset ksm_merging_pages, why ksm_unmerge() at 2) still leave ksm_merging_pages non-zero? And the one at 1) could help.
Or there is still some timing issue like sleep(1) you did?
Signature · 2 lines
Wei Yang Help you, Help me
Re: [PATCH v3 3/7] selftest/mm: Fix ksm_funtional_test failures
Quoted 82 lines · Wei Yang
On Wed, Aug 06, 2025 at 06:30:37PM +0530, Donet Tom wrote:
[...]Quoted 8 lines
Quoted 6 lines
Child process inherit the
ksm_merging_pagesfrom parent, which is reasonable to me. But I am confused whyksm_unmerge()would just resetksm_merging_pagesfor parent and leaveksm_merging_pagesin child process unchanged.
ksm_unmerge()writes to/sys/kernel/mm/ksm/run, which is a system wide sysfs interface. I expect it applies to both parent and child.I am not very familiar with the KSM code, but from what I understand:
Quoted 9 lines
The
ksm_merging_pagescounter is maintained permm_struct. When we write to/sys/kernel/mm/ksm/run, unmerging is triggered, and the counters are updated for allmm_structspresent in theksm_mm_slotlist.A
mm_structgets added to this list whenMADV_MERGEABLEis called. In the case of the child process, sinceMADV_MERGEABLEhas not been invoked yet, itsmm_structis not part of the list. As a result, itsksm_merging_pagescounter is not reset.Would this flag be inherited during fork?
VM_MERGEABLEis saved in related vma I don't see it would be dropped during fork. Maybe missed.Quoted 26 lines
Quoted 12 lines
Quoted 3 lines
value remained unchanged. That’s why
get_my_merging_page()in the child was returning a non-zero value.I guess you mean the
get_my_merging_page()in__mmap_and_merge_range()return a non-zero value. But there isksm_unmerge()before it. Why thisksm_unmerge()couldn't reset the value, but aksm_unmerge()in parent could.Quoted 5 lines
Initially, I fixed the issue by calling
ksm_unmerge()before thefork(), and that
resolved the problem. Later, I decided it would be cleaner to move theksm_unmerge()call to the test cleanup phase.Quoted 8 lines
Also all the tests before
test_prctl_fork(), excepttest_prctl(), callsksft_test_result(!range_maps_duplicates());If the previous tests succeed, it means there is no duplicate pages. This means
ksm_merging_pagesshould be 0 beforetest_prctl_fork()if other tests pass. And the child process would inherit a 0ksm_merging_pages. (A quick test proves it.)If I understand correctly, all the tests are calling
MADV_UNMERGEABLE, which internally callsbreak_ksm()in the kernel. This function replaces the KSM page with an exclusive anonymous page. However, theksm_merging_pagescounters are not updated at this point.Quoted 8 lines
The function
range_maps_duplicates(map, size)checks whether the pages have been unmerged. Sincebreak_ksm()does perform the unmerge, this function returns false, and the test passes.The
ksm_merging_pagesupdate happens later via theksm_scan_thread(). That’s why we observe thatksm_merging_pagesvalues are not reset immediately after the test finishes.Not familiar with ksm internal. But the
ksm_merging_pagescounter still has non-zero value when all merged pages are unmerged makes me feel odd.Quoted 12 lines
If we add a sleep(1) after the
MADV_UNMERGEABLEcall, we can see that theksm_merging_pagesvalues are reset after the sleep.Once the test completes successfully, we can call
ksm_unmerge(), which will immediately reset theksm_merging_pagesvalue. This way, in the fork test, the child process will also see the correct value.Quoted 2 lines
So which part of the story I missed?
So, during the cleanup phase after a successful test, we can call
ksm_unmerge()to reset the counter. Do you see any issue with this approach?It looks there is no issue with an extra
ksm_unmerge().But one more question. Why an extra
ksm_unmerge()could help.Here is what we have during test:
test_prot_none()
!range_maps_duplicates()
ksm_unmerge()1) <--- newly add
test_prctl_fork()
Quoted 7 lines
>--- in child __mmap_and_merge_range() ksm_unmerge() 2) <--- already haveAs you mentioned above
ksm_unmerge()would immediately resetksm_merging_pages, whyksm_unmerge()at 2) still leaveksm_merging_pagesnon-zero? And the one at 1) could help.
From the debugging, what I understood is:
When we perform fork(), MADV_MERGEABLE, or PR_SET_MEMORY_MERGE, the mm_struct of the process gets added to the ksm_mm_slot list. As a result, both the parent and child processes’ mm_struct structures will be present in ksm_mm_slot.
When KSM merges the pages, it creates a ksm_rmap_item for each page, and the ksm_merging_pages counter is incremented accordingly.
Since the parent process did the merge, its mm_struct is present in ksm_mm_slot, and ksm_rmap_item entries are created for all the merged pages.
When a process is forked, the child’s mm_struct is also added to ksm_mm_slot, and it inherits the ksm_merging_pages count. However, no ksm_rmap_item entries are created for the child process because it did not do any merge.
When ksm_unmerge() is called, it iterates over all processes in ksm_mm_slot. In our case, both the parent and child are present. It first processes the parent, which has ksm_rmap_item entries, so it unmerges the pages and resets the ksm_merging_pages counter.
For the child, since it did not perform any actual merging, it does not have any ksm_rmap_item entries. Therefore, there are no pages to unmerge, and the counter remains unchanged.
So, only processes that performed KSM merging will have their counters updated during ksm_unmerge(). The child process, having not initiated any merging, retains the inherited counter value without any update.
So from a testing point of view, I think it is better to reset the counters as part of the cleanup code to ensure that the next tests do not get incorrect values.
The question I have is: is it correct to keep the inherited |ksm_merging_page| value in the child or Should we reset it to 0 during |ksm_fork()|?
Quoted 3 lines
Or there is still some timing issue like sleep(1) you did?
Re: [PATCH v3 3/7] selftest/mm: Fix ksm_funtional_test failures
Quoted 112 lines · Donet Tom
Quoted 82 lines · Wei Yang
On Wed, Aug 06, 2025 at 06:30:37PM +0530, Donet Tom wrote:
[...]Quoted 8 lines
Quoted 6 lines
Child process inherit the
ksm_merging_pagesfrom parent, which is reasonable to me. But I am confused whyksm_unmerge()would just resetksm_merging_pagesfor parent and leaveksm_merging_pagesin child process unchanged.
ksm_unmerge()writes to/sys/kernel/mm/ksm/run, which is a system wide sysfs interface. I expect it applies to both parent and child.I am not very familiar with the KSM code, but from what I understand:
Quoted 9 lines
The
ksm_merging_pagescounter is maintained permm_struct. When we write to/sys/kernel/mm/ksm/run, unmerging is triggered, and the counters are updated for allmm_structspresent in theksm_mm_slotlist.A
mm_structgets added to this list whenMADV_MERGEABLEis called. In the case of the child process, sinceMADV_MERGEABLEhas not been invoked yet, itsmm_structis not part of the list. As a result, itsksm_merging_pagescounter is not reset.Would this flag be inherited during fork?
VM_MERGEABLEis saved in related vma I don't see it would be dropped during fork. Maybe missed.Quoted 26 lines
Quoted 12 lines
Quoted 3 lines
value remained unchanged. That’s why
get_my_merging_page()in the child was returning a non-zero value.I guess you mean the
get_my_merging_page()in__mmap_and_merge_range()return a non-zero value. But there isksm_unmerge()before it. Why thisksm_unmerge()couldn't reset the value, but aksm_unmerge()in parent could.Quoted 5 lines
Initially, I fixed the issue by calling
ksm_unmerge()before thefork(), and that
resolved the problem. Later, I decided it would be cleaner to move theksm_unmerge()call to the test cleanup phase.Quoted 8 lines
Also all the tests before
test_prctl_fork(), excepttest_prctl(), callsksft_test_result(!range_maps_duplicates());If the previous tests succeed, it means there is no duplicate pages. This means
ksm_merging_pagesshould be 0 beforetest_prctl_fork()if other tests pass. And the child process would inherit a 0ksm_merging_pages. (A quick test proves it.)If I understand correctly, all the tests are calling
MADV_UNMERGEABLE, which internally callsbreak_ksm()in the kernel. This function replaces the KSM page with an exclusive anonymous page. However, theksm_merging_pagescounters are not updated at this point.Quoted 8 lines
The function
range_maps_duplicates(map, size)checks whether the pages have been unmerged. Sincebreak_ksm()does perform the unmerge, this function returns false, and the test passes.The
ksm_merging_pagesupdate happens later via theksm_scan_thread(). That’s why we observe thatksm_merging_pagesvalues are not reset immediately after the test finishes.Not familiar with ksm internal. But the
ksm_merging_pagescounter still has non-zero value when all merged pages are unmerged makes me feel odd.Quoted 12 lines
If we add a sleep(1) after the
MADV_UNMERGEABLEcall, we can see that theksm_merging_pagesvalues are reset after the sleep.Once the test completes successfully, we can call
ksm_unmerge(), which will immediately reset theksm_merging_pagesvalue. This way, in the fork test, the child process will also see the correct value.Quoted 2 lines
So which part of the story I missed?
So, during the cleanup phase after a successful test, we can call
ksm_unmerge()to reset the counter. Do you see any issue with this approach?It looks there is no issue with an extra
ksm_unmerge().But one more question. Why an extra
ksm_unmerge()could help.Here is what we have during test:
test_prot_none()
!range_maps_duplicates()
ksm_unmerge()1) <--- newly add
test_prctl_fork()Quoted 7 lines
>--- in child __mmap_and_merge_range() ksm_unmerge() 2) <--- already haveAs you mentioned above
ksm_unmerge()would immediately resetksm_merging_pages, whyksm_unmerge()at 2) still leaveksm_merging_pagesnon-zero? And the one at 1) could help.From the debugging, what I understood is:
When we perform
fork(),MADV_MERGEABLE, orPR_SET_MEMORY_MERGE, themm_structof the process gets added to theksm_mm_slotlist. As a result, both the parent and child processes’mm_structstructures will be present inksm_mm_slot.When KSM merges the pages, it creates a
ksm_rmap_itemfor each page, and theksm_merging_pagescounter is incremented accordingly.Since the parent process did the merge, its
mm_structis present inksm_mm_slot, andksm_rmap_itementries are created for all the merged pages.When a process is forked, the child’s
mm_structis also added toksm_mm_slot, and it inherits theksm_merging_pagescount. However, noksm_rmap_itementries are created for the child process because it did not do any merge.
Quoted 9 lines
When
ksm_unmerge()is called, it iterates over all processes inksm_mm_slot. In our case, both the parent and child are present. It first processes the parent, which hasksm_rmap_itementries, so it unmerges the pages and resets theksm_merging_pagescounter.For the child, since it did not perform any actual merging, it does not have any
ksm_rmap_itementries. Therefore, there are no pages to unmerge, and the counter remains unchanged.
Thanks for the detailed analysis.
So the key is child has no ksm_rmap_item which will not clear ksm_merging_page on ksm_unmerge().
Quoted 8 lines
So, only processes that performed KSM merging will have their counters updated during
ksm_unmerge(). The child process, having not initiated any merging, retains the inherited counter value without any update.So from a testing point of view, I think it is better to reset the counters as part of the cleanup code to ensure that the next tests do not get incorrect values.
Hmm... I agree from the test point of view based on current situation.
While maybe this is also a check point for later version.
Quoted 4 lines
The question I have is: is it correct to keep the inherited
|ksm_merging_page| value in the child or Should we reset it to 0 during |ksm_fork()|?
Very good question. There looks to be something wrong, but I am not sure this is the correct way.
Quoted 3 lines
Quoted 3 lines
Or there is still some timing issue like sleep(1) you did?
Signature · 2 lines
Wei Yang Help you, Help me
Re: [PATCH v3 3/7] selftest/mm: Fix ksm_funtional_test failures
Quoted 127 lines · Wei Yang
Quoted 108 lines · Donet Tom
Quoted 85 lines · Wei Yang
On Wed, Aug 06, 2025 at 06:30:37PM +0530, Donet Tom wrote:
[...]Quoted 8 lines
Quoted 6 lines
Child process inherit the
ksm_merging_pagesfrom parent, which is reasonable to me. But I am confused whyksm_unmerge()would just resetksm_merging_pagesfor parent and leaveksm_merging_pagesin child process unchanged.
ksm_unmerge()writes to/sys/kernel/mm/ksm/run, which is a system wide sysfs interface. I expect it applies to both parent and child.I am not very familiar with the KSM code, but from what I understand:
Quoted 9 lines
The
ksm_merging_pagescounter is maintained permm_struct. When we write to/sys/kernel/mm/ksm/run, unmerging is triggered, and the counters are updated for allmm_structspresent in theksm_mm_slotlist.A
mm_structgets added to this list whenMADV_MERGEABLEis called. In the case of the child process, sinceMADV_MERGEABLEhas not been invoked yet, itsmm_structis not part of the list. As a result, itsksm_merging_pagescounter is not reset.Would this flag be inherited during fork?
VM_MERGEABLEis saved in related vma I don't see it would be dropped during fork. Maybe missed.Quoted 25 lines
Quoted 12 lines
Quoted 3 lines
value remained unchanged. That’s why
get_my_merging_page()in the child was returning a non-zero value.I guess you mean the
get_my_merging_page()in__mmap_and_merge_range()return a non-zero value. But there isksm_unmerge()before it. Why thisksm_unmerge()couldn't reset the value, but aksm_unmerge()in parent could.Quoted 5 lines
Initially, I fixed the issue by calling
ksm_unmerge()before thefork(), and that
resolved the problem. Later, I decided it would be cleaner to move theksm_unmerge()call to the test cleanup phase.Quoted 8 lines
Also all the tests before
test_prctl_fork(), excepttest_prctl(), callsksft_test_result(!range_maps_duplicates());If the previous tests succeed, it means there is no duplicate pages. This means
ksm_merging_pagesshould be 0 beforetest_prctl_fork()if other tests pass. And the child process would inherit a 0ksm_merging_pages. (A quick test proves it.)If I understand correctly, all the tests are calling
MADV_UNMERGEABLE, which internally callsbreak_ksm()in the kernel. This function replaces the KSM page with an exclusive anonymous page. However, theksm_merging_pagescounters are not updated at this point.Quoted 8 lines
The function
range_maps_duplicates(map, size)checks whether the pages have been unmerged. Sincebreak_ksm()does perform the unmerge, this function returns false, and the test passes.The
ksm_merging_pagesupdate happens later via theksm_scan_thread(). That’s why we observe thatksm_merging_pagesvalues are not reset immediately after the test finishes.Not familiar with ksm internal. But the
ksm_merging_pagescounter still has non-zero value when all merged pages are unmerged makes me feel odd.Quoted 12 lines
If we add a sleep(1) after the
MADV_UNMERGEABLEcall, we can see that theksm_merging_pagesvalues are reset after the sleep.Once the test completes successfully, we can call
ksm_unmerge(), which will immediately reset theksm_merging_pagesvalue. This way, in the fork test, the child process will also see the correct value.Quoted 2 lines
So which part of the story I missed?
So, during the cleanup phase after a successful test, we can call
ksm_unmerge()to reset the counter. Do you see any issue with this approach?It looks there is no issue with an extra
ksm_unmerge().But one more question. Why an extra
ksm_unmerge()could help.Here is what we have during test:
test_prot_none()
!range_maps_duplicates()
ksm_unmerge()1) <--- newly add
test_prctl_fork()
>--- in child
__mmap_and_merge_range()
ksm_unmerge()2) <--- already haveQuoted 3 lines
As you mentioned above
ksm_unmerge()would immediately resetksm_merging_pages, whyksm_unmerge()at 2) still leaveksm_merging_pagesnon-zero? And the one at 1) could help.From the debugging, what I understood is:
When we performfork(),MADV_MERGEABLE, orPR_SET_MEMORY_MERGE, themm_structof the process gets added to theksm_mm_slotlist. As a result, both the parent and child processes’mm_structstructures will be present inksm_mm_slot.When KSM merges the pages, it creates a
ksm_rmap_itemfor each page, and theksm_merging_pagescounter is incremented accordingly.Since the parent process did the merge, its
mm_structis present inksm_mm_slot, andksm_rmap_itementries are created for all the merged pages.When a process is forked, the child’s
mm_structis also added toksm_mm_slot, and it inherits theksm_merging_pagescount. However, noksm_rmap_itementries are created for the child process because it did not do any merge.Quoted 9 lines
When
ksm_unmerge()is called, it iterates over all processes inksm_mm_slot. In our case, both the parent and child are present. It first processes the parent, which hasksm_rmap_itementries, so it unmerges the pages and resets theksm_merging_pagescounter.For the child, since it did not perform any actual merging, it does not have any
ksm_rmap_itementries. Therefore, there are no pages to unmerge, and the counter remains unchanged.Thanks for the detailed analysis.
So the key is child has no
ksm_rmap_itemwhich will not clearksm_merging_pageonksm_unmerge().Quoted 4 lines
So, only processes that performed KSM merging will have their counters updated during
ksm_unmerge(). The child process, having not initiated any merging, retains the inherited counter value without any update.
Quoted 7 lines
Quoted 4 lines
So from a testing point of view, I think it is better to reset the counters as part of the cleanup code to ensure that the next tests do not get incorrect values.
Hmm... I agree from the test point of view based on current situation.
While maybe this is also a check point for later version.
Are you okay to proceed with the current patch in this series?
Quoted 7 lines
Quoted 5 lines
The question I have is: is it correct to keep the inherited
|ksm_merging_page| value in the child or Should we reset it to 0 during |ksm_fork()|?Very good question. There looks to be something wrong, but I am not sure this is the correct way.
ok.
I am going through it and will come up with a fix along with a test for this scenario. I will post it as a separate series.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Or there is still some timing issue like sleep(1) you did?
Re: [PATCH v3 3/7] selftest/mm: Fix ksm_funtional_test failures
On Fri, Aug 08, 2025 at 07:55:37PM +0530, Donet Tom wrote:
[...]
Quoted 13 lines
Quoted 13 lines
Thanks for the detailed analysis.
So the key is child has no
ksm_rmap_itemwhich will not clearksm_merging_pageonksm_unmerge().Quoted 8 lines
So, only processes that performed KSM merging will have their counters updated during
ksm_unmerge(). The child process, having not initiated any merging, retains the inherited counter value without any update.So from a testing point of view, I think it is better to reset the counters as part of the cleanup code to ensure that the next tests do not get incorrect values.
Quoted 6 lines
Quoted 3 lines
Hmm... I agree from the test point of view based on current situation.
While maybe this is also a check point for later version.
Are you okay to proceed with the current patch in this series?
Sure.
Signature · 2 lines
Wei Yang Help you, Help me
PATCH v3 4/7 mm/selftests: Fix split_huge_page_test failure on systems with 64KB page size
In reply to Aboorva DevarajanFrom: Donet Tom <[email protected]>
The split_huge_page_test fails on systems with a 64KB base page size. This is because the order of a 2MB huge page is different:
On 64KB systems, the order is 5.
On 4KB systems, it's 9.
The test currently assumes a maximum huge page order of 9, which is only valid for 4KB base page systems. On systems with 64KB pages, attempting to split huge pages beyond their actual order (5) causes the test to fail.
In this patch, we calculate the huge page order based on the system's base page size. With this change, the tests now run successfully on both 64KB and 4KB page size systems.
--- .../selftests/mm/split_huge_page_test.c | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-)
tools/testing/selftests/mm/split_huge_page_test.c +15 -8
@@ -36,6 +36,7 @@ uint64_t pmd_pagesize;#define PFN_MASK ((1UL<<55)-1)#define KPF_THP (1UL<<22) +#define GET_ORDER (nr_pages) (31 - __builtin_clz(nr_pages))int is_backed_by_thp (char *vaddr,int pagemap_file,int kpageflags_file) {
@@ -522,6 +523,9 @@ int main(int argc, char **argv)const char *fs_loc;bool created_tmp;int offset; +unsigned int max_order; +unsigned int nr_pages; +unsigned int tests;ksft_print_header ();
@@ -533,35 +537,38 @@ int main(int argc, char **argv)if (argc >1 ) optional_xfs_path = argv[1 ]; -ksft_set_plan (1 +8 +1 +9 +9 +8 *4 +2 ); - pagesize =getpagesize (); pageshift =ffs (pagesize) -1 ; pmd_pagesize =read_pmd_pagesize ();if (!pmd_pagesize)ksft_exit_fail_msg ("Reading PMD pagesize failed\n" ); + nr_pages = pmd_pagesize / pagesize; + max_order =GET_ORDER (nr_pages); + tests =2 + (max_order -1 ) + (2 * max_order) + (max_order -1 ) *4 +2 ; +ksft_set_plan (tests); + fd_size =2 * pmd_pagesize;split_pmd_zero_pages (); -for (i =0 ; i <9 ; i++) +for (i =0 ; i < max_order; i++)if (i !=1 )split_pmd_thp_to_order (i);split_pte_mapped_thp (); -for (i =0 ; i <9 ; i++) +for (i =0 ; i < max_order; i++)split_file_backed_thp (i); created_tmp =prepare_thp_fs (optional_xfs_path, fs_loc_template, &fs_loc); -for (i =8 ; i >=0 ; i--) +for (i = max_order -1 ; i >=0 ; i--)split_thp_in_pagecache_to_order_at (fd_size, fs_loc, i,-1 ); -for (i =0 ; i <9 ; i++) +for (i =0 ; i < max_order; i++)for (offset =0 ; - offset < pmd_pagesize / pagesize; - offset +=MAX (pmd_pagesize / pagesize /4 ,1 << i)) + offset < nr_pages; + offset +=MAX (nr_pages /4 ,1 << i))split_thp_in_pagecache_to_order_at (fd_size, fs_loc, i, offset);cleanup_thp_fs (fs_loc, created_tmp);
Signature · 1 line
2.47.1
Re: [PATCH v3 4/7] mm/selftests: Fix split_huge_page_test failure on systems with 64KB page size
Quoted 53 lines · Aboorva Devarajan
From: Donet Tom <[email protected]>
The
split_huge_page_testfails on systems with a 64KB base page size. This is because the order of a 2MB huge page is different:On 64KB systems, the order is 5.
On 4KB systems, it's 9.
The test currently assumes a maximum huge page order of 9, which is only valid for 4KB base page systems. On systems with 64KB pages, attempting to split huge pages beyond their actual order (5) causes the test to fail.
In this patch, we calculate the huge page order based on the system's base page size. With this change, the tests now run successfully on both 64KB and 4KB page size systems.
Fixes fa6c02315f745 ("mm: huge_memory: a new debugfs interface for splitting THP tests")Reviewed-by Dev Jain <[email protected]>Reviewed-by Zi Yan <[email protected]>Co-developed-by Aboorva Devarajan <[email protected]>Signed-off-by Aboorva Devarajan <[email protected]>Signed-off-by Donet Tom <[email protected]>---
.../selftests/mm/split_huge_page_test.c| 23 ++++++++++++-------1 file changed, 15 insertions(+), 8 deletions(-)tools/testing/selftests/mm/split_huge_page_test.c +4
-0@@ -36,6 +36,7 @@ uint64_t pmd_pagesize;#define PFN_MASK ((1UL<<55)-1)#define KPF_THP (1UL<<22) +#define GET_ORDER (nr_pages) (31 - __builtin_clz(nr_pages))int is_backed_by_thp (char *vaddr,int pagemap_file,int kpageflags_file) {@@ -522,6 +523,9 @@ int main(int argc, char **argv)const char *fs_loc;bool created_tmp;int offset; +unsigned int max_order; +unsigned int nr_pages; +unsigned int tests;ksft_print_header ();@@ -533,35 +537,38 @@ int main(int argc, char **argv)if (argc >1 ) optional_xfs_path = argv[1 ];
tools/testing/selftests/mm/split_huge_page_test.c int main(int argc, char **argv) · line 547
Quoted 10 lines
tools/testing/selftests/mm/split_huge_page_test.c +2
-2-ksft_set_plan (1 +8 +1 +9 +9 +8 *4 +2 ); - pagesize =getpagesize (); pageshift =ffs (pagesize) -1 ; pmd_pagesize =read_pmd_pagesize ();if (!pmd_pagesize)ksft_exit_fail_msg ("Reading PMD pagesize failed\n" ); + nr_pages = pmd_pagesize / pagesize; + max_order =GET_ORDER (nr_pages);
There is a sz2ord() in cow.c and uffd-wp-mremap.c.
Maybe we can factor it into vm_util.h and use it here.
Signature · 2 lines
Wei Yang Help you, Help me
Re: [PATCH v3 4/7] mm/selftests: Fix split_huge_page_test failure on systems with 64KB page size
Quoted 62 lines · Wei Yang
Quoted 61 lines · Aboorva Devarajan
From: Donet Tom <[email protected]>
The
split_huge_page_testfails on systems with a 64KB base page size. This is because the order of a 2MB huge page is different:On 64KB systems, the order is 5.
On 4KB systems, it's 9.
The test currently assumes a maximum huge page order of 9, which is only valid for 4KB base page systems. On systems with 64KB pages, attempting to split huge pages beyond their actual order (5) causes the test to fail.
In this patch, we calculate the huge page order based on the system's base page size. With this change, the tests now run successfully on both 64KB and 4KB page size systems.
Fixes fa6c02315f745 ("mm: huge_memory: a new debugfs interface for splitting THP tests")Reviewed-by Dev Jain <[email protected]>Reviewed-by Zi Yan <[email protected]>Co-developed-by Aboorva Devarajan <[email protected]>Signed-off-by Aboorva Devarajan <[email protected]>Signed-off-by Donet Tom <[email protected]>---
.../selftests/mm/split_huge_page_test.c| 23 ++++++++++++-------1 file changed, 15 insertions(+), 8 deletions(-)tools/testing/selftests/mm/split_huge_page_test.c +4
-2@@ -36,6 +36,7 @@ uint64_t pmd_pagesize;#define PFN_MASK ((1UL<<55)-1)#define KPF_THP (1UL<<22) +#define GET_ORDER (nr_pages) (31 - __builtin_clz(nr_pages))int is_backed_by_thp (char *vaddr,int pagemap_file,int kpageflags_file) {@@ -522,6 +523,9 @@ int main(int argc, char **argv)const char *fs_loc;bool created_tmp;int offset; +unsigned int max_order; +unsigned int nr_pages; +unsigned int tests;ksft_print_header ();@@ -533,35 +537,38 @@ int main(int argc, char **argv)if (argc >1 ) optional_xfs_path = argv[1 ]; -ksft_set_plan (1 +8 +1 +9 +9 +8 *4 +2 ); - pagesize =getpagesize (); pageshift =ffs (pagesize) -1 ; pmd_pagesize =read_pmd_pagesize ();if (!pmd_pagesize)ksft_exit_fail_msg ("Reading PMD pagesize failed\n" );
Quoted 6 lines
tools/testing/selftests/mm/split_huge_page_test.c int main(int argc, char **argv) · line 547
Quoted 2 lines
tools/testing/selftests/mm/split_huge_page_test.c +2
-0+ nr_pages = pmd_pagesize / pagesize; + max_order =GET_ORDER (nr_pages);There is a
sz2ord()incow.canduffd-wp-mremap.c.Maybe we can factor it into
vm_util.hand use it here.
That sounds reasonable to me.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v3 4/7] mm/selftests: Fix split_huge_page_test failure on systems with 64KB page size
In reply to Wei YangQuoted 62 lines · Wei Yang
Quoted 61 lines · Aboorva Devarajan
From: Donet Tom <[email protected]>
The
split_huge_page_testfails on systems with a 64KB base page size. This is because the order of a 2MB huge page is different:On 64KB systems, the order is 5.
On 4KB systems, it's 9.
The test currently assumes a maximum huge page order of 9, which is only valid for 4KB base page systems. On systems with 64KB pages, attempting to split huge pages beyond their actual order (5) causes the test to fail.
In this patch, we calculate the huge page order based on the system's base page size. With this change, the tests now run successfully on both 64KB and 4KB page size systems.
Fixes fa6c02315f745 ("mm: huge_memory: a new debugfs interface for splitting THP tests")Reviewed-by Dev Jain <[email protected]>Reviewed-by Zi Yan <[email protected]>Co-developed-by Aboorva Devarajan <[email protected]>Signed-off-by Aboorva Devarajan <[email protected]>Signed-off-by Donet Tom <[email protected]>---
.../selftests/mm/split_huge_page_test.c| 23 ++++++++++++-------1 file changed, 15 insertions(+), 8 deletions(-)tools/testing/selftests/mm/split_huge_page_test.c +4
-2@@ -36,6 +36,7 @@ uint64_t pmd_pagesize;#define PFN_MASK ((1UL<<55)-1)#define KPF_THP (1UL<<22) +#define GET_ORDER (nr_pages) (31 - __builtin_clz(nr_pages))int is_backed_by_thp (char *vaddr,int pagemap_file,int kpageflags_file) {@@ -522,6 +523,9 @@ int main(int argc, char **argv)const char *fs_loc;bool created_tmp;int offset; +unsigned int max_order; +unsigned int nr_pages; +unsigned int tests;ksft_print_header ();@@ -533,35 +537,38 @@ int main(int argc, char **argv)if (argc >1 ) optional_xfs_path = argv[1 ]; -ksft_set_plan (1 +8 +1 +9 +9 +8 *4 +2 ); - pagesize =getpagesize (); pageshift =ffs (pagesize) -1 ; pmd_pagesize =read_pmd_pagesize ();if (!pmd_pagesize)ksft_exit_fail_msg ("Reading PMD pagesize failed\n" );
Quoted 5 lines
tools/testing/selftests/mm/split_huge_page_test.c int main(int argc, char **argv) · line 547
Quoted 2 lines
tools/testing/selftests/mm/split_huge_page_test.c +2
-0+ nr_pages = pmd_pagesize / pagesize; + max_order =GET_ORDER (nr_pages);There is a
sz2ord()incow.canduffd-wp-mremap.c.Maybe we can factor it into
vm_util.hand use it here.
Sure, I will make the change and send a new version.
Quoted 1 line
PATCH v3 5/7 selftests/mm: Fix child process exit codes in ksm_functional_tests
In reply to Aboorva DevarajanIn ksm_functional_tests, test_child_ksm() returned negative values to indicate errors. However, when passed to exit(), these were interpreted as large unsigned values (e.g, -2 became 254), leading to incorrect handling in the parent process. As a result, some tests appeared to be skipped or silently failed.
This patch changes test_child_ksm() to return positive error codes (1, 2, 3) and updates test_child_ksm_err() to interpret them correctly. Additionally, test_prctl_fork_exec() now uses exit(4) after a failed execv() to clearly signal exec failures. This ensures the parent accurately detects and reports child process failures.
--------------
Before patch:
--------------
- [RUN] test_unmerge
ok 1 Pages were unmerged
...
- [RUN] test_prctl_fork
- No pages got merged
- [RUN] test_prctl_fork_exec
ok 7 PR_SET_MEMORY_MERGE value is inherited
...
Bail out! 1 out of 8 tests failed
- Planned tests != run tests (9 != 8)
- Totals: pass:7 fail:1 xfail:0 xpass:0 skip:0 error:0
--------------
After patch:
--------------
- [RUN] test_unmerge
ok 1 Pages were unmerged
...
- [RUN] test_prctl_fork
- No pages got merged
not ok 7 Merge in child failed
- [RUN] test_prctl_fork_exec
ok 8 PR_SET_MEMORY_MERGE value is inherited
...
Bail out! 2 out of 9 tests failed
- Totals: pass:7 fail:2 xfail:0 xpass:0 skip:0 error:0
--- .../testing/selftests/mm/ksm_functional_tests.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
tools/testing/selftests/mm/ksm_functional_tests.c +9 -7
@@ -512,14 +512,14 @@ static int test_child_ksm(void)/* Test if KSM is enabled for the process. */ if (prctl (PR_GET_MEMORY_MERGE ,0 ,0 ,0 ,0 ) !=1 ) -return -1 ; +return 1 ;/* Test if merge could really happen. */ map =__mmap_and_merge_range (0xcf , size,PROT_READ |PROT_WRITE ,KSM_MERGE_NONE );if (map ==MAP_MERGE_FAIL ) -return -2 ; +return 2 ;else if (map ==MAP_MERGE_SKIP ) -return -3 ; +return 3 ;ksm_unmerge ();munmap (map, size);
@@ -528,12 +528,14 @@ static int test_child_ksm(void)static void test_child_ksm_err (int status) { -if (status ==-1 ) +if (status ==1 )ksft_test_result_fail ("unexpected PR_GET_MEMORY_MERGE result in child\n" ); -else if (status ==-2 ) +else if (status ==2 )ksft_test_result_fail ("Merge in child failed\n" ); -else if (status ==-3 ) +else if (status ==3 )ksft_test_result_skip ("Merge in child skipped\n" ); +else if (status ==4 ) +ksft_test_result_fail ("Binary not found\n" ); }/* Verify that prctl ksm flag is inherited. */
@@ -606,7 +608,7 @@ static void test_prctl_fork_exec(void)char *argv_for_program[] = { prg_name,FORK_EXEC_CHILD_PRG_NAME ,NULL };execv (prg_name, argv_for_program); -return ; +exit (4 ); }if (waitpid (child_pid, &status,0 ) >0 ) {
Signature · 1 line
2.47.1
PATCH v3 6/7 selftests/mm: Skip thuge-gen test if system is not setup properly
In reply to Aboorva DevarajanMake thuge-gen skip instead of fail when it can't run due to system settings. If shmmax is too small or no 1G huge pages are available, the test now prints a warning and is marked as skipped.
-------------------
Before Patch:
-------------------
~ running ./thuge-gen
~ Bail out! Please do echo 262144 > /proc/sys/kernel/shmmax ~ Totals: pass:0 fail:0 xfail:0 xpass:0 skip:0 error:0 ~ [FAIL]
not ok 28 thuge-gen ~ exit=1
-------------------
After Patch:
-------------------
~ running ./thuge-gen
~ ~ WARNING: shmmax is too small to run this test. ~ ~ Please run the following command to increase shmmax:
~ ~ echo 262144 > /proc/sys/kernel/shmmax
~ 1..0 ~ SKIP Test skipped due to insufficient shmmax value. ~ [SKIP]
ok 29 thuge-gen ~ SKIP
--- tools/testing/selftests/mm/thuge-gen.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
tools/testing/selftests/mm/thuge-gen.c +7 -4
@@ -177,13 +177,16 @@ void find_pagesizes(void)globfree (&g);read_sysfs ("/proc/sys/kernel/shmmax" , &shmmax_val); -if (shmmax_val <NUM_PAGES * largest) -ksft_exit_fail_msg ("Please do echo %lu > /proc/sys/kernel/shmmax" , - largest *NUM_PAGES ); +if (shmmax_val <NUM_PAGES * largest) { +ksft_print_msg ("WARNING: shmmax is too small to run this test.\n" ); +ksft_print_msg ("Please run the following command to increase shmmax:\n" ); +ksft_print_msg ("echo %lu > /proc/sys/kernel/shmmax\n" , largest *NUM_PAGES ); +ksft_exit_skip ("Test skipped due to insufficient shmmax value.\n" ); + }#if defined(__x86_64__)if (largest !=1U <<30 ) { -ksft_exit_fail_msg ("No GB pages available on x86-64\n" +ksft_exit_skip ("No GB pages available on x86-64\n" "Please boot with hugepagesz=1G hugepages=%d\n" ,NUM_PAGES ); }#endif
Signature · 1 line
2.47.1
PATCH v3 7/7 selftests/mm: Skip hugepage-mremap test if userfaultfd unavailable
In reply to Aboorva DevarajanGracefully skip test if userfaultfd is not supported (ENOSYS) or not permitted (EPERM), instead of failing. This avoids misleading failures with clear skip messages.
--------------
Before Patch
--------------
~ running ./hugepage-mremap
...
~ Bail out! userfaultfd: Function not implemented
~ Planned tests != run tests (1 != 0)
~ Totals: pass:0 fail:0 xfail:0 xpass:0 skip:0 error:0
~ [FAIL]
not ok 4 hugepage-mremap # exit=1
--------------
After Patch
--------------
~ running ./hugepage-mremap
...
~ ok 2 # SKIP userfaultfd is not supported/not enabled. ~ 1 skipped test(s) detected.
~ Totals: pass:0 fail:0 xfail:0 xpass:0 skip:1 error:0 ~ [SKIP]
ok 4 hugepage-mremap # SKIP
--- tools/testing/selftests/mm/hugepage-mremap.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/hugepage-mremap.c +13 -3
@@ -65,10 +65,20 @@ static void register_region_with_uffd(char *addr, size_t len)struct uffdio_api uffdio_api;/* Create and enable userfaultfd object. */ - uffd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK ); -if (uffd ==-1 ) -ksft_exit_fail_msg ("userfaultfd: %s\n" ,strerror (errno)); +if (uffd ==-1 ) { +switch (errno) { +case EPERM : +ksft_exit_skip ("Insufficient permissions, try running as root.\n" ); +break ; +case ENOSYS : +ksft_exit_skip ("userfaultfd is not supported/not enabled.\n" ); +break ; +default : +ksft_exit_fail_msg ("userfaultfd failed with %s\n" ,strerror (errno)); +break ; + } + } uffdio_api.api =UFFD_API ; uffdio_api.features =0 ;
Signature · 1 line
2.47.1