reparent the THP split queue
Subsystems (4)
v5 cover reparent the THP split queue
From: Qi Zheng <[email protected]>
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
PATCH v5 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
@@ -4014,7 +4014,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
PATCH v5 2/4 mm: thp: introduce folio_split_queue_lock and its variants
In reply to Qi ZhengFrom: 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
@@ -1674,6 +1674,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
@@ -1705,6 +1710,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
PATCH v5 3/4 mm: thp: use folio_batch to handle THP splitting in deferred_split_scan()
In reply to Qi ZhengFrom: 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
@@ -3782,21 +3782,22 @@ static int __folio_split(struct folio *folio, unsigned int new_order,struct lruvec *lruvec;int expected_refs; -if (folio_order (folio) >1 && - !list_empty (&folio->_deferred_list)) { - ds_queue->split_queue_len--; +if (folio_order (folio) >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 (folio_order (folio),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) {
@@ -4188,35 +4189,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
@@ -4239,38 +4245,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
Re: [PATCH v5 3/4] mm: thp: use folio_batch to handle THP splitting in deferred_split_scan()
Quoted 33 lines · Qi Zheng
From: Muchun Song <[email protected]>
The maintenance of the
folio->_deferred_listis 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 commite66f3185fa04("mm/thp: fix deferred split queue notpartially_mapped"). It's rather tricky.We can use the
folio_batchinfrastructure to handle this clearly. In this case, ->split_queue_len will be consistent with the real number of folios in the split queue. Iflist_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.
Quoted 5 lines
Signed-off-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>Reviewed-by Zi Yan <[email protected]>Acked-by Shakeel Butt <[email protected]>
One nit below
Quoted 1 line
---
[...]
Quoted 35 lines
mm/huge_memory.c +0
-0@@ -4239,38 +4245,27 @@ static unsigned long deferred_split_scan(struct shrinker *shrink, }folio_unlock (folio);next:
mm/huge_memory.c +2
-0+if (did_split || !folio_test_partially_mapped (folio)) +continue ;/*mm/huge_memory.c +0
-1-* split_folio() removes folio from list on success. * Only add back to the queue if folio is partially mapped.
* Ifthp_underusedreturns false, or ifsplit_foliofails
* in the case it was underused, then consider it used and
* don't add it back tosplit_queue.
*/mm/huge_memory.c +4
-13-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++;}mm/huge_memory.c +1
-2-if (folio) -folio_put (folio); +split_queue_unlock_irqrestore (fqueue, flags);}
mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 4265
Quoted 10 lines
mm/huge_memory.c +2
-7+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)) {
Maybe we can use ds_queue->split_queue_len instead?
Quoted 9 lines
mm/huge_memory.c +3
-0+cond_resched (); + goto retry; + }/* * Stop shrinker if we didn't split any page, but the queue is empty.Signature · 1 line
2.20.1
Signature · 2 lines
Wei Yang Help you, Help me
Re: [PATCH v5 3/4] mm: thp: use folio_batch to handle THP splitting in deferred_split_scan()
Quoted 34 lines · Wei Yang
Quoted 33 lines · Qi Zheng
From: Muchun Song <[email protected]>
The maintenance of the
folio->_deferred_listis 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 commite66f3185fa04("mm/thp: fix deferred split queue notpartially_mapped"). It's rather tricky.We can use the
folio_batchinfrastructure to handle this clearly. In this case, ->split_queue_len will be consistent with the real number of folios in the split queue. Iflist_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.
Quoted 7 lines
Quoted 5 lines
Signed-off-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>Reviewed-by Zi Yan <[email protected]>Acked-by Shakeel Butt <[email protected]>Reviewed-by Wei Yang <[email protected]>
Thanks.
Quoted 42 lines
One nit below
Quoted 1 line
---
[...]
Quoted 37 lines
mm/huge_memory.c +0
-0@@ -4239,38 +4245,27 @@ static unsigned long deferred_split_scan(struct shrinker *shrink, }folio_unlock (folio);next:
mm/huge_memory.c +2
-0+if (did_split || !folio_test_partially_mapped (folio)) +continue ;/*mm/huge_memory.c +0
-1-* split_folio() removes folio from list on success. * Only add back to the queue if folio is partially mapped.
* Ifthp_underusedreturns false, or ifsplit_foliofails
* in the case it was underused, then consider it used and
* don't add it back tosplit_queue.
*/mm/huge_memory.c +4
-13-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++;}mm/huge_memory.c +1
-2-if (folio) -folio_put (folio); +split_queue_unlock_irqrestore (fqueue, flags);}mm/huge_memory.c +1
-0+folios_put (&fbatch);
Quoted 10 lines
mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 4265
Quoted 8 lines
mm/huge_memory.c +1
-7-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)) {Maybe we can use
ds_queue->split_queue_leninstead?
Maybe not, checking whether the linked list is empty before traversing it is more natural, and the overhead of the two methods is not much different.
Quoted 11 lines
Quoted 10 lines
mm/huge_memory.c +3
-0+cond_resched (); + goto retry; + }/* * Stop shrinker if we didn't split any page, but the queue is empty.Signature · 1 line
2.20.1
Re: [PATCH v5 3/4] mm: thp: use folio_batch to handle THP splitting in deferred_split_scan()
In reply to Qi ZhengQuoted 33 lines · Qi Zheng
From: Muchun Song <[email protected]>
The maintenance of the
folio->_deferred_listis 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 commite66f3185fa04("mm/thp: fix deferred split queue notpartially_mapped"). It's rather tricky.We can use the
folio_batchinfrastructure to handle this clearly. In this case, ->split_queue_len will be consistent with the real number of folios in the split queue. Iflist_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.
Quoted 6 lines
Signed-off-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>Reviewed-by Zi Yan <[email protected]>Acked-by Shakeel Butt <[email protected]>---
Nothing unusual came up during my review, so:
Signature · 2 lines
Cheers, Harry / Hyeonggon
Re: [PATCH v5 3/4] mm: thp: use folio_batch to handle THP splitting in deferred_split_scan()
In reply to Qi ZhengQuoted 66 lines · Qi Zheng
From: Muchun Song <[email protected]>
The maintenance of the
folio->_deferred_listis 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 commite66f3185fa04("mm/thp: fix deferred split queue notpartially_mapped"). It's rather tricky.We can use the
folio_batchinfrastructure to handle this clearly. In this case, ->split_queue_len will be consistent with the real number of folios in the split queue. Iflist_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.Signed-off-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>Reviewed-by Zi Yan <[email protected]>Acked-by Shakeel Butt <[email protected]>---
mm/huge_memory.c| 87 +++++++++++++++++++++++-------------------------1 file changed, 41 insertions(+), 46 deletions(-)mm/huge_memory.c +11
-3@@ -3782,21 +3782,22 @@ static int __folio_split(struct folio *folio, unsigned int new_order,struct lruvec *lruvec;int expected_refs; -if (folio_order (folio) >1 && - !list_empty (&folio->_deferred_list)) { - ds_queue->split_queue_len--; +if (folio_order (folio) >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);
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3799
Quoted 10 lines
mm/huge_memory.c +0
-7mod_mthp_stat (folio_order (folio),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);
@Andrew
Current mm-new looks not merge the code correctly?
The above removed code is still there.
@Qi
After rescan this, I am confused about this code change.
The difference here is originally it would check/clear partially_mapped if folio is on a list. But now we would do this even folio is not on a list.
If my understanding is correct, after this change, !list_empty() means folio is on its ds_queue. And there are total three places to remove it from ds_queue.
1) __folio_unqueue_deferred_split()
2) deferred_split_scan()
3) __folio_split()
In 1) and 2) we all clear partially_mapped bit before removing folio from ds_queue, this means if the folio is not on ds_queue in __folio_split(), it is not necessary to check/clear partially_mapped bit.
Maybe I missed something, would you mind correct me on this?
Signature · 2 lines
Wei Yang Help you, Help me
Andrew Morton
maintainerRe: [PATCH v5 3/4] mm: thp: use folio_batch to handle THP splitting in deferred_split_scan()
Quoted 26 lines · On Thu, 6 Nov 2025 14:52:13 +0000 Wei Yang <[email protected]> wrote:
Quoted 16 lines
mm/huge_memory.c +11
-0+if (folio_order (folio) >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(folio_order(folio), MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1); }mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3799
Quoted 7 lines
mm/huge_memory.c +0
-7-/* -* 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);@Andrew
Quoted 2 lines
Current mm-new looks not merge the code correctly?
It happens. We presently have 29 patches which alter mm/huge_memory.c.
Thanks for checking. I'll drop version 5 of "reparent the THP split queue".
Please check what remains, and send a v6 against tomorrow's mm-new/mm-unstable.
Re: [PATCH v5 3/4] mm: thp: use folio_batch to handle THP splitting in deferred_split_scan()
Quoted 31 lines · Andrew Morton
Quoted 26 lines · On Thu, 6 Nov 2025 14:52:13 +0000 Wei Yang <[email protected]> wrote:
Quoted 16 lines
mm/huge_memory.c +11
-0+if (folio_order (folio) >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(folio_order(folio), MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1); }mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3799
Quoted 7 lines
mm/huge_memory.c +0
-7-/* -* 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);@Andrew
Quoted 2 lines
Current mm-new looks not merge the code correctly?
Quoted 6 lines
It happens. We presently have 29 patches which alter
mm/huge_memory.c.Thanks for checking. I'll drop version 5 of "reparent the THP split queue".
Please check what remains, and send a v6 against tomorrow's mm-new/mm-unstable.
OK, will rebase and resend it.
Re: [PATCH v5 3/4] mm: thp: use folio_batch to handle THP splitting in deferred_split_scan()
In reply to Wei YangQuoted 95 lines · Wei Yang
Quoted 66 lines · Qi Zheng
From: Muchun Song <[email protected]>
The maintenance of the
folio->_deferred_listis 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 commite66f3185fa04("mm/thp: fix deferred split queue notpartially_mapped"). It's rather tricky.We can use the
folio_batchinfrastructure to handle this clearly. In this case, ->split_queue_len will be consistent with the real number of folios in the split queue. Iflist_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.Signed-off-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>Reviewed-by Zi Yan <[email protected]>Acked-by Shakeel Butt <[email protected]>---
mm/huge_memory.c| 87 +++++++++++++++++++++++-------------------------1 file changed, 41 insertions(+), 46 deletions(-)mm/huge_memory.c +11
-3@@ -3782,21 +3782,22 @@ static int __folio_split(struct folio *folio, unsigned int new_order,struct lruvec *lruvec;int expected_refs; -if (folio_order (folio) >1 && - !list_empty (&folio->_deferred_list)) { - ds_queue->split_queue_len--; +if (folio_order (folio) >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);mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3799
Quoted 10 lines
mm/huge_memory.c +0
-7mod_mthp_stat (folio_order (folio),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);@Andrew
Current mm-new looks not merge the code correctly?
The above removed code is still there.
@Qi
After rescan this, I am confused about this code change.
The difference here is originally it would check/clear
partially_mappedif folio is on a list. But now we would do this even folio is not on a list.If my understanding is correct, after this change, !list_empty() means folio is on its
ds_queue. And there are total three places to remove it fromds_queue.
Quoted 7 lines
1)
__folio_unqueue_deferred_split()
2)deferred_split_scan()
3)__folio_split()In 1) and 2) we all clear
partially_mappedbit before removing folio fromds_queue, this means if the folio is not onds_queuein__folio_split(), it is not necessary to check/clearpartially_mappedbit.
In deferred_split_scan(), if folio_try_get() succeeds, then only the folio will be removed from ds_queue, but not clear partially_mapped.
Quoted 3 lines
Maybe I missed something, would you mind correct me on this?
Re: [PATCH v5 3/4] mm: thp: use folio_batch to handle THP splitting in deferred_split_scan()
Quoted 102 lines · Qi Zheng
Quoted 99 lines · Wei Yang
Quoted 66 lines · Qi Zheng
From: Muchun Song <[email protected]>
The maintenance of the
folio->_deferred_listis 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 commite66f3185fa04("mm/thp: fix deferred split queue notpartially_mapped"). It's rather tricky.We can use the
folio_batchinfrastructure to handle this clearly. In this case, ->split_queue_len will be consistent with the real number of folios in the split queue. Iflist_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.Signed-off-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>Reviewed-by Zi Yan <[email protected]>Acked-by Shakeel Butt <[email protected]>---
mm/huge_memory.c| 87 +++++++++++++++++++++++-------------------------1 file changed, 41 insertions(+), 46 deletions(-)mm/huge_memory.c +11
-3@@ -3782,21 +3782,22 @@ static int __folio_split(struct folio *folio, unsigned int new_order,struct lruvec *lruvec;int expected_refs; -if (folio_order (folio) >1 && - !list_empty (&folio->_deferred_list)) { - ds_queue->split_queue_len--; +if (folio_order (folio) >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);mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3799
Quoted 10 lines
mm/huge_memory.c +0
-7mod_mthp_stat (folio_order (folio),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);@Andrew
Current mm-new looks not merge the code correctly?
The above removed code is still there.
@Qi
After rescan this, I am confused about this code change.
The difference here is originally it would check/clear
partially_mappedif folio is on a list. But now we would do this even folio is not on a list.If my understanding is correct, after this change, !list_empty() means folio is on its
ds_queue. And there are total three places to remove it fromds_queue.1)
__folio_unqueue_deferred_split()
2)deferred_split_scan()
3)__folio_split()
Quoted 7 lines
Quoted 3 lines
In 1) and 2) we all clear
partially_mappedbit before removing folio fromds_queue, this means if the folio is not onds_queuein__folio_split(), it is not necessary to check/clearpartially_mappedbit.In
deferred_split_scan(), iffolio_try_get()succeeds, then only the folio will be removed fromds_queue, but not clearpartially_mapped.
Hmm... you are right. Sorry for the trouble.
Quoted 3 lines
Quoted 3 lines
Maybe I missed something, would you mind correct me on this?
Signature · 2 lines
Wei Yang Help you, Help me
PATCH v5 4/4 mm: thp: reparent the split queue during memcg offline
In reply to Qi ZhengFrom: 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
@@ -417,6 +417,9 @@ static inline int split_huge_page(struct page *page)return split_huge_page_to_list_to_order (page,NULL , ret); }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);
@@ -611,6 +614,7 @@ static inline int try_folio_split(struct folio *folio, struct page *page, }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
@@ -1801,6 +1801,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 ) {
@@ -1867,6 +1872,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
@@ -3888,6 +3888,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
Re: [PATCH v5 4/4] mm: thp: reparent the split queue during memcg offline
Quoted 7 lines · 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 aslist_lruto reparent the split queue separately when memcg is offine.
Quoted 8 lines
This is also a preparation for reparenting LRU folios.
Signed-off-by Qi Zheng <[email protected]>Acked-by Zi Yan <[email protected]>Reviewed-by Muchun Song <[email protected]>Acked-by Shakeel Butt <[email protected]>---
Looks good to me,
with a question:
Quoted 6 lines
mm/huge_memory.c +0
-0@@ -1117,8 +1117,19 @@ static struct deferred_split *split_queue_lock(int nid, struct mem_cgroup *memcg {
mm/huge_memory.c static struct deferred_split *split_queue_lock(int nid, struct mem_cgroup *memcg · line 1127
Quoted 10 lines
mm/huge_memory.c +6
-0struct deferred_split *queue; +retry: queue =memcg_split_queue (nid, memcg);spin_lock (&queue->split_queue_lock); +/* +* There is a period between setting memcg to dying and reparenting +* deferred split queue, and during this period the THPs in the deferred +* split queue will be hidden from the shrinker side. +*/
You mean it will be hidden if the shrinker bit is not set for the node in the parent memcg, right?
Signature · 2 lines
Cheers, Harry / Hyeonggon
Quoted 7 lines
mm/huge_memory.c +5
-0+if (unlikely (memcg_is_dying (memcg))) { +spin_unlock (&queue->split_queue_lock); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }
Re: [PATCH v5 4/4] mm: thp: reparent the split queue during memcg offline
Quoted 17 lines · Harry Yoo
Quoted 7 lines · 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 aslist_lruto reparent the split queue separately when memcg is offine.Quoted 8 lines
This is also a preparation for reparenting LRU folios.
Signed-off-by Qi Zheng <[email protected]>Acked-by Zi Yan <[email protected]>Reviewed-by Muchun Song <[email protected]>Acked-by Shakeel Butt <[email protected]>---
Quoted 2 lines
Looks good to me,
Reviewed-by Harry Yoo <[email protected]>
Thanks.
Quoted 11 lines
with a question:
Quoted 8 lines
mm/huge_memory.c +0
-0@@ -1117,8 +1117,19 @@ static struct deferred_split *split_queue_lock(int nid, struct mem_cgroup *memcg {struct deferred_split *queue;
Quoted 11 lines
mm/huge_memory.c static struct deferred_split *split_queue_lock(int nid, struct mem_cgroup *memcg · line 1127
Quoted 8 lines
mm/huge_memory.c +6
-0+retry: queue =memcg_split_queue (nid, memcg);spin_lock (&queue->split_queue_lock); +/* +* There is a period between setting memcg to dying and reparenting +* deferred split queue, and during this period the THPs in the deferred +* split queue will be hidden from the shrinker side. +*/ You mean it will be hidden if the shrinker bit is not set for the node in the parent memcg, right?
Look at the following situation:
CPU 0 CPU 1
----- -----
set CSS_DYING
deferred_split_scan
/*
* See CSS_DYING, and return the parent
* memcg's ds_queue. But the pages on the
* child memcg's ds_queue has not yet been
* reparented to the parent memcg, that is,
* it is hidden.
*/
--> ds_queue = split_queue_lock_irqsave()
reparent_deferred_split_queue
Thanks,
Qi
Quoted 1 line
Re: [PATCH v5 4/4] mm: thp: reparent the split queue during memcg offline
Quoted 63 lines · Qi Zheng
Quoted 17 lines · Harry Yoo
Quoted 7 lines · 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 aslist_lruto reparent the split queue separately when memcg is offine.Quoted 8 lines
This is also a preparation for reparenting LRU folios.
Signed-off-by Qi Zheng <[email protected]>Acked-by Zi Yan <[email protected]>Reviewed-by Muchun Song <[email protected]>Acked-by Shakeel Butt <[email protected]>---
Quoted 2 lines
Looks good to me,
Reviewed-by Harry Yoo <[email protected]>Thanks.
Quoted 13 lines
with a question:
Quoted 10 lines
mm/huge_memory.c +1
-0@@ -1117,8 +1117,19 @@ static struct deferred_split *split_queue_lock(int nid, struct mem_cgroup *memcg {struct deferred_split *queue; +retry: queue =memcg_split_queue (nid, memcg);spin_lock (&queue->split_queue_lock);Quoted 8 lines
mm/huge_memory.c static struct deferred_split *split_queue_lock(int nid, struct mem_cgroup *memcg · line 1127
Quoted 5 lines
mm/huge_memory.c +5
-0+/* +* There is a period between setting memcg to dying and reparenting +* deferred split queue, and during this period the THPs in the deferred +* split queue will be hidden from the shrinker side. +*/ You mean it will be hidden if the shrinker bit is not set for the node in the parent memcg, right?
Look at the following situation:
CPU 0 CPU 1
----- -----set
CSS_DYING
deferred_split_scan
/*
* SeeCSS_DYING, and return the parent
* memcg'sds_queue. But the pages on the
* child memcg'sds_queuehas not yet been
* reparented to the parent memcg, that is,
* it is hidden.
*/
-->ds_queue=split_queue_lock_irqsave()
Quoted 1 line
reparent_deferred_split_queue
Ah, I see what you meant. Thanks.
So we may end up shrinking the parent memcg twice if it's hidden, but I guess it's fine as it'll be rare?
Quoted 2 lines
Thanks,
Qi
Signature · 2 lines
Cheers, Harry / Hyeonggon
Re: [PATCH v5 4/4] mm: thp: reparent the split queue during memcg offline
Quoted 64 lines · Harry Yoo
Quoted 63 lines · Qi Zheng
Quoted 17 lines · Harry Yoo
Quoted 7 lines · 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 aslist_lruto reparent the split queue separately when memcg is offine.Quoted 8 lines
This is also a preparation for reparenting LRU folios.
Signed-off-by Qi Zheng <[email protected]>Acked-by Zi Yan <[email protected]>Reviewed-by Muchun Song <[email protected]>Acked-by Shakeel Butt <[email protected]>---
Quoted 2 lines
Looks good to me,
Reviewed-by Harry Yoo <[email protected]>Thanks.
Quoted 13 lines
with a question:
Quoted 10 lines
mm/huge_memory.c +1
-0@@ -1117,8 +1117,19 @@ static struct deferred_split *split_queue_lock(int nid, struct mem_cgroup *memcg {struct deferred_split *queue; +retry: queue =memcg_split_queue (nid, memcg);spin_lock (&queue->split_queue_lock);Quoted 8 lines
mm/huge_memory.c static struct deferred_split *split_queue_lock(int nid, struct mem_cgroup *memcg · line 1127
Quoted 5 lines
mm/huge_memory.c +5
-0+/* +* There is a period between setting memcg to dying and reparenting +* deferred split queue, and during this period the THPs in the deferred +* split queue will be hidden from the shrinker side. +*/ You mean it will be hidden if the shrinker bit is not set for the node in the parent memcg, right?
Look at the following situation:
CPU 0 CPU 1
----- -----set
CSS_DYING
deferred_split_scan
/*
* SeeCSS_DYING, and return the parent
* memcg'sds_queue. But the pages on the
* child memcg'sds_queuehas not yet been
* reparented to the parent memcg, that is,
* it is hidden.
*/
-->ds_queue=split_queue_lock_irqsave()
Quoted 6 lines
Quoted 1 line
reparent_deferred_split_queueAh, I see what you meant. Thanks.
So we may end up shrinking the parent memcg twice if it's hidden, but I guess it's fine as it'll be rare?
Yeah.
Quoted 4 lines
Quoted 3 lines
Thanks,
Qi