Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp19678imm; Fri, 13 Jul 2018 16:09:02 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdCAIeRiok1XrlbWvSuX6TkFEPVsugO0eJClYFDIO5o5XYNkDcXM3FFR7Wcisw3LiPqgaHn X-Received: by 2002:a17:902:b58f:: with SMTP id a15-v6mr8191095pls.76.1531523342669; Fri, 13 Jul 2018 16:09:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531523342; cv=none; d=google.com; s=arc-20160816; b=ONIXbB46s0Ja8xFQIoUBVDlurRGcheXpPkDhglOvvFvOeJxWppJNLFh2FZbBXhjm6D h9x10aXWFfoXzRC6h51eKZiNCjnPCZhXwxUSF8ZnW2SAHoRUTIF79s21akWqOc0VfJh3 S6muDyBkJovQwTz5JkZPjqXYSNZqm6Wh5EBL5HjrHleJygeAT9nvpTc+ZxiccXVsOaHd NfOhZxRyWn1K4OgCYppWHwudeZzJFSLxS5C6Mxgwf+vjnASSiTTMKxK1uZj5IsvBCp1k +wqTCnrO7jvxYhCgjUKbF/axPrmwuik3UC25wO7fmdzXCkq2WPUN56j76L9bfctIo1Lc gFHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date:dkim-signature :arc-authentication-results; bh=drSc/yLyXYJewM0nnn0zDSduvYIMw4DeBViBIvRq4XU=; b=xI+RDgnuxx2/3Wf+DQjcpDAmqYl9DJ1ejimWwKkjg+hfsxQXadyHiel97zPxnYw/ei hTb/qIIGSJpvhKbQezDnLLyUtIZKh20Z9OQr1cfsfQo5Xz7KHNUiWx+h2p0M/0tc2Rr3 674P5LK5e6Ce2S2OKhzPiom3f9VKqmsjoRHAO+imkCySuMbb/tUmeyoB+5lJMUzs6YN8 tWeY6rZWEf20i9Z/dkKn9Dm3MgIgDrpt4f2Ot3pcoBuKPnXCI46vE06PTN7OLCBZ0pbY zBn6ZYEuDtYA3m5KQdk1difRH2HXlo+lZ/bSKuHMRd6bNCFY4W87fsFM/UkTHIfWhz5O uzqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=lpiLg3cj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x3-v6si24638493plb.478.2018.07.13.16.08.30; Fri, 13 Jul 2018 16:09:02 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=lpiLg3cj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731225AbeGMXYJ (ORCPT + 99 others); Fri, 13 Jul 2018 19:24:09 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:44471 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727506AbeGMXYJ (ORCPT ); Fri, 13 Jul 2018 19:24:09 -0400 Received: by mail-pf0-f193.google.com with SMTP id j3-v6so23401159pfh.11 for ; Fri, 13 Jul 2018 16:07: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=drSc/yLyXYJewM0nnn0zDSduvYIMw4DeBViBIvRq4XU=; b=lpiLg3cjkAB12viMdRujE7ROHpAeZZh1tuwQSJ/ZkmxhkMMa7TVDSSSJ0ln895yeiR EvX1fNvRovwbNOjBsQYlINcOsRpMlhBxFZafXciWzrWmKrojnFa80EBulKCQ0I7yNDDE wWiEzvrzlpOxLJtxtq6jIglt0L8kB+VWL/1DvomcuxKXA4MB1qTFvR3yWiXLH5EXCmH4 JHZU/vgIVB13osMH8Ay/v32TeespdFz+qnPNF4qQvRKE8/Ys2C29TtDRxFo0Wp/sosTL p99Cv8lhyitRmJ2wcuvlX3FZX9BDVF1FQDES61ygd0JsB/Z+WtF736qQnsS4Do+UjzE7 SESg== 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=drSc/yLyXYJewM0nnn0zDSduvYIMw4DeBViBIvRq4XU=; b=uYYRaBrMPxqrbKpHMx3pZW5ZbFnDWGc6WDCBxeJWPIKhQaKGQILZd+/dGx+cNmZ28S glidEvrWar1fjH470skTfMo3P4jZ1HQEpS/U7IKVPbvFEL9tcBWOfAehmiVL29FuCbRK 0M7MOGXpZHizgqmLaOwIqm2e2BnfJDXFhxNE/m1Rti946gmsnbP9q3l2m2xbksVtt8BT JIQ5Gi+RcrgS519qnJSVNASFoRFZmiXxtrdF1qViDwQ0ckUstLR4KFXA0a/SJ0xMwD7D XgE5evYfB5AtiIjx4rhOK83C/Uwf8huppLFHVyibbrbD2bjJFnt97uay4S35CUR10Jcu zMwA== X-Gm-Message-State: AOUpUlFzGmI0PCvZTJgsP65NffnATPNXkPS8XN4Oyi0MjPwy2oTdyWI4 S5qcUdcpZwCRTtnHfwG86fcZZg== X-Received: by 2002:a62:3b03:: with SMTP id i3-v6mr8935515pfa.197.1531523246245; Fri, 13 Jul 2018 16:07:26 -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 77-v6sm71871497pga.40.2018.07.13.16.07.25 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 13 Jul 2018 16:07:25 -0700 (PDT) Date: Fri, 13 Jul 2018 16:07:25 -0700 (PDT) From: David Rientjes X-X-Sender: rientjes@chino.kir.corp.google.com To: Andrew Morton , Roman Gushchin cc: Michal Hocko , Vladimir Davydov , Johannes Weiner , Tejun Heo , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [patch v3 -mm 1/6] mm, memcg: introduce per-memcg oom policy tunable In-Reply-To: Message-ID: References: User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The cgroup aware oom killer is needlessly enforced for the entire system by a mount option. It's unnecessary to force the system into a single oom policy: either cgroup aware, or the traditional process aware. This patch introduces a memory.oom_policy tunable for all mem cgroups. It is currently a no-op: it can only be set to "none", which is its default policy. It will be expanded in the next patch to define cgroup aware oom killer behavior for its subtree. This is an extensible interface that can be used to define cgroup aware assessment of mem cgroup subtrees or the traditional process aware assessment. Reading memory.oom_policy will specify the list of available policies. Another benefit of such an approach is that an admin can lock in a certain policy for the system or for a mem cgroup subtree and can delegate the policy decision to the user to determine if the kill should originate from a subcontainer, as indivisible memory consumers themselves, or selection should be done per process. Signed-off-by: David Rientjes --- Documentation/admin-guide/cgroup-v2.rst | 11 ++++++++ include/linux/memcontrol.h | 11 ++++++++ mm/memcontrol.c | 35 +++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1098,6 +1098,17 @@ PAGE_SIZE multiple when read back. If cgroup-aware OOM killer is not enabled, ENOTSUPP error is returned on attempt to access the file. + memory.oom_policy + + A read-write single string file which exists on all cgroups. The + default value is "none". + + If "none", the OOM killer will use the default policy to choose a + victim; that is, it will choose the single process with the largest + memory footprint adjusted by /proc/pid/oom_score_adj (see + Documentation/filesystems/proc.txt). This is the same policy as if + memory cgroups were not even mounted. + memory.events A read-only flat-keyed file which exists on non-root cgroups. The following entries are defined. Unless specified diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -66,6 +66,14 @@ enum mem_cgroup_protection { MEMCG_PROT_MIN, }; +enum memcg_oom_policy { + /* + * No special oom policy, process selection is determined by + * oom_badness() + */ + MEMCG_OOM_POLICY_NONE, +}; + struct mem_cgroup_reclaim_cookie { pg_data_t *pgdat; int priority; @@ -234,6 +242,9 @@ struct mem_cgroup { /* OOM-Killer disable */ int oom_kill_disable; + /* OOM policy for this subtree */ + enum memcg_oom_policy oom_policy; + /* * Treat the sub-tree as an indivisible memory consumer, * kill all belonging tasks if the memory cgroup selected diff --git a/mm/memcontrol.c b/mm/memcontrol.c --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4649,6 +4649,7 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) if (parent) { memcg->swappiness = mem_cgroup_swappiness(parent); memcg->oom_kill_disable = parent->oom_kill_disable; + memcg->oom_policy = parent->oom_policy; } if (parent && parent->use_hierarchy) { memcg->use_hierarchy = true; @@ -5810,6 +5811,34 @@ static int memory_stat_show(struct seq_file *m, void *v) return 0; } +static int memory_oom_policy_show(struct seq_file *m, void *v) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); + enum memcg_oom_policy policy = READ_ONCE(memcg->oom_policy); + + switch (policy) { + case MEMCG_OOM_POLICY_NONE: + default: + seq_puts(m, "[none]\n"); + }; + return 0; +} + +static ssize_t memory_oom_policy_write(struct kernfs_open_file *of, + char *buf, size_t nbytes, loff_t off) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); + ssize_t ret = nbytes; + + buf = strstrip(buf); + if (!memcmp("none", buf, min(sizeof("none")-1, nbytes))) + memcg->oom_policy = MEMCG_OOM_POLICY_NONE; + else + ret = -EINVAL; + + return ret; +} + static struct cftype memory_files[] = { { .name = "current", @@ -5857,6 +5886,12 @@ static struct cftype memory_files[] = { .flags = CFTYPE_NOT_ON_ROOT, .seq_show = memory_stat_show, }, + { + .name = "oom_policy", + .flags = CFTYPE_NS_DELEGATABLE, + .seq_show = memory_oom_policy_show, + .write = memory_oom_policy_write, + }, { } /* terminate */ };