reparent the THP split queue
Subsystems (7)
v2 cover reparent the THP split queue
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 | 2 + include/linux/memcontrol.h | 10 ++ include/linux/mmzone.h | 1 + mm/huge_memory.c | 229 +++++++++++++++++++++++++------------ mm/memcontrol.c | 1 + mm/mm_init.c | 1 + 6 files changed, 172 insertions(+), 72 deletions(-)
Signature · 1 line
2.20.1
PATCH v2 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 v2 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 | 104 ++++++++++++++++++++++++++++--------- 2 files changed, 89 insertions(+), 25 deletions(-)
include/linux/memcontrol.h +10 -0
@@ -1662,6 +1662,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
@@ -1693,6 +1698,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 +79 -25
180 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v2 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, this lock protects the local list, not the split queue.
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 | 84 ++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 46 deletions(-)
mm/huge_memory.c +38 -46
@@ -3781,21 +3781,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) {
@@ -4194,40 +4195,44 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,struct pglist_data *pgdata =NODE_DATA (sc->nid);struct deferred_split *ds_queue = &pgdata->deferred_split_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;#ifdef CONFIG_MEMCG if (sc->memcg) ds_queue = &sc->memcg->deferred_split_queue;#endif +folio_batch_init (&fbatch); +retry:spin_lock_irqsave (&ds_queue->split_queue_lock, 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 ; }spin_unlock_irqrestore (&ds_queue->split_queue_lock, 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
@@ -4250,38 +4255,25 @@ 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) + goto retry;/* * Stop shrinker if we didn't split any page, but the queue is empty.
Signature · 1 line
2.20.1
PATCH v2 4/4 mm: thp: reparent the split queue during memcg offline
In reply to Qi ZhengIn the future, we will reparent LRU folios during memcg offline to eliminate dying memory cgroups, which requires reparenting the split queue to its parent.
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 same mechanism as list_lru to reparent the split queue separately when memcg is offine.
--- include/linux/huge_mm.h | 2 ++ include/linux/mmzone.h | 1 + mm/huge_memory.c | 39 +++++++++++++++++++++++++++++++++++++++ mm/memcontrol.c | 1 + mm/mm_init.c | 1 + 5 files changed, 44 insertions(+)
include/linux/huge_mm.h +2 -0
@@ -417,6 +417,7 @@ 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); +void reparent_deferred_split_queue (struct mem_cgroup *memcg);void __split_huge_pmd (struct vm_area_struct *vma,pmd_t *pmd,unsigned long address,bool freeze);
@@ -611,6 +612,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/mmzone.h +1 -0
@@ -1346,6 +1346,7 @@ struct deferred_split {spinlock_t split_queue_lock;struct list_head split_queue;unsigned long split_queue_len; + bool is_dying; };#endif
mm/huge_memory.c +39 -0
@@ -1094,9 +1094,15 @@ static struct deferred_split *folio_split_queue_lock(struct folio *folio)struct deferred_split *queue; memcg =folio_memcg (folio); +retry: queue = memcg ? &memcg->deferred_split_queue : &NODE_DATA (folio_nid (folio))->deferred_split_queue;spin_lock (&queue->split_queue_lock); +if (unlikely (queue->is_dying == true)) { +spin_unlock (&queue->split_queue_lock); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }
@@ -1108,9 +1114,15 @@ folio_split_queue_lock_irqsave(struct folio *folio, unsigned long *flags)struct deferred_split *queue; memcg =folio_memcg (folio); +retry: queue = memcg ? &memcg->deferred_split_queue : &NODE_DATA (folio_nid (folio))->deferred_split_queue;spin_lock_irqsave (&queue->split_queue_lock, *flags); +if (unlikely (queue->is_dying == true)) { +spin_unlock_irqrestore (&queue->split_queue_lock, *flags); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }
@@ -4284,6 +4296,33 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,return split; } +void reparent_deferred_split_queue (struct mem_cgroup *memcg) +{ +struct mem_cgroup *parent =parent_mem_cgroup (memcg); +struct deferred_split *ds_queue = &memcg->deferred_split_queue; +struct deferred_split *parent_ds_queue = &parent->deferred_split_queue; +int nid; + +spin_lock_irq (&ds_queue->split_queue_lock); +spin_lock_nested (&parent_ds_queue->split_queue_lock,SINGLE_DEPTH_NESTING ); + +if (!ds_queue->split_queue_len) + goto unlock; + +list_splice_tail_init (&ds_queue->split_queue, &parent_ds_queue->split_queue); + parent_ds_queue->split_queue_len += ds_queue->split_queue_len; + ds_queue->split_queue_len =0 ; +/* Mark the ds_queue dead */ + ds_queue->is_dying = true; + +for_each_node (nid) +set_shrinker_bit (parent, nid,shrinker_id (deferred_split_shrinker)); + +unlock: +spin_unlock (&parent_ds_queue->split_queue_lock); +spin_unlock_irq (&ds_queue->split_queue_lock); +} +#ifdef CONFIG_DEBUG_FS static void split_huge_pages_all (void ) {
mm/memcontrol.c +1 -0
@@ -3887,6 +3887,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);
mm/mm_init.c +1 -0
@@ -1387,6 +1387,7 @@ static void pgdat_init_split_queue(struct pglist_data *pgdat)spin_lock_init (&ds_queue->split_queue_lock);INIT_LIST_HEAD (&ds_queue->split_queue); ds_queue->split_queue_len =0 ; + ds_queue->is_dying = false; }#else static void pgdat_init_split_queue (struct pglist_data *pgdat) {}
Signature · 1 line
2.20.1
Zi Yan
reviewerRe: [PATCH v2 3/4] mm: thp: use folio_batch to handle THP splitting in deferred_split_scan()
In reply to Qi ZhengQuoted 16 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, this lock protects the local list, not the split queue.
Quoted 7 lines
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
Can you add more details on how folio_batch handles the above three concerns in the original code? That would guide me what to look for during code review.
Quoted 33 lines
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.Signed-off-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>--- mm/huge_memory.c | 84 ++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 46 deletions(-)mm/huge_memory.c +6
-3@@ -3781,21 +3781,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
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3799
Quoted 10 lines
mm/huge_memory.c +5
-0+* 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 ); }
folio_test_partially_mapped() is done regardless the folio is on _deferred_list or not, is it because the folio can be on a folio batch and its _deferred_list is empty?
Quoted 41 lines
mm/huge_memory.c +7
-15-/* -* 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) {@@ -4194,40 +4195,44 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,struct pglist_data *pgdata =NODE_DATA (sc->nid);struct deferred_split *ds_queue = &pgdata->deferred_split_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;#ifdef CONFIG_MEMCG if (sc->memcg) ds_queue = &sc->memcg->deferred_split_queue;#endif +folio_batch_init (&fbatch); +retry:spin_lock_irqsave (&ds_queue->split_queue_lock, 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),
mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 4222
Quoted 10 lines
mm/huge_memory.c +5
-4- 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--;
At this point, the folio can be following conditions:
1. deferred_split_scan() gets it,
2. it is freed by folio_put().
In both cases, it is removed from deferred_split_queue, right?
Quoted 37 lines
mm/huge_memory.c +7
-9if (!--sc->nr_to_scan)break ; +if (!folio_batch_space (&fbatch)) +break ; }spin_unlock_irqrestore (&ds_queue->split_queue_lock, 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 @@ -4250,38 +4255,25 @@ 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 { -/*
mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 4269
Quoted 10 lines
mm/huge_memory.c +4
-6-* 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++;
fqueue should be the same as ds_queue, right? Just want to make sure I understand the code.
Quoted 21 lines
mm/huge_memory.c +4
-9} -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) + goto retry;/* * Stop shrinker if we didn't split any page, but the queue is empty. Signature · 1 line
2.20.1
Best Regards,
Yan, Zi
Zi Yan
reviewerRe: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
In reply to Qi ZhengQuoted 92 lines · Qi Zheng
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.
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 same mechanism aslist_lruto reparent the split queue separately when memcg is offine.Signed-off-by Qi Zheng <[email protected]>--- include/linux/huge_mm.h | 2 ++ include/linux/mmzone.h | 1 + mm/huge_memory.c | 39 +++++++++++++++++++++++++++++++++++++++ mm/memcontrol.c | 1 + mm/mm_init.c | 1 + 5 files changed, 44 insertions(+)include/linux/huge_mm.h +2
-0@@ -417,6 +417,7 @@ 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); +void reparent_deferred_split_queue (struct mem_cgroup *memcg);void __split_huge_pmd (struct vm_area_struct *vma,pmd_t *pmd,unsigned long address,bool freeze);@@ -611,6 +612,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/mmzone.h +1
-0@@ -1346,6 +1346,7 @@ struct deferred_split {spinlock_t split_queue_lock;struct list_head split_queue;unsigned long split_queue_len; + bool is_dying; };#endif mm/huge_memory.c +14
-0@@ -1094,9 +1094,15 @@ static struct deferred_split *folio_split_queue_lock(struct folio *folio)struct deferred_split *queue; memcg =folio_memcg (folio); +retry: queue = memcg ? &memcg->deferred_split_queue : &NODE_DATA (folio_nid (folio))->deferred_split_queue;spin_lock (&queue->split_queue_lock); +if (unlikely (queue->is_dying == true)) { +spin_unlock (&queue->split_queue_lock); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }@@ -1108,9 +1114,15 @@ folio_split_queue_lock_irqsave(struct folio *folio, unsigned long *flags)struct deferred_split *queue; memcg =folio_memcg (folio); +retry: queue = memcg ? &memcg->deferred_split_queue : &NODE_DATA (folio_nid (folio))->deferred_split_queue;spin_lock_irqsave (&queue->split_queue_lock, *flags); +if (unlikely (queue->is_dying == true)) { +spin_unlock_irqrestore (&queue->split_queue_lock, *flags); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }@@ -4284,6 +4296,33 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,return split; } +void reparent_deferred_split_queue (struct mem_cgroup *memcg) +{
mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 4310
Quoted 10 lines
mm/huge_memory.c +10
-0+struct mem_cgroup *parent =parent_mem_cgroup (memcg); +struct deferred_split *ds_queue = &memcg->deferred_split_queue; +struct deferred_split *parent_ds_queue = &parent->deferred_split_queue; +int nid; + +spin_lock_irq (&ds_queue->split_queue_lock); +spin_lock_nested (&parent_ds_queue->split_queue_lock,SINGLE_DEPTH_NESTING ); + +if (!ds_queue->split_queue_len) + goto unlock;
Should ds_queue still be marked as dying even if it is empty? Otherwise, new folios still can be added to it, based on my understanding of the changes to folio_split_queue_lock*().
Quoted 44 lines
mm/huge_memory.c +15
-0+ +list_splice_tail_init (&ds_queue->split_queue, &parent_ds_queue->split_queue); + parent_ds_queue->split_queue_len += ds_queue->split_queue_len; + ds_queue->split_queue_len =0 ; +/* Mark the ds_queue dead */ + ds_queue->is_dying = true; + +for_each_node (nid) +set_shrinker_bit (parent, nid,shrinker_id (deferred_split_shrinker)); + +unlock: +spin_unlock (&parent_ds_queue->split_queue_lock); +spin_unlock_irq (&ds_queue->split_queue_lock); +} +#ifdef CONFIG_DEBUG_FS static void split_huge_pages_all (void ) {mm/memcontrol.c +1
-0@@ -3887,6 +3887,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);mm/mm_init.c +1
-0@@ -1387,6 +1387,7 @@ static void pgdat_init_split_queue(struct pglist_data *pgdat)spin_lock_init (&ds_queue->split_queue_lock);INIT_LIST_HEAD (&ds_queue->split_queue); ds_queue->split_queue_len =0 ; + ds_queue->is_dying = false; }#else static void pgdat_init_split_queue (struct pglist_data *pgdat) {}Signature · 1 line
2.20.1
Best Regards,
Yan, Zi
Re: [PATCH v2 1/4] mm: thp: replace folio_memcg() with folio_memcg_charged()
In reply to Qi ZhengQuoted 11 lines · Qi Zheng
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 thanfolio_memcg(). Therefore, replacefolio_memcg()withfolio_memcg_charged().Signed-off-by Muchun Song <[email protected]>Acked-by Johannes Weiner <[email protected]>Acked-by Shakeel Butt <[email protected]>Signed-off-by Qi Zheng <[email protected]>
Roman Gushchin
maintainerRe: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
In reply to Qi ZhengQuoted 3 lines · Qi Zheng
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.
Nit: commit logs should really focus on the actual change, not the future plans.
Quoted 98 lines
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 same mechanism aslist_lruto reparent the split queue separately when memcg is offine.Signed-off-by Qi Zheng <[email protected]>--- include/linux/huge_mm.h | 2 ++ include/linux/mmzone.h | 1 + mm/huge_memory.c | 39 +++++++++++++++++++++++++++++++++++++++ mm/memcontrol.c | 1 + mm/mm_init.c | 1 + 5 files changed, 44 insertions(+)include/linux/huge_mm.h +2
-0@@ -417,6 +417,7 @@ 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); +void reparent_deferred_split_queue (struct mem_cgroup *memcg);void __split_huge_pmd (struct vm_area_struct *vma,pmd_t *pmd,unsigned long address,bool freeze);@@ -611,6 +612,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/mmzone.h +1
-0@@ -1346,6 +1346,7 @@ struct deferred_split {spinlock_t split_queue_lock;struct list_head split_queue;unsigned long split_queue_len; + bool is_dying; };#endif mm/huge_memory.c +23
-0@@ -1094,9 +1094,15 @@ static struct deferred_split *folio_split_queue_lock(struct folio *folio)struct deferred_split *queue; memcg =folio_memcg (folio); +retry: queue = memcg ? &memcg->deferred_split_queue : &NODE_DATA (folio_nid (folio))->deferred_split_queue;spin_lock (&queue->split_queue_lock); +if (unlikely (queue->is_dying == true)) { +spin_unlock (&queue->split_queue_lock); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }@@ -1108,9 +1114,15 @@ folio_split_queue_lock_irqsave(struct folio *folio, unsigned long *flags)struct deferred_split *queue; memcg =folio_memcg (folio); +retry: queue = memcg ? &memcg->deferred_split_queue : &NODE_DATA (folio_nid (folio))->deferred_split_queue;spin_lock_irqsave (&queue->split_queue_lock, *flags); +if (unlikely (queue->is_dying == true)) { +spin_unlock_irqrestore (&queue->split_queue_lock, *flags); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }@@ -4284,6 +4296,33 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,return split; } +void reparent_deferred_split_queue (struct mem_cgroup *memcg) +{ +struct mem_cgroup *parent =parent_mem_cgroup (memcg); +struct deferred_split *ds_queue = &memcg->deferred_split_queue; +struct deferred_split *parent_ds_queue = &parent->deferred_split_queue; +int nid; + +spin_lock_irq (&ds_queue->split_queue_lock); +spin_lock_nested (&parent_ds_queue->split_queue_lock,SINGLE_DEPTH_NESTING ); + +if (!ds_queue->split_queue_len)
mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 4319
Quoted 10 lines
mm/huge_memory.c +10
-0+ goto unlock; + +list_splice_tail_init (&ds_queue->split_queue, &parent_ds_queue->split_queue); + parent_ds_queue->split_queue_len += ds_queue->split_queue_len; + ds_queue->split_queue_len =0 ; +/* Mark the ds_queue dead */ + ds_queue->is_dying = true; + +for_each_node (nid) +set_shrinker_bit (parent, nid,shrinker_id (deferred_split_shrinker));
Does this loop need to be under locks?
Quoted 14 lines
mm/huge_memory.c +6
-0+ +unlock: +spin_unlock (&parent_ds_queue->split_queue_lock); +spin_unlock_irq (&ds_queue->split_queue_lock); +} +#ifdef CONFIG_DEBUG_FS static void split_huge_pages_all (void ) {mm/memcontrol.c +0
-0
mm/memcontrol.c static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) · line 3891
Quoted 5 lines
mm/memcontrol.c +1
-0@@ -3887,6 +3887,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);
I guess the naming can be a bit more consistent here :)
Thanks!
Re: [PATCH v2 3/4] mm: thp: use folio_batch to handle THP splitting in deferred_split_scan()
In reply to Zi YanHi Zi,
Quoted 24 lines · Zi Yan
Quoted 22 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, this lock protects the local list, not the split queue.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.
Quoted 4 lines
Quoted 1 line
We can use the
folio_batchinfrastructure to handle this clearly. In thisCan you add more details on how
folio_batchhandles the above three concerns in the original code? That would guide me what to look for during code review.
Sure.
For 1), after adding folio to folio_batch, we immediatelly decrement the ds_queue->split_queue_len, so there are no more inconsistencies.
For 2), after adding folio to folio_batch, we will see list_empty() in __folio_split(), so there is no longer a situation where split_queue_lock protects the local list.
For 3), after adding folio to folio_batch, we call folios_put() at the end to decrement the refcount of folios, which looks more natural.
Quoted 40 lines
Quoted 40 lines
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.Signed-off-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>--- mm/huge_memory.c | 84 ++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 46 deletions(-)mm/huge_memory.c +11
-3@@ -3781,21 +3781,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)) {
Quoted 8 lines
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3799
Quoted 4 lines
mm/huge_memory.c +0
-0folio_clear_partially_mapped (folio);mod_mthp_stat (folio_order (folio),MTHP_STAT_NR_ANON_PARTIALLY_MAPPED ,-1 ); }
folio_test_partially_mapped()is done regardless the folio is on_deferred_listor not, is it because the folio can be on a folio batch and its_deferred_listis empty?
Yes.
Quoted 57 lines
Quoted 42 lines
mm/huge_memory.c +7
-15-/* -* 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) {@@ -4194,40 +4195,44 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,struct pglist_data *pgdata =NODE_DATA (sc->nid);struct deferred_split *ds_queue = &pgdata->deferred_split_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;#ifdef CONFIG_MEMCG if (sc->memcg) ds_queue = &sc->memcg->deferred_split_queue;#endif +folio_batch_init (&fbatch); +retry:spin_lock_irqsave (&ds_queue->split_queue_lock, 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),mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 4222
Quoted 10 lines
mm/huge_memory.c +5
-4- 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--;At this point, the folio can be following conditions:
1.deferred_split_scan()gets it,
2. it is freed byfolio_put().
Quoted 1 line
In both cases, it is removed from
deferred_split_queue, right?
Right. For the case 1), we may add folio back to deferred_split_queue.
Quoted 43 lines
Quoted 43 lines
mm/huge_memory.c +7
-14if (!--sc->nr_to_scan)break ; +if (!folio_batch_space (&fbatch)) +break ; }spin_unlock_irqrestore (&ds_queue->split_queue_lock, 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 @@ -4250,38 +4255,25 @@ 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. -*/
Quoted 8 lines
mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 4269
Quoted 5 lines
mm/huge_memory.c +4
-1-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++;fqueue should be the same as
ds_queue, right? Just want to make sure I understand the code.
After patch #4, fqueue may be the deferred_split of parent memcg.
Thanks,
Qi
Quoted 26 lines
Quoted 22 lines
mm/huge_memory.c +4
-9} -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) + goto retry;/* * Stop shrinker if we didn't split any page, but the queue is empty. Signature · 1 line
2.20.1Best Regards,
Yan, Zi
Re: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
In reply to Zi YanQuoted 92 lines · Zi Yan
Quoted 90 lines · Qi Zheng
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.
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 same mechanism aslist_lruto reparent the split queue separately when memcg is offine.Signed-off-by Qi Zheng <[email protected]>--- include/linux/huge_mm.h | 2 ++ include/linux/mmzone.h | 1 + mm/huge_memory.c | 39 +++++++++++++++++++++++++++++++++++++++ mm/memcontrol.c | 1 + mm/mm_init.c | 1 + 5 files changed, 44 insertions(+)include/linux/huge_mm.h +2
-0@@ -417,6 +417,7 @@ 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); +void reparent_deferred_split_queue (struct mem_cgroup *memcg);void __split_huge_pmd (struct vm_area_struct *vma,pmd_t *pmd,unsigned long address,bool freeze);@@ -611,6 +612,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/mmzone.h +1
-0@@ -1346,6 +1346,7 @@ struct deferred_split {spinlock_t split_queue_lock;struct list_head split_queue;unsigned long split_queue_len; + bool is_dying; };#endif mm/huge_memory.c +12
-0@@ -1094,9 +1094,15 @@ static struct deferred_split *folio_split_queue_lock(struct folio *folio)struct deferred_split *queue; memcg =folio_memcg (folio); +retry: queue = memcg ? &memcg->deferred_split_queue : &NODE_DATA (folio_nid (folio))->deferred_split_queue;spin_lock (&queue->split_queue_lock); +if (unlikely (queue->is_dying == true)) { +spin_unlock (&queue->split_queue_lock); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }@@ -1108,9 +1114,15 @@ folio_split_queue_lock_irqsave(struct folio *folio, unsigned long *flags)struct deferred_split *queue; memcg =folio_memcg (folio); +retry: queue = memcg ? &memcg->deferred_split_queue : &NODE_DATA (folio_nid (folio))->deferred_split_queue;spin_lock_irqsave (&queue->split_queue_lock, *flags); +if (unlikely (queue->is_dying == true)) { +spin_unlock_irqrestore (&queue->split_queue_lock, *flags); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }@@ -4284,6 +4296,33 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,return split; }
Quoted 16 lines
mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 4310
Quoted 12 lines
mm/huge_memory.c +12
-0+void reparent_deferred_split_queue (struct mem_cgroup *memcg) +{ +struct mem_cgroup *parent =parent_mem_cgroup (memcg); +struct deferred_split *ds_queue = &memcg->deferred_split_queue; +struct deferred_split *parent_ds_queue = &parent->deferred_split_queue; +int nid; + +spin_lock_irq (&ds_queue->split_queue_lock); +spin_lock_nested (&parent_ds_queue->split_queue_lock,SINGLE_DEPTH_NESTING ); + +if (!ds_queue->split_queue_len) + goto unlock;Should
ds_queuestill be marked as dying even if it is empty? Otherwise, new folios still can be added to it, based on my understanding of the changes to folio_split_queue_lock*().
I think you are right, will do in the next version.
Thanks,
Qi
Quoted 49 lines
Quoted 45 lines
mm/huge_memory.c +15
-0+ +list_splice_tail_init (&ds_queue->split_queue, &parent_ds_queue->split_queue); + parent_ds_queue->split_queue_len += ds_queue->split_queue_len; + ds_queue->split_queue_len =0 ; +/* Mark the ds_queue dead */ + ds_queue->is_dying = true; + +for_each_node (nid) +set_shrinker_bit (parent, nid,shrinker_id (deferred_split_shrinker)); + +unlock: +spin_unlock (&parent_ds_queue->split_queue_lock); +spin_unlock_irq (&ds_queue->split_queue_lock); +} +#ifdef CONFIG_DEBUG_FS static void split_huge_pages_all (void ) {mm/memcontrol.c +1
-0@@ -3887,6 +3887,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);mm/mm_init.c +1
-0@@ -1387,6 +1387,7 @@ static void pgdat_init_split_queue(struct pglist_data *pgdat)spin_lock_init (&ds_queue->split_queue_lock);INIT_LIST_HEAD (&ds_queue->split_queue); ds_queue->split_queue_len =0 ; + ds_queue->is_dying = false; }#else static void pgdat_init_split_queue (struct pglist_data *pgdat) {}Signature · 1 line
2.20.1Best Regards,
Yan, Zi
Re: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
In reply to Roman GushchinHi Roman,
Quoted 8 lines · Roman Gushchin
Quoted 3 lines · Qi Zheng
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.
Nit: commit logs should really focus on the actual change, not the future plans.
Got it.
Quoted 103 lines
Quoted 103 lines
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 same mechanism aslist_lruto reparent the split queue separately when memcg is offine.Signed-off-by Qi Zheng <[email protected]>--- include/linux/huge_mm.h | 2 ++ include/linux/mmzone.h | 1 + mm/huge_memory.c | 39 +++++++++++++++++++++++++++++++++++++++ mm/memcontrol.c | 1 + mm/mm_init.c | 1 + 5 files changed, 44 insertions(+)include/linux/huge_mm.h +2
-0@@ -417,6 +417,7 @@ 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); +void reparent_deferred_split_queue (struct mem_cgroup *memcg);void __split_huge_pmd (struct vm_area_struct *vma,pmd_t *pmd,unsigned long address,bool freeze);@@ -611,6 +612,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/mmzone.h +1
-0@@ -1346,6 +1346,7 @@ struct deferred_split {spinlock_t split_queue_lock;struct list_head split_queue;unsigned long split_queue_len; + bool is_dying; };#endif mm/huge_memory.c +27
-0@@ -1094,9 +1094,15 @@ static struct deferred_split *folio_split_queue_lock(struct folio *folio)struct deferred_split *queue; memcg =folio_memcg (folio); +retry: queue = memcg ? &memcg->deferred_split_queue : &NODE_DATA (folio_nid (folio))->deferred_split_queue;spin_lock (&queue->split_queue_lock); +if (unlikely (queue->is_dying == true)) { +spin_unlock (&queue->split_queue_lock); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }@@ -1108,9 +1114,15 @@ folio_split_queue_lock_irqsave(struct folio *folio, unsigned long *flags)struct deferred_split *queue; memcg =folio_memcg (folio); +retry: queue = memcg ? &memcg->deferred_split_queue : &NODE_DATA (folio_nid (folio))->deferred_split_queue;spin_lock_irqsave (&queue->split_queue_lock, *flags); +if (unlikely (queue->is_dying == true)) { +spin_unlock_irqrestore (&queue->split_queue_lock, *flags); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }@@ -4284,6 +4296,33 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,return split; } +void reparent_deferred_split_queue (struct mem_cgroup *memcg) +{ +struct mem_cgroup *parent =parent_mem_cgroup (memcg); +struct deferred_split *ds_queue = &memcg->deferred_split_queue; +struct deferred_split *parent_ds_queue = &parent->deferred_split_queue; +int nid; + +spin_lock_irq (&ds_queue->split_queue_lock); +spin_lock_nested (&parent_ds_queue->split_queue_lock,SINGLE_DEPTH_NESTING ); + +if (!ds_queue->split_queue_len) + goto unlock; + +list_splice_tail_init (&ds_queue->split_queue, &parent_ds_queue->split_queue); + parent_ds_queue->split_queue_len += ds_queue->split_queue_len;
Quoted 8 lines
mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 4319
Quoted 6 lines
mm/huge_memory.c +6
-0+ ds_queue->split_queue_len =0 ; +/* Mark the ds_queue dead */ + ds_queue->is_dying = true; + +for_each_node (nid) +set_shrinker_bit (parent, nid,shrinker_id (deferred_split_shrinker));Does this loop need to be under locks?
I think it is not necessary, but the loop overhead should not be high.
Quoted 17 lines
Quoted 17 lines
mm/huge_memory.c +6
-0+ +unlock: +spin_unlock (&parent_ds_queue->split_queue_lock); +spin_unlock_irq (&ds_queue->split_queue_lock); +} +#ifdef CONFIG_DEBUG_FS static void split_huge_pages_all (void ) {mm/memcontrol.c +0
-0@@ -3887,6 +3887,7 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)zswap_memcg_offline_cleanup (memcg);
Quoted 5 lines
mm/memcontrol.c static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) · line 3891
Quoted 3 lines
mm/memcontrol.c +1
-0memcg_offline_kmem (memcg); +reparent_deferred_split_queue (memcg);reparent_shrinker_deferred (memcg);I guess the naming can be a bit more consistent here :)
Do you mean to change them all to:
memcg_offline_xxx()
or
reparent_xxx() ?
Thanks,
Qi
Quoted 2 lines
Thanks!
Re: [PATCH v2 3/4] mm: thp: use folio_batch to handle THP splitting in deferred_split_scan()
In reply to Qi ZhengQuoted 28 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, this lock protects the local list, not the split queue.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).
Quoted 8 lines
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]>---
Nothing jumped at me
Signature · 3 lines
Cheers David / dhildenb
Re: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
In reply to Qi ZhengQuoted 43 lines · Qi Zheng
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.
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 same mechanism aslist_lruto reparent the split queue separately when memcg is offine.Signed-off-by Qi Zheng <[email protected]>--- include/linux/huge_mm.h | 2 ++ include/linux/mmzone.h | 1 + mm/huge_memory.c | 39 +++++++++++++++++++++++++++++++++++++++ mm/memcontrol.c | 1 + mm/mm_init.c | 1 + 5 files changed, 44 insertions(+)include/linux/huge_mm.h +2
-0@@ -417,6 +417,7 @@ 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); +void reparent_deferred_split_queue (struct mem_cgroup *memcg);void __split_huge_pmd (struct vm_area_struct *vma,pmd_t *pmd,unsigned long address,bool freeze);@@ -611,6 +612,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/mmzone.h +0
-0
include/linux/mmzone.h struct deferred_split { · line 1349
Quoted 4 lines
include/linux/mmzone.h +1
-0@@ -1346,6 +1346,7 @@ struct deferred_split {spinlock_t split_queue_lock;struct list_head split_queue;unsigned long split_queue_len; +bool is_dying;
It's a bit weird to query whether the "struct deferred_split" is dying. Shouldn't this be a memcg property? (and in particular, not exist for the pglist_data part where it might not make sense at all?).
Quoted 8 lines
include/linux/mmzone.h +0
-0};#endif mm/huge_memory.c +0
-0
mm/huge_memory.c static struct deferred_split *folio_split_queue_lock(struct folio *folio) · line 1101
Quoted 8 lines
mm/huge_memory.c +2
-0@@ -1094,9 +1094,15 @@ static struct deferred_split *folio_split_queue_lock(struct folio *folio)struct deferred_split *queue; memcg =folio_memcg (folio); +retry: queue = memcg ? &memcg->deferred_split_queue : &NODE_DATA (folio_nid (folio))->deferred_split_queue;spin_lock (&queue->split_queue_lock); +if (unlikely (queue->is_dying == true)) {
if (unlikely(queue->is_dying))
Quoted 8 lines
mm/huge_memory.c +4
-0+spin_unlock (&queue->split_queue_lock); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }
mm/huge_memory.c folio_split_queue_lock_irqsave(struct folio *folio, unsigned long *flags) · line 1121
Quoted 8 lines
mm/huge_memory.c +2
-0@@ -1108,9 +1114,15 @@ folio_split_queue_lock_irqsave(struct folio *folio, unsigned long *flags)struct deferred_split *queue; memcg =folio_memcg (folio); +retry: queue = memcg ? &memcg->deferred_split_queue : &NODE_DATA (folio_nid (folio))->deferred_split_queue;spin_lock_irqsave (&queue->split_queue_lock, *flags); +if (unlikely (queue->is_dying == true)) {
if (unlikely(queue->is_dying))
mm/huge_memory.c folio_split_queue_lock_irqsave(struct folio *folio, unsigned long *flags) · line 1128
Quoted 7 lines
mm/huge_memory.c +4
-0+spin_unlock_irqrestore (&queue->split_queue_lock, *flags); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }
Nothing else jumped at me, but I am not a memcg expert :)
Signature · 3 lines
Cheers David / dhildenb
kernel test robot
build botRe: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
In reply to Qi Zhengkernel test robot noticed the following build errors:
Hi Qi,
kernel test robot noticed the following build errors:
[auto build test ERROR on next-20250922]
[also build test ERROR on v6.17-rc7]
[cannot apply to akpm-mm/mm-everything rppt-memblock/for-next rppt-memblock/fixes linus/master v6.17-rc7 v6.17-rc6 v6.17-rc5] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Qi-Zheng/mm-thp-replace-folio_memcg-with-folio_memcg_charged/20250923-171935 base: next-20250922 patch link: https://lore.kernel.org/r/55370bda7b2df617033ac12116c1712144bb7591.1758618527.git.zhengqi.arch%40bytedance.com patch subject: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline config: riscv-randconfig-001-20250924 (https://download.01.org/0day-ci/archive/20250924/[email protected]/config) compiler: riscv64-linux-gcc (GCC) 8.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250924/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
mm/huge_memory.c: In function 'reparent_deferred_split_queue':
>> mm/huge_memory.c:4302:42: error: dereferencing pointer to incomplete type 'struct mem_cgroup'
struct deferred_split *ds_queue = &memcg->deferred_split_queue;
^~Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for ARCH_HAS_ELF_CORE_EFLAGS Depends on [n]: BINFMT_ELF [=n] && ELF_CORE [=n]
Selected by [y]:
- RISCV [=y]
vim +4302 mm/huge_memory.c
4298
4299 void reparent_deferred_split_queue(struct mem_cgroup *memcg)
4300 {
4301 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
> 4302 struct deferred_split *ds_queue = &memcg->deferred_split_queue;
4303 struct deferred_split *parent_ds_queue = &parent->deferred_split_queue;
4304 int nid;
4305
4306 spin_lock_irq(&ds_queue->split_queue_lock);
4307 spin_lock_nested(&parent_ds_queue->split_queue_lock, SINGLE_DEPTH_NESTING);
4308
4309 if (!ds_queue->split_queue_len)
4310 goto unlock;
4311
4312 list_splice_tail_init(&ds_queue->split_queue, &parent_ds_queue->split_queue);
4313 parent_ds_queue->split_queue_len += ds_queue->split_queue_len;
4314 ds_queue->split_queue_len = 0;
4315 /* Mark the ds_queue dead */
4316 ds_queue->is_dying = true;
4317
4318 for_each_node(nid)
4319 set_shrinker_bit(parent, nid, shrinker_id(deferred_split_shrinker));
4320
4321 unlock:
4322 spin_unlock(&parent_ds_queue->split_queue_lock);
4323 spin_unlock_irq(&ds_queue->split_queue_lock);
4324 }
4325
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
In reply to Qi ZhengQuoted 43 lines · Qi Zheng
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.
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 same mechanism aslist_lruto reparent the split queue separately when memcg is offine.Signed-off-by Qi Zheng <[email protected]>--- include/linux/huge_mm.h | 2 ++ include/linux/mmzone.h | 1 + mm/huge_memory.c | 39 +++++++++++++++++++++++++++++++++++++++ mm/memcontrol.c | 1 + mm/mm_init.c | 1 + 5 files changed, 44 insertions(+)include/linux/huge_mm.h +2
-0@@ -417,6 +417,7 @@ 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); +void reparent_deferred_split_queue (struct mem_cgroup *memcg);void __split_huge_pmd (struct vm_area_struct *vma,pmd_t *pmd,unsigned long address,bool freeze);@@ -611,6 +612,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/mmzone.h +0
-0
include/linux/mmzone.h struct deferred_split { · line 1351
Quoted 6 lines
include/linux/mmzone.h +1
-0@@ -1346,6 +1346,7 @@ struct deferred_split {spinlock_t split_queue_lock;struct list_head split_queue;unsigned long split_queue_len; + bool is_dying; };#endif
The scheme in Muchun's version was:
retry:
queue = folio_split_queue(folio);spin_lock(&queue->split_queue_lock);
if (folio_memcg(folio) != folio_split_queue_memcg(folio, queue)) {
/* split queue was reparented, retry */
spin_unlock(&queue->split_queue_lock);
goto retry;
}/* now we have a stable mapping between the folio and the split queue */ spin_unlock(&queue->split_queue_lock);
Oh, I see. We can't use this scheme yet because we don't reparent LRU folios. (I was wondering why we're adding is_dying property)
mm/huge_memory.c static struct deferred_split *folio_split_queue_lock(struct folio *folio) · line 1094
Quoted 6 lines
mm/huge_memory.c +0
-0@@ -1094,9 +1094,15 @@ static struct deferred_split *folio_split_queue_lock(struct folio *folio)struct deferred_split *queue;
For now it's safe to not call rcu_read_lock() here because memcgs won't disappear under us as long as there are folios to split (we don't reparent LRU folios), right?
Quoted 12 lines
mm/huge_memory.c +6
-0memcg =folio_memcg (folio); +retry: queue = memcg ? &memcg->deferred_split_queue : &NODE_DATA (folio_nid (folio))->deferred_split_queue;spin_lock (&queue->split_queue_lock); +if (unlikely (queue->is_dying == true)) { +spin_unlock (&queue->split_queue_lock); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }
Signature · 2 lines
Cheers, Harry / Hyeonggon
Re: [PATCH v2 3/4] mm: thp: use folio_batch to handle THP splitting in deferred_split_scan()
In reply to Qi ZhengQuoted 208 lines · Qi Zheng
Hi Zi,
Quoted 24 lines · Zi Yan
Quoted 22 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, this lock protects the local list, not the split queue.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.Quoted 4 lines
Quoted 1 line
We can use the
folio_batchinfrastructure to handle this clearly. In thisCan you add more details on how
folio_batchhandles the above three concerns in the original code? That would guide me what to look for during code review.Sure.
For 1), after adding folio to
folio_batch, we immediatelly decrement theds_queue->split_queue_len, so there are no more inconsistencies.For 2), after adding folio to
folio_batch, we will seelist_empty()in__folio_split(), so there is no longer a situation wheresplit_queue_lockprotects the local list.For 3), after adding folio to
folio_batch, we callfolios_put()at the end to decrement the refcount of folios, which looks more natural.Quoted 40 lines
Quoted 40 lines
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.Signed-off-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>--- mm/huge_memory.c | 84 ++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 46 deletions(-)mm/huge_memory.c +11
-3@@ -3781,21 +3781,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)) {Quoted 8 lines
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3799
Quoted 4 lines
mm/huge_memory.c +0
-0folio_clear_partially_mapped (folio);mod_mthp_stat (folio_order (folio),MTHP_STAT_NR_ANON_PARTIALLY_MAPPED ,-1 ); }
folio_test_partially_mapped()is done regardless the folio is on_deferred_listor not, is it because the folio can be on a folio batch and its_deferred_listis empty?Yes.
Quoted 57 lines
Quoted 42 lines
mm/huge_memory.c +7
-15-/* -* 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) {@@ -4194,40 +4195,44 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,struct pglist_data *pgdata =NODE_DATA (sc->nid);struct deferred_split *ds_queue = &pgdata->deferred_split_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;#ifdef CONFIG_MEMCG if (sc->memcg) ds_queue = &sc->memcg->deferred_split_queue;#endif +folio_batch_init (&fbatch); +retry:spin_lock_irqsave (&ds_queue->split_queue_lock, 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),mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 4222
Quoted 10 lines
mm/huge_memory.c +5
-4- 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--;At this point, the folio can be following conditions:
1.deferred_split_scan()gets it,
2. it is freed byfolio_put().Quoted 1 line
In both cases, it is removed from
deferred_split_queue, right?Right. For the case 1), we may add folio back to
deferred_split_queue.Quoted 43 lines
Quoted 43 lines
mm/huge_memory.c +7
-14if (!--sc->nr_to_scan)break ; +if (!folio_batch_space (&fbatch)) +break ; }spin_unlock_irqrestore (&ds_queue->split_queue_lock, 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 @@ -4250,38 +4255,25 @@ 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. -*/ Quoted 8 lines
mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 4269
Quoted 5 lines
mm/huge_memory.c +4
-1-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++;fqueue should be the same as
ds_queue, right? Just want to make sure I understand the code.
Quoted 1 line
After patch #4, fqueue may be the
deferred_splitof parent memcg.
Thank you for the explanation. The changes look good to me.
Best Regards,
Yan, Zi
Re: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
In reply to David HildenbrandHi David,
Quoted 41 lines · David Hildenbrand
Quoted 40 lines · Qi Zheng
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.
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 same mechanism aslist_lruto reparent the split queue separately when memcg is offine.Signed-off-by Qi Zheng <[email protected]>---
include/linux/huge_mm.h| 2 ++
include/linux/mmzone.h | 1 +
mm/huge_memory.c | 39 +++++++++++++++++++++++++++++++++++++++ mm/memcontrol.c | 1 +
mm/mm_init.c | 1 +5 files changed, 44 insertions(+)include/linux/huge_mm.h +2
-0@@ -417,6 +417,7 @@ 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); +void reparent_deferred_split_queue (struct mem_cgroup *memcg); void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, unsigned long address, bool freeze);@@ -611,6 +612,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/mmzone.h +0
-1--- a/include/linux/mmzone.h
Quoted 8 lines
include/linux/mmzone.h struct deferred_split { · line 1349
Quoted 6 lines
+++
b/include/linux/mmzone.hinclude/linux/mmzone.h +1
-0@@ -1346,6 +1346,7 @@ struct deferred_split { spinlock_t split_queue_lock; struct list_head split_queue; unsigned long split_queue_len; + bool is_dying;It's a bit weird to query whether the "struct
deferred_split" is dying. Shouldn't this be a memcg property? (and in particular, not exist for
There is indeed a CSS_DYING flag. But we must modify 'is_dying' under the protection of the split_queue_lock, otherwise the folio may be added back to the deferred_split of child memcg.
Quoted 1 line
the
pglist_datapart where it might not make sense at all?).
Maybe:
#ifdef CONFIG_MEMCG
bool is_dying;#endif
Quoted 10 lines
Quoted 10 lines
include/linux/mmzone.h +0
-0}; #endifmm/huge_memory.c +0
-0@@ -1094,9 +1094,15 @@ static struct deferred_split *folio_split_queue_lock(struct folio *folio) struct deferred_split *queue;
Quoted 8 lines
Quoted 6 lines
mm/huge_memory.c +1
-0memcg = folio_memcg(folio); +retry:queue = memcg ?
&memcg->deferred_split_queue:
&NODE_DATA(folio_nid(folio))->deferred_split_queue;spin_lock(&queue->split_queue_lock);
+ if (unlikely(queue->is_dying == true)) {if (
unlikely(queue->is_dying))
Will do.
Quoted 10 lines
Quoted 10 lines
+
spin_unlock(&queue->split_queue_lock);
+ memcg =parent_mem_cgroup(memcg);
+ goto retry;
+ }
return queue;
}mm/huge_memory.c +0
-0@@ -1108,9 +1114,15 @@ folio_split_queue_lock_irqsave(struct folio *folio, unsigned long *flags) struct deferred_split *queue;
Quoted 8 lines
Quoted 6 lines
mm/huge_memory.c +1
-0memcg = folio_memcg(folio); +retry:queue = memcg ?
&memcg->deferred_split_queue:
&NODE_DATA(folio_nid(folio))->deferred_split_queue;spin_lock_irqsave(&queue->split_queue_lock, *flags); + if (unlikely(queue->is_dying == true)) {if (
unlikely(queue->is_dying))
Will do.
Quoted 9 lines
Quoted 7 lines
+
spin_unlock_irqrestore(&queue->split_queue_lock, *flags); + memcg =parent_mem_cgroup(memcg);
+ goto retry;
+ }
return queue;
}Nothing else jumped at me, but I am not a memcg expert :)
Thanks,
Qi
Quoted 1 line
Re: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
In reply to Harry YooHi Harry,
Quoted 61 lines · Harry Yoo
Quoted 43 lines · Qi Zheng
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.
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 same mechanism aslist_lruto reparent the split queue separately when memcg is offine.Signed-off-by Qi Zheng <[email protected]>--- include/linux/huge_mm.h | 2 ++ include/linux/mmzone.h | 1 + mm/huge_memory.c | 39 +++++++++++++++++++++++++++++++++++++++ mm/memcontrol.c | 1 + mm/mm_init.c | 1 + 5 files changed, 44 insertions(+)include/linux/huge_mm.h +2
-0@@ -417,6 +417,7 @@ 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); +void reparent_deferred_split_queue (struct mem_cgroup *memcg);void __split_huge_pmd (struct vm_area_struct *vma,pmd_t *pmd,unsigned long address,bool freeze);@@ -611,6 +612,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/mmzone.h +0
-0include/linux/mmzone.h struct deferred_split { · line 1351
Quoted 6 lines
include/linux/mmzone.h +1
-0@@ -1346,6 +1346,7 @@ struct deferred_split {spinlock_t split_queue_lock;struct list_head split_queue;unsigned long split_queue_len; + bool is_dying; };#endif The scheme in Muchun's version was:
retry:
queue =folio_split_queue(folio);spin_lock(&queue->split_queue_lock);if (folio_memcg(folio) != folio_split_queue_memcg(folio, queue)) { /* split queue was reparented, retry */ spin_unlock(&queue->split_queue_lock); goto retry; }
Quoted 5 lines
/* now we have a stable mapping between the folio and the split queue */
spin_unlock(&queue->split_queue_lock);Oh, I see. We can't use this scheme yet because we don't reparent LRU folios. (I was wondering why we're adding
is_dyingproperty)
Right. And reparenting THP split queue independently can avoid the following situations:
``` acquire child and parent split_queue_lock acquire child and parent objcg_lock acquire child and parent lru lock reparent THP split queue reparent objcg reparent LRU folios release child and parent lru lock release child and parent objcg_lock release child and parent split_queue_lock ```
Quoted 12 lines
mm/huge_memory.c static struct deferred_split *folio_split_queue_lock(struct folio *folio) · line 1094
Quoted 7 lines
mm/huge_memory.c +0
-0@@ -1094,9 +1094,15 @@ static struct deferred_split *folio_split_queue_lock(struct folio *folio)struct deferred_split *queue;For now it's safe to not call
rcu_read_lock()here because memcgs won't disappear under us as long as there are folios to split (we don't reparent LRU folios), right?
Right. We will add rcu_read_lock() when reparenting LRU folios.
Thanks,
Qi
Quoted 14 lines
Quoted 13 lines
mm/huge_memory.c +6
-0memcg =folio_memcg (folio); +retry: queue = memcg ? &memcg->deferred_split_queue : &NODE_DATA (folio_nid (folio))->deferred_split_queue;spin_lock (&queue->split_queue_lock); +if (unlikely (queue->is_dying == true)) { +spin_unlock (&queue->split_queue_lock); + memcg =parent_mem_cgroup (memcg); + goto retry; + }return queue; }
Re: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
In reply to Qi ZhengQuoted 1 line · Qi Zheng
Hi David,
Hi :)
[...]
Quoted 10 lines
Quoted 9 lines
include/linux/mmzone.h struct deferred_split { · line 1349
Quoted 6 lines
+++
b/include/linux/mmzone.hinclude/linux/mmzone.h +1
-0@@ -1346,6 +1346,7 @@ struct deferred_split { spinlock_t split_queue_lock; struct list_head split_queue; unsigned long split_queue_len; + bool is_dying;It's a bit weird to query whether the "struct
deferred_split" is dying. Shouldn't this be a memcg property? (and in particular, not exist for
Quoted 3 lines
There is indeed a
CSS_DYINGflag. But we must modify 'is_dying' under the protection of thesplit_queue_lock, otherwise the folio may be added back to thedeferred_splitof child memcg.
Is there no way to reuse the existing mechanisms, and find a way to have the shrinker / queue locking sync against that?
There is also the offline_css() function where we clear CSS_ONLINE. But it happens after calling ss->css_offline(css);
Being able to query "is the memcg going offline" and having a way to sync against that would be probably cleanest.
I'll let all the memcg people comment on how that could be done best.
Quoted 9 lines
Quoted 2 lines
the
pglist_datapart where it might not make sense at all?).Maybe:
#ifdef CONFIG_MEMCG bool is_dying;#endif
Still doesn't quite look like it would belong here :(
Also, is "dying" really the right terminology? It's more like "going offline"?
But then, the queue is not going offline, the memcg is ...
Signature · 3 lines
Cheers David / dhildenb
Zi Yan
reviewerRe: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
Quoted 17 lines · David Hildenbrand
Quoted 1 line · Qi Zheng
Hi David,
Hi :)
[...]
Quoted 10 lines
Quoted 9 lines
include/linux/mmzone.h struct deferred_split { · line 1349
Quoted 6 lines
+++
b/include/linux/mmzone.hinclude/linux/mmzone.h +1
-0@@ -1346,6 +1346,7 @@ struct deferred_split { spinlock_t split_queue_lock; struct list_head split_queue; unsigned long split_queue_len; + bool is_dying;It's a bit weird to query whether the "struct
deferred_split" is dying. Shouldn't this be a memcg property? (and in particular, not exist for
Quoted 7 lines
Quoted 3 lines
There is indeed a
CSS_DYINGflag. But we must modify 'is_dying' under the protection of thesplit_queue_lock, otherwise the folio may be added back to thedeferred_splitof child memcg.Is there no way to reuse the existing mechanisms, and find a way to have the shrinker / queue locking sync against that?
There is also the
offline_css()function where we clearCSS_ONLINE. But it happens after callingss->css_offline(css);
I see CSS_DYING will be set by kill_css() before offline_css() is called. Probably the code can check CSS_DYING instead.
Quoted 2 lines
Being able to query "is the memcg going offline" and having a way to sync against that would be probably cleanest.
So basically, something like:
1. at folio_split_queue_lock*() time, get folio’s memcg or its parent memcg until there is no CSS_DYING set or CSS_ONLINE is set.
2. return the associated deferred_split_queue.
Quoted 13 lines
I'll let all the memcg people comment on how that could be done best.
Quoted 9 lines
Quoted 2 lines
the
pglist_datapart where it might not make sense at all?).Maybe:
#ifdef CONFIG_MEMCG bool is_dying;#endif
Quoted 10 lines
Still doesn't quite look like it would belong here :(
Also, is "dying" really the right terminology? It's more like "going offline"?
But then, the queue is not going offline, the memcg is ...
Signature · 3 lines
Cheers David / dhildenb
Best Regards,
Yan, Zi
Shakeel Butt
maintainerRe: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
Quoted 31 lines · Zi Yan
Quoted 17 lines · David Hildenbrand
Quoted 1 line · Qi Zheng
Hi David,
Hi :)
[...]
Quoted 10 lines
Quoted 9 lines
include/linux/mmzone.h struct deferred_split { · line 1349
Quoted 6 lines
+++
b/include/linux/mmzone.hinclude/linux/mmzone.h +1
-0@@ -1346,6 +1346,7 @@ struct deferred_split { spinlock_t split_queue_lock; struct list_head split_queue; unsigned long split_queue_len; + bool is_dying;It's a bit weird to query whether the "struct
deferred_split" is dying. Shouldn't this be a memcg property? (and in particular, not exist forQuoted 7 lines
Quoted 3 lines
There is indeed a
CSS_DYINGflag. But we must modify 'is_dying' under the protection of thesplit_queue_lock, otherwise the folio may be added back to thedeferred_splitof child memcg.Is there no way to reuse the existing mechanisms, and find a way to have the shrinker / queue locking sync against that?
There is also the
offline_css()function where we clearCSS_ONLINE. But it happens after callingss->css_offline(css);I see
CSS_DYINGwill be set bykill_css()beforeoffline_css()is called. Probably the code can checkCSS_DYINGinstead.Quoted 1 line
Quoted 7 lines
Quoted 1 line
Being able to query "is the memcg going offline" and having a way to sync against that would be probably cleanest.
So basically, something like:
1. at folio_split_queue_lock*() time, get folio’s memcg or its parent memcg until there is noCSS_DYINGset orCSS_ONLINEis set.
2. return the associateddeferred_split_queue.
Yes, css_is_dying() can be used but please note that there is a rcu grace period between setting CSS_DYING and clearing CSS_ONLINE (i.e. reparenting deferred split queue) and during that period the deferred split THPs of the dying memcg will be hidden from shrinkers (which might be fine).
Shakeel Butt
maintainerRe: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
Quoted 40 lines · Shakeel Butt
Quoted 31 lines · Zi Yan
Quoted 17 lines · David Hildenbrand
Quoted 1 line · Qi Zheng
Hi David,
Hi :)
[...]
Quoted 10 lines
Quoted 9 lines
include/linux/mmzone.h struct deferred_split { · line 1349
Quoted 6 lines
+++
b/include/linux/mmzone.hinclude/linux/mmzone.h +1
-0@@ -1346,6 +1346,7 @@ struct deferred_split { spinlock_t split_queue_lock; struct list_head split_queue; unsigned long split_queue_len; + bool is_dying;It's a bit weird to query whether the "struct
deferred_split" is dying. Shouldn't this be a memcg property? (and in particular, not exist forQuoted 7 lines
Quoted 3 lines
There is indeed a
CSS_DYINGflag. But we must modify 'is_dying' under the protection of thesplit_queue_lock, otherwise the folio may be added back to thedeferred_splitof child memcg.Is there no way to reuse the existing mechanisms, and find a way to have the shrinker / queue locking sync against that?
There is also the
offline_css()function where we clearCSS_ONLINE. But it happens after callingss->css_offline(css);I see
CSS_DYINGwill be set bykill_css()beforeoffline_css()is called. Probably the code can checkCSS_DYINGinstead.Quoted 1 line
Quoted 7 lines
Quoted 1 line
Being able to query "is the memcg going offline" and having a way to sync against that would be probably cleanest.
So basically, something like:
1. at folio_split_queue_lock*() time, get folio’s memcg or its parent memcg until there is noCSS_DYINGset orCSS_ONLINEis set.
2. return the associateddeferred_split_queue.
Quoted 5 lines
Yes,
css_is_dying()can be used but please note that there is a rcu grace period between settingCSS_DYINGand clearingCSS_ONLINE(i.e. reparenting deferred split queue) and during that period the deferred split THPs of the dying memcg will be hidden from shrinkers (which might be fine).
BTW if this period is not acceptable and we don't want to add is_dying to struct deferred_split, we can use something similar to what list_lru does in the similar situation i.e. set a special value (LONG_MIN) in its nr_items variable. That is make split_queue_len a long and set it to LONG_MIN during memcg offlining/reparenting.
Re: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
Quoted 41 lines · Shakeel Butt
Quoted 40 lines · Shakeel Butt
Quoted 38 lines · Zi Yan
Quoted 17 lines · David Hildenbrand
Quoted 1 line · Qi Zheng
Hi David,
Hi :)
[...]
Quoted 10 lines
Quoted 9 lines
Review struct deferred_split { · line 1349
Quoted 6 lines
+++
b/include/linux/mmzone.h(quoted diff) +1
-0@@ -1346,6 +1346,7 @@ struct deferred_split { spinlock_t split_queue_lock; struct list_head split_queue; unsigned long split_queue_len; + bool is_dying;It's a bit weird to query whether the "struct
deferred_split" is dying. Shouldn't this be a memcg property? (and in particular, not exist forQuoted 7 lines
Quoted 3 lines
There is indeed a
CSS_DYINGflag. But we must modify 'is_dying' under the protection of thesplit_queue_lock, otherwise the folio may be added back to thedeferred_splitof child memcg.Is there no way to reuse the existing mechanisms, and find a way to have the shrinker / queue locking sync against that?
There is also the
offline_css()function where we clearCSS_ONLINE. But it happens after callingss->css_offline(css);I see
CSS_DYINGwill be set bykill_css()beforeoffline_css()is called. Probably the code can checkCSS_DYINGinstead.Quoted 2 lines
Being able to query "is the memcg going offline" and having a way to sync against that would be probably cleanest.
So basically, something like:
1. at folio_split_queue_lock*() time, get folio’s memcg or
its parent memcg until there is noCSS_DYINGset orCSS_ONLINEis set.
2. return the associateddeferred_split_queue.
Quoted 5 lines
Quoted 5 lines
Yes,
css_is_dying()can be used but please note that there is a rcu grace period between settingCSS_DYINGand clearingCSS_ONLINE(i.e. reparenting deferred split queue) and during that period the deferred split THPs of the dying memcg will be hidden from shrinkers (which might be fine).
My mistake, now I think using css_is_dying() is safe.
Quoted 6 lines
BTW if this period is not acceptable and we don't want to add
is_dyingto structdeferred_split, we can use something similar to whatlist_lrudoes in the similar situation i.e. set a special value (LONG_MIN) in itsnr_itemsvariable. That is makesplit_queue_lena long and set it toLONG_MINduring memcg offlining/reparenting.
I've considered this option, but I am concerned about the risk of overflow.
So I will try to use css_is_dying() in the next version.
Thanks,
Qi
Shakeel Butt
maintainerRe: [PATCH v2 4/4] mm: thp: reparent the split queue during memcg offline
Quoted 3 lines · Qi Zheng
On 9/26/25 6:35 AM, Shakeel Butt wrote:
[...]
Quoted 10 lines
Quoted 6 lines
Quoted 6 lines
Yes,
css_is_dying()can be used but please note that there is a rcu grace period between settingCSS_DYINGand clearingCSS_ONLINE(i.e. reparenting deferred split queue) and during that period the deferred split THPs of the dying memcg will be hidden from shrinkers (which might be fine).My mistake, now I think using
css_is_dying()is safe.Quoted 1 line
Quoted 8 lines
Quoted 5 lines
BTW if this period is not acceptable and we don't want to add
is_dyingto structdeferred_split, we can use something similar to whatlist_lrudoes in the similar situation i.e. set a special value (LONG_MIN) in itsnr_itemsvariable. That is makesplit_queue_lena long and set it toLONG_MINduring memcg offlining/reparenting.I've considered this option, but I am concerned about the risk of overflow.
If you modify it only within a lock then why would it over or underflow?
Quoted 2 lines
So I will try to use
css_is_dying()in the next version.
No objection from me but add a comment on the potential window where the deferred thps will be hidden from the shrinkers.