mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] mm/mglru: fix a few reclaim issues
@ 2026-11-29 14:37 Barry Song (Xiaomi)
  2026-11-29 14:37 ` [PATCH 1/3] mm/mglru: count all generations as evictable Barry Song (Xiaomi)
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Barry Song (Xiaomi) @ 2026-11-29 14:37 UTC (permalink / raw)
  To: akpm
  Cc: hannes, mhocko, qi.zheng, shakeel.butt, kasong, axelrasmussen,
	yuanchu, weixugc, baoquan.he, baolin.wang, mason, david,
	linux-mm, linux-kernel, ridong.chen, yuzhao, Barry Song (Xiaomi)

This series contains three MGLRU fixes originally proposed by Chris
Mason and passed on to us by David Hildenbrand.

The issues addressed are:

1. Reparented folios becoming unreclaimable due to generation accounting
   falling outside the parent's [min_seq, max_seq] range.
2. A potential deadlock between reclaim and compaction when isolating
   folios.
3. Missing vmpressure notifications for global reclaim on cgroup v1.

Kairui and I have reviewed the fixes and made minor adjustments where
necessary.

The series is based on mm-new.

Chris Mason (3):
  mm/mglru: count all generations as evictable
  mm/mglru: test PG_lru before taking a folio reference
  mm/mglru: report root reclaim to memory.pressure_level

 mm/vmscan.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

-- 
2.39.3 (Apple Git-146)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] mm/mglru: count all generations as evictable
  2026-11-29 14:37 [PATCH 0/3] mm/mglru: fix a few reclaim issues Barry Song (Xiaomi)
@ 2026-11-29 14:37 ` Barry Song (Xiaomi)
  2026-11-29 14:37 ` [PATCH 2/3] mm/mglru: test PG_lru before taking a folio reference Barry Song (Xiaomi)
  2026-11-29 14:37 ` [PATCH 3/3] mm/mglru: report root reclaim to memory.pressure_level Barry Song (Xiaomi)
  2 siblings, 0 replies; 4+ messages in thread
From: Barry Song (Xiaomi) @ 2026-11-29 14:37 UTC (permalink / raw)
  To: akpm
  Cc: hannes, mhocko, qi.zheng, shakeel.butt, kasong, axelrasmussen,
	yuanchu, weixugc, baoquan.he, baolin.wang, mason, david,
	linux-mm, linux-kernel, ridong.chen, yuzhao, Barry Song

From: Chris Mason <mason@kernel.org>

With MGLRU enabled, clean page cache that was reparented from a removed
memory cgroup can stay unreclaimable for as long as the parent has no
other evictable folios on the same node.

try_to_shrink_lruvec() derives its scan count from
lruvec_evictable_size(), and with a count of 0 it runs neither eviction
nor aging. lruvec_evictable_size() adds up nr_pages[] only for the
generations from min_seq to max_seq. That sum is complete only if the
count of a generation is 0 by the time min_seq advances past it.

But try_to_inc_min_seq() ignores the counts: it advances min_seq past a
generation as soon as the lists of that generation are empty. A folio
that folio_update_gen() promoted is counted in its new generation, but
stays on the list of the generation that it came from. Normally min_seq
stops at that list, before it reaches the new generation.

Reparenting moves each list and each count to the same index in the
parent, where that index can stand for a different age:

    index (seq % MAX_NR_GENS)    0  1  2  3
    seq in the child             0  1  2  3
    seq in the parent            4  5  6  3

    folio F:  on folios[0], counted in nr_pages[3]
    child:    folios[0] holds min_seq at 0
    parent:   folios[3] is empty, so min_seq goes from 3 to 4
    sum:      nr_pages[0] + nr_pages[1] + nr_pages[2] = 0, without F

The fix used here is to add up nr_pages[] of all MAX_NR_GENS generations
in lruvec_evictable_size().

Fixes: 163bc3d68c9f ("mm/mglru: restructure the reclaim loop")
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
[Barry: Rebased onto mm-new and added comments]
Signed-off-by: Barry Song (Xiaomi) <baohua@kernel.org>
---
 mm/vmscan.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/mm/vmscan.c b/mm/vmscan.c
