mm: mglru: fix stale batch updates after memcg reparenting
Subsystems (4)
Qi Zheng
reviewerPATCH v3 1/1 mm: mglru: fix stale batch updates after memcg reparenting
From: Qi Zheng <[email protected]>
The mglru page table walker batches per-generation size deltas in walk->nr_pages while walking page tables without holding the lruvec lock. The reset_batch_size() later folds those deltas into walk->lruvec under the lruvec lock.
The page table walker can run concurrently with the memcg reparenting path as follows:
CPU0 CPU1
==== ====
walk_mm
--> walk_page_range
--> update_batch_size
--> walk->nr_pages += delta
mem_cgroup_css_offline
--> memcg_reparent_objcgs
--> lock lruvec
lru_gen_reparent_memcg
--> reparent child folios to parent
unlock lruvec
lock lruvec
reset_batch_size
--> child lrugen->nr_pages += deltaThis will trigger the following warning in lru_gen_exit_memcg():
VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0, sizeof(lruvec->lrugen.nr_pages)));
And the user-visible impact of underestimated nr_pages in MGLRU was premature OOMs because MGLRU does not try to reclaim memory when nr_pages reaches zero, but there are still more pages.
To fix it, make reset_batch_size() check CSS_DYING under RCU before flushing the pending batch. A non-dying memcg keeps the original lruvec stable against RCU-delayed offlining; a dying memcg redirects the deltas to the first non-dying ancestor.
---
Changes in v3:
- re-implement lock_batch_lruvec() by checking CSS_DYING under the RCU lock (suggested by Harry)
- update the commit message (suggested by Harry)
- temporarily drop the previous Reviewed-by tags
(since the sync method has changed)
- rebase onto the next-20260624
Changes in v2:
- update the commit message (pointed by Barry)
- collect Reviewed-by
mm/vmscan.c | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-)
mm/vmscan.c +38 -7
@@ -3262,10 +3262,44 @@ static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, walk->nr_pages[new_gen][type][zone] += delta; } +#ifdef CONFIG_MEMCG +static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec) +{ +struct pglist_data *pgdat =lruvec_pgdat (lruvec); +struct mem_cgroup *memcg =lruvec_memcg (lruvec); + +rcu_read_lock (); + +/* +* The memcg can be NULL when the memory controller is disabled. +* Otherwise, the caller keeps the memcg owning @lruvec alive. +*/ +if (!memcg || !css_is_dying (&memcg->css)) + goto lock; + +do { + memcg =parent_mem_cgroup (memcg); + }while (memcg &&css_is_dying (&memcg->css)); + lruvec =mem_cgroup_lruvec (memcg, pgdat); + +lock: +spin_lock_irq (&lruvec->lru_lock); + +return lruvec; +} +#else +static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec) +{ +lruvec_lock_irq (lruvec); + +return lruvec; +} +#endif +static void reset_batch_size (struct lru_gen_mm_walk *walk) {int gen, type, zone; -struct lruvec *lruvec = walk->lruvec; +struct lruvec *lruvec =lock_batch_lruvec (walk->lruvec);struct lru_gen_folio *lrugen = &lruvec->lrugen; walk->batched =0 ;
@@ -3285,6 +3319,8 @@ static void reset_batch_size(struct lru_gen_mm_walk *walk) lru +=LRU_ACTIVE ;__update_lru_size (lruvec, lru, zone, delta); } + +lruvec_unlock_irq (lruvec); }static int should_skip_vma (unsigned long start,unsigned long end,struct mm_walk *args)
@@ -3779,11 +3815,8 @@ static void walk_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk)mmap_read_unlock (mm); } -if (walk->batched) { -lruvec_lock_irq (lruvec); +if (walk->batched)reset_batch_size (walk); -lruvec_unlock_irq (lruvec); - }cond_resched (); }while (err == -EAGAIN );
@@ -4867,9 +4900,7 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec, walk = current->reclaim_state->mm_walk;if (walk && walk->batched) { walk->lruvec = lruvec; -lruvec_lock_irq (lruvec);reset_batch_size (walk); -lruvec_unlock_irq (lruvec); } mod_lruvec_state(lruvec, PGDEMOTE_KSWAPD + reclaimer_offset(sc),
Signature · 1 line
2.54.0
Johannes Weiner
maintainerQuoted 69 lines · Qi Zheng
From: Qi Zheng <[email protected]>
The mglru page table walker batches per-generation size deltas in
walk->nr_pageswhile walking page tables without holding the lruvec lock. Thereset_batch_size()later folds those deltas intowalk->lruvecunder the lruvec lock.The page table walker can run concurrently with the memcg reparenting path as follows:
CPU0 CPU1
==== ====
walk_mm
-->walk_page_range--> update_batch_size --> walk->nr_pages += delta mem_cgroup_css_offline --> memcg_reparent_objcgs --> lock lruvec lru_gen_reparent_memcg --> reparent child folios to parent unlock lruvec lock lruvec reset_batch_size --> child lrugen->nr_pages += deltaThis will trigger the following warning in
lru_gen_exit_memcg():VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0, sizeof(lruvec->lrugen.nr_pages)));And the user-visible impact of underestimated
nr_pagesin MGLRU was premature OOMs because MGLRU does not try to reclaim memory whennr_pagesreaches zero, but there are still more pages.To fix it, make
reset_batch_size()checkCSS_DYINGunder RCU before flushing the pending batch. A non-dying memcg keeps the original lruvec stable against RCU-delayed offlining; a dying memcg redirects the deltas to the first non-dying ancestor.Reported-by Peiyang He <[email protected]>Closes https://lore.kernel.org/all/5A9E929D82717101+12fcf643-efb8-4b9a-a53a-1e28cc894f0b@smail.nju.edu.cn open hereFixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU folios")Cc <[email protected]>Signed-off-by Qi Zheng <[email protected]>---
Changes in v3:
- re-implementlock_batch_lruvec()by checkingCSS_DYINGunder the RCU lock (suggested by Harry)
- update the commit message (suggested by Harry)
- temporarily drop the previous Reviewed-by tags
(since the sync method has changed)
- rebase onto the next-20260624Changes in v2:
- update the commit message (pointed by Barry)
- collect Reviewed-bymm/vmscan.c | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-)mm/vmscan.c +0
-0
mm/vmscan.c static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, · line 3271
Quoted 10 lines
mm/vmscan.c +7
-0@@ -3262,10 +3262,44 @@ static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, walk->nr_pages[new_gen][type][zone] += delta; } +#ifdef CONFIG_MEMCG +static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec) +{ +struct pglist_data *pgdat =lruvec_pgdat (lruvec); +struct mem_cgroup *memcg =lruvec_memcg (lruvec); + +rcu_read_lock ();
Where is this unlocked?
mm/vmscan.c static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, · line 3283
Quoted 11 lines
mm/vmscan.c +11
-0+/* +* The memcg can be NULL when the memory controller is disabled. +* Otherwise, the caller keeps the memcg owning @lruvec alive. +*/ +if (!memcg || !css_is_dying (&memcg->css)) + goto lock; + +do { + memcg =parent_mem_cgroup (memcg); + }while (memcg &&css_is_dying (&memcg->css)); + lruvec =mem_cgroup_lruvec (memcg, pgdat);
while (unlikely(memcg && css_is_dying(&memcg->css))) {
memcg = parent_mem_cgroup(memcg);
lruvec = mem_cgroup_lruvec(memcg, pgdat);
}Shakeel Butt
reviewerQuoted 69 lines · Qi Zheng
From: Qi Zheng <[email protected]>
The mglru page table walker batches per-generation size deltas in
walk->nr_pageswhile walking page tables without holding the lruvec lock. Thereset_batch_size()later folds those deltas intowalk->lruvecunder the lruvec lock.The page table walker can run concurrently with the memcg reparenting path as follows:
CPU0 CPU1
==== ====
walk_mm
-->walk_page_range--> update_batch_size --> walk->nr_pages += delta mem_cgroup_css_offline --> memcg_reparent_objcgs --> lock lruvec lru_gen_reparent_memcg --> reparent child folios to parent unlock lruvec lock lruvec reset_batch_size --> child lrugen->nr_pages += deltaThis will trigger the following warning in
lru_gen_exit_memcg():VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0, sizeof(lruvec->lrugen.nr_pages)));And the user-visible impact of underestimated
nr_pagesin MGLRU was premature OOMs because MGLRU does not try to reclaim memory whennr_pagesreaches zero, but there are still more pages.To fix it, make
reset_batch_size()checkCSS_DYINGunder RCU before flushing the pending batch. A non-dying memcg keeps the original lruvec stable against RCU-delayed offlining; a dying memcg redirects the deltas to the first non-dying ancestor.Reported-by Peiyang He <[email protected]>Closes https://lore.kernel.org/all/5A9E929D82717101+12fcf643-efb8-4b9a-a53a-1e28cc894f0b@smail.nju.edu.cn open hereFixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU folios")Cc <[email protected]>Signed-off-by Qi Zheng <[email protected]>---
Changes in v3:
- re-implementlock_batch_lruvec()by checkingCSS_DYINGunder the RCU lock (suggested by Harry)
- update the commit message (suggested by Harry)
- temporarily drop the previous Reviewed-by tags
(since the sync method has changed)
- rebase onto the next-20260624Changes in v2:
- update the commit message (pointed by Barry)
- collect Reviewed-bymm/vmscan.c | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-)mm/vmscan.c +0
-0
mm/vmscan.c static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, · line 3266
Quoted 5 lines
mm/vmscan.c +2
-0@@ -3262,10 +3262,44 @@ static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, walk->nr_pages[new_gen][type][zone] += delta; } +#ifdef CONFIG_MEMCG +static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec)
This is memcg specific function, move this function next to similar functions like lruvec_lock_irq. Also put irq in the name.
BTW have you checked other places where lruvec_lock_irq is used and if similar kind of situation can happen?
Qi Zheng
reviewerHi Johannes,
Quoted 73 lines · Johannes Weiner
Quoted 72 lines · Qi Zheng
From: Qi Zheng <[email protected]>
The mglru page table walker batches per-generation size deltas in
walk->nr_pageswhile walking page tables without holding the lruvec lock. Thereset_batch_size()later folds those deltas intowalk->lruvecunder the lruvec lock.The page table walker can run concurrently with the memcg reparenting path as follows:
CPU0 CPU1
==== ====
walk_mm
-->walk_page_range--> update_batch_size --> walk->nr_pages += delta mem_cgroup_css_offline --> memcg_reparent_objcgs --> lock lruvec lru_gen_reparent_memcg --> reparent child folios to parent unlock lruvec lock lruvec reset_batch_size --> child lrugen->nr_pages += deltaThis will trigger the following warning in
lru_gen_exit_memcg():VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0, sizeof(lruvec->lrugen.nr_pages)));And the user-visible impact of underestimated
nr_pagesin MGLRU was premature OOMs because MGLRU does not try to reclaim memory whennr_pagesreaches zero, but there are still more pages.To fix it, make
reset_batch_size()checkCSS_DYINGunder RCU before flushing the pending batch. A non-dying memcg keeps the original lruvec stable against RCU-delayed offlining; a dying memcg redirects the deltas to the first non-dying ancestor.Reported-by Peiyang He <[email protected]>Closes https://lore.kernel.org/all/5A9E929D82717101+12fcf643-efb8-4b9a-a53a-1e28cc894f0b@smail.nju.edu.cn open hereFixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU folios")Cc <[email protected]>Signed-off-by Qi Zheng <[email protected]>---
Changes in v3:
- re-implementlock_batch_lruvec()by checkingCSS_DYINGunder the RCU lock (suggested by Harry)
- update the commit message (suggested by Harry)
- temporarily drop the previous Reviewed-by tags
(since the sync method has changed)
- rebase onto the next-20260624Changes in v2:
- update the commit message (pointed by Barry)
- collect Reviewed-bymm/vmscan.c | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-)mm/vmscan.c +0
-0@@ -3262,10 +3262,44 @@ static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, walk->nr_pages[new_gen][type][zone] += delta; }
Quoted 9 lines
mm/vmscan.c static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, · line 3271
Quoted 7 lines
mm/vmscan.c +7
-0+#ifdef CONFIG_MEMCG +static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec) +{ +struct pglist_data *pgdat =lruvec_pgdat (lruvec); +struct mem_cgroup *memcg =lruvec_memcg (lruvec); + +rcu_read_lock ();Where is this unlocked?
The lruvec_unlock_irq() in reset_batch_size() will handle the unlocking.
Quoted 16 lines
mm/vmscan.c static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, · line 3283
Quoted 12 lines
mm/vmscan.c +11
-0+/* +* The memcg can be NULL when the memory controller is disabled. +* Otherwise, the caller keeps the memcg owning @lruvec alive. +*/ +if (!memcg || !css_is_dying (&memcg->css)) + goto lock; + +do { + memcg =parent_mem_cgroup (memcg); + }while (memcg &&css_is_dying (&memcg->css)); + lruvec =mem_cgroup_lruvec (memcg, pgdat);while (unlikely(memcg && css_is_dying(&memcg->css))) { memcg = parent_mem_cgroup(memcg); lruvec = mem_cgroup_lruvec(memcg, pgdat);
There is no need to acquire the lruvec before finding the first non-dying memcg.
Thanks,
Qi
Quoted 1 line
}
Qi Zheng
reviewerHi Shakeel,
Quoted 73 lines · Shakeel Butt
Quoted 72 lines · Qi Zheng
From: Qi Zheng <[email protected]>
The mglru page table walker batches per-generation size deltas in
walk->nr_pageswhile walking page tables without holding the lruvec lock. Thereset_batch_size()later folds those deltas intowalk->lruvecunder the lruvec lock.The page table walker can run concurrently with the memcg reparenting path as follows:
CPU0 CPU1
==== ====
walk_mm
-->walk_page_range--> update_batch_size --> walk->nr_pages += delta mem_cgroup_css_offline --> memcg_reparent_objcgs --> lock lruvec lru_gen_reparent_memcg --> reparent child folios to parent unlock lruvec lock lruvec reset_batch_size --> child lrugen->nr_pages += deltaThis will trigger the following warning in
lru_gen_exit_memcg():VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0, sizeof(lruvec->lrugen.nr_pages)));And the user-visible impact of underestimated
nr_pagesin MGLRU was premature OOMs because MGLRU does not try to reclaim memory whennr_pagesreaches zero, but there are still more pages.To fix it, make
reset_batch_size()checkCSS_DYINGunder RCU before flushing the pending batch. A non-dying memcg keeps the original lruvec stable against RCU-delayed offlining; a dying memcg redirects the deltas to the first non-dying ancestor.Reported-by Peiyang He <[email protected]>Closes https://lore.kernel.org/all/5A9E929D82717101+12fcf643-efb8-4b9a-a53a-1e28cc894f0b@smail.nju.edu.cn open hereFixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU folios")Cc <[email protected]>Signed-off-by Qi Zheng <[email protected]>---
Changes in v3:
- re-implementlock_batch_lruvec()by checkingCSS_DYINGunder the RCU lock (suggested by Harry)
- update the commit message (suggested by Harry)
- temporarily drop the previous Reviewed-by tags
(since the sync method has changed)
- rebase onto the next-20260624Changes in v2:
- update the commit message (pointed by Barry)
- collect Reviewed-bymm/vmscan.c | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-)mm/vmscan.c +0
-0@@ -3262,10 +3262,44 @@ static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, walk->nr_pages[new_gen][type][zone] += delta; }
Quoted 5 lines
mm/vmscan.c static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, · line 3266
Quoted 2 lines
mm/vmscan.c +2
-0+#ifdef CONFIG_MEMCG +static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec)This is memcg specific function, move this function next to similar functions like
lruvec_lock_irq. Also put irq in the name.
Currently, the lock_batch_lruvec() is only used by reset_batch_size(). Are you intend to make it a common function for use in other places?
Perhaps we could defer making it generic until we actually have a second user. Since this is just a fix, keeping it self-contained within vmscan.c might be more compact for now. ;)
Quoted 3 lines
BTW have you checked other places where
lruvec_lock_irqis used and if similar kind of situation can happen?
I just checked and found no such callers.
Thanks,
Qi
Quoted 1 line
Hi Qi,
I have applied this patch and tested it against the PoC, the warning is not observed anymore in the kernel log. Thanks for your nice work!
Best,
Peiyang
Quoted 146 lines · Qi Zheng
From: Qi Zheng <[email protected]>
The mglru page table walker batches per-generation size deltas in
walk->nr_pageswhile walking page tables without holding the lruvec lock. Thereset_batch_size()later folds those deltas intowalk->lruvecunder the lruvec lock.The page table walker can run concurrently with the memcg reparenting path as follows:
CPU0 CPU1
==== ====
walk_mm
-->walk_page_range--> update_batch_size --> walk->nr_pages += delta mem_cgroup_css_offline --> memcg_reparent_objcgs --> lock lruvec lru_gen_reparent_memcg --> reparent child folios to parent unlock lruvec lock lruvec reset_batch_size --> child lrugen->nr_pages += deltaThis will trigger the following warning in
lru_gen_exit_memcg():VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0, sizeof(lruvec->lrugen.nr_pages)));And the user-visible impact of underestimated
nr_pagesin MGLRU was premature OOMs because MGLRU does not try to reclaim memory whennr_pagesreaches zero, but there are still more pages.To fix it, make
reset_batch_size()checkCSS_DYINGunder RCU before flushing the pending batch. A non-dying memcg keeps the original lruvec stable against RCU-delayed offlining; a dying memcg redirects the deltas to the first non-dying ancestor.Reported-by Peiyang He <[email protected]>Closes https://lore.kernel.org/all/5A9E929D82717101+12fcf643-efb8-4b9a-a53a-1e28cc894f0b@smail.nju.edu.cn open hereFixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU folios")Cc <[email protected]>Signed-off-by Qi Zheng <[email protected]>---
Changes in v3:
- re-implementlock_batch_lruvec()by checkingCSS_DYINGunder the RCU lock (suggested by Harry)
- update the commit message (suggested by Harry)
- temporarily drop the previous Reviewed-by tags
(since the sync method has changed)
- rebase onto the next-20260624Changes in v2:
- update the commit message (pointed by Barry)
- collect Reviewed-bymm/vmscan.c | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-)mm/vmscan.c +38
-7@@ -3262,10 +3262,44 @@ static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, walk->nr_pages[new_gen][type][zone] += delta; } +#ifdef CONFIG_MEMCG +static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec) +{ +struct pglist_data *pgdat =lruvec_pgdat (lruvec); +struct mem_cgroup *memcg =lruvec_memcg (lruvec); + +rcu_read_lock (); + +/* +* The memcg can be NULL when the memory controller is disabled. +* Otherwise, the caller keeps the memcg owning @lruvec alive. +*/ +if (!memcg || !css_is_dying (&memcg->css)) + goto lock; + +do { + memcg =parent_mem_cgroup (memcg); + }while (memcg &&css_is_dying (&memcg->css)); + lruvec =mem_cgroup_lruvec (memcg, pgdat); + +lock: +spin_lock_irq (&lruvec->lru_lock); + +return lruvec; +} +#else +static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec) +{ +lruvec_lock_irq (lruvec); + +return lruvec; +} +#endif +static void reset_batch_size (struct lru_gen_mm_walk *walk) {int gen, type, zone; -struct lruvec *lruvec = walk->lruvec; +struct lruvec *lruvec =lock_batch_lruvec (walk->lruvec);struct lru_gen_folio *lrugen = &lruvec->lrugen; walk->batched =0 ;@@ -3285,6 +3319,8 @@ static void reset_batch_size(struct lru_gen_mm_walk *walk) lru +=LRU_ACTIVE ;__update_lru_size (lruvec, lru, zone, delta); } + +lruvec_unlock_irq (lruvec); }static int should_skip_vma (unsigned long start,unsigned long end,struct mm_walk *args)@@ -3779,11 +3815,8 @@ static void walk_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk)mmap_read_unlock (mm); } -if (walk->batched) { -lruvec_lock_irq (lruvec); +if (walk->batched)reset_batch_size (walk); -lruvec_unlock_irq (lruvec); - }cond_resched (); }while (err == -EAGAIN );@@ -4867,9 +4900,7 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec, walk = current->reclaim_state->mm_walk;if (walk && walk->batched) { walk->lruvec = lruvec; -lruvec_lock_irq (lruvec);reset_batch_size (walk); -lruvec_unlock_irq (lruvec); } mod_lruvec_state(lruvec, PGDEMOTE_KSWAPD + reclaimer_offset(sc),
Quoted 107 lines · Qi Zheng
Hi Johannes,
Quoted 80 lines · Johannes Weiner
Quoted 79 lines · Qi Zheng
From: Qi Zheng <[email protected]>
The mglru page table walker batches per-generation size deltas in
walk->nr_pageswhile walking page tables without holding the lruvec lock.
Thereset_batch_size()later folds those deltas intowalk->lruvecunder the lruvec lock.The page table walker can run concurrently with the memcg reparenting path
as follows:CPU0 CPU1
==== ====
walk_mm
-->walk_page_range
-->update_batch_size
-->walk->nr_pages+= delta
mem_cgroup_css_offline-->memcg_reparent_objcgs--> lock lruveclru_gen_reparent_memcg--> reparent child folios to parent
unlock lruveclock lruvec
reset_batch_size
--> childlrugen->nr_pages+= deltaThis will trigger the following warning in
lru_gen_exit_memcg():VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0,
sizeof(lruvec->lrugen.nr_pages)));And the user-visible impact of underestimated
nr_pagesin MGLRU was premature OOMs because MGLRU does not try to reclaim memory whennr_pages
reaches zero, but there are still more pages.To fix it, make
reset_batch_size()checkCSS_DYINGunder RCU before flushing the pending batch. A non-dying memcg keeps the original lruvec stable against RCU-delayed offlining; a dying memcg redirects the deltas to the first non-dying ancestor.Reported-by Peiyang He <[email protected]>Fixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU folios")Cc <[email protected]>Signed-off-by Qi Zheng <[email protected]>---
Changes in v3:
- re-implementlock_batch_lruvec()by checkingCSS_DYINGunder the RCU lock
(suggested by Harry)
- update the commit message (suggested by Harry)
- temporarily drop the previous Reviewed-by tags
(since the sync method has changed)
- rebase onto the next-20260624Changes in v2:
- update the commit message (pointed by Barry)
- collect Reviewed-by
mm/vmscan.c| 45 ++++++++++++++++++++++++++++++++++++++-------1 file changed, 38 insertions(+), 7 deletions(-)mm/vmscan.c +0
-0@@ -3262,10 +3262,44 @@ static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, walk->nr_pages[new_gen][type][zone] += delta; }+#ifdef
CONFIG_MEMCGQuoted 8 lines
mm/vmscan.c static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, · line 3271
Quoted 6 lines
mm/vmscan.c +6
-0+static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec) +{ + struct pglist_data *pgdat = lruvec_pgdat(lruvec); + struct mem_cgroup *memcg = lruvec_memcg(lruvec); + + rcu_read_lock();Where is this unlocked?
The
lruvec_unlock_irq()inreset_batch_size()will handle the unlocking.Quoted 13 lines
Quoted 12 lines
+ /*
+ * The memcg can be NULL when the memory controller is disabled. + * Otherwise, the caller keeps the memcg owning @lruvec alive. + */
+ if (!memcg || !css_is_dying(&memcg->css))
+ goto lock;
+
+ do {
+ memcg =parent_mem_cgroup(memcg);
+ } while (memcg &&css_is_dying(&memcg->css));
+ lruvec =mem_cgroup_lruvec(memcg, pgdat);
Quoted 6 lines
Quoted 3 lines
while (
unlikely(memcg && css_is_dying(&memcg->css))) { memcg =parent_mem_cgroup(memcg);
lruvec =mem_cgroup_lruvec(memcg, pgdat);There is no need to acquire the lruvec before finding the first non-dying memcg.
struct pglist_data *pgdat = lruvec_pgdat(lruvec);
struct mem_cgroup *memcg = lruvec_memcg(lruvec);
rcu_read_lock()
while (unlikely(memcg_is_dying(memcg)))
memcg = parent_mem_cgroup(memcg);
lruvec = mem_cgroup_lruvec(memcg, pgdat);spin_lock_irq(&lruvec->lru_lock);
return lruvec;
should work?
if the memory controller is disabled, it's equivalent to:
rcu_read_lock();spin_lock_irq(&lruvec->lru_lock);
return lruvec;
Signature · 2 lines
Cheers, Harry / Hyeonggon
Qi Zheng
reviewerQuoted 120 lines · Harry Yoo
Quoted 107 lines · Qi Zheng
Hi Johannes,
Quoted 80 lines · Johannes Weiner
Quoted 79 lines · Qi Zheng
From: Qi Zheng <[email protected]>
The mglru page table walker batches per-generation size deltas in
walk->nr_pageswhile walking page tables without holding the lruvec lock.
Thereset_batch_size()later folds those deltas intowalk->lruvecunder the lruvec lock.The page table walker can run concurrently with the memcg reparenting path
as follows:CPU0 CPU1
==== ====
walk_mm
-->walk_page_range
-->update_batch_size
-->walk->nr_pages+= delta
mem_cgroup_css_offline-->memcg_reparent_objcgs--> lock lruveclru_gen_reparent_memcg--> reparent child folios to
parent
unlock lruveclock lruvec
reset_batch_size
--> childlrugen->nr_pages+= deltaThis will trigger the following warning in
lru_gen_exit_memcg():VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0,
sizeof(lruvec->lrugen.nr_pages)));And the user-visible impact of underestimated
nr_pagesin MGLRU was premature OOMs because MGLRU does not try to reclaim memory whennr_pages
reaches zero, but there are still more pages.To fix it, make
reset_batch_size()checkCSS_DYINGunder RCU before flushing the pending batch. A non-dying memcg keeps the original lruvec stable against RCU-delayed offlining; a dying memcg redirects the deltas to the first non-dying ancestor.Reported-by Peiyang He <[email protected]>Fixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU folios")Cc <[email protected]>Signed-off-by Qi Zheng <[email protected]>---
Changes in v3:
- re-implementlock_batch_lruvec()by checkingCSS_DYINGunder the
RCU lock
(suggested by Harry)
- update the commit message (suggested by Harry)
- temporarily drop the previous Reviewed-by tags
(since the sync method has changed)
- rebase onto the next-20260624Changes in v2:
- update the commit message (pointed by Barry)
- collect Reviewed-bymm/vmscan.c | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-)mm/vmscan.c +0
-0@@ -3262,10 +3262,44 @@ static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, walk->nr_pages[new_gen][type][zone] += delta; } +#ifdef CONFIG_MEMCGQuoted 8 lines
mm/vmscan.c static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, · line 3271
Quoted 6 lines
mm/vmscan.c +6
-0+static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec) +{ + struct pglist_data *pgdat = lruvec_pgdat(lruvec); + struct mem_cgroup *memcg = lruvec_memcg(lruvec); + + rcu_read_lock();Where is this unlocked?
The
lruvec_unlock_irq()inreset_batch_size()will handle the unlocking.Quoted 13 lines
Quoted 12 lines
mm/vmscan.c +11
-0+ /* + * The memcg can be NULL when the memory controller is disabled. + * Otherwise, the caller keeps the memcg owning @lruvec alive. + */ + if (!memcg || !css_is_dying(&memcg->css)) + goto lock; + + do { + memcg = parent_mem_cgroup(memcg); + } while (memcg && css_is_dying(&memcg->css)); + lruvec = mem_cgroup_lruvec(memcg, pgdat);Quoted 6 lines
Quoted 3 lines
while (
unlikely(memcg && css_is_dying(&memcg->css))) { memcg =parent_mem_cgroup(memcg);
lruvec =mem_cgroup_lruvec(memcg, pgdat);There is no need to acquire the lruvec before finding the first non-dying memcg.
struct
pglist_data*pgdat =lruvec_pgdat(lruvec);
structmem_cgroup*memcg =lruvec_memcg(lruvec);
Quoted 6 lines
rcu_read_lock()while (unlikely(memcg_is_dying(memcg)))
memcg = parent_mem_cgroup(memcg);lruvec =
mem_cgroup_lruvec(memcg, pgdat);
If the first memcg is already non-dying, there's no need to re-acquire the lruvec. ;)
Thanks,
Qi
Quoted 12 lines
spin_lock_irq(&lruvec->lru_lock);return lruvec;
should work?
if the memory controller is disabled, it's equivalent to:
rcu_read_lock();spin_lock_irq(&lruvec->lru_lock);
return lruvec;
Qi Zheng
reviewerHi Peiyang,
Quoted 4 lines · Peiyang He
Hi Qi,
I have applied this patch and tested it against the PoC, the warning is not observed anymore in the kernel log. Thanks for your nice work!
Thanks a ton for testing this!
Quoted 4 lines
Best,
Peiyang
Quoted 129 lines · Qi Zheng
Quoted 126 lines · Harry Yoo
Quoted 111 lines · Qi Zheng
Hi Johannes,
Quoted 84 lines · Johannes Weiner
Quoted 83 lines · Qi Zheng
From: Qi Zheng <[email protected]>
The mglru page table walker batches per-generation size deltas in
walk->nr_pageswhile walking page tables without holding the lruvec lock.
Thereset_batch_size()later folds those deltas intowalk->lruvecunder
the lruvec lock.The page table walker can run concurrently with the memcg reparenting path
as follows:CPU0 CPU1
==== ====
walk_mm
-->walk_page_range--> update_batch_size --> walk->nr_pages += delta mem_cgroup_css_offline --> memcg_reparent_objcgs --> lock lruvec lru_gen_reparent_memcg --> reparent child folios to parent unlock lruvec lock lruvec reset_batch_size --> child lrugen->nr_pages += deltaThis will trigger the following warning in
lru_gen_exit_memcg():VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0, sizeof(lruvec->lrugen.nr_pages)));And the user-visible impact of underestimated
nr_pagesin MGLRU was premature OOMs because MGLRU does not try to reclaim memory whennr_pages
reaches zero, but there are still more pages.To fix it, make
reset_batch_size()checkCSS_DYINGunder RCU before flushing the pending batch. A non-dying memcg keeps the original lruvec
stable against RCU-delayed offlining; a dying memcg redirects the deltas
to the first non-dying ancestor.Reported-by Peiyang He <[email protected]>Fixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRUfolios")
Cc <[email protected]>Signed-off-by Qi Zheng <[email protected]>---
Changes in v3:
- re-implementlock_batch_lruvec()by checkingCSS_DYINGunder the
RCU lock
(suggested by Harry)
- update the commit message (suggested by Harry)
- temporarily drop the previous Reviewed-by tags
(since the sync method has changed)
- rebase onto the next-20260624Changes in v2:
- update the commit message (pointed by Barry)
- collect Reviewed-bymm/vmscan.c | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-)mm/vmscan.c +0
-0@@ -3262,10 +3262,44 @@ static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, walk->nr_pages[new_gen][type][zone] += delta; } +#ifdef CONFIG_MEMCGQuoted 8 lines
mm/vmscan.c static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, · line 3271
Quoted 6 lines
mm/vmscan.c +6
-0+static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec) +{ +struct pglist_data *pgdat =lruvec_pgdat (lruvec); +struct mem_cgroup *memcg =lruvec_memcg (lruvec); + +rcu_read_lock ();Where is this unlocked?
The
lruvec_unlock_irq()inreset_batch_size()will handle the unlocking.Quoted 13 lines
Quoted 12 lines
mm/vmscan.c +11
-0+/* +* The memcg can be NULL when the memory controller is disabled. +* Otherwise, the caller keeps the memcg owning @lruvec alive. +*/ +if (!memcg || !css_is_dying (&memcg->css)) + goto lock; + +do { + memcg =parent_mem_cgroup (memcg); + }while (memcg &&css_is_dying (&memcg->css)); + lruvec =mem_cgroup_lruvec (memcg, pgdat);Quoted 6 lines
Quoted 3 lines
while (unlikely(memcg && css_is_dying(&memcg->css))) { memcg = parent_mem_cgroup(memcg); lruvec = mem_cgroup_lruvec(memcg, pgdat);There is no need to acquire the lruvec before finding the first non-dying memcg.
struct
pglist_data*pgdat =lruvec_pgdat(lruvec);
structmem_cgroup*memcg =lruvec_memcg(lruvec);
rcu_read_lock()
Quoted 7 lines
Quoted 4 lines
while (unlikely(memcg_is_dying(memcg)))
memcg = parent_mem_cgroup(memcg);lruvec =
mem_cgroup_lruvec(memcg, pgdat);If the first memcg is already non-dying, there's no need to re-acquire the lruvec. ;)
Oh, right :)
Hmm but I still think Johannes' suggestion makes the code cleaner. Observing a dying cgroup should be rare anyway, it's worth focusing more on readability?
Signature · 2 lines
Cheers, Harry / Hyeonggon
Qi Zheng
reviewerQuoted 135 lines · Harry Yoo
Quoted 132 lines · Qi Zheng
Quoted 129 lines · Harry Yoo
Quoted 111 lines · Qi Zheng
Hi Johannes,
Quoted 84 lines · Johannes Weiner
Quoted 83 lines · Qi Zheng
From: Qi Zheng <[email protected]>
The mglru page table walker batches per-generation size deltas in
walk->nr_pageswhile walking page tables without holding the lruvec lock.
Thereset_batch_size()later folds those deltas intowalk->lruvecunder
the lruvec lock.The page table walker can run concurrently with the memcg reparenting path
as follows:CPU0 CPU1
==== ====
walk_mm
-->walk_page_range--> update_batch_size --> walk->nr_pages += delta mem_cgroup_css_offline --> memcg_reparent_objcgs --> lock lruvec lru_gen_reparent_memcg --> reparent child folios to parent unlock lruvec lock lruvec reset_batch_size --> child lrugen->nr_pages += deltaThis will trigger the following warning in
lru_gen_exit_memcg():VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0, sizeof(lruvec->lrugen.nr_pages)));And the user-visible impact of underestimated
nr_pagesin MGLRU was premature OOMs because MGLRU does not try to reclaim memory whennr_pages
reaches zero, but there are still more pages.To fix it, make
reset_batch_size()checkCSS_DYINGunder RCU before flushing the pending batch. A non-dying memcg keeps the original lruvec
stable against RCU-delayed offlining; a dying memcg redirects the deltas
to the first non-dying ancestor.Reported-by Peiyang He <[email protected]>Fixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRUfolios")
Cc <[email protected]>Signed-off-by Qi Zheng <[email protected]>---
Changes in v3:
- re-implementlock_batch_lruvec()by checkingCSS_DYINGunder the
RCU lock
(suggested by Harry)
- update the commit message (suggested by Harry)
- temporarily drop the previous Reviewed-by tags
(since the sync method has changed)
- rebase onto the next-20260624Changes in v2:
- update the commit message (pointed by Barry)
- collect Reviewed-by
mm/vmscan.c| 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-)mm/vmscan.c +0
-0@@ -3262,10 +3262,44 @@ static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, walk->nr_pages[new_gen][type][zone] += delta; } +#ifdef CONFIG_MEMCGQuoted 8 lines
mm/vmscan.c static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, · line 3271
Quoted 6 lines
mm/vmscan.c +6
-0+static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec) +{ +struct pglist_data *pgdat =lruvec_pgdat (lruvec); +struct mem_cgroup *memcg =lruvec_memcg (lruvec); + +rcu_read_lock ();Where is this unlocked?
The
lruvec_unlock_irq()inreset_batch_size()will handle the unlocking.Quoted 13 lines
Quoted 12 lines
mm/vmscan.c +11
-0+/* +* The memcg can be NULL when the memory controller is disabled. +* Otherwise, the caller keeps the memcg owning @lruvec alive. +*/ +if (!memcg || !css_is_dying (&memcg->css)) + goto lock; + +do { + memcg =parent_mem_cgroup (memcg); + }while (memcg &&css_is_dying (&memcg->css)); + lruvec =mem_cgroup_lruvec (memcg, pgdat);Quoted 6 lines
Quoted 3 lines
while (unlikely(memcg && css_is_dying(&memcg->css))) { memcg = parent_mem_cgroup(memcg); lruvec = mem_cgroup_lruvec(memcg, pgdat);There is no need to acquire the lruvec before finding the first non-dying memcg.
struct
pglist_data*pgdat =lruvec_pgdat(lruvec);
structmem_cgroup*memcg =lruvec_memcg(lruvec);
rcu_read_lock()while (unlikely(memcg_is_dying(memcg)))
memcg = parent_mem_cgroup(memcg);
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
lruvec =
mem_cgroup_lruvec(memcg, pgdat);If the first memcg is already non-dying, there's no need to re-acquire the lruvec. ;)
Oh, right :)
Hmm but I still think Johannes' suggestion makes the code cleaner.
I don't have a strong preference on which of the two coding styles is more readable. BTW, is there any kernel documentation I could refer to for this?
Quoted 2 lines
Observing a dying cgroup should be rare anyway, it's worth focusing more on readability?
While it's rare to encounter consecutive dying memcgs, it can still happen, right?
Thanks,
Qi
Quoted 1 line
Quoted 148 lines · Qi Zheng
Quoted 145 lines · Harry Yoo
Quoted 141 lines · Qi Zheng
Quoted 129 lines · Harry Yoo
Quoted 116 lines · Qi Zheng
Hi Johannes,
Quoted 87 lines · Johannes Weiner
Quoted 86 lines · Qi Zheng
From: Qi Zheng <[email protected]>
The mglru page table walker batches per-generation size deltas in
walk->nr_pageswhile walking page tables without holding the lruvec lock.
Thereset_batch_size()later folds those deltas intowalk->lruvecunder
the lruvec lock.The page table walker can run concurrently with the memcg reparenting
path
as follows:CPU0 CPU1
==== ====
walk_mm
-->walk_page_range
-->update_batch_size
-->walk->nr_pages+= delta
mem_cgroup_css_offline-->memcg_reparent_objcgs--> lock lruveclru_gen_reparent_memcg--> reparent child folios to
parent
unlock lruveclock lruvec
reset_batch_size
--> childlrugen->nr_pages+= deltaThis will trigger the following warning in
lru_gen_exit_memcg():
VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0, sizeof(lruvec->lrugen.nr_pages)));And the user-visible impact of underestimated
nr_pagesin MGLRU was premature OOMs because MGLRU does not try to reclaim memory whennr_pages
reaches zero, but there are still more pages.To fix it, make
reset_batch_size()checkCSS_DYINGunder RCU before flushing the pending batch. A non-dying memcg keeps the original lruvec
stable against RCU-delayed offlining; a dying memcg redirects the deltas
to the first non-dying ancestor.Reported-by Peiyang He <[email protected]>Fixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRUfolios")
Cc <[email protected]>Signed-off-by Qi Zheng <[email protected]>---
Changes in v3:
- re-implementlock_batch_lruvec()by checkingCSS_DYINGunder the
RCU lock
(suggested by Harry)
- update the commit message (suggested by Harry)
- temporarily drop the previous Reviewed-by tags (since the sync method has changed)
- rebase onto the next-20260624Changes in v2:
- update the commit message (pointed by Barry)
- collect Reviewed-by
mm/vmscan.c| 45 ++++++++++++++++++++++++++++++++++++++-------1 file changed, 38 insertions(+), 7 deletions(-)mm/vmscan.c +0
-0@@ -3262,10 +3262,44 @@ static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, walk->nr_pages[new_gen][type][zone] += delta; }+#ifdef
CONFIG_MEMCGQuoted 8 lines
mm/vmscan.c
Quoted 6 lines
mm/vmscan.c +6
-0+static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec) +{ + struct pglist_data *pgdat = lruvec_pgdat(lruvec); + struct mem_cgroup *memcg = lruvec_memcg(lruvec); + + rcu_read_lock();Where is this unlocked?
The
lruvec_unlock_irq()inreset_batch_size()will handle the unlocking.Quoted 14 lines
Quoted 13 lines
+ /*
+ * The memcg can be NULL when the memory controller is disabled.
+ * Otherwise, the caller keeps the memcg owning @lruvec alive. + */
+ if (!memcg || !css_is_dying(&memcg->css))
+ goto lock;
+
+ do {
+ memcg =parent_mem_cgroup(memcg);
+ } while (memcg &&css_is_dying(&memcg->css));
+ lruvec =mem_cgroup_lruvec(memcg, pgdat);Quoted 6 lines
Quoted 3 lines
while (
unlikely(memcg && css_is_dying(&memcg->css))) { memcg =parent_mem_cgroup(memcg);
lruvec =mem_cgroup_lruvec(memcg, pgdat);There is no need to acquire the lruvec before finding the first non-dying memcg.
struct
pglist_data*pgdat =lruvec_pgdat(lruvec);
structmem_cgroup*memcg =lruvec_memcg(lruvec);Quoted 6 lines
rcu_read_lock()while (unlikely(memcg_is_dying(memcg)))
memcg =parent_mem_cgroup(memcg);lruvec =
mem_cgroup_lruvec(memcg, pgdat);If the first memcg is already non-dying, there's no need to re-acquire the lruvec. ;)
Quoted 7 lines
Quoted 3 lines
Oh, right :)
Hmm but I still think Johannes' suggestion makes the code cleaner.
I don't have a strong preference on which of the two coding styles is more readable. BTW, is there any kernel documentation I could refer to for this?
I don't think there's a coding style guide that specifically mentions this. Just thought it's cleaner because it merges if (...) goto lock; and do-while into a single while loop.
Quoted 5 lines
Quoted 2 lines
Observing a dying cgroup should be rare anyway, it's worth focusing more on readability?
While it's rare to encounter consecutive dying memcgs, it can still happen, right?
But is worth saving a few instruction in a basic block that is unlikely() to be executed?
I'm not a memcg maintainer myself, though. just my 2 cents.
Signature · 2 lines
Cheers, Harry / Hyeonggon
Qi Zheng
reviewerQuoted 163 lines · Harry Yoo
Quoted 148 lines · Qi Zheng
Quoted 145 lines · Harry Yoo
Quoted 141 lines · Qi Zheng
Quoted 129 lines · Harry Yoo
Quoted 116 lines · Qi Zheng
Hi Johannes,
Quoted 87 lines · Johannes Weiner
Quoted 86 lines · Qi Zheng
From: Qi Zheng <[email protected]>
The mglru page table walker batches per-generation size deltas in
walk->nr_pageswhile walking page tables without holding the lruvec lock.
Thereset_batch_size()later folds those deltas intowalk->lruvecunder
the lruvec lock.The page table walker can run concurrently with the memcg reparenting
path
as follows:CPU0 CPU1
==== ====
walk_mm
-->walk_page_range
-->update_batch_size
-->walk->nr_pages+= delta
mem_cgroup_css_offline-->memcg_reparent_objcgs--> lock lruveclru_gen_reparent_memcg--> reparent child
folios to
parent
unlock lruveclock lruvec
reset_batch_size
--> childlrugen->nr_pages+= deltaThis will trigger the following warning in
lru_gen_exit_memcg():
VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0, sizeof(lruvec->lrugen.nr_pages)));And the user-visible impact of underestimated
nr_pagesin MGLRU was premature OOMs because MGLRU does not try to reclaim memory whennr_pages
reaches zero, but there are still more pages.To fix it, make
reset_batch_size()checkCSS_DYINGunder RCU before flushing the pending batch. A non-dying memcg keeps the original lruvec
stable against RCU-delayed offlining; a dying memcg redirects the deltas
to the first non-dying ancestor.Reported-by Peiyang He <[email protected]>Fixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRUfolios")
Cc <[email protected]>Signed-off-by Qi Zheng <[email protected]>---
Changes in v3:
- re-implementlock_batch_lruvec()by checkingCSS_DYING
under the
RCU lock
(suggested by Harry)
- update the commit message (suggested by Harry)
- temporarily drop the previous Reviewed-by tags (since the sync method has changed)
- rebase onto the next-20260624Changes in v2:
- update the commit message (pointed by Barry)
- collect Reviewed-bymm/vmscan.c | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-)mm/vmscan.c +0
-0@@ -3262,10 +3262,44 @@ static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, walk->nr_pages[new_gen][type][zone] += delta; } +#ifdef CONFIG_MEMCGQuoted 8 lines
mm/vmscan.c static void update_batch_size(struct · line 3271
Quoted 6 lines
mm/vmscan.c +6
-0+static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec) +{ + struct pglist_data *pgdat = lruvec_pgdat(lruvec); + struct mem_cgroup *memcg = lruvec_memcg(lruvec); + + rcu_read_lock();Where is this unlocked?
The
lruvec_unlock_irq()inreset_batch_size()will handle the unlocking.Quoted 14 lines
Quoted 13 lines
mm/vmscan.c +2
-0+ /* +* The memcg can be NULL when the memory controller is disabled.
+ * Otherwise, the caller keeps the memcg owning @lruvec alive. + */
+ if (!memcg || !css_is_dying(&memcg->css))
+ goto lock;
+
+ do {
+ memcg =parent_mem_cgroup(memcg);
+ } while (memcg &&css_is_dying(&memcg->css));
+ lruvec =mem_cgroup_lruvec(memcg, pgdat);Quoted 6 lines
Quoted 3 lines
while (
unlikely(memcg && css_is_dying(&memcg->css))) { memcg =parent_mem_cgroup(memcg);
lruvec =mem_cgroup_lruvec(memcg, pgdat);There is no need to acquire the lruvec before finding the first non-dying memcg.
struct
pglist_data*pgdat =lruvec_pgdat(lruvec);
structmem_cgroup*memcg =lruvec_memcg(lruvec);Quoted 6 lines
rcu_read_lock()while (unlikely(memcg_is_dying(memcg)))
memcg =parent_mem_cgroup(memcg);lruvec =
mem_cgroup_lruvec(memcg, pgdat);If the first memcg is already non-dying, there's no need to re-acquire the lruvec. ;)
Quoted 7 lines
Quoted 3 lines
Oh, right :)
Hmm but I still think Johannes' suggestion makes the code cleaner.
I don't have a strong preference on which of the two coding styles is more readable. BTW, is there any kernel documentation I could refer to for this?
I don't think there's a coding style guide that specifically mentions this. Just thought it's cleaner because it merges if (...) goto lock; and do-while into a single while loop.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Observing a dying cgroup should be rare anyway, it's worth focusing more on readability?
While it's rare to encounter consecutive dying memcgs, it can still happen, right?
But is worth saving a few instruction in a basic block that is
unlikely()to be executed?
I don't have a strong opinion here. Hi Johannes, I'll leave the decision up to you. If necessary, I can send out the v4.
Quoted 2 lines
I'm not a memcg maintainer myself, though. just my 2 cents.
I'd like to express my gratitude for your reviews, and especially for your invaluable help with the earlier dying memcg work!
Thanks,
Qi
Quoted 1 line
Quoted 84 lines · Qi Zheng
Quoted 74 lines · Harry Yoo
Quoted 61 lines · Qi Zheng
Quoted 60 lines · Harry Yoo
Quoted 58 lines · Qi Zheng
Quoted 48 lines · Harry Yoo
Quoted 37 lines · Qi Zheng
Quoted 10 lines · Johannes Weiner
Quoted 9 lines · Qi Zheng
mm/vmscan.c +0
-0@@ -3262,10 +3262,44 @@ static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, walk->nr_pages[new_gen][type][zone] += delta; } +#ifdef CONFIG_MEMCGQuoted 8 lines
mm/vmscan.c static void update_batch_size(struct · line 3271
Quoted 6 lines
mm/vmscan.c +6
-0+static struct lruvec *lock_batch_lruvec (struct lruvec *lruvec) +{ +struct pglist_data *pgdat =lruvec_pgdat (lruvec); +struct mem_cgroup *memcg =lruvec_memcg (lruvec); + +rcu_read_lock ();Where is this unlocked?
The
lruvec_unlock_irq()inreset_batch_size()will handle the unlocking.Quoted 14 lines
Quoted 13 lines
mm/vmscan.c +2
-0+/* +* The memcg can be NULL when the memory controller is disabled.
+ * Otherwise, the caller keeps the memcg owning @lruvec
alive.mm/vmscan.c +8
-0+*/ +if (!memcg || !css_is_dying (&memcg->css)) + goto lock; + +do { + memcg =parent_mem_cgroup (memcg); + }while (memcg &&css_is_dying (&memcg->css)); + lruvec =mem_cgroup_lruvec (memcg, pgdat);Quoted 6 lines
Quoted 3 lines
while (unlikely(memcg && css_is_dying(&memcg->css))) { memcg = parent_mem_cgroup(memcg); lruvec = mem_cgroup_lruvec(memcg, pgdat);There is no need to acquire the lruvec before finding the first non-dying memcg.
struct
pglist_data*pgdat =lruvec_pgdat(lruvec);
structmem_cgroup*memcg =lruvec_memcg(lruvec);Quoted 6 lines
rcu_read_lock()while (unlikely(memcg_is_dying(memcg)))
memcg = parent_mem_cgroup(memcg);lruvec =
mem_cgroup_lruvec(memcg, pgdat);If the first memcg is already non-dying, there's no need to re-acquire the lruvec. ;)
Quoted 7 lines
Quoted 3 lines
Oh, right :)
Hmm but I still think Johannes' suggestion makes the code cleaner.
I don't have a strong preference on which of the two coding styles is more readable. BTW, is there any kernel documentation I could refer to for this?
I don't think there's a coding style guide that specifically mentions this. Just thought it's cleaner because it merges if (...) goto lock; and do-while into a single while loop.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Observing a dying cgroup should be rare anyway, it's worth focusing more on readability?
While it's rare to encounter consecutive dying memcgs, it can still happen, right?
But is worth saving a few instruction in a basic block that is
unlikely()to be executed?
Quoted 8 lines
I don't have a strong opinion here. Hi Johannes, I'll leave the decision up to you. If necessary, I can send out the v4.
Quoted 2 lines
I'm not a memcg maintainer myself, though. just my 2 cents.
I'd like to express my gratitude for your reviews, and especially for your invaluable help with the earlier dying memcg work!
No problem, likewise to you and Muchun for invaluable work ;)
Signature · 2 lines
Cheers, Harry / Hyeonggon
Quoted 43 lines · Qi Zheng
From: Qi Zheng <[email protected]>
The mglru page table walker batches per-generation size deltas in
walk->nr_pageswhile walking page tables without holding the lruvec lock. Thereset_batch_size()later folds those deltas intowalk->lruvecunder the lruvec lock.The page table walker can run concurrently with the memcg reparenting path as follows:
CPU0 CPU1
==== ====
walk_mm
-->walk_page_range--> update_batch_size --> walk->nr_pages += delta mem_cgroup_css_offline --> memcg_reparent_objcgs --> lock lruvec lru_gen_reparent_memcg --> reparent child folios to parent unlock lruvec lock lruvec reset_batch_size --> child lrugen->nr_pages += deltaThis will trigger the following warning in
lru_gen_exit_memcg():VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0, sizeof(lruvec->lrugen.nr_pages)));And the user-visible impact of underestimated
nr_pagesin MGLRU was premature OOMs because MGLRU does not try to reclaim memory whennr_pagesreaches zero, but there are still more pages.To fix it, make
reset_batch_size()checkCSS_DYINGunder RCU before flushing the pending batch. A non-dying memcg keeps the original lruvec stable against RCU-delayed offlining; a dying memcg redirects the deltas to the first non-dying ancestor.
Quoted 6 lines
Reported-by Peiyang He <[email protected]>Closes https://lore.kernel.org/all/5A9E929D82717101+12fcf643-efb8-4b9a-a53a-1e28cc894f0b@smail.nju.edu.cn open hereFixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU folios")Cc <[email protected]>Signed-off-by Qi Zheng <[email protected]>---
besides some nits mentioned elsewhere in the thread, the approach looks good to me, so:
Signature · 2 lines
Cheers, Harry / Hyeonggon
Johannes Weiner
maintainerQuoted 6 lines · Qi Zheng
Quoted 5 lines · Harry Yoo
Quoted 4 lines · Qi Zheng
Quoted 2 lines · Harry Yoo
Observing a dying cgroup should be rare anyway, it's worth focusing more on readability?
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
While it's rare to encounter consecutive dying memcgs, it can still happen, right?
But is worth saving a few instruction in a basic block that is
unlikely()to be executed?I don't have a strong opinion here. Hi Johannes, I'll leave the decision up to you. If necessary, I can send out the v4.
Yes, I was thinking what Harry actually bothered to spell out ;)
The race is rare, multiple levels even rarer, and even *then* mem_cgroup_lruvec() is a quick inline.
This way you have one block to handle that one rare race condition. One place to put the comment. No labels, no goto.
Simplicity wins :)
Qi Zheng
reviewerQuoted 18 lines · Johannes Weiner
Quoted 6 lines · Qi Zheng
Quoted 5 lines · Harry Yoo
Quoted 4 lines · Qi Zheng
Quoted 2 lines · Harry Yoo
Observing a dying cgroup should be rare anyway, it's worth focusing more on readability?
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
While it's rare to encounter consecutive dying memcgs, it can still happen, right?
But is worth saving a few instruction in a basic block that is
unlikely()to be executed?I don't have a strong opinion here. Hi Johannes, I'll leave the decision up to you. If necessary, I can send out the v4.
Yes, I was thinking what Harry actually bothered to spell out ;)
Quoted 7 lines
The race is rare, multiple levels even rarer, and even *then*
mem_cgroup_lruvec()is a quick inline.This way you have one block to handle that one rare race condition. One place to put the comment. No labels, no goto.
Simplicity wins :)
Okay, I will update it as you suggested and send out the v4.
Hi Shakeel, do we really need to move lock_batch_lruvec() to memcontrol.h? It's currently only used by reset_batch_size().
Thanks,
Qi
Shakeel Butt
reviewerQuoted 27 lines · Qi Zheng
Quoted 24 lines · Johannes Weiner
Quoted 6 lines · Qi Zheng
Quoted 5 lines · Harry Yoo
Quoted 4 lines · Qi Zheng
Quoted 2 lines · Harry Yoo
Observing a dying cgroup should be rare anyway, it's worth focusing more on readability?
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
While it's rare to encounter consecutive dying memcgs, it can still happen, right?
But is worth saving a few instruction in a basic block that is
unlikely()to be executed?I don't have a strong opinion here. Hi Johannes, I'll leave the decision up to you. If necessary, I can send out the v4.
Yes, I was thinking what Harry actually bothered to spell out ;)
The race is rare, multiple levels even rarer, and even *then*
mem_cgroup_lruvec()is a quick inline.This way you have one block to handle that one rare race condition. One place to put the comment. No labels, no goto.
Quoted 6 lines
Quoted 1 line
Simplicity wins :)
Okay, I will update it as you suggested and send out the v4.
Hi Shakeel, do we really need to move
lock_batch_lruvec()tomemcontrol.h? It's currently only used byreset_batch_size().
This function is very specific to memcg therefore I asked it move to memcontrol.h and not to keep in vmscan.c but we can always do the cleanup later, so proceed however you want.
Qi Zheng
reviewerQuoted 30 lines · Shakeel Butt
Quoted 29 lines · Qi Zheng
Quoted 25 lines · Johannes Weiner
Quoted 6 lines · Qi Zheng
Quoted 5 lines · Harry Yoo
Quoted 4 lines · Qi Zheng
Quoted 2 lines · Harry Yoo
Observing a dying cgroup should be rare anyway, it's worth focusing more on readability?
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
While it's rare to encounter consecutive dying memcgs, it can still happen, right?
But is worth saving a few instruction in a basic block that is
unlikely()to be executed?I don't have a strong opinion here. Hi Johannes, I'll leave the decision up to you. If necessary, I can send out the v4.
Yes, I was thinking what Harry actually bothered to spell out ;)
The race is rare, multiple levels even rarer, and even *then*
mem_cgroup_lruvec()is a quick inline.This way you have one block to handle that one rare race condition. One place to put the comment. No labels, no goto.
Simplicity wins :)
Quoted 8 lines
Quoted 4 lines
Okay, I will update it as you suggested and send out the v4.
Hi Shakeel, do we really need to move
lock_batch_lruvec()tomemcontrol.h? It's currently only used byreset_batch_size().This function is very specific to memcg therefore I asked it move to
memcontrol.hand not to keep invmscan.cbut we can always do the cleanup later, so proceed however you want.
Got it. I'll keep lock_batch_lruvec() in vmscan.c for now. Once there is a second caller, we can clean it up and move it to memcontrol.h.
Thanks,
Qi