blob: eda2e2a0bdc6260a936976523d6b4204d4f2a3e5 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/mm/oom_kill.c
4 *
5 * Copyright (C) 1998,2000 Rik van Riel
6 * Thanks go out to Claus Fischer for some serious inspiration and
7 * for goading me into coding this file...
David Rientjesa63d83f2010-08-09 17:19:46 -07008 * Copyright (C) 2010 Google, Inc.
9 * Rewritten by David Rientjes
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * The routines in this file are used to kill a process when
Paul Jacksona49335c2005-09-06 15:18:09 -070012 * we're seriously out of memory. This gets called from __alloc_pages()
13 * in mm/page_alloc.c when we really run out of memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * Since we won't call these routines often (on a well-configured
16 * machine) this file will double as a 'coding guide' and a signpost
17 * for newbie kernel hackers. It features several pointers to major
18 * kernel subsystems and hints as to where to find out what things do.
19 */
20
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070021#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040023#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010026#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010027#include <linux/sched/coredump.h>
Ingo Molnar29930022017-02-08 18:51:36 +010028#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/swap.h>
30#include <linux/timex.h>
31#include <linux/jiffies.h>
Paul Jacksonef08e3b2005-09-06 15:18:13 -070032#include <linux/cpuset.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040033#include <linux/export.h>
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070034#include <linux/notifier.h>
Pavel Emelianovc7ba5c92008-02-07 00:13:58 -080035#include <linux/memcontrol.h>
David Rientjes6f48d0eb2010-08-09 17:18:52 -070036#include <linux/mempolicy.h>
David Howells5cd9c582008-08-14 11:37:28 +010037#include <linux/security.h>
David Rientjesedd45542011-03-22 16:30:12 -070038#include <linux/ptrace.h>
David Rientjesf660daa2011-10-31 17:07:07 -070039#include <linux/freezer.h>
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080040#include <linux/ftrace.h>
David Rientjesdc3f21e2012-03-21 16:33:47 -070041#include <linux/ratelimit.h>
Michal Hockoaac45362016-03-25 14:20:24 -070042#include <linux/kthread.h>
43#include <linux/init.h>
Michal Hocko4d4bbd82017-10-03 16:14:50 -070044#include <linux/mmu_notifier.h>
Michal Hockoaac45362016-03-25 14:20:24 -070045
46#include <asm/tlb.h>
47#include "internal.h"
Yang Shi852d8be2017-11-15 17:32:07 -080048#include "slab.h"
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080049
50#define CREATE_TRACE_POINTS
51#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
KAMEZAWA Hiroyukifadd8fb2006-06-23 02:03:13 -070053int sysctl_panic_on_oom;
David Rientjesfe071d72007-10-16 23:25:56 -070054int sysctl_oom_kill_allocating_task;
David Rientjesad915c42010-08-09 17:18:53 -070055int sysctl_oom_dump_tasks = 1;
Johannes Weinerdc564012015-06-24 16:57:19 -070056
Michal Hockoa195d3f2018-08-17 15:49:10 -070057/*
58 * Serializes oom killer invocations (out_of_memory()) from all contexts to
59 * prevent from over eager oom killing (e.g. when the oom killer is invoked
60 * from different domains).
61 *
62 * oom_killer_disable() relies on this lock to stabilize oom_killer_disabled
63 * and mark_oom_victim
64 */
Johannes Weinerdc564012015-06-24 16:57:19 -070065DEFINE_MUTEX(oom_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Shakeel Buttac311a12019-07-11 21:00:31 -070067static inline bool is_memcg_oom(struct oom_control *oc)
68{
69 return oc->memcg != NULL;
70}
71
David Rientjes6f48d0eb2010-08-09 17:18:52 -070072#ifdef CONFIG_NUMA
73/**
Shakeel Buttac311a12019-07-11 21:00:31 -070074 * oom_cpuset_eligible() - check task eligiblity for kill
Oleg Nesterovad962442014-01-21 15:50:00 -080075 * @start: task struct of which task to consider
David Rientjes6f48d0eb2010-08-09 17:18:52 -070076 * @mask: nodemask passed to page allocator for mempolicy ooms
77 *
78 * Task eligibility is determined by whether or not a candidate task, @tsk,
79 * shares the same mempolicy nodes as current if it is bound by such a policy
80 * and whether or not it has the same set of allowed cpuset nodes.
Shakeel Buttac311a12019-07-11 21:00:31 -070081 *
82 * This function is assuming oom-killer context and 'current' has triggered
83 * the oom-killer.
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070084 */
Shakeel Buttac311a12019-07-11 21:00:31 -070085static bool oom_cpuset_eligible(struct task_struct *start,
86 struct oom_control *oc)
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070087{
Oleg Nesterovad962442014-01-21 15:50:00 -080088 struct task_struct *tsk;
89 bool ret = false;
Shakeel Buttac311a12019-07-11 21:00:31 -070090 const nodemask_t *mask = oc->nodemask;
91
92 if (is_memcg_oom(oc))
93 return true;
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070094
Oleg Nesterovad962442014-01-21 15:50:00 -080095 rcu_read_lock();
Oleg Nesterov1da4db02014-01-21 15:49:58 -080096 for_each_thread(start, tsk) {
David Rientjes6f48d0eb2010-08-09 17:18:52 -070097 if (mask) {
98 /*
99 * If this is a mempolicy constrained oom, tsk's
100 * cpuset is irrelevant. Only return true if its
101 * mempolicy intersects current, otherwise it may be
102 * needlessly killed.
103 */
Oleg Nesterovad962442014-01-21 15:50:00 -0800104 ret = mempolicy_nodemask_intersects(tsk, mask);
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700105 } else {
106 /*
107 * This is not a mempolicy constrained oom, so only
108 * check the mems of tsk's cpuset.
109 */
Oleg Nesterovad962442014-01-21 15:50:00 -0800110 ret = cpuset_mems_allowed_intersects(current, tsk);
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700111 }
Oleg Nesterovad962442014-01-21 15:50:00 -0800112 if (ret)
113 break;
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800114 }
Oleg Nesterovad962442014-01-21 15:50:00 -0800115 rcu_read_unlock();
KOSAKI Motohirodf1090a2010-08-09 17:19:39 -0700116
Oleg Nesterovad962442014-01-21 15:50:00 -0800117 return ret;
KOSAKI Motohiro495789a2009-09-21 17:03:14 -0700118}
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700119#else
Shakeel Buttac311a12019-07-11 21:00:31 -0700120static bool oom_cpuset_eligible(struct task_struct *tsk, struct oom_control *oc)
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700121{
122 return true;
123}
124#endif /* CONFIG_NUMA */
KOSAKI Motohiro495789a2009-09-21 17:03:14 -0700125
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700126/*
127 * The process p may have detached its own ->mm while exiting or through
128 * use_mm(), but one or more of its subthreads may still have a valid
129 * pointer. Return p, or any of its subthreads with a valid ->mm, with
130 * task_lock() held.
131 */
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -0700132struct task_struct *find_lock_task_mm(struct task_struct *p)
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700133{
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800134 struct task_struct *t;
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700135
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800136 rcu_read_lock();
137
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800138 for_each_thread(p, t) {
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700139 task_lock(t);
140 if (likely(t->mm))
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800141 goto found;
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700142 task_unlock(t);
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800143 }
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800144 t = NULL;
145found:
146 rcu_read_unlock();
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700147
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800148 return t;
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700149}
150
Yaowei Baidb2a0dd2015-11-06 16:28:06 -0800151/*
152 * order == -1 means the oom kill is required by sysrq, otherwise only
153 * for display purposes.
154 */
155static inline bool is_sysrq_oom(struct oom_control *oc)
156{
157 return oc->order == -1;
158}
159
KOSAKI Motohiroab290ad2010-08-09 17:19:35 -0700160/* return true if the task is not adequate as candidate victim task. */
Shakeel Buttac311a12019-07-11 21:00:31 -0700161static bool oom_unkillable_task(struct task_struct *p)
KOSAKI Motohiroab290ad2010-08-09 17:19:35 -0700162{
163 if (is_global_init(p))
164 return true;
165 if (p->flags & PF_KTHREAD)
166 return true;
KOSAKI Motohiroab290ad2010-08-09 17:19:35 -0700167 return false;
168}
169
Yang Shi852d8be2017-11-15 17:32:07 -0800170/*
171 * Print out unreclaimble slabs info when unreclaimable slabs amount is greater
172 * than all user memory (LRU pages)
173 */
174static bool is_dump_unreclaim_slabs(void)
175{
176 unsigned long nr_lru;
177
178 nr_lru = global_node_page_state(NR_ACTIVE_ANON) +
179 global_node_page_state(NR_INACTIVE_ANON) +
180 global_node_page_state(NR_ACTIVE_FILE) +
181 global_node_page_state(NR_INACTIVE_FILE) +
182 global_node_page_state(NR_ISOLATED_ANON) +
183 global_node_page_state(NR_ISOLATED_FILE) +
184 global_node_page_state(NR_UNEVICTABLE);
185
186 return (global_node_page_state(NR_SLAB_UNRECLAIMABLE) > nr_lru);
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/**
David Rientjesa63d83f2010-08-09 17:19:46 -0700190 * oom_badness - heuristic function to determine which candidate task to kill
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * @p: task struct of which task we should calculate
David Rientjesa63d83f2010-08-09 17:19:46 -0700192 * @totalpages: total present RAM allowed for page allocation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *
David Rientjesa63d83f2010-08-09 17:19:46 -0700194 * The heuristic for determining which task to kill is made to be as simple and
195 * predictable as possible. The goal is to return the highest value for the
196 * task consuming the most memory to avoid subsequent oom failures.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
Shakeel Buttac311a12019-07-11 21:00:31 -0700198unsigned long oom_badness(struct task_struct *p, unsigned long totalpages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
David Rientjes1e11ad82012-06-08 13:21:26 -0700200 long points;
David Rientjes61eafb02012-06-20 12:52:58 -0700201 long adj;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700202
Shakeel Buttac311a12019-07-11 21:00:31 -0700203 if (oom_unkillable_task(p))
KOSAKI Motohiro26ebc982010-08-09 17:19:37 -0700204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700206 p = find_lock_task_mm(p);
207 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return 0;
209
Michal Hockobb8a4b72016-05-20 16:57:18 -0700210 /*
211 * Do not even consider tasks which are explicitly marked oom
Michal Hockob18dc5f2016-07-28 15:44:46 -0700212 * unkillable or have been already oom reaped or the are in
213 * the middle of vfork
Michal Hockobb8a4b72016-05-20 16:57:18 -0700214 */
David Rientjesa9c58b902012-12-11 16:02:54 -0800215 adj = (long)p->signal->oom_score_adj;
Michal Hockobb8a4b72016-05-20 16:57:18 -0700216 if (adj == OOM_SCORE_ADJ_MIN ||
Michal Hocko862e3072016-10-07 16:58:57 -0700217 test_bit(MMF_OOM_SKIP, &p->mm->flags) ||
Michal Hockob18dc5f2016-07-28 15:44:46 -0700218 in_vfork(p)) {
Michal Hocko5aecc852011-11-15 14:36:07 -0800219 task_unlock(p);
220 return 0;
221 }
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /*
David Rientjesa63d83f2010-08-09 17:19:46 -0700224 * The baseline for the badness score is the proportion of RAM that each
KOSAKI Motohirof755a042011-04-27 15:26:50 -0700225 * task's rss, pagetable and swap space use.
David Rientjesa63d83f2010-08-09 17:19:46 -0700226 */
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800227 points = get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) +
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -0800228 mm_pgtables_bytes(p->mm) / PAGE_SIZE;
Andrew Morton97c2c9b82006-04-18 22:20:38 -0700229 task_unlock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
David Rientjes61eafb02012-06-20 12:52:58 -0700231 /* Normalize to oom_score_adj units */
232 adj *= totalpages / 1000;
233 points += adj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
David Rientjesf19e8aa2010-09-22 13:04:52 -0700235 /*
David Rientjesa7f638f2012-05-29 15:06:47 -0700236 * Never return 0 for an eligible task regardless of the root bonus and
237 * oom_score_adj (oom_score_adj can't be OOM_SCORE_ADJ_MIN here).
David Rientjesf19e8aa2010-09-22 13:04:52 -0700238 */
David Rientjes1e11ad82012-06-08 13:21:26 -0700239 return points > 0 ? points : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
yuzhoujianef8444e2018-12-28 00:36:07 -0800242static const char * const oom_constraint_text[] = {
243 [CONSTRAINT_NONE] = "CONSTRAINT_NONE",
244 [CONSTRAINT_CPUSET] = "CONSTRAINT_CPUSET",
245 [CONSTRAINT_MEMORY_POLICY] = "CONSTRAINT_MEMORY_POLICY",
246 [CONSTRAINT_MEMCG] = "CONSTRAINT_MEMCG",
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700247};
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/*
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800250 * Determine the type of allocation constraint.
251 */
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700252static enum oom_constraint constrained_alloc(struct oom_control *oc)
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800253{
Mel Gorman54a6eb52008-04-28 02:12:16 -0700254 struct zone *zone;
Mel Gormandd1a2392008-04-28 02:12:17 -0700255 struct zoneref *z;
David Rientjes6e0fc462015-09-08 15:00:36 -0700256 enum zone_type high_zoneidx = gfp_zone(oc->gfp_mask);
David Rientjesa63d83f2010-08-09 17:19:46 -0700257 bool cpuset_limited = false;
258 int nid;
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800259
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700260 if (is_memcg_oom(oc)) {
Roman Gushchinbbec2e12018-06-07 17:06:18 -0700261 oc->totalpages = mem_cgroup_get_max(oc->memcg) ?: 1;
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700262 return CONSTRAINT_MEMCG;
263 }
264
David Rientjesa63d83f2010-08-09 17:19:46 -0700265 /* Default to all available memory */
Arun KSca79b0c2018-12-28 00:34:29 -0800266 oc->totalpages = totalram_pages() + total_swap_pages;
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700267
268 if (!IS_ENABLED(CONFIG_NUMA))
269 return CONSTRAINT_NONE;
David Rientjesa63d83f2010-08-09 17:19:46 -0700270
David Rientjes6e0fc462015-09-08 15:00:36 -0700271 if (!oc->zonelist)
David Rientjesa63d83f2010-08-09 17:19:46 -0700272 return CONSTRAINT_NONE;
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800273 /*
274 * Reach here only when __GFP_NOFAIL is used. So, we should avoid
275 * to kill current.We have to random task kill in this case.
276 * Hopefully, CONSTRAINT_THISNODE...but no way to handle it, now.
277 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700278 if (oc->gfp_mask & __GFP_THISNODE)
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800279 return CONSTRAINT_NONE;
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800280
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800281 /*
David Rientjesa63d83f2010-08-09 17:19:46 -0700282 * This is not a __GFP_THISNODE allocation, so a truncated nodemask in
283 * the page allocator means a mempolicy is in effect. Cpuset policy
284 * is enforced in get_page_from_freelist().
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800285 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700286 if (oc->nodemask &&
287 !nodes_subset(node_states[N_MEMORY], *oc->nodemask)) {
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700288 oc->totalpages = total_swap_pages;
David Rientjes6e0fc462015-09-08 15:00:36 -0700289 for_each_node_mask(nid, *oc->nodemask)
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700290 oc->totalpages += node_spanned_pages(nid);
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800291 return CONSTRAINT_MEMORY_POLICY;
David Rientjesa63d83f2010-08-09 17:19:46 -0700292 }
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800293
294 /* Check this allocation failure is caused by cpuset's wall function */
David Rientjes6e0fc462015-09-08 15:00:36 -0700295 for_each_zone_zonelist_nodemask(zone, z, oc->zonelist,
296 high_zoneidx, oc->nodemask)
297 if (!cpuset_zone_allowed(zone, oc->gfp_mask))
David Rientjesa63d83f2010-08-09 17:19:46 -0700298 cpuset_limited = true;
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800299
David Rientjesa63d83f2010-08-09 17:19:46 -0700300 if (cpuset_limited) {
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700301 oc->totalpages = total_swap_pages;
David Rientjesa63d83f2010-08-09 17:19:46 -0700302 for_each_node_mask(nid, cpuset_current_mems_allowed)
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700303 oc->totalpages += node_spanned_pages(nid);
David Rientjesa63d83f2010-08-09 17:19:46 -0700304 return CONSTRAINT_CPUSET;
305 }
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800306 return CONSTRAINT_NONE;
307}
308
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700309static int oom_evaluate_task(struct task_struct *task, void *arg)
David Rientjes462607e2012-07-31 16:43:40 -0700310{
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700311 struct oom_control *oc = arg;
312 unsigned long points;
313
Shakeel Buttac311a12019-07-11 21:00:31 -0700314 if (oom_unkillable_task(task))
315 goto next;
316
317 /* p may not have freeable memory in nodemask */
318 if (!is_memcg_oom(oc) && !oom_cpuset_eligible(task, oc))
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700319 goto next;
David Rientjes462607e2012-07-31 16:43:40 -0700320
321 /*
322 * This task already has access to memory reserves and is being killed.
Michal Hockoa3739662016-07-28 15:45:01 -0700323 * Don't allow any other task to have access to the reserves unless
Michal Hocko862e3072016-10-07 16:58:57 -0700324 * the task has MMF_OOM_SKIP because chances that it would release
Michal Hockoa3739662016-07-28 15:45:01 -0700325 * any memory is quite low.
David Rientjes462607e2012-07-31 16:43:40 -0700326 */
Michal Hocko862e3072016-10-07 16:58:57 -0700327 if (!is_sysrq_oom(oc) && tsk_is_oom_victim(task)) {
328 if (test_bit(MMF_OOM_SKIP, &task->signal->oom_mm->flags))
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700329 goto next;
330 goto abort;
Michal Hockoa3739662016-07-28 15:45:01 -0700331 }
David Rientjes462607e2012-07-31 16:43:40 -0700332
David Rientjese1e12d22012-12-11 16:02:56 -0800333 /*
334 * If task is allocating a lot of memory and has been marked to be
335 * killed first if it triggers an oom, then select it.
336 */
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700337 if (oom_task_origin(task)) {
338 points = ULONG_MAX;
339 goto select;
340 }
David Rientjese1e12d22012-12-11 16:02:56 -0800341
Shakeel Buttac311a12019-07-11 21:00:31 -0700342 points = oom_badness(task, oc->totalpages);
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700343 if (!points || points < oc->chosen_points)
344 goto next;
345
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700346select:
347 if (oc->chosen)
348 put_task_struct(oc->chosen);
349 get_task_struct(task);
350 oc->chosen = task;
351 oc->chosen_points = points;
352next:
353 return 0;
354abort:
355 if (oc->chosen)
356 put_task_struct(oc->chosen);
357 oc->chosen = (void *)-1UL;
358 return 1;
David Rientjes462607e2012-07-31 16:43:40 -0700359}
360
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800361/*
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700362 * Simple selection loop. We choose the process with the highest number of
363 * 'points'. In case scan was aborted, oc->chosen is set to -1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700365static void select_bad_process(struct oom_control *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700367 if (is_memcg_oom(oc))
368 mem_cgroup_scan_tasks(oc->memcg, oom_evaluate_task, oc);
369 else {
370 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700372 rcu_read_lock();
373 for_each_process(p)
374 if (oom_evaluate_task(p, oc))
375 break;
376 rcu_read_unlock();
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700380static int dump_task(struct task_struct *p, void *arg)
381{
382 struct oom_control *oc = arg;
383 struct task_struct *task;
384
Shakeel Buttac311a12019-07-11 21:00:31 -0700385 if (oom_unkillable_task(p))
386 return 0;
387
388 /* p may not have freeable memory in nodemask */
389 if (!is_memcg_oom(oc) && !oom_cpuset_eligible(p, oc))
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700390 return 0;
391
392 task = find_lock_task_mm(p);
393 if (!task) {
394 /*
395 * This is a kthread or all of p's threads have already
396 * detached their mm's. There's no need to report
397 * them; they can't be oom killed anyway.
398 */
399 return 0;
400 }
401
402 pr_info("[%7d] %5d %5d %8lu %8lu %8ld %8lu %5hd %s\n",
403 task->pid, from_kuid(&init_user_ns, task_uid(task)),
404 task->tgid, task->mm->total_vm, get_mm_rss(task->mm),
405 mm_pgtables_bytes(task->mm),
406 get_mm_counter(task->mm, MM_SWAPENTS),
407 task->signal->oom_score_adj, task->comm);
408 task_unlock(task);
409
410 return 0;
411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/**
Randy Dunlap1b578df2008-03-19 17:00:42 -0700414 * dump_tasks - dump current memory state of all system tasks
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700415 * @oc: pointer to struct oom_control
Randy Dunlap1b578df2008-03-19 17:00:42 -0700416 *
David Rientjese85bfd32010-09-22 13:05:10 -0700417 * Dumps the current memory state of all eligible tasks. Tasks not in the same
418 * memcg, not in the same cpuset, or bound to a disjoint set of mempolicy nodes
419 * are not shown.
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -0800420 * State information includes task's pid, uid, tgid, vm size, rss,
421 * pgtables_bytes, swapents, oom_score_adj value, and name.
David Rientjesfef1bdd2008-02-07 00:14:07 -0800422 */
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700423static void dump_tasks(struct oom_control *oc)
David Rientjesfef1bdd2008-02-07 00:14:07 -0800424{
Rodrigo Freirec3b78b12018-08-21 21:52:41 -0700425 pr_info("Tasks state (memory values in pages):\n");
426 pr_info("[ pid ] uid tgid total_vm rss pgtables_bytes swapents oom_score_adj name\n");
David Rientjesfef1bdd2008-02-07 00:14:07 -0800427
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700428 if (is_memcg_oom(oc))
429 mem_cgroup_scan_tasks(oc->memcg, dump_task, oc);
430 else {
431 struct task_struct *p;
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700432
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700433 rcu_read_lock();
434 for_each_process(p)
435 dump_task(p, oc);
436 rcu_read_unlock();
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700437 }
David Rientjesfef1bdd2008-02-07 00:14:07 -0800438}
439
yuzhoujianef8444e2018-12-28 00:36:07 -0800440static void dump_oom_summary(struct oom_control *oc, struct task_struct *victim)
441{
442 /* one line summary of the oom killer context. */
443 pr_info("oom-kill:constraint=%s,nodemask=%*pbl",
444 oom_constraint_text[oc->constraint],
445 nodemask_pr_args(oc->nodemask));
446 cpuset_print_current_mems_allowed();
yuzhoujianf0c867d2018-12-28 00:36:10 -0800447 mem_cgroup_print_oom_context(oc->memcg, victim);
yuzhoujianef8444e2018-12-28 00:36:07 -0800448 pr_cont(",task=%s,pid=%d,uid=%d\n", victim->comm, victim->pid,
449 from_kuid(&init_user_ns, task_uid(victim)));
450}
451
Vladimir Davydov2a966b72016-07-26 15:22:33 -0700452static void dump_header(struct oom_control *oc, struct task_struct *p)
David Rientjes1b604d72009-12-14 17:57:47 -0800453{
yuzhoujianef8444e2018-12-28 00:36:07 -0800454 pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd\n",
455 current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order,
Michal Hocko0205f752017-11-15 17:39:14 -0800456 current->signal->oom_score_adj);
Michal Hocko92549902016-10-07 16:59:33 -0700457 if (!IS_ENABLED(CONFIG_COMPACTION) && oc->order)
458 pr_warn("COMPACTION is disabled!!!\n");
Vlastimil Babkaa0795cd2016-03-15 14:56:05 -0700459
David Rientjes1b604d72009-12-14 17:57:47 -0800460 dump_stack();
Yang Shi852d8be2017-11-15 17:32:07 -0800461 if (is_memcg_oom(oc))
yuzhoujianf0c867d2018-12-28 00:36:10 -0800462 mem_cgroup_print_oom_meminfo(oc->memcg);
Yang Shi852d8be2017-11-15 17:32:07 -0800463 else {
David Rientjes299c5172017-02-24 14:55:42 -0800464 show_mem(SHOW_MEM_FILTER_NODES, oc->nodemask);
Yang Shi852d8be2017-11-15 17:32:07 -0800465 if (is_dump_unreclaim_slabs())
466 dump_unreclaimable_slab();
467 }
David Rientjes1b604d72009-12-14 17:57:47 -0800468 if (sysctl_oom_dump_tasks)
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700469 dump_tasks(oc);
yuzhoujianef8444e2018-12-28 00:36:07 -0800470 if (p)
471 dump_oom_summary(oc, p);
David Rientjes1b604d72009-12-14 17:57:47 -0800472}
473
Michal Hocko5695be12014-10-20 18:12:32 +0200474/*
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800475 * Number of OOM victims in flight
Michal Hocko5695be12014-10-20 18:12:32 +0200476 */
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800477static atomic_t oom_victims = ATOMIC_INIT(0);
478static DECLARE_WAIT_QUEUE_HEAD(oom_victims_wait);
Michal Hocko5695be12014-10-20 18:12:32 +0200479
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700480static bool oom_killer_disabled __read_mostly;
Michal Hocko5695be12014-10-20 18:12:32 +0200481
Michal Hockobc448e82016-03-25 14:20:30 -0700482#define K(x) ((x) << (PAGE_SHIFT-10))
483
Michal Hocko3ef22df2016-05-19 17:13:12 -0700484/*
485 * task->mm can be NULL if the task is the exited group leader. So to
486 * determine whether the task is using a particular mm, we examine all the
487 * task's threads: if one of those is using this mm then this task was also
488 * using it.
489 */
Michal Hocko44a70ade2016-07-28 15:44:43 -0700490bool process_shares_mm(struct task_struct *p, struct mm_struct *mm)
Michal Hocko3ef22df2016-05-19 17:13:12 -0700491{
492 struct task_struct *t;
493
494 for_each_thread(p, t) {
495 struct mm_struct *t_mm = READ_ONCE(t->mm);
496 if (t_mm)
497 return t_mm == mm;
498 }
499 return false;
500}
501
Michal Hockoaac45362016-03-25 14:20:24 -0700502#ifdef CONFIG_MMU
503/*
504 * OOM Reaper kernel thread which tries to reap the memory used by the OOM
505 * victim (if that is possible) to help the OOM killer to move on.
506 */
507static struct task_struct *oom_reaper_th;
Michal Hockoaac45362016-03-25 14:20:24 -0700508static DECLARE_WAIT_QUEUE_HEAD(oom_reaper_wait);
Vladimir Davydov29c696e2016-03-25 14:20:39 -0700509static struct task_struct *oom_reaper_list;
Michal Hocko03049262016-03-25 14:20:33 -0700510static DEFINE_SPINLOCK(oom_reaper_lock);
511
Michal Hocko93065ac2018-08-21 21:52:33 -0700512bool __oom_reap_task_mm(struct mm_struct *mm)
Michal Hockoaac45362016-03-25 14:20:24 -0700513{
Michal Hockoaac45362016-03-25 14:20:24 -0700514 struct vm_area_struct *vma;
Michal Hocko93065ac2018-08-21 21:52:33 -0700515 bool ret = true;
David Rientjes27ae3572018-05-11 16:02:04 -0700516
517 /*
518 * Tell all users of get_user/copy_from_user etc... that the content
519 * is no longer stable. No barriers really needed because unmapping
520 * should imply barriers already and the reader would hit a page fault
521 * if it stumbled over a reaped memory.
522 */
523 set_bit(MMF_UNSTABLE, &mm->flags);
524
525 for (vma = mm->mmap ; vma; vma = vma->vm_next) {
526 if (!can_madv_dontneed_vma(vma))
527 continue;
528
529 /*
530 * Only anonymous pages have a good chance to be dropped
531 * without additional steps which we cannot afford as we
532 * are OOM already.
533 *
534 * We do not even care about fs backed pages because all
535 * which are reclaimable have already been reclaimed and
536 * we do not want to block exit_mmap by keeping mm ref
537 * count elevated without a good reason.
538 */
539 if (vma_is_anonymous(vma) || !(vma->vm_flags & VM_SHARED)) {
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800540 struct mmu_notifier_range range;
David Rientjes27ae3572018-05-11 16:02:04 -0700541 struct mmu_gather tlb;
542
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -0700543 mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0,
544 vma, mm, vma->vm_start,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800545 vma->vm_end);
546 tlb_gather_mmu(&tlb, mm, range.start, range.end);
547 if (mmu_notifier_invalidate_range_start_nonblock(&range)) {
548 tlb_finish_mmu(&tlb, range.start, range.end);
Michal Hocko93065ac2018-08-21 21:52:33 -0700549 ret = false;
550 continue;
551 }
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800552 unmap_page_range(&tlb, vma, range.start, range.end, NULL);
553 mmu_notifier_invalidate_range_end(&range);
554 tlb_finish_mmu(&tlb, range.start, range.end);
David Rientjes27ae3572018-05-11 16:02:04 -0700555 }
556 }
Michal Hocko93065ac2018-08-21 21:52:33 -0700557
558 return ret;
David Rientjes27ae3572018-05-11 16:02:04 -0700559}
560
Michal Hocko431f42fd2018-08-21 21:52:45 -0700561/*
562 * Reaps the address space of the give task.
563 *
564 * Returns true on success and false if none or part of the address space
565 * has been reclaimed and the caller should retry later.
566 */
David Rientjes27ae3572018-05-11 16:02:04 -0700567static bool oom_reap_task_mm(struct task_struct *tsk, struct mm_struct *mm)
568{
Michal Hockoaac45362016-03-25 14:20:24 -0700569 bool ret = true;
570
Michal Hockoaac45362016-03-25 14:20:24 -0700571 if (!down_read_trylock(&mm->mmap_sem)) {
Roman Gushchin422580c2017-07-10 15:49:05 -0700572 trace_skip_task_reaping(tsk->pid);
Michal Hockoaf5679f2018-08-21 21:52:37 -0700573 return false;
Michal Hocko4d4bbd82017-10-03 16:14:50 -0700574 }
575
576 /*
Andrea Arcangeli21292582017-09-06 16:25:00 -0700577 * MMF_OOM_SKIP is set by exit_mmap when the OOM reaper can't
578 * work on the mm anymore. The check for MMF_OOM_SKIP must run
579 * under mmap_sem for reading because it serializes against the
580 * down_write();up_write() cycle in exit_mmap().
Michal Hockoe5e3f4c2016-07-26 15:24:50 -0700581 */
Andrea Arcangeli21292582017-09-06 16:25:00 -0700582 if (test_bit(MMF_OOM_SKIP, &mm->flags)) {
Roman Gushchin422580c2017-07-10 15:49:05 -0700583 trace_skip_task_reaping(tsk->pid);
Michal Hocko431f42fd2018-08-21 21:52:45 -0700584 goto out_unlock;
Michal Hockoaac45362016-03-25 14:20:24 -0700585 }
586
Roman Gushchin422580c2017-07-10 15:49:05 -0700587 trace_start_task_reaping(tsk->pid);
588
Michal Hocko93065ac2018-08-21 21:52:33 -0700589 /* failed to reap part of the address space. Try again later */
Michal Hocko431f42fd2018-08-21 21:52:45 -0700590 ret = __oom_reap_task_mm(mm);
591 if (!ret)
592 goto out_finish;
Michal Hocko3f70dc32016-10-07 16:59:06 -0700593
Michal Hockobc448e82016-03-25 14:20:30 -0700594 pr_info("oom_reaper: reaped process %d (%s), now anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB\n",
595 task_pid_nr(tsk), tsk->comm,
596 K(get_mm_counter(mm, MM_ANONPAGES)),
597 K(get_mm_counter(mm, MM_FILEPAGES)),
598 K(get_mm_counter(mm, MM_SHMEMPAGES)));
Michal Hocko431f42fd2018-08-21 21:52:45 -0700599out_finish:
600 trace_finish_task_reaping(tsk->pid);
601out_unlock:
Michal Hockoaac45362016-03-25 14:20:24 -0700602 up_read(&mm->mmap_sem);
Michal Hocko36324a92016-03-25 14:20:27 -0700603
Michal Hockoaac45362016-03-25 14:20:24 -0700604 return ret;
605}
606
Michal Hockobc448e82016-03-25 14:20:30 -0700607#define MAX_OOM_REAP_RETRIES 10
Michal Hocko36324a92016-03-25 14:20:27 -0700608static void oom_reap_task(struct task_struct *tsk)
Michal Hockoaac45362016-03-25 14:20:24 -0700609{
610 int attempts = 0;
Michal Hocko26db62f2016-10-07 16:58:51 -0700611 struct mm_struct *mm = tsk->signal->oom_mm;
Michal Hockoaac45362016-03-25 14:20:24 -0700612
613 /* Retry the down_read_trylock(mmap_sem) a few times */
David Rientjes27ae3572018-05-11 16:02:04 -0700614 while (attempts++ < MAX_OOM_REAP_RETRIES && !oom_reap_task_mm(tsk, mm))
Michal Hockoaac45362016-03-25 14:20:24 -0700615 schedule_timeout_idle(HZ/10);
616
Tetsuo Handa97b12552018-04-05 16:25:45 -0700617 if (attempts <= MAX_OOM_REAP_RETRIES ||
618 test_bit(MMF_OOM_SKIP, &mm->flags))
Tetsuo Handa7ebffa42016-10-07 16:58:45 -0700619 goto done;
Michal Hocko11a410d2016-07-28 15:44:58 -0700620
Tetsuo Handa7ebffa42016-10-07 16:58:45 -0700621 pr_info("oom_reaper: unable to reap pid:%d (%s)\n",
622 task_pid_nr(tsk), tsk->comm);
Tetsuo Handa7ebffa42016-10-07 16:58:45 -0700623 debug_show_all_locks();
Michal Hockobc448e82016-03-25 14:20:30 -0700624
Tetsuo Handa7ebffa42016-10-07 16:58:45 -0700625done:
Michal Hocko449d7772016-05-19 17:13:15 -0700626 tsk->oom_reaper_list = NULL;
Michal Hocko449d7772016-05-19 17:13:15 -0700627
Michal Hocko26db62f2016-10-07 16:58:51 -0700628 /*
629 * Hide this mm from OOM killer because it has been either reaped or
630 * somebody can't call up_write(mmap_sem).
631 */
Michal Hocko862e3072016-10-07 16:58:57 -0700632 set_bit(MMF_OOM_SKIP, &mm->flags);
Michal Hocko26db62f2016-10-07 16:58:51 -0700633
Michal Hockoaac45362016-03-25 14:20:24 -0700634 /* Drop a reference taken by wake_oom_reaper */
Michal Hocko36324a92016-03-25 14:20:27 -0700635 put_task_struct(tsk);
Michal Hockoaac45362016-03-25 14:20:24 -0700636}
637
638static int oom_reaper(void *unused)
639{
640 while (true) {
Michal Hocko03049262016-03-25 14:20:33 -0700641 struct task_struct *tsk = NULL;
Michal Hockoaac45362016-03-25 14:20:24 -0700642
Vladimir Davydov29c696e2016-03-25 14:20:39 -0700643 wait_event_freezable(oom_reaper_wait, oom_reaper_list != NULL);
Michal Hocko03049262016-03-25 14:20:33 -0700644 spin_lock(&oom_reaper_lock);
Vladimir Davydov29c696e2016-03-25 14:20:39 -0700645 if (oom_reaper_list != NULL) {
646 tsk = oom_reaper_list;
647 oom_reaper_list = tsk->oom_reaper_list;
Michal Hocko03049262016-03-25 14:20:33 -0700648 }
649 spin_unlock(&oom_reaper_lock);
650
651 if (tsk)
652 oom_reap_task(tsk);
Michal Hockoaac45362016-03-25 14:20:24 -0700653 }
654
655 return 0;
656}
657
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700658static void wake_oom_reaper(struct task_struct *tsk)
Michal Hockoaac45362016-03-25 14:20:24 -0700659{
Tetsuo Handa9bcdeb52019-02-01 14:20:31 -0800660 /* mm is already queued? */
661 if (test_and_set_bit(MMF_OOM_REAP_QUEUED, &tsk->signal->oom_mm->flags))
Michal Hockoaac45362016-03-25 14:20:24 -0700662 return;
663
Michal Hocko36324a92016-03-25 14:20:27 -0700664 get_task_struct(tsk);
Michal Hockoaac45362016-03-25 14:20:24 -0700665
Michal Hocko03049262016-03-25 14:20:33 -0700666 spin_lock(&oom_reaper_lock);
Vladimir Davydov29c696e2016-03-25 14:20:39 -0700667 tsk->oom_reaper_list = oom_reaper_list;
668 oom_reaper_list = tsk;
Michal Hocko03049262016-03-25 14:20:33 -0700669 spin_unlock(&oom_reaper_lock);
Roman Gushchin422580c2017-07-10 15:49:05 -0700670 trace_wake_reaper(tsk->pid);
Michal Hocko03049262016-03-25 14:20:33 -0700671 wake_up(&oom_reaper_wait);
Michal Hockoaac45362016-03-25 14:20:24 -0700672}
673
674static int __init oom_init(void)
675{
676 oom_reaper_th = kthread_run(oom_reaper, NULL, "oom_reaper");
Michal Hockoaac45362016-03-25 14:20:24 -0700677 return 0;
678}
679subsys_initcall(oom_init)
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700680#else
681static inline void wake_oom_reaper(struct task_struct *tsk)
682{
683}
684#endif /* CONFIG_MMU */
Michal Hockoaac45362016-03-25 14:20:24 -0700685
Michal Hocko49550b62015-02-11 15:26:12 -0800686/**
Johannes Weiner16e95192015-06-24 16:57:07 -0700687 * mark_oom_victim - mark the given task as OOM victim
Michal Hocko49550b62015-02-11 15:26:12 -0800688 * @tsk: task to mark
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800689 *
Johannes Weinerdc564012015-06-24 16:57:19 -0700690 * Has to be called with oom_lock held and never after
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800691 * oom has been disabled already.
Michal Hocko26db62f2016-10-07 16:58:51 -0700692 *
693 * tsk->mm has to be non NULL and caller has to guarantee it is stable (either
694 * under task_lock or operate on the current).
Michal Hocko49550b62015-02-11 15:26:12 -0800695 */
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700696static void mark_oom_victim(struct task_struct *tsk)
Michal Hocko49550b62015-02-11 15:26:12 -0800697{
Michal Hocko26db62f2016-10-07 16:58:51 -0700698 struct mm_struct *mm = tsk->mm;
699
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800700 WARN_ON(oom_killer_disabled);
701 /* OOM killer might race with memcg OOM */
702 if (test_and_set_tsk_thread_flag(tsk, TIF_MEMDIE))
703 return;
Michal Hocko26db62f2016-10-07 16:58:51 -0700704
Michal Hocko26db62f2016-10-07 16:58:51 -0700705 /* oom_mm is bound to the signal struct life time. */
Michal Hocko4837fe32017-12-14 15:33:15 -0800706 if (!cmpxchg(&tsk->signal->oom_mm, NULL, mm)) {
Vegard Nossumf1f10072017-02-27 14:30:07 -0800707 mmgrab(tsk->signal->oom_mm);
Michal Hocko4837fe32017-12-14 15:33:15 -0800708 set_bit(MMF_OOM_VICTIM, &mm->flags);
709 }
Michal Hocko26db62f2016-10-07 16:58:51 -0700710
Michal Hocko63a8ca92015-02-11 15:26:15 -0800711 /*
712 * Make sure that the task is woken up from uninterruptible sleep
713 * if it is frozen because OOM killer wouldn't be able to free
714 * any memory and livelock. freezing_slow_path will tell the freezer
715 * that TIF_MEMDIE tasks should be ignored.
716 */
717 __thaw_task(tsk);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800718 atomic_inc(&oom_victims);
Roman Gushchin422580c2017-07-10 15:49:05 -0700719 trace_mark_victim(tsk->pid);
Michal Hocko49550b62015-02-11 15:26:12 -0800720}
721
722/**
Johannes Weiner16e95192015-06-24 16:57:07 -0700723 * exit_oom_victim - note the exit of an OOM victim
Michal Hocko49550b62015-02-11 15:26:12 -0800724 */
Tetsuo Handa38531202016-10-07 16:59:03 -0700725void exit_oom_victim(void)
Michal Hocko49550b62015-02-11 15:26:12 -0800726{
Tetsuo Handa38531202016-10-07 16:59:03 -0700727 clear_thread_flag(TIF_MEMDIE);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800728
Johannes Weinerc38f1022015-06-24 16:57:13 -0700729 if (!atomic_dec_return(&oom_victims))
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800730 wake_up_all(&oom_victims_wait);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800731}
732
733/**
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700734 * oom_killer_enable - enable OOM killer
735 */
736void oom_killer_enable(void)
737{
738 oom_killer_disabled = false;
Michal Hockod75da002017-05-03 14:54:57 -0700739 pr_info("OOM killer enabled.\n");
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700740}
741
742/**
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800743 * oom_killer_disable - disable OOM killer
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700744 * @timeout: maximum timeout to wait for oom victims in jiffies
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800745 *
746 * Forces all page allocations to fail rather than trigger OOM killer.
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700747 * Will block and wait until all OOM victims are killed or the given
748 * timeout expires.
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800749 *
750 * The function cannot be called when there are runnable user tasks because
751 * the userspace would see unexpected allocation failures as a result. Any
752 * new usage of this function should be consulted with MM people.
753 *
754 * Returns true if successful and false if the OOM killer cannot be
755 * disabled.
756 */
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700757bool oom_killer_disable(signed long timeout)
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800758{
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700759 signed long ret;
760
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800761 /*
Tetsuo Handa6afcf282016-03-17 14:20:45 -0700762 * Make sure to not race with an ongoing OOM killer. Check that the
763 * current is not killed (possibly due to sharing the victim's memory).
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800764 */
Tetsuo Handa6afcf282016-03-17 14:20:45 -0700765 if (mutex_lock_killable(&oom_lock))
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800766 return false;
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800767 oom_killer_disabled = true;
Johannes Weinerdc564012015-06-24 16:57:19 -0700768 mutex_unlock(&oom_lock);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800769
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700770 ret = wait_event_interruptible_timeout(oom_victims_wait,
771 !atomic_read(&oom_victims), timeout);
772 if (ret <= 0) {
773 oom_killer_enable();
774 return false;
775 }
Michal Hockod75da002017-05-03 14:54:57 -0700776 pr_info("OOM killer disabled.\n");
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800777
778 return true;
779}
780
Michal Hocko1af8bb42016-07-28 15:44:52 -0700781static inline bool __task_will_free_mem(struct task_struct *task)
782{
783 struct signal_struct *sig = task->signal;
784
785 /*
786 * A coredumping process may sleep for an extended period in exit_mm(),
787 * so the oom killer cannot assume that the process will promptly exit
788 * and release memory.
789 */
790 if (sig->flags & SIGNAL_GROUP_COREDUMP)
791 return false;
792
793 if (sig->flags & SIGNAL_GROUP_EXIT)
794 return true;
795
796 if (thread_group_empty(task) && (task->flags & PF_EXITING))
797 return true;
798
799 return false;
800}
801
802/*
803 * Checks whether the given task is dying or exiting and likely to
804 * release its address space. This means that all threads and processes
805 * sharing the same mm have to be killed or exiting.
Michal Hocko091f3622016-07-28 15:45:04 -0700806 * Caller has to make sure that task->mm is stable (hold task_lock or
807 * it operates on the current).
Michal Hocko1af8bb42016-07-28 15:44:52 -0700808 */
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700809static bool task_will_free_mem(struct task_struct *task)
Michal Hocko1af8bb42016-07-28 15:44:52 -0700810{
Michal Hocko091f3622016-07-28 15:45:04 -0700811 struct mm_struct *mm = task->mm;
Michal Hocko1af8bb42016-07-28 15:44:52 -0700812 struct task_struct *p;
Geert Uytterhoevenf33e6f02016-08-11 15:33:09 -0700813 bool ret = true;
Michal Hocko1af8bb42016-07-28 15:44:52 -0700814
Michal Hocko091f3622016-07-28 15:45:04 -0700815 /*
816 * Skip tasks without mm because it might have passed its exit_mm and
817 * exit_oom_victim. oom_reaper could have rescued that but do not rely
818 * on that for now. We can consider find_lock_task_mm in future.
819 */
820 if (!mm)
821 return false;
822
Michal Hocko1af8bb42016-07-28 15:44:52 -0700823 if (!__task_will_free_mem(task))
824 return false;
825
826 /*
Michal Hocko696453e2016-07-28 15:44:55 -0700827 * This task has already been drained by the oom reaper so there are
828 * only small chances it will free some more
829 */
Michal Hocko862e3072016-10-07 16:58:57 -0700830 if (test_bit(MMF_OOM_SKIP, &mm->flags))
Michal Hocko696453e2016-07-28 15:44:55 -0700831 return false;
Michal Hocko696453e2016-07-28 15:44:55 -0700832
Michal Hocko091f3622016-07-28 15:45:04 -0700833 if (atomic_read(&mm->mm_users) <= 1)
Michal Hocko1af8bb42016-07-28 15:44:52 -0700834 return true;
Michal Hocko1af8bb42016-07-28 15:44:52 -0700835
836 /*
Michal Hocko5870c2e2016-10-07 16:57:32 -0700837 * Make sure that all tasks which share the mm with the given tasks
838 * are dying as well to make sure that a) nobody pins its mm and
839 * b) the task is also reapable by the oom reaper.
Michal Hocko1af8bb42016-07-28 15:44:52 -0700840 */
841 rcu_read_lock();
842 for_each_process(p) {
843 if (!process_shares_mm(p, mm))
844 continue;
845 if (same_thread_group(task, p))
846 continue;
847 ret = __task_will_free_mem(p);
848 if (!ret)
849 break;
850 }
851 rcu_read_unlock();
Michal Hocko1af8bb42016-07-28 15:44:52 -0700852
853 return ret;
854}
855
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800856static void __oom_kill_process(struct task_struct *victim, const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Roman Gushchin5989ad72018-08-21 21:53:50 -0700858 struct task_struct *p;
David Rientjes647f2bd2012-03-21 16:33:46 -0700859 struct mm_struct *mm;
Tetsuo Handabb299022016-03-25 14:20:44 -0700860 bool can_oom_reap = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
David Rientjes6b0c81b2012-07-31 16:43:45 -0700862 p = find_lock_task_mm(victim);
863 if (!p) {
David Rientjes6b0c81b2012-07-31 16:43:45 -0700864 put_task_struct(victim);
David Rientjes647f2bd2012-03-21 16:33:46 -0700865 return;
David Rientjes6b0c81b2012-07-31 16:43:45 -0700866 } else if (victim != p) {
867 get_task_struct(p);
868 put_task_struct(victim);
869 victim = p;
870 }
David Rientjes647f2bd2012-03-21 16:33:46 -0700871
Tetsuo Handa880b76892015-11-05 18:47:51 -0800872 /* Get a reference to safely compare mm after task_unlock(victim) */
David Rientjes647f2bd2012-03-21 16:33:46 -0700873 mm = victim->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -0800874 mmgrab(mm);
Konstantin Khlebnikov8e675f72017-07-06 15:40:28 -0700875
876 /* Raise event before sending signal: task reaper must see this */
877 count_vm_event(OOM_KILL);
Roman Gushchinfe6bdfc2018-06-14 15:28:05 -0700878 memcg_memory_event_mm(mm, MEMCG_OOM_KILL);
Konstantin Khlebnikov8e675f72017-07-06 15:40:28 -0700879
Tetsuo Handa426fb5e2015-11-05 18:47:44 -0800880 /*
Michal Hockocd04ae12017-09-06 16:24:50 -0700881 * We should send SIGKILL before granting access to memory reserves
882 * in order to prevent the OOM victim from depleting the memory
883 * reserves from the user space under its control.
Tetsuo Handa426fb5e2015-11-05 18:47:44 -0800884 */
Eric W. Biederman079b22d2018-09-03 10:32:52 +0200885 do_send_sig_info(SIGKILL, SEND_SIG_PRIV, victim, PIDTYPE_TGID);
Johannes Weiner16e95192015-06-24 16:57:07 -0700886 mark_oom_victim(victim);
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800887 pr_err("%s: Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB\n",
888 message, task_pid_nr(victim), victim->comm,
889 K(victim->mm->total_vm),
David Rientjes647f2bd2012-03-21 16:33:46 -0700890 K(get_mm_counter(victim->mm, MM_ANONPAGES)),
Jerome Marchandeca56ff2016-01-14 15:19:26 -0800891 K(get_mm_counter(victim->mm, MM_FILEPAGES)),
892 K(get_mm_counter(victim->mm, MM_SHMEMPAGES)));
David Rientjes647f2bd2012-03-21 16:33:46 -0700893 task_unlock(victim);
894
895 /*
896 * Kill all user processes sharing victim->mm in other thread groups, if
897 * any. They don't get access to memory reserves, though, to avoid
898 * depletion of all memory. This prevents mm->mmap_sem livelock when an
899 * oom killed thread cannot exit because it requires the semaphore and
900 * its contended by another thread trying to allocate memory itself.
901 * That thread will now get access to memory reserves since it has a
902 * pending fatal signal.
903 */
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800904 rcu_read_lock();
Oleg Nesterovc3190252015-11-05 18:48:23 -0800905 for_each_process(p) {
Oleg Nesterov4d7b3392015-11-05 18:48:26 -0800906 if (!process_shares_mm(p, mm))
Oleg Nesterovc3190252015-11-05 18:48:23 -0800907 continue;
908 if (same_thread_group(p, victim))
909 continue;
Michal Hocko1b51e652016-10-07 16:59:09 -0700910 if (is_global_init(p)) {
Michal Hockoaac45362016-03-25 14:20:24 -0700911 can_oom_reap = false;
Michal Hocko862e3072016-10-07 16:58:57 -0700912 set_bit(MMF_OOM_SKIP, &mm->flags);
Michal Hockoa3739662016-07-28 15:45:01 -0700913 pr_info("oom killer %d (%s) has mm pinned by %d (%s)\n",
914 task_pid_nr(victim), victim->comm,
915 task_pid_nr(p), p->comm);
Oleg Nesterovc3190252015-11-05 18:48:23 -0800916 continue;
Michal Hockoaac45362016-03-25 14:20:24 -0700917 }
Michal Hocko1b51e652016-10-07 16:59:09 -0700918 /*
919 * No use_mm() user needs to read from the userspace so we are
920 * ok to reap it.
921 */
922 if (unlikely(p->flags & PF_KTHREAD))
923 continue;
Eric W. Biederman079b22d2018-09-03 10:32:52 +0200924 do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID);
Oleg Nesterovc3190252015-11-05 18:48:23 -0800925 }
David Rientjes6b0c81b2012-07-31 16:43:45 -0700926 rcu_read_unlock();
David Rientjes647f2bd2012-03-21 16:33:46 -0700927
Michal Hockoaac45362016-03-25 14:20:24 -0700928 if (can_oom_reap)
Michal Hocko36324a92016-03-25 14:20:27 -0700929 wake_oom_reaper(victim);
Michal Hockoaac45362016-03-25 14:20:24 -0700930
Tetsuo Handa880b76892015-11-05 18:47:51 -0800931 mmdrop(mm);
David Rientjes6b0c81b2012-07-31 16:43:45 -0700932 put_task_struct(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
David Rientjes647f2bd2012-03-21 16:33:46 -0700934#undef K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
David Rientjes309ed882010-08-09 17:18:54 -0700936/*
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700937 * Kill provided task unless it's secured by setting
938 * oom_score_adj to OOM_SCORE_ADJ_MIN.
939 */
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800940static int oom_kill_memcg_member(struct task_struct *task, void *message)
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700941{
Tetsuo Handad342a0b2019-03-05 15:48:22 -0800942 if (task->signal->oom_score_adj != OOM_SCORE_ADJ_MIN &&
943 !is_global_init(task)) {
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700944 get_task_struct(task);
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800945 __oom_kill_process(task, message);
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700946 }
947 return 0;
948}
949
Roman Gushchin5989ad72018-08-21 21:53:50 -0700950static void oom_kill_process(struct oom_control *oc, const char *message)
951{
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800952 struct task_struct *victim = oc->chosen;
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700953 struct mem_cgroup *oom_group;
Roman Gushchin5989ad72018-08-21 21:53:50 -0700954 static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL,
955 DEFAULT_RATELIMIT_BURST);
956
957 /*
958 * If the task is already exiting, don't alarm the sysadmin or kill
959 * its children or threads, just give it access to memory reserves
960 * so it can die quickly
961 */
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800962 task_lock(victim);
963 if (task_will_free_mem(victim)) {
964 mark_oom_victim(victim);
965 wake_oom_reaper(victim);
966 task_unlock(victim);
967 put_task_struct(victim);
Roman Gushchin5989ad72018-08-21 21:53:50 -0700968 return;
969 }
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800970 task_unlock(victim);
Roman Gushchin5989ad72018-08-21 21:53:50 -0700971
972 if (__ratelimit(&oom_rs))
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800973 dump_header(oc, victim);
Roman Gushchin5989ad72018-08-21 21:53:50 -0700974
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700975 /*
976 * Do we need to kill the entire memory cgroup?
977 * Or even one of the ancestor memory cgroups?
978 * Check this out before killing the victim task.
979 */
980 oom_group = mem_cgroup_get_oom_group(victim, oc->memcg);
981
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800982 __oom_kill_process(victim, message);
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700983
984 /*
985 * If necessary, kill all tasks in the selected memory cgroup.
986 */
987 if (oom_group) {
988 mem_cgroup_print_oom_group(oom_group);
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800989 mem_cgroup_scan_tasks(oom_group, oom_kill_memcg_member,
990 (void*)message);
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700991 mem_cgroup_put(oom_group);
992 }
Roman Gushchin5989ad72018-08-21 21:53:50 -0700993}
994
David Rientjes309ed882010-08-09 17:18:54 -0700995/*
996 * Determines whether the kernel must panic because of the panic_on_oom sysctl.
997 */
Yafang Shao432b1de2019-06-28 12:06:59 -0700998static void check_panic_on_oom(struct oom_control *oc)
David Rientjes309ed882010-08-09 17:18:54 -0700999{
1000 if (likely(!sysctl_panic_on_oom))
1001 return;
1002 if (sysctl_panic_on_oom != 2) {
1003 /*
1004 * panic_on_oom == 1 only affects CONSTRAINT_NONE, the kernel
1005 * does not panic for cpuset, mempolicy, or memcg allocation
1006 * failures.
1007 */
Yafang Shao432b1de2019-06-28 12:06:59 -07001008 if (oc->constraint != CONSTRAINT_NONE)
David Rientjes309ed882010-08-09 17:18:54 -07001009 return;
1010 }
David Rientjes071a4be2015-09-08 15:00:42 -07001011 /* Do not panic for oom kills triggered by sysrq */
Yaowei Baidb2a0dd2015-11-06 16:28:06 -08001012 if (is_sysrq_oom(oc))
David Rientjes071a4be2015-09-08 15:00:42 -07001013 return;
Vladimir Davydov2a966b72016-07-26 15:22:33 -07001014 dump_header(oc, NULL);
David Rientjes309ed882010-08-09 17:18:54 -07001015 panic("Out of memory: %s panic_on_oom is enabled\n",
1016 sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide");
1017}
1018
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -07001019static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
1020
1021int register_oom_notifier(struct notifier_block *nb)
1022{
1023 return blocking_notifier_chain_register(&oom_notify_list, nb);
1024}
1025EXPORT_SYMBOL_GPL(register_oom_notifier);
1026
1027int unregister_oom_notifier(struct notifier_block *nb)
1028{
1029 return blocking_notifier_chain_unregister(&oom_notify_list, nb);
1030}
1031EXPORT_SYMBOL_GPL(unregister_oom_notifier);
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033/**
David Rientjes6e0fc462015-09-08 15:00:36 -07001034 * out_of_memory - kill the "best" process when we run out of memory
1035 * @oc: pointer to struct oom_control
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 *
1037 * If we run out of memory, we have the choice between either
1038 * killing a random task (bad), letting the system crash (worse)
1039 * OR try to be smart about which process to kill. Note that we
1040 * don't have to be perfect here, we just have to be good.
1041 */
David Rientjes6e0fc462015-09-08 15:00:36 -07001042bool out_of_memory(struct oom_control *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -07001044 unsigned long freed = 0;
1045
Johannes Weinerdc564012015-06-24 16:57:19 -07001046 if (oom_killer_disabled)
1047 return false;
1048
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001049 if (!is_memcg_oom(oc)) {
1050 blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
1051 if (freed > 0)
1052 /* Got some memory back in the last second. */
1053 return true;
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
David Rientjes7b98c2e2010-08-09 17:18:48 -07001056 /*
David Rientjes9ff48682012-12-11 16:01:30 -08001057 * If current has a pending SIGKILL or is exiting, then automatically
1058 * select it. The goal is to allow it to allocate so that it may
1059 * quickly exit and free its memory.
David Rientjes7b98c2e2010-08-09 17:18:48 -07001060 */
Michal Hocko091f3622016-07-28 15:45:04 -07001061 if (task_will_free_mem(current)) {
Johannes Weiner16e95192015-06-24 16:57:07 -07001062 mark_oom_victim(current);
Michal Hocko1af8bb42016-07-28 15:44:52 -07001063 wake_oom_reaper(current);
David Rientjes75e8f8b2015-09-08 15:00:47 -07001064 return true;
David Rientjes7b98c2e2010-08-09 17:18:48 -07001065 }
1066
Christoph Lameter9b0f8b02006-02-20 18:27:52 -08001067 /*
Michal Hocko3da88fb32016-05-19 17:13:09 -07001068 * The OOM killer does not compensate for IO-less reclaim.
1069 * pagefault_out_of_memory lost its gfp context so we have to
1070 * make sure exclude 0 mask - all other users should have at least
1071 * ___GFP_DIRECT_RECLAIM to get here.
1072 */
Michal Hocko06ad2762017-02-22 15:46:22 -08001073 if (oc->gfp_mask && !(oc->gfp_mask & __GFP_FS))
Michal Hocko3da88fb32016-05-19 17:13:09 -07001074 return true;
1075
1076 /*
Christoph Lameter9b0f8b02006-02-20 18:27:52 -08001077 * Check if there were limitations on the allocation (only relevant for
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001078 * NUMA and memcg) that may require different handling.
Christoph Lameter9b0f8b02006-02-20 18:27:52 -08001079 */
Yafang Shao432b1de2019-06-28 12:06:59 -07001080 oc->constraint = constrained_alloc(oc);
1081 if (oc->constraint != CONSTRAINT_MEMORY_POLICY)
David Rientjes6e0fc462015-09-08 15:00:36 -07001082 oc->nodemask = NULL;
Yafang Shao432b1de2019-06-28 12:06:59 -07001083 check_panic_on_oom(oc);
David Rientjes0aad4b32010-08-09 17:18:59 -07001084
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001085 if (!is_memcg_oom(oc) && sysctl_oom_kill_allocating_task &&
Shakeel Buttac311a12019-07-11 21:00:31 -07001086 current->mm && !oom_unkillable_task(current) &&
1087 oom_cpuset_eligible(current, oc) &&
David Rientjes121d1ba2012-07-31 16:42:55 -07001088 current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {
David Rientjes6b0c81b2012-07-31 16:43:45 -07001089 get_task_struct(current);
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001090 oc->chosen = current;
1091 oom_kill_process(oc, "Out of memory (oom_kill_allocating_task)");
David Rientjes75e8f8b2015-09-08 15:00:47 -07001092 return true;
David Rientjes0aad4b32010-08-09 17:18:59 -07001093 }
1094
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001095 select_bad_process(oc);
Johannes Weiner3100dab2018-09-04 15:45:34 -07001096 /* Found nothing?!?! */
1097 if (!oc->chosen) {
Vladimir Davydov2a966b72016-07-26 15:22:33 -07001098 dump_header(oc, NULL);
Johannes Weiner3100dab2018-09-04 15:45:34 -07001099 pr_warn("Out of memory and no killable processes...\n");
1100 /*
1101 * If we got here due to an actual allocation at the
1102 * system level, we cannot survive this and will enter
1103 * an endless loop in the allocator. Bail out now.
1104 */
1105 if (!is_sysrq_oom(oc) && !is_memcg_oom(oc))
1106 panic("System is deadlocked on memory\n");
David Rientjes0aad4b32010-08-09 17:18:59 -07001107 }
Michal Hocko9bfe5de2018-08-17 15:49:04 -07001108 if (oc->chosen && oc->chosen != (void *)-1UL)
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001109 oom_kill_process(oc, !is_memcg_oom(oc) ? "Out of memory" :
1110 "Memory cgroup out of memory");
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001111 return !!oc->chosen;
Michal Hockoc32b3cb2015-02-11 15:26:24 -08001112}
1113
David Rientjese3658932010-08-09 17:18:55 -07001114/*
1115 * The pagefault handler calls here because it is out of memory, so kill a
Vladimir Davydov798fd752016-07-26 15:22:30 -07001116 * memory-hogging task. If oom_lock is held by somebody else, a parallel oom
1117 * killing is already in progress so do nothing.
David Rientjese3658932010-08-09 17:18:55 -07001118 */
1119void pagefault_out_of_memory(void)
1120{
David Rientjes6e0fc462015-09-08 15:00:36 -07001121 struct oom_control oc = {
1122 .zonelist = NULL,
1123 .nodemask = NULL,
Vladimir Davydov2a966b72016-07-26 15:22:33 -07001124 .memcg = NULL,
David Rientjes6e0fc462015-09-08 15:00:36 -07001125 .gfp_mask = 0,
1126 .order = 0,
David Rientjes6e0fc462015-09-08 15:00:36 -07001127 };
1128
Johannes Weiner49426422013-10-16 13:46:59 -07001129 if (mem_cgroup_oom_synchronize(true))
Johannes Weinerdc564012015-06-24 16:57:19 -07001130 return;
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001131
Johannes Weinerdc564012015-06-24 16:57:19 -07001132 if (!mutex_trylock(&oom_lock))
1133 return;
Tetsuo Handaa1048082016-10-07 17:00:49 -07001134 out_of_memory(&oc);
Johannes Weinerdc564012015-06-24 16:57:19 -07001135 mutex_unlock(&oom_lock);
David Rientjese3658932010-08-09 17:18:55 -07001136}