From 887311e5cd67566a21ac8c70b9545bcc449714a8 Mon Sep 17 00:00:00 2001 From: xu xin Date: Thu, 9 Jul 2026 17:32:12 +0800 Subject: [PATCH] mm/ksm: initialize the addr only once in collect_procs_ksm Patch series "KSM: use linear_page_index in collect_procs_ksm()", v2. In collect_procs_ksm() which is used to collect processes when the error hit an ksm page, there is the same issue with rmap_walk_ksm (see the previous discussion at [1]). So we apply the similar logic changes to the collect_procs_ksm(). The patch [1/2] move the initializaion of addr from the position inside loop to the position before the loop, since the variable will not change in the loop. The patch [2/2] optimize collect_procs_ksm by passing a suitable page offset range to the anon_vma_interval_tree_foreach loop to reduce ineffective checks. This patch (of 2): Similar to 318d87b8fa7 ("ksm: initialize the addr only once in rmap_walk_ksm"), only initialize the addr once in rmap_walk_ksm because the addr variable doesn't change across iterations. Link: https://lore.kernel.org/20260709173212190rZdwynySRyLr9EtPuXBRU@zte.com.cn Link: https://lore.kernel.org/all/20260703162253688u8Str9eFLR8TGCmo7nIOF@zte.com.cn/ [1] Signed-off-by: xu xin Acked-by: David Hildenbrand (Arm) Cc: Chengming Zhou Signed-off-by: Andrew Morton --- mm/ksm.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index c1dcc3cb4831..337eb04a9340 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3289,7 +3289,7 @@ void collect_procs_ksm(const struct folio *folio, const struct page *page, rcu_read_lock(); for_each_process(tsk) { struct anon_vma_chain *vmac; - unsigned long addr; + const unsigned long addr = rmap_item->address & PAGE_MASK; struct task_struct *t = task_early_kill(tsk, force_early); if (!t) @@ -3299,7 +3299,6 @@ void collect_procs_ksm(const struct folio *folio, const struct page *page, { vma = vmac->vma; if (vma->vm_mm == t->mm) { - addr = rmap_item->address & PAGE_MASK; add_to_kill_ksm(t, page, vma, to_kill, addr); }