1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/mm/oom_kill.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1998,2000 Rik van Riel
61da177e4SLinus Torvalds * Thanks go out to Claus Fischer for some serious inspiration and
71da177e4SLinus Torvalds * for goading me into coding this file...
8a63d83f4SDavid Rientjes * Copyright (C) 2010 Google, Inc.
9a63d83f4SDavid Rientjes * Rewritten by David Rientjes
101da177e4SLinus Torvalds *
111da177e4SLinus Torvalds * The routines in this file are used to kill a process when
12a49335ccSPaul Jackson * we're seriously out of memory. This gets called from __alloc_pages()
13a49335ccSPaul Jackson * in mm/page_alloc.c when we really run out of memory.
141da177e4SLinus Torvalds *
151da177e4SLinus Torvalds * Since we won't call these routines often (on a well-configured
161da177e4SLinus Torvalds * machine) this file will double as a 'coding guide' and a signpost
171da177e4SLinus Torvalds * for newbie kernel hackers. It features several pointers to major
181da177e4SLinus Torvalds * kernel subsystems and hints as to where to find out what things do.
191da177e4SLinus Torvalds */
201da177e4SLinus Torvalds
218ac773b4SAlexey Dobriyan #include <linux/oom.h>
221da177e4SLinus Torvalds #include <linux/mm.h>
234e950f6fSAlexey Dobriyan #include <linux/err.h>
245a0e3ad6STejun Heo #include <linux/gfp.h>
251da177e4SLinus Torvalds #include <linux/sched.h>
266e84f315SIngo Molnar #include <linux/sched/mm.h>
27f7ccbae4SIngo Molnar #include <linux/sched/coredump.h>
2829930025SIngo Molnar #include <linux/sched/task.h>
298a7ff02aSDavid Rientjes #include <linux/sched/debug.h>
301da177e4SLinus Torvalds #include <linux/swap.h>
31884a7e59SSuren Baghdasaryan #include <linux/syscalls.h>
321da177e4SLinus Torvalds #include <linux/timex.h>
331da177e4SLinus Torvalds #include <linux/jiffies.h>
34ef08e3b4SPaul Jackson #include <linux/cpuset.h>
35b95f1b31SPaul Gortmaker #include <linux/export.h>
368bc719d3SMartin Schwidefsky #include <linux/notifier.h>
37c7ba5c9eSPavel Emelianov #include <linux/memcontrol.h>
386f48d0ebSDavid Rientjes #include <linux/mempolicy.h>
395cd9c58fSDavid Howells #include <linux/security.h>
40edd45544SDavid Rientjes #include <linux/ptrace.h>
41f660daacSDavid Rientjes #include <linux/freezer.h>
4243d2b113SKAMEZAWA Hiroyuki #include <linux/ftrace.h>
43dc3f21eaSDavid Rientjes #include <linux/ratelimit.h>
44aac45363SMichal Hocko #include <linux/kthread.h>
45aac45363SMichal Hocko #include <linux/init.h>
464d4bbd85SMichal Hocko #include <linux/mmu_notifier.h>
470a657f6eSCarlos Galo #include <linux/cred.h>
48*972486d3SChen Ridong #include <linux/nmi.h>
49aac45363SMichal Hocko
50aac45363SMichal Hocko #include <asm/tlb.h>
51aac45363SMichal Hocko #include "internal.h"
52852d8be0SYang Shi #include "slab.h"
5343d2b113SKAMEZAWA Hiroyuki
5443d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS
5543d2b113SKAMEZAWA Hiroyuki #include <trace/events/oom.h>
561da177e4SLinus Torvalds
5743fe219aSsujiaxun static int sysctl_panic_on_oom;
5843fe219aSsujiaxun static int sysctl_oom_kill_allocating_task;
5943fe219aSsujiaxun static int sysctl_oom_dump_tasks = 1;
6043fe219aSsujiaxun
61a195d3f5SMichal Hocko /*
62a195d3f5SMichal Hocko * Serializes oom killer invocations (out_of_memory()) from all contexts to
63a195d3f5SMichal Hocko * prevent from over eager oom killing (e.g. when the oom killer is invoked
64a195d3f5SMichal Hocko * from different domains).
65a195d3f5SMichal Hocko *
66a195d3f5SMichal Hocko * oom_killer_disable() relies on this lock to stabilize oom_killer_disabled
67a195d3f5SMichal Hocko * and mark_oom_victim
68a195d3f5SMichal Hocko */
69dc56401fSJohannes Weiner DEFINE_MUTEX(oom_lock);
7067197a4fSSuren Baghdasaryan /* Serializes oom_score_adj and oom_score_adj_min updates */
7167197a4fSSuren Baghdasaryan DEFINE_MUTEX(oom_adj_mutex);
721da177e4SLinus Torvalds
is_memcg_oom(struct oom_control * oc)73ac311a14SShakeel Butt static inline bool is_memcg_oom(struct oom_control *oc)
74ac311a14SShakeel Butt {
75ac311a14SShakeel Butt return oc->memcg != NULL;
76ac311a14SShakeel Butt }
77ac311a14SShakeel Butt
786f48d0ebSDavid Rientjes #ifdef CONFIG_NUMA
796f48d0ebSDavid Rientjes /**
80f0953a1bSIngo Molnar * oom_cpuset_eligible() - check task eligibility for kill
81ad962441SOleg Nesterov * @start: task struct of which task to consider
82f364f06bSYi Wang * @oc: pointer to struct oom_control
836f48d0ebSDavid Rientjes *
846f48d0ebSDavid Rientjes * Task eligibility is determined by whether or not a candidate task, @tsk,
856f48d0ebSDavid Rientjes * shares the same mempolicy nodes as current if it is bound by such a policy
866f48d0ebSDavid Rientjes * and whether or not it has the same set of allowed cpuset nodes.
87ac311a14SShakeel Butt *
88ac311a14SShakeel Butt * This function is assuming oom-killer context and 'current' has triggered
89ac311a14SShakeel Butt * the oom-killer.
90495789a5SKOSAKI Motohiro */
oom_cpuset_eligible(struct task_struct * start,struct oom_control * oc)91ac311a14SShakeel Butt static bool oom_cpuset_eligible(struct task_struct *start,
92ac311a14SShakeel Butt struct oom_control *oc)
93495789a5SKOSAKI Motohiro {
94ad962441SOleg Nesterov struct task_struct *tsk;
95ad962441SOleg Nesterov bool ret = false;
96ac311a14SShakeel Butt const nodemask_t *mask = oc->nodemask;
97ac311a14SShakeel Butt
98ad962441SOleg Nesterov rcu_read_lock();
991da4db0cSOleg Nesterov for_each_thread(start, tsk) {
1006f48d0ebSDavid Rientjes if (mask) {
1016f48d0ebSDavid Rientjes /*
1026f48d0ebSDavid Rientjes * If this is a mempolicy constrained oom, tsk's
1036f48d0ebSDavid Rientjes * cpuset is irrelevant. Only return true if its
1046f48d0ebSDavid Rientjes * mempolicy intersects current, otherwise it may be
1056f48d0ebSDavid Rientjes * needlessly killed.
1066f48d0ebSDavid Rientjes */
107b26e517aSFeng Tang ret = mempolicy_in_oom_domain(tsk, mask);
1086f48d0ebSDavid Rientjes } else {
1096f48d0ebSDavid Rientjes /*
1106f48d0ebSDavid Rientjes * This is not a mempolicy constrained oom, so only
1116f48d0ebSDavid Rientjes * check the mems of tsk's cpuset.
1126f48d0ebSDavid Rientjes */
113ad962441SOleg Nesterov ret = cpuset_mems_allowed_intersects(current, tsk);
114495789a5SKOSAKI Motohiro }
115ad962441SOleg Nesterov if (ret)
116ad962441SOleg Nesterov break;
1171da4db0cSOleg Nesterov }
118ad962441SOleg Nesterov rcu_read_unlock();
119df1090a8SKOSAKI Motohiro
120ad962441SOleg Nesterov return ret;
1216f48d0ebSDavid Rientjes }
1226f48d0ebSDavid Rientjes #else
oom_cpuset_eligible(struct task_struct * tsk,struct oom_control * oc)123ac311a14SShakeel Butt static bool oom_cpuset_eligible(struct task_struct *tsk, struct oom_control *oc)
1246f48d0ebSDavid Rientjes {
1256f48d0ebSDavid Rientjes return true;
1266f48d0ebSDavid Rientjes }
1276f48d0ebSDavid Rientjes #endif /* CONFIG_NUMA */
128495789a5SKOSAKI Motohiro
1296f48d0ebSDavid Rientjes /*
1306f48d0ebSDavid Rientjes * The process p may have detached its own ->mm while exiting or through
131f5678e7fSChristoph Hellwig * kthread_use_mm(), but one or more of its subthreads may still have a valid
1326f48d0ebSDavid Rientjes * pointer. Return p, or any of its subthreads with a valid ->mm, with
1336f48d0ebSDavid Rientjes * task_lock() held.
1346f48d0ebSDavid Rientjes */
find_lock_task_mm(struct task_struct * p)135158e0a2dSKAMEZAWA Hiroyuki struct task_struct *find_lock_task_mm(struct task_struct *p)
136dd8e8f40SOleg Nesterov {
1371da4db0cSOleg Nesterov struct task_struct *t;
138dd8e8f40SOleg Nesterov
1394d4048beSOleg Nesterov rcu_read_lock();
1404d4048beSOleg Nesterov
1411da4db0cSOleg Nesterov for_each_thread(p, t) {
142dd8e8f40SOleg Nesterov task_lock(t);
143dd8e8f40SOleg Nesterov if (likely(t->mm))
1444d4048beSOleg Nesterov goto found;
145dd8e8f40SOleg Nesterov task_unlock(t);
1461da4db0cSOleg Nesterov }
1474d4048beSOleg Nesterov t = NULL;
1484d4048beSOleg Nesterov found:
1494d4048beSOleg Nesterov rcu_read_unlock();
150dd8e8f40SOleg Nesterov
1514d4048beSOleg Nesterov return t;
152dd8e8f40SOleg Nesterov }
153dd8e8f40SOleg Nesterov
154db2a0dd7SYaowei Bai /*
155db2a0dd7SYaowei Bai * order == -1 means the oom kill is required by sysrq, otherwise only
156db2a0dd7SYaowei Bai * for display purposes.
157db2a0dd7SYaowei Bai */
is_sysrq_oom(struct oom_control * oc)158db2a0dd7SYaowei Bai static inline bool is_sysrq_oom(struct oom_control *oc)
159db2a0dd7SYaowei Bai {
160db2a0dd7SYaowei Bai return oc->order == -1;
161db2a0dd7SYaowei Bai }
162db2a0dd7SYaowei Bai
163ab290adbSKOSAKI Motohiro /* return true if the task is not adequate as candidate victim task. */
oom_unkillable_task(struct task_struct * p)164ac311a14SShakeel Butt static bool oom_unkillable_task(struct task_struct *p)
165ab290adbSKOSAKI Motohiro {
166ab290adbSKOSAKI Motohiro if (is_global_init(p))
167ab290adbSKOSAKI Motohiro return true;
168ab290adbSKOSAKI Motohiro if (p->flags & PF_KTHREAD)
169ab290adbSKOSAKI Motohiro return true;
170ab290adbSKOSAKI Motohiro return false;
171ab290adbSKOSAKI Motohiro }
172ab290adbSKOSAKI Motohiro
173845be1cdSRandy Dunlap /*
174259b3633SHui Su * Check whether unreclaimable slab amount is greater than
175259b3633SHui Su * all user memory(LRU pages).
176259b3633SHui Su * dump_unreclaimable_slab() could help in the case that
177259b3633SHui Su * oom due to too much unreclaimable slab used by kernel.
178852d8be0SYang Shi */
should_dump_unreclaim_slab(void)179259b3633SHui Su static bool should_dump_unreclaim_slab(void)
180852d8be0SYang Shi {
181852d8be0SYang Shi unsigned long nr_lru;
182852d8be0SYang Shi
183852d8be0SYang Shi nr_lru = global_node_page_state(NR_ACTIVE_ANON) +
184852d8be0SYang Shi global_node_page_state(NR_INACTIVE_ANON) +
185852d8be0SYang Shi global_node_page_state(NR_ACTIVE_FILE) +
186852d8be0SYang Shi global_node_page_state(NR_INACTIVE_FILE) +
187852d8be0SYang Shi global_node_page_state(NR_ISOLATED_ANON) +
188852d8be0SYang Shi global_node_page_state(NR_ISOLATED_FILE) +
189852d8be0SYang Shi global_node_page_state(NR_UNEVICTABLE);
190852d8be0SYang Shi
191d42f3245SRoman Gushchin return (global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B) > nr_lru);
192852d8be0SYang Shi }
193852d8be0SYang Shi
1941da177e4SLinus Torvalds /**
195a63d83f4SDavid Rientjes * oom_badness - heuristic function to determine which candidate task to kill
1961da177e4SLinus Torvalds * @p: task struct of which task we should calculate
197a63d83f4SDavid Rientjes * @totalpages: total present RAM allowed for page allocation
1981da177e4SLinus Torvalds *
199a63d83f4SDavid Rientjes * The heuristic for determining which task to kill is made to be as simple and
200a63d83f4SDavid Rientjes * predictable as possible. The goal is to return the highest value for the
201a63d83f4SDavid Rientjes * task consuming the most memory to avoid subsequent oom failures.
2021da177e4SLinus Torvalds */
oom_badness(struct task_struct * p,unsigned long totalpages)2039066e5cfSYafang Shao long oom_badness(struct task_struct *p, unsigned long totalpages)
2041da177e4SLinus Torvalds {
2051e11ad8dSDavid Rientjes long points;
20661eafb00SDavid Rientjes long adj;
20728b83c51SKOSAKI Motohiro
208ac311a14SShakeel Butt if (oom_unkillable_task(p))
2099066e5cfSYafang Shao return LONG_MIN;
2101da177e4SLinus Torvalds
211dd8e8f40SOleg Nesterov p = find_lock_task_mm(p);
212dd8e8f40SOleg Nesterov if (!p)
2139066e5cfSYafang Shao return LONG_MIN;
2141da177e4SLinus Torvalds
215bb8a4b7fSMichal Hocko /*
216bb8a4b7fSMichal Hocko * Do not even consider tasks which are explicitly marked oom
217b18dc5f2SMichal Hocko * unkillable or have been already oom reaped or the are in
218b18dc5f2SMichal Hocko * the middle of vfork
219bb8a4b7fSMichal Hocko */
220a9c58b90SDavid Rientjes adj = (long)p->signal->oom_score_adj;
221bb8a4b7fSMichal Hocko if (adj == OOM_SCORE_ADJ_MIN ||
222862e3073SMichal Hocko test_bit(MMF_OOM_SKIP, &p->mm->flags) ||
223b18dc5f2SMichal Hocko in_vfork(p)) {
2245aecc85aSMichal Hocko task_unlock(p);
2259066e5cfSYafang Shao return LONG_MIN;
2265aecc85aSMichal Hocko }
2275aecc85aSMichal Hocko
2281da177e4SLinus Torvalds /*
229a63d83f4SDavid Rientjes * The baseline for the badness score is the proportion of RAM that each
230f755a042SKOSAKI Motohiro * task's rss, pagetable and swap space use.
231a63d83f4SDavid Rientjes */
232dc6c9a35SKirill A. Shutemov points = get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) +
233af5b0f6aSKirill A. Shutemov mm_pgtables_bytes(p->mm) / PAGE_SIZE;
23497c2c9b8SAndrew Morton task_unlock(p);
2351da177e4SLinus Torvalds
23661eafb00SDavid Rientjes /* Normalize to oom_score_adj units */
23761eafb00SDavid Rientjes adj *= totalpages / 1000;
23861eafb00SDavid Rientjes points += adj;
2391da177e4SLinus Torvalds
2409066e5cfSYafang Shao return points;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds
243ef8444eaSyuzhoujian static const char * const oom_constraint_text[] = {
244ef8444eaSyuzhoujian [CONSTRAINT_NONE] = "CONSTRAINT_NONE",
245ef8444eaSyuzhoujian [CONSTRAINT_CPUSET] = "CONSTRAINT_CPUSET",
246ef8444eaSyuzhoujian [CONSTRAINT_MEMORY_POLICY] = "CONSTRAINT_MEMORY_POLICY",
247ef8444eaSyuzhoujian [CONSTRAINT_MEMCG] = "CONSTRAINT_MEMCG",
2487c5f64f8SVladimir Davydov };
2497c5f64f8SVladimir Davydov
2501da177e4SLinus Torvalds /*
2519b0f8b04SChristoph Lameter * Determine the type of allocation constraint.
2529b0f8b04SChristoph Lameter */
constrained_alloc(struct oom_control * oc)2537c5f64f8SVladimir Davydov static enum oom_constraint constrained_alloc(struct oom_control *oc)
2544365a567SKAMEZAWA Hiroyuki {
25554a6eb5cSMel Gorman struct zone *zone;
256dd1a239fSMel Gorman struct zoneref *z;
25797a225e6SJoonsoo Kim enum zone_type highest_zoneidx = gfp_zone(oc->gfp_mask);
258a63d83f4SDavid Rientjes bool cpuset_limited = false;
259a63d83f4SDavid Rientjes int nid;
2609b0f8b04SChristoph Lameter
2617c5f64f8SVladimir Davydov if (is_memcg_oom(oc)) {
262bbec2e15SRoman Gushchin oc->totalpages = mem_cgroup_get_max(oc->memcg) ?: 1;
2637c5f64f8SVladimir Davydov return CONSTRAINT_MEMCG;
2647c5f64f8SVladimir Davydov }
2657c5f64f8SVladimir Davydov
266a63d83f4SDavid Rientjes /* Default to all available memory */
267ca79b0c2SArun KS oc->totalpages = totalram_pages() + total_swap_pages;
2687c5f64f8SVladimir Davydov
2697c5f64f8SVladimir Davydov if (!IS_ENABLED(CONFIG_NUMA))
2707c5f64f8SVladimir Davydov return CONSTRAINT_NONE;
271a63d83f4SDavid Rientjes
2726e0fc46dSDavid Rientjes if (!oc->zonelist)
273a63d83f4SDavid Rientjes return CONSTRAINT_NONE;
2744365a567SKAMEZAWA Hiroyuki /*
2754365a567SKAMEZAWA Hiroyuki * Reach here only when __GFP_NOFAIL is used. So, we should avoid
2764365a567SKAMEZAWA Hiroyuki * to kill current.We have to random task kill in this case.
2774365a567SKAMEZAWA Hiroyuki * Hopefully, CONSTRAINT_THISNODE...but no way to handle it, now.
2784365a567SKAMEZAWA Hiroyuki */
2796e0fc46dSDavid Rientjes if (oc->gfp_mask & __GFP_THISNODE)
2804365a567SKAMEZAWA Hiroyuki return CONSTRAINT_NONE;
2819b0f8b04SChristoph Lameter
2824365a567SKAMEZAWA Hiroyuki /*
283a63d83f4SDavid Rientjes * This is not a __GFP_THISNODE allocation, so a truncated nodemask in
284a63d83f4SDavid Rientjes * the page allocator means a mempolicy is in effect. Cpuset policy
285a63d83f4SDavid Rientjes * is enforced in get_page_from_freelist().
2864365a567SKAMEZAWA Hiroyuki */
2876e0fc46dSDavid Rientjes if (oc->nodemask &&
2886e0fc46dSDavid Rientjes !nodes_subset(node_states[N_MEMORY], *oc->nodemask)) {
2897c5f64f8SVladimir Davydov oc->totalpages = total_swap_pages;
2906e0fc46dSDavid Rientjes for_each_node_mask(nid, *oc->nodemask)
2911eb41bb0SMichal Hocko oc->totalpages += node_present_pages(nid);
2929b0f8b04SChristoph Lameter return CONSTRAINT_MEMORY_POLICY;
293a63d83f4SDavid Rientjes }
2944365a567SKAMEZAWA Hiroyuki
2954365a567SKAMEZAWA Hiroyuki /* Check this allocation failure is caused by cpuset's wall function */
2966e0fc46dSDavid Rientjes for_each_zone_zonelist_nodemask(zone, z, oc->zonelist,
29797a225e6SJoonsoo Kim highest_zoneidx, oc->nodemask)
2986e0fc46dSDavid Rientjes if (!cpuset_zone_allowed(zone, oc->gfp_mask))
299a63d83f4SDavid Rientjes cpuset_limited = true;
3009b0f8b04SChristoph Lameter
301a63d83f4SDavid Rientjes if (cpuset_limited) {
3027c5f64f8SVladimir Davydov oc->totalpages = total_swap_pages;
303a63d83f4SDavid Rientjes for_each_node_mask(nid, cpuset_current_mems_allowed)
3041eb41bb0SMichal Hocko oc->totalpages += node_present_pages(nid);
305a63d83f4SDavid Rientjes return CONSTRAINT_CPUSET;
306a63d83f4SDavid Rientjes }
3079b0f8b04SChristoph Lameter return CONSTRAINT_NONE;
3089b0f8b04SChristoph Lameter }
3099b0f8b04SChristoph Lameter
oom_evaluate_task(struct task_struct * task,void * arg)3107c5f64f8SVladimir Davydov static int oom_evaluate_task(struct task_struct *task, void *arg)
311462607ecSDavid Rientjes {
3127c5f64f8SVladimir Davydov struct oom_control *oc = arg;
3139066e5cfSYafang Shao long points;
3147c5f64f8SVladimir Davydov
315ac311a14SShakeel Butt if (oom_unkillable_task(task))
316ac311a14SShakeel Butt goto next;
317ac311a14SShakeel Butt
318ac311a14SShakeel Butt /* p may not have freeable memory in nodemask */
319ac311a14SShakeel Butt if (!is_memcg_oom(oc) && !oom_cpuset_eligible(task, oc))
3207c5f64f8SVladimir Davydov goto next;
321462607ecSDavid Rientjes
322462607ecSDavid Rientjes /*
323462607ecSDavid Rientjes * This task already has access to memory reserves and is being killed.
324a373966dSMichal Hocko * Don't allow any other task to have access to the reserves unless
325862e3073SMichal Hocko * the task has MMF_OOM_SKIP because chances that it would release
326a373966dSMichal Hocko * any memory is quite low.
327462607ecSDavid Rientjes */
328862e3073SMichal Hocko if (!is_sysrq_oom(oc) && tsk_is_oom_victim(task)) {
329862e3073SMichal Hocko if (test_bit(MMF_OOM_SKIP, &task->signal->oom_mm->flags))
3307c5f64f8SVladimir Davydov goto next;
3317c5f64f8SVladimir Davydov goto abort;
332a373966dSMichal Hocko }
333462607ecSDavid Rientjes
334e1e12d2fSDavid Rientjes /*
335e1e12d2fSDavid Rientjes * If task is allocating a lot of memory and has been marked to be
336e1e12d2fSDavid Rientjes * killed first if it triggers an oom, then select it.
337e1e12d2fSDavid Rientjes */
3387c5f64f8SVladimir Davydov if (oom_task_origin(task)) {
3399066e5cfSYafang Shao points = LONG_MAX;
3407c5f64f8SVladimir Davydov goto select;
3417c5f64f8SVladimir Davydov }
342e1e12d2fSDavid Rientjes
343ac311a14SShakeel Butt points = oom_badness(task, oc->totalpages);
3449066e5cfSYafang Shao if (points == LONG_MIN || points < oc->chosen_points)
3457c5f64f8SVladimir Davydov goto next;
3467c5f64f8SVladimir Davydov
3477c5f64f8SVladimir Davydov select:
3487c5f64f8SVladimir Davydov if (oc->chosen)
3497c5f64f8SVladimir Davydov put_task_struct(oc->chosen);
3507c5f64f8SVladimir Davydov get_task_struct(task);
3517c5f64f8SVladimir Davydov oc->chosen = task;
3527c5f64f8SVladimir Davydov oc->chosen_points = points;
3537c5f64f8SVladimir Davydov next:
3547c5f64f8SVladimir Davydov return 0;
3557c5f64f8SVladimir Davydov abort:
3567c5f64f8SVladimir Davydov if (oc->chosen)
3577c5f64f8SVladimir Davydov put_task_struct(oc->chosen);
3587c5f64f8SVladimir Davydov oc->chosen = (void *)-1UL;
3597c5f64f8SVladimir Davydov return 1;
360462607ecSDavid Rientjes }
361462607ecSDavid Rientjes
3629b0f8b04SChristoph Lameter /*
3637c5f64f8SVladimir Davydov * Simple selection loop. We choose the process with the highest number of
3647c5f64f8SVladimir Davydov * 'points'. In case scan was aborted, oc->chosen is set to -1.
3651da177e4SLinus Torvalds */
select_bad_process(struct oom_control * oc)3667c5f64f8SVladimir Davydov static void select_bad_process(struct oom_control *oc)
3671da177e4SLinus Torvalds {
3689066e5cfSYafang Shao oc->chosen_points = LONG_MIN;
3699066e5cfSYafang Shao
3707c5f64f8SVladimir Davydov if (is_memcg_oom(oc))
3717c5f64f8SVladimir Davydov mem_cgroup_scan_tasks(oc->memcg, oom_evaluate_task, oc);
3727c5f64f8SVladimir Davydov else {
373f44666b0STetsuo Handa struct task_struct *p;
3741da177e4SLinus Torvalds
3756b0c81b3SDavid Rientjes rcu_read_lock();
3767c5f64f8SVladimir Davydov for_each_process(p)
3777c5f64f8SVladimir Davydov if (oom_evaluate_task(p, oc))
378462607ecSDavid Rientjes break;
3796b0c81b3SDavid Rientjes rcu_read_unlock();
3807c5f64f8SVladimir Davydov }
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds
dump_task(struct task_struct * p,void * arg)3835eee7e1cSShakeel Butt static int dump_task(struct task_struct *p, void *arg)
384fef1bdd6SDavid Rientjes {
3855eee7e1cSShakeel Butt struct oom_control *oc = arg;
386c55db957SKOSAKI Motohiro struct task_struct *task;
387fef1bdd6SDavid Rientjes
388ac311a14SShakeel Butt if (oom_unkillable_task(p))
389ac311a14SShakeel Butt return 0;
390ac311a14SShakeel Butt
391ac311a14SShakeel Butt /* p may not have freeable memory in nodemask */
392ac311a14SShakeel Butt if (!is_memcg_oom(oc) && !oom_cpuset_eligible(p, oc))
3935eee7e1cSShakeel Butt return 0;
394fef1bdd6SDavid Rientjes
395c55db957SKOSAKI Motohiro task = find_lock_task_mm(p);
396c55db957SKOSAKI Motohiro if (!task) {
3976d2661edSDavid Rientjes /*
398f8159c13STang Yizhou * All of p's threads have already detached their mm's. There's
399f8159c13STang Yizhou * no need to report them; they can't be oom killed anyway.
4006d2661edSDavid Rientjes */
4015eee7e1cSShakeel Butt return 0;
4026d2661edSDavid Rientjes }
403c55db957SKOSAKI Motohiro
404c3b78b11SRodrigo Freire pr_info("[%7d] %5d %5d %8lu %8lu %8ld %8lu %5hd %s\n",
405078de5f7SEric W. Biederman task->pid, from_kuid(&init_user_ns, task_uid(task)),
406078de5f7SEric W. Biederman task->tgid, task->mm->total_vm, get_mm_rss(task->mm),
407af5b0f6aSKirill A. Shutemov mm_pgtables_bytes(task->mm),
408de34d965SDavid Rientjes get_mm_counter(task->mm, MM_SWAPENTS),
409a63d83f4SDavid Rientjes task->signal->oom_score_adj, task->comm);
410c55db957SKOSAKI Motohiro task_unlock(task);
4115eee7e1cSShakeel Butt
4125eee7e1cSShakeel Butt return 0;
413c55db957SKOSAKI Motohiro }
4145eee7e1cSShakeel Butt
4155eee7e1cSShakeel Butt /**
4165eee7e1cSShakeel Butt * dump_tasks - dump current memory state of all system tasks
4175eee7e1cSShakeel Butt * @oc: pointer to struct oom_control
4185eee7e1cSShakeel Butt *
4195eee7e1cSShakeel Butt * Dumps the current memory state of all eligible tasks. Tasks not in the same
4205eee7e1cSShakeel Butt * memcg, not in the same cpuset, or bound to a disjoint set of mempolicy nodes
4215eee7e1cSShakeel Butt * are not shown.
4225eee7e1cSShakeel Butt * State information includes task's pid, uid, tgid, vm size, rss,
4235eee7e1cSShakeel Butt * pgtables_bytes, swapents, oom_score_adj value, and name.
4245eee7e1cSShakeel Butt */
dump_tasks(struct oom_control * oc)4255eee7e1cSShakeel Butt static void dump_tasks(struct oom_control *oc)
4265eee7e1cSShakeel Butt {
4275eee7e1cSShakeel Butt pr_info("Tasks state (memory values in pages):\n");
4285eee7e1cSShakeel Butt pr_info("[ pid ] uid tgid total_vm rss pgtables_bytes swapents oom_score_adj name\n");
4295eee7e1cSShakeel Butt
4305eee7e1cSShakeel Butt if (is_memcg_oom(oc))
4315eee7e1cSShakeel Butt mem_cgroup_scan_tasks(oc->memcg, dump_task, oc);
4325eee7e1cSShakeel Butt else {
4335eee7e1cSShakeel Butt struct task_struct *p;
434*972486d3SChen Ridong int i = 0;
4355eee7e1cSShakeel Butt
4365eee7e1cSShakeel Butt rcu_read_lock();
437*972486d3SChen Ridong for_each_process(p) {
438*972486d3SChen Ridong /* Avoid potential softlockup warning */
439*972486d3SChen Ridong if ((++i & 1023) == 0)
440*972486d3SChen Ridong touch_softlockup_watchdog();
4415eee7e1cSShakeel Butt dump_task(p, oc);
442*972486d3SChen Ridong }
4436b0c81b3SDavid Rientjes rcu_read_unlock();
444fef1bdd6SDavid Rientjes }
4455eee7e1cSShakeel Butt }
446fef1bdd6SDavid Rientjes
dump_oom_summary(struct oom_control * oc,struct task_struct * victim)447ef8444eaSyuzhoujian static void dump_oom_summary(struct oom_control *oc, struct task_struct *victim)
448ef8444eaSyuzhoujian {
449ef8444eaSyuzhoujian /* one line summary of the oom killer context. */
450ef8444eaSyuzhoujian pr_info("oom-kill:constraint=%s,nodemask=%*pbl",
451ef8444eaSyuzhoujian oom_constraint_text[oc->constraint],
452ef8444eaSyuzhoujian nodemask_pr_args(oc->nodemask));
453ef8444eaSyuzhoujian cpuset_print_current_mems_allowed();
454f0c867d9Syuzhoujian mem_cgroup_print_oom_context(oc->memcg, victim);
455ef8444eaSyuzhoujian pr_cont(",task=%s,pid=%d,uid=%d\n", victim->comm, victim->pid,
456ef8444eaSyuzhoujian from_kuid(&init_user_ns, task_uid(victim)));
457ef8444eaSyuzhoujian }
458ef8444eaSyuzhoujian
dump_header(struct oom_control * oc,struct task_struct * p)4592a966b77SVladimir Davydov static void dump_header(struct oom_control *oc, struct task_struct *p)
4601b604d75SDavid Rientjes {
461ef8444eaSyuzhoujian pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd\n",
462ef8444eaSyuzhoujian current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order,
4630205f755SMichal Hocko current->signal->oom_score_adj);
4649254990fSMichal Hocko if (!IS_ENABLED(CONFIG_COMPACTION) && oc->order)
4659254990fSMichal Hocko pr_warn("COMPACTION is disabled!!!\n");
466a0795cd4SVlastimil Babka
4671b604d75SDavid Rientjes dump_stack();
468852d8be0SYang Shi if (is_memcg_oom(oc))
469f0c867d9Syuzhoujian mem_cgroup_print_oom_meminfo(oc->memcg);
470852d8be0SYang Shi else {
471974f4367SMichal Hocko __show_mem(SHOW_MEM_FILTER_NODES, oc->nodemask, gfp_zone(oc->gfp_mask));
472259b3633SHui Su if (should_dump_unreclaim_slab())
473852d8be0SYang Shi dump_unreclaimable_slab();
474852d8be0SYang Shi }
4751b604d75SDavid Rientjes if (sysctl_oom_dump_tasks)
4765eee7e1cSShakeel Butt dump_tasks(oc);
477ef8444eaSyuzhoujian if (p)
478ef8444eaSyuzhoujian dump_oom_summary(oc, p);
4791b604d75SDavid Rientjes }
4801b604d75SDavid Rientjes
4815695be14SMichal Hocko /*
482c32b3cbeSMichal Hocko * Number of OOM victims in flight
4835695be14SMichal Hocko */
484c32b3cbeSMichal Hocko static atomic_t oom_victims = ATOMIC_INIT(0);
485c32b3cbeSMichal Hocko static DECLARE_WAIT_QUEUE_HEAD(oom_victims_wait);
4865695be14SMichal Hocko
4877c5f64f8SVladimir Davydov static bool oom_killer_disabled __read_mostly;
4885695be14SMichal Hocko
4893ef22dffSMichal Hocko /*
4903ef22dffSMichal Hocko * task->mm can be NULL if the task is the exited group leader. So to
4913ef22dffSMichal Hocko * determine whether the task is using a particular mm, we examine all the
4923ef22dffSMichal Hocko * task's threads: if one of those is using this mm then this task was also
4933ef22dffSMichal Hocko * using it.
4943ef22dffSMichal Hocko */
process_shares_mm(struct task_struct * p,struct mm_struct * mm)49544a70adeSMichal Hocko bool process_shares_mm(struct task_struct *p, struct mm_struct *mm)
4963ef22dffSMichal Hocko {
4973ef22dffSMichal Hocko struct task_struct *t;
4983ef22dffSMichal Hocko
4993ef22dffSMichal Hocko for_each_thread(p, t) {
5003ef22dffSMichal Hocko struct mm_struct *t_mm = READ_ONCE(t->mm);
5013ef22dffSMichal Hocko if (t_mm)
5023ef22dffSMichal Hocko return t_mm == mm;
5033ef22dffSMichal Hocko }
5043ef22dffSMichal Hocko return false;
5053ef22dffSMichal Hocko }
5063ef22dffSMichal Hocko
507aac45363SMichal Hocko #ifdef CONFIG_MMU
508aac45363SMichal Hocko /*
509aac45363SMichal Hocko * OOM Reaper kernel thread which tries to reap the memory used by the OOM
510aac45363SMichal Hocko * victim (if that is possible) to help the OOM killer to move on.
511aac45363SMichal Hocko */
512aac45363SMichal Hocko static struct task_struct *oom_reaper_th;
513aac45363SMichal Hocko static DECLARE_WAIT_QUEUE_HEAD(oom_reaper_wait);
51429c696e1SVladimir Davydov static struct task_struct *oom_reaper_list;
51503049269SMichal Hocko static DEFINE_SPINLOCK(oom_reaper_lock);
51603049269SMichal Hocko
__oom_reap_task_mm(struct mm_struct * mm)517bf3980c8SSuren Baghdasaryan static bool __oom_reap_task_mm(struct mm_struct *mm)
518aac45363SMichal Hocko {
519aac45363SMichal Hocko struct vm_area_struct *vma;
52093065ac7SMichal Hocko bool ret = true;
521e1c2c775SLiam R. Howlett VMA_ITERATOR(vmi, mm, 0);
52227ae357fSDavid Rientjes
52327ae357fSDavid Rientjes /*
52427ae357fSDavid Rientjes * Tell all users of get_user/copy_from_user etc... that the content
52527ae357fSDavid Rientjes * is no longer stable. No barriers really needed because unmapping
52627ae357fSDavid Rientjes * should imply barriers already and the reader would hit a page fault
52727ae357fSDavid Rientjes * if it stumbled over a reaped memory.
52827ae357fSDavid Rientjes */
52927ae357fSDavid Rientjes set_bit(MMF_UNSTABLE, &mm->flags);
53027ae357fSDavid Rientjes
531e1c2c775SLiam R. Howlett for_each_vma(vmi, vma) {
532a213e5cfSHugh Dickins if (vma->vm_flags & (VM_HUGETLB|VM_PFNMAP))
53327ae357fSDavid Rientjes continue;
53427ae357fSDavid Rientjes
53527ae357fSDavid Rientjes /*
53627ae357fSDavid Rientjes * Only anonymous pages have a good chance to be dropped
53727ae357fSDavid Rientjes * without additional steps which we cannot afford as we
53827ae357fSDavid Rientjes * are OOM already.
53927ae357fSDavid Rientjes *
54027ae357fSDavid Rientjes * We do not even care about fs backed pages because all
54127ae357fSDavid Rientjes * which are reclaimable have already been reclaimed and
54227ae357fSDavid Rientjes * we do not want to block exit_mmap by keeping mm ref
54327ae357fSDavid Rientjes * count elevated without a good reason.
54427ae357fSDavid Rientjes */
54527ae357fSDavid Rientjes if (vma_is_anonymous(vma) || !(vma->vm_flags & VM_SHARED)) {
546ac46d4f3SJérôme Glisse struct mmu_notifier_range range;
54727ae357fSDavid Rientjes struct mmu_gather tlb;
54827ae357fSDavid Rientjes
5496f4f13e8SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0,
5507d4a8be0SAlistair Popple mm, vma->vm_start,
551ac46d4f3SJérôme Glisse vma->vm_end);
552a72afd87SWill Deacon tlb_gather_mmu(&tlb, mm);
553ac46d4f3SJérôme Glisse if (mmu_notifier_invalidate_range_start_nonblock(&range)) {
554ae8eba8bSWill Deacon tlb_finish_mmu(&tlb);
55593065ac7SMichal Hocko ret = false;
55693065ac7SMichal Hocko continue;
55793065ac7SMichal Hocko }
558ac46d4f3SJérôme Glisse unmap_page_range(&tlb, vma, range.start, range.end, NULL);
559ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range);
560ae8eba8bSWill Deacon tlb_finish_mmu(&tlb);
56127ae357fSDavid Rientjes }
56227ae357fSDavid Rientjes }
56393065ac7SMichal Hocko
56493065ac7SMichal Hocko return ret;
56527ae357fSDavid Rientjes }
56627ae357fSDavid Rientjes
567431f42fdSMichal Hocko /*
568431f42fdSMichal Hocko * Reaps the address space of the give task.
569431f42fdSMichal Hocko *
570431f42fdSMichal Hocko * Returns true on success and false if none or part of the address space
571431f42fdSMichal Hocko * has been reclaimed and the caller should retry later.
572431f42fdSMichal Hocko */
oom_reap_task_mm(struct task_struct * tsk,struct mm_struct * mm)57327ae357fSDavid Rientjes static bool oom_reap_task_mm(struct task_struct *tsk, struct mm_struct *mm)
57427ae357fSDavid Rientjes {
575aac45363SMichal Hocko bool ret = true;
576aac45363SMichal Hocko
577d8ed45c5SMichel Lespinasse if (!mmap_read_trylock(mm)) {
578422580c3SRoman Gushchin trace_skip_task_reaping(tsk->pid);
579af5679fbSMichal Hocko return false;
5804d4bbd85SMichal Hocko }
5814d4bbd85SMichal Hocko
5824d4bbd85SMichal Hocko /*
58321292580SAndrea Arcangeli * MMF_OOM_SKIP is set by exit_mmap when the OOM reaper can't
58421292580SAndrea Arcangeli * work on the mm anymore. The check for MMF_OOM_SKIP must run
5853e4e28c5SMichel Lespinasse * under mmap_lock for reading because it serializes against the
5863e4e28c5SMichel Lespinasse * mmap_write_lock();mmap_write_unlock() cycle in exit_mmap().
587e5e3f4c4SMichal Hocko */
58821292580SAndrea Arcangeli if (test_bit(MMF_OOM_SKIP, &mm->flags)) {
589422580c3SRoman Gushchin trace_skip_task_reaping(tsk->pid);
590431f42fdSMichal Hocko goto out_unlock;
591aac45363SMichal Hocko }
592aac45363SMichal Hocko
593422580c3SRoman Gushchin trace_start_task_reaping(tsk->pid);
594422580c3SRoman Gushchin
59593065ac7SMichal Hocko /* failed to reap part of the address space. Try again later */
596431f42fdSMichal Hocko ret = __oom_reap_task_mm(mm);
597431f42fdSMichal Hocko if (!ret)
598431f42fdSMichal Hocko goto out_finish;
5993f70dc38SMichal Hocko
600bc448e89SMichal Hocko pr_info("oom_reaper: reaped process %d (%s), now anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB\n",
601bc448e89SMichal Hocko task_pid_nr(tsk), tsk->comm,
602bc448e89SMichal Hocko K(get_mm_counter(mm, MM_ANONPAGES)),
603bc448e89SMichal Hocko K(get_mm_counter(mm, MM_FILEPAGES)),
604bc448e89SMichal Hocko K(get_mm_counter(mm, MM_SHMEMPAGES)));
605431f42fdSMichal Hocko out_finish:
606431f42fdSMichal Hocko trace_finish_task_reaping(tsk->pid);
607431f42fdSMichal Hocko out_unlock:
608d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
60936324a99SMichal Hocko
610aac45363SMichal Hocko return ret;
611aac45363SMichal Hocko }
612aac45363SMichal Hocko
613bc448e89SMichal Hocko #define MAX_OOM_REAP_RETRIES 10
oom_reap_task(struct task_struct * tsk)61436324a99SMichal Hocko static void oom_reap_task(struct task_struct *tsk)
615aac45363SMichal Hocko {
616aac45363SMichal Hocko int attempts = 0;
61726db62f1SMichal Hocko struct mm_struct *mm = tsk->signal->oom_mm;
618aac45363SMichal Hocko
6193e4e28c5SMichel Lespinasse /* Retry the mmap_read_trylock(mm) a few times */
62027ae357fSDavid Rientjes while (attempts++ < MAX_OOM_REAP_RETRIES && !oom_reap_task_mm(tsk, mm))
621aac45363SMichal Hocko schedule_timeout_idle(HZ/10);
622aac45363SMichal Hocko
62397b1255cSTetsuo Handa if (attempts <= MAX_OOM_REAP_RETRIES ||
62497b1255cSTetsuo Handa test_bit(MMF_OOM_SKIP, &mm->flags))
6257ebffa45STetsuo Handa goto done;
62611a410d5SMichal Hocko
627bc448e89SMichal Hocko pr_info("oom_reaper: unable to reap pid:%d (%s)\n",
628bc448e89SMichal Hocko task_pid_nr(tsk), tsk->comm);
6298a7ff02aSDavid Rientjes sched_show_task(tsk);
630bc448e89SMichal Hocko debug_show_all_locks();
631bc448e89SMichal Hocko
6327ebffa45STetsuo Handa done:
633449d777dSMichal Hocko tsk->oom_reaper_list = NULL;
634449d777dSMichal Hocko
63526db62f1SMichal Hocko /*
63626db62f1SMichal Hocko * Hide this mm from OOM killer because it has been either reaped or
6373e4e28c5SMichel Lespinasse * somebody can't call mmap_write_unlock(mm).
63826db62f1SMichal Hocko */
639862e3073SMichal Hocko set_bit(MMF_OOM_SKIP, &mm->flags);
64026db62f1SMichal Hocko
641e4a38402SNico Pache /* Drop a reference taken by queue_oom_reaper */
64236324a99SMichal Hocko put_task_struct(tsk);
643aac45363SMichal Hocko }
644aac45363SMichal Hocko
oom_reaper(void * unused)645aac45363SMichal Hocko static int oom_reaper(void *unused)
646aac45363SMichal Hocko {
6473723929eSSultan Alsawaf set_freezable();
6483723929eSSultan Alsawaf
649aac45363SMichal Hocko while (true) {
65003049269SMichal Hocko struct task_struct *tsk = NULL;
651aac45363SMichal Hocko
65229c696e1SVladimir Davydov wait_event_freezable(oom_reaper_wait, oom_reaper_list != NULL);
653e4a38402SNico Pache spin_lock_irq(&oom_reaper_lock);
65429c696e1SVladimir Davydov if (oom_reaper_list != NULL) {
65529c696e1SVladimir Davydov tsk = oom_reaper_list;
65629c696e1SVladimir Davydov oom_reaper_list = tsk->oom_reaper_list;
65703049269SMichal Hocko }
658e4a38402SNico Pache spin_unlock_irq(&oom_reaper_lock);
65903049269SMichal Hocko
66003049269SMichal Hocko if (tsk)
66136324a99SMichal Hocko oom_reap_task(tsk);
662aac45363SMichal Hocko }
663aac45363SMichal Hocko
664aac45363SMichal Hocko return 0;
665aac45363SMichal Hocko }
666aac45363SMichal Hocko
wake_oom_reaper(struct timer_list * timer)667e4a38402SNico Pache static void wake_oom_reaper(struct timer_list *timer)
668e4a38402SNico Pache {
669e4a38402SNico Pache struct task_struct *tsk = container_of(timer, struct task_struct,
670e4a38402SNico Pache oom_reaper_timer);
671e4a38402SNico Pache struct mm_struct *mm = tsk->signal->oom_mm;
672e4a38402SNico Pache unsigned long flags;
673e4a38402SNico Pache
674e4a38402SNico Pache /* The victim managed to terminate on its own - see exit_mmap */
675e4a38402SNico Pache if (test_bit(MMF_OOM_SKIP, &mm->flags)) {
676e4a38402SNico Pache put_task_struct(tsk);
677e4a38402SNico Pache return;
678e4a38402SNico Pache }
679e4a38402SNico Pache
680e4a38402SNico Pache spin_lock_irqsave(&oom_reaper_lock, flags);
681e4a38402SNico Pache tsk->oom_reaper_list = oom_reaper_list;
682e4a38402SNico Pache oom_reaper_list = tsk;
683e4a38402SNico Pache spin_unlock_irqrestore(&oom_reaper_lock, flags);
684e4a38402SNico Pache trace_wake_reaper(tsk->pid);
685e4a38402SNico Pache wake_up(&oom_reaper_wait);
686e4a38402SNico Pache }
687e4a38402SNico Pache
688e4a38402SNico Pache /*
689e4a38402SNico Pache * Give the OOM victim time to exit naturally before invoking the oom_reaping.
690e4a38402SNico Pache * The timers timeout is arbitrary... the longer it is, the longer the worst
691e4a38402SNico Pache * case scenario for the OOM can take. If it is too small, the oom_reaper can
692e4a38402SNico Pache * get in the way and release resources needed by the process exit path.
693e4a38402SNico Pache * e.g. The futex robust list can sit in Anon|Private memory that gets reaped
694e4a38402SNico Pache * before the exit path is able to wake the futex waiters.
695e4a38402SNico Pache */
696e4a38402SNico Pache #define OOM_REAPER_DELAY (2*HZ)
queue_oom_reaper(struct task_struct * tsk)697e4a38402SNico Pache static void queue_oom_reaper(struct task_struct *tsk)
698aac45363SMichal Hocko {
6999bcdeb51STetsuo Handa /* mm is already queued? */
7009bcdeb51STetsuo Handa if (test_and_set_bit(MMF_OOM_REAP_QUEUED, &tsk->signal->oom_mm->flags))
701aac45363SMichal Hocko return;
702aac45363SMichal Hocko
70336324a99SMichal Hocko get_task_struct(tsk);
704e4a38402SNico Pache timer_setup(&tsk->oom_reaper_timer, wake_oom_reaper, 0);
705e4a38402SNico Pache tsk->oom_reaper_timer.expires = jiffies + OOM_REAPER_DELAY;
706e4a38402SNico Pache add_timer(&tsk->oom_reaper_timer);
707aac45363SMichal Hocko }
708aac45363SMichal Hocko
709a19cad06SAndrew Morton #ifdef CONFIG_SYSCTL
710a19cad06SAndrew Morton static struct ctl_table vm_oom_kill_table[] = {
711a19cad06SAndrew Morton {
712a19cad06SAndrew Morton .procname = "panic_on_oom",
713a19cad06SAndrew Morton .data = &sysctl_panic_on_oom,
714a19cad06SAndrew Morton .maxlen = sizeof(sysctl_panic_on_oom),
715a19cad06SAndrew Morton .mode = 0644,
716a19cad06SAndrew Morton .proc_handler = proc_dointvec_minmax,
717a19cad06SAndrew Morton .extra1 = SYSCTL_ZERO,
718a19cad06SAndrew Morton .extra2 = SYSCTL_TWO,
719a19cad06SAndrew Morton },
720a19cad06SAndrew Morton {
721a19cad06SAndrew Morton .procname = "oom_kill_allocating_task",
722a19cad06SAndrew Morton .data = &sysctl_oom_kill_allocating_task,
723a19cad06SAndrew Morton .maxlen = sizeof(sysctl_oom_kill_allocating_task),
724a19cad06SAndrew Morton .mode = 0644,
725a19cad06SAndrew Morton .proc_handler = proc_dointvec,
726a19cad06SAndrew Morton },
727a19cad06SAndrew Morton {
728a19cad06SAndrew Morton .procname = "oom_dump_tasks",
729a19cad06SAndrew Morton .data = &sysctl_oom_dump_tasks,
730a19cad06SAndrew Morton .maxlen = sizeof(sysctl_oom_dump_tasks),
731a19cad06SAndrew Morton .mode = 0644,
732a19cad06SAndrew Morton .proc_handler = proc_dointvec,
733a19cad06SAndrew Morton },
734a19cad06SAndrew Morton {}
735a19cad06SAndrew Morton };
736a19cad06SAndrew Morton #endif
737a19cad06SAndrew Morton
oom_init(void)738aac45363SMichal Hocko static int __init oom_init(void)
739aac45363SMichal Hocko {
740aac45363SMichal Hocko oom_reaper_th = kthread_run(oom_reaper, NULL, "oom_reaper");
74143fe219aSsujiaxun #ifdef CONFIG_SYSCTL
74243fe219aSsujiaxun register_sysctl_init("vm", vm_oom_kill_table);
74343fe219aSsujiaxun #endif
744aac45363SMichal Hocko return 0;
745aac45363SMichal Hocko }
subsys_initcall(oom_init)746aac45363SMichal Hocko subsys_initcall(oom_init)
7477c5f64f8SVladimir Davydov #else
748e4a38402SNico Pache static inline void queue_oom_reaper(struct task_struct *tsk)
7497c5f64f8SVladimir Davydov {
7507c5f64f8SVladimir Davydov }
7517c5f64f8SVladimir Davydov #endif /* CONFIG_MMU */
752aac45363SMichal Hocko
75349550b60SMichal Hocko /**
75416e95196SJohannes Weiner * mark_oom_victim - mark the given task as OOM victim
75549550b60SMichal Hocko * @tsk: task to mark
756c32b3cbeSMichal Hocko *
757dc56401fSJohannes Weiner * Has to be called with oom_lock held and never after
758c32b3cbeSMichal Hocko * oom has been disabled already.
75926db62f1SMichal Hocko *
76026db62f1SMichal Hocko * tsk->mm has to be non NULL and caller has to guarantee it is stable (either
76126db62f1SMichal Hocko * under task_lock or operate on the current).
76249550b60SMichal Hocko */
7637c5f64f8SVladimir Davydov static void mark_oom_victim(struct task_struct *tsk)
76449550b60SMichal Hocko {
7650a657f6eSCarlos Galo const struct cred *cred;
76626db62f1SMichal Hocko struct mm_struct *mm = tsk->mm;
76726db62f1SMichal Hocko
768c32b3cbeSMichal Hocko WARN_ON(oom_killer_disabled);
769c32b3cbeSMichal Hocko /* OOM killer might race with memcg OOM */
770c32b3cbeSMichal Hocko if (test_and_set_tsk_thread_flag(tsk, TIF_MEMDIE))
771c32b3cbeSMichal Hocko return;
77226db62f1SMichal Hocko
77326db62f1SMichal Hocko /* oom_mm is bound to the signal struct life time. */
774b3541d91SSuren Baghdasaryan if (!cmpxchg(&tsk->signal->oom_mm, NULL, mm))
775f1f10076SVegard Nossum mmgrab(tsk->signal->oom_mm);
77626db62f1SMichal Hocko
77763a8ca9bSMichal Hocko /*
77863a8ca9bSMichal Hocko * Make sure that the task is woken up from uninterruptible sleep
77963a8ca9bSMichal Hocko * if it is frozen because OOM killer wouldn't be able to free
78063a8ca9bSMichal Hocko * any memory and livelock. freezing_slow_path will tell the freezer
78163a8ca9bSMichal Hocko * that TIF_MEMDIE tasks should be ignored.
78263a8ca9bSMichal Hocko */
78363a8ca9bSMichal Hocko __thaw_task(tsk);
784c32b3cbeSMichal Hocko atomic_inc(&oom_victims);
7850a657f6eSCarlos Galo cred = get_task_cred(tsk);
7860a657f6eSCarlos Galo trace_mark_victim(tsk, cred->uid.val);
7870a657f6eSCarlos Galo put_cred(cred);
78849550b60SMichal Hocko }
78949550b60SMichal Hocko
79049550b60SMichal Hocko /**
79116e95196SJohannes Weiner * exit_oom_victim - note the exit of an OOM victim
79249550b60SMichal Hocko */
exit_oom_victim(void)79338531201STetsuo Handa void exit_oom_victim(void)
79449550b60SMichal Hocko {
79538531201STetsuo Handa clear_thread_flag(TIF_MEMDIE);
796c32b3cbeSMichal Hocko
797c38f1025SJohannes Weiner if (!atomic_dec_return(&oom_victims))
798c32b3cbeSMichal Hocko wake_up_all(&oom_victims_wait);
799c32b3cbeSMichal Hocko }
800c32b3cbeSMichal Hocko
801c32b3cbeSMichal Hocko /**
8027d2e7a22SMichal Hocko * oom_killer_enable - enable OOM killer
8037d2e7a22SMichal Hocko */
oom_killer_enable(void)8047d2e7a22SMichal Hocko void oom_killer_enable(void)
8057d2e7a22SMichal Hocko {
8067d2e7a22SMichal Hocko oom_killer_disabled = false;
807d75da004SMichal Hocko pr_info("OOM killer enabled.\n");
8087d2e7a22SMichal Hocko }
8097d2e7a22SMichal Hocko
8107d2e7a22SMichal Hocko /**
811c32b3cbeSMichal Hocko * oom_killer_disable - disable OOM killer
8127d2e7a22SMichal Hocko * @timeout: maximum timeout to wait for oom victims in jiffies
813c32b3cbeSMichal Hocko *
814c32b3cbeSMichal Hocko * Forces all page allocations to fail rather than trigger OOM killer.
8157d2e7a22SMichal Hocko * Will block and wait until all OOM victims are killed or the given
8167d2e7a22SMichal Hocko * timeout expires.
817c32b3cbeSMichal Hocko *
818c32b3cbeSMichal Hocko * The function cannot be called when there are runnable user tasks because
819c32b3cbeSMichal Hocko * the userspace would see unexpected allocation failures as a result. Any
820c32b3cbeSMichal Hocko * new usage of this function should be consulted with MM people.
821c32b3cbeSMichal Hocko *
822c32b3cbeSMichal Hocko * Returns true if successful and false if the OOM killer cannot be
823c32b3cbeSMichal Hocko * disabled.
824c32b3cbeSMichal Hocko */
oom_killer_disable(signed long timeout)8257d2e7a22SMichal Hocko bool oom_killer_disable(signed long timeout)
826c32b3cbeSMichal Hocko {
8277d2e7a22SMichal Hocko signed long ret;
8287d2e7a22SMichal Hocko
829c32b3cbeSMichal Hocko /*
8306afcf289STetsuo Handa * Make sure to not race with an ongoing OOM killer. Check that the
8316afcf289STetsuo Handa * current is not killed (possibly due to sharing the victim's memory).
832c32b3cbeSMichal Hocko */
8336afcf289STetsuo Handa if (mutex_lock_killable(&oom_lock))
834c32b3cbeSMichal Hocko return false;
835c32b3cbeSMichal Hocko oom_killer_disabled = true;
836dc56401fSJohannes Weiner mutex_unlock(&oom_lock);
837c32b3cbeSMichal Hocko
8387d2e7a22SMichal Hocko ret = wait_event_interruptible_timeout(oom_victims_wait,
8397d2e7a22SMichal Hocko !atomic_read(&oom_victims), timeout);
8407d2e7a22SMichal Hocko if (ret <= 0) {
8417d2e7a22SMichal Hocko oom_killer_enable();
8427d2e7a22SMichal Hocko return false;
843c32b3cbeSMichal Hocko }
844d75da004SMichal Hocko pr_info("OOM killer disabled.\n");
845c32b3cbeSMichal Hocko
8467d2e7a22SMichal Hocko return true;
84749550b60SMichal Hocko }
84849550b60SMichal Hocko
__task_will_free_mem(struct task_struct * task)8491af8bb43SMichal Hocko static inline bool __task_will_free_mem(struct task_struct *task)
8501af8bb43SMichal Hocko {
8511af8bb43SMichal Hocko struct signal_struct *sig = task->signal;
8521af8bb43SMichal Hocko
8531af8bb43SMichal Hocko /*
854d67e03e3SEric W. Biederman * A coredumping process may sleep for an extended period in
85592307383SEric W. Biederman * coredump_task_exit(), so the oom killer cannot assume that
856d67e03e3SEric W. Biederman * the process will promptly exit and release memory.
8571af8bb43SMichal Hocko */
85898b24b16SEric W. Biederman if (sig->core_state)
8591af8bb43SMichal Hocko return false;
8601af8bb43SMichal Hocko
8611af8bb43SMichal Hocko if (sig->flags & SIGNAL_GROUP_EXIT)
8621af8bb43SMichal Hocko return true;
8631af8bb43SMichal Hocko
8641af8bb43SMichal Hocko if (thread_group_empty(task) && (task->flags & PF_EXITING))
8651af8bb43SMichal Hocko return true;
8661af8bb43SMichal Hocko
8671af8bb43SMichal Hocko return false;
8681af8bb43SMichal Hocko }
8691af8bb43SMichal Hocko
8701af8bb43SMichal Hocko /*
8711af8bb43SMichal Hocko * Checks whether the given task is dying or exiting and likely to
8721af8bb43SMichal Hocko * release its address space. This means that all threads and processes
8731af8bb43SMichal Hocko * sharing the same mm have to be killed or exiting.
874091f362cSMichal Hocko * Caller has to make sure that task->mm is stable (hold task_lock or
875091f362cSMichal Hocko * it operates on the current).
8761af8bb43SMichal Hocko */
task_will_free_mem(struct task_struct * task)8777c5f64f8SVladimir Davydov static bool task_will_free_mem(struct task_struct *task)
8781af8bb43SMichal Hocko {
879091f362cSMichal Hocko struct mm_struct *mm = task->mm;
8801af8bb43SMichal Hocko struct task_struct *p;
881f33e6f06SGeert Uytterhoeven bool ret = true;
8821af8bb43SMichal Hocko
883091f362cSMichal Hocko /*
884091f362cSMichal Hocko * Skip tasks without mm because it might have passed its exit_mm and
885091f362cSMichal Hocko * exit_oom_victim. oom_reaper could have rescued that but do not rely
886091f362cSMichal Hocko * on that for now. We can consider find_lock_task_mm in future.
887091f362cSMichal Hocko */
888091f362cSMichal Hocko if (!mm)
889091f362cSMichal Hocko return false;
890091f362cSMichal Hocko
8911af8bb43SMichal Hocko if (!__task_will_free_mem(task))
8921af8bb43SMichal Hocko return false;
8931af8bb43SMichal Hocko
8941af8bb43SMichal Hocko /*
895696453e6SMichal Hocko * This task has already been drained by the oom reaper so there are
896696453e6SMichal Hocko * only small chances it will free some more
897696453e6SMichal Hocko */
898862e3073SMichal Hocko if (test_bit(MMF_OOM_SKIP, &mm->flags))
899696453e6SMichal Hocko return false;
900696453e6SMichal Hocko
901091f362cSMichal Hocko if (atomic_read(&mm->mm_users) <= 1)
9021af8bb43SMichal Hocko return true;
9031af8bb43SMichal Hocko
9041af8bb43SMichal Hocko /*
9055870c2e1SMichal Hocko * Make sure that all tasks which share the mm with the given tasks
9065870c2e1SMichal Hocko * are dying as well to make sure that a) nobody pins its mm and
9075870c2e1SMichal Hocko * b) the task is also reapable by the oom reaper.
9081af8bb43SMichal Hocko */
9091af8bb43SMichal Hocko rcu_read_lock();
9101af8bb43SMichal Hocko for_each_process(p) {
9111af8bb43SMichal Hocko if (!process_shares_mm(p, mm))
9121af8bb43SMichal Hocko continue;
9131af8bb43SMichal Hocko if (same_thread_group(task, p))
9141af8bb43SMichal Hocko continue;
9151af8bb43SMichal Hocko ret = __task_will_free_mem(p);
9161af8bb43SMichal Hocko if (!ret)
9171af8bb43SMichal Hocko break;
9181af8bb43SMichal Hocko }
9191af8bb43SMichal Hocko rcu_read_unlock();
9201af8bb43SMichal Hocko
9211af8bb43SMichal Hocko return ret;
9221af8bb43SMichal Hocko }
9231af8bb43SMichal Hocko
__oom_kill_process(struct task_struct * victim,const char * message)924bbbe4802SShakeel Butt static void __oom_kill_process(struct task_struct *victim, const char *message)
9251da177e4SLinus Torvalds {
9265989ad7bSRoman Gushchin struct task_struct *p;
927647f2bdfSDavid Rientjes struct mm_struct *mm;
928bb29902aSTetsuo Handa bool can_oom_reap = true;
9291da177e4SLinus Torvalds
9306b0c81b3SDavid Rientjes p = find_lock_task_mm(victim);
9316b0c81b3SDavid Rientjes if (!p) {
932619b5b46SYafang Shao pr_info("%s: OOM victim %d (%s) is already exiting. Skip killing the task\n",
933619b5b46SYafang Shao message, task_pid_nr(victim), victim->comm);
9346b0c81b3SDavid Rientjes put_task_struct(victim);
935647f2bdfSDavid Rientjes return;
9366b0c81b3SDavid Rientjes } else if (victim != p) {
9376b0c81b3SDavid Rientjes get_task_struct(p);
9386b0c81b3SDavid Rientjes put_task_struct(victim);
9396b0c81b3SDavid Rientjes victim = p;
9406b0c81b3SDavid Rientjes }
941647f2bdfSDavid Rientjes
942880b7689STetsuo Handa /* Get a reference to safely compare mm after task_unlock(victim) */
943647f2bdfSDavid Rientjes mm = victim->mm;
944f1f10076SVegard Nossum mmgrab(mm);
9458e675f7aSKonstantin Khlebnikov
9468e675f7aSKonstantin Khlebnikov /* Raise event before sending signal: task reaper must see this */
9478e675f7aSKonstantin Khlebnikov count_vm_event(OOM_KILL);
948fe6bdfc8SRoman Gushchin memcg_memory_event_mm(mm, MEMCG_OOM_KILL);
9498e675f7aSKonstantin Khlebnikov
950426fb5e7STetsuo Handa /*
951cd04ae1eSMichal Hocko * We should send SIGKILL before granting access to memory reserves
952cd04ae1eSMichal Hocko * in order to prevent the OOM victim from depleting the memory
953cd04ae1eSMichal Hocko * reserves from the user space under its control.
954426fb5e7STetsuo Handa */
955079b22dcSEric W. Biederman do_send_sig_info(SIGKILL, SEND_SIG_PRIV, victim, PIDTYPE_TGID);
95616e95196SJohannes Weiner mark_oom_victim(victim);
95770cb6d26SEdward Chron pr_err("%s: Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB, UID:%u pgtables:%lukB oom_score_adj:%hd\n",
95870cb6d26SEdward Chron message, task_pid_nr(victim), victim->comm, K(mm->total_vm),
95970cb6d26SEdward Chron K(get_mm_counter(mm, MM_ANONPAGES)),
96070cb6d26SEdward Chron K(get_mm_counter(mm, MM_FILEPAGES)),
96170cb6d26SEdward Chron K(get_mm_counter(mm, MM_SHMEMPAGES)),
96270cb6d26SEdward Chron from_kuid(&init_user_ns, task_uid(victim)),
963941f762bSIlya Dryomov mm_pgtables_bytes(mm) >> 10, victim->signal->oom_score_adj);
964647f2bdfSDavid Rientjes task_unlock(victim);
965647f2bdfSDavid Rientjes
966647f2bdfSDavid Rientjes /*
967647f2bdfSDavid Rientjes * Kill all user processes sharing victim->mm in other thread groups, if
968647f2bdfSDavid Rientjes * any. They don't get access to memory reserves, though, to avoid
969c1e8d7c6SMichel Lespinasse * depletion of all memory. This prevents mm->mmap_lock livelock when an
970647f2bdfSDavid Rientjes * oom killed thread cannot exit because it requires the semaphore and
971647f2bdfSDavid Rientjes * its contended by another thread trying to allocate memory itself.
972647f2bdfSDavid Rientjes * That thread will now get access to memory reserves since it has a
973647f2bdfSDavid Rientjes * pending fatal signal.
974647f2bdfSDavid Rientjes */
9754d4048beSOleg Nesterov rcu_read_lock();
976c319025aSOleg Nesterov for_each_process(p) {
9774d7b3394SOleg Nesterov if (!process_shares_mm(p, mm))
978c319025aSOleg Nesterov continue;
979c319025aSOleg Nesterov if (same_thread_group(p, victim))
980c319025aSOleg Nesterov continue;
9811b51e65eSMichal Hocko if (is_global_init(p)) {
982aac45363SMichal Hocko can_oom_reap = false;
983862e3073SMichal Hocko set_bit(MMF_OOM_SKIP, &mm->flags);
984a373966dSMichal Hocko pr_info("oom killer %d (%s) has mm pinned by %d (%s)\n",
985a373966dSMichal Hocko task_pid_nr(victim), victim->comm,
986a373966dSMichal Hocko task_pid_nr(p), p->comm);
987c319025aSOleg Nesterov continue;
988aac45363SMichal Hocko }
9891b51e65eSMichal Hocko /*
9904c9c3809SRolf Eike Beer * No kthread_use_mm() user needs to read from the userspace so
991f5678e7fSChristoph Hellwig * we are ok to reap it.
9921b51e65eSMichal Hocko */
9931b51e65eSMichal Hocko if (unlikely(p->flags & PF_KTHREAD))
9941b51e65eSMichal Hocko continue;
995079b22dcSEric W. Biederman do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID);
9961da177e4SLinus Torvalds }
9976b0c81b3SDavid Rientjes rcu_read_unlock();
9981da177e4SLinus Torvalds
999aac45363SMichal Hocko if (can_oom_reap)
1000e4a38402SNico Pache queue_oom_reaper(victim);
1001aac45363SMichal Hocko
1002880b7689STetsuo Handa mmdrop(mm);
10036b0c81b3SDavid Rientjes put_task_struct(victim);
1004647f2bdfSDavid Rientjes }
1005647f2bdfSDavid Rientjes
1006309ed882SDavid Rientjes /*
10073d8b38ebSRoman Gushchin * Kill provided task unless it's secured by setting
10083d8b38ebSRoman Gushchin * oom_score_adj to OOM_SCORE_ADJ_MIN.
10093d8b38ebSRoman Gushchin */
oom_kill_memcg_member(struct task_struct * task,void * message)1010bbbe4802SShakeel Butt static int oom_kill_memcg_member(struct task_struct *task, void *message)
10113d8b38ebSRoman Gushchin {
1012d342a0b3STetsuo Handa if (task->signal->oom_score_adj != OOM_SCORE_ADJ_MIN &&
1013d342a0b3STetsuo Handa !is_global_init(task)) {
10143d8b38ebSRoman Gushchin get_task_struct(task);
1015bbbe4802SShakeel Butt __oom_kill_process(task, message);
10163d8b38ebSRoman Gushchin }
10173d8b38ebSRoman Gushchin return 0;
10183d8b38ebSRoman Gushchin }
10193d8b38ebSRoman Gushchin
oom_kill_process(struct oom_control * oc,const char * message)10205989ad7bSRoman Gushchin static void oom_kill_process(struct oom_control *oc, const char *message)
10215989ad7bSRoman Gushchin {
1022bbbe4802SShakeel Butt struct task_struct *victim = oc->chosen;
10233d8b38ebSRoman Gushchin struct mem_cgroup *oom_group;
10245989ad7bSRoman Gushchin static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL,
10255989ad7bSRoman Gushchin DEFAULT_RATELIMIT_BURST);
10265989ad7bSRoman Gushchin
10275989ad7bSRoman Gushchin /*
10285989ad7bSRoman Gushchin * If the task is already exiting, don't alarm the sysadmin or kill
10295989ad7bSRoman Gushchin * its children or threads, just give it access to memory reserves
10305989ad7bSRoman Gushchin * so it can die quickly
10315989ad7bSRoman Gushchin */
1032bbbe4802SShakeel Butt task_lock(victim);
1033bbbe4802SShakeel Butt if (task_will_free_mem(victim)) {
1034bbbe4802SShakeel Butt mark_oom_victim(victim);
1035e4a38402SNico Pache queue_oom_reaper(victim);
1036bbbe4802SShakeel Butt task_unlock(victim);
1037bbbe4802SShakeel Butt put_task_struct(victim);
10385989ad7bSRoman Gushchin return;
10395989ad7bSRoman Gushchin }
1040bbbe4802SShakeel Butt task_unlock(victim);
10415989ad7bSRoman Gushchin
10425989ad7bSRoman Gushchin if (__ratelimit(&oom_rs))
1043bbbe4802SShakeel Butt dump_header(oc, victim);
10445989ad7bSRoman Gushchin
10453d8b38ebSRoman Gushchin /*
10463d8b38ebSRoman Gushchin * Do we need to kill the entire memory cgroup?
10473d8b38ebSRoman Gushchin * Or even one of the ancestor memory cgroups?
10483d8b38ebSRoman Gushchin * Check this out before killing the victim task.
10493d8b38ebSRoman Gushchin */
10503d8b38ebSRoman Gushchin oom_group = mem_cgroup_get_oom_group(victim, oc->memcg);
10513d8b38ebSRoman Gushchin
1052bbbe4802SShakeel Butt __oom_kill_process(victim, message);
10533d8b38ebSRoman Gushchin
10543d8b38ebSRoman Gushchin /*
10553d8b38ebSRoman Gushchin * If necessary, kill all tasks in the selected memory cgroup.
10563d8b38ebSRoman Gushchin */
10573d8b38ebSRoman Gushchin if (oom_group) {
1058b6bf9abbSDan Schatzberg memcg_memory_event(oom_group, MEMCG_OOM_GROUP_KILL);
10593d8b38ebSRoman Gushchin mem_cgroup_print_oom_group(oom_group);
1060bbbe4802SShakeel Butt mem_cgroup_scan_tasks(oom_group, oom_kill_memcg_member,
1061bbbe4802SShakeel Butt (void *)message);
10623d8b38ebSRoman Gushchin mem_cgroup_put(oom_group);
10633d8b38ebSRoman Gushchin }
10645989ad7bSRoman Gushchin }
10655989ad7bSRoman Gushchin
1066309ed882SDavid Rientjes /*
1067309ed882SDavid Rientjes * Determines whether the kernel must panic because of the panic_on_oom sysctl.
1068309ed882SDavid Rientjes */
check_panic_on_oom(struct oom_control * oc)1069432b1de0SYafang Shao static void check_panic_on_oom(struct oom_control *oc)
1070309ed882SDavid Rientjes {
1071309ed882SDavid Rientjes if (likely(!sysctl_panic_on_oom))
1072309ed882SDavid Rientjes return;
1073309ed882SDavid Rientjes if (sysctl_panic_on_oom != 2) {
1074309ed882SDavid Rientjes /*
1075309ed882SDavid Rientjes * panic_on_oom == 1 only affects CONSTRAINT_NONE, the kernel
1076309ed882SDavid Rientjes * does not panic for cpuset, mempolicy, or memcg allocation
1077309ed882SDavid Rientjes * failures.
1078309ed882SDavid Rientjes */
1079432b1de0SYafang Shao if (oc->constraint != CONSTRAINT_NONE)
1080309ed882SDavid Rientjes return;
1081309ed882SDavid Rientjes }
1082071a4befSDavid Rientjes /* Do not panic for oom kills triggered by sysrq */
1083db2a0dd7SYaowei Bai if (is_sysrq_oom(oc))
1084071a4befSDavid Rientjes return;
10852a966b77SVladimir Davydov dump_header(oc, NULL);
1086309ed882SDavid Rientjes panic("Out of memory: %s panic_on_oom is enabled\n",
1087309ed882SDavid Rientjes sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide");
1088309ed882SDavid Rientjes }
1089309ed882SDavid Rientjes
10908bc719d3SMartin Schwidefsky static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
10918bc719d3SMartin Schwidefsky
register_oom_notifier(struct notifier_block * nb)10928bc719d3SMartin Schwidefsky int register_oom_notifier(struct notifier_block *nb)
10938bc719d3SMartin Schwidefsky {
10948bc719d3SMartin Schwidefsky return blocking_notifier_chain_register(&oom_notify_list, nb);
10958bc719d3SMartin Schwidefsky }
10968bc719d3SMartin Schwidefsky EXPORT_SYMBOL_GPL(register_oom_notifier);
10978bc719d3SMartin Schwidefsky
unregister_oom_notifier(struct notifier_block * nb)10988bc719d3SMartin Schwidefsky int unregister_oom_notifier(struct notifier_block *nb)
10998bc719d3SMartin Schwidefsky {
11008bc719d3SMartin Schwidefsky return blocking_notifier_chain_unregister(&oom_notify_list, nb);
11018bc719d3SMartin Schwidefsky }
11028bc719d3SMartin Schwidefsky EXPORT_SYMBOL_GPL(unregister_oom_notifier);
11038bc719d3SMartin Schwidefsky
11041da177e4SLinus Torvalds /**
11056e0fc46dSDavid Rientjes * out_of_memory - kill the "best" process when we run out of memory
11066e0fc46dSDavid Rientjes * @oc: pointer to struct oom_control
11071da177e4SLinus Torvalds *
11081da177e4SLinus Torvalds * If we run out of memory, we have the choice between either
11091da177e4SLinus Torvalds * killing a random task (bad), letting the system crash (worse)
11101da177e4SLinus Torvalds * OR try to be smart about which process to kill. Note that we
11111da177e4SLinus Torvalds * don't have to be perfect here, we just have to be good.
11121da177e4SLinus Torvalds */
out_of_memory(struct oom_control * oc)11136e0fc46dSDavid Rientjes bool out_of_memory(struct oom_control *oc)
11141da177e4SLinus Torvalds {
11158bc719d3SMartin Schwidefsky unsigned long freed = 0;
11168bc719d3SMartin Schwidefsky
1117dc56401fSJohannes Weiner if (oom_killer_disabled)
1118dc56401fSJohannes Weiner return false;
1119dc56401fSJohannes Weiner
11207c5f64f8SVladimir Davydov if (!is_memcg_oom(oc)) {
11218bc719d3SMartin Schwidefsky blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
1122f530243aSJann Horn if (freed > 0 && !is_sysrq_oom(oc))
11238bc719d3SMartin Schwidefsky /* Got some memory back in the last second. */
112475e8f8b2SDavid Rientjes return true;
11257c5f64f8SVladimir Davydov }
11261da177e4SLinus Torvalds
11277b98c2e4SDavid Rientjes /*
11289ff4868eSDavid Rientjes * If current has a pending SIGKILL or is exiting, then automatically
11299ff4868eSDavid Rientjes * select it. The goal is to allow it to allocate so that it may
11309ff4868eSDavid Rientjes * quickly exit and free its memory.
11317b98c2e4SDavid Rientjes */
1132091f362cSMichal Hocko if (task_will_free_mem(current)) {
113316e95196SJohannes Weiner mark_oom_victim(current);
1134e4a38402SNico Pache queue_oom_reaper(current);
113575e8f8b2SDavid Rientjes return true;
11367b98c2e4SDavid Rientjes }
11377b98c2e4SDavid Rientjes
11389b0f8b04SChristoph Lameter /*
11393da88fb3SMichal Hocko * The OOM killer does not compensate for IO-less reclaim.
11404822acb1SHaifeng Xu * But mem_cgroup_oom() has to invoke the OOM killer even
11414822acb1SHaifeng Xu * if it is a GFP_NOFS allocation.
11423da88fb3SMichal Hocko */
11434822acb1SHaifeng Xu if (!(oc->gfp_mask & __GFP_FS) && !is_memcg_oom(oc))
11443da88fb3SMichal Hocko return true;
11453da88fb3SMichal Hocko
11463da88fb3SMichal Hocko /*
11479b0f8b04SChristoph Lameter * Check if there were limitations on the allocation (only relevant for
11487c5f64f8SVladimir Davydov * NUMA and memcg) that may require different handling.
11499b0f8b04SChristoph Lameter */
1150432b1de0SYafang Shao oc->constraint = constrained_alloc(oc);
1151432b1de0SYafang Shao if (oc->constraint != CONSTRAINT_MEMORY_POLICY)
11526e0fc46dSDavid Rientjes oc->nodemask = NULL;
1153432b1de0SYafang Shao check_panic_on_oom(oc);
11540aad4b31SDavid Rientjes
11557c5f64f8SVladimir Davydov if (!is_memcg_oom(oc) && sysctl_oom_kill_allocating_task &&
1156ac311a14SShakeel Butt current->mm && !oom_unkillable_task(current) &&
1157ac311a14SShakeel Butt oom_cpuset_eligible(current, oc) &&
1158121d1ba0SDavid Rientjes current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {
11596b0c81b3SDavid Rientjes get_task_struct(current);
11607c5f64f8SVladimir Davydov oc->chosen = current;
11617c5f64f8SVladimir Davydov oom_kill_process(oc, "Out of memory (oom_kill_allocating_task)");
116275e8f8b2SDavid Rientjes return true;
11630aad4b31SDavid Rientjes }
11640aad4b31SDavid Rientjes
11657c5f64f8SVladimir Davydov select_bad_process(oc);
11663100dab2SJohannes Weiner /* Found nothing?!?! */
11673100dab2SJohannes Weiner if (!oc->chosen) {
11682a966b77SVladimir Davydov dump_header(oc, NULL);
11693100dab2SJohannes Weiner pr_warn("Out of memory and no killable processes...\n");
11703100dab2SJohannes Weiner /*
11713100dab2SJohannes Weiner * If we got here due to an actual allocation at the
11723100dab2SJohannes Weiner * system level, we cannot survive this and will enter
11733100dab2SJohannes Weiner * an endless loop in the allocator. Bail out now.
11743100dab2SJohannes Weiner */
11753100dab2SJohannes Weiner if (!is_sysrq_oom(oc) && !is_memcg_oom(oc))
11763100dab2SJohannes Weiner panic("System is deadlocked on memory\n");
11770aad4b31SDavid Rientjes }
11789bfe5dedSMichal Hocko if (oc->chosen && oc->chosen != (void *)-1UL)
11797c5f64f8SVladimir Davydov oom_kill_process(oc, !is_memcg_oom(oc) ? "Out of memory" :
11807c5f64f8SVladimir Davydov "Memory cgroup out of memory");
11817c5f64f8SVladimir Davydov return !!oc->chosen;
1182c32b3cbeSMichal Hocko }
1183c32b3cbeSMichal Hocko
1184e3658932SDavid Rientjes /*
118560e2793dSMichal Hocko * The pagefault handler calls here because some allocation has failed. We have
118660e2793dSMichal Hocko * to take care of the memcg OOM here because this is the only safe context without
118760e2793dSMichal Hocko * any locks held but let the oom killer triggered from the allocation context care
118860e2793dSMichal Hocko * about the global OOM.
1189e3658932SDavid Rientjes */
pagefault_out_of_memory(void)1190e3658932SDavid Rientjes void pagefault_out_of_memory(void)
1191e3658932SDavid Rientjes {
119260e2793dSMichal Hocko static DEFINE_RATELIMIT_STATE(pfoom_rs, DEFAULT_RATELIMIT_INTERVAL,
119360e2793dSMichal Hocko DEFAULT_RATELIMIT_BURST);
11946e0fc46dSDavid Rientjes
119549426420SJohannes Weiner if (mem_cgroup_oom_synchronize(true))
1196dc56401fSJohannes Weiner return;
11973812c8c8SJohannes Weiner
11980b28179aSVasily Averin if (fatal_signal_pending(current))
1199dc56401fSJohannes Weiner return;
12000b28179aSVasily Averin
120160e2793dSMichal Hocko if (__ratelimit(&pfoom_rs))
120260e2793dSMichal Hocko pr_warn("Huh VM_FAULT_OOM leaked out to the #PF handler. Retrying PF\n");
1203e3658932SDavid Rientjes }
1204884a7e59SSuren Baghdasaryan
SYSCALL_DEFINE2(process_mrelease,int,pidfd,unsigned int,flags)1205884a7e59SSuren Baghdasaryan SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
1206884a7e59SSuren Baghdasaryan {
1207884a7e59SSuren Baghdasaryan #ifdef CONFIG_MMU
1208884a7e59SSuren Baghdasaryan struct mm_struct *mm = NULL;
1209884a7e59SSuren Baghdasaryan struct task_struct *task;
1210884a7e59SSuren Baghdasaryan struct task_struct *p;
1211884a7e59SSuren Baghdasaryan unsigned int f_flags;
1212337546e8SSuren Baghdasaryan bool reap = false;
1213884a7e59SSuren Baghdasaryan long ret = 0;
1214884a7e59SSuren Baghdasaryan
1215884a7e59SSuren Baghdasaryan if (flags)
1216884a7e59SSuren Baghdasaryan return -EINVAL;
1217884a7e59SSuren Baghdasaryan
1218ee9955d6SChristian Brauner task = pidfd_get_task(pidfd, &f_flags);
1219ee9955d6SChristian Brauner if (IS_ERR(task))
1220ee9955d6SChristian Brauner return PTR_ERR(task);
1221884a7e59SSuren Baghdasaryan
1222884a7e59SSuren Baghdasaryan /*
1223884a7e59SSuren Baghdasaryan * Make sure to choose a thread which still has a reference to mm
1224884a7e59SSuren Baghdasaryan * during the group exit
1225884a7e59SSuren Baghdasaryan */
1226884a7e59SSuren Baghdasaryan p = find_lock_task_mm(task);
1227884a7e59SSuren Baghdasaryan if (!p) {
1228884a7e59SSuren Baghdasaryan ret = -ESRCH;
1229884a7e59SSuren Baghdasaryan goto put_task;
1230884a7e59SSuren Baghdasaryan }
1231884a7e59SSuren Baghdasaryan
1232884a7e59SSuren Baghdasaryan mm = p->mm;
1233ba535c1cSSuren Baghdasaryan mmgrab(mm);
1234ba535c1cSSuren Baghdasaryan
1235337546e8SSuren Baghdasaryan if (task_will_free_mem(p))
1236337546e8SSuren Baghdasaryan reap = true;
1237337546e8SSuren Baghdasaryan else {
1238337546e8SSuren Baghdasaryan /* Error only if the work has not been done already */
1239337546e8SSuren Baghdasaryan if (!test_bit(MMF_OOM_SKIP, &mm->flags))
1240884a7e59SSuren Baghdasaryan ret = -EINVAL;
1241884a7e59SSuren Baghdasaryan }
1242884a7e59SSuren Baghdasaryan task_unlock(p);
1243884a7e59SSuren Baghdasaryan
1244884a7e59SSuren Baghdasaryan if (!reap)
1245884a7e59SSuren Baghdasaryan goto drop_mm;
1246884a7e59SSuren Baghdasaryan
1247884a7e59SSuren Baghdasaryan if (mmap_read_lock_killable(mm)) {
1248884a7e59SSuren Baghdasaryan ret = -EINTR;
1249884a7e59SSuren Baghdasaryan goto drop_mm;
1250884a7e59SSuren Baghdasaryan }
1251ba535c1cSSuren Baghdasaryan /*
1252ba535c1cSSuren Baghdasaryan * Check MMF_OOM_SKIP again under mmap_read_lock protection to ensure
1253ba535c1cSSuren Baghdasaryan * possible change in exit_mmap is seen
1254ba535c1cSSuren Baghdasaryan */
1255ba535c1cSSuren Baghdasaryan if (!test_bit(MMF_OOM_SKIP, &mm->flags) && !__oom_reap_task_mm(mm))
1256884a7e59SSuren Baghdasaryan ret = -EAGAIN;
1257884a7e59SSuren Baghdasaryan mmap_read_unlock(mm);
1258884a7e59SSuren Baghdasaryan
1259884a7e59SSuren Baghdasaryan drop_mm:
1260ba535c1cSSuren Baghdasaryan mmdrop(mm);
1261884a7e59SSuren Baghdasaryan put_task:
1262884a7e59SSuren Baghdasaryan put_task_struct(task);
1263884a7e59SSuren Baghdasaryan return ret;
1264884a7e59SSuren Baghdasaryan #else
1265884a7e59SSuren Baghdasaryan return -ENOSYS;
1266884a7e59SSuren Baghdasaryan #endif /* CONFIG_MMU */
1267884a7e59SSuren Baghdasaryan }
1268