Skip to contentSkip to conversation

reparent the THP split queue

PATCH Applied Qi Zheng 4 participants 9 messages Nov 10, 2025 – Jan 14

Subsystems (4)
lore
Share & tools
lore

Qi Zheng

author
#0 lore raw

v6 cover reparent the THP split queue

From: Qi Zheng <[email protected]>

Changes in v6:
- collect Reviewed-bys
- rebase onto the next-20251110

Changes in v5:
- fix the softlockup issue in deferred_split_scan() (Zi Yan)
- modify the commit message in [PATCH v4 3/4] (Shakeel Butt)
- move memcg_is_dying() to memcontrol.h (Shakeel Butt)
- collect Acked-bys and Reviewed-bys
- rebase onto the next-20251014

Changes in v4:
- add split_queue_lock*() and let folio_split_queue_lock*() to use them. (I have kept everyone's Acked-bys and Reviewed-bys. If you need to discard it, please let me know.)
- let deferred_split_scan() to use split_queue_lock_irqsave(), which will fix the race problem in [PATCH v3 4/4].
(Muchun Song)
- collect Reviewed-bys
- rebase onto the next-20251002

Changes in v3:
- use css_is_dying() in folio_split_queue_lock*() to check if memcg is dying (David Hildenbrand, Shakeel Butt and Zi Yan)
- modify the commit message in [PATCH v2 4/4]
(Roman Gushchin)
- fix the build error in [PATCH v2 4/4]
- collect Acked-bys and Reviewed-bys
- rebase onto the next-20250926

Changes in v2:
- fix build errors in [PATCH 2/4] and [PATCH 4/4]
- some cleanups for [PATCH 3/4] (suggested by David Hildenbrand)
- collect Acked-bys and Reviewed-bys
- rebase onto the next-20250922

Hi all,

In the future, we will reparent LRU folios during memcg offline to eliminate dying memory cgroups, which requires reparenting the THP split queue to its parent memcg.

Similar to list_lru, the split queue is relatively independent and does not need to be reparented along with objcg and LRU folios (holding objcg lock and lru lock). Therefore, we can apply the same mechanism as list_lru to reparent the split queue first when memcg is offine.

The first three patches in this series are separated from the series "Eliminate Dying Memory Cgroup" [1], mainly to do some cleanup and preparatory work.

The last patch reparents the THP split queue to its parent memcg during memcg offline.

Comments and suggestions are welcome!

Thanks,
Qi

[1]. https://lore.kernel.org/all/[email protected]/

Muchun Song (3):
mm: thp: replace folio_memcg() with folio_memcg_charged()
mm: thp: introduce folio_split_queue_lock and its variants
mm: thp: use folio_batch to handle THP splitting in
deferred_split_scan()

Qi Zheng (1):
mm: thp: reparent the split queue during memcg offline

 include/linux/huge_mm.h    |   4 +
 include/linux/memcontrol.h |  20 +++
 mm/huge_memory.c           | 250 +++++++++++++++++++++++++------------
 mm/memcontrol.c            |   1 +
 4 files changed, 194 insertions(+), 81 deletions(-)
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH v6 1/4 mm: thp: replace folio_memcg() with folio_memcg_charged()

From: Muchun Song <[email protected]>

folio_memcg_charged() is intended for use when the user is unconcerned about the returned memcg pointer. It is more efficient than folio_memcg(). Therefore, replace folio_memcg() with folio_memcg_charged().

---
 mm/huge_memory.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/huge_memory.c +1 -1
index 949250932bb41..84ef014b2c11f 100644
@@ -4140,7 +4140,7 @@ bool __folio_unqueue_deferred_split(struct folio *folio)
 	bool unqueued = false;
 
 	WARN_ON_ONCE(folio_ref_count(folio));
-	WARN_ON_ONCE(!mem_cgroup_disabled() && !folio_memcg(folio));
+	WARN_ON_ONCE(!mem_cgroup_disabled() && !folio_memcg_charged(folio));
 
 	ds_queue = get_deferred_split_queue(folio);
 	spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
Signature · 1 line
2.20.1
author
#2 lore raw

PATCH v6 2/4 mm: thp: introduce folio_split_queue_lock and its variants

In reply to Qi Zheng

From: Muchun Song <[email protected]>

In future memcg removal, the binding between a folio and a memcg may change, making the split lock within the memcg unstable when held.

A new approach is required to reparent the split queue to its parent. This patch starts introducing a unified way to acquire the split lock for future work.

It's a code-only refactoring with no functional changes.

---
 include/linux/memcontrol.h |  10 ++++
 mm/huge_memory.c           | 119 ++++++++++++++++++++++++++-----------
 2 files changed, 94 insertions(+), 35 deletions(-)
include/linux/memcontrol.h +10 -0
index 8c0f15e5978fa..fad2661ca55d8 100644
@@ -1647,6 +1647,11 @@ int alloc_shrinker_info(struct mem_cgroup *memcg);
 void free_shrinker_info(struct mem_cgroup *memcg);
 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id);
 void reparent_shrinker_deferred(struct mem_cgroup *memcg);
