blob: e97a05d9621f47425a25f88fb9c0ae8fbd50004b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/oom_kill.c
3 *
4 * Copyright (C) 1998,2000 Rik van Riel
5 * Thanks go out to Claus Fischer for some serious inspiration and
6 * for goading me into coding this file...
David Rientjesa63d83f2010-08-09 17:19:46 -07007 * Copyright (C) 2010 Google, Inc.
8 * Rewritten by David Rientjes
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * The routines in this file are used to kill a process when
Paul Jacksona49335c2005-09-06 15:18:09 -070011 * we're seriously out of memory. This gets called from __alloc_pages()
12 * in mm/page_alloc.c when we really run out of memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * Since we won't call these routines often (on a well-configured
15 * machine) this file will double as a 'coding guide' and a signpost
16 * for newbie kernel hackers. It features several pointers to major
17 * kernel subsystems and hints as to where to find out what things do.
18 */
19
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070020#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040022#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/sched.h>
25#include <linux/swap.h>
26#include <linux/timex.h>
27#include <linux/jiffies.h>
Paul Jacksonef08e3b2005-09-06 15:18:13 -070028#include <linux/cpuset.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040029#include <linux/export.h>
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070030#include <linux/notifier.h>
Pavel Emelianovc7ba5c92008-02-07 00:13:58 -080031#include <linux/memcontrol.h>
David Rientjes6f48d0eb2010-08-09 17:18:52 -070032#include <linux/mempolicy.h>
David Howells5cd9c582008-08-14 11:37:28 +010033#include <linux/security.h>
David Rientjesedd45542011-03-22 16:30:12 -070034#include <linux/ptrace.h>
David Rientjesf660daa2011-10-31 17:07:07 -070035#include <linux/freezer.h>
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080036#include <linux/ftrace.h>
David Rientjesdc3f21e2012-03-21 16:33:47 -070037#include <linux/ratelimit.h>
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080038
39#define CREATE_TRACE_POINTS
40#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
KAMEZAWA Hiroyukifadd8fb2006-06-23 02:03:13 -070042int sysctl_panic_on_oom;
David Rientjesfe071d72007-10-16 23:25:56 -070043int sysctl_oom_kill_allocating_task;
David Rientjesad915c42010-08-09 17:18:53 -070044int sysctl_oom_dump_tasks = 1;
Johannes Weinerdc564012015-06-24 16:57:19 -070045
46DEFINE_MUTEX(oom_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
David Rientjes6f48d0eb2010-08-09 17:18:52 -070048#ifdef CONFIG_NUMA
49/**
50 * has_intersects_mems_allowed() - check task eligiblity for kill
Oleg Nesterovad962442014-01-21 15:50:00 -080051 * @start: task struct of which task to consider
David Rientjes6f48d0eb2010-08-09 17:18:52 -070052 * @mask: nodemask passed to page allocator for mempolicy ooms
53 *
54 * Task eligibility is determined by whether or not a candidate task, @tsk,
55 * shares the same mempolicy nodes as current if it is bound by such a policy
56 * and whether or not it has the same set of allowed cpuset nodes.
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070057 */
Oleg Nesterovad962442014-01-21 15:50:00 -080058static bool has_intersects_mems_allowed(struct task_struct *start,
David Rientjes6f48d0eb2010-08-09 17:18:52 -070059 const nodemask_t *mask)
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070060{
Oleg Nesterovad962442014-01-21 15:50:00 -080061 struct task_struct *tsk;
62 bool ret = false;
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070063
Oleg Nesterovad962442014-01-21 15:50:00 -080064 rcu_read_lock();
Oleg Nesterov1da4db02014-01-21 15:49:58 -080065 for_each_thread(start, tsk) {
David Rientjes6f48d0eb2010-08-09 17:18:52 -070066 if (mask) {
67 /*
68 * If this is a mempolicy constrained oom, tsk's
69 * cpuset is irrelevant. Only return true if its
70 * mempolicy intersects current, otherwise it may be
71 * needlessly killed.
72 */
Oleg Nesterovad962442014-01-21 15:50:00 -080073 ret = mempolicy_nodemask_intersects(tsk, mask);
David Rientjes6f48d0eb2010-08-09 17:18:52 -070074 } else {
75 /*
76 * This is not a mempolicy constrained oom, so only
77 * check the mems of tsk's cpuset.
78 */
Oleg Nesterovad962442014-01-21 15:50:00 -080079 ret = cpuset_mems_allowed_intersects(current, tsk);
David Rientjes6f48d0eb2010-08-09 17:18:52 -070080 }
Oleg Nesterovad962442014-01-21 15:50:00 -080081 if (ret)
82 break;
Oleg Nesterov1da4db02014-01-21 15:49:58 -080083 }
Oleg Nesterovad962442014-01-21 15:50:00 -080084 rcu_read_unlock();
KOSAKI Motohirodf1090a2010-08-09 17:19:39 -070085
Oleg Nesterovad962442014-01-21 15:50:00 -080086 return ret;
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070087}
David Rientjes6f48d0eb2010-08-09 17:18:52 -070088#else
89static bool has_intersects_mems_allowed(struct task_struct *tsk,
90 const nodemask_t *mask)
91{
92 return true;
93}
94#endif /* CONFIG_NUMA */
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070095
David Rientjes6f48d0eb2010-08-09 17:18:52 -070096/*
97 * The process p may have detached its own ->mm while exiting or through
98 * use_mm(), but one or more of its subthreads may still have a valid
99 * pointer. Return p, or any of its subthreads with a valid ->mm, with
100 * task_lock() held.
101 */
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -0700102struct task_struct *find_lock_task_mm(struct task_struct *p)
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700103{
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800104 struct task_struct *t;
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700105
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800106 rcu_read_lock();
107
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800108 for_each_thread(p, t) {
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700109 task_lock(t);
110 if (likely(t->mm))
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800111 goto found;
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700112 task_unlock(t);
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800113 }
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800114 t = NULL;
115found:
116 rcu_read_unlock();
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700117
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800118 return t;
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700119}
120
Yaowei Baidb2a0dd2015-11-06 16:28:06 -0800121/*
122 * order == -1 means the oom kill is required by sysrq, otherwise only
123 * for display purposes.
124 */
125static inline bool is_sysrq_oom(struct oom_control *oc)
126{
127 return oc->order == -1;
128}
129
KOSAKI Motohiroab290ad2010-08-09 17:19:35 -0700130/* return true if the task is not adequate as candidate victim task. */
David Rientjese85bfd32010-09-22 13:05:10 -0700131static bool oom_unkillable_task(struct task_struct *p,
Johannes Weiner2314b422014-12-10 15:44:33 -0800132 struct mem_cgroup *memcg, const nodemask_t *nodemask)
KOSAKI Motohiroab290ad2010-08-09 17:19:35 -0700133{
134 if (is_global_init(p))
135 return true;
136 if (p->flags & PF_KTHREAD)
137 return true;
138
139 /* When mem_cgroup_out_of_memory() and p is not member of the group */
Johannes Weiner72835c82012-01-12 17:18:32 -0800140 if (memcg && !task_in_mem_cgroup(p, memcg))
KOSAKI Motohiroab290ad2010-08-09 17:19:35 -0700141 return true;
142
143 /* p may not have freeable memory in nodemask */
144 if (!has_intersects_mems_allowed(p, nodemask))
145 return true;
146
147 return false;
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/**
David Rientjesa63d83f2010-08-09 17:19:46 -0700151 * oom_badness - heuristic function to determine which candidate task to kill
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * @p: task struct of which task we should calculate
David Rientjesa63d83f2010-08-09 17:19:46 -0700153 * @totalpages: total present RAM allowed for page allocation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 *
David Rientjesa63d83f2010-08-09 17:19:46 -0700155 * The heuristic for determining which task to kill is made to be as simple and
156 * predictable as possible. The goal is to return the highest value for the
157 * task consuming the most memory to avoid subsequent oom failures.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
David Rientjesa7f638f2012-05-29 15:06:47 -0700159unsigned long oom_badness(struct task_struct *p, struct mem_cgroup *memcg,
160 const nodemask_t *nodemask, unsigned long totalpages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
David Rientjes1e11ad82012-06-08 13:21:26 -0700162 long points;
David Rientjes61eafb02012-06-20 12:52:58 -0700163 long adj;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700164
Johannes Weiner72835c82012-01-12 17:18:32 -0800165 if (oom_unkillable_task(p, memcg, nodemask))
KOSAKI Motohiro26ebc982010-08-09 17:19:37 -0700166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700168 p = find_lock_task_mm(p);
169 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return 0;
171
David Rientjesa9c58b902012-12-11 16:02:54 -0800172 adj = (long)p->signal->oom_score_adj;
David Rientjes61eafb02012-06-20 12:52:58 -0700173 if (adj == OOM_SCORE_ADJ_MIN) {
Michal Hocko5aecc852011-11-15 14:36:07 -0800174 task_unlock(p);
175 return 0;
176 }
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /*
David Rientjesa63d83f2010-08-09 17:19:46 -0700179 * The baseline for the badness score is the proportion of RAM that each
KOSAKI Motohirof755a042011-04-27 15:26:50 -0700180 * task's rss, pagetable and swap space use.
David Rientjesa63d83f2010-08-09 17:19:46 -0700181 */
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800182 points = get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) +
183 atomic_long_read(&p->mm->nr_ptes) + mm_nr_pmds(p->mm);
Andrew Morton97c2c9b82006-04-18 22:20:38 -0700184 task_unlock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /*
David Rientjesa63d83f2010-08-09 17:19:46 -0700187 * Root processes get 3% bonus, just like the __vm_enough_memory()
188 * implementation used by LSMs.
Hugh Dickins7ba34852007-01-05 16:37:03 -0800189 */
David Rientjesa63d83f2010-08-09 17:19:46 -0700190 if (has_capability_noaudit(p, CAP_SYS_ADMIN))
David Rientjes778c14a2014-01-30 15:46:11 -0800191 points -= (points * 3) / 100;
Hugh Dickins7ba34852007-01-05 16:37:03 -0800192
David Rientjes61eafb02012-06-20 12:52:58 -0700193 /* Normalize to oom_score_adj units */
194 adj *= totalpages / 1000;
195 points += adj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
David Rientjesf19e8aa2010-09-22 13:04:52 -0700197 /*
David Rientjesa7f638f2012-05-29 15:06:47 -0700198 * Never return 0 for an eligible task regardless of the root bonus and
199 * oom_score_adj (oom_score_adj can't be OOM_SCORE_ADJ_MIN here).
David Rientjesf19e8aa2010-09-22 13:04:52 -0700200 */
David Rientjes1e11ad82012-06-08 13:21:26 -0700201 return points > 0 ? points : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204/*
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800205 * Determine the type of allocation constraint.
206 */
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800207#ifdef CONFIG_NUMA
David Rientjes6e0fc462015-09-08 15:00:36 -0700208static enum oom_constraint constrained_alloc(struct oom_control *oc,
209 unsigned long *totalpages)
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800210{
Mel Gorman54a6eb52008-04-28 02:12:16 -0700211 struct zone *zone;
Mel Gormandd1a2392008-04-28 02:12:17 -0700212 struct zoneref *z;
David Rientjes6e0fc462015-09-08 15:00:36 -0700213 enum zone_type high_zoneidx = gfp_zone(oc->gfp_mask);
David Rientjesa63d83f2010-08-09 17:19:46 -0700214 bool cpuset_limited = false;
215 int nid;
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800216
David Rientjesa63d83f2010-08-09 17:19:46 -0700217 /* Default to all available memory */
218 *totalpages = totalram_pages + total_swap_pages;
219
David Rientjes6e0fc462015-09-08 15:00:36 -0700220 if (!oc->zonelist)
David Rientjesa63d83f2010-08-09 17:19:46 -0700221 return CONSTRAINT_NONE;
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800222 /*
223 * Reach here only when __GFP_NOFAIL is used. So, we should avoid
224 * to kill current.We have to random task kill in this case.
225 * Hopefully, CONSTRAINT_THISNODE...but no way to handle it, now.
226 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700227 if (oc->gfp_mask & __GFP_THISNODE)
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800228 return CONSTRAINT_NONE;
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800229
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800230 /*
David Rientjesa63d83f2010-08-09 17:19:46 -0700231 * This is not a __GFP_THISNODE allocation, so a truncated nodemask in
232 * the page allocator means a mempolicy is in effect. Cpuset policy
233 * is enforced in get_page_from_freelist().
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800234 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700235 if (oc->nodemask &&
236 !nodes_subset(node_states[N_MEMORY], *oc->nodemask)) {
David Rientjesa63d83f2010-08-09 17:19:46 -0700237 *totalpages = total_swap_pages;
David Rientjes6e0fc462015-09-08 15:00:36 -0700238 for_each_node_mask(nid, *oc->nodemask)
David Rientjesa63d83f2010-08-09 17:19:46 -0700239 *totalpages += node_spanned_pages(nid);
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800240 return CONSTRAINT_MEMORY_POLICY;
David Rientjesa63d83f2010-08-09 17:19:46 -0700241 }
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800242
243 /* Check this allocation failure is caused by cpuset's wall function */
David Rientjes6e0fc462015-09-08 15:00:36 -0700244 for_each_zone_zonelist_nodemask(zone, z, oc->zonelist,
245 high_zoneidx, oc->nodemask)
246 if (!cpuset_zone_allowed(zone, oc->gfp_mask))
David Rientjesa63d83f2010-08-09 17:19:46 -0700247 cpuset_limited = true;
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800248
David Rientjesa63d83f2010-08-09 17:19:46 -0700249 if (cpuset_limited) {
250 *totalpages = total_swap_pages;
251 for_each_node_mask(nid, cpuset_current_mems_allowed)
252 *totalpages += node_spanned_pages(nid);
253 return CONSTRAINT_CPUSET;
254 }
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800255 return CONSTRAINT_NONE;
256}
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800257#else
David Rientjes6e0fc462015-09-08 15:00:36 -0700258static enum oom_constraint constrained_alloc(struct oom_control *oc,
259 unsigned long *totalpages)
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800260{
David Rientjesa63d83f2010-08-09 17:19:46 -0700261 *totalpages = totalram_pages + total_swap_pages;
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800262 return CONSTRAINT_NONE;
263}
264#endif
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800265
David Rientjes6e0fc462015-09-08 15:00:36 -0700266enum oom_scan_t oom_scan_process_thread(struct oom_control *oc,
267 struct task_struct *task, unsigned long totalpages)
David Rientjes462607e2012-07-31 16:43:40 -0700268{
David Rientjes6e0fc462015-09-08 15:00:36 -0700269 if (oom_unkillable_task(task, NULL, oc->nodemask))
David Rientjes462607e2012-07-31 16:43:40 -0700270 return OOM_SCAN_CONTINUE;
271
272 /*
273 * This task already has access to memory reserves and is being killed.
274 * Don't allow any other task to have access to the reserves.
275 */
276 if (test_tsk_thread_flag(task, TIF_MEMDIE)) {
Yaowei Baidb2a0dd2015-11-06 16:28:06 -0800277 if (!is_sysrq_oom(oc))
David Rientjes462607e2012-07-31 16:43:40 -0700278 return OOM_SCAN_ABORT;
279 }
280 if (!task->mm)
281 return OOM_SCAN_CONTINUE;
282
David Rientjese1e12d22012-12-11 16:02:56 -0800283 /*
284 * If task is allocating a lot of memory and has been marked to be
285 * killed first if it triggers an oom, then select it.
286 */
287 if (oom_task_origin(task))
288 return OOM_SCAN_SELECT;
289
Yaowei Baidb2a0dd2015-11-06 16:28:06 -0800290 if (task_will_free_mem(task) && !is_sysrq_oom(oc))
Oleg Nesterov6a2d5672014-12-12 16:56:27 -0800291 return OOM_SCAN_ABORT;
292
David Rientjes462607e2012-07-31 16:43:40 -0700293 return OOM_SCAN_OK;
294}
295
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800296/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * Simple selection loop. We chose the process with the highest
Rusty Russell6b4f2b52013-07-15 11:24:08 +0930298 * number of 'points'. Returns -1 on scan abort.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700300static struct task_struct *select_bad_process(struct oom_control *oc,
301 unsigned int *ppoints, unsigned long totalpages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
David Rientjes3a5dda72011-03-22 16:30:09 -0700303 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct task_struct *chosen = NULL;
David Rientjesa7f638f2012-05-29 15:06:47 -0700305 unsigned long chosen_points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
David Rientjes6b0c81b2012-07-31 16:43:45 -0700307 rcu_read_lock();
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800308 for_each_process_thread(g, p) {
David Rientjesa63d83f2010-08-09 17:19:46 -0700309 unsigned int points;
Paul Jacksona49335c2005-09-06 15:18:09 -0700310
David Rientjes6e0fc462015-09-08 15:00:36 -0700311 switch (oom_scan_process_thread(oc, p, totalpages)) {
David Rientjes462607e2012-07-31 16:43:40 -0700312 case OOM_SCAN_SELECT:
313 chosen = p;
314 chosen_points = ULONG_MAX;
315 /* fall through */
316 case OOM_SCAN_CONTINUE:
Andrey Vagin30e2b412011-03-22 16:30:11 -0700317 continue;
David Rientjes462607e2012-07-31 16:43:40 -0700318 case OOM_SCAN_ABORT:
David Rientjes6b0c81b2012-07-31 16:43:45 -0700319 rcu_read_unlock();
Rusty Russell6b4f2b52013-07-15 11:24:08 +0930320 return (struct task_struct *)(-1UL);
David Rientjes462607e2012-07-31 16:43:40 -0700321 case OOM_SCAN_OK:
322 break;
323 };
David Rientjes6e0fc462015-09-08 15:00:36 -0700324 points = oom_badness(p, NULL, oc->nodemask, totalpages);
David Rientjesd49ad932014-01-23 15:53:34 -0800325 if (!points || points < chosen_points)
326 continue;
327 /* Prefer thread group leaders for display purposes */
328 if (points == chosen_points && thread_group_leader(chosen))
329 continue;
330
331 chosen = p;
332 chosen_points = points;
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800333 }
David Rientjes6b0c81b2012-07-31 16:43:45 -0700334 if (chosen)
335 get_task_struct(chosen);
336 rcu_read_unlock();
Oleg Nesterov972c4ea2006-09-29 02:01:12 -0700337
David Rientjesa7f638f2012-05-29 15:06:47 -0700338 *ppoints = chosen_points * 1000 / totalpages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return chosen;
340}
341
342/**
Randy Dunlap1b578df2008-03-19 17:00:42 -0700343 * dump_tasks - dump current memory state of all system tasks
Wanpeng Lidad75572012-06-20 12:53:01 -0700344 * @memcg: current's memory controller, if constrained
David Rientjese85bfd32010-09-22 13:05:10 -0700345 * @nodemask: nodemask passed to page allocator for mempolicy ooms
Randy Dunlap1b578df2008-03-19 17:00:42 -0700346 *
David Rientjese85bfd32010-09-22 13:05:10 -0700347 * Dumps the current memory state of all eligible tasks. Tasks not in the same
348 * memcg, not in the same cpuset, or bound to a disjoint set of mempolicy nodes
349 * are not shown.
David Rientjesde34d962012-07-31 16:42:56 -0700350 * State information includes task's pid, uid, tgid, vm size, rss, nr_ptes,
351 * swapents, oom_score_adj value, and name.
David Rientjesfef1bdd2008-02-07 00:14:07 -0800352 */
Johannes Weiner2314b422014-12-10 15:44:33 -0800353static void dump_tasks(struct mem_cgroup *memcg, const nodemask_t *nodemask)
David Rientjesfef1bdd2008-02-07 00:14:07 -0800354{
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700355 struct task_struct *p;
356 struct task_struct *task;
David Rientjesfef1bdd2008-02-07 00:14:07 -0800357
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800358 pr_info("[ pid ] uid tgid total_vm rss nr_ptes nr_pmds swapents oom_score_adj name\n");
David Rientjes6b0c81b2012-07-31 16:43:45 -0700359 rcu_read_lock();
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700360 for_each_process(p) {
Johannes Weiner72835c82012-01-12 17:18:32 -0800361 if (oom_unkillable_task(p, memcg, nodemask))
David Rientjesfef1bdd2008-02-07 00:14:07 -0800362 continue;
363
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700364 task = find_lock_task_mm(p);
365 if (!task) {
David Rientjes6d2661e2009-05-28 14:34:19 -0700366 /*
David Rientjes74ab7f12010-08-09 17:18:46 -0700367 * This is a kthread or all of p's threads have already
368 * detached their mm's. There's no need to report
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700369 * them; they can't be oom killed anyway.
David Rientjes6d2661e2009-05-28 14:34:19 -0700370 */
David Rientjes6d2661e2009-05-28 14:34:19 -0700371 continue;
372 }
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700373
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800374 pr_info("[%5d] %5d %5d %8lu %8lu %7ld %7ld %8lu %5hd %s\n",
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800375 task->pid, from_kuid(&init_user_ns, task_uid(task)),
376 task->tgid, task->mm->total_vm, get_mm_rss(task->mm),
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800377 atomic_long_read(&task->mm->nr_ptes),
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800378 mm_nr_pmds(task->mm),
David Rientjesde34d962012-07-31 16:42:56 -0700379 get_mm_counter(task->mm, MM_SWAPENTS),
David Rientjesa63d83f2010-08-09 17:19:46 -0700380 task->signal->oom_score_adj, task->comm);
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700381 task_unlock(task);
382 }
David Rientjes6b0c81b2012-07-31 16:43:45 -0700383 rcu_read_unlock();
David Rientjesfef1bdd2008-02-07 00:14:07 -0800384}
385
David Rientjes6e0fc462015-09-08 15:00:36 -0700386static void dump_header(struct oom_control *oc, struct task_struct *p,
387 struct mem_cgroup *memcg)
David Rientjes1b604d72009-12-14 17:57:47 -0800388{
Vlastimil Babkaa0795cd2016-03-15 14:56:05 -0700389 pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, "
390 "oom_score_adj=%hd\n",
391 current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order,
David Rientjesa63d83f2010-08-09 17:19:46 -0700392 current->signal->oom_score_adj);
Vlastimil Babkaa0795cd2016-03-15 14:56:05 -0700393
David Rientjesda39da32015-11-05 18:48:05 -0800394 cpuset_print_current_mems_allowed();
David Rientjes1b604d72009-12-14 17:57:47 -0800395 dump_stack();
Sha Zhengju58cf1882013-02-22 16:32:05 -0800396 if (memcg)
397 mem_cgroup_print_oom_info(memcg, p);
398 else
399 show_mem(SHOW_MEM_FILTER_NODES);
David Rientjes1b604d72009-12-14 17:57:47 -0800400 if (sysctl_oom_dump_tasks)
David Rientjes6e0fc462015-09-08 15:00:36 -0700401 dump_tasks(memcg, oc->nodemask);
David Rientjes1b604d72009-12-14 17:57:47 -0800402}
403
Michal Hocko5695be12014-10-20 18:12:32 +0200404/*
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800405 * Number of OOM victims in flight
Michal Hocko5695be12014-10-20 18:12:32 +0200406 */
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800407static atomic_t oom_victims = ATOMIC_INIT(0);
408static DECLARE_WAIT_QUEUE_HEAD(oom_victims_wait);
Michal Hocko5695be12014-10-20 18:12:32 +0200409
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800410bool oom_killer_disabled __read_mostly;
Michal Hocko5695be12014-10-20 18:12:32 +0200411
Michal Hocko49550b62015-02-11 15:26:12 -0800412/**
Johannes Weiner16e95192015-06-24 16:57:07 -0700413 * mark_oom_victim - mark the given task as OOM victim
Michal Hocko49550b62015-02-11 15:26:12 -0800414 * @tsk: task to mark
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800415 *
Johannes Weinerdc564012015-06-24 16:57:19 -0700416 * Has to be called with oom_lock held and never after
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800417 * oom has been disabled already.
Michal Hocko49550b62015-02-11 15:26:12 -0800418 */
Johannes Weiner16e95192015-06-24 16:57:07 -0700419void mark_oom_victim(struct task_struct *tsk)
Michal Hocko49550b62015-02-11 15:26:12 -0800420{
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800421 WARN_ON(oom_killer_disabled);
422 /* OOM killer might race with memcg OOM */
423 if (test_and_set_tsk_thread_flag(tsk, TIF_MEMDIE))
424 return;
Michal Hocko63a8ca92015-02-11 15:26:15 -0800425 /*
426 * Make sure that the task is woken up from uninterruptible sleep
427 * if it is frozen because OOM killer wouldn't be able to free
428 * any memory and livelock. freezing_slow_path will tell the freezer
429 * that TIF_MEMDIE tasks should be ignored.
430 */
431 __thaw_task(tsk);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800432 atomic_inc(&oom_victims);
Michal Hocko49550b62015-02-11 15:26:12 -0800433}
434
435/**
Johannes Weiner16e95192015-06-24 16:57:07 -0700436 * exit_oom_victim - note the exit of an OOM victim
Michal Hocko49550b62015-02-11 15:26:12 -0800437 */
Johannes Weiner16e95192015-06-24 16:57:07 -0700438void exit_oom_victim(void)
Michal Hocko49550b62015-02-11 15:26:12 -0800439{
Johannes Weiner46402772015-06-24 16:57:10 -0700440 clear_thread_flag(TIF_MEMDIE);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800441
Johannes Weinerc38f1022015-06-24 16:57:13 -0700442 if (!atomic_dec_return(&oom_victims))
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800443 wake_up_all(&oom_victims_wait);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800444}
445
446/**
447 * oom_killer_disable - disable OOM killer
448 *
449 * Forces all page allocations to fail rather than trigger OOM killer.
450 * Will block and wait until all OOM victims are killed.
451 *
452 * The function cannot be called when there are runnable user tasks because
453 * the userspace would see unexpected allocation failures as a result. Any
454 * new usage of this function should be consulted with MM people.
455 *
456 * Returns true if successful and false if the OOM killer cannot be
457 * disabled.
458 */
459bool oom_killer_disable(void)
460{
461 /*
462 * Make sure to not race with an ongoing OOM killer
463 * and that the current is not the victim.
464 */
Johannes Weinerdc564012015-06-24 16:57:19 -0700465 mutex_lock(&oom_lock);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800466 if (test_thread_flag(TIF_MEMDIE)) {
Johannes Weinerdc564012015-06-24 16:57:19 -0700467 mutex_unlock(&oom_lock);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800468 return false;
469 }
470
471 oom_killer_disabled = true;
Johannes Weinerdc564012015-06-24 16:57:19 -0700472 mutex_unlock(&oom_lock);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800473
474 wait_event(oom_victims_wait, !atomic_read(&oom_victims));
475
476 return true;
477}
478
479/**
480 * oom_killer_enable - enable OOM killer
481 */
482void oom_killer_enable(void)
483{
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800484 oom_killer_disabled = false;
Michal Hocko49550b62015-02-11 15:26:12 -0800485}
486
Oleg Nesterov4d7b3392015-11-05 18:48:26 -0800487/*
488 * task->mm can be NULL if the task is the exited group leader. So to
489 * determine whether the task is using a particular mm, we examine all the
490 * task's threads: if one of those is using this mm then this task was also
491 * using it.
492 */
493static bool process_shares_mm(struct task_struct *p, struct mm_struct *mm)
494{
495 struct task_struct *t;
496
497 for_each_thread(p, t) {
498 struct mm_struct *t_mm = READ_ONCE(t->mm);
499 if (t_mm)
500 return t_mm == mm;
501 }
502 return false;
503}
504
KOSAKI Motohiro3b4798c2009-12-15 16:45:32 -0800505#define K(x) ((x) << (PAGE_SHIFT-10))
David Rientjes6b0c81b2012-07-31 16:43:45 -0700506/*
507 * Must be called while holding a reference to p, which will be released upon
508 * returning.
509 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700510void oom_kill_process(struct oom_control *oc, struct task_struct *p,
David Rientjes9cbb78b2012-07-31 16:43:44 -0700511 unsigned int points, unsigned long totalpages,
David Rientjes6e0fc462015-09-08 15:00:36 -0700512 struct mem_cgroup *memcg, const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Linus Torvalds52d3c032011-03-14 15:17:07 -0700514 struct task_struct *victim = p;
David Rientjes5e9d8342010-08-09 17:18:51 -0700515 struct task_struct *child;
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800516 struct task_struct *t;
David Rientjes647f2bd2012-03-21 16:33:46 -0700517 struct mm_struct *mm;
Linus Torvalds52d3c032011-03-14 15:17:07 -0700518 unsigned int victim_points = 0;
David Rientjesdc3f21e2012-03-21 16:33:47 -0700519 static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL,
520 DEFAULT_RATELIMIT_BURST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Nick Piggin50ec3bb2006-09-25 23:31:29 -0700522 /*
523 * If the task is already exiting, don't alarm the sysadmin or kill
524 * its children or threads, just set TIF_MEMDIE so it can die quickly
525 */
Michal Hocko83363b92015-02-11 15:24:56 -0800526 task_lock(p);
527 if (p->mm && task_will_free_mem(p)) {
Johannes Weiner16e95192015-06-24 16:57:07 -0700528 mark_oom_victim(p);
Michal Hocko83363b92015-02-11 15:24:56 -0800529 task_unlock(p);
David Rientjes6b0c81b2012-07-31 16:43:45 -0700530 put_task_struct(p);
David Rientjes2a1c9b12012-03-21 16:33:46 -0700531 return;
Nick Piggin50ec3bb2006-09-25 23:31:29 -0700532 }
Michal Hocko83363b92015-02-11 15:24:56 -0800533 task_unlock(p);
Nick Piggin50ec3bb2006-09-25 23:31:29 -0700534
David Rientjesdc3f21e2012-03-21 16:33:47 -0700535 if (__ratelimit(&oom_rs))
David Rientjes6e0fc462015-09-08 15:00:36 -0700536 dump_header(oc, p, memcg);
David Rientjes8447d952012-03-21 16:33:47 -0700537
Wang Longf0d66472015-06-24 16:58:01 -0700538 pr_err("%s: Kill process %d (%s) score %u or sacrifice child\n",
David Rientjes5e9d8342010-08-09 17:18:51 -0700539 message, task_pid_nr(p), p->comm, points);
Nick Pigginf3af38d2006-12-06 20:31:51 -0800540
David Rientjes5e9d8342010-08-09 17:18:51 -0700541 /*
542 * If any of p's children has a different mm and is eligible for kill,
David Rientjes11239832011-07-25 17:12:17 -0700543 * the one with the highest oom_badness() score is sacrificed for its
David Rientjes5e9d8342010-08-09 17:18:51 -0700544 * parent. This attempts to lose the minimal amount of work done while
545 * still freeing memory.
546 */
David Rientjes6b0c81b2012-07-31 16:43:45 -0700547 read_lock(&tasklist_lock);
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800548 for_each_thread(p, t) {
David Rientjes5e9d8342010-08-09 17:18:51 -0700549 list_for_each_entry(child, &t->children, sibling) {
David Rientjesa63d83f2010-08-09 17:19:46 -0700550 unsigned int child_points;
David Rientjes5e9d8342010-08-09 17:18:51 -0700551
Oleg Nesterov4d7b3392015-11-05 18:48:26 -0800552 if (process_shares_mm(child, p->mm))
David Rientjesedd45542011-03-22 16:30:12 -0700553 continue;
David Rientjesa63d83f2010-08-09 17:19:46 -0700554 /*
555 * oom_badness() returns 0 if the thread is unkillable
556 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700557 child_points = oom_badness(child, memcg, oc->nodemask,
David Rientjesa63d83f2010-08-09 17:19:46 -0700558 totalpages);
David Rientjes5e9d8342010-08-09 17:18:51 -0700559 if (child_points > victim_points) {
David Rientjes6b0c81b2012-07-31 16:43:45 -0700560 put_task_struct(victim);
David Rientjes5e9d8342010-08-09 17:18:51 -0700561 victim = child;
562 victim_points = child_points;
David Rientjes6b0c81b2012-07-31 16:43:45 -0700563 get_task_struct(victim);
David Rientjes5e9d8342010-08-09 17:18:51 -0700564 }
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700565 }
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800566 }
David Rientjes6b0c81b2012-07-31 16:43:45 -0700567 read_unlock(&tasklist_lock);
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700568
David Rientjes6b0c81b2012-07-31 16:43:45 -0700569 p = find_lock_task_mm(victim);
570 if (!p) {
David Rientjes6b0c81b2012-07-31 16:43:45 -0700571 put_task_struct(victim);
David Rientjes647f2bd2012-03-21 16:33:46 -0700572 return;
David Rientjes6b0c81b2012-07-31 16:43:45 -0700573 } else if (victim != p) {
574 get_task_struct(p);
575 put_task_struct(victim);
576 victim = p;
577 }
David Rientjes647f2bd2012-03-21 16:33:46 -0700578
Tetsuo Handa880b76892015-11-05 18:47:51 -0800579 /* Get a reference to safely compare mm after task_unlock(victim) */
David Rientjes647f2bd2012-03-21 16:33:46 -0700580 mm = victim->mm;
Tetsuo Handa880b76892015-11-05 18:47:51 -0800581 atomic_inc(&mm->mm_count);
Tetsuo Handa426fb5e2015-11-05 18:47:44 -0800582 /*
583 * We should send SIGKILL before setting TIF_MEMDIE in order to prevent
584 * the OOM victim from depleting the memory reserves from the user
585 * space under its control.
586 */
587 do_send_sig_info(SIGKILL, SEND_SIG_FORCED, victim, true);
Johannes Weiner16e95192015-06-24 16:57:07 -0700588 mark_oom_victim(victim);
Jerome Marchandeca56ff2016-01-14 15:19:26 -0800589 pr_err("Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB\n",
David Rientjes647f2bd2012-03-21 16:33:46 -0700590 task_pid_nr(victim), victim->comm, K(victim->mm->total_vm),
591 K(get_mm_counter(victim->mm, MM_ANONPAGES)),
Jerome Marchandeca56ff2016-01-14 15:19:26 -0800592 K(get_mm_counter(victim->mm, MM_FILEPAGES)),
593 K(get_mm_counter(victim->mm, MM_SHMEMPAGES)));
David Rientjes647f2bd2012-03-21 16:33:46 -0700594 task_unlock(victim);
595
596 /*
597 * Kill all user processes sharing victim->mm in other thread groups, if
598 * any. They don't get access to memory reserves, though, to avoid
599 * depletion of all memory. This prevents mm->mmap_sem livelock when an
600 * oom killed thread cannot exit because it requires the semaphore and
601 * its contended by another thread trying to allocate memory itself.
602 * That thread will now get access to memory reserves since it has a
603 * pending fatal signal.
604 */
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800605 rcu_read_lock();
Oleg Nesterovc3190252015-11-05 18:48:23 -0800606 for_each_process(p) {
Oleg Nesterov4d7b3392015-11-05 18:48:26 -0800607 if (!process_shares_mm(p, mm))
Oleg Nesterovc3190252015-11-05 18:48:23 -0800608 continue;
609 if (same_thread_group(p, victim))
610 continue;
611 if (unlikely(p->flags & PF_KTHREAD))
612 continue;
Chen Jiea2b829d2015-12-11 13:41:00 -0800613 if (is_global_init(p))
614 continue;
Oleg Nesterovc3190252015-11-05 18:48:23 -0800615 if (p->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
616 continue;
David Rientjes647f2bd2012-03-21 16:33:46 -0700617
Oleg Nesterovc3190252015-11-05 18:48:23 -0800618 do_send_sig_info(SIGKILL, SEND_SIG_FORCED, p, true);
619 }
David Rientjes6b0c81b2012-07-31 16:43:45 -0700620 rcu_read_unlock();
David Rientjes647f2bd2012-03-21 16:33:46 -0700621
Tetsuo Handa880b76892015-11-05 18:47:51 -0800622 mmdrop(mm);
David Rientjes6b0c81b2012-07-31 16:43:45 -0700623 put_task_struct(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
David Rientjes647f2bd2012-03-21 16:33:46 -0700625#undef K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
David Rientjes309ed882010-08-09 17:18:54 -0700627/*
628 * Determines whether the kernel must panic because of the panic_on_oom sysctl.
629 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700630void check_panic_on_oom(struct oom_control *oc, enum oom_constraint constraint,
Balasubramani Vivekanandan2415b9f2015-04-14 15:48:18 -0700631 struct mem_cgroup *memcg)
David Rientjes309ed882010-08-09 17:18:54 -0700632{
633 if (likely(!sysctl_panic_on_oom))
634 return;
635 if (sysctl_panic_on_oom != 2) {
636 /*
637 * panic_on_oom == 1 only affects CONSTRAINT_NONE, the kernel
638 * does not panic for cpuset, mempolicy, or memcg allocation
639 * failures.
640 */
641 if (constraint != CONSTRAINT_NONE)
642 return;
643 }
David Rientjes071a4be2015-09-08 15:00:42 -0700644 /* Do not panic for oom kills triggered by sysrq */
Yaowei Baidb2a0dd2015-11-06 16:28:06 -0800645 if (is_sysrq_oom(oc))
David Rientjes071a4be2015-09-08 15:00:42 -0700646 return;
David Rientjes6e0fc462015-09-08 15:00:36 -0700647 dump_header(oc, NULL, memcg);
David Rientjes309ed882010-08-09 17:18:54 -0700648 panic("Out of memory: %s panic_on_oom is enabled\n",
649 sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide");
650}
651
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700652static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
653
654int register_oom_notifier(struct notifier_block *nb)
655{
656 return blocking_notifier_chain_register(&oom_notify_list, nb);
657}
658EXPORT_SYMBOL_GPL(register_oom_notifier);
659
660int unregister_oom_notifier(struct notifier_block *nb)
661{
662 return blocking_notifier_chain_unregister(&oom_notify_list, nb);
663}
664EXPORT_SYMBOL_GPL(unregister_oom_notifier);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666/**
David Rientjes6e0fc462015-09-08 15:00:36 -0700667 * out_of_memory - kill the "best" process when we run out of memory
668 * @oc: pointer to struct oom_control
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 *
670 * If we run out of memory, we have the choice between either
671 * killing a random task (bad), letting the system crash (worse)
672 * OR try to be smart about which process to kill. Note that we
673 * don't have to be perfect here, we just have to be good.
674 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700675bool out_of_memory(struct oom_control *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
David Rientjes0aad4b32010-08-09 17:18:59 -0700677 struct task_struct *p;
David Rientjesa63d83f2010-08-09 17:19:46 -0700678 unsigned long totalpages;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700679 unsigned long freed = 0;
David Rientjes9cbb78b2012-07-31 16:43:44 -0700680 unsigned int uninitialized_var(points);
David Rientjese3658932010-08-09 17:18:55 -0700681 enum oom_constraint constraint = CONSTRAINT_NONE;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700682
Johannes Weinerdc564012015-06-24 16:57:19 -0700683 if (oom_killer_disabled)
684 return false;
685
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700686 blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
687 if (freed > 0)
688 /* Got some memory back in the last second. */
David Rientjes75e8f8b2015-09-08 15:00:47 -0700689 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
David Rientjes7b98c2e2010-08-09 17:18:48 -0700691 /*
David Rientjes9ff48682012-12-11 16:01:30 -0800692 * If current has a pending SIGKILL or is exiting, then automatically
693 * select it. The goal is to allow it to allocate so that it may
694 * quickly exit and free its memory.
Tetsuo Handad7a94e72015-02-11 15:24:54 -0800695 *
696 * But don't select if current has already released its mm and cleared
697 * TIF_MEMDIE flag at exit_mm(), otherwise an OOM livelock may occur.
David Rientjes7b98c2e2010-08-09 17:18:48 -0700698 */
Tetsuo Handad7a94e72015-02-11 15:24:54 -0800699 if (current->mm &&
700 (fatal_signal_pending(current) || task_will_free_mem(current))) {
Johannes Weiner16e95192015-06-24 16:57:07 -0700701 mark_oom_victim(current);
David Rientjes75e8f8b2015-09-08 15:00:47 -0700702 return true;
David Rientjes7b98c2e2010-08-09 17:18:48 -0700703 }
704
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800705 /*
706 * Check if there were limitations on the allocation (only relevant for
707 * NUMA) that may require different handling.
708 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700709 constraint = constrained_alloc(oc, &totalpages);
710 if (constraint != CONSTRAINT_MEMORY_POLICY)
711 oc->nodemask = NULL;
712 check_panic_on_oom(oc, constraint, NULL);
David Rientjes0aad4b32010-08-09 17:18:59 -0700713
David Rientjes121d1ba2012-07-31 16:42:55 -0700714 if (sysctl_oom_kill_allocating_task && current->mm &&
David Rientjes6e0fc462015-09-08 15:00:36 -0700715 !oom_unkillable_task(current, NULL, oc->nodemask) &&
David Rientjes121d1ba2012-07-31 16:42:55 -0700716 current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {
David Rientjes6b0c81b2012-07-31 16:43:45 -0700717 get_task_struct(current);
David Rientjes6e0fc462015-09-08 15:00:36 -0700718 oom_kill_process(oc, current, 0, totalpages, NULL,
David Rientjes2a1c9b12012-03-21 16:33:46 -0700719 "Out of memory (oom_kill_allocating_task)");
David Rientjes75e8f8b2015-09-08 15:00:47 -0700720 return true;
David Rientjes0aad4b32010-08-09 17:18:59 -0700721 }
722
David Rientjes6e0fc462015-09-08 15:00:36 -0700723 p = select_bad_process(oc, &points, totalpages);
David Rientjes0aad4b32010-08-09 17:18:59 -0700724 /* Found nothing?!?! Either we hang forever, or we panic. */
Yaowei Baidb2a0dd2015-11-06 16:28:06 -0800725 if (!p && !is_sysrq_oom(oc)) {
David Rientjes6e0fc462015-09-08 15:00:36 -0700726 dump_header(oc, NULL, NULL);
David Rientjes0aad4b32010-08-09 17:18:59 -0700727 panic("Out of memory and no killable processes...\n");
728 }
David Rientjes071a4be2015-09-08 15:00:42 -0700729 if (p && p != (void *)-1UL) {
David Rientjes6e0fc462015-09-08 15:00:36 -0700730 oom_kill_process(oc, p, points, totalpages, NULL,
731 "Out of memory");
David Rientjes75e8f8b2015-09-08 15:00:47 -0700732 /*
733 * Give the killed process a good chance to exit before trying
734 * to allocate memory again.
735 */
David Rientjes4f774b92012-07-31 16:42:37 -0700736 schedule_timeout_killable(1);
David Rientjes75e8f8b2015-09-08 15:00:47 -0700737 }
Johannes Weinerdc564012015-06-24 16:57:19 -0700738 return true;
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800739}
740
David Rientjese3658932010-08-09 17:18:55 -0700741/*
742 * The pagefault handler calls here because it is out of memory, so kill a
David Rientjesefacd022012-12-12 13:52:06 -0800743 * memory-hogging task. If any populated zone has ZONE_OOM_LOCKED set, a
744 * parallel oom killing is already in progress so do nothing.
David Rientjese3658932010-08-09 17:18:55 -0700745 */
746void pagefault_out_of_memory(void)
747{
David Rientjes6e0fc462015-09-08 15:00:36 -0700748 struct oom_control oc = {
749 .zonelist = NULL,
750 .nodemask = NULL,
751 .gfp_mask = 0,
752 .order = 0,
David Rientjes6e0fc462015-09-08 15:00:36 -0700753 };
754
Johannes Weiner49426422013-10-16 13:46:59 -0700755 if (mem_cgroup_oom_synchronize(true))
Johannes Weinerdc564012015-06-24 16:57:19 -0700756 return;
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700757
Johannes Weinerdc564012015-06-24 16:57:19 -0700758 if (!mutex_trylock(&oom_lock))
759 return;
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800760
David Rientjes6e0fc462015-09-08 15:00:36 -0700761 if (!out_of_memory(&oc)) {
Johannes Weinerdc564012015-06-24 16:57:19 -0700762 /*
763 * There shouldn't be any user tasks runnable while the
764 * OOM killer is disabled, so the current task has to
765 * be a racing OOM victim for which oom_killer_disable()
766 * is waiting for.
767 */
768 WARN_ON(test_thread_flag(TIF_MEMDIE));
David Rientjese3658932010-08-09 17:18:55 -0700769 }
Johannes Weinerdc564012015-06-24 16:57:19 -0700770
771 mutex_unlock(&oom_lock);
David Rientjese3658932010-08-09 17:18:55 -0700772}