mm/mglru: fix using folio_mark_accessed to replace folio_set_active
Subsystems (5)
Barry Song (Xiaomi)
reviewerPATCH 1/1 mm/mglru: fix using folio_mark_accessed to replace folio_set_active
Commit 6cbdd9726fb5 ("mm/mglru: use folio_mark_accessed to replace folio_set_active") attempted to avoid over-protecting in-fault read-ahead file folios by placing them in the second-oldest generation rather than the young generations.
However, it repurposes PG_referenced on in-fault read-ahead file folios for this purpose, potentially coupling their handling with that of regular file accesses through syscalls, which also use PG_referenced.
This could regress some workloads, as reported by Kairui Song[1] and Ehab Ababneh[2]. Many thanks to both for reporting these issues!
The long-term fix is Kairui's MGLRU-FG, but in the meantime, we also need a quick fix that can be backported to stable.
This patch reverts the original commit's abuse of PG_referenced while preserving its goal of avoiding over-protection of in-fault read-ahead file folios by adjusting only lru_gen_folio_seq().
Specifically, in-fault read-ahead file folios with PG_active set but PG_workingset clear are placed in the second-oldest generation (the third-youngest generation). Activated file folios and refaulted workingset file folios remain in the youngest generations.
Kernel build tests in a memcg show that this approach not only preserves the original patch's benefits but also further improves performance.
w/o fix:
real 1m49.094s user 25m31.484s sys 3m40.959s
pswpin: 662112
pswpout: 2504415
pgpgin: 4424336
pgpgout: 10355273refault_file: 359626refault_anon: 670724
w/ fix:
real 1m48.913s user 25m31.639s sys 3m30.875s
pswpin: 689815
pswpout: 2689894
pgpgin: 4422659
pgpgout: 11043464refault_file: 326566refault_anon: 697138
Revert original patch:
real 1m50.044s user 25m37.137s sys 4m4.493s
pswpin: 1349087
pswpout: 4317933
pgpgin: 7083597
pgpgout: 17602735refault_file: 337676refault_anon: 1368542
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
---
* An earlier discussion can be found here:
https://lore.kernel.org/linux-mm/[email protected]/
Thanks for Kairui's comment on it.
include/linux/mm_inline.h | 5 +++-- mm/folio.c | 16 +++------------- mm/vmscan.c | 9 ++------- mm/workingset.c | 10 ++++------ 4 files changed, 12 insertions(+), 28 deletions(-)
include/linux/mm_inline.h +3 -2
@@ -319,7 +319,8 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,* |<---------------------------- MAX_NR_GENS ---------------------------->| */ if (folio_test_active (folio)) - gen =MIN_NR_GENS -folio_test_workingset (folio); + gen =MIN_NR_GENS -folio_test_workingset (folio) + + (type ? !folio_test_workingset (folio) :0 );else if (reclaiming) gen =MAX_NR_GENS ; else if ((!folio_is_file_lru(folio) && !folio_test_swapcache(folio)) ||
@@ -327,7 +328,7 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, (folio_test_dirty(folio) || folio_test_writeback(folio)))) gen =MIN_NR_GENS ; else - gen =MAX_NR_GENS - (folio_test_workingset (folio) ||folio_test_referenced (folio)); + gen =MAX_NR_GENS -folio_test_workingset (folio);return max (READ_ONCE (lrugen->max_seq) - gen +1 ,READ_ONCE (lrugen->min_seq[type])); }
mm/folio.c +3 -13
@@ -474,20 +474,10 @@ void folio_add_lru(struct folio *folio) folio_test_unevictable(folio), folio);VM_BUG_ON_FOLIO (folio_test_lru (folio), folio); -/* -* For refaulted workingset folios, set PG_active so they -* can be added to active generations. -* For prefaulted file folios, folio_mark_accessed() sets -* PG_referenced so lru_gen_folio_seq() places them into -* the second oldest generation. -*/ +/* see the comment in lru_gen_folio_seq() */ if (lru_gen_enabled () && !folio_test_unevictable (folio) && -lru_gen_in_fault () && !(current->flags &PF_MEMALLOC )) { -if (folio_test_workingset (folio)) -folio_set_active (folio); -else if (!folio_test_referenced (folio)) -folio_mark_accessed (folio); - } +lru_gen_in_fault () && !(current->flags &PF_MEMALLOC )) +folio_set_active (folio);folio_batch_add_and_move (folio, lru_add); }
mm/vmscan.c +2 -7
@@ -952,13 +952,8 @@ static bool lru_gen_set_refs(struct folio *folio, const vma_flags_t *vma_flags) return false; } -/* Promote on second access */ -if (folio_lru_refs (folio) >1 ) { -folio_set_workingset (folio); -folio_set_lru_refs (folio,0 ); - }else { -folio_mark_accessed (folio); - } +folio_set_workingset (folio); +folio_set_lru_refs (folio,0 );return true; }#else
mm/workingset.c +4 -6
@@ -319,13 +319,11 @@ static void lru_gen_refault(struct folio *folio, void *shadow)atomic_long_add (delta, &lrugen->refaulted[hist][type][tier]); +/* see folio_add_lru() where folio_set_active() will be called */ +if (lru_gen_in_fault ()) +mod_lruvec_state (lruvec,WORKINGSET_ACTIVATE_BASE + type, delta); +if (workingset) { -/* -* see folio_add_lru(), where folio_set_active() is -* called for workingset folios -*/ -if (lru_gen_in_fault ()) -mod_lruvec_state (lruvec,WORKINGSET_ACTIVATE_BASE + type, delta);folio_set_workingset (folio);mod_lruvec_state (lruvec,WORKINGSET_RESTORE_BASE + type, delta); }else
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] mm/mglru: lru_gen_folio_seq() demotes non-workingset active file folios - [Medium] mm/mglru…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] mm/mglru: lru_gen_folio_seq() demotes non-workingset active file folios
- [Medium] mm/mglru: lru_gen_refault() miscounts WORKINGSET_ACTIVATE_FILE --
commit fa50b3cc1b1d64cb457afbbf37dc545c6d4e06f1
Author: Barry Song (Xiaomi) <[email protected]>
mm/mglru: fix using folio_mark_accessed to replace folio_set_active
This commit reverts the use of PG_referenced on in-fault readahead file folios from commit 6cbdd9726fb5 ("mm/mglru: use folio_mark_accessed to replace folio_set_active"), and instead adjusts lru_gen_folio_seq() to place file folios with PG_active set and PG_workingset clear into the third-youngest generation.
include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 323
Quoted 11 lines
include/linux/mm_inline.h +2
-1@@ -319,7 +319,8 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,* |<---------------------------- MAX_NR_GENS ---------------------------->| */ if (folio_test_active (folio)) - gen =MIN_NR_GENS -folio_test_workingset (folio); + gen =MIN_NR_GENS -folio_test_workingset (folio) + + (type ? !folio_test_workingset (folio) :0 );
[Severity: High]
Does assigning gen = 3 (lrugen->max_seq - 2) for all file folios (type == 1) that have PG_active set and PG_workingset clear break other MGLRU paths that set PG_active expecting the folio to land in an active generation?
First, in mm/vmscan.c, evict_folios() promotes folios rejected by shrink_folio_list() out of min_seq[type] by clearing LRU_REFS_FLAGS and setting PG_active:
mm/vmscan.c:evict_folios() {
...
folio_set_lru_refs(folio, 0);
if (lru_gen_folio_seq(lruvec, folio, false) == min_seq[type])
folio_set_active(folio);
}
move_folios_to_lru(&list, false);
}When 3 generations exist (lrugen->max_seq - lrugen->min_seq[1] == 2), max(lrugen->max_seq - 2, lrugen->min_seq[1]) equals min_seq[1]. A rejected !PG_workingset file folio (for example, a mapped file folio spared via FOLIOREF_KEEP on its first rmap scan in lru_gen_set_refs(), a DMA-pinned folio, or a folio failing try_to_unmap()) has its PTE accessed bit and PG_referenced cleared, and move_folios_to_lru() -> lru_gen_add_folio() puts it right back into min_seq[1] instead of promoting it. Even when 4 generations exist, evict_folios() clears PG_referenced and puts the folio in lrugen->max_seq - 2, so the next rmap scan in lru_gen_set_refs() sees !folio_test_referenced(folio) && !folio_test_workingset(folio) again and can never promote it to PG_workingset.
Second, in mm/vmscan.c, shrink_folio_list() sets PG_reclaim on dirty or writeback file folios and jumps to activate_locked, which sets PG_active:
mm/vmscan.c:shrink_folio_list() {
...
if (folio_test_dirty(folio)) {
if (folio_is_file_lru(folio)) {
node_stat_mod_folio(folio, NR_VMSCAN_IMMEDIATE,
nr_pages);
if (!folio_test_reclaim(folio))
folio_set_reclaim(folio);
goto activate_locked;
}
...
activate_locked:
...
folio_set_active(folio);
}Because lru_gen_folio_seq() checks folio_test_active(folio) before the PG_reclaim && (folio_test_dirty(folio) || folio_test_writeback(folio)) branch (which assigns gen = MIN_NR_GENS = 2), won't setting PG_active on a !PG_workingset dirty or writeback file folio downgrade it from gen = 2 (active) to gen = 3 (inactive, or min_seq[1] when 3 generations exist)?
Third, when a !PG_workingset file folio is explicitly activated via folio_activate() -> lru_activate(), or when it is isolated from the active generation lrugen->max_seq - 1 for migration/compaction via lru_gen_del_folio() (which sets BIT(PG_active) when !reclaiming && lru_gen_is_active(lruvec, gen)), re-adding it via lru_gen_add_folio() now places it into lrugen->max_seq - 2 (LRU_INACTIVE_FILE). Doesn't this demote activated and migrated active file folios into an inactive generation?
Quoted 12 lines
include/linux/mm_inline.h +1
-1else if (reclaiming) gen =MAX_NR_GENS ; else if ((!folio_is_file_lru(folio) && !folio_test_swapcache(folio)) ||@@ -327,7 +328,7 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, (folio_test_dirty(folio) || folio_test_writeback(folio)))) gen =MIN_NR_GENS ; else - gen =MAX_NR_GENS - (folio_test_workingset (folio) ||folio_test_referenced (folio)); + gen =MAX_NR_GENS -folio_test_workingset (folio);return max (READ_ONCE (lrugen->max_seq) - gen +1 ,READ_ONCE (lrugen->min_seq[type])); }
[ ... ]
Quoted 12 lines
mm/workingset.c +4
-0@@ -319,13 +319,11 @@ static void lru_gen_refault(struct folio *folio, void *shadow)atomic_long_add (delta, &lrugen->refaulted[hist][type][tier]); +/* see folio_add_lru() where folio_set_active() will be called */ +if (lru_gen_in_fault ()) +mod_lruvec_state (lruvec,WORKINGSET_ACTIVATE_BASE + type, delta); +
mm/workingset.c static void lru_gen_refault(struct folio *folio, void *shadow) · line 329
Quoted 10 lines
mm/workingset.c +0
-6if (workingset) { -/* -* see folio_add_lru(), where folio_set_active() is -* called for workingset folios -*/ -if (lru_gen_in_fault ()) -mod_lruvec_state (lruvec,WORKINGSET_ACTIVATE_BASE + type, delta);folio_set_workingset (folio);mod_lruvec_state (lruvec,WORKINGSET_RESTORE_BASE + type, delta); }else
[Severity: Medium]
When a file folio (type == 1) refaults in a page fault with workingset == false, should WORKINGSET_ACTIVATE_FILE still be incremented here?
With workingset == false, PG_workingset is not set. Although folio_add_lru() calls folio_set_active(folio), lru_gen_add_folio() clears PG_active and lru_gen_folio_seq() now maps a file folio with PG_active and !PG_workingset to gen = 3 (lrugen->max_seq - 2).
In include/linux/mm_inline.h, lru_gen_update_size() only accounts lrugen->max_seq and lrugen->max_seq - 1 as active:
include/linux/mm_inline.h:lru_gen_update_size() {
...
/* addition */
if (old_gen < 0) {
if (lru_gen_is_active(lruvec, new_gen))
lru += LRU_ACTIVE;
__update_lru_size(lruvec, lru, zone, delta);
return;
}
...
}When 3 or more generations exist, the folio is added to LRU_INACTIVE_FILE, so moving the mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta) call outside if (workingset) counts an activation for a file folio that is placed in an inactive generation.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Barry Song (Xiaomi)
reviewerQuoted 86 lines · On Sun, Oct 18, 2026 at 5:31 AM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] mm/mglru:lru_gen_folio_seq()demotes non-workingset active file folios
- [Medium] mm/mglru:lru_gen_refault()miscountsWORKINGSET_ACTIVATE_FILE--commit
fa50b3cc1b1d64cb457afbbf37dc545c6d4e06f1
Author: Barry Song (Xiaomi) <[email protected]>mm/mglru: fix using
folio_mark_accessedto replacefolio_set_activeThis commit reverts the use of
PG_referencedon in-fault readahead file folios from commit6cbdd9726fb5("mm/mglru: usefolio_mark_accessedto replacefolio_set_active"), and instead adjustslru_gen_folio_seq()to place file folios withPG_activeset andPG_workingsetclear into the third-youngest generation.include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 323
Quoted 11 lines
include/linux/mm_inline.h +2
-1@@ -319,7 +319,8 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,* |<---------------------------- MAX_NR_GENS ---------------------------->| */ if (folio_test_active(folio)) - gen = MIN_NR_GENS - folio_test_workingset(folio); + gen = MIN_NR_GENS - folio_test_workingset(folio) + + (type ? !folio_test_workingset(folio) : 0);[Severity: High]
Does assigning gen = 3 (lrugen->max_seq- 2) for all file folios (type == 1) that havePG_activeset andPG_workingsetclear break other MGLRU paths that setPG_activeexpecting the folio to land in an active generation?First, in
mm/vmscan.c,evict_folios()promotes folios rejected byshrink_folio_list()out of min_seq[type] by clearingLRU_REFS_FLAGSand settingPG_active:mm/vmscan.c:evict_folios() {
...
folio_set_lru_refs(folio, 0);
if (lru_gen_folio_seq(lruvec, folio, false)== min_seq[type])folio_set_active(folio);
}
move_folios_to_lru(&list, false);
}When 3 generations exist (
lrugen->max_seq- lrugen->min_seq[1] == 2),max(lrugen->max_seq - 2, lrugen->min_seq[1])equals min_seq[1]. A rejected !PG_workingset file folio (for example, a mapped file folio spared viaFOLIOREF_KEEPon its first rmap scan inlru_gen_set_refs(), a DMA-pinned folio, or a folio failingtry_to_unmap()) has its PTE accessed bit andPG_referencedcleared, andmove_folios_to_lru()->lru_gen_add_folio()puts it right back into min_seq[1] instead of promoting it. Even when 4 generations exist,evict_folios()clearsPG_referencedand puts the folio inlrugen->max_seq- 2, so the next rmap scan inlru_gen_set_refs()sees !folio_test_referenced(folio) && !folio_test_workingset(folio) again and can never promote it toPG_workingset.Second, in
mm/vmscan.c,shrink_folio_list()setsPG_reclaimon dirty or writeback file folios and jumps toactivate_locked, which setsPG_active:mm/vmscan.c:shrink_folio_list() {
...
if (folio_test_dirty(folio)) {
if (folio_is_file_lru(folio)) {node_stat_mod_folio(folio, NR_VMSCAN_IMMEDIATE, nr_pages);
if (!folio_test_reclaim(folio))folio_set_reclaim(folio);goto
activate_locked; }
...activate_locked:
...
folio_set_active(folio);
}Because
lru_gen_folio_seq()checksfolio_test_active(folio)before thePG_reclaim&& (folio_test_dirty(folio)||folio_test_writeback(folio)) branch (which assigns gen =MIN_NR_GENS= 2), won't settingPG_activeon a !PG_workingset dirty or writeback file folio downgrade it from gen = 2 (active) to gen = 3 (inactive, or min_seq[1] when 3 generations exist)?
Quoted 7 lines
Third, when a !PG_workingset file folio is explicitly activated via
folio_activate()->lru_activate(), or when it is isolated from the active generationlrugen->max_seq- 1 for migration/compaction vialru_gen_del_folio()(which setsBIT(PG_active)when !reclaiming &&lru_gen_is_active(lruvec, gen)), re-adding it vialru_gen_add_folio()now places it intolrugen->max_seq- 2 (LRU_INACTIVE_FILE). Doesn't this demote activated and migrated active file folios into an inactive generation?
Thanks. I guess the real problem is that we have no reliable way to identify whether a folio comes from in-fault readahead. I've tried various combinations of PG_ flags, but none of them works. So the only solution seems to be separate LRU-add callbacks for readahead file folios and other folios.
I've drafted a version below, but it's pretty ugly and feels like using a sledgehammer to crack a nut for a quick fix — simply not worth it. Maybe the best approach is to revert 6cbdd9726fb5 and leave the proper fix to Kairui's MGLRU-FG.
Unless there are objections, I'll send a v2 that simply reverts 6cbdd9726fb5.
Subject: [Don'tMerge] mm/mglru: fix using folio_mark_accessed to replace
folio_set_active
Commit 6cbdd9726fb5 ("mm/mglru: use folio_mark_accessed to replace folio_set_active") attempted to avoid over-protecting in-fault read-ahead file folios by placing them in the second-oldest generation rather than the young generations.
However, it repurposes PG_referenced on in-fault read-ahead file folios for this purpose, potentially coupling their handling with that of regular file accesses through syscalls, which also use PG_referenced.
This could regress some workloads, as reported by Kairui Song[1] and Ehab Ababneh[2]. Many thanks to both for reporting these issues!
The long-term fix is Kairui's MGLRU-FG, but in the meantime, we also need a quick fix that can be backported to stable.
This patch reverts the original commit's abuse of PG_referenced while preserving its goal of avoiding over-protection of in-fault read-ahead file folios.
This also restores the folio_set_workingset() behavior to what it was before commit 6cbdd9726fb5. We don't need folio_lru_refs(folio) > 1 to call folio_set_workingset(), since folio_add_lru() no longer sets PG_referenced.
We can't reliably identify fault-readahead files using a PG_ flag. v1 tried PG_active && !PG_workingset, but this doesn't work either, as some non-fault-readahead folios can have the same flags.
An alternative approach used by this patch is to give fault-readahead folios a separate code path (callback) for adding them to the LRU.
Specifically, in-fault read-ahead file folios are placed in the second-oldest generation (the third-youngest generation) through dedicated functions.
Activated file folios and refaulted file folios remain in the younger generations. Unlike the code before commit 6cbdd9726fb5, folio_set_active() is no longer always called in folio_add_lru() for the lru_gen_in_fault() case. Instead, it is called explicitly in the activation and refault paths.
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
--- include/linux/mm_inline.h | 24 ++++++++++++------- mm/folio.c | 50 ++++++++++++++++++++++++++++----------- mm/vmscan.c | 14 ++++------- mm/workingset.c | 11 ++++----- 4 files changed, 61 insertions(+), 38 deletions(-)
include/linux/mm_inline.h +15 -9
@@ -298,13 +298,17 @@ static inline void lru_gen_update_size(struct lruvec *lruvec, struct folio *foliVM_WARN_ON_ONCE (lru_gen_is_active (lruvec, old_gen) && !lru_gen_is_active (lruvec, new_gen)); } +#define LRU_GEN_ADD_RECLAIM BIT(0) +#define LRU_GEN_ADD_RA_FILE BIT(1)static inline unsigned long lru_gen_folio_seq (const struct lruvec *lruvec,const struct folio *folio, -bool reclaiming) +int types) {int gen;int type =folio_is_file_lru (folio);const struct lru_gen_folio *lrugen = &lruvec->lrugen; +bool reclaiming = !!(types &LRU_GEN_ADD_RECLAIM ); +bool ra_file = !!(types &LRU_GEN_ADD_RA_FILE );/* * +-----------------------------------+-----------------------------------+
@@ -318,7 +322,9 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,* |<---------- MIN_NR_GENS ---------->| | * |<---------------------------- MAX_NR_GENS ---------------------------->| */ -if (folio_test_active (folio)) +if (ra_file) + gen =MIN_NR_GENS +1 ; +else if (folio_test_active (folio)) gen =MIN_NR_GENS -folio_test_workingset (folio);else if (reclaiming) gen =MAX_NR_GENS ;
@@ -327,12 +333,12 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, (folio_test_dirty(folio) || folio_test_writeback(folio)))) gen =MIN_NR_GENS ; else - gen =MAX_NR_GENS - (folio_test_workingset (folio) ||folio_test_referenced (folio)); + gen =MAX_NR_GENS -folio_test_workingset (folio);return max (READ_ONCE (lrugen->max_seq) - gen +1 ,READ_ONCE (lrugen->min_seq[type])); } -static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming) +static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio,int types) {unsigned long seq;unsigned long flags;
@@ -346,7 +352,7 @@ static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio,if (folio_test_unevictable (folio) || !lrugen->enabled)return false; - seq =lru_gen_folio_seq (lruvec, folio, reclaiming); + seq =lru_gen_folio_seq (lruvec, folio, types); gen =lru_gen_from_seq (seq); flags = (gen +1UL ) <<LRU_GEN_PGOFF ;/* see the comment on MIN_NR_GENS about PG_active */
@@ -354,7 +360,7 @@ static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio,lru_gen_update_size (lruvec, folio,-1 , gen);/* for folio_rotate_reclaimable() */ -if (reclaiming) +if (types &LRU_GEN_ADD_RECLAIM )list_add_tail (&folio->lru, &lrugen->folios[gen][type][zone]);else list_add (&folio->lru, &lrugen->folios[gen][type][zone]);
@@ -415,7 +421,7 @@ static inline bool lru_gen_in_fault(void)return false; } -static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming) +static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio,int types) {return false; }
@@ -439,7 +445,7 @@ void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio)VM_WARN_ON_ONCE_FOLIO (!folio_matches_lruvec (folio, lruvec), folio); -if (lru_gen_add_folio (lruvec, folio, false)) +if (lru_gen_add_folio (lruvec, folio,0 ))return ; update_lru_size(lruvec, lru, folio_zonenum(folio),
@@ -455,7 +461,7 @@ void lruvec_add_folio_tail(struct lruvec *lruvec, struct folio *folio)VM_WARN_ON_ONCE_FOLIO (!folio_matches_lruvec (folio, lruvec), folio); -if (lru_gen_add_folio (lruvec, folio, true)) +if (lru_gen_add_folio (lruvec, folio,LRU_GEN_ADD_RECLAIM ))return ; update_lru_size(lruvec, lru, folio_zonenum(folio),
mm/folio.c +36 -14
91 lines not on this page · raw message
mm/vmscan.c +5 -9
@@ -952,13 +952,8 @@ static bool lru_gen_set_refs(struct folio *folio, const vma_flags_t *vma_flags) return false; } -/* Promote on second access */ -if (folio_lru_refs (folio) >1 ) { -folio_set_workingset (folio); -folio_set_lru_refs (folio,0 ); - }else { -folio_mark_accessed (folio); - } +folio_set_workingset (folio); +folio_set_lru_refs (folio,0 );return true; }#else
@@ -5527,10 +5522,11 @@ static bool fill_evictable(struct lruvec *lruvec)lruvec_del_folio (lruvec, folio);/* -* Borrow reclaiming=true to place inactive folios in +* Borrow LRU_GEN_ADD_RECLAIM to place inactive folios in * the older gens. */ - success =lru_gen_add_folio (lruvec, folio, !active); + success =lru_gen_add_folio (lruvec, folio, + active ?0 :LRU_GEN_ADD_RECLAIM );VM_WARN_ON_ONCE (!success);if (!--remaining)
mm/workingset.c +5 -6
@@ -319,13 +319,12 @@ static void lru_gen_refault(struct folio *folio, void *shadow)atomic_long_add (delta, &lrugen->refaulted[hist][type][tier]); +if (lru_gen_in_fault ()) { +folio_set_active (folio); +mod_lruvec_state (lruvec,WORKINGSET_ACTIVATE_BASE + type, delta); + } +if (workingset) { -/* -* see folio_add_lru(), where folio_set_active() is -* called for workingset folios -*/ -if (lru_gen_in_fault ()) -mod_lruvec_state (lruvec,WORKINGSET_ACTIVATE_BASE + type, delta);folio_set_workingset (folio);mod_lruvec_state (lruvec,WORKINGSET_RESTORE_BASE + type, delta); }else
Signature · 1 line
2.39.3 (Apple Git-146)
Barry Song
reviewerOn Sun, Oct 18, 2026 at 2:45 PM Barry Song (Xiaomi) <[email protected]> wrote:
[...]
Quoted 7 lines
Thanks. I guess the real problem is that we have no reliable way to identify whether a folio comes from in-fault readahead. I've tried various combinations of
PG_flags, but none of them works. So the only solution seems to be separate LRU-add callbacks for readahead file folios and other folios.
Quoted 5 lines
I've drafted a version below, but it's pretty ugly and feels like using a sledgehammer to crack a nut for a quick fix — simply not worth it. Maybe the best approach is to revert
6cbdd9726fb5and leave the proper fix to Kairui's MGLRU-FG.
Before reverting the patch (and regressing workloads such as kernel builds), I tried the following two options:
1. Remove folio_test_referenced() as PG_referenced might interfere with syscall.
include/linux/mm_inline.h +0 -0
@@ -327,7 +327,7 @@ static inline unsigned long
lru_gen_folio_seq(const struct lruvec *lruvec,
(folio_test_dirty(folio) || folio_test_writeback(folio))))
gen = MIN_NR_GENS;
else- gen = MAX_NR_GENS - (folio_test_workingset(folio) ||folio_test_referenced(folio));
+ gen = MAX_NR_GENS - folio_test_workingset(folio);
return max(READ_ONCE(lrugen->max_seq) - gen + 1,
READ_ONCE(lrugen->min_seq[type]));
}
2. changing to > 1 for folio_lru_refs() to reduce the syscall ra/non-ra interference:
include/linux/mm_inline.h +0 -0
@@ -327,7 +327,7 @@ static inline unsigned long
lru_gen_folio_seq(const struct lruvec *lruvec,
(folio_test_dirty(folio) || folio_test_writeback(folio))))
gen = MIN_NR_GENS;
else- gen = MAX_NR_GENS - (folio_test_workingset(folio) ||folio_test_referenced(folio));
+ gen = MAX_NR_GENS - (folio_test_workingset(folio) ||folio_lru_refs(folio) > 1);
return max(READ_ONCE(lrugen->max_seq) - gen + 1,
READ_ONCE(lrugen->min_seq[type]));
}
In both cases, kernel build performance holds up. Reverting the patch would regress system time from 3m40s to 4m4s.
I believe either option would eliminate or reduce interference with syscalls that use PG_referenced.
All we need is to give in-fault folios slightly higher priority through folio_mark_accessed(folio). I now realize the original patch might have gone a bit too far with || folio_test_referenced(folio).
I will wait for Ehab's and Kairui's testing results on these options.
Quoted 64 lines · Barry Song
On Sun, Oct 18, 2026 at 2:45 PM Barry Song (Xiaomi) <[email protected]> wrote:
[...]Quoted 7 lines
Thanks. I guess the real problem is that we have no reliable way to identify whether a folio comes from in-fault readahead. I've tried various combinations of
PG_flags, but none of them works. So the only solution seems to be separate LRU-add callbacks for readahead file folios and other folios.Quoted 5 lines
I've drafted a version below, but it's pretty ugly and feels like using a sledgehammer to crack a nut for a quick fix — simply not worth it. Maybe the best approach is to revert
6cbdd9726fb5and leave the proper fix to Kairui's MGLRU-FG.Before reverting the patch (and regressing workloads such as kernel builds), I tried the following two options:
1. Remove
folio_test_referenced()asPG_referencedmight interfere with syscall.include/linux/mm_inline.h +1
-1@@ -327,7 +327,7 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, (folio_test_dirty(folio) || folio_test_writeback(folio)))) gen = MIN_NR_GENS; else - gen =MAX_NR_GENS - (folio_test_workingset (folio) || folio_test_referenced(folio)); + gen =MAX_NR_GENS -folio_test_workingset (folio);return max(READ_ONCE(lrugen->max_seq) - gen + 1,
READ_ONCE(lrugen->min_seq[type]));
}2. changing to > 1 for
folio_lru_refs()to reduce the syscall ra/non-ra interference:include/linux/mm_inline.h +1
-1@@ -327,7 +327,7 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, (folio_test_dirty(folio) || folio_test_writeback(folio)))) gen = MIN_NR_GENS; else - gen =MAX_NR_GENS - (folio_test_workingset (folio) || folio_test_referenced(folio)); + gen =MAX_NR_GENS - (folio_test_workingset (folio) ||folio_lru_refs (folio) >1 );return max(READ_ONCE(lrugen->max_seq) - gen + 1,
READ_ONCE(lrugen->min_seq[type]));
}In both cases, kernel build performance holds up. Reverting the patch would regress system time from 3m40s to 4m4s.
I believe either option would eliminate or reduce interference with syscalls that use
PG_referenced.
Quoted 6 lines
All we need is to give in-fault folios slightly higher priority through
folio_mark_accessed(folio). I now realize the original patch might have gone a bit too far with ||folio_test_referenced(folio).I will wait for Ehab's and Kairui's testing results on these options.
Hello, Enab and Barry,
I can confirm folio_lru_refs(folio) > 1 works for me, the LevelDB test basically aligned with pre 6cbdd9726fb5, the same for some other DB that had regressions.
And any update from Enab? Given the fact that Enab's regression is also caused by that incidental bias, perhaps we can go forward with this path? I'm looking forward to rebase MGLRU-FG on top of this.
Thanks!
Barry Song
reviewerOn Thu, Nov 5, 2026 at 10:00 AM Kairui Song <[email protected]> wrote:
[...]
Quoted 36 lines
Quoted 27 lines
2. changing to > 1 for
folio_lru_refs()to reduce the syscall ra/non-ra interference:include/linux/mm_inline.h +1
-1@@ -327,7 +327,7 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, (folio_test_dirty(folio) || folio_test_writeback(folio)))) gen = MIN_NR_GENS; else - gen =MAX_NR_GENS - (folio_test_workingset (folio) || folio_test_referenced(folio)); + gen =MAX_NR_GENS - (folio_test_workingset (folio) ||folio_lru_refs (folio) >1 );return max(READ_ONCE(lrugen->max_seq) - gen + 1,
READ_ONCE(lrugen->min_seq[type]));
}In both cases, kernel build performance holds up. Reverting the patch would regress system time from 3m40s to 4m4s.
I believe either option would eliminate or reduce interference with syscalls that use
PG_referenced.Quoted 6 lines
All we need is to give in-fault folios slightly higher priority through
folio_mark_accessed(folio). I now realize the original patch might have gone a bit too far with ||folio_test_referenced(folio).I will wait for Ehab's and Kairui's testing results on these options.
Hello, Enab and Barry,
Quoted 7 lines
I can confirm
folio_lru_refs(folio)> 1 works for me, the LevelDB test basically aligned with pre6cbdd9726fb5, the same for some other DB that had regressions.And any update from Enab? Given the fact that Enab's regression is also caused by that incidental bias, perhaps we can go forward with this path? I'm looking forward to rebase MGLRU-FG on top of this.
Hi Kairui,
Thanks for your confirmation.
Unfortunately, things might be different for Ehab. He told me that this might not help with his Cassandra read workload, so I still don't quite understand what's going on there.
I'm waiting for Ehab's scripts to reproduce the workload. He emailed me today saying that he would prepare them. I really appreciate Ehab's efforts.
Best,
Barry