+
+static inline int shrinker_id(struct shrinker *shrinker)
+{
+	return shrinker->id;
+}
 #else
 #define mem_cgroup_sockets_enabled 0
 
@@ -1678,6 +1683,11 @@ static inline void set_shrinker_bit(struct mem_cgroup *memcg,
 				    int nid, int shrinker_id)
 {
 }
+
+static inline int shrinker_id(struct shrinker *shrinker)
+{
+	return -1;
+}
 #endif
 
 #ifdef CONFIG_MEMCG
mm/huge_memory.c +84 -35

213 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#3 lore raw

PATCH v6 3/4 mm: thp: use folio_batch to handle THP splitting in deferred_split_scan()

In reply to Qi Zheng

From: Muchun Song <[email protected]>

The maintenance of the folio->_deferred_list is intricate because it's reused in a local list.

Here are some peculiarities:

1) When a folio is removed from its split queue and added to a local on-stack list in deferred_split_scan(), the ->split_queue_len isn't updated, leading to an inconsistency between it and the actual number of folios in the split queue.

2) When the folio is split via split_folio() later, it's removed from the local list while holding the split queue lock. At this time, the lock is not needed as it is not protecting anything.

3) To handle the race condition with a third-party freeing or migrating the preceding folio, we must ensure there's always one safe (with raised refcount) folio before by delaying its folio_put(). More details can be found in commit e66f3185fa04 ("mm/thp: fix deferred split queue not partially_mapped"). It's rather tricky.

We can use the folio_batch infrastructure to handle this clearly. In this case, ->split_queue_len will be consistent with the real number of folios in the split queue. If list_empty(&folio->_deferred_list) returns false, it's clear the folio must be in its split queue (not in a local list anymore).

In the future, we will reparent LRU folios during memcg offline to eliminate dying memory cgroups, which requires reparenting the split queue to its parent first. So this patch prepares for using folio_split_queue_lock_irqsave() as the memcg may change then.

---
 mm/huge_memory.c | 87 +++++++++++++++++++++++-------------------------
 1 file changed, 41 insertions(+), 46 deletions(-)
mm/huge_memory.c +41 -46
index 858f896ef3bf3..db03853a73e3f 100644
@@ -3901,21 +3901,22 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 		struct lruvec *lruvec;
 		int expected_refs;
 
