Skip to contentSkip to conversation

mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits

PATCH In review Nico Pache (Red Hat) 3 participants 5 messages Nov 22 – Nov 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits

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")

---
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/[email protected]
V1: https://lore.kernel.org/all/[email protected]/
RFC: https://lore.kernel.org/all/[email protected]/

---
 include/linux/ksm.h | 6 ++++++
 mm/ksm.c            | 5 +++++
 mm/migrate.c        | 8 ++++++++
 3 files changed, 19 insertions(+)
include/linux/ksm.h +6 -0
index a0d358a1feb6..23c8e9f60118 100644
@@ -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)
 {
mm/ksm.c +5 -0
index d24b129932bf..60656b05419a 100644
@@ -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))
mm/migrate.c +8 -0
index 65869b8acec5..b0949f43ed5c 100644
@@ -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;
+
 	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: 2dc60bd9975fc8666a13d58410499e06e6799ba9
change-id: 20261025-b4-ksm_fixv2-2bf062e92946

Best regards,

Signature · 1 line
Nico Pache (Red Hat) <[email protected]>
#1 lore raw
Quoted 48 lines · 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")

---
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/[email protected]
V1: https://lore.kernel.org/all/[email protected]/
RFC: https://lore.kernel.org/all/[email protected]/
---

[...]

Quoted 9 lines
include/linux/ksm.h +1 -0
+
 static inline struct folio *ksm_might_need_to_copy(struct folio *folio,
 		struct page *page, struct vm_area_struct *vma, unsigned long addr)
 {
mm/ksm.c +0 -0
index d24b129932bf..60656b05419a 100644

mm/ksm.c static int ksm_scan_thread(void *nothing) · line 2879

Quoted 7 lines
mm/ksm.c +4 -0
@@ -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;
+}

I do wonder if we wan to make this an inline function to not require a function call just to check this.

Thoughts?

Quoted 9 lines
mm/ksm.c +1 -0
+
 static bool __ksm_should_add_vma(const struct file *file, vma_flags_t vma_flags)
 {
 	if (vma_flags_test(&vma_flags, VMA_MERGEABLE_BIT))
mm/migrate.c +0 -0
index 65869b8acec5..b0949f43ed5c 100644

mm/migrate.c static bool try_to_map_unused_to_zeropage(struct page_vma_mapped_walk *pvmw, · line 314

Quoted 10 lines
mm/migrate.c +7 -0
@@ -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;

Probably helpful to throw in an unlikely() and to to reverse the checks (checking the latter is cheaper than the former).

In general LGTM.

Signature · 3 lines
Cheers,

David
author
#2 lore raw
Quoted 9 lines · David Hildenbrand (Arm)

[...]

I do wonder if we wan to make this an inline function to not require a function call just to check this.

Thoughts?

we would have to also expose ksm_scan_thread; tbh im not sure what the impact of that is.

Quoted 4 lines

Probably helpful to throw in an unlikely() and to to reverse the checks (checking the latter is cheaper than the former).

Sounds good! ill send a fixup once we determine if we want to inline the function.

Quoted 2 lines

In general LGTM.

Thank you!
-- Nico

Quoted 1 line
#3 lore raw
Quoted 8 lines · Nico Pache
Quoted 5 lines · David Hildenbrand (Arm)

[...]

Quoted 7 lines
Quoted 4 lines

I do wonder if we wan to make this an inline function to not require a function call just to check this.

Thoughts?

we would have to also expose ksm_scan_thread; tbh im not sure what the impact of that is.

I wouldn't worry about the impact. Maybe put it in mm_inline.h such that it's limited to core-mm.

Signature · 3 lines
Cheers,

David
reviewer Acked-by on v3 1/1
#4 lore raw
In reply to Nico Pache (Red Hat)
Quoted 44 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 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")

---
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).

Quoted 8 lines

V2: https://lore.kernel.org/all/[email protected]
V1: https://lore.kernel.org/all/[email protected]/
RFC: https://lore.kernel.org/all/[email protected]/

---
 include/linux/ksm.h | 6 ++++++
 mm/ksm.c            | 5 +++++
 mm/migrate.c        | 8 ++++++++
 3 files changed, 19 insertions(+)

LGTM, so

Quoted 57 lines
include/linux/ksm.h +6 -0
index a0d358a1feb6..23c8e9f60118 100644
@@ -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)
 {
mm/ksm.c +5 -0
index d24b129932bf..60656b05419a 100644
@@ -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))
mm/migrate.c +8 -0
index 65869b8acec5..b0949f43ed5c 100644
@@ -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;
+