From: "Xu Xin (vivo)" <xu.xin@linux.dev>
To: xu.xin@linux.dev
Cc: chengming.zhou@linux.dev, david@kernel.org,
linux-kernel@vger.kernel.org, linux-mm@kvack.org, ljs@kernel.org
Subject: [PATCH 1/2] mm/ksm: extract helper functions for scan cycle cleanup
Date: Thu, 12 Nov 2026 20:59:28 +0800 [thread overview]
Message-ID: <20261112125928.177598-1-xu.xin@linux.dev> (raw)
In-Reply-To: <20261112125754.176930-1-xu.xin@linux.dev>
Since scan_get_next_rmap_item() is getting too large, extract two new
helper functions, ksm_prune_migrate_nodes() and ksm_reset_unstable_trees(),
to reduce indentation levels, isolate local variables, and improve the
readability of the main scan loop.
No functional change intended.
Signed-off-by: Xu Xin (vivo) <xu.xin@linux.dev>
---
mm/ksm.c | 56 +++++++++++++++++++++++++++++++++-----------------------
1 file changed, 33 insertions(+), 23 deletions(-)
diff --git a/mm/ksm.c b/mm/ksm.c
index 5e24b5ca8dc8..49977bcf04f7 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -2609,6 +2609,37 @@ static struct mm_walk_ops ksm_next_page_ops = {
.walk_lock = PGWALK_RDLOCK,
};
+static void ksm_prune_migrate_nodes(void)
+{
+ struct ksm_stable_node *stable_node, *next;
+ struct folio *folio;
+
+ if (ksm_merge_across_nodes)
+ return;
+
+ /*
+ * Whereas stale stable_nodes on the stable_tree itself
+ * get pruned in the regular course of stable_tree_search(),
+ * those moved out to the migrate_nodes list can accumulate:
+ * so prune them once before each full scan.
+ */
+ list_for_each_entry_safe(stable_node, next, &migrate_nodes, list) {
+ folio = ksm_get_folio(stable_node, KSM_GET_FOLIO_NOLOCK);
+
+ if (folio)
+ folio_put(folio);
+ cond_resched();
+ }
+}
+
+static void ksm_reset_unstable_trees(void)
+{
+ int nid;
+
+ for (nid = 0; nid < ksm_nr_node_ids; nid++)
+ root_unstable_tree[nid] = RB_ROOT;
+}
+
static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page)
{
struct mm_struct *mm;
@@ -2617,7 +2648,6 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page)
struct vm_area_struct *vma;
struct ksm_rmap_item *rmap_item;
struct vma_iterator vmi;
- int nid;
if (list_empty(&ksm_mm_head.slot.mm_node))
return NULL;
@@ -2639,28 +2669,8 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page)
*/
lru_add_drain_all();
- /*
- * Whereas stale stable_nodes on the stable_tree itself
- * get pruned in the regular course of stable_tree_search(),
- * those moved out to the migrate_nodes list can accumulate:
- * so prune them once before each full scan.
- */
- if (!ksm_merge_across_nodes) {
- struct ksm_stable_node *stable_node, *next;
- struct folio *folio;
-
- list_for_each_entry_safe(stable_node, next,
- &migrate_nodes, list) {
- folio = ksm_get_folio(stable_node,
- KSM_GET_FOLIO_NOLOCK);
- if (folio)
- folio_put(folio);
- cond_resched();
- }
- }
-
- for (nid = 0; nid < ksm_nr_node_ids; nid++)
- root_unstable_tree[nid] = RB_ROOT;
+ ksm_prune_migrate_nodes();
+ ksm_reset_unstable_trees();
spin_lock(&ksm_mmlist_lock);
slot = list_entry(mm_slot->slot.mm_node.next,
--
2.34.1
next prev parent reply other threads:[~2026-11-12 12:59 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 12:57 [PATCH 0/2] mm/ksm: clean up scan_get_next_rmap_item() initialization Xu Xin (vivo)
2026-11-12 12:59 ` Xu Xin (vivo) [this message]
2026-11-12 12:59 ` [PATCH 2/2] mm/ksm: add initialization helpers for scan_get_next_rmap_item() Xu Xin (vivo)
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=20261112125928.177598-1-xu.xin@linux.dev \
--to=xu.xin@linux.dev \
--cc=chengming.zhou@linux.dev \
--cc=david@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®