mm: mglru: reset the mm iteration cursor in try_to_inc_max_seq_nowalk()
Subsystems (4)
PATCH 1/1 mm: mglru: reset the mm iteration cursor in try_to_inc_max_seq_nowalk()
From: Hui Zhu <[email protected]>
iterate_mm_list_nowalk() and try_to_inc_max_seq_nowalk() both advance mm_state->seq without walking the mm_list, but only the former drops the cursor. When the latter races with an ongoing page table walk, head is left pointing into the middle of the fifo.
The next walker reads that as "this generation already started": it skips reset_bloom_filter() and resumes after the cursor. Every mm before the cursor is skipped, and none of them have bits in the filter being read, so walk_pmd_range() descends into none of their PMDs and harvests no accessed bits. try_to_shrink_lruvec() ignores the failed try_to_inc_max_seq() and keeps evicting, so those hot folios become reclaim candidates and refault in the parent memcg, until head wraps around a generation later.
Factor the pattern into skip_mm_iteration(), which carries the rationale that used to sit in a comment in try_to_inc_max_seq_nowalk(), so the two callers cannot drift apart again. The wrap-around in iterate_mm_list() is left open-coded on purpose: there head is already back at the fifo, and tail has to survive for the force_scan test. Dropping the cursor is safe:get_next_mm() holds an mmgrab() reference, and a concurrent walker bails out at "walk->seq <= mm_state->seq" before touching head again. The unwalked part of the fifo restarts from the head next generation, as iterate_mm_list_nowalk() already does.
Only reachable from memcg_reparent_objcgs() -> max_lru_gen_memcg(); not a memory safety issue, since lru_gen_del_mm() repoints head and tail under mm_list->lock.
--- mm/vmscan.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)
mm/vmscan.c +15 -8
@@ -3235,6 +3235,7 @@ static bool iterate_mm_list(struct lru_gen_mm_walk *walk, struct mm_struct **ite do { mm_state->head = mm_state->head->next; if (mm_state->head == &mm_list->fifo) { + /* head is back at the fifo; keep tail for force_scan below */ WRITE_ONCE(mm_state->seq, mm_state->seq + 1); last = true; break;
@@ -3263,6 +3264,18 @@ static bool iterate_mm_list(struct lru_gen_mm_walk *walk, struct mm_struct **itereturn last; } +/* +* Advance mm_state->seq without walking the mm_list. The cursor must be +* dropped as well: iterate_mm_list() resumes from head, and resets the Bloom +* filter only when head == &mm_list->fifo. +*/ +static void skip_mm_iteration (struct lru_gen_mm_state *mm_state) +{ + mm_state->head =NULL ; + mm_state->tail =NULL ; +WRITE_ONCE (mm_state->seq, mm_state->seq +1 ); +} +static bool iterate_mm_list_nowalk (struct lruvec *lruvec,unsigned long seq) {bool success = false;
@@ -3275,9 +3288,7 @@ static bool iterate_mm_list_nowalk(struct lruvec *lruvec, unsigned long seq)VM_WARN_ON_ONCE (mm_state->seq +1 < seq);if (seq > mm_state->seq) { - mm_state->head =NULL ; - mm_state->tail =NULL ; -WRITE_ONCE (mm_state->seq, mm_state->seq +1 ); +skip_mm_iteration (mm_state); success = true; }
@@ -4686,15 +4697,11 @@ static void try_to_inc_max_seq_nowalk(struct mem_cgroup *memcg,DEFINE_MAX_SEQ (lruvec);bool success = false; -/* -* We are not iterating the mm_list here, updating mm_state->seq is just -* to make mm walkers work properly. -*/ if (mm_state) {spin_lock (&mm_list->lock);VM_WARN_ON_ONCE (mm_state->seq +1 < max_seq);if (max_seq > mm_state->seq) { -WRITE_ONCE (mm_state->seq, mm_state->seq +1 ); +skip_mm_iteration (mm_state); success = true; }spin_unlock (&mm_list->lock);
Signature · 1 line
2.53.0
Quoted 10 lines · Hui Zhu
From: Hui Zhu <[email protected]>
iterate_mm_list_nowalk()andtry_to_inc_max_seq_nowalk()both advancemm_state->seqwithout walking themm_list, but only the former drops the cursor. When the latter races with an ongoing page table walk, head is left pointing into the middle of the fifo.The next walker reads that as "this generation already started": it skips
reset_bloom_filter()and resumes after the cursor. Every mm before the cursor is skipped, and none of them have bits in the filter being read, so
Nit:
Isn't it that mm after the cursor is skipped, none of them have bits in the filter being read? Because bits in the filter are set in the last generation iteration, mm after cursor didn't iterate, so no bites set for them?
Quoted 39 lines
walk_pmd_range()descends into none of their PMDs and harvests no accessed bits.try_to_shrink_lruvec()ignores the failedtry_to_inc_max_seq()and keeps evicting, so those hot folios become reclaim candidates and refault in the parent memcg, until head wraps around a generation later.Factor the pattern into
skip_mm_iteration(), which carries the rationale that used to sit in a comment intry_to_inc_max_seq_nowalk(), so the two callers cannot drift apart again. The wrap-around initerate_mm_list()is left open-coded on purpose: there head is already back at the fifo, and tail has to survive for theforce_scantest. Dropping the cursor is safe:get_next_mm()holds anmmgrab()reference, and a concurrent walker bails out at "walk->seq<=mm_state->seq" before touching head again. The unwalked part of the fifo restarts from the head next generation, asiterate_mm_list_nowalk()already does.Only reachable from
memcg_reparent_objcgs()->max_lru_gen_memcg(); not a memory safety issue, sincelru_gen_del_mm()repoints head and tail undermm_list->lock.Fixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU folios")Signed-off-by Hui Zhu <[email protected]>--- mm/vmscan.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)mm/vmscan.c +1
-0@@ -3235,6 +3235,7 @@ static bool iterate_mm_list(struct lru_gen_mm_walk *walk, struct mm_struct **ite do { mm_state->head = mm_state->head->next; if (mm_state->head == &mm_list->fifo) { + /* head is back at the fifo; keep tail for force_scan below */ WRITE_ONCE(mm_state->seq, mm_state->seq + 1); last = true; break;
mm/vmscan.c static bool iterate_mm_list(struct lru_gen_mm_walk *walk, struct mm_struct **ite · line 3271
Quoted 8 lines
mm/vmscan.c +5
-0@@ -3263,6 +3264,18 @@ static bool iterate_mm_list(struct lru_gen_mm_walk *walk, struct mm_struct **itereturn last; } +/* +* Advance mm_state->seq without walking the mm_list. The cursor must be +* dropped as well: iterate_mm_list() resumes from head, and resets the Bloom +* filter only when head == &mm_list->fifo. +*/
This looks like a good catch, while the function name here is a little bit confusing. From the code block inside, we can't see it's doing iteration, just a pair of pointer resetting and a max_seq advancing. I would rather not wrap up them if no good name is concluded. Personal opinion.
Other than the nits,
Quoted 41 lines
mm/vmscan.c +9
-8+static void skip_mm_iteration (struct lru_gen_mm_state *mm_state) +{ + mm_state->head =NULL ; + mm_state->tail =NULL ; +WRITE_ONCE (mm_state->seq, mm_state->seq +1 ); +} +static bool iterate_mm_list_nowalk (struct lruvec *lruvec,unsigned long seq) {bool success = false;@@ -3275,9 +3288,7 @@ static bool iterate_mm_list_nowalk(struct lruvec *lruvec, unsigned long seq)VM_WARN_ON_ONCE (mm_state->seq +1 < seq);if (seq > mm_state->seq) { - mm_state->head =NULL ; - mm_state->tail =NULL ; -WRITE_ONCE (mm_state->seq, mm_state->seq +1 ); +skip_mm_iteration (mm_state); success = true; }@@ -4686,15 +4697,11 @@ static void try_to_inc_max_seq_nowalk(struct mem_cgroup *memcg,DEFINE_MAX_SEQ (lruvec);bool success = false; -/* -* We are not iterating the mm_list here, updating mm_state->seq is just -* to make mm walkers work properly. -*/ if (mm_state) {spin_lock (&mm_list->lock);VM_WARN_ON_ONCE (mm_state->seq +1 < max_seq);if (max_seq > mm_state->seq) { -WRITE_ONCE (mm_state->seq, mm_state->seq +1 ); +skip_mm_iteration (mm_state); success = true; }spin_unlock (&mm_list->lock);Signature · 1 line
2.53.0
2026/11/19 15:29, Baoquan He:
Quoted 8 lines · Hui Zhu
Quoted 7 lines
From: Hui Zhu <[email protected]>
iterate_mm_list_nowalk()andtry_to_inc_max_seq_nowalk()both advancemm_state->seqwithout walking themm_list, but only the former drops the cursor. When the latter races with an ongoing page table walk, head is left pointing into the middle of the fifo.
Quoted 8 lines
Quoted 3 lines
The next walker reads that as "this generation already started": it skips
reset_bloom_filter()and resumes after the cursor. Every mm before the cursor is skipped, and none of them have bits in the filter being read, soNit:
Isn't it that mm after the cursor is skipped, none of them have bits in the filter being read? Because bits in the filter are set in the last generation iteration, mm after cursor didn't iterate, so no bites set for them?
You are right, thanks for catching it. test_bloom_filter() reads filters[walk->seq % 2], and that buffer was filled by the aborted walk of the previous generation, which only got as far as the cursor. So the mm the resumed walk visits -- the ones after the cursor -- are exactly the ones with no bits in it, and they lose their non-leaf walk. The mm before the cursor are simply left out of this generation. I will fix the wording in v2.
Quoted 42 lines
Quoted 42 lines
walk_pmd_range()descends into none of their PMDs and harvests no accessed bits.try_to_shrink_lruvec()ignores the failedtry_to_inc_max_seq()and keeps evicting, so those hot folios become reclaim candidates and refault in the parent memcg, until head wraps around a generation later.Factor the pattern into
skip_mm_iteration(), which carries the rationale that used to sit in a comment intry_to_inc_max_seq_nowalk(), so the two callers cannot drift apart again. The wrap-around initerate_mm_list()is left open-coded on purpose: there head is already back at the fifo, and tail has to survive for theforce_scantest. Dropping the cursor is safe:get_next_mm()holds anmmgrab()reference, and a concurrent walker bails out at "walk->seq<=mm_state->seq" before touching head again. The unwalked part of the fifo restarts from the head next generation, asiterate_mm_list_nowalk()already does.Only reachable from
memcg_reparent_objcgs()->max_lru_gen_memcg(); not a memory safety issue, sincelru_gen_del_mm()repoints head and tail undermm_list->lock.Fixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU folios")Signed-off-by Hui Zhu <[email protected]>--- mm/vmscan.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)mm/vmscan.c +1
-0@@ -3235,6 +3235,7 @@ static bool iterate_mm_list(struct lru_gen_mm_walk *walk, struct mm_struct **ite do { mm_state->head = mm_state->head->next; if (mm_state->head == &mm_list->fifo) { + /* head is back at the fifo; keep tail for force_scan below */ WRITE_ONCE(mm_state->seq, mm_state->seq + 1); last = true; break;@@ -3263,6 +3264,18 @@ static bool iterate_mm_list(struct lru_gen_mm_walk *walk, struct mm_struct **itereturn last; }
Quoted 9 lines
mm/vmscan.c static bool iterate_mm_list(struct lru_gen_mm_walk *walk, struct mm_struct **ite · line 3271
Quoted 5 lines
mm/vmscan.c +5
-0+/* +* Advance mm_state->seq without walking the mm_list. The cursor must be +* dropped as well: iterate_mm_list() resumes from head, and resets the Bloom +* filter only when head == &mm_list->fifo. +*/ This looks like a good catch, while the function name here is a little bit confusing. From the code block inside, we can't see it's doing iteration, just a pair of pointer resetting and a
max_seqadvancing. I would rather not wrap up them if no good name is concluded. Personal opinion.
How about inc_mm_state_seq(), after inc_max_seq(), leaving the "why" to the comment:
/*
* The cursor must be dropped along with mm_state->seq:
* iterate_mm_list() resumes from head, and resets the Bloom filter
* only when head == &mm_list->fifo.
*/
static void inc_mm_state_seq(struct lru_gen_mm_state *mm_state)
If that still does not read well to you, I will drop the helper in v2 and keep the two lines open-coded.
Quoted 4 lines
Other than the nits,
Reviewed-by Baoquan He <[email protected]>
Thank you!
Best,
Hui
Quoted 42 lines
Quoted 42 lines
mm/vmscan.c +9
-8+static void skip_mm_iteration (struct lru_gen_mm_state *mm_state) +{ + mm_state->head =NULL ; + mm_state->tail =NULL ; +WRITE_ONCE (mm_state->seq, mm_state->seq +1 ); +} +static bool iterate_mm_list_nowalk (struct lruvec *lruvec,unsigned long seq) {bool success = false;@@ -3275,9 +3288,7 @@ static bool iterate_mm_list_nowalk(struct lruvec *lruvec, unsigned long seq)VM_WARN_ON_ONCE (mm_state->seq +1 < seq);if (seq > mm_state->seq) { - mm_state->head =NULL ; - mm_state->tail =NULL ; -WRITE_ONCE (mm_state->seq, mm_state->seq +1 ); +skip_mm_iteration (mm_state); success = true; }@@ -4686,15 +4697,11 @@ static void try_to_inc_max_seq_nowalk(struct mem_cgroup *memcg,DEFINE_MAX_SEQ (lruvec);bool success = false; -/* -* We are not iterating the mm_list here, updating mm_state->seq is just -* to make mm walkers work properly. -*/ if (mm_state) {spin_lock (&mm_list->lock);VM_WARN_ON_ONCE (mm_state->seq +1 < max_seq);if (max_seq > mm_state->seq) { -WRITE_ONCE (mm_state->seq, mm_state->seq +1 ); +skip_mm_iteration (mm_state); success = true; }spin_unlock (&mm_list->lock);Signature · 1 line
2.53.0
Baoquan He
reviewerQuoted 5 lines · Hui Zhu
2026/11/19 15:29, Baoquan He:
Quoted 3 lines · Hui Zhu
Quoted 2 lines
From: Hui Zhu <[email protected]>
...snip...
Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
mm/vmscan.c +1
-0@@ -3263,6 +3264,18 @@ static bool iterate_mm_list(struct lru_gen_mm_walk *walk, struct mm_struct **itereturn last; } +/* Quoted 8 lines
mm/vmscan.c static bool iterate_mm_list(struct lru_gen_mm_walk *walk, struct mm_struct **ite · line 3271
Quoted 4 lines
mm/vmscan.c +4
-0+* Advance mm_state->seq without walking the mm_list. The cursor must be +* dropped as well: iterate_mm_list() resumes from head, and resets the Bloom +* filter only when head == &mm_list->fifo. +*/ This looks like a good catch, while the function name here is a little bit confusing. From the code block inside, we can't see it's doing iteration, just a pair of pointer resetting and a
max_seqadvancing. I would rather not wrap up them if no good name is concluded. Personal opinion.How about
inc_mm_state_seq(), afterinc_max_seq(), leaving the "why" to the comment:/*
* The cursor must be dropped along withmm_state->seq:
*iterate_mm_list()resumes from head, and resets the Bloom filter
* only when head ==&mm_list->fifo.
*/
static voidinc_mm_state_seq(struct lru_gen_mm_state *mm_state)
Quoted 2 lines
If that still does not read well to you, I will drop the helper in v2 and keep the two lines open-coded.
I feel like not wrapping them up is a little better than wrapping in this case, while it's purely personal flavor. I would like to hear other reviewers' opinion or it's up to you. Thanks.
Quoted 49 lines
Quoted 4 lines
Other than the nits,
Reviewed-by Baoquan He <[email protected]>Thank you!
Best,
HuiQuoted 39 lines
Quoted 39 lines
mm/vmscan.c +9
-8+static void skip_mm_iteration (struct lru_gen_mm_state *mm_state) +{ + mm_state->head =NULL ; + mm_state->tail =NULL ; +WRITE_ONCE (mm_state->seq, mm_state->seq +1 ); +} +static bool iterate_mm_list_nowalk (struct lruvec *lruvec,unsigned long seq) {bool success = false;@@ -3275,9 +3288,7 @@ static bool iterate_mm_list_nowalk(struct lruvec *lruvec, unsigned long seq)VM_WARN_ON_ONCE (mm_state->seq +1 < seq);if (seq > mm_state->seq) { - mm_state->head =NULL ; - mm_state->tail =NULL ; -WRITE_ONCE (mm_state->seq, mm_state->seq +1 ); +skip_mm_iteration (mm_state); success = true; }@@ -4686,15 +4697,11 @@ static void try_to_inc_max_seq_nowalk(struct mem_cgroup *memcg,DEFINE_MAX_SEQ (lruvec);bool success = false; -/* -* We are not iterating the mm_list here, updating mm_state->seq is just -* to make mm walkers work properly. -*/ if (mm_state) {spin_lock (&mm_list->lock);VM_WARN_ON_ONCE (mm_state->seq +1 < max_seq);if (max_seq > mm_state->seq) { -WRITE_ONCE (mm_state->seq, mm_state->seq +1 ); +skip_mm_iteration (mm_state); success = true; }spin_unlock (&mm_list->lock);Signature · 1 line
2.53.0