From nobody Tue Nov 24 11:35:59 2026 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=1795459993; b=2mPffEmPGTrv1ZmYvz9CRsLiFAZbRt5mmGlsVfaBgetYO/0S3yWoOfjO5MXZnZy8/Jjk 6YpiHQs6QvIakgPGsJHT4io+DdP9+9ntF7Y61bij/WODFuhFXSzEDxVBR53c/zQXXIpQo Qblpe19sWgtec4dih9lfZ0OCXTASXA5G5BqUVkQ6lmMi7sMH1t/lowW7YOhsINAANDRJ2 FN86S5Z+wDq3u3MMSlOA4clsCJ5dCdZCxpcnki6GIvV6RgnHlALXvNYCe1Vj4LvOX6zEC JWHNLCTW38ru4E6SZjfctQ612LBcNM4UV34TmhzhwGvJ768CFdhUFumXfsZzND9XzFQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795459993; 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=1rCE3K77C597tnpDa8DpHCzRg0yY18+BUyGptbSaO8M=; b=CeEx6NvagnKY4ElUH/EHG8jzXwj70iwvWB3oyIHQs61Z1T7YR58fFnaZBH5qgxmNY+w7 IsXt4v2pJcd3b6IbHfoYPaNE33tdCE54IHZ1A1CRuyMFiU1W80T35IWzN1NW3QchPAnyT iGHIww0LhBLpOhNkOK5jQjK/lgLqOnDZ+9to8uKTJ6N7et/bb+rFCGaqlI3OB+XidhEIp GSZw4SXsOss1tqMM0vs7mKjtAC4vcNvPchuOvERm10CjPe5w0zncqQFWq+Ado4PbiQZ2z TSUAMoZj8ZYXnrcv3wlLk8E8oMeD1ax31hFLHVRTYJwAapGysrDW9P9SRxuLHwsMJUg== 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=SoqzW3ro; 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 B9890606A32D; Mon, 23 Nov 2026 18:53:13 +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=SoqzW3ro 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=1795459993; b=bqhDcFLRttrhaMfzhfYIqqYHF5vH8qS8rizifTObbzmnr8Twidvfh+9RKu8JGW1GidIJ cN3OYmTWCmlIEbx2kYwS6cq4tr9kc7lHt6u8UZQEIChcXPBjR7txPL9gpV7onNF+QmkBK TKu6wpppshW/Rv0pOxCP4t4YeWf0un3HmZ3VRG+1ZtfnmTj3ubVNkm7MvSeSMD9oCwgzl tt+fGQlP3zdaAuzlGimbkjkGuvq6UgRAPLlhEnL0h7O3kQpuBXiFL/weCzLVWh3Xz7MOl qKxp/hFU/rPl4n8T9b4X/5+RS/EyqxVYCuTF8WmveDMemvFogIcuZ4p/cO5fuEUx5Lw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795459993; 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=1rCE3K77C597tnpDa8DpHCzRg0yY18+BUyGptbSaO8M=; b=Kl1dxIjc2Rb7fApi7pkPOXKg2BpbGwenatCX5e1jveIyRQUiiRaOytM2mn+C0ct6Mept S9Yi1/HEth80DAZi3Xw8R7pzKuGEDE3xUuMF8jSqqB/MCj+BRbR1btD3M+V+nuBST8AUp QLvv+AauNXKF/NNfEENQWdpUkofnGliwE/2Gacm3ds2bTQ/FndEwOGZpikCOiPwHyPke+ gQ+sFYEKUUqK+SoOEfcJrD6I+ZgvTXYrtXMZj5+zIhNeSnmVb0zJRdwouTUaEtQ7f03Lu 9rmsQGVv5/+Px8J6XjqptZoy8AoBKkB6mNDSfDdKZht6/uq9V8rBpdfkwO3kcSuQJaw== 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 933801F000E1; Mon, 23 Nov 2026 18:53:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795459993; bh=1rCE3K77C597tnpDa8DpHCzRg0yY18+BUyGptbSaO8M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=SoqzW3roBfAZ3tFOA8zhqdBmWzG3eJAbx5u8uC2YIErbKuw1WXJ/ttU+Qbm834M4d ZKeKcQo9nFKaXf4YJiqWwZ/tAx/WQxJ/VNNXNVOA4HWlAh788Hw/6pzYGUEIj276ND uzEGNMsf4058YXwvP8xPwoYiVKahcOGk5QpAIEVXeuBzcScj4qxM3Gbej3sdnz5Pdq OmFeDhgkJ6ANMmp1zAk0agWA8XEm4E4fch6F4sgt30qzHgbACgq2l043J2ZSPj0wcG TNjqyegDb3374F+bSHabFNNzzXSkUR50z1THb5LZNSwi4165e4eg/vFeQFkCI8pZEM ke0l6ojUH+zhQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 19F304C61E; Mon, 23 Nov 2026 18:53:00 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:52:53 +0800 Subject: [PATCH v6 01/18] mm/mglru: rename the generation offset in lru_gen_folio_seq() 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: quoted-printable Message-Id: <20261124-mglru-fg-v6-1-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=2457; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=on9PEefPrnrWULo5N6A+huTmLVTp8DOK4KQGDrtfCuk=; b=k5w2EsjoX/bzOEsAS9dWQMREbqipjNq4MTFKSvXR62sapovupL7OAfzxXHx/FEdKFSDvVsq/m LPR3LW9KAAICkw7ayuMLIbGlQoHMlZd+Jb96r6pjoa0gTwy/SGGSIPh 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 lru_gen_folio_seq() computes how many generations below max_seq a folio should be added to, but stores it in a variable named "gen". Elsewhere in MGLRU "gen" means an index into lrugen->folios[], i.e. seq % MAX_NR_GENS, so reusing the name for a relative offset is confusing. Rename it to "age", which matches what the value means: the newest generation is age 1, and a larger age is an older generation. No functional change. Signed-off-by: Ridong Chen [kasong: dropped the comment table part, the series rewrites it] Reviewed-by: Barry Song Reviewed-by: Lian Wang Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) Tested-by: Zhao Zhenzhuo --- include/linux/mm_inline.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index f6f4bc9a704f..2395ca8ee305 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -302,7 +302,7 @@ static inline unsigned long lru_gen_folio_seq(const str= uct lruvec *lruvec, const struct folio *folio, bool reclaiming) { - int gen; + int age; int type =3D folio_is_file_lru(folio); const struct lru_gen_folio *lrugen =3D &lruvec->lrugen; =20 @@ -319,17 +319,17 @@ static inline unsigned long lru_gen_folio_seq(const s= truct lruvec *lruvec, * |<---------------------------- MAX_NR_GENS ---------------------------= ->| */ if (folio_test_active(folio)) - gen =3D MIN_NR_GENS - folio_test_workingset(folio); + age =3D MIN_NR_GENS - folio_test_workingset(folio); else if (reclaiming) - gen =3D MAX_NR_GENS; + age =3D MAX_NR_GENS; else if ((!folio_is_file_lru(folio) && !folio_test_swapcache(folio)) || (folio_test_reclaim(folio) && (folio_test_dirty(folio) || folio_test_writeback(folio)))) - gen =3D MIN_NR_GENS; + age =3D MIN_NR_GENS; else - gen =3D MAX_NR_GENS - (folio_test_workingset(folio) || folio_test_refere= nced(folio)); + age =3D MAX_NR_GENS - (folio_test_workingset(folio) || folio_test_refere= nced(folio)); =20 - return max(READ_ONCE(lrugen->max_seq) - gen + 1, READ_ONCE(lrugen->min_se= q[type])); + return max(READ_ONCE(lrugen->max_seq) - age + 1, READ_ONCE(lrugen->min_se= q[type])); } =20 static inline bool __lru_gen_add_folio(struct lruvec *lruvec, struct folio= *folio, --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460003; b=DoXnCWZ+fDTLw7dd21piln8GdMqJsSxFDRZRQgdGWcvKkIlbs5fZD18+5Y3GzvEjccNc We+ZFv6uD40TFc8sMxZKAX4kkI8D6mNlTVM8cM/UhwHdbMtUf8USmwThwhn3vqSSe9/Td /5n4lfHPaxqZPqNL2c9sqPNgVKnLPqAXN7KQnWIeCq9mQNTQegxmdrs9suSjszs9aKYMf Ms9slqkav8hzy+b8stVku5GCNggY4Tai/eXoE6hJ0dW9mp8+UeIAHALfM+ay5PAJSZggJ /m+2tZvRFw+3e7ewcjhcOkWetAMytMiGoqI4EcvGbiclaO0ftHMfJoFFzU9hFMj7nYw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460003; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=asmN6q2HefJieUr+/zgw0602de6l9KxgRm2+Pk/d8bI=; b=0u9xaINXj7BMmDOxZaojon907Z9/O63gnZt12dYozB/GkDsNyZ4jof5kcZvRPkEtvv/3 Oj3t/qiSF4xA2KNRg0clzjjF/gSYQyzEkUK3IdpGk7yvaKFseHtYwV4Mt7/KSv2EWYIOA k5cjefQA/l5HWCg1GPAqHmDZ/kLjIBLYrCbnO5jTBefrHpiWR+bghGoK+X33nDoL9rXnV zs7iJgj9fQ6OSHv7/NogHihPfyix3xXPd3OZBuSWudAutgp4RRAZM4/D1HWFBPoVhaZ2L +adKCjm51azV4brUH4wX6Knne7MHN64Al1QxLH6S2G9SQDp1CB3vzqJgA8H4ocRTh2w== 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=JgmGfS2+; 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 1323E606A31F; Mon, 23 Nov 2026 18:53:22 +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=JgmGfS2+ 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=1795460002; b=wvamGluqC1VJF+/tu+O4Fim+yO1VBV6qnb2hooPY3Xrvy/Nm8O3UkGaoxoAxRlffcx0n r5I6hen65bkL/vHenqFzcfH1Y3kMaWAy1IvxM3iBexXlEFbh2MsKwzoaHzXLKspRsH+6K +3MZjf6Qc08T+RPNQisv8kNo2IHaNWFekGrrtBFmGky5PEpdsSkPABBsQ+OrX5wEdLFVM vdegYK6TMZcqv7QaXJvkNC4ScPjSmszKigKDaAitPjHkenEIIMNLpqh2FiWvGT9J7s0i7 Jv7XOvzv/CMive01GJ6bS7HS3Mq6Ml7fUgwuITrAKrQEXZZAVuvt1d3GNEecLNmRKaA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460002; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=asmN6q2HefJieUr+/zgw0602de6l9KxgRm2+Pk/d8bI=; b=SklfuEek9Me7pHIuqNTj0K6oHXNa1l65gwXiZK3Q86z9Mv0WVwnrUmBiUjh1Nfqm2FW6 UXO3HRIwbTPbZbRqom68VEr4mXCYGbCTZabmjyCefdcX2s3KGhwFQ73NXr1Of8VLi/RSd XUDqBWYjWEDySO2HXrPw9Wm7CThQ3hkRmf0WKbeoQR/PCjTzGtPmu4s3D8NL1RIdFOXL7 yKZXx1wshnGxk8aiUWHk4ug0Xry5akq4l2gQTiTzEjoXW/7/jgQ50FMpL7hopHbvJ3jJU 3XZUKWZXLvdDIPFOJy54TzLrFY8ZMyGgn6cmnx+w6r5KT3hcvQxLMfQpDDJUCSaLRGg== 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 C1F671F000E6; Mon, 23 Nov 2026 18:53:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460002; bh=asmN6q2HefJieUr+/zgw0602de6l9KxgRm2+Pk/d8bI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=JgmGfS2+TQqDRmOj5Q1iss+69zogpQKe+CWz6A5MRLf3GGpR4kKKvRRVFuLkE8OpK q6Odfqwail8d5V10tsAX51wKxSkZMlJTF4KLv6qRlk+BqfMAUM0CNmzAIlHt68AbrH agC4mJOp5dnqK81TjdCwJM4Cz6LRATChy6tdU0nlhhCxnahxWGeIlwR+ZiYK02RqOY MQjxQjUcTLFiBhXWbYa2pCB7UOalqVEzSCGOp7xJLDDbZ8e+vm3LKLgy+jrTCZzt23 SOytOR0m2zlF3DsQxL754ECoh/QjUMMYC39fMgIsE89OCI+uOXpFAT465Ss94sgMCx 5TEzwggcG+mgQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 3A66A4C631; Mon, 23 Nov 2026 18:53:00 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:52:54 +0800 Subject: [PATCH v6 02/18] mm/memcontrol: allow update of LRU statistic without holding LRU lock 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" Message-Id: <20261124-mglru-fg-v6-2-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=1434; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=uwS4fBCD5iPVACjO21QQfl2NP0AqWyZwBPx9Plu/+9U=; b=2pyXTqy8HzStaaOCjTzObU1npNvY6ZTpilr+RXfXelh2BoV3pXDzkQDh7YtNKuBinOgbf7HIr Mou6SAdaUjTCLlAY7r+pWaP7/QlkLcjr09/iTRLHgrkUcRHYDfXKH4T 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song To enable moving file pages in folio_mark_accessed directly and lazily for MGLRU, allow updating the LRU statistic atomically without holding a lock. It may cause temporary counter underflow, which should be fine as the counter is eventually consistent and only serves as an estimate. Reviewed-by: Barry Song Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Acked-by: Muchun Song (ByteDance) Reviewed-by: Lian Wang Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) Tested-by: Zhao Zhenzhuo --- include/linux/mm_inline.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 2395ca8ee305..2edb9beef6d3 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -47,12 +47,11 @@ static __always_inline void __update_lru_size(struct lr= uvec *lruvec, { struct pglist_data *pgdat =3D lruvec_pgdat(lruvec); =20 - lockdep_assert_held(&lruvec->lru_lock); WARN_ON_ONCE(nr_pages !=3D (int)nr_pages); =20 mod_lruvec_state(lruvec, NR_LRU_BASE + lru, nr_pages); - __mod_zone_page_state(&pgdat->node_zones[zid], - NR_ZONE_LRU_BASE + lru, nr_pages); + mod_zone_page_state(&pgdat->node_zones[zid], + NR_ZONE_LRU_BASE + lru, nr_pages); } =20 static __always_inline void update_lru_size(struct lruvec *lruvec, --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460003; b=uG+arzzinqUBYv7UfwWHxX1vYxsJ/Nd+iNBRuGvMbS5PsWDFo4TNmAHkJdtX3orL4T7w Eq52dyK4SaBPKtTipLDzNt2dWELrvSG0aOS1UW4OGGGf67P5zB/dqOjxZlTTUi1bfyTjf rL9FCCNgL3km1TfOFs5oa5nWHRoi/rL9x6CL1R1UbkmcFwzO+8r7Si2m+OegGrjEuZeaG 54XQsQ1rcKVRFk1JeCf5XapN95Y76sS+kFdKnTjUaiiJMRb+xtAUz48wJISUv9SwpRNBA 2W6zBkSzVeoolD/HXTDpE0Q+oXhmcoBqErIZ30O3tyvZx7GjHkqnmBsYvkzLVNOpzaA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460003; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=+wxKwr0AUBQ7HPKVFsvVRhysvOdPNYac/4XTxhyTbxM=; b=HXHsqtc2dqyBFZGEgzRdPPmF1XvnPOaG5PzSEnm9j8uF+46uHTOeNOIk7VKONFvwMcX0 EsSFuvQojt3PtX2QW9Z6QOgQfwGZ1q+3iwFcz/iGpEEb5HelLPCtBOFn7JZoLz41W6bc9 HyDbD4M73obVv2jk/3ve+K8ltl2pFCDK9tQAYtaoM7u8QvIW6T/Qo9hIJiDKXVgeKnepB BNWwMPWsttXXPncIMjH9OybG2f6SMiGCef9ONK7r5j7S6dCe1lQvp8Htt7+dVXLreoTmp YvOYdDdwFvjiqeduc1tQcUVRNUv4kypNr5XxO8VXFK0rTOmpKmgCP7KMmYPvJkW7mvg== 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="hZm4Gy/C"; 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 1318C606A31E; Mon, 23 Nov 2026 18:53:22 +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=hZm4Gy/C 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=1795460002; b=jbE5KYlXo3tTDI82I9lCHbVI432dR433YtiHse6vLoCtf42Ydr6n3VMFlLdeO6PFLJen aJpbu9/XGFAjM2aI3v3fFiDr8rS89gumGpv1smBjYblPeI3bAy1JoraiRBP46zgOw1KBd p1TxLc9/HovtirSL/RuhIHMbn5kuRxAKOHpmWkA3/UEFswB6b1EX7KrtKrmxzzL32fG9j gVZnFY9yeKfkxl4auCg1ddRQ3/mqvAnbDMVv3V/l2HTN/OVfqvLwTqHhVezIKinfnvxBd 8S9psTh+o1foVRVWqHrpCMa9/jbGPWVddakS3ZpAz1QojvoAIO0aIJvrT8aV9T+DWbg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460002; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=+wxKwr0AUBQ7HPKVFsvVRhysvOdPNYac/4XTxhyTbxM=; b=29BVlU2JunP+b+3Jzgz6CPAEXzXJwaDGBRUIbLUx7fCc07yh4Y1zwBH5ITp/7gt3/FC/ 22qfW2kT8SvtmcTM7FSFtfJxjEfm15X/uyt5TTYnrDyhX1NbSmXLjDKotCA8t8Fd3DCb6 iTA8oV/27VWWlavp7EhYYLWG2YFbdhOI8fPpZO6a7DZ1GRMhkLgx9OttajYsR2I6vDJ08 +VSf2EXkAszsCkxz/SbQ0JHHW5he8kt45xof9/vngY+TsFkHAptlX7/MqqfbNh5k3ddgy aUCKgO+FMWP7GHrJVh9RhkjVNzdwDltKgPqzFwjEKLzxo3NK2YgUFytWQbs5G395iAQ== 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 D22FE1F000ED; Mon, 23 Nov 2026 18:53:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460002; bh=+wxKwr0AUBQ7HPKVFsvVRhysvOdPNYac/4XTxhyTbxM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=hZm4Gy/ClI8J1x6AEuTLrcVAreI8T2KcHDqTKwIGHndDRltgDm57JjzZxk4UvcdSw WoW0rm86cKCHZ6ibPv/C4efJcuE0h4zLTWTrL6xIGvOdM+w2UUCkCizFkzXRWiRc/D YZrWalR1/bey0p1noYwS/U0Nv0diHeYjPJ8EIUKUTHrJKkKpB2RP/r4oElr8FqJGvg uz6zFjIUqUYR3vqlU2rQvKOGqqc8G7zLvpl8Qc2m4pAbeOA/8inbLHJtcHVdIxMo6A bRMLMvZP5Rz6HOfI/8XDJvVPwpsirk9Vv3zOXN09mK4Br/Uw/sA+TOVoInugWJaYYb PvF/kMHeAVZow== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 5A4E44C635; Mon, 23 Nov 2026 18:53:00 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:52:55 +0800 Subject: [PATCH v6 03/18] mm/memcontrol: avoid potential counter leak when reparenting local stats 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" Message-Id: <20261124-mglru-fg-v6-3-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=3407; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=DITLxusEF3jFekXchW/A6iHzf//ot6gPbXly+2fd+cQ=; b=oQEvwa9i+84qyXpmUPi7pXNiCe/5dSBG7wL+wB1oC2sXK73KSejhjUMAwogTXAJ3+Pcs3FOdO fyAevJbJU6HADPweDl0ahO3Xr133F1H4Ez41aJ+ynn60czLlmD8SsmG 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song lruvec_page_state_local() clamps negative values to zero on SMP. That is right for readers reporting page counts, but can be wrong for reparenting once MGLRU updates the counters without the lruvec lock. Reparenting runs after the forced stats flush, so a negative is a real balance that has to move to the parent, not be discarded. We will introduce lockless MGLRU promotion which may cause following problem: 1. A refs CAS on a folio on the child's lruvec moves it from inactive to active, and its active/inactive counter update follows as a separate step. 2. A concurrent isolation of the folio reads the folio as active, debits the child's NR_ACTIVE_* by delta before CSS_DYING is visible to it. 3. The child is killed, and the two __update_lru_size() calls from step 1 land on the parent: NR_INACTIVE_* -delta, then NR_ACTIVE_* +delta. The child is left with NR_ACTIVE_* -delta while the parent holds the matching +delta, and reparenting the child's raw value is what cancels the parent's credit. However, if the child happens to hold few enough folios for that counter to be negative, lruvec_page_state_local() clamps the child's negative to zero, which drops the debit instead, so the parent keeps NR_ACTIVE_* overcounted by delta, and its active + inactive total overcounted as well, permanently: the child's local counters are gone once reparenting is done. So use a raw signed helper and reparent with that. Ordinary readers still keep the clamp. Reported-by: Kunwu Chan Closes: https://lore.kernel.org/linux-mm/CAN_Opa-+nb0vxQ+HsENCG2fEEMZoxG9Sy= _DBC4CLkWjNLzDWKw@mail.gmail.com/ Signed-off-by: Kairui Song Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) Tested-by: Zhao Zhenzhuo --- mm/memcontrol.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 091401cecf39..3f551e2e3206 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -562,11 +562,10 @@ unsigned long lruvec_page_state_monotonic(const struc= t lruvec *lruvec, return (unsigned long)READ_ONCE(pn->lruvec_stats->state[i]); } =20 -unsigned long lruvec_page_state_local(const struct lruvec *lruvec, +static long __lruvec_page_state_local(const struct lruvec *lruvec, enum node_stat_item idx) { const struct mem_cgroup_per_node *pn; - long x; int i; =20 if (mem_cgroup_disabled()) @@ -577,7 +576,14 @@ unsigned long lruvec_page_state_local(const struct lru= vec *lruvec, return 0; =20 pn =3D container_of_const(lruvec, struct mem_cgroup_per_node, lruvec); - x =3D READ_ONCE(pn->lruvec_stats->state_local[i]); + return READ_ONCE(pn->lruvec_stats->state_local[i]); +} + +unsigned long lruvec_page_state_local(const struct lruvec *lruvec, + enum node_stat_item idx) +{ + long x =3D __lruvec_page_state_local(lruvec, idx); + #ifdef CONFIG_SMP if (x < 0) x =3D 0; @@ -595,7 +601,8 @@ void reparent_memcg_lruvec_state_local(struct mem_cgrou= p *memcg, =20 for_each_node(nid) { struct lruvec *lruvec =3D mem_cgroup_lruvec(memcg, NODE_DATA(nid)); - unsigned long value =3D lruvec_page_state_local(lruvec, idx); + /* Reparent the raw value: the clamp drops the child's debit. */ + long value =3D __lruvec_page_state_local(lruvec, idx); =20 __mod_memcg_lruvec_state(memcg, nid, idx, -value); __mod_memcg_lruvec_state(parent, nid, idx, value); --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460004; b=j4GP8/ibE0cC6ZW/ZONVPIz2gQKjW/msVI2aZHIE/Flv6GCJFHe3j8DSMISkkuFS6hRD 6jsAdQe3XBZVAJeyXKVRp2BnNEo8HnNa5xrjpKCb7p77lolj7Fej25wafUzfZYifPeuO2 goAEvZqwNuSAsmA92l0np7kZb2Dvdn/D1Q5Qsr7QUBoxLB+YAfBexD8Q3h3BOXSJ/JM2J b5zi7oZK7Bq5x0qRVE0gcAUfAhYqkiwOGJa4VHmVw9itIzDbO3fIVtRXwovKfpYN6Zhsb wS4u5LEf5kr9TuSxRC++rXAdR74JNIoAQR8CYLMJl+vVwbVSBsZqd1lJ3fdbKZQu6pg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460004; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=tn3ff8YYl9RnjGLJDIERe0STPUTUXTlN6iqTakkHSLE=; b=oNMG9oObE9XY1DKcK0selV+AVs2C3/fMDEvwKOm33boOFpJJC/hO8pcwkKOoL0wbWC7H EegvZ8yGO2eupglz/9bp41oDXqcp8t1VSyGHmtKryx5IYuW3rmCEQRbZ+BZsnCwCtYhXq K3IfQ6L2Kx0/wV4y4JMVaYvgXhQwGUsQPkRvYpQUOSCdzkKrpsK1JJ1wnIv2sCteA4B7f FtMhyj7piNDthm0ho7yW/lbu6vRwauBuEDiRVcKcb4GlCWkeOqt/cNiiLJlYnKgTf5YBY tPe5jKgZ+2EQ72Jv9kFhjXG8S6TtLDjPlfT6+K69Tf25iY3iwjqXh8mnIbdc134nPgw== 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=VorczYGR; 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 DAC35606A33B; Mon, 23 Nov 2026 18:53:24 +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=VorczYGR 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=1795460004; b=2ODKVInBfkV+mP5aiGpHMLxqGd5nNMoscnvCVCM/4imivTDL6dmr0Hbap0a9sH5K3UsP iI8XQ5HNFa2wvA+JiX0Cpt7EDk7X2WJd5CmOb01hdcrMk67zchpJB+EVI9yAKN2uqbK+n /TK2C02Eq7qQMdAKrfAdLz0fnAaYnKB6n5AqymdLN2x0uB0NIOL4vA9bWP8GzUAOZloIh uARe4mIujNoQqHa5aHh2ocYvmlGt2aCI7R9+58y5QUC//1RC1TJNM9KmA/lbduzrRpezk xBSmx1XMG2CduAFJsMpwSKt/mTNRKntDdF+olk+hvrPHUTlOkz0GYyHdTA3bAUBkcAg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460004; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=tn3ff8YYl9RnjGLJDIERe0STPUTUXTlN6iqTakkHSLE=; b=XzJB4remu8yG2ogETPUUTKAHQWKfXRsPEn2mb6QQy2ATb//fxBHBmsKlq7YDu0lJpqBS DTK+K/83Wa7Rr5fZ+ip5CXu9CBD8gTVVTfEH8BTURyOGlWiheClEIo0ZNOFqKGFGxGcoB 4EO1VUZxDKLIZMu1VFq6GsDQIyMne6i24FHGBrTAXM/BRsS0RAikNTS4EVrh35c8yYMoA rKVRXCzXZ7z4xzoiDnPjpNxEUzklIJj70QjSlpm1M23tXhnuY5qAlyUcK9xTeNscfA8Rt F/0Mmb1vXHcVbcMOPRhxHYzojY+k2aehYq6WLlXAGJUW6P189I/idZI5L3n4BHBipMg== 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 4C5951F00A03; Mon, 23 Nov 2026 18:53:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460004; bh=tn3ff8YYl9RnjGLJDIERe0STPUTUXTlN6iqTakkHSLE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=VorczYGRu8SCi8mm4gaolJRDqdzYs/nJvY0Ncxs9/gEmJtOqH32VT7QDg+noUHkOW Q/COuC6kQ9Z+Qx1MxzyA7KOSUAEBlGQizCCv77IhVdxunqKEGJ7wbJP6KZtOXVRvqu 2MpL0Hp5gWrRDtEvDkjF/XkaJt17STnFHV0GtSfDQsDwUC9EpUsEDuxkosIyvvwGAK I1kShB5vP+dQFLAr4GiyTgN02Po46dp8+xe6mjanijcRycX8VinLqhnm8nuAr9gAlY lfF0J42B60AD5qs4qeX0R5dpXqxxqIELQf/xLPACdZwJD8jyeUafxOkewRoGGnHpCa SY+crkgTvSbew== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 7B3194C639; Mon, 23 Nov 2026 18:53:00 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:52:56 +0800 Subject: [PATCH v6 04/18] mm/mglru: make generation page counters atomic 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" Message-Id: <20261124-mglru-fg-v6-4-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=5768; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=Vglaw21Vm4r80ayuV/+BBbtCvB3+vEXOhf7b40sCexI=; b=Ck9hVXdiDJT21PdrgJnuB2unk1HBDNmb2YyAjDOuLd2atdc8hN1vUvyXwNsOsOO7KJ3r1Cb1r ETM8FJPd4GWDmWtXv1L7n8ZosAWgDSifBU53dGsNoO+QuUZwuS3t+Kv 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song No feature change, convert them to atomic so we can update them without holding the LRU lock. A temporary underflow is fine: readers clamp a negative value to zero, and the counters only need to be eventually consistent. Tested-by: Zhao Zhenzhuo Reviewed-by: Barry Song Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Reviewed-by: Lian Wang Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) --- include/linux/mm_inline.h | 6 ++---- include/linux/mmzone.h | 2 +- mm/vmscan.c | 26 +++++++++++++------------- mm/vmscan.h | 2 +- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 2edb9beef6d3..46beded51c7d 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -265,11 +265,9 @@ static inline void lru_gen_update_size(struct lruvec *= lruvec, struct folio *foli VM_WARN_ON_ONCE(old_gen =3D=3D -1 && new_gen =3D=3D -1); =20 if (old_gen >=3D 0) - WRITE_ONCE(lrugen->nr_pages[old_gen][type][zone], - lrugen->nr_pages[old_gen][type][zone] - delta); + atomic_long_sub(delta, &lrugen->nr_pages[old_gen][type][zone]); if (new_gen >=3D 0) - WRITE_ONCE(lrugen->nr_pages[new_gen][type][zone], - lrugen->nr_pages[new_gen][type][zone] + delta); + atomic_long_add(delta, &lrugen->nr_pages[new_gen][type][zone]); =20 /* addition */ if (old_gen < 0) { diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 6a2ad0cff795..4e85bb520f76 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -554,7 +554,7 @@ struct lru_gen_folio { /* the multi-gen LRU lists, lazily sorted on eviction */ struct list_head folios[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES]; /* the multi-gen LRU sizes, eventually consistent */ - long nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES]; + atomic_long_t nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES]; /* the exponential moving average of refaulted */ unsigned long avg_refaulted[ANON_AND_FILE][MAX_NR_TIERS]; /* the exponential moving average of evicted+protected */ diff --git a/mm/vmscan.c b/mm/vmscan.c index bc7579d47f9b..202e0f874b23 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3350,8 +3350,7 @@ static void reset_batch_size(struct lru_gen_mm_walk *= walk) continue; =20 walk->nr_pages[gen][type][zone] =3D 0; - WRITE_ONCE(lrugen->nr_pages[gen][type][zone], - lrugen->nr_pages[gen][type][zone] + delta); + atomic_long_add(delta, &lrugen->nr_pages[gen][type][zone]); =20 if (lru_gen_is_active(lruvec, gen)) lru +=3D LRU_ACTIVE; @@ -3971,11 +3970,8 @@ static bool inc_min_seq(struct lruvec *lruvec, int t= ype, int swappiness) break; } flush_lru_batch(head, &batch_end, target_list); - - WRITE_ONCE(lrugen->nr_pages[old_gen][type][zone], - lrugen->nr_pages[old_gen][type][zone] - delta); - WRITE_ONCE(lrugen->nr_pages[target_gen][type][zone], - lrugen->nr_pages[target_gen][type][zone] + delta); + atomic_long_sub(delta, &lrugen->nr_pages[old_gen][type][zone]); + atomic_long_add(delta, &lrugen->nr_pages[target_gen][type][zone]); if (!remaining) return false; } @@ -4093,8 +4089,8 @@ static bool inc_max_seq(struct lruvec *lruvec, unsign= ed long seq, int swappiness for (type =3D 0; type < ANON_AND_FILE; type++) { for (zone =3D 0; zone < MAX_NR_ZONES; zone++) { enum lru_list lru =3D type * LRU_INACTIVE_FILE; - long delta =3D lrugen->nr_pages[prev][type][zone] - - lrugen->nr_pages[next][type][zone]; + long delta =3D atomic_long_read(&lrugen->nr_pages[prev][type][zone]) - + atomic_long_read(&lrugen->nr_pages[next][type][zone]); =20 if (!delta) continue; @@ -4625,8 +4621,13 @@ static void __lru_gen_reparent_memcg(struct lruvec *= child_lruvec, struct lruvec child_lrugen =3D &child_lruvec->lrugen; parent_lrugen =3D &parent_lruvec->lrugen; =20 + /* + * The child is offline and the lockless updaters, which hold + * rcu_read_lock() over the update, avoid racing with reparenting, + * so the read/set/add operations below are safe. + */ for (gen =3D 0; gen < MAX_NR_GENS; gen++) { - long nr_pages =3D child_lrugen->nr_pages[gen][type][zone]; + long nr_pages =3D atomic_long_read(&child_lrugen->nr_pages[gen][type][zo= ne]); int child_lru_active =3D lru_gen_is_active(child_lruvec, gen) ? LRU_ACTI= VE : 0; int parent_lru_active =3D lru_gen_is_active(parent_lruvec, gen) ? LRU_AC= TIVE : 0; =20 @@ -4634,9 +4635,8 @@ static void __lru_gen_reparent_memcg(struct lruvec *c= hild_lruvec, struct lruvec list_splice_tail_init(&child_lrugen->folios[gen][type][zone], &parent_lrugen->folios[gen][type][zone]); =20 - WRITE_ONCE(child_lrugen->nr_pages[gen][type][zone], 0); - WRITE_ONCE(parent_lrugen->nr_pages[gen][type][zone], - parent_lrugen->nr_pages[gen][type][zone] + nr_pages); + atomic_long_set(&child_lrugen->nr_pages[gen][type][zone], 0); + atomic_long_add(nr_pages, &parent_lrugen->nr_pages[gen][type][zone]); =20 if (lru_gen_is_active(child_lruvec, gen) !=3D lru_gen_is_active(parent_l= ruvec, gen)) { __update_lru_size(child_lruvec, lru + child_lru_active, zone, -nr_pages= ); diff --git a/mm/vmscan.h b/mm/vmscan.h index c2cea36289f3..6551f0a6691a 100644 --- a/mm/vmscan.h +++ b/mm/vmscan.h @@ -126,7 +126,7 @@ static inline unsigned long lruvec_gen_size(struct lru_= gen_folio *lrugen, unsigned long size =3D 0; =20 for (int zone =3D 0; zone < max_zone; zone++) - size +=3D max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L); + size +=3D max(atomic_long_read(&lrugen->nr_pages[gen][type][zone]), 0L); =20 return size; } --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460014; b=YzU7xY/vxsKW6HrnXyw/cTgZ/XoA4z8cCQ3HQqrK/g3JGnSakEBUYxaVlvXrRR+m0X1R iJZyax9Fgl1Bcr+bVNPzFCQB4lYJbi+JlfrCfsGVnWUb546SirO8W04dnk3UzJe6vcQh5 SMxG2XY9uPji/1kKYPbkviqDgrfrti3F0mNxISC22ifwBprEyS3H/Zi8WaPRNcV4keIW2 JYJrN7x3b60gIPhMrLzSGNzCy2I57no/Q+xxSb50QUVUbokkObAjWdXsne9BzyJ3Z+7c3 OHZxGtSKz3MrT7o3n8ctJMxK2aJeo5fBxmYRXgmAtPFNOTriXGSHW2BMWdZnzBZ3+sg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460014; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=Y9oObqSSiDMafmKBBZklAmQlgVzGVcaYHm/4oLczkO0=; b=bhTLne81jmKQGs6M//4DlcmWpNI4MLFmDUpL6fhyRO9tqoB8xUr6WvTAi3HNB+0/HR9w Sn6lw4RRDE8pnrMS9TjPemtm06gpd5y67JZT453q9t4OR33ZZ3ZWlp3beLJX3kNE/I3uH PLcx9HC6RdTWG4NFCmWAXTYs9v9kIuxi0YM9kFwIV2K4+j47j3MB4DqqwK05sYtxvVsfi /ZJ9L0I8kFpBA8rSJUL8Tda1axOwhQWYwL19U+qteDdjvfALGzAscFjV0deQRXlvDX7zh GgZ92LTKzUvYwQgAlohs8ZK+f21qwiZeMhBgeJ5TC/kEU0ysrkmH75dF0sVVY+DRKSw== 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=E+RwUVFP; 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 74244606A337; Mon, 23 Nov 2026 18:53:34 +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=E+RwUVFP 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=1795460014; b=W5w3p+kLw58kL6AaYgQJYYn9BGdHeOgkx6GrsCwReJWhHWG5zxQN7skXHOyDlOaP3Kga XDb0cp5Nxn95JN1KcU2QSsEfHIZDPiU+xNxmqwFjEr9zPKcsT8aP46p5sVpLFG2ZXSdoy NUcAy49AJjny+HGx+5NnGoJNvSxMOblCDGmZ8IqVQJmiARLBPnV2N2NnNiUO+ExbCFDnA 9Vz8ljHJlW5KC81tgQMUX5i7HAWUIr1OlsYQZgUG5LL3PevAIqJH/10o2A50SMYySbRHo P9ffGUa8qtJsA66hXCij75e4gdKCS1PNeEkPGlvtYOOCAFhSduqLz1gacNIRvxD/mHQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460014; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=Y9oObqSSiDMafmKBBZklAmQlgVzGVcaYHm/4oLczkO0=; b=MrBXC8FAwLfjuJlSHu1lLNoWsls4D11a1bKCH3NqTovbNcPZySz/XcDrGoRmiA6uw01t +m2WEmbcoKSP902QLrjbCWbt4EBzrN4byilp/OzfbJAF55dMXtI2YYitvdUeASXv7EOQJ CxcupmKL7ZPEdbtB8yoY7bfKQp1OtHjmv60sThF1qKZ1qCyOabR4aq18GdSxOauxGX4PX dAvgvJuJunwthsl0UHT27rHSDKW7PWVPo41HVtHOLpoWPXynacq1bYLINfckPulTtrTjk 51E6uc7VItthG0rdMHGXW2leksfdT8ff6EhA5Xncl+TVuDgenzp1OJfEbCtvvWcobGg== 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 5FE021F00A36; Mon, 23 Nov 2026 18:53:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460014; bh=Y9oObqSSiDMafmKBBZklAmQlgVzGVcaYHm/4oLczkO0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=E+RwUVFP8gqTGPrCDjJCsLEHgacFoQbnpOYiNIjbVWcbFvsuNPwJfjHMPgdjFgl/c VbQBpbJiCoeQCDgCuoD95XJv4l6fd1sKHntQFIU/iq+AiD5GX2A0CB8DD2nHpa11Wt bABXRvO7qbIi+V7ufjDfCNMLama+rfzHFTxQ+nonGQydghO2hujLw5DT2G0F58tFDg q6qYVVZx1638OEyliI861yQp+xPkAWIr38WbUBtGPPcrF13CHM5wtasJg/JWz3TKIR t6d/RHf+OGRa94dEwhci4Mm/4lHs6cX5wrITjeG3tCIEtc1U4xdpW9EtlfpE6Ln+k/ N+NQ9r/C/3iMA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 9C3284C63D; Mon, 23 Nov 2026 18:53:00 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:52:57 +0800 Subject: [PATCH v6 05/18] mm/memcg: add folio-based lruvec live helper 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" Message-Id: <20261124-mglru-fg-v6-5-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=5565; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=4nACRHHUT6uoBi4A3zLGJqQt1hkIcj1Dp7YpYy06ekc=; b=L8XeY1unOlmxAlxK2jWtG4TrI2zaQHkKujBrGJORzsRuLiFUKDDM2Z0QuMTOIy1sME7rZM35M Hfu4zMRX7/ODZuwwOA3Ffwe7jxyK1pS7sobMCnoUSEO0En1TeQnScMU 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song Add a helper that resolves a live lruvec for a folio under RCU without taking the lruvec lock. It takes a folio directly so the lruvec lookup happens inside the RCU read-side critical section, which a lruvec-based interface cannot guarantee. The first user is the lockless folio_inc_lru_refs() path later in the series. Factor the ancestor walk out of lruvec_live_lock_irq() into __lruvec_get_live(), so that both entry points share it, and spell out why the walk is enough while only the RCU read lock is held: reparenting of a memcg that starts dying waits for a grace period, so it cannot race with the caller until the lock is released. Move memcg_is_dying() up in memcontrol.h, next to the other memcg state predicates, and use it for the walk rather than repeating the open coded css_is_dying() test a few definitions away from where it is explained. No functional change for existing callers. Tested-by: Zhao Zhenzhuo Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Reviewed-by: Qi Zheng Reviewed-by: Muchun Song (ByteDance) Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) --- include/linux/memcontrol.h | 71 ++++++++++++++++++++++++++++++++++++------= ---- 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 980358bc6f49..a03f9711522e 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -571,6 +571,11 @@ static inline bool mem_cgroup_disabled(void) return !cgroup_subsys_enabled(memory_cgrp_subsys); } =20 +static inline bool memcg_is_dying(struct mem_cgroup *memcg) +{ + return memcg ? css_is_dying(&memcg->css) : false; +} + static inline void mem_cgroup_protection(const struct mem_cgroup *root, const struct mem_cgroup *memcg, unsigned long *min, @@ -1349,6 +1354,11 @@ static inline bool mem_cgroup_online(struct mem_cgro= up *memcg) return true; } =20 +static inline bool memcg_is_dying(struct mem_cgroup *memcg) +{ + return false; +} + static inline unsigned long mem_cgroup_get_zone_lru_size(const struct lruvec *lruvec, enum lru_list lru, int zone_idx) @@ -1510,28 +1520,41 @@ static inline void lruvec_lock_irq(struct lruvec *l= ruvec) spin_lock_irq(&lruvec->lru_lock); } =20 -static inline struct lruvec *lruvec_live_lock_irq(struct lruvec *lruvec) +/* + * Reparenting moves a dying memcg's LRU state to its parent at any time, + * so anything added to that LRU afterwards would be lost. This helper + * walks up to the nearest live ancestor while the caller must hold the + * RCU read lock: reparenting of a memcg that starts dying waits for a + * grace period, so it cannot race with the caller until the lock is + * released. + */ +static inline struct lruvec *__lruvec_get_live(struct lruvec *lruvec) { #ifdef CONFIG_MEMCG struct pglist_data *pgdat =3D lruvec_pgdat(lruvec); struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); =20 - rcu_read_lock(); - /* * The memcg can be NULL when the memory controller is disabled. * Otherwise, the caller keeps the memcg owning @lruvec alive. */ - while (unlikely(memcg && css_is_dying(&memcg->css))) { + while (unlikely(memcg_is_dying(memcg))) { memcg =3D parent_mem_cgroup(memcg); lruvec =3D mem_cgroup_lruvec(memcg, pgdat); } +#endif + return lruvec; +} =20 +static inline struct lruvec *lruvec_live_lock_irq(struct lruvec *lruvec) +{ +#ifdef CONFIG_MEMCG + rcu_read_lock(); + lruvec =3D __lruvec_get_live(lruvec); spin_lock_irq(&lruvec->lru_lock); #else lruvec_lock_irq(lruvec); #endif - return lruvec; } =20 @@ -1561,6 +1584,34 @@ static inline bool folio_matches_lruvec(const struct= folio *folio, lruvec_memcg(lruvec) =3D=3D folio_memcg(folio); } =20 +/** + * folio_lruvec_live_get - get a live lruvec for a folio under RCU + * @folio: the folio + * + * Same as folio_lruvec(), except that a folio whose memcg is dying is + * accounted to the nearest live ancestor, which is where reparenting + * would put it anyway. + * + * Return: the live lruvec, with rcu_read_lock() held. Pair it with + * folio_lruvec_live_put(). + */ +static inline struct lruvec *folio_lruvec_live_get(const struct folio *fol= io) +{ +#ifdef CONFIG_MEMCG + rcu_read_lock(); + return __lruvec_get_live(folio_lruvec(folio)); +#else + return folio_lruvec(folio); +#endif +} + +static inline void folio_lruvec_live_put(const struct lruvec *lruvec) +{ +#ifdef CONFIG_MEMCG + rcu_read_unlock(); +#endif +} + /* Don't lock again iff page's lruvec locked */ static inline struct lruvec *folio_lruvec_relock_irq(const struct folio *f= olio, struct lruvec *locked_lruvec) @@ -1826,11 +1877,6 @@ void mem_cgroup_node_filter_allowed(struct mem_cgrou= p *memcg, nodemask_t *mask); =20 void mem_cgroup_show_protected_memory(struct mem_cgroup *memcg); =20 -static inline bool memcg_is_dying(struct mem_cgroup *memcg) -{ - return memcg ? css_is_dying(&memcg->css) : false; -} - #else static inline bool mem_cgroup_kmem_disabled(void) { @@ -1901,11 +1947,6 @@ static inline void mem_cgroup_node_filter_allowed(st= ruct mem_cgroup *memcg, static inline void mem_cgroup_show_protected_memory(struct mem_cgroup *mem= cg) { } - -static inline bool memcg_is_dying(struct mem_cgroup *memcg) -{ - return false; -} #endif /* CONFIG_MEMCG */ =20 #if defined(CONFIG_MEMCG) && defined(CONFIG_LRU_GEN) --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460016; b=R2th9LCwxF7KvQFNS4rI1cp/lSaogxGtf3RtsewPe8vZhtDBdodXGqyjgm1DoN/Fd+iF cYW4goEp6CIuoGtZbTpfo89xabFab22rAJ8gOsDeP7sOGgGnDYwdcHyie6fve7CrcuOgf juVCZ4eI6D9KR/9dwg9a75WBvnQ0a1b+I5GOFhLf5h7MAwCYBZqk8sZtqDVElKRGhYvwX gzmkh2cCVmHIinbPqYeJ3NOHFxNhluIt4v/IlXETejD8WfPsEtrRHUtBxg9WjmVIYOk4+ jilbeiP/w1p0TIpRojDxiwsrLwqrnwcrNfsBMD0vgJvcjKVNtEJxaiT3l7Xm8lXppAg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460016; 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=FZRD7oqKaa21oADy1rxMl/sYyXjHVIlmH9ps+OCcBhU=; b=AsjCrOjvMSVaFWd4Lkd4PiKIToV2DDW+D3nAvrl0WldYioZdas0RWpM3JzMKa9ZqROc1 8uLRPNuCFXwDv5uSDuNje36+ssdgBGei03r16l/ENaep1TVzyZeILufimFbfoKOm9nckK IoB4nPRKLRpM+3NpKkchductMmVAAHFEONxnlSqiaMY5H6y8z8RSJq7n16FbHuzEETaEs 8elW9C8Rn9MJYzoj7loHv3qdpXN8iSiivjNGQZuRxG6TGhS45r0PIA/gdTGZPA53qr3es qEWzljuL1e23OEFULlZ+7EccSojsSIz1esvLoZ8qT2Fx25C7KHJE1xXX/Z90W60bNZA== 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=dcxTUMZ9; 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 3B9AC606A330; Mon, 23 Nov 2026 18:53:36 +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=dcxTUMZ9 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=1795460015; b=qLhgM1UyNv38rQT8tlr1razCioZFE0aVgcn6iK3H2UPkkeEkqS3tfsm1stZq/9DSaX8u V0SX2T1/SkjHWu/Mr3ULj30mHJIcxrKFXS0a6s0D6Wvu/2cKbE+vd/Mubp3RzPkoPJi8c eHIjmPANIlrnfsxpRAkhwLkR0HiM7+Wc0eI6LNm61vEFTD3WkgTLwCN9V5iXBfSa/fdxY Eqnkab8ju3/Utka/b/Rzxq6l63hIGI/Qv3+XPnIEAEcOxbfRjletYetWzI+VQL4mtU1uY U/bsh8PoXWxKQMPXaxahNdqrTVBX4XTucX5MgbXto6o5ckL+JqKLTrzlCIdGiTAOnaQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460015; 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=FZRD7oqKaa21oADy1rxMl/sYyXjHVIlmH9ps+OCcBhU=; b=yWb3XjNmEUfRNm+FvePk7x1dpfKAqBcFOMKQGoMwa4w+4C2tRZFQhDlQ1NnxY7/H2OHC NiNJeQZQns2dl+j+yU5dGefup7+jLT9vV0HSCtBfFZDxBuExcs7quxVxlzS0ussaHfkQq CSzPxtXPgq9g6H/Bht7wesvjNsXUdPeo6xBNDQM+EGW9ad0+ofKLkgZEJ1/PgsKjJ0JLn Bs+iEVPkvMmX8F/p75NIZpnxedTcQFOMfG0D0KXrqXHH/gkmx0b281PqBNttS/yFNq725 JKDj7A/nMZ9SVnOeSvDQilPQ9aZ//9PdSCZ9DUbs6TJmxpGGzR8boDQ9ZU7ymkVVs+g== 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 B7E891F000C4; Mon, 23 Nov 2026 18:53:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460015; bh=FZRD7oqKaa21oADy1rxMl/sYyXjHVIlmH9ps+OCcBhU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=dcxTUMZ9iXBIWh3bIxmENjyUCRtPLlXPNyapEWUV/O7o6jZZc/PfVVJm5fNuPB5Fp qxJA6tWREoe6Nmr/1qiSrbwSBHyz+q95AlJIEdz4guNq9rlxe8411zLsofwmJlFdJs aHICoTHA5nMDP8yVRKJDUOOlOA/jjLCQ7laJTobKEQMHDxXfbsVR29fUAgyGEnyLSZ Hm3+YZGAnUUNqoO6vHdjJ2oHZKnku8/mWbF61G2YVWi4Zw2mXQ0K7Jvk1I9HW4x0Xs wmg2s2G9bhNWKNdoVET+V5c30EmDlslwaRS5qtXEIvn/hrGjRJu77KJazBd54I8w6i KGmi102Hh6WTw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id BD7AA4C641; Mon, 23 Nov 2026 18:53:00 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:52:58 +0800 Subject: [PATCH v6 06/18] 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: quoted-printable Message-Id: <20261124-mglru-fg-v6-6-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=3997; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=//cwp1yvGPvx4hSAYTY0g5G2U3Dt6Ds7AHeyEL5Sfsk=; b=OR6OERTRzsjoPZgx5Bj8eld8+L1XVLhawLjPJ1B4sc7XEOe3NGkoxMtMmdek+EcOY+3113Uru m7OZMjFvbZsDbVamj3o5bX6sRQ2sYnivCMZ+kVKANGsxQx3MTuu1gKP 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=3Dnever) 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 Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) Tested-by: Zhao Zhenzhuo --- 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 46beded51c7d..2571b3f01226 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -233,6 +233,8 @@ static inline void folio_set_lru_refs(struct folio *fol= io, unsigned int refs) do { new_flags =3D old_flags; lru_set_refs_flags(&new_flags, refs); + if (new_flags =3D=3D old_flags) + break; } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); } =20 diff --git a/mm/vmscan.c b/mm/vmscan.c index 202e0f874b23..15df5618a3cf 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3274,6 +3274,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 |=3D BIT(PG_workingset); + if (new_flags =3D=3D old_flags) + break; } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); =20 *type =3D folio_flags_is_file_lru(&old_flags); --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460016; b=if+lXXkFPbN5ugFUxsEe49DFNgyWOXqctGAMnKXBvvgQRKM3WbR3lF7LFtMB3LTtAFww kbetWDhORl2/iqSSbQj5j9Bd6cmC/i3zUCaxiWy28J0xjMCww6cUbzpWspfyZZI6J4cCc 92F3BO0LkULJFyhu9p79608ATVglehd5hN4J8TGcEIpCoZChAZFs1W2onsSfFKTuULnMl d5RLOtup0PuNOx1qw/V2dUQxqNBzoQQL+wUBRhLCARIR9MswObJF1zoRgx4nVgpA+nsRy c1hw+oI3KRxyhCSnkuZSyJ1a6pzAZYpv34LBrJSnsDrdVeZQcc4zSIGSwWcuKeLndEQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460016; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=4PAkgAU32Q5m3chu2FFYusn5lXnadS9yTIK7UM/nkec=; b=zUlyVEQxRWTdxk3/iyRgwYzwZqYQz2d5gFKRetbRptxdYgCM1Eb0tj7Z1YIMwiC/JrKQ +l0x5LJdcVL4CXIwvpjdK4xa475ARc6vVP/18hNv3UurSRlY2WMLyWnrirTaqNBgXUZUH 0muj/RsqdQm90T3qsRZ9k/ZlIk+3mdfKc0hoyk28+3r5apMZWDuEx1GLR1g6rp9CIqE1z 6zjHqxP4QyclaAILFyVPF+xvoWO7/8JsIDFnSj9aZ2fJ17R8GeB+ZzzoYxw7Syh1aGvuO LezqYFvsPM47s1QSehOi1eM6/LuFYsU8hG54sBwh8Eryuu/R0f0zLJG0DxOS12UDXpg== 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=CE2KdpHP; 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 98E68606A31F; Mon, 23 Nov 2026 18:53:36 +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=CE2KdpHP 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=1795460016; b=itgacsEEUXjNZFDgAkshj2FVCgJJAr1UGpiEnm609zkPFFa7S6YN0hwyLEuM0FFHS/Sk 7yxO+N6xmCduVA6ehbkuKiA83FBYPPsHlQ4EGx0HYi33LBcR1FPKLxj0EH+wNEj/T/zob /udZJcQIob2db061mSAn4QrE0GnT6eslgx/gavZUJSyXED30n+0oQRGJEJ4JY64illBtl UCmFpqrx2TQ8aU4y1IedWJGQHoDCfogbbGCuqCCk8luDda1F0D8bdgTpxspOCopLDgY22 XEZF5YYtiH5WMQ1NFULTB6mlGdh7KODpvubLiXvZltsvuOql8OlQ0r/9p28CsYq1vEQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460016; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=4PAkgAU32Q5m3chu2FFYusn5lXnadS9yTIK7UM/nkec=; b=H68Sgq9arTfMaCDBAMVxQYhnCj2wct2wkLMX2xDCgYB0m5J/nYi7fb/9G+nandxgvOGy 1dUWBKkbSjqgHEdNXBNsrmC5MmIKjsOhf9zhD8eJGKY/VvUpk7AIXkzJULd31J3qF06xN aPKq4jdKtKcqDQKqBed6cbnPOpvcWvi+wJsS7fIf703BVMW1fmT7MF464M+ODyV4ZpFiZ slyMHGyyZktppkQFjvaykFXsTuJZj5qk1N0y49jW0pmpKDetafLlUqJg3YvrP5KCwu9yn 5+Ep6HNDg4RDYsnHS7njbIX7Bd3kP7KfA2gwgfNDcX6jorZTmLwcgXIlETKD0qBUs6g== 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 25EB31F000D1; Mon, 23 Nov 2026 18:53:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460016; bh=4PAkgAU32Q5m3chu2FFYusn5lXnadS9yTIK7UM/nkec=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=CE2KdpHPQ93B4MJ9y0V38lH801fMf/ulQ4nPmkI11L/tMJuYPPt0e3EuUvTlyV2yo znt8JWbNsMphxGvmvobVn9Xc9XVrUUh/5/shoB7cdwa5IIalw+rchaELuZA8eee1sL NYhbpYkUEuffO9O4Cv7hIQGhtkmWUyhhaE5a+E+H4mvLDS1LFWmfwKMzPVrzkJdSDJ lHCzUjEhcmSEdf3HU1+miDoZTrDfQ0o7oNJ4tbekBnvY0ttF5gUI1XT4i9AOrcUgNq 8rpQPKCoFS6ocAu8L8l6UFnjF6sgETYVzaK3hpBeAxCYvXJzV8fGsUcDj3+lA/zWYF 9UxP3OfELLecg== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id DD30C4C645; Mon, 23 Nov 2026 18:53:00 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:52:59 +0800 Subject: [PATCH v6 07/18] mm/mglru: frequency guided workingset promotion (MGLRU-FG) 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" Message-Id: <20261124-mglru-fg-v6-7-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=47655; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=TXVgiDtWk+J/lvMxN0/2nQwnAJhOvS1DIuL4aDWlwY0=; b=AR19UTt4jSslsZIm5BarNGI0kAqMPnUHmJddFu0dX+SKP2vKfEKXRrzwcFtwani5/YsHbyVr3 hhtCFhPIV4RAflfi+dWKTCb3OGVNHlzYqoKSdihg1gxTsgwk6bSokX1 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song Complement MGLRU's eviction-time tier-PID protection with access-time frequency-guided promotion. Introduce a unified set of helpers based on the referenced (access) count of a folio. Like LFU, this uses access frequency to distinguish hotter folios, but it still reclaims by generation: the generation and the bounded refs together form a "logical refs" count. Accesses increase refs; refs crossing a threshold trigger promotion, which moves the folio one generation newer and caps its refs at a lower threshold, so a folio in a newer generation with lower refs is hotter than a folio in an older generation with higher refs. Gen aging then lowers the logical refs without updating every folio. Accesses from the page table and from file descriptors are balanced with defined rules. This limits protection from old hotness at close to no overhead, and enhances MGLRU's ability to distinguish hotness levels over a much larger range. A folio's refs still map to a logarithmic tier just like before, but with more formal bit definitions and more finely defined thresholds: LRU_REFS_REFERENCED (1), LRU_REFS_WORKINGSET (2), LRU_REFS_PROTECTED (3), and LRU_REFS_MAX (7). The PID controller can still operate coherently with the new FG promotion, but the protection ratio is softer (2:3 -> 1:2) since proactive promotion is mostly good enough. The setpoint now also spans the cumulative mass of the tiers below the candidate instead of tier 0 alone, as the new thresholds are fallback tiers that a folio never falls below. By making use of the newly defined thresholds, simplify MGLRU's usage of PG_workingset and PG_referenced: they become the low two bits of the refs count, with the higher bits provided by LRU_REFS_MASK. This reduces MGLRU's original refs count bit usage by one, since only one extra bit is now needed to record a max referenced count of 7. This doesn't affect classical LRU, and it addresses several shortcomings of MGLRU's file-descriptor cache protection: - Long feedback loop: protection is only activated after enough re-faults, by which time the hot folios are already evicted, or no longer hot. - Limited tier resolution: once the refs count saturated, MGLRU could no longer distinguish hotter folios within a tier. PG_workingset also forces a folio to stay on tier 3. - Eviction hotness reversion: because PID protection activates upon eviction and always targets the LRU tail, it tends to protect cold tail folios at the expense of hotter head folios. Once the tail folios consume the PID protection budget, head folios lose their protection. - Additionally, the PID cannot distinguish the access time of folios that share the same reference count, and there are only 4 tiers. To achieve a frequency-guided framework, this commit introduces and reworks the LRU_REFS related helpers and definitions. Most of the work are done by the helpers below, check their inline comments for details. They collect references from different access channels, following the same rule but different context: - folio_inc_lru_refs(): Called on any cache access (folio_mark_accessed) or page table access. This is the main helper: it promotes folios according to their access frequency. This performs lazy promotion. - folio_inc_lru_refs_walk(): Used by the PTE walk path during aging and by the batched look-around, both without the LRU lock. This also performs lazy promotion. - folio_inc_lru_refs_isolated(): Used by the rmap check before eviction. The folio is isolated and hence this doesn't perform promotion by itself; the folio will be added back to the right gen upon return according to the refs and PG_active placement hint. Refaults restore the saved refs, capped at LRU_REFS_PROTECTED. Fault-driven refaults with prior references also set PG_active, keeping the stronger protection for page table access. Consequently the placement hint for newly added file folios also uses LRU_REFS_WORKINGSET instead of PG_referenced, so a folio accessed only once now starts in the oldest generation rather than the second oldest. The previous one-generation grace was an artifact of the fault bias which is well described in the cover letter of this series, and it depended on LRU drain timing. PG_workingset and PG_referenced are no longer independent flags under MGLRU, so existing raw folio_test_*() callers need to be adjusted, which is covered by the following commits. Tested-by: Andrew Morton Reviewed-by: Kunwu Chan Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) Tested-by: Zhao Zhenzhuo --- include/linux/mm_inline.h | 107 +++++++++------ include/linux/mmzone.h | 126 +++++++++++------ kernel/bounds.c | 2 +- mm/folio.c | 49 +------ mm/vmscan.c | 341 +++++++++++++++++++++++++++++++-----------= ---- mm/workingset.c | 47 ++++--- 6 files changed, 416 insertions(+), 256 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 2571b3f01226..f38dad1198fc 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -144,12 +144,14 @@ static inline int lru_hist_from_seq(unsigned long seq) return seq % NR_HIST_GENS; } =20 -static inline int lru_tier_from_refs(int refs, bool workingset) +static inline int lru_tier_from_refs(unsigned int refs) { - VM_WARN_ON_ONCE(refs > BIT(LRU_REFS_WIDTH)); - - /* see the comment on MAX_NR_TIERS */ - return workingset ? MAX_NR_TIERS - 1 : order_base_2(refs); + /* The refs range must map into the tier index space. */ + BUILD_BUG_ON(BIT(LRU_TIER_MAX) < LRU_REFS_MAX); + VM_WARN_ON_ONCE(refs > LRU_REFS_MAX); + if (refs < LRU_REFS_WORKINGSET) + return 0; + return fls(refs - 1); } =20 /** @@ -187,21 +189,24 @@ static inline int lru_get_gen_flags(unsigned long fla= gs) * @flags: pointer to the folio flags * @refs: referenced / access count number, between 0 and LRU_REFS_MAX, in= clusive. * - * For MGLRU, PG_referenced holds the first ref, and the extra bits hold t= he - * remaining refs. For classical LRU the extra bits are not used, so it can - * also be seen as the refs count never exceeds 1. In both cases, refs =3D= =3D 1 - * means PG_referenced is set and the extra bits are zero, and refs =3D=3D= 0 means - * PG_referenced and the extra bits are all unset. + * For MGLRU, PG_referenced, PG_workingset are used as the lower two bits = of + * refs counter, and extra bits hold the remaining higher bits. For classi= cal + * LRU the extra bits are not used, and the two flags have no direct + * relationship with each other, but this helper can still be used to sync + * them. For both cases, refs =3D=3D 0 means these two flags and the extra= bits + * are all unset. And refs =3D=3D 1 / 2 / 3 means PG_referenced and PG_wor= kingset + * encode the low two bits in binary order. */ static inline void lru_set_refs_flags(unsigned long *flags, unsigned int r= efs) { VM_WARN_ON_ONCE(refs > LRU_REFS_MAX); - BUILD_BUG_ON(LRU_REFS_MAX !=3D (LRU_REFS_MASK >> LRU_REFS_PGOFF) + 1); - + BUILD_BUG_ON(LRU_REFS_MASK & (BIT(PG_referenced) | BIT(PG_workingset))); *flags &=3D ~LRU_REFS_FLAGS; - if (!refs) - return; - *flags |=3D (BIT(PG_referenced) | ((refs - 1UL) << LRU_REFS_PGOFF)); + if (refs & BIT(0)) + *flags |=3D BIT(PG_referenced); + if (refs & BIT(1)) + *flags |=3D BIT(PG_workingset); + *flags |=3D ((unsigned long)refs >> 2) << LRU_REFS_PGOFF; } =20 /** @@ -210,22 +215,31 @@ static inline void lru_set_refs_flags(unsigned long *= flags, unsigned int refs) * * Reads the LRU referenced count set by lru_set_refs_flags(). */ -static inline int lru_get_refs_flags(unsigned long flags) +static inline unsigned int lru_get_refs_flags(unsigned long flags) { - if (!(flags & BIT(PG_referenced))) - return 0; - /* - * Return the total number of accesses including PG_referenced. Also see - * the comment on LRU_REFS_FLAGS. - */ - return ((flags & LRU_REFS_MASK) >> LRU_REFS_PGOFF) + 1; + unsigned int refs; + + /* Decode the per-folio reference state. */ + refs =3D !!(flags & BIT(PG_referenced)); + refs |=3D !!(flags & BIT(PG_workingset)) << 1; + refs |=3D ((flags & LRU_REFS_MASK) >> LRU_REFS_PGOFF) << 2; + return refs; } =20 -static inline int folio_lru_refs(const struct folio *folio) +static inline unsigned int folio_lru_refs(const struct folio *folio) { return lru_get_refs_flags(READ_ONCE(*const_folio_flags(folio, 0))); } =20 +/* + * Referenced at least twice. Unlike lru_refs_is_active() this is not + * bounds-checked: folio_inc_lru_refs() evaluates refs unclamped. + */ +static inline bool lru_refs_is_workingset(unsigned int refs) +{ + return refs >=3D LRU_REFS_WORKINGSET; +} + static inline void folio_set_lru_refs(struct folio *folio, unsigned int re= fs) { unsigned long new_flags, old_flags =3D READ_ONCE(*folio_flags(folio, 0)); @@ -238,14 +252,26 @@ static inline void folio_set_lru_refs(struct folio *f= olio, unsigned int refs) } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); } =20 +void folio_inc_lru_refs(struct folio *folio, unsigned int flags); + static inline int folio_lru_gen(const struct folio *folio) { return lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0))); } =20 +static inline bool lru_gen_is_old(struct lruvec *lruvec, int type, int gen) +{ + unsigned long min_seq =3D READ_ONCE(lruvec->lrugen.min_seq[type]); + + /* The MIN_NR_GENS oldest generations, which eviction scans next */ + BUILD_BUG_ON(MIN_NR_GENS !=3D 2); + return gen =3D=3D lru_gen_from_seq(min_seq) || + gen =3D=3D lru_gen_from_seq(min_seq + 1); +} + static inline bool lru_gen_is_active(const struct lruvec *lruvec, int gen) { - unsigned long max_seq =3D lruvec->lrugen.max_seq; + unsigned long max_seq =3D READ_ONCE(lruvec->lrugen.max_seq); =20 VM_WARN_ON_ONCE(gen >=3D MAX_NR_GENS); =20 @@ -302,23 +328,24 @@ static inline unsigned long lru_gen_folio_seq(const s= truct lruvec *lruvec, bool reclaiming) { int age; + bool workingset =3D lru_refs_is_workingset(folio_lru_refs(folio)); int type =3D folio_is_file_lru(folio); const struct lru_gen_folio *lrugen =3D &lruvec->lrugen; =20 /* - * +-----------------------------------+---------------------------------= --+ - * | Accessed through page tables and | Accessed through file descriptor= s | - * | promoted by folio_update_gen() | and protected by folio_inc_gen()= | - * +-----------------------------------+---------------------------------= --+ - * | PG_active (set while isolated) | = | - * +-----------------+-----------------+-----------------+---------------= --+ - * | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAG= S | - * +-----------------------------------+---------------------------------= --+ - * |<---------- MIN_NR_GENS ---------->| = | - * |<---------------------------- MAX_NR_GENS ---------------------------= ->| + * +------------------------------------------+--------------------------= ----------------+ + * | Accessed through page tables and | Accessed through file= descriptors | + * | promoted by folio_inc_lru_refs_walk() | protected by folio_inc_lr= u_refs/inc_gen | + * +------------------------------------------+--------------------------= ----------------+ + * | PG_active (set at isolation or refault) | = | + * +--------------------+---------------------+--------------------+-----= ----------------+ + * | LRU_REFS_MAX | LRU_REFS_WORKINGSET | LRU_REFS_MAX | LRU_= REFS_WORKINGSET | + * +------------------------------------------+--------------------------= ----------------+ + * |<-------------- MIN_NR_GENS ------------->| = | + * |<----------------------------------- MAX_NR_GENS --------------------= --------------->| */ if (folio_test_active(folio)) - age =3D MIN_NR_GENS - folio_test_workingset(folio); + age =3D MIN_NR_GENS - workingset; else if (reclaiming) age =3D MAX_NR_GENS; else if ((!folio_is_file_lru(folio) && !folio_test_swapcache(folio)) || @@ -326,7 +353,7 @@ static inline unsigned long lru_gen_folio_seq(const str= uct lruvec *lruvec, (folio_test_dirty(folio) || folio_test_writeback(folio)))) age =3D MIN_NR_GENS; else - age =3D MAX_NR_GENS - (folio_test_workingset(folio) || folio_test_refere= nced(folio)); + age =3D MAX_NR_GENS - workingset; =20 return max(READ_ONCE(lrugen->max_seq) - age + 1, READ_ONCE(lrugen->min_se= q[type])); } @@ -341,6 +368,7 @@ static inline bool __lru_gen_add_folio(struct lruvec *l= ruvec, struct folio *foli int zone =3D folio_zonenum(folio); struct lru_gen_folio *lrugen =3D &lruvec->lrugen; =20 + BUILD_BUG_ON(BIT(LRU_GEN_WIDTH - 1) !=3D MAX_NR_GENS); VM_WARN_ON_ONCE_FOLIO(gen !=3D -1, folio); =20 if (folio_test_unevictable(folio) || !lrugen->enabled) @@ -408,7 +436,6 @@ static inline bool lru_gen_del_folio(struct lruvec *lru= vec, struct folio *folio, */ static inline void folio_migrate_lru_refs(struct folio *new, const struct = folio *old) { - BUILD_BUG_ON(LRU_REFS_MASK & BIT(PG_referenced)); folio_set_lru_refs(new, folio_lru_refs(old)); } #else /* !CONFIG_LRU_GEN */ @@ -444,6 +471,10 @@ static inline bool lru_gen_del_folio(struct lruvec *lr= uvec, struct folio *folio, return false; } =20 +static inline void folio_inc_lru_refs(struct folio *folio, unsigned int fl= ags) +{ +} + static inline void folio_migrate_lru_refs(struct folio *new, const struct = folio *old) { if (folio_test_referenced(old)) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 4e85bb520f76..27ba7fa6ddd6 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -453,57 +453,103 @@ enum lruvec_flags { #define MAX_NR_GENS 4U =20 /* - * Each generation is divided into multiple tiers. A folio accessed N times - * through file descriptors is in tier order_base_2(N). A folio in the fir= st - * tier (N=3D0,1) is marked by PG_referenced unless it was faulted in thro= ugh page - * tables or read ahead. A folio in the last tier (MAX_NR_TIERS-1) is mark= ed by - * PG_workingset. A folio in any other tier (1flags. + * Each generation is divided into multiple tiers. A folio's referenced + * count maps to a tier as shown below (with one extra refs bit): * - * In contrast to moving across generations which requires the LRU lock, m= oving - * across tiers only involves atomic operations on folio->flags and theref= ore - * has a negligible cost in the buffered access path. In the eviction path, - * comparisons of refaulted/(evicted+protected) from the first tier and th= e rest - * infer whether folios accessed multiple times through file descriptors a= re - * statistically hot and thus worth protecting. + * MGLRU (frequency guidance) + * Refs Tier |- Refs: observed accesses, capped on promotion. + * 0 0 |- Mostly cold pages, readahead, etc. [1] + * 1 0 |=3D LRU_REFS_REFERENCED: Used at least once. [2] + * -WORKINGSET-+|- Workingset floor, unless explicitly reset. [3] + * 2 1<-+|=3D LRU_REFS_WORKINGSET: Classical workingset, accessed t= wice, protected. [4] + * 3 2 |- LRU_REFS_PROTECTED: Protected workingset, promoted page= s capped at here. [5] + * 4 2 | + * 5 3 |- The tier here is MAX_NR_TIERS - 1 + * 6 3 | + * 7 3 |=3D LRU_REFS_MAX: Promotion candidate. [6] + * -PROMOTION->-/ * - * MAX_NR_TIERS is set to 4 so that the multi-gen LRU can support twice the - * number of categories of the active/inactive LRU when keeping track of - * accesses through file descriptors. This uses MAX_NR_TIERS-2 spare bits = in - * folio->flags, masked by LRU_REFS_MASK. + * Ideally each tier holds folios of similar access patterns: lower tiers + * are less important and evicted faster. A page's reference count and + * tier are capped when it changes generation, preventing it from + * dominating the new generation based on old-generation access history. + * The generation and refs together represent logical refs: hotness relati= ve + * to the aging window. Promotion moves the folio forward and caps refs; + * gen aging then lowers logical refs even without updating the stored cou= nt. + * This avoids a separate full-cache aging walk to reduce old hotness. + * + * Refs tracks accesses from two sources: page table (lazily collected by + * the page table aging walk or rmap eviction lookup) and file descriptors + * (by folio_mark_accessed). Page table accesses promote more readily + * because the accessed bit is sticky (undercounts repeated accesses), + * passively collected, and page faults are generally more important as + * userspace does not expect a memory access to block on reclaim. Both + * access types increment refs by one; the result is capped at + * LRU_REFS_PROTECTED on promotion or deferral, or LRU_REFS_MAX otherwise. + * + * 1. Tier is fls(N-1) for N > 1, 0 otherwise. Folios with zero + * accesses (refs =3D=3D 0) are generally cold, e.g. readahead folios. + * + * 2. Folios accessed once stay on tier 0: one-time usage does not + * qualify for tier protection. A second access can advance the folio, + * aligning with classical LRU's use-twice threshold. A page table + * access that brings refs to LRU_REFS_WORKINGSET promotes to the + * latest gen. A file access at that refs count only defers eviction + * from the tail gens of its type. + * + * 3. Folios accessed at least twice are considered workingset. This + * mostly aligns with classical LRU: at least one I/O is saved by + * keeping them in memory. Unless explicitly reset, these folios stay + * at or above tier 1 (the workingset floor), so tier 0 stays a clean t= ier + * for cold cache while tier 1 serves as the fallback line for + * actually reused or historically hot folios. + * + * 4. Starting from tier 1, PID protection sacrifices lower tiers to + * protect higher tiers by comparing refault rates for long-term + * accuracy, and caps higher refs to LRU_REFS_WORKINGSET. Rejected + * folios also keep at most that count on putback, allowing further + * accesses to distinguish their hotness again. + * + * 5. Pages beyond the ordinary workingset tier form new tiers for the + * PID controller to protect differently. Folios at or above this + * level are capped at LRU_REFS_PROTECTED on promotion or deferral, + * and at LRU_REFS_WORKINGSET under PID protection in the oldest + * generation, where they represent a historical workingset. + * + * 6. A further cache access at LRU_REFS_MAX advances an older folio by one + * generation, with refs capped to LRU_REFS_PROTECTED. Folios already in + * the newest generation just saturate their refs. + * + * Tiering uses PG_referenced and PG_workingset as the lower two bits, + * and the bits masked by LRU_REFS_MASK as the higher bits, so the refs + * count ranges from 0 to LRU_REFS_MAX. + * + * Cold hints can reset refs with lru_gen_clear_refs(). Refault restores + * its referenced count, capped at LRU_REFS_PROTECTED, which aligns with + * promotion. Fault-driven refaults of previous workingset folios set + * PG_active, so they are placed in the latest gen. + * + * MAX_NR_TIERS is set to 4 so that the multi-gen LRU can support twice + * the number of categories of the active/inactive LRU. */ #define MAX_NR_TIERS 4U #define LRU_TIER_MIN 0U #define LRU_TIER_MAX (MAX_NR_TIERS - 1) =20 +/* Access source flags for folio_inc_lru_refs() */ +#define LRU_REF_MAPPED 0x1U +#define LRU_REF_EXEC 0x2U + +#define LRU_REFS_REFERENCED 0x1 +#define LRU_REFS_WORKINGSET 0x2 +#define LRU_REFS_PROTECTED 0x3 + #ifndef __GENERATING_BOUNDS_H =20 #define LRU_GEN_MASK ((BIT(LRU_GEN_WIDTH) - 1) << LRU_GEN_PGOFF) #define LRU_REFS_MASK ((BIT(LRU_REFS_WIDTH) - 1) << LRU_REFS_PGOFF) -#define LRU_REFS_MAX BIT(LRU_REFS_WIDTH) - -/* - * For folios accessed multiple times through file descriptors, - * lru_gen_inc_refs() sets additional bits of LRU_REFS_WIDTH in folio->fla= gs - * after PG_referenced, then PG_workingset after LRU_REFS_WIDTH. After all= its - * bits are set, i.e., LRU_REFS_FLAGS|BIT(PG_workingset), a folio is lazily - * promoted into the second oldest generation in the eviction path. And wh= en - * folio_inc_gen() does that, it clears LRU_REFS_FLAGS so that - * lru_gen_inc_refs() can start over. Note that for this case, LRU_REFS_MA= SK is - * only valid when PG_referenced is set. - * - * For folios accessed multiple times through page tables, folio_update_ge= n() - * from a page table walk or lru_gen_set_refs() from a rmap walk sets - * PG_referenced after the accessed bit is cleared for the first time. - * Thereafter, those two paths set PG_workingset and promote folios to the - * youngest generation. Like folio_inc_gen(), folio_update_gen() also clea= rs - * PG_referenced. Note that for this case, LRU_REFS_MASK is not used. - * - * For both cases above, after PG_workingset is set on a folio, it remains= until - * this folio is either reclaimed, or "deactivated" by lru_gen_clear_refs(= ). It - * can be set again if lru_gen_test_recent() returns true upon a refault. - */ -#define LRU_REFS_FLAGS (LRU_REFS_MASK | BIT(PG_referenced)) +#define LRU_REFS_FLAGS (LRU_REFS_MASK | BIT(PG_referenced) | BIT(PG_worki= ngset)) +#define LRU_REFS_MAX (BIT(LRU_REFS_WIDTH + 2) - 1) =20 struct lruvec; struct page_vma_mapped_walk; diff --git a/kernel/bounds.c b/kernel/bounds.c index 02b619eb6106..06a034713b5d 100644 --- a/kernel/bounds.c +++ b/kernel/bounds.c @@ -25,7 +25,7 @@ int main(void) DEFINE(SPINLOCK_SIZE, sizeof(spinlock_t)); #ifdef CONFIG_LRU_GEN DEFINE(LRU_GEN_WIDTH, order_base_2(MAX_NR_GENS + 1)); - DEFINE(__LRU_REFS_WIDTH, MAX_NR_TIERS - 2); + DEFINE(__LRU_REFS_WIDTH, MAX_NR_TIERS - 3); #else DEFINE(LRU_GEN_WIDTH, 0); DEFINE(__LRU_REFS_WIDTH, 0); diff --git a/mm/folio.c b/mm/folio.c index 7a3394e7c5dc..204c11a71cc5 100644 --- a/mm/folio.c +++ b/mm/folio.c @@ -378,32 +378,6 @@ static void __lru_cache_activate_folio(struct folio *f= olio) =20 #ifdef CONFIG_LRU_GEN =20 -static void lru_gen_inc_refs(struct folio *folio) -{ - unsigned long new_flags, old_flags =3D READ_ONCE(*folio_flags(folio, 0)); - int refs; - - if (folio_test_unevictable(folio)) - return; - - /* see the comment on LRU_REFS_FLAGS */ - if (!folio_lru_refs(folio)) { - folio_set_lru_refs(folio, 1); - return; - } - - do { - new_flags =3D old_flags; - refs =3D lru_get_refs_flags(old_flags); - if (refs =3D=3D LRU_REFS_MAX) { - if (!folio_test_workingset(folio)) - folio_set_workingset(folio); - return; - } - lru_set_refs_flags(&new_flags, refs + 1); - } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); -} - static bool lru_gen_clear_refs(struct folio *folio) { int gen =3D folio_lru_gen(folio); @@ -414,7 +388,6 @@ static bool lru_gen_clear_refs(struct folio *folio) return true; =20 folio_set_lru_refs(folio, 0); - folio_clear_workingset(folio); =20 rcu_read_lock(); seq =3D READ_ONCE(folio_lruvec(folio)->lrugen.min_seq[type]); @@ -425,10 +398,6 @@ static bool lru_gen_clear_refs(struct folio *folio) =20 #else /* !CONFIG_LRU_GEN */ =20 -static void lru_gen_inc_refs(struct folio *folio) -{ -} - static bool lru_gen_clear_refs(struct folio *folio) { return false; @@ -454,7 +423,8 @@ void folio_mark_accessed(struct folio *folio) if (folio_test_dropbehind(folio)) return; if (lru_gen_enabled()) { - lru_gen_inc_refs(folio); + if (!folio_test_unevictable(folio)) + folio_inc_lru_refs(folio, 0); return; } =20 @@ -500,21 +470,6 @@ void folio_add_lru(struct folio *folio) folio_test_unevictable(folio), folio); VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); =20 - /* - * For refaulted workingset folios, set PG_active so they - * can be added to active generations. - * For prefaulted file folios, folio_mark_accessed() sets - * PG_referenced so lru_gen_folio_seq() places them into - * the second oldest generation. - */ - if (lru_gen_enabled() && !folio_test_unevictable(folio) && - lru_gen_in_fault() && !(current->flags & PF_MEMALLOC)) { - if (folio_test_workingset(folio)) - folio_set_active(folio); - else if (!folio_test_referenced(folio)) - folio_mark_accessed(folio); - } - folio_batch_add_and_move(folio, lru_add); } EXPORT_SYMBOL(folio_add_lru); diff --git a/mm/vmscan.c b/mm/vmscan.c index 15df5618a3cf..755bc738ee57 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -818,38 +818,204 @@ enum folio_references { }; =20 #ifdef CONFIG_LRU_GEN +/*************************************************************************= ***** + * Referenced count feedback + *************************************************************************= *****/ + /* - * Only used on a mapped folio in the eviction (rmap walk) path, where pro= motion - * needs to be done by taking the folio off the LRU list and then adding i= t back - * with PG_active set. In contrast, the aging (page table walk) path uses - * folio_update_gen(). + * The folio_inc_lru_refs{_*} helpers below collect the referenced info + * (hotness) from the page table walker, the rmap walk upon eviction, the + * rmap look-around, and file descriptors (folio_mark_accessed). + * + * Each access adds one to the refs count, capped at LRU_REFS_MAX. A + * fresh folio's refs go from 0 to LRU_REFS_REFERENCED to + * LRU_REFS_WORKINGSET, and so on. + * + * Mapped access (PT, LRU_REF_MAPPED) has a higher priority since the + * accessed bit is sticky (it under-counts repeated accesses) and a fault + * could be more costly than FD I/O. So, a page table access always + * promotes a folio on an expiring gen by at least one gen. And starting + * from the second access (refs =3D=3D LRU_REFS_WORKINGSET), it moves the = folio + * to the newest gen. Executable file folios promote on their first access + * to avoid I/O thrashing. The refs are increased at a normal pace though. + * + * File descriptor accesses (FD) are a precise access method. So, only + * since the second access (refs =3D=3D LRU_REFS_WORKINGSET), every access + * will promote a folio if the folio is on an expiring gen (close to + * min_seq). If the folio is not on an expiring gen, it will be promoted + * when its refs exceed LRU_REFS_MAX; the newest gen just saturates. + * + * Promotion, regarding the source, always caps the refs at + * LRU_REFS_PROTECTED, so folios in a newer gen start at or below the + * protected tier, and folios with lower refs in a newer gen are logically + * hotter than a folio in an older gen with higher refs. + * + * A folio with refs at or above LRU_REFS_WORKINGSET is considered a + * workingset, and the tiers starting there engage PID protection. + * PID-protected folios (folio_inc_gen) are capped at + * LRU_REFS_WORKINGSET independently to make full use of tiering. */ -static bool lru_gen_set_refs(struct folio *folio, const vma_flags_t *vma_f= lags) -{ - /* see the comment on LRU_REFS_FLAGS */ - if (!folio_test_referenced(folio) && !folio_test_workingset(folio)) { - /* Activate file-backed executable folios after first usage. */ - if (is_exec_file_folio(folio, vma_flags)) { - folio_set_workingset(folio); - folio_set_lru_refs(folio, 0); - return true; + +/** + * folio_inc_lru_refs - Update the folio's LRU refs and lazily promote it. + * @folio: The folio being accessed. + * @flags: The type of ref: FD access, or mapped access optionally with + * executable access. + * + * Used by both cache access (flags =3D=3D 0) and page table access (flags= =3D=3D + * LRU_REF_MAPPED, optionally with LRU_REF_EXEC). + * Context: The caller must keep the folio alive by a reference or the page + * table lock of a mapped folio. + */ +void folio_inc_lru_refs(struct folio *folio, unsigned int flags) +{ + int max_gen; + int type, refs, old_gen, gen; + unsigned long new_flags, old_flags, max_seq; + struct lru_gen_folio *lrugen; + struct lruvec *lruvec =3D NULL; + + type =3D folio_is_file_lru(folio); + old_flags =3D READ_ONCE(*folio_flags(folio, 0)); + do { + new_flags =3D old_flags; + old_gen =3D lru_get_gen_flags(old_flags); + refs =3D lru_get_refs_flags(old_flags) + 1; + gen =3D old_gen; + if (old_gen < 0) + goto out; + /* + * Lock the lruvec if the folio is on-list. We are already + * doing lazy promotion so in theory we don't need this, + * but for now, concurrent aging would still corrupt the + * size counters. This is a temporary limitation and + * will be lifted very soon, so the lock here is not a + * performance concern. + */ + if (!lruvec) { + lruvec =3D lruvec_live_lock_irq(folio_lruvec(folio)); + lrugen =3D &lruvec->lrugen; } + max_seq =3D READ_ONCE(lrugen->max_seq); + max_gen =3D lru_gen_from_seq(max_seq); + if (old_gen =3D=3D max_gen) + goto out; =20 - folio_set_lru_refs(folio, 1); - return false; - } + if (flags & (LRU_REF_MAPPED | LRU_REF_EXEC)) { + /* Promote second page table access or executable */ + if (refs > LRU_REFS_REFERENCED || flags & LRU_REF_EXEC) + gen =3D max_gen; + /* First access only defers eviction of an old generation */ + else if (lru_gen_is_old(lruvec, type, old_gen)) + gen =3D (old_gen + 1) % MAX_NR_GENS; + refs =3D min(refs, LRU_REFS_PROTECTED); + } else if (refs > LRU_REFS_MAX) { + /* LRU refs counting overflow, bump the gen */ + gen =3D (old_gen + 1) % MAX_NR_GENS; + refs =3D LRU_REFS_PROTECTED; + } else if (lru_gen_is_old(lruvec, type, old_gen) && + lru_refs_is_workingset(refs)) { + /* Defer eviction of just accessed workingset */ + gen =3D (old_gen + 1) % MAX_NR_GENS; + refs =3D min(refs, LRU_REFS_PROTECTED); + } +out: + refs =3D min(refs, LRU_REFS_MAX); + lru_set_refs_flags(&new_flags, refs); + if (gen !=3D old_gen) + lru_set_gen_flags(&new_flags, gen); + if (new_flags =3D=3D old_flags) + break; + } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); =20 - /* Promote on second access */ - if (folio_lru_refs(folio) > 1) { - folio_set_workingset(folio); - folio_set_lru_refs(folio, 0); - } else { - folio_mark_accessed(folio); - } - return true; + if (gen !=3D old_gen) + lru_gen_update_size(lruvec, folio, old_gen, gen); + if (lruvec) + lruvec_unlock_irq(lruvec); +} + +/** + * folio_inc_lru_refs_walk - Update the folio's LRU refs and lazily promot= e it. + * @folio: The folio. + * @lruvec: The lruvec the folio belongs to. + * @vma_flags: The flags of the VMA being walked. + * @new_gen: The gen the folio is moved to; may be unchanged. + * @type: Returns the folio's type: file or anon. + * + * Similar to folio_inc_lru_refs(), but for the page table walker and the + * look-around. The LRU size accounting is batched by the caller for + * better performance. + * + * Return: The old generation the folio belongs to if the folio is still + * on the LRU and not in the newest generation, or -1 otherwise. -1 means + * the folio should not be promoted and @new_gen is not set. + */ +static int folio_inc_lru_refs_walk(struct folio *folio, struct lruvec *lru= vec, + const vma_flags_t *vma_flags, + int *new_gen, int *type) +{ + unsigned long new_flags, old_flags =3D READ_ONCE(*folio_flags(folio, 0)); + int refs, gen, max_gen, ret; + unsigned long max_seq; + + do { + max_seq =3D READ_ONCE(lruvec->lrugen.max_seq); + max_gen =3D lru_gen_from_seq(max_seq); + gen =3D lru_get_gen_flags(old_flags); + refs =3D lru_get_refs_flags(old_flags) + 1; + *type =3D folio_flags_is_file_lru(&old_flags); + new_flags =3D old_flags; + + if (gen >=3D 0 && gen !=3D max_gen) { + ret =3D gen; + /* Promote second page table access or executable */ + if (lru_refs_is_workingset(refs) || is_exec_file_folio(folio, vma_flags= )) + *new_gen =3D max_gen; + /* First access only defers eviction of an old generation */ + else if (lru_gen_is_old(lruvec, *type, gen)) + *new_gen =3D (gen + 1) % MAX_NR_GENS; + else + *new_gen =3D gen; + lru_set_gen_flags(&new_flags, *new_gen); + lru_set_refs_flags(&new_flags, min(refs, LRU_REFS_PROTECTED)); + } else { + ret =3D -1; + lru_set_refs_flags(&new_flags, min(refs, LRU_REFS_MAX)); + } + if (new_flags =3D=3D old_flags) + break; + } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); + + return ret; +} + +/** + * folio_inc_lru_refs_isolated - Update the folio's LRU refs + * @folio: The folio. + * @vma_flags: The flags of all the VMAs that referenced the folio. + * + * Similar to folio_inc_lru_refs(), but the folio is isolated. Only used + * on mapped folios upon the final eviction RMAP walk. Since the folio is + * isolated, folio promotion needs to be done by the caller. + * + * Return: true if the caller should activate the folio, false to put it + * back in an older gen and wait for a second chance. + */ +static bool folio_inc_lru_refs_isolated(struct folio *folio, const vma_fla= gs_t *vma_flags) +{ + unsigned long new_flags, old_flags =3D READ_ONCE(*folio_flags(folio, 0)); + unsigned int refs; + + do { + new_flags =3D old_flags; + refs =3D lru_get_refs_flags(old_flags) + 1; + lru_set_refs_flags(&new_flags, min(refs, LRU_REFS_PROTECTED)); + } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); + /* Promote second page table access or executable */ + return lru_refs_is_workingset(refs) || is_exec_file_folio(folio, vma_flag= s); } #else -static bool lru_gen_set_refs(struct folio *folio, const vma_flags_t *vma_f= lags) +static bool folio_inc_lru_refs_isolated(struct folio *folio, const vma_fla= gs_t *vma_flags) { return false; } @@ -884,7 +1050,8 @@ static enum folio_references folio_check_references(st= ruct folio *folio, if (!referenced_ptes) return FOLIOREF_RECLAIM; =20 - return lru_gen_set_refs(folio, &vma_flags) ? FOLIOREF_ACTIVATE : FOLIORE= F_KEEP; + return folio_inc_lru_refs_isolated(folio, &vma_flags) ? + FOLIOREF_ACTIVATE : FOLIOREF_KEEP; } =20 referenced_folio =3D folio_test_clear_referenced(folio); @@ -3155,9 +3322,9 @@ static bool iterate_mm_list_nowalk(struct lruvec *lru= vec, unsigned long seq) * P term over the generations previously evicted, using the smoothing fac= tor * 1/2; the D term isn't supported. * - * The setpoint (SP) is always the first tier of one type; the process var= iable - * (PV) is either any tier of the other type or any other tier of the same - * type. + * To select a tier to protect, the setpoint (SP) is the cumulative tiers = below + * it and the process variable (PV) is the tier itself. Anon/file scan bud= gets + * are handled separately by lru_gen_balance_scan(). * * The error is the difference between the SP and the PV; the correction i= s to * turn off protection when SP>PV or turn on protection when SP=3D 0 && new_gen !=3D old_gen) { if (increased) *increased =3D false; @@ -3299,9 +3429,9 @@ static int __folio_inc_gen(struct folio *folio, int o= ld_gen, bool *increased) =20 new_flags =3D old_flags; new_gen =3D (old_gen + 1) % MAX_NR_GENS; - + refs =3D lru_get_refs_flags(old_flags); lru_set_gen_flags(&new_flags, new_gen); - lru_set_refs_flags(&new_flags, 0); + lru_set_refs_flags(&new_flags, min(refs, LRU_REFS_WORKINGSET)); } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); =20 if (increased) @@ -3309,7 +3439,7 @@ static int __folio_inc_gen(struct folio *folio, int o= ld_gen, bool *increased) return new_gen; } =20 -/* protect pages accessed multiple times through file descriptors */ +/* protect pages selected by the tier PID controller */ static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio, int o= ld_gen) { int new_gen; @@ -3318,6 +3448,7 @@ static int folio_inc_gen(struct lruvec *lruvec, struc= t folio *folio, int old_gen new_gen =3D __folio_inc_gen(folio, old_gen, &gen_increased); if (gen_increased) lru_gen_update_size(lruvec, folio, old_gen, new_gen); + return new_gen; } =20 @@ -3512,25 +3643,25 @@ static void walk_update_folio(struct lru_gen_mm_wal= k *walk, struct vm_area_struc struct lruvec *lruvec, struct folio *folio, bool dirty) { int new_gen, old_gen, type; + unsigned int flags =3D LRU_REF_MAPPED; =20 if (!folio) return; =20 - new_gen =3D lru_gen_from_seq(READ_ONCE(lruvec->lrugen.max_seq)); - if (dirty && !folio_test_dirty(folio) && !(folio_test_anon(folio) && folio_test_swapbacked(folio) && !folio_test_swapcache(folio))) folio_mark_dirty(folio); =20 if (walk) { - old_gen =3D folio_update_gen(folio, new_gen, &type, &vma->flags); + old_gen =3D folio_inc_lru_refs_walk(folio, lruvec, &vma->flags, + &new_gen, &type); if (old_gen >=3D 0 && old_gen !=3D new_gen) update_batch_size(walk, folio, old_gen, new_gen, type); - } else if (lru_gen_set_refs(folio, &vma->flags)) { - old_gen =3D folio_lru_gen(folio); - if (old_gen >=3D 0 && old_gen !=3D new_gen) - folio_activate(folio); + } else { + if (is_exec_file_folio(folio, &vma->flags)) + flags |=3D LRU_REF_EXEC; + folio_inc_lru_refs(folio, flags); } } =20 @@ -3936,8 +4067,8 @@ static bool inc_min_seq(struct lruvec *lruvec, int ty= pe, int swappiness) while (pos !=3D head) { struct folio *folio =3D list_entry(pos, struct folio, lru); long nr_pages =3D folio_nr_pages(folio); - int refs =3D folio_lru_refs(folio); - bool workingset =3D folio_test_workingset(folio); + unsigned int refs =3D folio_lru_refs(folio); + int tier =3D lru_tier_from_refs(refs); bool gen_increased; =20 VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); @@ -3957,13 +4088,8 @@ static bool inc_min_seq(struct lruvec *lruvec, int t= ype, int swappiness) delta +=3D nr_pages; batch_end =3D &folio->lru; =20 - /* don't count the workingset being lazily promoted */ - if (refs + workingset !=3D BIT(LRU_REFS_WIDTH) + 1) { - int tier =3D lru_tier_from_refs(refs, workingset); - - WRITE_ONCE(lrugen->protected[hist][type][tier], - lrugen->protected[hist][type][tier] + nr_pages); - } + WRITE_ONCE(lrugen->protected[hist][type][tier], + lrugen->protected[hist][type][tier] + nr_pages); } else { flush_lru_batch(head, &batch_end, target_list); list_move(&folio->lru, &lrugen->folios[new_gen][type][zone]); @@ -4688,9 +4814,8 @@ static bool sort_folio(struct lruvec *lruvec, struct = folio *folio, struct scan_c int type =3D folio_is_file_lru(folio); int zone =3D folio_zonenum(folio); int delta =3D folio_nr_pages(folio); - int refs =3D folio_lru_refs(folio); - bool workingset =3D folio_test_workingset(folio); - int tier =3D lru_tier_from_refs(refs, workingset); + unsigned int refs =3D folio_lru_refs(folio); + int tier =3D lru_tier_from_refs(refs); struct lru_gen_folio *lrugen =3D &lruvec->lrugen; =20 VM_WARN_ON_ONCE_FOLIO(gen >=3D MAX_NR_GENS, folio); @@ -4715,17 +4840,15 @@ static bool sort_folio(struct lruvec *lruvec, struc= t folio *folio, struct scan_c return false; =20 /* protected */ - if (tier > tier_idx || refs + workingset =3D=3D BIT(LRU_REFS_WIDTH) + 1) { + if (tier > tier_idx) { + int hist =3D lru_hist_from_seq(lrugen->min_seq[type]); + gen =3D folio_inc_gen(lruvec, folio, min_gen); list_move(&folio->lru, &lrugen->folios[gen][type][zone]); =20 - /* don't count the workingset being lazily promoted */ - if (refs + workingset !=3D BIT(LRU_REFS_WIDTH) + 1) { - int hist =3D lru_hist_from_seq(lrugen->min_seq[type]); + WRITE_ONCE(lrugen->protected[hist][type][tier], + lrugen->protected[hist][type][tier] + delta); =20 - WRITE_ONCE(lrugen->protected[hist][type][tier], - lrugen->protected[hist][type][tier] + delta); - } return true; } =20 @@ -4746,10 +4869,6 @@ static bool isolate_folio(struct lruvec *lruvec, str= uct folio *folio, struct sca return false; } =20 - /* see the comment on LRU_REFS_FLAGS */ - if (!folio_test_referenced(folio)) - folio_set_lru_refs(folio, 0); - success =3D lru_gen_del_folio(lruvec, folio, true); VM_WARN_ON_ONCE_FOLIO(!success, folio); =20 @@ -4861,13 +4980,14 @@ static int get_tier_idx(struct lruvec *lruvec, int = type) struct ctrl_pos sp, pv; =20 /* - * To leave a margin for fluctuations, use a larger gain factor (2:3). - * This value is chosen because any other tier would have at least twice - * as many refaults as the first tier. + * To leave a margin for fluctuations, use a larger gain factor (1:2). + * Stop at the first tier whose refault rate is clearly worse than + * that of the cumulative mass of the tiers below it; the PID + * protects the tiers above it. */ - read_ctrl_pos(lruvec, type, LRU_TIER_MIN, LRU_TIER_MIN, 2, &sp); for (tier =3D LRU_TIER_MIN + 1; tier <=3D LRU_TIER_MAX; tier++) { - read_ctrl_pos(lruvec, type, tier, tier, 3, &pv); + read_ctrl_pos(lruvec, type, LRU_TIER_MIN, tier - 1, 1, &sp); + read_ctrl_pos(lruvec, type, tier, tier, 2, &pv); if (!positive_ctrl_err(&sp, &pv)) break; } @@ -5045,12 +5165,11 @@ static int evict_folios(unsigned long nr_to_scan, s= truct lruvec *lruvec, } =20 /* - * See the comments on LRU_REFS_FLAGS. - * - * The rejected folios are never added to the oldest generation, - * so this effectively promotes them by at least one generation. + * Putback keeps rejected folios beyond scanned_seq when possible. + * PG_active and refs are placement hints; cap refs to retain + * workingset history without carrying a high tier into the new gen. */ - folio_set_lru_refs(folio, 0); + folio_set_lru_refs(folio, min(folio_lru_refs(folio), LRU_REFS_WORKINGSET= )); } =20 move_folios_to_lru(&list, scanned_seq, false); diff --git a/mm/workingset.c b/mm/workingset.c index 87d5e55dd684..eea711057c72 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -188,6 +188,12 @@ #define EVICTION_SHIFT_ANON (EVICTION_SHIFT + SWAP_COUNT_SHIFT) #define EVICTION_MASK (~0UL >> EVICTION_SHIFT) #define EVICTION_MASK_ANON (~0UL >> EVICTION_SHIFT_ANON) +/* + * LRU refs uses LRU_REFS_WIDTH + 2 bits, the 2 bits being PG_workingset + * and PG_referenced. The lowest bit is recorded in the workingset field + * of the shadow entry (to reuse pack_shadow()). + */ +#define LRU_REFS_BITS ((LRU_REFS_WIDTH + 2) - 1) =20 /* * Eviction timestamps need to be able to cover the full range of @@ -241,14 +247,13 @@ static void *lru_gen_eviction(struct folio *folio) struct lru_gen_folio *lrugen; int type =3D folio_is_file_lru(folio); int delta =3D folio_nr_pages(folio); - int refs =3D folio_lru_refs(folio); - bool workingset =3D folio_test_workingset(folio); - int tier =3D lru_tier_from_refs(refs, workingset); + unsigned int refs =3D folio_lru_refs(folio); + int tier =3D lru_tier_from_refs(refs); struct mem_cgroup *memcg; struct pglist_data *pgdat =3D folio_pgdat(folio); unsigned short memcg_id; =20 - BUILD_BUG_ON(LRU_GEN_WIDTH + LRU_REFS_WIDTH > + BUILD_BUG_ON(LRU_GEN_WIDTH + LRU_REFS_BITS > BITS_PER_LONG - max(EVICTION_SHIFT, EVICTION_SHIFT_ANON)); =20 rcu_read_lock(); @@ -256,14 +261,14 @@ static void *lru_gen_eviction(struct folio *folio) lruvec =3D mem_cgroup_lruvec(memcg, pgdat); lrugen =3D &lruvec->lrugen; min_seq =3D READ_ONCE(lrugen->min_seq[type]); - token =3D (min_seq << LRU_REFS_WIDTH) | max(refs - 1, 0); + token =3D (min_seq << LRU_REFS_BITS) | refs >> 1; =20 hist =3D lru_hist_from_seq(min_seq); atomic_long_add(delta, &lrugen->evicted[hist][type][tier]); memcg_id =3D mem_cgroup_private_id(memcg); rcu_read_unlock(); =20 - return pack_shadow(memcg_id, pgdat, token, workingset, type); + return pack_shadow(memcg_id, pgdat, token, refs & 1, type); } =20 /* @@ -284,9 +289,9 @@ static bool lru_gen_test_recent(void *shadow, struct lr= uvec **lruvec, *lruvec =3D mem_cgroup_lruvec(memcg, pgdat); =20 max_seq =3D READ_ONCE((*lruvec)->lrugen.max_seq); - max_seq &=3D (file ? EVICTION_MASK : EVICTION_MASK_ANON) >> LRU_REFS_WIDT= H; + max_seq &=3D (file ? EVICTION_MASK : EVICTION_MASK_ANON) >> LRU_REFS_BITS; =20 - return abs_diff(max_seq, *token >> LRU_REFS_WIDTH) < MAX_NR_GENS; + return abs_diff(max_seq, *token >> LRU_REFS_BITS) < MAX_NR_GENS; } =20 static void lru_gen_refault(struct folio *folio, void *shadow) @@ -314,22 +319,26 @@ static void lru_gen_refault(struct folio *folio, void= *shadow) lrugen =3D &lruvec->lrugen; =20 hist =3D lru_hist_from_seq(READ_ONCE(lrugen->min_seq[type])); - refs =3D (token & (BIT(LRU_REFS_WIDTH) - 1)) + 1; - tier =3D lru_tier_from_refs(refs, workingset); + refs =3D ((token & (BIT(LRU_REFS_BITS) - 1)) << 1) + workingset; + tier =3D lru_tier_from_refs(refs); =20 atomic_long_add(delta, &lrugen->refaulted[hist][type][tier]); =20 - if (workingset) { - /* - * see folio_add_lru(), where folio_set_active() is - * called for workingset folios - */ - if (lru_gen_in_fault()) + /* + * Activate a fault-driven refault folio, which would have been + * promoted had it stayed in memory. + */ + if (refs >=3D LRU_REFS_REFERENCED) { + if (lru_gen_in_fault()) { + folio_set_active(folio); mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta); - folio_set_workingset(folio); + } + /* Refault is also promotion, cap the refs like folio_inc_lru_refs */ + folio_set_lru_refs(folio, min(refs, LRU_REFS_PROTECTED)); + } + + if (lru_refs_is_workingset(refs)) mod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + type, delta); - } else - set_mask_bits(&folio->flags.f, LRU_REFS_MASK, (refs - 1UL) << LRU_REFS_P= GOFF); unlock: rcu_read_unlock(); } --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460018; b=5eAHLFbdELB8SzU6a9corYidEu6dIh3sidczEkSTqFIcNXm/e8NKR20Pr+C25MRAAYYD EKU5RrOAHGRwUDGfTSsNpMqUWjWkCUKn+Ua6gRckbeRPJ277fQwLlOBpzazIZdhI2IlXs CWVI/dZKpO+btPiqfHH6Bi/FkIWsJQnAQBuLO8LoBlfbjzTWBEYd8au8k1K+SLDhETdmu JZHAe1Q8UZf2/5bb0gcih+fL2oaWnEWHnHCedVSGcFDTZENKgPNtJqPSZWuxmgctEvMLI 2Dh8JrxOvt9hKXOurpMuwFamFBqzhC4qJlnT/BIFwxsJGec/GYF2e9vcqkXBo6Yenfg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460018; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=G/flV6tJgyZ+NqYZar1xR5I4MimyYfehG8yCe8+zdxk=; b=IjYnj+HvwGeaHGucc4/LUbgtejzOStCTsKpDrOMYQs5BVUpeW8cxM8v8AUUyjyT28zA4 N0e1yQTpYA0mS8UAP9lNZr+3jRX/2gqYyQcJ3PcNi6seIpWzHpgDaHEVRKlmzv8VLROpQ TsCrEZBSVZXeciNgYlICUaFZPWJSYadoAgxIpgEPbpmxMKAu/km3dCbVM6sr4k7U7sTF4 PPWzFAhlR0DcLuxHjY2l/rHkLqDr9Cl3434bXcxvVy70fQRp+f45wy9sz6HcCJDpsk9W0 FPrDGEJsUzgumi8xRcDjkT6u2zXLzXczFAjK3G7HCqTyzUqx/sVd/1KuWKPzqCUJfXw== 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=ddSdKZmu; 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 0EC6A606A336; Mon, 23 Nov 2026 18:53:38 +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=ddSdKZmu 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=1795460017; b=yZ+Ly6+MaGx/dBI1wtxMomHBd8NzvHNfrNhRtSCKGyu+lMh463GNFZ9m6TkEWkWBDcID OHSPlATOB8lxHB5mEE7rEe9vuMR6WOpamgSib+UiInYUohmyjh9/bnBGVai9hreMk8bEx YaOGHvWD05h3lIhvYIXunemPH+gIsvTLfpt7cL6ZnyOoCNiztpyxE3f0H9dBNJlKTvrB0 7GMDbzB/PY86hUTKwo5iiktSF/f9ZaslU2wX1M8oaV+h2sXKXkFBP8xfZTKLAIvtFraXd rfyw/qYK1iRTujho6dXHFzfXVjW6m6R8z3GMWVBH0k71yWE4oweCg5D/TL1V9bfCBwQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460017; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=G/flV6tJgyZ+NqYZar1xR5I4MimyYfehG8yCe8+zdxk=; b=Ld0E6zlHgi8gCyjdwbt6n31HDScSy//eC7xm9us5NGdIiDq9/ucXLOwnn1cJ5PeBrahw MV6EGFzgoCMzB32qQW9r1FR8JbXRjI+UumcWdB+MiF0gZmx0kkCPiNdEiNar3Hq0l06Or KKn8alORszXeZaZ0BGk+cRWcGXJZH3Krp7gsATUH6n6onkHN661GgL7PgXdoAxpHt77Cs LTFAkAT4vw/BD19fl4Fp1F5W5lHZaRomvm+6IX1uQqti7PO4dsdDvfTyKVTcxgriKJw0Y XlHp5PflY1D1aFuQsI99IO2eZEW+Er6vhdSDy/zmtwaO4K93Thwoymlxhav6H1iTGpw== 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 AB38B1F000ED; Mon, 23 Nov 2026 18:53:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460017; bh=G/flV6tJgyZ+NqYZar1xR5I4MimyYfehG8yCe8+zdxk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=ddSdKZmuXc/Z48Iyq8+CPCokefKnnSVIZJRPuaHFnJp8PPcGlMfuAv/KIGrrdYii/ 8Ixh/n/SNQCnCCLhuFrsN95vE4B+oOhZNq7J5wS8QsesTDEIdJVfENl5KhMY3BTFFA jRM5lLXAO8rEXfpmBQqku696hnwzwmKgNf5m22R4eQAW5v3yeYeiyfdWLekcKbtYTp pIIjGUQgjTWN+bFOHqCryKGc8nUMlkCr+eLF+m9zmnO551r8tq9rxjCJWRmPjYpcJk ljP063qM65/xqskbCCjATImhDmqNmakQuevxwyBUTqyg8TSwnEipCk2wBchXEkM4Vd Wcdj5t2ezg8lw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 080574C649; Mon, 23 Nov 2026 18:53:01 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:53:00 +0800 Subject: [PATCH v6 08/18] mm/mglru: make folio lru referenced times count a generic API 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" Message-Id: <20261124-mglru-fg-v6-8-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=4795; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=PBYnbcUr9dUAP8lVbFcm0yrXQShhXuM2Q3GvdJcxzZ4=; b=4IBQ/rfrcPb4A6znz9CjQVL0ric1Ec41qoBxJYerdLkN9zyX5+X8c7XbuMh+IFCahStZfmZzL V6ntTsPl53LDvoF9eCtZGnxiDKfw/BcvCiB+68Ie6SrKJ6RXj4uonwS 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song Pure code shuffle, no behavior change. Make the folio LRU refs helpers available regardless of CONFIG_LRU_GEN. Without MGLRU the extra bits are never set, so they are inert. Move the lru_gen_* helpers into the CONFIG_LRU_GEN section. This prepares for unifying the API for checking folio referenced and workingset status. folio_migrate_lru_refs() now migrates the complete refs count, including PG_workingset (bit 1 of the encoding), bitwise identical to the direct copy dropped from folio_migrate_flags(). lru_set_refs_flags() skips the nonexistent mask bits when LRU_REFS_WIDTH collapses to 0, preserving the two independent flags for !CONFIG_LRU_GEN builds. Tested-by: Zhao Zhenzhuo Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) --- include/linux/mm_inline.h | 55 ++++++++++++++++++++++++-------------------= ---- mm/migrate.c | 2 -- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index f38dad1198fc..180db43c3a85 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -184,6 +184,12 @@ static inline int lru_get_gen_flags(unsigned long flag= s) return gen; } =20 +static inline int folio_lru_gen(const struct folio *folio) +{ + return lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0))); +} +#endif + /** * lru_set_refs_flags - Set the LRU referenced count to folio flags. * @flags: pointer to the folio flags @@ -206,7 +212,8 @@ static inline void lru_set_refs_flags(unsigned long *fl= ags, unsigned int refs) *flags |=3D BIT(PG_referenced); if (refs & BIT(1)) *flags |=3D BIT(PG_workingset); - *flags |=3D ((unsigned long)refs >> 2) << LRU_REFS_PGOFF; + if (LRU_REFS_WIDTH) + *flags |=3D ((unsigned long)refs >> 2) << LRU_REFS_PGOFF; } =20 /** @@ -252,13 +259,31 @@ static inline void folio_set_lru_refs(struct folio *f= olio, unsigned int refs) } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); } =20 +#ifdef CONFIG_LRU_GEN void folio_inc_lru_refs(struct folio *folio, unsigned int flags); +#else +static inline void folio_inc_lru_refs(struct folio *folio, unsigned int fl= ags) +{ + /* Should not be called with !CONFIG_LRU_GEN */ + WARN_ON_ONCE(1); +} +#endif =20 -static inline int folio_lru_gen(const struct folio *folio) +/** + * folio_migrate_lru_refs - copy the reference state to a new folio + * @new: the destination folio + * @old: the source folio + * + * Transfer the reference state to @new during migration: the MGLRU + * refs count, or PG_referenced and PG_workingset (bits 0-1 of the + * encoding) for the active/inactive LRU. + */ +static inline void folio_migrate_lru_refs(struct folio *new, const struct = folio *old) { - return lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0))); + folio_set_lru_refs(new, folio_lru_refs(old)); } =20 +#ifdef CONFIG_LRU_GEN static inline bool lru_gen_is_old(struct lruvec *lruvec, int type, int gen) { unsigned long min_seq =3D READ_ONCE(lruvec->lrugen.min_seq[type]); @@ -424,20 +449,6 @@ static inline bool lru_gen_del_folio(struct lruvec *lr= uvec, struct folio *folio, =20 return true; } - -/** - * folio_migrate_lru_refs - copy the reference state to a new folio - * @new: the destination folio - * @old: the source folio - * - * Transfer the reference state to @new during migration: the MGLRU - * refs count, including PG_referenced, or just PG_referenced for the - * active/inactive LRU. - */ -static inline void folio_migrate_lru_refs(struct folio *new, const struct = folio *old) -{ - folio_set_lru_refs(new, folio_lru_refs(old)); -} #else /* !CONFIG_LRU_GEN */ =20 static inline bool lru_gen_enabled(void) @@ -470,16 +481,6 @@ static inline bool lru_gen_del_folio(struct lruvec *lr= uvec, struct folio *folio, { return false; } - -static inline void folio_inc_lru_refs(struct folio *folio, unsigned int fl= ags) -{ -} - -static inline void folio_migrate_lru_refs(struct folio *new, const struct = folio *old) -{ - if (folio_test_referenced(old)) - folio_set_referenced(new); -} #endif /* CONFIG_LRU_GEN */ =20 static __always_inline diff --git a/mm/migrate.c b/mm/migrate.c index 65869b8acec5..0430197fdc04 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -807,8 +807,6 @@ void folio_migrate_flags(struct folio *newfolio, struct= folio *folio) folio_set_active(newfolio); } else if (folio_test_clear_unevictable(folio)) folio_set_unevictable(newfolio); - if (folio_test_workingset(folio)) - folio_set_workingset(newfolio); if (folio_test_checked(folio)) folio_set_checked(newfolio); /* --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460015; b=3wme9w5GLmWya32tSjo4wLa/wKOG21enlPbSGtO9hptgj5h1fQ0RzoTEABfPqDoWWJ4N SkOlcINrZY7v8j+14poTKaNcOnaiyxFAiQgF7L9aJ5y4r8V1z7YyKaxDKVfCaf6p0mnEA ND91Pt/JCi2trnlO1zUJo+ChvvxDPDA08UFXdgaPFBFItw7WaLKJDxIx59J0ZFzFBYyEC wo4AeDdkNUR/TIxKUse+5qDzZlOW375h+6GF9RLai37m91FVS2H3G483PnM92NQHRa/DO Jq+BcvixOevaYy2m9VsV5PDBxyLCguFMBIsyw/jhv0Iwm73IQobu8/N+KSe2/PPgYCg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460015; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=RNY/s9d03b2O9JkaUTE9InrLkFGqrNIrXDiipCDaU1M=; b=ekLeCvvAgAVVb/zj0jFKdMYdPYnFbHNlBq+YM3LSoz0SmmIROM6BVeikHB49SoQup5i9 pAs4ceEXDHvwFXHfi7K3MGm0JDhVaczJuMbejK2NhT6Q3455NPXZuILlEsA5/aAZg1IoT pu4almeagZ9udUB54GXcGUnCv3DX9JRc6nIo9vJOezQpu8FN1KRX7wmNPTtybendAJIm3 B1nRprPqGY0fVKAbIiN1QhtbD+t0jwp+dEnAU/JeNqjBqTHrwycnx3TBefipH+8jJVg7P smawn4pSXHIV+L1920bmkXjur9cPrW2BPRzE1wCIMUS95TqiOhIG4l3BrbxtXqLcLbw== 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=HeSHWslK; 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 E3BA9606A684; Mon, 23 Nov 2026 18:53:34 +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=HeSHWslK 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=1795460014; b=wwCHX3jGEleRCtHJ/Qt4lAQ6yRK9wug5dBCSdGkiXoqZ9LwmzkGQrYrTLOpAPQg5h3q5 jA+YydmQyd+4eY7hhL/x0CxJhNDqUMrhheN/GrWWjYZWlclQjpnGooEh9dZHEFw0JepEN wgqiswg+kvg5p1zOJNTB17ETuoClPzuRC0Ki+SHdZebkYT5rsTkoF501AwewWMlmdn3cf g6+Laa+huzGww3OE3OcJsx08Ndhjfgq65vcHTh/FvvWJNY+wUzkany9kGiuBdsfseALE5 fsYQ+XpO5LfYurv6m3gx34p5C9fhaXS1Ref9/opZc0vKzUxDY9lPKuMnuVZiBOpFCWg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460014; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=RNY/s9d03b2O9JkaUTE9InrLkFGqrNIrXDiipCDaU1M=; b=Z2pUHQ5jxUUQTPq5MjqdGNY/W+kd/XZIvqBc0sqM2DjuVM6i2NQja+uaLX/V9NRnYiZP gQwz+YhfbWgNV0XOQTz7KdFnNhx8XUyLKA6ry7gQvx7TiuO8NuAUmqS/DS4nNutzkYpm4 7MmL7Wzu2bjSwYBnqFw29N87xom/oZEEcsvf9j2blHqchl1zjReci+DU4+QRxu/zJta3S 5gqY8Whj2clZMdyyD3CwXi8F7Q0FpLU7ThGpUlGYe09CFKWA5kmTrPfnLOT8QMgWb73SJ afa3vZ1uq7OE02IrgmgmvEVWDk68WSehBhMauletCkalytgoq2k+kcyEp8sPd1dr1gQ== 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 AEDC81F000F0; Mon, 23 Nov 2026 18:53:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460014; bh=RNY/s9d03b2O9JkaUTE9InrLkFGqrNIrXDiipCDaU1M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=HeSHWslKwcNQJrvUXhAfACNEoLxqySJCinZ3W2/1DHdLvX/Yx7DBitsIX+3aehawn 9+32yaYqbqTr7y0LDLBJ30MBEqrCMpnWNMN7Ucz4ownA9EoKUQrEg5iZL8wOLeMhHI qgrJWxswwZPk2+NNxjJMkvUAKLpNyR0l7FLsO4GF+wPfrQCchXU1kH8mIrlrXxDr7c pkO4V7MWfJ8exNGtkHsDU3IqpVhT92Y4xSkU/9F+Rlic/LKc97Jew55yiUhQHtbyBU CJtiPiuLuy+RO1jc6IMXJRCxg4vYWH4rtY4iYSxUsYOTg6h9pkRCauV77OnCCRQWCx 4graAwGIr+kEw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 28BEF4C64D; Mon, 23 Nov 2026 18:53:01 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:53:01 +0800 Subject: [PATCH v6 09/18] mm/mglru: move add/del LRU size accounting out of lru_gen_update_size() 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" Message-Id: <20261124-mglru-fg-v6-9-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=3303; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=XjHe+HTYVXiCfy5n6y3gV9Xu+nsGkLXvrSZveSG9Y+A=; b=6PcLKWJPkD9Wt9zW/Q9V+XxxJuDjPsiQTYZQZ7sdj63T1Q8ux+NwJLNGLG1Ut3i3fZi9bdzti /HiciYRGuqEAXfzhybTHfHBrA7MVjcWL0sl4vcUv4YI6NtgyPflAA1h 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song Pure code shuffle, no behavior change. lru_gen_update_size() now only updates the per-generation counters plus the promotion move; the addition and deletion active/inactive accounting move to lru_gen_add_folio() and lru_gen_del_folio(), which know the generation and can apply the active-window test directly. The unevictable case is untouched: unevictable folios never enter lru_gen_add_folio() and keep being accounted by lruvec_add_folio() as before. Tested-by: Zhao Zhenzhuo Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) --- include/linux/mm_inline.h | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 180db43c3a85..b15c3618a1f2 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -322,21 +322,9 @@ static inline void lru_gen_update_size(struct lruvec *= lruvec, struct folio *foli if (new_gen >=3D 0) atomic_long_add(delta, &lrugen->nr_pages[new_gen][type][zone]); =20 - /* addition */ - if (old_gen < 0) { - if (lru_gen_is_active(lruvec, new_gen)) - lru +=3D LRU_ACTIVE; - __update_lru_size(lruvec, lru, zone, delta); + /* return now if not a promotion */ + if (old_gen < 0 || new_gen < 0) return; - } - - /* deletion */ - if (new_gen < 0) { - if (lru_gen_is_active(lruvec, old_gen)) - lru +=3D LRU_ACTIVE; - __update_lru_size(lruvec, lru, zone, -delta); - return; - } =20 /* promotion */ if (!lru_gen_is_active(lruvec, old_gen) && lru_gen_is_active(lruvec, new_= gen)) { @@ -391,6 +379,8 @@ static inline bool __lru_gen_add_folio(struct lruvec *l= ruvec, struct folio *foli int gen =3D folio_lru_gen(folio); int type =3D folio_is_file_lru(folio); int zone =3D folio_zonenum(folio); + int delta =3D folio_nr_pages(folio); + enum lru_list lru =3D type * LRU_INACTIVE_FILE; struct lru_gen_folio *lrugen =3D &lruvec->lrugen; =20 BUILD_BUG_ON(BIT(LRU_GEN_WIDTH - 1) !=3D MAX_NR_GENS); @@ -408,6 +398,10 @@ static inline bool __lru_gen_add_folio(struct lruvec *= lruvec, struct folio *foli set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK | BIT(PG_active), flags= ); =20 lru_gen_update_size(lruvec, folio, -1, gen); + if (lru_gen_is_active(lruvec, gen)) + lru +=3D LRU_ACTIVE; + __update_lru_size(lruvec, lru, zone, delta); + /* for folio_rotate_reclaimable() */ if (reclaiming) list_add_tail(&folio->lru, &lrugen->folios[gen][type][zone]); @@ -432,6 +426,9 @@ static inline bool lru_gen_del_folio(struct lruvec *lru= vec, struct folio *folio, { unsigned long flags; int gen =3D folio_lru_gen(folio); + int zone =3D folio_zonenum(folio); + int delta =3D folio_nr_pages(folio); + enum lru_list lru =3D folio_is_file_lru(folio) * LRU_INACTIVE_FILE; =20 if (gen < 0) return false; @@ -445,6 +442,9 @@ static inline bool lru_gen_del_folio(struct lruvec *lru= vec, struct folio *folio, gen =3D ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1; =20 lru_gen_update_size(lruvec, folio, gen, -1); + if (lru_gen_is_active(lruvec, gen)) + lru +=3D LRU_ACTIVE; + __update_lru_size(lruvec, lru, zone, -delta); list_del(&folio->lru); =20 return true; --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460015; b=CI2EXAxEGfzItRfHV89fKyAjlTZDrS+82StNKBV/1yuaK5hIQDmfukSORNGftoLVx9PD 33U/EF9sYR1E06sygao7OAauPPF2T5RryfKzIZImKe771N6VBt8NSRXb1HtHan0iVebM+ Ovt3ZJsXvt9eSeVsIS9Rp/7kfHubimEAAi5+aAUo9nW50RTVevcfo6kFCVjfglMHj75tQ O/hfGRJxhuLSlDNfQUNktZc4kQGhGgtS8MtvQf/NzJcqQKv57LM2ule9CNt+3E6Gw8i2T HD/v7W83SEl1Pnw+02/rMgStpCqb9/aIkCLvKsUrPPtn2p+/mbXcnESPtwMnVlM/0bw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460015; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=qPY6Gpk1GxeXHfsNbxjcPs+9GQpLxPguF+KSpyU0BSA=; b=Mu//PEHc3oSikhMg/PFHD6B+QHeCOQZyTZU4FuDES2tMbSxK5gVVwPbZRfrXnTb9e6vo pV0VFwn4qt891ljM+j6f0IEiVpgNrJXKz+hpccQyXeHlBI14kLLNeEp9crS8wTH0t0jYy oFr2OHuV8KWQUi4lEZ1acmwgRDixgJqRw/FE/91ubRu+uZNl2X7mS1tnRRMZDnxtxXVti CgHCKtbaXCuisrgz13xQvahkwcQcjqvslg47nazMte3tckf2NI4jDKPvBwjVdjwPcf3gd RsLhYBAiASr+S7PYC6mMwFG+oq6Lnmbl4FWIJ6HRd6M7oCH+dnwYWcfcpCunFZLI2rQ== 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=oaYagua1; 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 E4081606A687; Mon, 23 Nov 2026 18:53:34 +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=oaYagua1 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=1795460014; b=teJzzcnnBi2t4vXM0jfMZGj2/53zzEi8eChdL8czxgK+GhIgqyyacwWzKF5ncc0jVMHg 67XGSQtAdJPQx8nblHQF05rTCJPErvMhjhDu6OzPC6nV8q6V0w8u10dwfnf3kHpzMFHL6 hlHBNkdaV/WiIrZxG9ivSHSj51/3Fcx/SM/kzQQWI6dh/VDej01AhRtfc7JfnOAy6yRm8 L85afhPm3sApO3wGixOQMzpVCPzQQL+BuGFs48qZ1riYxxvXpqZHccYpgdNOvA9Lf3zSR jKh9kaf/OVUseKP2YH65+2haORO45ajLYpi25f9RMFUKsGK51o/PC6iHSKB6L6/hy2g== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460014; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=qPY6Gpk1GxeXHfsNbxjcPs+9GQpLxPguF+KSpyU0BSA=; b=yOX3Q+N9ThgmNTXp85EKDTsGXFyBNjSbotncMMmVBNe6u0qGWpr9KX55HdxLIeS9Csco 61odxqceje9ez2J5VAQulqViklJNj//c0WatILZYT+qqm/RX2qJNcB8ykz09wa0C50cLk fxcUEtu8aJOyuvf9wD4driOjxq2nJBnByL4VsGY+hLkhCH4CJeDR07DJ1SLMqQ0gqijXu Buk/ke0VR10B9xHfjhT1zs14dUdU7xHimobiRV9m3w9PYXbhKUCmvv3bZEuWdo64XM/Q1 7VbgZXCO4RaOIKjtQJ/LThnQsTT7fQv3BzVWabd5EirfAXEGTtxd2XxRarY3TRi97ZA== 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 7E5531F01566; Mon, 23 Nov 2026 18:53:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460014; bh=qPY6Gpk1GxeXHfsNbxjcPs+9GQpLxPguF+KSpyU0BSA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=oaYagua1ueuIU09SpzMk7Ekb46Ym2rdWFTkD0tVVNT2lFLW2IdKsG8v+39WgjICpX 5oz2wNqu1mKBmCWOxARlrKjBb88GwIO/wGtRe0QrhoyzDlIeOMtegVjPUh+SrHMQDL 8BuAplqbtWy4fOHUkwnjFmdKs70sSgKa/H7JtPrQIzB+qx0mbdu7b3DHxLgefw4yi3 EqL9CY6CDxvHyYlMerugvfBD6jgVXSxKaVIhw4P2lyVOUr/I8u1uHGfdsD/LPPXsd1 2KrEtbd+/UnRSh0jLQma28CEFVvo3fkzVtZ5M+lM9k0hdU+GwnENnyZEGLpMFYGRFK k6aPpj872ajHw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 471CE4C651; Mon, 23 Nov 2026 18:53:01 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:53:02 +0800 Subject: [PATCH v6 10/18] mm/mglru, gup: mark folios referenced via a fast helper 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" Message-Id: <20261124-mglru-fg-v6-10-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=5455; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=EDvLpnxrpVD1Kfz+v/3OI17Dk6Yj7fBAiXtjWEmtcfY=; b=n7lufqexladK34F/awLOMu/YkEQJxZMmTJft2kkPNiKhixsNSlw2tnT0Eri4RZ4Nl8hmLaSz7 plNCLhLOSEeDefNIfoeK4sf8RYsnkDXloTqw6+THIDruqVfCyK7ORqH 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song Introduce folio_inc_lru_refs_fast(): raise refs from 0 to LRU_REFS_REFERENCED via cmpxchg, or just set PG_referenced for the classical LRU as before. No counter update or promotion is needed, nonzero MGLRU refs are left unchanged. The classical path is taken for a folio that is off the gen lists while MGLRU is off, not on lru_gen_enabled() alone: while MGLRU is being turned off, a folio can still be on a gen list after the static key flips, and setting PG_referenced there would bump its refs count. That check uses lru_get_gen_flags(), so move the helper out of the CONFIG_LRU_GEN block, it is now built with the option off, where LRU_GEN_WIDTH is 0 and LRU_GEN_MASK is zero. Keep the decoded value in an int so the subtraction of the offset-by-one encoding cannot fold to ULONG_MAX before it happens to avoid -Woverflow error with CONFIG_WERROR. This helper only aims to be fast, it exists so the gup fast path can stay a fast path. The new helper doesn't account for refs above LRU_REFS_REFERENCED but that's fine for now as GUP never used that or anything similar for CLRU either. Convert the gup fast-path sites. No behavior change for classical LRU, no regression for MGLRU either. Just prepare for the following changes by providing a helper that follows the new convention. Tested-by: Zhao Zhenzhuo Reviewed-by: Kunwu Chan Signed-off-by: Kairui Song Tested-by: Andrew Morton Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) --- include/linux/mm_inline.h | 62 +++++++++++++++++++++++++++++++++++--------= ---- mm/gup.c | 6 ++--- 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index b15c3618a1f2..13c8b6bce349 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -105,6 +105,22 @@ static __always_inline enum lru_list folio_lru_list(co= nst struct folio *folio) return lru; } =20 +/** + * lru_get_gen_flags - Return the LRU generation number from folio flags. + * @flags: folio flags + * + * Returns: A number between 0 and (MAX_NR_GENS - 1), inclusive. Returns + * -1 if the flags indicate the folio is off the list (e.g., isolated). + */ +static inline int lru_get_gen_flags(unsigned long flags) +{ + int gen =3D (int)((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF); + + /* The gen is stored offset by one, so zero means off-list */ + VM_WARN_ON_ONCE(gen > MAX_NR_GENS); + return gen - 1; +} + #ifdef CONFIG_LRU_GEN =20 static inline bool lru_gen_switching(void) @@ -168,22 +184,6 @@ static inline void lru_set_gen_flags(unsigned long *fl= ags, int gen) *flags |=3D (gen + 1UL) << LRU_GEN_PGOFF; } =20 -/** - * lru_get_gen_flags - Return the LRU generation number from folio flags. - * @flags: folio flags - * - * Returns: A number between 0 and (MAX_NR_GENS - 1), inclusive. Returns - * -1 if the flags indicate the folio is off the list (e.g., isolated). - */ -static inline int lru_get_gen_flags(unsigned long flags) -{ - int gen =3D ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1; - - /* Exclude the legal -1 from the unsigned MAX_NR_GENS comparison */ - VM_WARN_ON_ONCE(gen !=3D -1 && gen >=3D MAX_NR_GENS); - return gen; -} - static inline int folio_lru_gen(const struct folio *folio) { return lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0))); @@ -483,6 +483,36 @@ static inline bool lru_gen_del_folio(struct lruvec *lr= uvec, struct folio *folio, } #endif /* CONFIG_LRU_GEN */ =20 +/** + * folio_inc_lru_refs_fast - Bump folio refs count without promotion. + * @folio: the folio + * + * Raise refs from 0 to LRU_REFS_REFERENCED and leave hotter folios + * untouched. For a folio off the gen lists with MGLRU off, a plain + * PG_referenced set. + */ +static __always_inline void folio_inc_lru_refs_fast(struct folio *folio) +{ + unsigned long new_flags, old_flags =3D READ_ONCE(*folio_flags(folio, 0)); + unsigned int refs; + + do { + refs =3D lru_get_refs_flags(old_flags); + if (lru_get_gen_flags(old_flags) < 0 && !lru_gen_enabled()) { + /* For CLRU, just do folio_set_referenced() */ + if (refs & LRU_REFS_REFERENCED) + break; + refs |=3D LRU_REFS_REFERENCED; + } else { + if (refs) + break; + refs =3D LRU_REFS_REFERENCED; + } + new_flags =3D old_flags; + lru_set_refs_flags(&new_flags, refs); + } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); +} + static __always_inline void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio) { diff --git a/mm/gup.c b/mm/gup.c index 7605da0e06d8..da04bebfff43 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2921,7 +2921,7 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pm= d_t *pmdp, gup_put_folio(folio, 1, flags); goto pte_unmap; } - folio_set_referenced(folio); + folio_inc_lru_refs_fast(folio); pages[nr_pages] =3D page; nr_pages++; } while (ptep++, addr +=3D PAGE_SIZE, addr !=3D end); @@ -2990,7 +2990,7 @@ static unsigned long gup_fast_pmd_leaf(pmd_t orig, pm= d_t *pmdp, =20 for (i =3D 0; i < nr_pages; i++) *(pages++) =3D page++; - folio_set_referenced(folio); + folio_inc_lru_refs_fast(folio); return nr_pages; } =20 @@ -3032,7 +3032,7 @@ static unsigned long gup_fast_pud_leaf(pud_t orig, pu= d_t *pudp, =20 for (i =3D 0; i < nr_pages; i++) *(pages++) =3D page++; - folio_set_referenced(folio); + folio_inc_lru_refs_fast(folio); return nr_pages; } =20 --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460017; b=nJACq6MGfUpmgCq95MdhSNVgCNXk4nI3DC1PuhiDD/Y/YIi1yRtRtwgsS0KTU8AiALLn Mh9bn8cemooe6KEqJRt7S+BFaAQVs6GgOjn/ARaKZ4mVHRIUrTvqd4fGo3fRod4Aynb/3 CV1qMdDVlhWT/6bmImDSgSvjcDcVzNN/K9+w14Is+/pVvpj6ax8FCYL6DF1kqHyEW04Y/ FLZptGCrvy5BVMLqoo1CyYX9mAPRuC+JfQyP3m8hfkMVYsTjMzKU8bNl0EHxx8HU5LtzR 2A0SDkLUi2ha+J5WYDKIp6EgXRfA8W6CJjrFVYCbAXQi7ngCnku9UyfScf+R1B6G3Zw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460017; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=horbJ+WRFpXrHIUpt9mddEfhWXyxTruu2VhXlMsfh58=; b=akXpbPZQYOP3YcU9+yHE0KICnrluccy9RGxEGagEDpJK+ws1wnjAVNVp9Kopi90EhqKh 9XauR7axLQa9cvBY9j2/aIaQwsWDnSLDhwIDrvtyNl0D+jM/6HnkJrL+4NEatO8H7d2pE 1vZ0O7DK4jfGuJoMNG3NDLcjV/CUngXtzdhOpSoP6Lp6RGYtPn28MZL+3LazvJ4os1ZFd QneMjZGaDG1zsBZuBFe8S5ZBc0y/rLmSjSLP/+EADdBFtieVFNiPZvYQMp5IVpb9fD2Gn 3/ZdEEmlhnNuqO+PdSh3Jqi4zv5/4NzX9UXwdG4zgP+oFViNgY/hLfAm1dwuvnYFygA== 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=EMsjoHrk; 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 E9D7E606A690; Mon, 23 Nov 2026 18:53:36 +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=EMsjoHrk 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=1795460016; b=plPWeGjfEZ3bTdKOz55h8saeWZEkJpR5jJmu3r+CiirM8+r5fJ1HBC/hLRGeQoClnvTG Nhp8O/GuPFmxEe5s8HlCQw2KspweuhtGB9sAquAe2gPhDwWyTd6nLQnuNc2U+Rv42oxnP y0XXmrOB/+d8Adw4i7loX0HrckleIMGgkx3k1SQuybOQBccG5E4Q1Nw/eDcd7gl6QV7TT Bazsx/Jbp1lIjbVqSKSlUeVoE5pJc9OscXkewRjbZkMgnyUWjruWpioh6VOsP5WDyn9u4 GT89h52s+ilVOVLFXD6O2BamyA54QC0hScyO+sQ4oSb/votV2xQVSQWBjsIW6drF76g== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460016; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=horbJ+WRFpXrHIUpt9mddEfhWXyxTruu2VhXlMsfh58=; b=WqPWsg9ANHJgvoBLVS8ZdA8TfFSwwu2mBmh2nzs3GySdO1wYHKbrfTKywhVBxA+pobSF yiIcu+w1PRbERkxFn6wd1TZaz5/bTlEEC17IL2BcxdtihChciWZ20Hd/2pUUFgcdgjOGy lkgRCVUYtNzkQefV/UGmEEB+woadsi68reDdENye+UbhPL81aZ25sBvSEAwiPbQPKVqCQ 0VmvbngRhXyhMvu0m/MClgwGc33SbaR35BSgU9BVoMVaG4QhJBkSE2QIor0NTpRjSuypl 0h0sNj2Piov1K3ywHRtrQ/Cq4J+Yx2fVnQ0X4IXhJT6+DNZ2UdyioLerGQphsQ+Xf4g== 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 025A21F00A00; Mon, 23 Nov 2026 18:53:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460016; bh=horbJ+WRFpXrHIUpt9mddEfhWXyxTruu2VhXlMsfh58=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=EMsjoHrkoukJH0WJ6cAQrzxLVECxGQbZiHjN1a1TCOGrqYP9Gj2bMlxns7MB/sW+2 rlKkWPVtRLr+9DUibY5bZIJthJgLxVXBOkI3lrsVjoKkB1csrtp1g0SC/ZKMxLyeji u81j4IIHhaEvkF3O3RZoDfq7rKxoXxPR3YLwP47kzYWNAsTFrUryBNKfHQzjcRhcZB pmQIC5Ul1eSfmIUFBLLjWwcAeU1KO7/QnRlBYfM1fgx1umzJaTXJy2N6/E2oJG1EZG 7QOv18bsyrs4cbPrgG23Mappd3UkKAMskDueMXW60q3LQZyFEA2Jm9J/ognhvHmIZm rrWn9zqcnyv1A== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 630B84C655; Mon, 23 Nov 2026 18:53:01 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:53:03 +0800 Subject: [PATCH v6 11/18] mm/mglru, smaps: convert to LRU refs based operations for MGLRU 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" Message-Id: <20261124-mglru-fg-v6-11-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=3877; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=SyGC47j2sShmDrstNnpICQjj8oxfwuGK/s3C+V+Hk1k=; b=zDeAcKA97ZXD5+wk30UPTAW0+f1NPxG7RvOr5muhguEVkFZjwS92VJecDA26puiKkUFemGJIY FU8Dgte6mVHAd28IbxPk82XmytTsRUsfRtz/LrUROT9BqXOCuZBS2gq 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song PG_referenced is only the lowest bit of the folio LRU refs count (LRU_REFS_FLAGS) now, and MGLRU never kept it stable: promotion used to clear it, and now it toggles as the count grows. But with the LRU referenced count as a stable API now, we can use two helpers to provide in-kernel users a stable and sane reading of the referenced status of an LRU managed folio. Add folio_check_lru_refs() and folio_clear_lru_refs(), which use the refs count when MGLRU is enabled and PG_referenced otherwise, and switch the smaps paths over to them. The lru_gen_enabled() test stays inside the helpers, so its static key is not splattered over the few call sites. Convert smaps as the first user of them. MGLRU now reports every folio with a nonzero count, so the reading no longer flickers and clearing no longer results in an ambiguous status. The classical LRU case is unchanged. Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) Tested-by: Zhao Zhenzhuo --- fs/proc/task_mmu.c | 6 +++--- include/linux/mm_inline.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 540c936aad47..d2a524c6ad4b 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -890,7 +890,7 @@ static void smaps_account(struct mem_size_stats *mss, s= truct page *page, =20 mss->resident +=3D size; /* Accumulate the size in pages that have been accessed. */ - if (young || folio_test_young(folio) || folio_test_referenced(folio)) + if (young || folio_test_young(folio) || folio_check_lru_refs(folio)) mss->referenced +=3D size; =20 /* @@ -1690,7 +1690,7 @@ static int clear_refs_pte_range(pmd_t *pmd, unsigned = long addr, /* Clear accessed and referenced bits. */ pmdp_test_and_clear_young(vma, addr, pmd); folio_test_clear_young(folio); - folio_clear_referenced(folio); + folio_clear_lru_refs(folio); out: spin_unlock(ptl); return 0; @@ -1719,7 +1719,7 @@ static int clear_refs_pte_range(pmd_t *pmd, unsigned = long addr, /* Clear accessed and referenced bits. */ ptep_test_and_clear_young(vma, addr, pte); folio_test_clear_young(folio); - folio_clear_referenced(folio); + folio_clear_lru_refs(folio); } pte_unmap_unlock(pte - 1, ptl); cond_resched(); diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 13c8b6bce349..0f470ddf5c48 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -513,6 +513,37 @@ static __always_inline void folio_inc_lru_refs_fast(st= ruct folio *folio) } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); } =20 +/** + * folio_check_lru_refs - Test if an LRU managed folio has been referenced. + * @folio: The folio to test. + * + * Returns true if the MGLRU refs count is nonzero, or PG_referenced is se= t, + * which is also correct for the non-MGLRU case. Kept as a separate helper, + * to avoid splattering the lru_gen_enabled() static key. + */ +static inline bool folio_check_lru_refs(const struct folio *folio) +{ + if (lru_gen_enabled()) + return folio_lru_refs(folio) > 0; + return folio_test_referenced(folio); +} + +/** + * folio_clear_lru_refs - Clear the referenced state of an LRU managed fol= io. + * @folio: The folio to clear. + * + * Reset the MGLRU refs count to zero, or clear PG_referenced, leaving + * PG_workingset alone for the non-MGLRU case. Kept as a separate helper + * just like folio_check_lru_refs(). + */ +static inline void folio_clear_lru_refs(struct folio *folio) +{ + if (lru_gen_enabled()) + folio_set_lru_refs(folio, 0); + else + folio_clear_referenced(folio); +} + static __always_inline void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio) { --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460019; b=SRu82UGy6C6sHV6z5tiDWuQ56QoXKKGry9NdH8vNfjfUnDWiwsBPqooGQYWxSlWE5IkQ PEDLG0vROFzNVknzitdw2sgjNDDOFHzuAnh10ilOvc/YtbGhmbRHOgW/cAWvBagC2PjAV cZVu2Zu+TiU0tFHtZTfjXOVa8PsV5iFfYZmn7iz39jjO74JdtOaI4G267R/Icbop1sbEc gZNgFt1ngqyezL/ZONnta864Y/Fd/y7GZVEeoaX1O/sJXmq8jBFkJji11XERnIf1+b6TM B2kKQ1NF6+LJFywJg4Ta0Ob7eZSSkYP6A5O2m5jdsnTiDSSxzf8PoNGmnxlrZORbjeg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460019; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=Fy4iqtyOKVldxfrRoRe9ktEnWMk4sYM46noWj6UrSRw=; b=LB7jSQvqaJP5wv3vBHWuExfpIylGUWVN0T9zHNflK0vEwVnvkQJyW4gHcC+N1EPYOIIX qhFey3AVqDoVCXdXGGZ8u/L+vb3/gz17VJw7dEWJZmw66yx4R2f7bwmvz/ZKG51pV/SeD ZfdIex9i8lJ2d6lmDkNbJasym8Mcfpezt157xe06Udv2odOVVM2WB8i4OshWwnfoShNwA 2xC816e6fQ1O6plR7UsvHwyt2WFdku0cmj1f2bkk7vC2qP7lxUD7jVsDbC98ITYt7ZLMG ntGdYvofxH/hBzxOLgYQo2mrzvy29bd8RLy4k2Xg4lUgylEsXkLzE/d4oEBlNlpqFrA== 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=WRrWw7sz; 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 7005A606A31A; Mon, 23 Nov 2026 18:53:39 +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=WRrWw7sz 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=1795460019; b=0iK2ae6mopuJ0IoB/G7zte7NK3gse26Y2WQV7aRpnYAxsxQ8Bv3klxUopVe9N+aSu3OT RvHkBDQiItOoHWGvaiRXhpP+Kp8Z77ie9OAHk1kb7yPPF8o90D822weoqyKBEGSonFWB4 DvVflKU403l/OOeEh0DnnOBvhfj2co/AK8XIJxQ4lXGSzE0RXbDsnKK3wmrQYzJLMWHBW jNWpXaZlTKxzh3zXyfc/8yIzy+ZofLbmZaCAIbJDCLugmFxG+I1KuDVyW1Bmo2S5bZMhF hlmMRhd4oliQq7/4kbDuLw4kf5FsIHsnP0nvul2KfmHQDJuoGtGOvoEbynjz1bK1Zxw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460019; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=Fy4iqtyOKVldxfrRoRe9ktEnWMk4sYM46noWj6UrSRw=; b=2wrCtgCZIF3IDoJcQwOChbxVvGnUV8POxBumXTcyRmJngazLsCgsc6vFiC6bCJAKyB8j ptH6Vh6k4N5/mw1hkPSS4ocdGqHlD61ZP+1ZOECFl6zkVm5M1XS1p6iJs0Mt5aUvHHxpu KC3acsha/NRGM3pJdaJUDq8wX+ukkG5UM61HIWHb1KN4EhXaZh3HqfCjXlRME8OTUvask 7r7gbqpmXeR1ThWYv6QeCKOEP4NaD30RZLIS2C56D/+l60LTu7YyyPPFEE9xq3P/jC8NA ChyHuOx3cdTCuEIP9U/o8cOiM3KSo6yewHRTvoBHaoy3xogiWWxdJ7RUBcr21fGtcLQ== 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 4CC7E1F000EB; Mon, 23 Nov 2026 18:53:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460019; bh=Fy4iqtyOKVldxfrRoRe9ktEnWMk4sYM46noWj6UrSRw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=WRrWw7szzQ5c2Ixg9GNxJwZhVJx+JaZdiNuOtn9joRqVcN8lYkXo1sf4whH5Y11DQ L9LYGyAQd7JXLrF2yOSEhFgit2KGTDFznAq/aB478JnncipmyjEpsoi1GyTM09+Gqb WOy4TFOjBq7l9gxry6yT+GTKkHk82RRUJWGeiWM0IeJoE9gAVbbx8oNP/BCBBDCzPS bW7QmzaVCrnAK2A+TYVDhMAj4OuA2JvASNEf857lnNjeoUTKJKcvvbuEcZR3ip+cIz VuiuBwIVfmz52AVxZ3s7sVvtWuFQct6E2oemC9iFtXNvv5ppl74wdNBO7Jx2J/3F1S fBx3hS0rd0JJg== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 7F6304C659; Mon, 23 Nov 2026 18:53:01 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:53:04 +0800 Subject: [PATCH v6 12/18] mm/damon: convert to LRU refs based operations for MGLRU 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" Message-Id: <20261124-mglru-fg-v6-12-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=1016; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=ofT3DkXyC3h7ptCQPZjIa/LhrvPGmBU9+3gfY2rFnss=; b=o6eHIETcJuwaJUc97SH5F7SQ/kGVMK/yHu942m9yR9l+9EVkcSYS7fRXRI9yRCIPDg6wTwp7x /7/d/ERJJiaCtmPfVXmihoibf7n0JZz3OuQL/SSmum1iHaht3PhdLNZ 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song damon_pa_pageout() cleared PG_referenced directly, which under MGLRU corrupts the folio's refs count. That flag is now bit 0 of the count (LRU_REFS_FLAGS). Reset the count instead under MGLRU. Tested-by: Zhao Zhenzhuo Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) --- mm/damon/paddr.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/damon/paddr.c b/mm/damon/paddr.c index e89cb14ee17b..411e1d557ba3 100644 --- a/mm/damon/paddr.c +++ b/mm/damon/paddr.c @@ -279,7 +279,8 @@ static unsigned long damon_pa_pageout(struct damon_regi= on *r, else *sz_filter_passed +=3D folio_size(folio) / addr_unit; =20 - folio_clear_referenced(folio); + /* The region is cold, better drop the hotness info. */ + folio_clear_lru_refs(folio); folio_test_clear_young(folio); if (!folio_isolate_lru(folio)) goto put_folio; --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460025; b=OpHWn+c94K8hje0+s2IYcD5FxDaUDzy4kfqQl0EhZT6BaiId9Mt+/BxUQi6M9gltftwq mReIz6QxmVrQJv7FD6lZIUOrXKB40/JV7A5dQo+ghNjL8WSalhmmj3l/zgUh53C7Skro5 8K2pFkWzqwnyqQ47Jw3q0mpCrcyCeDu5RdlT0dNmoXpNR/+wc9mwGkNQyKFx0pae4zAEp AjVgAwRWhBU3j8A7sSYherOWIrYqWsiGSFgSGslOiGzZbKgl+buR3uMUSkwCA/X/9psAW 2YFSkFGNa3PbpLyJLXlR4c01sLtrtvnSbHb7eR8Hc72VVPSEN9KFs7IiHaO3tbO8p1w== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460025; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=S0g05G9qZGx4poDmgO7lZ71KlplbuRcjeb30J2se0iw=; b=ZRfcf2q1Q+wuI4hCHzWqq9x/PlmTsDFyd5enLrlL7ppKMah7fthjdKHYiigI81VCH+Gb lIqc52sIIfB1ZcWeidn0YoDBy4Reck+g3cPSfVX8TV2QH3uGpRoVDsAu7NZRFU890UPkG LGkHuNzlyjyUqix2NupD5jkpo78IypjEoGHnK7yJpz/+3gMWEx9wtVhR2OkPOuonCZq5g qRrEGGwRTXBU46A+KtZlBVudl+M0TcwBI3s0myatIPafkptNzoYDs/lAeM/w+ndtZdeDC EAIXP/9aRvzQEr5YRA1sOA11xX7KVhvurOIGddxq5/Qf8Kt2t71cD6dqu53VmxauSgQ== 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=ic4UV9yU; 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 C0078606A688; Mon, 23 Nov 2026 18:53:45 +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=ic4UV9yU 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=1795460025; b=2hVQywA1agQxG0dEmU0KZ2ymnjnbKFAAGdu6xNGnT4JvVK2oSgi//uIrV4qmLrFmf6zS YEl39KOl4oWkFKot+Yy9J+hXvpA7b3IMpimJSs6CCJZEtpoI2rPuAi5vv35DPGaVjXiFy dtnRDlOKzkKllmwRIUz2PgIWnU5ivSAWgKo8Aab+VnCn9LSNyPzVfe1Huz0lFW/QMLLvY U+caSwIAzGQVcT8LLUnrG7POlz5bRN42uD05PSsoAlo7fKlalzJ0aP1jymgS31JWxZ22A zDaip5eRZuZNGvO/1Ux1YVmlDI1u5+5XJlh70OzDBAgfwXeYIuXmpVoHesLURW72bhQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460025; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=S0g05G9qZGx4poDmgO7lZ71KlplbuRcjeb30J2se0iw=; b=f4Gl74scEwgguJWfuT5Rq/VeNez08ryCTsQD/fVwoxIt+ve7T5Nik7xIHKxEouLeZ+pA 2lgSUOWs8ngGJAq8MnZKrtweHZEHYstEdFLOTy1VcwdOJdjtqgppqDj8kcsLMAAgOq9Bc 9QejczBBMJ9Q4D8kNlojQW4Ua9Iw6NMHERe7qbwJ11UWwMmdkhP0hzZCc3Jcj7mURM1/M Imuk5doQvlU30duGA1LwnaLOKkC4aC3vWZ8VEF/C8b5KjJ6K0OlrGWC92HeDTJVl2QmIk ww19jS+iW+Q77D+dMibTJYXQoZ+w5mIHgEKWlsGLJObO8e+bp8aaV3w1M9h6P2gs+SQ== 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 0E59D1F00A3D; Mon, 23 Nov 2026 18:53:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460025; bh=S0g05G9qZGx4poDmgO7lZ71KlplbuRcjeb30J2se0iw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=ic4UV9yU19MeLMIiiMuY6S798cUREJRPPSFF4KS0TTw9oD0KaRico3mAsQU2BjTPu LP8bkcWRVM7zsGaQzV9mCay09xkmh2t6/AkhUm/wqvc/OQQhhBjiuw4Jb35rCOeJ5M 1xGYJFa0bgtm1gS/hA+02aYR7zJ9mpVxVo4EVdmi1Z2XAj2r6tNBG9xBPJwpUTTl1N 1VBqnpvMOa5qstAnO6qfesIxSetuILWmDF6wWDZAew8csQ7yVbQxxBWnpY5/3b0mM+ Ky7Q7jwCk+CDVVFpccmj2m299B5jtzTevNvdx3pq7Yr1Tm3OS4mAqZNwlq4RfAPrtf ZFHINYD7H92YQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 9B9F14C65D; Mon, 23 Nov 2026 18:53:01 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:53:05 +0800 Subject: [PATCH v6 13/18] mm/khugepaged: convert to LRU refs based operations for MGLRU 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" Message-Id: <20261124-mglru-fg-v6-13-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=1410; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=ylGSrEczLifu/DdmhwMoGhk6njBmtr2EJO7xuMx10kQ=; b=pXXLUep3I/vltYX0L1UH0GTTRjdiy0b3iBc9jWcCRdtT3cKw00OaEvGBk3uaweRY+TaoRFVrh ZG6rFxHmKKzBv05zdRFuW60YLUnIX7HDic9FG8ptG6NTl5y6QM0dqWa 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song For MGLRU, PG_referenced is only the lowest bit of the folio LRU refs count, so folios with nonzero even refs no longer have the bit set. folio_pte_referenced() testing the raw bit misses hot folios at refs 2, 4 or 6, under-counting the referenced folios of a candidate range and aborting otherwise good collapses with SCAN_LACK_REFERENCED_PAGE. Test the refs count directly when MGLRU is enabled, mirroring the smaps conversion. The classical LRU keeps the plain bit test. Tested-by: Zhao Zhenzhuo Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) --- mm/khugepaged.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 2dc4245814df..5e768481ac22 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -579,7 +579,9 @@ static bool folio_pte_referenced(struct folio *folio, struct vm_area_struct *vma, unsigned long addr, pte_t pteval) { /* The folio was referenced previously ... */ - if (folio_test_young(folio) || folio_test_referenced(folio)) + if (folio_test_young(folio)) + return true; + if (folio_check_lru_refs(folio)) return true; /* ... or the PTE mapping was recently used */ return pte_young(pteval) || mmu_notifier_test_young(vma->vm_mm, addr); --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460025; b=bFRNIZkf3/mZlK2NrDuw13FedK9suILwx7WyKeDx3GpdK1dkS0/1tbvyMWRz9IJ2YQWt 1hMUTmWXFoQt4aEb1qjyUFzRL55P4HKhfr7x4VBVGdK/LIth9uJowVc00mzTMuCyxaHg2 ekpaoU6fszdtm3Fj8bFL8/sBp40MSh6tbqBJWhDXv14QQeMVWzSly6lAYXGd9+B4zdinE kQYl0qToPBcpGc1nY0TsMhezfTPNP4zJcIoqpsuruPuRqgXb97ZBXFPNGNbB4JyZKVcaj FpL6Z5+OUOKG0id5wBZn+zv+BJfc/JzXz9KJrNbUT5YNerc5/9PG3VR6douQgpshx9Q== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460025; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=1JqhXb31mV0qrsAJQ932SZX/2Yz5Qz0UbPnOXo+N5Vo=; b=iBWZQQVPxwd8VDhfRijbZd+agcGss1Y6+wDWWtPbYNQDc8jAXfrcn0o7prZaJYqhDxUI 1Qpk4jRsE44ngffepGS4VyyPZ96mmVLvNC0nRU9jepzJpII+1wKO9B0fbEMqdvNZgErs/ lG3JUdi/BMiWldRVC0k4Tx+qVqlZC1FFQOs26daQdVUz8N7vEzXKlTImX5eOMycek538Q Dpm7aOUl2QuG5CGnRTBAYsOhMTka8MMjYvFkR6EsSFpL/0fwTN+EgkzRLaV/rjtaF57pj WEXGjiHQmlTGATdsCsj25q3DhnT1ovhAOrHdm3VPGWFrNLO35ZxRBzrETuTrWMDxfng== 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=fuMxN0Nc; 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 4DAF5606A31A; Mon, 23 Nov 2026 18:53:45 +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=fuMxN0Nc 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=1795460025; b=j5uEAAqqtJr0P2AIBRVs/qtYduwt8irNX6y71mZhQBM7SZlEgJdiaBeAMyHWVIc1C528 JMIKU9fZjFFJCQ7ppnXn1gXrGFZgd6YbvZ84iD5F4F0rtqn7vFcMyYgj4cnU4CcrtmtOe j74DZ+LR8Vp97gQTI9cofXGNnX/Zmrj3jEftHf7x79CznT3xI4nhp//znsL30J6o6s9BQ /S4BJrH9/g+wc0ZJrP1w1aFHEpd1cmWZBhT2VFeYppi5yYJiiF4E4iILMDw2eNTtxCMxM 2aRwUmU6neBmaoSl7vrabBA+Evq2RhjctnLOz1EyAkv6PBRum/4ov8tPhN3iwZLp53w== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460025; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=1JqhXb31mV0qrsAJQ932SZX/2Yz5Qz0UbPnOXo+N5Vo=; b=C2wl9MtmjdOUJOxfwa/+jrlEVbvZ+zH1p0w/kEnFvJb062hWsyy+N3FvgV9xVo6MtewZ DVHTx1gxzzlcbwyjr2xMN2zRuf/esUHZdvx27qpOcl7uJfDt5kwP99rJgI8udhsZy2LPP Hzx1Le/5wNHF2LRfSnWJdD6swr94FKkQPeYmf57Mrh73nSYiEk2tfTtWqwqpLyFvhbOj4 ICSksh7UncfnffPHOypvmRgNn04yiWQQdYhKQ0lB2zr1BVkJJED3ENmmH/b4Q5H/6KkKy tQBGcIM0RgzNJOY5hKIijFxTgBTlfLkr187NU3h5QNQNj5l5bVLztQo8xpb6bka3yFw== 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 551F31F01570; Mon, 23 Nov 2026 18:53:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460025; bh=1JqhXb31mV0qrsAJQ932SZX/2Yz5Qz0UbPnOXo+N5Vo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=fuMxN0Ncu2Kdljre23hS7DhaUsB/CuBV58EDVjopYlTAxM+kgr46CW5q9C5pU6Lzw 8P1b3MrcBdiNQHS5psrZ72DqCvENtiQoMoCCW38wV8rx1BlcFXqhMKX196VetYDjfh YgCBUpfCKuWXchF3NmJIJFMVhfZlhAXJWAEwQAxI/ox/JFLkeC/uHrV2cm//kMHTtB XrLqJ0JP+UbcRsObOoFfV3AldfWva5KpR11HwyoutQup9ytJLQrS28XmsU8DG73uR2 3m8ydy9rtjkxJPrXSCiyEiOGpnjOODGpBfEOzDlsozSmu/atTmjx4BbHaOsGcGp6p9 QqqJf5JN7wJGw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id B9F084C661; Mon, 23 Nov 2026 18:53:01 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:53:06 +0800 Subject: [PATCH v6 14/18] mm/madvise: adapt for LRU refs based operations for MGLRU 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" Message-Id: <20261124-mglru-fg-v6-14-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=4328; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=1pDxzB68yPhUBkq/LYDWcJlzcSVuKtrX815nQg6xt3E=; b=DOr3YEmRaaDrax55p05BK/Ym73sex2jTTSZET1t+yFv2QVn5WM0QXp/bEkQ6KU//bQ+Wisxn1 Qig9+X5yGcZBYXczo/YcZy4ddA5fdisR5SnE+N+dG0f464/kaCt983n 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song For the active/inactive LRU, madvise clears PG_referenced so that one more access is not enough to reactivate a folio, and keeps PG_workingset on a folio demoted out of the active list so its refault is still accounted as a workingset refault (PSI). MGLRU keeps that history in the folio's refs count instead, which now lives in PG_referenced, PG_workingset and LRU_REFS_MASK, so writing to those bits directly corrupts the count. The two hints want different things: - MADV_COLD resets the count in folio_deactivate(), which also requests deactivation. - MADV_PAGEOUT clears non-workingset refs and caps the rest at LRU_REFS_WORKINGSET after isolation, before handing the folio to reclaim_pages(). The hint marks the folio as cold, so the eviction shadow records the capped count and does not inflate the eviction/refault counters of the hot tiers that drive aging, while folios at or above the workingset threshold keep their PSI classification. PG_young is cleared for both LRUs as before, so the referenced walk does not count stale history as a recent access. Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) Tested-by: Zhao Zhenzhuo --- mm/madvise.c | 54 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index a36cfe98dd81..7e562e78d52a 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -360,6 +360,42 @@ static inline int madvise_folio_pte_batch(unsigned lon= g addr, unsigned long end, FPB_MERGE_YOUNG_DIRTY); } =20 +static void madvise_cold_prep_folio(struct folio *folio) +{ + /* + * VM couldn't reclaim the folio unless we clear PG_young. + * As a side effect, idle-page tracking loses recent referenced history. + */ + folio_test_clear_young(folio); + + /* + * For the active/inactive LRU, a folio demoted out of the active + * list should have PG_workingset so its refault is still accounted + * as a workingset refault, and the referenced bit always needs to + * be cleared even for inactive folios. For MGLRU, MADV_COLD clears + * refs in folio_deactivate(); MADV_PAGEOUT handles refs after isolation. + */ + if (!lru_gen_enabled()) { + folio_clear_lru_refs(folio); + if (folio_test_active(folio)) + folio_set_workingset(folio); + } +} + +static void madvise_pageout_prep_folio(struct folio *folio) +{ + unsigned int refs =3D folio_lru_refs(folio); + + /* + * For MGLRU, clear refs below LRU_REFS_WORKINGSET and cap + * the rest at it: MADV suggests the folio is cold, so the eviction shadow + * should not record its hotness in the high tier counters, but + * the workingset classification is preserved for PSI. + */ + if (lru_gen_enabled()) + folio_set_lru_refs(folio, refs >=3D LRU_REFS_WORKINGSET ? LRU_REFS_WORKI= NGSET : 0); +} + static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, struct mm_walk *walk) @@ -434,12 +470,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *p= md, tlb_remove_pmd_tlb_entry(tlb, pmd, addr); } =20 - folio_clear_referenced(folio); - folio_test_clear_young(folio); - if (folio_test_active(folio)) - folio_set_workingset(folio); + madvise_cold_prep_folio(folio); if (pageout) { if (folio_isolate_lru(folio)) { + madvise_pageout_prep_folio(folio); if (folio_test_unevictable(folio)) folio_putback_lru(folio); else @@ -544,18 +578,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *p= md, tlb_remove_tlb_entries(tlb, pte, nr, addr); } =20 - /* - * We are deactivating a folio for accelerating reclaiming. - * VM couldn't reclaim the folio unless we clear PG_young. - * As a side effect, it makes confuse idle-page tracking - * because they will miss recent referenced history. - */ - folio_clear_referenced(folio); - folio_test_clear_young(folio); - if (folio_test_active(folio)) - folio_set_workingset(folio); + madvise_cold_prep_folio(folio); if (pageout) { if (folio_isolate_lru(folio)) { + madvise_pageout_prep_folio(folio); if (folio_test_unevictable(folio)) folio_putback_lru(folio); else --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460024; b=jjrbnDxhBU7c+BaTBA5g3OZ5VqDxbIVvi/iy4AEGmr231Jd1YG5G3cSH3Y3I7WExmq86 apBLYtmlXqbghr/SsQEgjAtjkJ6DqDaLNswjmovRmtDhw0tEwHsgZI76KC9VRdafdS0y8 FLIaMPm+Nhiy7HY/KzIQqQGchBezTIOVH1dhWPqiH44oK5JGT4BNmy0YoG3gnW9cxuRXO Vp0zwDKkL9NymgkMsJXtw9plp76zQ0KriHWHjGmS1bSO1nl5dgsbY2IIPHKOHbg2PJ5JX PPQcmNzwJoQ78/kOy2MKg66JovzEeLUx2/0ULbC2E1WbQnDKaT/CXj+hiO8NjPnzCWA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460024; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=mw7v666B0O0bXEelFGMEGNGOC4m5brM448d+fEgwE4Q=; b=QLS1VpEhyI+yJu6O9LOEKxn31Zykndli+uJoUz5FRK/KDOjEn6wPBCp4vs10zZEDx5Hk QRvjL10LuXei1gFx+5DZ9xDLF/51LFnqEw81eRSX1vzz62UaDqIP0rcmikDodam2+4IfI cAmLkCGYxRAq5VprpFomeF8nI4pCsw+3SeldPw9yr2S/pKb/JJULzLpk5zjh+GFBmuJPK JmSjb9NjdxwHh01UAG1S+xaqhWuj5Zsp8QaguoOTZxQkCtaPs/Q+1XYelUNl9CnEbU2uR 3EBS0y/xNTgi/hBxhS3CbDeTgSwiKu49z9tkxljp+9Wz8+FKTGetz6OOWto+A72ts5A== 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=mOiEIFaO; 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 6059A606A334; Mon, 23 Nov 2026 18:53:44 +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=mOiEIFaO 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=1795460024; b=z2GzWZiDNaVs4LZlZKHjbxWru7y34/egki64secC2NKFZV6BUgD7Wd0E9J5ZuciMWEh1 vF8ciNqlX/yX0VwUCZ4waYwWYMRGNwMUJTMuijIrwmvoh3om/z+Sf1phoyX1iwIvFXN9U 6jBh7FbM3uV1tMy/ZiytIwhaJlWVeIoqUHextC0HBtZqDpyr1njDCKpSmv7UT+6Ew0uWs AgrbEeXr1G1s67PWOaiNa5SN70WFfK5j0sC18WVP10JF2eVIChugvT7ElZyqfA5WFa4fe Q3Waat2yS2JJjwxSrdfWl4Eah2xobvYmlmYOEoxXPX1QGIP+b7cnIqg9y4hz0BtTgeg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460024; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=mw7v666B0O0bXEelFGMEGNGOC4m5brM448d+fEgwE4Q=; b=gMCnIn4aUXDGhBcxa3kkZfBTuDvWZebB+1s8WRwntyhxCVkKAhJ/g9Mvudg5Ja5ToDyt Vw4mckQamSR+/11JAovekklejIfVzW5/nDl82K/h3cU6bbZvw2TFMgZZNiMkTwv6Snsf1 9lV/ne9cAD2gseClZRfX+iRrH5sjATUSyHQA1Ypij2y3Jv3nmBgNjoolcIMKxxbA5oNRJ iLvIe5UeqesCWlWs6Qk6+WgLrVlBHf4nJ7UpeEYNjZw3Cf/Gs3aHnTGD8Npgq82X+ekFf ZJJ9PpQlLIVtTVxYuTVKy4L/rd4cBU4iH78+NOJb/rJwluKw1Xp7t5ZWV9pSu3Gl+WQ== 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 8BF3B1F01572; Mon, 23 Nov 2026 18:53:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460024; bh=mw7v666B0O0bXEelFGMEGNGOC4m5brM448d+fEgwE4Q=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=mOiEIFaO4VIpA+R/yd/1YUMV7KeiI2oBhzBt+l00H2YWPcLqv6gx3cgP+6HMUV98b I9BovPzLz8Q1IYRnkGRZlca4wUhahQpfq9ByR5Fjtjq1o/mKY8dZyW8sdaX9LQ9UIr 6g2VHOANTZN/Ex0rcLJXSJVJMZSWEiWMyB1nn66NXfPKWNQ7zWlokuYgELitD879Fk mYNe/Q3oX9bVTqxUbKwQ40kX/rpYfH2atRTPMk9cRZYF6qf/Pec79coDd32GpYy5OJ IT9TmGfI6q9s2vKSAXl/AhL+xENuKlA7HLpBF6CxLYTFIFooE8xGuUzGH6orGuluT/ CuqpZ24qtXYhw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id D6E814C665; Mon, 23 Nov 2026 18:53:01 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:53:07 +0800 Subject: [PATCH v6 15/18] mm/huge_memory: mark folio as accessed more accurately on split 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" Message-Id: <20261124-mglru-fg-v6-15-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=2081; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=svKswWLQsOqCODPXI2P9d3ZShVpGHldhUm7oZl8WLHs=; b=uGyjbdLY9Xmj/REU9NlfpO+n2XY34DVfiQMG2A8ErYXhlavxnUkdesLKD0RF2+NCUWBfn8wnX n5ET/23+qQAD8v/x6lWvWEUPWgb1QREe0OKsx1v+54gfQ08nLyRKQjZ 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song The behavior of updating the folio's access info isn't consistent for huge mapping splitting or ordinary unmapping. The page table's young flag has to be translated into folio's access info. Right now it only checks and sets the folio's referenced flag, which isn't enough since folio flag updates on access have their own rules. Ordinary unmapping (zapping) calls folio_mark_accessed(), and it also checks if the VMA has recency to avoid false updates. So first just use the right helper here to be more consistent. Keep accounting the young PTE as a cache access, sticking to the current behavior. Tested-by: Zhao Zhenzhuo Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) --- mm/huge_memory.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 75e8641bf9ff..fa9f6d21e786 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3122,8 +3122,8 @@ static void __split_huge_pud_locked(struct vm_area_st= ruct *vma, pud_t *pud, =20 if (!folio_test_dirty(folio) && pud_dirty(old_pud)) folio_mark_dirty(folio); - if (!folio_test_referenced(folio) && pud_young(old_pud)) - folio_set_referenced(folio); + if (pud_young(old_pud) && vma_has_recency(vma)) + folio_mark_accessed(folio); folio_remove_rmap_pud(folio, page, vma); add_mm_counter(vma->vm_mm, mm_counter_file(folio), -HPAGE_PUD_NR); @@ -3245,8 +3245,8 @@ static void __split_huge_pmd_locked(struct vm_area_st= ruct *vma, pmd_t *pmd, folio =3D page_folio(page); if (!folio_test_dirty(folio) && pmd_dirty(old_pmd)) folio_mark_dirty(folio); - if (!folio_test_referenced(folio) && pmd_young(old_pmd)) - folio_set_referenced(folio); + if (pmd_young(old_pmd) && vma_has_recency(vma)) + folio_mark_accessed(folio); folio_remove_rmap_pmd(folio, page, vma); add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR); folio_put(folio); --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460028; b=t9WlI/WRgvBcCPal/jdpUZE/nO1PdGryXr/BRB6mOWe7NrOF+pLKS87NPIkTTiN6JEE0 3uJ25IgbI3aEmCDSmWV0GlJxzaWNr8m4+y+eysuPfPkTOkoAFGsk3c0dYFGwjGeOoUdbi fOrOi8UjtbC7/uLY8dgiJZs3E2PPjVD8qzpqajsAzfhTWzIrdMIfIsm8FKWHZQbwg33W4 m5Wq/Fhpfyryq1jYsSnmnm+ClaRgMXypVhcxp4VxOCusb79xR1fb1LwaMcjl3A/oXK/xG TBD8hrl1l9z8UF/IvAshG51YmZ5Ks/LNv2bq3Td3AVA54d79RvncWOnLNGKbHgt41Zg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460028; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=HW3m15cwOhOmCQi5QS7IBcTVWDziQbgROwHGLA00qiA=; b=kVk6uwIofZcyZPDZEKmVvQb3XALCD0dWyfqhV6558/UdjOlnwnwLlEiuc3jADalgmK5N Let8gy+vOH/FoBjO/Qs1qvCx7A2ex8fdr8g8UyhJLxZceXR8cYpIYY4yOJbIfDm+NvtxB lSkJI0tOr2rMa9ZwZ8++KoZ1VFzsRNQPa8YC2ysJG1VoCStPf8aV0Nv7qkGMDz5QWTK3B +OmcAPwp5WClysW1PyyLyFrg7qj2o3uqruxaqIREcUann9D9f30nBJY14DbSZcDao2Sbp 6LHjGIGve7S2ZGaHCfYAMduo2f7/GEhPGg7JF+koRNmaOhqUtyzwGVeY5251HHqLopw== 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=W+cWUfD+; 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 7C93A606A31A; Mon, 23 Nov 2026 18:53:48 +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=W+cWUfD+ 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=1795460028; b=LVKkst8/8RbCJcBLVShOCW3aBlTfnAd4hc96P/GIpd8uElPc05bMXlaANMxJGgXGAWht ETuD5k5CoNso83EZ9WNrMfuJkQToLkw4X3dbRB9aSVQhoomCHkszai9l5lsXv0EC1g3H8 I7ZCp6xqVclPXVtPHNcLRrQGm1EjHT7bjLb1n+g+oS6SvurOVkguDWTMcZJ6UBKgiG9FR q0VyhMAzy6XTqT0pi4W3HqpaJujBw2QGYYGjxjyzFwU5KiOOgaMzsm35eB/xIjvsGFGYR hS8sTRT2ddDxtKAzqkg9lYRfXUu2c981Abkb9lINEhFTrKyZjjJguckhoVxkWdAwMRQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460028; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=HW3m15cwOhOmCQi5QS7IBcTVWDziQbgROwHGLA00qiA=; b=xbICeuFIOKvxVm/brVqwFuv3wFDuZiXB4Pkigo+6qCW8NnKfxouniyjHOWVc/WLYr4w7 A4SR6NKbI4YVrxoK/xtGOO2ynnGLPIHflAixvKgF3bVf5yBczT8cl3OL5jl8e0OkaZKPy dGq5Y3vBtuT2E6OR5yDLYF6/CsF/qRTCjcQevUZW9aR/qHQX9XHZVZGwWzdeczkdlP6jt rMA3CcU9+bbT5xL2UnpzjXe/ZHP3JrH3iREqdFGJEKyCZ+pxYspEGY8qkwBt83FBTTdF/ PeTkzOQrgazHLY8TWLp3ChjOl0f2hLvSBIK6zQbAop0jqYOxuvKwGle2l/n2LMxrzNQ== 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 C19BF1F000EE; Mon, 23 Nov 2026 18:53:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460028; bh=HW3m15cwOhOmCQi5QS7IBcTVWDziQbgROwHGLA00qiA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=W+cWUfD+EV2139/AlMMM4kwHz10da/jilCMVMAEUXVfMtK2sUOxlDhGNwduDAWYT2 HUCPX+QZJDtSntZC4tbVt6P51m7/gw/tdd6ZWousNV/T/VwhfqfFt2JDIrZXNuSClR wSSV8FgRMcXM+regetFS3wcP5IPS13HUN8dFEB0WZ0Lb4YB7SXfkiTD+vMt0AowxZs 9/ymI9ZhYu40lvEf5b0mLyN5M6ZVTD+60F097qdf7KNofx7gkLT393sYmfoEytb31c rAcX+J4F4CqvQw1eTSKkYf4sgY6qACE8Yt1Uyp02feCtWlM1Qsr4YOZqTkp2D2OMp+ MZsJYZG0uPFkQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 0038E4C669; Mon, 23 Nov 2026 18:53:02 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:53:08 +0800 Subject: [PATCH v6 16/18] mm/mglru: make PG_workingset helpers work based on folio LRU refs 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" Message-Id: <20261124-mglru-fg-v6-16-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=6298; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=FeZ1X36y0gjLoNuGBhnIl7GXAntfQovoxU409olinNk=; b=1VQ9pndS33m4Kvn5Bxu4wL7Vfltv3HU9XfA3NipSyzY8gijlcuTI7FnfCR5lNBgS/0H+Szg78 zZU1/x1iy0HCGin+8N0ZqTISRFUtg1fGvsKT9WkEUVt/8aQ4GtDYp8c 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song folio_test_workingset() currently tests the PG_workingset bit, which is only the second lowest access bit of the folio LRU refs count now. Folios with refs 4 or 5 carry it in the LRU_REFS_MASK field and no longer have PG_workingset set, so the bit test misses them. folio_set_workingset() currently sets the PG_workingset bit with a plain set_bit(), which also over-promotes folios at a higher refs count: e.g. a folio at refs 4 is bumped to refs 6, advancing it toward LRU_REFS_MAX and an earlier generation promotion. Move both helpers into mm_inline.h, the test as an inline helper based on folio_lru_refs(), checking refs >=3D LRU_REFS_WORKINGSET. The set stays reserved for the classical LRU, as under MGLRU the refs count is maintained by folio_inc_lru_refs(): it now warns when the folio is on a gen list, where the raw bit would corrupt its refs count. MADV_COLD, the one caller that runs on an on-list folio, sets the count with folio_set_lru_refs() instead. The one PageWorkingset() user in erofs is converted to the folio helper. There is no behavior change for the classical LRU: it does not maintain the refs count, so for a folio that was never on a gen list the test is equivalent to the PG_workingset bit test. This prepares the convention for follow up MGLRU change. Tested-by: Zhao Zhenzhuo Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) --- fs/btrfs/compression.c | 1 + fs/erofs/zdata.c | 3 ++- include/linux/mm_inline.h | 30 ++++++++++++++++++++++++++++++ include/linux/page-flags.h | 2 -- mm/filemap.c | 1 + mm/madvise.c | 2 +- mm/page_io.c | 1 + 7 files changed, 36 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c index ad90e14032db..127c5ee38979 100644 --- a/fs/btrfs/compression.c +++ b/fs/btrfs/compression.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c index e981e371d6c2..34ee41e1ccdc 100644 --- a/fs/erofs/zdata.c +++ b/fs/erofs/zdata.c @@ -6,6 +6,7 @@ */ #include "compress.h" #include +#include #include #include =20 @@ -1713,7 +1714,7 @@ static void z_erofs_submit_queue(struct z_erofs_front= end *f, DBG_BUGON(bvec.bv_len < sb->s_blocksize); } =20 - if (unlikely(PageWorkingset(bvec.bv_page)) && + if (unlikely(folio_test_workingset(page_folio(bvec.bv_page))) && !memstall) { psi_memstall_enter(&pflags); memstall =3D 1; diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 0f470ddf5c48..a4daf366e6aa 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -283,6 +283,21 @@ static inline void folio_migrate_lru_refs(struct folio= *new, const struct folio folio_set_lru_refs(new, folio_lru_refs(old)); } =20 +/** + * folio_test_workingset - Test if a folio is in the workingset. + * @folio: the folio + * + * A folio is workingset when its LRU refs count reaches + * LRU_REFS_WORKINGSET. Under the classical LRU flag encoding, + * this is just testing the PG_workingset bit. + * + * Return: true if the folio is workingset. + */ +static __always_inline bool folio_test_workingset(const struct folio *foli= o) +{ + return lru_refs_is_workingset(folio_lru_refs(folio)); +} + #ifdef CONFIG_LRU_GEN static inline bool lru_gen_is_old(struct lruvec *lruvec, int type, int gen) { @@ -544,6 +559,21 @@ static inline void folio_clear_lru_refs(struct folio *= folio) folio_clear_referenced(folio); } =20 +/* + * For the classical LRU or isolated folios only: with MGLRU the PG_workin= gset + * bit is part of the folio refs count maintained by folio_inc_lru_refs(), + * a raw set would corrupt it. The warning below catches folios still on a + * gen list, the classical paths in the switching window only see off-list= ones. + */ +static __always_inline void folio_set_workingset(struct folio *folio) +{ + unsigned long flags; + + flags =3D set_mask_bits(folio_flags(folio, 0), BIT(PG_workingset), + BIT(PG_workingset)); + VM_WARN_ON_ONCE(lru_get_gen_flags(flags) !=3D -1); +} + static __always_inline void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio) { diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index b0ddc652e76c..f6625dc042da 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -549,8 +549,6 @@ PAGEFLAG(LRU, lru, PF_HEAD) __CLEARPAGEFLAG(LRU, lru, P= F_HEAD) FOLIO_FLAG(active, FOLIO_HEAD_PAGE) __FOLIO_CLEAR_FLAG(active, FOLIO_HEAD_PAGE) FOLIO_TEST_CLEAR_FLAG(active, FOLIO_HEAD_PAGE) -PAGEFLAG(Workingset, workingset, PF_HEAD) - TESTCLEARFLAG(Workingset, workingset, PF_HEAD) PAGEFLAG(Checked, checked, PF_NO_COMPOUND) /* Used by some filesystems = */ =20 /* Xen */ diff --git a/mm/filemap.c b/mm/filemap.c index dc0ceaa58a0f..acf5e562b732 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include diff --git a/mm/madvise.c b/mm/madvise.c index 7e562e78d52a..e138082c518a 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -378,7 +378,7 @@ static void madvise_cold_prep_folio(struct folio *folio) if (!lru_gen_enabled()) { folio_clear_lru_refs(folio); if (folio_test_active(folio)) - folio_set_workingset(folio); + folio_set_lru_refs(folio, LRU_REFS_WORKINGSET); } } =20 diff --git a/mm/page_io.c b/mm/page_io.c index a57cdcf2892d..12deab296718 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460029; b=rOlq4yEK5ZehHpTLKVqrHeYWyOQ3ZuamYIJW7//cH80GNKVgMRSmyfmoi+PS3q59Zzxy oBYOx1343vpiNL591lr42qfqJ7Z/AveOSw/lFohdXQ/bBNuAHwxgPpHsG3K8lBkfqXuhI mFn9njbQIGa8P9spImr95oVMLbMmfceqzPrznH/mjca6NOfnysPrZOnH5RGhqsTenOOWL ax3EEnYjc+FIjjhRf7j1quluNTS+yKpHEzVJtfeAaX2dj8VEeHmhJK//+I0eeCFv0JbMx OfG6R0JL4mDExLXa4fn1gztZGaPFFndCGNAv1kV8WRboSzVzwDLsSA11teZB9VzrEwQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460029; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=msbZKHy1ebjgobt0Xz7UoYUjVViJx0l/tZh4mOG0mfM=; b=GD0OtFwI6l/puXj89D0I5w871Bpj3UE7jQjYUlZYqvRIUfxO3dWOmKeD6t6msSstOc+B y1ipLCcybN0VdGwYvEDQrh21b+fvWQbAX3IixOufM4+9xHXG7X1BS6VV0ISNdcf3D9DvT rJlz5uaztGzru7aIWNQgPIN/yNbGYxepRkQ1LwB+mcmJ5/iPNvRgY+abT2OkssdiHnA7Q xfUeBpHBNlZqsttEOyG+PeX9IFfMQ3UOPXgT35ZbgIbPXk2JPm3zIRoj8SADFcH+opweY 2Ai+e0KDurc1lgVsiGYMnvccubrm2UaznNX3RI/LEQDl0NrotjZErKEUpv/Y7uzjQhw== 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="MkR/OWcJ"; 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 726F7606A33F; Mon, 23 Nov 2026 18:53:49 +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=MkR/OWcJ 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=1795460029; b=SHk2NhzrBDx5tzBk0AUQaXntneNoQLkxm3h/dy7EnocDQhzkApLU68pc3qFBuICdhtuq see2X5Hto/rJe0p0QP8iZJ37fwGehNbUQeVCzG/FgmQjv+K0KGM4TleZwg7busQb9w0Y7 OGG1gto+vdJ+N2kj/YOWQJalwOz/CET3NJl9X9CVsq8490iEAd4BsPm3kOviuJRZ78AY6 8pRg/uX1ECrkV+3Rp4HVGw6mRPZb6cWUFtDilIEu/1pyE2wOqZfkmI2j053yfd3WjhrNe lWUNhiui6Z5ch3NfW95nhL1tHARLokKzz8l/xq+IanCdLTUX/xeyA0aDWYJwpjsxMKA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460029; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=msbZKHy1ebjgobt0Xz7UoYUjVViJx0l/tZh4mOG0mfM=; b=oro56AYYPykOxuzcJSi/Kk4YmTbuLnFw12DH5t0c1NdXa+N0jzoSVDm7qp/NwJX8vPcT WgVNl6/J6/jq1eLtcpKh1BWXZdCYbje33hLhqLwkU00nKkz2rwLFHWtUb+E1KW2Rt6WZg eBhpfMDPDSvNgr1CwjlnCmOCZbCwQDOu7781LUAUFB7612gutOFJ2p9QKEFZ0LwZK+wHK XT+2FHB/AAI8ccKIkDM6u8VA5/ZwWZaUJRf3/DmIz6a1gTUMBCYXVIerjGeeWmVny9RL/ 5bzsrIwB4CO8VDUoWNOdvM2QPsmyjH9GRDs9mCphUkLigxPL4f3Rc5EWSYq14eH3bkA== 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 1122C1F01802; Mon, 23 Nov 2026 18:53:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460029; bh=msbZKHy1ebjgobt0Xz7UoYUjVViJx0l/tZh4mOG0mfM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=MkR/OWcJnM2aU/xEnoDUj7jzezGVwkN8V9QvtRpHDbAJgssZW3rsKfdqDI1IAMY9Q kT9X+8xy8terZOSJIyu4ZjQdrd1qozvYJi8foPy8sjaphaS+r2y+fLc8DyHikj3WMl QCZns7FnV7oXqoc+wmEwE3ypdP592m6+q5CMti8SHz0KV+Q2DWTN+was6tpF9i5M9d eQ2XW0ZR48FspoDpEN46cFmBoEGJrXwokYNV94FjJJMz7xJaTueHzyBwXZmeP+MRQB dfKn6TiyZe45YbYiuB/4yMK5qiE5IDxXMi5SuHi282gGc60fWsx3ehGd8/WhW+Z6sM +ou3KiOFr4c+A== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 1D95F4C66D; Mon, 23 Nov 2026 18:53:02 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:53:09 +0800 Subject: [PATCH v6 17/18] mm/mglru: folio LRU refs based active/inactive number accounting 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" Message-Id: <20261124-mglru-fg-v6-17-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=36184; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=Ql1OsGIqRkjrksHm7f5BrDjlNotBkrRSLlgsWXt3Zv8=; b=ikNOW6NPLTy58UTIpSZBEvuKEng5oRkG4q1H5eCkh8wKgJ0ua17LHrASLEGgvgPiZhHEYAzfz t4nb3zoazxpCHJPtWWFSKL1BbM8e59dzeaaG96nVI7mwu/dyPjkS0+W 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song Currently the active/inactive LRU sizes exported through /proc/vmstat and memory.stat are derived from the generation window: folios in the two newest generations are accounted as active, the rest as inactive. This causes several problems: - Unmapped file folios can stay in older gens despite repeated use, so active files can be under-reported, unlike classical LRU. - The reading is jumpy: on aging, the second-newest gen, previously active, becomes inactive all at once, causing a huge drop of active folios without any change in use. - The reading is reversed on swapless machines. When swapping is disabled or unavailable, MGLRU ages anon by moving the gens directly without walking or touching any folio, to reduce the overhead. So inactive anon folios can suddenly count as active, or vice versa, despite no change in their hotness. With frequency guided promotion, hotness is already tracked per folio by its referenced count, so we can do folio level accounting instead: a folio is active once its refs reach LRU_REFS_ACTIVATED (currently LRU_REFS_PROTECTED). On reaching this threshold, folios are accounted to the active part. On forced per-folio aging or PID protection, refs are capped at LRU_REFS_WORKINGSET, dropping active folios to the inactive part. Note that on swapless machines anon refs are not reset by aging (the shortcut skips the per-folio walk), but the counts no longer oscillate as the generation indices wrap. The accounting is now per folio and lruvec independent, so no size adjustment is needed when reparenting a dying memcg: the hierarchical stats keep the counts in the parent automatically. folio_migrate_lru_refs() also gains off-LRU VM_WARN_ON_ONCE checks and __folio_set_lru_refs() leaves a folio that is still on a gen list alone, as its refs are already in the active/inactive counters. Reviewed-by: Kunwu Chan Signed-off-by: Kairui Song Tested-by: Andrew Morton Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) Tested-by: Zhao Zhenzhuo --- Documentation/admin-guide/mm/multigen_lru.rst | 4 +- include/linux/mm_inline.h | 100 ++++++----- include/linux/mmzone.h | 23 ++- mm/folio.c | 43 +---- mm/madvise.c | 4 +- mm/vmscan.c | 228 +++++++++++++++++-----= ---- mm/workingset.c | 2 +- 7 files changed, 235 insertions(+), 169 deletions(-) diff --git a/Documentation/admin-guide/mm/multigen_lru.rst b/Documentation/= admin-guide/mm/multigen_lru.rst index 9cb54b4ff5d9..808d2e97b3f3 100644 --- a/Documentation/admin-guide/mm/multigen_lru.rst +++ b/Documentation/admin-guide/mm/multigen_lru.rst @@ -149,8 +149,8 @@ generations less than or equal to ``min_gen_nr``. ``- memcg_id node_id min_gen_nr [swappiness [nr_to_reclaim]]`` =20 ``min_gen_nr`` should be less than ``max_gen_nr-1``, since -``max_gen_nr`` and ``max_gen_nr-1`` are not fully aged (equivalent to -the active list) and therefore cannot be evicted. ``swappiness`` +``max_gen_nr`` and ``max_gen_nr-1`` are not fully aged and therefore +cannot be evicted. ``swappiness`` overrides the default value in ``/proc/sys/vm/swappiness`` and the valid range is [0-200, max], with max being exclusively used for the reclamation of anonymous memory. ``nr_to_reclaim`` limits the number of pages to evict. diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index a4daf366e6aa..b0f062e3a190 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -247,13 +247,24 @@ static inline bool lru_refs_is_workingset(unsigned in= t refs) return refs >=3D LRU_REFS_WORKINGSET; } =20 -static inline void folio_set_lru_refs(struct folio *folio, unsigned int re= fs) +/** + * __folio_set_lru_refs - Set a folio's LRU refs. + * @folio: the folio + * @refs: the new referenced count (0 .. LRU_REFS_MAX) + * + * Set the folio's LRU refs. A folio already on a gen list is left alone, + * its refs went into the active/inactive counters; use folio_inc_lru_refs= () + * or folio_reset_lru_refs() for those. + */ +static inline void __folio_set_lru_refs(struct folio *folio, unsigned int = refs) { unsigned long new_flags, old_flags =3D READ_ONCE(*folio_flags(folio, 0)); =20 do { new_flags =3D old_flags; lru_set_refs_flags(&new_flags, refs); + if (lru_get_gen_flags(old_flags) !=3D -1) + break; if (new_flags =3D=3D old_flags) break; } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); @@ -261,12 +272,19 @@ static inline void folio_set_lru_refs(struct folio *f= olio, unsigned int refs) =20 #ifdef CONFIG_LRU_GEN void folio_inc_lru_refs(struct folio *folio, unsigned int flags); +bool folio_reset_lru_refs(struct folio *folio); #else static inline void folio_inc_lru_refs(struct folio *folio, unsigned int fl= ags) { /* Should not be called with !CONFIG_LRU_GEN */ WARN_ON_ONCE(1); } + +static inline bool folio_reset_lru_refs(struct folio *folio) +{ + folio_clear_referenced(folio); + return false; +} #endif =20 /** @@ -280,7 +298,9 @@ static inline void folio_inc_lru_refs(struct folio *fol= io, unsigned int flags) */ static inline void folio_migrate_lru_refs(struct folio *new, const struct = folio *old) { - folio_set_lru_refs(new, folio_lru_refs(old)); + VM_WARN_ON_ONCE_FOLIO(folio_test_lru(old), old); + VM_WARN_ON_ONCE_FOLIO(folio_test_lru(new), new); + __folio_set_lru_refs(new, folio_lru_refs(old)); } =20 /** @@ -299,6 +319,12 @@ static __always_inline bool folio_test_workingset(cons= t struct folio *folio) } =20 #ifdef CONFIG_LRU_GEN +static inline bool lru_refs_is_active(unsigned int refs) +{ + VM_WARN_ON_ONCE(refs > LRU_REFS_MAX); + return refs >=3D LRU_REFS_ACTIVATED; +} + static inline bool lru_gen_is_old(struct lruvec *lruvec, int type, int gen) { unsigned long min_seq =3D READ_ONCE(lruvec->lrugen.min_seq[type]); @@ -309,23 +335,12 @@ static inline bool lru_gen_is_old(struct lruvec *lruv= ec, int type, int gen) gen =3D=3D lru_gen_from_seq(min_seq + 1); } =20 -static inline bool lru_gen_is_active(const struct lruvec *lruvec, int gen) -{ - unsigned long max_seq =3D READ_ONCE(lruvec->lrugen.max_seq); - - VM_WARN_ON_ONCE(gen >=3D MAX_NR_GENS); - - /* see the comment on MIN_NR_GENS */ - return gen =3D=3D lru_gen_from_seq(max_seq) || gen =3D=3D lru_gen_from_se= q(max_seq - 1); -} - static inline void lru_gen_update_size(struct lruvec *lruvec, struct folio= *folio, int old_gen, int new_gen) { int type =3D folio_is_file_lru(folio); int zone =3D folio_zonenum(folio); int delta =3D folio_nr_pages(folio); - enum lru_list lru =3D type * LRU_INACTIVE_FILE; struct lru_gen_folio *lrugen =3D &lruvec->lrugen; =20 VM_WARN_ON_ONCE(old_gen !=3D -1 && old_gen >=3D MAX_NR_GENS); @@ -336,19 +351,6 @@ static inline void lru_gen_update_size(struct lruvec *= lruvec, struct folio *foli atomic_long_sub(delta, &lrugen->nr_pages[old_gen][type][zone]); if (new_gen >=3D 0) atomic_long_add(delta, &lrugen->nr_pages[new_gen][type][zone]); - - /* return now if not a promotion */ - if (old_gen < 0 || new_gen < 0) - return; - - /* promotion */ - if (!lru_gen_is_active(lruvec, old_gen) && lru_gen_is_active(lruvec, new_= gen)) { - __update_lru_size(lruvec, lru, zone, -delta); - __update_lru_size(lruvec, lru + LRU_ACTIVE, zone, delta); - } - - /* demotion requires isolation, e.g., lru_deactivate_fn() */ - VM_WARN_ON_ONCE(lru_gen_is_active(lruvec, old_gen) && !lru_gen_is_active(= lruvec, new_gen)); } =20 static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, @@ -391,7 +393,8 @@ static inline bool __lru_gen_add_folio(struct lruvec *l= ruvec, struct folio *foli { unsigned long seq; unsigned long flags; - int gen =3D folio_lru_gen(folio); + unsigned int refs; + int gen; int type =3D folio_is_file_lru(folio); int zone =3D folio_zonenum(folio); int delta =3D folio_nr_pages(folio); @@ -399,7 +402,7 @@ static inline bool __lru_gen_add_folio(struct lruvec *l= ruvec, struct folio *foli struct lru_gen_folio *lrugen =3D &lruvec->lrugen; =20 BUILD_BUG_ON(BIT(LRU_GEN_WIDTH - 1) !=3D MAX_NR_GENS); - VM_WARN_ON_ONCE_FOLIO(gen !=3D -1, folio); + VM_WARN_ON_ONCE_FOLIO(folio_lru_gen(folio) !=3D -1, folio); =20 if (folio_test_unevictable(folio) || !lrugen->enabled) return false; @@ -410,10 +413,12 @@ static inline bool __lru_gen_add_folio(struct lruvec = *lruvec, struct folio *foli gen =3D lru_gen_from_seq(seq); flags =3D (gen + 1UL) << LRU_GEN_PGOFF; /* see the comment on MIN_NR_GENS about PG_active */ - set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK | BIT(PG_active), flags= ); + flags =3D set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK | BIT(PG_acti= ve), flags); + /* use the refs from the atomic snapshot to avoid raced update */ + refs =3D lru_get_refs_flags(flags); =20 lru_gen_update_size(lruvec, folio, -1, gen); - if (lru_gen_is_active(lruvec, gen)) + if (lru_refs_is_active(refs)) lru +=3D LRU_ACTIVE; __update_lru_size(lruvec, lru, zone, delta); =20 @@ -440,24 +445,33 @@ static inline bool lru_gen_add_rejected_folio(struct = lruvec *lruvec, struct foli static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *= folio, bool reclaiming) { unsigned long flags; - int gen =3D folio_lru_gen(folio); + unsigned int refs; + int gen; int zone =3D folio_zonenum(folio); int delta =3D folio_nr_pages(folio); enum lru_list lru =3D folio_is_file_lru(folio) * LRU_INACTIVE_FILE; + unsigned long max_seq =3D READ_ONCE(lruvec->lrugen.max_seq); =20 + /* Keep LRU_REFS_FLAGS: refs carry access history across isolation */ + flags =3D set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK, 0); + gen =3D lru_get_gen_flags(flags); + refs =3D lru_get_refs_flags(flags); if (gen < 0) return false; =20 VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio); VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); =20 - /* for folio_migrate_flags() */ - flags =3D !reclaiming && lru_gen_is_active(lruvec, gen) ? BIT(PG_active) = : 0; - flags =3D set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK, flags); - gen =3D ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1; + /* + * See the comment in lru_gen_folio_seq. For migration, compaction, + * or any other isolation of a hot folio, try best to retain its gen + * info. Ideally we would keep the full gen info. + */ + if (!reclaiming && ((max_seq - gen) % MAX_NR_GENS) < MIN_NR_GENS) + folio_set_active(folio); =20 lru_gen_update_size(lruvec, folio, gen, -1); - if (lru_gen_is_active(lruvec, gen)) + if (lru_refs_is_active(refs)) lru +=3D LRU_ACTIVE; __update_lru_size(lruvec, lru, zone, -delta); list_del(&folio->lru); @@ -511,6 +525,7 @@ static __always_inline void folio_inc_lru_refs_fast(str= uct folio *folio) unsigned long new_flags, old_flags =3D READ_ONCE(*folio_flags(folio, 0)); unsigned int refs; =20 + BUILD_BUG_ON(LRU_REFS_REFERENCED >=3D LRU_REFS_ACTIVATED); do { refs =3D lru_get_refs_flags(old_flags); if (lru_get_gen_flags(old_flags) < 0 && !lru_gen_enabled()) { @@ -547,16 +562,15 @@ static inline bool folio_check_lru_refs(const struct = folio *folio) * folio_clear_lru_refs - Clear the referenced state of an LRU managed fol= io. * @folio: The folio to clear. * - * Reset the MGLRU refs count to zero, or clear PG_referenced, leaving - * PG_workingset alone for the non-MGLRU case. Kept as a separate helper - * just like folio_check_lru_refs(). + * Reset the refs count, or just PG_referenced for the classical LRU. Duri= ng + * lru_gen_switching() a folio is still on a gen list after lru_gen_enable= d() + * turns false, so folio_clear_referenced() is only safe for isolated foli= os. */ static inline void folio_clear_lru_refs(struct folio *folio) { - if (lru_gen_enabled()) - folio_set_lru_refs(folio, 0); - else - folio_clear_referenced(folio); + /* A folio that does not read as referenced has nothing to clear. */ + if (folio_check_lru_refs(folio)) + folio_reset_lru_refs(folio); } =20 /* diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 27ba7fa6ddd6..42afb5375c27 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -434,15 +434,15 @@ enum lruvec_flags { * least twice before handing this folio over to the eviction. The first c= heck * clears the accessed bit from the initial fault; the second check makes = sure * this folio hasn't been used since then. This process, AKA second chance, - * requires a minimum of two generations, hence MIN_NR_GENS. And to mainta= in ABI - * compatibility with the active/inactive LRU, e.g., /proc/vmstat, these t= wo - * generations are considered active; the rest of generations, if they exi= st, - * are considered inactive. See lru_gen_is_active(). + * requires a minimum of two generations, hence MIN_NR_GENS. + * + * Active/inactive is per-folio based on the referenced count; + * see the comment above MAX_NR_TIERS. * * PG_active is always cleared while a folio is on one of lrugen->folios[]= so - * that the sliding window needs not to worry about it. And it's set again= when - * a folio considered active is isolated for non-reclaiming purposes, e.g., - * migration. See lru_gen_add_folio() and lru_gen_del_folio(). + * that the sliding window needs not to worry about it. It is set on a + * fault-driven refault (see lru_gen_refault), or isolation (see + * lru_gen_del_folio). * * MAX_NR_GENS is set to 4 so that the multi-gen LRU can support twice the * number of categories of the active/inactive LRU when keeping track of @@ -524,11 +524,15 @@ enum lruvec_flags { * and the bits masked by LRU_REFS_MASK as the higher bits, so the refs * count ranges from 0 to LRU_REFS_MAX. * - * Cold hints can reset refs with lru_gen_clear_refs(). Refault restores + * Cold hints can reset refs with folio_reset_lru_refs(). Refault restores * its referenced count, capped at LRU_REFS_PROTECTED, which aligns with * promotion. Fault-driven refaults of previous workingset folios set * PG_active, so they are placed in the latest gen. * + * For the active/inactive LRU ABI (/proc/vmstat), a folio is active + * if its referenced count reaches LRU_REFS_ACTIVATED. The threshold + * is currently set to LRU_REFS_PROTECTED, which is tier 2. + * * MAX_NR_TIERS is set to 4 so that the multi-gen LRU can support twice * the number of categories of the active/inactive LRU. */ @@ -543,6 +547,7 @@ enum lruvec_flags { #define LRU_REFS_REFERENCED 0x1 #define LRU_REFS_WORKINGSET 0x2 #define LRU_REFS_PROTECTED 0x3 +#define LRU_REFS_ACTIVATED LRU_REFS_PROTECTED =20 #ifndef __GENERATING_BOUNDS_H =20 @@ -651,6 +656,8 @@ struct lru_gen_mm_walk { unsigned long seq; /* the next address within an mm to scan */ unsigned long next_addr; + /* to batch activated pages */ + int nr_activated[ANON_AND_FILE][MAX_NR_ZONES]; /* to batch promoted pages */ int nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES]; /* to batch the mm stats */ diff --git a/mm/folio.c b/mm/folio.c index 204c11a71cc5..7521680874c5 100644 --- a/mm/folio.c +++ b/mm/folio.c @@ -376,35 +376,6 @@ static void __lru_cache_activate_folio(struct folio *f= olio) local_unlock(&cpu_fbatches.lock); } =20 -#ifdef CONFIG_LRU_GEN - -static bool lru_gen_clear_refs(struct folio *folio) -{ - int gen =3D folio_lru_gen(folio); - int type =3D folio_is_file_lru(folio); - unsigned long seq; - - if (gen < 0) - return true; - - folio_set_lru_refs(folio, 0); - - rcu_read_lock(); - seq =3D READ_ONCE(folio_lruvec(folio)->lrugen.min_seq[type]); - rcu_read_unlock(); - /* whether can do without shuffling under the LRU lock */ - return gen =3D=3D lru_gen_from_seq(seq); -} - -#else /* !CONFIG_LRU_GEN */ - -static bool lru_gen_clear_refs(struct folio *folio) -{ - return false; -} - -#endif /* CONFIG_LRU_GEN */ - /** * folio_mark_accessed - Mark a folio as having seen activity. * @folio: The folio to mark. @@ -429,7 +400,7 @@ void folio_mark_accessed(struct folio *folio) } =20 if (!folio_test_referenced(folio)) { - folio_set_referenced(folio); + folio_inc_lru_refs_fast(folio); } else if (folio_test_unevictable(folio)) { /* * Unevictable pages are on the "LRU_UNEVICTABLE" list. But, @@ -447,7 +418,7 @@ void folio_mark_accessed(struct folio *folio) folio_activate(folio); else __lru_cache_activate_folio(folio); - folio_clear_referenced(folio); + folio_clear_lru_refs(folio); workingset_activation(folio); } if (folio_test_idle(folio)) @@ -578,10 +549,12 @@ static void lru_lazyfree(struct lruvec *lruvec, struc= t folio *folio) folio_test_swapcache(folio) || folio_test_unevictable(folio)) return; =20 - lru_gen_clear_refs(folio); lruvec_del_folio(lruvec, folio); folio_clear_active(folio); - folio_clear_referenced(folio); + if (lru_gen_enabled()) + __folio_set_lru_refs(folio, 0); + else + folio_clear_referenced(folio); /* * Lazyfree folios are clean anonymous folios. They have * the swapbacked flag cleared, to distinguish them from normal @@ -652,7 +625,7 @@ void deactivate_file_folio(struct folio *folio) if (folio_test_unevictable(folio) || !folio_test_lru(folio)) return; =20 - if (lru_gen_enabled() && lru_gen_clear_refs(folio)) + if (lru_gen_enabled() && !folio_reset_lru_refs(folio)) return; =20 folio_batch_add_and_move(folio, lru_deactivate_file); @@ -671,7 +644,7 @@ void folio_deactivate(struct folio *folio) if (folio_test_unevictable(folio) || !folio_test_lru(folio)) return; =20 - if (lru_gen_enabled() ? lru_gen_clear_refs(folio) : !folio_test_active(fo= lio)) + if (lru_gen_enabled() ? !folio_reset_lru_refs(folio) : !folio_test_active= (folio)) return; =20 folio_batch_add_and_move(folio, lru_deactivate); diff --git a/mm/madvise.c b/mm/madvise.c index e138082c518a..c7bbf443eabf 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -378,7 +378,7 @@ static void madvise_cold_prep_folio(struct folio *folio) if (!lru_gen_enabled()) { folio_clear_lru_refs(folio); if (folio_test_active(folio)) - folio_set_lru_refs(folio, LRU_REFS_WORKINGSET); + __folio_set_lru_refs(folio, LRU_REFS_WORKINGSET); } } =20 @@ -393,7 +393,7 @@ static void madvise_pageout_prep_folio(struct folio *fo= lio) * the workingset classification is preserved for PSI. */ if (lru_gen_enabled()) - folio_set_lru_refs(folio, refs >=3D LRU_REFS_WORKINGSET ? LRU_REFS_WORKI= NGSET : 0); + __folio_set_lru_refs(folio, lru_refs_is_workingset(refs) ? LRU_REFS_WORK= INGSET : 0); } =20 static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, diff --git a/mm/vmscan.c b/mm/vmscan.c index 755bc738ee57..ccf3497bd3a4 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -870,17 +870,20 @@ enum folio_references { void folio_inc_lru_refs(struct folio *folio, unsigned int flags) { int max_gen; - int type, refs, old_gen, gen; + unsigned int refs, old_refs; + int file, old_gen, gen; unsigned long new_flags, old_flags, max_seq; + long nr_pages =3D folio_nr_pages(folio); struct lru_gen_folio *lrugen; struct lruvec *lruvec =3D NULL; =20 - type =3D folio_is_file_lru(folio); old_flags =3D READ_ONCE(*folio_flags(folio, 0)); do { new_flags =3D old_flags; old_gen =3D lru_get_gen_flags(old_flags); - refs =3D lru_get_refs_flags(old_flags) + 1; + old_refs =3D lru_get_refs_flags(old_flags); + file =3D folio_flags_is_file_lru(&old_flags); + refs =3D old_refs + 1; gen =3D old_gen; if (old_gen < 0) goto out; @@ -906,14 +909,14 @@ void folio_inc_lru_refs(struct folio *folio, unsigned= int flags) if (refs > LRU_REFS_REFERENCED || flags & LRU_REF_EXEC) gen =3D max_gen; /* First access only defers eviction of an old generation */ - else if (lru_gen_is_old(lruvec, type, old_gen)) + else if (lru_gen_is_old(lruvec, file, old_gen)) gen =3D (old_gen + 1) % MAX_NR_GENS; refs =3D min(refs, LRU_REFS_PROTECTED); } else if (refs > LRU_REFS_MAX) { /* LRU refs counting overflow, bump the gen */ gen =3D (old_gen + 1) % MAX_NR_GENS; refs =3D LRU_REFS_PROTECTED; - } else if (lru_gen_is_old(lruvec, type, old_gen) && + } else if (lru_gen_is_old(lruvec, file, old_gen) && lru_refs_is_workingset(refs)) { /* Defer eviction of just accessed workingset */ gen =3D (old_gen + 1) % MAX_NR_GENS; @@ -930,63 +933,130 @@ void folio_inc_lru_refs(struct folio *folio, unsigne= d int flags) =20 if (gen !=3D old_gen) lru_gen_update_size(lruvec, folio, old_gen, gen); + if (lru_refs_is_active(old_refs) !=3D lru_refs_is_active(refs) && old_gen= >=3D 0) { + enum lru_list lru =3D file * LRU_INACTIVE_FILE; + + __update_lru_size(lruvec, lru + lru_refs_is_active(old_refs), + folio_zonenum(folio), -nr_pages); + __update_lru_size(lruvec, lru + lru_refs_is_active(refs), + folio_zonenum(folio), nr_pages); + } if (lruvec) lruvec_unlock_irq(lruvec); } =20 +/** + * folio_reset_lru_refs - Reset the LRU refs of a folio. + * @folio: The folio. + * + * Reset the refs count, or just PG_referenced for the classical LRU, and + * keep the active and inactive sizes in sync. + * + * Context: The caller must keep the folio alive by a reference or the page + * table lock of a mapped folio. + * Return: true if the folio is on a gen list that is not the oldest one, = so + * the caller may also want to demote it. + */ +bool folio_reset_lru_refs(struct folio *folio) +{ + unsigned int refs; + int type, gen; + unsigned long seq, new_flags, old_flags; + long nr_pages =3D folio_nr_pages(folio); + struct lruvec *lruvec; + bool reset =3D false; + + old_flags =3D READ_ONCE(*folio_flags(folio, 0)); + do { + new_flags =3D old_flags; + refs =3D lru_get_refs_flags(old_flags); + type =3D folio_flags_is_file_lru(&old_flags); + gen =3D lru_get_gen_flags(old_flags); + if (!refs) + break; + /* For the classical LRU, and the lru_gen_switching() window */ + if (gen < 0 && !lru_gen_enabled()) + new_flags &=3D ~BIT(PG_referenced); + else + lru_set_refs_flags(&new_flags, 0); + } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); + + if (gen >=3D 0) { + lruvec =3D folio_lruvec_live_get(folio); + + /* If the folio is on list, caller might also want to demote it */ + seq =3D READ_ONCE(lruvec->lrugen.min_seq[type]); + reset =3D (gen !=3D lru_gen_from_seq(seq)); + if (lru_refs_is_active(refs)) { + __update_lru_size(lruvec, type * LRU_FILE + LRU_ACTIVE, + folio_zonenum(folio), -nr_pages); + __update_lru_size(lruvec, type * LRU_FILE, + folio_zonenum(folio), nr_pages); + } + + folio_lruvec_live_put(lruvec); + } + + return reset; +} + /** * folio_inc_lru_refs_walk - Update the folio's LRU refs and lazily promot= e it. * @folio: The folio. * @lruvec: The lruvec the folio belongs to. * @vma_flags: The flags of the VMA being walked. * @new_gen: The gen the folio is moved to; may be unchanged. + * @old_refs: Returns the refs count before this access. + * @new_refs: Returns the refs count after this access. * @type: Returns the folio's type: file or anon. * * Similar to folio_inc_lru_refs(), but for the page table walker and the * look-around. The LRU size accounting is batched by the caller for * better performance. * - * Return: The old generation the folio belongs to if the folio is still - * on the LRU and not in the newest generation, or -1 otherwise. -1 means - * the folio should not be promoted and @new_gen is not set. + * Return: The old generation, or -1 if the folio is off the LRU list. */ static int folio_inc_lru_refs_walk(struct folio *folio, struct lruvec *lru= vec, - const vma_flags_t *vma_flags, - int *new_gen, int *type) + const vma_flags_t *vma_flags, int *new_gen, + int *old_refs, int *new_refs, int *type) { unsigned long new_flags, old_flags =3D READ_ONCE(*folio_flags(folio, 0)); - int refs, gen, max_gen, ret; + unsigned int refs; + int old_gen, max_gen; unsigned long max_seq; =20 do { max_seq =3D READ_ONCE(lruvec->lrugen.max_seq); max_gen =3D lru_gen_from_seq(max_seq); - gen =3D lru_get_gen_flags(old_flags); + old_gen =3D lru_get_gen_flags(old_flags); refs =3D lru_get_refs_flags(old_flags) + 1; *type =3D folio_flags_is_file_lru(&old_flags); new_flags =3D old_flags; =20 - if (gen >=3D 0 && gen !=3D max_gen) { - ret =3D gen; + if (old_gen >=3D 0 && old_gen !=3D max_gen) { + *new_refs =3D min(refs, LRU_REFS_PROTECTED); /* Promote second page table access or executable */ if (lru_refs_is_workingset(refs) || is_exec_file_folio(folio, vma_flags= )) *new_gen =3D max_gen; /* First access only defers eviction of an old generation */ - else if (lru_gen_is_old(lruvec, *type, gen)) - *new_gen =3D (gen + 1) % MAX_NR_GENS; + else if (lru_gen_is_old(lruvec, *type, old_gen)) + *new_gen =3D (old_gen + 1) % MAX_NR_GENS; else - *new_gen =3D gen; + *new_gen =3D old_gen; lru_set_gen_flags(&new_flags, *new_gen); - lru_set_refs_flags(&new_flags, min(refs, LRU_REFS_PROTECTED)); + lru_set_refs_flags(&new_flags, *new_refs); } else { - ret =3D -1; - lru_set_refs_flags(&new_flags, min(refs, LRU_REFS_MAX)); + *new_gen =3D old_gen; + *new_refs =3D min(refs, LRU_REFS_MAX); + lru_set_refs_flags(&new_flags, *new_refs); } if (new_flags =3D=3D old_flags) break; } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); =20 - return ret; + *old_refs =3D lru_get_refs_flags(old_flags); + + return old_gen; } =20 /** @@ -3412,10 +3482,12 @@ static bool positive_ctrl_err(struct ctrl_pos *sp, = struct ctrl_pos *pv) * the aging *************************************************************************= *****/ =20 -static int __folio_inc_gen(struct folio *folio, int old_gen, bool *increas= ed) +static int __folio_inc_gen(struct lruvec *lruvec, struct folio *folio, + int old_gen, bool *increased) { unsigned long new_flags, old_flags =3D READ_ONCE(*folio_flags(folio, 0)); - int refs, new_gen; + unsigned int refs, old_refs; + int file, new_gen; =20 do { new_gen =3D lru_get_gen_flags(old_flags); @@ -3429,11 +3501,24 @@ static int __folio_inc_gen(struct folio *folio, int= old_gen, bool *increased) =20 new_flags =3D old_flags; new_gen =3D (old_gen + 1) % MAX_NR_GENS; - refs =3D lru_get_refs_flags(old_flags); + old_refs =3D lru_get_refs_flags(old_flags); + file =3D folio_flags_is_file_lru(&old_flags); + refs =3D min(old_refs, LRU_REFS_WORKINGSET); lru_set_gen_flags(&new_flags, new_gen); - lru_set_refs_flags(&new_flags, min(refs, LRU_REFS_WORKINGSET)); + lru_set_refs_flags(&new_flags, refs); } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); =20 + /* Refs capped below PROTECTED: demote if previously active. */ + if (lru_refs_is_active(old_refs) !=3D lru_refs_is_active(refs)) { + enum lru_list lru =3D file * LRU_INACTIVE_FILE; + int nr_pages =3D folio_nr_pages(folio); + + __update_lru_size(lruvec, lru + lru_refs_is_active(old_refs), + folio_zonenum(folio), -nr_pages); + __update_lru_size(lruvec, lru + lru_refs_is_active(refs), + folio_zonenum(folio), nr_pages); + } + if (increased) *increased =3D true; return new_gen; @@ -3445,7 +3530,7 @@ static int folio_inc_gen(struct lruvec *lruvec, struc= t folio *folio, int old_gen int new_gen; bool gen_increased; =20 - new_gen =3D __folio_inc_gen(folio, old_gen, &gen_increased); + new_gen =3D __folio_inc_gen(lruvec, folio, old_gen, &gen_increased); if (gen_increased) lru_gen_update_size(lruvec, folio, old_gen, new_gen); =20 @@ -3453,18 +3538,26 @@ static int folio_inc_gen(struct lruvec *lruvec, str= uct folio *folio, int old_gen } =20 static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *= folio, - int old_gen, int new_gen, int type) + int old_gen, int new_gen, int old_refs, int new_refs, int type) { int zone =3D folio_zonenum(folio); int delta =3D folio_nr_pages(folio); =20 - VM_WARN_ON_ONCE(old_gen >=3D MAX_NR_GENS); - VM_WARN_ON_ONCE(new_gen >=3D MAX_NR_GENS); - - walk->batched++; + /* gen counter update */ + if (old_gen !=3D new_gen) { + VM_WARN_ON_ONCE(old_gen >=3D MAX_NR_GENS); + VM_WARN_ON_ONCE(new_gen >=3D MAX_NR_GENS); + walk->batched++; + walk->nr_pages[old_gen][type][zone] -=3D delta; + walk->nr_pages[new_gen][type][zone] +=3D delta; + } =20 - walk->nr_pages[old_gen][type][zone] -=3D delta; - walk->nr_pages[new_gen][type][zone] +=3D delta; + /* active/inactive counter update */ + if (lru_refs_is_active(old_refs) !=3D lru_refs_is_active(new_refs)) { + walk->nr_activated[type][zone] +=3D + lru_refs_is_active(new_refs) ? delta : -delta; + walk->batched++; + } } =20 static void reset_batch_size(struct lru_gen_mm_walk *walk) @@ -3476,7 +3569,6 @@ static void reset_batch_size(struct lru_gen_mm_walk *= walk) walk->batched =3D 0; =20 for_each_gen_type_zone(gen, type, zone) { - enum lru_list lru =3D type * LRU_INACTIVE_FILE; int delta =3D walk->nr_pages[gen][type][zone]; =20 if (!delta) @@ -3484,10 +3576,21 @@ static void reset_batch_size(struct lru_gen_mm_walk= *walk) =20 walk->nr_pages[gen][type][zone] =3D 0; atomic_long_add(delta, &lrugen->nr_pages[gen][type][zone]); + } + + /* apply batched active/inactive updates */ + for (type =3D 0; type < ANON_AND_FILE; type++) { + for (zone =3D 0; zone < MAX_NR_ZONES; zone++) { + enum lru_list lru =3D type * LRU_INACTIVE_FILE; + int delta =3D walk->nr_activated[type][zone]; =20 - if (lru_gen_is_active(lruvec, gen)) - lru +=3D LRU_ACTIVE; - __update_lru_size(lruvec, lru, zone, delta); + if (delta) { + __update_lru_size(lruvec, lru + LRU_ACTIVE, + zone, delta); + __update_lru_size(lruvec, lru, zone, -delta); + walk->nr_activated[type][zone] =3D 0; + } + } } =20 lruvec_unlock_irq(lruvec); @@ -3642,7 +3745,7 @@ static bool suitable_to_scan(int total, int young) static void walk_update_folio(struct lru_gen_mm_walk *walk, struct vm_area= _struct *vma, struct lruvec *lruvec, struct folio *folio, bool dirty) { - int new_gen, old_gen, type; + int new_gen, old_gen, old_refs, new_refs, type; unsigned int flags =3D LRU_REF_MAPPED; =20 if (!folio) @@ -3655,9 +3758,10 @@ static void walk_update_folio(struct lru_gen_mm_walk= *walk, struct vm_area_struc =20 if (walk) { old_gen =3D folio_inc_lru_refs_walk(folio, lruvec, &vma->flags, - &new_gen, &type); - if (old_gen >=3D 0 && old_gen !=3D new_gen) - update_batch_size(walk, folio, old_gen, new_gen, type); + &new_gen, &old_refs, &new_refs, &type); + if (old_gen >=3D 0) + update_batch_size(walk, folio, old_gen, new_gen, + old_refs, new_refs, type); } else { if (is_exec_file_folio(folio, &vma->flags)) flags |=3D LRU_REF_EXEC; @@ -4016,6 +4120,7 @@ static void clear_mm_walk(void) =20 VM_WARN_ON_ONCE(walk && memchr_inv(walk->nr_pages, 0, sizeof(walk->nr_pag= es))); VM_WARN_ON_ONCE(walk && memchr_inv(walk->mm_stats, 0, sizeof(walk->mm_sta= ts))); + VM_WARN_ON_ONCE(walk && memchr_inv(walk->nr_activated, 0, sizeof(walk->nr= _activated))); =20 current->reclaim_state->mm_walk =3D NULL; =20 @@ -4054,8 +4159,6 @@ static bool inc_min_seq(struct lruvec *lruvec, int ty= pe, int swappiness) goto done; =20 VM_WARN_ON_ONCE(get_nr_gens(lruvec, type) !=3D MAX_NR_GENS); - VM_WARN_ON_ONCE(lru_gen_is_active(lruvec, old_gen) !=3D - lru_gen_is_active(lruvec, target_gen)); /* prevent cold/hot inversion if the type is evictable */ for (zone =3D 0; zone < MAX_NR_ZONES; zone++) { struct list_head *target_list =3D &lrugen->folios[target_gen][type][zone= ]; @@ -4078,7 +4181,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int ty= pe, int swappiness) =20 prefetchw_next_lru_folio(folio, head); pos =3D pos->next; - new_gen =3D __folio_inc_gen(folio, old_gen, &gen_increased); + new_gen =3D __folio_inc_gen(lruvec, folio, old_gen, &gen_increased); /* * If gen_increased is false, this is a promotion. Put folios * at the head of the promoted gen. Otherwise, put them at @@ -4178,8 +4281,7 @@ static void try_to_inc_min_seq(struct lruvec *lruvec,= int swappiness) static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swap= piness) { bool success; - int prev, next; - int type, zone; + int type, next; struct lru_gen_folio *lrugen =3D &lruvec->lrugen; restart: if (seq < READ_ONCE(lrugen->max_seq)) @@ -4205,32 +4307,10 @@ static bool inc_max_seq(struct lruvec *lruvec, unsi= gned long seq, int swappiness goto restart; } =20 - /* - * Update the active/inactive LRU sizes for compatibility. Both sides of - * the current max_seq need to be covered, since max_seq+1 can overlap - * with min_seq[LRU_GEN_ANON] if swapping is constrained. And if they do - * overlap, cold/hot inversion happens. - */ - prev =3D lru_gen_from_seq(lrugen->max_seq - 1); - next =3D lru_gen_from_seq(lrugen->max_seq + 1); - - for (type =3D 0; type < ANON_AND_FILE; type++) { - for (zone =3D 0; zone < MAX_NR_ZONES; zone++) { - enum lru_list lru =3D type * LRU_INACTIVE_FILE; - long delta =3D atomic_long_read(&lrugen->nr_pages[prev][type][zone]) - - atomic_long_read(&lrugen->nr_pages[next][type][zone]); - - if (!delta) - continue; - - __update_lru_size(lruvec, lru, zone, delta); - __update_lru_size(lruvec, lru + LRU_ACTIVE, zone, -delta); - } - } - for (type =3D 0; type < ANON_AND_FILE; type++) reset_ctrl_pos(lruvec, type, false); =20 + next =3D lru_gen_from_seq(lrugen->max_seq + 1); WRITE_ONCE(lrugen->timestamps[next], jiffies); /* make sure preceding modifications appear */ smp_store_release(&lrugen->max_seq, lrugen->max_seq + 1); @@ -4743,7 +4823,6 @@ static void __lru_gen_reparent_memcg(struct lruvec *c= hild_lruvec, struct lruvec int zone, int type) { struct lru_gen_folio *child_lrugen, *parent_lrugen; - enum lru_list lru =3D type * LRU_INACTIVE_FILE; int gen; =20 child_lrugen =3D &child_lruvec->lrugen; @@ -4756,8 +4835,6 @@ static void __lru_gen_reparent_memcg(struct lruvec *c= hild_lruvec, struct lruvec */ for (gen =3D 0; gen < MAX_NR_GENS; gen++) { long nr_pages =3D atomic_long_read(&child_lrugen->nr_pages[gen][type][zo= ne]); - int child_lru_active =3D lru_gen_is_active(child_lruvec, gen) ? LRU_ACTI= VE : 0; - int parent_lru_active =3D lru_gen_is_active(parent_lruvec, gen) ? LRU_AC= TIVE : 0; =20 /* Assuming that child pages are colder than parent pages */ list_splice_tail_init(&child_lrugen->folios[gen][type][zone], @@ -4765,11 +4842,6 @@ static void __lru_gen_reparent_memcg(struct lruvec *= child_lruvec, struct lruvec =20 atomic_long_set(&child_lrugen->nr_pages[gen][type][zone], 0); atomic_long_add(nr_pages, &parent_lrugen->nr_pages[gen][type][zone]); - - if (lru_gen_is_active(child_lruvec, gen) !=3D lru_gen_is_active(parent_l= ruvec, gen)) { - __update_lru_size(child_lruvec, lru + child_lru_active, zone, -nr_pages= ); - __update_lru_size(parent_lruvec, lru + parent_lru_active, zone, nr_page= s); - } } } =20 @@ -5169,7 +5241,7 @@ static int evict_folios(unsigned long nr_to_scan, str= uct lruvec *lruvec, * PG_active and refs are placement hints; cap refs to retain * workingset history without carrying a high tier into the new gen. */ - folio_set_lru_refs(folio, min(folio_lru_refs(folio), LRU_REFS_WORKINGSET= )); + __folio_set_lru_refs(folio, min(folio_lru_refs(folio), LRU_REFS_WORKINGS= ET)); } =20 move_folios_to_lru(&list, scanned_seq, false); diff --git a/mm/workingset.c b/mm/workingset.c index eea711057c72..79cfaefad772 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -334,7 +334,7 @@ static void lru_gen_refault(struct folio *folio, void *= shadow) mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta); } /* Refault is also promotion, cap the refs like folio_inc_lru_refs */ - folio_set_lru_refs(folio, min(refs, LRU_REFS_PROTECTED)); + __folio_set_lru_refs(folio, min(refs, LRU_REFS_PROTECTED)); } =20 if (lru_refs_is_workingset(refs)) --=20 2.56.1 From nobody Tue Nov 24 11:35:59 2026 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=1795460031; b=rVrensBLlTe0/vk9TJlWfVai9aDAVkYH6eqAGYOvH1vgetj2F+WV+DydK1AJJqBZOCyq UVSIQVejVF001bU54C0tIlw4jtPdb5MltqC/+2UenN+NWWVO4CGxpV9gR92YKu2Lllwbj IVvoMJp0Tp4KHeyW0BHZUB6iO2JJOi8EWnXftpGx5f23A3eHbjTfAd9bkLIIyou8oBMvn kHt2sxxI3eOtJ3p+1ZkzLnLlzLI8onICTLAzE4pU/Tj63/DqopJNq80R91I/ZQuIoXFne nDqqC1ppqLBpsZDo1d49qJcF6dezhhG+UkHs9onp+TnJrWv7edTdJfXJqLRRx1nYtEw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795460031; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=DzT/xUc8ZCEsmES1lvYoTZAcG/yg9FkH/3wyr37nKM0=; b=sBevy9e3kPuzKJykRRTGD5aA6q2xepS5DKlH+gdp0woDcFF8ISmWC35ZeIQczxz1ox95 gpwjNO6eYzJld8WRvkCaNEiEGlX24OyyL1m8lvKIXCnRmle2yzYECz12j5SQvsPzqlokD x/VYaqAXl3o7YbjbXNR8BtnTWgFjFuso96/2/rFlLEwghDs3mS/zDb9KtwjGDhu/2i2QF 3QOnceJuBv/JmWTSHQ2Ho76LUfsb23K84qMTNEMKk5tKqiIYiy6TiCR6+EzHpGc4r6K+n HkkuzHby+bdA8FCT/+QTmdLhuCumAGO2SfQcpaqKnCnpaL0TFvNQ0OSKcSWkMpf0BYA== 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="C/v5q04U"; 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 84604606A33E; Mon, 23 Nov 2026 18:53:51 +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=C/v5q04U 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=1795460031; b=hVSj1WuV1HleJnZTMJDXKGbaiyL4qR6C5Qh878MX2metAr7Mj4tdHPzMymSXoBzrieTG OonAwXAINeynVNPlKBw0YIcJQnEYaHqtR/80zGWTdIGgpLOxbx/vJTfKwG148yHTQvZY7 ZQZOOwFnjhWp89yMscO6Z7qGNLGfvrA5DMdsuYMihK1B0I2TqBbJvbeMKXBtP5so/O0Rn DUsDnGPnz/WWvILUa+EpA4xnG93qqaHcuR942bc9LZiz8SjBTUB4IcRHl6DFY1uGZCOy6 +Opvz1Xu9EY9T8DCj+JyQdRBBlZXa+QT0CLy1VreT7zkoVzGg9VzjSwlbvdAVf9NnxA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795460031; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=DzT/xUc8ZCEsmES1lvYoTZAcG/yg9FkH/3wyr37nKM0=; b=b7BFueW5SP6Tg5HWYmKhYrmLQNe9byH19XF+gdqcwpUWqs84q32VRQwuP/xCWl5pwM0V 2+trSAfULZBHYwH6UvF8I8xgk+cW56+krjt1AVsqUa0LOjTDWFT6zU0Cmf8GQ83Om1/Ui NgzjrHHjk+cvfsJzw6hPfpf/XqKEYDexLvneFi+XPUQISmK2nCAnG42+W887d8+IqqCMt /gvfqUIGyoycP7RmvBRzt7/66Y3ZDAbgFEXOwW4TUPC1cZg8voFG6MWKm5TpiVr1UN5Dc qokg/zz3EI1JYw8x0qJAYRLTcBUl3bANHbLvSOWlAz+G79/NoP5k1VoI4VaU9o0vYVw== 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 701EE1F000C4; Mon, 23 Nov 2026 18:53:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795460031; bh=DzT/xUc8ZCEsmES1lvYoTZAcG/yg9FkH/3wyr37nKM0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=C/v5q04U8/3ug6jVO2zWIZfehF5Nj1RdJsGEvyuCOgL9vJxLHvUijCPkVw864qYzw cRyiAKxrjve1QLZBXM8gn7pbQW67pzuRgrznNOfQGOdXMimEohj7T0DxHu8a3LqmGM ZiQe/LAS6bABw0fppZcsbzm4z39TcQcbRea0yhn866KgUKd5BZzpPh63EBbbCYXIFD Y/qkj+Q1uvXBu7jHbeH20oFGaVqYG+p2u06oOv8y5MAm6yPJyi7DoiraIiWMa5fcRk gzxezQxLXYXIikV5/MVtH6tWOXvc1FAZdmNDTQRwxgfLFllVyW+nOkkjSiuT2ktXKv KdQavuoffQCtA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 3ACFA4C671; Mon, 23 Nov 2026 18:53:02 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 24 Nov 2026 02:53:10 +0800 Subject: [PATCH v6 18/18] mm/mglru: make folio_inc_lru_refs lruvec lockless 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" Message-Id: <20261124-mglru-fg-v6-18-8ffea39934a6@tencent.com> References: <20261124-mglru-fg-v6-0-8ffea39934a6@tencent.com> In-Reply-To: <20261124-mglru-fg-v6-0-8ffea39934a6@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 , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Tianyi Chen , Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795459977; l=8138; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=98177vt1OglL82gztUX/EbOw0UvOi4GjiFKgTcy/MHo=; b=lBjYmotb+ySa2fIWkROeOCyMpcTaqydsJhwBMvXZJ4Uy1RPDLNQjk8vOBroK1JvAny2eG5OzE r+CJ+hmVAgqBKWNO6Lw8XYQN07RofDMXRn5buGju896Qbd0+UHf79zD 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 Content-Transfer-Encoding: quoted-printable From: Kairui Song The lruvec spinlock in folio_inc_lru_refs was only taken to keep concurrent aging from corrupting the size counters. This is no longer needed: the per-generation counters are atomic, and the active/inactive ABI counters are updated per folio and linearized by the CAS on the folio flags, so concurrent aging can no longer corrupt them. The same argument is what lets folio_reset_lru_refs() update the counters without the lruvec lock. Use folio_lruvec_live_get()/folio_lruvec_live_put() to hold the RCU read lock around the lruvec lookup, and drop the spinlock entirely. Without the lock, aging can advance max_seq while a promotion is in flight. Generations are indexed by seq % MAX_NR_GENS, so the folio flags can return to the value read earlier and the cmpxchg succeeds with a target gen computed from a stale max_seq. Recheck max_seq after a gen-changing cmpxchg: if it moved, account the committed update and retry with LRU_REF_FORCE, which promotes the folio to the newest gen. The retry may count the access twice, which is acceptable and avoids folio_activate(), whose LRU lock causes latency jitter. smp_rmb() orders the flags read, including the one returned by a failed cmpxchg(), before the max_seq read. Also simplify the post-CAS accounting guards: gen is only assigned non-negative values after the old_gen < 0 early exit, so gen !=3D old_gen implies gen >=3D 0, and lru_gen_update_size() needs no explicit guard. The active/inactive ABI update keeps its gen >=3D 0 check because an off-LRU folio has no lruvec to update. Tested-by: Andrew Morton Reviewed-by: Kunwu Chan Signed-off-by: Kairui Song Tested-by: Tianyi Chen Tested-by: Wang Lian (ByteDance) Tested-by: Zhao Zhenzhuo --- include/linux/mm_inline.h | 10 +++++----- include/linux/mmzone.h | 3 ++- mm/vmscan.c | 51 +++++++++++++++++++++++++++++--------------= ---- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index b0f062e3a190..36b80a2a0e74 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -335,10 +335,9 @@ static inline bool lru_gen_is_old(struct lruvec *lruve= c, int type, int gen) gen =3D=3D lru_gen_from_seq(min_seq + 1); } =20 -static inline void lru_gen_update_size(struct lruvec *lruvec, struct folio= *folio, - int old_gen, int new_gen) +static inline void lru_gen_update_size(struct lruvec *lruvec, int type, + struct folio *folio, int old_gen, int new_gen) { - int type =3D folio_is_file_lru(folio); int zone =3D folio_zonenum(folio); int delta =3D folio_nr_pages(folio); struct lru_gen_folio *lrugen =3D &lruvec->lrugen; @@ -417,7 +416,7 @@ static inline bool __lru_gen_add_folio(struct lruvec *l= ruvec, struct folio *foli /* use the refs from the atomic snapshot to avoid raced update */ refs =3D lru_get_refs_flags(flags); =20 - lru_gen_update_size(lruvec, folio, -1, gen); + lru_gen_update_size(lruvec, type, folio, -1, gen); if (lru_refs_is_active(refs)) lru +=3D LRU_ACTIVE; __update_lru_size(lruvec, lru, zone, delta); @@ -447,6 +446,7 @@ static inline bool lru_gen_del_folio(struct lruvec *lru= vec, struct folio *folio, unsigned long flags; unsigned int refs; int gen; + int type =3D folio_is_file_lru(folio); int zone =3D folio_zonenum(folio); int delta =3D folio_nr_pages(folio); enum lru_list lru =3D folio_is_file_lru(folio) * LRU_INACTIVE_FILE; @@ -470,7 +470,7 @@ static inline bool lru_gen_del_folio(struct lruvec *lru= vec, struct folio *folio, if (!reclaiming && ((max_seq - gen) % MAX_NR_GENS) < MIN_NR_GENS) folio_set_active(folio); =20 - lru_gen_update_size(lruvec, folio, gen, -1); + lru_gen_update_size(lruvec, type, folio, gen, -1); if (lru_refs_is_active(refs)) lru +=3D LRU_ACTIVE; __update_lru_size(lruvec, lru, zone, -delta); diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 42afb5375c27..14c58bbd1606 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -540,9 +540,10 @@ enum lruvec_flags { #define LRU_TIER_MIN 0U #define LRU_TIER_MAX (MAX_NR_TIERS - 1) =20 -/* Access source flags for folio_inc_lru_refs() */ +/* Flags for folio_inc_lru_refs() */ #define LRU_REF_MAPPED 0x1U #define LRU_REF_EXEC 0x2U +#define LRU_REF_FORCE 0x4U =20 #define LRU_REFS_REFERENCED 0x1 #define LRU_REFS_WORKINGSET 0x2 diff --git a/mm/vmscan.c b/mm/vmscan.c index ccf3497bd3a4..81797b71228f 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -876,37 +876,35 @@ void folio_inc_lru_refs(struct folio *folio, unsigned= int flags) long nr_pages =3D folio_nr_pages(folio); struct lru_gen_folio *lrugen; struct lruvec *lruvec =3D NULL; + bool aged =3D false; =20 +retry: old_flags =3D READ_ONCE(*folio_flags(folio, 0)); do { new_flags =3D old_flags; old_gen =3D lru_get_gen_flags(old_flags); old_refs =3D lru_get_refs_flags(old_flags); - file =3D folio_flags_is_file_lru(&old_flags); refs =3D old_refs + 1; gen =3D old_gen; + file =3D folio_flags_is_file_lru(&old_flags); if (old_gen < 0) goto out; - /* - * Lock the lruvec if the folio is on-list. We are already - * doing lazy promotion so in theory we don't need this, - * but for now, concurrent aging would still corrupt the - * size counters. This is a temporary limitation and - * will be lifted very soon, so the lock here is not a - * performance concern. - */ if (!lruvec) { - lruvec =3D lruvec_live_lock_irq(folio_lruvec(folio)); + lruvec =3D folio_lruvec_live_get(folio); lrugen =3D &lruvec->lrugen; } + /* + * Read the flags before max_seq, paired with the store of a + * gen, which samples max_seq first. + */ + smp_rmb(); max_seq =3D READ_ONCE(lrugen->max_seq); max_gen =3D lru_gen_from_seq(max_seq); if (old_gen =3D=3D max_gen) goto out; - - if (flags & (LRU_REF_MAPPED | LRU_REF_EXEC)) { - /* Promote second page table access or executable */ - if (refs > LRU_REFS_REFERENCED || flags & LRU_REF_EXEC) + if (flags & (LRU_REF_MAPPED | LRU_REF_EXEC | LRU_REF_FORCE)) { + if (lru_refs_is_workingset(refs) || + flags & (LRU_REF_EXEC | LRU_REF_FORCE)) gen =3D max_gen; /* First access only defers eviction of an old generation */ else if (lru_gen_is_old(lruvec, file, old_gen)) @@ -931,9 +929,19 @@ void folio_inc_lru_refs(struct folio *folio, unsigned = int flags) break; } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); =20 - if (gen !=3D old_gen) - lru_gen_update_size(lruvec, folio, old_gen, gen); - if (lru_refs_is_active(old_refs) !=3D lru_refs_is_active(refs) && old_gen= >=3D 0) { + if (gen !=3D old_gen) { + /* + * Gen-index reuse can fool cmpxchg(). Retry locklessly, accepting + * an extra reference to avoid folio_activate() latency. + */ + if (unlikely(READ_ONCE(lrugen->max_seq) !=3D max_seq)) { + flags =3D LRU_REF_FORCE; + aged =3D true; + } + lru_gen_update_size(lruvec, file, folio, old_gen, gen); + } + + if (lru_refs_is_active(old_refs) !=3D lru_refs_is_active(refs) && gen >= =3D 0) { enum lru_list lru =3D file * LRU_INACTIVE_FILE; =20 __update_lru_size(lruvec, lru + lru_refs_is_active(old_refs), @@ -941,8 +949,12 @@ void folio_inc_lru_refs(struct folio *folio, unsigned = int flags) __update_lru_size(lruvec, lru + lru_refs_is_active(refs), folio_zonenum(folio), nr_pages); } + if (aged) { + aged =3D false; + goto retry; + } if (lruvec) - lruvec_unlock_irq(lruvec); + folio_lruvec_live_put(lruvec); } =20 /** @@ -3527,12 +3539,13 @@ static int __folio_inc_gen(struct lruvec *lruvec, s= truct folio *folio, /* protect pages selected by the tier PID controller */ static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio, int o= ld_gen) { + int type =3D folio_is_file_lru(folio); int new_gen; bool gen_increased; =20 new_gen =3D __folio_inc_gen(lruvec, folio, old_gen, &gen_increased); if (gen_increased) - lru_gen_update_size(lruvec, folio, old_gen, new_gen); + lru_gen_update_size(lruvec, type, folio, old_gen, new_gen); =20 return new_gen; } --=20 2.56.1