Skip to contentSkip to conversation

mm/mglru: cleanup series and a small correctness

PATCH In review Baoquan He 3 participants 13 messages Dec 2 – Dec 3

Subsystems (3)
lore
Share & tools
lore

Baoquan He

author reviewer
#0 lore raw

cover mm/mglru: cleanup series and a small correctness

Four small cleanups in the MGLRU reclaim path. Three of them are pure refactoring with no behaviour change; the fourth corrects a vmstat counter that was being fed the wrong thing.

Baoquan He (4):
mm/mglru: use a common helper to check evictable type mm/mglru: rename positive_ctrl_err() to should_reclaim() mm/mglru: split reset_ctrl_pos() into _update and _reset mm/mglru: stop counting isolation races as PGSCAN_SKIP in
scan_folios()

 mm/vmscan.c | 118 +++++++++++++++++++++++++++++++---------------------
 1 file changed, 70 insertions(+), 48 deletions(-)
Signature · 1 line
2.52.0
author reviewer
#1 lore raw

PATCH 1/4 mm/mglru: use a common helper to check evictable type

From: Baoquan He <[email protected]>

inc_min_seq() open-codes the evictable type check that for_each_evictable_type() already expresses through min_type() and max_type(). Use a shared is_type_evictable() helper so the two cannot drift apart, and rename the done label to advance to make it clear that a skipped rotation still advances min_seq.

No functional change.

---
 mm/vmscan.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
mm/vmscan.c +9 -8
index be89bc2528a9..257305fe1cce 100644
@@ -2785,6 +2785,11 @@ static bool should_clear_pmd_young(void)
 #define min_type(swappiness) (!(swappiness))
 #define max_type(swappiness) ((swappiness) < SWAPPINESS_ANON_ONLY)
 
+static inline bool is_type_evictable(int type, int swappiness)
+{
+	return type >= min_type(swappiness) && type <= max_type(swappiness);
+}
+
 #define evictable_min_seq(min_seq, swappiness)				\
 	min((min_seq)[min_type(swappiness)], (min_seq)[max_type(swappiness)])
 
@@ -3976,13 +3981,9 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
 	int new_gen, old_gen = lru_gen_from_seq(lrugen->min_seq[type]);
 	int target_gen = (old_gen + 1) % MAX_NR_GENS;
 
-	/* For file type, skip the check if swappiness is anon only */
-	if (type && (swappiness == SWAPPINESS_ANON_ONLY))
-		goto done;
-
-	/* For anon type, skip the check if swappiness is zero (file only) */
-	if (!type && !swappiness)
-		goto done;
+	/* A type that isn't being reclaimed needs no rotation */
+	if (!is_type_evictable(type, swappiness))
+		goto advance;
 
 	VM_WARN_ON_ONCE(get_nr_gens(lruvec, type) != MAX_NR_GENS);
 	VM_WARN_ON_ONCE(lru_gen_is_active(lruvec, old_gen) !=
@@ -4042,7 +4043,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
 		if (!remaining)
 			return false;
 	}
-done:
+advance:
 	reset_ctrl_pos(lruvec, type, true);
 	WRITE_ONCE(lrugen->min_seq[type], lrugen->min_seq[type] + 1);
 
Signature · 1 line
2.52.0
author reviewer
#7 lore raw

PATCH 2/4 mm/mglru: rename positive_ctrl_err() to should_reclaim()

In reply to Baoquan He

From: Baoquan He <[email protected]>

positive_ctrl_err() is named after control-theory jargon, which obscures what the function actually asks: whether the candidate position (pv) is worth reclaiming, i.e. it has a limited number of refaults or a lower refaulted/total than the current position (sp).

Rename it to should_reclaim() so the reclaim decision is self-evident at its call site.

---
 mm/vmscan.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
mm/vmscan.c +5 -4
index 257305fe1cce..ab5ba9997bd5 100644
@@ -3298,11 +3298,12 @@ static void reset_ctrl_pos(struct lruvec *lruvec, int type, bool carryover)
 	}
 }
 
