From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796321959; b=HoMhZS/Qu3eoBHXlA044lvo77pnoHjL/wJsH76ty8b7tbGgeExZDO8PTj3V8KG5tZNlc hOiBzP1Qhm5Je1obCBgCzp13jQBCxoM/D2ETWKs6ciqDCMmv44dSD2igVsaNDGfuL10r0 Fe8TdennK8CI0tdxvZE1MFge2JCV39zOxHDIxol1g3VWtvbCvCeJ3AaDePjnPbj/2FSIz UhPJXPbAGJm3RLTUdF1sb3Q4Rg9T9nmnn0EveJ+a7exT5ExcTtDO67p9vKgYHHCPWRRC9 xcPHYNAYCO/I45s9ISOvegHeVuKZJdjwKwkXfwgJq7GnXc4l68Q13AmuqwPIJJCn7Ow== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796321959; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=yX7KB9ZCH+BzpXMFYCFCHIrifBJdqex8zWLqLGupDRA=; b=biEfEYYAiL6MvRYzBALvRRdxODmi8FuBeXq9RgUkdBF1/9nutNWKom0SY8VfC5sixldO DeOHGeCrWgpI2ADZtShmo7+SxqMVqHVoA+FwYJlCAt9xXsbKGXVQB11qiSXLzlVWJFzXx mdCQczd/EB+NGrnUfhuV/RaIFViWDq9v6r3ccBp0k/PrWUGB3IxYZ5Yzy3/WsZxVTMIG0 kTdSSDXwTHWOIg1v3UBqUARtNWJPnEdS1P6MpGroXffQmA9RSDfWAlXS6pWAydFqVtOHB ElwogY1h2M08DaxDMZc7V59bQwflmtrNU+tAwqYyw8PE1XN9JY15VF9tIPoLAEZ760w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=lfxnp4tK; arc=none 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 6057C606A322 for ; Thu, 3 Dec 2026 18:19:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=lfxnp4tK Received: by smtp.kernel.org (Postfix) with ESMTPSA id D4C101F00896; Thu, 3 Dec 2026 18:19:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796321958; bh=yX7KB9ZCH+BzpXMFYCFCHIrifBJdqex8zWLqLGupDRA=; h=Date:To:From:In-Reply-To:References:Subject; b=lfxnp4tKz0Mcl/jMUiNsbbgH/0FiFl7aDQ/wBxL9XeDUzf0cjeEamjsZiLhxo2DyG dM08pY32Gjl5sIf66eonOgW35vpREPd+AvuRYnANWOlMX6S984BT2MaOB9s0xumujc w8q4JiXuYhyFK9Emb6iOwKtbyJ6+7ixxyHxNn3XY= Date: Thu, 03 Dec 2026 10:19:18 -0800 To: mm-commits@vger.kernel.org,chenridong@xiaomi.com,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261204-mglru-fg-v8-6-64f81c876c15@tencent.com> References: <20261204-mglru-fg-v8-6-64f81c876c15@tencent.com> Subject: + mm-mglru-skip-the-flags-cmpxchg-when-nothing-changes.patch added to mm-unstable branch Message-Id: <20261203181918.D4C101F00896@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: mm/mglru: skip the flags cmpxchg when nothing changes has been added to the -mm mm-unstable branch. Its filename is mm-mglru-skip-the-flags-cmpxchg-when-nothing-changes.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-mglru-skip-the-flags-cmpxchg-when-nothing-changes.patch This patch will later appear in the mm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Ridong Chen Subject: mm/mglru: skip the flags cmpxchg when nothing changes Date: Fri, 04 Dec 2026 01:02:37 +0800 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. [kasong@tencent.com: adapted and rebased for the series] Link: https://patch.msgid.link/20261204-mglru-fg-v8-6-64f81c876c15@tencent.com Signed-off-by: Ridong Chen Signed-off-by: Kairui Song Signed-off-by: Andrew Morton Reviewed-by: Kairui Song Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Reviewed-by: Vernon Yang Tested-by: Wang Lian (ByteDance) Tested-by: Tianyi Chen Cc: Johannes Weiner Cc: Muchun Song Cc: Qi Zheng Cc: Ying Huang Cc: Christopher Li Cc: Baoquan He Cc: Nico Pache Cc: Usama Arif Cc: Michal Hocko Cc: Roman Gushchin Cc: Shakeel Butt Cc: David Hildenbrand Cc: Lorenzo Stoakes Cc: Liam R. Howlett Cc: Barry Song Cc: Axel Rasmussen Cc: Yuanchu Xie Cc: Wei Xu Cc: Vlastimil Babka Cc: Suren Baghdasaryan Cc: Nhat Pham Cc: Youngjun Park Cc: Zi Yan Cc: Gregory Price Cc: Matthew Wilcox (Oracle) Cc: Baolin Wang Cc: Ryan Roberts Cc: Dev Jain Cc: Lance Yang Cc: Hugh Dickins Cc: SJ Park Cc: David Rientjes Cc: Yu Zhao Cc: wangzicheng Cc: Tal Zussman Cc: Kairui Song Cc: Xiang Liu Cc: Ehab Ababneh Cc: zhaozhengzhuo Cc: Joanne Koong Cc: Hui Zhu Cc: Maxim Muzafarov Cc: Aveline Noir include/linux/mm_inline.h | 2 ++ mm/vmscan.c | 2 ++ 2 files changed, 4 insertions(+) --- a/include/linux/mm_inline.h~mm-mglru-skip-the-flags-cmpxchg-when-nothing-changes +++ a/include/linux/mm_inline.h @@ -255,6 +255,8 @@ static inline void folio_set_lru_refs(st 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)); } --- a/mm/vmscan.c~mm-mglru-skip-the-flags-cmpxchg-when-nothing-changes +++ a/mm/vmscan.c @@ -3337,6 +3337,8 @@ static int folio_update_gen(struct folio 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); _ Patches currently in -mm which might be from chenridong@xiaomi.com are mm-memcontrol-drop-unused-vmscan-tracepoint-include.patch mm-vmscan-move-vmscan-tracepoints-to-a-local-header.patch mm-vmscan-move-struct-scan_control-to-a-dedicated-header.patch mm-vmscan-pass-scan_control-to-the-vmscan-tracepoints.patch mm-mglru-add-tracepoint-for-scan_folios.patch mm-mglru-add-lruvec_gen_size-helper.patch mm-mglru-add-tracepoint-for-aging.patch mm-mglru-pass-a-seq-range-to-lru_gen_add_folio.patch mm-mglru-deactivate-anon-folios-into-the-oldest-generation.patch mm-mglru-rename-the-generation-offset-in-lru_gen_folio_seq.patch mm-mglru-skip-the-flags-cmpxchg-when-nothing-changes.patch