From patchwork Tue Mar 10 21:55:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Rientjes X-Patchwork-Id: 11430365 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 A8CC4138D for ; Tue, 10 Mar 2020 21:55:55 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 67131222C3 for ; Tue, 10 Mar 2020 21:55:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="slxenHyc" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 67131222C3 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id AB67E6B0003; Tue, 10 Mar 2020 17:55:54 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A67356B0006; Tue, 10 Mar 2020 17:55:54 -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 97E086B0007; Tue, 10 Mar 2020 17:55:54 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0170.hostedemail.com [216.40.44.170]) by kanga.kvack.org (Postfix) with ESMTP id 802916B0003 for ; Tue, 10 Mar 2020 17:55:54 -0400 (EDT) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 346E59424 for ; Tue, 10 Mar 2020 21:55:54 +0000 (UTC) X-FDA: 76580810628.26.house59_337c18b0a484d X-Spam-Summary: 40,2.5,0,8fec55170c954021,d41d8cd98f00b204,rientjes@google.com,,RULES_HIT:41:355:379:800:960:966:973:988:989:1260:1277:1313:1314:1345:1437:1516:1518:1535:1544:1593:1594:1711:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:2731:3138:3139:3140:3141:3142:3152:3355:3865:3866:3867:3868:3870:3871:3874:4118:4385:4605:5007:6261:6653:7208:7903:8660:9592:10011:10128:11026:11657:11658:11914:12043:12295:12296:12297:12438:12517:12519:12550:12555:12679:12895:13095:13148:13208:13229:13230:13255:13439:13869:13870:14096:14097:14181:14659:14721:14819:21067:21080:21433:21444:21451:21627:21795:21966:21990:30005:30051:30054:30056:30070:30091,0,RBL:209.85.214.196:@google.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.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:1:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: house59_337c18b0a484d X-Filterd-Recvd-Size: 7019 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by imf01.hostedemail.com (Postfix) with ESMTP for ; Tue, 10 Mar 2020 21:55:53 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id g6so73515plt.2 for ; Tue, 10 Mar 2020 14:55:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:user-agent:mime-version; bh=NcbwCXxUWslIYcC/IMvtiJA7e1LVai6+WIY+FfCtsI4=; b=slxenHycS43HvwTp9eEVzF+wztjB8QJFeQRxOro/81jxR6ZI4o6+LapbIyCB8fRRLR KPEIY41GPcjQtfWMgJCB7ByvRSqed1vi7SuyNTZNjachFGHzpq0SQTd420m76wNe4eeQ oQrLii1+vos27HHOvdTTamlWHA4TKmXCxLw5dCEhrNKBypII22+RT46KwfQsChwkFiN9 iepDf+3sdOvSL9QvwpyZmxg0YM6umlPgMpcV6CIKdqeqTEBtYg4lGXSQtZu2zlRipmiv eb6Vv6YD+CNCqy4Pco2nmd3bcgii1HrJPO2796aUFhj9lJ8Lr8wE/oQ2TeADCDj9drFr TPPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:user-agent :mime-version; bh=NcbwCXxUWslIYcC/IMvtiJA7e1LVai6+WIY+FfCtsI4=; b=hldIPweRO/ZCFDGhJ1fsQC9U5fdYLVKXb/FRnxyaJvOIRC/v0s+MMvLqnKGYmjn8Vh ylN0GulpsMJz8yUV7QWuZehg4Kt6ps+XUfDGOa5KrVdQD9Mi1NdFjGeuysU9wGYpAIuT pJmKnu+wkQmkP8GNiO4moTQ8t5PShAQQn/3K6/APVo+YbyV461HMbojvgMgzQM5Ueye6 C6e4tW8Mb5R0B5d72/SNkEEpd9wRxG45HLGAT1VNLbbDM8Mv2nzCig4AajR3YQFCfKmp dxiR3DJ/djAPkV1rQMJRmVm5YiU42SBs0cnB/jnS1P0advhn/mrcZoyb727bL76Vyg0I fpqQ== X-Gm-Message-State: ANhLgQ1iBAj4t3BwUpJiQyWJnv3poUM/4VTiMNYAnEcDicdC5PIYXjyq qvrfsRDD5QRq60HI5myXYjww+g== X-Google-Smtp-Source: ADFU+vugU09Jze4dd0S5K5zWLDJJTy74D0sfUDWhtwZo6IZIK7WLHp/D87fNZyQUWPZ2k+5vqd3JQw== X-Received: by 2002:a17:902:a611:: with SMTP id u17mr7plq.343.1583877352222; Tue, 10 Mar 2020 14:55:52 -0700 (PDT) Received: from [2620:15c:17:3:3a5:23a7:5e32:4598] ([2620:15c:17:3:3a5:23a7:5e32:4598]) by smtp.gmail.com with ESMTPSA id u14sm30006734pgg.67.2020.03.10.14.55.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Mar 2020 14:55:51 -0700 (PDT) Date: Tue, 10 Mar 2020 14:55:50 -0700 (PDT) From: David Rientjes X-X-Sender: rientjes@chino.kir.corp.google.com To: Andrew Morton cc: Vlastimil Babka , Michal Hocko , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [patch] mm, oom: make a last minute check to prevent unnecessary memcg oom kills Message-ID: User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 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: Killing a user process as a result of hitting memcg limits is a serious decision that is unfortunately needed only when no forward progress in reclaiming memory can be made. Deciding the appropriate oom victim can take a sufficient amount of time that allows another process that is exiting to actually uncharge to the same memcg hierarchy and prevent unnecessarily killing user processes. An example is to prevent *multiple* unnecessary oom kills on a system with two cores where the oom kill occurs when there is an abundance of free memory available: Memory cgroup out of memory: Killed process 628 (repro) total-vm:41944kB, anon-rss:40888kB, file-rss:496kB, shmem-rss:0kB, UID:0 pgtables:116kB oom_score_adj:0 repro invoked oom-killer: gfp_mask=0xcc0(GFP_KERNEL), order=0, oom_score_adj=0 CPU: 1 PID: 629 Comm: repro Not tainted 5.6.0-rc5+ #130 Call Trace: dump_stack+0x78/0xb6 dump_header+0x55/0x240 oom_kill_process+0xc5/0x170 out_of_memory+0x305/0x4a0 try_charge+0x77b/0xac0 mem_cgroup_try_charge+0x10a/0x220 mem_cgroup_try_charge_delay+0x1e/0x40 handle_mm_fault+0xdf2/0x15f0 do_user_addr_fault+0x21f/0x420 async_page_fault+0x2f/0x40 memory: usage 61336kB, limit 102400kB, failcnt 74 Notice the second memcg oom kill shows usage is >40MB below its limit of 100MB but a process is still unnecessarily killed because the decision has already been made to oom kill by calling out_of_memory() before the initial victim had a chance to uncharge its memory. Make a last minute check to determine if an oom kill is really needed to prevent unnecessary oom killing. Cc: Vlastimil Babka Cc: Michal Hocko Cc: stable@vger.kernel.org Signed-off-by: David Rientjes --- include/linux/memcontrol.h | 7 +++++++ mm/memcontrol.c | 2 +- mm/oom_kill.c | 16 +++++++++++++--- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -445,6 +445,8 @@ void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *); int mem_cgroup_scan_tasks(struct mem_cgroup *, int (*)(struct task_struct *, void *), void *); +unsigned long mem_cgroup_margin(struct mem_cgroup *memcg); + static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg) { if (mem_cgroup_disabled()) @@ -945,6 +947,11 @@ static inline int mem_cgroup_scan_tasks(struct mem_cgroup *memcg, return 0; } +static inline unsigned long mem_cgroup_margin(struct mem_cgroup *memcg) +{ + return 0; +} + static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg) { return 0; diff --git a/mm/memcontrol.c b/mm/memcontrol.c --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1286,7 +1286,7 @@ void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru, * Returns the maximum amount of memory @mem can be charged with, in * pages. */ -static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg) +unsigned long mem_cgroup_margin(struct mem_cgroup *memcg) { unsigned long margin = 0; unsigned long count; diff --git a/mm/oom_kill.c b/mm/oom_kill.c --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -972,9 +972,6 @@ static void oom_kill_process(struct oom_control *oc, const char *message) } task_unlock(victim); - if (__ratelimit(&oom_rs)) - dump_header(oc, victim); - /* * Do we need to kill the entire memory cgroup? * Or even one of the ancestor memory cgroups? @@ -982,6 +979,19 @@ static void oom_kill_process(struct oom_control *oc, const char *message) */ oom_group = mem_cgroup_get_oom_group(victim, oc->memcg); + if (is_memcg_oom(oc)) { + cond_resched(); + + /* One last check: do we *really* need to kill? */ + if (mem_cgroup_margin(oc->memcg) >= (1 << oc->order)) { + put_task_struct(victim); + return; + } + } + + if (__ratelimit(&oom_rs)) + dump_header(oc, victim); + __oom_kill_process(victim, message); /*