-static bool positive_ctrl_err(struct ctrl_pos *sp, struct ctrl_pos *pv)
+static bool should_reclaim(struct ctrl_pos *sp, struct ctrl_pos *pv)
 {
 	/*
-	 * Return true if the PV has a limited number of refaults or a lower
-	 * refaulted/total than the SP.
+	 * Return true if the candidate (PV) is worth reclaiming: it has a
+	 * limited number of refaults, or a lower refaulted/total than the
+	 * current position (SP).
 	 */
 	return pv->refaulted < MIN_LRU_BATCH ||
 	       pv->refaulted * (sp->total + MIN_LRU_BATCH) * sp->gain <=
@@ -4944,7 +4945,7 @@ static int get_tier_idx(struct lruvec *lruvec, int type)
 	read_ctrl_pos(lruvec, type, LRU_TIER_MIN, LRU_TIER_MIN, 2, &sp);
 	for (tier = LRU_TIER_MIN + 1; tier <= LRU_TIER_MAX; tier++) {
 		read_ctrl_pos(lruvec, type, tier, tier, 3, &pv);
-		if (!positive_ctrl_err(&sp, &pv))
+		if (!should_reclaim(&sp, &pv))
 			break;
 	}
 
Signature · 1 line
2.52.0
author reviewer
#9 lore raw

PATCH 3/4 mm/mglru: split reset_ctrl_pos() into _update and _reset

In reply to Baoquan He

From: Baoquan He <[email protected]>

The single function reset_ctrl_pos() handled two opposite operations controlled by a boolean 'carryover' flag:

- carryover=true: fold retiring generation stats into the EMA, and optionally clear the slot (from inc_min_seq)
- carryover=false: clear the slot for a new generation (from inc_max_seq)

The boolean also controlled which seq to use (min_seq or max_seq+1) and whether to early-return. This made the logic unnecessarily twisted.

Split it into two self-explanatory functions:

- lru_gen_update_ctrl(): fold stats into EMA when retiring an old generation; also clears the single history slot when NR_HIST_GENS==1.

- lru_gen_reset_ctrl(): clear the slot for the upcoming new generation; no-op when NR_HIST_GENS==1 since _update already handled it.

No functional change.

---
 mm/vmscan.c | 76 +++++++++++++++++++++++++++++++++++------------------
 1 file changed, 50 insertions(+), 26 deletions(-)
mm/vmscan.c +50 -26
index ab5ba9997bd5..9211ffcdcea9 100644
@@ -3262,42 +3262,66 @@ static void __noipa read_ctrl_pos(struct lruvec *lruvec, int type, int tier_min,
 	}
 }
 
