ksm_tests fills regions with identical data and checks KSM's counters
after merging them. allocate_memory() maps these regions without any
THP hint.

With mTHP enabled the regions can be backed by large folios, which KSM
splits before merging. Splitting an anonymous folio maps its
zero-filled pages to the shared zeropage, so with use_zero_pages=0
those pages never become KSM pages and "KSM zero page merging" fails
its count. "ksm_tests -Z -p 10 -z 0", 20 runs:

  THP setup                     before    after
  default                         0/20     0/20
  every mTHP size "always"       20/20     0/20

Map the regions with MADV_NOHUGEPAGE, as ksm_functional_tests does.
ksm_merge_hugepages_time() maps its own THP-backed region and is not
affected.

Fixes: b1f202060afe ("mm: remap unused subpages to shared zeropage when 
splitting isolated thp")
Signed-off-by: Qiliang Yuan <[email protected]>
---
 tools/testing/selftests/mm/ksm_tests.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/tools/testing/selftests/mm/ksm_tests.c 
b/tools/testing/selftests/mm/ksm_tests.c
index 5fd7792a0d479..140ea833353b6 100644
--- a/tools/testing/selftests/mm/ksm_tests.c
+++ b/tools/testing/selftests/mm/ksm_tests.c
@@ -178,6 +178,16 @@ static void  *allocate_memory(void *ptr, int prot, int 
mapping, char data, size_
                ksft_perror("mmap");
                return NULL;
        }
+       /*
+        * Splitting a large folio before merging maps its zero-filled pages
+        * to the shared zeropage, so keep the pages small. Ignore EINVAL on
+        * kernels without THP.
+        */
+       if (madvise(map_ptr, map_size, MADV_NOHUGEPAGE) && errno != EINVAL) {
+               ksft_perror("madvise");
+               munmap(map_ptr, map_size);
+               return NULL;
+       }
        memset(map_ptr, data, map_size);
        if (mprotect(map_ptr, map_size, prot)) {
                ksft_perror("mprotect");

---
base-commit: 90201351a862182bdabfc1ad1c97b1e1b6ed5931
change-id: 20261016-bug-selftests-mm-ksm-tests-nohugepage-1414868db0ef

Best regards,
-- 
Qiliang Yuan <[email protected]>


Reply via email to