Skip to contentSkip to conversation

mm: mglru: fix stale batch updates after memcg reparenting

PATCH Superseded Qi Zheng 5 participants 19 messages Jun 25 – Jun 27

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Qi Zheng

author reviewer
#0 lore raw

PATCH 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 += delta

This 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
index 35c3bb15ae96..1ec8c23c72b9 100644
@@ -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
maintainer
#1 lore raw
Quoted 69 lines · Qi Zheng

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 += delta

This 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 +0 -0
index 35c3bb15ae96..1ec8c23c72b9 100644

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);
	}
reviewer
#14 lore raw
In reply to Qi Zheng
Quoted 69 lines · Qi Zheng

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 += delta

This 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 +0 -0
index 35c3bb15ae96..1ec8c23c72b9 100644

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?

author reviewer
#2 lore raw
In reply to Johannes Weiner

Hi 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_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 += delta

This 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 +0 -0
index 35c3bb15ae96..1ec8c23c72b9 100644
@@ -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
	}
author reviewer
#15 lore raw
In reply to Shakeel Butt

Hi 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_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 += delta

This 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 +0 -0
index 35c3bb15ae96..1ec8c23c72b9 100644
@@ -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_irq is used and if similar kind of situation can happen?

I just checked and found no such callers.

Thanks,
Qi

Quoted 1 line
#16 lore raw
In reply to Qi Zheng

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_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 += delta

This 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
index 35c3bb15ae96..1ec8c23c72b9 100644
@@ -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),
#3 lore raw
In reply to Qi Zheng
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_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 += delta

This 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.

[email protected]

Fixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU folios")

---
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 +0 -0
index 35c3bb15ae96..1ec8c23c72b9 100644
@@ -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

Quoted 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() in reset_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
author reviewer
#4 lore raw
Quoted 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_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 += delta

This 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.

[email protected]

Fixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU folios")

---
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 +0 -0
index 35c3bb15ae96..1ec8c23c72b9 100644
@@ -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
Quoted 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() in reset_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);
struct mem_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;

author reviewer
#17 lore raw
In reply to Peiyang He

Hi 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

#5 lore raw
In reply to Qi Zheng
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_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 += delta

This 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.

[email protected]

Fixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU

folios")

---
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 +0 -0
index 35c3bb15ae96..1ec8c23c72b9 100644
@@ -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
Quoted 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() in reset_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);
struct mem_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
author reviewer
#6 lore raw
Quoted 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_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 += delta

This 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.

[email protected]

Fixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU

folios")

---
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 +0 -0
index 35c3bb15ae96..1ec8c23c72b9 100644
@@ -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
Quoted 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() in reset_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);
struct mem_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
#7 lore raw
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_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 += delta

This 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.

[email protected]

Fixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU

folios")

---
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 +0 -0
index 35c3bb15ae96..1ec8c23c72b9 100644
@@ -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

Quoted 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() in reset_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);
struct mem_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
author reviewer
#8 lore raw
Quoted 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_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 += delta

This 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.

[email protected]

Fixes f304652609ea ("mm: vmscan: prepare for reparenting MGLRU

folios")

---
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 +0 -0
index 35c3bb15ae96..1ec8c23c72b9 100644
@@ -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
Quoted 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() in reset_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);
struct mem_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
#9 lore raw
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
index 35c3bb15ae96..1ec8c23c72b9 100644
@@ -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
Quoted 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() in reset_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);
struct mem_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
Reviewed-by on v3 1/1
#18 lore raw
In reply to Qi Zheng
Quoted 43 lines · Qi Zheng

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 += delta

This 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.

Quoted 6 lines

---

besides some nits mentioned elsewhere in the thread, the approach looks good to me, so:

Signature · 2 lines
Cheers,
Harry / Hyeonggon
maintainer
#10 lore raw
In reply to Qi Zheng
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 :)

author reviewer
#11 lore raw
Quoted 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

reviewer
#12 lore raw
Quoted 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() to memcontrol.h? It's currently only used by reset_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.

author reviewer
#13 lore raw
Quoted 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() to memcontrol.h? It's currently only used by reset_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.

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

Quoted 1 line