Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752399AbdHNSdn (ORCPT ); Mon, 14 Aug 2017 14:33:43 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:44936 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751509AbdHNScy (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 2/4] mm, oom: cgroup-aware OOM killer Date: Mon, 14 Aug 2017 19:32:11 +0100 Message-ID: <20170814183213.12319-3-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: e335cb3f-fae7-4620-bf43-08d4e342d9dd 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:hF26rnpymKvO+9LMaTsTy5D8/4f5WYlc13+mYJxTsnDML7yz5pKTg3cqY08Vy0b9CU9WSsjdohb98t5tqso2IuaqDOkJH6hV98BKSeJn4AwWwWwpWokall6wIxHpGJ+kY+mFfIFMwrffLpBtnwsEuPSleqtSNyiI4WM/4XCdbXTUctfZLoOTMbrVEhmdLeUibayn/O+ujg9cucbb2j53FLCwDKkw6M2k2h+uiZnRB9U38oDBRBH/bjL/7SbTl7er;25:hg/X5AKe8aH3FYNsA5mMhGh7xUeC9JFsj5piakqNMNLH7+ROK1m3gBVBNd2h9yrCUwxrncC6cslOc1lxWaJiKVHeCE4cboW0gio56rtkL9THcM6trwbBFxAcq4csg2olGQ1F6xuvPj1gSiV/ATk13NGWZldwxJQ4ECabsvJSjYYXxO0Xvjnkd5RQukAiKhubUz/kUVczJrZAlOY02e+MQLpml0C4frNIjtkcNozLhyewxo+5Z7wkrVuz4xZNFUwWO3yOqS+BOVLaSOrZI192R5l0PYKqJUXF5OFxnwJYgbjiKUPfODxsSOtMc25fHzX4rCdjNQ14+zVPKDNpAaoGMg==;31:cy99jMI37Cr5hkhXjy6HKQ0pttrNeWpMF7OwfIFo1rLr1SLYPSopotcoufvx6Glgfzwr2ZfFnXQWq8uT1c6P1PeOTj02G7+xu8aG5roQi28gWVlr5md2T1bKgqHk99bXCy0qb2iouJFdpRC5ffmaabFyeAAFF37ZSSCWcn335GA93VudFn51xLQI3KMhsOyfcWegD04dFII8ovPwV4WwTCwgMeUTkuOO1fNkVy0kT4Y= X-MS-TrafficTypeDiagnostic: BL2PR15MB1076: X-Microsoft-Exchange-Diagnostics: 1;BL2PR15MB1076;20:plksWjU9AwIEFbeudrKXqzB0xqEj6kDffVvOLQe0uUjLOqiaGavHX75vWwA+6t/WFih5BAars66sjZ7I+OSdAbdRqI1d+t0LgcrQ/xgvFBhjUGHGPpoo/pHMRV8AWoTSsJMLj6O0RWtDyY1inEPulYfL/Mqs835JqS77lFqD1eFKEKFaj7gMJmuD7LoRALwVbj0c5zq1KaxV1IjVkP5l9WDnN9WmM0anXlDA3VIAkJkNDN5wjRrnIb3TEMNyuvn75xwEv9O30g14QJadc0bRs4CawKacxpWV7gGi8S+b6PSSBqqammOQa3e9djTb/xkRkVaUWa0w4bR2ebFlaEQzf/vxxQSRMsRF+un/ygeVTrrvpbC3vk2RYWwkmzwzKZO2nZCbz4Vim71Qdhp0w37vcja5DlWI+p2ayXKkmVTTUaewJ8f6WJbswR8lx3MtG3eWZcxvfi5SyB4Oo/cFvl1/CA29EOm8PEa8mLp8fPLUAb1Gvs3w6UlwAJ3Yrg2Wj7sE 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)(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-Microsoft-Exchange-Diagnostics: 1;BL2PR15MB1076;4:RxXM6ZfqFvmV11Z+UPPbVa6FADPyZ0iAgcrdmgIn3bgBnILH1UwNl/LCC7OwpP7C+4qewzUUnDzB+rU2Vmx8rpMVjrhcqrU185CDfrOlxsjGqR2qp+tRSwp2/PYpq8cyvOlF56/TbA82jbrgh2JwYsE4HpQ8Lh4WRoCuv0GyZzKn7x8Ndl65sVkpPxTUfIjPYslcHfuqzjnQQDFfCqePNZ/6ojJ1Cel/Rj6ioWpzTeAx58hdQmycefuafy4y9EbIGJFdOwbumGg7zRwwZjWIfDNjXpLmC0jRKSi1XwjVWDNtuYmbQ3R/MzuHVqg3BfDrTl7D7bzB8G2fedwmOtfB6IVGJsh4nappAododQ4OOOrcwBZwG9ID5dP9x7JWIM+Ne+AKIRpxrWJuApX4CcRyRVmCvKtt8vJZSP5SCebWNhpGmbTc+UcpE7TWILVcUWWE 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)(575784001)(7350300001)(25786009)(42262002)(309714004);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:/jGFPqcoVvjy8UmjEvXqPbjjrIbzPo74ESC1fsoxV?= =?us-ascii?Q?AAfRoQlK5+vIxIP3m7zTJnK+EUNUAAyjKnuxCOIj4kVQUWB7zZUeiA025MGV?= =?us-ascii?Q?ObCNgf8k0yDMbstTSbltcrkPRvmZIOD68tnEYvNpurzF2wGsH5Tg/LQRawnL?= =?us-ascii?Q?MenS7wkZkWsj9GxQQ+SnVA3y5TvObD4uEjzxt35X89oaoW/Ogbd6H4wkVFZm?= =?us-ascii?Q?YjZQMHfkUsuSUiuHxMSktFoxPEUpVTLOIH7xbawoKTPad5FTWqdFyOkIT1LY?= =?us-ascii?Q?fnPqMKpDJcTGTRL5MG6lKH9NvG1a6TbIx5WZXo6rIuWq7mbumJ+cRrhc42G4?= =?us-ascii?Q?HDsSjlQBpznh6DZoPv8+9BrQ84Ot0pLLHALBj0F0ix4fflYpFbtTvwNCM0Er?= =?us-ascii?Q?/9mDqxj3aPNiMCn6ehqpqc9RbwQVO7h7mEANIEGh5mtP5IJooR0EWZ8v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W3Bv3amzr9zekQ+T8PE= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2017 18:32:40.9710 (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: 13406 Lines: 487 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: 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 | 3 + mm/memcontrol.c | 208 +++++++++++++++++++++++++++++++++++++++++++++ mm/oom_kill.c | 62 +++++++++++++- 4 files changed, 305 insertions(+), 1 deletion(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 8556f1b86d40..796666dc3282 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_kill_all_tasks; + + /* 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_kill_all_tasks(struct mem_cgroup *memcg) +{ + return memcg->oom_kill_all_tasks; +} + #ifdef CONFIG_MEMCG_SWAP extern int do_swap_account; #endif @@ -743,6 +762,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, @@ -930,6 +953,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_kill_all_tasks(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 8a266e2be5a6..b7ec3bd441be 100644 --- a/include/linux/oom.h +++ b/include/linux/oom.h @@ -39,6 +39,7 @@ struct oom_control { unsigned long totalpages; struct task_struct *chosen; unsigned long chosen_points; + struct mem_cgroup *chosen_memcg; }; extern struct mutex oom_lock; @@ -79,6 +80,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 df6f63ee95d6..0b81dc55c6ac 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2639,6 +2639,181 @@ 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; + + 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)); + } + + points += memcg_page_state(memcg, MEMCG_KERNEL_STACK_KB) / + (PAGE_SIZE / 1024); + points += memcg_page_state(memcg, NR_SLAB_UNRECLAIMABLE); + points += memcg_page_state(memcg, MEMCG_SOCK); + points += memcg_page_state(memcg, MEMCG_SWAP); + + return points; +} + +static long oom_evaluate_memcg(struct mem_cgroup *memcg, + const nodemask_t *nodemask) +{ + struct css_task_iter it; + struct task_struct *task; + int elegible = 0; + + css_task_iter_start(&memcg->css, 0, &it); + while ((task = css_task_iter_next(&it))) { + /* + * If there are no tasks, or all tasks have oom_score_adj set + * to OOM_SCORE_ADJ_MIN and oom_kill_all_tasks is not set, + * don't select this memory cgroup. + */ + if (!elegible && + (memcg->oom_kill_all_tasks || + task->signal->oom_score_adj != OOM_SCORE_ADJ_MIN)) + elegible = 1; + + /* + * If there are previously selected OOM victims, + * abort memcg selection. + */ + if (tsk_is_oom_victim(task) && + !test_bit(MMF_OOM_SKIP, &task->signal->oom_mm->flags)) { + elegible = -1; + break; + } + } + css_task_iter_end(&it); + + return elegible > 0 ? memcg_oom_badness(memcg, nodemask) : elegible; +} + +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); + if (iter->oom_score == -1) { + oc->chosen_memcg = (void *)-1UL; + mem_cgroup_iter_break(root, iter); + return; + } + + if (!iter->oom_score) + continue; + + 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); + + 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_kill_all_tasks || !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, 0, &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 = 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 (oc->chosen_memcg == (void *)-1UL) { + /* Existing OOM victims are found. */ + 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 && 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 || !!oc->chosen_memcg; +} + /* * Reclaims as many pages from the given memcg as possible. * @@ -5190,6 +5365,33 @@ static ssize_t memory_max_write(struct kernfs_open_file *of, return nbytes; } +static int memory_oom_kill_all_tasks_show(struct seq_file *m, void *v) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); + bool oom_kill_all_tasks = memcg->oom_kill_all_tasks; + + seq_printf(m, "%d\n", oom_kill_all_tasks); + + return 0; +} + +static ssize_t memory_oom_kill_all_tasks_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_kill_all_tasks; + int err; + + err = kstrtoint(strstrip(buf), 0, &oom_kill_all_tasks); + if (err) + return err; + + memcg->oom_kill_all_tasks = !!oom_kill_all_tasks; + + return nbytes; +} + static int memory_events_show(struct seq_file *m, void *v) { struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); @@ -5310,6 +5512,12 @@ static struct cftype memory_files[] = { .write = memory_max_write, }, { + .name = "oom_kill_all_tasks", + .flags = CFTYPE_NOT_ON_ROOT, + .seq_show = memory_oom_kill_all_tasks_show, + .write = memory_oom_kill_all_tasks_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 5c29a3dd591b..28e42a0d5eee 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; @@ -823,6 +823,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); @@ -958,6 +961,60 @@ static void oom_kill_process(struct oom_control *oc, const char *message) put_task_struct(victim); } +static int oom_kill_memcg_member(struct task_struct *task, void *unused) +{ + if (!tsk_is_oom_victim(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) { + if (oc->chosen != (void *)-1UL) { + if (__ratelimit(&oom_rs)) + dump_header(oc, oc->chosen); + + __oom_kill_process(oc->chosen); + put_task_struct(oc->chosen); + schedule_timeout_killable(1); + } + return true; + + } else if (oc->chosen_memcg) { + if (oc->chosen_memcg == (void *)-1UL) + return true; + + /* Always begin with the biggest task */ + oc->chosen_points = 0; + oc->chosen = NULL; + mem_cgroup_scan_tasks(oc->chosen_memcg, oom_evaluate_task, oc); + if (oc->chosen && oc->chosen != (void *)-1UL) { + if (__ratelimit(&oom_rs)) + dump_header(oc, oc->chosen); + + __oom_kill_process(oc->chosen); + put_task_struct(oc->chosen); + + if (mem_cgroup_oom_kill_all_tasks(oc->chosen_memcg)) + mem_cgroup_scan_tasks(oc->chosen_memcg, + oom_kill_memcg_member, + NULL); + } + + mem_cgroup_put(oc->chosen_memcg); + oc->chosen_memcg = NULL; + return !!oc->chosen; + + } else { + oc->chosen_points = 0; + return false; + } +} + /* * Determines whether the kernel must panic because of the panic_on_oom sysctl. */ @@ -1059,6 +1116,9 @@ bool out_of_memory(struct oom_control *oc) 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)) { -- 2.13.5