index cbed395955d4..60c416819c57 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -4272,8 +4272,6 @@ static void lruvec_evictable_size(struct lruvec *lruvec, struct scan_control *sc
 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
 	int nid = lruvec_pgdat(lruvec)->node_id;
-	DEFINE_MAX_SEQ(lruvec);
-	DEFINE_MIN_SEQ(lruvec);
 
 	nr[LRU_GEN_ANON] = 0;
 	nr[LRU_GEN_FILE] = 0;
@@ -4281,7 +4279,12 @@ static void lruvec_evictable_size(struct lruvec *lruvec, struct scan_control *sc
 	for_each_evictable_type(type, swappiness) {
 		if (type == LRU_GEN_ANON && !can_reclaim_anon_pages(memcg, nid, sc))
 			continue;
-		for (seq = min_seq[type]; seq <= max_seq; seq++)
+		/*
+		 * Reparenting can leave folios accounted for in
+		 * generations outside [min_seq, max_seq]. Count
+		 * them as well to ensure reclaim makes progress.
+		 */
+		for (seq = 0; seq < MAX_NR_GENS; seq++)
 			nr[type] += lruvec_gen_size(lrugen, type, seq, max_zone);
 	}
 }
-- 
2.39.3 (Apple Git-146)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] mm/mglru: test PG_lru before taking a folio reference
  2026-11-29 14:37 [PATCH 0/3] mm/mglru: fix a few reclaim issues Barry Song (Xiaomi)
  2026-11-29 14:37 ` [PATCH 1/3] mm/mglru: count all generations as evictable Barry Song (Xiaomi)
@ 2026-11-29 14:37 ` Barry Song (Xiaomi)
  2026-11-29 14:37 ` [PATCH 3/3] mm/mglru: report root reclaim to memory.pressure_level Barry Song (Xiaomi)
  2 siblings, 0 replies; 4+ messages in thread
From: Barry Song (Xiaomi) @ 2026-11-29 14:37 UTC (permalink / raw)
  To: akpm
  Cc: hannes, mhocko, qi.zheng, shakeel.butt, kasong, axelrasmussen,
	yuanchu, weixugc, baoquan.he, baolin.wang, mason, david,
	linux-mm, linux-kernel, ridong.chen, yuzhao, stable, Barry Song

From: Chris Mason <mason@kernel.org>

With MGLRU, reclaim that races with compaction for a folio can deadlock
on the lruvec->lru_lock that it already holds.

isolate_folio() runs under the lru_lock. It takes a reference, and drops
it again if another isolator has already cleared PG_lru. That
folio_put() must not be the last one, since the free path takes the
lru_lock if PG_lru is set. But compaction can release the lru_lock while
the folio is still on the list with PG_lru clear. If the folio is
truncated or unmapped meanwhile, the two isolators hold the only
references:

    isolate_migratepages_block()  evict_folios()
    ============================  ==============
    folio_get_nontail_page()
    folio_test_clear_lru()
    goto isolate_abort
    lruvec_unlock_irqrestore()
                                  lruvec_lock_irq()
                                  isolate_folio()
                                    folio_try_get()
                                    folio_test_clear_lru() fails
    folio_set_lru()
    folio_put()
                                    folio_put()
                                      __page_cache_release()
                                        folio_lruvec_lock_irqsave()

The fix used here is to leave a folio alone if PG_lru is already clear,
before a reference is taken, as isolate_lru_folios() does. An isolator
that clears PG_lru after that test has to take the lru_lock before it
drops its own reference.

Fixes: ac35a4902374 ("mm: multi-gen LRU: minimal implementation")
Cc: stable@vger.kernel.org
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
[Kairui: Improved the comment]
Signed-off-by: Kairui Song <kasong@tencent.com>
Signed-off-by: Barry Song (Xiaomi) <baohua@kernel.org>
---
 mm/vmscan.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/mm/vmscan.c b/mm/vmscan.c
index 60c416819c57..5fb3055a4302 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -4802,6 +4802,15 @@ static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct sca
 {
 	bool success;
 
+	/*
+	 * If PG_lru is set, the folio_put() below can't be the last one and
+	 * free the folio: whoever clears the flag keeps its reference until
+	 * it sets the flag again or takes the folio off the list, and it has
+	 * to take the lru_lock we hold before it can do either.
+	 */
+	if (!folio_test_lru(folio))
+		return false;
+
 	/* raced with release_pages() */
 	if (!folio_try_get(folio))
 		return false;
-- 
2.39.3 (Apple Git-146)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] mm/mglru: report root reclaim to memory.pressure_level
  2026-11-29 14:37 [PATCH 0/3] mm/mglru: fix a few reclaim issues Barry Song (Xiaomi)
  2026-11-29 14:37 ` [PATCH 1/3] mm/mglru: count all generations as evictable Barry Song (Xiaomi)
  2026-11-29 14:37 ` [PATCH 2/3] mm/mglru: test PG_lru before taking a folio reference Barry Song (Xiaomi)
@ 2026-11-29 14:37 ` Barry Song (Xiaomi)
  2 siblings, 0 replies; 4+ messages in thread
