mm/mglru: clarify the generation placement in lru_gen_folio_seq()
Subsystems (3)
PATCH 1/1 mm/mglru: clarify the generation placement in lru_gen_folio_seq()
From: Ridong Chen <[email protected]>
lru_gen_folio_seq() computes how many generations below max_seq a folio should be added to, but stores it in a variable named "gen". Elsewhere in MGLRU "gen" means an index into lrugen->folios[], i.e. seq % MAX_NR_GENS, so reusing the name for a relative offset is confusing.
The comment table above the code also no longer matches it. It only covers the PG_active and the final branch, leaving out the reclaiming and "can't evict yet" cases, and it lists LRU_REFS_FLAGS as an input although lru_gen_folio_seq() never looks at it.
Rename the local variable to "age", redraw the table so that each branch maps to the age and seq it produces, and note that the result is clamped to min_seq[type].
No functional change.
--- include/linux/mm_inline.h | 48 ++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 16 deletions(-)
include/linux/mm_inline.h +32 -16
@@ -302,34 +302,50 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,const struct folio *folio, bool reclaiming) { - int gen; + int age;int type =folio_is_file_lru (folio);const struct lru_gen_folio *lrugen = &lruvec->lrugen;/* -* +-----------------------------------+-----------------------------------+ -* | Accessed through page tables and | Accessed through file descriptors | -* | promoted by folio_update_gen() | and protected by folio_inc_gen() | -* +-----------------------------------+-----------------------------------+ -* | PG_active (set while isolated) | | -* +-----------------+-----------------+-----------------+-----------------+ -* | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAGS | -* +-----------------------------------+-----------------------------------+ -* |<---------- MIN_NR_GENS ---------->| | -* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------+-----------------+-----------------+-----------------+ +* age | 1 | 2 | 3 | 4 | +* +-----------------+-----------------+-----------------+-----------------+ +* seq | max_seq | max_seq-1 | max_seq-2 | max_seq-3 | +* +-----------------------------------+-----------------------------------+ +* |<---------- MIN_NR_GENS ---------->| | +* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------------------------+-----------------------------------+ +* | Accessed through page tables and | Accessed through file descriptors | +* | promoted by folio_update_gen() | and protected by folio_inc_gen() | +* +-----------------+-----------------+-----------------+-----------------+ +* PG_active | PG_workingset | !PG_workingset | | | +* +-----------------+-----------------+-----------------+-----------------+ +* reclaiming | | | | always | +* +-----------------+-----------------+-----------------+-----------------+ +* can't | | anon && not in | | | +* evict yet | | swapcache, or | | | +* | | PG_reclaim && | | | +* | | dirty/writeback | | | +* +-----------------+-----------------+-----------------+-----------------+ +* else | | | PG_workingset | !PG_workingset | +* | | | or | && | +* | | | PG_referenced | !PG_referenced | +* +-----------------+-----------------+-----------------+-----------------+ +* +* The resulting seq is clamped to min_seq[type]. */ if (folio_test_active (folio)) - gen =MIN_NR_GENS -folio_test_workingset (folio); + age =MIN_NR_GENS -folio_test_workingset (folio);else if (reclaiming) - gen =MAX_NR_GENS ; + age =MAX_NR_GENS ;else if ((!folio_is_file_lru (folio) && !folio_test_swapcache (folio)) || (folio_test_reclaim (folio) && (folio_test_dirty (folio) ||folio_test_writeback (folio)))) - gen =MIN_NR_GENS ; + age =MIN_NR_GENS ;else - gen =MAX_NR_GENS - (folio_test_workingset (folio) ||folio_test_referenced (folio)); + age =MAX_NR_GENS - (folio_test_workingset (folio) ||folio_test_referenced (folio)); -return max (READ_ONCE (lrugen->max_seq) - gen +1 ,READ_ONCE (lrugen->min_seq[type])); +return max (READ_ONCE (lrugen->max_seq) - age +1 ,READ_ONCE (lrugen->min_seq[type])); }static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming)
Signature · 1 line
2.34.1
Barry Song
reviewerQuoted 18 lines · Ridong Chen
From: Ridong Chen <[email protected]>
lru_gen_folio_seq()computes how many generations belowmax_seqa folio should be added to, but stores it in a variable named "gen". Elsewhere in MGLRU "gen" means an index into lrugen->folios[], i.e. seq %MAX_NR_GENS, so reusing the name for a relative offset is confusing.The comment table above the code also no longer matches it. It only covers the
PG_activeand the final branch, leaving out the reclaiming and "can't evict yet" cases, and it listsLRU_REFS_FLAGSas an input althoughlru_gen_folio_seq()never looks at it.Rename the local variable to "age", redraw the table so that each branch maps to the age and seq it produces, and note that the result is clamped to min_seq[type].
Quoted 5 lines
No functional change.
Assisted-by LLMSigned-off-by Ridong Chen <[email protected]>---
Hi Ridong,
The cleanup might be right, but this part is changing rapidly with Kairui's MGLRU-FG series. Maybe we should wait until that series lands and then see if this cleanup is still needed?
Best Regards
Barry
Quoted 25 lines · Barry Song
Quoted 18 lines · Ridong Chen
From: Ridong Chen <[email protected]>
lru_gen_folio_seq()computes how many generations belowmax_seqa folio should be added to, but stores it in a variable named "gen". Elsewhere in MGLRU "gen" means an index into lrugen->folios[], i.e. seq %MAX_NR_GENS, so reusing the name for a relative offset is confusing.The comment table above the code also no longer matches it. It only covers the
PG_activeand the final branch, leaving out the reclaiming and "can't evict yet" cases, and it listsLRU_REFS_FLAGSas an input althoughlru_gen_folio_seq()never looks at it.Rename the local variable to "age", redraw the table so that each branch maps to the age and seq it produces, and note that the result is clamped to min_seq[type].
Quoted 5 lines
No functional change.
Assisted-by LLMSigned-off-by Ridong Chen <[email protected]>---
Quoted 6 lines
Hi Ridong,
The cleanup might be right, but this part is changing rapidly with Kairui's MGLRU-FG series. Maybe we should wait until that series lands and then see if this cleanup is still needed?
Sure, I'm fine to wait. Looking at MGLRU-FG v4, though, only [4/17] touches this function, and it mostly adapts the workingset checks to LRU refs. The local is still called "gen" and the table still leaves out the reclaiming and "can't evict yet" branches, so the function stays just as confusing after the series.
Or,Kairui, do you think this is worth cleaning up? If so, would you mind adding a patch to MGLRU-FG to clean it up, so it doesn't conflict with your changes?
Signature · 2 lines
Best regards Ridong
Quoted 38 lines · Ridong Chen
Quoted 25 lines · Barry Song
Quoted 18 lines · Ridong Chen
From: Ridong Chen <[email protected]>
lru_gen_folio_seq()computes how many generations belowmax_seqa folio should be added to, but stores it in a variable named "gen". Elsewhere in MGLRU "gen" means an index into lrugen->folios[], i.e. seq %MAX_NR_GENS, so reusing the name for a relative offset is confusing.The comment table above the code also no longer matches it. It only covers the
PG_activeand the final branch, leaving out the reclaiming and "can't evict yet" cases, and it listsLRU_REFS_FLAGSas an input althoughlru_gen_folio_seq()never looks at it.Rename the local variable to "age", redraw the table so that each branch maps to the age and seq it produces, and note that the result is clamped to min_seq[type].
Quoted 5 lines
No functional change.
Assisted-by LLMSigned-off-by Ridong Chen <[email protected]>---
Quoted 6 lines
Hi Ridong,
The cleanup might be right, but this part is changing rapidly with Kairui's MGLRU-FG series. Maybe we should wait until that series lands and then see if this cleanup is still needed?
Sure, I'm fine to wait. Looking at MGLRU-FG v4, though, only [4/17] touches this function, and it mostly adapts the workingset checks to LRU refs. The local is still called "gen" and the table still leaves out the reclaiming and "can't evict yet" branches, so the function stays just as confusing after the series.
Quoted 3 lines
Or,Kairui, do you think this is worth cleaning up? If so, would you mind adding a patch to MGLRU-FG to clean it up, so it doesn't conflict with your changes?
Sure, I can rebase or add this patch in next FG series, BTW for the comment part I left some review reply, I think we better keep that part simpler as the if branches ain't too complex to follow, repeating it again in the comment seems not helpful. How do you think?
Quoted 44 lines · Ridong Chen
From: Ridong Chen <[email protected]>
lru_gen_folio_seq()computes how many generations belowmax_seqa folio should be added to, but stores it in a variable named "gen". Elsewhere in MGLRU "gen" means an index into lrugen->folios[], i.e. seq %MAX_NR_GENS, so reusing the name for a relative offset is confusing.The comment table above the code also no longer matches it. It only covers the
PG_activeand the final branch, leaving out the reclaiming and "can't evict yet" cases, and it listsLRU_REFS_FLAGSas an input althoughlru_gen_folio_seq()never looks at it.Rename the local variable to "age", redraw the table so that each branch maps to the age and seq it produces, and note that the result is clamped to min_seq[type].
No functional change.
Assisted-by LLMSigned-off-by Ridong Chen <[email protected]>--- include/linux/mm_inline.h | 48 ++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 16 deletions(-)include/linux/mm_inline.h +1
-5@@ -302,34 +302,50 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,const struct folio *folio, bool reclaiming) { - int gen; + int age;int type =folio_is_file_lru (folio);const struct lru_gen_folio *lrugen = &lruvec->lrugen;/* -* +-----------------------------------+-----------------------------------+ -* | Accessed through page tables and | Accessed through file descriptors | -* | promoted by folio_update_gen() | and protected by folio_inc_gen() | -* +-----------------------------------+-----------------------------------+
include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 313
Quoted 10 lines
include/linux/mm_inline.h +4
-6-* | PG_active (set while isolated) | | -* +-----------------+-----------------+-----------------+-----------------+ -* | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAGS | -* +-----------------------------------+-----------------------------------+ -* |<---------- MIN_NR_GENS ---------->| | -* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------+-----------------+-----------------+-----------------+ +* age | 1 | 2 | 3 | 4 | +* +-----------------+-----------------+-----------------+-----------------+ +* seq | max_seq | max_seq-1 | max_seq-2 | max_seq-3 |
I think this comment update is not beneficial, we might have more than 4 gens in the furture, so hardcoding a number is not a good idea.
Quoted 5 lines
include/linux/mm_inline.h +5
-0+* +-----------------------------------+-----------------------------------+ +* |<---------- MIN_NR_GENS ---------->| | +* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------------------------+-----------------------------------+ +* | Accessed through page tables and | Accessed through file descriptors |
include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 328
Quoted 10 lines
include/linux/mm_inline.h +10
-0+* | promoted by folio_update_gen() | and protected by folio_inc_gen() | +* +-----------------+-----------------+-----------------+-----------------+ +* PG_active | PG_workingset | !PG_workingset | | | +* +-----------------+-----------------+-----------------+-----------------+ +* reclaiming | | | | always | +* +-----------------+-----------------+-----------------+-----------------+ +* can't | | anon && not in | | | +* evict yet | | swapcache, or | | | +* | | PG_reclaim && | | | +* | | dirty/writeback | | |
I think it's better not to describe too many details in the comment as it actually easier to just look at the code.
include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 338
Quoted 11 lines
include/linux/mm_inline.h +8
-1+* +-----------------+-----------------+-----------------+-----------------+ +* else | | | PG_workingset | !PG_workingset | +* | | | or | && | +* | | | PG_referenced | !PG_referenced | +* +-----------------+-----------------+-----------------+-----------------+ +* +* The resulting seq is clamped to min_seq[type]. */ if (folio_test_active (folio)) - gen =MIN_NR_GENS -folio_test_workingset (folio); + age =MIN_NR_GENS -folio_test_workingset (folio);
That's a very nice rename, the gen variable is so confusing to me for a long time, I thought about renaming it to gap, age looks much better. Thanks!
Barry Song
reviewerQuoted 91 lines · Kairui Song
Quoted 44 lines · Ridong Chen
From: Ridong Chen <[email protected]>
lru_gen_folio_seq()computes how many generations belowmax_seqa folio should be added to, but stores it in a variable named "gen". Elsewhere in MGLRU "gen" means an index into lrugen->folios[], i.e. seq %MAX_NR_GENS, so reusing the name for a relative offset is confusing.The comment table above the code also no longer matches it. It only covers the
PG_activeand the final branch, leaving out the reclaiming and "can't evict yet" cases, and it listsLRU_REFS_FLAGSas an input althoughlru_gen_folio_seq()never looks at it.Rename the local variable to "age", redraw the table so that each branch maps to the age and seq it produces, and note that the result is clamped to min_seq[type].
No functional change.
Assisted-by LLMSigned-off-by Ridong Chen <[email protected]>--- include/linux/mm_inline.h | 48 ++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 16 deletions(-)include/linux/mm_inline.h +1
-5@@ -302,34 +302,50 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,const struct folio *folio, bool reclaiming) { - int gen; + int age;int type =folio_is_file_lru (folio);const struct lru_gen_folio *lrugen = &lruvec->lrugen;/* -* +-----------------------------------+-----------------------------------+ -* | Accessed through page tables and | Accessed through file descriptors | -* | promoted by folio_update_gen() | and protected by folio_inc_gen() | -* +-----------------------------------+-----------------------------------+ include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 313
Quoted 10 lines
include/linux/mm_inline.h +4
-6-* | PG_active (set while isolated) | | -* +-----------------+-----------------+-----------------+-----------------+ -* | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAGS | -* +-----------------------------------+-----------------------------------+ -* |<---------- MIN_NR_GENS ---------->| | -* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------+-----------------+-----------------+-----------------+ +* age | 1 | 2 | 3 | 4 | +* +-----------------+-----------------+-----------------+-----------------+ +* seq | max_seq | max_seq-1 | max_seq-2 | max_seq-3 | I think this comment update is not beneficial, we might have more than 4 gens in the furture, so hardcoding a number is not a good idea.
Quoted 5 lines
include/linux/mm_inline.h +5
-0+* +-----------------------------------+-----------------------------------+ +* |<---------- MIN_NR_GENS ---------->| | +* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------------------------+-----------------------------------+ +* | Accessed through page tables and | Accessed through file descriptors | include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 328
Quoted 10 lines
include/linux/mm_inline.h +10
-0+* | promoted by folio_update_gen() | and protected by folio_inc_gen() | +* +-----------------+-----------------+-----------------+-----------------+ +* PG_active | PG_workingset | !PG_workingset | | | +* +-----------------+-----------------+-----------------+-----------------+ +* reclaiming | | | | always | +* +-----------------+-----------------+-----------------+-----------------+ +* can't | | anon && not in | | | +* evict yet | | swapcache, or | | | +* | | PG_reclaim && | | | +* | | dirty/writeback | | | I think it's better not to describe too many details in the comment as it actually easier to just look at the code.
include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 338
Quoted 11 lines
include/linux/mm_inline.h +8
-1+* +-----------------+-----------------+-----------------+-----------------+ +* else | | | PG_workingset | !PG_workingset | +* | | | or | && | +* | | | PG_referenced | !PG_referenced | +* +-----------------+-----------------+-----------------+-----------------+ +* +* The resulting seq is clamped to min_seq[type]. */ if (folio_test_active (folio)) - gen =MIN_NR_GENS -folio_test_workingset (folio); + age =MIN_NR_GENS -folio_test_workingset (folio);
Quoted 3 lines
That's a very nice rename, the
genvariable is so confusing to me for a long time, I thought about renaming it togap,agelooks much better. Thanks!
Agreed with the rename part. Maybe this doesn't conflict much with MGLRU-FG either. It could be cherry-picked before MGLRU-FG or included as part of the MGLRU-FG series. I'd vote for the latter.
Quoted 77 lines · Barry Song
Quoted 76 lines · Kairui Song
Quoted 44 lines · Ridong Chen
From: Ridong Chen <[email protected]>
lru_gen_folio_seq()computes how many generations belowmax_seqa folio should be added to, but stores it in a variable named "gen". Elsewhere in MGLRU "gen" means an index into lrugen->folios[], i.e. seq %MAX_NR_GENS, so reusing the name for a relative offset is confusing.The comment table above the code also no longer matches it. It only covers the
PG_activeand the final branch, leaving out the reclaiming and "can't evict yet" cases, and it listsLRU_REFS_FLAGSas an input althoughlru_gen_folio_seq()never looks at it.Rename the local variable to "age", redraw the table so that each branch maps to the age and seq it produces, and note that the result is clamped to min_seq[type].
No functional change.
Assisted-by LLMSigned-off-by Ridong Chen <[email protected]>--- include/linux/mm_inline.h | 48 ++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 16 deletions(-)include/linux/mm_inline.h +1
-5@@ -302,34 +302,50 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,const struct folio *folio, bool reclaiming) { - int gen; + int age;int type =folio_is_file_lru (folio);const struct lru_gen_folio *lrugen = &lruvec->lrugen;/* -* +-----------------------------------+-----------------------------------+ -* | Accessed through page tables and | Accessed through file descriptors | -* | promoted by folio_update_gen() | and protected by folio_inc_gen() | -* +-----------------------------------+-----------------------------------+ include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 313
Quoted 10 lines
include/linux/mm_inline.h +4
-6-* | PG_active (set while isolated) | | -* +-----------------+-----------------+-----------------+-----------------+ -* | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAGS | -* +-----------------------------------+-----------------------------------+ -* |<---------- MIN_NR_GENS ---------->| | -* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------+-----------------+-----------------+-----------------+ +* age | 1 | 2 | 3 | 4 | +* +-----------------+-----------------+-----------------+-----------------+ +* seq | max_seq | max_seq-1 | max_seq-2 | max_seq-3 | I think this comment update is not beneficial, we might have more than 4 gens in the furture, so hardcoding a number is not a good idea.
Quoted 15 lines
include/linux/mm_inline.h +15
-0+* +-----------------------------------+-----------------------------------+ +* |<---------- MIN_NR_GENS ---------->| | +* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------------------------+-----------------------------------+ +* | Accessed through page tables and | Accessed through file descriptors | +* | promoted by folio_update_gen() | and protected by folio_inc_gen() | +* +-----------------+-----------------+-----------------+-----------------+ +* PG_active | PG_workingset | !PG_workingset | | | +* +-----------------+-----------------+-----------------+-----------------+ +* reclaiming | | | | always | +* +-----------------+-----------------+-----------------+-----------------+ +* can't | | anon && not in | | | +* evict yet | | swapcache, or | | | +* | | PG_reclaim && | | | +* | | dirty/writeback | | |
Quoted 3 lines
Quoted 3 lines
I think it's better not to describe too many details in the comment as it actually easier to just look at the code.
Fair points.
What confused me about the current table is that it is not clear which dimension it is drawn along. The columns are generations, but the rows mix access paths (page tables vs. file descriptors) with the flags that protect each of them, and some of those, like LRU_REFS_FLAGS, are not inputs to lru_gen_folio_seq() at all. So when reading it next to the code, it is hard to tell which branch puts a folio where.
How about keeping the existing table for the design overview, and just adding a short note above it on what "age" means, without hardcoding the number of generations, e.g.:
/*
* The folio is added to seq = max_seq + 1 - age, so age 1 is
* the youngest generation. Ages up to MIN_NR_GENS are active,
* and the result is clamped to min_seq[type].
*/
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
include/linux/mm_inline.h +8
-1+* +-----------------+-----------------+-----------------+-----------------+ +* else | | | PG_workingset | !PG_workingset | +* | | | or | && | +* | | | PG_referenced | !PG_referenced | +* +-----------------+-----------------+-----------------+-----------------+ +* +* The resulting seq is clamped to min_seq[type]. */ if (folio_test_active (folio)) - gen =MIN_NR_GENS -folio_test_workingset (folio); + age =MIN_NR_GENS -folio_test_workingset (folio);
Quoted 8 lines
Quoted 3 lines
That's a very nice rename, the
genvariable is so confusing to me for a long time, I thought about renaming it togap,agelooks much better. Thanks!Agreed with the rename part. Maybe this doesn't conflict much with MGLRU-FG either. It could be cherry-picked before MGLRU-FG or included as part of the MGLRU-FG series. I'd vote for the latter.
Sounds good to me. Kairui, would you like to pick up the rename, and the comment change above if you agree, as part of MGLRU-FG?
Signature · 2 lines
Best regards Ridong
Quoted 118 lines · Ridong Chen
Quoted 77 lines · Barry Song
Quoted 76 lines · Kairui Song
Quoted 44 lines · Ridong Chen
From: Ridong Chen <[email protected]>
lru_gen_folio_seq()computes how many generations belowmax_seqa folio should be added to, but stores it in a variable named "gen". Elsewhere in MGLRU "gen" means an index into lrugen->folios[], i.e. seq %MAX_NR_GENS, so reusing the name for a relative offset is confusing.The comment table above the code also no longer matches it. It only covers the
PG_activeand the final branch, leaving out the reclaiming and "can't evict yet" cases, and it listsLRU_REFS_FLAGSas an input althoughlru_gen_folio_seq()never looks at it.Rename the local variable to "age", redraw the table so that each branch maps to the age and seq it produces, and note that the result is clamped to min_seq[type].
No functional change.
Assisted-by LLMSigned-off-by Ridong Chen <[email protected]>--- include/linux/mm_inline.h | 48 ++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 16 deletions(-)include/linux/mm_inline.h +1
-5@@ -302,34 +302,50 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,const struct folio *folio, bool reclaiming) { - int gen; + int age;int type =folio_is_file_lru (folio);const struct lru_gen_folio *lrugen = &lruvec->lrugen;/* -* +-----------------------------------+-----------------------------------+ -* | Accessed through page tables and | Accessed through file descriptors | -* | promoted by folio_update_gen() | and protected by folio_inc_gen() | -* +-----------------------------------+-----------------------------------+ include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 313
Quoted 10 lines
include/linux/mm_inline.h +4
-6-* | PG_active (set while isolated) | | -* +-----------------+-----------------+-----------------+-----------------+ -* | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAGS | -* +-----------------------------------+-----------------------------------+ -* |<---------- MIN_NR_GENS ---------->| | -* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------+-----------------+-----------------+-----------------+ +* age | 1 | 2 | 3 | 4 | +* +-----------------+-----------------+-----------------+-----------------+ +* seq | max_seq | max_seq-1 | max_seq-2 | max_seq-3 | I think this comment update is not beneficial, we might have more than 4 gens in the furture, so hardcoding a number is not a good idea.
Quoted 15 lines
include/linux/mm_inline.h +15
-0+* +-----------------------------------+-----------------------------------+ +* |<---------- MIN_NR_GENS ---------->| | +* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------------------------+-----------------------------------+ +* | Accessed through page tables and | Accessed through file descriptors | +* | promoted by folio_update_gen() | and protected by folio_inc_gen() | +* +-----------------+-----------------+-----------------+-----------------+ +* PG_active | PG_workingset | !PG_workingset | | | +* +-----------------+-----------------+-----------------+-----------------+ +* reclaiming | | | | always | +* +-----------------+-----------------+-----------------+-----------------+ +* can't | | anon && not in | | | +* evict yet | | swapcache, or | | | +* | | PG_reclaim && | | | +* | | dirty/writeback | | | Quoted 3 lines
Quoted 3 lines
I think it's better not to describe too many details in the comment as it actually easier to just look at the code.
Fair points.
What confused me about the current table is that it is not clear which dimension it is drawn along. The columns are generations, but the rows mix access paths (page tables vs. file descriptors) with the flags that protect each of them, and some of those, like
LRU_REFS_FLAGS, are not inputs tolru_gen_folio_seq()at all. So when reading it next to the code, it is hard to tell which branch puts a folio where.How about keeping the existing table for the design overview, and just adding a short note above it on what "age" means, without hardcoding the number of generations, e.g.:
/*
* The folio is added to seq =max_seq+ 1 - age, so age 1 is
* the youngest generation. Ages up toMIN_NR_GENSare active,
* and the result is clamped to min_seq[type].
*/Quoted 16 lines
Quoted 15 lines
include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 338
Quoted 11 lines
include/linux/mm_inline.h +8
-1+* +-----------------+-----------------+-----------------+-----------------+ +* else | | | PG_workingset | !PG_workingset | +* | | | or | && | +* | | | PG_referenced | !PG_referenced | +* +-----------------+-----------------+-----------------+-----------------+ +* +* The resulting seq is clamped to min_seq[type]. */ if (folio_test_active (folio)) - gen =MIN_NR_GENS -folio_test_workingset (folio); + age =MIN_NR_GENS -folio_test_workingset (folio);That's a very nice rename, the
genvariable is so confusing to me for a long time, I thought about renaming it togap,agelooks much better. Thanks!
Quoted 7 lines
Quoted 4 lines
Agreed with the rename part. Maybe this doesn't conflict much with MGLRU-FG either. It could be cherry-picked before MGLRU-FG or included as part of the MGLRU-FG series. I'd vote for the latter.
Sounds good to me. Kairui, would you like to pick up the rename, and the comment change above if you agree, as part of MGLRU-FG?
Good idea, will do!
Quoted 41 lines · Ridong Chen
From: Ridong Chen <[email protected]>
lru_gen_folio_seq()computes how many generations belowmax_seqa folio should be added to, but stores it in a variable named "gen". Elsewhere in MGLRU "gen" means an index into lrugen->folios[], i.e. seq %MAX_NR_GENS, so reusing the name for a relative offset is confusing.The comment table above the code also no longer matches it. It only covers the
PG_activeand the final branch, leaving out the reclaiming and "can't evict yet" cases, and it listsLRU_REFS_FLAGSas an input althoughlru_gen_folio_seq()never looks at it.Rename the local variable to "age", redraw the table so that each branch maps to the age and seq it produces, and note that the result is clamped to min_seq[type].
No functional change.
Assisted-by LLMSigned-off-by Ridong Chen <[email protected]>--- include/linux/mm_inline.h | 48 ++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 16 deletions(-)include/linux/mm_inline.h +1
-3@@ -302,34 +302,50 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,const struct folio *folio, bool reclaiming) { - int gen; + int age;int type =folio_is_file_lru (folio);const struct lru_gen_folio *lrugen = &lruvec->lrugen;/* -* +-----------------------------------+-----------------------------------+ -* | Accessed through page tables and | Accessed through file descriptors |
include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 311
Quoted 10 lines
include/linux/mm_inline.h +2
-8-* | promoted by folio_update_gen() | and protected by folio_inc_gen() | -* +-----------------------------------+-----------------------------------+ -* | PG_active (set while isolated) | | -* +-----------------+-----------------+-----------------+-----------------+ -* | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAGS | -* +-----------------------------------+-----------------------------------+ -* |<---------- MIN_NR_GENS ---------->| | -* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------+-----------------+-----------------+-----------------+ +* age | 1 | 2 | 3 | 4 |
Hi Ridong,
Agreed, I also found this part confusing.
One problem with the new name: in MGLRU a larger seq/gen means a younger generation, but a larger age means an older one, so every read needs a flip (max_seq - age + 1). Assigning the seq directly in each branch might read more naturally.
Also, both tables run past 80 columns (~95 for the art in mglru-fg 4/17, ~96 for the redrawn one here, tab as 8). Would you consider something vertical and shorter? I had prepared a comment for mglru-fg, and since this is being discussed here too, here it is (conditions are per mglru-fg, where the flags became the refs count):
/*
* The seq is clamped to min_seq[type]:
*
* destination | folio state
* ----------------+--------------------------------------------
* youngest | PG_active, refs >= LRU_REFS_WORKINGSET
* (max_seq) |
*
* 2nd youngest | PG_active, refs < LRU_REFS_WORKINGSET
* (max_seq - 1) | anon folio not yet in swapcache
* | PG_reclaim folio, dirty or under writeback
*
* 2nd oldest | refs >= LRU_REFS_WORKINGSET
* (max_seq - 2) |
*
* oldest | reclaiming
* (min_seq[type]) | otherwise: refs < LRU_REFS_WORKINGSET
*/
With a comment like this, developers can see the generation placement directly, instead of working it out from the code.
Best,
Zicheng
Quoted 49 lines
include/linux/mm_inline.h +29
-5+* +-----------------+-----------------+-----------------+-----------------+ +* seq | max_seq | max_seq-1 | max_seq-2 | max_seq-3 | +* +-----------------------------------+-----------------------------------+ +* |<---------- MIN_NR_GENS ---------->| | +* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------------------------+-----------------------------------+ +* | Accessed through page tables and | Accessed through file descriptors | +* | promoted by folio_update_gen() | and protected by folio_inc_gen() | +* +-----------------+-----------------+-----------------+-----------------+ +* PG_active | PG_workingset | !PG_workingset | | | +* +-----------------+-----------------+-----------------+-----------------+ +* reclaiming | | | | always | +* +-----------------+-----------------+-----------------+-----------------+ +* can't | | anon && not in | | | +* evict yet | | swapcache, or | | | +* | | PG_reclaim && | | | +* | | dirty/writeback | | | +* +-----------------+-----------------+-----------------+-----------------+ +* else | | | PG_workingset | !PG_workingset | +* | | | or | && | +* | | | PG_referenced | !PG_referenced | +* +-----------------+-----------------+-----------------+-----------------+ +* +* The resulting seq is clamped to min_seq[type]. */ if (folio_test_active (folio)) - gen =MIN_NR_GENS -folio_test_workingset (folio); + age =MIN_NR_GENS -folio_test_workingset (folio);else if (reclaiming) - gen =MAX_NR_GENS ; + age =MAX_NR_GENS ;else if ((!folio_is_file_lru (folio) && !folio_test_swapcache (folio)) || (folio_test_reclaim (folio) && (folio_test_dirty (folio) ||folio_test_writeback (folio)))) - gen =MIN_NR_GENS ; + age =MIN_NR_GENS ;else - gen =MAX_NR_GENS - (folio_test_workingset (folio) ||folio_test_referenced (folio)); + age =MAX_NR_GENS - (folio_test_workingset (folio) ||folio_test_referenced (folio)); -return max (READ_ONCE (lrugen->max_seq) - gen +1 ,READ_ONCE (lrugen->min_seq[type])); +return max (READ_ONCE (lrugen->max_seq) - age +1 ,READ_ONCE (lrugen->min_seq[type])); }static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming)Signature · 1 line
2.34.1
Quoted 54 lines · On 10/21/2026 4:38 PM, [email protected] wrote:
Quoted 41 lines · Ridong Chen
From: Ridong Chen <[email protected]>
lru_gen_folio_seq()computes how many generations belowmax_seqa folio should be added to, but stores it in a variable named "gen". Elsewhere in MGLRU "gen" means an index into lrugen->folios[], i.e. seq %MAX_NR_GENS, so reusing the name for a relative offset is confusing.The comment table above the code also no longer matches it. It only covers the
PG_activeand the final branch, leaving out the reclaiming and "can't evict yet" cases, and it listsLRU_REFS_FLAGSas an input althoughlru_gen_folio_seq()never looks at it.Rename the local variable to "age", redraw the table so that each branch maps to the age and seq it produces, and note that the result is clamped to min_seq[type].
No functional change.
Assisted-by LLMSigned-off-by Ridong Chen <[email protected]>--- include/linux/mm_inline.h | 48 ++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 16 deletions(-)include/linux/mm_inline.h +1
-3@@ -302,34 +302,50 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,const struct folio *folio, bool reclaiming) { - int gen; + int age;int type =folio_is_file_lru (folio);const struct lru_gen_folio *lrugen = &lruvec->lrugen;/* -* +-----------------------------------+-----------------------------------+ -* | Accessed through page tables and | Accessed through file descriptors | include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 311
Quoted 10 lines
include/linux/mm_inline.h +2
-8-* | promoted by folio_update_gen() | and protected by folio_inc_gen() | -* +-----------------------------------+-----------------------------------+ -* | PG_active (set while isolated) | | -* +-----------------+-----------------+-----------------+-----------------+ -* | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAGS | -* +-----------------------------------+-----------------------------------+ -* |<---------- MIN_NR_GENS ---------->| | -* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------+-----------------+-----------------+-----------------+ +* age | 1 | 2 | 3 | 4 | Hi Ridong,
Quoted 7 lines
Agreed, I also found this part confusing.
One problem with the new name: in MGLRU a larger seq/gen means a younger generation, but a larger age means an older one, so every read needs a flip (
max_seq- age + 1). Assigning the seq directly in each branch might read more naturally.
Hi Zicheng,
Thank you for your reply. We both agree that this part of the code is confusing. 'age' is not perfect, but it is much better. The bigger the age, the elder it is, just like human age. And it is a local variable, not used elsewhere, so it is fine for me.
Quoted 26 lines
Also, both tables run past 80 columns (~95 for the art in mglru-fg 4/17, ~96 for the redrawn one here, tab as 8). Would you consider something vertical and shorter? I had prepared a comment for mglru-fg, and since this is being discussed here too, here it is (conditions are per mglru-fg, where the flags became the refs count):
/*
* The seq is clamped to min_seq[type]:
*
* destination | folio state
* ----------------+--------------------------------------------
* youngest |PG_active, refs >=LRU_REFS_WORKINGSET
* (max_seq) |
*
* 2nd youngest |PG_active, refs <LRU_REFS_WORKINGSET
* (max_seq- 1) | anon folio not yet in swapcache
* |PG_reclaimfolio, dirty or under writeback
*
* 2nd oldest | refs >=LRU_REFS_WORKINGSET
* (max_seq- 2) |
*
* oldest | reclaiming
* (min_seq[type]) | otherwise: refs <LRU_REFS_WORKINGSET
*/
Quoted 3 lines
With a comment like this, developers can see the generation placement directly, instead of working it out from the code.
Thanks for the table,
As Kairui replied, he did not want more code details in the comment, which may change a lot. And Kairui will pick this patch with his FG series. Although I wanted to add the table, Kairui felt there shouldn't be too much detail.
What is more, I think the following is needed—it is what this function initially implemented.
+-----------------------------------+-----------------------------------+ | Accessed through page tables and | Accessed through file descriptors | | promoted by folio_update_gen() | and protected by folio_inc_gen() | +-----------------+-----------------+-----------------+-----------------+
Quoted 54 lines
Best,
ZichengQuoted 49 lines
include/linux/mm_inline.h +29
-5+* +-----------------+-----------------+-----------------+-----------------+ +* seq | max_seq | max_seq-1 | max_seq-2 | max_seq-3 | +* +-----------------------------------+-----------------------------------+ +* |<---------- MIN_NR_GENS ---------->| | +* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------------------------+-----------------------------------+ +* | Accessed through page tables and | Accessed through file descriptors | +* | promoted by folio_update_gen() | and protected by folio_inc_gen() | +* +-----------------+-----------------+-----------------+-----------------+ +* PG_active | PG_workingset | !PG_workingset | | | +* +-----------------+-----------------+-----------------+-----------------+ +* reclaiming | | | | always | +* +-----------------+-----------------+-----------------+-----------------+ +* can't | | anon && not in | | | +* evict yet | | swapcache, or | | | +* | | PG_reclaim && | | | +* | | dirty/writeback | | | +* +-----------------+-----------------+-----------------+-----------------+ +* else | | | PG_workingset | !PG_workingset | +* | | | or | && | +* | | | PG_referenced | !PG_referenced | +* +-----------------+-----------------+-----------------+-----------------+ +* +* The resulting seq is clamped to min_seq[type]. */ if (folio_test_active (folio)) - gen =MIN_NR_GENS -folio_test_workingset (folio); + age =MIN_NR_GENS -folio_test_workingset (folio);else if (reclaiming) - gen =MAX_NR_GENS ; + age =MAX_NR_GENS ;else if ((!folio_is_file_lru (folio) && !folio_test_swapcache (folio)) || (folio_test_reclaim (folio) && (folio_test_dirty (folio) ||folio_test_writeback (folio)))) - gen =MIN_NR_GENS ; + age =MIN_NR_GENS ;else - gen =MAX_NR_GENS - (folio_test_workingset (folio) ||folio_test_referenced (folio)); + age =MAX_NR_GENS - (folio_test_workingset (folio) ||folio_test_referenced (folio)); -return max (READ_ONCE (lrugen->max_seq) - gen +1 ,READ_ONCE (lrugen->min_seq[type])); +return max (READ_ONCE (lrugen->max_seq) - age +1 ,READ_ONCE (lrugen->min_seq[type])); }static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming)Signature · 1 line
2.34.1
Signature · 2 lines
Best regards Ridong
Quoted 65 lines · Ridong Chen
Quoted 54 lines · On 10/21/2026 4:38 PM, [email protected] wrote:
Quoted 41 lines · Ridong Chen
From: Ridong Chen <[email protected]>
lru_gen_folio_seq()computes how many generations belowmax_seqa folio should be added to, but stores it in a variable named "gen". Elsewhere in MGLRU "gen" means an index into lrugen->folios[], i.e. seq %MAX_NR_GENS, so reusing the name for a relative offset is confusing.The comment table above the code also no longer matches it. It only covers the
PG_activeand the final branch, leaving out the reclaiming and "can't evict yet" cases, and it listsLRU_REFS_FLAGSas an input althoughlru_gen_folio_seq()never looks at it.Rename the local variable to "age", redraw the table so that each branch maps to the age and seq it produces, and note that the result is clamped to min_seq[type].
No functional change.
Assisted-by LLMSigned-off-by Ridong Chen <[email protected]>--- include/linux/mm_inline.h | 48 ++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 16 deletions(-)include/linux/mm_inline.h +1
-3@@ -302,34 +302,50 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,const struct folio *folio, bool reclaiming) { - int gen; + int age;int type =folio_is_file_lru (folio);const struct lru_gen_folio *lrugen = &lruvec->lrugen;/* -* +-----------------------------------+-----------------------------------+ -* | Accessed through page tables and | Accessed through file descriptors | include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 311
Quoted 10 lines
include/linux/mm_inline.h +2
-8-* | promoted by folio_update_gen() | and protected by folio_inc_gen() | -* +-----------------------------------+-----------------------------------+ -* | PG_active (set while isolated) | | -* +-----------------+-----------------+-----------------+-----------------+ -* | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAGS | -* +-----------------------------------+-----------------------------------+ -* |<---------- MIN_NR_GENS ---------->| | -* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------+-----------------+-----------------+-----------------+ +* age | 1 | 2 | 3 | 4 | Hi Ridong,
Quoted 7 lines
Agreed, I also found this part confusing.
One problem with the new name: in MGLRU a larger seq/gen means a younger generation, but a larger age means an older one, so every read needs a flip (
max_seq- age + 1). Assigning the seq directly in each branch might read more naturally.
Quoted 7 lines
Hi Zicheng,
Thank you for your reply. We both agree that this part of the code is confusing. 'age' is not perfect, but it is much better. The bigger the age, the elder it is, just like human age. And it is a local variable, not used elsewhere, so it is fine for me.
Hi Ridong,
Thanks for the explanation. To be clear, I have no problem with the name itself: it is a local variable and there is no functional impact either way.
Quoted 88 lines
Quoted 26 lines
Also, both tables run past 80 columns (~95 for the art in mglru-fg 4/17, ~96 for the redrawn one here, tab as 8). Would you consider something vertical and shorter? I had prepared a comment for mglru-fg, and since this is being discussed here too, here it is (conditions are per mglru-fg, where the flags became the refs count):
/*
* The seq is clamped to min_seq[type]:
*
* destination | folio state
* ----------------+--------------------------------------------
* youngest |PG_active, refs >=LRU_REFS_WORKINGSET
* (max_seq) |
*
* 2nd youngest |PG_active, refs <LRU_REFS_WORKINGSET
* (max_seq- 1) | anon folio not yet in swapcache
* |PG_reclaimfolio, dirty or under writeback
*
* 2nd oldest | refs >=LRU_REFS_WORKINGSET
* (max_seq- 2) |
*
* oldest | reclaiming
* (min_seq[type]) | otherwise: refs <LRU_REFS_WORKINGSET
*/Quoted 3 lines
With a comment like this, developers can see the generation placement directly, instead of working it out from the code.
Thanks for the table,
As Kairui replied, he did not want more code details in the comment, which may change a lot. And Kairui will pick this patch with his FG series. Although I wanted to add the table, Kairui felt there shouldn't be too much detail.
What is more, I think the following is needed—it is what this function initially implemented.
+-----------------------------------+-----------------------------------+ | Accessed through page tables and | Accessed through file descriptors | | promoted by folio_update_gen() | and protected by folio_inc_gen() | +-----------------+-----------------+-----------------+-----------------+Quoted 44 lines
Best,
ZichengQuoted 40 lines
include/linux/mm_inline.h +28
-4+* +-----------------+-----------------+-----------------+-----------------+ +* seq | max_seq | max_seq-1 | max_seq-2 | max_seq-3 | +* +-----------------------------------+-----------------------------------+ +* |<---------- MIN_NR_GENS ---------->| | +* |<---------------------------- MAX_NR_GENS ---------------------------->| +* +-----------------------------------+-----------------------------------+ +* | Accessed through page tables and | Accessed through file descriptors | +* | promoted by folio_update_gen() | and protected by folio_inc_gen() | +* +-----------------+-----------------+-----------------+-----------------+ +* PG_active | PG_workingset | !PG_workingset | | | +* +-----------------+-----------------+-----------------+-----------------+ +* reclaiming | | | | always | +* +-----------------+-----------------+-----------------+-----------------+ +* can't | | anon && not in | | | +* evict yet | | swapcache, or | | | +* | | PG_reclaim && | | | +* | | dirty/writeback | | | +* +-----------------+-----------------+-----------------+-----------------+ +* else | | | PG_workingset | !PG_workingset | +* | | | or | && | +* | | | PG_referenced | !PG_referenced | +* +-----------------+-----------------+-----------------+-----------------+ +* +* The resulting seq is clamped to min_seq[type]. */ if (folio_test_active (folio)) - gen =MIN_NR_GENS -folio_test_workingset (folio); + age =MIN_NR_GENS -folio_test_workingset (folio);else if (reclaiming) - gen =MAX_NR_GENS ; + age =MAX_NR_GENS ;else if ((!folio_is_file_lru (folio) && !folio_test_swapcache (folio)) || (folio_test_reclaim (folio) && (folio_test_dirty (folio) ||folio_test_writeback (folio)))) - gen =MIN_NR_GENS ; + age =MIN_NR_GENS ;else - gen =MAX_NR_GENS - (folio_test_workingset (folio) ||folio_test_referenced (folio)); + age =MAX_NR_GENS - (folio_test_workingset (folio) ||folio_test_referenced (folio));
Quoted 3 lines
Quoted 3 lines
include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 349
Quoted 3 lines
include/linux/mm_inline.h +1
-1-return max (READ_ONCE (lrugen->max_seq) - gen +1 ,READ_ONCE (lrugen->min_seq[type])); +return max (READ_ONCE (lrugen->max_seq) - age +1 ,READ_ONCE (lrugen->min_seq[type])); }
My real point was about the form of the computation: whatever the variable is called, the return still does max_seq - X + 1 and then clamps to min_seq[type], so a reader has to juggle a plus, a minus and a clamp at the same time. When I first read it I had to go back and forth. By comparison, the 6.6-era code assigned the placement directly in each branch (roughly `seq = max_seq or
seq = min_seq + 1` style), which is a bit more verbose but much more direct.
Anyway, these are minor points with no functional impact. I don't feel strongly about this, so I will defer to you and Kairui.
Thanks,
Zicheng
Quoted 13 lines
Quoted 7 lines
Quoted 6 lines
include/linux/mm_inline.h +0
-0static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming)Signature · 1 line
2.34.1Signature · 2 lines
Best regards Ridong