From: Ridong Chen <ridong.chen@linux.dev>
To: Barry Song <baohua@kernel.org>
Cc: David Hildenbrand <david@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Kairui Song <kasong@tencent.com>,
Johannes Weiner <hannes@cmpxchg.org>,
"Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>, Michal Hocko <mhocko@suse.com>,
Qi Zheng <qi.zheng@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>,
Axel Rasmussen <axelrasmussen@google.com>,
Yuanchu Xie <yuanchu@google.com>, Wei Xu <weixugc@google.com>,
Baoquan He <baoquan.he@linux.dev>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Yu Zhao <yuzhao@google.com>,
linux-mm@kvack.org, Ridong Chen <chenridong@xiaomi.com>
Subject: Re: [PATCH v3 2/2] mm/mglru: deactivate anon folios into the oldest generation
Date: Thu, 26 Nov 2026 10:18:48 +0800 [thread overview]
Message-ID: <635682b8-6e6b-4f0f-a3aa-48c458960b19@linux.dev> (raw)
In-Reply-To: <CAGsJ_4wScDHmPin2H+Va7SFZ1zwa4XcYMghkreMMyO+RK5W4gw@mail.gmail.com>
On 11/26/2026 8:02 AM, Barry Song wrote:
> On Wed, Nov 25, 2026 at 5:41 PM Ridong Chen <ridong.chen@linux.dev> wrote:
>>
>> From: Ridong Chen <chenridong@xiaomi.com>
>>
>> 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 are placed into the
>> oldest generation with their refs cleared, except the ones that have
>> PG_reclaim set and are dirty or under writeback, which 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, and after it, B is moved to min_seq.
>> Three runs on mm-new:
>>
>> A swapped out B (MADV_COLD) swapped out
>> before 16384 pages 63 ~ 150 pages
>> after 58 ~ 78 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.
>>
>> Fixes: 081488051d28 ("mm/mglru: fix underprotected page cache")
>> Suggested-by: Barry Song <baohua@kernel.org>
>> Signed-off-by: Ridong Chen <chenridong@xiaomi.com>
>
> With a few minor nits.
>
> Reviewed-by: Barry Song <baohua@kernel.org>
>
>> ---
>> mm/folio.c | 19 ++++++++++++++++++-
>> 1 file changed, 18 insertions(+), 1 deletion(-)
>>
>> diff --git a/mm/folio.c b/mm/folio.c
>> index 7521680874c5..0220419612ff 100644
>> --- a/mm/folio.c
>> +++ b/mm/folio.c
>> @@ -525,6 +525,23 @@ static void lru_deactivate_file(struct lruvec *lruvec, struct folio *folio)
>> }
>> }
>>
>> +#ifdef CONFIG_LRU_GEN
>> +/* with MGLRU, add the folio to the head of the oldest generation */
>> +static 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 void lruvec_add_deactivated_folio(struct lruvec *lruvec, struct folio *folio)
>> +{
>> + lruvec_add_folio(lruvec, folio);
>> +}
>> +#endif
>
> I'm not sure whether compilers will inline them, but explicitly
> marking them as inline should be fine?
>
Sure, will mark them inline in v4. Thanks for the review!
--
Best regards
Ridong
prev parent reply other threads:[~2026-11-26 2:19 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 9:40 [PATCH v3 0/2] mm/mglru: deactivate anon folios into the oldest generation Ridong Chen
2026-11-25 9:40 ` [PATCH v3 1/2] mm/mglru: pass a seq range to lru_gen_add_folio() Ridong Chen
2026-11-25 23:57 ` Barry Song
2026-11-26 2:16 ` Ridong Chen
2026-11-26 12:47 ` Barry Song
2026-11-27 1:51 ` Ridong Chen
2026-11-25 9:40 ` [PATCH v3 2/2] mm/mglru: deactivate anon folios into the oldest generation Ridong Chen
2026-11-26 0:02 ` Barry Song
2026-11-26 2:18 ` Ridong Chen [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=635682b8-6e6b-4f0f-a3aa-48c458960b19@linux.dev \
--to=ridong.chen@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=axelrasmussen@google.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=baoquan.he@linux.dev \
--cc=chenridong@xiaomi.com \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=kasong@tencent.com \
--cc=liam@infradead.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
--cc=qi.zheng@linux.dev \
--cc=rppt@kernel.org \
--cc=shakeel.butt@linux.dev \
--cc=vbabka@kernel.org \
--cc=weixugc@google.com \
--cc=yuanchu@google.com \
--cc=yuzhao@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.