Blame view

mm/oom_kill.c 29 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
  /*
   *  linux/mm/oom_kill.c
   * 
   *  Copyright (C)  1998,2000  Rik van Riel
   *	Thanks go out to Claus Fischer for some serious inspiration and
   *	for goading me into coding this file...
a63d83f42   David Rientjes   oom: badness heur...
7
8
   *  Copyright (C)  2010  Google, Inc.
   *	Rewritten by David Rientjes
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
   *
   *  The routines in this file are used to kill a process when
a49335cce   Paul Jackson   [PATCH] cpusets: ...
11
12
   *  we're seriously out of memory. This gets called from __alloc_pages()
   *  in mm/page_alloc.c when we really run out of memory.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
16
17
18
   *
   *  Since we won't call these routines often (on a well-configured
   *  machine) this file will double as a 'coding guide' and a signpost
   *  for newbie kernel hackers. It features several pointers to major
   *  kernel subsystems and hints as to where to find out what things do.
   */
8ac773b4f   Alexey Dobriyan   [PATCH] OOM kille...
19
  #include <linux/oom.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
  #include <linux/mm.h>
4e950f6f0   Alexey Dobriyan   Remove fs.h from ...
21
  #include <linux/err.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
22
  #include <linux/gfp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
25
26
  #include <linux/sched.h>
  #include <linux/swap.h>
  #include <linux/timex.h>
  #include <linux/jiffies.h>
ef08e3b49   Paul Jackson   [PATCH] cpusets: ...
27
  #include <linux/cpuset.h>
b95f1b31b   Paul Gortmaker   mm: Map most file...
28
  #include <linux/export.h>
8bc719d3c   Martin Schwidefsky   [PATCH] out of me...
29
  #include <linux/notifier.h>
c7ba5c9e8   Pavel Emelianov   Memory controller...
30
  #include <linux/memcontrol.h>
6f48d0ebd   David Rientjes   oom: select task ...
31
  #include <linux/mempolicy.h>
5cd9c58fb   David Howells   security: Fix set...
32
  #include <linux/security.h>
edd45544c   David Rientjes   oom: avoid deferr...
33
  #include <linux/ptrace.h>
f660daac4   David Rientjes   oom: thaw threads...
34
  #include <linux/freezer.h>
43d2b1132   KAMEZAWA Hiroyuki   tracepoint: add t...
35
  #include <linux/ftrace.h>
dc3f21ead   David Rientjes   mm, oom: introduc...
36
  #include <linux/ratelimit.h>
aac453635   Michal Hocko   mm, oom: introduc...
37
38
39
40
41
  #include <linux/kthread.h>
  #include <linux/init.h>
  
  #include <asm/tlb.h>
  #include "internal.h"
43d2b1132   KAMEZAWA Hiroyuki   tracepoint: add t...
42
43
44
  
  #define CREATE_TRACE_POINTS
  #include <trace/events/oom.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45

fadd8fbd1   KAMEZAWA Hiroyuki   [PATCH] support f...
46
  int sysctl_panic_on_oom;
fe071d7e8   David Rientjes   oom: add oom_kill...
47
  int sysctl_oom_kill_allocating_task;
ad915c432   David Rientjes   oom: enable oom t...
48
  int sysctl_oom_dump_tasks = 1;
