From: "Xu Xin (vivo)" <xu.xin@linux.dev>
To: nico.pache@linux.dev
Cc: aarcange@redhat.com, akpm@linux-foundation.org,
apopple@nvidia.com, baohua@kernel.org,
baolin.wang@linux.alibaba.com, byungchul@sk.com,
chengming.zhou@linux.dev, david@kernel.org, dev.jain@arm.com,
gourry@gourry.net, joshua.hahnjy@gmail.com, kas@kernel.org,
lance.yang@linux.dev, liam@infradead.org,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
matthew.brost@intel.com, ryan.roberts@arm.com,
usama.arif@linux.dev, usamaarif642@gmail.com, xu.xin@linux.dev,
ying.huang@linux.alibaba.com, yuzhao@google.com, ziy@nvidia.com
Subject: Re: [PATCH v3] mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits
Date: Wed, 25 Nov 2026 10:13:30 +0800 [thread overview]
Message-ID: <20261125021330.54446-1-xu.xin@linux.dev> (raw)
In-Reply-To: <20261122-b4-ksm_fixv2-v3-1-f78ea7779746@linux.dev>
> 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 v3:
> - Drop the patch gating zero-page remapping on shrink_underused.
> - Allow zero-page remapping during reclaim running in ksmd's context
> - Add a comment explaining the ksmd check (David Hildenbrand).
>
> 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(+)
LGTM, so
Acked-by: Xu Xin (vivo) <xu.xin@linux.dev>
>
> 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 65869b8acec5..b0949f43ed5c 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 (current_is_ksmd() && !current->reclaim_state)
> + return false;
> +
prev parent reply other threads:[~2026-11-25 2:13 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-22 22:20 [PATCH v3] mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits Nico Pache (Red Hat)
2026-11-23 16:33 ` David Hildenbrand (Arm)
2026-11-24 23:28 ` Nico Pache
2026-11-25 10:27 ` David Hildenbrand (Arm)
2026-11-25 2:13 ` Xu Xin (vivo) [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125021330.54446-1-xu.xin@linux.dev \
--to=xu.xin@linux.dev \
--cc=aarcange@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=apopple@nvidia.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=byungchul@sk.com \
--cc=chengming.zhou@linux.dev \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=gourry@gourry.net \
--cc=joshua.hahnjy@gmail.com \
--cc=kas@kernel.org \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=matthew.brost@intel.com \
--cc=nico.pache@linux.dev \
--cc=ryan.roberts@arm.com \
--cc=usama.arif@linux.dev \
--cc=usamaarif642@gmail.com \
--cc=ying.huang@linux.alibaba.com \
--cc=yuzhao@google.com \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.