-static void reset_ctrl_pos(struct lruvec *lruvec, int type, bool carryover)
+/*
+ * Fold the retiring generation's controller statistics into the running
+ * exponential moving average. Called when evicting an old generation
+ * (from inc_min_seq and try_to_inc_min_seq).
+ *
+ * With NR_HIST_GENS==1, the single slot must also be cleared for reuse.
+ */
+static void lru_gen_update_ctrl(struct lruvec *lruvec, int type)
 {
-	int hist, tier;
+	int tier;
 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
-	bool clear = carryover ? NR_HIST_GENS == 1 : NR_HIST_GENS > 1;
-	unsigned long seq = carryover ? lrugen->min_seq[type] : lrugen->max_seq + 1;
+	int hist_idx = lru_hist_from_seq(lrugen->min_seq[type]);
 
 	lockdep_assert_held(&lruvec->lru_lock);
 
-	if (!carryover && !clear)
-		return;
-
-	hist = lru_hist_from_seq(seq);
-
 	for (tier = 0; tier < MAX_NR_TIERS; tier++) {
-		if (carryover) {
-			unsigned long sum;
+		unsigned long sum;
 
-			sum = lrugen->avg_refaulted[type][tier] +
-			      atomic_long_read(&lrugen->refaulted[hist][type][tier]);
-			WRITE_ONCE(lrugen->avg_refaulted[type][tier], sum / 2);
+		sum = lrugen->avg_refaulted[type][tier] +
+		      atomic_long_read(&lrugen->refaulted[hist_idx][type][tier]);
+		WRITE_ONCE(lrugen->avg_refaulted[type][tier], sum / 2);
 
-			sum = lrugen->avg_total[type][tier] +
-			      lrugen->protected[hist][type][tier] +
-			      atomic_long_read(&lrugen->evicted[hist][type][tier]);
-			WRITE_ONCE(lrugen->avg_total[type][tier], sum / 2);
-		}
+		sum = lrugen->avg_total[type][tier] +
+		      lrugen->protected[hist_idx][type][tier] +
+		      atomic_long_read(&lrugen->evicted[hist_idx][type][tier]);
+		WRITE_ONCE(lrugen->avg_total[type][tier], sum / 2);
 
-		if (clear) {
-			atomic_long_set(&lrugen->refaulted[hist][type][tier], 0);
-			atomic_long_set(&lrugen->evicted[hist][type][tier], 0);
-			WRITE_ONCE(lrugen->protected[hist][type][tier], 0);
+		if (NR_HIST_GENS == 1) {
+			atomic_long_set(&lrugen->refaulted[hist_idx][type][tier], 0);
+			atomic_long_set(&lrugen->evicted[hist_idx][type][tier], 0);
+			WRITE_ONCE(lrugen->protected[hist_idx][type][tier], 0);
 		}
 	}
 }
 
+/*
+ * Clear the controller statistics slot for the upcoming new generation.
+ * Called when opening a new generation (from inc_max_seq).
+ *
+ * With NR_HIST_GENS==1, there is nothing to do — the single slot was
+ * already cleared by lru_gen_update_ctrl().
+ */
+static void lru_gen_reset_ctrl(struct lruvec *lruvec, int type)
+{
+	int tier;
+	struct lru_gen_folio *lrugen = &lruvec->lrugen;
+	int hist_idx = lru_hist_from_seq(lrugen->max_seq + 1);
+
+	lockdep_assert_held(&lruvec->lru_lock);
+
+	if (NR_HIST_GENS == 1)
+		return;
+
+	for (tier = 0; tier < MAX_NR_TIERS; tier++) {
+		atomic_long_set(&lrugen->refaulted[hist_idx][type][tier], 0);
+		atomic_long_set(&lrugen->evicted[hist_idx][type][tier], 0);
+		WRITE_ONCE(lrugen->protected[hist_idx][type][tier], 0);
+	}
+}
+
 static bool should_reclaim(struct ctrl_pos *sp, struct ctrl_pos *pv)
 {
 	/*
@@ -4045,7 +4069,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
 			return false;
 	}
 advance:
-	reset_ctrl_pos(lruvec, type, true);
+	lru_gen_update_ctrl(lruvec, type);
 	WRITE_ONCE(lrugen->min_seq[type], lrugen->min_seq[type] + 1);
 
 	return true;
@@ -4111,7 +4135,7 @@ static void try_to_inc_min_seq(struct lruvec *lruvec, int swappiness)
 		if (min_seq[type] <= lrugen->min_seq[type])
 			continue;
 
-		reset_ctrl_pos(lruvec, type, true);
+		lru_gen_update_ctrl(lruvec, type);
 		WRITE_ONCE(lrugen->min_seq[type], min_seq[type]);
 	}
 }
@@ -4170,7 +4194,7 @@ static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swappiness
 	}
 
 	for (type = 0; type < ANON_AND_FILE; type++)
-		reset_ctrl_pos(lruvec, type, false);
+		lru_gen_reset_ctrl(lruvec, type);
 
 	WRITE_ONCE(lrugen->timestamps[next], jiffies);
 	/* make sure preceding modifications appear */
Signature · 1 line
2.52.0
author reviewer
#11 lore raw

PATCH 4/4 mm/mglru: stop counting isolation races as PGSCAN_SKIP in scan_folios()

In reply to Baoquan He

From: Baoquan He <[email protected]>

scan_folios() counted pages it failed to isolate because of a race (folio_try_get() or folio_test_clear_lru() failed) and reported them as PGSCAN_SKIP and nr_skipped.

PGSCAN_SKIP is for pages skipped because they sit in zones above the reclaim target. MGLRU only iterates zones at or below that target, so it never has ineligible-zone pages at all; the only thing that fed PGSCAN_SKIP here was isolation races, which misrepresents the counter.

Rename skipped_zone to isolate_failed and drop the PGSCAN_SKIP accounting. nr_skipped is passed as 0: the field is kept so the event still lines up with mm_vmscan_lru_isolate, and the races themselves are transient, the folios go back to the generation list and are retried.

---
 mm/vmscan.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
mm/vmscan.c +6 -10
index 9211ffcdcea9..9894221cbb6d 100644
@@ -4867,7 +4867,6 @@ static int scan_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
 	int sorted = 0;
 	int scanned = 0;
 	int isolated = 0;
-	int skipped = 0;
 	int gen, max_gen;
 	unsigned long seq;
 	unsigned long remaining = nr_to_scan;
@@ -4901,7 +4900,7 @@ static int scan_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
 	/* Scan eligible zones in descending order. */
 	for (zone_idx = sc->reclaim_idx; zone_idx >= 0; zone_idx--) {
 		LIST_HEAD(moved);
-		int skipped_zone = 0;
+		int isolate_failed = 0;
 		struct zone *zone = &pgdat->node_zones[zone_idx];
 		struct list_head *head = &lrugen->folios[gen][type][zone_idx];
 
@@ -4925,18 +4924,15 @@ static int scan_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
 				isolated += delta;
 			} else {
 				list_move(&folio->lru, &moved);
-				skipped_zone += delta;
+				isolate_failed += delta;
 			}
 
-			if (!--remaining || max(isolated, skipped_zone) >= MIN_LRU_BATCH)
+			if (!--remaining || max(isolated, isolate_failed) >= MIN_LRU_BATCH)
 				break;
 		}
 
-		if (skipped_zone) {
+		if (isolate_failed)
 			list_splice(&moved, head);
-			__count_zid_vm_events(PGSCAN_SKIP, zone_idx, skipped_zone);
-			skipped += skipped_zone;
-		}
 
 		if (!remaining || isolated >= MIN_LRU_BATCH)
 			break;
@@ -4947,10 +4943,10 @@ static int scan_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
 	mod_lruvec_state(lruvec, PGREFILL, sorted);
 	mod_lruvec_state(lruvec, PGSCAN_ANON + type, isolated);
 	trace_mm_vmscan_lru_isolate(sc, nr_to_scan,
-				    scanned, skipped, isolated,
+				    scanned, 0, isolated,
 				    type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON);
 	trace_mm_mglru_scan_folios(lruvec, sc, nr_to_scan, scanned, sorted,
-				   skipped, isolated, type, tier);
+				   0, isolated, type, tier);
 
 	*isolatedp = isolated;
 	return scanned;