From: Barry Song (Xiaomi) @ 2026-11-29 14:37 UTC (permalink / raw)
  To: akpm
  Cc: hannes, mhocko, qi.zheng, shakeel.butt, kasong, axelrasmussen,
	yuanchu, weixugc, baoquan.he, baolin.wang, mason, david,
	linux-mm, linux-kernel, ridong.chen, yuzhao, Barry Song

From: Chris Mason <mason@kernel.org>

With MGLRU enabled, memory.pressure_level listeners on the cgroup v1
root can miss global reclaim: kswapd sends them no events, and direct
reclaim only "critical" ones.

On v1 the eventfds are only fed by vmpressure() calls with tree=true.
shrink_node() makes that call after walking the memcgs, but it hands
root reclaim to lru_gen_shrink_node() and returns first. The MGLRU walk
only calls vmpressure() from shrink_one(), with tree=false, which has no
consumer on v1. That leaves vmpressure_prio() in direct reclaim.

The fix used here is to make the same subtree report at the end of the
MGLRU walk of a node.

Listeners for "medium" and "critical" can miss events they got before,
since a vmpressure_prio() report is now averaged with the pages that the
walk freed in the same window.

Fixes: e4dde56cd208 ("mm: multi-gen LRU: per-node lru_gen_folio lists")
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
Signed-off-by: Barry Song (Xiaomi) <baohua@kernel.org>
---
 mm/vmscan.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/mm/vmscan.c b/mm/vmscan.c
index 5fb3055a4302..99cb209797b4 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -5511,6 +5511,7 @@ static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc
 static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *sc)
 {
 	struct blk_plug plug;
+	unsigned long scanned = sc->nr_scanned;
 	unsigned long reclaimed = sc->nr_reclaimed;
 
 	VM_WARN_ON_ONCE(!root_reclaim(sc));
@@ -5545,6 +5546,11 @@ static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *
 	clear_mm_walk();
 
 	blk_finish_plug(&plug);
+
+	/* Record the subtree's reclaim efficiency */
+	if (!sc->proactive)
+		vmpressure(sc->gfp_mask, sc->order, sc->target_mem_cgroup, true,
+			   sc->nr_scanned - scanned, sc->nr_reclaimed - reclaimed);
 done:
 	if (sc->nr_reclaimed > reclaimed)
 		kswapd_try_clear_hopeless(pgdat, sc->order, sc->reclaim_idx);
-- 
2.39.3 (Apple Git-146)


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-29 14:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-29 14:37 [PATCH 0/3] mm/mglru: fix a few reclaim issues Barry Song (Xiaomi)
2026-11-29 14:37 ` [PATCH 1/3] mm/mglru: count all generations as evictable Barry Song (Xiaomi)
2026-11-29 14:37 ` [PATCH 2/3] mm/mglru: test PG_lru before taking a folio reference Barry Song (Xiaomi)
2026-11-29 14:37 ` [PATCH 3/3] mm/mglru: report root reclaim to memory.pressure_level Barry Song (Xiaomi)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®