From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796156386; b=YI/ZU3eoWKpk8BsgzjFSFa9CmjBeG8esZfmBq6pjhUf/B2yakeNvvAbEZHq1Utnhdgi0 TzScvzYM3zyNkoeyuKOFlUXreN8zPy/HZ/G96QTn7OGgjTqUwoqbhhF/BCrPYhNS2PrIh Z2C3Y0RVeXg1IkKYjGCgjKLa0zm/fIVbHhLJ7UyRebwc+iVt5rjIqbC3oO8YxoxLrgjUD yp+KyqJcBR/8gXMbcF/Lz2NMrjgUa6Lsl7PZFBrbNFjpsBUmSAF6O9eVJG+zVRJ1kg+Ke DCCy5Wtc2ka69PFoKZ9NWke1uZ09W5JOeeV0IErQmJ95gHVMiMtQQ0zl+9wVjfkNXUw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796156386; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Content-Transfer-Encoding:Message-Id:References: In-Reply-To:To:Cc:X-Mailer:X-Developer-Signature:X-Developer-Key: X-Endpoint-Received:X-Original-From:Reply-To; bh=3qTcxbFirWxF9Z6sp8s949iu8mLyAPVjtRchFNxclMI=; b=vYf4+ayshtB4R1hnaLFWZMtF3oilQe4uRNz255zODsAVZtFFkGYcyrtPbKTti+Qx6P9R 88Qz4BKxJketVNwkgzc3/sL/fhAlsrS0N+F2LcafLZza8Sc+MLEy9k+EjDplcj2IV/vB5 IYv17HgfoVcT95nh8gpNknjcs0UxHlyx2pwFw6Mw3e6l1t8zKG/KijXRcrmkgSP2iC65X 2+uURlI8VMyb22zPBXnnS3XVGqiTUNT0oYhR44fWk/IpLE8bqdq586S7a7KFvuAVGfAcD 5J6wbdLtgHMVbhwy6P3k+gvdgQPEP1ofEuCPclh6cpf9tC7WMII4Z2Sbxew1Q9yr0mQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=KlNCO5Ig; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C22F4606A33C; Tue, 1 Dec 2026 20:19:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=KlNCO5Ig Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796156386; b=sLZo/sZNgAPD/w/XijzRbxM59fx0ZG668ebEK2zu+VvZmpxaKTwxja1bquksfNVYpF1z ji016O//0YbkkqplEZjmuArcesmmvwSUfv5nii7plpvX31pIicw0c9rsOwccGyWBbMoQ/ VtG49LUfuAFTge68pYrUHiy1csBSyskXAPJGZbTSZ0t4nWj0r84zvgxbgtpikk/YykoXS CbqG9PJQrbGVPvFIf4Q+pHlobrhWd8chzBDysNkYxyAPom+Y9zRn/9rbXIqy8ccTQrjZl 8IJQRQfHUG5zxESIIZaWbT+DrBbmRqJN4daa1bOVV14SnDfBVQ4BOzB4tk2whdF4nwg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796156386; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To; bh=3qTcxbFirWxF9Z6sp8s949iu8mLyAPVjtRchFNxclMI=; b=cJYPrZGSsol2xV1jFhabgyZTTqKlQ6P+PGun219vlVmTCYaptrtOLhEfZ7qlsM0u+OGU 6ei+7bxkWSVTsDAPwFz1bwC6XjWSjDsg7pmZiu5QZVI871OjXBHe1FeLLh3b6Cms0FM0Y uVyM5pYy6MgzPBsGijtOtIo1UMnfZTU0rYOUSrNQFNCwk5A3OzZ5xrsWd9qkNrjMrhKhz XPbyNSsSYTUceEtT5nxUy01CfLZZK7WH4YReOAmHrUZwLyPwoev9y5h/tn9ktQliRO6Br Y8ryU9UIwV9oxp+0Gr4tIXGYqQFzu9BdF4L/34ggyH3TxxXhuYcKfUJ7tM51Gkwv5Mw== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 19D1C1F000C1; Tue, 1 Dec 2026 20:19:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796156386; bh=3qTcxbFirWxF9Z6sp8s949iu8mLyAPVjtRchFNxclMI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=KlNCO5IgcYWVkYRmsBZNuX1L0weJKcr6H+hVhJ1c8MpmN7EL8zL/IE7ToRKgH6Lsx /SRJ5BI6theytlb5hSDcCNZEjMp2hjNUmjxXVjX6Urv8O9//9qGXs8BYdJ6ZC8zOMa 7jmUw+cOtKA2XQDGtwS6yB/Qb2Sbj7TkEhLmnzet7eEljSrEXhGMJZHE5B0hROae9W o3fZvPTKw5D4oYntyeD/nRJFkA2wDJsAhe3c/Sy2F5FpvgR6sdB3UN94gZFb4tMEUP i+M5D/DS9oGvGBxbRTXPA+Pc/Aw1NmJDxIX7pV2ORY0/V660cEtynwd4PD+AQCvEue 1Gc1BMGIh6ABA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 31EA04F0AB; Tue, 1 Dec 2026 20:19:21 +0000 (UTC) From: Kairui Song via B4 Relay Date: Wed, 02 Dec 2026 04:18:46 +0800 Subject: [PATCH v7 06/19] mm/mglru: skip the flags cmpxchg when nothing changes X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261202-mglru-fg-v7-6-5965a96d86d0@tencent.com> References: <20261202-mglru-fg-v7-0-5965a96d86d0@tencent.com> In-Reply-To: <20261202-mglru-fg-v7-0-5965a96d86d0@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Johannes Weiner , Muchun Song , Qi Zheng , Ying Huang , Chris Li , Baoquan He , Nico Pache , Usama Arif , Michal Hocko , Roman Gushchin , Shakeel Butt , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Vlastimil Babka , Suren Baghdasaryan , Nhat Pham , Youngjun Park , Zi Yan , Gregory Price , "Matthew Wilcox (Oracle)" , Baolin Wang , Ryan Roberts , Dev Jain , Lance Yang , Hugh Dickins , SJ Park , David Rientjes , Yu Zhao , Vernon Yang , Zicheng Wang , Tal Zussman , Kairui Song , Xiang Liu , Ehab Ababneh , Zhao Zhenzhuo , Lian Wang , Joanne Koong , Kunwu Chan , Hui Zhu , Maxim Muzafarov , Tianyi Chen , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796156357; l=4140; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=V6DorPkD4F5tkYfjdiNN4Mt8zyh5JoUi9t3HbOCrHEQ=; b=1qCKma9Sjq4koXZtbBCIGq9mdhoBOcSsjDPwjudwtUuAaLNONKa2R+DYruB0RBJ/vtLDA0bPo VSx8GimnJCaCA3QJ0f0v/Uw8Fx66iJmp3QVbxNOmljuHKZ5tK7yRAK2 X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Ridong Chen folio_update_gen() and folio_set_lru_refs() always end with a try_cmpxchg() on folio->flags, even when the new value equals the old one. The locked instruction still takes the struct page cacheline in exclusive state. That is common in practice: a folio mapped by many processes is visited once per mm during a page table walk, and after the first visit it is already in the youngest generation with refs cleared and PG_workingset set. isolate_folio() and the eviction paths also call folio_set_lru_refs(folio, 0) on folios whose refs are already zero. Break out of the loop when the flags would not change. The update is complete at the point of that read: the flags already hold the target value, and a concurrent change to them is ordered after it. When the cmpxchg would have succeeded, the value written is the same, so no new state becomes reachable. What the patch drops is the retry. A retry re-reads the flags and recomputes new_flags from the newer value, so it overwrites a concurrent update with what this CPU wanted. For folio_update_gen(), a walker that read an older max_seq can lose the cmpxchg against a promotion done by another CPU, and then write its older generation back on the retry. folio_set_lru_refs() can likewise clear a refs increment that raced with it. Breaking out keeps the newer state. No caller relies on the full barrier that the locked instruction implied on the skipped path: the eviction and isolation callers run under the LRU lock, and the others (page table walks, folio_mark_accessed(), lru_gen_clear_refs()) only care about the resulting flags value, which is already in place. Note that lru_gen_inc_refs() already returns without any atomic when refs is at LRU_REFS_MAX and the folio is in the workingset. Tested on an i7-13700HX with turbo disabled and the performance governor. N processes map the same 2GiB tmpfs file (huge=never) and read every page, then one aging pass is triggered on their memcg through /sys/kernel/debug/lru_gen ("+ 0 200 1"). Time per aging pass in ms, median of 5 runs of 10 passes each, averaged over 2 boots of each kernel: processes base patched 1 32.9 31.7 -3.5% 8 195.1 160.2 -17.9% 32 755.8 592.2 -21.7% 64 1597.2 1260.6 -21.1% With one process, every visit changes the generation, so there is nothing to skip. Before the patch, perf annotate attributes 53-58% of the walk_update_folio() samples to the lock cmpxchg in the inlined folio_update_gen() in this test. Reviewed-by: Kairui Song Signed-off-by: Ridong Chen [kasong: adapted and rebased for the series] Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Reviewed-by: Vernon Yang Tested-by: Wang Lian (ByteDance) Tested-by: Tianyi Chen Signed-off-by: Kairui Song --- include/linux/mm_inline.h | 2 ++ mm/vmscan.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 48d617bb04e2..a86f680f1257 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -255,6 +255,8 @@ static inline void folio_set_lru_refs(struct folio *folio, unsigned int refs) do { new_flags = old_flags; lru_set_refs_flags(&new_flags, refs); + if (new_flags == old_flags) + break; } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); } diff --git a/mm/vmscan.c b/mm/vmscan.c index 34014a58b044..e6dd61583607 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3337,6 +3337,8 @@ static int folio_update_gen(struct folio *folio, int new_gen, int *type, lru_set_gen_flags(&new_flags, new_gen); lru_set_refs_flags(&new_flags, 0); new_flags |= BIT(PG_workingset); + if (new_flags == old_flags) + break; } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); *type = folio_flags_is_file_lru(&old_flags); -- 2.56.1