From nobody Thu Apr 2 01:51:38 2026 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [160.30.148.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19F19344DBD for ; Thu, 12 Feb 2026 11:29:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=160.30.148.35 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770895788; cv=none; b=AwY10HApYE5HbueqTvbCBdxTeqRqWJZyPzo9Q0jSWfDmFdalwOBbDGsKIWDqSulwOEgCUAAraiTavf94Y9c1vIac898wr9hcLxPNvEX9Azyat/lGMOzgxaCNrLTXKgDiQXqBTkV5BrIUIIs7s0kuD/wQKDMo70xT4sXV493/zF0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770895788; c=relaxed/simple; bh=TWjv1WZu3xMhZybeWnfCye+0DXnlsEW0wyXbrAqYOmo=; h=Message-ID:In-Reply-To:References:Date:Mime-Version:From:To:Cc: Subject:Content-Type; b=Jtix8Yg1cQ1QzKKbDaW76O30C7UQA5aQQ2bgTD1nCXqjEJlyX8H7H3n+YgJOW0unYHkfHnJ7S0iBIfYIYZrLPvZJQzuLzpnrzMg6LY+zaTfwcX5tJ/xEn4oekCEfKG8EUSuK6V0qxAW2Jio3Nkrv9gVAmI7shCbzHROJkn0oJGg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=160.30.148.35 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4fBY432Nsqz8Xs6w; Thu, 12 Feb 2026 19:29:39 +0800 (CST) Received: from xaxapp01.zte.com.cn ([10.88.99.176]) by mse-fl1.zte.com.cn with SMTP id 61CBTTM7085942; Thu, 12 Feb 2026 19:29:30 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp01[null]) by mapi (Zmail) with MAPI id mid32; Thu, 12 Feb 2026 19:29:32 +0800 (CST) X-Zmail-TransId: 2af9698db99c623-3d550 X-Mailer: Zmail v1.0 Message-ID: <20260212192932941MSsJEAyoRW4YdLBN7_myn@zte.com.cn> In-Reply-To: <20260212192820223O_r2NQzSEPG_C56cs-z4l@zte.com.cn> References: 20260212192820223O_r2NQzSEPG_C56cs-z4l@zte.com.cn Date: Thu, 12 Feb 2026 19:29:32 +0800 (CST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 From: To: , Cc: , , , , , Subject: =?UTF-8?B?W1BBVENIIHYzIDEvMl0ga3NtOiBJbml0aWFsaXplIHRoZSBhZGRyIG9ubHkgb25jZSBpbiBybWFwX3dhbGtfa3Nt?= Content-Type: text/plain; charset="utf-8" X-MAIL: mse-fl1.zte.com.cn 61CBTTM7085942 X-TLS: YES X-SPF-DOMAIN: zte.com.cn X-ENVELOPE-SENDER: xu.xin16@zte.com.cn X-SPF: None X-SOURCE-IP: 10.5.228.132 unknown Thu, 12 Feb 2026 19:29:39 +0800 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 698DB9A3.000/4fBY432Nsqz8Xs6w Content-Transfer-Encoding: quoted-printable From: xu xin This is a minor performance optimization, especially when there are many for-loop iterations, because the addr variable doesn=E2=80=99t change across iterations. Therefore, it only needs to be initialized once before the loop. Signed-off-by: xu xin Acked-by: David Hildenbrand (Arm) --- mm/ksm.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 2d89a7c8b4eb..950e122bcbf4 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3168,6 +3168,8 @@ void rmap_walk_ksm(struct folio *folio, struct rmap_w= alk_control *rwc) return; again: hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) { + /* Ignore the stable/unstable/sqnr flags */ + const unsigned long addr =3D rmap_item->address & PAGE_MASK; struct anon_vma *anon_vma =3D rmap_item->anon_vma; struct anon_vma_chain *vmac; struct vm_area_struct *vma; @@ -3180,16 +3182,13 @@ void rmap_walk_ksm(struct folio *folio, struct rmap= _walk_control *rwc) } 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 =3D vmac->vma; - /* Ignore the stable/unstable/sqnr flags */ - addr =3D rmap_item->address & PAGE_MASK; - if (addr < vma->vm_start || addr >=3D vma->vm_end) continue; /* --=20 2.25.1 From nobody Thu Apr 2 01:51:38 2026 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [160.30.148.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1B0B27E7EB for ; Thu, 12 Feb 2026 11:30:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=160.30.148.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770895854; cv=none; b=Thf8XHkBpMqfABnD30ykYhrf6kHnk1Mwk7LYDaStUzprKSfURMzdTOZP7lfCgwA5FKKhE0VH4iUDBWPVD+KtqTotCZI7hGlOYCyO6Un0GOBz9XVNJxpdtLPWjSlBsAax5SFYYqbXbtEqt6c8ENNBMRPNPCayDV+YCWl2OoDYF/4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770895854; c=relaxed/simple; bh=RmxmplbvO9yCBe9Xi4IfBS7q/GvGXW9jvIJf3A4eyKs=; h=Message-ID:In-Reply-To:References:Date:Mime-Version:From:To:Cc: Subject:Content-Type; b=WUz32OcO00ZUOV6c92CjEtU9ThHVHbKHh1eLcCCUjMgxchiXCoZClA9BueKHnayjrLKRnY2+lAaWVMKGb/FPWvxAvvYVVk46E2/ANumh4Y+yO8xZmuaoWskOBN4GlWr0fVbFbDkIel5OhWYTFHSdVDC0BhJtvYGRZmckDYQyNnY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=160.30.148.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4fBY5R098mz5B12g; Thu, 12 Feb 2026 19:30:51 +0800 (CST) Received: from xaxapp05.zte.com.cn ([10.99.98.109]) by mse-fl1.zte.com.cn with SMTP id 61CBUgRV087278; Thu, 12 Feb 2026 19:30:42 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp01[null]) by mapi (Zmail) with MAPI id mid32; Thu, 12 Feb 2026 19:30:45 +0800 (CST) X-Zmail-TransId: 2af9698db9e5ab7-3e9e3 X-Mailer: Zmail v1.0 Message-ID: <20260212193045556CbzCX8p9gDu73tQ2nvHEI@zte.com.cn> In-Reply-To: <20260212192820223O_r2NQzSEPG_C56cs-z4l@zte.com.cn> References: 20260212192820223O_r2NQzSEPG_C56cs-z4l@zte.com.cn Date: Thu, 12 Feb 2026 19:30:45 +0800 (CST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 From: To: , , Cc: , , , , , Subject: =?UTF-8?B?W1BBVENIIHYzIDIvMl0ga3NtOiBPcHRpbWl6ZSBybWFwX3dhbGtfa3NtIGJ5IHBhc3NpbmcgYSBzdWl0YWJsZSBhZGRyZXNzIHJhbmdl?= X-MAIL: mse-fl1.zte.com.cn 61CBUgRV087278 X-TLS: YES X-SPF-DOMAIN: zte.com.cn X-ENVELOPE-SENDER: xu.xin16@zte.com.cn X-SPF: None X-SOURCE-IP: 10.5.228.132 unknown Thu, 12 Feb 2026 19:30:51 +0800 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 698DB9EB.000/4fBY5R098mz5B12g Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: xu xin Problem =3D=3D=3D=3D=3D=3D=3D When available memory is extremely tight, causing KSM pages to be swapped out, or when there is significant memory fragmentation and THP triggers memory compaction, the system will invoke the rmap_walk_ksm function to perform reverse mapping. However, we observed that this function becomes particularly time-consuming when a large number of VMAs (e.g., 20,000) share the same anon_vma. Through debug trace analysis, we found that most of the latency occurs within anon_vma_interval_tree_foreach, leading to an excessively long hold time on the anon_vma lock (even reaching 500ms or more), which in turn causes upper-layer applications (waiting for the anon_vma lock) to be blocked for extended periods. Root Cause =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Further investigation revealed that 99.9% of iterations inside the anon_vma_interval_tree_foreach loop are skipped due to the first check "if (addr < vma->vm_start || addr >=3D vma->vm_end)), indicating that a lar= ge number of loop iterations are ineffective. This inefficiency arises because the pgoff_start and pgoff_end parameters passed to anon_vma_interval_tree_foreach span the entire address space from 0 to ULONG_MAX, resulting in very poor loop efficiency. Solution =3D=3D=3D=3D=3D=3D=3D=3D In fact, we can significantly improve performance by passing a more precise range based on the given addr. Since the original pages merged by KSM correspond to anonymous VMAs, the page offset can be calculated as pgoff =3D address >> PAGE_SHIFT. Therefore, we can optimize the call by defining: pgoff =3D rmap_item->address >> PAGE_SHIFT; Performance =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D In our real embedded Linux environment, the measured metrcis were as follows: 1) Time_ms: Max time for holding anon_vma lock in a single rmap_walk_ksm. 2) Nr_iteration_total: The max times of iterations in a loop of anon_vma_in= terval_tree_foreach 3) Skip_addr_out_of_range: The max times of skipping due to the first check= (vma->vm_start and vma->vm_end) in a loop of anon_vma_interval_tree_foreach. 4) Skip_mm_mismatch: The max times of skipping due to the second check (rma= p_item->mm =3D=3D vma->vm_mm) in a loop of anon_vma_interval_tree_foreach. The result is as follows: Time_ms Nr_iteration_total Skip_addr_out_of_range Skip_m= m_mismatch Before: 228.65 22169 22168 0 After : 0.396 3 0 2 The referenced reproducer of rmap_walk_ksm can be found at: https://lore.kernel.org/all/20260206151424734QIyWL_pA-1QeJPbJlUxsO@zte.com.= cn/ Co-developed-by: Wang Yaxin Signed-off-by: Wang Yaxin Signed-off-by: xu xin Acked-by: David Hildenbrand (Arm) --- mm/ksm.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mm/ksm.c b/mm/ksm.c index 950e122bcbf4..7b974f333391 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3170,6 +3170,7 @@ void rmap_walk_ksm(struct folio *folio, struct rmap_w= alk_control *rwc) hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) { /* Ignore the stable/unstable/sqnr flags */ const unsigned long addr =3D rmap_item->address & PAGE_MASK; + const pgoff_t pgoff =3D rmap_item->address >> PAGE_SHIFT; struct anon_vma *anon_vma =3D rmap_item->anon_vma; struct anon_vma_chain *vmac; struct vm_area_struct *vma; @@ -3183,8 +3184,12 @@ void rmap_walk_ksm(struct folio *folio, struct rmap_= walk_control *rwc) anon_vma_lock_read(anon_vma); } + /* + * Currently KSM folios are order-0 normal pages, so pgoff_end + * should be the same as pgoff_start. + */ anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root, - 0, ULONG_MAX) { + pgoff, pgoff) { cond_resched(); vma =3D vmac->vma; --=20 2.25.1