Make deferred split shrinker memcg aware
Subsystems (8)
v5 cover Make deferred split shrinker memcg aware
Currently THP deferred split shrinker is not memcg aware, this may cause premature OOM with some configuration. For example the below test would run into premature OOM easily:
$ cgcreate -g memory:thp $ echo 4G > /sys/fs/cgroup/memory/thp/memory/limit_in_bytes $ cgexec -g memory:thp transhuge-stress 4000
transhuge-stress comes from kernel selftest.
It is easy to hit OOM, but there are still a lot THP on the deferred split queue, memcg direct reclaim can't touch them since the deferred split shrinker is not memcg aware.
Convert deferred split shrinker memcg aware by introducing per memcg deferred split queue. The THP should be on either per node or per memcg deferred split queue if it belongs to a memcg. When the page is immigrated to the other memcg, it will be immigrated to the target memcg's deferred split queue too.
Reuse the second tail page's deferred_list for per memcg list since the same THP can't be on multiple deferred split queues.
Make deferred split shrinker not depend on memcg kmem since it is not slab. It doesn’t make sense to not shrink THP even though memcg kmem is disabled.
With the above change the test demonstrated above doesn’t trigger OOM even though with cgroup.memory=nokmem.
Changelog:
v5: * Fixed the issue reported by Qian Cai, folded the fix in.
* Squashed build fix patches in.
v4: * Replace list_del() to list_del_init() per Andrew.
* Fixed the build failure for different kconfig combo and tested the below combo:
MEMCG + TRANSPARENT_HUGEPAGE
!MEMCG + TRANSPARENT_HUGEPAGE
MEMCG + !TRANSPARENT_HUGEPAGE
!MEMCG + !TRANSPARENT_HUGEPAGE
* Added Acked-by from Kirill Shutemov.
v3: * Adopted the suggestion from Kirill Shutemov to move mem_cgroup_uncharge()
out of __page_cache_release() in order to handle THP free properly.
* Adjusted the sequence of the patches per Kirill Shutemov. Dropped the patch 3/4 in v2.
* Moved enqueuing THP onto "to" memcg deferred split queue after page->mem_cgroup is changed in memcg account move per Kirill Tkhai.
v2: * Adopted the suggestion from Krill Shutemov to extract deferred split
fields into a struct to reduce code duplication (patch 1/4). With this change, the lines of change is shrunk down to 198 from 278.
* Removed memcg_deferred_list. Use deferred_list for both global and memcg. With the code deduplication, it doesn't make too much sense to keep it. Kirill Tkhai also suggested so.
* Fixed typo for SHRINKER_NONSLAB.
Yang Shi (4):
mm: thp: extract split_queue_* into a struct
mm: move mem_cgroup_uncharge out of __page_cache_release()
mm: shrinker: make shrinker not depend on memcg kmem
mm: thp: make deferred split shrinker memcg aware
include/linux/huge_mm.h | 9 ++++++ include/linux/memcontrol.h | 23 +++++++++----- include/linux/mm_types.h | 1 + include/linux/mmzone.h | 12 ++++++-- include/linux/shrinker.h | 3 +- mm/huge_memory.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++---------------- mm/memcontrol.c | 33 +++++++++++++++----- mm/page_alloc.c | 9 ++++-- mm/swap.c | 2 +- mm/vmscan.c | 66 +++++++++++++++++++-------------------- 10 files changed, 186 insertions(+), 83 deletions(-)
PATCH v5 1/4 mm: thp: extract split_queue_* into a struct
Put split_queue, split_queue_lock and split_queue_len into a struct in order to reduce code duplication when we convert deferred_split to memcg aware in the later patches.
--- include/linux/mmzone.h | 12 +++++++++--- mm/huge_memory.c | 45 +++++++++++++++++++++++++-------------------- mm/page_alloc.c | 8 +++++--- 3 files changed, 39 insertions(+), 26 deletions(-)
include/linux/mmzone.h +9 -3
@@ -676,6 +676,14 @@ struct zonelist {extern struct page *mem_map;#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +struct deferred_split { +spinlock_t split_queue_lock; +struct list_head split_queue; +unsigned long split_queue_len; +}; +#endif +/* * On NUMA machines, each NUMA node would have a pg_data_t to describe * it's memory layout. On UMA machines there is a single pglist_data which
@@ -755,9 +763,7 @@ struct zonelist {#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ #ifdef CONFIG_TRANSPARENT_HUGEPAGE -spinlock_t split_queue_lock; -struct list_head split_queue; -unsigned long split_queue_len; +struct deferred_split deferred_split_queue;#endif /* Fields commonly accessed by the page reclaim scanner */
mm/huge_memory.c +25 -20
134 lines not on this page · raw message
mm/page_alloc.c +5 -3
@@ -6649,9 +6649,11 @@ static unsigned long __init calc_memmap_size(unsigned long spanned_pages,#ifdef CONFIG_TRANSPARENT_HUGEPAGE static void pgdat_init_split_queue (struct pglist_data *pgdat) { -spin_lock_init (&pgdat->split_queue_lock); -INIT_LIST_HEAD (&pgdat->split_queue); - pgdat->split_queue_len =0 ; +struct deferred_split *ds_queue = &pgdat->deferred_split_queue; + +spin_lock_init (&ds_queue->split_queue_lock); +INIT_LIST_HEAD (&ds_queue->split_queue); + ds_queue->split_queue_len =0 ; }#else static void pgdat_init_split_queue (struct pglist_data *pgdat) {}
Signature · 1 line
1.8.3.1
Re: [v5 PATCH 1/4] mm: thp: extract split_queue_* into a struct
Quoted 4 lines · Yang Shi
Put
split_queue,split_queue_lockandsplit_queue_leninto a struct in order to reduce code duplication when we convertdeferred_splitto memcg aware in the later patches.
Quoted 9 lines
Suggested-by "Kirill A . Shutemov" <[email protected]>Cc Kirill Tkhai <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shakeel Butt <[email protected]>Cc David Rientjes <[email protected]>Cc Qian Cai <[email protected]>
Quoted 201 lines
Signed-off-by Yang Shi <[email protected]>--- include/linux/mmzone.h | 12 +++++++++--- mm/huge_memory.c | 45 +++++++++++++++++++++++++-------------------- mm/page_alloc.c | 8 +++++--- 3 files changed, 39 insertions(+), 26 deletions(-)include/linux/mmzone.h +9
-3@@ -676,6 +676,14 @@ struct zonelist {extern struct page *mem_map;#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +struct deferred_split { +spinlock_t split_queue_lock; +struct list_head split_queue; +unsigned long split_queue_len; +}; +#endif +/* * On NUMA machines, each NUMA node would have a pg_data_t to describe * it's memory layout. On UMA machines there is a single pglist_data which @@ -755,9 +763,7 @@ struct zonelist {#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ #ifdef CONFIG_TRANSPARENT_HUGEPAGE -spinlock_t split_queue_lock; -struct list_head split_queue; -unsigned long split_queue_len; +struct deferred_split deferred_split_queue;#endif /* Fields commonly accessed by the page reclaim scanner */ mm/huge_memory.c +25
-20@@ -2658,6 +2658,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) {struct page *head =compound_head (page);struct pglist_data *pgdata =NODE_DATA (page_to_nid (head)); +struct deferred_split *ds_queue = &pgdata->deferred_split_queue;struct anon_vma *anon_vma =NULL ;struct address_space *mapping =NULL ;int count, mapcount, extra_pins, ret;@@ -2744,17 +2745,17 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) }/* Prevent deferred_split_scan() touching ->_refcount */ -spin_lock (&pgdata->split_queue_lock); +spin_lock (&ds_queue->split_queue_lock); count =page_count (head); mapcount =total_mapcount (head);if (!mapcount &&page_ref_freeze (head,1 + extra_pins)) {if (!list_empty (page_deferred_list (head))) { - pgdata->split_queue_len--; + ds_queue->split_queue_len--;list_del (page_deferred_list (head)); }if (mapping)__dec_node_page_state (page,NR_SHMEM_THPS ); -spin_unlock (&pgdata->split_queue_lock); +spin_unlock (&ds_queue->split_queue_lock);__split_huge_page (page, list, end, flags);if (PageSwapCache (head)) {swp_entry_t entry = { .val =page_private (head) };@@ -2771,7 +2772,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)dump_page (page,"total_mapcount(head) > 0" );BUG (); } -spin_unlock (&pgdata->split_queue_lock); +spin_unlock (&ds_queue->split_queue_lock); fail:if (mapping)xa_unlock (&mapping->i_pages);spin_unlock_irqrestore (&pgdata->lru_lock, flags);@@ -2794,52 +2795,56 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)void free_transhuge_page (struct page *page) {struct pglist_data *pgdata =NODE_DATA (page_to_nid (page)); +struct deferred_split *ds_queue = &pgdata->deferred_split_queue;unsigned long flags; -spin_lock_irqsave (&pgdata->split_queue_lock, flags); +spin_lock_irqsave (&ds_queue->split_queue_lock, flags);if (!list_empty (page_deferred_list (page))) { - pgdata->split_queue_len--; + ds_queue->split_queue_len--;list_del (page_deferred_list (page)); } -spin_unlock_irqrestore (&pgdata->split_queue_lock, flags); +spin_unlock_irqrestore (&ds_queue->split_queue_lock, flags);free_compound_page (page); }void deferred_split_huge_page (struct page *page) {struct pglist_data *pgdata =NODE_DATA (page_to_nid (page)); +struct deferred_split *ds_queue = &pgdata->deferred_split_queue;unsigned long flags;VM_BUG_ON_PAGE (!PageTransHuge (page), page); -spin_lock_irqsave (&pgdata->split_queue_lock, flags); +spin_lock_irqsave (&ds_queue->split_queue_lock, flags);if (list_empty (page_deferred_list (page))) {count_vm_event (THP_DEFERRED_SPLIT_PAGE ); -list_add_tail (page_deferred_list (page), &pgdata->split_queue); - pgdata->split_queue_len++; +list_add_tail (page_deferred_list (page), &ds_queue->split_queue); + ds_queue->split_queue_len++; } -spin_unlock_irqrestore (&pgdata->split_queue_lock, flags); +spin_unlock_irqrestore (&ds_queue->split_queue_lock, flags); }static unsigned long deferred_split_count (struct shrinker *shrink,struct shrink_control *sc) {struct pglist_data *pgdata =NODE_DATA (sc->nid); -return READ_ONCE (pgdata->split_queue_len); +struct deferred_split *ds_queue = &pgdata->deferred_split_queue; +return READ_ONCE (ds_queue->split_queue_len); }static unsigned long deferred_split_scan (struct shrinker *shrink,struct shrink_control *sc) {struct pglist_data *pgdata =NODE_DATA (sc->nid); +struct deferred_split *ds_queue = &pgdata->deferred_split_queue;unsigned long flags;LIST_HEAD (list), *pos, *next;struct page *page;int split =0 ; -spin_lock_irqsave (&pgdata->split_queue_lock, flags); +spin_lock_irqsave (&ds_queue->split_queue_lock, flags);/* Take pin on all head pages to avoid freeing them under us */ -list_for_each_safe (pos, next, &pgdata->split_queue) { +list_for_each_safe (pos, next, &ds_queue->split_queue) { page = list_entry((void *)pos, struct page, mapping); page =compound_head (page);if (get_page_unless_zero (page)) {@@ -2847,12 +2852,12 @@ static unsigned long deferred_split_scan(struct shrinker *shrink, }else {/* We lost race with put_compound_page() */ list_del_init (page_deferred_list (page)); - pgdata->split_queue_len--; + ds_queue->split_queue_len--; }if (!--sc->nr_to_scan)break ; } -spin_unlock_irqrestore (&pgdata->split_queue_lock, flags); +spin_unlock_irqrestore (&ds_queue->split_queue_lock, flags);list_for_each_safe (pos, next, &list) { page = list_entry((void *)pos, struct page, mapping);@@ -2866,15 +2871,15 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,put_page (page); } -spin_lock_irqsave (&pgdata->split_queue_lock, flags); -list_splice_tail (&list, &pgdata->split_queue); -spin_unlock_irqrestore (&pgdata->split_queue_lock, flags); +spin_lock_irqsave (&ds_queue->split_queue_lock, flags); +list_splice_tail (&list, &ds_queue->split_queue); +spin_unlock_irqrestore (&ds_queue->split_queue_lock, flags);/* * Stop shrinker if we didn't split any page, but the queue is empty. * This can happen if pages were freed under us. */ -if (!split &&list_empty (&pgdata->split_queue)) +if (!split &&list_empty (&ds_queue->split_queue))return SHRINK_STOP ;return split; }mm/page_alloc.c +5
-3@@ -6649,9 +6649,11 @@ static unsigned long __init calc_memmap_size(unsigned long spanned_pages,#ifdef CONFIG_TRANSPARENT_HUGEPAGE static void pgdat_init_split_queue (struct pglist_data *pgdat) { -spin_lock_init (&pgdat->split_queue_lock); -INIT_LIST_HEAD (&pgdat->split_queue); - pgdat->split_queue_len =0 ; +struct deferred_split *ds_queue = &pgdat->deferred_split_queue; + +spin_lock_init (&ds_queue->split_queue_lock); +INIT_LIST_HEAD (&ds_queue->split_queue); + ds_queue->split_queue_len =0 ; }#else static void pgdat_init_split_queue (struct pglist_data *pgdat) {}
PATCH v5 2/4 mm: move mem_cgroup_uncharge out of __page_cache_release()
In reply to Yang ShiThe later patch would make THP deferred split shrinker memcg aware, but it needs page->mem_cgroup information in THP destructor, which is called after mem_cgroup_uncharge() now.
So, move mem_cgroup_uncharge() from __page_cache_release() to compound page destructor, which is called by both THP and other compound pages except HugeTLB. And call it in __put_single_page() for single order page.
--- mm/page_alloc.c | 1 + mm/swap.c | 2 +- mm/vmscan.c | 6 ++---- 3 files changed, 4 insertions(+), 5 deletions(-)
mm/page_alloc.c +1 -0
@@ -670,6 +670,7 @@ static void bad_page(struct page *page, const char *reason,void free_compound_page (struct page *page) { +mem_cgroup_uncharge (page);__free_pages_ok (page,compound_order (page)); }
mm/swap.c +1 -1
@@ -71,12 +71,12 @@ static void __page_cache_release(struct page *page)spin_unlock_irqrestore (&pgdat->lru_lock, flags); }__ClearPageWaiters (page); -mem_cgroup_uncharge (page); }static void __put_single_page (struct page *page) {__page_cache_release (page); +mem_cgroup_uncharge (page);free_unref_page (page); }
mm/vmscan.c +2 -4
@@ -1490,10 +1490,9 @@ static unsigned long shrink_page_list(struct list_head *page_list,* Is there need to periodically free_page_list? It would * appear not as the counts should be low */ -if (unlikely (PageTransHuge (page))) { -mem_cgroup_uncharge (page); +if (unlikely (PageTransHuge (page))) (*get_compound_page_dtor (page))(page); - }else +else list_add (&page->lru, &free_pages);continue ;
@@ -1914,7 +1913,6 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,if (unlikely (PageCompound (page))) {spin_unlock_irq (&pgdat->lru_lock); -mem_cgroup_uncharge (page); (*get_compound_page_dtor (page))(page);spin_lock_irq (&pgdat->lru_lock); }else
Signature · 1 line
1.8.3.1
Re: [v5 PATCH 2/4] mm: move mem_cgroup_uncharge out of __page_cache_release()
Quoted 9 lines · Yang Shi
The later patch would make THP deferred split shrinker memcg aware, but it needs
page->mem_cgroupinformation in THP destructor, which is called aftermem_cgroup_uncharge()now.So, move
mem_cgroup_uncharge()from__page_cache_release()to compound page destructor, which is called by both THP and other compound pages except HugeTLB. And call it in__put_single_page()for single order page.
Quoted 10 lines
Suggested-by "Kirill A . Shutemov" <[email protected]>Cc Kirill Tkhai <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shakeel Butt <[email protected]>Cc David Rientjes <[email protected]>Cc Qian Cai <[email protected]>Signed-off-by Yang Shi <[email protected]>
Quoted 62 lines
--- mm/page_alloc.c | 1 + mm/swap.c | 2 +- mm/vmscan.c | 6 ++---- 3 files changed, 4 insertions(+), 5 deletions(-)mm/page_alloc.c +1
-0@@ -670,6 +670,7 @@ static void bad_page(struct page *page, const char *reason,void free_compound_page (struct page *page) { +mem_cgroup_uncharge (page);__free_pages_ok (page,compound_order (page)); }mm/swap.c +1
-1@@ -71,12 +71,12 @@ static void __page_cache_release(struct page *page)spin_unlock_irqrestore (&pgdat->lru_lock, flags); }__ClearPageWaiters (page); -mem_cgroup_uncharge (page); }static void __put_single_page (struct page *page) {__page_cache_release (page); +mem_cgroup_uncharge (page);free_unref_page (page); }mm/vmscan.c +2
-4@@ -1490,10 +1490,9 @@ static unsigned long shrink_page_list(struct list_head *page_list,* Is there need to periodically free_page_list? It would * appear not as the counts should be low */ -if (unlikely (PageTransHuge (page))) { -mem_cgroup_uncharge (page); +if (unlikely (PageTransHuge (page))) (*get_compound_page_dtor (page))(page); - }else +else list_add (&page->lru, &free_pages);continue ;@@ -1914,7 +1913,6 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,if (unlikely (PageCompound (page))) {spin_unlock_irq (&pgdat->lru_lock); -mem_cgroup_uncharge (page); (*get_compound_page_dtor (page))(page);spin_lock_irq (&pgdat->lru_lock); }else
PATCH v5 3/4 mm: shrinker: make shrinker not depend on memcg kmem
In reply to Yang ShiCurrently shrinker is just allocated and can work when memcg kmem is enabled. But, THP deferred split shrinker is not slab shrinker, it doesn't make too much sense to have such shrinker depend on memcg kmem. It should be able to reclaim THP even though memcg kmem is disabled.
Introduce a new shrinker flag, SHRINKER_NONSLAB, for non-slab shrinker. When memcg kmem is disabled, just such shrinkers can be called in shrinking memcg slab.
--- include/linux/memcontrol.h | 19 ++++++++------- include/linux/shrinker.h | 3 ++- mm/memcontrol.c | 9 +------ mm/vmscan.c | 60 ++++++++++++++++++++++++---------------------- 4 files changed, 45 insertions(+), 46 deletions(-)
include/linux/memcontrol.h +11 -8
@@ -128,9 +128,8 @@ struct mem_cgroup_per_node {struct mem_cgroup_reclaim_iter iter[DEF_PRIORITY +1 ]; -#ifdef CONFIG_MEMCG_KMEM struct memcg_shrinker_map __rcu *shrinker_map; -#endif +struct rb_node tree_node;/* RB tree node */ unsigned long usage_in_excess;/* Set to the value by which */ /* the soft limit is exceeded*/
@@ -1253,6 +1252,11 @@ static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) } while ((memcg = parent_mem_cgroup(memcg)));return false; } + +extern int memcg_expand_shrinker_maps (int new_id); + +extern void memcg_set_shrinker_bit (struct mem_cgroup *memcg, +int nid,int shrinker_id);#else #define mem_cgroup_sockets_enabled 0static inline void mem_cgroup_sk_alloc (struct sock *sk) { };
@@ -1261,6 +1265,11 @@ static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) {return false; } + +static inline void memcg_set_shrinker_bit (struct mem_cgroup *memcg, +int nid,int shrinker_id) +{ +}#endif struct kmem_cache *memcg_kmem_get_cache (struct kmem_cache *cachep);
@@ -1332,10 +1341,6 @@ static inline int memcg_cache_id(struct mem_cgroup *memcg) return memcg ? memcg->kmemcg_id : -1; } -extern int memcg_expand_shrinker_maps (int new_id); - -extern void memcg_set_shrinker_bit (struct mem_cgroup *memcg, -int nid,int shrinker_id);#else static inline int memcg_kmem_charge (struct page *page,gfp_t gfp,int order)
@@ -1377,8 +1382,6 @@ static inline void memcg_put_cache_ids(void) { } -static inline void memcg_set_shrinker_bit (struct mem_cgroup *memcg, -int nid,int shrinker_id) { }#endif /* CONFIG_MEMCG_KMEM */ #endif /* _LINUX_MEMCONTROL_H */
include/linux/shrinker.h +2 -1
@@ -69,7 +69,7 @@ struct shrinker {/* These are for internal use */ struct list_head list; -#ifdef CONFIG_MEMCG_KMEM +#ifdef CONFIG_MEMCG /* ID in shrinker_idr */ int id;#endif
@@ -81,6 +81,7 @@ struct shrinker {/* Flags */ #define SHRINKER_NUMA_AWARE (1 << 0)#define SHRINKER_MEMCG_AWARE (1 << 1) +#define SHRINKER_NONSLAB (1 << 2)extern int prealloc_shrinker (struct shrinker *shrinker);extern void register_shrinker_prepared (struct shrinker *shrinker);
mm/memcontrol.c +1 -8
@@ -313,6 +313,7 @@ void memcg_put_cache_ids(void)EXPORT_SYMBOL (memcg_kmem_enabled_key);struct workqueue_struct *memcg_kmem_cache_wq; +#endif static int memcg_shrinker_map_size;static DEFINE_MUTEX (memcg_shrinker_map_mutex);
@@ -436,14 +437,6 @@ void memcg_set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id) } } -#else /* CONFIG_MEMCG_KMEM */ -static int memcg_alloc_shrinker_maps (struct mem_cgroup *memcg) -{ -return 0 ; -} -static void memcg_free_shrinker_maps (struct mem_cgroup *memcg) { } -#endif /* CONFIG_MEMCG_KMEM */ -/** * mem_cgroup_css_from_page - css of the memcg associated with a page * @page: page of interest
mm/vmscan.c +31 -29
111 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [v5 PATCH 3/4] mm: shrinker: make shrinker not depend on memcg kmem
Quoted 9 lines · Yang Shi
Currently shrinker is just allocated and can work when memcg kmem is enabled. But, THP deferred split shrinker is not slab shrinker, it doesn't make too much sense to have such shrinker depend on memcg kmem. It should be able to reclaim THP even though memcg kmem is disabled.
Introduce a new shrinker flag,
SHRINKER_NONSLAB, for non-slab shrinker. When memcg kmem is disabled, just such shrinkers can be called in shrinking memcg slab.
Quoted 10 lines
Cc Kirill Tkhai <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc "Kirill A . Shutemov" <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shakeel Butt <[email protected]>Cc David Rientjes <[email protected]>Cc Qian Cai <[email protected]>Signed-off-by Yang Shi <[email protected]>
Looks OK for me. But some doubts about naming.
SHRINKER_NONSLAB. There are a lot of shrinkers, which are not related to slab. For example, mmu_shrinker in arch/x86/kvm/mmu.c. Intuitively and without mm knowledge, I assume, I would be surprised why it's not masked as NONSLAB. Can we improve this in some way?
The rest looks OK for me.
Quoted 237 lines
--- include/linux/memcontrol.h | 19 ++++++++------- include/linux/shrinker.h | 3 ++- mm/memcontrol.c | 9 +------ mm/vmscan.c | 60 ++++++++++++++++++++++++---------------------- 4 files changed, 45 insertions(+), 46 deletions(-)include/linux/memcontrol.h +11
-8@@ -128,9 +128,8 @@ struct mem_cgroup_per_node {struct mem_cgroup_reclaim_iter iter[DEF_PRIORITY +1 ]; -#ifdef CONFIG_MEMCG_KMEM struct memcg_shrinker_map __rcu *shrinker_map; -#endif +struct rb_node tree_node;/* RB tree node */ unsigned long usage_in_excess;/* Set to the value by which */ /* the soft limit is exceeded*/ @@ -1253,6 +1252,11 @@ static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) } while ((memcg = parent_mem_cgroup(memcg)));return false; } + +extern int memcg_expand_shrinker_maps (int new_id); + +extern void memcg_set_shrinker_bit (struct mem_cgroup *memcg, +int nid,int shrinker_id);#else #define mem_cgroup_sockets_enabled 0static inline void mem_cgroup_sk_alloc (struct sock *sk) { };@@ -1261,6 +1265,11 @@ static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) {return false; } + +static inline void memcg_set_shrinker_bit (struct mem_cgroup *memcg, +int nid,int shrinker_id) +{ +}#endif struct kmem_cache *memcg_kmem_get_cache (struct kmem_cache *cachep);@@ -1332,10 +1341,6 @@ static inline int memcg_cache_id(struct mem_cgroup *memcg) return memcg ? memcg->kmemcg_id : -1; } -extern int memcg_expand_shrinker_maps (int new_id); - -extern void memcg_set_shrinker_bit (struct mem_cgroup *memcg, -int nid,int shrinker_id);#else static inline int memcg_kmem_charge (struct page *page,gfp_t gfp,int order)@@ -1377,8 +1382,6 @@ static inline void memcg_put_cache_ids(void) { } -static inline void memcg_set_shrinker_bit (struct mem_cgroup *memcg, -int nid,int shrinker_id) { }#endif /* CONFIG_MEMCG_KMEM */ #endif /* _LINUX_MEMCONTROL_H */ include/linux/shrinker.h +2
-1@@ -69,7 +69,7 @@ struct shrinker {/* These are for internal use */ struct list_head list; -#ifdef CONFIG_MEMCG_KMEM +#ifdef CONFIG_MEMCG /* ID in shrinker_idr */ int id;#endif @@ -81,6 +81,7 @@ struct shrinker {/* Flags */ #define SHRINKER_NUMA_AWARE (1 << 0)#define SHRINKER_MEMCG_AWARE (1 << 1) +#define SHRINKER_NONSLAB (1 << 2)extern int prealloc_shrinker (struct shrinker *shrinker);extern void register_shrinker_prepared (struct shrinker *shrinker);mm/memcontrol.c +1
-8@@ -313,6 +313,7 @@ void memcg_put_cache_ids(void)EXPORT_SYMBOL (memcg_kmem_enabled_key);struct workqueue_struct *memcg_kmem_cache_wq; +#endif static int memcg_shrinker_map_size;static DEFINE_MUTEX (memcg_shrinker_map_mutex);@@ -436,14 +437,6 @@ void memcg_set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id) } } -#else /* CONFIG_MEMCG_KMEM */ -static int memcg_alloc_shrinker_maps (struct mem_cgroup *memcg) -{ -return 0 ; -} -static void memcg_free_shrinker_maps (struct mem_cgroup *memcg) { } -#endif /* CONFIG_MEMCG_KMEM */ -/** * mem_cgroup_css_from_page - css of the memcg associated with a page * @page: page of interest mm/vmscan.c +31
-29@@ -174,11 +174,22 @@ struct scan_control {*/ unsigned long vm_total_pages; +static void set_task_reclaim_state (struct task_struct *task, +struct reclaim_state *rs) +{ +/* Check for an overwrite */ +WARN_ON_ONCE (rs && task->reclaim_state); + +/* Check for the nulling of an already-nulled member */ +WARN_ON_ONCE (!rs && !task->reclaim_state); + + task->reclaim_state = rs; +} +static LIST_HEAD (shrinker_list);static DECLARE_RWSEM (shrinker_rwsem); -#ifdef CONFIG_MEMCG_KMEM - +#ifdef CONFIG_MEMCG /* * We allow subsystems to populate their shrinker-related * LRU lists before register_shrinker_prepared() is called @@ -230,30 +241,7 @@ static void unregister_memcg_shrinker(struct shrinker *shrinker)idr_remove (&shrinker_idr, id);up_write (&shrinker_rwsem); } -#else /* CONFIG_MEMCG_KMEM */ -static int prealloc_memcg_shrinker (struct shrinker *shrinker) -{ -return 0 ; -} -static void unregister_memcg_shrinker (struct shrinker *shrinker) -{ -} -#endif /* CONFIG_MEMCG_KMEM */ - -static void set_task_reclaim_state (struct task_struct *task, -struct reclaim_state *rs) -{ -/* Check for an overwrite */ -WARN_ON_ONCE (rs && task->reclaim_state); - -/* Check for the nulling of an already-nulled member */ -WARN_ON_ONCE (!rs && !task->reclaim_state); - - task->reclaim_state = rs; -} - -#ifdef CONFIG_MEMCG static bool global_reclaim (struct scan_control *sc) {return !sc->target_mem_cgroup;@@ -308,6 +296,15 @@ static bool memcg_congested(pg_data_t *pgdat, }#else +static int prealloc_memcg_shrinker (struct shrinker *shrinker) +{ +return 0 ; +} + +static void unregister_memcg_shrinker (struct shrinker *shrinker) +{ +} +static bool global_reclaim (struct scan_control *sc) {return true;@@ -594,7 +591,7 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,return freed; } -#ifdef CONFIG_MEMCG_KMEM +#ifdef CONFIG_MEMCG static unsigned long shrink_slab_memcg (gfp_t gfp_mask,int nid,struct mem_cgroup *memcg,int priority) {@@ -602,7 +599,7 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,unsigned long ret, freed =0 ;int i; -if (!memcg_kmem_enabled () || !mem_cgroup_online (memcg)) +if (!mem_cgroup_online (memcg))return 0 ;if (!down_read_trylock (&shrinker_rwsem))@@ -628,6 +625,11 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,continue ; } +/* Call non-slab shrinkers even though kmem is disabled */ +if (!memcg_kmem_enabled () && + !(shrinker->flags &SHRINKER_NONSLAB )) +continue ; + ret =do_shrink_slab (&sc, shrinker, priority);if (ret ==SHRINK_EMPTY ) {clear_bit (i, map->map);@@ -664,13 +666,13 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,up_read (&shrinker_rwsem); return freed; } -#else /* CONFIG_MEMCG_KMEM */ +#else /* CONFIG_MEMCG */ static unsigned long shrink_slab_memcg (gfp_t gfp_mask,int nid,struct mem_cgroup *memcg,int priority) {return 0 ; } -#endif /* CONFIG_MEMCG_KMEM */ +#endif /* CONFIG_MEMCG */ /** * shrink_slab - shrink slab caches
Re: [v5 PATCH 3/4] mm: shrinker: make shrinker not depend on memcg kmem
Quoted 20 lines · Kirill Tkhai
Quoted 9 lines · Yang Shi
Currently shrinker is just allocated and can work when memcg kmem is enabled. But, THP deferred split shrinker is not slab shrinker, it doesn't make too much sense to have such shrinker depend on memcg kmem. It should be able to reclaim THP even though memcg kmem is disabled.
Introduce a new shrinker flag,
SHRINKER_NONSLAB, for non-slab shrinker. When memcg kmem is disabled, just such shrinkers can be called in shrinking memcg slab.Quoted 10 lines
Cc Kirill Tkhai <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc "Kirill A . Shutemov" <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shakeel Butt <[email protected]>Cc David Rientjes <[email protected]>Cc Qian Cai <[email protected]>Signed-off-by Yang Shi <[email protected]>
Quoted 6 lines
Looks OK for me. But some doubts about naming.
SHRINKER_NONSLAB. There are a lot of shrinkers, which are not related to slab. For example,mmu_shrinkerinarch/x86/kvm/mmu.c. Intuitively and without mm knowledge, I assume, I would be surprised why it's not masked as NONSLAB. Can we improve this in some way?
Actually, SHRINKER_NONSLAB just makes sense when the shrinker is also MEMCG_AWARE for now.
I didn't think of a better name, any suggestion? I could add some comment to explain this, non-MEMCG_AWARE shrinker should not care about setting this flag even though it is non-slab.
And, once this patch is in Linus's tree, I will double check if there is any MEMCG_AWARE non-slab shrinker although my quick search didn't show others except inode/dcache and workingset node.
Quoted 242 lines
The rest looks OK for me.
Reviewed-by Kirill Tkhai <[email protected]>Quoted 237 lines
--- include/linux/memcontrol.h | 19 ++++++++------- include/linux/shrinker.h | 3 ++- mm/memcontrol.c | 9 +------ mm/vmscan.c | 60 ++++++++++++++++++++++++---------------------- 4 files changed, 45 insertions(+), 46 deletions(-)include/linux/memcontrol.h +11
-8@@ -128,9 +128,8 @@ struct mem_cgroup_per_node {struct mem_cgroup_reclaim_iter iter[DEF_PRIORITY +1 ]; -#ifdef CONFIG_MEMCG_KMEM struct memcg_shrinker_map __rcu *shrinker_map; -#endif +struct rb_node tree_node;/* RB tree node */ unsigned long usage_in_excess;/* Set to the value by which */ /* the soft limit is exceeded*/ @@ -1253,6 +1252,11 @@ static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) } while ((memcg = parent_mem_cgroup(memcg)));return false; } + +extern int memcg_expand_shrinker_maps (int new_id); + +extern void memcg_set_shrinker_bit (struct mem_cgroup *memcg, +int nid,int shrinker_id);#else #define mem_cgroup_sockets_enabled 0static inline void mem_cgroup_sk_alloc (struct sock *sk) { };@@ -1261,6 +1265,11 @@ static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) {return false; } + +static inline void memcg_set_shrinker_bit (struct mem_cgroup *memcg, +int nid,int shrinker_id) +{ +}#endif struct kmem_cache *memcg_kmem_get_cache (struct kmem_cache *cachep);@@ -1332,10 +1341,6 @@ static inline int memcg_cache_id(struct mem_cgroup *memcg) return memcg ? memcg->kmemcg_id : -1; } -extern int memcg_expand_shrinker_maps (int new_id); - -extern void memcg_set_shrinker_bit (struct mem_cgroup *memcg, -int nid,int shrinker_id);#else static inline int memcg_kmem_charge (struct page *page,gfp_t gfp,int order)@@ -1377,8 +1382,6 @@ static inline void memcg_put_cache_ids(void) { } -static inline void memcg_set_shrinker_bit (struct mem_cgroup *memcg, -int nid,int shrinker_id) { }#endif /* CONFIG_MEMCG_KMEM */ #endif /* _LINUX_MEMCONTROL_H */ include/linux/shrinker.h +2
-1@@ -69,7 +69,7 @@ struct shrinker {/* These are for internal use */ struct list_head list; -#ifdef CONFIG_MEMCG_KMEM +#ifdef CONFIG_MEMCG /* ID in shrinker_idr */ int id;#endif @@ -81,6 +81,7 @@ struct shrinker {/* Flags */ #define SHRINKER_NUMA_AWARE (1 << 0)#define SHRINKER_MEMCG_AWARE (1 << 1) +#define SHRINKER_NONSLAB (1 << 2)extern int prealloc_shrinker (struct shrinker *shrinker);extern void register_shrinker_prepared (struct shrinker *shrinker);mm/memcontrol.c +1
-8@@ -313,6 +313,7 @@ void memcg_put_cache_ids(void)EXPORT_SYMBOL (memcg_kmem_enabled_key);struct workqueue_struct *memcg_kmem_cache_wq; +#endif static int memcg_shrinker_map_size;static DEFINE_MUTEX (memcg_shrinker_map_mutex);@@ -436,14 +437,6 @@ void memcg_set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id) } } -#else /* CONFIG_MEMCG_KMEM */ -static int memcg_alloc_shrinker_maps (struct mem_cgroup *memcg) -{ -return 0 ; -} -static void memcg_free_shrinker_maps (struct mem_cgroup *memcg) { } -#endif /* CONFIG_MEMCG_KMEM */ -/** * mem_cgroup_css_from_page - css of the memcg associated with a page * @page: page of interest mm/vmscan.c +31
-29@@ -174,11 +174,22 @@ struct scan_control {*/ unsigned long vm_total_pages; +static void set_task_reclaim_state (struct task_struct *task, +struct reclaim_state *rs) +{ +/* Check for an overwrite */ +WARN_ON_ONCE (rs && task->reclaim_state); + +/* Check for the nulling of an already-nulled member */ +WARN_ON_ONCE (!rs && !task->reclaim_state); + + task->reclaim_state = rs; +} +static LIST_HEAD (shrinker_list);static DECLARE_RWSEM (shrinker_rwsem); -#ifdef CONFIG_MEMCG_KMEM - +#ifdef CONFIG_MEMCG /* * We allow subsystems to populate their shrinker-related * LRU lists before register_shrinker_prepared() is called @@ -230,30 +241,7 @@ static void unregister_memcg_shrinker(struct shrinker *shrinker)idr_remove (&shrinker_idr, id);up_write (&shrinker_rwsem); } -#else /* CONFIG_MEMCG_KMEM */ -static int prealloc_memcg_shrinker (struct shrinker *shrinker) -{ -return 0 ; -} -static void unregister_memcg_shrinker (struct shrinker *shrinker) -{ -} -#endif /* CONFIG_MEMCG_KMEM */ - -static void set_task_reclaim_state (struct task_struct *task, -struct reclaim_state *rs) -{ -/* Check for an overwrite */ -WARN_ON_ONCE (rs && task->reclaim_state); - -/* Check for the nulling of an already-nulled member */ -WARN_ON_ONCE (!rs && !task->reclaim_state); - - task->reclaim_state = rs; -} - -#ifdef CONFIG_MEMCG static bool global_reclaim (struct scan_control *sc) {return !sc->target_mem_cgroup;@@ -308,6 +296,15 @@ static bool memcg_congested(pg_data_t *pgdat, }#else +static int prealloc_memcg_shrinker (struct shrinker *shrinker) +{ +return 0 ; +} + +static void unregister_memcg_shrinker (struct shrinker *shrinker) +{ +} +static bool global_reclaim (struct scan_control *sc) {return true;@@ -594,7 +591,7 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,return freed; } -#ifdef CONFIG_MEMCG_KMEM +#ifdef CONFIG_MEMCG static unsigned long shrink_slab_memcg (gfp_t gfp_mask,int nid,struct mem_cgroup *memcg,int priority) {@@ -602,7 +599,7 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,unsigned long ret, freed =0 ;int i; -if (!memcg_kmem_enabled () || !mem_cgroup_online (memcg)) +if (!mem_cgroup_online (memcg))return 0 ;if (!down_read_trylock (&shrinker_rwsem))@@ -628,6 +625,11 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,continue ; } +/* Call non-slab shrinkers even though kmem is disabled */ +if (!memcg_kmem_enabled () && + !(shrinker->flags &SHRINKER_NONSLAB )) +continue ; + ret =do_shrink_slab (&sc, shrinker, priority);if (ret ==SHRINK_EMPTY ) {clear_bit (i, map->map);@@ -664,13 +666,13 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,up_read (&shrinker_rwsem); return freed; } -#else /* CONFIG_MEMCG_KMEM */ +#else /* CONFIG_MEMCG */ static unsigned long shrink_slab_memcg (gfp_t gfp_mask,int nid,struct mem_cgroup *memcg,int priority) {return 0 ; } -#endif /* CONFIG_MEMCG_KMEM */ +#endif /* CONFIG_MEMCG */ /** * shrink_slab - shrink slab caches
PATCH v5 4/4 mm: thp: make deferred split shrinker memcg aware
In reply to Yang ShiCurrently THP deferred split shrinker is not memcg aware, this may cause premature OOM with some configuration. For example the below test would run into premature OOM easily:
$ cgcreate -g memory:thp $ echo 4G > /sys/fs/cgroup/memory/thp/memory/limit_in_bytes $ cgexec -g memory:thp transhuge-stress 4000
transhuge-stress comes from kernel selftest.
It is easy to hit OOM, but there are still a lot THP on the deferred split queue, memcg direct reclaim can't touch them since the deferred split shrinker is not memcg aware.
Convert deferred split shrinker memcg aware by introducing per memcg deferred split queue. The THP should be on either per node or per memcg deferred split queue if it belongs to a memcg. When the page is immigrated to the other memcg, it will be immigrated to the target memcg's deferred split queue too.
Reuse the second tail page's deferred_list for per memcg list since the same THP can't be on multiple deferred split queues.
--- include/linux/huge_mm.h | 9 ++++++ include/linux/memcontrol.h | 4 +++ include/linux/mm_types.h | 1 + mm/huge_memory.c | 76 +++++++++++++++++++++++++++++++++++++++------- mm/memcontrol.c | 24 +++++++++++++++ 5 files changed, 103 insertions(+), 11 deletions(-)
include/linux/huge_mm.h +9 -0
@@ -267,6 +267,15 @@ static inline bool thp_migration_supported(void) return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION); } +static inline struct list_head *page_deferred_list (struct page *page) +{ +/* +* Global or memcg deferred list in the second tail pages is +* occupied by compound_head. +*/ +return &page[2 ].deferred_list; +} +#else /* CONFIG_TRANSPARENT_HUGEPAGE */ #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })#define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; })
include/linux/memcontrol.h +4 -0
@@ -312,6 +312,10 @@ struct mem_cgroup {struct list_head event_list;spinlock_t event_list_lock; +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +struct deferred_split deferred_split_queue; +#endif +struct mem_cgroup_per_node *nodeinfo[0 ];/* WARNING: nodeinfo must be the last member here */ };
include/linux/mm_types.h +1 -0
@@ -139,6 +139,7 @@ struct page {struct {/* Second tail page of compound page */ unsigned long _compound_pad_1;/* compound_head */ unsigned long _compound_pad_2; +/* For both global and memcg */ struct list_head deferred_list; };struct {/* Page table pages */
mm/huge_memory.c +65 -11
135 lines not on this page · raw message
mm/memcontrol.c +24 -0
@@ -4698,6 +4698,11 @@ static struct mem_cgroup *mem_cgroup_alloc(void)#ifdef CONFIG_CGROUP_WRITEBACK INIT_LIST_HEAD (&memcg->cgwb_list);#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +spin_lock_init (&memcg->deferred_split_queue.split_queue_lock); +INIT_LIST_HEAD (&memcg->deferred_split_queue.split_queue); + memcg->deferred_split_queue.split_queue_len =0 ; +#endif idr_replace (&mem_cgroup_idr, memcg, memcg->id.id);return memcg; fail:
@@ -5071,6 +5076,14 @@ static int mem_cgroup_move_account(struct page *page,__mod_memcg_state (to,NR_WRITEBACK , nr_pages); } +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (compound && !list_empty (page_deferred_list (page))) { +spin_lock (&from->deferred_split_queue.split_queue_lock); +list_del_init (page_deferred_list (page)); + from->deferred_split_queue.split_queue_len--; +spin_unlock (&from->deferred_split_queue.split_queue_lock); + } +#endif /* * It is safe to change page->mem_cgroup here because the page * is referenced, charged, and isolated - we can't race with
@@ -5079,6 +5092,17 @@ static int mem_cgroup_move_account(struct page *page,/* caller should have done css_get */ page->mem_cgroup = to; + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (compound &&list_empty (page_deferred_list (page))) { +spin_lock (&to->deferred_split_queue.split_queue_lock); +list_add_tail (page_deferred_list (page), + &to->deferred_split_queue.split_queue); + to->deferred_split_queue.split_queue_len++; +spin_unlock (&to->deferred_split_queue.split_queue_lock); + } +#endif +spin_unlock_irqrestore (&from->move_lock, flags); ret =0 ;
Signature · 1 line
1.8.3.1
Re: [v5 PATCH 4/4] mm: thp: make deferred split shrinker memcg aware
Quoted 23 lines · Yang Shi
Currently THP deferred split shrinker is not memcg aware, this may cause premature OOM with some configuration. For example the below test would run into premature OOM easily:
$ cgcreate -g memory:thp $ echo 4G > /sys/fs/cgroup/memory/thp/memory/limit_in_bytes $ cgexec -g memory:thp transhuge-stress 4000transhuge-stress comes from kernel selftest.
It is easy to hit OOM, but there are still a lot THP on the deferred split queue, memcg direct reclaim can't touch them since the deferred split shrinker is not memcg aware.
Convert deferred split shrinker memcg aware by introducing per memcg deferred split queue. The THP should be on either per node or per memcg deferred split queue if it belongs to a memcg. When the page is immigrated to the other memcg, it will be immigrated to the target memcg's deferred split queue too.
Reuse the second tail page's
deferred_listfor per memcg list since the same THP can't be on multiple deferred split queues.
Quoted 10 lines
Cc Kirill Tkhai <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc "Kirill A . Shutemov" <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shakeel Butt <[email protected]>Cc David Rientjes <[email protected]>Cc Qian Cai <[email protected]>Signed-off-by Yang Shi <[email protected]>
But, please, see below one small suggestion.
Quoted 157 lines
--- include/linux/huge_mm.h | 9 ++++++ include/linux/memcontrol.h | 4 +++ include/linux/mm_types.h | 1 + mm/huge_memory.c | 76 +++++++++++++++++++++++++++++++++++++++------- mm/memcontrol.c | 24 +++++++++++++++ 5 files changed, 103 insertions(+), 11 deletions(-)include/linux/huge_mm.h +9
-0@@ -267,6 +267,15 @@ static inline bool thp_migration_supported(void) return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION); } +static inline struct list_head *page_deferred_list (struct page *page) +{ +/* +* Global or memcg deferred list in the second tail pages is +* occupied by compound_head. +*/ +return &page[2 ].deferred_list; +} +#else /* CONFIG_TRANSPARENT_HUGEPAGE */ #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })#define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; })include/linux/memcontrol.h +4
-0@@ -312,6 +312,10 @@ struct mem_cgroup {struct list_head event_list;spinlock_t event_list_lock; +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +struct deferred_split deferred_split_queue; +#endif +struct mem_cgroup_per_node *nodeinfo[0 ];/* WARNING: nodeinfo must be the last member here */ };include/linux/mm_types.h +1
-0@@ -139,6 +139,7 @@ struct page {struct {/* Second tail page of compound page */ unsigned long _compound_pad_1;/* compound_head */ unsigned long _compound_pad_2; +/* For both global and memcg */ struct list_head deferred_list; };struct {/* Page table pages */ mm/huge_memory.c +45
-9@@ -495,11 +495,25 @@ pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)return pmd; } -static inline struct list_head *page_deferred_list (struct page *page) +#ifdef CONFIG_MEMCG +static inline struct deferred_split *get_deferred_split_queue (struct page *page) { -/* ->lru in the tail pages is occupied by compound_head. */ -return &page[2 ].deferred_list; +struct mem_cgroup *memcg =compound_head (page)->mem_cgroup; +struct pglist_data *pgdat =NODE_DATA (page_to_nid (page)); + +if (memcg) +return &memcg->deferred_split_queue; +else +return &pgdat->deferred_split_queue; +} +#else +static inline struct deferred_split *get_deferred_split_queue (struct page *page) +{ +struct pglist_data *pgdat =NODE_DATA (page_to_nid (page)); + +return &pgdat->deferred_split_queue; } +#endif void prep_transhuge_page (struct page *page) {@@ -2658,7 +2672,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) {struct page *head =compound_head (page);struct pglist_data *pgdata =NODE_DATA (page_to_nid (head)); -struct deferred_split *ds_queue = &pgdata->deferred_split_queue; +struct deferred_split *ds_queue =get_deferred_split_queue (page);struct anon_vma *anon_vma =NULL ;struct address_space *mapping =NULL ;int count, mapcount, extra_pins, ret;@@ -2794,8 +2808,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)void free_transhuge_page (struct page *page) { -struct pglist_data *pgdata =NODE_DATA (page_to_nid (page)); -struct deferred_split *ds_queue = &pgdata->deferred_split_queue; +struct deferred_split *ds_queue =get_deferred_split_queue (page);unsigned long flags;spin_lock_irqsave (&ds_queue->split_queue_lock, flags);@@ -2809,17 +2822,37 @@ void free_transhuge_page(struct page *page)void deferred_split_huge_page (struct page *page) { -struct pglist_data *pgdata =NODE_DATA (page_to_nid (page)); -struct deferred_split *ds_queue = &pgdata->deferred_split_queue; +struct deferred_split *ds_queue =get_deferred_split_queue (page); +#ifdef CONFIG_MEMCG +struct mem_cgroup *memcg =compound_head (page)->mem_cgroup; +#endif unsigned long flags;VM_BUG_ON_PAGE (!PageTransHuge (page), page); +/* +* The try_to_unmap() in page reclaim path might reach here too, +* this may cause a race condition to corrupt deferred split queue. +* And, if page reclaim is already handling the same page, it is +* unnecessary to handle it again in shrinker. +* +* Check PageSwapCache to determine if the page is being +* handled by page reclaim since THP swap would add the page into +* swap cache before calling try_to_unmap(). +*/ +if (PageSwapCache (page)) +return ; +spin_lock_irqsave (&ds_queue->split_queue_lock, flags);if (list_empty (page_deferred_list (page))) {count_vm_event (THP_DEFERRED_SPLIT_PAGE );list_add_tail (page_deferred_list (page), &ds_queue->split_queue); ds_queue->split_queue_len++; +#ifdef CONFIG_MEMCG +if (memcg) +memcg_set_shrinker_bit (memcg,page_to_nid (page), + deferred_split_shrinker.id); +#endif }spin_unlock_irqrestore (&ds_queue->split_queue_lock, flags); }@@ -2827,8 +2860,19 @@ void deferred_split_huge_page(struct page *page)static unsigned long deferred_split_count (struct shrinker *shrink,struct shrink_control *sc) { +struct deferred_split *ds_queue;struct pglist_data *pgdata =NODE_DATA (sc->nid); -struct deferred_split *ds_queue = &pgdata->deferred_split_queue; + +#ifdef CONFIG_MEMCG +if (!sc->memcg) {
mm/huge_memory.c void deferred_split_huge_page(struct page *page) · line 2877
Quoted 10 lines
mm/huge_memory.c +8
-0+ ds_queue = &pgdata->deferred_split_queue; + return READ_ONCE(ds_queue->split_queue_len); + } + + ds_queue = &sc->memcg->deferred_split_queue; +#else + ds_queue = &pgdata->deferred_split_queue; +#endif return READ_ONCE (ds_queue->split_queue_len); }
Can we write this a little more compact?
struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
#ifdef CONFIG_MEMCG if (sc->memcg) ds_queue = &sc->memcg->deferred_split_queue;
#endif
Or just introduce a helper (something like get_sc_deferred_split_queue). The same is in .scan method.
Quoted 84 lines
mm/huge_memory.c +12
-2@@ -2836,12 +2880,21 @@ static unsigned long deferred_split_scan(struct shrinker *shrink, struct shrink_control *sc) {struct pglist_data *pgdata =NODE_DATA (sc->nid); -struct deferred_split *ds_queue = &pgdata->deferred_split_queue; +struct deferred_split *ds_queue;unsigned long flags;LIST_HEAD (list), *pos, *next;struct page *page;int split =0 ; +#ifdef CONFIG_MEMCG +if (sc->memcg) + ds_queue = &sc->memcg->deferred_split_queue; +else + ds_queue = &pgdata->deferred_split_queue; +#else + ds_queue = &pgdata->deferred_split_queue; +#endif +spin_lock_irqsave (&ds_queue->split_queue_lock, flags);/* Take pin on all head pages to avoid freeing them under us */ list_for_each_safe (pos, next, &ds_queue->split_queue) {@@ -2888,7 +2941,8 @@ static unsigned long deferred_split_scan(struct shrinker *shrink, .count_objects = deferred_split_count, .scan_objects = deferred_split_scan, .seeks = DEFAULT_SEEKS, - .flags = SHRINKER_NUMA_AWARE, + .flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE | + SHRINKER_NONSLAB, };#ifdef CONFIG_DEBUG_FS mm/memcontrol.c +24
-0@@ -4698,6 +4698,11 @@ static struct mem_cgroup *mem_cgroup_alloc(void)#ifdef CONFIG_CGROUP_WRITEBACK INIT_LIST_HEAD (&memcg->cgwb_list);#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +spin_lock_init (&memcg->deferred_split_queue.split_queue_lock); +INIT_LIST_HEAD (&memcg->deferred_split_queue.split_queue); + memcg->deferred_split_queue.split_queue_len =0 ; +#endif idr_replace (&mem_cgroup_idr, memcg, memcg->id.id);return memcg; fail:@@ -5071,6 +5076,14 @@ static int mem_cgroup_move_account(struct page *page,__mod_memcg_state (to,NR_WRITEBACK , nr_pages); } +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (compound && !list_empty (page_deferred_list (page))) { +spin_lock (&from->deferred_split_queue.split_queue_lock); +list_del_init (page_deferred_list (page)); + from->deferred_split_queue.split_queue_len--; +spin_unlock (&from->deferred_split_queue.split_queue_lock); + } +#endif /* * It is safe to change page->mem_cgroup here because the page * is referenced, charged, and isolated - we can't race with @@ -5079,6 +5092,17 @@ static int mem_cgroup_move_account(struct page *page,/* caller should have done css_get */ page->mem_cgroup = to; + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (compound &&list_empty (page_deferred_list (page))) { +spin_lock (&to->deferred_split_queue.split_queue_lock); +list_add_tail (page_deferred_list (page), + &to->deferred_split_queue.split_queue); + to->deferred_split_queue.split_queue_len++; +spin_unlock (&to->deferred_split_queue.split_queue_lock); + } +#endif +spin_unlock_irqrestore (&from->move_lock, flags); ret =0 ;
Re: [v5 PATCH 4/4] mm: thp: make deferred split shrinker memcg aware
Quoted 207 lines · Kirill Tkhai
Quoted 23 lines · Yang Shi
Currently THP deferred split shrinker is not memcg aware, this may cause premature OOM with some configuration. For example the below test would run into premature OOM easily:
$ cgcreate -g memory:thp $ echo 4G > /sys/fs/cgroup/memory/thp/memory/limit_in_bytes $ cgexec -g memory:thp transhuge-stress 4000transhuge-stress comes from kernel selftest.
It is easy to hit OOM, but there are still a lot THP on the deferred split queue, memcg direct reclaim can't touch them since the deferred split shrinker is not memcg aware.
Convert deferred split shrinker memcg aware by introducing per memcg deferred split queue. The THP should be on either per node or per memcg deferred split queue if it belongs to a memcg. When the page is immigrated to the other memcg, it will be immigrated to the target memcg's deferred split queue too.
Reuse the second tail page's
deferred_listfor per memcg list since the same THP can't be on multiple deferred split queues.Quoted 10 lines
Cc Kirill Tkhai <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc "Kirill A . Shutemov" <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shakeel Butt <[email protected]>Cc David Rientjes <[email protected]>Cc Qian Cai <[email protected]>Signed-off-by Yang Shi <[email protected]>Reviewed-by Kirill Tkhai <[email protected]>But, please, see below one small suggestion.
Quoted 157 lines
--- include/linux/huge_mm.h | 9 ++++++ include/linux/memcontrol.h | 4 +++ include/linux/mm_types.h | 1 + mm/huge_memory.c | 76 +++++++++++++++++++++++++++++++++++++++------- mm/memcontrol.c | 24 +++++++++++++++ 5 files changed, 103 insertions(+), 11 deletions(-)include/linux/huge_mm.h +9
-0@@ -267,6 +267,15 @@ static inline bool thp_migration_supported(void) return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION); } +static inline struct list_head *page_deferred_list (struct page *page) +{ +/* +* Global or memcg deferred list in the second tail pages is +* occupied by compound_head. +*/ +return &page[2 ].deferred_list; +} +#else /* CONFIG_TRANSPARENT_HUGEPAGE */ #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })#define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; })include/linux/memcontrol.h +4
-0@@ -312,6 +312,10 @@ struct mem_cgroup {struct list_head event_list;spinlock_t event_list_lock; +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +struct deferred_split deferred_split_queue; +#endif +struct mem_cgroup_per_node *nodeinfo[0 ];/* WARNING: nodeinfo must be the last member here */ };include/linux/mm_types.h +1
-0@@ -139,6 +139,7 @@ struct page {struct {/* Second tail page of compound page */ unsigned long _compound_pad_1;/* compound_head */ unsigned long _compound_pad_2; +/* For both global and memcg */ struct list_head deferred_list; };struct {/* Page table pages */ mm/huge_memory.c +45
-9@@ -495,11 +495,25 @@ pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)return pmd; } -static inline struct list_head *page_deferred_list (struct page *page) +#ifdef CONFIG_MEMCG +static inline struct deferred_split *get_deferred_split_queue (struct page *page) { -/* ->lru in the tail pages is occupied by compound_head. */ -return &page[2 ].deferred_list; +struct mem_cgroup *memcg =compound_head (page)->mem_cgroup; +struct pglist_data *pgdat =NODE_DATA (page_to_nid (page)); + +if (memcg) +return &memcg->deferred_split_queue; +else +return &pgdat->deferred_split_queue; +} +#else +static inline struct deferred_split *get_deferred_split_queue (struct page *page) +{ +struct pglist_data *pgdat =NODE_DATA (page_to_nid (page)); + +return &pgdat->deferred_split_queue; } +#endif void prep_transhuge_page (struct page *page) {@@ -2658,7 +2672,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) {struct page *head =compound_head (page);struct pglist_data *pgdata =NODE_DATA (page_to_nid (head)); -struct deferred_split *ds_queue = &pgdata->deferred_split_queue; +struct deferred_split *ds_queue =get_deferred_split_queue (page);struct anon_vma *anon_vma =NULL ;struct address_space *mapping =NULL ;int count, mapcount, extra_pins, ret;@@ -2794,8 +2808,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)void free_transhuge_page (struct page *page) { -struct pglist_data *pgdata =NODE_DATA (page_to_nid (page)); -struct deferred_split *ds_queue = &pgdata->deferred_split_queue; +struct deferred_split *ds_queue =get_deferred_split_queue (page);unsigned long flags;spin_lock_irqsave (&ds_queue->split_queue_lock, flags);@@ -2809,17 +2822,37 @@ void free_transhuge_page(struct page *page)void deferred_split_huge_page (struct page *page) { -struct pglist_data *pgdata =NODE_DATA (page_to_nid (page)); -struct deferred_split *ds_queue = &pgdata->deferred_split_queue; +struct deferred_split *ds_queue =get_deferred_split_queue (page); +#ifdef CONFIG_MEMCG +struct mem_cgroup *memcg =compound_head (page)->mem_cgroup; +#endif unsigned long flags;VM_BUG_ON_PAGE (!PageTransHuge (page), page); +/* +* The try_to_unmap() in page reclaim path might reach here too, +* this may cause a race condition to corrupt deferred split queue. +* And, if page reclaim is already handling the same page, it is +* unnecessary to handle it again in shrinker. +* +* Check PageSwapCache to determine if the page is being +* handled by page reclaim since THP swap would add the page into +* swap cache before calling try_to_unmap(). +*/ +if (PageSwapCache (page)) +return ; +spin_lock_irqsave (&ds_queue->split_queue_lock, flags);if (list_empty (page_deferred_list (page))) {count_vm_event (THP_DEFERRED_SPLIT_PAGE );list_add_tail (page_deferred_list (page), &ds_queue->split_queue); ds_queue->split_queue_len++; +#ifdef CONFIG_MEMCG +if (memcg) +memcg_set_shrinker_bit (memcg,page_to_nid (page), + deferred_split_shrinker.id); +#endif }spin_unlock_irqrestore (&ds_queue->split_queue_lock, flags); }@@ -2827,8 +2860,19 @@ void deferred_split_huge_page(struct page *page)static unsigned long deferred_split_count (struct shrinker *shrink,struct shrink_control *sc) { +struct deferred_split *ds_queue;struct pglist_data *pgdata =NODE_DATA (sc->nid); -struct deferred_split *ds_queue = &pgdata->deferred_split_queue; + +#ifdef CONFIG_MEMCG +if (!sc->memcg) {mm/huge_memory.c void deferred_split_huge_page(struct page *page) · line 2877
Quoted 10 lines
mm/huge_memory.c +8
-0+ ds_queue = &pgdata->deferred_split_queue; + return READ_ONCE(ds_queue->split_queue_len); + } + + ds_queue = &sc->memcg->deferred_split_queue; +#else + ds_queue = &pgdata->deferred_split_queue; +#endif return READ_ONCE (ds_queue->split_queue_len); }Can we write this a little more compact?
Quoted 6 lines
struct deferred_split *ds_queue = &pgdata->deferred_split_queue;#ifdef CONFIG_MEMCG if (sc->memcg) ds_queue = &sc->memcg->deferred_split_queue;#endif
Yes, sure.
Quoted 3 lines
Or just introduce a helper (something like
get_sc_deferred_split_queue). The same is in .scan method.
A helper sounds fine too, but IMHO I don't see to much benefit since just _count and _scan would call them.
Quoted 85 lines
Quoted 85 lines
mm/huge_memory.c +12
-2@@ -2836,12 +2880,21 @@ static unsigned long deferred_split_scan(struct shrinker *shrink, struct shrink_control *sc) {struct pglist_data *pgdata =NODE_DATA (sc->nid); -struct deferred_split *ds_queue = &pgdata->deferred_split_queue; +struct deferred_split *ds_queue;unsigned long flags;LIST_HEAD (list), *pos, *next;struct page *page;int split =0 ; +#ifdef CONFIG_MEMCG +if (sc->memcg) + ds_queue = &sc->memcg->deferred_split_queue; +else + ds_queue = &pgdata->deferred_split_queue; +#else + ds_queue = &pgdata->deferred_split_queue; +#endif +spin_lock_irqsave (&ds_queue->split_queue_lock, flags);/* Take pin on all head pages to avoid freeing them under us */ list_for_each_safe (pos, next, &ds_queue->split_queue) {@@ -2888,7 +2941,8 @@ static unsigned long deferred_split_scan(struct shrinker *shrink, .count_objects = deferred_split_count, .scan_objects = deferred_split_scan, .seeks = DEFAULT_SEEKS, - .flags = SHRINKER_NUMA_AWARE, + .flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE | + SHRINKER_NONSLAB, };#ifdef CONFIG_DEBUG_FS mm/memcontrol.c +24
-0@@ -4698,6 +4698,11 @@ static struct mem_cgroup *mem_cgroup_alloc(void)#ifdef CONFIG_CGROUP_WRITEBACK INIT_LIST_HEAD (&memcg->cgwb_list);#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +spin_lock_init (&memcg->deferred_split_queue.split_queue_lock); +INIT_LIST_HEAD (&memcg->deferred_split_queue.split_queue); + memcg->deferred_split_queue.split_queue_len =0 ; +#endif idr_replace (&mem_cgroup_idr, memcg, memcg->id.id);return memcg; fail:@@ -5071,6 +5076,14 @@ static int mem_cgroup_move_account(struct page *page,__mod_memcg_state (to,NR_WRITEBACK , nr_pages); } +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (compound && !list_empty (page_deferred_list (page))) { +spin_lock (&from->deferred_split_queue.split_queue_lock); +list_del_init (page_deferred_list (page)); + from->deferred_split_queue.split_queue_len--; +spin_unlock (&from->deferred_split_queue.split_queue_lock); + } +#endif /* * It is safe to change page->mem_cgroup here because the page * is referenced, charged, and isolated - we can't race with @@ -5079,6 +5092,17 @@ static int mem_cgroup_move_account(struct page *page,/* caller should have done css_get */ page->mem_cgroup = to; + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (compound &&list_empty (page_deferred_list (page))) { +spin_lock (&to->deferred_split_queue.split_queue_lock); +list_add_tail (page_deferred_list (page), + &to->deferred_split_queue.split_queue); + to->deferred_split_queue.split_queue_len++; +spin_unlock (&to->deferred_split_queue.split_queue_lock); + } +#endif +spin_unlock_irqrestore (&from->move_lock, flags); ret =0 ;