blob: 2b97e8f04607b2ccb0011a3af5996aa41efe069b [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>
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070029#include <linux/module.h>
30#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
KAMEZAWA Hiroyukifadd8fb2006-06-23 02:03:13 -070037int sysctl_panic_on_oom;
David Rientjesfe071d72007-10-16 23:25:56 -070038int sysctl_oom_kill_allocating_task;
David Rientjesad915c42010-08-09 17:18:53 -070039int sysctl_oom_dump_tasks = 1;
David Rientjesc7d4cae2009-01-06 14:39:00 -080040static DEFINE_SPINLOCK(zone_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
David Rientjes72788c32011-05-24 17:11:40 -070042/**
43 * test_set_oom_score_adj() - set current's oom_score_adj and return old value
44 * @new_val: new oom_score_adj value
45 *
46 * Sets the oom_score_adj value for current to @new_val with proper
47 * synchronization and returns the old value. Usually used to temporarily
48 * set a value, save the old value in the caller, and then reinstate it later.
49 */
50int test_set_oom_score_adj(int new_val)
51{
52 struct sighand_struct *sighand = current->sighand;
53 int old_val;
54
55 spin_lock_irq(&sighand->siglock);
56 old_val = current->signal->oom_score_adj;
David Rientjesc9f01242011-10-31 17:07:15 -070057 current->signal->oom_score_adj = new_val;
David Rientjes72788c32011-05-24 17:11:40 -070058 spin_unlock_irq(&sighand->siglock);
59
60 return old_val;
61}
62
David Rientjes6f48d0eb2010-08-09 17:18:52 -070063#ifdef CONFIG_NUMA
64/**
65 * has_intersects_mems_allowed() - check task eligiblity for kill
66 * @tsk: task struct of which task to consider
67 * @mask: nodemask passed to page allocator for mempolicy ooms
68 *
69 * Task eligibility is determined by whether or not a candidate task, @tsk,
70 * shares the same mempolicy nodes as current if it is bound by such a policy
71 * and whether or not it has the same set of allowed cpuset nodes.
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070072 */
David Rientjes6f48d0eb2010-08-09 17:18:52 -070073static bool has_intersects_mems_allowed(struct task_struct *tsk,
74 const nodemask_t *mask)
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070075{
David Rientjes6f48d0eb2010-08-09 17:18:52 -070076 struct task_struct *start = tsk;
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070077
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070078 do {
David Rientjes6f48d0eb2010-08-09 17:18:52 -070079 if (mask) {
80 /*
81 * If this is a mempolicy constrained oom, tsk's
82 * cpuset is irrelevant. Only return true if its
83 * mempolicy intersects current, otherwise it may be
84 * needlessly killed.
85 */
86 if (mempolicy_nodemask_intersects(tsk, mask))
87 return true;
88 } else {
89 /*
90 * This is not a mempolicy constrained oom, so only
91 * check the mems of tsk's cpuset.
92 */
93 if (cpuset_mems_allowed_intersects(current, tsk))
94 return true;
95 }
KOSAKI Motohirodf1090a2010-08-09 17:19:39 -070096 } while_each_thread(start, tsk);
97
David Rientjes6f48d0eb2010-08-09 17:18:52 -070098 return false;
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070099}
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700100#else
101static bool has_intersects_mems_allowed(struct task_struct *tsk,
102 const nodemask_t *mask)
103{
104 return true;
105}
106#endif /* CONFIG_NUMA */
KOSAKI Motohiro495789a2009-09-21 17:03:14 -0700107
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700108/*
109 * The process p may have detached its own ->mm while exiting or through
110 * use_mm(), but one or more of its subthreads may still have a valid
111 * pointer. Return p, or any of its subthreads with a valid ->mm, with
112 * task_lock() held.
113 */
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -0700114struct task_struct *find_lock_task_mm(struct task_struct *p)
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700115{
116 struct task_struct *t = p;
117
118 do {
119 task_lock(t);
120 if (likely(t->mm))
121 return t;
122 task_unlock(t);
123 } while_each_thread(p, t);
124
125 return NULL;
126}
127
KOSAKI Motohiroab290ad2010-08-09 17:19:35 -0700128/* return true if the task is not adequate as candidate victim task. */
David Rientjese85bfd32010-09-22 13:05:10 -0700129static bool oom_unkillable_task(struct task_struct *p,
130 const struct mem_cgroup *mem, const nodemask_t *nodemask)
KOSAKI Motohiroab290ad2010-08-09 17:19:35 -0700131{
132 if (is_global_init(p))
133 return true;
134 if (p->flags & PF_KTHREAD)
135 return true;
136
137 /* When mem_cgroup_out_of_memory() and p is not member of the group */
138 if (mem && !task_in_mem_cgroup(p, mem))
139 return true;
140
141 /* p may not have freeable memory in nodemask */
142 if (!has_intersects_mems_allowed(p, nodemask))
143 return true;
144
145 return false;
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/**
David Rientjesa63d83f2010-08-09 17:19:46 -0700149 * oom_badness - heuristic function to determine which candidate task to kill
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * @p: task struct of which task we should calculate
David Rientjesa63d83f2010-08-09 17:19:46 -0700151 * @totalpages: total present RAM allowed for page allocation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 *
David Rientjesa63d83f2010-08-09 17:19:46 -0700153 * The heuristic for determining which task to kill is made to be as simple and
154 * predictable as possible. The goal is to return the highest value for the
155 * task consuming the most memory to avoid subsequent oom failures.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
David Rientjesa63d83f2010-08-09 17:19:46 -0700157unsigned int oom_badness(struct task_struct *p, struct mem_cgroup *mem,
158 const nodemask_t *nodemask, unsigned long totalpages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
David Rientjesa63d83f2010-08-09 17:19:46 -0700160 int points;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700161
KOSAKI Motohiro26ebc982010-08-09 17:19:37 -0700162 if (oom_unkillable_task(p, mem, nodemask))
163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700165 p = find_lock_task_mm(p);
166 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return 0;
168
169 /*
David Rientjesa63d83f2010-08-09 17:19:46 -0700170 * The memory controller may have a limit of 0 bytes, so avoid a divide
171 * by zero, if necessary.
172 */
173 if (!totalpages)
174 totalpages = 1;
175
176 /*
177 * The baseline for the badness score is the proportion of RAM that each
KOSAKI Motohirof755a042011-04-27 15:26:50 -0700178 * task's rss, pagetable and swap space use.
David Rientjesa63d83f2010-08-09 17:19:46 -0700179 */
KOSAKI Motohirof755a042011-04-27 15:26:50 -0700180 points = get_mm_rss(p->mm) + p->mm->nr_ptes;
181 points += get_mm_counter(p->mm, MM_SWAPENTS);
182
183 points *= 1000;
184 points /= totalpages;
Andrew Morton97c2c9b2006-04-18 22:20:38 -0700185 task_unlock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 /*
David Rientjesa63d83f2010-08-09 17:19:46 -0700188 * Root processes get 3% bonus, just like the __vm_enough_memory()
189 * implementation used by LSMs.
Hugh Dickins7ba34852007-01-05 16:37:03 -0800190 */
David Rientjesa63d83f2010-08-09 17:19:46 -0700191 if (has_capability_noaudit(p, CAP_SYS_ADMIN))
192 points -= 30;
Hugh Dickins7ba34852007-01-05 16:37:03 -0800193
194 /*
David Rientjesa63d83f2010-08-09 17:19:46 -0700195 * /proc/pid/oom_score_adj ranges from -1000 to +1000 such that it may
196 * either completely disable oom killing or always prefer a certain
197 * task.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
David Rientjesa63d83f2010-08-09 17:19:46 -0700199 points += p->signal->oom_score_adj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
David Rientjesf19e8aa2010-09-22 13:04:52 -0700201 /*
202 * Never return 0 for an eligible task that may be killed since it's
203 * possible that no single user task uses more than 0.1% of memory and
204 * no single admin tasks uses more than 3.0%.
205 */
206 if (points <= 0)
207 return 1;
David Rientjesa63d83f2010-08-09 17:19:46 -0700208 return (points < 1000) ? points : 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211/*
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800212 * Determine the type of allocation constraint.
213 */
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800214#ifdef CONFIG_NUMA
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800215static enum oom_constraint constrained_alloc(struct zonelist *zonelist,
David Rientjesa63d83f2010-08-09 17:19:46 -0700216 gfp_t gfp_mask, nodemask_t *nodemask,
217 unsigned long *totalpages)
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800218{
Mel Gorman54a6eb52008-04-28 02:12:16 -0700219 struct zone *zone;
Mel Gormandd1a2392008-04-28 02:12:17 -0700220 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -0700221 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
David Rientjesa63d83f2010-08-09 17:19:46 -0700222 bool cpuset_limited = false;
223 int nid;
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800224
David Rientjesa63d83f2010-08-09 17:19:46 -0700225 /* Default to all available memory */
226 *totalpages = totalram_pages + total_swap_pages;
227
228 if (!zonelist)
229 return CONSTRAINT_NONE;
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800230 /*
231 * Reach here only when __GFP_NOFAIL is used. So, we should avoid
232 * to kill current.We have to random task kill in this case.
233 * Hopefully, CONSTRAINT_THISNODE...but no way to handle it, now.
234 */
235 if (gfp_mask & __GFP_THISNODE)
236 return CONSTRAINT_NONE;
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800237
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800238 /*
David Rientjesa63d83f2010-08-09 17:19:46 -0700239 * This is not a __GFP_THISNODE allocation, so a truncated nodemask in
240 * the page allocator means a mempolicy is in effect. Cpuset policy
241 * is enforced in get_page_from_freelist().
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800242 */
David Rientjesa63d83f2010-08-09 17:19:46 -0700243 if (nodemask && !nodes_subset(node_states[N_HIGH_MEMORY], *nodemask)) {
244 *totalpages = total_swap_pages;
245 for_each_node_mask(nid, *nodemask)
246 *totalpages += node_spanned_pages(nid);
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800247 return CONSTRAINT_MEMORY_POLICY;
David Rientjesa63d83f2010-08-09 17:19:46 -0700248 }
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800249
250 /* Check this allocation failure is caused by cpuset's wall function */
251 for_each_zone_zonelist_nodemask(zone, z, zonelist,
252 high_zoneidx, nodemask)
253 if (!cpuset_zone_allowed_softwall(zone, gfp_mask))
David Rientjesa63d83f2010-08-09 17:19:46 -0700254 cpuset_limited = true;
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800255
David Rientjesa63d83f2010-08-09 17:19:46 -0700256 if (cpuset_limited) {
257 *totalpages = total_swap_pages;
258 for_each_node_mask(nid, cpuset_current_mems_allowed)
259 *totalpages += node_spanned_pages(nid);
260 return CONSTRAINT_CPUSET;
261 }
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800262 return CONSTRAINT_NONE;
263}
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800264#else
265static enum oom_constraint constrained_alloc(struct zonelist *zonelist,
David Rientjesa63d83f2010-08-09 17:19:46 -0700266 gfp_t gfp_mask, nodemask_t *nodemask,
267 unsigned long *totalpages)
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800268{
David Rientjesa63d83f2010-08-09 17:19:46 -0700269 *totalpages = totalram_pages + total_swap_pages;
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800270 return CONSTRAINT_NONE;
271}
272#endif
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800273
274/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * Simple selection loop. We chose the process with the highest
276 * number of 'points'. We expect the caller will lock the tasklist.
277 *
278 * (not docbooked, we don't want this one cluttering up the manual)
279 */
David Rientjesa63d83f2010-08-09 17:19:46 -0700280static struct task_struct *select_bad_process(unsigned int *ppoints,
281 unsigned long totalpages, struct mem_cgroup *mem,
282 const nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
David Rientjes3a5dda72011-03-22 16:30:09 -0700284 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct task_struct *chosen = NULL;
Kurt Garloff9827b782006-02-20 18:27:51 -0800286 *ppoints = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
David Rientjes3a5dda72011-03-22 16:30:09 -0700288 do_each_thread(g, p) {
David Rientjesa63d83f2010-08-09 17:19:46 -0700289 unsigned int points;
Paul Jacksona49335c2005-09-06 15:18:09 -0700290
Oleg Nesterovc027a472011-07-30 16:35:02 +0200291 if (p->exit_state)
Andrey Vagin30e2b412011-03-22 16:30:11 -0700292 continue;
KOSAKI Motohiroab290ad2010-08-09 17:19:35 -0700293 if (oom_unkillable_task(p, mem, nodemask))
David Rientjes6cf86ac2010-08-09 17:18:50 -0700294 continue;
Paul Jacksonef08e3b2005-09-06 15:18:13 -0700295
Paul Jacksona49335c2005-09-06 15:18:09 -0700296 /*
Nick Pigginb78483a2006-09-29 02:01:14 -0700297 * This task already has access to memory reserves and is
298 * being killed. Don't allow any other task access to the
299 * memory reserve.
300 *
301 * Note: this may have a chance of deadlock if it gets
302 * blocked waiting for another task which itself is waiting
303 * for memory. Is there a better alternative?
304 */
David Rientjesf660daa2011-10-31 17:07:07 -0700305 if (test_tsk_thread_flag(p, TIF_MEMDIE)) {
306 if (unlikely(frozen(p)))
307 thaw_process(p);
Nick Pigginb78483a2006-09-29 02:01:14 -0700308 return ERR_PTR(-1UL);
David Rientjesf660daa2011-10-31 17:07:07 -0700309 }
Oleg Nesterovc027a472011-07-30 16:35:02 +0200310 if (!p->mm)
311 continue;
Nick Pigginb78483a2006-09-29 02:01:14 -0700312
Andrey Vagin30e2b412011-03-22 16:30:11 -0700313 if (p->flags & PF_EXITING) {
David Rientjesedd45542011-03-22 16:30:12 -0700314 /*
315 * If p is the current task and is in the process of
316 * releasing memory, we allow the "kill" to set
317 * TIF_MEMDIE, which will allow it to gain access to
318 * memory reserves. Otherwise, it may stall forever.
319 *
320 * The loop isn't broken here, however, in case other
321 * threads are found to have already been oom killed.
322 */
323 if (p == current) {
324 chosen = p;
325 *ppoints = 1000;
326 } else {
327 /*
328 * If this task is not being ptraced on exit,
329 * then wait for it to finish before killing
330 * some other task unnecessarily.
331 */
Tejun Heod21142e2011-06-17 16:50:34 +0200332 if (!(p->group_leader->ptrace & PT_TRACE_EXIT))
David Rientjesedd45542011-03-22 16:30:12 -0700333 return ERR_PTR(-1UL);
334 }
Nick Piggin50ec3bb2006-09-25 23:31:29 -0700335 }
Oleg Nesterov972c4ea2006-09-29 02:01:12 -0700336
David Rientjesa63d83f2010-08-09 17:19:46 -0700337 points = oom_badness(p, mem, nodemask, totalpages);
338 if (points > *ppoints) {
Paul Jacksona49335c2005-09-06 15:18:09 -0700339 chosen = p;
Kurt Garloff9827b782006-02-20 18:27:51 -0800340 *ppoints = points;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
David Rientjes3a5dda72011-03-22 16:30:09 -0700342 } while_each_thread(g, p);
Oleg Nesterov972c4ea2006-09-29 02:01:12 -0700343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return chosen;
345}
346
347/**
Randy Dunlap1b578df2008-03-19 17:00:42 -0700348 * dump_tasks - dump current memory state of all system tasks
David Rientjes74ab7f12010-08-09 17:18:46 -0700349 * @mem: current's memory controller, if constrained
David Rientjese85bfd32010-09-22 13:05:10 -0700350 * @nodemask: nodemask passed to page allocator for mempolicy ooms
Randy Dunlap1b578df2008-03-19 17:00:42 -0700351 *
David Rientjese85bfd32010-09-22 13:05:10 -0700352 * Dumps the current memory state of all eligible tasks. Tasks not in the same
353 * memcg, not in the same cpuset, or bound to a disjoint set of mempolicy nodes
354 * are not shown.
David Rientjesfef1bdd2008-02-07 00:14:07 -0800355 * State information includes task's pid, uid, tgid, vm size, rss, cpu, oom_adj
David Rientjesa63d83f2010-08-09 17:19:46 -0700356 * value, oom_score_adj value, and name.
David Rientjesfef1bdd2008-02-07 00:14:07 -0800357 *
David Rientjesfef1bdd2008-02-07 00:14:07 -0800358 * Call with tasklist_lock read-locked.
359 */
David Rientjese85bfd32010-09-22 13:05:10 -0700360static void dump_tasks(const struct mem_cgroup *mem, const nodemask_t *nodemask)
David Rientjesfef1bdd2008-02-07 00:14:07 -0800361{
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700362 struct task_struct *p;
363 struct task_struct *task;
David Rientjesfef1bdd2008-02-07 00:14:07 -0800364
David Rientjesa63d83f2010-08-09 17:19:46 -0700365 pr_info("[ pid ] uid tgid total_vm rss cpu oom_adj oom_score_adj name\n");
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700366 for_each_process(p) {
David Rientjese85bfd32010-09-22 13:05:10 -0700367 if (oom_unkillable_task(p, mem, nodemask))
David Rientjesfef1bdd2008-02-07 00:14:07 -0800368 continue;
369
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700370 task = find_lock_task_mm(p);
371 if (!task) {
David Rientjes6d2661e2009-05-28 14:34:19 -0700372 /*
David Rientjes74ab7f12010-08-09 17:18:46 -0700373 * This is a kthread or all of p's threads have already
374 * detached their mm's. There's no need to report
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700375 * them; they can't be oom killed anyway.
David Rientjes6d2661e2009-05-28 14:34:19 -0700376 */
David Rientjes6d2661e2009-05-28 14:34:19 -0700377 continue;
378 }
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700379
David Rientjesa63d83f2010-08-09 17:19:46 -0700380 pr_info("[%5d] %5d %5d %8lu %8lu %3u %3d %5d %s\n",
KOSAKI Motohiro8d6c83f2010-08-19 14:13:39 -0700381 task->pid, task_uid(task), task->tgid,
David Rientjesa63d83f2010-08-09 17:19:46 -0700382 task->mm->total_vm, get_mm_rss(task->mm),
383 task_cpu(task), task->signal->oom_adj,
384 task->signal->oom_score_adj, task->comm);
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700385 task_unlock(task);
386 }
David Rientjesfef1bdd2008-02-07 00:14:07 -0800387}
388
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -0800389static void dump_header(struct task_struct *p, gfp_t gfp_mask, int order,
David Rientjese85bfd32010-09-22 13:05:10 -0700390 struct mem_cgroup *mem, const nodemask_t *nodemask)
David Rientjes1b604d72009-12-14 17:57:47 -0800391{
David Rientjes5e9d8342010-08-09 17:18:51 -0700392 task_lock(current);
David Rientjes1b604d72009-12-14 17:57:47 -0800393 pr_warning("%s invoked oom-killer: gfp_mask=0x%x, order=%d, "
David Rientjesa63d83f2010-08-09 17:19:46 -0700394 "oom_adj=%d, oom_score_adj=%d\n",
395 current->comm, gfp_mask, order, current->signal->oom_adj,
396 current->signal->oom_score_adj);
David Rientjes1b604d72009-12-14 17:57:47 -0800397 cpuset_print_task_mems_allowed(current);
398 task_unlock(current);
399 dump_stack();
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -0800400 mem_cgroup_print_oom_info(mem, p);
David Rientjesb2b755b2011-03-24 15:18:15 -0700401 show_mem(SHOW_MEM_FILTER_NODES);
David Rientjes1b604d72009-12-14 17:57:47 -0800402 if (sysctl_oom_dump_tasks)
David Rientjese85bfd32010-09-22 13:05:10 -0700403 dump_tasks(mem, nodemask);
David Rientjes1b604d72009-12-14 17:57:47 -0800404}
405
KOSAKI Motohiro3b4798c2009-12-15 16:45:32 -0800406#define K(x) ((x) << (PAGE_SHIFT-10))
Luis Claudio R. Goncalves93b43fa2010-08-09 17:19:41 -0700407static int oom_kill_task(struct task_struct *p, struct mem_cgroup *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
David Rientjes1e99bad2010-10-26 14:21:24 -0700409 struct task_struct *q;
410 struct mm_struct *mm;
411
David Rientjesb940fd72010-08-09 17:18:57 -0700412 p = find_lock_task_mm(p);
KOSAKI Motohirobe71cf22010-08-19 14:13:38 -0700413 if (!p)
Dave Peterson01315922006-04-18 22:20:44 -0700414 return 1;
KOSAKI Motohirobe71cf22010-08-19 14:13:38 -0700415
David Rientjes1e99bad2010-10-26 14:21:24 -0700416 /* mm cannot be safely dereferenced after task_unlock(p) */
417 mm = p->mm;
418
David Rientjesb940fd72010-08-09 17:18:57 -0700419 pr_err("Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB\n",
420 task_pid_nr(p), p->comm, K(p->mm->total_vm),
421 K(get_mm_counter(p->mm, MM_ANONPAGES)),
422 K(get_mm_counter(p->mm, MM_FILEPAGES)));
423 task_unlock(p);
KOSAKI Motohiro0753ba02009-08-18 14:11:10 -0700424
David Rientjes1e99bad2010-10-26 14:21:24 -0700425 /*
David Rientjes7b0d44f2011-10-31 17:07:11 -0700426 * Kill all user processes sharing p->mm in other thread groups, if any.
David Rientjes1e99bad2010-10-26 14:21:24 -0700427 * They don't get access to memory reserves or a higher scheduler
428 * priority, though, to avoid depletion of all memory or task
429 * starvation. This prevents mm->mmap_sem livelock when an oom killed
430 * task cannot exit because it requires the semaphore and its contended
431 * by another thread trying to allocate memory itself. That thread will
432 * now get access to memory reserves since it has a pending fatal
433 * signal.
434 */
435 for_each_process(q)
David Rientjes7b0d44f2011-10-31 17:07:11 -0700436 if (q->mm == mm && !same_thread_group(q, p) &&
437 !(q->flags & PF_KTHREAD)) {
David Rientjesc9f01242011-10-31 17:07:15 -0700438 if (q->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
439 continue;
440
David Rientjes1e99bad2010-10-26 14:21:24 -0700441 task_lock(q); /* Protect ->comm from prctl() */
442 pr_err("Kill process %d (%s) sharing same memory\n",
443 task_pid_nr(q), q->comm);
444 task_unlock(q);
445 force_sig(SIGKILL, q);
446 }
Luis Claudio R. Goncalves93b43fa2010-08-09 17:19:41 -0700447
David Rientjesb940fd72010-08-09 17:18:57 -0700448 set_tsk_thread_flag(p, TIF_MEMDIE);
449 force_sig(SIGKILL, p);
Luis Claudio R. Goncalves93b43fa2010-08-09 17:19:41 -0700450
Dave Peterson01315922006-04-18 22:20:44 -0700451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
David Rientjesb940fd72010-08-09 17:18:57 -0700453#undef K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
David Rientjes7213f502007-10-16 23:25:57 -0700455static int oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
David Rientjesa63d83f2010-08-09 17:19:46 -0700456 unsigned int points, unsigned long totalpages,
457 struct mem_cgroup *mem, nodemask_t *nodemask,
458 const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Linus Torvalds52d3c032011-03-14 15:17:07 -0700460 struct task_struct *victim = p;
David Rientjes5e9d8342010-08-09 17:18:51 -0700461 struct task_struct *child;
Linus Torvalds52d3c032011-03-14 15:17:07 -0700462 struct task_struct *t = p;
463 unsigned int victim_points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
David Rientjes1b604d72009-12-14 17:57:47 -0800465 if (printk_ratelimit())
David Rientjese85bfd32010-09-22 13:05:10 -0700466 dump_header(p, gfp_mask, order, mem, nodemask);
David Rientjes7213f502007-10-16 23:25:57 -0700467
Nick Piggin50ec3bb2006-09-25 23:31:29 -0700468 /*
469 * If the task is already exiting, don't alarm the sysadmin or kill
470 * its children or threads, just set TIF_MEMDIE so it can die quickly
471 */
KOSAKI Motohiro0753ba02009-08-18 14:11:10 -0700472 if (p->flags & PF_EXITING) {
David Rientjes43589972010-08-09 17:18:49 -0700473 set_tsk_thread_flag(p, TIF_MEMDIE);
Nick Piggin50ec3bb2006-09-25 23:31:29 -0700474 return 0;
475 }
476
David Rientjes5e9d8342010-08-09 17:18:51 -0700477 task_lock(p);
David Rientjesa63d83f2010-08-09 17:19:46 -0700478 pr_err("%s: Kill process %d (%s) score %d or sacrifice child\n",
David Rientjes5e9d8342010-08-09 17:18:51 -0700479 message, task_pid_nr(p), p->comm, points);
480 task_unlock(p);
Nick Pigginf3af38d2006-12-06 20:31:51 -0800481
David Rientjes5e9d8342010-08-09 17:18:51 -0700482 /*
483 * If any of p's children has a different mm and is eligible for kill,
David Rientjes11239832011-07-25 17:12:17 -0700484 * the one with the highest oom_badness() score is sacrificed for its
David Rientjes5e9d8342010-08-09 17:18:51 -0700485 * parent. This attempts to lose the minimal amount of work done while
486 * still freeing memory.
487 */
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700488 do {
David Rientjes5e9d8342010-08-09 17:18:51 -0700489 list_for_each_entry(child, &t->children, sibling) {
David Rientjesa63d83f2010-08-09 17:19:46 -0700490 unsigned int child_points;
David Rientjes5e9d8342010-08-09 17:18:51 -0700491
David Rientjesedd45542011-03-22 16:30:12 -0700492 if (child->mm == p->mm)
493 continue;
David Rientjesa63d83f2010-08-09 17:19:46 -0700494 /*
495 * oom_badness() returns 0 if the thread is unkillable
496 */
497 child_points = oom_badness(child, mem, nodemask,
498 totalpages);
David Rientjes5e9d8342010-08-09 17:18:51 -0700499 if (child_points > victim_points) {
500 victim = child;
501 victim_points = child_points;
502 }
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700503 }
504 } while_each_thread(p, t);
505
Luis Claudio R. Goncalves93b43fa2010-08-09 17:19:41 -0700506 return oom_kill_task(victim, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
David Rientjes309ed882010-08-09 17:18:54 -0700509/*
510 * Determines whether the kernel must panic because of the panic_on_oom sysctl.
511 */
512static void check_panic_on_oom(enum oom_constraint constraint, gfp_t gfp_mask,
David Rientjese85bfd32010-09-22 13:05:10 -0700513 int order, const nodemask_t *nodemask)
David Rientjes309ed882010-08-09 17:18:54 -0700514{
515 if (likely(!sysctl_panic_on_oom))
516 return;
517 if (sysctl_panic_on_oom != 2) {
518 /*
519 * panic_on_oom == 1 only affects CONSTRAINT_NONE, the kernel
520 * does not panic for cpuset, mempolicy, or memcg allocation
521 * failures.
522 */
523 if (constraint != CONSTRAINT_NONE)
524 return;
525 }
526 read_lock(&tasklist_lock);
David Rientjese85bfd32010-09-22 13:05:10 -0700527 dump_header(NULL, gfp_mask, order, NULL, nodemask);
David Rientjes309ed882010-08-09 17:18:54 -0700528 read_unlock(&tasklist_lock);
529 panic("Out of memory: %s panic_on_oom is enabled\n",
530 sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide");
531}
532
Balbir Singh00f0b822008-03-04 14:28:39 -0800533#ifdef CONFIG_CGROUP_MEM_RES_CTLR
Pavel Emelianovc7ba5c92008-02-07 00:13:58 -0800534void mem_cgroup_out_of_memory(struct mem_cgroup *mem, gfp_t gfp_mask)
535{
David Rientjesa63d83f2010-08-09 17:19:46 -0700536 unsigned long limit;
537 unsigned int points = 0;
Pavel Emelianovc7ba5c92008-02-07 00:13:58 -0800538 struct task_struct *p;
539
David Rientjesf9434ad2011-03-23 16:42:44 -0700540 /*
541 * If current has a pending SIGKILL, then automatically select it. The
542 * goal is to allow it to allocate so that it may quickly exit and free
543 * its memory.
544 */
545 if (fatal_signal_pending(current)) {
546 set_thread_flag(TIF_MEMDIE);
David Rientjesf9434ad2011-03-23 16:42:44 -0700547 return;
548 }
549
David Rientjese85bfd32010-09-22 13:05:10 -0700550 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, 0, NULL);
David Rientjesa63d83f2010-08-09 17:19:46 -0700551 limit = mem_cgroup_get_limit(mem) >> PAGE_SHIFT;
Li Zefane115f2d2008-04-15 14:34:37 -0700552 read_lock(&tasklist_lock);
Pavel Emelianovc7ba5c92008-02-07 00:13:58 -0800553retry:
David Rientjesa63d83f2010-08-09 17:19:46 -0700554 p = select_bad_process(&points, limit, mem, NULL);
David Rientjesdf64f812010-05-26 14:42:41 -0700555 if (!p || PTR_ERR(p) == -1UL)
Pavel Emelianovc7ba5c92008-02-07 00:13:58 -0800556 goto out;
557
David Rientjesa63d83f2010-08-09 17:19:46 -0700558 if (oom_kill_process(p, gfp_mask, 0, points, limit, mem, NULL,
Pavel Emelianovc7ba5c92008-02-07 00:13:58 -0800559 "Memory cgroup out of memory"))
560 goto retry;
561out:
Li Zefane115f2d2008-04-15 14:34:37 -0700562 read_unlock(&tasklist_lock);
Pavel Emelianovc7ba5c92008-02-07 00:13:58 -0800563}
564#endif
565
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700566static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
567
568int register_oom_notifier(struct notifier_block *nb)
569{
570 return blocking_notifier_chain_register(&oom_notify_list, nb);
571}
572EXPORT_SYMBOL_GPL(register_oom_notifier);
573
574int unregister_oom_notifier(struct notifier_block *nb)
575{
576 return blocking_notifier_chain_unregister(&oom_notify_list, nb);
577}
578EXPORT_SYMBOL_GPL(unregister_oom_notifier);
579
David Rientjes098d7f12007-10-16 23:25:55 -0700580/*
581 * Try to acquire the OOM killer lock for the zones in zonelist. Returns zero
582 * if a parallel OOM killing is already taking place that includes a zone in
583 * the zonelist. Otherwise, locks all zones in the zonelist and returns 1.
584 */
Minchan Kimff321fe2010-08-09 17:18:57 -0700585int try_set_zonelist_oom(struct zonelist *zonelist, gfp_t gfp_mask)
David Rientjes098d7f12007-10-16 23:25:55 -0700586{
Mel Gormandd1a2392008-04-28 02:12:17 -0700587 struct zoneref *z;
588 struct zone *zone;
David Rientjes098d7f12007-10-16 23:25:55 -0700589 int ret = 1;
590
David Rientjesc7d4cae2009-01-06 14:39:00 -0800591 spin_lock(&zone_scan_lock);
Mel Gormandd1a2392008-04-28 02:12:17 -0700592 for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
593 if (zone_is_oom_locked(zone)) {
David Rientjes098d7f12007-10-16 23:25:55 -0700594 ret = 0;
595 goto out;
596 }
Mel Gormandd1a2392008-04-28 02:12:17 -0700597 }
David Rientjes098d7f12007-10-16 23:25:55 -0700598
Mel Gormandd1a2392008-04-28 02:12:17 -0700599 for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
600 /*
David Rientjesc7d4cae2009-01-06 14:39:00 -0800601 * Lock each zone in the zonelist under zone_scan_lock so a
Minchan Kimff321fe2010-08-09 17:18:57 -0700602 * parallel invocation of try_set_zonelist_oom() doesn't succeed
Mel Gormandd1a2392008-04-28 02:12:17 -0700603 * when it shouldn't.
604 */
605 zone_set_flag(zone, ZONE_OOM_LOCKED);
606 }
607
David Rientjes098d7f12007-10-16 23:25:55 -0700608out:
David Rientjesc7d4cae2009-01-06 14:39:00 -0800609 spin_unlock(&zone_scan_lock);
David Rientjes098d7f12007-10-16 23:25:55 -0700610 return ret;
611}
612
613/*
614 * Clears the ZONE_OOM_LOCKED flag for all zones in the zonelist so that failed
615 * allocation attempts with zonelists containing them may now recall the OOM
616 * killer, if necessary.
617 */
Mel Gormandd1a2392008-04-28 02:12:17 -0700618void clear_zonelist_oom(struct zonelist *zonelist, gfp_t gfp_mask)
David Rientjes098d7f12007-10-16 23:25:55 -0700619{
Mel Gormandd1a2392008-04-28 02:12:17 -0700620 struct zoneref *z;
621 struct zone *zone;
David Rientjes098d7f12007-10-16 23:25:55 -0700622
David Rientjesc7d4cae2009-01-06 14:39:00 -0800623 spin_lock(&zone_scan_lock);
Mel Gormandd1a2392008-04-28 02:12:17 -0700624 for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
625 zone_clear_flag(zone, ZONE_OOM_LOCKED);
626 }
David Rientjesc7d4cae2009-01-06 14:39:00 -0800627 spin_unlock(&zone_scan_lock);
David Rientjes098d7f12007-10-16 23:25:55 -0700628}
629
Nick Piggin1c0fe6e2009-01-06 14:38:59 -0800630/*
David Rientjese3658932010-08-09 17:18:55 -0700631 * Try to acquire the oom killer lock for all system zones. Returns zero if a
632 * parallel oom killing is taking place, otherwise locks all zones and returns
633 * non-zero.
634 */
635static int try_set_system_oom(void)
636{
637 struct zone *zone;
638 int ret = 1;
639
640 spin_lock(&zone_scan_lock);
641 for_each_populated_zone(zone)
642 if (zone_is_oom_locked(zone)) {
643 ret = 0;
644 goto out;
645 }
646 for_each_populated_zone(zone)
647 zone_set_flag(zone, ZONE_OOM_LOCKED);
648out:
649 spin_unlock(&zone_scan_lock);
650 return ret;
651}
652
653/*
654 * Clears ZONE_OOM_LOCKED for all system zones so that failed allocation
655 * attempts or page faults may now recall the oom killer, if necessary.
656 */
657static void clear_system_oom(void)
658{
659 struct zone *zone;
660
661 spin_lock(&zone_scan_lock);
662 for_each_populated_zone(zone)
663 zone_clear_flag(zone, ZONE_OOM_LOCKED);
664 spin_unlock(&zone_scan_lock);
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/**
Dave Peterson6937a252006-06-23 02:03:13 -0700668 * out_of_memory - kill the "best" process when we run out of memory
Randy Dunlap1b578df2008-03-19 17:00:42 -0700669 * @zonelist: zonelist pointer
670 * @gfp_mask: memory allocation flags
671 * @order: amount of memory being requested as a power of 2
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700672 * @nodemask: nodemask passed to page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 *
674 * If we run out of memory, we have the choice between either
675 * killing a random task (bad), letting the system crash (worse)
676 * OR try to be smart about which process to kill. Note that we
677 * don't have to be perfect here, we just have to be good.
678 */
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800679void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask,
680 int order, nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
David Rientjese85bfd32010-09-22 13:05:10 -0700682 const nodemask_t *mpol_mask;
David Rientjes0aad4b32010-08-09 17:18:59 -0700683 struct task_struct *p;
David Rientjesa63d83f2010-08-09 17:19:46 -0700684 unsigned long totalpages;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700685 unsigned long freed = 0;
David Rientjesa63d83f2010-08-09 17:19:46 -0700686 unsigned int points;
David Rientjese3658932010-08-09 17:18:55 -0700687 enum oom_constraint constraint = CONSTRAINT_NONE;
KOSAKI Motohirob52723c2010-08-19 14:13:39 -0700688 int killed = 0;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700689
690 blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
691 if (freed > 0)
692 /* Got some memory back in the last second. */
693 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
David Rientjes7b98c2e2010-08-09 17:18:48 -0700695 /*
696 * If current has a pending SIGKILL, then automatically select it. The
697 * goal is to allow it to allocate so that it may quickly exit and free
698 * its memory.
699 */
700 if (fatal_signal_pending(current)) {
701 set_thread_flag(TIF_MEMDIE);
702 return;
703 }
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 Rientjesa63d83f2010-08-09 17:19:46 -0700709 constraint = constrained_alloc(zonelist, gfp_mask, nodemask,
710 &totalpages);
David Rientjese85bfd32010-09-22 13:05:10 -0700711 mpol_mask = (constraint == CONSTRAINT_MEMORY_POLICY) ? nodemask : NULL;
712 check_panic_on_oom(constraint, gfp_mask, order, mpol_mask);
David Rientjes0aad4b32010-08-09 17:18:59 -0700713
David Rientjes2b45ab32007-05-06 14:50:00 -0700714 read_lock(&tasklist_lock);
KOSAKI Motohirof88ccad2010-08-09 17:19:36 -0700715 if (sysctl_oom_kill_allocating_task &&
KOSAKI Motohiroa96cfd62010-08-09 17:19:38 -0700716 !oom_unkillable_task(current, NULL, nodemask) &&
David Rientjesc9f01242011-10-31 17:07:15 -0700717 current->mm) {
David Rientjes0aad4b32010-08-09 17:18:59 -0700718 /*
719 * oom_kill_process() needs tasklist_lock held. If it returns
720 * non-zero, current could not be killed so we must fallback to
721 * the tasklist scan.
722 */
David Rientjesa63d83f2010-08-09 17:19:46 -0700723 if (!oom_kill_process(current, gfp_mask, order, 0, totalpages,
724 NULL, nodemask,
David Rientjes0aad4b32010-08-09 17:18:59 -0700725 "Out of memory (oom_kill_allocating_task)"))
KOSAKI Motohirob52723c2010-08-19 14:13:39 -0700726 goto out;
David Rientjes0aad4b32010-08-09 17:18:59 -0700727 }
728
729retry:
David Rientjese85bfd32010-09-22 13:05:10 -0700730 p = select_bad_process(&points, totalpages, NULL, mpol_mask);
David Rientjes0aad4b32010-08-09 17:18:59 -0700731 if (PTR_ERR(p) == -1UL)
KOSAKI Motohirob52723c2010-08-19 14:13:39 -0700732 goto out;
David Rientjes0aad4b32010-08-09 17:18:59 -0700733
734 /* Found nothing?!?! Either we hang forever, or we panic. */
735 if (!p) {
David Rientjese85bfd32010-09-22 13:05:10 -0700736 dump_header(NULL, gfp_mask, order, NULL, mpol_mask);
David Rientjes0aad4b32010-08-09 17:18:59 -0700737 read_unlock(&tasklist_lock);
738 panic("Out of memory and no killable processes...\n");
739 }
740
David Rientjesa63d83f2010-08-09 17:19:46 -0700741 if (oom_kill_process(p, gfp_mask, order, points, totalpages, NULL,
742 nodemask, "Out of memory"))
David Rientjes0aad4b32010-08-09 17:18:59 -0700743 goto retry;
KOSAKI Motohirob52723c2010-08-19 14:13:39 -0700744 killed = 1;
745out:
Andrew Morton140ffce2006-03-02 02:54:28 -0800746 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /*
749 * Give "p" a good chance of killing itself before we
Kirill Korotaev2f659f42006-01-08 01:01:05 -0800750 * retry to allocate memory unless "p" is current
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
KOSAKI Motohirob52723c2010-08-19 14:13:39 -0700752 if (killed && !test_thread_flag(TIF_MEMDIE))
Andrew Morton140ffce2006-03-02 02:54:28 -0800753 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
David Rientjese3658932010-08-09 17:18:55 -0700755
756/*
757 * The pagefault handler calls here because it is out of memory, so kill a
758 * memory-hogging task. If a populated zone has ZONE_OOM_LOCKED set, a parallel
759 * oom killing is already in progress so do nothing. If a task is found with
760 * TIF_MEMDIE set, it has been killed so do nothing and allow it to exit.
761 */
762void pagefault_out_of_memory(void)
763{
764 if (try_set_system_oom()) {
765 out_of_memory(NULL, 0, 0, NULL);
766 clear_system_oom();
767 }
768 if (!test_thread_flag(TIF_MEMDIE))
769 schedule_timeout_uninterruptible(1);
770}