From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796156398; b=xDgvaXop5zcAxGZ7VDfTLy/F2rlQOZ7Uxl61KDuHmhe/6loglKLMSC/82R0NqbaPqnNh 1RpnkjR0bv0CLJkiZyY7HGQwf/fIFkOnwzGAdQ4lP+KFr52xc1yW2avBx1+eyimEeBEuh oaqpgSGAZRF5lhePe+Ep4+4bLY2VL8oazAWIachkjpyhEL82P1VwEdeF20cKs/nuYowwK QiQ9XiAY4RLpcPzinFADV6u3qKpht1r5LsnxE4ZJG6KPn8XOsZ+/3FEbHv5SfUZe1ALUm HMIXzHC8PF72aUinORDKmutTJc0/iNubSwNgAcxNHdosS525T1/oFpHrQ+JhedIhxJA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796156398; 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=YCt9Mjl2wYLFCznV8NAOGsiDJpFJXsD7PhiEAfRz8zg=; b=M4a3oZuXRSL+6BMBnAHTbS8CvmvRShPDMrxa1NN4oqDVPZ8JBxlfrT/AFvxWG3a3HMMt 9ACyUe8p9c6jXgchb2aGpfRTkZGPfm1Go1bCrwmUjIwTehcVoPCsFokJ1E5MgottCJUIB ldplv69fA4GqXig3NqhGXVlJDYlKMQz6oi7aXX0huf15PJIXZ6WoqhJMvPjqYdUV19e1h iByOeAMx67iyKX9Yzhmj6cbyWGyV/EIBsFuuW8HZ24wQMol11VFe/C+vsOoWQEhNZ6MRa FmTid3FAZTCtuxzZ+AV4L7iqLPYLkJ6D14pXCGLIAv1gaoD7viWsrbn9VTZMquNG2Nw== 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=XDBruf+I; 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 4106E60A261E; Tue, 1 Dec 2026 20:19:58 +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=XDBruf+I 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=1796156397; b=KkSUH/wsPUJEmLfyT7It2tjQ1G2Cu1fMbZTk2kCXdNUAcQVkFgxWQnj2IE09Ez+xFTA3 23QoNOXMwa0r4tzAerhqEOrTyUR0eTXeqm4Gdyqn//USGzUkk3jPS0mhT9ICBhxFRfQJz AwXTaVtrzVGEQA9+n8nfwBb6QytjEapomRIHqpXKCcVp3rjCcvP8HoyYJdl12ktxn2uby aAfZiN6tXs8mCmoRw/aEbY200HiHfEZaNeQxCAp07k8f177Q/NgRcWsDNKl4YgquJnc8y HSPhlb53zmYxf6OxZ2s/fom0xCk89Joibghw0+m4uU3preA1Fxk9C+loDogpsKno5+A== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796156397; 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=YCt9Mjl2wYLFCznV8NAOGsiDJpFJXsD7PhiEAfRz8zg=; b=tU9rRc5UXRnLfhS1/h7Bu2lM8PFkyWd+NSVTXQ/r80jek7japbF0C9rwklboHs1FaKKs BGUFm2/Q6nKn4r6GwV/a5IbK1Tay7EeHrOULY2KuA1P1k3gyS+Y7mq6o3NW1DSA0/wpPH xaAAdOg4jx18IE6KGSzsNpjJegdORJBVhbriWiRGcGPKS4e7iKCYyEi8IgWsp+ks+ENyT FwuWww/vehOTeH5d213ikKFwP91XcQmK5URgtk0rvPM4LsrEwDkp1GQNiPKbsPfV8csU4 E3zvOR1gJ0msP8HeGOfzjuDCYOtGwnA1aUSw4lcYwK+xDweZ8uBX9EByGVHc959UAug== 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 C78651F0092F; Tue, 1 Dec 2026 20:19:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796156397; bh=YCt9Mjl2wYLFCznV8NAOGsiDJpFJXsD7PhiEAfRz8zg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=XDBruf+IhpUNkIL+JGRmgblDH0puJjgrePWEUnpdr6hNTPwkguNeu9mcNB2YNU8qF N9BUtQ7r+CrwKwKJIj+JBLV/VlYEjKSzlslX/MKzYSai/w99qmdhjRwMpe0XpC6TRS r9q+z9Hfnfwo3mX0PPmzavRMSwDDHMMf6vXTq6e5l6riXT41DbUdAWAin/9codC/bS WyzzT3EUPOlHQuiEUirc5+xzZ72H0k1eM/e/jpNFOLYcOFS+OyjwlZYTy2h1c/kGPC keNbGfBp5WFYu9DxpG8a7QyON85HchCpLVs/uCrwAlS0wvYdgLLIO1Hy+yd/8IU3Tc 7qBb2rj6nv8cQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 6F7E14F0CA; Tue, 1 Dec 2026 20:19:22 +0000 (UTC) From: Kairui Song via B4 Relay Date: Wed, 02 Dec 2026 04:18:54 +0800 Subject: [PATCH v7 14/19] 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: <20261202-mglru-fg-v7-14-5965a96d86d0@tencent.com> References: <20261202-mglru-fg-v7-0-5965a96d86d0@tencent.com> In-Reply-To: <20261202-mglru-fg-v7-0-5965a96d86d0@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Johannes Weiner , Muchun Song , Qi Zheng , Ying Huang , Chris Li , Baoquan He , Nico Pache , Usama Arif , Michal Hocko , Roman Gushchin , Shakeel Butt , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Vlastimil Babka , Suren Baghdasaryan , Nhat Pham , Youngjun Park , Zi Yan , Gregory Price , "Matthew Wilcox (Oracle)" , Baolin Wang , Ryan Roberts , Dev Jain , Lance Yang , Hugh Dickins , SJ Park , David Rientjes , Yu Zhao , Vernon Yang , Zicheng Wang , Tal Zussman , Kairui Song , Xiang Liu , Ehab Ababneh , Zhao Zhenzhuo , Lian Wang , Joanne Koong , Kunwu Chan , Hui Zhu , Maxim Muzafarov , Tianyi Chen , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796156357; l=4183; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=Rtt4TZ0m28dwX5GtIjEc/P3wlkAwBd7EBux4f2L1vpo=; b=Mt+1NWYbolHdkZBa/vLuqKdv7R8ZKIRUqmC6bh7xgj5NXFQLJl0yX3tju8J3epXH95c3/0wgP ZFmTqfsb++PAD431DwOJHhd6xAE2rLvqRqbuDOp4Z4EUY7nmR/5XQxp 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: 7bit 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 --- mm/madvise.c | 50 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index 66600e82605c..c6c2e8189f4b 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -359,6 +359,38 @@ static inline int madvise_folio_pte_batch(unsigned long addr, unsigned long end, FPB_MERGE_YOUNG_DIRTY); } +static void madvise_cold_prep_folio(struct folio *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); + } else { + folio_test_clear_young(folio); + } +} + +static void madvise_pageout_prep_folio(struct folio *folio) +{ + unsigned int refs = 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 >= LRU_REFS_WORKINGSET ? LRU_REFS_WORKINGSET : 0); +} + static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, struct mm_walk *walk) @@ -433,12 +465,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, tlb_remove_pmd_tlb_entry(tlb, pmd, addr); } - 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 @@ -543,18 +573,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, tlb_remove_tlb_entries(tlb, pte, nr, addr); } - /* - * 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 -- 2.56.1