From patchwork Fri Nov 6 08:51:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhong jiang X-Patchwork-Id: 11886403 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AAADE6A2 for ; Fri, 6 Nov 2020 08:51:20 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4536A206DC for ; Fri, 6 Nov 2020 08:51:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4536A206DC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 4D8746B005C; Fri, 6 Nov 2020 03:51:19 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4619F6B005D; Fri, 6 Nov 2020 03:51:19 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 350606B0068; Fri, 6 Nov 2020 03:51:19 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0249.hostedemail.com [216.40.44.249]) by kanga.kvack.org (Postfix) with ESMTP id F385B6B005C for ; Fri, 6 Nov 2020 03:51:18 -0500 (EST) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id A37F645B3 for ; Fri, 6 Nov 2020 08:51:18 +0000 (UTC) X-FDA: 77453374236.08.mass11_0403b08272d1 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin08.hostedemail.com (Postfix) with ESMTP id 82AD41819E764 for ; Fri, 6 Nov 2020 08:51:18 +0000 (UTC) X-Spam-Summary: 1,0,0,ff9f0c3498f2b4af,d41d8cd98f00b204,zhongjiang-ali@linux.alibaba.com,,RULES_HIT:41:355:379:541:800:960:967:973:988:989:1260:1261:1263:1345:1431:1437:1534:1542:1711:1730:1747:1777:1792:1801:2198:2199:2393:2525:2559:2563:2682:2685:2731:2859:2902:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3353:3865:3867:3868:3870:3871:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4321:4605:5007:6261:7514:7576:7903:8660:9025:9121:9149:9391:10004:10913:11026:11233:11473:11658:11914:12043:12296:12297:12438:12550:12555:12679:12783:12895:12986:13148:13161:13229:13230:13846:14096:14181:14394:14721:14849:21080:21324:21433:21451:21554:21627:21819:21939:21990:30054:30070,0,RBL:115.124.30.54:@linux.alibaba.com:.lbl8.mailshell.net-62.20.2.100 64.201.201.201;04yr9n6qtupk3pu1m67r4q9jyhdpxopod8xur67dmmxswsz3jkknseyb4bfcx3f.szfjf47u1ntiwj5i31mbi8iooq9exac3j74dmg68mr9cxfauj93btjqp7zn4ejw.w-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0. 5,Netche X-HE-Tag: mass11_0403b08272d1 X-Filterd-Recvd-Size: 3719 Received: from out30-54.freemail.mail.aliyun.com (out30-54.freemail.mail.aliyun.com [115.124.30.54]) by imf02.hostedemail.com (Postfix) with ESMTP for ; Fri, 6 Nov 2020 08:51:15 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R561e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=alimailimapcm10staff010182156082;MF=zhongjiang-ali@linux.alibaba.com;NM=1;PH=DS;RN=4;SR=0;TI=SMTPD_---0UEPUE3w_1604652665; Received: from localhost(mailfrom:zhongjiang-ali@linux.alibaba.com fp:SMTPD_---0UEPUE3w_1604652665) by smtp.aliyun-inc.com(127.0.0.1); Fri, 06 Nov 2020 16:51:10 +0800 From: zhongjiang-ali To: hannes@cmpxchg.org, mhocko@kernel.org, akpm@linux-foundation.org Cc: linux-mm@kvack.org Subject: [PATCH] memcg, oom: check memcg margin for parallel oom Date: Fri, 6 Nov 2020 16:51:05 +0800 Message-Id: <1604652665-13640-1-git-send-email-zhongjiang-ali@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Yafang Shao Memcg oom killer invocation is synchronized by the global oom_lock and tasks are sleeping on the lock while somebody is selecting the victim or potentially race with the oom_reaper is releasing the victim's memory. This can result in a pointless oom killer invocation because a waiter might be racing with the oom_reaper P1 oom_reaper P2 oom_reap_task mutex_lock(oom_lock) out_of_memory # no victim because we have one already __oom_reap_task_mm mute_unlock(oom_lock) mutex_lock(oom_lock) set MMF_OOM_SKIP select_bad_process # finds a new victim The page allocator prevents from this race by trying to allocate after the lock can be acquired (in __alloc_pages_may_oom) which acts as a last minute check. Moreover page allocator simply doesn't block on the oom_lock and simply retries the whole reclaim process. Memcg oom killer should do the last minute check as well. Call mem_cgroup_margin to do that. Trylock on the oom_lock could be done as well but this doesn't seem to be necessary at this stage. [mhocko@kernel.org: commit log] Suggested-by: Michal Hocko Signed-off-by: Yafang Shao Signed-off-by: Andrew Morton Acked-by: Michal Hocko Acked-by: Chris Down Cc: Tetsuo Handa Cc: David Rientjes Cc: Johannes Weiner Link: http://lkml.kernel.org/r/1594735034-19190-1-git-send-email-laoar.shao@gmail.com Signed-off-by: Linus Torvalds Signed-off-by: zhongjiang-ali --- mm/memcontrol.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index b30a52d..369d9e1 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1663,15 +1663,21 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, .gfp_mask = gfp_mask, .order = order, }; - bool ret; + bool ret = true; if (mutex_lock_killable(&oom_lock)) return true; + + if (mem_cgroup_margin(memcg) >= (1 << order)) + goto unlock; + /* * A few threads which were not waiting at mutex_lock_killable() can * fail to bail out. Therefore, check again after holding oom_lock. */ ret = should_force_charge() || out_of_memory(&oc); + +unlock: mutex_unlock(&oom_lock); return ret; }