From: Ridong Chen 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 Signed-off-by: Ridong Chen --- 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 + static void lru_deactivate(struct lruvec *lruvec, struct folio *folio) { long nr_pages = folio_nr_pages(folio); @@ -535,7 +552,7 @@ static void lru_deactivate(struct lruvec *lruvec, struct folio *folio) 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); -- 2.34.1