blob: c12680993ff334560dbfc84ffcee4e0bd0487650 [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{
389 pr_warning("%s invoked oom-killer: gfp_mask=0x%x, order=%d, "
David Rientjesa9c58b902012-12-11 16:02:54 -0800390 "oom_score_adj=%hd\n",
David Rientjes6e0fc462015-09-08 15:00:36 -0700391 current->comm, oc->gfp_mask, oc->order,
David Rientjesa63d83f2010-08-09 17:19:46 -0700392 current->signal->oom_score_adj);
David Rientjesda39da32015-11-05 18:48:05 -0800393 cpuset_print_current_mems_allowed();
David Rientjes1b604d72009-12-14 17:57:47 -0800394 dump_stack();
Sha Zhengju58cf1882013-02-22 16:32:05 -0800395 if (memcg)
396 mem_cgroup_print_oom_info(memcg, p);
397 else
398 show_mem(SHOW_MEM_FILTER_NODES);
David Rientjes1b604d72009-12-14 17:57:47 -0800399 if (sysctl_oom_dump_tasks)
David Rientjes6e0fc462015-09-08 15:00:36 -0700400 dump_tasks(memcg, oc->nodemask);
David Rientjes1b604d72009-12-14 17:57:47 -0800401}
402
Michal Hocko5695be12014-10-20 18:12:32 +0200403/*
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800404 * Number of OOM victims in flight
Michal Hocko5695be12014-10-20 18:12:32 +0200405 */
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800406static atomic_t oom_victims = ATOMIC_INIT(0);
407static DECLARE_WAIT_QUEUE_HEAD(oom_victims_wait);
Michal Hocko5695be12014-10-20 18:12:32 +0200408
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800409bool oom_killer_disabled __read_mostly;
Michal Hocko5695be12014-10-20 18:12:32 +0200410
Michal Hocko49550b62015-02-11 15:26:12 -0800411/**
Johannes Weiner16e95192015-06-24 16:57:07 -0700412 * mark_oom_victim - mark the given task as OOM victim
Michal Hocko49550b62015-02-11 15:26:12 -0800413 * @tsk: task to mark
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800414 *
Johannes Weinerdc564012015-06-24 16:57:19 -0700415 * Has to be called with oom_lock held and never after
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800416 * oom has been disabled already.
Michal Hocko49550b62015-02-11 15:26:12 -0800417 */
Johannes Weiner16e95192015-06-24 16:57:07 -0700418void mark_oom_victim(struct task_struct *tsk)
Michal Hocko49550b62015-02-11 15:26:12 -0800419{
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800420 WARN_ON(oom_killer_disabled);
421 /* OOM killer might race with memcg OOM */
422 if (test_and_set_tsk_thread_flag(tsk, TIF_MEMDIE))
423 return;
Michal Hocko63a8ca92015-02-11 15:26:15 -0800424 /*
425 * Make sure that the task is woken up from uninterruptible sleep
426 * if it is frozen because OOM killer wouldn't be able to free
427 * any memory and livelock. freezing_slow_path will tell the freezer
428 * that TIF_MEMDIE tasks should be ignored.
429 */
430 __thaw_task(tsk);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800431 atomic_inc(&oom_victims);
Michal Hocko49550b62015-02-11 15:26:12 -0800432}
433
434/**
Johannes Weiner16e95192015-06-24 16:57:07 -0700435 * exit_oom_victim - note the exit of an OOM victim
Michal Hocko49550b62015-02-11 15:26:12 -0800436 */
Johannes Weiner16e95192015-06-24 16:57:07 -0700437void exit_oom_victim(void)
Michal Hocko49550b62015-02-11 15:26:12 -0800438{
Johannes Weiner46402772015-06-24 16:57:10 -0700439 clear_thread_flag(TIF_MEMDIE);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800440
Johannes Weinerc38f1022015-06-24 16:57:13 -0700441 if (!atomic_dec_return(&oom_victims))
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800442 wake_up_all(&oom_victims_wait);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800443}
444
445/**
446 * oom_killer_disable - disable OOM killer
447 *
448 * Forces all page allocations to fail rather than trigger OOM killer.
449 * Will block and wait until all OOM victims are killed.
450 *
451 * The function cannot be called when there are runnable user tasks because
452 * the userspace would see unexpected allocation failures as a result. Any
453 * new usage of this function should be consulted with MM people.
454 *
455 * Returns true if successful and false if the OOM killer cannot be
456 * disabled.
457 */
458bool oom_killer_disable(void)
459{
460 /*
461 * Make sure to not race with an ongoing OOM killer
462 * and that the current is not the victim.
463 */
Johannes Weinerdc564012015-06-24 16:57:19 -0700464 mutex_lock(&oom_lock);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800465 if (test_thread_flag(TIF_MEMDIE)) {
Johannes Weinerdc564012015-06-24 16:57:19 -0700466 mutex_unlock(&oom_lock);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800467 return false;
468 }
469
470 oom_killer_disabled = true;
Johannes Weinerdc564012015-06-24 16:57:19 -0700471 mutex_unlock(&oom_lock);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800472
473 wait_event(oom_victims_wait, !atomic_read(&oom_victims));
474
475 return true;
476}
477
478/**
479 * oom_killer_enable - enable OOM killer
480 */
481void oom_killer_enable(void)
482{
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800483 oom_killer_disabled = false;
Michal Hocko49550b62015-02-11 15:26:12 -0800484}
485
Oleg Nesterov4d7b3392015-11-05 18:48:26 -0800486/*
487 * task->mm can be NULL if the task is the exited group leader. So to
488 * determine whether the task is using a particular mm, we examine all the
489 * task's threads: if one of those is using this mm then this task was also
490 * using it.
491 */
492static bool process_shares_mm(struct task_struct *p, struct mm_struct *mm)
493{
494 struct task_struct *t;
495
496 for_each_thread(p, t) {
497 struct mm_struct *t_mm = READ_ONCE(t->mm);
498 if (t_mm)
499 return t_mm == mm;
500 }
501 return false;
502}
503
KOSAKI Motohiro3b4798c2009-12-15 16:45:32 -0800504#define K(x) ((x) << (PAGE_SHIFT-10))
David Rientjes6b0c81b2012-07-31 16:43:45 -0700505/*
506 * Must be called while holding a reference to p, which will be released upon
507 * returning.
508 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700509void oom_kill_process(struct oom_control *oc, struct task_struct *p,
David Rientjes9cbb78b2012-07-31 16:43:44 -0700510 unsigned int points, unsigned long totalpages,
David Rientjes6e0fc462015-09-08 15:00:36 -0700511 struct mem_cgroup *memcg, const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Linus Torvalds52d3c032011-03-14 15:17:07 -0700513 struct task_struct *victim = p;
David Rientjes5e9d8342010-08-09 17:18:51 -0700514 struct task_struct *child;
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800515 struct task_struct *t;
David Rientjes647f2bd2012-03-21 16:33:46 -0700516 struct mm_struct *mm;
Linus Torvalds52d3c032011-03-14 15:17:07 -0700517 unsigned int victim_points = 0;
David Rientjesdc3f21e2012-03-21 16:33:47 -0700518 static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL,
519 DEFAULT_RATELIMIT_BURST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Nick Piggin50ec3bb2006-09-25 23:31:29 -0700521 /*
522 * If the task is already exiting, don't alarm the sysadmin or kill
523 * its children or threads, just set TIF_MEMDIE so it can die quickly
524 */
Michal Hocko83363b92015-02-11 15:24:56 -0800525 task_lock(p);
526 if (p->mm && task_will_free_mem(p)) {
Johannes Weiner16e95192015-06-24 16:57:07 -0700527 mark_oom_victim(p);
Michal Hocko83363b92015-02-11 15:24:56 -0800528 task_unlock(p);
David Rientjes6b0c81b2012-07-31 16:43:45 -0700529 put_task_struct(p);
David Rientjes2a1c9b12012-03-21 16:33:46 -0700530 return;
Nick Piggin50ec3bb2006-09-25 23:31:29 -0700531 }
Michal Hocko83363b92015-02-11 15:24:56 -0800532 task_unlock(p);
Nick Piggin50ec3bb2006-09-25 23:31:29 -0700533
David Rientjesdc3f21e2012-03-21 16:33:47 -0700534 if (__ratelimit(&oom_rs))
David Rientjes6e0fc462015-09-08 15:00:36 -0700535 dump_header(oc, p, memcg);
David Rientjes8447d952012-03-21 16:33:47 -0700536
Wang Longf0d66472015-06-24 16:58:01 -0700537 pr_err("%s: Kill process %d (%s) score %u or sacrifice child\n",
David Rientjes5e9d8342010-08-09 17:18:51 -0700538 message, task_pid_nr(p), p->comm, points);
Nick Pigginf3af38d2006-12-06 20:31:51 -0800539
David Rientjes5e9d8342010-08-09 17:18:51 -0700540 /*
541 * If any of p's children has a different mm and is eligible for kill,
David Rientjes11239832011-07-25 17:12:17 -0700542 * the one with the highest oom_badness() score is sacrificed for its
David Rientjes5e9d8342010-08-09 17:18:51 -0700543 * parent. This attempts to lose the minimal amount of work done while
544 * still freeing memory.
545 */
David Rientjes6b0c81b2012-07-31 16:43:45 -0700546 read_lock(&tasklist_lock);
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800547 for_each_thread(p, t) {
David Rientjes5e9d8342010-08-09 17:18:51 -0700548 list_for_each_entry(child, &t->children, sibling) {
David Rientjesa63d83f2010-08-09 17:19:46 -0700549 unsigned int child_points;
David Rientjes5e9d8342010-08-09 17:18:51 -0700550
Oleg Nesterov4d7b3392015-11-05 18:48:26 -0800551 if (process_shares_mm(child, p->mm))
David Rientjesedd45542011-03-22 16:30:12 -0700552 continue;
David Rientjesa63d83f2010-08-09 17:19:46 -0700553 /*
554 * oom_badness() returns 0 if the thread is unkillable
555 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700556 child_points = oom_badness(child, memcg, oc->nodemask,
David Rientjesa63d83f2010-08-09 17:19:46 -0700557 totalpages);
David Rientjes5e9d8342010-08-09 17:18:51 -0700558 if (child_points > victim_points) {
David Rientjes6b0c81b2012-07-31 16:43:45 -0700559 put_task_struct(victim);
David Rientjes5e9d8342010-08-09 17:18:51 -0700560 victim = child;
561 victim_points = child_points;
David Rientjes6b0c81b2012-07-31 16:43:45 -0700562 get_task_struct(victim);
David Rientjes5e9d8342010-08-09 17:18:51 -0700563 }
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700564 }
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800565 }
David Rientjes6b0c81b2012-07-31 16:43:45 -0700566 read_unlock(&tasklist_lock);
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700567
David Rientjes6b0c81b2012-07-31 16:43:45 -0700568 p = find_lock_task_mm(victim);
569 if (!p) {
David Rientjes6b0c81b2012-07-31 16:43:45 -0700570 put_task_struct(victim);
David Rientjes647f2bd2012-03-21 16:33:46 -0700571 return;
David Rientjes6b0c81b2012-07-31 16:43:45 -0700572 } else if (victim != p) {
573 get_task_struct(p);
574 put_task_struct(victim);
575 victim = p;
576 }
David Rientjes647f2bd2012-03-21 16:33:46 -0700577
Tetsuo Handa880b7682015-11-05 18:47:51 -0800578 /* Get a reference to safely compare mm after task_unlock(victim) */
David Rientjes647f2bd2012-03-21 16:33:46 -0700579 mm = victim->mm;
Tetsuo Handa880b7682015-11-05 18:47:51 -0800580 atomic_inc(&mm->mm_count);
Tetsuo Handa426fb5e2015-11-05 18:47:44 -0800581 /*
582 * We should send SIGKILL before setting TIF_MEMDIE in order to prevent
583 * the OOM victim from depleting the memory reserves from the user
584 * space under its control.
585 */
586 do_send_sig_info(SIGKILL, SEND_SIG_FORCED, victim, true);
Johannes Weiner16e95192015-06-24 16:57:07 -0700587 mark_oom_victim(victim);
David Rientjes647f2bd2012-03-21 16:33:46 -0700588 pr_err("Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB\n",
589 task_pid_nr(victim), victim->comm, K(victim->mm->total_vm),
590 K(get_mm_counter(victim->mm, MM_ANONPAGES)),
591 K(get_mm_counter(victim->mm, MM_FILEPAGES)));
592 task_unlock(victim);
593
594 /*
595 * Kill all user processes sharing victim->mm in other thread groups, if
596 * any. They don't get access to memory reserves, though, to avoid
597 * depletion of all memory. This prevents mm->mmap_sem livelock when an
598 * oom killed thread cannot exit because it requires the semaphore and
599 * its contended by another thread trying to allocate memory itself.
600 * That thread will now get access to memory reserves since it has a
601 * pending fatal signal.
602 */
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800603 rcu_read_lock();
Oleg Nesterovc3190252015-11-05 18:48:23 -0800604 for_each_process(p) {
Oleg Nesterov4d7b3392015-11-05 18:48:26 -0800605 if (!process_shares_mm(p, mm))
Oleg Nesterovc3190252015-11-05 18:48:23 -0800606 continue;
607 if (same_thread_group(p, victim))
608 continue;
609 if (unlikely(p->flags & PF_KTHREAD))
610 continue;
Chen Jiea2b829d2015-12-11 13:41:00 -0800611 if (is_global_init(p))
612 continue;
Oleg Nesterovc3190252015-11-05 18:48:23 -0800613 if (p->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
614 continue;
David Rientjes647f2bd2012-03-21 16:33:46 -0700615
Oleg Nesterovc3190252015-11-05 18:48:23 -0800616 do_send_sig_info(SIGKILL, SEND_SIG_FORCED, p, true);
617 }
David Rientjes6b0c81b2012-07-31 16:43:45 -0700618 rcu_read_unlock();
David Rientjes647f2bd2012-03-21 16:33:46 -0700619
Tetsuo Handa880b7682015-11-05 18:47:51 -0800620 mmdrop(mm);
David Rientjes6b0c81b2012-07-31 16:43:45 -0700621 put_task_struct(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
David Rientjes647f2bd2012-03-21 16:33:46 -0700623#undef K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
David Rientjes309ed882010-08-09 17:18:54 -0700625/*
626 * Determines whether the kernel must panic because of the panic_on_oom sysctl.
627 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700628void check_panic_on_oom(struct oom_control *oc, enum oom_constraint constraint,
Balasubramani Vivekanandan2415b9f2015-04-14 15:48:18 -0700629 struct mem_cgroup *memcg)
David Rientjes309ed882010-08-09 17:18:54 -0700630{
631 if (likely(!sysctl_panic_on_oom))
632 return;
633 if (sysctl_panic_on_oom != 2) {
634 /*
635 * panic_on_oom == 1 only affects CONSTRAINT_NONE, the kernel
636 * does not panic for cpuset, mempolicy, or memcg allocation
637 * failures.
638 */
639 if (constraint != CONSTRAINT_NONE)
640 return;
641 }
David Rientjes071a4be2015-09-08 15:00:42 -0700642 /* Do not panic for oom kills triggered by sysrq */
Yaowei Baidb2a0dd2015-11-06 16:28:06 -0800643 if (is_sysrq_oom(oc))
David Rientjes071a4be2015-09-08 15:00:42 -0700644 return;
David Rientjes6e0fc462015-09-08 15:00:36 -0700645 dump_header(oc, NULL, memcg);
David Rientjes309ed882010-08-09 17:18:54 -0700646 panic("Out of memory: %s panic_on_oom is enabled\n",
647 sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide");
648}
649
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700650static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
651
652int register_oom_notifier(struct notifier_block *nb)
653{
654 return blocking_notifier_chain_register(&oom_notify_list, nb);
655}
656EXPORT_SYMBOL_GPL(register_oom_notifier);
657
658int unregister_oom_notifier(struct notifier_block *nb)
659{
660 return blocking_notifier_chain_unregister(&oom_notify_list, nb);
661}
662EXPORT_SYMBOL_GPL(unregister_oom_notifier);
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664/**
David Rientjes6e0fc462015-09-08 15:00:36 -0700665 * out_of_memory - kill the "best" process when we run out of memory
666 * @oc: pointer to struct oom_control
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 *
668 * If we run out of memory, we have the choice between either
669 * killing a random task (bad), letting the system crash (worse)
670 * OR try to be smart about which process to kill. Note that we
671 * don't have to be perfect here, we just have to be good.
672 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700673bool out_of_memory(struct oom_control *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
David Rientjes0aad4b32010-08-09 17:18:59 -0700675 struct task_struct *p;
David Rientjesa63d83f2010-08-09 17:19:46 -0700676 unsigned long totalpages;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700677 unsigned long freed = 0;
David Rientjes9cbb78b2012-07-31 16:43:44 -0700678 unsigned int uninitialized_var(points);
David Rientjese3658932010-08-09 17:18:55 -0700679 enum oom_constraint constraint = CONSTRAINT_NONE;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700680
Johannes Weinerdc564012015-06-24 16:57:19 -0700681 if (oom_killer_disabled)
682 return false;
683
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700684 blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
685 if (freed > 0)
686 /* Got some memory back in the last second. */
David Rientjes75e8f8b2015-09-08 15:00:47 -0700687 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
David Rientjes7b98c2e2010-08-09 17:18:48 -0700689 /*
David Rientjes9ff48682012-12-11 16:01:30 -0800690 * If current has a pending SIGKILL or is exiting, then automatically
691 * select it. The goal is to allow it to allocate so that it may
692 * quickly exit and free its memory.
Tetsuo Handad7a94e72015-02-11 15:24:54 -0800693 *
694 * But don't select if current has already released its mm and cleared
695 * TIF_MEMDIE flag at exit_mm(), otherwise an OOM livelock may occur.
David Rientjes7b98c2e2010-08-09 17:18:48 -0700696 */
Tetsuo Handad7a94e72015-02-11 15:24:54 -0800697 if (current->mm &&
698 (fatal_signal_pending(current) || task_will_free_mem(current))) {
Johannes Weiner16e95192015-06-24 16:57:07 -0700699 mark_oom_victim(current);
David Rientjes75e8f8b2015-09-08 15:00:47 -0700700 return true;
David Rientjes7b98c2e2010-08-09 17:18:48 -0700701 }
702
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800703 /*
704 * Check if there were limitations on the allocation (only relevant for
705 * NUMA) that may require different handling.
706 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700707 constraint = constrained_alloc(oc, &totalpages);
708 if (constraint != CONSTRAINT_MEMORY_POLICY)
709 oc->nodemask = NULL;
710 check_panic_on_oom(oc, constraint, NULL);
David Rientjes0aad4b32010-08-09 17:18:59 -0700711
David Rientjes121d1ba2012-07-31 16:42:55 -0700712 if (sysctl_oom_kill_allocating_task && current->mm &&
David Rientjes6e0fc462015-09-08 15:00:36 -0700713 !oom_unkillable_task(current, NULL, oc->nodemask) &&
David Rientjes121d1ba2012-07-31 16:42:55 -0700714 current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {
David Rientjes6b0c81b2012-07-31 16:43:45 -0700715 get_task_struct(current);
David Rientjes6e0fc462015-09-08 15:00:36 -0700716 oom_kill_process(oc, current, 0, totalpages, NULL,
David Rientjes2a1c9b12012-03-21 16:33:46 -0700717 "Out of memory (oom_kill_allocating_task)");
David Rientjes75e8f8b2015-09-08 15:00:47 -0700718 return true;
David Rientjes0aad4b32010-08-09 17:18:59 -0700719 }
720
David Rientjes6e0fc462015-09-08 15:00:36 -0700721 p = select_bad_process(oc, &points, totalpages);
David Rientjes0aad4b32010-08-09 17:18:59 -0700722 /* Found nothing?!?! Either we hang forever, or we panic. */
Yaowei Baidb2a0dd2015-11-06 16:28:06 -0800723 if (!p && !is_sysrq_oom(oc)) {
David Rientjes6e0fc462015-09-08 15:00:36 -0700724 dump_header(oc, NULL, NULL);
David Rientjes0aad4b32010-08-09 17:18:59 -0700725 panic("Out of memory and no killable processes...\n");
726 }
David Rientjes071a4be2015-09-08 15:00:42 -0700727 if (p && p != (void *)-1UL) {
David Rientjes6e0fc462015-09-08 15:00:36 -0700728 oom_kill_process(oc, p, points, totalpages, NULL,
729 "Out of memory");
David Rientjes75e8f8b2015-09-08 15:00:47 -0700730 /*
731 * Give the killed process a good chance to exit before trying
732 * to allocate memory again.
733 */
David Rientjes4f774b92012-07-31 16:42:37 -0700734 schedule_timeout_killable(1);
David Rientjes75e8f8b2015-09-08 15:00:47 -0700735 }
Johannes Weinerdc564012015-06-24 16:57:19 -0700736 return true;
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800737}
738
David Rientjese3658932010-08-09 17:18:55 -0700739/*
740 * The pagefault handler calls here because it is out of memory, so kill a
David Rientjesefacd022012-12-12 13:52:06 -0800741 * memory-hogging task. If any populated zone has ZONE_OOM_LOCKED set, a
742 * parallel oom killing is already in progress so do nothing.
David Rientjese3658932010-08-09 17:18:55 -0700743 */
744void pagefault_out_of_memory(void)
745{
David Rientjes6e0fc462015-09-08 15:00:36 -0700746 struct oom_control oc = {
747 .zonelist = NULL,
748 .nodemask = NULL,
749 .gfp_mask = 0,
750 .order = 0,
David Rientjes6e0fc462015-09-08 15:00:36 -0700751 };
752
Johannes Weiner49426422013-10-16 13:46:59 -0700753 if (mem_cgroup_oom_synchronize(true))
Johannes Weinerdc564012015-06-24 16:57:19 -0700754 return;
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700755
Johannes Weinerdc564012015-06-24 16:57:19 -0700756 if (!mutex_trylock(&oom_lock))
757 return;
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800758
David Rientjes6e0fc462015-09-08 15:00:36 -0700759 if (!out_of_memory(&oc)) {
Johannes Weinerdc564012015-06-24 16:57:19 -0700760 /*
761 * There shouldn't be any user tasks runnable while the
762 * OOM killer is disabled, so the current task has to
763 * be a racing OOM victim for which oom_killer_disable()
764 * is waiting for.
765 */
766 WARN_ON(test_thread_flag(TIF_MEMDIE));
David Rientjese3658932010-08-09 17:18:55 -0700767 }
Johannes Weinerdc564012015-06-24 16:57:19 -0700768
769 mutex_unlock(&oom_lock);
David Rientjese3658932010-08-09 17:18:55 -0700770}