From 9cef2ea5e7777de822b6eb2118f0c7a653735bc4 Mon Sep 17 00:00:00 2001 From: Johannes Weiner Date: Thu, 6 Aug 2020 23:22:15 -0700 Subject: [PATCH 2433/2944] mm: memcontrol: don't count limit-setting reclaim as memory pressure to #31164083 commit e22c6ed90aa91abc08f107344428ebb8c2629e98 upstream When an outside process lowers one of the memory limits of a cgroup (or uses the force_empty knob in cgroup1), direct reclaim is performed in the context of the write(), in order to directly enforce the new limit and have it being met by the time the write() returns. Currently, this reclaim activity is accounted as memory pressure in the cgroup that the writer(!) belongs to. This is unexpected. It specifically causes problems for senpai (https://github.com/facebookincubator/senpai), which is an agent that routinely adjusts the memory limits and performs associated reclaim work in tens or even hundreds of cgroups running on the host. The cgroup that senpai is running in itself will report elevated levels of memory pressure, even though it itself is under no memory shortage or any sort of distress. Move the psi annotation from the central cgroup reclaim function to callsites in the allocation context, and thereby no longer count any limit-setting reclaim as memory pressure. If the newly set limit causes the workload inside the cgroup into direct reclaim, that of course will continue to count as memory pressure. Signed-off-by: Johannes Weiner Signed-off-by: Andrew Morton Reviewed-by: Shakeel Butt Reviewed-by: Roman Gushchin Acked-by: Chris Down Acked-by: Michal Hocko Link: http://lkml.kernel.org/r/20200728135210.379885-2-hannes@cmpxchg.org Signed-off-by: Linus Torvalds Signed-off-by: zhongjiang-ali Reviewed-by: Xunlei Pang --- mm/memcontrol.c | 14 +++++++++++++- mm/vmscan.c | 5 ----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 208268b..c335c83 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2445,6 +2445,7 @@ static void reclaim_wmark(struct mem_cgroup *memcg) long nr_pages; struct mem_cgroup *iter; u64 start, duration; + unsigned long pflags; if (is_wmark_ok(memcg, false)) return; @@ -2463,7 +2464,9 @@ static void reclaim_wmark(struct mem_cgroup *memcg) * overhead-accuracy trade-off. */ start = ktime_get_ns(); + psi_memstall_enter(&pflags); try_to_free_mem_cgroup_pages(memcg, nr_pages, GFP_KERNEL, true); + psi_memstall_leave(&pflags); duration = ktime_get_ns() - start; css_get(&memcg->css); @@ -2491,12 +2494,18 @@ static unsigned long reclaim_high(struct mem_cgroup *memcg, unsigned long nr_reclaimed = 0; do { + unsigned long pflags; + if (page_counter_read(&memcg->memory) <= READ_ONCE(memcg->memory.high)) continue; + memcg_memory_event(memcg, MEMCG_HIGH); + + psi_memstall_enter(&pflags); nr_reclaimed += try_to_free_mem_cgroup_pages(memcg, nr_pages, gfp_mask, true); + psi_memstall_leave(&pflags); } while ((memcg = parent_mem_cgroup(memcg)) && !mem_cgroup_is_root(memcg)); @@ -2765,11 +2774,12 @@ static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, int nr_retries = MAX_RECLAIM_RETRIES; struct mem_cgroup *mem_over_limit; struct page_counter *counter; + enum oom_status oom_status; unsigned long nr_reclaimed; bool may_swap = true; bool drained = false; bool oomed = false; - enum oom_status oom_status; + unsigned long pflags; u64 start; if (mem_cgroup_is_root(memcg)) @@ -2831,8 +2841,10 @@ static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, memcg_memory_event(mem_over_limit, MEMCG_MAX); memcg_lat_stat_start(&start); + psi_memstall_enter(&pflags); nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages, gfp_mask, may_swap); + psi_memstall_leave(&pflags); memcg_lat_stat_end(MEM_LAT_MEMCG_DIRECT_RECLAIM, start); if (mem_cgroup_margin(mem_over_limit) >= nr_pages) diff --git a/mm/vmscan.c b/mm/vmscan.c index 0b8f963..0066520 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3340,7 +3340,6 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, { struct zonelist *zonelist; unsigned long nr_reclaimed; - unsigned long pflags; int nid; unsigned int noreclaim_flag; struct scan_control sc = { @@ -3368,15 +3367,11 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, sc.may_writepage, sc.gfp_mask, sc.reclaim_idx); - - psi_memstall_enter(&pflags); noreclaim_flag = memalloc_noreclaim_save(); nr_reclaimed = do_try_to_free_pages(zonelist, &sc); memalloc_noreclaim_restore(noreclaim_flag); - psi_memstall_leave(&pflags); - trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); return nr_reclaimed; -- 1.8.3.1