Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795940631; b=5XQQpDPCC8Xj5oNtqyO+kTqg2e9YvfmPLsmRXJuL5krCRe3Zl5nPuTF42wcmyHolH3z5 MaLF07OyNMK9XNa6/hDuVdGUF6tp036/dAZx2c9G1xFqd+JYCKMerjZ+nFCFbXcf1Y2RT pj5bSUSOAp4hBhhoYQyLc0ijAm4X/AC5Ki/Gqbk+y4FPSI4rNm6qbku/lYAYjagjbXs79 iaqCZyjmFtF7i3/uZ4xgtij9hA2TmwZFuPHfm4rcHdQJwAsmLnoZwPCHVTQwa12a1rB9Z 2UuFRUPWGTByOSf5Annte7XCfkcXlSycWjv0hVAHIBTNR19y3HUtjGHz/gE2SxbIk4A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795940631; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=tnEPBxwaKW/UT80TEk310682ILWcWZzjG9bcDENkFDs=; b=VbpCHdKvv2cZGDJWMhq16UEfpjhAcUC7e+lEDd1Uvh4svW4YTtRC0PYqMYAa/c3HWKrn dvno2dlKIFUxFvQo9+OYY1bmf90F9L08o1oDZDrFNy8Q//4HrY3GSv5UE/06IsmllZzHu 1msmg3qaRiHEULfjC8F5aizogrsQDheji91pdsg0lss8a7mtN+AywV4efJ24gVh2odEvW jDuLVPOi4W6NAJ4u8mAyfM1kN0yAsmf2BI7lzNTj2NRUGIgKpCUlbBFc4P/dB041FQKmI hqcj1cNqPbKDWJ8kgovvydYaKxt1AWs4HtUNHPJGRM4Uh+hpJvIXYj5Oqwx0Wdc1wcA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=dtJWlacw; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42B82606A31C for ; Sun, 29 Nov 2026 08:23:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=dtJWlacw Received: by smtp.kernel.org (Postfix) with ESMTPSA id B79411F00898; Sun, 29 Nov 2026 08:23:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1795940630; bh=tnEPBxwaKW/UT80TEk310682ILWcWZzjG9bcDENkFDs=; h=Date:To:From:In-Reply-To:References:Subject; b=dtJWlacwMbE3l08nhhp/pw0ITu629caZZQanRNDn3E3mYCEK3YIbLhQAhhxTxTS+h CcxZBkjaiudVdLA+Ah4/wIvF0DB060SJ9wbpEbZIdVznzQQpdEvFZTopn9H8JdJffd 4njM9LwKVRlMIB5q7FUmutKZg2UKFlMuY8rqWm/g= Date: Sun, 29 Nov 2026 00:23:50 -0800 To: mm-commits@vger.kernel.org,yuzhao@google.com,yuanchu@google.com, weixugc@google.com,vbabka@kernel.org,shakeel.butt@linux.dev,rppt@kernel.org, qi.zheng@linux.dev,mhocko@suse.com,liam@infradead.org,kasong@tencent.com, hannes@cmpxchg.org,david@kernel.org,baoquan.he@linux.dev, baolin.wang@linux.alibaba.com,baohua@kernel.org,axelrasmussen@google.com, chenridong@xiaomi.com,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261127031110.1034854-3-ridong.chen@linux.dev> References: <20261127031110.1034854-3-ridong.chen@linux.dev> Subject: + mm-mglru-deactivate-anon-folios-into-the-oldest-generation.patch added to mm-new branch Message-Id: <20261129082350.B79411F00898@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: mm/mglru: deactivate anon folios into the oldest generation has been added to the -mm mm-new branch. Its filename is mm-mglru-deactivate-anon-folios-into-the-oldest-generation.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-mglru-deactivate-anon-folios-into-the-oldest-generation.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Ridong Chen Subject: mm/mglru: deactivate anon folios into the oldest generation Date: Fri, 27 Nov 2026 11:11:10 +0800 folio_deactivate() is used by MADV_COLD and DAMOS_LRU_DEPRIO to move a folio towards eviction. lru_deactivate() does that by deleting the folio from its LRU list, clearing PG_active and PG_referenced, and adding it back with lruvec_add_folio(). With MGLRU, lru_gen_folio_seq() places a swap-backed folio that is not in the swap cache, i.e. an anon or shmem folio, into max_seq - 1, regardless of its refs. So a folio that has aged into an older generation, e.g. min_seq + 1, is moved to a younger generation instead, while PGDEACTIVATE is still counted. Other folios go to an older generation with their refs cleared: the oldest one, or the second oldest if PG_workingset is set. The ones that have PG_reclaim set and are dirty or under writeback also go to max_seq - 1. As a result, MADV_COLD can make anon memory less likely to be reclaimed. It can be reproduced with a single process in a memcg with swap enabled: 1) map two 64MB anon regions A and B, and fault in A; 2) age the memcg once through /sys/kernel/debug/lru_gen, then fault in B, so that B is one generation younger than A; 3) apply MADV_COLD to B only, A is the control; 4) reclaim the size of one region from anon only: echo "64M swappiness=max" > memory.reclaim; 5) count the swapped out pages of A and B from /proc/self/pagemap. B is expected to be swapped out instead of A. Before this patch, B stays in its generation after MADV_COLD, so A is swapped out instead. After it, B is moved to min_seq. Three runs on mm-new with this patch: A swapped out B (MADV_COLD) swapped out 0 ~ 190 pages 16384 pages Cap the generation to min_seq[type] for MGLRU, so that the folio is added to the head of the oldest generation. This matches the classic LRU, which adds a deactivated folio to the head of the inactive list, and is where most file folios already go. Folios that have PG_reclaim set and are dirty or under writeback are now placed there too, as the classic LRU puts them on the inactive list. min_seq only advances under the LRU lock, which is held here. Link: https://patch.msgid.link/20261127031110.1034854-3-ridong.chen@linux.dev Fixes: 081488051d28 ("mm/mglru: fix underprotected page cache") Signed-off-by: Ridong Chen Signed-off-by: Andrew Morton Suggested-by: Barry Song Reviewed-by: Barry Song Cc: David Hildenbrand Cc: Kairui Song Cc: Johannes Weiner Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Mike Rapoport Cc: Michal Hocko Cc: Qi Zheng Cc: Shakeel Butt Cc: Axel Rasmussen Cc: Yuanchu Xie Cc: Wei Xu Cc: Baoquan He Cc: Baolin Wang Cc: Yu Zhao mm/folio.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) --- a/mm/folio.c~mm-mglru-deactivate-anon-folios-into-the-oldest-generation +++ a/mm/folio.c @@ -599,6 +599,25 @@ static void lru_deactivate_file(struct l } } +#ifdef CONFIG_LRU_GEN +/* with MGLRU, add the folio to the head of the oldest generation */ +static inline void lruvec_add_deactivated_folio(struct lruvec *lruvec, + struct folio *folio) +{ + int type = folio_is_file_lru(folio); + unsigned long seq = lruvec->lrugen.min_seq[type]; + + if (!lru_gen_add_folio(lruvec, folio, MIN_SEQ_UNBOUNDED, seq, false)) + lruvec_add_folio(lruvec, folio); +} +#else +static inline void lruvec_add_deactivated_folio(struct lruvec *lruvec, + struct folio *folio) +{ + lruvec_add_folio(lruvec, folio); +} +#endif + static void lru_deactivate(struct lruvec *lruvec, struct folio *folio) { long nr_pages = folio_nr_pages(folio); @@ -609,7 +628,7 @@ static void lru_deactivate(struct lruvec lruvec_del_folio(lruvec, folio); folio_clear_active(folio); folio_clear_referenced(folio); - lruvec_add_folio(lruvec, folio); + lruvec_add_deactivated_folio(lruvec, folio); __count_vm_events(PGDEACTIVATE, nr_pages); count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_pages); _ Patches currently in -mm which might be from chenridong@xiaomi.com are mm-memcontrol-drop-unused-vmscan-tracepoint-include.patch mm-vmscan-move-vmscan-tracepoints-to-a-local-header.patch mm-vmscan-move-struct-scan_control-to-a-dedicated-header.patch mm-vmscan-pass-scan_control-to-the-vmscan-tracepoints.patch mm-mglru-add-tracepoint-for-scan_folios.patch mm-mglru-add-lruvec_gen_size-helper.patch mm-mglru-add-tracepoint-for-aging.patch mm-mglru-pass-a-seq-range-to-lru_gen_add_folio.patch mm-mglru-deactivate-anon-folios-into-the-oldest-generation.patch