Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753933AbdIDOXC (ORCPT ); Mon, 4 Sep 2017 10:23:02 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:49144 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753836AbdIDOVx (ORCPT ); Mon, 4 Sep 2017 10:21:53 -0400 From: Roman Gushchin To: CC: Roman Gushchin , Michal Hocko , Vladimir Davydov , Johannes Weiner , Tetsuo Handa , David Rientjes , Andrew Morton , Tejun Heo , , , , Subject: [v7 2/5] mm, oom: cgroup-aware OOM killer Date: Mon, 4 Sep 2017 15:21:05 +0100 Message-ID: <20170904142108.7165-3-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: 6d8880d9-2ae7-4a0d-e092-08d4f3a04361 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:2jPaCBFuGFxwakZdyvEtzefmMCOPTWA5svhdIrMrhtxXJ7ivBEj1ISkMZNh6jkT0b/UmoZam6Vt9kKVrsQcbWqWRdkOeMNfZ7/ACEhasKmoIiAecq2m99HKhJYo66jm30eORLDjpYaGNYpsJm0VoFPFh96Bjx0CH4tG/t+0HGSSEnOdPdxzH2ZeqOoLvfMhjfFMeV3do0kYJcFXk5VE0XcIJ9ibWIujd0+qYzpzH4eI+00W5ppKQQHDVwK13iPcl;25:1/azcJSadmODUtBIudOfEPoUdkH0T2wKmhUXKN6XfrvhkiyjlmS8wrD00ewfmq9gy2GzI+O+BBWwtyut5h/aN6YWCwaVzjD+qO66USrqiX8OrKJRJFIXOL+TryT03MAAf2YBtYoyJOdrZWoemVkoP5ORaZ+3am5OXoUYvXHbLrjWesPhTQy9ayYYpSz36go2ck0uWgppIs1Wpse0qQvhB6Wbuh/JGU6KGITGV7+ruph1GXJGHblwBWhIIm/n7AagOfrVsqbLLte86ZbJraXSj6D5D+rtcveKwnWa/eRhIoAtr6smahKT28hTvY+07HO0eQfDFR7BrW3k8FCco+VT9g==;31:lb2lGktPL1h3ULS+/z99T3No1shSVgr51qIHeD1q6W42qgaQ52ipneISKQT90giv2ISsY2vk1GFlksBUC6sB3qPb64AiATRlElkASVqRJNPGPZaXAMbizW4ecjcRqLGSZYck5UdjKaxVG7SXm8LEVRHd1qMra4r88DM1LzQkB75JqZqRPPY6JUuHh6tnzIecM6d5XkvNU3InzTGaDvMRdXvPdInp2ZXLknOvYzckveg= X-MS-TrafficTypeDiagnostic: DM3PR15MB1084: X-Microsoft-Exchange-Diagnostics: 1;DM3PR15MB1084;20:fYtJ8S5fInPJVWOE5briwnN1Ukp6LEBfve2npIGWPHzVLggCOtBzYsYoqQ2D7G39lareJiuve1mRpKedlhGCu6xD5wlAELyROdNcpAxhF/X9vmARYrgKr+qRYiBR87uSHWUPwHJrZZ0rWqWIMlS5DmCpdKrmQ78lDi/2a4oqfpaZsbpskwJAsGsipdBxsvMgIN7BiVbls7veTpwcUhfXvTXrF4EB2lNA2XdiMoyUUDUlft1MAhwGzZ3mTnU6dv4mrFZBV6nrLBvQQZkRwQkemHg3UQSthPTkPEqKM8QdTpCP3dA8HDIAxU+4OOXXIFZLlRX14Bay9WoA530r+wK4dnor7K5+9f0wFuKoUCu6osGmX4zHOrOpcmdg9GVhwVf5su8B9aOktQRhbahWJQEhkXhyYTEDS44K/v57oneQVUgwV2LF89Ajq+EAn86GBUzFq55vMPg2qYCh/nShpD+b9tY3jji6crFyKTD9ySWSOl1yf21uri5NUuTdTfsiPcBH X-Exchange-Antispam-Report-Test: UriScan:(9452136761055)(67672495146484)(211936372134217)(153496737603132)(17755550239193); 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-Microsoft-Exchange-Diagnostics: 1;DM3PR15MB1084;4:VkHy3FNMiVc8gc0SbsJkKtrpFs9Fozu04vkmBHzpjqWHDXZUvC5qyWBa8B5N4QtG495czo8UQT8+SFyPIO3wmUQgYiP/YUQlGNlvwzWAgaA+VQZA1bAOjTrxr4rZ2jcGaWmL6pk/v1LtjR5OzKWNbcp21RybGhp08mgevmet4hkWdD8AQZq2O5Wrc7eoWmH6BS7M128fM7W2BhBSYVphkKsx3mOJ1vvoZiRCTOH5MiyQYsD23p5LuE3XnHAi8Uv2mMV0c3nXgnB71ozrtvVlQkwPovHeCE0ffLvs7UTF2l2bQ1qyuEZBJlYf3HCKtfTTDXmAEijdLfeNfXqjRm89ILEPk8m+peQCj/325VCwTZeG/ss9FakASwWtPNYl2B5ymRliJTQoNxB0YN6VpGpoa8RMPurb/OHgy3DMpY2iTe/eAzt2gkPFpGfla07AmElx 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)(309714004);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:4XYX/GorHyRRB3XHDu8vjOhPFihgmHGCw7H/4S5e3?= =?us-ascii?Q?UH2St20ZCpqZ4KvGBDRSW8MlZsr/1LIL0Qg6wNiBZMxwPTzeQyKFJm8Fx65p?= =?us-ascii?Q?ZyntoAapdB92WQdjNCm6goQyim6D8VtR/LUpwrwpQLj0h62Oy4SdiCRP8NGA?= =?us-ascii?Q?fzPikoYzL7udRP8XNEklVhF4AAADC3QnAur4wY7+pC6WE6ty7guDk1Mge0wG?= =?us-ascii?Q?4C+IIHs8p9ziLgX9vPqbKvhxGOIl0JVs9sGDQk0fvMe4UB08UB1UxC4jQMQF?= =?us-ascii?Q?7FbNke9GXDAGUIwywEToikeFKwce0q1COCw779Wbk7DUcjOUT/d8N5HS1Hdc?= =?us-ascii?Q?epFlMZz/i5D+1Y+3O25RdyuKJWfKe/6JttIFXy73SuDNOMBOrPVQCnsXTN6c?= =?us-ascii?Q?SqXBruFNET5vRSVJmoxvT8TjYk9z0Utp6iG5kKxoJzH9LRv+pWj3raI3xGCT?= =?us-ascii?Q?mGnidIeHskzLvXX5H7j9bkIHQTq1Hu31gEF/7790MVpOkd12wANz8PP1zbrp?= =?us-ascii?Q?QgwoTaCjLVLf2x/8GLR21Bgb1BQqbqStV3ZvAVM3lKdii8TAd8BIpqMdVcAv?= =?us-ascii?Q?pdk4KN+smmavfyOSMsMYM+BrChlXQYf1o/3XOBFsEkvwyGkbVNhbBhzSQWwm?= =?us-ascii?Q?07tqlojZvKv1P92s5W+GVdRHie/h2yrJh0Nrq7Y2GC2KVgjjDOT67Qu3HbyK?= =?us-ascii?Q?cia4sDuFItrPYGaHCNtkxdFCVjAXkUNFLd1aI8Ao9WuMLsZ3ZwI1GykEliX+?= =?us-ascii?Q?f92EQkSdZ5IATGM0j/6lu4Hp6maHXpiDNHplSQ3p4O0yWOvKO5fepI5zLpLQ?= =?us-ascii?Q?ID8wagWwI4CFG6yCk45OffDXBcK8cUO2D49JwbhOxh0wlMZgOwfpM0BuwLyH?= =?us-ascii?Q?9eYvzbvYClpHybfJZwCQhgOSnHEUQatLp2x2+27RsSDXDZj/YYhbAlJcFjgs?= =?us-ascii?Q?SgwzNjJBdn3F0bNQCwlJD5vogH7Rz9JsA/PBOMs1oHGSZtmzQHWMad9JDazn?= =?us-ascii?Q?Ho2lmHSKrmBWIjBxfUPkC8NT1kQw4Giio/AD1A+xJAqZAEa1M6yTAJ85WoaN?= =?us-ascii?Q?31+HT9AkX2S43ZisLF4z6jDJTXqA5ip+qfMCCVCPsioSYmkMFGr2kWnT6mvy?= =?us-ascii?Q?y4iTgE2dcIBAm07Al48mpWMX2N+rIqIjhP46NS69nAIl40SVARKwdcZhlPry?= =?us-ascii?Q?+1lSGLVmysKtx/wSgtYLIGD1HMtoeQWXDUPj/QnqK3h7PEFpjHv/aM3+g=3D?= =?us-ascii?Q?=3D?= X-Microsoft-Exchange-Diagnostics: 1;DM3PR15MB1084;6:gHT32oENn5NsAUKrLslAfq4FizRqSnslEBGfKarppEcWLUXHvX5kWi8Q44E5uT/q5dGTUfnoago6jR28Dd/1iCoyRTgl14mr3igDQYcOtWUNUuJQA+2kj9bUQYONpK2rwHxyV+kltiCXUjEPovWt59/J3kLOnv88TF9bKrT64iO2jZEswfsPO/YV5kMVjBjIMKvICD6eLAnhfHwe3uOaE3rGOheYOW1rZw0PTrVnObbrkJ+SHrMGuWMsoQvO6tRxOXFEzU4evkmOGNykVwUfWZn5S+OhzRI1LgkUZKh5t2uTUqqKS1lEMiH7YM7SJNGuBzHIFRvW6JU2PcCxGQp4+Q==;5:FxePXhjr7clcnA2jCTsPqBLTMz9dolW3icFbq92i05r03wvR0yawDrpm+2UWRetk3qbr7vamwpwQx77uinJ2fqqTp39JeNInrPbXnfzaLYHXpcO3SWMyh7W2f0GslQWTvpHmdMMqFj7vmkzyiWtTeQ==;24:w3r6kmbH+0xOe8YMU0cbfie4naPPmW7IzN9HsFjVq3adab6G3F8AX3xaSR5HLJrhht16AJBy6DnTRJctPgpV7WbSnPBlbKgo2afHuOOC9T4=;7:4xzLNamJevS4/ZUn1oKGtMO/ITZRR7g8u0I3rxdRXYn6ljusdH3hCBDjRtPzengWYDg06/w2ryPrCnghfLlgKEr0RfVdxaV2NrPYMit7b6NzctxBMmY+QgTUS1Uu9xBn79P8xh1yGKpiLIUlmyicdFEn5tyl07bEiVnybMQHWsGRa/mAS6GqJSM6EaUpxG98XHfvFndJ/HQgWQhLvhlB/GuKQsM+dUcHhKPVFam3AtU= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;DM3PR15MB1084;20:e81ha+WJ2oeyNosFvobW1XuHtXN2sZ4ogbQ3JS3yg0kFPWYfn4UQAzsMyhLM2STFZg1y4gUr9llrz0NJNUt9iwYnJIw3mOg6oZ94J1A1xdOnW/Rx7ozStUWY/kKmzXXqEyjd8qit065CEfftsrUHfuOP829hIrwO3qyQ1HGbMVw= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Sep 2017 14:21:38.8136 (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: 16762 Lines: 608 Traditionally, the OOM killer is operating on a process level. Under oom conditions, it finds a process with the highest oom score and kills it. This behavior doesn't suit well the system with many running containers: 1) There is no fairness between containers. A small container with few large processes will be chosen over a large one with huge number of small processes. 2) Containers often do not expect that some random process inside will be killed. In many cases much safer behavior is to kill all tasks in the container. Traditionally, this was implemented in userspace, but doing it in the kernel has some advantages, especially in a case of a system-wide OOM. 3) Per-process oom_score_adj affects global OOM, so it's a breache in the isolation. To address these issues, cgroup-aware OOM killer is introduced. Under OOM conditions, it tries to find the biggest memory consumer, and free memory by killing corresponding task(s). The difference the "traditional" OOM killer is that it can treat memory cgroups as memory consumers as well as single processes. By default, it will look for the biggest leaf cgroup, and kill the largest task inside. But a user can change this behavior by enabling the per-cgroup oom_kill_all_tasks option. If set, it causes the OOM killer treat the whole cgroup as an indivisible memory consumer. In case if it's selected as on OOM victim, all belonging tasks will be killed. Tasks in the root cgroup are treated as independent memory consumers, and are compared with other memory consumers (e.g. leaf cgroups). The root cgroup doesn't support the oom_kill_all_tasks feature. Signed-off-by: Roman Gushchin Cc: Michal Hocko Cc: Vladimir Davydov Cc: Johannes Weiner Cc: Tetsuo Handa Cc: David Rientjes Cc: Andrew Morton Cc: Tejun Heo 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 | 33 +++++++ include/linux/oom.h | 12 ++- mm/memcontrol.c | 240 +++++++++++++++++++++++++++++++++++++++++++++ mm/oom_kill.c | 92 ++++++++++++++--- 4 files changed, 362 insertions(+), 15 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 69966c461d1c..5b5c2b89968e 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -35,6 +35,7 @@ struct mem_cgroup; struct page; struct mm_struct; struct kmem_cache; +struct oom_control; /* Cgroup-specific page state, on top of universal node page state */ enum memcg_stat_item { @@ -199,6 +200,12 @@ struct mem_cgroup { /* OOM-Killer disable */ int oom_kill_disable; + /* kill all tasks in the subtree in case of OOM */ + bool oom_group; + + /* cached OOM score */ + long oom_score; + /* handle for "memory.events" */ struct cgroup_file events_file; @@ -342,6 +349,11 @@ struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){ return css ? container_of(css, struct mem_cgroup, css) : NULL; } +static inline void mem_cgroup_put(struct mem_cgroup *memcg) +{ + css_put(&memcg->css); +} + #define mem_cgroup_from_counter(counter, member) \ container_of(counter, struct mem_cgroup, member) @@ -480,6 +492,13 @@ static inline bool task_in_memcg_oom(struct task_struct *p) bool mem_cgroup_oom_synchronize(bool wait); +bool mem_cgroup_select_oom_victim(struct oom_control *oc); + +static inline bool mem_cgroup_oom_group(struct mem_cgroup *memcg) +{ + return memcg->oom_group; +} + #ifdef CONFIG_MEMCG_SWAP extern int do_swap_account; #endif @@ -744,6 +763,10 @@ static inline bool task_in_mem_cgroup(struct task_struct *task, return true; } +static inline void mem_cgroup_put(struct mem_cgroup *memcg) +{ +} + static inline struct mem_cgroup * mem_cgroup_iter(struct mem_cgroup *root, struct mem_cgroup *prev, @@ -936,6 +959,16 @@ static inline void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx) { } + +static inline bool mem_cgroup_select_oom_victim(struct oom_control *oc) +{ + return false; +} + +static inline bool mem_cgroup_oom_group(struct mem_cgroup *memcg) +{ + return false; +} #endif /* CONFIG_MEMCG */ /* idx can be of type enum memcg_stat_item or node_stat_item */ diff --git a/include/linux/oom.h b/include/linux/oom.h index 76aac4ce39bc..ca78e2d5956e 100644 --- a/include/linux/oom.h +++ b/include/linux/oom.h @@ -9,6 +9,13 @@ #include /* MMF_* */ #include /* VM_FAULT* */ + +/* + * Special value returned by victim selection functions to indicate + * that are inflight OOM victims. + */ +#define INFLIGHT_VICTIM ((void *)-1UL) + struct zonelist; struct notifier_block; struct mem_cgroup; @@ -39,7 +46,8 @@ struct oom_control { /* Used by oom implementation, do not set */ unsigned long totalpages; - struct task_struct *chosen; + struct task_struct *chosen_task; + struct mem_cgroup *chosen_memcg; unsigned long chosen_points; }; @@ -101,6 +109,8 @@ extern void oom_killer_enable(void); extern struct task_struct *find_lock_task_mm(struct task_struct *p); +extern int oom_evaluate_task(struct task_struct *task, void *arg); + /* sysctls */ extern int sysctl_oom_dump_tasks; extern int sysctl_oom_kill_allocating_task; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index a69d23082abf..97813c56163b 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2649,6 +2649,213 @@ static inline bool memcg_has_children(struct mem_cgroup *memcg) return ret; } +static long memcg_oom_badness(struct mem_cgroup *memcg, + const nodemask_t *nodemask) +{ + long points = 0; + int nid; + pg_data_t *pgdat; + + for_each_node_state(nid, N_MEMORY) { + if (nodemask && !node_isset(nid, *nodemask)) + continue; + + points += mem_cgroup_node_nr_lru_pages(memcg, nid, + LRU_ALL_ANON | BIT(LRU_UNEVICTABLE)); + + pgdat = NODE_DATA(nid); + points += lruvec_page_state(mem_cgroup_lruvec(pgdat, memcg), + NR_SLAB_UNRECLAIMABLE); + } + + points += memcg_page_state(memcg, MEMCG_KERNEL_STACK_KB) / + (PAGE_SIZE / 1024); + points += memcg_page_state(memcg, MEMCG_SOCK); + points += memcg_page_state(memcg, MEMCG_SWAP); + + return points; +} + +/* + * Checks if the given memcg is a valid OOM victim and returns a number, + * which means the folowing: + * -1: there are inflight OOM victim tasks, belonging to the memcg + * 0: memcg is not eligible, e.g. all belonging tasks are protected + * by oom_score_adj set to OOM_SCORE_ADJ_MIN + * >0: memcg is eligible, and the returned value is an estimation + * of the memory footprint + */ +static long oom_evaluate_memcg(struct mem_cgroup *memcg, + const nodemask_t *nodemask) +{ + struct css_task_iter it; + struct task_struct *task; + int eligible = 0; + + /* + * Memcg is OOM eligible if there are OOM killable tasks inside. + * + * We treat tasks with oom_score_adj set to OOM_SCORE_ADJ_MIN + * as unkillable. + * + * If there are inflight OOM victim tasks inside the memcg, + * we return -1. + */ + css_task_iter_start(&memcg->css, &it); + while ((task = css_task_iter_next(&it))) { + if (!eligible && + task->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) + eligible = 1; + + if (tsk_is_oom_victim(task) && + !test_bit(MMF_OOM_SKIP, &task->signal->oom_mm->flags)) { + eligible = -1; + break; + } + } + css_task_iter_end(&it); + + if (eligible <= 0) + return eligible; + + return memcg_oom_badness(memcg, nodemask); +} + +static void select_victim_memcg(struct mem_cgroup *root, struct oom_control *oc) +{ + struct mem_cgroup *iter, *parent; + + for_each_mem_cgroup_tree(iter, root) { + if (memcg_has_children(iter)) { + iter->oom_score = 0; + continue; + } + + iter->oom_score = oom_evaluate_memcg(iter, oc->nodemask); + + /* + * Ignore empty and non-eligible memory cgroups. + */ + if (iter->oom_score == 0) + continue; + + /* + * If there are inflight OOM victims, we don't need to look + * further for new victims. + */ + if (iter->oom_score == -1) { + oc->chosen_memcg = INFLIGHT_VICTIM; + mem_cgroup_iter_break(root, iter); + return; + } + + for (parent = parent_mem_cgroup(iter); parent && parent != root; + parent = parent_mem_cgroup(parent)) + parent->oom_score += iter->oom_score; + } + + for (;;) { + struct cgroup_subsys_state *css; + struct mem_cgroup *memcg = NULL; + long score = LONG_MIN; + + css_for_each_child(css, &root->css) { + struct mem_cgroup *iter = mem_cgroup_from_css(css); + + /* + * Ignore empty and non-eligible memory cgroups. + */ + if (iter->oom_score == 0) + continue; + + if (iter->oom_score > score) { + memcg = iter; + score = iter->oom_score; + } + } + + if (!memcg) { + if (oc->memcg && root == oc->memcg) { + oc->chosen_memcg = oc->memcg; + css_get(&oc->chosen_memcg->css); + oc->chosen_points = oc->memcg->oom_score; + } + break; + } + + if (memcg->oom_group || !memcg_has_children(memcg)) { + oc->chosen_memcg = memcg; + css_get(&oc->chosen_memcg->css); + oc->chosen_points = score; + break; + } + + root = memcg; + } +} + +static void select_victim_root_cgroup_task(struct oom_control *oc) +{ + struct css_task_iter it; + struct task_struct *task; + int ret = 0; + + css_task_iter_start(&root_mem_cgroup->css, &it); + while (!ret && (task = css_task_iter_next(&it))) + ret = oom_evaluate_task(task, oc); + css_task_iter_end(&it); +} + +bool mem_cgroup_select_oom_victim(struct oom_control *oc) +{ + struct mem_cgroup *root = root_mem_cgroup; + + oc->chosen_task = NULL; + oc->chosen_memcg = NULL; + + if (mem_cgroup_disabled()) + return false; + + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) + return false; + + if (oc->memcg) + root = oc->memcg; + + rcu_read_lock(); + + select_victim_memcg(root, oc); + + /* + * If existing OOM victims are found, no need to look further. + */ + if (oc->chosen_memcg == INFLIGHT_VICTIM) { + rcu_read_unlock(); + return true; + } + + /* + * For system-wide OOMs we should consider tasks in the root cgroup + * with oom_score larger than oc->chosen_points. + */ + if (!oc->memcg) { + select_victim_root_cgroup_task(oc); + + if (oc->chosen_task && oc->chosen_memcg) { + /* + * If we've decided to kill a task in the root memcg, + * release chosen_memcg. + */ + css_put(&oc->chosen_memcg->css); + oc->chosen_memcg = NULL; + } + } + + rcu_read_unlock(); + + return oc->chosen_task || oc->chosen_memcg; +} + /* * Reclaims as many pages from the given memcg as possible. * @@ -5246,6 +5453,33 @@ static ssize_t memory_max_write(struct kernfs_open_file *of, return nbytes; } +static int memory_oom_group_show(struct seq_file *m, void *v) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); + bool oom_group = memcg->oom_group; + + seq_printf(m, "%d\n", oom_group); + + return 0; +} + +static ssize_t memory_oom_group_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_group; + int err; + + err = kstrtoint(strstrip(buf), 0, &oom_group); + if (err) + return err; + + memcg->oom_group = oom_group; + + return nbytes; +} + static int memory_events_show(struct seq_file *m, void *v) { struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); @@ -5366,6 +5600,12 @@ static struct cftype memory_files[] = { .write = memory_max_write, }, { + .name = "oom_group", + .flags = CFTYPE_NOT_ON_ROOT, + .seq_show = memory_oom_group_show, + .write = memory_oom_group_write, + }, + { .name = "events", .flags = CFTYPE_NOT_ON_ROOT, .file_offset = offsetof(struct mem_cgroup, events_file), diff --git a/mm/oom_kill.c b/mm/oom_kill.c index f061b627092c..b90a41ec16a1 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -288,7 +288,7 @@ static enum oom_constraint constrained_alloc(struct oom_control *oc) return CONSTRAINT_NONE; } -static int oom_evaluate_task(struct task_struct *task, void *arg) +int oom_evaluate_task(struct task_struct *task, void *arg) { struct oom_control *oc = arg; unsigned long points; @@ -322,26 +322,26 @@ static int oom_evaluate_task(struct task_struct *task, void *arg) goto next; /* Prefer thread group leaders for display purposes */ - if (points == oc->chosen_points && thread_group_leader(oc->chosen)) + if (points == oc->chosen_points && thread_group_leader(oc->chosen_task)) goto next; select: - if (oc->chosen) - put_task_struct(oc->chosen); + if (oc->chosen_task) + put_task_struct(oc->chosen_task); get_task_struct(task); - oc->chosen = task; + oc->chosen_task = task; oc->chosen_points = points; next: return 0; abort: - if (oc->chosen) - put_task_struct(oc->chosen); - oc->chosen = (void *)-1UL; + if (oc->chosen_task) + put_task_struct(oc->chosen_task); + oc->chosen_task = INFLIGHT_VICTIM; return 1; } /* * Simple selection loop. We choose the process with the highest number of - * 'points'. In case scan was aborted, oc->chosen is set to -1. + * 'points'. In case scan was aborted, oc->chosen_task is set to -1. */ static void select_bad_process(struct oom_control *oc) { @@ -810,6 +810,9 @@ static void __oom_kill_process(struct task_struct *victim) struct mm_struct *mm; bool can_oom_reap = true; + if (is_global_init(victim) || (victim->flags & PF_KTHREAD)) + return; + p = find_lock_task_mm(victim); if (!p) { put_task_struct(victim); @@ -885,7 +888,7 @@ static void __oom_kill_process(struct task_struct *victim) static void oom_kill_process(struct oom_control *oc, const char *message) { - struct task_struct *p = oc->chosen; + struct task_struct *p = oc->chosen_task; unsigned int points = oc->chosen_points; struct task_struct *victim = p; struct task_struct *child; @@ -946,6 +949,64 @@ static void oom_kill_process(struct oom_control *oc, const char *message) __oom_kill_process(victim); } +static int oom_kill_memcg_member(struct task_struct *task, void *unused) +{ + if (!tsk_is_oom_victim(task)) { + get_task_struct(task); + __oom_kill_process(task); + } + return 0; +} + +static bool oom_kill_memcg_victim(struct oom_control *oc) +{ + static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL, + DEFAULT_RATELIMIT_BURST); + + if (oc->chosen_task) { + if (oc->chosen_task == INFLIGHT_VICTIM) + return true; + + if (__ratelimit(&oom_rs)) + dump_header(oc, oc->chosen_task); + + __oom_kill_process(oc->chosen_task); + + schedule_timeout_killable(1); + return true; + + } else if (oc->chosen_memcg) { + if (oc->chosen_memcg == INFLIGHT_VICTIM) + return true; + + /* Always begin with the biggest task */ + oc->chosen_points = 0; + oc->chosen_task = NULL; + mem_cgroup_scan_tasks(oc->chosen_memcg, oom_evaluate_task, oc); + + if (oc->chosen_task && oc->chosen_task != INFLIGHT_VICTIM) { + if (__ratelimit(&oom_rs)) + dump_header(oc, oc->chosen_task); + + __oom_kill_process(oc->chosen_task); + + if (mem_cgroup_oom_group(oc->chosen_memcg)) + mem_cgroup_scan_tasks(oc->chosen_memcg, + oom_kill_memcg_member, + NULL); + schedule_timeout_killable(1); + } + + mem_cgroup_put(oc->chosen_memcg); + oc->chosen_memcg = NULL; + return oc->chosen_task; + + } else { + oc->chosen_points = 0; + return false; + } +} + /* * Determines whether the kernel must panic because of the panic_on_oom sysctl. */ @@ -1042,18 +1103,21 @@ bool out_of_memory(struct oom_control *oc) current->mm && !oom_unkillable_task(current, NULL, oc->nodemask) && current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) { get_task_struct(current); - oc->chosen = current; + oc->chosen_task = current; oom_kill_process(oc, "Out of memory (oom_kill_allocating_task)"); return true; } + if (mem_cgroup_select_oom_victim(oc) && oom_kill_memcg_victim(oc)) + return true; + select_bad_process(oc); /* Found nothing?!?! Either we hang forever, or we panic. */ - if (!oc->chosen && !is_sysrq_oom(oc) && !is_memcg_oom(oc)) { + if (!oc->chosen_task && !is_sysrq_oom(oc) && !is_memcg_oom(oc)) { dump_header(oc, NULL); panic("Out of memory and no killable processes...\n"); } - if (oc->chosen && oc->chosen != (void *)-1UL) { + if (oc->chosen_task && oc->chosen_task != INFLIGHT_VICTIM) { oom_kill_process(oc, !is_memcg_oom(oc) ? "Out of memory" : "Memory cgroup out of memory"); /* @@ -1062,7 +1126,7 @@ bool out_of_memory(struct oom_control *oc) */ schedule_timeout_killable(1); } - return !!oc->chosen; + return !!oc->chosen_task; } /* -- 2.13.5