Signature · 1 line
2.52.0
reviewer
#2 lore raw

Re: [PATCH 1/4] mm/mglru: use a common helper to check evictable type

In reply to Baoquan He
Quoted 21 lines · Baoquan He

From: Baoquan He <[email protected]>

inc_min_seq() open-codes the evictable type check that for_each_evictable_type() already expresses through min_type() and max_type(). Use a shared is_type_evictable() helper so the two cannot drift apart, and rename the done label to advance to make it clear that a skipped rotation still advances min_seq.

No functional change.

---
 mm/vmscan.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
mm/vmscan.c +0 -0
index be89bc2528a9..257305fe1cce 100644

mm/vmscan.c static bool should_clear_pmd_young(void) · line 2792

Quoted 8 lines
mm/vmscan.c +5 -0
@@ -2785,6 +2785,11 @@ static bool should_clear_pmd_young(void)
 #define min_type(swappiness) (!(swappiness))
 #define max_type(swappiness) ((swappiness) < SWAPPINESS_ANON_ONLY)
 
+static inline bool is_type_evictable(int type, int swappiness)
+{
+       return type >= min_type(swappiness) && type <= max_type(swappiness);
+}
+

To be honest, I find this quite hard to follow. Can't we just say something more explicit, like this?

is_type_unevictable:

