From patchwork Tue Jul 14 13:57:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 11662935 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 B40C6913 for ; Tue, 14 Jul 2020 13:58:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 804B1224F9 for ; Tue, 14 Jul 2020 13:58:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="sooCPts6" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 804B1224F9 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C6D526B000A; Tue, 14 Jul 2020 09:58:08 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C1B886B000C; Tue, 14 Jul 2020 09:58:08 -0400 (EDT) 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 B32ED8D0001; Tue, 14 Jul 2020 09:58:08 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0090.hostedemail.com [216.40.44.90]) by kanga.kvack.org (Postfix) with ESMTP id 9C5596B000A for ; Tue, 14 Jul 2020 09:58:08 -0400 (EDT) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 48DFC1DF2 for ; Tue, 14 Jul 2020 13:58:08 +0000 (UTC) X-FDA: 77036835456.29.patch05_35050c626ef1 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin29.hostedemail.com (Postfix) with ESMTP id 1336318086E34 for ; Tue, 14 Jul 2020 13:58:08 +0000 (UTC) X-Spam-Summary: 1,0,0,de9a6df969d41c2f,d41d8cd98f00b204,laoar.shao@gmail.com,,RULES_HIT:41:355:379:541:800:960:973:988:989:1260:1345:1437:1535:1542:1711:1730:1747:1777:1792:1801:2198:2199:2393:2559:2562:2731:3138:3139:3140:3141:3142:3353:3865:3867:3868:3870:3871:3874:4321:4605:5007:6261:6653:7514:7875:7903:8660:9121:9149:9413:10004:11026:11233:11473:11658:11914:12043:12048:12296:12297:12438:12517:12519:12533:12550:12555:12895:12986:13148:13161:13229:13230:14096:14181:14394:14687:14721:21080:21324:21444:21451:21627:21666:21939:21990:30012:30054:30070,0,RBL:209.85.222.195:@gmail.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100;04y8t4jzd745wxahpk39miq5qhycaoce5cmsufb3imfzyfis7hyp9u33y3knmft.65nsgrnxeoir7jhdjj3aqurpj7xyt16mo6cdmg68mr9cxfauj93btjqp7zn4ejw.w-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:68,LUA_SUMMARY:none X-HE-Tag: patch05_35050c626ef1 X-Filterd-Recvd-Size: 5151 Received: from mail-qk1-f195.google.com (mail-qk1-f195.google.com [209.85.222.195]) by imf35.hostedemail.com (Postfix) with ESMTP for ; Tue, 14 Jul 2020 13:58:07 +0000 (UTC) Received: by mail-qk1-f195.google.com with SMTP id c139so15585669qkg.12 for ; Tue, 14 Jul 2020 06:58:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=xhQ3En01jhcO6s2/tItYKu6u92mHuyw/WesVyq7m74o=; b=sooCPts6EWnaJTaQNWIvS5PpRpcxz5AFWbpYBC7RPmAMdwcmw7yhIXvny4i0ud8vN2 Bxl/c6Sd4kL6b74iNd148whXKGYUD2kEq0uH4UuFBeDCe4jnSaDg0LeB1GVz27mWT1uq 8foxgJ9qA1LrbK80B9zmmnctZHoS5fZQOk70MuNmi3Q3xT9N+qRvQlAtbYkXLh2lLCF4 gFIr+HkqZHwiALD8dJB7nQNnRNHevqindkmio6XBArzBbUMJnHxqKmo1YDIUxM/bdxIL hd9HdUbX2pIiEyZCvmFo+Ez4j1Dv+tQser8WKnHYEgbC1NntWeRotrZpn3+rNTU7KLhn 2Ssg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=xhQ3En01jhcO6s2/tItYKu6u92mHuyw/WesVyq7m74o=; b=QJQilIg+sftEWhF2YF/uYw3EXvcoWq0XbgvderLRjQBAqaTikxgMgrH5tBiPGj4sjb WngrNPOhutfWOBCIbND9E4nT+eqaq8iOyg3oiNglCHxHwfsu7pxqnPY1Wp8RoFPF13EO ClqLhl7OImiz5oVUC9xsCbUwPy4/ID7m0SH5VCiKdI11n9BYN9PQmH4TdW9Ii7/G4isM WDGTQNMIB20cCOcezLMUWM9L1YWmMXchENidA40S65B1lbuV1nB3hHGNhQfOioCzOwnz Y5wFrj1K4A8yc2T7JTj7wk6nKd8Dg83IrmEF+mYe4+Hxg7r90FDqmQJEzUWc8GQJfmB1 4ehA== X-Gm-Message-State: AOAM531KWPrVumotNrLwDazDQWdNpgvwW4PBVavWJOzi5LiiyUD6NFI+ ob62fytBx98pvPZYqCYDkPc= X-Google-Smtp-Source: ABdhPJw0y1JHFk47j14myPnQ+6KBKxkpH3kMBZit7mA64l4/JxspUgvEEmMn8QxOfT/dDHuNXdjOSg== X-Received: by 2002:a37:9405:: with SMTP id w5mr4652740qkd.18.1594735086779; Tue, 14 Jul 2020 06:58:06 -0700 (PDT) Received: from dev.localdomain ([183.134.211.52]) by smtp.gmail.com with ESMTPSA id w18sm22366599qtn.3.2020.07.14.06.58.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 Jul 2020 06:58:06 -0700 (PDT) From: Yafang Shao To: mhocko@kernel.org, penguin-kernel@i-love.sakura.ne.jp, rientjes@google.com, akpm@linux-foundation.org, hannes@cmpxchg.org Cc: linux-mm@kvack.org, Yafang Shao Subject: [PATCH v2] memcg, oom: check memcg margin for parallel oom Date: Tue, 14 Jul 2020 09:57:14 -0400 Message-Id: <1594735034-19190-1-git-send-email-laoar.shao@gmail.com> X-Mailer: git-send-email 1.8.3.1 X-Rspamd-Queue-Id: 1336318086E34 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 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: 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 Cc: Michal Hocko Cc: Tetsuo Handa Cc: David Rientjes Cc: Johannes Weiner Acked-by: Michal Hocko Acked-by: Chris Down --- v1 -> v2: - commit log improved by Michal - retitle the subject from "mm, oom: check memcg margin for parallel oom" - code simplicity, per Michal --- mm/memcontrol.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 1962232..15e0e18 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1560,15 +1560,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; }