From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.1 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 69185C433F7 for ; Tue, 14 Jul 2020 18:46:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 09B1C22AAD for ; Tue, 14 Jul 2020 18:46:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="VNOHYZIu" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 09B1C22AAD 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 23E398D0001; Tue, 14 Jul 2020 14:46:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1C6726B0005; Tue, 14 Jul 2020 14:46:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 08FB78D0001; Tue, 14 Jul 2020 14:46:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0112.hostedemail.com [216.40.44.112]) by kanga.kvack.org (Postfix) with ESMTP id E01496B0002 for ; Tue, 14 Jul 2020 14:46:27 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 547DE181AC9CB for ; Tue, 14 Jul 2020 18:46:27 +0000 (UTC) X-FDA: 77037562014.04.sofa03_11008d826ef3 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin04.hostedemail.com (Postfix) with ESMTP id 3208C8003FA5 for ; Tue, 14 Jul 2020 18:46:27 +0000 (UTC) X-HE-Tag: sofa03_11008d826ef3 X-Filterd-Recvd-Size: 6643 Received: from mail-pl1-f194.google.com (mail-pl1-f194.google.com [209.85.214.194]) by imf08.hostedemail.com (Postfix) with ESMTP for ; Tue, 14 Jul 2020 18:46:26 +0000 (UTC) Received: by mail-pl1-f194.google.com with SMTP id k5so7369871plk.13 for ; Tue, 14 Jul 2020 11:46:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=38APsKPXShFQBPzqEJzPblTQPz+ZiMpqs38JPHohb/w=; b=VNOHYZIuxxwcNxmJtIlrnHhp9vMVgRpyNbo9PBMQQIhcikK0smUUpu00YABdB89e2K GqUrQRxCaZLdUISLuXYLELerIBlk/4f964wqG7bqx4t8RiA9kxUWMlVN2jKor8UfIFNQ KTcF4dj/YKnZDXGlySdVnNpIGDxa/Kgn3OxrVzCZKs21IpcMW7QbKI22yzH8MTX5k88b oR+DqC85QCIVCLsDMcXLDk9xQY3PhZnR8Iv7B80GH2Ri8OdGknmgkPnwEE8NGSlIe1Yx DXSifVCqzx2+yjf+s4M8y8dxvD7TKQsmrA2LUMQmi95967m94nVU+6UGu3b2xii6Si+4 OFug== 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:in-reply-to:message-id :references:user-agent:mime-version; bh=38APsKPXShFQBPzqEJzPblTQPz+ZiMpqs38JPHohb/w=; b=O3jlITUcIJgRTftFHhy5wdomdBwE7dlOUBQ/fpPGFqhz75DkBtfxcc5apHKBY5uVWx f1UenARZISDZEI3DTTFwMVSVbHvdf+2jEfX2csxGjsyTm1SKvQiAMQfsh17Hrb0MJo5s dZOLdCFuVBcJvXY07ACXA9nGpvD8F+XbCFY33l7WHyLH1KNlACNP3eqf+QHFJwy2dwQW mCLvTbWl9ZlNy2zOTYqCQZflrrIO9lbE2ZpiQRG9JcfhEMLvXnZBgTdH4J2ioTb3MiK+ TusWZPQ29kS+pBjxsEROsvXGd2baq8quNxy4NXy4Eo4JWiscfsrQiwROyM4wj6se56gi tt+Q== X-Gm-Message-State: AOAM5319gGGpQqRAANKeaRHGqXXZ2ILy7kNpatYKE2MJdEaPHNrQup7u RJ8nK5OhV/LF/wS1g3zqeXEcVg== X-Google-Smtp-Source: ABdhPJyWPUErKTsi+1qsN8EKeBQyHIWwy1+oUQKdYwSMmoK2cQbui6wDnOXiMtehdqm+xBv0CxveDA== X-Received: by 2002:a17:902:be0e:: with SMTP id r14mr4905311pls.309.1594752385415; Tue, 14 Jul 2020 11:46:25 -0700 (PDT) Received: from [2620:15c:17:3:4a0f:cfff:fe51:6667] ([2620:15c:17:3:4a0f:cfff:fe51:6667]) by smtp.gmail.com with ESMTPSA id q10sm19545340pfk.86.2020.07.14.11.46.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jul 2020 11:46:24 -0700 (PDT) Date: Tue, 14 Jul 2020 11:46:24 -0700 (PDT) From: David Rientjes X-X-Sender: rientjes@chino.kir.corp.google.com To: Yafang Shao cc: mhocko@kernel.org, penguin-kernel@i-love.sakura.ne.jp, Andrew Morton , hannes@cmpxchg.org, linux-mm@kvack.org Subject: Re: [PATCH v2] memcg, oom: check memcg margin for parallel oom In-Reply-To: <1594735034-19190-1-git-send-email-laoar.shao@gmail.com> Message-ID: References: <1594735034-19190-1-git-send-email-laoar.shao@gmail.com> User-Agent: Alpine 2.23 (DEB 453 2020-06-18) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Rspamd-Queue-Id: 3208C8003FA5 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 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: On Tue, 14 Jul 2020, Yafang Shao wrote: > 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; > } Hi Yafang, We've run with a patch very much like this for several years and it works quite successfully to prevent the unnecessary oom killing of processes. We do this in out_of_memory() directly, however, because we found that we could prevent even *more* unnecessary killing if we checked this at the "point of no return" because the selection of processes takes some additional time when we might resolve the oom condition. Some may argue that this is unnecessarily exposing mem_cgroup_margin() to generic mm code, but in the interest of preventing any unnecessary oom kill we've found it to be helpful. I proposed a variant of this in https://lkml.org/lkml/2020/3/11/1089. diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -798,6 +798,8 @@ static inline void memcg_memory_event_mm(struct mm_struct *mm, void mem_cgroup_split_huge_fixup(struct page *head); #endif +unsigned long mem_cgroup_margin(struct mem_cgroup *memcg); + #else /* CONFIG_MEMCG */ #define MEM_CGROUP_ID_SHIFT 0 @@ -825,6 +827,10 @@ static inline void memcg_memory_event_mm(struct mm_struct *mm, { } +static inline unsigned long mem_cgroup_margin(struct mem_cgroup *memcg) +{ +} + static inline unsigned long mem_cgroup_protection(struct mem_cgroup *memcg, bool in_low_reclaim) { diff --git a/mm/memcontrol.c b/mm/memcontrol.c --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1282,7 +1282,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 @@ -1109,9 +1109,23 @@ bool out_of_memory(struct oom_control *oc) if (!is_sysrq_oom(oc) && !is_memcg_oom(oc)) panic("System is deadlocked on memory\n"); } - if (oc->chosen && oc->chosen != (void *)-1UL) + if (oc->chosen && oc->chosen != (void *)-1UL) { + if (is_memcg_oom(oc)) { + /* + * If a memcg is now under its limit or current will be + * exiting and freeing memory, avoid needlessly killing + * chosen. + */ + if (mem_cgroup_margin(oc->memcg) >= (1 << oc->order) || + task_will_free_mem(current)) { + put_task_struct(oc->chosen); + return true; + } + } + oom_kill_process(oc, !is_memcg_oom(oc) ? "Out of memory" : "Memory cgroup out of memory"); + } return !!oc->chosen; }