-		if (old_order > 1 &&
-		    !list_empty(&folio->_deferred_list)) {
-			ds_queue->split_queue_len--;
+		if (old_order > 1) {
+			if (!list_empty(&folio->_deferred_list)) {
+				ds_queue->split_queue_len--;
+				/*
+				 * Reinitialize page_deferred_list after removing the
+				 * page from the split_queue, otherwise a subsequent
+				 * split will see list corruption when checking the
+				 * page_deferred_list.
+				 */
+				list_del_init(&folio->_deferred_list);
+			}
 			if (folio_test_partially_mapped(folio)) {
 				folio_clear_partially_mapped(folio);
 				mod_mthp_stat(old_order,
 					MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1);
 			}
-			/*
-			 * Reinitialize page_deferred_list after removing the
-			 * page from the split_queue, otherwise a subsequent
-			 * split will see list corruption when checking the
-			 * page_deferred_list.
-			 */
-			list_del_init(&folio->_deferred_list);
 		}
 		split_queue_unlock(ds_queue);
 		if (mapping) {
@@ -4314,35 +4315,40 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
 {
 	struct deferred_split *ds_queue;
 	unsigned long flags;
-	LIST_HEAD(list);
-	struct folio *folio, *next, *prev = NULL;
-	int split = 0, removed = 0;
+	struct folio *folio, *next;
+	int split = 0, i;
+	struct folio_batch fbatch;
 
+	folio_batch_init(&fbatch);
+
+retry:
 	ds_queue = split_queue_lock_irqsave(sc->nid, sc->memcg, &flags);
 	/* Take pin on all head pages to avoid freeing them under us */
 	list_for_each_entry_safe(folio, next, &ds_queue->split_queue,
 							_deferred_list) {
 		if (folio_try_get(folio)) {
-			list_move(&folio->_deferred_list, &list);
-		} else {
+			folio_batch_add(&fbatch, folio);
+		} else if (folio_test_partially_mapped(folio)) {
 			/* We lost race with folio_put() */
-			if (folio_test_partially_mapped(folio)) {
-				folio_clear_partially_mapped(folio);
-				mod_mthp_stat(folio_order(folio),
-					      MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1);
-			}
-			list_del_init(&folio->_deferred_list);
-			ds_queue->split_queue_len--;
+			folio_clear_partially_mapped(folio);
+			mod_mthp_stat(folio_order(folio),
+				      MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1);
 		}
+		list_del_init(&folio->_deferred_list);
+		ds_queue->split_queue_len--;
 		if (!--sc->nr_to_scan)
 			break;
+		if (!folio_batch_space(&fbatch))
+			break;
 	}
 	split_queue_unlock_irqrestore(ds_queue, flags);
 
-	list_for_each_entry_safe(folio, next, &list, _deferred_list) {
+	for (i = 0; i < folio_batch_count(&fbatch); i++) {
 		bool did_split = false;
 		bool underused = false;
+		struct deferred_split *fqueue;
 
+		folio = fbatch.folios[i];
 		if (!folio_test_partially_mapped(folio)) {
 			/*
 			 * See try_to_map_unused_to_zeropage(): we cannot
@@ -4365,38 +4371,27 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
 		}
 		folio_unlock(folio);
 next:
+		if (did_split || !folio_test_partially_mapped(folio))
+			continue;
 		/*
-		 * split_folio() removes folio from list on success.
 		 * Only add back to the queue if folio is partially mapped.
 		 * If thp_underused returns false, or if split_folio fails
 		 * in the case it was underused, then consider it used and
 		 * don't add it back to split_queue.
 		 */
-		if (did_split) {
-			; /* folio already removed from list */
-		} else if (!folio_test_partially_mapped(folio)) {
-			list_del_init(&folio->_deferred_list);
-			removed++;
-		} else {
-			/*
-			 * That unlocked list_del_init() above would be unsafe,
-			 * unless its folio is separated from any earlier folios
-			 * left on the list (which may be concurrently unqueued)
-			 * by one safe folio with refcount still raised.
-			 */
-			swap(folio, prev);
+		fqueue = folio_split_queue_lock_irqsave(folio, &flags);
+		if (list_empty(&folio->_deferred_list)) {
+			list_add_tail(&folio->_deferred_list, &fqueue->split_queue);
+			fqueue->split_queue_len++;
 		}
-		if (folio)
-			folio_put(folio);
+		split_queue_unlock_irqrestore(fqueue, flags);
 	}
+	folios_put(&fbatch);
 
-	spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
-	list_splice_tail(&list, &ds_queue->split_queue);
-	ds_queue->split_queue_len -= removed;
-	spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
-
-	if (prev)
-		folio_put(prev);
+	if (sc->nr_to_scan && !list_empty(&ds_queue->split_queue)) {
+		cond_resched();
+		goto retry;
+	}
 
 	/*
 	 * Stop shrinker if we didn't split any page, but the queue is empty.
Signature · 1 line
2.20.1
author
#4 lore raw

PATCH v6 4/4 mm: thp: reparent the split queue during memcg offline

In reply to Qi Zheng

From: Qi Zheng <[email protected]>

Similar to list_lru, the split queue is relatively independent and does not need to be reparented along with objcg and LRU folios (holding objcg lock and lru lock). So let's apply the similar mechanism as list_lru to reparent the split queue separately when memcg is offine.

This is also a preparation for reparenting LRU folios.

---
 include/linux/huge_mm.h    |  4 ++++
 include/linux/memcontrol.h | 10 +++++++++
 mm/huge_memory.c           | 44 ++++++++++++++++++++++++++++++++++++++
 mm/memcontrol.c            |  1 +
 4 files changed, 59 insertions(+)
include/linux/huge_mm.h +4 -0
index 5ba9cac440b92..f381339842fa1 100644
@@ -412,6 +412,9 @@ static inline int split_huge_page(struct page *page)
 	return split_huge_page_to_list_to_order(page, NULL, 0);
 }
 void deferred_split_folio(struct folio *folio, bool partially_mapped);
+#ifdef CONFIG_MEMCG
+void reparent_deferred_split_queue(struct mem_cgroup *memcg);
+#endif
 
 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
 		unsigned long address, bool freeze);
@@ -644,6 +647,7 @@ static inline int try_folio_split_to_order(struct folio *folio,
 }
 
 static inline void deferred_split_folio(struct folio *folio, bool partially_mapped) {}
+static inline void reparent_deferred_split_queue(struct mem_cgroup *memcg) {}
 #define split_huge_pmd(__vma, __pmd, __address)	\
 	do { } while (0)
 
include/linux/memcontrol.h +10 -0
index fad2661ca55d8..8d2e250535a8a 100644
@@ -1774,6 +1774,11 @@ static inline void count_objcg_events(struct obj_cgroup *objcg,
 
 bool mem_cgroup_node_allowed(struct mem_cgroup *memcg, int nid);
 
+static inline bool memcg_is_dying(struct mem_cgroup *memcg)
+{
+	return memcg ? css_is_dying(&memcg->css) : false;
+}
+
 #else
 static inline bool mem_cgroup_kmem_disabled(void)
 {
@@ -1840,6 +1845,11 @@ static inline bool mem_cgroup_node_allowed(struct mem_cgroup *memcg, int nid)
 {
 	return true;
 }
+
+static inline bool memcg_is_dying(struct mem_cgroup *memcg)
+{
+	return false;
+}
 #endif /* CONFIG_MEMCG */
 
 #if defined(CONFIG_MEMCG) && defined(CONFIG_ZSWAP)
mm/huge_memory.c +44 -0

66 lines not on this page · raw message

mm/memcontrol.c +1 -0
index 025da46d9959f..c34029e92baba 100644
@@ -3920,6 +3920,7 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
 	zswap_memcg_offline_cleanup(memcg);
 
 	memcg_offline_kmem(memcg);
+	reparent_deferred_split_queue(memcg);
 	reparent_shrinker_deferred(memcg);
 	wb_memcg_offline(memcg);
 	lru_gen_offline_memcg(memcg);
Signature · 1 line
2.20.1
#5 lore raw

Re: [PATCH v6 4/4] mm: thp: reparent the split queue during memcg offline

Quoted 7 lines · On Mon, 10 Nov 2025 16:17:58 +0800 Qi Zheng <[email protected]> wrote:

From: Qi Zheng <[email protected]>

Similar to list_lru, the split queue is relatively independent and does not need to be reparented along with objcg and LRU folios (holding objcg lock and lru lock). So let's apply the similar mechanism as list_lru to reparent the split queue separately when memcg is offine.

Hi everyone,

I'm running all of the MM commits since v6.18 through the AI patch review tools, and this one was flagged. I'm much less confident in this report, AI tends to get these races wrong:

[AI review output]

Quoted 13 lines
include/linux/memcontrol.h +5 -0
index b0c6a4635c67b..cc6db20d7dca0 100644
@@ -1775,6 +1775,12 @@ static inline void count_objcg_events(struct obj_cgroup *objcg,
 bool mem_cgroup_node_allowed(struct mem_cgroup *memcg, int nid);
 
 void mem_cgroup_show_protected_memory(struct mem_cgroup *memcg);
+
+static inline bool memcg_is_dying(struct mem_cgroup *memcg)
+{
+	return memcg ? css_is_dying(&memcg->css) : false;
+}

[ ... ]

Quoted 2 lines
mm/huge_memory.c +0 -0
index 13684e5376e82..d17d3810a882c 100644

[ ... ]

Quoted 5 lines
mm/huge_memory.c +1 -0
@@ -1129,8 +1140,14 @@ split_queue_lock_irqsave(int nid, struct mem_cgroup *memcg, unsigned long *flags
 {
 	struct deferred_split *queue;
 
+retry:

mm/huge_memory.c split_queue_lock_irqsave(int nid, struct mem_cgroup *memcg, unsigned long *flags · line 1153

Quoted 10 lines
mm/huge_memory.c +5 -0
 	queue = memcg_split_queue(nid, memcg);
 	spin_lock_irqsave(&queue->split_queue_lock, *flags);
+	if (unlikely(memcg_is_dying(memcg))) {
+		spin_unlock_irqrestore(&queue->split_queue_lock, *flags);
+		memcg = parent_mem_cgroup(memcg);
+		goto retry;
+	}
 
 	return queue;
 }

There appears to be a race window that can cause split_queue_len counter mismatch. Between the time CSS_DYING is set (in kill_css()) and when reparent_deferred_split_queue() actually runs (asynchronously in css_offline), folios can be unqueued via __folio_unqueue_deferred_split().

In this scenario:
__folio_unqueue_deferred_split()

    -> folio_split_queue_lock_irqsave()
       -> split_queue_lock_irqsave() sees memcg_is_dying() == true
       -> returns parent's queue (locked)
    -> ds_queue->split_queue_len-- decrements parent's counter
    -> list_del_init() removes folio from child's list

The folio is correctly removed from the child's list (list_del_init is self-referential), but the wrong queue's counter is decremented. If the parent queue was empty (split_queue_len == 0), this would cause an unsigned long underflow to ULONG_MAX.

Could this be addressed by checking that the folio is actually in the queue being unlocked, perhaps by verifying the list head matches before decrementing split_queue_len?

[ ... ]

mm/huge_memory.c static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) · line 3923

Quoted 5 lines
mm/huge_memory.c +1 -0
@@ -3920,6 +3920,7 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
 	zswap_memcg_offline_cleanup(memcg);
 
 	memcg_offline_kmem(memcg);
+	reparent_deferred_split_queue(memcg);

The race window exists because CSS_DYING is set in kill_css() before css_offline() callbacks run asynchronously. The comment in split_queue_lock mentions that THPs will be "hidden from the shrinker side" during this period, but does not address the counter accounting issue when folios are unqueued.

#6 lore raw

Re: [PATCH v6 4/4] mm: thp: reparent the split queue during memcg offline

Quoted 62 lines · Chris Mason
Quoted 7 lines · On Mon, 10 Nov 2025 16:17:58 +0800 Qi Zheng <[email protected]> wrote:

From: Qi Zheng <[email protected]>

Similar to list_lru, the split queue is relatively independent and does not need to be reparented along with objcg and LRU folios (holding objcg lock and lru lock). So let's apply the similar mechanism as list_lru to reparent the split queue separately when memcg is offine.

Hi everyone,

I'm running all of the MM commits since v6.18 through the AI patch review tools, and this one was flagged. I'm much less confident in this report, AI tends to get these races wrong:

[AI review output]

Quoted 13 lines
include/linux/memcontrol.h +5 -0
index b0c6a4635c67b..cc6db20d7dca0 100644
@@ -1775,6 +1775,12 @@ static inline void count_objcg_events(struct obj_cgroup *objcg,
 bool mem_cgroup_node_allowed(struct mem_cgroup *memcg, int nid);
 
 void mem_cgroup_show_protected_memory(struct mem_cgroup *memcg);
+
+static inline bool memcg_is_dying(struct mem_cgroup *memcg)
+{
+	return memcg ? css_is_dying(&memcg->css) : false;
+}

[ ... ]

Quoted 2 lines
mm/huge_memory.c +0 -0
index 13684e5376e82..d17d3810a882c 100644

[ ... ]

Quoted 5 lines
mm/huge_memory.c +1 -0
@@ -1129,8 +1140,14 @@ split_queue_lock_irqsave(int nid, struct mem_cgroup *memcg, unsigned long *flags
 {
 	struct deferred_split *queue;
 
+retry:

mm/huge_memory.c split_queue_lock_irqsave(int nid, struct mem_cgroup *memcg, unsigned long *flags · line 1153

Quoted 10 lines
mm/huge_memory.c +5 -0
 	queue = memcg_split_queue(nid, memcg);
 	spin_lock_irqsave(&queue->split_queue_lock, *flags);
+	if (unlikely(memcg_is_dying(memcg))) {
+		spin_unlock_irqrestore(&queue->split_queue_lock, *flags);
+		memcg = parent_mem_cgroup(memcg);
+		goto retry;
+	}
 
 	return queue;
 }

There appears to be a race window that can cause split_queue_len counter mismatch. Between the time CSS_DYING is set (in kill_css()) and when reparent_deferred_split_queue() actually runs (asynchronously in css_offline), folios can be unqueued via __folio_unqueue_deferred_split().

In this scenario:
__folio_unqueue_deferred_split()

Quoted 8 lines
    -> folio_split_queue_lock_irqsave()
       -> split_queue_lock_irqsave() sees memcg_is_dying() == true
       -> returns parent's queue (locked)
    -> ds_queue->split_queue_len-- decrements parent's counter
    -> list_del_init() removes folio from child's list

The folio is correctly removed from the child's list (list_del_init is self-referential), but the wrong queue's counter is decremented.

Good point. Sounds pretty possible to me?

I don't think there's anything that prevents it from unqueued before it's reparented.

Quoted 2 lines

If the parent queue was empty (split_queue_len == 0), this would cause an unsigned long underflow to ULONG_MAX.

Although the accounting mismatch will only persist until reparent_deferred_split_queue() reparents the deferred split queue.

Ideally this should be fixed by checking if the folio has been reparented after acquiring the split queue lock, but since we don't reparent LRU pages yet ... do we need a band-aid before then?

Do we want to have is_dying property in the split queue as it was in v2? [1]
[1] https://lore.kernel.org/linux-mm/55370bda7b2df617033ac12116c1712144bb7591.1758618527.git.zhengqi.arch@bytedance.com

Quoted 17 lines

Could this be addressed by checking that the folio is actually in the queue being unlocked, perhaps by verifying the list head matches before decrementing split_queue_len?

[ ... ]

mm/huge_memory.c static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) · line 3923

Quoted 5 lines
mm/huge_memory.c +1 -0
@@ -3920,6 +3920,7 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
 	zswap_memcg_offline_cleanup(memcg);
 
 	memcg_offline_kmem(memcg);
+	reparent_deferred_split_queue(memcg);

The race window exists because CSS_DYING is set in kill_css() before css_offline() callbacks run asynchronously. The comment in split_queue_lock mentions that THPs will be "hidden from the shrinker side" during this period, but does not address the counter accounting issue when folios are unqueued.

Signature · 2 lines
Cheers,
Harry / Hyeonggon
author
#7 lore raw

Re: [PATCH v6 4/4] mm: thp: reparent the split queue during memcg offline

Quoted 74 lines · Harry Yoo
Quoted 68 lines · Chris Mason
Quoted 7 lines · On Mon, 10 Nov 2025 16:17:58 +0800 Qi Zheng <[email protected]> wrote:

From: Qi Zheng <[email protected]>

Similar to list_lru, the split queue is relatively independent and does not need to be reparented along with objcg and LRU folios (holding objcg lock and lru lock). So let's apply the similar mechanism as list_lru to reparent the split queue separately when memcg is offine.

Hi everyone,

I'm running all of the MM commits since v6.18 through the AI patch review tools, and this one was flagged. I'm much less confident in this report, AI tends to get these races wrong:

[AI review output]

Quoted 13 lines
include/linux/memcontrol.h +5 -0
index b0c6a4635c67b..cc6db20d7dca0 100644
@@ -1775,6 +1775,12 @@ static inline void count_objcg_events(struct obj_cgroup *objcg,
  bool mem_cgroup_node_allowed(struct mem_cgroup *memcg, int nid);
 
  void mem_cgroup_show_protected_memory(struct mem_cgroup *memcg);
+
+static inline bool memcg_is_dying(struct mem_cgroup *memcg)
+{
+	return memcg ? css_is_dying(&memcg->css) : false;
+}

[ ... ]

Quoted 2 lines
mm/huge_memory.c +0 -0
index 13684e5376e82..d17d3810a882c 100644

[ ... ]

Quoted 5 lines
mm/huge_memory.c +1 -0
@@ -1129,8 +1140,14 @@ split_queue_lock_irqsave(int nid, struct mem_cgroup *memcg, unsigned long *flags
  {
  	struct deferred_split *queue;
 
+retry:

mm/huge_memory.c split_queue_lock_irqsave(int nid, struct mem_cgroup *memcg, unsigned long *flags · line 1153

Quoted 10 lines
mm/huge_memory.c +5 -0
  	queue = memcg_split_queue(nid, memcg);
  	spin_lock_irqsave(&queue->split_queue_lock, *flags);
+	if (unlikely(memcg_is_dying(memcg))) {
+		spin_unlock_irqrestore(&queue->split_queue_lock, *flags);
+		memcg = parent_mem_cgroup(memcg);
+		goto retry;
+	}
 
  	return queue;
  }

There appears to be a race window that can cause split_queue_len counter mismatch. Between the time CSS_DYING is set (in kill_css()) and when reparent_deferred_split_queue() actually runs (asynchronously in css_offline), folios can be unqueued via __folio_unqueue_deferred_split().

In this scenario:

   __folio_unqueue_deferred_split()
     -> folio_split_queue_lock_irqsave()
        -> split_queue_lock_irqsave() sees memcg_is_dying() == true
        -> returns parent's queue (locked)
     -> ds_queue->split_queue_len-- decrements parent's counter
     -> list_del_init() removes folio from child's list
Quoted 2 lines

The folio is correctly removed from the child's list (list_del_init is self-referential), but the wrong queue's counter is decremented.

Good point. Sounds pretty possible to me?

Quoted 8 lines

I don't think there's anything that prevents it from unqueued before it's reparented.

Quoted 2 lines

If the parent queue was empty (split_queue_len == 0), this would cause an unsigned long underflow to ULONG_MAX.

Although the accounting mismatch will only persist until reparent_deferred_split_queue() reparents the deferred split queue.

This period is very short, and the only thing affected should be deferred_split_count(), but it does not cause a system error.

So I think maybe we can leave it unrepaired.

Quoted 26 lines

Ideally this should be fixed by checking if the folio has been reparented after acquiring the split queue lock, but since we don't reparent LRU pages yet ... do we need a band-aid before then?

Do we want to have is_dying property in the split queue as it was in v2? [1]
[1] https://lore.kernel.org/linux-mm/55370bda7b2df617033ac12116c1712144bb7591.1758618527.git.zhengqi.arch@bytedance.com

Quoted 17 lines

Could this be addressed by checking that the folio is actually in the queue being unlocked, perhaps by verifying the list head matches before decrementing split_queue_len?

[ ... ]

mm/huge_memory.c static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) · line 3923

Quoted 5 lines
mm/huge_memory.c +1 -0
@@ -3920,6 +3920,7 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
  	zswap_memcg_offline_cleanup(memcg);
 
  	memcg_offline_kmem(memcg);
+	reparent_deferred_split_queue(memcg);

The race window exists because CSS_DYING is set in kill_css() before css_offline() callbacks run asynchronously. The comment in split_queue_lock mentions that THPs will be "hidden from the shrinker side" during this period, but does not address the counter accounting issue when folios are unqueued.

reviewer
#8 lore raw

Re: [PATCH v6 4/4] mm: thp: reparent the split queue during memcg offline

Quoted 83 lines · Qi Zheng
Quoted 76 lines · Harry Yoo
Quoted 62 lines · Chris Mason
Quoted 7 lines · On Mon, 10 Nov 2025 16:17:58 +0800 Qi Zheng <[email protected]> wrote:

From: Qi Zheng <[email protected]>

Similar to list_lru, the split queue is relatively independent and does not need to be reparented along with objcg and LRU folios (holding objcg lock and lru lock). So let's apply the similar mechanism as list_lru to reparent the split queue separately when memcg is offine.

Hi everyone,

I'm running all of the MM commits since v6.18 through the AI patch review tools, and this one was flagged. I'm much less confident in this report, AI tends to get these races wrong:

[AI review output]

Quoted 13 lines
include/linux/memcontrol.h +5 -0
index b0c6a4635c67b..cc6db20d7dca0 100644
@@ -1775,6 +1775,12 @@ static inline void count_objcg_events(struct obj_cgroup *objcg,
 bool mem_cgroup_node_allowed(struct mem_cgroup *memcg, int nid);
 
 void mem_cgroup_show_protected_memory(struct mem_cgroup *memcg);
+
+static inline bool memcg_is_dying(struct mem_cgroup *memcg)
+{
+ return memcg ? css_is_dying(&memcg->css) : false;
+}

[ ... ]

Quoted 2 lines
mm/huge_memory.c +0 -0
index 13684e5376e82..d17d3810a882c 100644

[ ... ]

Quoted 5 lines
mm/huge_memory.c +1 -0
@@ -1129,8 +1140,14 @@ split_queue_lock_irqsave(int nid, struct mem_cgroup *memcg, unsigned long *flags
 {
  struct deferred_split *queue;
 
+retry:

mm/huge_memory.c split_queue_lock_irqsave(int nid, struct mem_cgroup *memcg, unsigned long *flags · line 1153

Quoted 10 lines
mm/huge_memory.c +5 -0
  queue = memcg_split_queue(nid, memcg);
  spin_lock_irqsave(&queue->split_queue_lock, *flags);
+ if (unlikely(memcg_is_dying(memcg))) {
+ spin_unlock_irqrestore(&queue->split_queue_lock, *flags);
+ memcg = parent_mem_cgroup(memcg);
+ goto retry;
+ }
 
  return queue;
 }

There appears to be a race window that can cause split_queue_len counter mismatch. Between the time CSS_DYING is set (in kill_css()) and when reparent_deferred_split_queue() actually runs (asynchronously in css_offline), folios can be unqueued via __folio_unqueue_deferred_split().

In this scenario:
__folio_unqueue_deferred_split()

Quoted 8 lines
    -> folio_split_queue_lock_irqsave()
       -> split_queue_lock_irqsave() sees memcg_is_dying() == true
       -> returns parent's queue (locked)
    -> ds_queue->split_queue_len-- decrements parent's counter
    -> list_del_init() removes folio from child's list

The folio is correctly removed from the child's list (list_del_init is self-referential), but the wrong queue's counter is decremented.

Good point. Sounds pretty possible to me?
I don't think there's anything that prevents it from unqueued before it's reparented.

Quoted 2 lines

If the parent queue was empty (split_queue_len == 0), this would cause an unsigned long underflow to ULONG_MAX.

Quoted 7 lines
Quoted 2 lines

Although the accounting mismatch will only persist until reparent_deferred_split_queue() reparents the deferred split queue.

This period is very short, and the only thing affected should be deferred_split_count(), but it does not cause a system error.

So I think maybe we can leave it unrepaired.

I didn’t look closely at the specific issue, but based on Qi’s point, we can actually take a cue from list_lru_count_one and provide a similar workaround to keep deferred_split_count from returning an enormous value—see commit 41d17431df4aa.

Quoted 23 lines
Quoted 22 lines

Ideally this should be fixed by checking if the folio has been
reparented after acquiring the split queue lock, but since we don't reparent
LRU pages yet ... do we need a band-aid before then?
Do we want to have is_dying property in the split queue as it was in v2? [1]
[1] https://lore.kernel.org/linux-mm/55370bda7b2df617033ac12116c1712144bb7591.1758618527.git.zhengqi.arch@bytedance.com

Quoted 16 lines

Could this be addressed by checking that the folio is actually in the queue being unlocked, perhaps by verifying the list head matches before decrementing split_queue_len?
[ ... ]

mm/huge_memory.c static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) · line 3923

Quoted 5 lines
mm/huge_memory.c +1 -0
@@ -3920,6 +3920,7 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
  zswap_memcg_offline_cleanup(memcg);
 
  memcg_offline_kmem(memcg);
+ reparent_deferred_split_queue(memcg);

The race window exists because CSS_DYING is set in kill_css() before css_offline() callbacks run asynchronously. The comment in split_queue_lock mentions that THPs will be "hidden from the shrinker side" during this period, but does not address the counter accounting issue when folios are unqueued.