blob: 832fb330376efe9f509045516a72d795af5e39a8 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/mm/oom_kill.c
4 *
5 * Copyright (C) 1998,2000 Rik van Riel
6 * Thanks go out to Claus Fischer for some serious inspiration and
7 * for goading me into coding this file...
David Rientjesa63d83f2010-08-09 17:19:46 -07008 * Copyright (C) 2010 Google, Inc.
9 * Rewritten by David Rientjes
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * The routines in this file are used to kill a process when
Paul Jacksona49335c2005-09-06 15:18:09 -070012 * we're seriously out of memory. This gets called from __alloc_pages()
13 * in mm/page_alloc.c when we really run out of memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * Since we won't call these routines often (on a well-configured
16 * machine) this file will double as a 'coding guide' and a signpost
17 * for newbie kernel hackers. It features several pointers to major
18 * kernel subsystems and hints as to where to find out what things do.
19 */
20
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070021#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040023#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010026#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010027#include <linux/sched/coredump.h>
Ingo Molnar29930022017-02-08 18:51:36 +010028#include <linux/sched/task.h>
David Rientjes8a7ff022020-01-30 22:14:26 -080029#include <linux/sched/debug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/swap.h>
Suren Baghdasaryan884a7e52021-09-02 15:00:29 -070031#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/timex.h>
33#include <linux/jiffies.h>
Paul Jacksonef08e3b2005-09-06 15:18:13 -070034#include <linux/cpuset.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040035#include <linux/export.h>
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070036#include <linux/notifier.h>
Pavel Emelianovc7ba5c92008-02-07 00:13:58 -080037#include <linux/memcontrol.h>
David Rientjes6f48d0eb2010-08-09 17:18:52 -070038#include <linux/mempolicy.h>
David Howells5cd9c582008-08-14 11:37:28 +010039#include <linux/security.h>
David Rientjesedd45542011-03-22 16:30:12 -070040#include <linux/ptrace.h>
David Rientjesf660daa2011-10-31 17:07:07 -070041#include <linux/freezer.h>
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080042#include <linux/ftrace.h>
David Rientjesdc3f21e2012-03-21 16:33:47 -070043#include <linux/ratelimit.h>
Michal Hockoaac45362016-03-25 14:20:24 -070044#include <linux/kthread.h>
45#include <linux/init.h>
Michal Hocko4d4bbd82017-10-03 16:14:50 -070046#include <linux/mmu_notifier.h>
Michal Hockoaac45362016-03-25 14:20:24 -070047
48#include <asm/tlb.h>
49#include "internal.h"
Yang Shi852d8be2017-11-15 17:32:07 -080050#include "slab.h"
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080051
52#define CREATE_TRACE_POINTS
53#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
KAMEZAWA Hiroyukifadd8fb2006-06-23 02:03:13 -070055int sysctl_panic_on_oom;
David Rientjesfe071d72007-10-16 23:25:56 -070056int sysctl_oom_kill_allocating_task;
David Rientjesad915c42010-08-09 17:18:53 -070057int sysctl_oom_dump_tasks = 1;
Johannes Weinerdc564012015-06-24 16:57:19 -070058
Michal Hockoa195d3f2018-08-17 15:49:10 -070059/*
60 * Serializes oom killer invocations (out_of_memory()) from all contexts to
61 * prevent from over eager oom killing (e.g. when the oom killer is invoked
62 * from different domains).
63 *
64 * oom_killer_disable() relies on this lock to stabilize oom_killer_disabled
65 * and mark_oom_victim
66 */
Johannes Weinerdc564012015-06-24 16:57:19 -070067DEFINE_MUTEX(oom_lock);
Suren Baghdasaryan67197a42020-10-13 16:58:35 -070068/* Serializes oom_score_adj and oom_score_adj_min updates */
69DEFINE_MUTEX(oom_adj_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Shakeel Buttac311a12019-07-11 21:00:31 -070071static inline bool is_memcg_oom(struct oom_control *oc)
72{
73 return oc->memcg != NULL;
74}
75
David Rientjes6f48d0eb2010-08-09 17:18:52 -070076#ifdef CONFIG_NUMA
77/**
Ingo Molnarf0953a12021-05-06 18:06:47 -070078 * oom_cpuset_eligible() - check task eligibility for kill
Oleg Nesterovad962442014-01-21 15:50:00 -080079 * @start: task struct of which task to consider
Yi Wangf364f062019-09-23 15:37:14 -070080 * @oc: pointer to struct oom_control
David Rientjes6f48d0eb2010-08-09 17:18:52 -070081 *
82 * Task eligibility is determined by whether or not a candidate task, @tsk,
83 * shares the same mempolicy nodes as current if it is bound by such a policy
84 * and whether or not it has the same set of allowed cpuset nodes.
Shakeel Buttac311a12019-07-11 21:00:31 -070085 *
86 * This function is assuming oom-killer context and 'current' has triggered
87 * the oom-killer.
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070088 */
Shakeel Buttac311a12019-07-11 21:00:31 -070089static bool oom_cpuset_eligible(struct task_struct *start,
90 struct oom_control *oc)
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070091{
Oleg Nesterovad962442014-01-21 15:50:00 -080092 struct task_struct *tsk;
93 bool ret = false;
Shakeel Buttac311a12019-07-11 21:00:31 -070094 const nodemask_t *mask = oc->nodemask;
95
96 if (is_memcg_oom(oc))
97 return true;
KOSAKI Motohiro495789a2009-09-21 17:03:14 -070098
Oleg Nesterovad962442014-01-21 15:50:00 -080099 rcu_read_lock();
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800100 for_each_thread(start, tsk) {
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700101 if (mask) {
102 /*
103 * If this is a mempolicy constrained oom, tsk's
104 * cpuset is irrelevant. Only return true if its
105 * mempolicy intersects current, otherwise it may be
106 * needlessly killed.
107 */
Feng Tangb26e5172021-06-30 18:50:56 -0700108 ret = mempolicy_in_oom_domain(tsk, mask);
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700109 } else {
110 /*
111 * This is not a mempolicy constrained oom, so only
112 * check the mems of tsk's cpuset.
113 */
Oleg Nesterovad962442014-01-21 15:50:00 -0800114 ret = cpuset_mems_allowed_intersects(current, tsk);
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700115 }
Oleg Nesterovad962442014-01-21 15:50:00 -0800116 if (ret)
117 break;
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800118 }
Oleg Nesterovad962442014-01-21 15:50:00 -0800119 rcu_read_unlock();
KOSAKI Motohirodf1090a2010-08-09 17:19:39 -0700120
Oleg Nesterovad962442014-01-21 15:50:00 -0800121 return ret;
KOSAKI Motohiro495789a2009-09-21 17:03:14 -0700122}
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700123#else
Shakeel Buttac311a12019-07-11 21:00:31 -0700124static bool oom_cpuset_eligible(struct task_struct *tsk, struct oom_control *oc)
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700125{
126 return true;
127}
128#endif /* CONFIG_NUMA */
KOSAKI Motohiro495789a2009-09-21 17:03:14 -0700129
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700130/*
131 * The process p may have detached its own ->mm while exiting or through
Christoph Hellwigf5678e72020-06-10 18:42:06 -0700132 * kthread_use_mm(), but one or more of its subthreads may still have a valid
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700133 * pointer. Return p, or any of its subthreads with a valid ->mm, with
134 * task_lock() held.
135 */
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -0700136struct task_struct *find_lock_task_mm(struct task_struct *p)
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700137{
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800138 struct task_struct *t;
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700139
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800140 rcu_read_lock();
141
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800142 for_each_thread(p, t) {
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700143 task_lock(t);
144 if (likely(t->mm))
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800145 goto found;
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700146 task_unlock(t);
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800147 }
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800148 t = NULL;
149found:
150 rcu_read_unlock();
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700151
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800152 return t;
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700153}
154
Yaowei Baidb2a0dd2015-11-06 16:28:06 -0800155/*
156 * order == -1 means the oom kill is required by sysrq, otherwise only
157 * for display purposes.
158 */
159static inline bool is_sysrq_oom(struct oom_control *oc)
160{
161 return oc->order == -1;
162}
163
KOSAKI Motohiroab290ad2010-08-09 17:19:35 -0700164/* return true if the task is not adequate as candidate victim task. */
Shakeel Buttac311a12019-07-11 21:00:31 -0700165static bool oom_unkillable_task(struct task_struct *p)
KOSAKI Motohiroab290ad2010-08-09 17:19:35 -0700166{
167 if (is_global_init(p))
168 return true;
169 if (p->flags & PF_KTHREAD)
170 return true;
KOSAKI Motohiroab290ad2010-08-09 17:19:35 -0700171 return false;
172}
173
Randy Dunlap845be1c2021-04-16 15:45:54 -0700174/*
Hui Su259b3632020-12-14 19:12:49 -0800175 * Check whether unreclaimable slab amount is greater than
176 * all user memory(LRU pages).
177 * dump_unreclaimable_slab() could help in the case that
178 * oom due to too much unreclaimable slab used by kernel.
179*/
180static bool should_dump_unreclaim_slab(void)
Yang Shi852d8be2017-11-15 17:32:07 -0800181{
182 unsigned long nr_lru;
183
184 nr_lru = global_node_page_state(NR_ACTIVE_ANON) +
185 global_node_page_state(NR_INACTIVE_ANON) +
186 global_node_page_state(NR_ACTIVE_FILE) +
187 global_node_page_state(NR_INACTIVE_FILE) +
188 global_node_page_state(NR_ISOLATED_ANON) +
189 global_node_page_state(NR_ISOLATED_FILE) +
190 global_node_page_state(NR_UNEVICTABLE);
191
Roman Gushchind42f3242020-08-06 23:20:39 -0700192 return (global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B) > nr_lru);
Yang Shi852d8be2017-11-15 17:32:07 -0800193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/**
David Rientjesa63d83f2010-08-09 17:19:46 -0700196 * oom_badness - heuristic function to determine which candidate task to kill
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * @p: task struct of which task we should calculate
David Rientjesa63d83f2010-08-09 17:19:46 -0700198 * @totalpages: total present RAM allowed for page allocation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *
David Rientjesa63d83f2010-08-09 17:19:46 -0700200 * The heuristic for determining which task to kill is made to be as simple and
201 * predictable as possible. The goal is to return the highest value for the
202 * task consuming the most memory to avoid subsequent oom failures.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
Yafang Shao9066e5c2020-08-11 18:31:22 -0700204long oom_badness(struct task_struct *p, unsigned long totalpages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
David Rientjes1e11ad82012-06-08 13:21:26 -0700206 long points;
David Rientjes61eafb02012-06-20 12:52:58 -0700207 long adj;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700208
Shakeel Buttac311a12019-07-11 21:00:31 -0700209 if (oom_unkillable_task(p))
Yafang Shao9066e5c2020-08-11 18:31:22 -0700210 return LONG_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Oleg Nesterovdd8e8f42010-08-09 17:18:45 -0700212 p = find_lock_task_mm(p);
213 if (!p)
Yafang Shao9066e5c2020-08-11 18:31:22 -0700214 return LONG_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Michal Hockobb8a4b72016-05-20 16:57:18 -0700216 /*
217 * Do not even consider tasks which are explicitly marked oom
Michal Hockob18dc5f2016-07-28 15:44:46 -0700218 * unkillable or have been already oom reaped or the are in
219 * the middle of vfork
Michal Hockobb8a4b72016-05-20 16:57:18 -0700220 */
David Rientjesa9c58b902012-12-11 16:02:54 -0800221 adj = (long)p->signal->oom_score_adj;
Michal Hockobb8a4b72016-05-20 16:57:18 -0700222 if (adj == OOM_SCORE_ADJ_MIN ||
Michal Hocko862e3072016-10-07 16:58:57 -0700223 test_bit(MMF_OOM_SKIP, &p->mm->flags) ||
Michal Hockob18dc5f2016-07-28 15:44:46 -0700224 in_vfork(p)) {
Michal Hocko5aecc852011-11-15 14:36:07 -0800225 task_unlock(p);
Yafang Shao9066e5c2020-08-11 18:31:22 -0700226 return LONG_MIN;
Michal Hocko5aecc852011-11-15 14:36:07 -0800227 }
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /*
David Rientjesa63d83f2010-08-09 17:19:46 -0700230 * The baseline for the badness score is the proportion of RAM that each
KOSAKI Motohirof755a042011-04-27 15:26:50 -0700231 * task's rss, pagetable and swap space use.
David Rientjesa63d83f2010-08-09 17:19:46 -0700232 */
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800233 points = get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) +
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -0800234 mm_pgtables_bytes(p->mm) / PAGE_SIZE;
Andrew Morton97c2c9b82006-04-18 22:20:38 -0700235 task_unlock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
David Rientjes61eafb02012-06-20 12:52:58 -0700237 /* Normalize to oom_score_adj units */
238 adj *= totalpages / 1000;
239 points += adj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Yafang Shao9066e5c2020-08-11 18:31:22 -0700241 return points;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
yuzhoujianef8444e2018-12-28 00:36:07 -0800244static const char * const oom_constraint_text[] = {
245 [CONSTRAINT_NONE] = "CONSTRAINT_NONE",
246 [CONSTRAINT_CPUSET] = "CONSTRAINT_CPUSET",
247 [CONSTRAINT_MEMORY_POLICY] = "CONSTRAINT_MEMORY_POLICY",
248 [CONSTRAINT_MEMCG] = "CONSTRAINT_MEMCG",
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700249};
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800252 * Determine the type of allocation constraint.
253 */
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700254static enum oom_constraint constrained_alloc(struct oom_control *oc)
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800255{
Mel Gorman54a6eb52008-04-28 02:12:16 -0700256 struct zone *zone;
Mel Gormandd1a2392008-04-28 02:12:17 -0700257 struct zoneref *z;
Joonsoo Kim97a225e2020-06-03 15:59:01 -0700258 enum zone_type highest_zoneidx = gfp_zone(oc->gfp_mask);
David Rientjesa63d83f2010-08-09 17:19:46 -0700259 bool cpuset_limited = false;
260 int nid;
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800261
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700262 if (is_memcg_oom(oc)) {
Roman Gushchinbbec2e12018-06-07 17:06:18 -0700263 oc->totalpages = mem_cgroup_get_max(oc->memcg) ?: 1;
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700264 return CONSTRAINT_MEMCG;
265 }
266
David Rientjesa63d83f2010-08-09 17:19:46 -0700267 /* Default to all available memory */
Arun KSca79b0c2018-12-28 00:34:29 -0800268 oc->totalpages = totalram_pages() + total_swap_pages;
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700269
270 if (!IS_ENABLED(CONFIG_NUMA))
271 return CONSTRAINT_NONE;
David Rientjesa63d83f2010-08-09 17:19:46 -0700272
David Rientjes6e0fc462015-09-08 15:00:36 -0700273 if (!oc->zonelist)
David Rientjesa63d83f2010-08-09 17:19:46 -0700274 return CONSTRAINT_NONE;
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800275 /*
276 * Reach here only when __GFP_NOFAIL is used. So, we should avoid
277 * to kill current.We have to random task kill in this case.
278 * Hopefully, CONSTRAINT_THISNODE...but no way to handle it, now.
279 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700280 if (oc->gfp_mask & __GFP_THISNODE)
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800281 return CONSTRAINT_NONE;
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800282
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800283 /*
David Rientjesa63d83f2010-08-09 17:19:46 -0700284 * This is not a __GFP_THISNODE allocation, so a truncated nodemask in
285 * the page allocator means a mempolicy is in effect. Cpuset policy
286 * is enforced in get_page_from_freelist().
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800287 */
David Rientjes6e0fc462015-09-08 15:00:36 -0700288 if (oc->nodemask &&
289 !nodes_subset(node_states[N_MEMORY], *oc->nodemask)) {
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700290 oc->totalpages = total_swap_pages;
David Rientjes6e0fc462015-09-08 15:00:36 -0700291 for_each_node_mask(nid, *oc->nodemask)
Michal Hocko1eb41bb02019-09-23 15:37:16 -0700292 oc->totalpages += node_present_pages(nid);
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800293 return CONSTRAINT_MEMORY_POLICY;
David Rientjesa63d83f2010-08-09 17:19:46 -0700294 }
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800295
296 /* Check this allocation failure is caused by cpuset's wall function */
David Rientjes6e0fc462015-09-08 15:00:36 -0700297 for_each_zone_zonelist_nodemask(zone, z, oc->zonelist,
Joonsoo Kim97a225e2020-06-03 15:59:01 -0700298 highest_zoneidx, oc->nodemask)
David Rientjes6e0fc462015-09-08 15:00:36 -0700299 if (!cpuset_zone_allowed(zone, oc->gfp_mask))
David Rientjesa63d83f2010-08-09 17:19:46 -0700300 cpuset_limited = true;
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800301
David Rientjesa63d83f2010-08-09 17:19:46 -0700302 if (cpuset_limited) {
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700303 oc->totalpages = total_swap_pages;
David Rientjesa63d83f2010-08-09 17:19:46 -0700304 for_each_node_mask(nid, cpuset_current_mems_allowed)
Michal Hocko1eb41bb02019-09-23 15:37:16 -0700305 oc->totalpages += node_present_pages(nid);
David Rientjesa63d83f2010-08-09 17:19:46 -0700306 return CONSTRAINT_CPUSET;
307 }
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800308 return CONSTRAINT_NONE;
309}
310
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700311static int oom_evaluate_task(struct task_struct *task, void *arg)
David Rientjes462607e2012-07-31 16:43:40 -0700312{
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700313 struct oom_control *oc = arg;
Yafang Shao9066e5c2020-08-11 18:31:22 -0700314 long points;
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700315
Shakeel Buttac311a12019-07-11 21:00:31 -0700316 if (oom_unkillable_task(task))
317 goto next;
318
319 /* p may not have freeable memory in nodemask */
320 if (!is_memcg_oom(oc) && !oom_cpuset_eligible(task, oc))
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700321 goto next;
David Rientjes462607e2012-07-31 16:43:40 -0700322
323 /*
324 * This task already has access to memory reserves and is being killed.
Michal Hockoa3739662016-07-28 15:45:01 -0700325 * Don't allow any other task to have access to the reserves unless
Michal Hocko862e3072016-10-07 16:58:57 -0700326 * the task has MMF_OOM_SKIP because chances that it would release
Michal Hockoa3739662016-07-28 15:45:01 -0700327 * any memory is quite low.
David Rientjes462607e2012-07-31 16:43:40 -0700328 */
Michal Hocko862e3072016-10-07 16:58:57 -0700329 if (!is_sysrq_oom(oc) && tsk_is_oom_victim(task)) {
330 if (test_bit(MMF_OOM_SKIP, &task->signal->oom_mm->flags))
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700331 goto next;
332 goto abort;
Michal Hockoa3739662016-07-28 15:45:01 -0700333 }
David Rientjes462607e2012-07-31 16:43:40 -0700334
David Rientjese1e12d22012-12-11 16:02:56 -0800335 /*
336 * If task is allocating a lot of memory and has been marked to be
337 * killed first if it triggers an oom, then select it.
338 */
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700339 if (oom_task_origin(task)) {
Yafang Shao9066e5c2020-08-11 18:31:22 -0700340 points = LONG_MAX;
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700341 goto select;
342 }
David Rientjese1e12d22012-12-11 16:02:56 -0800343
Shakeel Buttac311a12019-07-11 21:00:31 -0700344 points = oom_badness(task, oc->totalpages);
Yafang Shao9066e5c2020-08-11 18:31:22 -0700345 if (points == LONG_MIN || points < oc->chosen_points)
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700346 goto next;
347
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700348select:
349 if (oc->chosen)
350 put_task_struct(oc->chosen);
351 get_task_struct(task);
352 oc->chosen = task;
353 oc->chosen_points = points;
354next:
355 return 0;
356abort:
357 if (oc->chosen)
358 put_task_struct(oc->chosen);
359 oc->chosen = (void *)-1UL;
360 return 1;
David Rientjes462607e2012-07-31 16:43:40 -0700361}
362
Christoph Lameter9b0f8b02006-02-20 18:27:52 -0800363/*
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700364 * Simple selection loop. We choose the process with the highest number of
365 * 'points'. In case scan was aborted, oc->chosen is set to -1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 */
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700367static void select_bad_process(struct oom_control *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Yafang Shao9066e5c2020-08-11 18:31:22 -0700369 oc->chosen_points = LONG_MIN;
370
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700371 if (is_memcg_oom(oc))
372 mem_cgroup_scan_tasks(oc->memcg, oom_evaluate_task, oc);
373 else {
374 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700376 rcu_read_lock();
377 for_each_process(p)
378 if (oom_evaluate_task(p, oc))
379 break;
380 rcu_read_unlock();
Oleg Nesterov1da4db02014-01-21 15:49:58 -0800381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700384static int dump_task(struct task_struct *p, void *arg)
385{
386 struct oom_control *oc = arg;
387 struct task_struct *task;
388
Shakeel Buttac311a12019-07-11 21:00:31 -0700389 if (oom_unkillable_task(p))
390 return 0;
391
392 /* p may not have freeable memory in nodemask */
393 if (!is_memcg_oom(oc) && !oom_cpuset_eligible(p, oc))
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700394 return 0;
395
396 task = find_lock_task_mm(p);
397 if (!task) {
398 /*
Tang Yizhouf8159c12021-02-24 12:09:50 -0800399 * All of p's threads have already detached their mm's. There's
400 * no need to report them; they can't be oom killed anyway.
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700401 */
402 return 0;
403 }
404
405 pr_info("[%7d] %5d %5d %8lu %8lu %8ld %8lu %5hd %s\n",
406 task->pid, from_kuid(&init_user_ns, task_uid(task)),
407 task->tgid, task->mm->total_vm, get_mm_rss(task->mm),
408 mm_pgtables_bytes(task->mm),
409 get_mm_counter(task->mm, MM_SWAPENTS),
410 task->signal->oom_score_adj, task->comm);
411 task_unlock(task);
412
413 return 0;
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/**
Randy Dunlap1b578df2008-03-19 17:00:42 -0700417 * dump_tasks - dump current memory state of all system tasks
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700418 * @oc: pointer to struct oom_control
Randy Dunlap1b578df2008-03-19 17:00:42 -0700419 *
David Rientjese85bfd32010-09-22 13:05:10 -0700420 * Dumps the current memory state of all eligible tasks. Tasks not in the same
421 * memcg, not in the same cpuset, or bound to a disjoint set of mempolicy nodes
422 * are not shown.
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -0800423 * State information includes task's pid, uid, tgid, vm size, rss,
424 * pgtables_bytes, swapents, oom_score_adj value, and name.
David Rientjesfef1bdd2008-02-07 00:14:07 -0800425 */
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700426static void dump_tasks(struct oom_control *oc)
David Rientjesfef1bdd2008-02-07 00:14:07 -0800427{
Rodrigo Freirec3b78b12018-08-21 21:52:41 -0700428 pr_info("Tasks state (memory values in pages):\n");
429 pr_info("[ pid ] uid tgid total_vm rss pgtables_bytes swapents oom_score_adj name\n");
David Rientjesfef1bdd2008-02-07 00:14:07 -0800430
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700431 if (is_memcg_oom(oc))
432 mem_cgroup_scan_tasks(oc->memcg, dump_task, oc);
433 else {
434 struct task_struct *p;
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700435
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700436 rcu_read_lock();
437 for_each_process(p)
438 dump_task(p, oc);
439 rcu_read_unlock();
KOSAKI Motohiroc55db952010-08-09 17:18:46 -0700440 }
David Rientjesfef1bdd2008-02-07 00:14:07 -0800441}
442
yuzhoujianef8444e2018-12-28 00:36:07 -0800443static void dump_oom_summary(struct oom_control *oc, struct task_struct *victim)
444{
445 /* one line summary of the oom killer context. */
446 pr_info("oom-kill:constraint=%s,nodemask=%*pbl",
447 oom_constraint_text[oc->constraint],
448 nodemask_pr_args(oc->nodemask));
449 cpuset_print_current_mems_allowed();
yuzhoujianf0c867d2018-12-28 00:36:10 -0800450 mem_cgroup_print_oom_context(oc->memcg, victim);
yuzhoujianef8444e2018-12-28 00:36:07 -0800451 pr_cont(",task=%s,pid=%d,uid=%d\n", victim->comm, victim->pid,
452 from_kuid(&init_user_ns, task_uid(victim)));
453}
454
Vladimir Davydov2a966b72016-07-26 15:22:33 -0700455static void dump_header(struct oom_control *oc, struct task_struct *p)
David Rientjes1b604d72009-12-14 17:57:47 -0800456{
yuzhoujianef8444e2018-12-28 00:36:07 -0800457 pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd\n",
458 current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order,
Michal Hocko0205f752017-11-15 17:39:14 -0800459 current->signal->oom_score_adj);
Michal Hocko92549902016-10-07 16:59:33 -0700460 if (!IS_ENABLED(CONFIG_COMPACTION) && oc->order)
461 pr_warn("COMPACTION is disabled!!!\n");
Vlastimil Babkaa0795cd2016-03-15 14:56:05 -0700462
David Rientjes1b604d72009-12-14 17:57:47 -0800463 dump_stack();
Yang Shi852d8be2017-11-15 17:32:07 -0800464 if (is_memcg_oom(oc))
yuzhoujianf0c867d2018-12-28 00:36:10 -0800465 mem_cgroup_print_oom_meminfo(oc->memcg);
Yang Shi852d8be2017-11-15 17:32:07 -0800466 else {
David Rientjes299c5172017-02-24 14:55:42 -0800467 show_mem(SHOW_MEM_FILTER_NODES, oc->nodemask);
Hui Su259b3632020-12-14 19:12:49 -0800468 if (should_dump_unreclaim_slab())
Yang Shi852d8be2017-11-15 17:32:07 -0800469 dump_unreclaimable_slab();
470 }
David Rientjes1b604d72009-12-14 17:57:47 -0800471 if (sysctl_oom_dump_tasks)
Shakeel Butt5eee7e12019-07-11 21:00:23 -0700472 dump_tasks(oc);
yuzhoujianef8444e2018-12-28 00:36:07 -0800473 if (p)
474 dump_oom_summary(oc, p);
David Rientjes1b604d72009-12-14 17:57:47 -0800475}
476
Michal Hocko5695be12014-10-20 18:12:32 +0200477/*
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800478 * Number of OOM victims in flight
Michal Hocko5695be12014-10-20 18:12:32 +0200479 */
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800480static atomic_t oom_victims = ATOMIC_INIT(0);
481static DECLARE_WAIT_QUEUE_HEAD(oom_victims_wait);
Michal Hocko5695be12014-10-20 18:12:32 +0200482
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700483static bool oom_killer_disabled __read_mostly;
Michal Hocko5695be12014-10-20 18:12:32 +0200484
Michal Hockobc448e82016-03-25 14:20:30 -0700485#define K(x) ((x) << (PAGE_SHIFT-10))
486
Michal Hocko3ef22df2016-05-19 17:13:12 -0700487/*
488 * task->mm can be NULL if the task is the exited group leader. So to
489 * determine whether the task is using a particular mm, we examine all the
490 * task's threads: if one of those is using this mm then this task was also
491 * using it.
492 */
Michal Hocko44a70ade2016-07-28 15:44:43 -0700493bool process_shares_mm(struct task_struct *p, struct mm_struct *mm)
Michal Hocko3ef22df2016-05-19 17:13:12 -0700494{
495 struct task_struct *t;
496
497 for_each_thread(p, t) {
498 struct mm_struct *t_mm = READ_ONCE(t->mm);
499 if (t_mm)
500 return t_mm == mm;
501 }
502 return false;
503}
504
Michal Hockoaac45362016-03-25 14:20:24 -0700505#ifdef CONFIG_MMU
506/*
507 * OOM Reaper kernel thread which tries to reap the memory used by the OOM
508 * victim (if that is possible) to help the OOM killer to move on.
509 */
510static struct task_struct *oom_reaper_th;
Michal Hockoaac45362016-03-25 14:20:24 -0700511static DECLARE_WAIT_QUEUE_HEAD(oom_reaper_wait);
Vladimir Davydov29c696e2016-03-25 14:20:39 -0700512static struct task_struct *oom_reaper_list;
Michal Hocko03049262016-03-25 14:20:33 -0700513static DEFINE_SPINLOCK(oom_reaper_lock);
514
Michal Hocko93065ac2018-08-21 21:52:33 -0700515bool __oom_reap_task_mm(struct mm_struct *mm)
Michal Hockoaac45362016-03-25 14:20:24 -0700516{
Michal Hockoaac45362016-03-25 14:20:24 -0700517 struct vm_area_struct *vma;
Michal Hocko93065ac2018-08-21 21:52:33 -0700518 bool ret = true;
David Rientjes27ae3572018-05-11 16:02:04 -0700519
520 /*
521 * Tell all users of get_user/copy_from_user etc... that the content
522 * is no longer stable. No barriers really needed because unmapping
523 * should imply barriers already and the reader would hit a page fault
524 * if it stumbled over a reaped memory.
525 */
526 set_bit(MMF_UNSTABLE, &mm->flags);
527
528 for (vma = mm->mmap ; vma; vma = vma->vm_next) {
Minchan Kim9c276cc2019-09-25 16:49:08 -0700529 if (!can_madv_lru_vma(vma))
David Rientjes27ae3572018-05-11 16:02:04 -0700530 continue;
531
532 /*
533 * Only anonymous pages have a good chance to be dropped
534 * without additional steps which we cannot afford as we
535 * are OOM already.
536 *
537 * We do not even care about fs backed pages because all
538 * which are reclaimable have already been reclaimed and
539 * we do not want to block exit_mmap by keeping mm ref
540 * count elevated without a good reason.
541 */
542 if (vma_is_anonymous(vma) || !(vma->vm_flags & VM_SHARED)) {
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800543 struct mmu_notifier_range range;
David Rientjes27ae3572018-05-11 16:02:04 -0700544 struct mmu_gather tlb;
545
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -0700546 mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0,
547 vma, mm, vma->vm_start,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800548 vma->vm_end);
Will Deacona72afd82021-01-27 23:53:45 +0000549 tlb_gather_mmu(&tlb, mm);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800550 if (mmu_notifier_invalidate_range_start_nonblock(&range)) {
Will Deaconae8eba82021-01-27 23:53:43 +0000551 tlb_finish_mmu(&tlb);
Michal Hocko93065ac2018-08-21 21:52:33 -0700552 ret = false;
553 continue;
554 }
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800555 unmap_page_range(&tlb, vma, range.start, range.end, NULL);
556 mmu_notifier_invalidate_range_end(&range);
Will Deaconae8eba82021-01-27 23:53:43 +0000557 tlb_finish_mmu(&tlb);
David Rientjes27ae3572018-05-11 16:02:04 -0700558 }
559 }
Michal Hocko93065ac2018-08-21 21:52:33 -0700560
561 return ret;
David Rientjes27ae3572018-05-11 16:02:04 -0700562}
563
Michal Hocko431f42fd2018-08-21 21:52:45 -0700564/*
565 * Reaps the address space of the give task.
566 *
567 * Returns true on success and false if none or part of the address space
568 * has been reclaimed and the caller should retry later.
569 */
David Rientjes27ae3572018-05-11 16:02:04 -0700570static bool oom_reap_task_mm(struct task_struct *tsk, struct mm_struct *mm)
571{
Michal Hockoaac45362016-03-25 14:20:24 -0700572 bool ret = true;
573
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700574 if (!mmap_read_trylock(mm)) {
Roman Gushchin422580c2017-07-10 15:49:05 -0700575 trace_skip_task_reaping(tsk->pid);
Michal Hockoaf5679f2018-08-21 21:52:37 -0700576 return false;
Michal Hocko4d4bbd82017-10-03 16:14:50 -0700577 }
578
579 /*
Andrea Arcangeli21292582017-09-06 16:25:00 -0700580 * MMF_OOM_SKIP is set by exit_mmap when the OOM reaper can't
581 * work on the mm anymore. The check for MMF_OOM_SKIP must run
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -0700582 * under mmap_lock for reading because it serializes against the
583 * mmap_write_lock();mmap_write_unlock() cycle in exit_mmap().
Michal Hockoe5e3f4c2016-07-26 15:24:50 -0700584 */
Andrea Arcangeli21292582017-09-06 16:25:00 -0700585 if (test_bit(MMF_OOM_SKIP, &mm->flags)) {
Roman Gushchin422580c2017-07-10 15:49:05 -0700586 trace_skip_task_reaping(tsk->pid);
Michal Hocko431f42fd2018-08-21 21:52:45 -0700587 goto out_unlock;
Michal Hockoaac45362016-03-25 14:20:24 -0700588 }
589
Roman Gushchin422580c2017-07-10 15:49:05 -0700590 trace_start_task_reaping(tsk->pid);
591
Michal Hocko93065ac2018-08-21 21:52:33 -0700592 /* failed to reap part of the address space. Try again later */
Michal Hocko431f42fd2018-08-21 21:52:45 -0700593 ret = __oom_reap_task_mm(mm);
594 if (!ret)
595 goto out_finish;
Michal Hocko3f70dc32016-10-07 16:59:06 -0700596
Michal Hockobc448e82016-03-25 14:20:30 -0700597 pr_info("oom_reaper: reaped process %d (%s), now anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB\n",
598 task_pid_nr(tsk), tsk->comm,
599 K(get_mm_counter(mm, MM_ANONPAGES)),
600 K(get_mm_counter(mm, MM_FILEPAGES)),
601 K(get_mm_counter(mm, MM_SHMEMPAGES)));
Michal Hocko431f42fd2018-08-21 21:52:45 -0700602out_finish:
603 trace_finish_task_reaping(tsk->pid);
604out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700605 mmap_read_unlock(mm);
Michal Hocko36324a92016-03-25 14:20:27 -0700606
Michal Hockoaac45362016-03-25 14:20:24 -0700607 return ret;
608}
609
Michal Hockobc448e82016-03-25 14:20:30 -0700610#define MAX_OOM_REAP_RETRIES 10
Michal Hocko36324a92016-03-25 14:20:27 -0700611static void oom_reap_task(struct task_struct *tsk)
Michal Hockoaac45362016-03-25 14:20:24 -0700612{
613 int attempts = 0;
Michal Hocko26db62f2016-10-07 16:58:51 -0700614 struct mm_struct *mm = tsk->signal->oom_mm;
Michal Hockoaac45362016-03-25 14:20:24 -0700615
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -0700616 /* Retry the mmap_read_trylock(mm) a few times */
David Rientjes27ae3572018-05-11 16:02:04 -0700617 while (attempts++ < MAX_OOM_REAP_RETRIES && !oom_reap_task_mm(tsk, mm))
Michal Hockoaac45362016-03-25 14:20:24 -0700618 schedule_timeout_idle(HZ/10);
619
Tetsuo Handa97b12552018-04-05 16:25:45 -0700620 if (attempts <= MAX_OOM_REAP_RETRIES ||
621 test_bit(MMF_OOM_SKIP, &mm->flags))
Tetsuo Handa7ebffa42016-10-07 16:58:45 -0700622 goto done;
Michal Hocko11a410d2016-07-28 15:44:58 -0700623
Tetsuo Handa7ebffa42016-10-07 16:58:45 -0700624 pr_info("oom_reaper: unable to reap pid:%d (%s)\n",
625 task_pid_nr(tsk), tsk->comm);
David Rientjes8a7ff022020-01-30 22:14:26 -0800626 sched_show_task(tsk);
Tetsuo Handa7ebffa42016-10-07 16:58:45 -0700627 debug_show_all_locks();
Michal Hockobc448e82016-03-25 14:20:30 -0700628
Tetsuo Handa7ebffa42016-10-07 16:58:45 -0700629done:
Michal Hocko449d7772016-05-19 17:13:15 -0700630 tsk->oom_reaper_list = NULL;
Michal Hocko449d7772016-05-19 17:13:15 -0700631
Michal Hocko26db62f2016-10-07 16:58:51 -0700632 /*
633 * Hide this mm from OOM killer because it has been either reaped or
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -0700634 * somebody can't call mmap_write_unlock(mm).
Michal Hocko26db62f2016-10-07 16:58:51 -0700635 */
Michal Hocko862e3072016-10-07 16:58:57 -0700636 set_bit(MMF_OOM_SKIP, &mm->flags);
Michal Hocko26db62f2016-10-07 16:58:51 -0700637
Michal Hockoaac45362016-03-25 14:20:24 -0700638 /* Drop a reference taken by wake_oom_reaper */
Michal Hocko36324a92016-03-25 14:20:27 -0700639 put_task_struct(tsk);
Michal Hockoaac45362016-03-25 14:20:24 -0700640}
641
642static int oom_reaper(void *unused)
643{
Sultan Alsawaf37239292021-11-05 13:43:25 -0700644 set_freezable();
645
Michal Hockoaac45362016-03-25 14:20:24 -0700646 while (true) {
Michal Hocko03049262016-03-25 14:20:33 -0700647 struct task_struct *tsk = NULL;
Michal Hockoaac45362016-03-25 14:20:24 -0700648
Vladimir Davydov29c696e2016-03-25 14:20:39 -0700649 wait_event_freezable(oom_reaper_wait, oom_reaper_list != NULL);
Michal Hocko03049262016-03-25 14:20:33 -0700650 spin_lock(&oom_reaper_lock);
Vladimir Davydov29c696e2016-03-25 14:20:39 -0700651 if (oom_reaper_list != NULL) {
652 tsk = oom_reaper_list;
653 oom_reaper_list = tsk->oom_reaper_list;
Michal Hocko03049262016-03-25 14:20:33 -0700654 }
655 spin_unlock(&oom_reaper_lock);
656
657 if (tsk)
658 oom_reap_task(tsk);
Michal Hockoaac45362016-03-25 14:20:24 -0700659 }
660
661 return 0;
662}
663
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700664static void wake_oom_reaper(struct task_struct *tsk)
Michal Hockoaac45362016-03-25 14:20:24 -0700665{
Tetsuo Handa9bcdeb52019-02-01 14:20:31 -0800666 /* mm is already queued? */
667 if (test_and_set_bit(MMF_OOM_REAP_QUEUED, &tsk->signal->oom_mm->flags))
Michal Hockoaac45362016-03-25 14:20:24 -0700668 return;
669
Michal Hocko36324a92016-03-25 14:20:27 -0700670 get_task_struct(tsk);
Michal Hockoaac45362016-03-25 14:20:24 -0700671
Michal Hocko03049262016-03-25 14:20:33 -0700672 spin_lock(&oom_reaper_lock);
Vladimir Davydov29c696e2016-03-25 14:20:39 -0700673 tsk->oom_reaper_list = oom_reaper_list;
674 oom_reaper_list = tsk;
Michal Hocko03049262016-03-25 14:20:33 -0700675 spin_unlock(&oom_reaper_lock);
Roman Gushchin422580c2017-07-10 15:49:05 -0700676 trace_wake_reaper(tsk->pid);
Michal Hocko03049262016-03-25 14:20:33 -0700677 wake_up(&oom_reaper_wait);
Michal Hockoaac45362016-03-25 14:20:24 -0700678}
679
680static int __init oom_init(void)
681{
682 oom_reaper_th = kthread_run(oom_reaper, NULL, "oom_reaper");
Michal Hockoaac45362016-03-25 14:20:24 -0700683 return 0;
684}
685subsys_initcall(oom_init)
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700686#else
687static inline void wake_oom_reaper(struct task_struct *tsk)
688{
689}
690#endif /* CONFIG_MMU */
Michal Hockoaac45362016-03-25 14:20:24 -0700691
Michal Hocko49550b62015-02-11 15:26:12 -0800692/**
Johannes Weiner16e95192015-06-24 16:57:07 -0700693 * mark_oom_victim - mark the given task as OOM victim
Michal Hocko49550b62015-02-11 15:26:12 -0800694 * @tsk: task to mark
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800695 *
Johannes Weinerdc564012015-06-24 16:57:19 -0700696 * Has to be called with oom_lock held and never after
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800697 * oom has been disabled already.
Michal Hocko26db62f2016-10-07 16:58:51 -0700698 *
699 * tsk->mm has to be non NULL and caller has to guarantee it is stable (either
700 * under task_lock or operate on the current).
Michal Hocko49550b62015-02-11 15:26:12 -0800701 */
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700702static void mark_oom_victim(struct task_struct *tsk)
Michal Hocko49550b62015-02-11 15:26:12 -0800703{
Michal Hocko26db62f2016-10-07 16:58:51 -0700704 struct mm_struct *mm = tsk->mm;
705
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800706 WARN_ON(oom_killer_disabled);
707 /* OOM killer might race with memcg OOM */
708 if (test_and_set_tsk_thread_flag(tsk, TIF_MEMDIE))
709 return;
Michal Hocko26db62f2016-10-07 16:58:51 -0700710
Michal Hocko26db62f2016-10-07 16:58:51 -0700711 /* oom_mm is bound to the signal struct life time. */
Michal Hocko4837fe32017-12-14 15:33:15 -0800712 if (!cmpxchg(&tsk->signal->oom_mm, NULL, mm)) {
Vegard Nossumf1f10072017-02-27 14:30:07 -0800713 mmgrab(tsk->signal->oom_mm);
Michal Hocko4837fe32017-12-14 15:33:15 -0800714 set_bit(MMF_OOM_VICTIM, &mm->flags);
715 }
Michal Hocko26db62f2016-10-07 16:58:51 -0700716
Michal Hocko63a8ca92015-02-11 15:26:15 -0800717 /*
718 * Make sure that the task is woken up from uninterruptible sleep
719 * if it is frozen because OOM killer wouldn't be able to free
720 * any memory and livelock. freezing_slow_path will tell the freezer
721 * that TIF_MEMDIE tasks should be ignored.
722 */
723 __thaw_task(tsk);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800724 atomic_inc(&oom_victims);
Roman Gushchin422580c2017-07-10 15:49:05 -0700725 trace_mark_victim(tsk->pid);
Michal Hocko49550b62015-02-11 15:26:12 -0800726}
727
728/**
Johannes Weiner16e95192015-06-24 16:57:07 -0700729 * exit_oom_victim - note the exit of an OOM victim
Michal Hocko49550b62015-02-11 15:26:12 -0800730 */
Tetsuo Handa38531202016-10-07 16:59:03 -0700731void exit_oom_victim(void)
Michal Hocko49550b62015-02-11 15:26:12 -0800732{
Tetsuo Handa38531202016-10-07 16:59:03 -0700733 clear_thread_flag(TIF_MEMDIE);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800734
Johannes Weinerc38f1022015-06-24 16:57:13 -0700735 if (!atomic_dec_return(&oom_victims))
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800736 wake_up_all(&oom_victims_wait);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800737}
738
739/**
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700740 * oom_killer_enable - enable OOM killer
741 */
742void oom_killer_enable(void)
743{
744 oom_killer_disabled = false;
Michal Hockod75da002017-05-03 14:54:57 -0700745 pr_info("OOM killer enabled.\n");
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700746}
747
748/**
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800749 * oom_killer_disable - disable OOM killer
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700750 * @timeout: maximum timeout to wait for oom victims in jiffies
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800751 *
752 * Forces all page allocations to fail rather than trigger OOM killer.
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700753 * Will block and wait until all OOM victims are killed or the given
754 * timeout expires.
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800755 *
756 * The function cannot be called when there are runnable user tasks because
757 * the userspace would see unexpected allocation failures as a result. Any
758 * new usage of this function should be consulted with MM people.
759 *
760 * Returns true if successful and false if the OOM killer cannot be
761 * disabled.
762 */
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700763bool oom_killer_disable(signed long timeout)
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800764{
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700765 signed long ret;
766
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800767 /*
Tetsuo Handa6afcf282016-03-17 14:20:45 -0700768 * Make sure to not race with an ongoing OOM killer. Check that the
769 * current is not killed (possibly due to sharing the victim's memory).
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800770 */
Tetsuo Handa6afcf282016-03-17 14:20:45 -0700771 if (mutex_lock_killable(&oom_lock))
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800772 return false;
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800773 oom_killer_disabled = true;
Johannes Weinerdc564012015-06-24 16:57:19 -0700774 mutex_unlock(&oom_lock);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800775
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700776 ret = wait_event_interruptible_timeout(oom_victims_wait,
777 !atomic_read(&oom_victims), timeout);
778 if (ret <= 0) {
779 oom_killer_enable();
780 return false;
781 }
Michal Hockod75da002017-05-03 14:54:57 -0700782 pr_info("OOM killer disabled.\n");
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800783
784 return true;
785}
786
Michal Hocko1af8bb42016-07-28 15:44:52 -0700787static inline bool __task_will_free_mem(struct task_struct *task)
788{
789 struct signal_struct *sig = task->signal;
790
791 /*
Eric W. Biedermand67e03e2021-09-01 11:23:38 -0500792 * A coredumping process may sleep for an extended period in
Eric W. Biederman92307382021-09-01 11:33:50 -0500793 * coredump_task_exit(), so the oom killer cannot assume that
Eric W. Biedermand67e03e2021-09-01 11:23:38 -0500794 * the process will promptly exit and release memory.
Michal Hocko1af8bb42016-07-28 15:44:52 -0700795 */
Eric W. Biederman98b24b12021-11-19 11:29:48 -0600796 if (sig->core_state)
Michal Hocko1af8bb42016-07-28 15:44:52 -0700797 return false;
798
799 if (sig->flags & SIGNAL_GROUP_EXIT)
800 return true;
801
802 if (thread_group_empty(task) && (task->flags & PF_EXITING))
803 return true;
804
805 return false;
806}
807
808/*
809 * Checks whether the given task is dying or exiting and likely to
810 * release its address space. This means that all threads and processes
811 * sharing the same mm have to be killed or exiting.
Michal Hocko091f3622016-07-28 15:45:04 -0700812 * Caller has to make sure that task->mm is stable (hold task_lock or
813 * it operates on the current).
Michal Hocko1af8bb42016-07-28 15:44:52 -0700814 */
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700815static bool task_will_free_mem(struct task_struct *task)
Michal Hocko1af8bb42016-07-28 15:44:52 -0700816{
Michal Hocko091f3622016-07-28 15:45:04 -0700817 struct mm_struct *mm = task->mm;
Michal Hocko1af8bb42016-07-28 15:44:52 -0700818 struct task_struct *p;
Geert Uytterhoevenf33e6f02016-08-11 15:33:09 -0700819 bool ret = true;
Michal Hocko1af8bb42016-07-28 15:44:52 -0700820
Michal Hocko091f3622016-07-28 15:45:04 -0700821 /*
822 * Skip tasks without mm because it might have passed its exit_mm and
823 * exit_oom_victim. oom_reaper could have rescued that but do not rely
824 * on that for now. We can consider find_lock_task_mm in future.
825 */
826 if (!mm)
827 return false;
828
Michal Hocko1af8bb42016-07-28 15:44:52 -0700829 if (!__task_will_free_mem(task))
830 return false;
831
832 /*
Michal Hocko696453e2016-07-28 15:44:55 -0700833 * This task has already been drained by the oom reaper so there are
834 * only small chances it will free some more
835 */
Michal Hocko862e3072016-10-07 16:58:57 -0700836 if (test_bit(MMF_OOM_SKIP, &mm->flags))
Michal Hocko696453e2016-07-28 15:44:55 -0700837 return false;
Michal Hocko696453e2016-07-28 15:44:55 -0700838
Michal Hocko091f3622016-07-28 15:45:04 -0700839 if (atomic_read(&mm->mm_users) <= 1)
Michal Hocko1af8bb42016-07-28 15:44:52 -0700840 return true;
Michal Hocko1af8bb42016-07-28 15:44:52 -0700841
842 /*
Michal Hocko5870c2e2016-10-07 16:57:32 -0700843 * Make sure that all tasks which share the mm with the given tasks
844 * are dying as well to make sure that a) nobody pins its mm and
845 * b) the task is also reapable by the oom reaper.
Michal Hocko1af8bb42016-07-28 15:44:52 -0700846 */
847 rcu_read_lock();
848 for_each_process(p) {
849 if (!process_shares_mm(p, mm))
850 continue;
851 if (same_thread_group(task, p))
852 continue;
853 ret = __task_will_free_mem(p);
854 if (!ret)
855 break;
856 }
857 rcu_read_unlock();
Michal Hocko1af8bb42016-07-28 15:44:52 -0700858
859 return ret;
860}
861
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800862static void __oom_kill_process(struct task_struct *victim, const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Roman Gushchin5989ad72018-08-21 21:53:50 -0700864 struct task_struct *p;
David Rientjes647f2bd2012-03-21 16:33:46 -0700865 struct mm_struct *mm;
Tetsuo Handabb299022016-03-25 14:20:44 -0700866 bool can_oom_reap = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
David Rientjes6b0c81b2012-07-31 16:43:45 -0700868 p = find_lock_task_mm(victim);
869 if (!p) {
Yafang Shao619b5b42020-08-11 18:31:32 -0700870 pr_info("%s: OOM victim %d (%s) is already exiting. Skip killing the task\n",
871 message, task_pid_nr(victim), victim->comm);
David Rientjes6b0c81b2012-07-31 16:43:45 -0700872 put_task_struct(victim);
David Rientjes647f2bd2012-03-21 16:33:46 -0700873 return;
David Rientjes6b0c81b2012-07-31 16:43:45 -0700874 } else if (victim != p) {
875 get_task_struct(p);
876 put_task_struct(victim);
877 victim = p;
878 }
David Rientjes647f2bd2012-03-21 16:33:46 -0700879
Tetsuo Handa880b76892015-11-05 18:47:51 -0800880 /* Get a reference to safely compare mm after task_unlock(victim) */
David Rientjes647f2bd2012-03-21 16:33:46 -0700881 mm = victim->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -0800882 mmgrab(mm);
Konstantin Khlebnikov8e675f72017-07-06 15:40:28 -0700883
884 /* Raise event before sending signal: task reaper must see this */
885 count_vm_event(OOM_KILL);
Roman Gushchinfe6bdfc2018-06-14 15:28:05 -0700886 memcg_memory_event_mm(mm, MEMCG_OOM_KILL);
Konstantin Khlebnikov8e675f72017-07-06 15:40:28 -0700887
Tetsuo Handa426fb5e2015-11-05 18:47:44 -0800888 /*
Michal Hockocd04ae12017-09-06 16:24:50 -0700889 * We should send SIGKILL before granting access to memory reserves
890 * in order to prevent the OOM victim from depleting the memory
891 * reserves from the user space under its control.
Tetsuo Handa426fb5e2015-11-05 18:47:44 -0800892 */
Eric W. Biederman079b22d2018-09-03 10:32:52 +0200893 do_send_sig_info(SIGKILL, SEND_SIG_PRIV, victim, PIDTYPE_TGID);
Johannes Weiner16e95192015-06-24 16:57:07 -0700894 mark_oom_victim(victim);
Edward Chron70cb6d22019-09-23 15:37:11 -0700895 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",
896 message, task_pid_nr(victim), victim->comm, K(mm->total_vm),
897 K(get_mm_counter(mm, MM_ANONPAGES)),
898 K(get_mm_counter(mm, MM_FILEPAGES)),
899 K(get_mm_counter(mm, MM_SHMEMPAGES)),
900 from_kuid(&init_user_ns, task_uid(victim)),
Ilya Dryomov941f7622020-01-04 13:00:09 -0800901 mm_pgtables_bytes(mm) >> 10, victim->signal->oom_score_adj);
David Rientjes647f2bd2012-03-21 16:33:46 -0700902 task_unlock(victim);
903
904 /*
905 * Kill all user processes sharing victim->mm in other thread groups, if
906 * any. They don't get access to memory reserves, though, to avoid
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700907 * depletion of all memory. This prevents mm->mmap_lock livelock when an
David Rientjes647f2bd2012-03-21 16:33:46 -0700908 * oom killed thread cannot exit because it requires the semaphore and
909 * its contended by another thread trying to allocate memory itself.
910 * That thread will now get access to memory reserves since it has a
911 * pending fatal signal.
912 */
Oleg Nesterov4d4048b2014-01-21 15:50:01 -0800913 rcu_read_lock();
Oleg Nesterovc3190252015-11-05 18:48:23 -0800914 for_each_process(p) {
Oleg Nesterov4d7b3392015-11-05 18:48:26 -0800915 if (!process_shares_mm(p, mm))
Oleg Nesterovc3190252015-11-05 18:48:23 -0800916 continue;
917 if (same_thread_group(p, victim))
918 continue;
Michal Hocko1b51e652016-10-07 16:59:09 -0700919 if (is_global_init(p)) {
Michal Hockoaac45362016-03-25 14:20:24 -0700920 can_oom_reap = false;
Michal Hocko862e3072016-10-07 16:58:57 -0700921 set_bit(MMF_OOM_SKIP, &mm->flags);
Michal Hockoa3739662016-07-28 15:45:01 -0700922 pr_info("oom killer %d (%s) has mm pinned by %d (%s)\n",
923 task_pid_nr(victim), victim->comm,
924 task_pid_nr(p), p->comm);
Oleg Nesterovc3190252015-11-05 18:48:23 -0800925 continue;
Michal Hockoaac45362016-03-25 14:20:24 -0700926 }
Michal Hocko1b51e652016-10-07 16:59:09 -0700927 /*
Rolf Eike Beer4c9c3802021-03-17 10:24:51 +0100928 * No kthread_use_mm() user needs to read from the userspace so
Christoph Hellwigf5678e72020-06-10 18:42:06 -0700929 * we are ok to reap it.
Michal Hocko1b51e652016-10-07 16:59:09 -0700930 */
931 if (unlikely(p->flags & PF_KTHREAD))
932 continue;
Eric W. Biederman079b22d2018-09-03 10:32:52 +0200933 do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID);
Oleg Nesterovc3190252015-11-05 18:48:23 -0800934 }
David Rientjes6b0c81b2012-07-31 16:43:45 -0700935 rcu_read_unlock();
David Rientjes647f2bd2012-03-21 16:33:46 -0700936
Michal Hockoaac45362016-03-25 14:20:24 -0700937 if (can_oom_reap)
Michal Hocko36324a92016-03-25 14:20:27 -0700938 wake_oom_reaper(victim);
Michal Hockoaac45362016-03-25 14:20:24 -0700939
Tetsuo Handa880b76892015-11-05 18:47:51 -0800940 mmdrop(mm);
David Rientjes6b0c81b2012-07-31 16:43:45 -0700941 put_task_struct(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
David Rientjes647f2bd2012-03-21 16:33:46 -0700943#undef K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
David Rientjes309ed882010-08-09 17:18:54 -0700945/*
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700946 * Kill provided task unless it's secured by setting
947 * oom_score_adj to OOM_SCORE_ADJ_MIN.
948 */
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800949static int oom_kill_memcg_member(struct task_struct *task, void *message)
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700950{
Tetsuo Handad342a0b2019-03-05 15:48:22 -0800951 if (task->signal->oom_score_adj != OOM_SCORE_ADJ_MIN &&
952 !is_global_init(task)) {
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700953 get_task_struct(task);
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800954 __oom_kill_process(task, message);
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700955 }
956 return 0;
957}
958
Roman Gushchin5989ad72018-08-21 21:53:50 -0700959static void oom_kill_process(struct oom_control *oc, const char *message)
960{
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800961 struct task_struct *victim = oc->chosen;
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700962 struct mem_cgroup *oom_group;
Roman Gushchin5989ad72018-08-21 21:53:50 -0700963 static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL,
964 DEFAULT_RATELIMIT_BURST);
965
966 /*
967 * If the task is already exiting, don't alarm the sysadmin or kill
968 * its children or threads, just give it access to memory reserves
969 * so it can die quickly
970 */
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800971 task_lock(victim);
972 if (task_will_free_mem(victim)) {
973 mark_oom_victim(victim);
974 wake_oom_reaper(victim);
975 task_unlock(victim);
976 put_task_struct(victim);
Roman Gushchin5989ad72018-08-21 21:53:50 -0700977 return;
978 }
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800979 task_unlock(victim);
Roman Gushchin5989ad72018-08-21 21:53:50 -0700980
981 if (__ratelimit(&oom_rs))
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800982 dump_header(oc, victim);
Roman Gushchin5989ad72018-08-21 21:53:50 -0700983
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700984 /*
985 * Do we need to kill the entire memory cgroup?
986 * Or even one of the ancestor memory cgroups?
987 * Check this out before killing the victim task.
988 */
989 oom_group = mem_cgroup_get_oom_group(victim, oc->memcg);
990
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800991 __oom_kill_process(victim, message);
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700992
993 /*
994 * If necessary, kill all tasks in the selected memory cgroup.
995 */
996 if (oom_group) {
Dan Schatzbergb6bf9ab2022-01-14 14:05:35 -0800997 memcg_memory_event(oom_group, MEMCG_OOM_GROUP_KILL);
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700998 mem_cgroup_print_oom_group(oom_group);
Shakeel Buttbbbe4802019-03-05 15:46:12 -0800999 mem_cgroup_scan_tasks(oom_group, oom_kill_memcg_member,
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07001000 (void *)message);
Roman Gushchin3d8b38e2018-08-21 21:53:54 -07001001 mem_cgroup_put(oom_group);
1002 }
Roman Gushchin5989ad72018-08-21 21:53:50 -07001003}
1004
David Rientjes309ed882010-08-09 17:18:54 -07001005/*
1006 * Determines whether the kernel must panic because of the panic_on_oom sysctl.
1007 */
Yafang Shao432b1de2019-06-28 12:06:59 -07001008static void check_panic_on_oom(struct oom_control *oc)
David Rientjes309ed882010-08-09 17:18:54 -07001009{
1010 if (likely(!sysctl_panic_on_oom))
1011 return;
1012 if (sysctl_panic_on_oom != 2) {
1013 /*
1014 * panic_on_oom == 1 only affects CONSTRAINT_NONE, the kernel
1015 * does not panic for cpuset, mempolicy, or memcg allocation
1016 * failures.
1017 */
Yafang Shao432b1de2019-06-28 12:06:59 -07001018 if (oc->constraint != CONSTRAINT_NONE)
David Rientjes309ed882010-08-09 17:18:54 -07001019 return;
1020 }
David Rientjes071a4be2015-09-08 15:00:42 -07001021 /* Do not panic for oom kills triggered by sysrq */
Yaowei Baidb2a0dd2015-11-06 16:28:06 -08001022 if (is_sysrq_oom(oc))
David Rientjes071a4be2015-09-08 15:00:42 -07001023 return;
Vladimir Davydov2a966b72016-07-26 15:22:33 -07001024 dump_header(oc, NULL);
David Rientjes309ed882010-08-09 17:18:54 -07001025 panic("Out of memory: %s panic_on_oom is enabled\n",
1026 sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide");
1027}
1028
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -07001029static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
1030
1031int register_oom_notifier(struct notifier_block *nb)
1032{
1033 return blocking_notifier_chain_register(&oom_notify_list, nb);
1034}
1035EXPORT_SYMBOL_GPL(register_oom_notifier);
1036
1037int unregister_oom_notifier(struct notifier_block *nb)
1038{
1039 return blocking_notifier_chain_unregister(&oom_notify_list, nb);
1040}
1041EXPORT_SYMBOL_GPL(unregister_oom_notifier);
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/**
David Rientjes6e0fc462015-09-08 15:00:36 -07001044 * out_of_memory - kill the "best" process when we run out of memory
1045 * @oc: pointer to struct oom_control
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 *
1047 * If we run out of memory, we have the choice between either
1048 * killing a random task (bad), letting the system crash (worse)
1049 * OR try to be smart about which process to kill. Note that we
1050 * don't have to be perfect here, we just have to be good.
1051 */
David Rientjes6e0fc462015-09-08 15:00:36 -07001052bool out_of_memory(struct oom_control *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -07001054 unsigned long freed = 0;
1055
Johannes Weinerdc564012015-06-24 16:57:19 -07001056 if (oom_killer_disabled)
1057 return false;
1058
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001059 if (!is_memcg_oom(oc)) {
1060 blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
Jann Hornf5302432022-01-14 14:08:27 -08001061 if (freed > 0 && !is_sysrq_oom(oc))
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001062 /* Got some memory back in the last second. */
1063 return true;
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
David Rientjes7b98c2e2010-08-09 17:18:48 -07001066 /*
David Rientjes9ff48682012-12-11 16:01:30 -08001067 * If current has a pending SIGKILL or is exiting, then automatically
1068 * select it. The goal is to allow it to allocate so that it may
1069 * quickly exit and free its memory.
David Rientjes7b98c2e2010-08-09 17:18:48 -07001070 */
Michal Hocko091f3622016-07-28 15:45:04 -07001071 if (task_will_free_mem(current)) {
Johannes Weiner16e95192015-06-24 16:57:07 -07001072 mark_oom_victim(current);
Michal Hocko1af8bb42016-07-28 15:44:52 -07001073 wake_oom_reaper(current);
David Rientjes75e8f8b2015-09-08 15:00:47 -07001074 return true;
David Rientjes7b98c2e2010-08-09 17:18:48 -07001075 }
1076
Christoph Lameter9b0f8b02006-02-20 18:27:52 -08001077 /*
Michal Hocko3da88fb32016-05-19 17:13:09 -07001078 * The OOM killer does not compensate for IO-less reclaim.
1079 * pagefault_out_of_memory lost its gfp context so we have to
1080 * make sure exclude 0 mask - all other users should have at least
Tetsuo Handaf9c64562019-09-23 15:37:08 -07001081 * ___GFP_DIRECT_RECLAIM to get here. But mem_cgroup_oom() has to
1082 * invoke the OOM killer even if it is a GFP_NOFS allocation.
Michal Hocko3da88fb32016-05-19 17:13:09 -07001083 */
Tetsuo Handaf9c64562019-09-23 15:37:08 -07001084 if (oc->gfp_mask && !(oc->gfp_mask & __GFP_FS) && !is_memcg_oom(oc))
Michal Hocko3da88fb32016-05-19 17:13:09 -07001085 return true;
1086
1087 /*
Christoph Lameter9b0f8b02006-02-20 18:27:52 -08001088 * Check if there were limitations on the allocation (only relevant for
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001089 * NUMA and memcg) that may require different handling.
Christoph Lameter9b0f8b02006-02-20 18:27:52 -08001090 */
Yafang Shao432b1de2019-06-28 12:06:59 -07001091 oc->constraint = constrained_alloc(oc);
1092 if (oc->constraint != CONSTRAINT_MEMORY_POLICY)
David Rientjes6e0fc462015-09-08 15:00:36 -07001093 oc->nodemask = NULL;
Yafang Shao432b1de2019-06-28 12:06:59 -07001094 check_panic_on_oom(oc);
David Rientjes0aad4b32010-08-09 17:18:59 -07001095
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001096 if (!is_memcg_oom(oc) && sysctl_oom_kill_allocating_task &&
Shakeel Buttac311a12019-07-11 21:00:31 -07001097 current->mm && !oom_unkillable_task(current) &&
1098 oom_cpuset_eligible(current, oc) &&
David Rientjes121d1ba2012-07-31 16:42:55 -07001099 current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {
David Rientjes6b0c81b2012-07-31 16:43:45 -07001100 get_task_struct(current);
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001101 oc->chosen = current;
1102 oom_kill_process(oc, "Out of memory (oom_kill_allocating_task)");
David Rientjes75e8f8b2015-09-08 15:00:47 -07001103 return true;
David Rientjes0aad4b32010-08-09 17:18:59 -07001104 }
1105
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001106 select_bad_process(oc);
Johannes Weiner3100dab2018-09-04 15:45:34 -07001107 /* Found nothing?!?! */
1108 if (!oc->chosen) {
Vladimir Davydov2a966b72016-07-26 15:22:33 -07001109 dump_header(oc, NULL);
Johannes Weiner3100dab2018-09-04 15:45:34 -07001110 pr_warn("Out of memory and no killable processes...\n");
1111 /*
1112 * If we got here due to an actual allocation at the
1113 * system level, we cannot survive this and will enter
1114 * an endless loop in the allocator. Bail out now.
1115 */
1116 if (!is_sysrq_oom(oc) && !is_memcg_oom(oc))
1117 panic("System is deadlocked on memory\n");
David Rientjes0aad4b32010-08-09 17:18:59 -07001118 }
Michal Hocko9bfe5de2018-08-17 15:49:04 -07001119 if (oc->chosen && oc->chosen != (void *)-1UL)
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001120 oom_kill_process(oc, !is_memcg_oom(oc) ? "Out of memory" :
1121 "Memory cgroup out of memory");
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001122 return !!oc->chosen;
Michal Hockoc32b3cb2015-02-11 15:26:24 -08001123}
1124
David Rientjese3658932010-08-09 17:18:55 -07001125/*
Michal Hocko60e27932021-11-05 13:38:06 -07001126 * The pagefault handler calls here because some allocation has failed. We have
1127 * to take care of the memcg OOM here because this is the only safe context without
1128 * any locks held but let the oom killer triggered from the allocation context care
1129 * about the global OOM.
David Rientjese3658932010-08-09 17:18:55 -07001130 */
1131void pagefault_out_of_memory(void)
1132{
Michal Hocko60e27932021-11-05 13:38:06 -07001133 static DEFINE_RATELIMIT_STATE(pfoom_rs, DEFAULT_RATELIMIT_INTERVAL,
1134 DEFAULT_RATELIMIT_BURST);
David Rientjes6e0fc462015-09-08 15:00:36 -07001135
Johannes Weiner49426422013-10-16 13:46:59 -07001136 if (mem_cgroup_oom_synchronize(true))
Johannes Weinerdc564012015-06-24 16:57:19 -07001137 return;
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001138
Vasily Averin0b281792021-11-05 13:38:02 -07001139 if (fatal_signal_pending(current))
Johannes Weinerdc564012015-06-24 16:57:19 -07001140 return;
Vasily Averin0b281792021-11-05 13:38:02 -07001141
Michal Hocko60e27932021-11-05 13:38:06 -07001142 if (__ratelimit(&pfoom_rs))
1143 pr_warn("Huh VM_FAULT_OOM leaked out to the #PF handler. Retrying PF\n");
David Rientjese3658932010-08-09 17:18:55 -07001144}
Suren Baghdasaryan884a7e52021-09-02 15:00:29 -07001145
1146SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
1147{
1148#ifdef CONFIG_MMU
1149 struct mm_struct *mm = NULL;
1150 struct task_struct *task;
1151 struct task_struct *p;
1152 unsigned int f_flags;
Suren Baghdasaryan337546e2021-10-28 14:36:14 -07001153 bool reap = false;
Suren Baghdasaryan884a7e52021-09-02 15:00:29 -07001154 long ret = 0;
1155
1156 if (flags)
1157 return -EINVAL;
1158
Christian Brauneree9955d2021-10-11 15:32:45 +02001159 task = pidfd_get_task(pidfd, &f_flags);
1160 if (IS_ERR(task))
1161 return PTR_ERR(task);
Suren Baghdasaryan884a7e52021-09-02 15:00:29 -07001162
1163 /*
1164 * Make sure to choose a thread which still has a reference to mm
1165 * during the group exit
1166 */
1167 p = find_lock_task_mm(task);
1168 if (!p) {
1169 ret = -ESRCH;
1170 goto put_task;
1171 }
1172
Suren Baghdasaryanba535c12022-01-14 14:06:22 -08001173 mm = p->mm;
1174 mmgrab(mm);
1175
1176 if (task_will_free_mem(p))
1177 reap = true;
1178 else {
1179 /* Error only if the work has not been done already */
1180 if (!test_bit(MMF_OOM_SKIP, &mm->flags))
1181 ret = -EINVAL;
Suren Baghdasaryan884a7e52021-09-02 15:00:29 -07001182 }
1183 task_unlock(p);
1184
1185 if (!reap)
1186 goto drop_mm;
1187
1188 if (mmap_read_lock_killable(mm)) {
1189 ret = -EINTR;
1190 goto drop_mm;
1191 }
Suren Baghdasaryanba535c12022-01-14 14:06:22 -08001192 /*
1193 * Check MMF_OOM_SKIP again under mmap_read_lock protection to ensure
1194 * possible change in exit_mmap is seen
1195 */
1196 if (!test_bit(MMF_OOM_SKIP, &mm->flags) && !__oom_reap_task_mm(mm))
Suren Baghdasaryan884a7e52021-09-02 15:00:29 -07001197 ret = -EAGAIN;
1198 mmap_read_unlock(mm);
1199
1200drop_mm:
Suren Baghdasaryanba535c12022-01-14 14:06:22 -08001201 mmdrop(mm);
Suren Baghdasaryan884a7e52021-09-02 15:00:29 -07001202put_task:
1203 put_task_struct(task);
Suren Baghdasaryan884a7e52021-09-02 15:00:29 -07001204 return ret;
1205#else
1206 return -ENOSYS;
1207#endif /* CONFIG_MMU */
1208}