mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] selftests/mm: keep ksm_tests on small pages
@ 2026-10-18  4:53 Qiliang Yuan
  2026-10-19  6:25 ` Sarthak Sharma
  2026-10-19 13:37 ` David Hildenbrand (Arm)
  0 siblings, 2 replies; 4+ messages in thread
From: Qiliang Yuan @ 2026-10-18  4:53 UTC (permalink / raw)
  To: Andrew Morton, David Hildenbrand, Lorenzo Stoakes,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Shuah Khan, Usama Arif,
	Yu Zhao, Xu Xin, Chengming Zhou
  Cc: linux-mm, linux-kselftest, linux-kernel, Qiliang Yuan

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 <odys.yuan@gmail.com>
---
 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 <odys.yuan@gmail.com>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] selftests/mm: keep ksm_tests on small pages
  2026-10-18  4:53 [PATCH] selftests/mm: keep ksm_tests on small pages Qiliang Yuan
@ 2026-10-19  6:25 ` Sarthak Sharma
  2026-10-19 13:37 ` David Hildenbrand (Arm)
  1 sibling, 0 replies; 4+ messages in thread
From: Sarthak Sharma @ 2026-10-19  6:25 UTC (permalink / raw)
  To: Qiliang Yuan, Andrew Morton, David Hildenbrand, Lorenzo Stoakes,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Shuah Khan, Usama Arif,
	Yu Zhao, Xu Xin, Chengming Zhou
  Cc: linux-mm, linux-kselftest, linux-kernel



On 10/18/26 10:23 AM, Qiliang Yuan wrote:
> 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 <odys.yuan@gmail.com>
> ---
Tested the patch by enabling all mTHP sizes as mentioned. Before the
patch, the test failed and applying the patch fixed it. So:

Tested-by: Sarthak Sharma <sarthak.sharma@arm.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] selftests/mm: keep ksm_tests on small pages
  2026-10-18  4:53 [PATCH] selftests/mm: keep ksm_tests on small pages Qiliang Yuan
  2026-10-19  6:25 ` Sarthak Sharma
@ 2026-10-19 13:37 ` David Hildenbrand (Arm)
  2026-10-19 14:24   ` Nico Pache
  1 sibling, 1 reply; 4+ messages in thread
From: David Hildenbrand (Arm) @ 2026-10-19 13:37 UTC (permalink / raw)
  To: Qiliang Yuan, Andrew Morton, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Shuah Khan, Usama Arif, Yu Zhao, Xu Xin, Chengming Zhou,
	Nico Pache
  Cc: linux-mm, linux-kselftest, linux-kernel

On 10/18/26 06:53, Qiliang Yuan wrote:
> 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.


That's actually something Nico intends to fix:

https://lore.kernel.org/all/20260609114619.144416-1-npache@redhat.com/

> 
> Fixes: b1f202060afe ("mm: remap unused subpages to shared zeropage when splitting isolated thp")
> Signed-off-by: Qiliang Yuan <odys.yuan@gmail.com>
> ---
>  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,


-- 
Cheers,

David

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] selftests/mm: keep ksm_tests on small pages
  2026-10-19 13:37 ` David Hildenbrand (Arm)
@ 2026-10-19 14:24   ` Nico Pache
  0 siblings, 0 replies; 4+ messages in thread
From: Nico Pache @ 2026-10-19 14:24 UTC (permalink / raw)
  To: David Hildenbrand (Arm)
  Cc: Qiliang Yuan, Andrew Morton, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Shuah Khan, Usama Arif, Yu Zhao, Xu Xin, Chengming Zhou,
	linux-mm, linux-kselftest, linux-kernel

On Mon, Oct 19, 2026 at 7:38 AM David Hildenbrand (Arm)
<david@kernel.org> wrote:
>
> On 10/18/26 06:53, Qiliang Yuan wrote:
> > 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.
>
>
> That's actually something Nico intends to fix:
>
> https://lore.kernel.org/all/20260609114619.144416-1-npache@redhat.com/

I will try to get the fix posted today!

>
> >
> > Fixes: b1f202060afe ("mm: remap unused subpages to shared zeropage when splitting isolated thp")
> > Signed-off-by: Qiliang Yuan <odys.yuan@gmail.com>
> > ---
> >  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,
>
>
> --
> Cheers,
>
> David
>


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-10-19 14:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-18  4:53 [PATCH] selftests/mm: keep ksm_tests on small pages Qiliang Yuan
2026-10-19  6:25 ` Sarthak Sharma
2026-10-19 13:37 ` David Hildenbrand (Arm)
2026-10-19 14:24   ` Nico Pache

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®