From patchwork Fri Jul 10 15:57:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 11656931 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 5103E618 for ; Fri, 10 Jul 2020 15:58:31 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0F3ED207DF for ; Fri, 10 Jul 2020 15:58:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="N3gsW9S4" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0F3ED207DF 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 0B0918D0007; Fri, 10 Jul 2020 11:58:30 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 061B38D0001; Fri, 10 Jul 2020 11:58:30 -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 E92758D0007; Fri, 10 Jul 2020 11:58:29 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0096.hostedemail.com [216.40.44.96]) by kanga.kvack.org (Postfix) with ESMTP id D47628D0001 for ; Fri, 10 Jul 2020 11:58:29 -0400 (EDT) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 768D018023879 for ; Fri, 10 Jul 2020 15:58:29 +0000 (UTC) X-FDA: 77022623538.29.light20_30035b026ecf Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin29.hostedemail.com (Postfix) with ESMTP id 3F73A18086E3F for ; Fri, 10 Jul 2020 15:58:29 +0000 (UTC) X-Spam-Summary: 10,1,0,3e3e2cbc73cadc02,d41d8cd98f00b204,laoar.shao@gmail.com,,RULES_HIT:1:2:41:69:355:379:541:800:960:973:982:988:989:1260:1345:1437:1605:1730:1747:1777:1792:2198:2199:2393:2559:2562:2693:2895:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:3874:4051:4321:4605:5007:6117:6119:6261:6653:7208:7514:7556:7875:7903:8784:9413:10004:11026:11233:11473:11658:11914:12043:12048:12296:12297:12438:12517:12519:12555:12895:12986:13161:13229:14096:14394:14664:14687:21064:21080:21324:21433:21444:21450:21451:21611:21627:21666:21889:21990:30029:30054:30056:30070:30075,0,RBL:209.85.219.66:@gmail.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100;04yr1e9z8g7am1bddnx99foesn5kjocu76k83ddm3tomgsk5z176uji1x9wokqw.dq75p3xmc8rr5efyico9m9uwobbu7imtskj4tnd1upey7akca6rhdbb859kyobb.n-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:24,LUA_SUMMARY:none X-HE-Tag: light20_30035b026ecf X-Filterd-Recvd-Size: 11640 Received: from mail-qv1-f66.google.com (mail-qv1-f66.google.com [209.85.219.66]) by imf48.hostedemail.com (Postfix) with ESMTP for ; Fri, 10 Jul 2020 15:58:28 +0000 (UTC) Received: by mail-qv1-f66.google.com with SMTP id h18so2793882qvl.3 for ; Fri, 10 Jul 2020 08:58:28 -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=igEQSjkv/sGS5wJNt9Cad8nGfSf4xH7n0lwAXj7sRnE=; b=N3gsW9S42BPFihzTkYozk9CpPbhU809QieweC4Tun2Hf2yHqNfKsPR1othe6LzGx14 Jg3gmoDtNGosNHiH7k4JT2sH/J5cxoCMi+Dz86Dq3TQqn3VEagga/HmVutF10QQKsEXE IwgEn5/LbAQTskWDNxEt90xp4zryl99I+Fz4+Rvq5VuVlGqREzhEdNtjqY6zhxOHL8t6 xM4EguJ05vAZIN99vnOiVUjOqIBQWM7zZZ5h78/MP/TtS6oeVrHbTgf4ZXE/EUunMsZ/ dxO6MG47fMBWqmGZ22TzWeekU+ZjcPZvBajzwuAXtFmoE0k2xU4QbuHm/MZp2SfDCTKF 6Q+Q== 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=igEQSjkv/sGS5wJNt9Cad8nGfSf4xH7n0lwAXj7sRnE=; b=ALPWuYODBP/er6anUGa7hBeJqINMdmOwN7ddPB7iNV20XR6LPNET2a8fwNvVAuEWTf lBB+K7NsEkvwstxHWM2jB9wIpXVb7zw900BjXdMFqf7OtUxHZH/HrGrndc/pYasDxoH6 clxVgmSC6eCN6K8HpzfAMZh4v5yU0p7vKYIiTP2WwVRDLqezYu1+tt+BWzUdntn/uwkQ o1hfXTYU2/x4xZweMWSsauCX3TgfBuEKVYqelk42N1Pgv5z/WgIDizipptut2f0Z0fLg Bx+2xLvrVajcoqObLVH/oVQDfDxX708X9XfP8xQ4PtfT+w5wiqUHZtp+0Eh4E0+moow2 yOow== X-Gm-Message-State: AOAM532UoTiUryRBuHLzw6Td2W2U1XMsSb6vJFdALIRJb6aqHcy7bGB3 +RYGAYDg68uxOam1/YmIfUo= X-Google-Smtp-Source: ABdhPJzG2LvBmu5l1e9tQ7MdJvJUrTtm0Bo1F/8Ee6F0Lcgct3NUDdlFpGuKj2Volx8DLNdoImcf/g== X-Received: by 2002:ad4:5148:: with SMTP id g8mr28746706qvq.173.1594396708022; Fri, 10 Jul 2020 08:58:28 -0700 (PDT) Received: from dev.localdomain ([183.134.211.52]) by smtp.gmail.com with ESMTPSA id k18sm7712313qki.30.2020.07.10.08.58.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Jul 2020 08:58:27 -0700 (PDT) From: Yafang Shao To: mhocko@kernel.org, cai@lca.pw, rientjes@google.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org, sfr@canb.auug.org.au, linux-next@vger.kernel.org, linux-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH v3] mm, oom: make the calculation of oom badness more accurate Date: Fri, 10 Jul 2020 11:57:31 -0400 Message-Id: <1594396651-9931-1-git-send-email-laoar.shao@gmail.com> X-Mailer: git-send-email 1.8.3.1 X-Rspamd-Queue-Id: 3F73A18086E3F X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam04 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: Recently we found an issue on our production environment that when memcg oom is triggered the oom killer doesn't chose the process with largest resident memory but chose the first scanned process. Note that all processes in this memcg have the same oom_score_adj, so the oom killer should chose the process with largest resident memory. Bellow is part of the oom info, which is enough to analyze this issue. [7516987.983223] memory: usage 16777216kB, limit 16777216kB, failcnt 52843037 [7516987.983224] memory+swap: usage 16777216kB, limit 9007199254740988kB, failcnt 0 [7516987.983225] kmem: usage 301464kB, limit 9007199254740988kB, failcnt 0 [...] [7516987.983293] [ pid ] uid tgid total_vm rss pgtables_bytes swapents oom_score_adj name [7516987.983510] [ 5740] 0 5740 257 1 32768 0 -998 pause [7516987.983574] [58804] 0 58804 4594 771 81920 0 -998 entry_point.bas [7516987.983577] [58908] 0 58908 7089 689 98304 0 -998 cron [7516987.983580] [58910] 0 58910 16235 5576 163840 0 -998 supervisord [7516987.983590] [59620] 0 59620 18074 1395 188416 0 -998 sshd [7516987.983594] [59622] 0 59622 18680 6679 188416 0 -998 python [7516987.983598] [59624] 0 59624 1859266 5161 548864 0 -998 odin-agent [7516987.983600] [59625] 0 59625 707223 9248 983040 0 -998 filebeat [7516987.983604] [59627] 0 59627 416433 64239 774144 0 -998 odin-log-agent [7516987.983607] [59631] 0 59631 180671 15012 385024 0 -998 python3 [7516987.983612] [61396] 0 61396 791287 3189 352256 0 -998 client [7516987.983615] [61641] 0 61641 1844642 29089 946176 0 -998 client [7516987.983765] [ 9236] 0 9236 2642 467 53248 0 -998 php_scanner [7516987.983911] [42898] 0 42898 15543 838 167936 0 -998 su [7516987.983915] [42900] 1000 42900 3673 867 77824 0 -998 exec_script_vr2 [7516987.983918] [42925] 1000 42925 36475 19033 335872 0 -998 python [7516987.983921] [57146] 1000 57146 3673 848 73728 0 -998 exec_script_J2p [7516987.983925] [57195] 1000 57195 186359 22958 491520 0 -998 python2 [7516987.983928] [58376] 1000 58376 275764 14402 290816 0 -998 rosmaster [7516987.983931] [58395] 1000 58395 155166 4449 245760 0 -998 rosout [7516987.983935] [58406] 1000 58406 18285584 3967322 37101568 0 -998 data_sim [7516987.984221] oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=3aa16c9482ae3a6f6b78bda68a55d32c87c99b985e0f11331cddf05af6c4d753,mems_allowed=0-1,oom_memcg=/kubepods/podf1c273d3-9b36-11ea-b3df-246e9693c184,task_memcg=/kubepods/podf1c273d3-9b36-11ea-b3df-246e9693c184/1f246a3eeea8f70bf91141eeaf1805346a666e225f823906485ea0b6c37dfc3d,task=pause,pid=5740,uid=0 [7516987.984254] Memory cgroup out of memory: Killed process 5740 (pause) total-vm:1028kB, anon-rss:4kB, file-rss:0kB, shmem-rss:0kB [7516988.092344] oom_reaper: reaped process 5740 (pause), now anon-rss:0kB, file-rss:0kB, shmem-rss:0kB We can find that the first scanned process 5740 (pause) was killed, but its rss is only one page. That is because, when we calculate the oom badness in oom_badness(), we always ignore the negtive point and convert all of these negtive points to 1. Now as oom_score_adj of all the processes in this targeted memcg have the same value -998, the points of these processes are all negtive value. As a result, the first scanned process will be killed. The oom_socre_adj (-998) in this memcg is set by kubelet, because it is a a Guaranteed pod, which has higher priority to prevent from being killed by system oom. To fix this issue, we should make the calculation of oom point more accurate. We can achieve it by convert the chosen_point from 'unsigned long' to 'long'. [cai@lca.pw: reported a issue in the previous version] [mhocko@suse.com: fixed the issue reported by Cai] [mhocko@suse.com: add the comment in proc_oom_score()] Signed-off-by: Yafang Shao Acked-by: Michal Hocko Cc: David Rientjes Cc: Qian Cai Tested-by: Naresh Kamboju Signed-off-by: Stephen Rothwell --- v2 -> v3: - fix the type of variable 'point' in oom_evaluate_task() - initialize oom_control->chosen_points in select_bad_process() per Michal - update the comment in proc_oom_score() per Michal Signed-off-by: Yafang Shao --- fs/proc/base.c | 11 ++++++++++- include/linux/oom.h | 4 ++-- mm/oom_kill.c | 22 ++++++++++------------ 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/fs/proc/base.c b/fs/proc/base.c index d86c0af..10c2a26 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -551,8 +551,17 @@ static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns, { unsigned long totalpages = totalram_pages() + total_swap_pages; unsigned long points = 0; + long badness; + + badness = oom_badness(task, totalpages); + /* + * Special case OOM_SCORE_ADJ_MIN for all others scale the + * badness value into [0, 2000] range which we have been + * exporting for a long time so userspace might depend on it. + */ + if (badness != LONG_MIN) + points = (1000 + badness * 1000 / (long)totalpages) * 2 / 3; - points = oom_badness(task, totalpages) * 1000 / totalpages; seq_printf(m, "%lu\n", points); return 0; diff --git a/include/linux/oom.h b/include/linux/oom.h index c696c26..f022f58 100644 --- a/include/linux/oom.h +++ b/include/linux/oom.h @@ -48,7 +48,7 @@ struct oom_control { /* Used by oom implementation, do not set */ unsigned long totalpages; struct task_struct *chosen; - unsigned long chosen_points; + long chosen_points; /* Used to print the constraint info. */ enum oom_constraint constraint; @@ -107,7 +107,7 @@ static inline vm_fault_t check_stable_address_space(struct mm_struct *mm) bool __oom_reap_task_mm(struct mm_struct *mm); -extern unsigned long oom_badness(struct task_struct *p, +long oom_badness(struct task_struct *p, unsigned long totalpages); extern bool out_of_memory(struct oom_control *oc); diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 6e94962..b0ac617 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -196,17 +196,17 @@ static bool is_dump_unreclaim_slabs(void) * predictable as possible. The goal is to return the highest value for the * task consuming the most memory to avoid subsequent oom failures. */ -unsigned long oom_badness(struct task_struct *p, unsigned long totalpages) +long oom_badness(struct task_struct *p, unsigned long totalpages) { long points; long adj; if (oom_unkillable_task(p)) - return 0; + return LONG_MIN; p = find_lock_task_mm(p); if (!p) - return 0; + return LONG_MIN; /* * Do not even consider tasks which are explicitly marked oom @@ -218,7 +218,7 @@ unsigned long oom_badness(struct task_struct *p, unsigned long totalpages) test_bit(MMF_OOM_SKIP, &p->mm->flags) || in_vfork(p)) { task_unlock(p); - return 0; + return LONG_MIN; } /* @@ -233,11 +233,7 @@ unsigned long oom_badness(struct task_struct *p, unsigned long totalpages) adj *= totalpages / 1000; points += adj; - /* - * Never return 0 for an eligible task regardless of the root bonus and - * oom_score_adj (oom_score_adj can't be OOM_SCORE_ADJ_MIN here). - */ - return points > 0 ? points : 1; + return points; } static const char * const oom_constraint_text[] = { @@ -310,7 +306,7 @@ static enum oom_constraint constrained_alloc(struct oom_control *oc) static int oom_evaluate_task(struct task_struct *task, void *arg) { struct oom_control *oc = arg; - unsigned long points; + long points; if (oom_unkillable_task(task)) goto next; @@ -336,12 +332,12 @@ static int oom_evaluate_task(struct task_struct *task, void *arg) * killed first if it triggers an oom, then select it. */ if (oom_task_origin(task)) { - points = ULONG_MAX; + points = LONG_MAX; goto select; } points = oom_badness(task, oc->totalpages); - if (!points || points < oc->chosen_points) + if (points == LONG_MIN || points < oc->chosen_points) goto next; select: @@ -365,6 +361,8 @@ static int oom_evaluate_task(struct task_struct *task, void *arg) */ static void select_bad_process(struct oom_control *oc) { + oc->chosen_points = LONG_MIN; + if (is_memcg_oom(oc)) mem_cgroup_scan_tasks(oc->memcg, oom_evaluate_task, oc); else {