mm, thp: make deferred_split_shrinker memcg-aware
Subsystems (8)
RFC PATCH 1/1 mm, thp: make deferred_split_shrinker memcg-aware
deferred_split_shrinker is NUMA aware. Making it memcg-aware if CONFIG_MEMCG is enabled to prevent shrinking memory of memcg(s) that are not under memory pressure. This change isolates memory pressure across memcgs from deferred_split_shrinker perspective, by not prematurely splitting huge pages for the memcg that is not under memory pressure.
Note that a pte-mapped compound huge page charge is not moved to the dst memcg on task migration. Look mem_cgroup_move_charge_pte_range() for more information. Thus, mem_cgroup_move_account doesn't get called on pte-mapped compound huge pages, hence we do not need to transfer the page from source-memcg's split to destinations-memcg's split_queue.
Tested: Ran two copies of a microbenchmark with partially unmapped thp(s) in two separate memory cgroups. When first memory cgroup is put under memory pressure, it's own thp(s) split. Other memcg's thp(s) remain intact.
Current implementation is not NUMA aware if MEMCG is compiled. If it is important to have this shrinker both NUMA and MEMCG aware, I can work on that. Some feedback on this front will be useful.
--- include/linux/huge_mm.h | 1 + include/linux/memcontrol.h | 5 ++ include/linux/mmzone.h | 9 ++-- include/linux/shrinker.h | 19 +++++++ mm/huge_memory.c | 124 ++++++++++++++++++++++++++++++++++++--------- mm/memcontrol.c | 21 ++++++++ mm/page_alloc.c | 8 ++- 7 files changed, 153 insertions(+), 34 deletions(-)
include/linux/huge_mm.h +1 -0
@@ -130,6 +130,7 @@ extern unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags); +extern struct list_head *page_deferred_list (struct page *page);extern void prep_transhuge_page (struct page *page);extern void free_transhuge_page (struct page *page);
include/linux/memcontrol.h +5 -0
@@ -30,6 +30,7 @@#include <linux/vmstat.h> #include <linux/writeback.h> #include <linux/page-flags.h> +#include <linux/shrinker.h> struct mem_cgroup ;struct page ;
@@ -257,6 +258,10 @@ struct mem_cgroup {struct wb_domain cgwb_domain;#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +struct thp_split_shrinker thp_split_shrinker; +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ +/* List of events which userspace want to receive */ struct list_head event_list;spinlock_t event_list_lock;
include/linux/mmzone.h +4 -5
@@ -18,6 +18,7 @@#include <linux/page-flags-layout.h> #include <linux/atomic.h> #include <asm/page.h> +#include <linux/shrinker.h> /* Free memory management - zoned buddy allocator. */ #ifndef CONFIG_FORCE_MAX_ZONEORDER
@@ -702,11 +703,9 @@ typedef struct pglist_data {unsigned long static_init_size;#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; -#endif +#if definedCONFIG_TRANSPARENT_HUGEPAGE && !definedCONFIG_MEMCG +struct thp_split_shrinker thp_split_shrinker; +#endif /*CONFIG_TRANSPARENT_HUGEPAGE && !CONFIG_MEMCG */ /* Fields commonly accessed by the page reclaim scanner */ struct lruvec lruvec;
include/linux/shrinker.h +19 -0
@@ -76,4 +76,23 @@ struct shrinker {extern int register_shrinker (struct shrinker *);extern void unregister_shrinker (struct shrinker *); + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +struct thp_split_shrinker { +spinlock_t split_queue_lock; +/* +* List of partially mapped THPs, that can be split under memory +* pressure. +*/ +struct list_head split_queue; +unsigned long split_queue_len; +}; + +static inline void thp_split_shrinker_init (struct thp_split_shrinker *thp_ss) +{ +spin_lock_init (&thp_ss->split_queue_lock); +INIT_LIST_HEAD (&thp_ss->split_queue); + thp_ss->split_queue_len =0 ; +} +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ #endif
mm/huge_memory.c +100 -24
237 lines not on this page · raw message
mm/memcontrol.c +21 -0
@@ -35,6 +35,7 @@#include <linux/memcontrol.h> #include <linux/cgroup.h> #include <linux/mm.h> +#include <linux/huge_mm.h> #include <linux/sched/mm.h> #include <linux/shmem_fs.h> #include <linux/hugetlb.h>
@@ -4252,6 +4253,9 @@ static struct mem_cgroup *mem_cgroup_alloc(void)#ifdef CONFIG_CGROUP_WRITEBACK INIT_LIST_HEAD (&memcg->cgwb_list);#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +thp_split_shrinker_init (&memcg->thp_split_shrinker); +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ idr_replace (&mem_cgroup_idr, memcg, memcg->id.id);return memcg; fail:
@@ -5682,8 +5686,25 @@ static void uncharge_page(struct page *page, struct uncharge_gather *ug)unsigned int nr_pages =1 ;if (PageTransHuge (page)) { +struct page *head =compound_head (page); +unsigned long flags; +struct thp_split_shrinker *thp_ss = + &ug->memcg->thp_split_shrinker; + nr_pages <<=compound_order (page); ug->nr_huge += nr_pages; + +/* +* If this transhuge_page is being uncharged from this +* memcg, then remove it from this memcg's split queue. +*/ +spin_lock_irqsave (&thp_ss->split_queue_lock, flags); +if (!list_empty (page_deferred_list (head))) { + thp_ss->split_queue_len--; +list_del (page_deferred_list (head)); + } +spin_unlock_irqrestore (&thp_ss->split_queue_lock, + flags); }if (PageAnon (page)) ug->nr_anon += nr_pages;
mm/page_alloc.c +3 -5
@@ -6045,11 +6045,9 @@ static void __paginginit free_area_init_core(struct pglist_data *pgdat) pgdat->numabalancing_migrate_nr_pages =0 ; pgdat->numabalancing_migrate_next_window = jiffies;#endif -#ifdef CONFIG_TRANSPARENT_HUGEPAGE -spin_lock_init (&pgdat->split_queue_lock); -INIT_LIST_HEAD (&pgdat->split_queue); - pgdat->split_queue_len =0 ; -#endif +#if definedCONFIG_TRANSPARENT_HUGEPAGE && !definedCONFIG_MEMCG +thp_split_shrinker_init (&pgdata->thp_split_shrinker); +#endif /* CONFIG_TRANSPARENT_HUGEPAGE && !CONFIG_MEMCG */ init_waitqueue_head (&pgdat->kswapd_wait);init_waitqueue_head (&pgdat->pfmemalloc_wait);#ifdef CONFIG_COMPACTION
Signature · 7 lines
2.15.0.rc1.287.g2b38de12cc-goog -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Michal Hocko
maintainerQuoted 5 lines · Neha Agarwal
deferred_split_shrinkeris NUMA aware. Making it memcg-aware ifCONFIG_MEMCGis enabled to prevent shrinking memory of memcg(s) that are not under memory pressure. This change isolates memory pressure across memcgs fromdeferred_split_shrinkerperspective, by not prematurely splitting huge pages for the memcg that is not under memory pressure.
Why do we need this? THP pages are usually not shared between memcgs. Or do you have a real world example where this is not the case? Your patch is adding quite a lot of (and to be really honest very ugly) code so there better should be a _very_ good reason to justify it. I haven't looked very closely to the code, at least all those ifdefs in the code are too ugly to live.
Signature · 2 lines
Michal Hocko SUSE Labs
Hi Michal,
Let me try to pitch the motivation first:
In the case of NUMA-aware shrinker, memory pressure may lead to splitting and freeing subpages within a THP, irrespective of whether the page belongs to the memcg that is under memory pressure. THP sharing between memcgs is not a pre-condition for above to happen.
Let's consider two memcgs: memcg-A and memcg-B. Say memcg-A is under memory pressure that is hitting its limit. If this memory pressure invokes the shrinker (non-memcg-aware) and splits pages from memcg-B queued for deferred splits, then that won't reduce memcg-A's usage. It will reduce memcg-B's usage. Also, why should memcg-A's memory pressure reduce memcg-B's usage.
By making this shrinker memcg-aware, we can invoke respective memcg shrinkers to handle the memory pressure. Furthermore, with this approach we can isolate the THPs of other memcg(s) (not under memory pressure) from premature splits. Isolation aids in reducing performance impact when we have several memcgs on the same machine.
Regarding ifdef ugliness: I get your point and agree with you on that. I think I can do a better job at restricting the ugliness, will post another version.
Thanks,
-Neha Agarwal
Quoted 17 lines · Michal Hocko
Quoted 5 lines · Neha Agarwal
deferred_split_shrinkeris NUMA aware. Making it memcg-aware ifCONFIG_MEMCGis enabled to prevent shrinking memory of memcg(s) that are not under memory pressure. This change isolates memory pressure across memcgs fromdeferred_split_shrinkerperspective, by not prematurely splitting huge pages for the memcg that is not under memory pressure.Why do we need this? THP pages are usually not shared between memcgs. Or do you have a real world example where this is not the case? Your patch is adding quite a lot of (and to be really honest very ugly) code so there better should be a _very_ good reason to justify it. I haven't looked very closely to the code, at least all those ifdefs in the code are too ugly to live.
--
Michal Hocko
SUSE Labs
[Sorry for multiple emails, it wasn't in plain text before, thus resending.]
Quoted 7 lines · Michal Hocko
Quoted 5 lines · Neha Agarwal
deferred_split_shrinkeris NUMA aware. Making it memcg-aware ifCONFIG_MEMCGis enabled to prevent shrinking memory of memcg(s) that are not under memory pressure. This change isolates memory pressure across memcgs fromdeferred_split_shrinkerperspective, by not prematurely splitting huge pages for the memcg that is not under memory pressure.
Quoted 9 lines
Why do we need this? THP pages are usually not shared between memcgs. Or do you have a real world example where this is not the case? Your patch is adding quite a lot of (and to be really honest very ugly) code so there better should be a _very_ good reason to justify it. I haven't looked very closely to the code, at least all those ifdefs in the code are too ugly to live.
--
Michal Hocko
SUSE Labs
Hi Michal,
Let me try to pitch the motivation first:
In the case of NUMA-aware shrinker, memory pressure may lead to splitting and freeing subpages within a THP, irrespective of whether the page belongs to the memcg that is under memory pressure. THP sharing between memcgs is not a pre-condition for above to happen.
Let's consider two memcgs: memcg-A and memcg-B. Say memcg-A is under memory pressure that is hitting its limit. If this memory pressure invokes the shrinker (non-memcg-aware) and splits pages from memcg-B queued for deferred splits, then that won't reduce memcg-A's usage. It will reduce memcg-B's usage. Also, why should memcg-A's memory pressure reduce memcg-B's usage.
By making this shrinker memcg-aware, we can invoke respective memcg shrinkers to handle the memory pressure. Furthermore, with this approach we can isolate the THPs of other memcg(s) (not under memory pressure) from premature splits. Isolation aids in reducing performance impact when we have several memcgs on the same machine.
Regarding ifdef ugliness: I get your point and agree with you on that. I think I can do a better job at restricting the ugliness, will post another version.
Signature · 2 lines
Thanks, Neha Agarwal
Quoted 20 lines · Neha Agarwal
[Sorry for multiple emails, it wasn't in plain text before, thus resending.]
Quoted 7 lines · Michal Hocko
Quoted 5 lines · Neha Agarwal
deferred_split_shrinkeris NUMA aware. Making it memcg-aware ifCONFIG_MEMCGis enabled to prevent shrinking memory of memcg(s) that are not under memory pressure. This change isolates memory pressure across memcgs fromdeferred_split_shrinkerperspective, by not prematurely splitting huge pages for the memcg that is not under memory pressure.Quoted 9 lines
Why do we need this? THP pages are usually not shared between memcgs. Or do you have a real world example where this is not the case? Your patch is adding quite a lot of (and to be really honest very ugly) code so there better should be a _very_ good reason to justify it. I haven't looked very closely to the code, at least all those ifdefs in the code are too ugly to live.
--
Michal Hocko
SUSE Labs
Quoted 7 lines
Hi Michal,
Let me try to pitch the motivation first:
In the case of NUMA-aware shrinker, memory pressure may lead to splitting and freeing subpages within a THP, irrespective of whether the page belongs to the memcg that is under memory pressure. THP sharing between memcgs is not a pre-condition for above to happen.
I think I got confused here. The point I want to make is that when a memcg is under memory pressure, only memcg-aware shrinkers are called. However, a memcg with partially-mapped THPs (which can be split and thus free up subpages) should be be able to split such THPs, to avoid oom-kills under memory pressure. By making this shrinker memcg-aware, we will be able to free up subpages by splitting partially-mapped THPs under memory pressure.
Quoted 21 lines
Let's consider two memcgs: memcg-A and memcg-B. Say memcg-A is under memory pressure that is hitting its limit. If this memory pressure invokes the shrinker (non-memcg-aware) and splits pages from memcg-B queued for deferred splits, then that won't reduce memcg-A's usage. It will reduce memcg-B's usage. Also, why should memcg-A's memory pressure reduce memcg-B's usage.
By making this shrinker memcg-aware, we can invoke respective memcg shrinkers to handle the memory pressure. Furthermore, with this approach we can isolate the THPs of other memcg(s) (not under memory pressure) from premature splits. Isolation aids in reducing performance impact when we have several memcgs on the same machine.
Regarding ifdef ugliness: I get your point and agree with you on that. I think I can do a better job at restricting the ugliness, will post another version.
--
Thanks,
Neha Agarwal
Signature · 2 lines
Thanks, Neha Agarwal
Michal Hocko
maintainerQuoted 29 lines · Neha Agarwal
Quoted 20 lines · Neha Agarwal
[Sorry for multiple emails, it wasn't in plain text before, thus resending.]
Quoted 7 lines · Michal Hocko
Quoted 5 lines · Neha Agarwal
deferred_split_shrinkeris NUMA aware. Making it memcg-aware ifCONFIG_MEMCGis enabled to prevent shrinking memory of memcg(s) that are not under memory pressure. This change isolates memory pressure across memcgs fromdeferred_split_shrinkerperspective, by not prematurely splitting huge pages for the memcg that is not under memory pressure.Quoted 9 lines
Why do we need this? THP pages are usually not shared between memcgs. Or do you have a real world example where this is not the case? Your patch is adding quite a lot of (and to be really honest very ugly) code so there better should be a _very_ good reason to justify it. I haven't looked very closely to the code, at least all those ifdefs in the code are too ugly to live.
--
Michal Hocko
SUSE LabsQuoted 7 lines
Hi Michal,
Let me try to pitch the motivation first:
In the case of NUMA-aware shrinker, memory pressure may lead to splitting and freeing subpages within a THP, irrespective of whether the page belongs to the memcg that is under memory pressure. THP sharing between memcgs is not a pre-condition for above to happen.
Quoted 7 lines
I think I got confused here. The point I want to make is that when a memcg is under memory pressure, only memcg-aware shrinkers are called. However, a memcg with partially-mapped THPs (which can be split and thus free up subpages) should be be able to split such THPs, to avoid oom-kills under memory pressure. By making this shrinker memcg-aware, we will be able to free up subpages by splitting partially-mapped THPs under memory pressure.
I still do not understand, sorry. How can we result in OOM due to THP splitting. Please make sure to describe user visible effects that you are seeing and why you think they need fixing along with a description on how the fix works.
So far I am kinda lost to see what you are trying to achieve and why.
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 12 lines · Neha Agarwal
deferred_split_shrinkeris NUMA aware. Making it memcg-aware ifCONFIG_MEMCGis enabled to prevent shrinking memory of memcg(s) that are not under memory pressure. This change isolates memory pressure across memcgs fromdeferred_split_shrinkerperspective, by not prematurely splitting huge pages for the memcg that is not under memory pressure.Note that a pte-mapped compound huge page charge is not moved to the dst memcg on task migration. Look
mem_cgroup_move_charge_pte_range()for more information. Thus,mem_cgroup_move_accountdoesn't get called on pte-mapped compound huge pages, hence we do not need to transfer the page from source-memcg's split to destinations-memcg'ssplit_queue.
Quoted 8 lines
Tested: Ran two copies of a microbenchmark with partially unmapped thp(s) in two separate memory cgroups. When first memory cgroup is put under memory pressure, it's own thp(s) split. Other memcg's thp(s) remain intact.
Current implementation is not NUMA aware if MEMCG is compiled. If it is important to have this shrinker both NUMA and MEMCG aware, I can work on that. Some feedback on this front will be useful.
I thin, this should be done. That's strange compromise -- memcg vs NUMA. And I think solving will help a lot with ifdefs.
Signature · 1 line
Kirill A. Shutemov