Blame view
mm/oom_kill.c
30 KB
457c89965
|
1 |
// SPDX-License-Identifier: GPL-2.0-only |
1da177e4c
|
2 3 4 5 6 7 |
/* * 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
|
8 9 |
* Copyright (C) 2010 Google, Inc. * Rewritten by David Rientjes |
1da177e4c
|
10 11 |
* * The routines in this file are used to kill a process when |
a49335cce
|
12 13 |
* 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
|
14 15 16 17 18 19 |
* * 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
|
20 |
#include <linux/oom.h> |
1da177e4c
|
21 |
#include <linux/mm.h> |
4e950f6f0
|
22 |
#include <linux/err.h> |
5a0e3ad6a
|
23 |
#include <linux/gfp.h> |
1da177e4c
|
24 |
#include <linux/sched.h> |
6e84f3152
|
25 |
#include <linux/sched/mm.h> |
f7ccbae45
|
26 |
#include <linux/sched/coredump.h> |
299300258
|
27 |
#include <linux/sched/task.h> |
1da177e4c
|
28 29 30 |
#include <linux/swap.h> #include <linux/timex.h> #include <linux/jiffies.h> |
ef08e3b49
|
31 |
#include <linux/cpuset.h> |
b95f1b31b
|
32 |
#include <linux/export.h> |
8bc719d3c
|
33 |
#include <linux/notifier.h> |
c7ba5c9e8
|
34 |
#include <linux/memcontrol.h> |
6f48d0ebd
|
35 |
#include <linux/mempolicy.h> |
5cd9c58fb
|
36 |
#include <linux/security.h> |
edd45544c
|
37 |
#include <linux/ptrace.h> |
f660daac4
|
38 |
#include <linux/freezer.h> |
43d2b1132
|
39 |
#include <linux/ftrace.h> |
dc3f21ead
|
40 |
#include <linux/ratelimit.h> |
aac453635
|
41 42 |
#include <linux/kthread.h> #include <linux/init.h> |
4d4bbd852
|
43 |
#include <linux/mmu_notifier.h> |
aac453635
|
44 45 46 |
#include <asm/tlb.h> #include "internal.h" |
852d8be0a
|
47 |
#include "slab.h" |
43d2b1132
|
48 49 50 |
#define CREATE_TRACE_POINTS #include <trace/events/oom.h> |
1da177e4c
|
51 |
|
fadd8fbd1
|
52 |
int sysctl_panic_on_oom; |
fe071d7e8
|
53 |
int sysctl_oom_kill_allocating_task; |
ad915c432
|
54 |
int sysctl_oom_dump_tasks = 1; |
dc56401fc
|
55 |
|
a195d3f5b
|
56 57 58 59 60 61 62 63 |
/* * Serializes oom killer invocations (out_of_memory()) from all contexts to * prevent from over eager oom killing (e.g. when the oom killer is invoked * from different domains). * * oom_killer_disable() relies on this lock to stabilize oom_killer_disabled * and mark_oom_victim */ |
dc56401fc
|
64 |
DEFINE_MUTEX(oom_lock); |
1da177e4c
|
65 |
|
ac311a14c
|
66 67 68 69 |
static inline bool is_memcg_oom(struct oom_control *oc) { return oc->memcg != NULL; } |
6f48d0ebd
|
70 71 |
#ifdef CONFIG_NUMA /** |
ac311a14c
|
72 |
* oom_cpuset_eligible() - check task eligiblity for kill |
ad9624417
|
73 |
* @start: task struct of which task to consider |
6f48d0ebd
|
74 75 76 77 78 |
* @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. |
ac311a14c
|
79 80 81 |
* * This function is assuming oom-killer context and 'current' has triggered * the oom-killer. |
495789a51
|
82 |
*/ |
ac311a14c
|
83 84 |
static bool oom_cpuset_eligible(struct task_struct *start, struct oom_control *oc) |
495789a51
|
85 |
{ |
ad9624417
|
86 87 |
struct task_struct *tsk; bool ret = false; |
ac311a14c
|
88 89 90 91 |
const nodemask_t *mask = oc->nodemask; if (is_memcg_oom(oc)) return true; |
495789a51
|
92 |
|
ad9624417
|
93 |
rcu_read_lock(); |
1da4db0cd
|
94 |
for_each_thread(start, tsk) { |
6f48d0ebd
|
95 96 97 98 99 100 101 |
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
|
102 |
ret = mempolicy_nodemask_intersects(tsk, mask); |
6f48d0ebd
|
103 104 105 106 107 |
} else { /* * This is not a mempolicy constrained oom, so only * check the mems of tsk's cpuset. */ |
ad9624417
|
108 |
ret = cpuset_mems_allowed_intersects(current, tsk); |
6f48d0ebd
|
109 |
} |
ad9624417
|
110 111 |
if (ret) break; |
1da4db0cd
|
112 |
} |
ad9624417
|
113 |
rcu_read_unlock(); |
df1090a8d
|
114 |
|
ad9624417
|
115 |
return ret; |
6f48d0ebd
|
116 117 |
} #else |
ac311a14c
|
118 |
static bool oom_cpuset_eligible(struct task_struct *tsk, struct oom_control *oc) |
6f48d0ebd
|
119 120 |
{ return true; |
495789a51
|
121 |
} |
6f48d0ebd
|
122 |
#endif /* CONFIG_NUMA */ |
495789a51
|
123 |
|
6f48d0ebd
|
124 125 126 127 128 129 |
/* * 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
|
130 |
struct task_struct *find_lock_task_mm(struct task_struct *p) |
dd8e8f405
|
131 |
{ |
1da4db0cd
|
132 |
struct task_struct *t; |
dd8e8f405
|
133 |
|
4d4048be8
|
134 |
rcu_read_lock(); |
1da4db0cd
|
135 |
for_each_thread(p, t) { |
dd8e8f405
|
136 137 |
task_lock(t); if (likely(t->mm)) |
4d4048be8
|
138 |
goto found; |
dd8e8f405
|
139 |
task_unlock(t); |
1da4db0cd
|
140 |
} |
4d4048be8
|
141 142 143 |
t = NULL; found: rcu_read_unlock(); |
dd8e8f405
|
144 |
|
4d4048be8
|
145 |
return t; |
dd8e8f405
|
146 |
} |
db2a0dd7a
|
147 148 149 150 151 152 153 154 |
/* * 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
|
155 |
/* return true if the task is not adequate as candidate victim task. */ |
ac311a14c
|
156 |
static bool oom_unkillable_task(struct task_struct *p) |
ab290adba
|
157 158 159 160 161 |
{ if (is_global_init(p)) return true; if (p->flags & PF_KTHREAD) return true; |
ab290adba
|
162 163 |
return false; } |
852d8be0a
|
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
/* * Print out unreclaimble slabs info when unreclaimable slabs amount is greater * than all user memory (LRU pages) */ static bool is_dump_unreclaim_slabs(void) { unsigned long nr_lru; nr_lru = global_node_page_state(NR_ACTIVE_ANON) + global_node_page_state(NR_INACTIVE_ANON) + global_node_page_state(NR_ACTIVE_FILE) + global_node_page_state(NR_INACTIVE_FILE) + global_node_page_state(NR_ISOLATED_ANON) + global_node_page_state(NR_ISOLATED_FILE) + global_node_page_state(NR_UNEVICTABLE); return (global_node_page_state(NR_SLAB_UNRECLAIMABLE) > nr_lru); } |
1da177e4c
|
182 |
/** |
a63d83f42
|
183 |
* oom_badness - heuristic function to determine which candidate task to kill |
1da177e4c
|
184 |
* @p: task struct of which task we should calculate |
a63d83f42
|
185 |
* @totalpages: total present RAM allowed for page allocation |
1da177e4c
|
186 |
* |
a63d83f42
|
187 188 189 |
* 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
|
190 |
*/ |
ac311a14c
|
191 |
unsigned long oom_badness(struct task_struct *p, unsigned long totalpages) |
1da177e4c
|
192 |
{ |
1e11ad8dc
|
193 |
long points; |
61eafb00d
|
194 |
long adj; |
28b83c519
|
195 |
|
ac311a14c
|
196 |
if (oom_unkillable_task(p)) |
26ebc9849
|
197 |
return 0; |
1da177e4c
|
198 |
|
dd8e8f405
|
199 200 |
p = find_lock_task_mm(p); if (!p) |
1da177e4c
|
201 |
return 0; |
bb8a4b7fd
|
202 203 |
/* * Do not even consider tasks which are explicitly marked oom |
b18dc5f29
|
204 205 |
* unkillable or have been already oom reaped or the are in * the middle of vfork |
bb8a4b7fd
|
206 |
*/ |
a9c58b907
|
207 |
adj = (long)p->signal->oom_score_adj; |
bb8a4b7fd
|
208 |
if (adj == OOM_SCORE_ADJ_MIN || |
862e3073b
|
209 |
test_bit(MMF_OOM_SKIP, &p->mm->flags) || |
b18dc5f29
|
210 |
in_vfork(p)) { |
5aecc85ab
|
211 212 213 |
task_unlock(p); return 0; } |
1da177e4c
|
214 |
/* |
a63d83f42
|
215 |
* The baseline for the badness score is the proportion of RAM that each |
f755a042d
|
216 |
* task's rss, pagetable and swap space use. |
1da177e4c
|
217 |
*/ |
dc6c9a35b
|
218 |
points = get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) + |
af5b0f6a0
|
219 |
mm_pgtables_bytes(p->mm) / PAGE_SIZE; |
a63d83f42
|
220 |
task_unlock(p); |
1da177e4c
|
221 |
|
61eafb00d
|
222 223 224 |
/* Normalize to oom_score_adj units */ adj *= totalpages / 1000; points += adj; |
1da177e4c
|
225 |
|
f19e8aa11
|
226 |
/* |
a7f638f99
|
227 228 |
* 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
|
229 |
*/ |
1e11ad8dc
|
230 |
return points > 0 ? points : 1; |
1da177e4c
|
231 |
} |
ef8444ea0
|
232 233 234 235 236 |
static const char * const oom_constraint_text[] = { [CONSTRAINT_NONE] = "CONSTRAINT_NONE", [CONSTRAINT_CPUSET] = "CONSTRAINT_CPUSET", [CONSTRAINT_MEMORY_POLICY] = "CONSTRAINT_MEMORY_POLICY", [CONSTRAINT_MEMCG] = "CONSTRAINT_MEMCG", |
7c5f64f84
|
237 |
}; |
1da177e4c
|
238 |
/* |
9b0f8b040
|
239 240 |
* Determine the type of allocation constraint. */ |
7c5f64f84
|
241 |
static enum oom_constraint constrained_alloc(struct oom_control *oc) |
4365a5676
|
242 |
{ |
54a6eb5c4
|
243 |
struct zone *zone; |
dd1a239f6
|
244 |
struct zoneref *z; |
6e0fc46dc
|
245 |
enum zone_type high_zoneidx = gfp_zone(oc->gfp_mask); |
a63d83f42
|
246 247 |
bool cpuset_limited = false; int nid; |
9b0f8b040
|
248 |
|
7c5f64f84
|
249 |
if (is_memcg_oom(oc)) { |
bbec2e151
|
250 |
oc->totalpages = mem_cgroup_get_max(oc->memcg) ?: 1; |
7c5f64f84
|
251 252 |
return CONSTRAINT_MEMCG; } |
a63d83f42
|
253 |
/* Default to all available memory */ |
ca79b0c21
|
254 |
oc->totalpages = totalram_pages() + total_swap_pages; |
7c5f64f84
|
255 256 257 |
if (!IS_ENABLED(CONFIG_NUMA)) return CONSTRAINT_NONE; |
a63d83f42
|
258 |
|
6e0fc46dc
|
259 |
if (!oc->zonelist) |
a63d83f42
|
260 |
return CONSTRAINT_NONE; |
4365a5676
|
261 262 263 264 265 |
/* * 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
|
266 |
if (oc->gfp_mask & __GFP_THISNODE) |
4365a5676
|
267 |
return CONSTRAINT_NONE; |
9b0f8b040
|
268 |
|
4365a5676
|
269 |
/* |
a63d83f42
|
270 271 272 |
* 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
|
273 |
*/ |
6e0fc46dc
|
274 275 |
if (oc->nodemask && !nodes_subset(node_states[N_MEMORY], *oc->nodemask)) { |
7c5f64f84
|
276 |
oc->totalpages = total_swap_pages; |
6e0fc46dc
|
277 |
for_each_node_mask(nid, *oc->nodemask) |
7c5f64f84
|
278 |
oc->totalpages += node_spanned_pages(nid); |
9b0f8b040
|
279 |
return CONSTRAINT_MEMORY_POLICY; |
a63d83f42
|
280 |
} |
4365a5676
|
281 282 |
/* Check this allocation failure is caused by cpuset's wall function */ |
6e0fc46dc
|
283 284 285 |
for_each_zone_zonelist_nodemask(zone, z, oc->zonelist, high_zoneidx, oc->nodemask) if (!cpuset_zone_allowed(zone, oc->gfp_mask)) |
a63d83f42
|
286 |
cpuset_limited = true; |
9b0f8b040
|
287 |
|
a63d83f42
|
288 |
if (cpuset_limited) { |
7c5f64f84
|
289 |
oc->totalpages = total_swap_pages; |
a63d83f42
|
290 |
for_each_node_mask(nid, cpuset_current_mems_allowed) |
7c5f64f84
|
291 |
oc->totalpages += node_spanned_pages(nid); |
a63d83f42
|
292 293 |
return CONSTRAINT_CPUSET; } |
9b0f8b040
|
294 295 |
return CONSTRAINT_NONE; } |
7c5f64f84
|
296 |
static int oom_evaluate_task(struct task_struct *task, void *arg) |
462607ecc
|
297 |
{ |
7c5f64f84
|
298 299 |
struct oom_control *oc = arg; unsigned long points; |
ac311a14c
|
300 301 302 303 304 |
if (oom_unkillable_task(task)) goto next; /* p may not have freeable memory in nodemask */ if (!is_memcg_oom(oc) && !oom_cpuset_eligible(task, oc)) |
7c5f64f84
|
305 |
goto next; |
462607ecc
|
306 307 308 |
/* * This task already has access to memory reserves and is being killed. |
a373966d1
|
309 |
* Don't allow any other task to have access to the reserves unless |
862e3073b
|
310 |
* the task has MMF_OOM_SKIP because chances that it would release |
a373966d1
|
311 |
* any memory is quite low. |
462607ecc
|
312 |
*/ |
862e3073b
|
313 314 |
if (!is_sysrq_oom(oc) && tsk_is_oom_victim(task)) { if (test_bit(MMF_OOM_SKIP, &task->signal->oom_mm->flags)) |
7c5f64f84
|
315 316 |
goto next; goto abort; |
a373966d1
|
317 |
} |
462607ecc
|
318 |
|
e1e12d2f3
|
319 320 321 322 |
/* * If task is allocating a lot of memory and has been marked to be * killed first if it triggers an oom, then select it. */ |
7c5f64f84
|
323 324 325 326 |
if (oom_task_origin(task)) { points = ULONG_MAX; goto select; } |
e1e12d2f3
|
327 |
|
ac311a14c
|
328 |
points = oom_badness(task, oc->totalpages); |
7c5f64f84
|
329 330 |
if (!points || points < oc->chosen_points) goto next; |
7c5f64f84
|
331 332 333 334 335 336 337 338 339 340 341 342 343 |
select: if (oc->chosen) put_task_struct(oc->chosen); get_task_struct(task); oc->chosen = task; oc->chosen_points = points; next: return 0; abort: if (oc->chosen) put_task_struct(oc->chosen); oc->chosen = (void *)-1UL; return 1; |
462607ecc
|
344 |
} |
9b0f8b040
|
345 |
/* |
7c5f64f84
|
346 347 |
* Simple selection loop. We choose the process with the highest number of * 'points'. In case scan was aborted, oc->chosen is set to -1. |
1da177e4c
|
348 |
*/ |
7c5f64f84
|
349 |
static void select_bad_process(struct oom_control *oc) |
1da177e4c
|
350 |
{ |
7c5f64f84
|
351 352 353 354 |
if (is_memcg_oom(oc)) mem_cgroup_scan_tasks(oc->memcg, oom_evaluate_task, oc); else { struct task_struct *p; |
d49ad9355
|
355 |
|
7c5f64f84
|
356 357 358 359 360 |
rcu_read_lock(); for_each_process(p) if (oom_evaluate_task(p, oc)) break; rcu_read_unlock(); |
1da4db0cd
|
361 |
} |
1da177e4c
|
362 |
} |
5eee7e1cd
|
363 364 365 366 |
static int dump_task(struct task_struct *p, void *arg) { struct oom_control *oc = arg; struct task_struct *task; |
ac311a14c
|
367 368 369 370 371 |
if (oom_unkillable_task(p)) return 0; /* p may not have freeable memory in nodemask */ if (!is_memcg_oom(oc) && !oom_cpuset_eligible(p, oc)) |
5eee7e1cd
|
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
return 0; task = find_lock_task_mm(p); if (!task) { /* * This is a kthread or all of p's threads have already * detached their mm's. There's no need to report * them; they can't be oom killed anyway. */ return 0; } pr_info("[%7d] %5d %5d %8lu %8lu %8ld %8lu %5hd %s ", task->pid, from_kuid(&init_user_ns, task_uid(task)), task->tgid, task->mm->total_vm, get_mm_rss(task->mm), mm_pgtables_bytes(task->mm), get_mm_counter(task->mm, MM_SWAPENTS), task->signal->oom_score_adj, task->comm); task_unlock(task); return 0; } |
1da177e4c
|
395 |
/** |
1b578df02
|
396 |
* dump_tasks - dump current memory state of all system tasks |
5eee7e1cd
|
397 |
* @oc: pointer to struct oom_control |
1b578df02
|
398 |
* |
e85bfd3aa
|
399 400 401 |
* 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. |
af5b0f6a0
|
402 403 |
* State information includes task's pid, uid, tgid, vm size, rss, * pgtables_bytes, swapents, oom_score_adj value, and name. |
fef1bdd68
|
404 |
*/ |
5eee7e1cd
|
405 |
static void dump_tasks(struct oom_control *oc) |
fef1bdd68
|
406 |
{ |
c3b78b11e
|
407 408 409 410 |
pr_info("Tasks state (memory values in pages): "); pr_info("[ pid ] uid tgid total_vm rss pgtables_bytes swapents oom_score_adj name "); |
fef1bdd68
|
411 |
|
5eee7e1cd
|
412 413 414 415 |
if (is_memcg_oom(oc)) mem_cgroup_scan_tasks(oc->memcg, dump_task, oc); else { struct task_struct *p; |
c55db9578
|
416 |
|
5eee7e1cd
|
417 418 419 420 |
rcu_read_lock(); for_each_process(p) dump_task(p, oc); rcu_read_unlock(); |
c55db9578
|
421 |
} |
fef1bdd68
|
422 |
} |
ef8444ea0
|
423 424 425 426 427 428 429 |
static void dump_oom_summary(struct oom_control *oc, struct task_struct *victim) { /* one line summary of the oom killer context. */ pr_info("oom-kill:constraint=%s,nodemask=%*pbl", oom_constraint_text[oc->constraint], nodemask_pr_args(oc->nodemask)); cpuset_print_current_mems_allowed(); |
f0c867d95
|
430 |
mem_cgroup_print_oom_context(oc->memcg, victim); |
ef8444ea0
|
431 432 433 434 |
pr_cont(",task=%s,pid=%d,uid=%d ", victim->comm, victim->pid, from_kuid(&init_user_ns, task_uid(victim))); } |
2a966b77a
|
435 |
static void dump_header(struct oom_control *oc, struct task_struct *p) |
1b604d75b
|
436 |
{ |
ef8444ea0
|
437 438 439 |
pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd ", current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order, |
0205f7557
|
440 |
current->signal->oom_score_adj); |
9254990fb
|
441 442 443 |
if (!IS_ENABLED(CONFIG_COMPACTION) && oc->order) pr_warn("COMPACTION is disabled!!! "); |
a0795cd41
|
444 |
|
1b604d75b
|
445 |
dump_stack(); |
852d8be0a
|
446 |
if (is_memcg_oom(oc)) |
f0c867d95
|
447 |
mem_cgroup_print_oom_meminfo(oc->memcg); |
852d8be0a
|
448 |
else { |
299c517ad
|
449 |
show_mem(SHOW_MEM_FILTER_NODES, oc->nodemask); |
852d8be0a
|
450 451 452 |
if (is_dump_unreclaim_slabs()) dump_unreclaimable_slab(); } |
1b604d75b
|
453 |
if (sysctl_oom_dump_tasks) |
5eee7e1cd
|
454 |
dump_tasks(oc); |
ef8444ea0
|
455 456 |
if (p) dump_oom_summary(oc, p); |
1b604d75b
|
457 |
} |
5695be142
|
458 |
/* |
c32b3cbe0
|
459 |
* Number of OOM victims in flight |
5695be142
|
460 |
*/ |
c32b3cbe0
|
461 462 |
static atomic_t oom_victims = ATOMIC_INIT(0); static DECLARE_WAIT_QUEUE_HEAD(oom_victims_wait); |
5695be142
|
463 |
|
7c5f64f84
|
464 |
static bool oom_killer_disabled __read_mostly; |
5695be142
|
465 |
|
bc448e897
|
466 |
#define K(x) ((x) << (PAGE_SHIFT-10)) |
3ef22dfff
|
467 468 469 470 471 472 |
/* * 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
|
473 |
bool process_shares_mm(struct task_struct *p, struct mm_struct *mm) |
3ef22dfff
|
474 475 476 477 478 479 480 481 482 483 |
{ 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
|
484 485 486 487 488 489 |
#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
|
490 |
static DECLARE_WAIT_QUEUE_HEAD(oom_reaper_wait); |
29c696e1c
|
491 |
static struct task_struct *oom_reaper_list; |
03049269d
|
492 |
static DEFINE_SPINLOCK(oom_reaper_lock); |
93065ac75
|
493 |
bool __oom_reap_task_mm(struct mm_struct *mm) |
aac453635
|
494 |
{ |
aac453635
|
495 |
struct vm_area_struct *vma; |
93065ac75
|
496 |
bool ret = true; |
27ae357fa
|
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
/* * Tell all users of get_user/copy_from_user etc... that the content * is no longer stable. No barriers really needed because unmapping * should imply barriers already and the reader would hit a page fault * if it stumbled over a reaped memory. */ set_bit(MMF_UNSTABLE, &mm->flags); for (vma = mm->mmap ; vma; vma = vma->vm_next) { if (!can_madv_dontneed_vma(vma)) 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)) { |
ac46d4f3c
|
521 |
struct mmu_notifier_range range; |
27ae357fa
|
522 |
struct mmu_gather tlb; |
6f4f13e8d
|
523 524 |
mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, mm, vma->vm_start, |
ac46d4f3c
|
525 526 527 528 |
vma->vm_end); tlb_gather_mmu(&tlb, mm, range.start, range.end); if (mmu_notifier_invalidate_range_start_nonblock(&range)) { tlb_finish_mmu(&tlb, range.start, range.end); |
93065ac75
|
529 530 531 |
ret = false; continue; } |
ac46d4f3c
|
532 533 534 |
unmap_page_range(&tlb, vma, range.start, range.end, NULL); mmu_notifier_invalidate_range_end(&range); tlb_finish_mmu(&tlb, range.start, range.end); |
27ae357fa
|
535 536 |
} } |
93065ac75
|
537 538 |
return ret; |
27ae357fa
|
539 |
} |
431f42fdf
|
540 541 542 543 544 545 |
/* * Reaps the address space of the give task. * * Returns true on success and false if none or part of the address space * has been reclaimed and the caller should retry later. */ |
27ae357fa
|
546 547 |
static bool oom_reap_task_mm(struct task_struct *tsk, struct mm_struct *mm) { |
aac453635
|
548 |
bool ret = true; |
aac453635
|
549 |
if (!down_read_trylock(&mm->mmap_sem)) { |
422580c3c
|
550 |
trace_skip_task_reaping(tsk->pid); |
af5679fbc
|
551 |
return false; |
4d4bbd852
|
552 553 554 |
} /* |
212925802
|
555 556 557 558 |
* MMF_OOM_SKIP is set by exit_mmap when the OOM reaper can't * work on the mm anymore. The check for MMF_OOM_SKIP must run * under mmap_sem for reading because it serializes against the * down_write();up_write() cycle in exit_mmap(). |
e5e3f4c4f
|
559 |
*/ |
212925802
|
560 |
if (test_bit(MMF_OOM_SKIP, &mm->flags)) { |
422580c3c
|
561 |
trace_skip_task_reaping(tsk->pid); |
431f42fdf
|
562 |
goto out_unlock; |
aac453635
|
563 |
} |
422580c3c
|
564 |
trace_start_task_reaping(tsk->pid); |
93065ac75
|
565 |
/* failed to reap part of the address space. Try again later */ |
431f42fdf
|
566 567 568 |
ret = __oom_reap_task_mm(mm); if (!ret) goto out_finish; |
aac453635
|
569 |
|
bc448e897
|
570 571 572 573 574 575 |
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))); |
431f42fdf
|
576 577 578 |
out_finish: trace_finish_task_reaping(tsk->pid); out_unlock: |
aac453635
|
579 |
up_read(&mm->mmap_sem); |
36324a990
|
580 |
|
aac453635
|
581 582 |
return ret; } |
bc448e897
|
583 |
#define MAX_OOM_REAP_RETRIES 10 |
36324a990
|
584 |
static void oom_reap_task(struct task_struct *tsk) |
aac453635
|
585 586 |
{ int attempts = 0; |
26db62f17
|
587 |
struct mm_struct *mm = tsk->signal->oom_mm; |
aac453635
|
588 589 |
/* Retry the down_read_trylock(mmap_sem) a few times */ |
27ae357fa
|
590 |
while (attempts++ < MAX_OOM_REAP_RETRIES && !oom_reap_task_mm(tsk, mm)) |
aac453635
|
591 |
schedule_timeout_idle(HZ/10); |
97b1255cb
|
592 593 |
if (attempts <= MAX_OOM_REAP_RETRIES || test_bit(MMF_OOM_SKIP, &mm->flags)) |
7ebffa455
|
594 |
goto done; |
11a410d51
|
595 |
|
7ebffa455
|
596 597 598 |
pr_info("oom_reaper: unable to reap pid:%d (%s) ", task_pid_nr(tsk), tsk->comm); |
7ebffa455
|
599 |
debug_show_all_locks(); |
bc448e897
|
600 |
|
7ebffa455
|
601 |
done: |
449d777d7
|
602 |
tsk->oom_reaper_list = NULL; |
449d777d7
|
603 |
|
26db62f17
|
604 605 606 607 |
/* * Hide this mm from OOM killer because it has been either reaped or * somebody can't call up_write(mmap_sem). */ |
862e3073b
|
608 |
set_bit(MMF_OOM_SKIP, &mm->flags); |
26db62f17
|
609 |
|
aac453635
|
610 |
/* Drop a reference taken by wake_oom_reaper */ |
36324a990
|
611 |
put_task_struct(tsk); |
aac453635
|
612 613 614 615 616 |
} static int oom_reaper(void *unused) { while (true) { |
03049269d
|
617 |
struct task_struct *tsk = NULL; |
aac453635
|
618 |
|
29c696e1c
|
619 |
wait_event_freezable(oom_reaper_wait, oom_reaper_list != NULL); |
03049269d
|
620 |
spin_lock(&oom_reaper_lock); |
29c696e1c
|
621 622 623 |
if (oom_reaper_list != NULL) { tsk = oom_reaper_list; oom_reaper_list = tsk->oom_reaper_list; |
03049269d
|
624 625 626 627 628 |
} spin_unlock(&oom_reaper_lock); if (tsk) oom_reap_task(tsk); |
aac453635
|
629 630 631 632 |
} return 0; } |
7c5f64f84
|
633 |
static void wake_oom_reaper(struct task_struct *tsk) |
aac453635
|
634 |
{ |
9bcdeb51b
|
635 636 |
/* mm is already queued? */ if (test_and_set_bit(MMF_OOM_REAP_QUEUED, &tsk->signal->oom_mm->flags)) |
aac453635
|
637 |
return; |
36324a990
|
638 |
get_task_struct(tsk); |
aac453635
|
639 |
|
03049269d
|
640 |
spin_lock(&oom_reaper_lock); |
29c696e1c
|
641 642 |
tsk->oom_reaper_list = oom_reaper_list; oom_reaper_list = tsk; |
03049269d
|
643 |
spin_unlock(&oom_reaper_lock); |
422580c3c
|
644 |
trace_wake_reaper(tsk->pid); |
03049269d
|
645 |
wake_up(&oom_reaper_wait); |
aac453635
|
646 647 648 649 650 |
} static int __init oom_init(void) { oom_reaper_th = kthread_run(oom_reaper, NULL, "oom_reaper"); |
aac453635
|
651 652 653 |
return 0; } subsys_initcall(oom_init) |
7c5f64f84
|
654 655 656 657 658 |
#else static inline void wake_oom_reaper(struct task_struct *tsk) { } #endif /* CONFIG_MMU */ |
aac453635
|
659 |
|
49550b605
|
660 |
/** |
16e951966
|
661 |
* mark_oom_victim - mark the given task as OOM victim |
49550b605
|
662 |
* @tsk: task to mark |
c32b3cbe0
|
663 |
* |
dc56401fc
|
664 |
* Has to be called with oom_lock held and never after |
c32b3cbe0
|
665 |
* oom has been disabled already. |
26db62f17
|
666 667 668 |
* * tsk->mm has to be non NULL and caller has to guarantee it is stable (either * under task_lock or operate on the current). |
49550b605
|
669 |
*/ |
7c5f64f84
|
670 |
static void mark_oom_victim(struct task_struct *tsk) |
49550b605
|
671 |
{ |
26db62f17
|
672 |
struct mm_struct *mm = tsk->mm; |
c32b3cbe0
|
673 674 675 676 |
WARN_ON(oom_killer_disabled); /* OOM killer might race with memcg OOM */ if (test_and_set_tsk_thread_flag(tsk, TIF_MEMDIE)) return; |
26db62f17
|
677 |
|
26db62f17
|
678 |
/* oom_mm is bound to the signal struct life time. */ |
4837fe37a
|
679 |
if (!cmpxchg(&tsk->signal->oom_mm, NULL, mm)) { |
f1f100764
|
680 |
mmgrab(tsk->signal->oom_mm); |
4837fe37a
|
681 682 |
set_bit(MMF_OOM_VICTIM, &mm->flags); } |
26db62f17
|
683 |
|
63a8ca9b2
|
684 685 686 687 688 689 690 |
/* * 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
|
691 |
atomic_inc(&oom_victims); |
422580c3c
|
692 |
trace_mark_victim(tsk->pid); |
49550b605
|
693 694 695 |
} /** |
16e951966
|
696 |
* exit_oom_victim - note the exit of an OOM victim |
49550b605
|
697 |
*/ |
38531201c
|
698 |
void exit_oom_victim(void) |
49550b605
|
699 |
{ |
38531201c
|
700 |
clear_thread_flag(TIF_MEMDIE); |
c32b3cbe0
|
701 |
|
c38f1025f
|
702 |
if (!atomic_dec_return(&oom_victims)) |
c32b3cbe0
|
703 |
wake_up_all(&oom_victims_wait); |
c32b3cbe0
|
704 705 706 |
} /** |
7d2e7a22c
|
707 708 709 710 711 |
* oom_killer_enable - enable OOM killer */ void oom_killer_enable(void) { oom_killer_disabled = false; |
d75da004c
|
712 713 |
pr_info("OOM killer enabled. "); |
7d2e7a22c
|
714 715 716 |
} /** |
c32b3cbe0
|
717 |
* oom_killer_disable - disable OOM killer |
7d2e7a22c
|
718 |
* @timeout: maximum timeout to wait for oom victims in jiffies |
c32b3cbe0
|
719 720 |
* * Forces all page allocations to fail rather than trigger OOM killer. |
7d2e7a22c
|
721 722 |
* Will block and wait until all OOM victims are killed or the given * timeout expires. |
c32b3cbe0
|
723 724 725 726 727 728 729 730 |
* * 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. */ |
7d2e7a22c
|
731 |
bool oom_killer_disable(signed long timeout) |
c32b3cbe0
|
732 |
{ |
7d2e7a22c
|
733 |
signed long ret; |
c32b3cbe0
|
734 |
/* |
6afcf2895
|
735 736 |
* 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
|
737 |
*/ |
6afcf2895
|
738 |
if (mutex_lock_killable(&oom_lock)) |
c32b3cbe0
|
739 |
return false; |
c32b3cbe0
|
740 |
oom_killer_disabled = true; |
dc56401fc
|
741 |
mutex_unlock(&oom_lock); |
c32b3cbe0
|
742 |
|
7d2e7a22c
|
743 744 745 746 747 748 |
ret = wait_event_interruptible_timeout(oom_victims_wait, !atomic_read(&oom_victims), timeout); if (ret <= 0) { oom_killer_enable(); return false; } |
d75da004c
|
749 750 |
pr_info("OOM killer disabled. "); |
c32b3cbe0
|
751 752 753 |
return true; } |
1af8bb432
|
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 |
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
|
779 780 |
* Caller has to make sure that task->mm is stable (hold task_lock or * it operates on the current). |
1af8bb432
|
781 |
*/ |
7c5f64f84
|
782 |
static bool task_will_free_mem(struct task_struct *task) |
1af8bb432
|
783 |
{ |
091f362c5
|
784 |
struct mm_struct *mm = task->mm; |
1af8bb432
|
785 |
struct task_struct *p; |
f33e6f067
|
786 |
bool ret = true; |
1af8bb432
|
787 |
|
1af8bb432
|
788 |
/* |
091f362c5
|
789 790 791 |
* 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
|
792 |
*/ |
091f362c5
|
793 |
if (!mm) |
1af8bb432
|
794 |
return false; |
091f362c5
|
795 796 |
if (!__task_will_free_mem(task)) return false; |
696453e66
|
797 798 799 800 801 |
/* * This task has already been drained by the oom reaper so there are * only small chances it will free some more */ |
862e3073b
|
802 |
if (test_bit(MMF_OOM_SKIP, &mm->flags)) |
696453e66
|
803 |
return false; |
696453e66
|
804 |
|
091f362c5
|
805 |
if (atomic_read(&mm->mm_users) <= 1) |
1af8bb432
|
806 |
return true; |
1af8bb432
|
807 808 |
/* |
5870c2e1d
|
809 810 811 |
* Make sure that all tasks which share the mm with the given tasks * are dying as well to make sure that a) nobody pins its mm and * b) the task is also reapable by the oom reaper. |
1af8bb432
|
812 813 814 815 816 817 818 819 820 821 822 823 |
*/ 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
|
824 825 826 |
return ret; } |
bbbe48029
|
827 |
static void __oom_kill_process(struct task_struct *victim, const char *message) |
1da177e4c
|
828 |
{ |
5989ad7b5
|
829 |
struct task_struct *p; |
647f2bdf4
|
830 |
struct mm_struct *mm; |
bb29902a7
|
831 |
bool can_oom_reap = true; |
1da177e4c
|
832 |
|
6b0c81b3b
|
833 834 |
p = find_lock_task_mm(victim); if (!p) { |
6b0c81b3b
|
835 |
put_task_struct(victim); |
647f2bdf4
|
836 |
return; |
6b0c81b3b
|
837 838 839 840 841 |
} else if (victim != p) { get_task_struct(p); put_task_struct(victim); victim = p; } |
647f2bdf4
|
842 |
|
880b76893
|
843 |
/* Get a reference to safely compare mm after task_unlock(victim) */ |
647f2bdf4
|
844 |
mm = victim->mm; |
f1f100764
|
845 |
mmgrab(mm); |
8e675f7af
|
846 847 848 |
/* Raise event before sending signal: task reaper must see this */ count_vm_event(OOM_KILL); |
fe6bdfc8e
|
849 |
memcg_memory_event_mm(mm, MEMCG_OOM_KILL); |
8e675f7af
|
850 |
|
426fb5e72
|
851 |
/* |
cd04ae1e2
|
852 853 854 |
* We should send SIGKILL before granting access to memory reserves * in order to prevent the OOM victim from depleting the memory * reserves from the user space under its control. |
426fb5e72
|
855 |
*/ |
079b22dc9
|
856 |
do_send_sig_info(SIGKILL, SEND_SIG_PRIV, victim, PIDTYPE_TGID); |
16e951966
|
857 |
mark_oom_victim(victim); |
bbbe48029
|
858 859 860 861 |
pr_err("%s: Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB ", message, task_pid_nr(victim), victim->comm, K(victim->mm->total_vm), |
647f2bdf4
|
862 |
K(get_mm_counter(victim->mm, MM_ANONPAGES)), |
eca56ff90
|
863 864 |
K(get_mm_counter(victim->mm, MM_FILEPAGES)), K(get_mm_counter(victim->mm, MM_SHMEMPAGES))); |
647f2bdf4
|
865 866 867 868 869 870 871 872 873 874 875 |
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
|
876 |
rcu_read_lock(); |
c319025a6
|
877 |
for_each_process(p) { |
4d7b3394f
|
878 |
if (!process_shares_mm(p, mm)) |
c319025a6
|
879 880 881 |
continue; if (same_thread_group(p, victim)) continue; |
1b51e65ea
|
882 |
if (is_global_init(p)) { |
aac453635
|
883 |
can_oom_reap = false; |
862e3073b
|
884 |
set_bit(MMF_OOM_SKIP, &mm->flags); |
a373966d1
|
885 886 887 888 |
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
|
889 |
continue; |
aac453635
|
890 |
} |
1b51e65ea
|
891 892 893 894 895 896 |
/* * No use_mm() user needs to read from the userspace so we are * ok to reap it. */ if (unlikely(p->flags & PF_KTHREAD)) continue; |
079b22dc9
|
897 |
do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID); |
c319025a6
|
898 |
} |
6b0c81b3b
|
899 |
rcu_read_unlock(); |
647f2bdf4
|
900 |
|
aac453635
|
901 |
if (can_oom_reap) |
36324a990
|
902 |
wake_oom_reaper(victim); |
aac453635
|
903 |
|
880b76893
|
904 |
mmdrop(mm); |
6b0c81b3b
|
905 |
put_task_struct(victim); |
1da177e4c
|
906 |
} |
647f2bdf4
|
907 |
#undef K |
1da177e4c
|
908 |
|
309ed8825
|
909 |
/* |
3d8b38eb8
|
910 911 912 |
* Kill provided task unless it's secured by setting * oom_score_adj to OOM_SCORE_ADJ_MIN. */ |
bbbe48029
|
913 |
static int oom_kill_memcg_member(struct task_struct *task, void *message) |
3d8b38eb8
|
914 |
{ |
d342a0b38
|
915 916 |
if (task->signal->oom_score_adj != OOM_SCORE_ADJ_MIN && !is_global_init(task)) { |
3d8b38eb8
|
917 |
get_task_struct(task); |
bbbe48029
|
918 |
__oom_kill_process(task, message); |
3d8b38eb8
|
919 920 921 |
} return 0; } |
5989ad7b5
|
922 923 |
static void oom_kill_process(struct oom_control *oc, const char *message) { |
bbbe48029
|
924 |
struct task_struct *victim = oc->chosen; |
3d8b38eb8
|
925 |
struct mem_cgroup *oom_group; |
5989ad7b5
|
926 927 928 929 930 931 932 933 |
static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST); /* * If the task is already exiting, don't alarm the sysadmin or kill * its children or threads, just give it access to memory reserves * so it can die quickly */ |
bbbe48029
|
934 935 936 937 938 939 |
task_lock(victim); if (task_will_free_mem(victim)) { mark_oom_victim(victim); wake_oom_reaper(victim); task_unlock(victim); put_task_struct(victim); |
5989ad7b5
|
940 941 |
return; } |
bbbe48029
|
942 |
task_unlock(victim); |
5989ad7b5
|
943 944 |
if (__ratelimit(&oom_rs)) |
bbbe48029
|
945 |
dump_header(oc, victim); |
5989ad7b5
|
946 |
|
3d8b38eb8
|
947 948 949 950 951 952 |
/* * Do we need to kill the entire memory cgroup? * Or even one of the ancestor memory cgroups? * Check this out before killing the victim task. */ oom_group = mem_cgroup_get_oom_group(victim, oc->memcg); |
bbbe48029
|
953 |
__oom_kill_process(victim, message); |
3d8b38eb8
|
954 955 956 957 958 959 |
/* * If necessary, kill all tasks in the selected memory cgroup. */ if (oom_group) { mem_cgroup_print_oom_group(oom_group); |
bbbe48029
|
960 961 |
mem_cgroup_scan_tasks(oom_group, oom_kill_memcg_member, (void*)message); |
3d8b38eb8
|
962 963 |
mem_cgroup_put(oom_group); } |
5989ad7b5
|
964 |
} |
309ed8825
|
965 966 967 |
/* * Determines whether the kernel must panic because of the panic_on_oom sysctl. */ |
432b1de0d
|
968 |
static void check_panic_on_oom(struct oom_control *oc) |
309ed8825
|
969 970 971 972 973 974 975 976 977 |
{ 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. */ |
432b1de0d
|
978 |
if (oc->constraint != CONSTRAINT_NONE) |
309ed8825
|
979 980 |
return; } |
071a4befe
|
981 |
/* Do not panic for oom kills triggered by sysrq */ |
db2a0dd7a
|
982 |
if (is_sysrq_oom(oc)) |
071a4befe
|
983 |
return; |
2a966b77a
|
984 |
dump_header(oc, NULL); |
309ed8825
|
985 986 987 988 |
panic("Out of memory: %s panic_on_oom is enabled ", sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide"); } |
8bc719d3c
|
989 990 991 992 993 994 995 996 997 998 999 1000 1001 |
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
|
1002 |
/** |
6e0fc46dc
|
1003 1004 |
* out_of_memory - kill the "best" process when we run out of memory * @oc: pointer to struct oom_control |
1da177e4c
|
1005 1006 1007 1008 1009 1010 |
* * 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
|
1011 |
bool out_of_memory(struct oom_control *oc) |
1da177e4c
|
1012 |
{ |
8bc719d3c
|
1013 |
unsigned long freed = 0; |
dc56401fc
|
1014 1015 |
if (oom_killer_disabled) return false; |
7c5f64f84
|
1016 1017 1018 1019 1020 1021 |
if (!is_memcg_oom(oc)) { blocking_notifier_call_chain(&oom_notify_list, 0, &freed); if (freed > 0) /* Got some memory back in the last second. */ return true; } |
1da177e4c
|
1022 |
|
7b98c2e40
|
1023 |
/* |
9ff4868e3
|
1024 1025 1026 |
* 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
|
1027 |
*/ |
091f362c5
|
1028 |
if (task_will_free_mem(current)) { |
16e951966
|
1029 |
mark_oom_victim(current); |
1af8bb432
|
1030 |
wake_oom_reaper(current); |
75e8f8b24
|
1031 |
return true; |
7b98c2e40
|
1032 |
} |
9b0f8b040
|
1033 |
/* |
3da88fb3b
|
1034 1035 1036 1037 1038 |
* 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. */ |
06ad276ac
|
1039 |
if (oc->gfp_mask && !(oc->gfp_mask & __GFP_FS)) |
3da88fb3b
|
1040 1041 1042 |
return true; /* |
9b0f8b040
|
1043 |
* Check if there were limitations on the allocation (only relevant for |
7c5f64f84
|
1044 |
* NUMA and memcg) that may require different handling. |
9b0f8b040
|
1045 |
*/ |
432b1de0d
|
1046 1047 |
oc->constraint = constrained_alloc(oc); if (oc->constraint != CONSTRAINT_MEMORY_POLICY) |
6e0fc46dc
|
1048 |
oc->nodemask = NULL; |
432b1de0d
|
1049 |
check_panic_on_oom(oc); |
0aad4b312
|
1050 |
|
7c5f64f84
|
1051 |
if (!is_memcg_oom(oc) && sysctl_oom_kill_allocating_task && |
ac311a14c
|
1052 1053 |
current->mm && !oom_unkillable_task(current) && oom_cpuset_eligible(current, oc) && |
121d1ba0a
|
1054 |
current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) { |
6b0c81b3b
|
1055 |
get_task_struct(current); |
7c5f64f84
|
1056 1057 |
oc->chosen = current; oom_kill_process(oc, "Out of memory (oom_kill_allocating_task)"); |
75e8f8b24
|
1058 |
return true; |
0aad4b312
|
1059 |
} |
7c5f64f84
|
1060 |
select_bad_process(oc); |
3100dab2a
|
1061 1062 |
/* Found nothing?!?! */ if (!oc->chosen) { |
2a966b77a
|
1063 |
dump_header(oc, NULL); |
3100dab2a
|
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 |
pr_warn("Out of memory and no killable processes... "); /* * If we got here due to an actual allocation at the * system level, we cannot survive this and will enter * an endless loop in the allocator. Bail out now. */ if (!is_sysrq_oom(oc) && !is_memcg_oom(oc)) panic("System is deadlocked on memory "); |
0aad4b312
|
1074 |
} |
9bfe5ded0
|
1075 |
if (oc->chosen && oc->chosen != (void *)-1UL) |
7c5f64f84
|
1076 1077 |
oom_kill_process(oc, !is_memcg_oom(oc) ? "Out of memory" : "Memory cgroup out of memory"); |
7c5f64f84
|
1078 |
return !!oc->chosen; |
c32b3cbe0
|
1079 |
} |
e36589323
|
1080 1081 |
/* * The pagefault handler calls here because it is out of memory, so kill a |
798fd7569
|
1082 1083 |
* memory-hogging task. If oom_lock is held by somebody else, a parallel oom * killing is already in progress so do nothing. |
e36589323
|
1084 1085 1086 |
*/ void pagefault_out_of_memory(void) { |
6e0fc46dc
|
1087 1088 1089 |
struct oom_control oc = { .zonelist = NULL, .nodemask = NULL, |
2a966b77a
|
1090 |
.memcg = NULL, |
6e0fc46dc
|
1091 1092 |
.gfp_mask = 0, .order = 0, |
6e0fc46dc
|
1093 |
}; |
494264208
|
1094 |
if (mem_cgroup_oom_synchronize(true)) |
dc56401fc
|
1095 |
return; |
3812c8c8f
|
1096 |
|
dc56401fc
|
1097 1098 |
if (!mutex_trylock(&oom_lock)) return; |
a104808e2
|
1099 |
out_of_memory(&oc); |
dc56401fc
|
1100 |
mutex_unlock(&oom_lock); |
e36589323
|
1101 |
} |