Re: [PATCH v8 00/16] mm: mglru: fix swappiness behavior

From: Andrew Morton

Date: Fri Nov 13 2026 - 16:05:40 EST


On Sat, 14 Nov 2026 02:55:25 +0800 "Barry Song (Xiaomi)" <baohua@xxxxxxxxxx> wrote:

> Classic LRU respects swappiness, while MGLRU only weakly
> honors it when selecting the scan type. The fallback logic
> can easily override the bias established by
> get_type_to_scan(). As a result, swappiness has a much
> smaller overall impact when changing from 1 to 200, as
> shown by the data presented later in this cover letter.
>
> In this patchset, patches 1-8 from Kairui introduce a model that uses
> refaults to determine nr_to_scan[] for both file and anon in MGLRU.
> For each reclaim operation, we calculate how many file and anon
> folios to scan, similar to CLRU, rather than relying on
> get_type_to_scan() and fallback mechanisms to decide what to
> reclaim, which have proven rather unreliable in honoring swappiness.
> This model also allows scanning and reclaiming from the two youngest
> generations, including the youngest generation itself. This can help
> resolve OOM situations[1] and reduce the need for aging.
>
> ...
>

Thanks, I've updated mm.git's mm-new branch to this version. I expect
I'll advance the series into mm-unstable (hence linux-next) over the
weekend.

> v8:
> * Fixed Gaofei Lyu's name;
> * for debugfs proactive reclaim, use DEF_PRIORITY;
> * refine no-populatable aging (limit frequency, allow an aging
> after eviction returns 0 scanned)
> * refine "restrict eviction accounting to eligible zones" for
> potential premature OOM;
> * Collect Baolin's reviewed-by tag, thanks!

Here's how v8 altered mm.git:


mm/vmscan.c | 34 ++++++++++++++++++++++++++--------
1 file changed, 26 insertions(+), 8 deletions(-)

--- a/mm/vmscan.c~b
+++ a/mm/vmscan.c
@@ -4034,8 +4034,6 @@ static void try_to_inc_min_seq(struct lr

/* see the comment on lru_gen_folio */
if (swappiness && swappiness <= MAX_SWAPPINESS) {
- unsigned long seq = lrugen->max_seq - MIN_NR_GENS;
-
if (min_seq[LRU_GEN_ANON] > seq && min_seq[LRU_GEN_FILE] < seq)
min_seq[LRU_GEN_ANON] = seq;
else if (min_seq[LRU_GEN_FILE] > seq && min_seq[LRU_GEN_ANON] < seq)
@@ -4246,7 +4244,13 @@ static bool lruvec_is_reclaimable(struct
if (mem_cgroup_below_min(NULL, memcg))
return false;

- if (!lruvec_is_sizable(lruvec, sc))
+ /*
+ * As in __alloc_pages_may_oom(), avoid triggering OOM for low-zone
+ * allocations. Also, defer the LRU size check until reclaim reaches
+ * a very low priority to avoid premature OOM.
+ */
+ if (sc->priority <= 1 && sc->reclaim_idx >= ZONE_NORMAL &&
+ !lruvec_is_sizable(lruvec, sc))
return false;

gen = lru_gen_from_seq(evictable_min_seq(min_seq, swappiness));
@@ -5220,6 +5224,13 @@ static bool try_to_shrink_lruvec(struct
bool need_rotate = false, should_age = false;
int swappiness = get_swappiness(lruvec, sc);
struct mem_cgroup *memcg = lruvec_memcg(lruvec);
+ /*
+ * Avoid aging the lagging type under extreme swappiness,
+ * as reclaim of the unpreferred type is disabled until
+ * DEF_PRIORITY / 2.
+ */
+ bool allow_no_populatable_age = (sc->priority <= DEF_PRIORITY / 2) ||
+ (swappiness > MIN_SWAPPINESS + 1 && swappiness < MAX_SWAPPINESS);

/*
* Proactive reclaim initiated by userspace for anonymous memory only.
@@ -5238,8 +5249,9 @@ static bool try_to_shrink_lruvec(struct

while (nr[LRU_GEN_ANON] > 0 || nr[LRU_GEN_FILE] > 0) {
unsigned long orig_file, orig_anon;
- DEFINE_MAX_SEQ(lruvec);
+ unsigned long max_seq;
bool no_populatable;
+ long scanned;

if (mem_cgroup_below_min(sc->target_mem_cgroup, memcg)) {
need_rotate = true;
@@ -5248,8 +5260,7 @@ static bool try_to_shrink_lruvec(struct

orig_anon = nr[LRU_GEN_ANON];
orig_file = nr[LRU_GEN_FILE];
- if (!evict_folio_lists(nr, lruvec, sc, swappiness, MIN_LRU_BATCH))
- break;
+ scanned = evict_folio_lists(nr, lruvec, sc, swappiness, MIN_LRU_BATCH);

if (should_abort_scan(lruvec, sc))
break;
@@ -5261,14 +5272,20 @@ static bool try_to_shrink_lruvec(struct
if (root_reclaim(sc) && should_age)
break;

- no_populatable = (nr[LRU_GEN_ANON] && nr[LRU_GEN_ANON] == orig_anon) ||
- (nr[LRU_GEN_FILE] && nr[LRU_GEN_FILE] == orig_file);
+ /* Limit no_populatable aging to once per try_to_shrink_lruvec() */
+ no_populatable = allow_no_populatable_age &&
+ ((nr[LRU_GEN_ANON] && nr[LRU_GEN_ANON] == orig_anon) ||
+ (nr[LRU_GEN_FILE] && nr[LRU_GEN_FILE] == orig_file));
+ allow_no_populatable_age &= !no_populatable;
+ max_seq = READ_ONCE((lruvec)->lrugen.max_seq);
if (should_run_aging(lruvec, max_seq, sc, swappiness, no_populatable)) {
trace_mm_mglru_aging(lruvec, sc, swappiness);
if (try_to_inc_max_seq(lruvec, max_seq, swappiness, false))
need_rotate = true;
should_age = true;
}
+ if (!scanned)
+ break;

cond_resched();
}
@@ -5965,6 +5982,7 @@ static ssize_t lru_gen_seq_write(struct
.may_writepage = true,
.may_unmap = true,
.may_swap = true,
+ .priority = DEF_PRIORITY,
.reclaim_idx = MAX_NR_ZONES - 1,
.gfp_mask = GFP_KERNEL,
.proactive = true,
_