((type == LRU_GEN_FILE) && (swappiness == SWAPPINESS_ANON_ONLY)) ||
((type == LRU_GEN_ANON) && !swappiness)

Thanks,
Barry

author reviewer
#3 lore raw

Re: [PATCH 1/4] mm/mglru: use a common helper to check evictable type

Quoted 25 lines · Barry Song
Quoted 24 lines · Baoquan He

From: Baoquan He <[email protected]>

inc_min_seq() open-codes the evictable type check that for_each_evictable_type() already expresses through min_type() and max_type(). Use a shared is_type_evictable() helper so the two cannot drift apart, and rename the done label to advance to make it clear that a skipped rotation still advances min_seq.

No functional change.

---
 mm/vmscan.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
mm/vmscan.c +0 -0
index be89bc2528a9..257305fe1cce 100644
@@ -2785,6 +2785,11 @@ static bool should_clear_pmd_young(void)
 #define min_type(swappiness) (!(swappiness))
 #define max_type(swappiness) ((swappiness) < SWAPPINESS_ANON_ONLY)
 
Quoted 8 lines

mm/vmscan.c static bool should_clear_pmd_young(void) · line 2792

Quoted 5 lines
mm/vmscan.c +5 -0
+static inline bool is_type_evictable(int type, int swappiness)
+{
+       return type >= min_type(swappiness) && type <= max_type(swappiness);
+}
+

To be honest, I find this quite hard to follow. Can't we just say something more explicit, like this?

LOL, I know people must complain about this. It's truly logically twisted so that mglru expert need think a while to make it clear, I like it a bit though, it's correct and neat. I will update it as you suggested. Thanks for careful reviewing, and sorry for the code, Barry.

Quoted 10 lines

is_type_unevictable:

((type == LRU_GEN_FILE) && (swappiness == SWAPPINESS_ANON_ONLY)) ||
((type == LRU_GEN_ANON) && !swappiness)

Thanks,
Barry

reviewer
#12 lore raw

Re: [PATCH 4/4] mm/mglru: stop counting isolation races as PGSCAN_SKIP in scan_folios()

In reply to Baoquan He
Quoted 12 lines · Baoquan He

From: Baoquan He <[email protected]>

scan_folios() counted pages it failed to isolate because of a race (folio_try_get() or folio_test_clear_lru() failed) and reported them as PGSCAN_SKIP and nr_skipped.

PGSCAN_SKIP is for pages skipped because they sit in zones above the reclaim target. MGLRU only iterates zones at or below that target, so it never has ineligible-zone pages at all; the only thing that fed PGSCAN_SKIP here was isolation races, which misrepresents the counter.

Yes, it doesn't seem particularly useful for MGLRU now, since we have separate lists for each zone, whereas CLRU uses (folio_zonenum(folio) > sc->reclaim_idx) to skip ineligible folios in a shared list.

Quoted 4 lines

Rename skipped_zone to isolate_failed and drop the PGSCAN_SKIP accounting. nr_skipped is passed as 0: the field is kept so the event still lines up with mm_vmscan_lru_isolate, and the races themselves are transient, the folios go back to the generation list and are retried.

[...]

