Re: [PATCH v2 2/2] mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits

From: Xu Xin (vivo)

Date: Mon Nov 16 2026 - 20:02:29 EST


On Mon, Nov 16, 2026 at 04:58:55PM -0700, Nico Pache wrote:
>
>
> On 11/16/26 3:21 PM, David Hildenbrand (Arm) wrote:
> > On 10/25/26 18:39, 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(), which uses kthread_func() to check whether
> > > the current task is the KSM scanning thread, and use it in
> > > try_to_map_unused_to_zeropage() to skip zero-page remapping when the
> > > split is triggered by ksmd.
> > >
> > > Fixes: b1f202060afe ("mm: remap unused subpages to shared zeropage when splitting isolated thp")
> > > Signed-off-by: Nico Pache (Red Hat) <nico.pache@xxxxxxxxx>
> > > ---
> >
> > [...]
> >
> > > 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 7bdcdb57652f..c33ae3fa1043 100644
> > > --- a/mm/migrate.c
> > > +++ b/mm/migrate.c
> > > @@ -305,6 +305,9 @@ static bool try_to_map_unused_to_zeropage(struct page_vma_mapped_walk *pvmw,
> > > if (PageCompound(page) || PageHWPoison(page))
> > > return false;
> >
> > This really needs a comment like:
> >
> > /*
> > * When the split is triggered by KSM, let it take care of scanning-for and
> > * de-duplicating the shared zeropage.
> > */
>
> Ack! ill wait a day or two before sending a fixup in case others review.
> >
> > > + if (current_is_ksmd())
> > > + 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);
> > >
> >
> > I'm still a bit unclear of what happens when the split is triggered by something
> > else in the system just before we walk into this THP.
> >
> > Like, imagine the deferred shrinker scans for 0-filled pages and splits under
> > memory pressure. Then KSM scans and essentially skips the (now-split) THP?
>
> IIUC, we dont skip the (now-split) THP (ksm works by page, not folio), we
> would only skip over the parts that were remapped to the zeropage. and
> because the shrinker already split the folio, it wont end up even needing to
> split it.

Yes, that's right, KSM would skip zero page during scan_get_next_rmap_item, but still
scan non-zero parts.

>
> Inversely, if ksm does run into a large folio, it splits it, but doesnt do
> the remapping. Which KSM will either eventually deduplicate itself (and in
> the case of use_zero_page=1, it will also do the zeropage remapping).
>
> I asked our robot overlords if im missing anything and it did find this
> potential issue:
>
> KSM could trigger alloc/reclaim that would then prevent the zero-page
> remapping for any reclaim done under the daemon.
>
> It did suggest this fix `current_is_ksmd() && !current->reclaim_state`
> basically check if we are in reclaim and ksm. This would close the gap.
>

Yeah, `current_is_ksmd() && !current->reclaim_state` would be more precise.

Generally, LGTM if you fix that.

> Or we could go back to adding a flag to split function (but i believe this
> is still somewhat cleaner and doesnt complicate the already messy split
> code).
>
> Cheers,
> -- Nico
>
>
>
>
>
> >
>