From nobody Mon Feb 9 14:28:07 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 B5F722C11CF for ; Fri, 6 Feb 2026 09:58:07 +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=1770371888; cv=none; b=Pyg8EnAEJCyEOGYPWs3eb9jURhbWEmUXWg6lCvDUcQsKAxBM5dqY+cJRcdR2lhlHgyMVrVDmQBtuN4ySYc+ucOcB2ZIp37cQlDlxM8Vq0x6K2iOCA3OIp9QTy0AZRnN+tCIsUzLrOuqfUObaPg6yYtR2dz8XKh3I2deFo5SPDBA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770371888; c=relaxed/simple; bh=aNuwTMOYF8IhgHHqDSWzxPocv7fYpUdmDd/e20G7KhE=; h=Message-ID:In-Reply-To:References:Date:Mime-Version:From:To:Cc: Subject:Content-Type; b=jQkmErP3K9lpZMSqoGg2+GeEHCzH7f/SPufHhVVvda4TYQ5Tp0nrXhYzQwDU+xq+nGQxolsFebfZovU7T1v1uKB1XXTwX1iGbvEFAqTIhRj5WVr0R6SvI+y4VuUtICAuOidZ262ow+ZDo14pjgj1AogZcW1NBYDvFaD5kNImoXw= 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 4f6qK35vYlz5B1K3; Fri, 06 Feb 2026 17:57:59 +0800 (CST) Received: from xaxapp02.zte.com.cn ([10.88.97.241]) by mse-fl1.zte.com.cn with SMTP id 6169vg3f001817; Fri, 6 Feb 2026 17:57:42 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp02[null]) by mapi (Zmail) with MAPI id mid32; Fri, 6 Feb 2026 17:57:45 +0800 (CST) X-Zmail-TransId: 2afa6985bb19e39-1d165 X-Mailer: Zmail v1.0 Message-ID: <20260206175745201sbMJ9Ru8QZsjAo7YMopKS@zte.com.cn> In-Reply-To: <20260206175609696_A7uH3a1F7VmQN-iTzjC3@zte.com.cn> References: 20260206175609696_A7uH3a1F7VmQN-iTzjC3@zte.com.cn Date: Fri, 6 Feb 2026 17:57: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?W1BBVENIIHYyIDEvMl0ga3NtOiBJbml0aWFsaXplIHRoZSBhZGRyIG9ubHkgb25jZSBpbiBybWFwX3dhbGtfa3Nt?= Content-Type: text/plain; charset="utf-8" X-MAIL: mse-fl1.zte.com.cn 6169vg3f001817 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 Fri, 06 Feb 2026 17:57:59 +0800 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6985BB27.002/4f6qK35vYlz5B1K3 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 Mon Feb 9 14:28:07 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 566DE32E751 for ; Fri, 6 Feb 2026 10:01:43 +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=1770372103; cv=none; b=t+448TnLa4E/wsLdPOs3S35V9h7Fp9iDmgMXAM/2g8DOFC2jifydXfaKVQw0VkE7aCxBofS5A1XCQ4yAYUVYpDo0HNHCeWn74dCIW3uaUFUEjsmOgY1omO23StXL/BjhbT+C8olxqpNj3D/qVWjQWTowu2scrXBp98vIdobzbBY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770372103; c=relaxed/simple; bh=M5nHeftkvAfUJTq+fSdpQ52Dgx1ZySXQ9L+fKLuDxDM=; h=Message-ID:In-Reply-To:References:Date:Mime-Version:From:To:Cc: Subject:Content-Type; b=CF5DBEOEoUl6SGTQbr8tBDNdQk+BQ8JxpMFbthjGFtgE7ndApd3qBSYJSJAJq25jDqFE5gWYgD2LF4i7PoS0qas0fgeSZvVxAxD5uxdWoN/YqtEVTNCsXmmd9noVJ3bSRfZlObeO+8xve3t2KAwDB5RxsQhKRN/h9cARKBfEVXw= 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-fl2.zte.com.cn (unknown [10.5.228.133]) (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 4f6qPK4PW4z5B108; Fri, 06 Feb 2026 18:01:41 +0800 (CST) Received: from xaxapp05.zte.com.cn ([10.99.98.109]) by mse-fl2.zte.com.cn with SMTP id 616A1TjN026627; Fri, 6 Feb 2026 18:01:30 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp01[null]) by mapi (Zmail) with MAPI id mid32; Fri, 6 Feb 2026 18:01:32 +0800 (CST) X-Zmail-TransId: 2af96985bbfc374-0d6af X-Mailer: Zmail v1.0 Message-ID: <20260206180132364R_lYZujwOYfhazgKmxArZ@zte.com.cn> In-Reply-To: <20260206175609696_A7uH3a1F7VmQN-iTzjC3@zte.com.cn> References: 20260206175609696_A7uH3a1F7VmQN-iTzjC3@zte.com.cn Date: Fri, 6 Feb 2026 18:01: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?W1BBVENIIHYyIDIvMl0ga3NtOiBPcHRpbWl6ZSBybWFwX3dhbGtfa3NtIGJ5IHBhc3NpbmcgYSBzdWl0YWJsZcKgYWRkcmVzcyByYW5nZQ==?= X-MAIL: mse-fl2.zte.com.cn 616A1TjN026627 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.133 unknown Fri, 06 Feb 2026 18:01:41 +0800 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6985BC05.001/4f6qPK4PW4z5B108 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 Reaon =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_start =3D rmap_item->address >> PAGE_SHIFT; since KSM folios are always order-0, so folio_nr_pages(KSM folio) is always= 1, so the line: "pgoff_end =3D pgoff_start + folio_nr_pages(folio) - 1;" becomes directly: "pgoff_end =3D pgoff_start;" Performance =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D In our real embedded Linux environment, the measured metrcis were as follow= s: 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_mm_mismatch Before patched: 228.65 22169 22168 = 0 After pacthed: 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/ Signed-off-by: xu xin --- mm/ksm.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mm/ksm.c b/mm/ksm.c index 950e122bcbf4..54f72e92b7f3 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3170,6 +3170,9 @@ 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_start =3D rmap_item->address >> PAGE_SHIFT; + /* KSM folios are always order-0 normal pages */ + const pgoff_t pgoff_end =3D pgoff_start; struct anon_vma *anon_vma =3D rmap_item->anon_vma; struct anon_vma_chain *vmac; struct vm_area_struct *vma; @@ -3184,7 +3187,7 @@ void rmap_walk_ksm(struct folio *folio, struct rmap_w= alk_control *rwc) } anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root, - 0, ULONG_MAX) { + pgoff_start, pgoff_end) { cond_resched(); vma =3D vmac->vma; --=20 2.25.1