Make deferred split shrinker memcg aware
Subsystems (8)
v4 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:
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 | 4 +++ include/linux/mm_types.h | 1 + include/linux/mmzone.h | 12 ++++++--- include/linux/shrinker.h | 3 ++- mm/huge_memory.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++------------------ mm/memcontrol.c | 24 +++++++++++++++++ mm/page_alloc.c | 9 ++++--- mm/swap.c | 2 +- mm/vmscan.c | 36 +++++++++++++------------ 10 files changed, 147 insertions(+), 51 deletions(-)
PATCH v4 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
@@ -6582,9 +6582,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
PATCH v4 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 +- 2 files changed, 2 insertions(+), 1 deletion(-)
mm/page_alloc.c +1 -0
@@ -624,6 +624,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); }
Signature · 1 line
1.8.3.1
PATCH v4 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/shrinker.h | 3 ++- mm/vmscan.c | 36 +++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 18 deletions(-)
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/vmscan.c +19 -17
@@ -174,8 +174,7 @@ struct scan_control {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
@@ -227,18 +226,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 */ - -#ifdef CONFIG_MEMCG static bool global_reclaim (struct scan_control *sc) {return !sc->target_mem_cgroup;
@@ -293,6 +281,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;
@@ -579,7 +576,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) {
@@ -587,7 +584,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))
@@ -613,6 +610,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);
@@ -649,13 +651,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
Signature · 1 line
1.8.3.1
PATCH v4 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 | 63 ++++++++++++++++++++++++++++++++++++++-------- mm/memcontrol.c | 24 ++++++++++++++++++ 5 files changed, 90 insertions(+), 11 deletions(-)
include/linux/huge_mm.h +9 -0
@@ -250,6 +250,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
@@ -311,6 +311,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 +52 -11
119 lines not on this page · raw message
mm/memcontrol.c +24 -0
@@ -4585,6 +4585,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:
@@ -4955,6 +4960,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
@@ -4963,6 +4976,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