Received: by 10.213.65.68 with SMTP id h4csp65176imn; Mon, 12 Mar 2018 17:59:19 -0700 (PDT) X-Google-Smtp-Source: AG47ELtLhPLjPkef+9taOvYYmdm7Ti2K2NVEXXo0xui9ypOgyizFLv4dLvNHPxcGbdgvL6UylARJ X-Received: by 2002:a17:902:146:: with SMTP id 64-v6mr9955150plb.30.1520902758978; Mon, 12 Mar 2018 17:59:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520902758; cv=none; d=google.com; s=arc-20160816; b=K98CtYHHDHjkEXwpdpq2VuRmyvbUPzvYG4Zlh1c40dJQj/1NCqVMIkycenJX7jUvpz lunP5JqebX88f9L/pv47UES7AOAFyajlhwRB3XyNcNhJzNizh7pjTWGUHOyrM4vV3/jA nSY/f7lURtA2jIIGSUEm4ouQjDs+30r4hBp4EfKN0y0l/Jb+XkrxNTZUYA0pJ+fi5Zna fZUn0x/gfBg7MpPQJT1//fYQx2YQTm8i+tyNr68a3T3NuZs0+LpnFt6hxwQxViFeOrgi cvof0U1Q2bl6q2xY5DQOQN8kflAcVNGq+VGdSI2j6JBn9TLLJqTTTDBdZZuaMTBp0yRf a8gA== 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=fMcKEf45zHNYHE1FscNCwPHSyvUIUeh9SKWTx5QKEgs=; b=qq5B3T6xqhD1tfobgg/rWrH+H65Zgx4uQH4Ktmuojtw3UIDYdNTU3OicpJsbHpzqtq lIY9qx/vf6Fv5xfcRh6GyOtxdpDgmP+TTFl/WAlibbDF44Fqd9cC9Vc607j2/TJRjj36 /tORVKK4zDjqF1iff/fGK2yaaBZM4a7RbLAvbjaXdvIz7IE7gSmrggzIAHdHbdtE5D4h EGMigx16TX1+MpKEGEIJdCEioWgEGxNTuzLflc9x3qBfj1koOXvEHeD9q7XlLT5ADnvI RjXHp4og97FECLr6ZGei/9JFjsWzRzZy36zBSd28UquAEaZY5QWwJOz0rdrTZQcqIyiG jcGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=DuSBitcN; 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 e11si5722836pgr.231.2018.03.12.17.59.02; Mon, 12 Mar 2018 17:59:18 -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=DuSBitcN; 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 S932443AbeCMA6C (ORCPT + 99 others); Mon, 12 Mar 2018 20:58:02 -0400 Received: from mail-pg0-f67.google.com ([74.125.83.67]:37917 "EHLO mail-pg0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932406AbeCMA57 (ORCPT ); Mon, 12 Mar 2018 20:57:59 -0400 Received: by mail-pg0-f67.google.com with SMTP id a15so2914997pgn.5 for ; Mon, 12 Mar 2018 17:57:59 -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=fMcKEf45zHNYHE1FscNCwPHSyvUIUeh9SKWTx5QKEgs=; b=DuSBitcNbezDgf8XrxXNDmIkjz26va04FrX4kI8qiVhxlUcyXWsfEDf50jpIaqVzj4 RCAUVzbj4HntWKJoUaq7j2U7BJbJme+yygVof8PZwckM9jU3xw3OubefzqRVjN183h4r 0RiCFnHCUjtpGWEmbWrYO4sgLbxJ9pnHaliZTYUolNElKulZn94ryxr1d8ULpqYy318T R6KlJEnivIHqR9ai/tAmQh3sInJG+QIru9NLj0I80+KoAgLf1JV96tWGlUzrf+j/qP4B 2+WJWhwAeqEmgQluy2srjrqTVtsenXhlD4nyve+qVHrX9YUgFB3d+C1xWMrx01aUE22a llBQ== 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=fMcKEf45zHNYHE1FscNCwPHSyvUIUeh9SKWTx5QKEgs=; b=TKH5CfVvkfYzbXe6cqUvofPKwxgPJuzB2lUw0xBSd1jmX5oFwxCP0qWflB7QFsjdfJ hYI9b+lwEiVcWwPbAnWmPznS72FwqyENQKcpaEDEvPR6+sUZEY2nIwjeGGd1fVrT6c15 0grkvhfswQAv4eBHpQj194PrbIy7A4Xw434bDP0x5h1FdS8vCsmcSgG4eWeRCeseNlVV wAHPCAuhuRIKJxJxkG2CXOknGhjij5tas78nNSwus53NQTwVMj+XIJ1J4/vfUsQ1mpJ/ wZVG03tYqU6EwJdqS+lQr2Og3SES/HXYDbQztbfOH6Gq9my5sDWC7JGOIPF+csKk6uvi BkKQ== X-Gm-Message-State: AElRT7HoK/Tn2z9AsxiqAvyYEkVZt3ykbMbwDQOBwanzPdc6NUXHM4jM zQub64e3hDzNn0YDm2LIrzrblQ== X-Received: by 10.99.130.199 with SMTP id w190mr7495249pgd.15.1520902678915; Mon, 12 Mar 2018 17:57:58 -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 z28sm15808970pgc.69.2018.03.12.17.57.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Mar 2018 17:57:58 -0700 (PDT) Date: Mon, 12 Mar 2018 17:57:57 -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 -mm v3 3/3] mm, memcg: add hierarchical usage oom policy In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (DEB 67 2015-01-07) 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 One of the three significant concerns brought up about the cgroup aware oom killer is that its decisionmaking is completely evaded by creating subcontainers and attaching processes such that the ancestor's usage does not exceed another cgroup on the system. Consider the example from the previous patch where "memory" is set in each mem cgroup's cgroup.controllers: mem cgroup cgroup.procs ========== ============ /cg1 1 process consuming 250MB /cg2 3 processes consuming 100MB each /cg3/cg31 2 processes consuming 100MB each /cg3/cg32 2 processes consuming 100MB each If memory.oom_policy is "cgroup", a process from /cg2 is chosen because it is in the single indivisible memory consumer with the greatest usage. The true usage of /cg3 is actually 400MB, but a process from /cg2 is chosen because cgroups are compared individually rather than hierarchically. If a system is divided into two users, for example: mem cgroup memory.max ========== ========== /userA 250MB /userB 250MB If /userA runs all processes attached to the local mem cgroup, whereas /userB distributes their processes over a set of subcontainers under /userB, /userA will be unfairly penalized. There is incentive with cgroup v2 to distribute processes over a set of subcontainers if those processes shall be constrained by other cgroup controllers; this is a direct result of mandating a single, unified hierarchy for cgroups. A user may also reasonably do this for mem cgroup control or statistics. And, a user may do this to evade the cgroup-aware oom killer selection logic. This patch adds an oom policy, "tree", that accounts for hierarchical usage when comparing cgroups and the cgroup aware oom killer is enabled by an ancestor. This allows administrators, for example, to require users in their own top-level mem cgroup subtree to be accounted for with hierarchical usage. In other words, they can longer evade the oom killer by using other controllers or subcontainers. If an oom policy of "tree" is in place for a subtree, such as /cg3 above, the hierarchical usage is used for comparisons with other cgroups if either "cgroup" or "tree" is the oom policy of the oom mem cgroup. Thus, if /cg3/memory.oom_policy is "tree", one of the processes from /cg3's subcontainers is chosen for oom kill. Signed-off-by: David Rientjes --- Documentation/cgroup-v2.txt | 15 +++++++++++++-- include/linux/memcontrol.h | 5 +++++ mm/memcontrol.c | 14 ++++++++++---- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Documentation/cgroup-v2.txt b/Documentation/cgroup-v2.txt --- a/Documentation/cgroup-v2.txt +++ b/Documentation/cgroup-v2.txt @@ -1080,6 +1080,10 @@ PAGE_SIZE multiple when read back. memory consumers; that is, they will compare mem cgroup usage rather than process memory footprint. See the "OOM Killer" section below. + If "tree", the OOM killer will compare mem cgroups and its subtree + as a single indivisible memory consumer. This policy cannot be set + on the root mem cgroup. See the "OOM Killer" section below. + When an OOM condition occurs, the policy is dictated by the mem cgroup that is OOM (the root mem cgroup for a system-wide OOM condition). If a descendant mem cgroup has a policy of "none", for @@ -1087,6 +1091,10 @@ PAGE_SIZE multiple when read back. the heuristic will still compare mem cgroups as indivisible memory consumers. + When an OOM condition occurs in a mem cgroup with an OOM policy of + "cgroup" or "tree", the OOM killer will compare mem cgroups with + "cgroup" policy individually with "tree" policy subtrees. + memory.events A read-only flat-keyed file which exists on non-root cgroups. The following entries are defined. Unless specified @@ -1310,6 +1318,9 @@ There are currently two available oom policies: subtree as an OOM victim and kill at least one process, depending on memory.oom_group, from it. + - "tree": choose the cgroup with the largest memory footprint considering + itself and its subtree and kill at least one process. + When selecting a cgroup as a victim, the OOM killer will kill the process with the largest memory footprint. A user can control this behavior by enabling the per-cgroup memory.oom_group option. If set, it causes the @@ -1323,8 +1334,8 @@ Please, note that memory charges are not migrating if tasks are moved between different memory cgroups. Moving tasks with significant memory footprint may affect OOM victim selection logic. If it's a case, please, consider creating a common ancestor for -the source and destination memory cgroups and enabling oom_group -on ancestor layer. +the source and destination memory cgroups and setting a policy of "tree" +and enabling oom_group on an ancestor layer. IO diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -69,6 +69,11 @@ enum memcg_oom_policy { * mem cgroup as an indivisible consumer */ MEMCG_OOM_POLICY_CGROUP, + /* + * Tree cgroup usage for all descendant memcg groups, treating each mem + * cgroup and its subtree as an indivisible consumer + */ + MEMCG_OOM_POLICY_TREE, }; struct mem_cgroup_reclaim_cookie { diff --git a/mm/memcontrol.c b/mm/memcontrol.c --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2726,7 +2726,7 @@ static void select_victim_memcg(struct mem_cgroup *root, struct oom_control *oc) /* * The oom_score is calculated for leaf memory cgroups (including * the root memcg). - * Non-leaf oom_group cgroups accumulating score of descendant + * Cgroups with oom policy of "tree" accumulate the score of descendant * leaf memory cgroups. */ rcu_read_lock(); @@ -2735,10 +2735,11 @@ static void select_victim_memcg(struct mem_cgroup *root, struct oom_control *oc) /* * We don't consider non-leaf non-oom_group memory cgroups - * as OOM victims. + * without the oom policy of "tree" as OOM victims. */ if (memcg_has_children(iter) && iter != root_mem_cgroup && - !mem_cgroup_oom_group(iter)) + !mem_cgroup_oom_group(iter) && + iter->oom_policy != MEMCG_OOM_POLICY_TREE) continue; /* @@ -2801,7 +2802,7 @@ bool mem_cgroup_select_oom_victim(struct oom_control *oc) else root = root_mem_cgroup; - if (root->oom_policy != MEMCG_OOM_POLICY_CGROUP) + if (root->oom_policy == MEMCG_OOM_POLICY_NONE) return false; select_victim_memcg(root, oc); @@ -5536,6 +5537,9 @@ static int memory_oom_policy_show(struct seq_file *m, void *v) case MEMCG_OOM_POLICY_CGROUP: seq_puts(m, "cgroup\n"); break; + case MEMCG_OOM_POLICY_TREE: + seq_puts(m, "tree\n"); + break; case MEMCG_OOM_POLICY_NONE: default: seq_puts(m, "none\n"); @@ -5554,6 +5558,8 @@ static ssize_t memory_oom_policy_write(struct kernfs_open_file *of, memcg->oom_policy = MEMCG_OOM_POLICY_NONE; else if (!memcmp("cgroup", buf, min(sizeof("cgroup")-1, nbytes))) memcg->oom_policy = MEMCG_OOM_POLICY_CGROUP; + else if (!memcmp("tree", buf, min(sizeof("tree")-1, nbytes))) + memcg->oom_policy = MEMCG_OOM_POLICY_TREE; else ret = -EINVAL;