]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
ksm: initialize the addr only once in rmap_walk_ksm
authorxu xin <xu.xin16@zte.com.cn>
Thu, 12 Feb 2026 11:29:32 +0000 (19:29 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Sun, 5 Apr 2026 20:52:35 +0000 (13:52 -0700)
This is a minor performance optimization, especially when there are many
for-loop iterations, because the addr variable doesn't change across
iterations.

Therefore, it only needs to be initialized once before the loop.

Link: https://lkml.kernel.org/r/20260212192820223O_r2NQzSEPG_C56cs-z4l@zte.com.cn
Link: https://lkml.kernel.org/r/20260212192932941MSsJEAyoRW4YdLBN7_myn@zte.com.cn
Signed-off-by: xu xin <xu.xin16@zte.com.cn>
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Cc: Chengming Zhou <chengming.zhou@linux.dev>
Cc: Hugh Dickins <hughd@google.com>
Cc: Wang Yaxin <wang.yaxin@zte.com.cn>
Cc: Yang Yang <yang.yang29@zte.com.cn>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/ksm.c

index bda71ae609ff3d13a5cc64f6f0b7552f0d260eec..2a2f2f005fc37afbee26abd6c9f6b21d787108a4 100644 (file)
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -3168,6 +3168,8 @@ void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc)
                return;
 again:
        hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) {
+               /* Ignore the stable/unstable/sqnr flags */
+               const unsigned long addr = rmap_item->address & PAGE_MASK;
                struct anon_vma *anon_vma = rmap_item->anon_vma;
                struct anon_vma_chain *vmac;
                struct vm_area_struct *vma;
@@ -3180,16 +3182,13 @@ again:
                        }
                        anon_vma_lock_read(anon_vma);
                }
+
                anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root,
                                               0, ULONG_MAX) {
-                       unsigned long addr;
 
                        cond_resched();
                        vma = vmac->vma;
 
-                       /* Ignore the stable/unstable/sqnr flags */
-                       addr = rmap_item->address & PAGE_MASK;
-
                        if (addr < vma->vm_start || addr >= vma->vm_end)
                                continue;
                        /*