[PATCH 08/14] mm/ksm: document the two remove_rmap_item_from_tree() calls
From: Longlong Xia
Date: Tue Sep 15 2026 - 12:57:51 EST
From: Longlong Xia <xialonglong@xxxxxxxxxx>
Explain why cmp_and_merge_page() needs both tree-removal calls:
non-KSM pages are detached before the early returns, while KSM pages
are handled after stable_tree_search().
Assisted-by: Zcode:GLM-5.3
Signed-off-by: Longlong Xia <xialonglong@xxxxxxxxxx>
---
mm/ksm.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/mm/ksm.c b/mm/ksm.c
index 7fc9dac5d67c..1733b23cfaa2 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -2313,6 +2313,10 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
if (!is_page_sharing_candidate(stable_node))
max_page_sharing_bypass = true;
} else {
+ /*
+ * Detach before the checksum and zero-page checks, which can
+ * return without reaching the removal below.
+ */
remove_rmap_item_from_tree(rmap_item);
/*
@@ -2338,6 +2342,10 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
return;
}
+ /*
+ * A KSM page can still have an old tree association here; non-KSM
+ * pages were already detached before the checks above.
+ */
remove_rmap_item_from_tree(rmap_item);
if (kfolio) {
--
2.43.0