Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753897AbdIDOWJ (ORCPT ); Mon, 4 Sep 2017 10:22:09 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:52520 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753770AbdIDOWB (ORCPT ); Mon, 4 Sep 2017 10:22:01 -0400 From: Roman Gushchin To: CC: Roman Gushchin , Michal Hocko , Vladimir Davydov , Johannes Weiner , David Rientjes , Andrew Morton , Tejun Heo , Tetsuo Handa , , , , Subject: [v7 3/5] mm, oom: introduce oom_priority for memory cgroups Date: Mon, 4 Sep 2017 15:21:06 +0100 Message-ID: <20170904142108.7165-4-guro@fb.com> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20170904142108.7165-1-guro@fb.com> References: <20170904142108.7165-1-guro@fb.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [2620:10d:c092:200::1:6ba5] X-ClientProxiedBy: HE1PR05CA0180.eurprd05.prod.outlook.com (2603:10a6:3:f8::28) To DM3PR15MB1084.namprd15.prod.outlook.com (2603:10b6:0:12::10) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: cdfbbd51-a824-40b6-096f-08d4f3a0456c X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(300000503095)(300135400095)(2017052603199)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);SRVR:DM3PR15MB1084; X-Microsoft-Exchange-Diagnostics: 1;DM3PR15MB1084;3:UyDtgnTpOv7k68cWrb3R12qm+2bKyRDNhCXCZkdmxyKGUn3iZg7bciOui0ZRZQ2NHIWe81FnNV7fdso7XcRyIomAS/LsZColg3zKncD8Stk2Ba/gg26vHQF3Vxip/Ptel5VXRnI+yDVcLQiyaFKRio8jEp/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VZne2Dd5izO6EwuLbwJTXWCoyEogoccY06EH1Taeni2UbnBZXbSSEXIXU6X8tHzEb50T7kKVu1CHXVPHseldoQhQWkq6ZwraDJYQT4Y+sUzYRONy9oDeT0Z7/UDd0ndnb0bdfUJg3gbKMxr7CTGxc8OemfI4kmQThz/oU8AP2HPx82qrwqk5cYv;4:NG8+5/DPyPVwuoTbG0w+/WsPE6sF4SxplVwHwmkA6D9kGZ9vpsqxbl3Q2Sxc+NJGZwyj/xBQyt91kqUUh9Ga/Tx+leJQxIazJa8t5W00cPZsgl8CmkeiKFl5FCpmfzHDhTUYytW2iupKgIZ/9Nk8bZgZfbrhNFMC6SMVQtjo94n6bPIZDgh3F5mtHikfWBHxOrwtpgqlJ09MSX0qpTcRYi288MEfPvHGGRvgPhonKv6QuFFTCg7PgAp7kxzVeJK+GnZjWYqEGAdG4GUnKyJ4vl9xQD05oOx1cDRhNsCurPL4QI9TvtemV5HXzaB8jz7hWq022m6kQUrxZJlTFkL3+MV3oIkaEaWeDUE7Dgn/SSdwICD5nZ68Y5nJmujoNzvRxjfWoaq0Fhdg6g54dilomQ== 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)(2401047)(8121501046)(5005006)(100000703101)(100105400095)(3002001)(93006095)(93001095)(10201501046)(920507026)(6041248)(20161123564025)(20161123555025)(20161123560025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(20161123558100)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:DM3PR15MB1084;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:DM3PR15MB1084; X-Forefront-PRVS: 0420213CCD X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(6009001)(189002)(199003)(86362001)(6666003)(53936002)(1076002)(48376002)(33646002)(50466002)(189998001)(7416002)(69596002)(97736004)(305945005)(6506006)(7736002)(6486002)(5660300001)(6916009)(2950100002)(34040400001)(101416001)(105586002)(6512007)(25786009)(5003940100001)(4326008)(76176999)(50986999)(81156014)(54906002)(36756003)(53416004)(8936002)(2361001)(81166006)(106356001)(2351001)(42186005)(8676002)(110136004)(47776003)(2906002)(39060400002)(50226002)(478600001)(68736007)(6116002)(42262002);DIR:OUT;SFP:1102;SCL:1;SRVR:DM3PR15MB1084;H:castle.thefacebook.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;DM3PR15MB1084;23:mtBbdgKdMWIE0wdCy/+j2SezfvOmN0/dkAZxBB38x?= =?us-ascii?Q?NiNgV75Takx/+S7ihiAZklYjan6KgfXNILdjE8ncWUdU8kNE8Uhd8bfW2sIP?= =?us-ascii?Q?pj19eDgw5M/LQNMxiazrj+kyYVC8LWm8C4bFy3GBr7G/rvxFBLx9/oEaf1ml?= =?us-ascii?Q?yTrK7EA1CrjcacAHUi7e8AtEPeU39ufy40hc8HH6RMLK0bWsoPTn8pktkfVv?= =?us-ascii?Q?afARENemKXN4tAxonzk3TNMHT8h9GYvRWS9wNf3lHIf+Qq91DfySgio8RwYC?= =?us-ascii?Q?cqiDA+rVzH3tpeaApQfXTt/1RpUVGU8qceO0vF9zKFd2X6ZABailPF+m+okZ?= =?us-ascii?Q?CM5CcVOtKbH5yNxmEKJ5WWIrQbe0q3/iw0KsPxKAsGpMtLlRuUt4k88jdYad?= =?us-ascii?Q?F+/SMPrqFj3y377krrO4ECZ/IV7cXsl1BPO+TZ1jZFpCxx6XcPDfdmgG/YF2?= =?us-ascii?Q?lgnjawGwo9aJoWGoJxIKJvcxEh2XaSFDlChYZRhgURKBziuswRfkE/nTTPqb?= =?us-ascii?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NznpE6KNrkCt/BYiSBrxvgQ5U1mCYy7jPjkk/w6o3+ggZLa/jpUYYnmknjzwPfkYA27s+8OweOSYbxg3j6ojBhLnUT3bVRGya+gWreVgFMIyI6j3lZ4DxEg8E1lrunpyz5WNu8qUbDAA==;5:Qmyj2WVQBaQhhLIqYAxIak8Pzie+PD8Q3SidHCpex4NTPIAFVsGMpaSQWpvnNurmfxEvb0heiZp2zU69G0pYEysnp21Ze+oYRGflo0PRFY1xzBYDlzfCKwVoLB+AG+45tEW+r+oOv0zUT4r+qVsUmw==;24:O5KxPBwsBS1pCgTH/Ch0EkFHwX8qsKUwjmetlCHl94yeWzcwZOuwKg+Jt+OORKo0fyaE/jr4hpJDO6smdhqCi3Sc056OiaaqNlvjC/Gzmz4=;7:Ow7kpJDnhcfWIVKIjn37fI4KtX/P3C0C2Aegkey0QH5PqT+Q4apUGWEhN3fCFG6F+YVZfeuRrW6Dq1SP2EbEFGAE/5jMmYNs4b0jR00CKStZ1kRe6HKzHo/j00biX+1o/ZfBMZh2LNVC//Ezb6g1vF7pQ1tnjNhrt7l3xf1GURyUe640SynkYbno3mBHoCyrHiebG1rEC3WOYtqDoCzU5p3IN3NlCd+P/4iasLO2Tyg= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;DM3PR15MB1084;20:RE31tnYHtlkUEHpIe9FWAa7IfadV0Ma47cztvw+qm3TasrtY2pPDD3TQMhLiUiYEWjnpb+Nq2hjpck4pc3+MQOK/ln6xNCF2ylO+tT0s07JTemDzw/00i/rc53WLRNtYYaftUeX+RmtMGjF5zB+cnROjWJavO4t1W/wZtxegre0= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Sep 2017 14:21:42.2356 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM3PR15MB1084 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-09-04_09:,, signatures=0 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4160 Lines: 136 Introduce a per-memory-cgroup oom_priority setting: an integer number, 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 eligible tasks. The oom_priority value is compared within sibling cgroups. If two or more sibling cgroups have the same oom_priority, the decision is based on their memory footprint. 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: Andrew Morton 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 | 49 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 5b5c2b89968e..73a0291948fd 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 */ + int oom_priority; + /* handle for "memory.events" */ struct cgroup_file events_file; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 97813c56163b..d7dd293897ca 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2757,6 +2757,7 @@ static void select_victim_memcg(struct mem_cgroup *root, struct oom_control *oc) for (;;) { struct cgroup_subsys_state *css; struct mem_cgroup *memcg = NULL; + int prio = INT_MIN; long score = LONG_MIN; css_for_each_child(css, &root->css) { @@ -2768,7 +2769,12 @@ static void select_victim_memcg(struct mem_cgroup *root, struct oom_control *oc) if (iter->oom_score == 0) continue; - if (iter->oom_score > score) { + 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; } @@ -2838,7 +2844,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_task && oc->chosen_memcg) { @@ -5480,6 +5494,31 @@ static ssize_t memory_oom_group_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; + + memcg->oom_priority = 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)); @@ -5606,6 +5645,12 @@ static struct cftype memory_files[] = { .write = memory_oom_group_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