From: Baoquan He <baoquan.he@linux.dev>
To: linux-mm@kvack.org
Cc: akpm@linux-foundation.org, kasong@tencent.com, baohua@kernel.org,
qi.zheng@linux.dev, shakeel.butt@linux.dev, baoquan.he@linux.dev,
baolin.wang@linux.alibaba.com, hannes@cmpxchg.org,
mhocko@kernel.org, Baoquan He <hebaoquan@kylinos.cn>
Subject: [PATCH 3/4] mm/mglru: split reset_ctrl_pos() into _update and _reset
Date: Wed, 2 Dec 2026 14:17:17 +0800 [thread overview]
Message-ID: <20261202061720.284064-4-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261202061720.284064-1-baoquan.he@linux.dev>
From: Baoquan He <hebaoquan@kylinos.cn>
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.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
mm/vmscan.c | 76 +++++++++++++++++++++++++++++++++++------------------
1 file changed, 50 insertions(+), 26 deletions(-)
diff --git a/mm/vmscan.c b/mm/vmscan.c
index ab5ba9997bd5..9211ffcdcea9 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -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 */
--
2.52.0
next prev parent reply other threads:[~2026-12-02 6:17 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 6:17 [PATCH 0/4] mm/mglru: cleanup series and a small correctness Baoquan He
2026-12-02 6:17 ` [PATCH 1/4] mm/mglru: use a common helper to check evictable type Baoquan He
2026-12-02 6:17 ` [PATCH 2/4] mm/mglru: rename positive_ctrl_err() to should_reclaim() Baoquan He
2026-12-02 6:17 ` Baoquan He [this message]
2026-12-02 6:20 ` [PATCH 4/4] mm/mglru: stop counting isolation races as PGSCAN_SKIP in scan_folios() Baoquan He
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=20261202061720.284064-4-baoquan.he@linux.dev \
--to=baoquan.he@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=hannes@cmpxchg.org \
--cc=hebaoquan@kylinos.cn \
--cc=kasong@tencent.com \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=qi.zheng@linux.dev \
--cc=shakeel.butt@linux.dev \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox