Blame view
mm/oom_kill.c
29 KB
1da177e4c
|
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
|
7 8 |
* Copyright (C) 2010 Google, Inc. * Rewritten by David Rientjes |
1da177e4c
|
9 10 |
* * The routines in this file are used to kill a process when |
a49335cce
|
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
|
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
|
19 |
#include <linux/oom.h> |
1da177e4c
|
20 |
#include <linux/mm.h> |
4e950f6f0
|
21 |
#include <linux/err.h> |
5a0e3ad6a
|
22 |
#include <linux/gfp.h> |
1da177e4c
|
23 24 25 26 |
#include <linux/sched.h> #include <linux/swap.h> #include <linux/timex.h> #include <linux/jiffies.h> |
ef08e3b49
|
27 |
#include <linux/cpuset.h> |
b95f1b31b
|
28 |
#include <linux/export.h> |
8bc719d3c
|
29 |
#include <linux/notifier.h> |
c7ba5c9e8
|
30 |
#include <linux/memcontrol.h> |
6f48d0ebd
|
31 |
#include <linux/mempolicy.h> |
5cd9c58fb
|
32 |
#include <linux/security.h> |
edd45544c
|
33 |
#include <linux/ptrace.h> |
f660daac4
|
34 |
#include <linux/freezer.h> |
43d2b1132
|
35 |
#include <linux/ftrace.h> |
dc3f21ead
|
36 |
#include <linux/ratelimit.h> |
aac453635
|
37 38 39 40 41 |
#include <linux/kthread.h> #include <linux/init.h> #include <asm/tlb.h> #include "internal.h" |
43d2b1132
|
42 43 44 |
#define CREATE_TRACE_POINTS #include <trace/events/oom.h> |
1da177e4c
|
45 |
|
fadd8fbd1
|
46 |
int sysctl_panic_on_oom; |
fe071d7e8
|
47 |
int sysctl_oom_kill_allocating_task; |
ad915c432
|
48 |
int sysctl_oom_dump_tasks = 1; |
dc56401fc
|
49 50 |
DEFINE_MUTEX(oom_lock); |
1da177e4c
|
51 |
|
6f48d0ebd
|
52 53 54 |
#ifdef CONFIG_NUMA /** * has_intersects_mems_allowed() - check task eligiblity for kill |
ad9624417
|
55 |
* @start: task struct of which task to consider |
6f48d0ebd
|
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
|
61 |
*/ |
ad9624417
|
62 |
static bool has_intersects_mems_allowed(struct task_struct *start, |
6f48d0ebd
|
63 |
const nodemask_t *mask) |
495789a51
|
64 |
{ |
ad9624417
|
65 66 |
struct task_struct *tsk; bool ret = false; |
495789a51
|
67 |
|
ad9624417
|
68 |
rcu_read_lock(); |
1da4db0cd
|
69 |
for_each_thread(start, tsk) { |
6f48d0ebd
|
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
|
77 |
ret = mempolicy_nodemask_intersects(tsk, mask); |
6f48d0ebd
|
78 79 80 81 82 |
} else { /* * This is not a mempolicy constrained oom, so only * check the mems of tsk's cpuset. */ |
ad9624417
|
83 |
ret = cpuset_mems_allowed_intersects(current, tsk); |
6f48d0ebd
|
84 |
} |
ad9624417
|
85 86 |
if (ret) break; |
1da4db0cd
|
87 |
} |
ad9624417
|
88 |
rcu_read_unlock(); |
df1090a8d
|
89 |
|
ad9624417
|
90 |
return ret; |
6f48d0ebd
|
91 92 93 94 95 96 |
} #else static bool has_intersects_mems_allowed(struct task_struct *tsk, const nodemask_t *mask) { return true; |
495789a51
|
97 |
} |
6f48d0ebd
|
98 |
#endif /* CONFIG_NUMA */ |
495789a51
|
99 |
|
6f48d0ebd
|
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
|
106 |
struct task_struct *find_lock_task_mm(struct task_struct *p) |
dd8e8f405
|
107 |
{ |
1da4db0cd
|
108 |
struct task_struct *t; |
dd8e8f405
|
109 |
|
4d4048be8
|
110 |
rcu_read_lock(); |
1da4db0cd
|
111 |
for_each_thread(p, t) { |
dd8e8f405
|
112 113 |
task_lock(t); if (likely(t->mm)) |
4d4048be8
|
114 |
goto found; |
dd8e8f405
|
115 |
task_unlock(t); |
1da4db0cd
|
116 |
} |
4d4048be8
|
117 118 119 |
t = NULL; found: rcu_read_unlock(); |
dd8e8f405
|
120 |
|
4d4048be8
|
121 |
return t; |
dd8e8f405
|
122 |
} |
db2a0dd7a
|
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
|
131 |
/* return true if the task is not adequate as candidate victim task. */ |
e85bfd3aa
|
132 |
static bool oom_unkillable_task(struct task_struct *p, |
2314b42db
|
133 |
struct mem_cgroup *memcg, const nodemask_t *nodemask) |
ab290adba
|
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
|
141 |
if (memcg && !task_in_mem_cgroup(p, memcg)) |
ab290adba
|
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
|
150 |
/** |
a63d83f42
|
151 |
* oom_badness - heuristic function to determine which candidate task to kill |
1da177e4c
|
152 |
* @p: task struct of which task we should calculate |
a63d83f42
|
153 |
* @totalpages: total present RAM allowed for page allocation |
1da177e4c
|
154 |
* |
a63d83f42
|
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
|
158 |
*/ |
a7f638f99
|
159 160 |
unsigned long oom_badness(struct task_struct *p, struct mem_cgroup *memcg, const nodemask_t *nodemask, unsigned long totalpages) |
1da177e4c
|
161 |
{ |
1e11ad8dc
|
162 |
long points; |
61eafb00d
|
163 |
long adj; |
28b83c519
|
164 |
|
72835c86c
|
165 |
if (oom_unkillable_task(p, memcg, nodemask)) |
26ebc9849
|
166 |
return 0; |
1da177e4c
|
167 |
|
dd8e8f405
|
168 169 |
p = find_lock_task_mm(p); if (!p) |
1da177e4c
|
170 |
return 0; |
bb8a4b7fd
|
171 172 |
/* * Do not even consider tasks which are explicitly marked oom |
b18dc5f29
|
173 174 |
* unkillable or have been already oom reaped or the are in * the middle of vfork |
bb8a4b7fd
|
175 |
*/ |
a9c58b907
|
176 |
adj = (long)p->signal->oom_score_adj; |
bb8a4b7fd
|
177 |
if (adj == OOM_SCORE_ADJ_MIN || |
b18dc5f29
|
178 179 |
test_bit(MMF_OOM_REAPED, &p->mm->flags) || in_vfork(p)) { |
5aecc85ab
|
180 181 182 |
task_unlock(p); return 0; } |
1da177e4c
|
183 |
/* |
a63d83f42
|
184 |
* The baseline for the badness score is the proportion of RAM that each |
f755a042d
|
185 |
* task's rss, pagetable and swap space use. |
1da177e4c
|
186 |
*/ |
dc6c9a35b
|
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
|
189 |
task_unlock(p); |
1da177e4c
|
190 191 |
/* |
a63d83f42
|
192 193 |
* Root processes get 3% bonus, just like the __vm_enough_memory() * implementation used by LSMs. |
1da177e4c
|
194 |
*/ |
a63d83f42
|
195 |
if (has_capability_noaudit(p, CAP_SYS_ADMIN)) |
778c14aff
|
196 |
points -= (points * 3) / 100; |
1da177e4c
|
197 |
|
61eafb00d
|
198 199 200 |
/* Normalize to oom_score_adj units */ adj *= totalpages / 1000; points += adj; |
1da177e4c
|
201 |
|
f19e8aa11
|
202 |
/* |
a7f638f99
|
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
|
205 |
*/ |
1e11ad8dc
|
206 |
return points > 0 ? points : 1; |
1da177e4c
|
207 208 209 |
} /* |
9b0f8b040
|
210 211 |
* Determine the type of allocation constraint. */ |
9b0f8b040
|
212 |
#ifdef CONFIG_NUMA |
6e0fc46dc
|
213 214 |
static enum oom_constraint constrained_alloc(struct oom_control *oc, unsigned long *totalpages) |
4365a5676
|
215 |
{ |
54a6eb5c4
|
216 |
struct zone *zone; |
dd1a239f6
|
217 |
struct zoneref *z; |
6e0fc46dc
|
218 |
enum zone_type high_zoneidx = gfp_zone(oc->gfp_mask); |
a63d83f42
|
219 220 |
bool cpuset_limited = false; int nid; |
9b0f8b040
|
221 |
|
a63d83f42
|
222 223 |
/* Default to all available memory */ *totalpages = totalram_pages + total_swap_pages; |
6e0fc46dc
|
224 |
if (!oc->zonelist) |
a63d83f42
|
225 |
return CONSTRAINT_NONE; |
4365a5676
|
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
|
231 |
if (oc->gfp_mask & __GFP_THISNODE) |
4365a5676
|
232 |
return CONSTRAINT_NONE; |
9b0f8b040
|
233 |
|
4365a5676
|
234 |
/* |
a63d83f42
|
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
|
238 |
*/ |
6e0fc46dc
|
239 240 |
if (oc->nodemask && !nodes_subset(node_states[N_MEMORY], *oc->nodemask)) { |
a63d83f42
|
241 |
*totalpages = total_swap_pages; |
6e0fc46dc
|
242 |
for_each_node_mask(nid, *oc->nodemask) |
a63d83f42
|
243 |
*totalpages += node_spanned_pages(nid); |
9b0f8b040
|
244 |
return CONSTRAINT_MEMORY_POLICY; |
a63d83f42
|
245 |
} |
4365a5676
|
246 247 |
/* Check this allocation failure is caused by cpuset's wall function */ |
6e0fc46dc
|
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
|
251 |
cpuset_limited = true; |
9b0f8b040
|
252 |
|
a63d83f42
|
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
|
259 260 |
return CONSTRAINT_NONE; } |
4365a5676
|
261 |
#else |
6e0fc46dc
|
262 263 |
static enum oom_constraint constrained_alloc(struct oom_control *oc, unsigned long *totalpages) |
4365a5676
|
264 |
{ |
a63d83f42
|
265 |
*totalpages = totalram_pages + total_swap_pages; |
4365a5676
|
266 267 268 |
return CONSTRAINT_NONE; } #endif |
9b0f8b040
|
269 |
|
6e0fc46dc
|
270 |
enum oom_scan_t oom_scan_process_thread(struct oom_control *oc, |
fbe84a09d
|
271 |
struct task_struct *task) |
462607ecc
|
272 |
{ |
6e0fc46dc
|
273 |
if (oom_unkillable_task(task, NULL, oc->nodemask)) |
462607ecc
|
274 275 276 277 |
return OOM_SCAN_CONTINUE; /* * This task already has access to memory reserves and is being killed. |
a373966d1
|
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
|
281 |
*/ |
a373966d1
|
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
|
294 |
|
e1e12d2f3
|
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
|
301 302 |
return OOM_SCAN_OK; } |
9b0f8b040
|
303 |
/* |
1da177e4c
|
304 |
* Simple selection loop. We chose the process with the highest |
6b4f2b56a
|
305 |
* number of 'points'. Returns -1 on scan abort. |
1da177e4c
|
306 |
*/ |
6e0fc46dc
|
307 308 |
static struct task_struct *select_bad_process(struct oom_control *oc, unsigned int *ppoints, unsigned long totalpages) |
1da177e4c
|
309 |
{ |
f44666b04
|
310 |
struct task_struct *p; |
1da177e4c
|
311 |
struct task_struct *chosen = NULL; |
a7f638f99
|
312 |
unsigned long chosen_points = 0; |
1da177e4c
|
313 |
|
6b0c81b3b
|
314 |
rcu_read_lock(); |
f44666b04
|
315 |
for_each_process(p) { |
a63d83f42
|
316 |
unsigned int points; |
a49335cce
|
317 |
|
fbe84a09d
|
318 |
switch (oom_scan_process_thread(oc, p)) { |
462607ecc
|
319 320 321 322 323 |
case OOM_SCAN_SELECT: chosen = p; chosen_points = ULONG_MAX; /* fall through */ case OOM_SCAN_CONTINUE: |
c027a474a
|
324 |
continue; |
462607ecc
|
325 |
case OOM_SCAN_ABORT: |
6b0c81b3b
|
326 |
rcu_read_unlock(); |
6b4f2b56a
|
327 |
return (struct task_struct *)(-1UL); |
462607ecc
|
328 329 330 |
case OOM_SCAN_OK: break; }; |
6e0fc46dc
|
331 |
points = oom_badness(p, NULL, oc->nodemask, totalpages); |
d49ad9355
|
332 333 |
if (!points || points < chosen_points) continue; |
d49ad9355
|
334 335 336 |
chosen = p; chosen_points = points; |
1da4db0cd
|
337 |
} |
6b0c81b3b
|
338 339 340 |
if (chosen) get_task_struct(chosen); rcu_read_unlock(); |
972c4ea59
|
341 |
|
a7f638f99
|
342 |
*ppoints = chosen_points * 1000 / totalpages; |
1da177e4c
|
343 344 345 346 |
return chosen; } /** |
1b578df02
|
347 |
* dump_tasks - dump current memory state of all system tasks |
dad7557eb
|
348 |
* @memcg: current's memory controller, if constrained |
e85bfd3aa
|
349 |
* @nodemask: nodemask passed to page allocator for mempolicy ooms |
1b578df02
|
350 |
* |
e85bfd3aa
|
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
|
354 355 |
* State information includes task's pid, uid, tgid, vm size, rss, nr_ptes, * swapents, oom_score_adj value, and name. |
fef1bdd68
|
356 |
*/ |
2314b42db
|
357 |
static void dump_tasks(struct mem_cgroup *memcg, const nodemask_t *nodemask) |
fef1bdd68
|
358 |
{ |
c55db9578
|
359 360 |
struct task_struct *p; struct task_struct *task; |
fef1bdd68
|
361 |
|
dc6c9a35b
|
362 363 |
pr_info("[ pid ] uid tgid total_vm rss nr_ptes nr_pmds swapents oom_score_adj name "); |
6b0c81b3b
|
364 |
rcu_read_lock(); |
c55db9578
|
365 |
for_each_process(p) { |
72835c86c
|
366 |
if (oom_unkillable_task(p, memcg, nodemask)) |
b4416d2be
|
367 |
continue; |
fef1bdd68
|
368 |
|
c55db9578
|
369 370 |
task = find_lock_task_mm(p); if (!task) { |
6d2661ede
|
371 |
/* |
74ab7f1d3
|
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
|
374 |
* them; they can't be oom killed anyway. |
6d2661ede
|
375 |
*/ |
6d2661ede
|
376 377 |
continue; } |
c55db9578
|
378 |
|
dc6c9a35b
|
379 380 |
pr_info("[%5d] %5d %5d %8lu %8lu %7ld %7ld %8lu %5hd %s ", |
078de5f70
|
381 382 |
task->pid, from_kuid(&init_user_ns, task_uid(task)), task->tgid, task->mm->total_vm, get_mm_rss(task->mm), |
e1f56c89b
|
383 |
atomic_long_read(&task->mm->nr_ptes), |
dc6c9a35b
|
384 |
mm_nr_pmds(task->mm), |
de34d965a
|
385 |
get_mm_counter(task->mm, MM_SWAPENTS), |
a63d83f42
|
386 |
task->signal->oom_score_adj, task->comm); |
c55db9578
|
387 388 |
task_unlock(task); } |
6b0c81b3b
|
389 |
rcu_read_unlock(); |
fef1bdd68
|
390 |
} |
2a966b77a
|
391 |
static void dump_header(struct oom_control *oc, struct task_struct *p) |
1b604d75b
|
392 |
{ |
756a025f0
|
393 394 |
pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd ", |
a0795cd41
|
395 |
current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order, |
a63d83f42
|
396 |
current->signal->oom_score_adj); |
a0795cd41
|
397 |
|
da39da3a5
|
398 |
cpuset_print_current_mems_allowed(); |
1b604d75b
|
399 |
dump_stack(); |
2a966b77a
|
400 401 |
if (oc->memcg) mem_cgroup_print_oom_info(oc->memcg, p); |
58cf188ed
|
402 403 |
else show_mem(SHOW_MEM_FILTER_NODES); |
1b604d75b
|
404 |
if (sysctl_oom_dump_tasks) |
2a966b77a
|
405 |
dump_tasks(oc->memcg, oc->nodemask); |
1b604d75b
|
406 |
} |
5695be142
|
407 |
/* |
c32b3cbe0
|
408 |
* Number of OOM victims in flight |
5695be142
|
409 |
*/ |
c32b3cbe0
|
410 411 |
static atomic_t oom_victims = ATOMIC_INIT(0); static DECLARE_WAIT_QUEUE_HEAD(oom_victims_wait); |
5695be142
|
412 |
|
c32b3cbe0
|
413 |
bool oom_killer_disabled __read_mostly; |
5695be142
|
414 |
|
bc448e897
|
415 |
#define K(x) ((x) << (PAGE_SHIFT-10)) |
3ef22dfff
|
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
|
422 |
bool process_shares_mm(struct task_struct *p, struct mm_struct *mm) |
3ef22dfff
|
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
|
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
|
439 |
static DECLARE_WAIT_QUEUE_HEAD(oom_reaper_wait); |
29c696e1c
|
440 |
static struct task_struct *oom_reaper_list; |
03049269d
|
441 |
static DEFINE_SPINLOCK(oom_reaper_lock); |
36324a990
|
442 |
static bool __oom_reap_task(struct task_struct *tsk) |
aac453635
|
443 444 445 |
{ struct mmu_gather tlb; struct vm_area_struct *vma; |
e2fe14564
|
446 |
struct mm_struct *mm = NULL; |
36324a990
|
447 |
struct task_struct *p; |
aac453635
|
448 449 450 |
struct zap_details details = {.check_swap_entries = true, .ignore_dirty = true}; bool ret = true; |
36324a990
|
451 |
/* |
e2fe14564
|
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
|
455 |
* mmget_not_zero |
e2fe14564
|
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
|
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
|
475 |
goto unlock_oom; |
36324a990
|
476 |
mm = p->mm; |
e5e3f4c4f
|
477 |
atomic_inc(&mm->mm_count); |
36324a990
|
478 |
task_unlock(p); |
aac453635
|
479 480 481 |
if (!down_read_trylock(&mm->mmap_sem)) { ret = false; |
e5e3f4c4f
|
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
|
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
|
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
|
528 |
up_read(&mm->mmap_sem); |
36324a990
|
529 530 |
/* |
449d777d7
|
531 532 |
* This task can be safely ignored because we cannot do much more * to release its memory. |
36324a990
|
533 |
*/ |
bb8a4b7fd
|
534 |
set_bit(MMF_OOM_REAPED, &mm->flags); |
ec8d7c14e
|
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
|
540 541 542 543 544 |
mmput_async(mm); mm_drop: mmdrop(mm); unlock_oom: mutex_unlock(&oom_lock); |
aac453635
|
545 546 |
return ret; } |
bc448e897
|
547 |
#define MAX_OOM_REAP_RETRIES 10 |
36324a990
|
548 |
static void oom_reap_task(struct task_struct *tsk) |
aac453635
|
549 550 551 552 |
{ int attempts = 0; /* Retry the down_read_trylock(mmap_sem) a few times */ |
bc448e897
|
553 |
while (attempts++ < MAX_OOM_REAP_RETRIES && !__oom_reap_task(tsk)) |
aac453635
|
554 |
schedule_timeout_idle(HZ/10); |
bc448e897
|
555 |
if (attempts > MAX_OOM_REAP_RETRIES) { |
11a410d51
|
556 |
struct task_struct *p; |
bc448e897
|
557 558 559 |
pr_info("oom_reaper: unable to reap pid:%d (%s) ", task_pid_nr(tsk), tsk->comm); |
11a410d51
|
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
|
577 578 |
debug_show_all_locks(); } |
449d777d7
|
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
|
587 |
/* Drop a reference taken by wake_oom_reaper */ |
36324a990
|
588 |
put_task_struct(tsk); |
aac453635
|
589 590 591 592 |
} static int oom_reaper(void *unused) { |
e26796066
|
593 |
set_freezable(); |
aac453635
|
594 |
while (true) { |
03049269d
|
595 |
struct task_struct *tsk = NULL; |
aac453635
|
596 |
|
29c696e1c
|
597 |
wait_event_freezable(oom_reaper_wait, oom_reaper_list != NULL); |
03049269d
|
598 |
spin_lock(&oom_reaper_lock); |
29c696e1c
|
599 600 601 |
if (oom_reaper_list != NULL) { tsk = oom_reaper_list; oom_reaper_list = tsk->oom_reaper_list; |
03049269d
|
602 603 604 605 606 |
} spin_unlock(&oom_reaper_lock); if (tsk) oom_reap_task(tsk); |
aac453635
|
607 608 609 610 |
} return 0; } |
1af8bb432
|
611 |
void wake_oom_reaper(struct task_struct *tsk) |
aac453635
|
612 |
{ |
af8e15cc8
|
613 614 615 616 617 |
if (!oom_reaper_th) return; /* tsk is already queued? */ if (tsk == oom_reaper_list || tsk->oom_reaper_list) |
aac453635
|
618 |
return; |
36324a990
|
619 |
get_task_struct(tsk); |
aac453635
|
620 |
|
03049269d
|
621 |
spin_lock(&oom_reaper_lock); |
29c696e1c
|
622 623 |
tsk->oom_reaper_list = oom_reaper_list; oom_reaper_list = tsk; |
03049269d
|
624 625 |
spin_unlock(&oom_reaper_lock); wake_up(&oom_reaper_wait); |
aac453635
|
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
|
640 |
#endif |
49550b605
|
641 |
/** |
16e951966
|
642 |
* mark_oom_victim - mark the given task as OOM victim |
49550b605
|
643 |
* @tsk: task to mark |
c32b3cbe0
|
644 |
* |
dc56401fc
|
645 |
* Has to be called with oom_lock held and never after |
c32b3cbe0
|
646 |
* oom has been disabled already. |
49550b605
|
647 |
*/ |
16e951966
|
648 |
void mark_oom_victim(struct task_struct *tsk) |
49550b605
|
649 |
{ |
c32b3cbe0
|
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
|
654 |
atomic_inc(&tsk->signal->oom_victims); |
63a8ca9b2
|
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
|
662 |
atomic_inc(&oom_victims); |
49550b605
|
663 664 665 |
} /** |
16e951966
|
666 |
* exit_oom_victim - note the exit of an OOM victim |
49550b605
|
667 |
*/ |
36324a990
|
668 |
void exit_oom_victim(struct task_struct *tsk) |
49550b605
|
669 |
{ |
36324a990
|
670 671 |
if (!test_and_clear_tsk_thread_flag(tsk, TIF_MEMDIE)) return; |
f44666b04
|
672 |
atomic_dec(&tsk->signal->oom_victims); |
c32b3cbe0
|
673 |
|
c38f1025f
|
674 |
if (!atomic_dec_return(&oom_victims)) |
c32b3cbe0
|
675 |
wake_up_all(&oom_victims_wait); |
c32b3cbe0
|
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
|
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
|
696 |
*/ |
6afcf2895
|
697 |
if (mutex_lock_killable(&oom_lock)) |
c32b3cbe0
|
698 |
return false; |
c32b3cbe0
|
699 |
oom_killer_disabled = true; |
dc56401fc
|
700 |
mutex_unlock(&oom_lock); |
c32b3cbe0
|
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
|
712 |
oom_killer_disabled = false; |
49550b605
|
713 |
} |
1af8bb432
|
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
|
739 740 |
* Caller has to make sure that task->mm is stable (hold task_lock or * it operates on the current). |
1af8bb432
|
741 742 743 |
*/ bool task_will_free_mem(struct task_struct *task) { |
091f362c5
|
744 |
struct mm_struct *mm = task->mm; |
1af8bb432
|
745 |
struct task_struct *p; |
f33e6f067
|
746 |
bool ret = true; |
1af8bb432
|
747 |
|
1af8bb432
|
748 |
/* |
091f362c5
|
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
|
752 |
*/ |
091f362c5
|
753 |
if (!mm) |
1af8bb432
|
754 |
return false; |
091f362c5
|
755 756 |
if (!__task_will_free_mem(task)) return false; |
696453e66
|
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
|
762 |
if (test_bit(MMF_OOM_REAPED, &mm->flags)) |
696453e66
|
763 |
return false; |
696453e66
|
764 |
|
091f362c5
|
765 |
if (atomic_read(&mm->mm_users) <= 1) |
1af8bb432
|
766 |
return true; |
1af8bb432
|
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
|
783 784 785 |
return ret; } |
4d7b3394f
|
786 |
/* |
6b0c81b3b
|
787 788 789 |
* Must be called while holding a reference to p, which will be released upon * returning. */ |
6e0fc46dc
|
790 |
void oom_kill_process(struct oom_control *oc, struct task_struct *p, |
9cbb78bb3
|
791 |
unsigned int points, unsigned long totalpages, |
2a966b77a
|
792 |
const char *message) |
1da177e4c
|
793 |
{ |
52d3c0367
|
794 |
struct task_struct *victim = p; |
5e9d834a0
|
795 |
struct task_struct *child; |
1da4db0cd
|
796 |
struct task_struct *t; |
647f2bdf4
|
797 |
struct mm_struct *mm; |
52d3c0367
|
798 |
unsigned int victim_points = 0; |
dc3f21ead
|
799 800 |
static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST); |
bb29902a7
|
801 |
bool can_oom_reap = true; |
1da177e4c
|
802 |
|
50ec3bbff
|
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
|
807 |
task_lock(p); |
1af8bb432
|
808 |
if (task_will_free_mem(p)) { |
16e951966
|
809 |
mark_oom_victim(p); |
1af8bb432
|
810 |
wake_oom_reaper(p); |
091f362c5
|
811 |
task_unlock(p); |
6b0c81b3b
|
812 |
put_task_struct(p); |
2a1c9b1fc
|
813 |
return; |
50ec3bbff
|
814 |
} |
091f362c5
|
815 |
task_unlock(p); |
50ec3bbff
|
816 |
|
dc3f21ead
|
817 |
if (__ratelimit(&oom_rs)) |
2a966b77a
|
818 |
dump_header(oc, p); |
8447d950e
|
819 |
|
f0d6647e8
|
820 821 |
pr_err("%s: Kill process %d (%s) score %u or sacrifice child ", |
5e9d834a0
|
822 |
message, task_pid_nr(p), p->comm, points); |
f3af38d30
|
823 |
|
5e9d834a0
|
824 825 |
/* * If any of p's children has a different mm and is eligible for kill, |
11239836c
|
826 |
* the one with the highest oom_badness() score is sacrificed for its |
5e9d834a0
|
827 828 829 |
* parent. This attempts to lose the minimal amount of work done while * still freeing memory. */ |
6b0c81b3b
|
830 |
read_lock(&tasklist_lock); |
1da4db0cd
|
831 |
for_each_thread(p, t) { |
5e9d834a0
|
832 |
list_for_each_entry(child, &t->children, sibling) { |
a63d83f42
|
833 |
unsigned int child_points; |
5e9d834a0
|
834 |
|
4d7b3394f
|
835 |
if (process_shares_mm(child, p->mm)) |
edd45544c
|
836 |
continue; |
a63d83f42
|
837 838 839 |
/* * oom_badness() returns 0 if the thread is unkillable */ |
2a966b77a
|
840 841 |
child_points = oom_badness(child, oc->memcg, oc->nodemask, totalpages); |
5e9d834a0
|
842 |
if (child_points > victim_points) { |
6b0c81b3b
|
843 |
put_task_struct(victim); |
5e9d834a0
|
844 845 |
victim = child; victim_points = child_points; |
6b0c81b3b
|
846 |
get_task_struct(victim); |
5e9d834a0
|
847 |
} |
dd8e8f405
|
848 |
} |
1da4db0cd
|
849 |
} |
6b0c81b3b
|
850 |
read_unlock(&tasklist_lock); |
dd8e8f405
|
851 |
|
6b0c81b3b
|
852 853 |
p = find_lock_task_mm(victim); if (!p) { |
6b0c81b3b
|
854 |
put_task_struct(victim); |
647f2bdf4
|
855 |
return; |
6b0c81b3b
|
856 857 858 859 860 |
} else if (victim != p) { get_task_struct(p); put_task_struct(victim); victim = p; } |
647f2bdf4
|
861 |
|
880b76893
|
862 |
/* Get a reference to safely compare mm after task_unlock(victim) */ |
647f2bdf4
|
863 |
mm = victim->mm; |
880b76893
|
864 |
atomic_inc(&mm->mm_count); |
426fb5e72
|
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
|
871 |
mark_oom_victim(victim); |
eca56ff90
|
872 873 |
pr_err("Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB ", |
647f2bdf4
|
874 875 |
task_pid_nr(victim), victim->comm, K(victim->mm->total_vm), K(get_mm_counter(victim->mm, MM_ANONPAGES)), |
eca56ff90
|
876 877 |
K(get_mm_counter(victim->mm, MM_FILEPAGES)), K(get_mm_counter(victim->mm, MM_SHMEMPAGES))); |
647f2bdf4
|
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
|
889 |
rcu_read_lock(); |
c319025a6
|
890 |
for_each_process(p) { |
4d7b3394f
|
891 |
if (!process_shares_mm(p, mm)) |
c319025a6
|
892 893 894 |
continue; if (same_thread_group(p, victim)) continue; |
97fd49c23
|
895 |
if (unlikely(p->flags & PF_KTHREAD) || is_global_init(p)) { |
aac453635
|
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
|
899 900 |
* memory might be still used. Hide the mm from the oom * killer to guarantee OOM forward progress. |
aac453635
|
901 902 |
*/ can_oom_reap = false; |
a373966d1
|
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
|
908 |
continue; |
aac453635
|
909 |
} |
c319025a6
|
910 911 |
do_send_sig_info(SIGKILL, SEND_SIG_FORCED, p, true); } |
6b0c81b3b
|
912 |
rcu_read_unlock(); |
647f2bdf4
|
913 |
|
aac453635
|
914 |
if (can_oom_reap) |
36324a990
|
915 |
wake_oom_reaper(victim); |
aac453635
|
916 |
|
880b76893
|
917 |
mmdrop(mm); |
6b0c81b3b
|
918 |
put_task_struct(victim); |
1da177e4c
|
919 |
} |
647f2bdf4
|
920 |
#undef K |
1da177e4c
|
921 |
|
309ed8825
|
922 923 924 |
/* * Determines whether the kernel must panic because of the panic_on_oom sysctl. */ |
2a966b77a
|
925 |
void check_panic_on_oom(struct oom_control *oc, enum oom_constraint constraint) |
309ed8825
|
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
|
938 |
/* Do not panic for oom kills triggered by sysrq */ |
db2a0dd7a
|
939 |
if (is_sysrq_oom(oc)) |
071a4befe
|
940 |
return; |
2a966b77a
|
941 |
dump_header(oc, NULL); |
309ed8825
|
942 943 944 945 |
panic("Out of memory: %s panic_on_oom is enabled ", sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide"); } |
8bc719d3c
|
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
|
959 |
/** |
6e0fc46dc
|
960 961 |
* out_of_memory - kill the "best" process when we run out of memory * @oc: pointer to struct oom_control |
1da177e4c
|
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
|
968 |
bool out_of_memory(struct oom_control *oc) |
1da177e4c
|
969 |
{ |
0aad4b312
|
970 |
struct task_struct *p; |
a63d83f42
|
971 |
unsigned long totalpages; |
8bc719d3c
|
972 |
unsigned long freed = 0; |
9cbb78bb3
|
973 |
unsigned int uninitialized_var(points); |
e36589323
|
974 |
enum oom_constraint constraint = CONSTRAINT_NONE; |
8bc719d3c
|
975 |
|
dc56401fc
|
976 977 |
if (oom_killer_disabled) return false; |
8bc719d3c
|
978 979 980 |
blocking_notifier_call_chain(&oom_notify_list, 0, &freed); if (freed > 0) /* Got some memory back in the last second. */ |
75e8f8b24
|
981 |
return true; |
1da177e4c
|
982 |
|
7b98c2e40
|
983 |
/* |
9ff4868e3
|
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
|
987 |
*/ |
091f362c5
|
988 |
if (task_will_free_mem(current)) { |
16e951966
|
989 |
mark_oom_victim(current); |
1af8bb432
|
990 |
wake_oom_reaper(current); |
75e8f8b24
|
991 |
return true; |
7b98c2e40
|
992 |
} |
9b0f8b040
|
993 |
/* |
3da88fb3b
|
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
|
1003 1004 1005 |
* Check if there were limitations on the allocation (only relevant for * NUMA) that may require different handling. */ |
6e0fc46dc
|
1006 1007 1008 |
constraint = constrained_alloc(oc, &totalpages); if (constraint != CONSTRAINT_MEMORY_POLICY) oc->nodemask = NULL; |
2a966b77a
|
1009 |
check_panic_on_oom(oc, constraint); |
0aad4b312
|
1010 |
|
121d1ba0a
|
1011 |
if (sysctl_oom_kill_allocating_task && current->mm && |
6e0fc46dc
|
1012 |
!oom_unkillable_task(current, NULL, oc->nodemask) && |
121d1ba0a
|
1013 |
current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) { |
6b0c81b3b
|
1014 |
get_task_struct(current); |
2a966b77a
|
1015 |
oom_kill_process(oc, current, 0, totalpages, |
2a1c9b1fc
|
1016 |
"Out of memory (oom_kill_allocating_task)"); |
75e8f8b24
|
1017 |
return true; |
0aad4b312
|
1018 |
} |
6e0fc46dc
|
1019 |
p = select_bad_process(oc, &points, totalpages); |
0aad4b312
|
1020 |
/* Found nothing?!?! Either we hang forever, or we panic. */ |
db2a0dd7a
|
1021 |
if (!p && !is_sysrq_oom(oc)) { |
2a966b77a
|
1022 |
dump_header(oc, NULL); |
0aad4b312
|
1023 1024 1025 |
panic("Out of memory and no killable processes... "); } |
071a4befe
|
1026 |
if (p && p != (void *)-1UL) { |
2a966b77a
|
1027 |
oom_kill_process(oc, p, points, totalpages, "Out of memory"); |
75e8f8b24
|
1028 1029 1030 1031 |
/* * Give the killed process a good chance to exit before trying * to allocate memory again. */ |
4f774b912
|
1032 |
schedule_timeout_killable(1); |
75e8f8b24
|
1033 |
} |
dc56401fc
|
1034 |
return true; |
c32b3cbe0
|
1035 |
} |
e36589323
|
1036 1037 |
/* * The pagefault handler calls here because it is out of memory, so kill a |
798fd7569
|
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
|
1040 1041 1042 |
*/ void pagefault_out_of_memory(void) { |
6e0fc46dc
|
1043 1044 1045 |
struct oom_control oc = { .zonelist = NULL, .nodemask = NULL, |
2a966b77a
|
1046 |
.memcg = NULL, |
6e0fc46dc
|
1047 1048 |
.gfp_mask = 0, .order = 0, |
6e0fc46dc
|
1049 |
}; |
494264208
|
1050 |
if (mem_cgroup_oom_synchronize(true)) |
dc56401fc
|
1051 |
return; |
3812c8c8f
|
1052 |
|
dc56401fc
|
1053 1054 |
if (!mutex_trylock(&oom_lock)) return; |
c32b3cbe0
|
1055 |
|
6e0fc46dc
|
1056 |
if (!out_of_memory(&oc)) { |
dc56401fc
|
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
|
1064 |
} |
dc56401fc
|
1065 1066 |
mutex_unlock(&oom_lock); |
e36589323
|
1067 |
} |