Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752361AbdHNSdk (ORCPT ); Mon, 14 Aug 2017 14:33:40 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:59222 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751518AbdHNScy (ORCPT ); Mon, 14 Aug 2017 14:32:54 -0400 From: Roman Gushchin To: CC: Roman Gushchin , Michal Hocko , Vladimir Davydov , Johannes Weiner , Tetsuo Handa , David Rientjes , Tejun Heo , , , , Subject: [v5 3/4] mm, oom: introduce oom_priority for memory cgroups Date: Mon, 14 Aug 2017 19:32:12 +0100 Message-ID: <20170814183213.12319-4-guro@fb.com> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20170814183213.12319-1-guro@fb.com> References: <20170814183213.12319-1-guro@fb.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [2620:10d:c092:200::1:ff3d] X-ClientProxiedBy: VI1P194CA0014.EURP194.PROD.OUTLOOK.COM (2603:10a6:800:be::24) To BL2PR15MB1076.namprd15.prod.outlook.com (2603:10b6:201:17::10) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d54af46e-0644-4068-630f-08d4e342db43 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(300000503095)(300135400095)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);SRVR:BL2PR15MB1076; X-Microsoft-Exchange-Diagnostics: 1;BL2PR15MB1076;3:/hGDmF9AObIpDI10QzsamHtmbTMDXWwv+UDH0DBjLMJDtms07V3Ig956NuYvOanP31CURfDAnSHZH7rkdRCT1MR/bL/5RjkGy1nD7+y/xuFNlmoli4FrWl4zI88CUFaUs3vlCR9wyW7Mjlqs8ZkluIHW7hD5ABzYbC0bnATNeP3teIDkNG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wnP4TtgMSYvSGsUBXatp4j0vIQXKX2AjnziGZEEzzxpgMXnTTrrt1RY3nFyF0APMeuj75tiCBpyBJ7sBBFxTXHLCoLZ1aC7m3QJ9POsUDafNtsjFJcAIEL2XUoipatpKh58qtNIk3FsSt01M5x8evqsQjXXzZJ;4:mfKZpjcfNzGPgt0d2R+mdMI9QwiVwtyOCStFyaVGDxmU9Pa4b2v7VDPGx58m7cv5FlhwOYVKYraS9blE/KRKa4po2OA8DMHrUEOb4R13Ct8jeQgAZJPALzCG8/rbUzAQS3MZEnrcFgoklQMue9A4MGOyiUv6p4X4QSltmLayXAsiOmxRH5sI9IsAEVUTzUFrkkSYsg/UymiOM/8NllkBu/ii8475Ikh5/zhMzZ5rTdegYY7AICfJabpAsRLefn6+I5UCenLkpGiGN82SRXbSAVDRwxmEoZvIgLOCZa5EqP56UmnX1qVvUBGAFFASWUMwxr08G4vIOQ6tkP27cDy8MkckDWfdTv9D9CP9RDtKbzst/1fKvFnBLGKZkhrpX2nOe8AeMi/KAyM1bdYdpAIHdA== X-Exchange-Antispam-Report-Test: UriScan:(9452136761055)(67672495146484)(211936372134217)(153496737603132); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(5005006)(8121501046)(93006095)(93001095)(100000703101)(100105400095)(10201501046)(3002001)(920507026)(6041248)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123564025)(20161123560025)(20161123558100)(20161123555025)(20161123562025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:BL2PR15MB1076;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:BL2PR15MB1076; X-Forefront-PRVS: 039975700A X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(7370300001)(4630300001)(6009001)(189002)(199003)(54906002)(6506006)(2906002)(76176999)(50986999)(4326008)(42186005)(8676002)(2361001)(50226002)(33646002)(50466002)(7416002)(53416004)(105586002)(106356001)(6116002)(1076002)(5660300001)(48376002)(2351001)(81166006)(81156014)(6486002)(101416001)(68736007)(34040400001)(478600001)(6666003)(110136004)(7736002)(69596002)(53936002)(97736004)(36756003)(6916009)(5003940100001)(189998001)(86362001)(2950100002)(47776003)(6512007)(305945005)(7350300001)(25786009)(42262002);DIR:OUT;SFP:1102;SCL:1;SRVR:BL2PR15MB1076;H:castle.thefacebook.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;BL2PR15MB1076;23:BFRtuGyhl7mIc0GZKZBONwS8pT4Jz2sQZ9x+USqm/?= =?us-ascii?Q?HG4h9SkRzkQKSzKEgGEyEBhTP7jRmM06ImNb/8MTaWs3A07x+I2RoBumoa83?= =?us-ascii?Q?Q+21pABb44NRZar8ihfF/rl2ohzLBaumYCAiMkBtt8FnsfY0Jo5tGSD32vEt?= =?us-ascii?Q?V7x1htHBsuxjW1SszaNcVljBvRh1fulmcXcQTJtYFmTRRMlY/5g5pQ2kyTqy?= =?us-ascii?Q?cWPEU3KLcEqrNtit6mEbCEh0K7zfcG89SFjIHBff92XrFau7HnbAYPTcafFF?= =?us-ascii?Q?xqPRyrE9smCmhQPje2z9t+6IUFUKcrDtlYQClJqsdjf3vK7/AlUBSom0Qg5G?= =?us-ascii?Q?oGNaKuX4a6Kod+HooMv0UpKKJGe81Pe2PjrGDsQKvJbLr7sYDUrIMDsI7tAV?= =?us-ascii?Q?C3s7VgulsYw2u4m5aFr7BaMUdC5dkfQCbKqRFm9WLOLTr9S2BV2Hfh2Ef3nM?= =?us-ascii?Q?oE5L4lYztUPV7r4WcFcf1Qe14qtMbttAKTFxW831bVHhUvTbd82klQjQYf97?= =?us-ascii?Q?oZihEOHF+YjpGsH0GZalLZ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eO2EgZOkZ7VpU9yigkWYLx/+seLlXWxc+zTVrtwWh2QVNL1xWaLJzm+6VzFE7e1Idh1nUlan6kS7b4jklVLo1FOsD8C6+nw12qyNfIOgccF5LA/eEmxz0PUOJy2Or+inipfN+DxNxdaxtUqqz5W5wu9jjY/JgXFKT/x7MBel+FCPm74ZavqAA==;5:s+3TrbZCfgnESp3tqVk4YDO39GP7nxpEVhD4bu9gliTmZm+MqXF6eSvfy1ilTKYp8m2yICy7gyiGT28gZQIquR98ddYUSkNHvSD+2lYSei7pphH5VWf7dqa0tB2FEOLtCOZxqBCpo0qQemGnt2PN/vnfEzJcXg7oFtG90ndH1NI=;24:Lfd0E8dYqjFgX4oQ8B0x0nLqVVGfa1WbQ3xfjOXPmgm08PozCAIT7SntOaG0JJos5sxo6oHfhELGMtENLHCREcitLtrhLAFUg6eeQGlgZn8=;7:+Tjvc/YXodijNp0lZYwH6XxtDMBG7FSPmw5VsiaSAIZ1mChFQkQWULM8J+RFDlJlHjm2ATW6unTADWhDaHxOVYw50WXcK24un7HCEoVP9Pyvj7Dxx5PmiWJZwxPq8Z6iOlDBwaMnwBS1ZXT2ltYBF360LllJVOBO+DtPzSVW1eu1DbSN01+BX8vinqkqt3nSWTgc0/dq4LdBxjv2l0SEPCrpwLwty7QuX1D1fJ0uJxY= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;BL2PR15MB1076;20:yuh20DBUWxjTGWf6sT+LR2pQM3nw20kGf4d7uEMeclJfmUGba31RpkFhTfxbnirMZnNRJ7eXtDrVgevT3u5I0xuhdHmVnl4HjWgli9ydIPZo+iv/VFTt84s88tpbM5/vmXb+5vIW8hf7Qn8Pz2EaWWodwgOuTq8GnSaWI3Jzbw8= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2017 18:32:43.3148 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL2PR15MB1076 X-OriginatorOrg: fb.com X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-08-14_16:,, signatures=0 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4093 Lines: 136 Introduce a per-memory-cgroup oom_priority setting: an integer number within the [-10000, 10000] range, which defines the order in which the OOM killer selects victim memory cgroups. OOM killer prefers memory cgroups with larger priority if they are populated with elegible tasks. The oom_priority value is compared within sibling cgroups. The root cgroup has the oom_priority 0, which cannot be changed. Signed-off-by: Roman Gushchin Cc: Michal Hocko Cc: Vladimir Davydov Cc: Johannes Weiner Cc: David Rientjes Cc: Tejun Heo Cc: Tetsuo Handa Cc: kernel-team@fb.com Cc: cgroups@vger.kernel.org Cc: linux-doc@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org --- include/linux/memcontrol.h | 3 +++ mm/memcontrol.c | 55 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 796666dc3282..3c1ab3aedebe 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -206,6 +206,9 @@ struct mem_cgroup { /* cached OOM score */ long oom_score; + /* OOM killer priority */ + short oom_priority; + /* handle for "memory.events" */ struct cgroup_file events_file; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 0b81dc55c6ac..f61e9a9c8bdc 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2724,12 +2724,21 @@ static void select_victim_memcg(struct mem_cgroup *root, struct oom_control *oc) for (;;) { struct cgroup_subsys_state *css; struct mem_cgroup *memcg = NULL; + short prio = SHRT_MIN; long score = LONG_MIN; css_for_each_child(css, &root->css) { struct mem_cgroup *iter = mem_cgroup_from_css(css); - if (iter->oom_score > score) { + if (iter->oom_score == 0) + continue; + + if (iter->oom_priority > prio) { + memcg = iter; + prio = iter->oom_priority; + score = iter->oom_score; + } else if (iter->oom_priority == prio && + iter->oom_score > score) { memcg = iter; score = iter->oom_score; } @@ -2796,7 +2805,15 @@ bool mem_cgroup_select_oom_victim(struct oom_control *oc) * For system-wide OOMs we should consider tasks in the root cgroup * with oom_score larger than oc->chosen_points. */ - if (!oc->memcg) { + if (!oc->memcg && !(oc->chosen_memcg && + oc->chosen_memcg->oom_priority > 0)) { + /* + * Root memcg has priority 0, so if chosen memcg has lower + * priority, any task in root cgroup is preferable. + */ + if (oc->chosen_memcg && oc->chosen_memcg->oom_priority < 0) + oc->chosen_points = 0; + select_victim_root_cgroup_task(oc); if (oc->chosen && oc->chosen_memcg) { @@ -5392,6 +5409,34 @@ static ssize_t memory_oom_kill_all_tasks_write(struct kernfs_open_file *of, return nbytes; } +static int memory_oom_priority_show(struct seq_file *m, void *v) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); + + seq_printf(m, "%d\n", memcg->oom_priority); + + return 0; +} + +static ssize_t memory_oom_priority_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)); + int oom_priority; + int err; + + err = kstrtoint(strstrip(buf), 0, &oom_priority); + if (err) + return err; + + if (oom_priority < -10000 || oom_priority > 10000) + return -EINVAL; + + memcg->oom_priority = (short)oom_priority; + + return nbytes; +} + static int memory_events_show(struct seq_file *m, void *v) { struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); @@ -5518,6 +5563,12 @@ static struct cftype memory_files[] = { .write = memory_oom_kill_all_tasks_write, }, { + .name = "oom_priority", + .flags = CFTYPE_NOT_ON_ROOT, + .seq_show = memory_oom_priority_show, + .write = memory_oom_priority_write, + }, + { .name = "events", .flags = CFTYPE_NOT_ON_ROOT, .file_offset = offsetof(struct mem_cgroup, events_file), -- 2.13.5