mm: mglru: clear the reference counter for rejected folios
Subsystems (3)
Baolin Wang
reviewerPATCH v2 1/1 mm: mglru: clear the reference counter for rejected folios
As per the comment on LRU_REFS_FLAGS, when accessed folios are promoted to a new generation, LRU_REFS_FLAGS should be cleared so that the reference counter can start over.
For folios rejected by shrink_folio_list(), we clear LRU_REFS_FLAGS and set the PG_active flag when lru_gen_folio_seq() would place them in the oldest generation. That's fine.
But for rejected folios where lru_gen_folio_seq() returns a generation other than the oldest one (which can be treated as a promotion), we do not clear LRU_REFS_FLAGS. This can violate the promotion mechanism. And this means the rejected folio enters the new generation with stale, inflated tier bits, which can inflate reference counts and distort eviction statistics for these rejected folios.
Fix this by clearing LRU_REFS_FLAGS for rejected folios. Of course, I need to evaluate the impact of the changes, which mainly falls into 3 cases:
1. When lru_gen_folio_seq() returns the oldest generation for rejected folios, there are no logic changes, and they will be put back into the 2nd youngest generation.
2. For rejected folios with PG_active set by shrink_folio_list(), we only clear the LRU_REFS_FLAGS and do not change the generation.
3. For rejected folios with PG_referenced set, the original code would put them back into the 2nd oldest generation. After this patch, we will put them back into the 2nd youngest generation.
I think case 3 is also reasonable, before commmit 6cbdd9726fb5 ("mm/mglru:
use folio_mark_accessed to replace folio_set_active"), a rejected referenced folio was also put back to the 2nd youngest gen. Meanwhile, I didn't see any noticeable performance impact on my 32-core Arm machine when running 'make -j32' to build the kernel inside a 3G-limited memcg with either zram or NVMe swap.
---
Changes from v1:
- Update the commit message.
- Clear LRU_REFS_FLAGS earlier.
--- mm/vmscan.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
mm/vmscan.c +4 -3
@@ -5020,11 +5020,12 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,continue ; } +/* See the comments on LRU_REFS_FLAGS */ +folio_set_lru_refs (folio,0 ); +/* don't add rejected folios to the oldest generation */ -if (lru_gen_folio_seq (lruvec, folio, false) == min_seq[type]) { -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);
Signature · 1 line
2.47.3
Quoted 26 lines · Baolin Wang
As per the comment on
LRU_REFS_FLAGS, when accessed folios are promoted to a new generation,LRU_REFS_FLAGSshould be cleared so that the reference counter can start over.For folios rejected by
shrink_folio_list(), we clearLRU_REFS_FLAGSand set thePG_activeflag whenlru_gen_folio_seq()would place them in the oldest generation. That's fine.But for rejected folios where
lru_gen_folio_seq()returns a generation other than the oldest one (which can be treated as a promotion), we do not clearLRU_REFS_FLAGS. This can violate the promotion mechanism. And this means the rejected folio enters the new generation with stale, inflated tier bits, which can inflate reference counts and distort eviction statistics for these rejected folios.Fix this by clearing
LRU_REFS_FLAGSfor rejected folios. Of course, I need to evaluate the impact of the changes, which mainly falls into 3 cases:
1. Whenlru_gen_folio_seq()returns the oldest generation for rejected folios, there are no logic changes, and they will be put back into the 2nd youngest generation.
2. For rejected folios withPG_activeset byshrink_folio_list(), we only clear theLRU_REFS_FLAGSand do not change the generation.
3. For rejected folios withPG_referencedset, the original code would put them back into the 2nd oldest generation. After this patch, we will put them back into the 2nd youngest generation.
Quoted 6 lines
I think case 3 is also reasonable, before commmit
6cbdd9726fb5("mm/mglru:
usefolio_mark_accessedto replacefolio_set_active"), a rejected referenced folio was also put back to the 2nd youngest gen. Meanwhile, I didn't see any noticeable performance impact on my 32-core Arm machine when running 'make -j32' to build the kernel inside a 3G-limited memcg with either zram or NVMe swap.
According to our discussion in v1 thread, the patch log clearly states the justification, and the code change looks good to me. Thanks for the effort.
Quoted 33 lines
Signed-off-by Baolin Wang <[email protected]>---
Changes from v1:
- Update the commit message.
- ClearLRU_REFS_FLAGSearlier.--- mm/vmscan.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)mm/vmscan.c +4
-3@@ -5020,11 +5020,12 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,continue ; } +/* See the comments on LRU_REFS_FLAGS */ +folio_set_lru_refs (folio,0 ); +/* don't add rejected folios to the oldest generation */ -if (lru_gen_folio_seq (lruvec, folio, false) == min_seq[type]) { -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);Signature · 1 line
2.47.3
Quoted 48 lines · Baolin Wang
As per the comment on
LRU_REFS_FLAGS, when accessed folios are promoted to a new generation,LRU_REFS_FLAGSshould be cleared so that the reference counter can start over.For folios rejected by
shrink_folio_list(), we clearLRU_REFS_FLAGSand set thePG_activeflag whenlru_gen_folio_seq()would place them in the oldest generation. That's fine.But for rejected folios where
lru_gen_folio_seq()returns a generation other than the oldest one (which can be treated as a promotion), we do not clearLRU_REFS_FLAGS. This can violate the promotion mechanism. And this means the rejected folio enters the new generation with stale, inflated tier bits, which can inflate reference counts and distort eviction statistics for these rejected folios.Fix this by clearing
LRU_REFS_FLAGSfor rejected folios. Of course, I need to evaluate the impact of the changes, which mainly falls into 3 cases:
1. Whenlru_gen_folio_seq()returns the oldest generation for rejected folios, there are no logic changes, and they will be put back into the 2nd youngest generation.
2. For rejected folios withPG_activeset byshrink_folio_list(), we only clear theLRU_REFS_FLAGSand do not change the generation.
3. For rejected folios withPG_referencedset, the original code would put them back into the 2nd oldest generation. After this patch, we will put them back into the 2nd youngest generation.I think case 3 is also reasonable, before commmit
6cbdd9726fb5("mm/mglru:
usefolio_mark_accessedto replacefolio_set_active"), a rejected referenced folio was also put back to the 2nd youngest gen. Meanwhile, I didn't see any noticeable performance impact on my 32-core Arm machine when running 'make -j32' to build the kernel inside a 3G-limited memcg with either zram or NVMe swap.Signed-off-by Baolin Wang <[email protected]>---
Changes from v1:
- Update the commit message.
- ClearLRU_REFS_FLAGSearlier.--- mm/vmscan.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)mm/vmscan.c +0
-0
mm/vmscan.c static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec, · line 5025
Quoted 6 lines
mm/vmscan.c +3
-0@@ -5020,11 +5020,12 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,continue ; } +/* See the comments on LRU_REFS_FLAGS */ +folio_set_lru_refs (folio,0 ); +
Will it be better to mention that we never add the folio to the oldest gen (since folio_set_active below prevents that) so this is effectively promotes the folio by at least one generation? Maybe can be combined with the comment below.
Quoted 11 lines
mm/vmscan.c +1
-4/* don't add rejected folios to the oldest generation */ -if (lru_gen_folio_seq (lruvec, folio, false) == min_seq[type]) { -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); --2.47.3
I tested this, and also tested with MGLRU-FG on top (the posted version is on top of this):
https://lore.kernel.org/linux-mm/[email protected]/
Both cases looked good with no performance regression observed from this patch with a few quick tests. It also has no conflict with further changes. Nothing surprising was observed, so:
Baolin Wang
reviewerQuoted 53 lines · Kairui Song
Quoted 51 lines · Baolin Wang
As per the comment on
LRU_REFS_FLAGS, when accessed folios are promoted to a new generation,LRU_REFS_FLAGSshould be cleared so that the reference counter can start over.For folios rejected by
shrink_folio_list(), we clearLRU_REFS_FLAGSand set thePG_activeflag whenlru_gen_folio_seq()would place them in the oldest generation. That's fine.But for rejected folios where
lru_gen_folio_seq()returns a generation other than the oldest one (which can be treated as a promotion), we do not clearLRU_REFS_FLAGS. This can violate the promotion mechanism. And this means the rejected folio enters the new generation with stale, inflated tier bits, which can inflate reference counts and distort eviction statistics for these rejected folios.Fix this by clearing
LRU_REFS_FLAGSfor rejected folios. Of course, I need to evaluate the impact of the changes, which mainly falls into 3 cases:
1. Whenlru_gen_folio_seq()returns the oldest generation for rejected folios, there are no logic changes, and they will be put back into the 2nd youngest generation.
2. For rejected folios withPG_activeset byshrink_folio_list(), we only clear theLRU_REFS_FLAGSand do not change the generation.
3. For rejected folios withPG_referencedset, the original code would put them back into the 2nd oldest generation. After this patch, we will put them back into the 2nd youngest generation.I think case 3 is also reasonable, before commmit
6cbdd9726fb5("mm/mglru:
usefolio_mark_accessedto replacefolio_set_active"), a rejected referenced folio was also put back to the 2nd youngest gen. Meanwhile, I didn't see any noticeable performance impact on my 32-core Arm machine when running 'make -j32' to build the kernel inside a 3G-limited memcg with either zram or NVMe swap.Signed-off-by Baolin Wang <[email protected]>---
Changes from v1:
- Update the commit message.
- ClearLRU_REFS_FLAGSearlier.--- mm/vmscan.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)mm/vmscan.c +0
-0@@ -5020,11 +5020,12 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,continue ; }
Quoted 8 lines
mm/vmscan.c static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec, · line 5025
Quoted 3 lines
mm/vmscan.c +3
-0+/* See the comments on LRU_REFS_FLAGS */ +folio_set_lru_refs (folio,0 ); +Will it be better to mention that we never add the folio to the oldest gen (since
folio_set_activebelow prevents that) so this is effectively promotes the folio by at least one generation? Maybe can be combined with the comment below.
OK. How about the following comments?
mm/vmscan.c +6 -3
@@ -5071,10 +5071,13 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,continue ; } -/* See the comments on LRU_REFS_FLAGS */ +/* +* See the comments on LRU_REFS_FLAGS. +* +* The rejected folios are never added to the oldest generation, +* so this effectively promotes them by at least one generation. +*/ folio_set_lru_refs (folio,0 ); - -/* don't add rejected folios to the oldest generation */ if (lru_gen_folio_seq(lruvec, folio, false) ==
min_seq[type])
folio_set_active(folio);
}Quoted 13 lines
Quoted 11 lines
mm/vmscan.c +1
-4/* don't add rejected folios to the oldest generation */ -if (lru_gen_folio_seq (lruvec, folio, false) == min_seq[type]) { -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); --Quoted 1 line
2.47.3
Quoted 7 lines
I tested this, and also tested with MGLRU-FG on top (the posted version is on top of this):
https://lore.kernel.org/linux-mm/[email protected]/Both cases looked good with no performance regression observed from this patch with a few quick tests. It also has no conflict with further changes. Nothing surprising was observed, so:
Great. Thanks for testing.
Quoted 1 line
Reviewed-by Kairui Song <[email protected]>
Quoted 81 lines · Baolin Wang
Quoted 53 lines · Kairui Song
Quoted 51 lines · Baolin Wang
As per the comment on
LRU_REFS_FLAGS, when accessed folios are promoted to a new generation,LRU_REFS_FLAGSshould be cleared so that the reference counter can start over.For folios rejected by
shrink_folio_list(), we clearLRU_REFS_FLAGSand set thePG_activeflag whenlru_gen_folio_seq()would place them in the oldest generation. That's fine.But for rejected folios where
lru_gen_folio_seq()returns a generation other than the oldest one (which can be treated as a promotion), we do not clearLRU_REFS_FLAGS. This can violate the promotion mechanism. And this means the rejected folio enters the new generation with stale, inflated tier bits, which can inflate reference counts and distort eviction statistics for these rejected folios.Fix this by clearing
LRU_REFS_FLAGSfor rejected folios. Of course, I need to evaluate the impact of the changes, which mainly falls into 3 cases:
1. Whenlru_gen_folio_seq()returns the oldest generation for rejected folios, there are no logic changes, and they will be put back into the 2nd youngest generation.
2. For rejected folios withPG_activeset byshrink_folio_list(), we only clear theLRU_REFS_FLAGSand do not change the generation.
3. For rejected folios withPG_referencedset, the original code would put them back into the 2nd oldest generation. After this patch, we will put them back into the 2nd youngest generation.I think case 3 is also reasonable, before commmit
6cbdd9726fb5("mm/mglru:
usefolio_mark_accessedto replacefolio_set_active"), a rejected referenced folio was also put back to the 2nd youngest gen. Meanwhile, I didn't see any noticeable performance impact on my 32-core Arm machine when running 'make -j32' to build the kernel inside a 3G-limited memcg with either zram or NVMe swap.Signed-off-by Baolin Wang <[email protected]>---
Changes from v1:
- Update the commit message.
- ClearLRU_REFS_FLAGSearlier.--- mm/vmscan.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)mm/vmscan.c +0
-0@@ -5020,11 +5020,12 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,continue ; }Quoted 8 lines
mm/vmscan.c static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec, · line 5025
Quoted 3 lines
mm/vmscan.c +3
-0+/* See the comments on LRU_REFS_FLAGS */ +folio_set_lru_refs (folio,0 ); +Will it be better to mention that we never add the folio to the oldest gen (since
folio_set_activebelow prevents that) so this is effectively promotes the folio by at least one generation? Maybe can be combined with the comment below.OK. How about the following comments?
mm/vmscan.c +5
-1@@ -5071,10 +5071,13 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec, continue; } -/* See the comments on LRU_REFS_FLAGS */ +/* +* See the comments on LRU_REFS_FLAGS. +* +* The rejected folios are never added to the oldest generation, +* so this effectively promotes them by at least one
Quoted 2 lines
generation.
+ */
It looks good to me, thanks!
Quoted 1 line · Kairui Song
[...]
Quoted 10 lines
Quoted 10 lines
mm/vmscan.c +0
-0@@ -5071,10 +5071,13 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec, continue; }
Quoted 11 lines
Quoted 9 lines
mm/vmscan.c +5
-1-/* See the comments on LRU_REFS_FLAGS */ +/* +* See the comments on LRU_REFS_FLAGS. +* +* The rejected folios are never added to the oldest generation, +* so this effectively promotes them by at least one generation.
+ */It looks good to me, thanks!
+1, with the above change,