* [PATCH v4] mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits
@ 2026-12-05 2:57 Nico Pache (Red Hat)
2026-12-05 17:46 ` David Hildenbrand (Arm)
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Nico Pache (Red Hat) @ 2026-12-05 2:57 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, David Hildenbrand, Zi Yan, Baolin Wang,
Liam R. Howlett, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Kiryl Shutsemau, Yu Zhao, Xu Xin, Chengming Zhou,
Matthew Brost, Joshua Hahn, Byungchul Park, Gregory Price,
Ying Huang, Alistair Popple, Usama Arif, aarcange,
Nico Pache (Red Hat)
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 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.
Introduce current_is_ksmd(), to check if the current task is the KSM
thread. Use it in try_to_map_unused_to_zeropage() to skip zero-page
remapping when the split is triggered by ksmd. Allow remapping during
reclaim running in ksmd's context.
Fixes: b1f202060afe ("mm: remap unused subpages to shared zeropage when splitting isolated thp")
Signed-off-by: Nico Pache (Red Hat) <nico.pache@linux.dev>
---
This patch fixes a bug in KSM where the pages_to_scan limit is
bypassed and the policy (use_zero_pages) is ignored.
Commit b1f202060afe ("mm: remap unused subpages to shared zeropage when
splitting isolated thp") added zero-page remapping when an anonymous
folio is split, including splits triggered by KSM.
Skip zero-page remapping when ksmd triggers the split, while allowing
remapping during reclaim running in ksmd's context.
Changes in v4:
- wrap the check in unlikely()
V3: https://lore.kernel.org/all/20261122-b4-ksm_fixv2-v3-1-f78ea7779746@linux.dev
V2: https://lore.kernel.org/all/20261025-b4-ksm_fixv2-v2-0-8b61ad0491f1@linux.dev
V1: https://lore.kernel.org/all/20260609114619.144416-1-npache@redhat.com/
RFC: https://lore.kernel.org/all/20260508170509.640851-1-npache@redhat.com/
---
include/linux/ksm.h | 6 ++++++
mm/ksm.c | 5 +++++
mm/migrate.c | 8 ++++++++
3 files changed, 19 insertions(+)
diff --git a/include/linux/ksm.h b/include/linux/ksm.h
index a0d358a1feb6..23c8e9f60118 100644
--- a/include/linux/ksm.h
+++ b/include/linux/ksm.h
@@ -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 current_is_ksmd(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 current_is_ksmd(void)
+{
+ return false;
+}
+
static inline struct folio *ksm_might_need_to_copy(struct folio *folio,
struct page *page, struct vm_area_struct *vma, unsigned long addr)
{
diff --git a/mm/ksm.c b/mm/ksm.c
index d24b129932bf..60656b05419a 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -2873,6 +2873,11 @@ static int ksm_scan_thread(void *nothing)
return 0;
}
+bool current_is_ksmd(void)
+{
+ return kthread_func(current) == ksm_scan_thread;
+}
+
static bool __ksm_should_add_vma(const struct file *file, vma_flags_t vma_flags)
{
if (vma_flags_test(&vma_flags, VMA_MERGEABLE_BIT))
diff --git a/mm/migrate.c b/mm/migrate.c
index 7841968dd1f5..29cbb8aecc01 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -305,6 +305,14 @@ static bool try_to_map_unused_to_zeropage(struct page_vma_mapped_walk *pvmw,
if (PageCompound(page) || PageHWPoison(page))
return false;
+ /*
+ * When a split is triggered by KSM, let KSM take care of scanning for
+ * and deduplicating the shared zeropage. Reclaim in the ksmd context
+ * should still be able to reclaim zero-filled pages.
+ */
+ if (unlikely(current_is_ksmd() && !current->reclaim_state))
+ 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);
---
base-commit: 2076c0591c1f232f3c1e6ce047f189bcf59610b4
change-id: 20261025-b4-ksm_fixv2-2bf062e92946
Best regards,
--
Nico Pache (Red Hat) <nico.pache@linux.dev>
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v4] mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits
2026-12-05 2:57 [PATCH v4] mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits Nico Pache (Red Hat)
@ 2026-12-05 17:46 ` David Hildenbrand (Arm)
2026-12-05 20:17 ` Nico Pache
2026-12-05 17:50 ` David Hildenbrand
2026-12-05 17:52 ` sashiko-bot
2 siblings, 1 reply; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-12-05 17:46 UTC (permalink / raw)
To: Nico Pache (Red Hat), linux-mm, linux-kernel
Cc: Andrew Morton, Zi Yan, Baolin Wang, Liam R. Howlett, Ryan Roberts,
Dev Jain, Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau,
Yu Zhao, Xu Xin, Chengming Zhou, Matthew Brost, Joshua Hahn,
Byungchul Park, Gregory Price, Ying Huang, Alistair Popple,
Usama Arif, aarcange
On 12/5/26 03:57, Nico Pache (Red Hat) wrote:
> 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 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.
>
> Introduce current_is_ksmd(), to check if the current task is the KSM
> thread. Use it in try_to_map_unused_to_zeropage() to skip zero-page
> remapping when the split is triggered by ksmd. Allow remapping during
> reclaim running in ksmd's context.
>
> Fixes: b1f202060afe ("mm: remap unused subpages to shared zeropage when splitting isolated thp")
> Signed-off-by: Nico Pache (Red Hat) <nico.pache@linux.dev>
> ---
> This patch fixes a bug in KSM where the pages_to_scan limit is
> bypassed and the policy (use_zero_pages) is ignored.
>
> Commit b1f202060afe ("mm: remap unused subpages to shared zeropage when
> splitting isolated thp") added zero-page remapping when an anonymous
> folio is split, including splits triggered by KSM.
>
> Skip zero-page remapping when ksmd triggers the split, while allowing
> remapping during reclaim running in ksmd's context.
>
> Changes in v4:
> - wrap the check in unlikely()
>
> V3: https://lore.kernel.org/all/20261122-b4-ksm_fixv2-v3-1-f78ea7779746@linux.dev
> V2: https://lore.kernel.org/all/20261025-b4-ksm_fixv2-v2-0-8b61ad0491f1@linux.dev
> V1: https://lore.kernel.org/all/20260609114619.144416-1-npache@redhat.com/
> RFC: https://lore.kernel.org/all/20260508170509.640851-1-npache@redhat.com/
> ---
> include/linux/ksm.h | 6 ++++++
> mm/ksm.c | 5 +++++
> mm/migrate.c | 8 ++++++++
> 3 files changed, 19 insertions(+)
>
> diff --git a/include/linux/ksm.h b/include/linux/ksm.h
> index a0d358a1feb6..23c8e9f60118 100644
> --- a/include/linux/ksm.h
> +++ b/include/linux/ksm.h
> @@ -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 current_is_ksmd(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 current_is_ksmd(void)
> +{
> + return false;
> +}
> +
> static inline struct folio *ksm_might_need_to_copy(struct folio *folio,
> struct page *page, struct vm_area_struct *vma, unsigned long addr)
> {
> diff --git a/mm/ksm.c b/mm/ksm.c
> index d24b129932bf..60656b05419a 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -2873,6 +2873,11 @@ static int ksm_scan_thread(void *nothing)
> return 0;
> }
>
> +bool current_is_ksmd(void)
> +{
> + return kthread_func(current) == ksm_scan_thread;
> +}
> +
> static bool __ksm_should_add_vma(const struct file *file, vma_flags_t vma_flags)
> {
> if (vma_flags_test(&vma_flags, VMA_MERGEABLE_BIT))
> diff --git a/mm/migrate.c b/mm/migrate.c
> index 7841968dd1f5..29cbb8aecc01 100644
> --- a/mm/migrate.c
> +++ b/mm/migrate.c
> @@ -305,6 +305,14 @@ static bool try_to_map_unused_to_zeropage(struct page_vma_mapped_walk *pvmw,
> if (PageCompound(page) || PageHWPoison(page))
> return false;
>
> + /*
> + * When a split is triggered by KSM, let KSM take care of scanning for
> + * and deduplicating the shared zeropage. Reclaim in the ksmd context
> + * should still be able to reclaim zero-filled pages.
> + */
> + if (unlikely(current_is_ksmd() && !current->reclaim_state))
> + return false;
I think I'll reverse both checks when applying.
I don't think we want to CC stable, right?
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v4] mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits
2026-12-05 2:57 [PATCH v4] mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits Nico Pache (Red Hat)
2026-12-05 17:46 ` David Hildenbrand (Arm)
@ 2026-12-05 17:50 ` David Hildenbrand
2026-12-05 17:52 ` sashiko-bot
2 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand @ 2026-12-05 17:50 UTC (permalink / raw)
To: linux-mm, linux-kernel, Nico Pache (Red Hat)
Cc: Andrew Morton, David Hildenbrand, Zi Yan, Baolin Wang,
Liam R. Howlett, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Kiryl Shutsemau, Yu Zhao, Xu Xin, Chengming Zhou,
Matthew Brost, Joshua Hahn, Byungchul Park, Gregory Price,
Ying Huang, Alistair Popple, Usama Arif, aarcange
On Fri, 04 Dec 2026 19:57:24 -0700, Nico Pache (Red Hat) wrote:
> 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 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:
>
> [...]
Applied, thanks!
tree: mm/core.git
branch: ksm-7.5.misc
Applied patches will appear soon in mm-next, followed by linux-next.
Please report any outstanding bugs that were missed during review, so we
can either drop the patch series or decide how to fix it up.
It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even
though the patches have already been applied. If possible, patch trailers
will be updated.
Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. Further, this is not a guarantee that the
patches will go upstream. If in doubt, please check the listed branch.
[1/1] mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits
https://git.kernel.org/mm/core/c/cc37f689
--
Cheers,
David
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v4] mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits
2026-12-05 2:57 [PATCH v4] mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits Nico Pache (Red Hat)
2026-12-05 17:46 ` David Hildenbrand (Arm)
2026-12-05 17:50 ` David Hildenbrand
@ 2026-12-05 17:52 ` sashiko-bot
2 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-05 17:52 UTC (permalink / raw)
To: Nico Pache (Red Hat), linux-mm
Cc: Andrew Morton, David Hildenbrand, Zi Yan, Baolin Wang,
Liam R. Howlett, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Kiryl Shutsemau, Yu Zhao, Xu Xin, Chengming Zhou,
Matthew Brost, Joshua Hahn, Byungchul Park, Gregory Price,
Ying Huang, Alistair Popple, Usama Arif, aarcange
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] mm: reverse check order in try_to_map_unused_to_zeropage() per
v3 review
--
commit a53defdb23ee83f9b6a62a1f77e077bb1dd6d7de
Author: Nico Pache (Red Hat) <nico.pache@linux.dev>
mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits
This commit adds current_is_ksmd() and uses it in
try_to_map_unused_to_zeropage() to skip remapping zero-filled subpages to
the shared zeropage during ksmd-triggered THP splits, while still allowing
it during reclaim in ksmd's context.
> diff --git a/mm/migrate.c b/mm/migrate.c
> index 7841968dd1f52..29cbb8aecc013 100644
> --- a/mm/migrate.c
> +++ b/mm/migrate.c
> @@ -305,6 +305,14 @@ static bool try_to_map_unused_to_zeropage(struct page_vma_mapped_walk *pvmw,
> if (PageCompound(page) || PageHWPoison(page))
> return false;
>
> + /*
> + * When a split is triggered by KSM, let KSM take care of scanning for
> + * and deduplicating the shared zeropage. Reclaim in the ksmd context
> + * should still be able to reclaim zero-filled pages.
> + */
> + if (unlikely(current_is_ksmd() && !current->reclaim_state))
> + return false;
[Severity: Low]
As reported by David Hildenbrand (Arm) on v3 [1], should the checks be
reversed to test !current->reclaim_state before current_is_ksmd()?
On reclaim-triggered THP splits such as deferred_split_scan() via
shrink_slab(), current->reclaim_state is non-NULL. Checking
!current->reclaim_state first short-circuits on a single load from current
and avoids calling out-of-line current_is_ksmd() and kthread_func() on
every subpage.
Inlining current_is_ksmd() in a core-mm header such as mm/internal.h was
also suggested on v3 to drop the extra call layer when
!current->reclaim_state is true.
[1] https://lore.kernel.org/r/27c7a176-cd38-4e2c-b2e6-ad63b79ffc07@kernel.org
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-b4-ksm_fixv2-v4-1-c5053d804e00@linux.dev?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v4] mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits
2026-12-05 17:46 ` David Hildenbrand (Arm)
@ 2026-12-05 20:17 ` Nico Pache
0 siblings, 0 replies; 5+ messages in thread
From: Nico Pache @ 2026-12-05 20:17 UTC (permalink / raw)
To: David Hildenbrand (Arm), linux-mm, linux-kernel
Cc: Andrew Morton, Zi Yan, Baolin Wang, Liam R. Howlett, Ryan Roberts,
Dev Jain, Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau,
Yu Zhao, Xu Xin, Chengming Zhou, Matthew Brost, Joshua Hahn,
Byungchul Park, Gregory Price, Ying Huang, Alistair Popple,
Usama Arif, aarcange
On 12/5/26 10:46 AM, David Hildenbrand (Arm) wrote:
>
> I think I'll reverse both checks when applying.
Oh yeah sorry you did state that in the V3. I also forgot to pick up Xu
Xin's Ack.
>
> I don't think we want to CC stable, right?
We could, but i wouldnt say its a urgent issue given all the other
stable work that is being done.
>
> Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
Thank you :)
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-05 20:17 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 2:57 [PATCH v4] mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits Nico Pache (Red Hat)
2026-12-05 17:46 ` David Hildenbrand (Arm)
2026-12-05 20:17 ` Nico Pache
2026-12-05 17:50 ` David Hildenbrand
2026-12-05 17:52 ` sashiko-bot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.