MM: Tighten control over zero-page remapping
cover MM: Tighten control over zero-page remapping
This patch series fixes a bug in KSM where the pages_to_scan limit is bypassed and the policy (use_zero_pages) is ignored. It also gives users further control of the zero-page remapping that occurs when a folio is split.
Commit b1f202060afe ("mm: remap unused subpages to shared zeropage when splitting isolated thp") added unconditional zero-page remapping when a folio is split. This was sold as part of the underutilized shrinker, but has a larger effect. Since this commit, all splits do this zero-page scanning and remapping, even if the user has the underutilized shrinker disabled.
This unconditional zero-page remapping was also problematic for KSM, as when KSM would try to merge a page, it would split the folio and the zero-page remapping would remap all zero-filled pages to the shared zero-page bypassing the use_zero_pages policy. When the user had this feature disabled, KSM would then waste cycles scanning these already freed pages, causing little progress to be made, and KSMs performance to degrade.
In this series we also gate the zero-page remapping behind split_underused_thp (the sysfs for the underutilized shrinker). This provides users with more control over this behavior, while also preventing the KSM bug when it is enabled.
RFC: https://lore.kernel.org/all/[email protected]/
Nico Pache (3):
mm/ksm: export ksm_is_running() to check KSM merge state mm/migrate.c: Prevent folio splitting from interacting with KSM mm/huge_memory.c: Skip zero-page remapping when underused THP shrinker
is disabled
include/linux/ksm.h | 6 ++++++ mm/huge_memory.c | 2 +- mm/ksm.c | 6 ++++++ mm/migrate.c | 9 +++++++++ 4 files changed, 22 insertions(+), 1 deletion(-)
base-commit: be18cf77e1e749c6469ff44df00eb026f7c0a365
Signature · 1 line
2.54.0
PATCH 1/3 mm/ksm: export ksm_is_running() to check KSM merge state
Add ksm_is_running() which returns true when KSM is actively merging (ksm_run & KSM_RUN_MERGE).
This will be used by try_to_map_unused_to_zeropage() to skip zero-page remapping in VM_MERGEABLE VMAs when KSM is active.
--- include/linux/ksm.h | 6 ++++++ mm/ksm.c | 6 ++++++ 2 files changed, 12 insertions(+)
include/linux/ksm.h +6 -0
@@ -17,6 +17,7 @@#ifdef CONFIG_KSM int ksm_madvise (struct vm_area_struct *vma,unsigned long start,unsigned long end,int advice,vm_flags_t *vm_flags); +bool ksm_is_running (void );vma_flags_t ksm_vma_flags (struct mm_struct *mm,const struct file *file,vma_flags_t vma_flags);int ksm_enable_merge_any (struct mm_struct *mm);
@@ -144,6 +145,11 @@ static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,return 0 ; } +static inline bool ksm_is_running (void ) +{ +return false; +} +static inline struct folio *ksm_might_need_to_copy (struct folio *folio,struct vm_area_struct *vma,unsigned long addr) {
mm/ksm.c +6 -0
@@ -3015,6 +3015,12 @@ int ksm_madvise(struct vm_area_struct *vma, unsigned long start, }EXPORT_SYMBOL_GPL (ksm_madvise); +bool ksm_is_running (void ) +{ +return ksm_run &KSM_RUN_MERGE ; +} +EXPORT_SYMBOL_GPL (ksm_is_running); +int __ksm_enter (struct mm_struct *mm) {struct ksm_mm_slot *mm_slot;
Signature · 1 line
2.54.0
Re: [PATCH mm-unstable v1 1/3] mm/ksm: export ksm_is_running() to check KSM merge state
Quoted 41 lines · Nico Pache
Add
ksm_is_running()which returns true when KSM is actively merging (ksm_run&KSM_RUN_MERGE).This will be used by
try_to_map_unused_to_zeropage()to skip zero-page remapping inVM_MERGEABLEVMAs when KSM is active.Signed-off-by Nico Pache <[email protected]>--- include/linux/ksm.h | 6 ++++++ mm/ksm.c | 6 ++++++ 2 files changed, 12 insertions(+)include/linux/ksm.h +6
-0@@ -17,6 +17,7 @@#ifdef CONFIG_KSM int ksm_madvise (struct vm_area_struct *vma,unsigned long start,unsigned long end,int advice,vm_flags_t *vm_flags); +bool ksm_is_running (void );vma_flags_t ksm_vma_flags (struct mm_struct *mm,const struct file *file,vma_flags_t vma_flags);int ksm_enable_merge_any (struct mm_struct *mm);@@ -144,6 +145,11 @@ static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,return 0 ; } +static inline bool ksm_is_running (void ) +{ +return false; +} +static inline struct folio *ksm_might_need_to_copy (struct folio *folio,struct vm_area_struct *vma,unsigned long addr) {mm/ksm.c +0
-0
mm/ksm.c int ksm_madvise(struct vm_area_struct *vma, unsigned long start, · line 3022
Quoted 8 lines
mm/ksm.c +5
-0@@ -3015,6 +3015,12 @@ int ksm_madvise(struct vm_area_struct *vma, unsigned long start, }EXPORT_SYMBOL_GPL (ksm_madvise); +bool ksm_is_running (void ) +{ +return ksm_run &KSM_RUN_MERGE ; +} +EXPORT_SYMBOL_GPL (ksm_is_running);
Why are we exporting this? What modules need this? We should never export unless we have a very specific reason to.
Quoted 7 lines
mm/ksm.c +1
-1+int __ksm_enter (struct mm_struct *mm) {struct ksm_mm_slot *mm_slot; --2.54.0
PATCH 2/3 mm/migrate.c: Prevent folio splitting from interacting with KSM
In reply to Nico PacheSince commit b1f202060afe ("mm: remap unused subpages to shared zeropage when splitting isolated thp"), splitting an anonymous THP remaps all zero-filled subpages to the shared zeropage via TTU_USE_SHARED_ZEROPAGE. This flag is set unconditionally for every anonymous folio split, including splits triggered by KSM.
When KSM is enabled with THP=always, this causes two regressions:
1. use_zero_pages=1: KSM calls try_to_merge_one_page() which triggers split_huge_page(). The split remaps all 512 zero-filled subpages to the shared zeropage at once, freeing the entire 2MB THP when KSM only intended to process a single 4KB page. This bypasses KSM's pages_to_scan rate limiting, causing ~1GB to be freed almost instantly.
2. use_zero_pages=0: The same split side-effect occurs through the stable/unstable tree merge paths. Each pages_to_scan iteration triggers an expensive split_huge_page() that silently frees 2MB, while the scanner wastes cycles on tree searches for zero-filled pages that were already freed as a side-effect.
Fix this by restricting TTU_USE_SHARED_ZEROPAGE being set in the case that KSM is running and the VMA has VM_MERGEABLE.
--- mm/migrate.c | 9 +++++++++ 1 file changed, 9 insertions(+)
mm/migrate.c +9 -0
@@ -304,6 +304,15 @@ static bool try_to_map_unused_to_zeropage(struct page_vma_mapped_walk *pvmw,if (PageCompound (page) ||PageHWPoison (page))return false; +/* +* Let KSM handle the zero-filled page deduplication according to its +* own rate limit (pages_to_scan) and policy (use_zero_pages). Without +* this, a KSM-triggered THP split would remap all zero-filled subpages +* to the shared zeropage as a side effect. +*/ +if (ksm_is_running () && (pvmw->vma->vm_flags &VM_MERGEABLE )) +return false; +VM_BUG_ON_PAGE (!PageAnon (page), page);VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (pte_present (old_pte), page);
Signature · 1 line
2.54.0
Re: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 6 lines
Since commit
b1f202060afe("mm: remap unused subpages to shared zeropage when splitting isolated thp"), splitting an anonymous THP remaps all zero-filled subpages to the shared zeropage viaTTU_USE_SHARED_ZEROPAGE. This flag is set unconditionally for every anonymous folio split, including splits triggered by KSM.
Quoted 9 lines
When KSM is enabled with THP=always, this causes two regressions:
1. use_zero_pages=1: KSM calls
try_to_merge_one_page()which triggerssplit_huge_page(). The split remaps all 512 zero-filled subpages to the shared zeropage at once, freeing the entire 2MB THP when KSM only intended to process a single 4KB page. This bypasses KSM'spages_to_scanrate limiting, causing ~1GB to be freed almost instantly.
Why do you see it as regressions?
AFAIU, KSM and THP do often conflict with each other. THP tries hard to collapse a huge page (which may contain many zero pages). If KSM is enabled and part of that huge page is mergeable, it can easily be split by KSM, rendering THP's efforts futile.
Therefore, in our actual production environment, we typically avoid making the same region both KSM mergeable and THP always.
Quoted 8 lines
2. use_zero_pages=0: The same split side-effect occurs through the stable/unstable tree merge paths. Each
pages_to_scaniteration triggers an expensivesplit_huge_page()that silently frees 2MB, while the scanner wastes cycles on tree searches for zero-filled pages that were already freed as a side-effect.Fix this by restricting
TTU_USE_SHARED_ZEROPAGEbeing set in the case that KSM is running and the VMA hasVM_MERGEABLE.
Re: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 17 lines · On Tue, Jun 9, 2026 at 6:12 AM <[email protected]> wrote:
Quoted 7 lines
Since commit
b1f202060afe("mm: remap unused subpages to shared zeropage when splitting isolated thp"), splitting an anonymous THP remaps all zero-filled subpages to the shared zeropage viaTTU_USE_SHARED_ZEROPAGE. This flag is set unconditionally for every anonymous folio split, including splits triggered by KSM.Quoted 9 lines
When KSM is enabled with THP=always, this causes two regressions:
1. use_zero_pages=1: KSM calls
try_to_merge_one_page()which triggerssplit_huge_page(). The split remaps all 512 zero-filled subpages to the shared zeropage at once, freeing the entire 2MB THP when KSM only intended to process a single 4KB page. This bypasses KSM'spages_to_scanrate limiting, causing ~1GB to be freed almost instantly.
Quoted 1 line
Why do you see it as regressions?
Since the zero-page remapping was introduced our test has shown the following behavior changes:
With use_zero_pages=0, the merge rate drops from 60MB/s to ~6 MB/s even after raising pages_to_scan. The KSM merging is now much slower and CPU utilization has increased.
With use_zero_pages=1, ~1 GB is freed almost instantly, and it no longer respects the pages_to_scan behavior.
Even with just this patch (1 & 2) or the RFC linked in the cover letter, the issue no longer occurs.
Quoted 8 lines
AFAIU, KSM and THP do often conflict with each other. THP tries hard to collapse a huge page (which may contain many zero pages). If KSM is enabled and part of that huge page is mergeable, it can easily be split by KSM, rendering THP's efforts futile.
Therefore, in our actual production environment, we typically avoid making the same region both KSM mergeable and THP always.
THP=always is a global setting used in many production environments, so these features now interact very poorly together.
Let me know if that answers your question!
Cheers,
-- Nico
Quoted 11 lines
Quoted 10 lines
2. use_zero_pages=0: The same split side-effect occurs through the stable/unstable tree merge paths. Each
pages_to_scaniteration triggers an expensivesplit_huge_page()that silently frees 2MB, while the scanner wastes cycles on tree searches for zero-filled pages that were already freed as a side-effect.Fix this by restricting
TTU_USE_SHARED_ZEROPAGEbeing set in the case that KSM is running and the VMA hasVM_MERGEABLE.
David Hildenbrand (Arm)
maintainerRe: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 39 lines · Nico Pache
Quoted 17 lines · On Tue, Jun 9, 2026 at 6:12 AM <[email protected]> wrote:
Quoted 7 lines
Since commit
b1f202060afe("mm: remap unused subpages to shared zeropage when splitting isolated thp"), splitting an anonymous THP remaps all zero-filled subpages to the shared zeropage viaTTU_USE_SHARED_ZEROPAGE. This flag is set unconditionally for every anonymous folio split, including splits triggered by KSM.Quoted 9 lines
When KSM is enabled with THP=always, this causes two regressions:
1. use_zero_pages=1: KSM calls
try_to_merge_one_page()which triggerssplit_huge_page(). The split remaps all 512 zero-filled subpages to the shared zeropage at once, freeing the entire 2MB THP when KSM only intended to process a single 4KB page. This bypasses KSM'spages_to_scanrate limiting, causing ~1GB to be freed almost instantly.Quoted 1 line
Why do you see it as regressions?
Since the zero-page remapping was introduced our test has shown the following behavior changes:
With use_zero_pages=0, the merge rate drops from 60MB/s to ~6 MB/s even after raising
pages_to_scan. The KSM merging is now much slower and CPU utilization has increased.With use_zero_pages=1, ~1 GB is freed almost instantly, and it no longer respects the
pages_to_scanbehavior.Even with just this patch (1 & 2) or the RFC linked in the cover letter, the issue no longer occurs.
Quoted 6 lines
AFAIU, KSM and THP do often conflict with each other. THP tries hard to collapse a huge page (which may contain many zero pages). If KSM is enabled and part of that huge page is mergeable, it can easily be split by KSM, rendering THP's efforts futile.
Quoted 5 lines
Quoted 2 lines
Therefore, in our actual production environment, we typically avoid making the same region both KSM mergeable and THP always.
THP=always is a global setting used in many production environments, so these features now interact very poorly together.
Red Hat documents, though, that both things in combination are shaky:
"As KSM can reduce the occurrence of transparent huge pages, you may want to disable it before enabling THP." [1]
(for RHEL 6, but nothing should have changed in that regard)
But yeah, having KSM and THP enabled at the same time is not uncommon.
Signature · 3 lines
Cheers, David
Re: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
In reply to Nico PacheQuoted 25 lines
Quoted 16 lines
Quoted 6 lines
Since commit
b1f202060afe("mm: remap unused subpages to shared zeropage when splitting isolated thp"), splitting an anonymous THP remaps all zero-filled subpages to the shared zeropage viaTTU_USE_SHARED_ZEROPAGE. This flag is set unconditionally for every anonymous folio split, including splits triggered by KSM.Quoted 9 lines
When KSM is enabled with THP=always, this causes two regressions:
1. use_zero_pages=1: KSM calls
try_to_merge_one_page()which triggerssplit_huge_page(). The split remaps all 512 zero-filled subpages to the shared zeropage at once, freeing the entire 2MB THP when KSM only intended to process a single 4KB page. This bypasses KSM'spages_to_scanrate limiting, causing ~1GB to be freed almost instantly.Quoted 1 line
Why do you see it as regressions?
Since the zero-page remapping was introduced our test has shown the following behavior changes:
With use_zero_pages=0, the merge rate drops from 60MB/s to ~6 MB/s even after raising
pages_to_scan. The KSM merging is now much slower and CPU utilization has increased.
Quoted 5 lines
With use_zero_pages=1, ~1 GB is freed almost instantly, and it no longer respects the
pages_to_scanbehavior.Even with just this patch (1 & 2) or the RFC linked in the cover letter, the issue no longer occurs.
Understood. You're saying that the additional processing action in split_huge_page (remap unused subpages to shared zeropage) increases the scanning cost of ksmd.
However, I still wouldn't simply classify this as a performance regression, because commit b1f202060afe increases memory savings through this action — so it saves memory at the cost of additional CPU overhead.
If you want to address the increased overhead on ksmd, I think we could add a check for the shared zeropage in cmp_and_merge_page, and skip merging when a shared zeropage is detected.
Quoted 6 lines
Quoted 6 lines
AFAIU, KSM and THP do often conflict with each other. THP tries hard to collapse a huge page (which may contain many zero pages). If KSM is enabled and part of that huge page is mergeable, it can easily be split by KSM, rendering THP's efforts futile.
Quoted 6 lines
Quoted 2 lines
Therefore, in our actual production environment, we typically avoid making the same region both KSM mergeable and THP always.
THP=always is a global setting used in many production environments, so these features now interact very poorly together.
Actually, I have long thought about submitting a patch: add a new interface 'skip_huge_page' under KSM's sysfs, allowing users to choose not to split huge pages.
Zi Yan
reviewerRe: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 51 lines · On 9 Jun 2026, at 9:47, [email protected] wrote:
Quoted 25 lines
Quoted 16 lines
Quoted 6 lines
Since commit
b1f202060afe("mm: remap unused subpages to shared zeropage when splitting isolated thp"), splitting an anonymous THP remaps all zero-filled subpages to the shared zeropage viaTTU_USE_SHARED_ZEROPAGE. This flag is set unconditionally for every anonymous folio split, including splits triggered by KSM.Quoted 9 lines
When KSM is enabled with THP=always, this causes two regressions:
1. use_zero_pages=1: KSM calls
try_to_merge_one_page()which triggerssplit_huge_page(). The split remaps all 512 zero-filled subpages to the shared zeropage at once, freeing the entire 2MB THP when KSM only intended to process a single 4KB page. This bypasses KSM'spages_to_scanrate limiting, causing ~1GB to be freed almost instantly.Quoted 1 line
Why do you see it as regressions?
Since the zero-page remapping was introduced our test has shown the following behavior changes:
With use_zero_pages=0, the merge rate drops from 60MB/s to ~6 MB/s even after raising
pages_to_scan. The KSM merging is now much slower and CPU utilization has increased.Quoted 5 lines
With use_zero_pages=1, ~1 GB is freed almost instantly, and it no longer respects the
pages_to_scanbehavior.Even with just this patch (1 & 2) or the RFC linked in the cover letter, the issue no longer occurs.
Understood. You're saying that the additional processing action in
split_huge_page(remap unused subpages to shared zeropage) increases the scanning cost of ksmd.However, I still wouldn't simply classify this as a performance regression, because commit
b1f202060afeincreases memory savings through this action — so it saves memory at the cost of additional CPU overhead.If you want to address the increased overhead on ksmd, I think we could add a check for the shared zeropage in
cmp_and_merge_page, and skip merging when a shared zeropage is detected.Quoted 9 lines
Quoted 8 lines
AFAIU, KSM and THP do often conflict with each other. THP tries hard to collapse a huge page (which may contain many zero pages). If KSM is enabled and part of that huge page is mergeable, it can easily be split by KSM, rendering THP's efforts futile.
Therefore, in our actual production environment, we typically avoid making the same region both KSM mergeable and THP always.
Quoted 6 lines
Quoted 3 lines
THP=always is a global setting used in many production environments, so these features now interact very poorly together.
Actually, I have long thought about submitting a patch: add a new interface '
skip_huge_page' under KSM's sysfs, allowing users to choose not to split huge pages.
Just think out loud. Or just skip huge pages all the time unless memory pressure is present. Basically treat KSM as a way of reducing memory pressure by merging pages.
Best Regards,
Yan, Zi
Usama Arif
reviewerRe: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 57 lines · On Tue, 09 Jun 2026 10:07:20 -0400 Zi Yan <[email protected]> wrote:
Quoted 55 lines · On 9 Jun 2026, at 9:47, [email protected] wrote:
Quoted 25 lines
Quoted 16 lines
Quoted 6 lines
Since commit
b1f202060afe("mm: remap unused subpages to shared zeropage when splitting isolated thp"), splitting an anonymous THP remaps all zero-filled subpages to the shared zeropage viaTTU_USE_SHARED_ZEROPAGE. This flag is set unconditionally for every anonymous folio split, including splits triggered by KSM.Quoted 9 lines
When KSM is enabled with THP=always, this causes two regressions:
1. use_zero_pages=1: KSM calls
try_to_merge_one_page()which triggerssplit_huge_page(). The split remaps all 512 zero-filled subpages to the shared zeropage at once, freeing the entire 2MB THP when KSM only intended to process a single 4KB page. This bypasses KSM'spages_to_scanrate limiting, causing ~1GB to be freed almost instantly.Quoted 1 line
Why do you see it as regressions?
Since the zero-page remapping was introduced our test has shown the following behavior changes:
With use_zero_pages=0, the merge rate drops from 60MB/s to ~6 MB/s even after raising
pages_to_scan. The KSM merging is now much slower and CPU utilization has increased.Quoted 5 lines
With use_zero_pages=1, ~1 GB is freed almost instantly, and it no longer respects the
pages_to_scanbehavior.Even with just this patch (1 & 2) or the RFC linked in the cover letter, the issue no longer occurs.
Understood. You're saying that the additional processing action in
split_huge_page(remap unused subpages to shared zeropage) increases the scanning cost of ksmd.However, I still wouldn't simply classify this as a performance regression, because commit
b1f202060afeincreases memory savings through this action — so it saves memory at the cost of additional CPU overhead.If you want to address the increased overhead on ksmd, I think we could add a check for the shared zeropage in
cmp_and_merge_page, and skip merging when a shared zeropage is detected.Quoted 12 lines
Quoted 8 lines
AFAIU, KSM and THP do often conflict with each other. THP tries hard to collapse a huge page (which may contain many zero pages). If KSM is enabled and part of that huge page is mergeable, it can easily be split by KSM, rendering THP's efforts futile.
Therefore, in our actual production environment, we typically avoid making the same region both KSM mergeable and THP always.
THP=always is a global setting used in many production environments, so these features now interact very poorly together.
Quoted 6 lines
Quoted 2 lines
Actually, I have long thought about submitting a patch: add a new interface '
skip_huge_page' under KSM's sysfs, allowing users to choose not to split huge pages.Just think out loud. Or just skip huge pages all the time unless memory pressure is present. Basically treat KSM as a way of reducing memory pressure by merging pages.
I do agree with this. I questioned about if it even makes sense to split THP with KSM in RFC (https://lore.kernel.org/all/[email protected]/)
Quoted 4 lines
Best Regards,
Yan, Zi
Re: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 56 lines · Usama Arif
Quoted 53 lines · On Tue, 09 Jun 2026 10:07:20 -0400 Zi Yan <[email protected]> wrote:
Quoted 51 lines · On 9 Jun 2026, at 9:47, [email protected] wrote:
Quoted 25 lines
Quoted 16 lines
Quoted 6 lines
Since commit
b1f202060afe("mm: remap unused subpages to shared zeropage when splitting isolated thp"), splitting an anonymous THP remaps all zero-filled subpages to the shared zeropage viaTTU_USE_SHARED_ZEROPAGE. This flag is set unconditionally for every anonymous folio split, including splits triggered by KSM.Quoted 9 lines
When KSM is enabled with THP=always, this causes two regressions:
1. use_zero_pages=1: KSM calls
try_to_merge_one_page()which triggerssplit_huge_page(). The split remaps all 512 zero-filled subpages to the shared zeropage at once, freeing the entire 2MB THP when KSM only intended to process a single 4KB page. This bypasses KSM'spages_to_scanrate limiting, causing ~1GB to be freed almost instantly.Quoted 1 line
Why do you see it as regressions?
Since the zero-page remapping was introduced our test has shown the following behavior changes:
With use_zero_pages=0, the merge rate drops from 60MB/s to ~6 MB/s even after raising
pages_to_scan. The KSM merging is now much slower and CPU utilization has increased.Quoted 5 lines
With use_zero_pages=1, ~1 GB is freed almost instantly, and it no longer respects the
pages_to_scanbehavior.Even with just this patch (1 & 2) or the RFC linked in the cover letter, the issue no longer occurs.
Understood. You're saying that the additional processing action in
split_huge_page(remap unused subpages to shared zeropage) increases the scanning cost of ksmd.However, I still wouldn't simply classify this as a performance regression, because commit
b1f202060afeincreases memory savings through this action — so it saves memory at the cost of additional CPU overhead.If you want to address the increased overhead on ksmd, I think we could add a check for the shared zeropage in
cmp_and_merge_page, and skip merging when a shared zeropage is detected.Quoted 9 lines
Quoted 8 lines
AFAIU, KSM and THP do often conflict with each other. THP tries hard to collapse a huge page (which may contain many zero pages). If KSM is enabled and part of that huge page is mergeable, it can easily be split by KSM, rendering THP's efforts futile.
Therefore, in our actual production environment, we typically avoid making the same region both KSM mergeable and THP always.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
THP=always is a global setting used in many production environments, so these features now interact very poorly together.
Actually, I have long thought about submitting a patch: add a new interface '
skip_huge_page' under KSM's sysfs, allowing users to choose not to split huge pages.
This would be a good intermediate solution while allowing backward compatibility.
Quoted 4 lines
Quoted 4 lines
Just think out loud. Or just skip huge pages all the time unless memory pressure is present. Basically treat KSM as a way of reducing memory pressure by merging pages.
Hmm interesting. We should decide on future changes before committing to one direction; however, i think the immediate solution would be what I currently have prepped for v2.
My v2 follows my RFC alternative approach: skip zeropage remapping if the split comes from KSM. It also contains the patch (3) from this version, which skips zeropage remapping if the shrinker is disabled.
Quoted 3 lines
I do agree with this. I questioned about if it even makes sense to split THP with KSM in RFC (https://lore.kernel.org/all/[email protected]/)
This is indeed a good question but I don't know the right solution.
Perhaps we can discuss it here.
So far here are the potential solutions:
a) skip zeropage remapping if the split comes from KSM
b) skip zeropage remapping completely if the shrinker is disabled
c) Disable KSM THP splitting completely
d) add a skip_huge_page sysfs toggle
e) only scan THPs when memory pressure is present
(a)+(b) would be my V2 which I can send out whenever.
(c) is the nuclear option, but perhaps KSM for THP is rather inefficient and pointless
(d) could be used with (a) and (b) and allows more flexibility than (c)
(e) is a more dynamic approach, and could be combined with (b) and (d)
Let me know what you think :)
-- Nico
Quoted 7 lines
Quoted 6 lines
Best Regards,
Yan, Zi
Re: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 12 lines
My v2 follows my RFC alternative approach: skip zeropage remapping if the split comes from KSM. It also contains the patch (3) from this version, which skips zeropage remapping if the shrinker is disabled.
Quoted 3 lines
I do agree with this. I questioned about if it even makes sense to split THP with KSM in RFC (https://lore.kernel.org/all/[email protected]/)
This is indeed a good question but I don't know the right solution.
Perhaps we can discuss it here.
Quoted 8 lines
So far here are the potential solutions:
a) skip zeropage remapping if the split comes from KSM
b) skip zeropage remapping completely if the shrinker is disabled
c) Disable KSM THP splitting completely
d) add askip_huge_pagesysfs toggle
e) only scan THPs when memory pressure is present(a)+(b) would be my V2 which I can send out whenever.
Regarding the issue you currently raised (which breaks the semantics of KSM's pages_to_scan), I think (a) + (b) is acceptable, and I suggest you go ahead and send it out.
As for options (c), (d), and (e), they are somewhat off-topic, touching on broader discussions about KSM and THP.
Quoted 12 lines
(c) is the nuclear option, but perhaps KSM for THP is rather inefficient and pointless
(d) could be used with (a) and (b) and allows more flexibility than (c)
(e) is a more dynamic approach, and could be combined with (b) and (d)
Let me know what you think :)
-- NicoQuoted 1 line
Re: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 9 lines
c) Disable KSM THP splitting completely
d) add askip_huge_pagesysfs toggle
e) only scan THPs when memory pressure is present(a)+(b) would be my V2 which I can send out whenever.
(c) is the nuclear option, but perhaps KSM for THP is rather inefficient and pointless
Quoted 6 lines
(d) could be used with (a) and (b) and allows more flexibility than (c)
(e) is a more dynamic approach, and could be combined with (b) and (d)
Let me know what you think :)
-- Nico
Maybe (f): contains c+d+e: as a general and compromise approach to maintain backward compatibility, providing a skip_huge_page switch with three options — [always], [memory_sufficient], and [never] — could be a viable solution.
David Hildenbrand (Arm)
maintainerRe: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 11 lines · On 7/15/26 10:56, [email protected] wrote:
Quoted 11 lines
c) Disable KSM THP splitting completely
d) add askip_huge_pagesysfs toggle
e) only scan THPs when memory pressure is present(a)+(b) would be my V2 which I can send out whenever.
(c) is the nuclear option, but perhaps KSM for THP is rather inefficient and pointless
(d) could be used with (a) and (b) and allows more flexibility than (c)
Quoted 8 lines
Quoted 4 lines
(e) is a more dynamic approach, and could be combined with (b) and (d)
Let me know what you think :)
-- NicoMaybe (f): contains c+d+e: as a general and compromise approach to maintain backward compatibility, providing a
skip_huge_pageswitch with three options — [always], [memory_sufficient], and [never] — could be a viable solution.
Any new toggle for this is the wrong approach, so d) is not an option :)
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
In reply to Nico PacheQuoted 8 lines · Nico Pache
Quoted 4 lines · Usama Arif
Quoted 1 line · On Tue, 09 Jun 2026 10:07:20 -0400 Zi Yan <[email protected]> wrote:
This would be a good intermediate solution while allowing backward compatibility.
And an absolute horrible user-visible toggle :)
Quoted 31 lines
Quoted 5 lines
Quoted 5 lines
Just think out loud. Or just skip huge pages all the time unless memory pressure is present. Basically treat KSM as a way of reducing memory pressure by merging pages.
Hmm interesting. We should decide on future changes before committing to one direction; however, i think the immediate solution would be what I currently have prepped for v2.
My v2 follows my RFC alternative approach: skip zeropage remapping if the split comes from KSM. It also contains the patch (3) from this version, which skips zeropage remapping if the shrinker is disabled.
Quoted 3 lines
I do agree with this. I questioned about if it even makes sense to split THP with KSM in RFC (https://lore.kernel.org/all/[email protected]/)
This is indeed a good question but I don't know the right solution.
Perhaps we can discuss it here.
So far here are the potential solutions:
a) skip zeropage remapping if the split comes from KSM
b) skip zeropage remapping completely if the shrinker is disabled
c) Disable KSM THP splitting completely
d) add askip_huge_pagesysfs toggle
e) only scan THPs when memory pressure is present(a)+(b) would be my V2 which I can send out whenever.
Quoted 8 lines
(c) is the nuclear option, but perhaps KSM for THP is rather inefficient and pointless
(d) could be used with (a) and (b) and allows more flexibility than (c)
(e) is a more dynamic approach, and could be combined with (b) and (d)
Let me know what you think :)
Where does the current patch set fall into and what's the problem with it?
Signature · 3 lines
Cheers, David
Re: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 47 lines · David Hildenbrand (Arm)
Quoted 8 lines · Nico Pache
Quoted 4 lines · Usama Arif
Quoted 1 line · On Tue, 09 Jun 2026 10:07:20 -0400 Zi Yan <[email protected]> wrote:
This would be a good intermediate solution while allowing backward compatibility.
And an absolute horrible user-visible toggle :)
Quoted 34 lines
Quoted 5 lines
Quoted 5 lines
Just think out loud. Or just skip huge pages all the time unless memory pressure is present. Basically treat KSM as a way of reducing memory pressure by merging pages.
Hmm interesting. We should decide on future changes before committing to one direction; however, i think the immediate solution would be what I currently have prepped for v2.
My v2 follows my RFC alternative approach: skip zeropage remapping if the split comes from KSM. It also contains the patch (3) from this version, which skips zeropage remapping if the shrinker is disabled.
Quoted 3 lines
I do agree with this. I questioned about if it even makes sense to split THP with KSM in RFC (https://lore.kernel.org/all/[email protected]/)
This is indeed a good question but I don't know the right solution.
Perhaps we can discuss it here.
So far here are the potential solutions:
a) skip zeropage remapping if the split comes from KSM
b) skip zeropage remapping completely if the shrinker is disabled
c) Disable KSM THP splitting completely
d) add askip_huge_pagesysfs toggle
e) only scan THPs when memory pressure is present(a)+(b) would be my V2 which I can send out whenever.
(c) is the nuclear option, but perhaps KSM for THP is rather inefficient and pointless
Quoted 7 lines
Quoted 5 lines
(d) could be used with (a) and (b) and allows more flexibility than (c)
(e) is a more dynamic approach, and could be combined with (b) and (d)
Let me know what you think :)
Where does the current patch set fall into and what's the problem with it?
this series (v1) would be "dont scan VM_MERGEABLE entries when KSM is running"
It stops all VM_MEREABLE entries from being candidates, while the V2 I would like to propose goes back to my RFC's alternative solution a ) "skip zeropage remapping if the split comes from KSM"
I think (a) is better than V1 as it doesnt indiscriminately target all VM_MERGEABLE mapping but rather those that are triggering the issue (the split).
Cheers,
-- Nico
Quoted 6 lines
--
Cheers,David
David Hildenbrand (Arm)
maintainerRe: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 47 lines · Nico Pache
Quoted 35 lines · David Hildenbrand (Arm)
Quoted 3 lines · Nico Pache
This would be a good intermediate solution while allowing backward compatibility.
And an absolute horrible user-visible toggle :)
Quoted 27 lines
Hmm interesting. We should decide on future changes before committing to one direction; however, i think the immediate solution would be what I currently have prepped for v2.
My v2 follows my RFC alternative approach: skip zeropage remapping if the split comes from KSM. It also contains the patch (3) from this version, which skips zeropage remapping if the shrinker is disabled.
This is indeed a good question but I don't know the right solution.
Perhaps we can discuss it here.
So far here are the potential solutions:
a) skip zeropage remapping if the split comes from KSM
b) skip zeropage remapping completely if the shrinker is disabled
c) Disable KSM THP splitting completely
d) add askip_huge_pagesysfs toggle
e) only scan THPs when memory pressure is present(a)+(b) would be my V2 which I can send out whenever.
(c) is the nuclear option, but perhaps KSM for THP is rather inefficient and pointless
Quoted 7 lines
Quoted 5 lines
(d) could be used with (a) and (b) and allows more flexibility than (c)
(e) is a more dynamic approach, and could be combined with (b) and (d)
Let me know what you think :)
Where does the current patch set fall into and what's the problem with it?
this series (v1) would be "dont scan
VM_MERGEABLEentries when KSM is running"
Quoted 7 lines
It stops all
VM_MEREABLEentries from being candidates, while the V2 I would like to propose goes back to my RFC's alternative solution a ) "skip zeropage remapping if the split comes from KSM"I think (a) is better than V1 as it doesnt indiscriminately target all
VM_MERGEABLEmapping but rather those that are triggering the issue (the split).
Not sure. There could be anything else in the system triggering the split before KSM comes around.
So v1 just cleanly separates them, independent who triggers the split.
Why is this a problem?
Signature · 3 lines
Cheers, David
Re: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 53 lines · David Hildenbrand (Arm)
Quoted 51 lines · Nico Pache
Quoted 35 lines · David Hildenbrand (Arm)
Quoted 3 lines · Nico Pache
This would be a good intermediate solution while allowing backward compatibility.
And an absolute horrible user-visible toggle :)
Quoted 27 lines
Hmm interesting. We should decide on future changes before committing to one direction; however, i think the immediate solution would be what I currently have prepped for v2.
My v2 follows my RFC alternative approach: skip zeropage remapping if the split comes from KSM. It also contains the patch (3) from this version, which skips zeropage remapping if the shrinker is disabled.
This is indeed a good question but I don't know the right solution.
Perhaps we can discuss it here.
So far here are the potential solutions:
a) skip zeropage remapping if the split comes from KSM
b) skip zeropage remapping completely if the shrinker is disabled
c) Disable KSM THP splitting completely
d) add askip_huge_pagesysfs toggle
e) only scan THPs when memory pressure is present(a)+(b) would be my V2 which I can send out whenever.
(c) is the nuclear option, but perhaps KSM for THP is rather inefficient and pointless
Quoted 7 lines
Quoted 5 lines
(d) could be used with (a) and (b) and allows more flexibility than (c)
(e) is a more dynamic approach, and could be combined with (b) and (d)
Let me know what you think :)
Where does the current patch set fall into and what's the problem with it?
this series (v1) would be "dont scan
VM_MERGEABLEentries when KSM is running"It stops all
VM_MEREABLEentries from being candidates, while the V2 I would like to propose goes back to my RFC's alternative solution a ) "skip zeropage remapping if the split comes from KSM"
Quoted 8 lines
Quoted 3 lines
I think (a) is better than V1 as it doesnt indiscriminately target all
VM_MERGEABLEmapping but rather those that are triggering the issue (the split).Not sure. There could be anything else in the system triggering the split before KSM comes around.
So v1 just cleanly separates them, independent who triggers the split.
Yes but now all VM_MERGEABLEs lose the underutilized shrinker independent from weather KSM was the one who triggered it. So if you split a folio from another path, and its VM_MERGEABLE we lose the ability to reclaim these underutilized pages, despite not being from KSM (where the behavior/"bug" lives).
Quoted 2 lines
Why is this a problem?
its not a problem, but it now affects a larger scope than where the problem originates. We could easily shrink the scope to only include splits coming from KSM; then KSM would respect its sysctls, while not regressing the shrinker's functionality on VM_MERGEABLE mappings.
Perhaps I'm not properly expressing the differences, or I'm not fully understanding your concern with my V2 proposed solution.
Cheers,
-- Nico
Quoted 6 lines
--
Cheers,David
David Hildenbrand (Arm)
maintainerRe: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 17 lines · Nico Pache
Quoted 15 lines · David Hildenbrand (Arm)
Quoted 12 lines · Nico Pache
On Wed, Jul 15, 2026 at 3:30 AM David Hildenbrand (Arm) <[email protected]> wrote:
this series (v1) would be "dont scan
VM_MERGEABLEentries when KSM is running"It stops all
VM_MEREABLEentries from being candidates, while the V2 I would like to propose goes back to my RFC's alternative solution a ) "skip zeropage remapping if the split comes from KSM"I think (a) is better than V1 as it doesnt indiscriminately target all
VM_MERGEABLEmapping but rather those that are triggering the issue (the split).
Quoted 7 lines
Quoted 4 lines
Not sure. There could be anything else in the system triggering the split before KSM comes around.
So v1 just cleanly separates them, independent who triggers the split.
Yes but now all
VM_MERGEABLEslose the underutilized shrinker independent from weather KSM was the one who triggered it.
Because KSM takes care of it?
What am I missing?
Signature · 3 lines
Cheers, David
Re: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 22 lines · David Hildenbrand (Arm)
Quoted 20 lines · Nico Pache
Quoted 18 lines · David Hildenbrand (Arm)
Quoted 5 lines · Nico Pache
On Wed, Jul 15, 2026 at 3:30 AM David Hildenbrand (Arm) <[email protected]> wrote:
this series (v1) would be "dont scan
VM_MERGEABLEentries when KSM is running"Quoted 7 lines
It stops all
VM_MEREABLEentries from being candidates, while the V2 I would like to propose goes back to my RFC's alternative solution a ) "skip zeropage remapping if the split comes from KSM"I think (a) is better than V1 as it doesnt indiscriminately target all
VM_MERGEABLEmapping but rather those that are triggering the issue (the split).Not sure. There could be anything else in the system triggering the split before KSM comes around.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
So v1 just cleanly separates them, independent who triggers the split.
Yes but now all
VM_MERGEABLEslose the underutilized shrinker independent from weather KSM was the one who triggered it.Because KSM takes care of it?
KSM only takes care of it in the case of 'use_zero_pages' being set. If we stop KSM splits from reclaiming, we regain the intended sysctl behavior; otherwise, KSM splits and reclaims indiscriminately. The secondary effect is that if we don't have this toggle set, the split would still reclaim these pages. KSM then has to process those pages (despite them already being freed), wasting cycles.
Your proposed solution works too, I just think its a more aggressive approach (stop all VM_MERGEABLEs from split-reclaiming, not just those that are currently being operated on by KSM).
-- Nico
Quoted 8 lines
What am I missing?
--
Cheers,David
David Hildenbrand (Arm)
maintainerRe: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 19 lines · Nico Pache
Quoted 9 lines · David Hildenbrand (Arm)
Quoted 5 lines · Nico Pache
On Tue, Jul 21, 2026 at 8:02 AM David Hildenbrand (Arm) <[email protected]> wrote:
Yes but now all
VM_MERGEABLEslose the underutilized shrinker independent from weather KSM was the one who triggered it.Because KSM takes care of it?
KSM only takes care of it in the case of '
use_zero_pages' being set. If we stop KSM splits from reclaiming, we regain the intended sysctl behavior; otherwise, KSM splits and reclaims indiscriminately. The secondary effect is that if we don't have this toggle set, the split would still reclaim these pages. KSM then has to process those pages (despite them already being freed), wasting cycles.
Quoted 3 lines
Your proposed solution works too, I just think its a more aggressive approach (stop all
VM_MERGEABLEsfrom split-reclaiming, not just those that are currently being operated on by KSM).
I think we should really just stop two mechanisms to compete entirely: if A is active for a region, B is disabled.
Not trying to follow "who split what" and operate based on that.
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 14 lines · David Hildenbrand (Arm)
Quoted 13 lines · Nico Pache
Quoted 3 lines · David Hildenbrand (Arm)
Because KSM takes care of it?
KSM only takes care of it in the case of '
use_zero_pages' being set. If we stop KSM splits from reclaiming, we regain the intended sysctl behavior; otherwise, KSM splits and reclaims indiscriminately. The secondary effect is that if we don't have this toggle set, the split would still reclaim these pages. KSM then has to process those pages (despite them already being freed), wasting cycles.
Quoted 9 lines
Quoted 3 lines
Your proposed solution works too, I just think its a more aggressive approach (stop all
VM_MERGEABLEsfrom split-reclaiming, not just those that are currently being operated on by KSM).I think we should really just stop two mechanisms to compete entirely: if A is active for a region, B is disabled.
Not trying to follow "who split what" and operate based on that.
I took another look and there is just no perfect solution.
Option A: don't let mechanisms interfere - if KSM is enabled, don't remap to the
shared zeropage
Positive: same KSM behavior even if some other mechanism decides to split.
Negative: reclaim cannot free up these pages immediately (waits for KSM), and
having the deferred shrinker split to then not reclaim any actual memory is stupid.
Given that the second KSM run would already deduplicate these pages, I am not sure how much reclaim actually matters.
But taming the deferred shrinker is a bit complicated (no VMA available). In configurations where the deferred shrinker will never trigger a split that's not a problem, but with lower max_ptes_none it would be possible to trigger.
Option B: don't let KSM splits remap to the shared zeropage
Positive: Fix isolated to the actual problematic part we observed.
Negative: Different KSM behavior depending on who split a THP first.
Assume KSM scanned the range first, and then the deferred shrinker split many THP in that area and creates shared zeropage. On the next KSM run, we'd have a very similar behavior. If KSM runs first, you either get zeropages or deduplication to a zero-filled page.
I would not go down a path where we would let KSM skip THPs entirely.
Maybe one could teach KSM about "the deferred shrinker is active, don't split a THP if we are to deduplciate the shared zeropage"? But then, whether the deferred shrinker will actually split depends on the max_ptes_non toggle.
Gahhh so complicated.
Signature · 3 lines
Cheers, David
Re: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 57 lines · David Hildenbrand (Arm)
Quoted 14 lines · David Hildenbrand (Arm)
Quoted 13 lines · Nico Pache
Quoted 3 lines · David Hildenbrand (Arm)
Because KSM takes care of it?
KSM only takes care of it in the case of '
use_zero_pages' being set. If we stop KSM splits from reclaiming, we regain the intended sysctl behavior; otherwise, KSM splits and reclaims indiscriminately. The secondary effect is that if we don't have this toggle set, the split would still reclaim these pages. KSM then has to process those pages (despite them already being freed), wasting cycles.Quoted 9 lines
Quoted 3 lines
Your proposed solution works too, I just think its a more aggressive approach (stop all
VM_MERGEABLEsfrom split-reclaiming, not just those that are currently being operated on by KSM).I think we should really just stop two mechanisms to compete entirely: if A is active for a region, B is disabled.
Not trying to follow "who split what" and operate based on that.
I took another look and there is just no perfect solution.
Option A: don't let mechanisms interfere - if KSM is enabled, don't remap to the
shared zeropagePositive: same KSM behavior even if some other mechanism decides to split.
Negative: reclaim cannot free up these pages immediately (waits for KSM), and
having the deferred shrinker split to then not reclaim any actual memory is stupid.Given that the second KSM run would already deduplicate these pages, I am not sure how much reclaim actually matters.
But taming the deferred shrinker is a bit complicated (no VMA available). In configurations where the deferred shrinker will never trigger a split that's not a problem, but with lower
max_ptes_noneit would be possible to trigger.Option B: don't let KSM splits remap to the shared zeropage
Positive: Fix isolated to the actual problematic part we observed.
Negative: Different KSM behavior depending on who split a THP first.Assume KSM scanned the range first, and then the deferred shrinker split many THP in that area and creates shared zeropage. On the next KSM run, we'd have a very similar behavior. If KSM runs first, you either get zeropages or deduplication to a zero-filled page.
Quoted 8 lines
I would not go down a path where we would let KSM skip THPs entirely.
Maybe one could teach KSM about "the deferred shrinker is active, don't split a THP if we are to deduplciate the shared zeropage"? But then, whether the deferred shrinker will actually split depends on the
max_ptes_nontoggle.Gahhh so complicated.
haha very much so!
This might be the correct solution below, similar to your logic above....
static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_item)
{
...
checksum = calc_checksum(
if (rmap_item->oldchecksum != checksum)
rmap_item->oldchecksum = checksum;
return;
}+ if (folio_test_large(folio) && split_underused_thp &&
+ checksum == zero_checksum &&
+ pages_identical(page, ZERO_PAGE(0)))
+ return;
+
Don't let KSM split a THP if it's a zero page (unless the underused shrinker is inactive)
We also do patch 3 of this series, which disables zero-page remapping if the underused shrinker is disabled.
With this I believe all edge cases and feature interactions are now exclusive.
I will retest. Let me know what you think, and if you agree, I can post it.
Thanks for taking the time to look back at this :) Sorry I've been a bit MIA too, I've been focused on some downstream issues. I've been meaning to review/reply to the high volume of khugepaged patches recently.
-- Nico
Quoted 6 lines
--
Cheers,David
Usama Arif
reviewerRe: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
In reply to Nico PacheQuoted 95 lines · On Tue, 14 Jul 2026 22:05:31 -0600 Nico Pache <[email protected]> wrote:
Quoted 56 lines · Usama Arif
Quoted 53 lines · On Tue, 09 Jun 2026 10:07:20 -0400 Zi Yan <[email protected]> wrote:
Quoted 51 lines · On 9 Jun 2026, at 9:47, [email protected] wrote:
Quoted 25 lines
Quoted 16 lines
Quoted 6 lines
Since commit
b1f202060afe("mm: remap unused subpages to shared zeropage when splitting isolated thp"), splitting an anonymous THP remaps all zero-filled subpages to the shared zeropage viaTTU_USE_SHARED_ZEROPAGE. This flag is set unconditionally for every anonymous folio split, including splits triggered by KSM.Quoted 9 lines
When KSM is enabled with THP=always, this causes two regressions:
1. use_zero_pages=1: KSM calls
try_to_merge_one_page()which triggerssplit_huge_page(). The split remaps all 512 zero-filled subpages to the shared zeropage at once, freeing the entire 2MB THP when KSM only intended to process a single 4KB page. This bypasses KSM'spages_to_scanrate limiting, causing ~1GB to be freed almost instantly.Quoted 1 line
Why do you see it as regressions?
Since the zero-page remapping was introduced our test has shown the following behavior changes:
With use_zero_pages=0, the merge rate drops from 60MB/s to ~6 MB/s even after raising
pages_to_scan. The KSM merging is now much slower and CPU utilization has increased.Quoted 5 lines
With use_zero_pages=1, ~1 GB is freed almost instantly, and it no longer respects the
pages_to_scanbehavior.Even with just this patch (1 & 2) or the RFC linked in the cover letter, the issue no longer occurs.
Understood. You're saying that the additional processing action in
split_huge_page(remap unused subpages to shared zeropage) increases the scanning cost of ksmd.However, I still wouldn't simply classify this as a performance regression, because commit
b1f202060afeincreases memory savings through this action — so it saves memory at the cost of additional CPU overhead.If you want to address the increased overhead on ksmd, I think we could add a check for the shared zeropage in
cmp_and_merge_page, and skip merging when a shared zeropage is detected.Quoted 9 lines
Quoted 8 lines
AFAIU, KSM and THP do often conflict with each other. THP tries hard to collapse a huge page (which may contain many zero pages). If KSM is enabled and part of that huge page is mergeable, it can easily be split by KSM, rendering THP's efforts futile.
Therefore, in our actual production environment, we typically avoid making the same region both KSM mergeable and THP always.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
THP=always is a global setting used in many production environments, so these features now interact very poorly together.
Actually, I have long thought about submitting a patch: add a new interface '
skip_huge_page' under KSM's sysfs, allowing users to choose not to split huge pages.This would be a good intermediate solution while allowing backward compatibility.
Quoted 4 lines
Quoted 4 lines
Just think out loud. Or just skip huge pages all the time unless memory pressure is present. Basically treat KSM as a way of reducing memory pressure by merging pages.
Hmm interesting. We should decide on future changes before committing to one direction; however, i think the immediate solution would be what I currently have prepped for v2.
My v2 follows my RFC alternative approach: skip zeropage remapping if the split comes from KSM. It also contains the patch (3) from this version, which skips zeropage remapping if the shrinker is disabled.
Quoted 3 lines
I do agree with this. I questioned about if it even makes sense to split THP with KSM in RFC (https://lore.kernel.org/all/[email protected]/)
This is indeed a good question but I don't know the right solution.
Perhaps we can discuss it here.
So far here are the potential solutions:
a) skip zeropage remapping if the split comes from KSM
b) skip zeropage remapping completely if the shrinker is disabled
c) Disable KSM THP splitting completely
d) add askip_huge_pagesysfs toggle
e) only scan THPs when memory pressure is present
Quoted 4 lines
(a)+(b) would be my V2 which I can send out whenever.
(c) is the nuclear option, but perhaps KSM for THP is rather inefficient and pointless
I really like and want (c), but I know David has already pointed out that there might be someone already deploying and expecting this.
Quoted 5 lines
(d) could be used with (a) and (b) and allows more flexibility than (c)
(e) is a more dynamic approach, and could be combined with (b) and (d)
(e) sounds like a good idea, but then KSM effectively becomes a THP shrinker?
Quoted 12 lines
Let me know what you think :)
-- NicoQuoted 7 lines
Quoted 6 lines
Best Regards,
Yan, Zi
Lance Yang
reviewerRe: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
In reply to [email protected]Quoted 18 lines · On Tue, Jun 09, 2026 at 08:12:02PM +0800, [email protected] wrote:
Quoted 6 lines
Since commit
b1f202060afe("mm: remap unused subpages to shared zeropage when splitting isolated thp"), splitting an anonymous THP remaps all zero-filled subpages to the shared zeropage viaTTU_USE_SHARED_ZEROPAGE. This flag is set unconditionally for every anonymous folio split, including splits triggered by KSM.Quoted 9 lines
When KSM is enabled with THP=always, this causes two regressions:
1. use_zero_pages=1: KSM calls
try_to_merge_one_page()which triggerssplit_huge_page(). The split remaps all 512 zero-filled subpages to the shared zeropage at once, freeing the entire 2MB THP when KSM only intended to process a single 4KB page. This bypasses KSM'spages_to_scanrate limiting, causing ~1GB to be freed almost instantly.Why do you see it as regressions?
Quoted 7 lines
AFAIU, KSM and THP do often conflict with each other. THP tries hard to collapse a huge page (which may contain many zero pages). If KSM is enabled and part of that huge page is mergeable, it can easily be split by KSM, rendering THP's efforts futile.
Therefore, in our actual production environment, we typically avoid making the same region both KSM mergeable and THP always.
Right, some setups may choose to avoid using KSM and THP always on the same region. But that is not something the kernel can assume :)
David noted in the RFC that QEMU may use both MADV_HUGEPAGE and MADV_MERGEABLE, while KSM can be enabled later system-wide.
And I think Nico means something different from KSM spliting THPs in general.
KSM has been able to split THP before. the new part from b1f202060afe is that a KSM-triggered split can also remap zero-filled subpages to the shared zeropage, outside KSM's own use_zero_pages/pages_to_scan controls.
Maybe the changelog could spell that out :)
Quoted 10 lines
Quoted 9 lines
2. use_zero_pages=0: The same split side-effect occurs through the stable/unstable tree merge paths. Each
pages_to_scaniteration triggers an expensivesplit_huge_page()that silently frees 2MB, while the scanner wastes cycles on tree searches for zero-filled pages that were already freed as a side-effect.Fix this by restricting
TTU_USE_SHARED_ZEROPAGEbeing set in the case that KSM is running and the VMA hasVM_MERGEABLE.
Re: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 35 lines · Lance Yang
Quoted 18 lines · On Tue, Jun 09, 2026 at 08:12:02PM +0800, [email protected] wrote:
Quoted 6 lines
Since commit
b1f202060afe("mm: remap unused subpages to shared zeropage when splitting isolated thp"), splitting an anonymous THP remaps all zero-filled subpages to the shared zeropage viaTTU_USE_SHARED_ZEROPAGE. This flag is set unconditionally for every anonymous folio split, including splits triggered by KSM.Quoted 9 lines
When KSM is enabled with THP=always, this causes two regressions:
1. use_zero_pages=1: KSM calls
try_to_merge_one_page()which triggerssplit_huge_page(). The split remaps all 512 zero-filled subpages to the shared zeropage at once, freeing the entire 2MB THP when KSM only intended to process a single 4KB page. This bypasses KSM'spages_to_scanrate limiting, causing ~1GB to be freed almost instantly.Why do you see it as regressions?
Quoted 7 lines
AFAIU, KSM and THP do often conflict with each other. THP tries hard to collapse a huge page (which may contain many zero pages). If KSM is enabled and part of that huge page is mergeable, it can easily be split by KSM, rendering THP's efforts futile.
Therefore, in our actual production environment, we typically avoid making the same region both KSM mergeable and THP always.
Right, some setups may choose to avoid using KSM and THP always on the same region. But that is not something the kernel can assume :)
David noted in the RFC that QEMU may use both
MADV_HUGEPAGEandMADV_MERGEABLE, while KSM can be enabled later system-wide.
Quoted 8 lines
And I think Nico means something different from KSM spliting THPs in general.
KSM has been able to split THP before. the new part from
b1f202060afeis that a KSM-triggered split can also remap zero-filled subpages to the shared zeropage, outside KSM's own use_zero_pages/pages_to_scan controls.Maybe the changelog could spell that out :)
Yeah maybe I didnt properly explain that :p
After some thought I still think the alternative approach i mentioned in the RFC may be better.
ie) prevent the zero-page merging that results from KSM splitting a folio. The check we add here is more general and will skip this zero-page merging with all MERGEABLE mappings, not just those causing the issue (the KSM splitting). The result is that even migrations, etc that are also MERGEABLE will be skipped needlessly.
If we use this approach we also don't need the first patch of the series.
Cheers,
-- Nico
Quoted 12 lines
Quoted 11 lines
Quoted 10 lines
2. use_zero_pages=0: The same split side-effect occurs through the stable/unstable tree merge paths. Each
pages_to_scaniteration triggers an expensivesplit_huge_page()that silently frees 2MB, while the scanner wastes cycles on tree searches for zero-filled pages that were already freed as a side-effect.Fix this by restricting
TTU_USE_SHARED_ZEROPAGEbeing set in the case that KSM is running and the VMA hasVM_MERGEABLE.
Re: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 28 lines
Quoted 14 lines
Quoted 7 lines
AFAIU, KSM and THP do often conflict with each other. THP tries hard to collapse a huge page (which may contain many zero pages). If KSM is enabled and part of that huge page is mergeable, it can easily be split by KSM, rendering THP's efforts futile.
Therefore, in our actual production environment, we typically avoid making the same region both KSM mergeable and THP always.
Right, some setups may choose to avoid using KSM and THP always on the same region. But that is not something the kernel can assume :)
David noted in the RFC that QEMU may use both
MADV_HUGEPAGEandMADV_MERGEABLE, while KSM can be enabled later system-wide.Quoted 8 lines
And I think Nico means something different from KSM spliting THPs in general.
KSM has been able to split THP before. the new part from
b1f202060afeis that a KSM-triggered split can also remap zero-filled subpages to the shared zeropage, outside KSM's own use_zero_pages/pages_to_scan controls.Maybe the changelog could spell that out :)
Yeah maybe I didnt properly explain that :p
After some thought I still think the alternative approach i mentioned in the RFC may be better.
Quoted 7 lines
ie) prevent the zero-page merging that results from KSM splitting a folio. The check we add here is more general and will skip this zero-page merging with all MERGEABLE mappings, not just those causing the issue (the KSM splitting). The result is that even migrations, etc that are also MERGEABLE will be skipped needlessly.
If we use this approach we also don't need the first patch of the series.
Yeah, That's also what I want to suggest.
Re: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
Quoted 34 lines · On Tue, Jun 09, 2026 at 09:49:47PM +0800, [email protected] wrote:
Quoted 34 lines
Quoted 14 lines
Quoted 7 lines
AFAIU, KSM and THP do often conflict with each other. THP tries hard to collapse a huge page (which may contain many zero pages). If KSM is enabled and part of that huge page is mergeable, it can easily be split by KSM, rendering THP's efforts futile.
Therefore, in our actual production environment, we typically avoid making the same region both KSM mergeable and THP always.
Right, some setups may choose to avoid using KSM and THP always on the same region. But that is not something the kernel can assume :)
David noted in the RFC that QEMU may use both
MADV_HUGEPAGEandMADV_MERGEABLE, while KSM can be enabled later system-wide.Quoted 8 lines
And I think Nico means something different from KSM spliting THPs in general.
KSM has been able to split THP before. the new part from
b1f202060afeis that a KSM-triggered split can also remap zero-filled subpages to the shared zeropage, outside KSM's own use_zero_pages/pages_to_scan controls.Maybe the changelog could spell that out :)
Yeah maybe I didnt properly explain that :p
After some thought I still think the alternative approach i mentioned in the RFC may be better.
ie) prevent the zero-page merging that results from KSM splitting a folio. The check we add here is more general and will skip this zero-page merging with all MERGEABLE mappings, not just those causing the issue (the KSM splitting). The result is that even migrations, etc that are also MERGEABLE will be skipped needlessly.
Quoted 3 lines
Quoted 1 line
If we use this approach we also don't need the first patch of the series.
Yeah, That's also what I want to suggest.
I guess I'll hold off on review until the new version is posted then.
This feels like something we should have worked out in the RFC though?
Thanks, Lorenzo
Re: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM
In reply to Nico PacheQuoted 35 lines · Nico Pache
Since commit
b1f202060afe("mm: remap unused subpages to shared zeropage when splitting isolated thp"), splitting an anonymous THP remaps all zero-filled subpages to the shared zeropage viaTTU_USE_SHARED_ZEROPAGE. This flag is set unconditionally for every anonymous folio split, including splits triggered by KSM.When KSM is enabled with THP=always, this causes two regressions:
1. use_zero_pages=1: KSM calls
try_to_merge_one_page()which triggerssplit_huge_page(). The split remaps all 512 zero-filled subpages to the shared zeropage at once, freeing the entire 2MB THP when KSM only intended to process a single 4KB page. This bypasses KSM'spages_to_scanrate limiting, causing ~1GB to be freed almost instantly.2. use_zero_pages=0: The same split side-effect occurs through the stable/unstable tree merge paths. Each
pages_to_scaniteration triggers an expensivesplit_huge_page()that silently frees 2MB, while the scanner wastes cycles on tree searches for zero-filled pages that were already freed as a side-effect.Fix this by restricting
TTU_USE_SHARED_ZEROPAGEbeing set in the case that KSM is running and the VMA hasVM_MERGEABLE.Fixes b1f202060afe ("mm: remap unused subpages to shared zeropage when splitting isolated thp")Signed-off-by Nico Pache <[email protected]>--- mm/migrate.c | 9 +++++++++ 1 file changed, 9 insertions(+)mm/migrate.c +0
-0
mm/migrate.c static bool try_to_map_unused_to_zeropage(struct page_vma_mapped_walk *pvmw, · line 313
Quoted 10 lines
mm/migrate.c +7
-0@@ -304,6 +304,15 @@ static bool try_to_map_unused_to_zeropage(struct page_vma_mapped_walk *pvmw,if (PageCompound (page) ||PageHWPoison (page))return false; +/* +* Let KSM handle the zero-filled page deduplication according to its +* own rate limit (pages_to_scan) and policy (use_zero_pages). Without +* this, a KSM-triggered THP split would remap all zero-filled subpages +* to the shared zeropage as a side effect. +*/ +if (ksm_is_running () && (pvmw->vma->vm_flags &VM_MERGEABLE ))
Please use the new VMA flag API.
This would be vma_test(pvmw->vma, VMA_MERGEABLE_BIT).
Quoted 8 lines
mm/migrate.c +2
-1+return false; +VM_BUG_ON_PAGE (!PageAnon (page), page);VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (pte_present (old_pte), page); --2.54.0
Thanks, Lorenzo
PATCH 3/3 mm/huge_memory.c: Skip zero-page remapping when underused THP shrinker is disabled
In reply to Nico PacheThe zero-page remapping in __folio_split() was added to support the underused THP shrinker. When the shrinker is disabled via shrink_underused, gate TTU_USE_SHARED_ZEROPAGE on split_underused_thp to avoid scanning subpages for zero content unnecessarily.
--- mm/huge_memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/huge_memory.c +1 -1
@@ -4103,7 +4103,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,if (nr_shmem_dropped)shmem_uncharge (mapping->host, nr_shmem_dropped); -if (!ret && is_anon && !folio_is_device_private (folio)) +if (!ret && is_anon && !folio_is_device_private (folio) && split_underused_thp) ttu_flags =TTU_USE_SHARED_ZEROPAGE ;remap_page (folio,1 << old_order, ttu_flags);
Signature · 1 line
2.54.0