mm: memcg softlimit reclaim rework
Subsystems (8)
PATCH v5 3/5 mm: memcg detect no memcgs above softlimit under zone reclaim
In reply to Ying HanIn memcg kernel, cgroup under its softlimit is not targeted under global reclaim. It could be possible that all memcgs are under their softlimit for a particular zone. If that is the case, the current implementation will burn extra cpu cycles without making forward progress.
The idea is from LSF discussion where we detect it after the first round of scanning and restart the reclaim by not looking at softlimit at all. This allows us to make forward progress on shrink_zone().
--- mm/vmscan.c | 17 +++++++++++++++-- 1 files changed, 15 insertions(+), 2 deletions(-)
mm/vmscan.c +15 -2
@@ -1827,6 +1827,10 @@ static void shrink_zone(struct zone *zone, struct scan_control *sc) .priority = sc->priority, };struct mem_cgroup *memcg; +bool over_softlimit, ignore_softlimit = false; + +restart: + over_softlimit = false; memcg =mem_cgroup_iter (root,NULL , &reclaim); do {
@@ -1845,10 +1849,14 @@ static void shrink_zone(struct zone *zone, struct scan_control *sc)* we have to reclaim under softlimit instead of burning more * cpu cycles. */ -if (!global_reclaim (sc) || sc->priority <DEF_PRIORITY -2 || -should_reclaim_mem_cgroup (memcg)) +if (ignore_softlimit || !global_reclaim (sc) || + sc->priority <DEF_PRIORITY -2 || +should_reclaim_mem_cgroup (memcg)) {shrink_lruvec (lruvec, sc); + over_softlimit = true; + } +/* * Limit reclaim has historically picked one memcg and * scanned it with decreasing priority levels until
@@ -1865,6 +1873,11 @@ static void shrink_zone(struct zone *zone, struct scan_control *sc) } memcg =mem_cgroup_iter (root, memcg, &reclaim); }while (memcg); + +if (!over_softlimit) { + ignore_softlimit = true; + goto restart; + } }/* Returns true if compaction should go ahead for a high-order request */
Signature · 7 lines
1.7.7.3 -- 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>
PATCH v5 1/5 mm: memcg softlimit reclaim rework
This patch reverts all the existing softlimit reclaim implementations and instead integrates the softlimit reclaim into existing global reclaim logic.
The new softlimit reclaim includes the following changes:
1. add function should_reclaim_mem_cgroup()
Add the filter function should_reclaim_mem_cgroup() under the common function shrink_zone(). The later one is being called both from per-memcg reclaim as well as global reclaim.
Today the softlimit takes effect only under global memory pressure. The memcgs get free run above their softlimit until there is a global memory contention. This patch doesn't change the semantics.
Under the global reclaim, we try to skip reclaiming from a memcg under its softlimit. To prevent reclaim from trying too hard on hitting memcgs (above softlimit) w/ only hard-to-reclaim pages, the reclaim priority is used to skip the softlimit check. This is a trade-off of system performance and resource isolation.
2. "hierarchical" softlimit reclaim
This is consistant to how softlimit was previously implemented, where the pressure is put for the whole hiearchy as long as the "root" of the hierarchy over its softlimit.
This part is not in my previous posts, and is quite different from my understanding of softlimit reclaim. After quite a lot of discussions with Johannes and Michal, i decided to go with it for now. And this is designed to work with both trusted setups and untrusted setups.
What's the trusted and untrusted setups ?
case 1 : Administrator is the only one setting up the limits and also he expects gurantees of memory under each cgroup's softlimit:
Considering the following:
root (soft: unlimited, use_hierarchy = 1)
-- A (soft: unlimited, usage 22G)
-- A1 (soft: 10G, usage 17G)
-- A2 (soft: 6G, usage 5G)-- B (soft: 16G, usage 10G)
So we have A1 above its softlimit and none of its ancestor does, then global reclaim will only pick A1 to reclaim first.
case 2: Untrusted enviroment where cgroups changes its softlimit or adminstrator could make mistakes. In that case, we still want to attack the mis-configured child if its parent is above softlimit.
Considering the following:
root (soft: unlimited, use_hierarchy = 1)
-- A (soft: 16G, usage 22G)
-- A1 (soft: 10G, usage 17G)
-- A2 (soft: 1000G, usage 5G)-- B (soft: 16G, usage 10G)
Here A2 would set its softlimit way higher than its parent, but the current logic makes sure to still attack it when A exceeds its softlimit.
v4..v5:
1. rebase the patchset on memcg-dev tree
2. apply KOSAKI's patch on do_try_to_free_pages()
--- include/linux/memcontrol.h | 18 +- include/linux/swap.h | 4 - mm/memcontrol.c | 455 +++----------------------------------------- mm/vmscan.c | 82 ++------- 4 files changed, 49 insertions(+), 510 deletions(-)
include/linux/memcontrol.h +7 -11
@@ -124,6 +124,8 @@ extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,extern void mem_cgroup_replace_page_cache (struct page *oldpage,struct page *newpage); +extern bool should_reclaim_mem_cgroup (struct mem_cgroup *memcg); +#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP extern int do_swap_account;#endif
@@ -179,9 +181,6 @@ static inline void mem_cgroup_dec_page_stat(struct page *page,mem_cgroup_update_page_stat (page, idx,-1 ); } -unsigned long mem_cgroup_soft_limit_reclaim (struct zone *zone,int order, -gfp_t gfp_mask, -unsigned long *total_scanned);u64 mem_cgroup_get_limit (struct mem_cgroup *memcg);void mem_cgroup_count_vm_event (struct mm_struct *mm,enum vm_event_item idx);
@@ -359,14 +358,6 @@ static inline void mem_cgroup_dec_page_stat(struct page *page, }static inline -unsigned long mem_cgroup_soft_limit_reclaim (struct zone *zone,int order, -gfp_t gfp_mask, -unsigned long *total_scanned) -{ -return 0 ; -} - -static inline u64 mem_cgroup_get_limit (struct mem_cgroup *memcg) {return 0 ;
@@ -384,6 +375,11 @@ static inline void mem_cgroup_replace_page_cache(struct page *oldpage, struct page *newpage) { } +static inline +bool should_reclaim_mem_cgroup (struct mem_cgroup *memcg) +{ +return true; +}#endif /* CONFIG_CGROUP_MEM_CONT */ #if !defined(CONFIG_CGROUP_MEM_RES_CTLR ) || !defined(CONFIG_DEBUG_VM )
include/linux/swap.h +0 -4
@@ -254,10 +254,6 @@ extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order,extern int __isolate_lru_page (struct page *page,isolate_mode_t mode);extern unsigned long try_to_free_mem_cgroup_pages (struct mem_cgroup *mem,gfp_t gfp_mask,bool noswap); -extern unsigned long mem_cgroup_shrink_node_zone (struct mem_cgroup *mem, -gfp_t gfp_mask,bool noswap, -struct zone *zone, -unsigned long *nr_scanned);extern unsigned long shrink_all_memory (unsigned long nr_pages);extern int vm_swappiness;extern int remove_mapping (struct address_space *mapping,struct page *page);
mm/memcontrol.c +26 -429
578 lines not on this page · raw message
mm/vmscan.c +16 -66
118 lines not on this page · raw message
Signature · 7 lines
1.7.7.3 -- 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>
PATCH v5 4/5 mm, vmscan: fix do_try_to_free_pages() livelock
Currently, do_try_to_free_pages() can enter livelock. Because of, now vmscan has two conflicted policies.
1) kswapd sleep when it couldn't reclaim any page even though reach priority 0. This is because to avoid kswapd() infinite loop. That said, kswapd assume direct reclaim makes enough free pages either regular page reclaim or oom-killer. This logic makes kswapd -> direct-reclaim dependency.
2) direct reclaim continue to reclaim without oom-killer until kswapd turn on zone->all_unreclaimble. This is because to avoid too early oom-kill.
This logic makes direct-reclaim -> kswapd dependency.
In worst case, direct-reclaim may continue to page reclaim forever when kswapd is slept and any other thread don't wakeup kswapd.
We can't turn on zone->all_unreclaimable because this is racy. direct reclaim path don't take any lock. Thus this patch removes zone->all_unreclaimable field completely and recalculates every time.
Note: we can't take the idea that direct-reclaim see zone->pages_scanned directly and kswapd continue to use zone->all_unreclaimable. Because, it is racy. commit 929bea7c71 (vmscan: all_unreclaimable() use zone->all_unreclaimable as a name) describes the detail.
--- include/linux/mm_inline.h | 19 ++++++++++++++++ include/linux/mmzone.h | 2 +- include/linux/vmstat.h | 1 - mm/page-writeback.c | 2 + mm/page_alloc.c | 5 +-- mm/vmscan.c | 51 +++++++++++++------------------------------- mm/vmstat.c | 3 +- 7 files changed, 41 insertions(+), 42 deletions(-)
include/linux/mm_inline.h +19 -0
@@ -2,6 +2,7 @@#define LINUX_MM_INLINE_H #include <linux/huge_mm.h> +#include <linux/swap.h> /** * page_is_file_cache - should the page be on a file LRU or anon LRU?
@@ -99,4 +100,22 @@ static __always_inline enum lru_list page_lru(struct page *page) return lru; } +static inline unsigned long zone_reclaimable_pages (struct zone *zone) +{ +int nr; + + nr =zone_page_state (zone,NR_ACTIVE_FILE ) + +zone_page_state (zone,NR_INACTIVE_FILE ); + +if (nr_swap_pages >0 ) + nr +=zone_page_state (zone,NR_ACTIVE_ANON ) + +zone_page_state (zone,NR_INACTIVE_ANON ); + +return nr; +} + +static inline bool zone_reclaimable (struct zone *zone) +{ +return zone->pages_scanned <zone_reclaimable_pages (zone) *6 ; +}#endif
include/linux/mmzone.h +1 -1
@@ -343,7 +343,7 @@ struct zone {* free areas of different sizes */ spinlock_t lock; -int all_unreclaimable;/* All pages pinned */ +#ifdef CONFIG_MEMORY_HOTPLUG /* see spanned/present_pages for more description */ seqlock_t span_seqlock;
include/linux/vmstat.h +0 -1
@@ -140,7 +140,6 @@ static inline unsigned long zone_page_state_snapshot(struct zone *zone, }extern unsigned long global_reclaimable_pages (void ); -extern unsigned long zone_reclaimable_pages (struct zone *zone);#ifdef CONFIG_NUMA /*
mm/page-writeback.c +2 -0
@@ -34,6 +34,8 @@#include <linux/syscalls.h> #include <linux/buffer_head.h> /* __set_page_dirty_buffers */ #include <linux/pagevec.h> +#include <linux/mm_inline.h> +#include <trace/events/writeback.h> /*
mm/page_alloc.c +2 -3
@@ -58,6 +58,7 @@#include <linux/memcontrol.h> #include <linux/prefetch.h> #include <linux/page-debug-flags.h> +#include <linux/mm_inline.h> #include <asm/tlbflush.h> #include <asm/div64.h>
@@ -637,7 +638,6 @@ static void free_pcppages_bulk(struct zone *zone, int count,int to_free = count;spin_lock (&zone->lock); - zone->all_unreclaimable =0 ; zone->pages_scanned =0 ;while (to_free) {
@@ -679,7 +679,6 @@ static void free_one_page(struct zone *zone, struct page *page, int order, int migratetype) {spin_lock (&zone->lock); - zone->all_unreclaimable =0 ; zone->pages_scanned =0 ;__free_one_page (page, zone, order, migratetype);
@@ -2814,7 +2813,7 @@ void show_free_areas(unsigned int filter)K (zone_page_state (zone,NR_BOUNCE )),K (zone_page_state (zone,NR_WRITEBACK_TEMP )), zone->pages_scanned, - (zone->all_unreclaimable ?"yes" :"no" ) + (zone_reclaimable (zone) ?"yes" :"no" ) );printk ("lowmem_reserve[]:" );for (i =0 ; i <MAX_NR_ZONES ; i++)
mm/vmscan.c +15 -36
131 lines not on this page · raw message
mm/vmstat.c +2 -1
@@ -19,6 +19,7 @@#include <linux/math64.h> #include <linux/writeback.h> #include <linux/compaction.h> +#include <linux/mm_inline.h> #ifdef CONFIG_VM_EVENT_COUNTERS DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
@@ -1019,7 +1020,7 @@ static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,"\n all_unreclaimable: %u" "\n start_pfn: %lu" "\n inactive_ratio: %u" , - zone->all_unreclaimable, + !zone_reclaimable (zone), zone->zone_start_pfn, zone->inactive_ratio);seq_putc (m,'\n' );
Signature · 7 lines
1.7.7.3 -- 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>
PATCH v2 2/5 mm: memcg set soft_limit_in_bytes to 0 by default
In reply to Ying HanThis idea is based on discussion with Michal and Johannes from LSF.
1. If soft_limit are all set to MAX, it wastes first three priority iterations without scanning anything.
2. By default every memcg is eligible for softlimit reclaim, and we can also set the value to MAX for special memcg which is immune to soft limit reclaim.
There is a behavior change after this patch: (N == DEF_PRIORITY - 2)
A: usage > softlimit B: usage <= softlimit U: softlimit unset old: reclaim at each priority reclaim when priority < N reclaim when priority < N new: reclaim at each priority reclaim when priority < N reclaim at each priority
Note: I can leave the counter->soft_limit uninitialized, at least all the caller of res_counter_init() have the memcg as pre-zeroed structure. However, I might be better not rely on that.
--- kernel/res_counter.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/res_counter.c +1 -1
@@ -18,7 +18,7 @@ void res_counter_init(struct res_counter *counter, struct res_counter *parent) {spin_lock_init (&counter->lock); counter->limit =RESOURCE_MAX ; - counter->soft_limit =RESOURCE_MAX ; + counter->soft_limit =0 ; counter->parent = parent; }
Signature · 7 lines
1.7.7.3 -- 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>
PATCH v5 5/5 mm: memcg discount pages under softlimit from per-zone reclaimable_pages
In reply to Ying HanThe function zone_reclaimable() marks zone->all_unreclaimable based on per-zone pages_scanned and reclaimable_pages. If all_unreclaimable is true, alloc_pages could go to OOM instead of getting stuck in page reclaim.
In memcg kernel, cgroup under its softlimit is not targeted under global reclaim. So we need to remove those pages from reclaimable_pages, otherwise it will cause reclaim mechanism to get stuck trying to reclaim from all_unreclaimable zone.
--- include/linux/mm_inline.h | 32 +++++++++++++++++++++++++------- mm/vmscan.c | 8 -------- 2 files changed, 25 insertions(+), 15 deletions(-)
include/linux/mm_inline.h +25 -7
@@ -100,18 +100,36 @@ static __always_inline enum lru_list page_lru(struct page *page)return lru; } +static inline unsigned long get_lru_size (struct lruvec *lruvec, +enum lru_list lru) +{ +if (!mem_cgroup_disabled ()) +return mem_cgroup_get_lru_size (lruvec, lru); + +return zone_page_state (lruvec_zone (lruvec),NR_LRU_BASE + lru); +} +static inline unsigned long zone_reclaimable_pages (struct zone *zone) { -int nr; +int nr =0 ; +struct mem_cgroup *memcg; + + memcg =mem_cgroup_iter (NULL ,NULL ,NULL ); +do { +struct lruvec *lruvec =mem_cgroup_zone_lruvec (zone, memcg); - nr =zone_page_state (zone,NR_ACTIVE_FILE ) + -zone_page_state (zone,NR_INACTIVE_FILE ); +if (should_reclaim_mem_cgroup (memcg)) { + nr +=get_lru_size (lruvec,LRU_INACTIVE_FILE ) + +get_lru_size (lruvec,LRU_ACTIVE_FILE ); -if (nr_swap_pages >0 ) - nr +=zone_page_state (zone,NR_ACTIVE_ANON ) + -zone_page_state (zone,NR_INACTIVE_ANON ); +if (nr_swap_pages >0 ) + nr +=get_lru_size (lruvec,LRU_ACTIVE_ANON ) + +get_lru_size (lruvec,LRU_INACTIVE_ANON ); + } + memcg =mem_cgroup_iter (NULL , memcg,NULL ); + }while (memcg); -return nr; +return nr; }static inline bool zone_reclaimable (struct zone *zone)
mm/vmscan.c +0 -8
@@ -145,14 +145,6 @@ static bool global_reclaim(struct scan_control *sc) }#endif -static unsigned long get_lru_size (struct lruvec *lruvec,enum lru_list lru) -{ -if (!mem_cgroup_disabled ()) -return mem_cgroup_get_lru_size (lruvec, lru); - -return zone_page_state (lruvec_zone (lruvec),NR_LRU_BASE + lru); -} -/* * Add a shrinker callback to be called from the vm */
Signature · 7 lines
1.7.7.3 -- 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>
Johannes Weiner
maintainerQuoted 7 lines · Ying Han
This patch reverts all the existing softlimit reclaim implementations and instead integrates the softlimit reclaim into existing global reclaim logic.
The new softlimit reclaim includes the following changes:
1. add function
should_reclaim_mem_cgroup()
Quoted 7 lines
Add the filter function
should_reclaim_mem_cgroup()under the common functionshrink_zone(). The later one is being called both from per-memcg reclaim as well as global reclaim.Today the softlimit takes effect only under global memory pressure. The memcgs get free run above their softlimit until there is a global memory contention. This patch doesn't change the semantics.
But it's quite a performance regression. Maybe it would be better after all to combine this change with 'make 0 the default'?
Yes, I was the one asking for the changes to be separated, if possible, but I didn't mean regressing in between. No forward dependencies in patch series, please.
Quoted 8 lines
Under the global reclaim, we try to skip reclaiming from a memcg under its softlimit. To prevent reclaim from trying too hard on hitting memcgs (above softlimit) w/ only hard-to-reclaim pages, the reclaim priority is used to skip the softlimit check. This is a trade-off of system performance and resource isolation.
2. "hierarchical" softlimit reclaim
Quoted 8 lines
This is consistant to how softlimit was previously implemented, where the pressure is put for the whole hiearchy as long as the "root" of the hierarchy over its softlimit.
This part is not in my previous posts, and is quite different from my understanding of softlimit reclaim. After quite a lot of discussions with Johannes and Michal, i decided to go with it for now. And this is designed to work with both trusted setups and untrusted setups.
This may be really confusing to someone uninvolved reading the changelog as it doesn't have anything to do with what the patch actually does.
It may be better to include past discussion outcomes in the introductary email of a series.
Quoted 10 lines
mm/memcontrol.c +0
-2@@ -870,8 +672,6 @@ static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)preempt_enable ();mem_cgroup_threshold (memcg); -if (unlikely (do_softlimit)) -mem_cgroup_update_tree (memcg, page);#if MAX_NUMNODES >1 if (unlikely (do_numainfo))atomic_inc (&memcg->numainfo_events);
mm/memcontrol.c struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) · line 725
Quoted 4 lines
mm/memcontrol.c +1
-0@@ -922,6 +722,31 @@ struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)return memcg; } +bool should_reclaim_mem_cgroup (struct mem_cgroup *memcg)
I'm not too fond of the magical name. The API provides an information about soft limits, the decision should rest with vmscan.c.
mem_cgroup_over_soft_limit() e.g.?
mm/memcontrol.c struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) · line 737
Quoted 12 lines
mm/memcontrol.c +12
-0+{ +if (mem_cgroup_disabled ()) +return true; + +/* +* We treat the root cgroup special here to always reclaim pages. +* Now root cgroup has its own lru, and the only chance to reclaim +* pages from it is through global reclaim. note, root cgroup does +* not trigger targeted reclaim. +*/ +if (mem_cgroup_is_root (memcg)) +return true;
With the soft limit at 0, the comment is no longer accurate because this check turns into a simple optimization. We could check the res_counter soft limit, which would always result in the root group being above the limit, but we take the short cut.
mm/memcontrol.c struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) · line 742
Quoted 4 lines
mm/memcontrol.c +4
-0+for (; memcg; memcg =parent_mem_cgroup (memcg)) { +/* This is global reclaim, stop at root cgroup */ +if (mem_cgroup_is_root (memcg)) +break ;
I don't see why you add this check and the comment does not help.
mm/memcontrol.c struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) · line 752
Quoted 10 lines
mm/memcontrol.c +7
-0+if (res_counter_soft_limit_excess (&memcg->res)) +return true; + } + +return false; +} +/** * mem_cgroup_iter - iterate over memory cgroup hierarchy * @root: hierarchy root
--
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>
Johannes Weiner
maintainerRe: [PATCH V5 5/5] mm: memcg discount pages under softlimit from per-zone reclaimable_pages
In reply to Ying HanQuoted 3 lines · Ying Han
The function
zone_reclaimable()markszone->all_unreclaimablebased on per-zonepages_scannedandreclaimable_pages. Ifall_unreclaimableis true,alloc_pagescould go to OOM instead of getting stuck in page reclaim.
There is no zone->all_unreclaimable at this point, you removed it in the previous patch.
Quoted 4 lines
In memcg kernel, cgroup under its softlimit is not targeted under global reclaim. So we need to remove those pages from
reclaimable_pages, otherwise it will cause reclaim mechanism to get stuck trying to reclaim fromall_unreclaimablezone.
Can't you check if zone->pages_scanned changed in between reclaim runs?
Or sum up the scanned and reclaimable pages encountered while iterating the hierarchy during regular reclaim and then use those numbers in the equation instead of the per-zone counters?
Walking the full global hierarchy in all the places where we check if a zone is reclaimable is a scalability nightmare.
Quoted 18 lines
include/linux/mm_inline.h +11
-1@@ -100,18 +100,36 @@ static __always_inline enum lru_list page_lru(struct page *page)return lru; } +static inline unsigned long get_lru_size (struct lruvec *lruvec, +enum lru_list lru) +{ +if (!mem_cgroup_disabled ()) +return mem_cgroup_get_lru_size (lruvec, lru); + +return zone_page_state (lruvec_zone (lruvec),NR_LRU_BASE + lru); +} +static inline unsigned long zone_reclaimable_pages (struct zone *zone) { -int nr; +int nr =0 ; +struct mem_cgroup *memcg;
include/linux/mm_inline.h static __always_inline enum lru_list page_lru(struct page *page) · line 123
Quoted 10 lines
include/linux/mm_inline.h +7
-2+ + memcg =mem_cgroup_iter (NULL ,NULL ,NULL ); + do { + struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); - nr =zone_page_state (zone,NR_ACTIVE_FILE ) + -zone_page_state (zone,NR_INACTIVE_FILE ); + if (should_reclaim_mem_cgroup(memcg)) { + nr += get_lru_size(lruvec, LRU_INACTIVE_FILE) + + get_lru_size(lruvec, LRU_ACTIVE_FILE);
Sometimes, the number of reclaimable pages DO include those of groups for which should_reclaim_mem_cgroup() is false: when the priority level is <= DEF_PRIORITY - 2, as you defined in 1/5! This means that you consider pages you just scanned unreclaimable, which can result in the zone being unreclaimable after the DEF_PRIORITY - 2 cycle, no?
--
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>
Re: [PATCH V5 4/5] mm, vmscan: fix do_try_to_free_pages() livelock
In reply to Ying HanQuoted 26 lines · Ying Han
Currently,
do_try_to_free_pages()can enter livelock. Because of, now vmscan has two conflicted policies.1) kswapd sleep when it couldn't reclaim any page even though reach priority 0. This is because to avoid
kswapd()infinite loop. That said, kswapd assume direct reclaim makes enough free pages either regular page reclaim or oom-killer. This logic makes kswapd -> direct-reclaim dependency.
2) direct reclaim continue to reclaim without oom-killer until kswapd turn onzone->all_unreclaimble. This is because to avoid too early oom-kill.
This logic makes direct-reclaim -> kswapd dependency.In worst case, direct-reclaim may continue to page reclaim forever when kswapd is slept and any other thread don't wakeup kswapd.
We can't turn on
zone->all_unreclaimablebecause this is racy. direct reclaim path don't take any lock. Thus this patch removeszone->all_unreclaimablefield completely and recalculates every time.Note: we can't take the idea that direct-reclaim see
zone->pages_scanneddirectly and kswapd continue to usezone->all_unreclaimable. Because, it is racy. commit929bea7c71(vmscan:all_unreclaimable()usezone->all_unreclaimableas a name) describes the detail.
Quoted 4 lines
Reported-by Aaditya Kumar <[email protected]>Reported-by Ying Han <[email protected]>Signed-off-by KOSAKI Motohiro <[email protected]>Acked-by Rik van Riel <[email protected]>
Please drop this. I've got some review comment about this patch and i need respin. but thank you for paying attention this.
--
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>
Re: [PATCH V5 4/5] mm, vmscan: fix do_try_to_free_pages() livelock
Quoted 27 lines · KOSAKI Motohiro
Quoted 26 lines · Ying Han
Currently,
do_try_to_free_pages()can enter livelock. Because of, now vmscan has two conflicted policies.1) kswapd sleep when it couldn't reclaim any page even though reach priority 0. This is because to avoid
kswapd()infinite loop. That said, kswapd assume direct reclaim makes enough free pages either regular page reclaim or oom-killer. This logic makes kswapd -> direct-reclaim dependency.
2) direct reclaim continue to reclaim without oom-killer until kswapd turn onzone->all_unreclaimble. This is because to avoid too early oom-kill.
This logic makes direct-reclaim -> kswapd dependency.In worst case, direct-reclaim may continue to page reclaim forever when kswapd is slept and any other thread don't wakeup kswapd.
We can't turn on
zone->all_unreclaimablebecause this is racy. direct reclaim path don't take any lock. Thus this patch removeszone->all_unreclaimablefield completely and recalculates every time.Note: we can't take the idea that direct-reclaim see
zone->pages_scanneddirectly and kswapd continue to usezone->all_unreclaimable. Because, it is racy. commit929bea7c71(vmscan:all_unreclaimable()usezone->all_unreclaimableas a name) describes the detail.
Quoted 7 lines
Quoted 4 lines
Reported-by Aaditya Kumar <[email protected]>Reported-by Ying Han <[email protected]>Signed-off-by KOSAKI Motohiro <[email protected]>Acked-by Rik van Riel <[email protected]>Please drop this. I've got some review comment about this patch and i need respin. but thank you for paying attention this.
Thanks for the heads up. Are you working on the new version of it, since I included this patch in my softlimit reclaim patchset as a replacement of one patch i had.
--Ying
Quoted 3 lines
--
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>
Quoted 16 lines · Johannes Weiner
Quoted 7 lines · Ying Han
This patch reverts all the existing softlimit reclaim implementations and instead integrates the softlimit reclaim into existing global reclaim logic.
The new softlimit reclaim includes the following changes:
1. add function
should_reclaim_mem_cgroup()Quoted 7 lines
Add the filter function
should_reclaim_mem_cgroup()under the common functionshrink_zone(). The later one is being called both from per-memcg reclaim as well as global reclaim.Today the softlimit takes effect only under global memory pressure. The memcgs get free run above their softlimit until there is a global memory contention. This patch doesn't change the semantics.
Quoted 6 lines
But it's quite a performance regression. Maybe it would be better after all to combine this change with 'make 0 the default'?
Yes, I was the one asking for the changes to be separated, if possible, but I didn't mean regressing in between. No forward dependencies in patch series, please.
Ok, I don't have problem to squash that patch in next time.
Quoted 18 lines
Quoted 9 lines
Under the global reclaim, we try to skip reclaiming from a memcg under its softlimit. To prevent reclaim from trying too hard on hitting memcgs (above softlimit) w/ only hard-to-reclaim pages, the reclaim priority is used to skip the softlimit check. This is a trade-off of system performance and resource isolation.
2. "hierarchical" softlimit reclaim
Quoted 8 lines
This is consistant to how softlimit was previously implemented, where the pressure is put for the whole hiearchy as long as the "root" of the hierarchy over its softlimit.
This part is not in my previous posts, and is quite different from my understanding of softlimit reclaim. After quite a lot of discussions with Johannes and Michal, i decided to go with it for now. And this is designed to work with both trusted setups and untrusted setups.
Quoted 6 lines
This may be really confusing to someone uninvolved reading the changelog as it doesn't have anything to do with what the patch actually does.
It may be better to include past discussion outcomes in the introductary email of a series.
I will try to include some of the points from our last discussion in the commit log.
Quoted 13 lines
Quoted 13 lines
mm/memcontrol.c +0
-2@@ -870,8 +672,6 @@ static void memcg_check_events(struct mem_cgroup *memcg, struct page *page) preempt_enable(); mem_cgroup_threshold(memcg); - if (unlikely(do_softlimit)) - mem_cgroup_update_tree(memcg, page); #if MAX_NUMNODES > 1 if (unlikely(do_numainfo)) atomic_inc(&memcg->numainfo_events);@@ -922,6 +722,31 @@ struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) return memcg; }
Quoted 6 lines
Review struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) · line 725
Quoted 1 line
(quoted diff) +1
-0+bool should_reclaim_mem_cgroup(struct mem_cgroup *memcg)I'm not too fond of the magical name. The API provides an information about soft limits, the decision should rest with
vmscan.c.
mem_cgroup_over_soft_limit()e.g.?
That is fine w/ me.
Quoted 10 lines
Quoted 10 lines
mm/memcontrol.c +9
-0+{ + if (mem_cgroup_disabled()) + return true; + + /* +* We treat the root cgroup special here to always reclaim pages. +* Now root cgroup has its own lru, and the only chance to reclaim +* pages from it is through global reclaim. note, root cgroup does +* not trigger targeted reclaim.
Quoted 8 lines
mm/memcontrol.c struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) · line 737
Quoted 3 lines
mm/memcontrol.c +3
-0+*/ + if (mem_cgroup_is_root(memcg)) + return true;With the soft limit at 0, the comment is no longer accurate because this check turns into a simple optimization. We could check the
res_countersoft limit, which would always result in the root group being above the limit, but we take the short cut.
For root group, my intention here is always reclaim pages from it regardless of the softlimit setting. And the reason is exactly the one in the comment. If the softlimit is set to 0 as default, I agree this is then a short cut.
Anything you suggest that I need to change here?
Quoted 7 lines
mm/memcontrol.c struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) · line 742
Quoted 5 lines
mm/memcontrol.c +4
-0+ for (; memcg; memcg = parent_mem_cgroup(memcg)) { + /* This is global reclaim, stop at root cgroup */ + if (mem_cgroup_is_root(memcg)) + break;I don't see why you add this check and the comment does not help.
The root cgroup would have softlimit set to 0 ( in most of the cases ), and not skipping root will make everyone reclaimable here.
Thank you for reviewing !
--Ying
Quoted 11 lines
mm/memcontrol.c struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) · line 752
Quoted 11 lines
mm/memcontrol.c +7
-0+ if (res_counter_soft_limit_excess(&memcg->res)) + return true; + } + + return false; +} + /*** mem_cgroup_iter - iterate over memory cgroup hierarchy * @root: hierarchy root
--
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>
Re: [PATCH V5 5/5] mm: memcg discount pages under softlimit from per-zone reclaimable_pages
In reply to Johannes WeinerQuoted 7 lines · Johannes Weiner
Quoted 3 lines · Ying Han
The function
zone_reclaimable()markszone->all_unreclaimablebased on per-zonepages_scannedandreclaimable_pages. Ifall_unreclaimableis true,alloc_pagescould go to OOM instead of getting stuck in page reclaim.There is no
zone->all_unreclaimableat this point, you removed it in the previous patch.
Ah, forgot to update the commit log after applying the recent patch from Kosaki.
Quoted 8 lines
Quoted 4 lines
In memcg kernel, cgroup under its softlimit is not targeted under global reclaim. So we need to remove those pages from
reclaimable_pages, otherwise it will cause reclaim mechanism to get stuck trying to reclaim fromall_unreclaimablezone.Can't you check if
zone->pages_scannedchanged in between reclaim runs?
Quoted 6 lines
Or sum up the scanned and reclaimable pages encountered while iterating the hierarchy during regular reclaim and then use those numbers in the equation instead of the per-zone counters?
Walking the full global hierarchy in all the places where we check if a zone is reclaimable is a scalability nightmare.
I agree on that, i will exploring a bit more on that.
Quoted 24 lines
Quoted 24 lines
include/linux/mm_inline.h +15
-1@@ -100,18 +100,36 @@ static __always_inline enum lru_list page_lru(struct page *page) return lru; } +static inline unsigned long get_lru_size (struct lruvec *lruvec, + enum lru_list lru) +{ + if (!mem_cgroup_disabled()) + return mem_cgroup_get_lru_size(lruvec, lru); + + return zone_page_state(lruvec_zone(lruvec), NR_LRU_BASE + lru); +} + static inline unsigned long zone_reclaimable_pages(struct zone *zone) { - int nr; + int nr = 0; + struct mem_cgroup *memcg; + + memcg = mem_cgroup_iter(NULL, NULL, NULL); + do { + struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
Quoted 11 lines
include/linux/mm_inline.h static __always_inline enum lru_list page_lru(struct page *page) · line 123
Quoted 5 lines
include/linux/mm_inline.h +3
-2- nr = zone_page_state(zone, NR_ACTIVE_FILE) + - zone_page_state(zone, NR_INACTIVE_FILE); + if (should_reclaim_mem_cgroup(memcg)) { + nr += get_lru_size(lruvec, LRU_INACTIVE_FILE) + + get_lru_size(lruvec, LRU_ACTIVE_FILE);Sometimes, the number of reclaimable pages DO include those of groups for which
should_reclaim_mem_cgroup()is false: when the priority level is <=DEF_PRIORITY- 2, as you defined in 1/5! This means that you consider pages you just scanned unreclaimable, which can result in the zone being unreclaimable after theDEF_PRIORITY- 2 cycle, no?
That is true and I thought about it as well. I would as well adding the priority check here where only start considering the pages if the priority < DEF_PRIORITY - 2
--Ying
--
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>
Johannes Weiner
maintainerQuoted 20 lines · Ying Han
Quoted 10 lines · Johannes Weiner
Quoted 9 lines · Ying Han
(quoted diff) +9
-0+{ + if (mem_cgroup_disabled()) + return true; + + /* + * We treat the root cgroup special here to always reclaim pages. + * Now root cgroup has its own lru, and the only chance to reclaim + * pages from it is through global reclaim. note, root cgroup does + * not trigger targeted reclaim.Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ */ + if (mem_cgroup_is_root(memcg)) + return true;With the soft limit at 0, the comment is no longer accurate because this check turns into a simple optimization. We could check the
res_countersoft limit, which would always result in the root group being above the limit, but we take the short cut.
Quoted 6 lines
For root group, my intention here is always reclaim pages from it regardless of the softlimit setting. And the reason is exactly the one in the comment. If the softlimit is set to 0 as default, I agree this is then a short cut.
Anything you suggest that I need to change here?
Well, not in this patch as it stands. But once you squash the '0 per default', it may be good to note that this is a shortcut.
Quoted 9 lines
Quoted 6 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ for (; memcg; memcg = parent_mem_cgroup(memcg)) { + /* This is global reclaim, stop at root cgroup */ + if (mem_cgroup_is_root(memcg)) + break;I don't see why you add this check and the comment does not help.
The root cgroup would have softlimit set to 0 ( in most of the cases ), and not skipping root will make everyone reclaimable here.
Only if root_mem_cgroup->use_hierarchy is set. At the same time, we usually behave as if this was the case, in accounting and reclaim.
Right now we allow setting the soft limit in root_mem_cgroup but it does not make any sense. After your patch, even less so, because of these shortcut checks that now actually change semantics. Could we make this more consistent to users and forbid setting as soft limit in root_mem_cgroup? Patch below.
The reason this behaves differently from hard limits is because the soft limits now have double meaning; they are upper limit and minimum guarantee at the same time. The unchangeable defaults in the root cgroup should be "no guarantee" and "unlimited soft limit" at the same time, but that is obviously not possible if these are opposing range ends of the same knob. So we pick no guarantees, always up for reclaim when looking top down but also behave as if the soft limit was unlimited in the root cgroup when looking bottom up.
This is what the second check does. But I think it needs a clearer comment.
---
From: Johannes Weiner <[email protected]>
Subject: mm: memcg: forbid setting soft limit on root cgroup
Setting a soft limit in the root cgroup does not make sense, as soft limits are enforced hierarchically and the root cgroup is the hierarchical parent of every other cgroup. It would not provide the discrimination between groups that soft limits are usually used for.
With the current implementation of soft limits, it would only make global reclaim more aggressive compared to target reclaim, but we absolutely don't want anyone to rely on this behaviour.
---
mm/memcontrol.c +4 -0
@@ -3905,6 +3967,10 @@ static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, ret =mem_cgroup_resize_memsw_limit (memcg, val);break ;case RES_SOFT_LIMIT : +if (mem_cgroup_is_root (memcg)) {/* Can't set limit on root */ + ret = -EINVAL ; +break ; + } ret =res_counter_memparse_write_strategy (buffer, &val);if (ret)break ;
--
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>
Quoted 26 lines · Johannes Weiner
Quoted 25 lines · Ying Han
Quoted 10 lines · Johannes Weiner
Quoted 9 lines · Ying Han
(quoted diff) +9
-0+{ + if (mem_cgroup_disabled()) + return true; + + /* + * We treat the root cgroup special here to always reclaim pages. + * Now root cgroup has its own lru, and the only chance to reclaim + * pages from it is through global reclaim. note, root cgroup does + * not trigger targeted reclaim.Quoted 8 lines
Quoted 3 lines
+ */
+ if (mem_cgroup_is_root(memcg))
+ return true;With the soft limit at 0, the comment is no longer accurate because this check turns into a simple optimization. We could check the
res_countersoft limit, which would always result in the root group being above the limit, but we take the short cut.For root group, my intention here is always reclaim pages from it regardless of the softlimit setting. And the reason is exactly the one in the comment. If the softlimit is set to 0 as default, I agree this is then a short cut.
Quoted 4 lines
Quoted 1 line
Anything you suggest that I need to change here?
Well, not in this patch as it stands. But once you squash the '0 per default', it may be good to note that this is a shortcut.
Will include some notes next time.
Quoted 55 lines
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines
+ for (; memcg; memcg =
parent_mem_cgroup(memcg)) {
+ /* This is global reclaim, stop at root cgroup */
+ if (mem_cgroup_is_root(memcg))
+ break;I don't see why you add this check and the comment does not help.
The root cgroup would have softlimit set to 0 ( in most of the cases ), and not skipping root will make everyone reclaimable here.
Only if
root_mem_cgroup->use_hierarchyis set. At the same time, we usually behave as if this was the case, in accounting and reclaim.Right now we allow setting the soft limit in
root_mem_cgroupbut it does not make any sense. After your patch, even less so, because of these shortcut checks that now actually change semantics. Could we make this more consistent to users and forbid setting as soft limit inroot_mem_cgroup? Patch below.The reason this behaves differently from hard limits is because the soft limits now have double meaning; they are upper limit and minimum guarantee at the same time. The unchangeable defaults in the root cgroup should be "no guarantee" and "unlimited soft limit" at the same time, but that is obviously not possible if these are opposing range ends of the same knob. So we pick no guarantees, always up for reclaim when looking top down but also behave as if the soft limit was unlimited in the root cgroup when looking bottom up.
This is what the second check does. But I think it needs a clearer comment.
---
From: Johannes Weiner <[email protected]>
Subject: mm: memcg: forbid setting soft limit on root cgroupSetting a soft limit in the root cgroup does not make sense, as soft limits are enforced hierarchically and the root cgroup is the hierarchical parent of every other cgroup. It would not provide the discrimination between groups that soft limits are usually used for.
With the current implementation of soft limits, it would only make global reclaim more aggressive compared to target reclaim, but we absolutely don't want anyone to rely on this behaviour.
Signed-off-by Johannes Weiner <[email protected]>---
mm/memcontrol.c +0
-0@@ -3905,6 +3967,10 @@ static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, ret = mem_cgroup_resize_memsw_limit(memcg, val); break;
Quoted 8 lines
case
RES_SOFT_LIMIT:
+ if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
+ ret =-EINVAL;
+ break;
+ }
ret =res_counter_memparse_write_strategy(buffer, &val); if (ret)
break;
Thanks, the patch makes sense to me and I will include in the next post.
--Ying
--
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>
Re: [PATCH V5 5/5] mm: memcg discount pages under softlimit from per-zone reclaimable_pages
In reply to Johannes WeinerQuoted 16 lines · Johannes Weiner
Quoted 3 lines · Ying Han
The function
zone_reclaimable()markszone->all_unreclaimablebased on per-zonepages_scannedandreclaimable_pages. Ifall_unreclaimableis true,alloc_pagescould go to OOM instead of getting stuck in page reclaim.There is no
zone->all_unreclaimableat this point, you removed it in the previous patch.Quoted 4 lines
In memcg kernel, cgroup under its softlimit is not targeted under global reclaim. So we need to remove those pages from
reclaimable_pages, otherwise it will cause reclaim mechanism to get stuck trying to reclaim fromall_unreclaimablezone.Can't you check if
zone->pages_scannedchanged in between reclaim runs?
Quoted 6 lines
Or sum up the scanned and reclaimable pages encountered while iterating the hierarchy during regular reclaim and then use those numbers in the equation instead of the per-zone counters?
Walking the full global hierarchy in all the places where we check if a zone is reclaimable is a scalability nightmare.
One way to solve this is to record the per-zone reclaimable pages ( sum of reclaimable pages of memcg above softlimits ) after each shrink_zone(). The later function does walk the memcg hierarchy and also checks the softlimit, so we don't need to do it again. The new value pages_reclaimed is recorded per-zone, and the caller side could use that to compare w/ zone->pages_scanned.
While I run tests on the patch, it turns out that I can not reproduce the problem ( machine hang while over-committing the softlimit) even w/o the patch. Then I realize that the problem only exist in the internal version we don't have the check "sc->priority < DEF_PRIORITY
- 2" to bypass softlimit check. The reason we did that part is to guarantee no global pressure on high priority memcgs. So In that case, global reclaim can never steal any pages from any memgs and the system can easily hang.
This is not the case in the version I am posting here. The patch guarantees not looping in memcgs all under softlimit by :
1. detects whether no memcg above their softlimit, if so, skip checking softlimit
2. only check softlimit memcg if priority is >= DEF_PRIORITY - 2
In summary, the problem described in this patch doesn't exist. So I am thinking to drop this one on my next post. Please comment.
--Ying
Quoted 23 lines
Quoted 23 lines
include/linux/mm_inline.h +15
-1@@ -100,18 +100,36 @@ static __always_inline enum lru_list page_lru(struct page *page) return lru; } +static inline unsigned long get_lru_size (struct lruvec *lruvec, + enum lru_list lru) +{ + if (!mem_cgroup_disabled()) + return mem_cgroup_get_lru_size(lruvec, lru); + + return zone_page_state(lruvec_zone(lruvec), NR_LRU_BASE + lru); +} + static inline unsigned long zone_reclaimable_pages(struct zone *zone) { - int nr; + int nr = 0; + struct mem_cgroup *memcg; + + memcg = mem_cgroup_iter(NULL, NULL, NULL); + do { + struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
Quoted 11 lines
include/linux/mm_inline.h static __always_inline enum lru_list page_lru(struct page *page) · line 123
Quoted 5 lines
include/linux/mm_inline.h +3
-2- nr = zone_page_state(zone, NR_ACTIVE_FILE) + - zone_page_state(zone, NR_INACTIVE_FILE); + if (should_reclaim_mem_cgroup(memcg)) { + nr += get_lru_size(lruvec, LRU_INACTIVE_FILE) + + get_lru_size(lruvec, LRU_ACTIVE_FILE);Sometimes, the number of reclaimable pages DO include those of groups for which
should_reclaim_mem_cgroup()is false: when the priority level is <=DEF_PRIORITY- 2, as you defined in 1/5! This means that you consider pages you just scanned unreclaimable, which can result in the zone being unreclaimable after theDEF_PRIORITY- 2 cycle, no?
--
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>