dc56401fc   Johannes Weiner   mm: oom_kill: sim...
49
50
  
  DEFINE_MUTEX(oom_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51

6f48d0ebd   David Rientjes   oom: select task ...
52
53
54
  #ifdef CONFIG_NUMA
  /**
   * has_intersects_mems_allowed() - check task eligiblity for kill
ad9624417   Oleg Nesterov   oom_kill: has_int...
55
   * @start: task struct of which task to consider
6f48d0ebd   David Rientjes   oom: select task ...
56
57
58
59
60
   * @mask: nodemask passed to page allocator for mempolicy ooms
   *
   * Task eligibility is determined by whether or not a candidate task, @tsk,
   * shares the same mempolicy nodes as current if it is bound by such a policy
   * and whether or not it has the same set of allowed cpuset nodes.
495789a51   KOSAKI Motohiro   oom: make oom_sco...
61
   */
ad9624417   Oleg Nesterov   oom_kill: has_int...
62
  static bool has_intersects_mems_allowed(struct task_struct *start,
6f48d0ebd   David Rientjes   oom: select task ...
63
  					const nodemask_t *mask)
495789a51   KOSAKI Motohiro   oom: make oom_sco...
64
  {
ad9624417   Oleg Nesterov   oom_kill: has_int...
65
66
  	struct task_struct *tsk;
  	bool ret = false;
495789a51   KOSAKI Motohiro   oom: make oom_sco...
67

ad9624417   Oleg Nesterov   oom_kill: has_int...
68
  	rcu_read_lock();
1da4db0cd   Oleg Nesterov   oom_kill: change ...
69
  	for_each_thread(start, tsk) {
6f48d0ebd   David Rientjes   oom: select task ...
70
71
72
73
74
75
76
  		if (mask) {
  			/*
  			 * If this is a mempolicy constrained oom, tsk's
  			 * cpuset is irrelevant.  Only return true if its
  			 * mempolicy intersects current, otherwise it may be
  			 * needlessly killed.
  			 */
ad9624417   Oleg Nesterov   oom_kill: has_int...
77
  			ret = mempolicy_nodemask_intersects(tsk, mask);
6f48d0ebd   David Rientjes   oom: select task ...
78
79
80
81
82
  		} else {
  			/*
  			 * This is not a mempolicy constrained oom, so only
  			 * check the mems of tsk's cpuset.
  			 */
ad9624417   Oleg Nesterov   oom_kill: has_int...
83
  			ret = cpuset_mems_allowed_intersects(current, tsk);
6f48d0ebd   David Rientjes   oom: select task ...
84
  		}
ad9624417   Oleg Nesterov   oom_kill: has_int...
85
86
  		if (ret)
  			break;
1da4db0cd   Oleg Nesterov   oom_kill: change ...
87
  	}
ad9624417   Oleg Nesterov   oom_kill: has_int...
88
  	rcu_read_unlock();
df1090a8d   KOSAKI Motohiro   oom: cleanup has_...
89

ad9624417   Oleg Nesterov   oom_kill: has_int...
90
  	return ret;
6f48d0ebd   David Rientjes   oom: select task ...
91
92
93
94
95
96
  }
  #else
  static bool has_intersects_mems_allowed(struct task_struct *tsk,
  					const nodemask_t *mask)
  {
  	return true;
495789a51   KOSAKI Motohiro   oom: make oom_sco...
97
  }
6f48d0ebd   David Rientjes   oom: select task ...
98
  #endif /* CONFIG_NUMA */
495789a51   KOSAKI Motohiro   oom: make oom_sco...
99

6f48d0ebd   David Rientjes   oom: select task ...
100
101
102
103
104
105
  /*
   * The process p may have detached its own ->mm while exiting or through
   * use_mm(), but one or more of its subthreads may still have a valid
   * pointer.  Return p, or any of its subthreads with a valid ->mm, with
   * task_lock() held.
   */
158e0a2d1   KAMEZAWA Hiroyuki   memcg: use find_l...
106
  struct task_struct *find_lock_task_mm(struct task_struct *p)
dd8e8f405   Oleg Nesterov   oom: introduce fi...
107
  {
1da4db0cd   Oleg Nesterov   oom_kill: change ...
108
  	struct task_struct *t;
dd8e8f405   Oleg Nesterov   oom: introduce fi...
109

4d4048be8   Oleg Nesterov   oom_kill: add rcu...
110
  	rcu_read_lock();
1da4db0cd   Oleg Nesterov   oom_kill: change ...
111
  	for_each_thread(p, t) {
dd8e8f405   Oleg Nesterov   oom: introduce fi...
112
113
  		task_lock(t);
  		if (likely(t->mm))
4d4048be8   Oleg Nesterov   oom_kill: add rcu...
114
  			goto found;
dd8e8f405   Oleg Nesterov   oom: introduce fi...
115
  		task_unlock(t);
1da4db0cd   Oleg Nesterov   oom_kill: change ...
116
  	}
4d4048be8   Oleg Nesterov   oom_kill: add rcu...
117
118
119
  	t = NULL;
  found:
  	rcu_read_unlock();
dd8e8f405   Oleg Nesterov   oom: introduce fi...
120

4d4048be8   Oleg Nesterov   oom_kill: add rcu...
121
  	return t;
dd8e8f405   Oleg Nesterov   oom: introduce fi...
122
  }
db2a0dd7a   Yaowei Bai   mm/oom_kill.c: in...
123
124
125
126
127
128
129
130
  /*
   * order == -1 means the oom kill is required by sysrq, otherwise only
   * for display purposes.
   */
  static inline bool is_sysrq_oom(struct oom_control *oc)
  {
  	return oc->order == -1;
  }
ab290adba   KOSAKI Motohiro   oom: make oom_unk...
131
  /* return true if the task is not adequate as candidate victim task. */
e85bfd3aa   David Rientjes   oom: filter unkil...
132
  static bool oom_unkillable_task(struct task_struct *p,
2314b42db   Johannes Weiner   mm: memcontrol: d...
133
  		struct mem_cgroup *memcg, const nodemask_t *nodemask)
ab290adba   KOSAKI Motohiro   oom: make oom_unk...
134
135
136
137
138
139
140
  {
  	if (is_global_init(p))
  		return true;
  	if (p->flags & PF_KTHREAD)
  		return true;
  
  	/* When mem_cgroup_out_of_memory() and p is not member of the group */
72835c86c   Johannes Weiner   mm: unify remaini...
141
  	if (memcg && !task_in_mem_cgroup(p, memcg))
ab290adba   KOSAKI Motohiro   oom: make oom_unk...
142
143
144
145
146
147
148
149
  		return true;
  
  	/* p may not have freeable memory in nodemask */
  	if (!has_intersects_mems_allowed(p, nodemask))
  		return true;
  
  	return false;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
  /**
a63d83f42   David Rientjes   oom: badness heur...
151
   * oom_badness - heuristic function to determine which candidate task to kill
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
   * @p: task struct of which task we should calculate
a63d83f42   David Rientjes   oom: badness heur...
153
   * @totalpages: total present RAM allowed for page allocation
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
   *
a63d83f42   David Rientjes   oom: badness heur...
155
156
157
   * The heuristic for determining which task to kill is made to be as simple and
   * predictable as possible.  The goal is to return the highest value for the
   * task consuming the most memory to avoid subsequent oom failures.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
   */
a7f638f99   David Rientjes   mm, oom: normaliz...
159
160
  unsigned long oom_badness(struct task_struct *p, struct mem_cgroup *memcg,
  			  const nodemask_t *nodemask, unsigned long totalpages)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
  {
1e11ad8dc   David Rientjes   mm, oom: fix badn...
162
  	long points;
61eafb00d   David Rientjes   mm, oom: fix and ...
163
  	long adj;
28b83c519   KOSAKI Motohiro   oom: move oom_adj...
164

72835c86c   Johannes Weiner   mm: unify remaini...
165
  	if (oom_unkillable_task(p, memcg, nodemask))
26ebc9849   KOSAKI Motohiro   oom: /proc/<pid>/...
166
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167

dd8e8f405   Oleg Nesterov   oom: introduce fi...
168
169
  	p = find_lock_task_mm(p);
  	if (!p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
  		return 0;
bb8a4b7fd   Michal Hocko   mm, oom_reaper: h...
171
172
  	/*
  	 * Do not even consider tasks which are explicitly marked oom
b18dc5f29   Michal Hocko   mm, oom: skip vfo...
173
174
  	 * unkillable or have been already oom reaped or the are in
  	 * the middle of vfork
bb8a4b7fd   Michal Hocko   mm, oom_reaper: h...
175
  	 */
a9c58b907   David Rientjes   mm, oom: change t...
176
  	adj = (long)p->signal->oom_score_adj;
bb8a4b7fd   Michal Hocko   mm, oom_reaper: h...
177
  	if (adj == OOM_SCORE_ADJ_MIN ||
b18dc5f29   Michal Hocko   mm, oom: skip vfo...
178
179
  			test_bit(MMF_OOM_REAPED, &p->mm->flags) ||
  			in_vfork(p)) {
5aecc85ab   Michal Hocko   oom: do not kill ...
180
181
182
  		task_unlock(p);
  		return 0;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
  	/*
a63d83f42   David Rientjes   oom: badness heur...
184
  	 * The baseline for the badness score is the proportion of RAM that each
f755a042d   KOSAKI Motohiro   oom: use pte page...
185
  	 * task's rss, pagetable and swap space use.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186
  	 */
dc6c9a35b   Kirill A. Shutemov   mm: account pmd p...
187
188
  	points = get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) +
  		atomic_long_read(&p->mm->nr_ptes) + mm_nr_pmds(p->mm);
a63d83f42   David Rientjes   oom: badness heur...
189
  	task_unlock(p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190
191
  
  	/*
a63d83f42   David Rientjes   oom: badness heur...
192
193
  	 * Root processes get 3% bonus, just like the __vm_enough_memory()
  	 * implementation used by LSMs.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
  	 */
a63d83f42   David Rientjes   oom: badness heur...
195
  	if (has_capability_noaudit(p, CAP_SYS_ADMIN))
778c14aff   David Rientjes   mm, oom: base roo...
196
  		points -= (points * 3) / 100;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
197

61eafb00d   David Rientjes   mm, oom: fix and ...
198
199
200
  	/* Normalize to oom_score_adj units */
  	adj *= totalpages / 1000;
  	points += adj;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201

f19e8aa11   David Rientjes   oom: always retur...
202
  	/*
a7f638f99   David Rientjes   mm, oom: normaliz...
203
204
  	 * Never return 0 for an eligible task regardless of the root bonus and
  	 * oom_score_adj (oom_score_adj can't be OOM_SCORE_ADJ_MIN here).
f19e8aa11   David Rientjes   oom: always retur...
205
  	 */
1e11ad8dc   David Rientjes   mm, oom: fix badn...
206
  	return points > 0 ? points : 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
208
209
  }
  
  /*
9b0f8b040   Christoph Lameter   [PATCH] Terminate...
210
211
   * Determine the type of allocation constraint.
   */
9b0f8b040   Christoph Lameter   [PATCH] Terminate...
212
  #ifdef CONFIG_NUMA
6e0fc46dc   David Rientjes   mm, oom: organize...
213
214
  static enum oom_constraint constrained_alloc(struct oom_control *oc,
  					     unsigned long *totalpages)
4365a5676   KAMEZAWA Hiroyuki   oom-kill: fix NUM...
215
  {
54a6eb5c4   Mel Gorman   mm: use two zonel...
216
  	struct zone *zone;
dd1a239f6   Mel Gorman   mm: have zonelist...
217
  	struct zoneref *z;
6e0fc46dc   David Rientjes   mm, oom: organize...
218
  	enum zone_type high_zoneidx = gfp_zone(oc->gfp_mask);
a63d83f42   David Rientjes   oom: badness heur...
219
220
  	bool cpuset_limited = false;
  	int nid;
9b0f8b040   Christoph Lameter   [PATCH] Terminate...
221

a63d83f42   David Rientjes   oom: badness heur...
222
223
  	/* Default to all available memory */
  	*totalpages = totalram_pages + total_swap_pages;
6e0fc46dc   David Rientjes   mm, oom: organize...
224
  	if (!oc->zonelist)
a63d83f42   David Rientjes   oom: badness heur...
225
  		return CONSTRAINT_NONE;
4365a5676   KAMEZAWA Hiroyuki   oom-kill: fix NUM...
226
227
228
229
230
  	/*
  	 * Reach here only when __GFP_NOFAIL is used. So, we should avoid
  	 * to kill current.We have to random task kill in this case.
  	 * Hopefully, CONSTRAINT_THISNODE...but no way to handle it, now.
  	 */
6e0fc46dc   David Rientjes   mm, oom: organize...
231
  	if (oc->gfp_mask & __GFP_THISNODE)
4365a5676   KAMEZAWA Hiroyuki   oom-kill: fix NUM...
232
  		return CONSTRAINT_NONE;
9b0f8b040   Christoph Lameter   [PATCH] Terminate...
233

4365a5676   KAMEZAWA Hiroyuki   oom-kill: fix NUM...
234
  	/*
a63d83f42   David Rientjes   oom: badness heur...
235
236
237
  	 * This is not a __GFP_THISNODE allocation, so a truncated nodemask in
  	 * the page allocator means a mempolicy is in effect.  Cpuset policy
  	 * is enforced in get_page_from_freelist().
4365a5676   KAMEZAWA Hiroyuki   oom-kill: fix NUM...
238
  	 */
6e0fc46dc   David Rientjes   mm, oom: organize...
239
240
  	if (oc->nodemask &&
  	    !nodes_subset(node_states[N_MEMORY], *oc->nodemask)) {
a63d83f42   David Rientjes   oom: badness heur...
241
  		*totalpages = total_swap_pages;
6e0fc46dc   David Rientjes   mm, oom: organize...
242
  		for_each_node_mask(nid, *oc->nodemask)
a63d83f42   David Rientjes   oom: badness heur...
243
  			*totalpages += node_spanned_pages(nid);
9b0f8b040   Christoph Lameter   [PATCH] Terminate...
244
  		return CONSTRAINT_MEMORY_POLICY;
a63d83f42   David Rientjes   oom: badness heur...
245
  	}
4365a5676   KAMEZAWA Hiroyuki   oom-kill: fix NUM...
246
247
  
  	/* Check this allocation failure is caused by cpuset's wall function */
6e0fc46dc   David Rientjes   mm, oom: organize...
248
249
250
  	for_each_zone_zonelist_nodemask(zone, z, oc->zonelist,
  			high_zoneidx, oc->nodemask)
  		if (!cpuset_zone_allowed(zone, oc->gfp_mask))
a63d83f42   David Rientjes   oom: badness heur...
251
  			cpuset_limited = true;
9b0f8b040   Christoph Lameter   [PATCH] Terminate...
252

a63d83f42   David Rientjes   oom: badness heur...
253
254
255
256
257
258
  	if (cpuset_limited) {
  		*totalpages = total_swap_pages;
  		for_each_node_mask(nid, cpuset_current_mems_allowed)
  			*totalpages += node_spanned_pages(nid);
  		return CONSTRAINT_CPUSET;
  	}
9b0f8b040   Christoph Lameter   [PATCH] Terminate...
259
260
  	return CONSTRAINT_NONE;
  }
4365a5676   KAMEZAWA Hiroyuki   oom-kill: fix NUM...
261
  #else
6e0fc46dc   David Rientjes   mm, oom: organize...
262
263
  static enum oom_constraint constrained_alloc(struct oom_control *oc,
  					     unsigned long *totalpages)
4365a5676   KAMEZAWA Hiroyuki   oom-kill: fix NUM...
264
  {
a63d83f42   David Rientjes   oom: badness heur...
265
  	*totalpages = totalram_pages + total_swap_pages;
4365a5676   KAMEZAWA Hiroyuki   oom-kill: fix NUM...
266
267
268
  	return CONSTRAINT_NONE;
  }
  #endif
9b0f8b040   Christoph Lameter   [PATCH] Terminate...
269

6e0fc46dc   David Rientjes   mm, oom: organize...
270
  enum oom_scan_t oom_scan_process_thread(struct oom_control *oc,
fbe84a09d   Tetsuo Handa   mm,oom: remove un...
271
  					struct task_struct *task)
462607ecc   David Rientjes   mm, oom: introduc...
272
  {
6e0fc46dc   David Rientjes   mm, oom: organize...
273
  	if (oom_unkillable_task(task, NULL, oc->nodemask))
462607ecc   David Rientjes   mm, oom: introduc...
274
275
276
277
  		return OOM_SCAN_CONTINUE;
  
  	/*
  	 * This task already has access to memory reserves and is being killed.
a373966d1   Michal Hocko   mm, oom: hide mm ...
278
279
280
  	 * Don't allow any other task to have access to the reserves unless
  	 * the task has MMF_OOM_REAPED because chances that it would release
  	 * any memory is quite low.
462607ecc   David Rientjes   mm, oom: introduc...
281
  	 */
a373966d1   Michal Hocko   mm, oom: hide mm ...
282
283
284
285
286
287
288
289
290
291
292
293
  	if (!is_sysrq_oom(oc) && atomic_read(&task->signal->oom_victims)) {
  		struct task_struct *p = find_lock_task_mm(task);
  		enum oom_scan_t ret = OOM_SCAN_ABORT;
  
  		if (p) {
  			if (test_bit(MMF_OOM_REAPED, &p->mm->flags))
  				ret = OOM_SCAN_CONTINUE;
  			task_unlock(p);
  		}
  
  		return ret;
  	}
462607ecc   David Rientjes   mm, oom: introduc...
294

e1e12d2f3   David Rientjes   mm, oom: fix race...
295
296
297
298
299
300
  	/*
  	 * If task is allocating a lot of memory and has been marked to be
  	 * killed first if it triggers an oom, then select it.
  	 */
  	if (oom_task_origin(task))
  		return OOM_SCAN_SELECT;
462607ecc   David Rientjes   mm, oom: introduc...
301
302
  	return OOM_SCAN_OK;
  }
9b0f8b040   Christoph Lameter   [PATCH] Terminate...
303
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
304
   * Simple selection loop. We chose the process with the highest
6b4f2b56a   Rusty Russell   mm/oom_kill: remo...
305
   * number of 'points'.  Returns -1 on scan abort.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
306
   */
6e0fc46dc   David Rientjes   mm, oom: organize...
307
308
  static struct task_struct *select_bad_process(struct oom_control *oc,
  		unsigned int *ppoints, unsigned long totalpages)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
309
  {
f44666b04   Tetsuo Handa   mm,oom: speed up ...
310
  	struct task_struct *p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
311
  	struct task_struct *chosen = NULL;
a7f638f99   David Rientjes   mm, oom: normaliz...
312
  	unsigned long chosen_points = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
313

6b0c81b3b   David Rientjes   mm, oom: reduce d...
314
  	rcu_read_lock();
f44666b04   Tetsuo Handa   mm,oom: speed up ...
315
  	for_each_process(p) {
a63d83f42   David Rientjes   oom: badness heur...
316
  		unsigned int points;
a49335cce   Paul Jackson   [PATCH] cpusets: ...
317

fbe84a09d   Tetsuo Handa   mm,oom: remove un...
318
  		switch (oom_scan_process_thread(oc, p)) {
462607ecc   David Rientjes   mm, oom: introduc...
319
320
321
322
323
  		case OOM_SCAN_SELECT:
  			chosen = p;
  			chosen_points = ULONG_MAX;
  			/* fall through */
  		case OOM_SCAN_CONTINUE:
c027a474a   Oleg Nesterov   oom: task->mm == ...
324
  			continue;
462607ecc   David Rientjes   mm, oom: introduc...
325
  		case OOM_SCAN_ABORT:
6b0c81b3b   David Rientjes   mm, oom: reduce d...
326
  			rcu_read_unlock();
6b4f2b56a   Rusty Russell   mm/oom_kill: remo...
327
  			return (struct task_struct *)(-1UL);
462607ecc   David Rientjes   mm, oom: introduc...
328
329
330
  		case OOM_SCAN_OK:
  			break;
  		};
6e0fc46dc   David Rientjes   mm, oom: organize...
331
  		points = oom_badness(p, NULL, oc->nodemask, totalpages);
d49ad9355   David Rientjes   mm, oom: prefer t...
332
333
  		if (!points || points < chosen_points)
  			continue;
d49ad9355   David Rientjes   mm, oom: prefer t...
334
335
336
  
  		chosen = p;
  		chosen_points = points;
1da4db0cd   Oleg Nesterov   oom_kill: change ...
337
  	}
6b0c81b3b   David Rientjes   mm, oom: reduce d...
338
339
340
  	if (chosen)
  		get_task_struct(chosen);
  	rcu_read_unlock();
972c4ea59   Oleg Nesterov   [PATCH] select_ba...
341

a7f638f99   David Rientjes   mm, oom: normaliz...
342
  	*ppoints = chosen_points * 1000 / totalpages;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
344
345
346
  	return chosen;
  }
  
  /**
1b578df02   Randy Dunlap   mm/oom_kill: fix ...
347
   * dump_tasks - dump current memory state of all system tasks
dad7557eb   Wanpeng Li   mm: fix kernel-do...
348
   * @memcg: current's memory controller, if constrained
e85bfd3aa   David Rientjes   oom: filter unkil...
349
   * @nodemask: nodemask passed to page allocator for mempolicy ooms
1b578df02   Randy Dunlap   mm/oom_kill: fix ...
350
   *
e85bfd3aa   David Rientjes   oom: filter unkil...
351
352
353
   * Dumps the current memory state of all eligible tasks.  Tasks not in the same
   * memcg, not in the same cpuset, or bound to a disjoint set of mempolicy nodes
   * are not shown.
de34d965a   David Rientjes   mm, oom: replace ...
354
355
   * State information includes task's pid, uid, tgid, vm size, rss, nr_ptes,
   * swapents, oom_score_adj value, and name.
fef1bdd68   David Rientjes   oom: add sysctl t...
356
   */
2314b42db   Johannes Weiner   mm: memcontrol: d...
357
  static void dump_tasks(struct mem_cgroup *memcg, const nodemask_t *nodemask)
fef1bdd68   David Rientjes   oom: add sysctl t...
358
  {
c55db9578   KOSAKI Motohiro   oom: dump_tasks u...
359
360
  	struct task_struct *p;
  	struct task_struct *task;
fef1bdd68   David Rientjes   oom: add sysctl t...
361

dc6c9a35b   Kirill A. Shutemov   mm: account pmd p...
362
363
  	pr_info("[ pid ]   uid  tgid total_vm      rss nr_ptes nr_pmds swapents oom_score_adj name
  ");
6b0c81b3b   David Rientjes   mm, oom: reduce d...
364
  	rcu_read_lock();
c55db9578   KOSAKI Motohiro   oom: dump_tasks u...
365
  	for_each_process(p) {
72835c86c   Johannes Weiner   mm: unify remaini...
366
  		if (oom_unkillable_task(p, memcg, nodemask))
b4416d2be   David Rientjes   oom: do not dump ...
367
  			continue;
fef1bdd68   David Rientjes   oom: add sysctl t...
368

c55db9578   KOSAKI Motohiro   oom: dump_tasks u...
369
370
  		task = find_lock_task_mm(p);
  		if (!task) {
6d2661ede   David Rientjes   oom: fix possible...
371
  			/*
74ab7f1d3   David Rientjes   oom: improve comm...
372
373
  			 * This is a kthread or all of p's threads have already
  			 * detached their mm's.  There's no need to report
c55db9578   KOSAKI Motohiro   oom: dump_tasks u...
374
  			 * them; they can't be oom killed anyway.
6d2661ede   David Rientjes   oom: fix possible...
375
  			 */
6d2661ede   David Rientjes   oom: fix possible...
376
377
  			continue;
  		}
c55db9578   KOSAKI Motohiro   oom: dump_tasks u...
378

dc6c9a35b   Kirill A. Shutemov   mm: account pmd p...
379
380
  		pr_info("[%5d] %5d %5d %8lu %8lu %7ld %7ld %8lu         %5hd %s
  ",
078de5f70   Eric W. Biederman   userns: Store uid...
381
382
  			task->pid, from_kuid(&init_user_ns, task_uid(task)),
  			task->tgid, task->mm->total_vm, get_mm_rss(task->mm),
e1f56c89b   Kirill A. Shutemov   mm: convert mm->n...
383
  			atomic_long_read(&task->mm->nr_ptes),
dc6c9a35b   Kirill A. Shutemov   mm: account pmd p...
384
  			mm_nr_pmds(task->mm),
de34d965a   David Rientjes   mm, oom: replace ...
385
  			get_mm_counter(task->mm, MM_SWAPENTS),
a63d83f42   David Rientjes   oom: badness heur...
386
  			task->signal->oom_score_adj, task->comm);
c55db9578   KOSAKI Motohiro   oom: dump_tasks u...
387
388
  		task_unlock(task);
  	}
6b0c81b3b   David Rientjes   mm, oom: reduce d...
389
  	rcu_read_unlock();
fef1bdd68   David Rientjes   oom: add sysctl t...
390
  }
2a966b77a   Vladimir Davydov   mm: oom: add memc...
391
  static void dump_header(struct oom_control *oc, struct task_struct *p)
1b604d75b   David Rientjes   oom: dump stack a...
392
  {
756a025f0   Joe Perches   mm: coalesce spli...
393
394
  	pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd
  ",
a0795cd41   Vlastimil Babka   mm, oom: print sy...
395
  		current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order,
a63d83f42   David Rientjes   oom: badness heur...
396
  		current->signal->oom_score_adj);
a0795cd41   Vlastimil Babka   mm, oom: print sy...
397

da39da3a5   David Rientjes   mm, oom: remove t...
398
  	cpuset_print_current_mems_allowed();
1b604d75b   David Rientjes   oom: dump stack a...
399
  	dump_stack();
2a966b77a   Vladimir Davydov   mm: oom: add memc...
400
401
  	if (oc->memcg)
  		mem_cgroup_print_oom_info(oc->memcg, p);
58cf188ed   Sha Zhengju   memcg, oom: provi...
402
403
  	else
  		show_mem(SHOW_MEM_FILTER_NODES);
1b604d75b   David Rientjes   oom: dump stack a...
404
  	if (sysctl_oom_dump_tasks)
2a966b77a   Vladimir Davydov   mm: oom: add memc...
405
  		dump_tasks(oc->memcg, oc->nodemask);
1b604d75b   David Rientjes   oom: dump stack a...
406
  }
5695be142   Michal Hocko   OOM, PM: OOM kill...
407
  /*
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
408
   * Number of OOM victims in flight
5695be142   Michal Hocko   OOM, PM: OOM kill...
409
   */
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
410
411
  static atomic_t oom_victims = ATOMIC_INIT(0);
  static DECLARE_WAIT_QUEUE_HEAD(oom_victims_wait);
5695be142   Michal Hocko   OOM, PM: OOM kill...
412

c32b3cbe0   Michal Hocko   oom, PM: make OOM...
413
  bool oom_killer_disabled __read_mostly;
5695be142   Michal Hocko   OOM, PM: OOM kill...
414

bc448e897   Michal Hocko   mm, oom_reaper: r...
415
  #define K(x) ((x) << (PAGE_SHIFT-10))
3ef22dfff   Michal Hocko   oom, oom_reaper: ...
416
417
418
419
420
421
  /*
   * task->mm can be NULL if the task is the exited group leader.  So to
   * determine whether the task is using a particular mm, we examine all the
   * task's threads: if one of those is using this mm then this task was also
   * using it.
   */
44a70adec   Michal Hocko   mm, oom_adj: make...
422
  bool process_shares_mm(struct task_struct *p, struct mm_struct *mm)
3ef22dfff   Michal Hocko   oom, oom_reaper: ...
423
424
425
426
427
428
429
430
431
432
  {
  	struct task_struct *t;
  
  	for_each_thread(p, t) {
  		struct mm_struct *t_mm = READ_ONCE(t->mm);
  		if (t_mm)
  			return t_mm == mm;
  	}
  	return false;
  }
aac453635   Michal Hocko   mm, oom: introduc...
433
434
435
436
437
438
  #ifdef CONFIG_MMU
  /*
   * OOM Reaper kernel thread which tries to reap the memory used by the OOM
   * victim (if that is possible) to help the OOM killer to move on.
   */
  static struct task_struct *oom_reaper_th;
aac453635   Michal Hocko   mm, oom: introduc...
439
  static DECLARE_WAIT_QUEUE_HEAD(oom_reaper_wait);
29c696e1c   Vladimir Davydov   oom: make oom_rea...
440
  static struct task_struct *oom_reaper_list;
03049269d   Michal Hocko   mm, oom_reaper: i...
441
  static DEFINE_SPINLOCK(oom_reaper_lock);
36324a990   Michal Hocko   oom: clear TIF_ME...
442
  static bool __oom_reap_task(struct task_struct *tsk)
aac453635   Michal Hocko   mm, oom: introduc...
443
444
445
  {
  	struct mmu_gather tlb;
  	struct vm_area_struct *vma;
e2fe14564   Michal Hocko   oom_reaper: close...
446
  	struct mm_struct *mm = NULL;
36324a990   Michal Hocko   oom: clear TIF_ME...
447
  	struct task_struct *p;
aac453635   Michal Hocko   mm, oom: introduc...
448
449
450
  	struct zap_details details = {.check_swap_entries = true,
  				      .ignore_dirty = true};
  	bool ret = true;
36324a990   Michal Hocko   oom: clear TIF_ME...
451
  	/*
e2fe14564   Michal Hocko   oom_reaper: close...
452
453
454
  	 * We have to make sure to not race with the victim exit path
  	 * and cause premature new oom victim selection:
  	 * __oom_reap_task		exit_mm
e5e3f4c4f   Michal Hocko   mm, oom_reaper: m...
455
  	 *   mmget_not_zero
e2fe14564   Michal Hocko   oom_reaper: close...
456
457
458
459
460
461
462
463
464
465
466
467
  	 *				  mmput
  	 *				    atomic_dec_and_test
  	 *				  exit_oom_victim
  	 *				[...]
  	 *				out_of_memory
  	 *				  select_bad_process
  	 *				    # no TIF_MEMDIE task selects new victim
  	 *  unmap_page_range # frees some memory
  	 */
  	mutex_lock(&oom_lock);
  
  	/*
36324a990   Michal Hocko   oom: clear TIF_ME...
468
469
470
471
472
473
474
  	 * Make sure we find the associated mm_struct even when the particular
  	 * thread has already terminated and cleared its mm.
  	 * We might have race with exit path so consider our work done if there
  	 * is no mm.
  	 */
  	p = find_lock_task_mm(tsk);
  	if (!p)
e2fe14564   Michal Hocko   oom_reaper: close...
475
  		goto unlock_oom;
36324a990   Michal Hocko   oom: clear TIF_ME...
476
  	mm = p->mm;
e5e3f4c4f   Michal Hocko   mm, oom_reaper: m...
477
  	atomic_inc(&mm->mm_count);
36324a990   Michal Hocko   oom: clear TIF_ME...
478
  	task_unlock(p);
aac453635   Michal Hocko   mm, oom: introduc...
479
480
481
  
  	if (!down_read_trylock(&mm->mmap_sem)) {
  		ret = false;
e5e3f4c4f   Michal Hocko   mm, oom_reaper: m...
482
483
484
485
486
487
488
489
490
491
492
  		goto mm_drop;
  	}
  
  	/*
  	 * increase mm_users only after we know we will reap something so
  	 * that the mmput_async is called only when we have reaped something
  	 * and delayed __mmput doesn't matter that much
  	 */
  	if (!mmget_not_zero(mm)) {
  		up_read(&mm->mmap_sem);
  		goto mm_drop;
aac453635   Michal Hocko   mm, oom: introduc...
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
  	}
  
  	tlb_gather_mmu(&tlb, mm, 0, -1);
  	for (vma = mm->mmap ; vma; vma = vma->vm_next) {
  		if (is_vm_hugetlb_page(vma))
  			continue;
  
  		/*
  		 * mlocked VMAs require explicit munlocking before unmap.
  		 * Let's keep it simple here and skip such VMAs.
  		 */
  		if (vma->vm_flags & VM_LOCKED)
  			continue;
  
  		/*
  		 * Only anonymous pages have a good chance to be dropped
  		 * without additional steps which we cannot afford as we
  		 * are OOM already.
  		 *
  		 * We do not even care about fs backed pages because all
  		 * which are reclaimable have already been reclaimed and
  		 * we do not want to block exit_mmap by keeping mm ref
  		 * count elevated without a good reason.
  		 */
  		if (vma_is_anonymous(vma) || !(vma->vm_flags & VM_SHARED))
  			unmap_page_range(&tlb, vma, vma->vm_start, vma->vm_end,
  					 &details);
  	}
  	tlb_finish_mmu(&tlb, 0, -1);
bc448e897   Michal Hocko   mm, oom_reaper: r...
522
523
524
525
526
527
  	pr_info("oom_reaper: reaped process %d (%s), now anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB
  ",
  			task_pid_nr(tsk), tsk->comm,
  			K(get_mm_counter(mm, MM_ANONPAGES)),
  			K(get_mm_counter(mm, MM_FILEPAGES)),
  			K(get_mm_counter(mm, MM_SHMEMPAGES)));
aac453635   Michal Hocko   mm, oom: introduc...
528
  	up_read(&mm->mmap_sem);
36324a990   Michal Hocko   oom: clear TIF_ME...
529
530
  
  	/*
449d777d7   Michal Hocko   mm, oom_reaper: c...
531
532
  	 * This task can be safely ignored because we cannot do much more
  	 * to release its memory.
36324a990   Michal Hocko   oom: clear TIF_ME...
533
  	 */
bb8a4b7fd   Michal Hocko   mm, oom_reaper: h...
534
  	set_bit(MMF_OOM_REAPED, &mm->flags);
ec8d7c14e   Michal Hocko   mm, oom_reaper: d...
535
536
537
538
539
  	/*
  	 * Drop our reference but make sure the mmput slow path is called from a
  	 * different context because we shouldn't risk we get stuck there and
  	 * put the oom_reaper out of the way.
  	 */
e5e3f4c4f   Michal Hocko   mm, oom_reaper: m...
540
541
542
543
544
  	mmput_async(mm);
  mm_drop:
  	mmdrop(mm);
  unlock_oom:
  	mutex_unlock(&oom_lock);
aac453635   Michal Hocko   mm, oom: introduc...
545
546
  	return ret;
  }
bc448e897   Michal Hocko   mm, oom_reaper: r...
547
  #define MAX_OOM_REAP_RETRIES 10
36324a990   Michal Hocko   oom: clear TIF_ME...
548
  static void oom_reap_task(struct task_struct *tsk)
aac453635   Michal Hocko   mm, oom: introduc...
549
550
551
552
  {
  	int attempts = 0;
  
  	/* Retry the down_read_trylock(mmap_sem) a few times */
bc448e897   Michal Hocko   mm, oom_reaper: r...
553
  	while (attempts++ < MAX_OOM_REAP_RETRIES && !__oom_reap_task(tsk))
aac453635   Michal Hocko   mm, oom: introduc...
554
  		schedule_timeout_idle(HZ/10);
bc448e897   Michal Hocko   mm, oom_reaper: r...
555
  	if (attempts > MAX_OOM_REAP_RETRIES) {
11a410d51   Michal Hocko   mm, oom_reaper: d...
556
  		struct task_struct *p;
bc448e897   Michal Hocko   mm, oom_reaper: r...
557
558
559
  		pr_info("oom_reaper: unable to reap pid:%d (%s)
  ",
  				task_pid_nr(tsk), tsk->comm);
11a410d51   Michal Hocko   mm, oom_reaper: d...
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
  
  		/*
  		 * If we've already tried to reap this task in the past and
  		 * failed it probably doesn't make much sense to try yet again
  		 * so hide the mm from the oom killer so that it can move on
  		 * to another task with a different mm struct.
  		 */
  		p = find_lock_task_mm(tsk);
  		if (p) {
  			if (test_and_set_bit(MMF_OOM_NOT_REAPABLE, &p->mm->flags)) {
  				pr_info("oom_reaper: giving up pid:%d (%s)
  ",
  						task_pid_nr(tsk), tsk->comm);
  				set_bit(MMF_OOM_REAPED, &p->mm->flags);
  			}
  			task_unlock(p);
  		}
bc448e897   Michal Hocko   mm, oom_reaper: r...
577
578
  		debug_show_all_locks();
  	}
449d777d7   Michal Hocko   mm, oom_reaper: c...
579
580
581
582
583
584
585
586
  	/*
  	 * Clear TIF_MEMDIE because the task shouldn't be sitting on a
  	 * reasonably reclaimable memory anymore or it is not a good candidate
  	 * for the oom victim right now because it cannot release its memory
  	 * itself nor by the oom reaper.
  	 */
  	tsk->oom_reaper_list = NULL;
  	exit_oom_victim(tsk);
aac453635   Michal Hocko   mm, oom: introduc...
587
  	/* Drop a reference taken by wake_oom_reaper */
36324a990   Michal Hocko   oom: clear TIF_ME...
588
  	put_task_struct(tsk);
aac453635   Michal Hocko   mm, oom: introduc...
589
590
591
592
  }
  
  static int oom_reaper(void *unused)
  {
e26796066   Michal Hocko   oom: make oom_rea...
593
  	set_freezable();
aac453635   Michal Hocko   mm, oom: introduc...
594
  	while (true) {
03049269d   Michal Hocko   mm, oom_reaper: i...
595
  		struct task_struct *tsk = NULL;
aac453635   Michal Hocko   mm, oom: introduc...
596

29c696e1c   Vladimir Davydov   oom: make oom_rea...
597
  		wait_event_freezable(oom_reaper_wait, oom_reaper_list != NULL);
03049269d   Michal Hocko   mm, oom_reaper: i...
598
  		spin_lock(&oom_reaper_lock);
29c696e1c   Vladimir Davydov   oom: make oom_rea...
599
600
601
  		if (oom_reaper_list != NULL) {
  			tsk = oom_reaper_list;
  			oom_reaper_list = tsk->oom_reaper_list;
03049269d   Michal Hocko   mm, oom_reaper: i...
602
603
604
605
606
  		}
  		spin_unlock(&oom_reaper_lock);
  
  		if (tsk)
  			oom_reap_task(tsk);
aac453635   Michal Hocko   mm, oom: introduc...
607
608
609
610
  	}
  
  	return 0;
  }
1af8bb432   Michal Hocko   mm, oom: fortify ...
611
  void wake_oom_reaper(struct task_struct *tsk)
aac453635   Michal Hocko   mm, oom: introduc...
612
  {
af8e15cc8   Michal Hocko   oom, oom_reaper: ...
613
614
615
616
617
  	if (!oom_reaper_th)
  		return;
  
  	/* tsk is already queued? */
  	if (tsk == oom_reaper_list || tsk->oom_reaper_list)
aac453635   Michal Hocko   mm, oom: introduc...
618
  		return;
36324a990   Michal Hocko   oom: clear TIF_ME...
619
  	get_task_struct(tsk);
aac453635   Michal Hocko   mm, oom: introduc...
620

03049269d   Michal Hocko   mm, oom_reaper: i...
621
  	spin_lock(&oom_reaper_lock);
29c696e1c   Vladimir Davydov   oom: make oom_rea...
622
623
  	tsk->oom_reaper_list = oom_reaper_list;
  	oom_reaper_list = tsk;
03049269d   Michal Hocko   mm, oom_reaper: i...
624
625
  	spin_unlock(&oom_reaper_lock);
  	wake_up(&oom_reaper_wait);
aac453635   Michal Hocko   mm, oom: introduc...
626
627
628
629
630
631
632
633
634
635
636
637
638
639
  }
  
  static int __init oom_init(void)
  {
  	oom_reaper_th = kthread_run(oom_reaper, NULL, "oom_reaper");
  	if (IS_ERR(oom_reaper_th)) {
  		pr_err("Unable to start OOM reaper %ld. Continuing regardless
  ",
  				PTR_ERR(oom_reaper_th));
  		oom_reaper_th = NULL;
  	}
  	return 0;
  }
  subsys_initcall(oom_init)
aac453635   Michal Hocko   mm, oom: introduc...
640
  #endif
49550b605   Michal Hocko   oom: add helpers ...
641
  /**
16e951966   Johannes Weiner   mm: oom_kill: cle...
642
   * mark_oom_victim - mark the given task as OOM victim
49550b605   Michal Hocko   oom: add helpers ...
643
   * @tsk: task to mark
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
644
   *
dc56401fc   Johannes Weiner   mm: oom_kill: sim...
645
   * Has to be called with oom_lock held and never after
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
646
   * oom has been disabled already.
49550b605   Michal Hocko   oom: add helpers ...
647
   */
16e951966   Johannes Weiner   mm: oom_kill: cle...
648
  void mark_oom_victim(struct task_struct *tsk)
49550b605   Michal Hocko   oom: add helpers ...
649
  {
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
650
651
652
653
  	WARN_ON(oom_killer_disabled);
  	/* OOM killer might race with memcg OOM */
  	if (test_and_set_tsk_thread_flag(tsk, TIF_MEMDIE))
  		return;
f44666b04   Tetsuo Handa   mm,oom: speed up ...
654
  	atomic_inc(&tsk->signal->oom_victims);
63a8ca9b2   Michal Hocko   oom: thaw the OOM...
655
656
657
658
659
660
661
  	/*
  	 * Make sure that the task is woken up from uninterruptible sleep
  	 * if it is frozen because OOM killer wouldn't be able to free
  	 * any memory and livelock. freezing_slow_path will tell the freezer
  	 * that TIF_MEMDIE tasks should be ignored.
  	 */
  	__thaw_task(tsk);
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
662
  	atomic_inc(&oom_victims);
49550b605   Michal Hocko   oom: add helpers ...
663
664
665
  }
  
  /**
16e951966   Johannes Weiner   mm: oom_kill: cle...
666
   * exit_oom_victim - note the exit of an OOM victim
49550b605   Michal Hocko   oom: add helpers ...
667
   */
36324a990   Michal Hocko   oom: clear TIF_ME...
668
  void exit_oom_victim(struct task_struct *tsk)
49550b605   Michal Hocko   oom: add helpers ...
669
  {
36324a990   Michal Hocko   oom: clear TIF_ME...
670
671
  	if (!test_and_clear_tsk_thread_flag(tsk, TIF_MEMDIE))
  		return;
f44666b04   Tetsuo Handa   mm,oom: speed up ...
672
  	atomic_dec(&tsk->signal->oom_victims);
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
673

c38f1025f   Johannes Weiner   mm: oom_kill: gen...
674
  	if (!atomic_dec_return(&oom_victims))
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
675
  		wake_up_all(&oom_victims_wait);
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
  }
  
  /**
   * oom_killer_disable - disable OOM killer
   *
   * Forces all page allocations to fail rather than trigger OOM killer.
   * Will block and wait until all OOM victims are killed.
   *
   * The function cannot be called when there are runnable user tasks because
   * the userspace would see unexpected allocation failures as a result. Any
   * new usage of this function should be consulted with MM people.
   *
   * Returns true if successful and false if the OOM killer cannot be
   * disabled.
   */
  bool oom_killer_disable(void)
  {
  	/*
6afcf2895   Tetsuo Handa   mm,oom: make oom_...
694
695
  	 * Make sure to not race with an ongoing OOM killer. Check that the
  	 * current is not killed (possibly due to sharing the victim's memory).
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
696
  	 */
6afcf2895   Tetsuo Handa   mm,oom: make oom_...
697
  	if (mutex_lock_killable(&oom_lock))
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
698
  		return false;
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
699
  	oom_killer_disabled = true;
dc56401fc   Johannes Weiner   mm: oom_kill: sim...
700
  	mutex_unlock(&oom_lock);
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
701
702
703
704
705
706
707
708
709
710
711
  
  	wait_event(oom_victims_wait, !atomic_read(&oom_victims));
  
  	return true;
  }
  
  /**
   * oom_killer_enable - enable OOM killer
   */
  void oom_killer_enable(void)
  {
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
712
  	oom_killer_disabled = false;
49550b605   Michal Hocko   oom: add helpers ...
713
  }
1af8bb432   Michal Hocko   mm, oom: fortify ...
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
  static inline bool __task_will_free_mem(struct task_struct *task)
  {
  	struct signal_struct *sig = task->signal;
  
  	/*
  	 * A coredumping process may sleep for an extended period in exit_mm(),
  	 * so the oom killer cannot assume that the process will promptly exit
  	 * and release memory.
  	 */
  	if (sig->flags & SIGNAL_GROUP_COREDUMP)
  		return false;
  
  	if (sig->flags & SIGNAL_GROUP_EXIT)
  		return true;
  
  	if (thread_group_empty(task) && (task->flags & PF_EXITING))
  		return true;
  
  	return false;
  }
  
  /*
   * Checks whether the given task is dying or exiting and likely to
   * release its address space. This means that all threads and processes
   * sharing the same mm have to be killed or exiting.
091f362c5   Michal Hocko   mm, oom: tighten ...
739
740
   * Caller has to make sure that task->mm is stable (hold task_lock or
   * it operates on the current).
1af8bb432   Michal Hocko   mm, oom: fortify ...
741
742
743
   */
  bool task_will_free_mem(struct task_struct *task)
  {
091f362c5   Michal Hocko   mm, oom: tighten ...
744
  	struct mm_struct *mm = task->mm;
1af8bb432   Michal Hocko   mm, oom: fortify ...
745
  	struct task_struct *p;
f33e6f067   Geert Uytterhoeven   mm, oom: fix unin...
746
  	bool ret = true;
1af8bb432   Michal Hocko   mm, oom: fortify ...
747

1af8bb432   Michal Hocko   mm, oom: fortify ...
748
  	/*
091f362c5   Michal Hocko   mm, oom: tighten ...
749
750
751
  	 * Skip tasks without mm because it might have passed its exit_mm and
  	 * exit_oom_victim. oom_reaper could have rescued that but do not rely
  	 * on that for now. We can consider find_lock_task_mm in future.
1af8bb432   Michal Hocko   mm, oom: fortify ...
752
  	 */
091f362c5   Michal Hocko   mm, oom: tighten ...
753
  	if (!mm)
1af8bb432   Michal Hocko   mm, oom: fortify ...
754
  		return false;
091f362c5   Michal Hocko   mm, oom: tighten ...
755
756
  	if (!__task_will_free_mem(task))
  		return false;
696453e66   Michal Hocko   mm, oom: task_wil...
757
758
759
760
761
  
  	/*
  	 * This task has already been drained by the oom reaper so there are
  	 * only small chances it will free some more
  	 */
091f362c5   Michal Hocko   mm, oom: tighten ...
762
  	if (test_bit(MMF_OOM_REAPED, &mm->flags))
696453e66   Michal Hocko   mm, oom: task_wil...
763
  		return false;
696453e66   Michal Hocko   mm, oom: task_wil...
764

091f362c5   Michal Hocko   mm, oom: tighten ...
765
  	if (atomic_read(&mm->mm_users) <= 1)
1af8bb432   Michal Hocko   mm, oom: fortify ...
766
  		return true;
1af8bb432   Michal Hocko   mm, oom: fortify ...
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
  
  	/*
  	 * This is really pessimistic but we do not have any reliable way
  	 * to check that external processes share with our mm
  	 */
  	rcu_read_lock();
  	for_each_process(p) {
  		if (!process_shares_mm(p, mm))
  			continue;
  		if (same_thread_group(task, p))
  			continue;
  		ret = __task_will_free_mem(p);
  		if (!ret)
  			break;
  	}
  	rcu_read_unlock();
1af8bb432   Michal Hocko   mm, oom: fortify ...
783
784
785
  
  	return ret;
  }
4d7b3394f   Oleg Nesterov   mm/oom_kill: fix ...
786
  /*
6b0c81b3b   David Rientjes   mm, oom: reduce d...
787
788
789
   * Must be called while holding a reference to p, which will be released upon
   * returning.
   */
6e0fc46dc   David Rientjes   mm, oom: organize...
790
  void oom_kill_process(struct oom_control *oc, struct task_struct *p,
9cbb78bb3   David Rientjes   mm, memcg: introd...
791
  		      unsigned int points, unsigned long totalpages,
2a966b77a   Vladimir Davydov   mm: oom: add memc...
792
  		      const char *message)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
793
  {
52d3c0367   Linus Torvalds   Revert "oom: oom_...
794
  	struct task_struct *victim = p;
5e9d834a0   David Rientjes   oom: sacrifice ch...
795
  	struct task_struct *child;
1da4db0cd   Oleg Nesterov   oom_kill: change ...
796
  	struct task_struct *t;
647f2bdf4   David Rientjes   mm, oom: fold oom...
797
  	struct mm_struct *mm;
52d3c0367   Linus Torvalds   Revert "oom: oom_...
798
  	unsigned int victim_points = 0;
dc3f21ead   David Rientjes   mm, oom: introduc...
799
800
  	static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL,
  					      DEFAULT_RATELIMIT_BURST);
bb29902a7   Tetsuo Handa   oom, oom_reaper: ...
801
  	bool can_oom_reap = true;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
802

50ec3bbff   Nick Piggin   [PATCH] oom: hand...
803
804
805
806
  	/*
  	 * If the task is already exiting, don't alarm the sysadmin or kill
  	 * its children or threads, just set TIF_MEMDIE so it can die quickly
  	 */
091f362c5   Michal Hocko   mm, oom: tighten ...
807
  	task_lock(p);
1af8bb432   Michal Hocko   mm, oom: fortify ...
808
  	if (task_will_free_mem(p)) {
16e951966   Johannes Weiner   mm: oom_kill: cle...
809
  		mark_oom_victim(p);
1af8bb432   Michal Hocko   mm, oom: fortify ...
810
  		wake_oom_reaper(p);
091f362c5   Michal Hocko   mm, oom: tighten ...
811
  		task_unlock(p);
6b0c81b3b   David Rientjes   mm, oom: reduce d...
812
  		put_task_struct(p);
2a1c9b1fc   David Rientjes   mm, oom: avoid lo...
813
  		return;
50ec3bbff   Nick Piggin   [PATCH] oom: hand...
814
  	}
091f362c5   Michal Hocko   mm, oom: tighten ...
815
  	task_unlock(p);
50ec3bbff   Nick Piggin   [PATCH] oom: hand...
816

dc3f21ead   David Rientjes   mm, oom: introduc...
817
  	if (__ratelimit(&oom_rs))
2a966b77a   Vladimir Davydov   mm: oom: add memc...
818
  		dump_header(oc, p);
8447d950e   David Rientjes   mm, oom: do not e...
819

f0d6647e8   Wang Long   mm/oom_kill.c: pr...
820
821
  	pr_err("%s: Kill process %d (%s) score %u or sacrifice child
  ",
5e9d834a0   David Rientjes   oom: sacrifice ch...
822
  		message, task_pid_nr(p), p->comm, points);
f3af38d30   Nick Piggin   [PATCH] oom: clea...
823

5e9d834a0   David Rientjes   oom: sacrifice ch...
824
825
  	/*
  	 * If any of p's children has a different mm and is eligible for kill,
11239836c   David Rientjes   oom: remove refer...
826
  	 * the one with the highest oom_badness() score is sacrificed for its
5e9d834a0   David Rientjes   oom: sacrifice ch...
827
828
829
  	 * parent.  This attempts to lose the minimal amount of work done while
  	 * still freeing memory.
  	 */
6b0c81b3b   David Rientjes   mm, oom: reduce d...
830
  	read_lock(&tasklist_lock);
1da4db0cd   Oleg Nesterov   oom_kill: change ...
831
  	for_each_thread(p, t) {
5e9d834a0   David Rientjes   oom: sacrifice ch...
832
  		list_for_each_entry(child, &t->children, sibling) {
a63d83f42   David Rientjes   oom: badness heur...
833
  			unsigned int child_points;
5e9d834a0   David Rientjes   oom: sacrifice ch...
834

4d7b3394f   Oleg Nesterov   mm/oom_kill: fix ...
835
  			if (process_shares_mm(child, p->mm))
edd45544c   David Rientjes   oom: avoid deferr...
836
  				continue;
a63d83f42   David Rientjes   oom: badness heur...
837
838
839
  			/*
  			 * oom_badness() returns 0 if the thread is unkillable
  			 */
2a966b77a   Vladimir Davydov   mm: oom: add memc...
840
841
  			child_points = oom_badness(child,
  					oc->memcg, oc->nodemask, totalpages);
5e9d834a0   David Rientjes   oom: sacrifice ch...
842
  			if (child_points > victim_points) {
6b0c81b3b   David Rientjes   mm, oom: reduce d...
843
  				put_task_struct(victim);
5e9d834a0   David Rientjes   oom: sacrifice ch...
844
845
  				victim = child;
  				victim_points = child_points;
6b0c81b3b   David Rientjes   mm, oom: reduce d...
846
  				get_task_struct(victim);
5e9d834a0   David Rientjes   oom: sacrifice ch...
847
  			}
dd8e8f405   Oleg Nesterov   oom: introduce fi...
848
  		}
1da4db0cd   Oleg Nesterov   oom_kill: change ...
849
  	}
6b0c81b3b   David Rientjes   mm, oom: reduce d...
850
  	read_unlock(&tasklist_lock);
dd8e8f405   Oleg Nesterov   oom: introduce fi...
851

6b0c81b3b   David Rientjes   mm, oom: reduce d...
852
853
  	p = find_lock_task_mm(victim);
  	if (!p) {
6b0c81b3b   David Rientjes   mm, oom: reduce d...
854
  		put_task_struct(victim);
647f2bdf4   David Rientjes   mm, oom: fold oom...
855
  		return;
6b0c81b3b   David Rientjes   mm, oom: reduce d...
856
857
858
859
860
  	} else if (victim != p) {
  		get_task_struct(p);
  		put_task_struct(victim);
  		victim = p;
  	}
647f2bdf4   David Rientjes   mm, oom: fold oom...
861

880b76893   Tetsuo Handa   mm/oom_kill.c: fi...
862
  	/* Get a reference to safely compare mm after task_unlock(victim) */
647f2bdf4   David Rientjes   mm, oom: fold oom...
863
  	mm = victim->mm;
880b76893   Tetsuo Handa   mm/oom_kill.c: fi...
864
  	atomic_inc(&mm->mm_count);
426fb5e72   Tetsuo Handa   mm/oom_kill.c: re...
865
866
867
868
869
870
  	/*
  	 * We should send SIGKILL before setting TIF_MEMDIE in order to prevent
  	 * the OOM victim from depleting the memory reserves from the user
  	 * space under its control.
  	 */
  	do_send_sig_info(SIGKILL, SEND_SIG_FORCED, victim, true);
16e951966   Johannes Weiner   mm: oom_kill: cle...
871
  	mark_oom_victim(victim);
eca56ff90   Jerome Marchand   mm, shmem: add in...
872
873
  	pr_err("Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB
  ",
647f2bdf4   David Rientjes   mm, oom: fold oom...
874
875
  		task_pid_nr(victim), victim->comm, K(victim->mm->total_vm),
  		K(get_mm_counter(victim->mm, MM_ANONPAGES)),
eca56ff90   Jerome Marchand   mm, shmem: add in...
876
877
  		K(get_mm_counter(victim->mm, MM_FILEPAGES)),
  		K(get_mm_counter(victim->mm, MM_SHMEMPAGES)));
647f2bdf4   David Rientjes   mm, oom: fold oom...
878
879
880
881
882
883
884
885
886
887
888
  	task_unlock(victim);
  
  	/*
  	 * Kill all user processes sharing victim->mm in other thread groups, if
  	 * any.  They don't get access to memory reserves, though, to avoid
  	 * depletion of all memory.  This prevents mm->mmap_sem livelock when an
  	 * oom killed thread cannot exit because it requires the semaphore and
  	 * its contended by another thread trying to allocate memory itself.
  	 * That thread will now get access to memory reserves since it has a
  	 * pending fatal signal.
  	 */
4d4048be8   Oleg Nesterov   oom_kill: add rcu...
889
  	rcu_read_lock();
c319025a6   Oleg Nesterov   mm/oom_kill: clea...
890
  	for_each_process(p) {
4d7b3394f   Oleg Nesterov   mm/oom_kill: fix ...
891
  		if (!process_shares_mm(p, mm))
c319025a6   Oleg Nesterov   mm/oom_kill: clea...
892
893
894
  			continue;
  		if (same_thread_group(p, victim))
  			continue;
97fd49c23   Michal Hocko   mm, oom: kill all...
895
  		if (unlikely(p->flags & PF_KTHREAD) || is_global_init(p)) {
aac453635   Michal Hocko   mm, oom: introduc...
896
897
898
  			/*
  			 * We cannot use oom_reaper for the mm shared by this
  			 * process because it wouldn't get killed and so the
a373966d1   Michal Hocko   mm, oom: hide mm ...
899
900
  			 * memory might be still used. Hide the mm from the oom
  			 * killer to guarantee OOM forward progress.
aac453635   Michal Hocko   mm, oom: introduc...
901
902
  			 */
  			can_oom_reap = false;
a373966d1   Michal Hocko   mm, oom: hide mm ...
903
904
905
906
907
  			set_bit(MMF_OOM_REAPED, &mm->flags);
  			pr_info("oom killer %d (%s) has mm pinned by %d (%s)
  ",
  					task_pid_nr(victim), victim->comm,
  					task_pid_nr(p), p->comm);
c319025a6   Oleg Nesterov   mm/oom_kill: clea...
908
  			continue;
aac453635   Michal Hocko   mm, oom: introduc...
909
  		}
c319025a6   Oleg Nesterov   mm/oom_kill: clea...
910
911
  		do_send_sig_info(SIGKILL, SEND_SIG_FORCED, p, true);
  	}
6b0c81b3b   David Rientjes   mm, oom: reduce d...
912
  	rcu_read_unlock();
647f2bdf4   David Rientjes   mm, oom: fold oom...
913

aac453635   Michal Hocko   mm, oom: introduc...
914
  	if (can_oom_reap)
36324a990   Michal Hocko   oom: clear TIF_ME...
915
  		wake_oom_reaper(victim);
aac453635   Michal Hocko   mm, oom: introduc...
916

880b76893   Tetsuo Handa   mm/oom_kill.c: fi...
917
  	mmdrop(mm);
6b0c81b3b   David Rientjes   mm, oom: reduce d...
918
  	put_task_struct(victim);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
919
  }
647f2bdf4   David Rientjes   mm, oom: fold oom...
920
  #undef K
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
921

309ed8825   David Rientjes   oom: extract pani...
922
923
924
  /*
   * Determines whether the kernel must panic because of the panic_on_oom sysctl.
   */
2a966b77a   Vladimir Davydov   mm: oom: add memc...
925
  void check_panic_on_oom(struct oom_control *oc, enum oom_constraint constraint)
309ed8825   David Rientjes   oom: extract pani...
926
927
928
929
930
931
932
933
934
935
936
937
  {
  	if (likely(!sysctl_panic_on_oom))
  		return;
  	if (sysctl_panic_on_oom != 2) {
  		/*
  		 * panic_on_oom == 1 only affects CONSTRAINT_NONE, the kernel
  		 * does not panic for cpuset, mempolicy, or memcg allocation
  		 * failures.
  		 */
  		if (constraint != CONSTRAINT_NONE)
  			return;
  	}
071a4befe   David Rientjes   mm, oom: do not p...
938
  	/* Do not panic for oom kills triggered by sysrq */
db2a0dd7a   Yaowei Bai   mm/oom_kill.c: in...
939
  	if (is_sysrq_oom(oc))
071a4befe   David Rientjes   mm, oom: do not p...
940
  		return;
2a966b77a   Vladimir Davydov   mm: oom: add memc...
941
  	dump_header(oc, NULL);
309ed8825   David Rientjes   oom: extract pani...
942
943
944
945
  	panic("Out of memory: %s panic_on_oom is enabled
  ",
  		sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide");
  }
8bc719d3c   Martin Schwidefsky   [PATCH] out of me...
946
947
948
949
950
951
952
953
954
955
956
957
958
  static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
  
  int register_oom_notifier(struct notifier_block *nb)
  {
  	return blocking_notifier_chain_register(&oom_notify_list, nb);
  }
  EXPORT_SYMBOL_GPL(register_oom_notifier);
  
  int unregister_oom_notifier(struct notifier_block *nb)
  {
  	return blocking_notifier_chain_unregister(&oom_notify_list, nb);
  }
  EXPORT_SYMBOL_GPL(unregister_oom_notifier);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
959
  /**
6e0fc46dc   David Rientjes   mm, oom: organize...
960
961
   * out_of_memory - kill the "best" process when we run out of memory
   * @oc: pointer to struct oom_control
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
962
963
964
965
966
967
   *
   * If we run out of memory, we have the choice between either
   * killing a random task (bad), letting the system crash (worse)
   * OR try to be smart about which process to kill. Note that we
   * don't have to be perfect here, we just have to be good.
   */
6e0fc46dc   David Rientjes   mm, oom: organize...
968
  bool out_of_memory(struct oom_control *oc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
969
  {
0aad4b312   David Rientjes   oom: fold __out_o...
970
  	struct task_struct *p;
a63d83f42   David Rientjes   oom: badness heur...
971
  	unsigned long totalpages;
8bc719d3c   Martin Schwidefsky   [PATCH] out of me...
972
  	unsigned long freed = 0;
9cbb78bb3   David Rientjes   mm, memcg: introd...
973
  	unsigned int uninitialized_var(points);
e36589323   David Rientjes   oom: remove speci...
974
  	enum oom_constraint constraint = CONSTRAINT_NONE;
8bc719d3c   Martin Schwidefsky   [PATCH] out of me...
975

dc56401fc   Johannes Weiner   mm: oom_kill: sim...
976
977
  	if (oom_killer_disabled)
  		return false;
8bc719d3c   Martin Schwidefsky   [PATCH] out of me...
978
979
980
  	blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
  	if (freed > 0)
  		/* Got some memory back in the last second. */
75e8f8b24   David Rientjes   mm, oom: remove u...
981
  		return true;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
982

7b98c2e40   David Rientjes   oom: give current...
983
  	/*
9ff4868e3   David Rientjes   mm, oom: allow ex...
984
985
986
  	 * If current has a pending SIGKILL or is exiting, then automatically
  	 * select it.  The goal is to allow it to allocate so that it may
  	 * quickly exit and free its memory.
7b98c2e40   David Rientjes   oom: give current...
987
  	 */
091f362c5   Michal Hocko   mm, oom: tighten ...
988
  	if (task_will_free_mem(current)) {
16e951966   Johannes Weiner   mm: oom_kill: cle...
989
  		mark_oom_victim(current);
1af8bb432   Michal Hocko   mm, oom: fortify ...
990
  		wake_oom_reaper(current);
75e8f8b24   David Rientjes   mm, oom: remove u...
991
  		return true;
7b98c2e40   David Rientjes   oom: give current...
992
  	}
9b0f8b040   Christoph Lameter   [PATCH] Terminate...
993
  	/*
3da88fb3b   Michal Hocko   mm, oom: move GFP...
994
995
996
997
998
999
1000
1001
1002
  	 * The OOM killer does not compensate for IO-less reclaim.
  	 * pagefault_out_of_memory lost its gfp context so we have to
  	 * make sure exclude 0 mask - all other users should have at least
  	 * ___GFP_DIRECT_RECLAIM to get here.
  	 */
  	if (oc->gfp_mask && !(oc->gfp_mask & (__GFP_FS|__GFP_NOFAIL)))
  		return true;
  
  	/*
9b0f8b040   Christoph Lameter   [PATCH] Terminate...
1003
1004
1005
  	 * Check if there were limitations on the allocation (only relevant for
  	 * NUMA) that may require different handling.
  	 */
6e0fc46dc   David Rientjes   mm, oom: organize...
1006
1007
1008
  	constraint = constrained_alloc(oc, &totalpages);
  	if (constraint != CONSTRAINT_MEMORY_POLICY)
  		oc->nodemask = NULL;
2a966b77a   Vladimir Davydov   mm: oom: add memc...
1009
  	check_panic_on_oom(oc, constraint);
0aad4b312   David Rientjes   oom: fold __out_o...
1010

121d1ba0a   David Rientjes   mm, oom: fix pote...
1011
  	if (sysctl_oom_kill_allocating_task && current->mm &&
6e0fc46dc   David Rientjes   mm, oom: organize...
1012
  	    !oom_unkillable_task(current, NULL, oc->nodemask) &&
121d1ba0a   David Rientjes   mm, oom: fix pote...
1013
  	    current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {
6b0c81b3b   David Rientjes   mm, oom: reduce d...
1014
  		get_task_struct(current);
2a966b77a   Vladimir Davydov   mm: oom: add memc...
1015
  		oom_kill_process(oc, current, 0, totalpages,
2a1c9b1fc   David Rientjes   mm, oom: avoid lo...
1016
  				 "Out of memory (oom_kill_allocating_task)");
75e8f8b24   David Rientjes   mm, oom: remove u...
1017
  		return true;
0aad4b312   David Rientjes   oom: fold __out_o...
1018
  	}
6e0fc46dc   David Rientjes   mm, oom: organize...
1019
  	p = select_bad_process(oc, &points, totalpages);
0aad4b312   David Rientjes   oom: fold __out_o...
1020
  	/* Found nothing?!?! Either we hang forever, or we panic. */
db2a0dd7a   Yaowei Bai   mm/oom_kill.c: in...
1021
  	if (!p && !is_sysrq_oom(oc)) {
2a966b77a   Vladimir Davydov   mm: oom: add memc...
1022
  		dump_header(oc, NULL);
0aad4b312   David Rientjes   oom: fold __out_o...
1023
1024
1025
  		panic("Out of memory and no killable processes...
  ");
  	}
071a4befe   David Rientjes   mm, oom: do not p...
1026
  	if (p && p != (void *)-1UL) {
2a966b77a   Vladimir Davydov   mm: oom: add memc...
1027
  		oom_kill_process(oc, p, points, totalpages, "Out of memory");
75e8f8b24   David Rientjes   mm, oom: remove u...
1028
1029
1030
1031
  		/*
  		 * Give the killed process a good chance to exit before trying
  		 * to allocate memory again.
  		 */
4f774b912   David Rientjes   mm, oom: do not s...
1032
  		schedule_timeout_killable(1);
75e8f8b24   David Rientjes   mm, oom: remove u...
1033
  	}
dc56401fc   Johannes Weiner   mm: oom_kill: sim...
1034
  	return true;
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
1035
  }
e36589323   David Rientjes   oom: remove speci...
1036
1037
  /*
   * The pagefault handler calls here because it is out of memory, so kill a
798fd7569   Vladimir Davydov   mm: zap ZONE_OOM_...
1038
1039
   * memory-hogging task. If oom_lock is held by somebody else, a parallel oom
   * killing is already in progress so do nothing.
e36589323   David Rientjes   oom: remove speci...
1040
1041
1042
   */
  void pagefault_out_of_memory(void)
  {
6e0fc46dc   David Rientjes   mm, oom: organize...
1043
1044
1045
  	struct oom_control oc = {
  		.zonelist = NULL,
  		.nodemask = NULL,
2a966b77a   Vladimir Davydov   mm: oom: add memc...
1046
  		.memcg = NULL,
6e0fc46dc   David Rientjes   mm, oom: organize...
1047
1048
  		.gfp_mask = 0,
  		.order = 0,
6e0fc46dc   David Rientjes   mm, oom: organize...
1049
  	};
494264208   Johannes Weiner   mm: memcg: handle...
1050
  	if (mem_cgroup_oom_synchronize(true))
dc56401fc   Johannes Weiner   mm: oom_kill: sim...
1051
  		return;
3812c8c8f   Johannes Weiner   mm: memcg: do not...
1052

dc56401fc   Johannes Weiner   mm: oom_kill: sim...
1053
1054
  	if (!mutex_trylock(&oom_lock))
  		return;
c32b3cbe0   Michal Hocko   oom, PM: make OOM...
1055

6e0fc46dc   David Rientjes   mm, oom: organize...
1056
  	if (!out_of_memory(&oc)) {
dc56401fc   Johannes Weiner   mm: oom_kill: sim...
1057
1058
1059
1060
1061
1062
1063
  		/*
  		 * There shouldn't be any user tasks runnable while the
  		 * OOM killer is disabled, so the current task has to
  		 * be a racing OOM victim for which oom_killer_disable()
  		 * is waiting for.
  		 */
  		WARN_ON(test_thread_flag(TIF_MEMDIE));
e36589323   David Rientjes   oom: remove speci...
1064
  	}
dc56401fc   Johannes Weiner   mm: oom_kill: sim...
1065
1066
  
  	mutex_unlock(&oom_lock);
e36589323   David Rientjes   oom: remove speci...
1067
  }