mm/vmscan.c static int scan_folios(unsigned long nr_to_scan, struct lruvec *lruvec, · line 4946

Quoted 6 lines
mm/vmscan.c +1 -1
@@ -4947,10 +4943,10 @@ static int scan_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
        mod_lruvec_state(lruvec, PGREFILL, sorted);
        mod_lruvec_state(lruvec, PGSCAN_ANON + type, isolated);
        trace_mm_vmscan_lru_isolate(sc, nr_to_scan,
-                                   scanned, skipped, isolated,
+                                   scanned, 0, isolated,

Maybe we could use this argument to trace isolate_failed instead of always setting it to 0? The tracepoint is still in mm-new and hasn't reached mainline yet, so we should still have time to get your change in?

Thanks,
Barry

#8 lore raw

Re: [PATCH 2/4] mm/mglru: rename positive_ctrl_err() to should_reclaim()

In reply to Baoquan He
Quoted 10 lines · Baoquan He

From: Baoquan He <[email protected]>

positive_ctrl_err() is named after control-theory jargon, which obscures what the function actually asks: whether the candidate position (pv) is worth reclaiming, i.e. it has a limited number of refaults or a lower refaulted/total than the current position (sp).

Rename it to should_reclaim() so the reclaim decision is self-evident at its call site.

Yes, I agree we should definitely do something here. But simply renaming the function to should_reclaim() doesn't seem sufficient. Perhaps we could make things much clearer by doing something like this?

mm/vmscan.c +14 -12
index 443ee5d30531..65a0ae024524 100644
@@ -3483,7 +3483,7 @@ static bool iterate_mm_list_nowalk(struct lruvec *lruvec, unsigned long seq)
  * 1. The D term may discount the other two terms over time so that long-lived
  *    generations can resist stale information.
  */
-struct ctrl_pos {
+struct refault_stats {
 	unsigned long refaulted;
 	unsigned long total;
 	int gain;
@@ -3492,8 +3492,8 @@ struct ctrl_pos {
 /*
  * __noipa works around gcc-16 warning for uninitialized use of pos->refaulted
  */
-static void __noipa read_ctrl_pos(struct lruvec *lruvec, int type, int tier_min,
-				  int tier_max, int gain, struct ctrl_pos *pos)
+static void __noipa read_refault_stats(struct lruvec *lruvec, int type, int tier_min,
+				  int tier_max, int gain, struct refault_stats *pos)
 {
 	int i;
 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
@@ -3511,7 +3511,7 @@ static void __noipa read_ctrl_pos(struct lruvec *lruvec, int type, int tier_min,
 	}
 }
 
-static void reset_ctrl_pos(struct lruvec *lruvec, int type, bool carryover)
+static void reset_refault_stats(struct lruvec *lruvec, int type, bool carryover)
 {
 	int hist, tier;
 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
@@ -3547,8 +3547,10 @@ static void reset_ctrl_pos(struct lruvec *lruvec, int type, bool carryover)
 	}
 }
 
-static bool positive_ctrl_err(struct ctrl_pos *sp, struct ctrl_pos *pv)
+static bool can_reclaim_tier(struct refault_stats *base, struct refault_stats *cand)
 {
+	struct refault_stats *sp = base, *pv = cand;
+
 	/*
 	 * Return true if the PV has a limited number of refaults or a lower
 	 * refaulted/total than the SP.
@@ -4288,7 +4290,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
 			return false;
 	}
 done:
-	reset_ctrl_pos(lruvec, type, true);
+	reset_refault_stats(lruvec, type, true);
 	WRITE_ONCE(lrugen->min_seq[type], lrugen->min_seq[type] + 1);
 
 	return true;
@@ -4354,7 +4356,7 @@ static void try_to_inc_min_seq(struct lruvec *lruvec, int swappiness)
 		if (min_seq[type] <= lrugen->min_seq[type])
 			continue;
 
-		reset_ctrl_pos(lruvec, type, true);
+		reset_refault_stats(lruvec, type, true);
 		WRITE_ONCE(lrugen->min_seq[type], min_seq[type]);
 	}
 }
@@ -4389,7 +4391,7 @@ static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swappiness
 	}
 
 	for (type = 0; type < ANON_AND_FILE; type++)
-		reset_ctrl_pos(lruvec, type, false);
+		reset_refault_stats(lruvec, type, false);
 
 	next = lru_gen_from_seq(lrugen->max_seq + 1);
 	WRITE_ONCE(lrugen->timestamps[next], jiffies);
@@ -5193,7 +5195,7 @@ static int scan_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
 static int get_tier_idx(struct lruvec *lruvec, int type)
 {
 	int tier;
-	struct ctrl_pos sp, pv;
+	struct refault_stats base, cand;
 
 	/*
 	 * To leave a margin for fluctuations, use a larger gain factor (1:2).
@@ -5202,9 +5204,9 @@ static int get_tier_idx(struct lruvec *lruvec, int type)
 	 * protects the tiers above it.
 	 */
 	for (tier = LRU_TIER_MIN + 1; tier <= LRU_TIER_MAX; tier++) {
-		read_ctrl_pos(lruvec, type, LRU_TIER_MIN, tier - 1, 1, &sp);
-		read_ctrl_pos(lruvec, type, tier, tier, 2, &pv);
-		if (!positive_ctrl_err(&sp, &pv))
+		read_refault_stats(lruvec, type, LRU_TIER_MIN, tier - 1, 1, &base);
+		read_refault_stats(lruvec, type, tier, tier, 2, &cand);
+		if (!can_reclaim_tier(&base, &cand))
 			break;
 	}
 
reviewer
#10 lore raw

Re: [PATCH 3/4] mm/mglru: split reset_ctrl_pos() into _update and _reset

In reply to Baoquan He
Quoted 16 lines · Baoquan He

From: Baoquan He <[email protected]>

The single function reset_ctrl_pos() handled two opposite operations controlled by a boolean 'carryover' flag:

- carryover=true: fold retiring generation stats into the EMA, and optionally clear the slot (from inc_min_seq)
- carryover=false: clear the slot for a new generation (from inc_max_seq)

The boolean also controlled which seq to use (min_seq or max_seq+1) and whether to early-return. This made the logic unnecessarily twisted.

Split it into two self-explanatory functions:

Quoted 8 lines

- lru_gen_update_ctrl(): fold stats into EMA when retiring an old generation; also clears the single history slot when NR_HIST_GENS==1.

- lru_gen_reset_ctrl(): clear the slot for the upcoming new generation; no-op when NR_HIST_GENS==1 since _update already handled it.

If we revise patch 2 along the lines of:

https://lore.kernel.org/linux-mm/[email protected]/

we would end up with the following names here:

update_refault_stats()
reset_refault_stats()

Let's try to hide all the control-theory details inside the helpers. LRU callers shouldn't need to know anything about "ctrl". The interfaces should expose only LRU-related semantics.

Thanks,
Barry

maintainer
#4 lore raw

Re: [PATCH 1/4] mm/mglru: use a common helper to check evictable type

In reply to Baoquan He
Quoted 1 line · On Wed, 2 Dec 2026 16:43:19 +0800 Baoquan He <[email protected]> wrote:

I will update it as you suggested.

Whimper. mm.git presently has 56 patches which mention mglru in their changelogs. Can I ask that we defer new changes and focus on what we have now?

reviewer
#5 lore raw

Re: [PATCH 1/4] mm/mglru: use a common helper to check evictable type

Quoted 8 lines · Andrew Morton
Quoted 1 line · On Wed, 2 Dec 2026 16:43:19 +0800 Baoquan He <[email protected]> wrote:

I will update it as you suggested.

Whimper. mm.git presently has 56 patches which mention mglru in their changelogs. Can I ask that we defer new changes and focus on what we have now?

Sure. Thanks for all your hard work, Andrew. People have been contributing to MGLRU with incredible enthusiasm lately.

Let's defer the other cleanups for now.

I guess revised versions of these two fixes [1, 2] could still go in, as they are both very minor and entirely independent of the other changes.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/

Thanks,
Barry

author reviewer
#6 lore raw

Re: [PATCH 1/4] mm/mglru: use a common helper to check evictable type

In reply to Andrew Morton
Quoted 7 lines · Andrew Morton
Quoted 1 line · On Wed, 2 Dec 2026 16:43:19 +0800 Baoquan He <[email protected]> wrote:

I will update it as you suggested.

Whimper. mm.git presently has 56 patches which mention mglru in their changelogs. Can I ask that we defer new changes and focus on what we have now?

No problem, Andrew. I will hold this series until 7.5 window closes, then post v2.

Sorry, I haven't been following mglru closely recently. I made these mglru patches in August, so organized them and sent out while building and testing on other tasks were running this week. I will finish discussion with Barry about his reviewing suggestion since he has spent effort to review.

Thanks
Baoquan