18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/mm/oom_kill.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 1998,2000 Rik van Riel 68c2ecf20Sopenharmony_ci * Thanks go out to Claus Fischer for some serious inspiration and 78c2ecf20Sopenharmony_ci * for goading me into coding this file... 88c2ecf20Sopenharmony_ci * Copyright (C) 2010 Google, Inc. 98c2ecf20Sopenharmony_ci * Rewritten by David Rientjes 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * The routines in this file are used to kill a process when 128c2ecf20Sopenharmony_ci * we're seriously out of memory. This gets called from __alloc_pages() 138c2ecf20Sopenharmony_ci * in mm/page_alloc.c when we really run out of memory. 148c2ecf20Sopenharmony_ci * 158c2ecf20Sopenharmony_ci * Since we won't call these routines often (on a well-configured 168c2ecf20Sopenharmony_ci * machine) this file will double as a 'coding guide' and a signpost 178c2ecf20Sopenharmony_ci * for newbie kernel hackers. It features several pointers to major 188c2ecf20Sopenharmony_ci * kernel subsystems and hints as to where to find out what things do. 198c2ecf20Sopenharmony_ci */ 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#include <linux/oom.h> 228c2ecf20Sopenharmony_ci#include <linux/mm.h> 238c2ecf20Sopenharmony_ci#include <linux/err.h> 248c2ecf20Sopenharmony_ci#include <linux/gfp.h> 258c2ecf20Sopenharmony_ci#include <linux/sched.h> 268c2ecf20Sopenharmony_ci#include <linux/sched/mm.h> 278c2ecf20Sopenharmony_ci#include <linux/sched/coredump.h> 288c2ecf20Sopenharmony_ci#include <linux/sched/task.h> 298c2ecf20Sopenharmony_ci#include <linux/sched/debug.h> 308c2ecf20Sopenharmony_ci#include <linux/swap.h> 318c2ecf20Sopenharmony_ci#include <linux/timex.h> 328c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 338c2ecf20Sopenharmony_ci#include <linux/cpuset.h> 348c2ecf20Sopenharmony_ci#include <linux/export.h> 358c2ecf20Sopenharmony_ci#include <linux/notifier.h> 368c2ecf20Sopenharmony_ci#include <linux/memcontrol.h> 378c2ecf20Sopenharmony_ci#include <linux/mempolicy.h> 388c2ecf20Sopenharmony_ci#include <linux/security.h> 398c2ecf20Sopenharmony_ci#include <linux/ptrace.h> 408c2ecf20Sopenharmony_ci#include <linux/freezer.h> 418c2ecf20Sopenharmony_ci#include <linux/ftrace.h> 428c2ecf20Sopenharmony_ci#include <linux/ratelimit.h> 438c2ecf20Sopenharmony_ci#include <linux/kthread.h> 448c2ecf20Sopenharmony_ci#include <linux/init.h> 458c2ecf20Sopenharmony_ci#include <linux/mmu_notifier.h> 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci#include <asm/tlb.h> 488c2ecf20Sopenharmony_ci#include "internal.h" 498c2ecf20Sopenharmony_ci#include "slab.h" 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci#define CREATE_TRACE_POINTS 528c2ecf20Sopenharmony_ci#include <trace/events/oom.h> 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ciint sysctl_panic_on_oom; 558c2ecf20Sopenharmony_ciint sysctl_oom_kill_allocating_task; 568c2ecf20Sopenharmony_ciint sysctl_oom_dump_tasks = 1; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci/* 598c2ecf20Sopenharmony_ci * Serializes oom killer invocations (out_of_memory()) from all contexts to 608c2ecf20Sopenharmony_ci * prevent from over eager oom killing (e.g. when the oom killer is invoked 618c2ecf20Sopenharmony_ci * from different domains). 628c2ecf20Sopenharmony_ci * 638c2ecf20Sopenharmony_ci * oom_killer_disable() relies on this lock to stabilize oom_killer_disabled 648c2ecf20Sopenharmony_ci * and mark_oom_victim 658c2ecf20Sopenharmony_ci */ 668c2ecf20Sopenharmony_ciDEFINE_MUTEX(oom_lock); 678c2ecf20Sopenharmony_ci/* Serializes oom_score_adj and oom_score_adj_min updates */ 688c2ecf20Sopenharmony_ciDEFINE_MUTEX(oom_adj_mutex); 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cistatic inline bool is_memcg_oom(struct oom_control *oc) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci return oc->memcg != NULL; 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci#ifdef CONFIG_NUMA 768c2ecf20Sopenharmony_ci/** 778c2ecf20Sopenharmony_ci * oom_cpuset_eligible() - check task eligiblity for kill 788c2ecf20Sopenharmony_ci * @start: task struct of which task to consider 798c2ecf20Sopenharmony_ci * @oc: pointer to struct oom_control 808c2ecf20Sopenharmony_ci * 818c2ecf20Sopenharmony_ci * Task eligibility is determined by whether or not a candidate task, @tsk, 828c2ecf20Sopenharmony_ci * shares the same mempolicy nodes as current if it is bound by such a policy 838c2ecf20Sopenharmony_ci * and whether or not it has the same set of allowed cpuset nodes. 848c2ecf20Sopenharmony_ci * 858c2ecf20Sopenharmony_ci * This function is assuming oom-killer context and 'current' has triggered 868c2ecf20Sopenharmony_ci * the oom-killer. 878c2ecf20Sopenharmony_ci */ 888c2ecf20Sopenharmony_cistatic bool oom_cpuset_eligible(struct task_struct *start, 898c2ecf20Sopenharmony_ci struct oom_control *oc) 908c2ecf20Sopenharmony_ci{ 918c2ecf20Sopenharmony_ci struct task_struct *tsk; 928c2ecf20Sopenharmony_ci bool ret = false; 938c2ecf20Sopenharmony_ci const nodemask_t *mask = oc->nodemask; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci if (is_memcg_oom(oc)) 968c2ecf20Sopenharmony_ci return true; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci rcu_read_lock(); 998c2ecf20Sopenharmony_ci for_each_thread(start, tsk) { 1008c2ecf20Sopenharmony_ci if (mask) { 1018c2ecf20Sopenharmony_ci /* 1028c2ecf20Sopenharmony_ci * If this is a mempolicy constrained oom, tsk's 1038c2ecf20Sopenharmony_ci * cpuset is irrelevant. Only return true if its 1048c2ecf20Sopenharmony_ci * mempolicy intersects current, otherwise it may be 1058c2ecf20Sopenharmony_ci * needlessly killed. 1068c2ecf20Sopenharmony_ci */ 1078c2ecf20Sopenharmony_ci ret = mempolicy_nodemask_intersects(tsk, mask); 1088c2ecf20Sopenharmony_ci } else { 1098c2ecf20Sopenharmony_ci /* 1108c2ecf20Sopenharmony_ci * This is not a mempolicy constrained oom, so only 1118c2ecf20Sopenharmony_ci * check the mems of tsk's cpuset. 1128c2ecf20Sopenharmony_ci */ 1138c2ecf20Sopenharmony_ci ret = cpuset_mems_allowed_intersects(current, tsk); 1148c2ecf20Sopenharmony_ci } 1158c2ecf20Sopenharmony_ci if (ret) 1168c2ecf20Sopenharmony_ci break; 1178c2ecf20Sopenharmony_ci } 1188c2ecf20Sopenharmony_ci rcu_read_unlock(); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci return ret; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci#else 1238c2ecf20Sopenharmony_cistatic bool oom_cpuset_eligible(struct task_struct *tsk, struct oom_control *oc) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci return true; 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ci#endif /* CONFIG_NUMA */ 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci/* 1308c2ecf20Sopenharmony_ci * The process p may have detached its own ->mm while exiting or through 1318c2ecf20Sopenharmony_ci * kthread_use_mm(), but one or more of its subthreads may still have a valid 1328c2ecf20Sopenharmony_ci * pointer. Return p, or any of its subthreads with a valid ->mm, with 1338c2ecf20Sopenharmony_ci * task_lock() held. 1348c2ecf20Sopenharmony_ci */ 1358c2ecf20Sopenharmony_cistruct task_struct *find_lock_task_mm(struct task_struct *p) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci struct task_struct *t; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci rcu_read_lock(); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci for_each_thread(p, t) { 1428c2ecf20Sopenharmony_ci task_lock(t); 1438c2ecf20Sopenharmony_ci if (likely(t->mm)) 1448c2ecf20Sopenharmony_ci goto found; 1458c2ecf20Sopenharmony_ci task_unlock(t); 1468c2ecf20Sopenharmony_ci } 1478c2ecf20Sopenharmony_ci t = NULL; 1488c2ecf20Sopenharmony_cifound: 1498c2ecf20Sopenharmony_ci rcu_read_unlock(); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci return t; 1528c2ecf20Sopenharmony_ci} 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci/* 1558c2ecf20Sopenharmony_ci * order == -1 means the oom kill is required by sysrq, otherwise only 1568c2ecf20Sopenharmony_ci * for display purposes. 1578c2ecf20Sopenharmony_ci */ 1588c2ecf20Sopenharmony_cistatic inline bool is_sysrq_oom(struct oom_control *oc) 1598c2ecf20Sopenharmony_ci{ 1608c2ecf20Sopenharmony_ci return oc->order == -1; 1618c2ecf20Sopenharmony_ci} 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci/* return true if the task is not adequate as candidate victim task. */ 1648c2ecf20Sopenharmony_cistatic bool oom_unkillable_task(struct task_struct *p) 1658c2ecf20Sopenharmony_ci{ 1668c2ecf20Sopenharmony_ci if (is_global_init(p)) 1678c2ecf20Sopenharmony_ci return true; 1688c2ecf20Sopenharmony_ci if (p->flags & PF_KTHREAD) 1698c2ecf20Sopenharmony_ci return true; 1708c2ecf20Sopenharmony_ci return false; 1718c2ecf20Sopenharmony_ci} 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci/* 1748c2ecf20Sopenharmony_ci * Print out unreclaimble slabs info when unreclaimable slabs amount is greater 1758c2ecf20Sopenharmony_ci * than all user memory (LRU pages) 1768c2ecf20Sopenharmony_ci */ 1778c2ecf20Sopenharmony_cistatic bool is_dump_unreclaim_slabs(void) 1788c2ecf20Sopenharmony_ci{ 1798c2ecf20Sopenharmony_ci unsigned long nr_lru; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci nr_lru = global_node_page_state(NR_ACTIVE_ANON) + 1828c2ecf20Sopenharmony_ci global_node_page_state(NR_INACTIVE_ANON) + 1838c2ecf20Sopenharmony_ci global_node_page_state(NR_ACTIVE_FILE) + 1848c2ecf20Sopenharmony_ci global_node_page_state(NR_INACTIVE_FILE) + 1858c2ecf20Sopenharmony_ci global_node_page_state(NR_ISOLATED_ANON) + 1868c2ecf20Sopenharmony_ci global_node_page_state(NR_ISOLATED_FILE) + 1878c2ecf20Sopenharmony_ci global_node_page_state(NR_UNEVICTABLE); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci return (global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B) > nr_lru); 1908c2ecf20Sopenharmony_ci} 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci/** 1938c2ecf20Sopenharmony_ci * oom_badness - heuristic function to determine which candidate task to kill 1948c2ecf20Sopenharmony_ci * @p: task struct of which task we should calculate 1958c2ecf20Sopenharmony_ci * @totalpages: total present RAM allowed for page allocation 1968c2ecf20Sopenharmony_ci * 1978c2ecf20Sopenharmony_ci * The heuristic for determining which task to kill is made to be as simple and 1988c2ecf20Sopenharmony_ci * predictable as possible. The goal is to return the highest value for the 1998c2ecf20Sopenharmony_ci * task consuming the most memory to avoid subsequent oom failures. 2008c2ecf20Sopenharmony_ci */ 2018c2ecf20Sopenharmony_cilong oom_badness(struct task_struct *p, unsigned long totalpages) 2028c2ecf20Sopenharmony_ci{ 2038c2ecf20Sopenharmony_ci long points; 2048c2ecf20Sopenharmony_ci long adj; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci if (oom_unkillable_task(p)) 2078c2ecf20Sopenharmony_ci return LONG_MIN; 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci p = find_lock_task_mm(p); 2108c2ecf20Sopenharmony_ci if (!p) 2118c2ecf20Sopenharmony_ci return LONG_MIN; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci /* 2148c2ecf20Sopenharmony_ci * Do not even consider tasks which are explicitly marked oom 2158c2ecf20Sopenharmony_ci * unkillable or have been already oom reaped or the are in 2168c2ecf20Sopenharmony_ci * the middle of vfork 2178c2ecf20Sopenharmony_ci */ 2188c2ecf20Sopenharmony_ci adj = (long)p->signal->oom_score_adj; 2198c2ecf20Sopenharmony_ci if (adj == OOM_SCORE_ADJ_MIN || 2208c2ecf20Sopenharmony_ci test_bit(MMF_OOM_SKIP, &p->mm->flags) || 2218c2ecf20Sopenharmony_ci in_vfork(p)) { 2228c2ecf20Sopenharmony_ci task_unlock(p); 2238c2ecf20Sopenharmony_ci return LONG_MIN; 2248c2ecf20Sopenharmony_ci } 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci /* 2278c2ecf20Sopenharmony_ci * The baseline for the badness score is the proportion of RAM that each 2288c2ecf20Sopenharmony_ci * task's rss, pagetable and swap space use. 2298c2ecf20Sopenharmony_ci */ 2308c2ecf20Sopenharmony_ci points = get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) + 2318c2ecf20Sopenharmony_ci mm_pgtables_bytes(p->mm) / PAGE_SIZE; 2328c2ecf20Sopenharmony_ci task_unlock(p); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci /* Normalize to oom_score_adj units */ 2358c2ecf20Sopenharmony_ci adj *= totalpages / 1000; 2368c2ecf20Sopenharmony_ci points += adj; 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci return points; 2398c2ecf20Sopenharmony_ci} 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_cistatic const char * const oom_constraint_text[] = { 2428c2ecf20Sopenharmony_ci [CONSTRAINT_NONE] = "CONSTRAINT_NONE", 2438c2ecf20Sopenharmony_ci [CONSTRAINT_CPUSET] = "CONSTRAINT_CPUSET", 2448c2ecf20Sopenharmony_ci [CONSTRAINT_MEMORY_POLICY] = "CONSTRAINT_MEMORY_POLICY", 2458c2ecf20Sopenharmony_ci [CONSTRAINT_MEMCG] = "CONSTRAINT_MEMCG", 2468c2ecf20Sopenharmony_ci}; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci/* 2498c2ecf20Sopenharmony_ci * Determine the type of allocation constraint. 2508c2ecf20Sopenharmony_ci */ 2518c2ecf20Sopenharmony_cistatic enum oom_constraint constrained_alloc(struct oom_control *oc) 2528c2ecf20Sopenharmony_ci{ 2538c2ecf20Sopenharmony_ci struct zone *zone; 2548c2ecf20Sopenharmony_ci struct zoneref *z; 2558c2ecf20Sopenharmony_ci enum zone_type highest_zoneidx = gfp_zone(oc->gfp_mask); 2568c2ecf20Sopenharmony_ci bool cpuset_limited = false; 2578c2ecf20Sopenharmony_ci int nid; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci if (is_memcg_oom(oc)) { 2608c2ecf20Sopenharmony_ci oc->totalpages = mem_cgroup_get_max(oc->memcg) ?: 1; 2618c2ecf20Sopenharmony_ci return CONSTRAINT_MEMCG; 2628c2ecf20Sopenharmony_ci } 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci /* Default to all available memory */ 2658c2ecf20Sopenharmony_ci oc->totalpages = totalram_pages() + total_swap_pages; 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci if (!IS_ENABLED(CONFIG_NUMA)) 2688c2ecf20Sopenharmony_ci return CONSTRAINT_NONE; 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci if (!oc->zonelist) 2718c2ecf20Sopenharmony_ci return CONSTRAINT_NONE; 2728c2ecf20Sopenharmony_ci /* 2738c2ecf20Sopenharmony_ci * Reach here only when __GFP_NOFAIL is used. So, we should avoid 2748c2ecf20Sopenharmony_ci * to kill current.We have to random task kill in this case. 2758c2ecf20Sopenharmony_ci * Hopefully, CONSTRAINT_THISNODE...but no way to handle it, now. 2768c2ecf20Sopenharmony_ci */ 2778c2ecf20Sopenharmony_ci if (oc->gfp_mask & __GFP_THISNODE) 2788c2ecf20Sopenharmony_ci return CONSTRAINT_NONE; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci /* 2818c2ecf20Sopenharmony_ci * This is not a __GFP_THISNODE allocation, so a truncated nodemask in 2828c2ecf20Sopenharmony_ci * the page allocator means a mempolicy is in effect. Cpuset policy 2838c2ecf20Sopenharmony_ci * is enforced in get_page_from_freelist(). 2848c2ecf20Sopenharmony_ci */ 2858c2ecf20Sopenharmony_ci if (oc->nodemask && 2868c2ecf20Sopenharmony_ci !nodes_subset(node_states[N_MEMORY], *oc->nodemask)) { 2878c2ecf20Sopenharmony_ci oc->totalpages = total_swap_pages; 2888c2ecf20Sopenharmony_ci for_each_node_mask(nid, *oc->nodemask) 2898c2ecf20Sopenharmony_ci oc->totalpages += node_present_pages(nid); 2908c2ecf20Sopenharmony_ci return CONSTRAINT_MEMORY_POLICY; 2918c2ecf20Sopenharmony_ci } 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci /* Check this allocation failure is caused by cpuset's wall function */ 2948c2ecf20Sopenharmony_ci for_each_zone_zonelist_nodemask(zone, z, oc->zonelist, 2958c2ecf20Sopenharmony_ci highest_zoneidx, oc->nodemask) 2968c2ecf20Sopenharmony_ci if (!cpuset_zone_allowed(zone, oc->gfp_mask)) 2978c2ecf20Sopenharmony_ci cpuset_limited = true; 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci if (cpuset_limited) { 3008c2ecf20Sopenharmony_ci oc->totalpages = total_swap_pages; 3018c2ecf20Sopenharmony_ci for_each_node_mask(nid, cpuset_current_mems_allowed) 3028c2ecf20Sopenharmony_ci oc->totalpages += node_present_pages(nid); 3038c2ecf20Sopenharmony_ci return CONSTRAINT_CPUSET; 3048c2ecf20Sopenharmony_ci } 3058c2ecf20Sopenharmony_ci return CONSTRAINT_NONE; 3068c2ecf20Sopenharmony_ci} 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_cistatic int oom_evaluate_task(struct task_struct *task, void *arg) 3098c2ecf20Sopenharmony_ci{ 3108c2ecf20Sopenharmony_ci struct oom_control *oc = arg; 3118c2ecf20Sopenharmony_ci long points; 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci if (oom_unkillable_task(task)) 3148c2ecf20Sopenharmony_ci goto next; 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ci /* p may not have freeable memory in nodemask */ 3178c2ecf20Sopenharmony_ci if (!is_memcg_oom(oc) && !oom_cpuset_eligible(task, oc)) 3188c2ecf20Sopenharmony_ci goto next; 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci /* 3218c2ecf20Sopenharmony_ci * This task already has access to memory reserves and is being killed. 3228c2ecf20Sopenharmony_ci * Don't allow any other task to have access to the reserves unless 3238c2ecf20Sopenharmony_ci * the task has MMF_OOM_SKIP because chances that it would release 3248c2ecf20Sopenharmony_ci * any memory is quite low. 3258c2ecf20Sopenharmony_ci */ 3268c2ecf20Sopenharmony_ci if (!is_sysrq_oom(oc) && tsk_is_oom_victim(task)) { 3278c2ecf20Sopenharmony_ci if (test_bit(MMF_OOM_SKIP, &task->signal->oom_mm->flags)) 3288c2ecf20Sopenharmony_ci goto next; 3298c2ecf20Sopenharmony_ci goto abort; 3308c2ecf20Sopenharmony_ci } 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci /* 3338c2ecf20Sopenharmony_ci * If task is allocating a lot of memory and has been marked to be 3348c2ecf20Sopenharmony_ci * killed first if it triggers an oom, then select it. 3358c2ecf20Sopenharmony_ci */ 3368c2ecf20Sopenharmony_ci if (oom_task_origin(task)) { 3378c2ecf20Sopenharmony_ci points = LONG_MAX; 3388c2ecf20Sopenharmony_ci goto select; 3398c2ecf20Sopenharmony_ci } 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci points = oom_badness(task, oc->totalpages); 3428c2ecf20Sopenharmony_ci if (points == LONG_MIN || points < oc->chosen_points) 3438c2ecf20Sopenharmony_ci goto next; 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ciselect: 3468c2ecf20Sopenharmony_ci if (oc->chosen) 3478c2ecf20Sopenharmony_ci put_task_struct(oc->chosen); 3488c2ecf20Sopenharmony_ci get_task_struct(task); 3498c2ecf20Sopenharmony_ci oc->chosen = task; 3508c2ecf20Sopenharmony_ci oc->chosen_points = points; 3518c2ecf20Sopenharmony_cinext: 3528c2ecf20Sopenharmony_ci return 0; 3538c2ecf20Sopenharmony_ciabort: 3548c2ecf20Sopenharmony_ci if (oc->chosen) 3558c2ecf20Sopenharmony_ci put_task_struct(oc->chosen); 3568c2ecf20Sopenharmony_ci oc->chosen = (void *)-1UL; 3578c2ecf20Sopenharmony_ci return 1; 3588c2ecf20Sopenharmony_ci} 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci/* 3618c2ecf20Sopenharmony_ci * Simple selection loop. We choose the process with the highest number of 3628c2ecf20Sopenharmony_ci * 'points'. In case scan was aborted, oc->chosen is set to -1. 3638c2ecf20Sopenharmony_ci */ 3648c2ecf20Sopenharmony_cistatic void select_bad_process(struct oom_control *oc) 3658c2ecf20Sopenharmony_ci{ 3668c2ecf20Sopenharmony_ci oc->chosen_points = LONG_MIN; 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci if (is_memcg_oom(oc)) 3698c2ecf20Sopenharmony_ci mem_cgroup_scan_tasks(oc->memcg, oom_evaluate_task, oc); 3708c2ecf20Sopenharmony_ci else { 3718c2ecf20Sopenharmony_ci struct task_struct *p; 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci rcu_read_lock(); 3748c2ecf20Sopenharmony_ci for_each_process(p) 3758c2ecf20Sopenharmony_ci if (oom_evaluate_task(p, oc)) 3768c2ecf20Sopenharmony_ci break; 3778c2ecf20Sopenharmony_ci rcu_read_unlock(); 3788c2ecf20Sopenharmony_ci } 3798c2ecf20Sopenharmony_ci} 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_cistatic int dump_task(struct task_struct *p, void *arg) 3828c2ecf20Sopenharmony_ci{ 3838c2ecf20Sopenharmony_ci struct oom_control *oc = arg; 3848c2ecf20Sopenharmony_ci struct task_struct *task; 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci if (oom_unkillable_task(p)) 3878c2ecf20Sopenharmony_ci return 0; 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci /* p may not have freeable memory in nodemask */ 3908c2ecf20Sopenharmony_ci if (!is_memcg_oom(oc) && !oom_cpuset_eligible(p, oc)) 3918c2ecf20Sopenharmony_ci return 0; 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci task = find_lock_task_mm(p); 3948c2ecf20Sopenharmony_ci if (!task) { 3958c2ecf20Sopenharmony_ci /* 3968c2ecf20Sopenharmony_ci * This is a kthread or all of p's threads have already 3978c2ecf20Sopenharmony_ci * detached their mm's. There's no need to report 3988c2ecf20Sopenharmony_ci * them; they can't be oom killed anyway. 3998c2ecf20Sopenharmony_ci */ 4008c2ecf20Sopenharmony_ci return 0; 4018c2ecf20Sopenharmony_ci } 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_ci pr_info("[%7d] %5d %5d %8lu %8lu %8ld %8lu %5hd %s\n", 4048c2ecf20Sopenharmony_ci task->pid, from_kuid(&init_user_ns, task_uid(task)), 4058c2ecf20Sopenharmony_ci task->tgid, task->mm->total_vm, get_mm_rss(task->mm), 4068c2ecf20Sopenharmony_ci mm_pgtables_bytes(task->mm), 4078c2ecf20Sopenharmony_ci get_mm_counter(task->mm, MM_SWAPENTS), 4088c2ecf20Sopenharmony_ci task->signal->oom_score_adj, task->comm); 4098c2ecf20Sopenharmony_ci task_unlock(task); 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci return 0; 4128c2ecf20Sopenharmony_ci} 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci/** 4158c2ecf20Sopenharmony_ci * dump_tasks - dump current memory state of all system tasks 4168c2ecf20Sopenharmony_ci * @oc: pointer to struct oom_control 4178c2ecf20Sopenharmony_ci * 4188c2ecf20Sopenharmony_ci * Dumps the current memory state of all eligible tasks. Tasks not in the same 4198c2ecf20Sopenharmony_ci * memcg, not in the same cpuset, or bound to a disjoint set of mempolicy nodes 4208c2ecf20Sopenharmony_ci * are not shown. 4218c2ecf20Sopenharmony_ci * State information includes task's pid, uid, tgid, vm size, rss, 4228c2ecf20Sopenharmony_ci * pgtables_bytes, swapents, oom_score_adj value, and name. 4238c2ecf20Sopenharmony_ci */ 4248c2ecf20Sopenharmony_cistatic void dump_tasks(struct oom_control *oc) 4258c2ecf20Sopenharmony_ci{ 4268c2ecf20Sopenharmony_ci pr_info("Tasks state (memory values in pages):\n"); 4278c2ecf20Sopenharmony_ci pr_info("[ pid ] uid tgid total_vm rss pgtables_bytes swapents oom_score_adj name\n"); 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci if (is_memcg_oom(oc)) 4308c2ecf20Sopenharmony_ci mem_cgroup_scan_tasks(oc->memcg, dump_task, oc); 4318c2ecf20Sopenharmony_ci else { 4328c2ecf20Sopenharmony_ci struct task_struct *p; 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci rcu_read_lock(); 4358c2ecf20Sopenharmony_ci for_each_process(p) 4368c2ecf20Sopenharmony_ci dump_task(p, oc); 4378c2ecf20Sopenharmony_ci rcu_read_unlock(); 4388c2ecf20Sopenharmony_ci } 4398c2ecf20Sopenharmony_ci} 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_cistatic void dump_oom_summary(struct oom_control *oc, struct task_struct *victim) 4428c2ecf20Sopenharmony_ci{ 4438c2ecf20Sopenharmony_ci /* one line summary of the oom killer context. */ 4448c2ecf20Sopenharmony_ci pr_info("oom-kill:constraint=%s,nodemask=%*pbl", 4458c2ecf20Sopenharmony_ci oom_constraint_text[oc->constraint], 4468c2ecf20Sopenharmony_ci nodemask_pr_args(oc->nodemask)); 4478c2ecf20Sopenharmony_ci cpuset_print_current_mems_allowed(); 4488c2ecf20Sopenharmony_ci mem_cgroup_print_oom_context(oc->memcg, victim); 4498c2ecf20Sopenharmony_ci pr_cont(",task=%s,pid=%d,uid=%d\n", victim->comm, victim->pid, 4508c2ecf20Sopenharmony_ci from_kuid(&init_user_ns, task_uid(victim))); 4518c2ecf20Sopenharmony_ci} 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_cistatic void dump_header(struct oom_control *oc, struct task_struct *p) 4548c2ecf20Sopenharmony_ci{ 4558c2ecf20Sopenharmony_ci pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd\n", 4568c2ecf20Sopenharmony_ci current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order, 4578c2ecf20Sopenharmony_ci current->signal->oom_score_adj); 4588c2ecf20Sopenharmony_ci if (!IS_ENABLED(CONFIG_COMPACTION) && oc->order) 4598c2ecf20Sopenharmony_ci pr_warn("COMPACTION is disabled!!!\n"); 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci dump_stack(); 4628c2ecf20Sopenharmony_ci if (is_memcg_oom(oc)) 4638c2ecf20Sopenharmony_ci mem_cgroup_print_oom_meminfo(oc->memcg); 4648c2ecf20Sopenharmony_ci else { 4658c2ecf20Sopenharmony_ci show_mem(SHOW_MEM_FILTER_NODES, oc->nodemask); 4668c2ecf20Sopenharmony_ci if (is_dump_unreclaim_slabs()) 4678c2ecf20Sopenharmony_ci dump_unreclaimable_slab(); 4688c2ecf20Sopenharmony_ci } 4698c2ecf20Sopenharmony_ci if (sysctl_oom_dump_tasks) 4708c2ecf20Sopenharmony_ci dump_tasks(oc); 4718c2ecf20Sopenharmony_ci if (p) 4728c2ecf20Sopenharmony_ci dump_oom_summary(oc, p); 4738c2ecf20Sopenharmony_ci} 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci/* 4768c2ecf20Sopenharmony_ci * Number of OOM victims in flight 4778c2ecf20Sopenharmony_ci */ 4788c2ecf20Sopenharmony_cistatic atomic_t oom_victims = ATOMIC_INIT(0); 4798c2ecf20Sopenharmony_cistatic DECLARE_WAIT_QUEUE_HEAD(oom_victims_wait); 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_cistatic bool oom_killer_disabled __read_mostly; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci#define K(x) ((x) << (PAGE_SHIFT-10)) 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci/* 4868c2ecf20Sopenharmony_ci * task->mm can be NULL if the task is the exited group leader. So to 4878c2ecf20Sopenharmony_ci * determine whether the task is using a particular mm, we examine all the 4888c2ecf20Sopenharmony_ci * task's threads: if one of those is using this mm then this task was also 4898c2ecf20Sopenharmony_ci * using it. 4908c2ecf20Sopenharmony_ci */ 4918c2ecf20Sopenharmony_cibool process_shares_mm(struct task_struct *p, struct mm_struct *mm) 4928c2ecf20Sopenharmony_ci{ 4938c2ecf20Sopenharmony_ci struct task_struct *t; 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci for_each_thread(p, t) { 4968c2ecf20Sopenharmony_ci struct mm_struct *t_mm = READ_ONCE(t->mm); 4978c2ecf20Sopenharmony_ci if (t_mm) 4988c2ecf20Sopenharmony_ci return t_mm == mm; 4998c2ecf20Sopenharmony_ci } 5008c2ecf20Sopenharmony_ci return false; 5018c2ecf20Sopenharmony_ci} 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci#ifdef CONFIG_MMU 5048c2ecf20Sopenharmony_ci/* 5058c2ecf20Sopenharmony_ci * OOM Reaper kernel thread which tries to reap the memory used by the OOM 5068c2ecf20Sopenharmony_ci * victim (if that is possible) to help the OOM killer to move on. 5078c2ecf20Sopenharmony_ci */ 5088c2ecf20Sopenharmony_cistatic struct task_struct *oom_reaper_th; 5098c2ecf20Sopenharmony_cistatic DECLARE_WAIT_QUEUE_HEAD(oom_reaper_wait); 5108c2ecf20Sopenharmony_cistatic struct task_struct *oom_reaper_list; 5118c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(oom_reaper_lock); 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_cibool __oom_reap_task_mm(struct mm_struct *mm) 5148c2ecf20Sopenharmony_ci{ 5158c2ecf20Sopenharmony_ci struct vm_area_struct *vma; 5168c2ecf20Sopenharmony_ci bool ret = true; 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci /* 5198c2ecf20Sopenharmony_ci * Tell all users of get_user/copy_from_user etc... that the content 5208c2ecf20Sopenharmony_ci * is no longer stable. No barriers really needed because unmapping 5218c2ecf20Sopenharmony_ci * should imply barriers already and the reader would hit a page fault 5228c2ecf20Sopenharmony_ci * if it stumbled over a reaped memory. 5238c2ecf20Sopenharmony_ci */ 5248c2ecf20Sopenharmony_ci set_bit(MMF_UNSTABLE, &mm->flags); 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci for (vma = mm->mmap ; vma; vma = vma->vm_next) { 5278c2ecf20Sopenharmony_ci if (!can_madv_lru_vma(vma)) 5288c2ecf20Sopenharmony_ci continue; 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_ci /* 5318c2ecf20Sopenharmony_ci * Only anonymous pages have a good chance to be dropped 5328c2ecf20Sopenharmony_ci * without additional steps which we cannot afford as we 5338c2ecf20Sopenharmony_ci * are OOM already. 5348c2ecf20Sopenharmony_ci * 5358c2ecf20Sopenharmony_ci * We do not even care about fs backed pages because all 5368c2ecf20Sopenharmony_ci * which are reclaimable have already been reclaimed and 5378c2ecf20Sopenharmony_ci * we do not want to block exit_mmap by keeping mm ref 5388c2ecf20Sopenharmony_ci * count elevated without a good reason. 5398c2ecf20Sopenharmony_ci */ 5408c2ecf20Sopenharmony_ci if (vma_is_anonymous(vma) || !(vma->vm_flags & VM_SHARED)) { 5418c2ecf20Sopenharmony_ci struct mmu_notifier_range range; 5428c2ecf20Sopenharmony_ci struct mmu_gather tlb; 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, 5458c2ecf20Sopenharmony_ci vma, mm, vma->vm_start, 5468c2ecf20Sopenharmony_ci vma->vm_end); 5478c2ecf20Sopenharmony_ci tlb_gather_mmu(&tlb, mm, range.start, range.end); 5488c2ecf20Sopenharmony_ci if (mmu_notifier_invalidate_range_start_nonblock(&range)) { 5498c2ecf20Sopenharmony_ci tlb_finish_mmu(&tlb, range.start, range.end); 5508c2ecf20Sopenharmony_ci ret = false; 5518c2ecf20Sopenharmony_ci continue; 5528c2ecf20Sopenharmony_ci } 5538c2ecf20Sopenharmony_ci unmap_page_range(&tlb, vma, range.start, range.end, NULL); 5548c2ecf20Sopenharmony_ci mmu_notifier_invalidate_range_end(&range); 5558c2ecf20Sopenharmony_ci tlb_finish_mmu(&tlb, range.start, range.end); 5568c2ecf20Sopenharmony_ci } 5578c2ecf20Sopenharmony_ci } 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci return ret; 5608c2ecf20Sopenharmony_ci} 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_ci/* 5638c2ecf20Sopenharmony_ci * Reaps the address space of the give task. 5648c2ecf20Sopenharmony_ci * 5658c2ecf20Sopenharmony_ci * Returns true on success and false if none or part of the address space 5668c2ecf20Sopenharmony_ci * has been reclaimed and the caller should retry later. 5678c2ecf20Sopenharmony_ci */ 5688c2ecf20Sopenharmony_cistatic bool oom_reap_task_mm(struct task_struct *tsk, struct mm_struct *mm) 5698c2ecf20Sopenharmony_ci{ 5708c2ecf20Sopenharmony_ci bool ret = true; 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_ci if (!mmap_read_trylock(mm)) { 5738c2ecf20Sopenharmony_ci trace_skip_task_reaping(tsk->pid); 5748c2ecf20Sopenharmony_ci return false; 5758c2ecf20Sopenharmony_ci } 5768c2ecf20Sopenharmony_ci 5778c2ecf20Sopenharmony_ci /* 5788c2ecf20Sopenharmony_ci * MMF_OOM_SKIP is set by exit_mmap when the OOM reaper can't 5798c2ecf20Sopenharmony_ci * work on the mm anymore. The check for MMF_OOM_SKIP must run 5808c2ecf20Sopenharmony_ci * under mmap_lock for reading because it serializes against the 5818c2ecf20Sopenharmony_ci * mmap_write_lock();mmap_write_unlock() cycle in exit_mmap(). 5828c2ecf20Sopenharmony_ci */ 5838c2ecf20Sopenharmony_ci if (test_bit(MMF_OOM_SKIP, &mm->flags)) { 5848c2ecf20Sopenharmony_ci trace_skip_task_reaping(tsk->pid); 5858c2ecf20Sopenharmony_ci goto out_unlock; 5868c2ecf20Sopenharmony_ci } 5878c2ecf20Sopenharmony_ci 5888c2ecf20Sopenharmony_ci trace_start_task_reaping(tsk->pid); 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_ci /* failed to reap part of the address space. Try again later */ 5918c2ecf20Sopenharmony_ci ret = __oom_reap_task_mm(mm); 5928c2ecf20Sopenharmony_ci if (!ret) 5938c2ecf20Sopenharmony_ci goto out_finish; 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci pr_info("oom_reaper: reaped process %d (%s), now anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB\n", 5968c2ecf20Sopenharmony_ci task_pid_nr(tsk), tsk->comm, 5978c2ecf20Sopenharmony_ci K(get_mm_counter(mm, MM_ANONPAGES)), 5988c2ecf20Sopenharmony_ci K(get_mm_counter(mm, MM_FILEPAGES)), 5998c2ecf20Sopenharmony_ci K(get_mm_counter(mm, MM_SHMEMPAGES))); 6008c2ecf20Sopenharmony_ciout_finish: 6018c2ecf20Sopenharmony_ci trace_finish_task_reaping(tsk->pid); 6028c2ecf20Sopenharmony_ciout_unlock: 6038c2ecf20Sopenharmony_ci mmap_read_unlock(mm); 6048c2ecf20Sopenharmony_ci 6058c2ecf20Sopenharmony_ci return ret; 6068c2ecf20Sopenharmony_ci} 6078c2ecf20Sopenharmony_ci 6088c2ecf20Sopenharmony_ci#define MAX_OOM_REAP_RETRIES 10 6098c2ecf20Sopenharmony_cistatic void oom_reap_task(struct task_struct *tsk) 6108c2ecf20Sopenharmony_ci{ 6118c2ecf20Sopenharmony_ci int attempts = 0; 6128c2ecf20Sopenharmony_ci struct mm_struct *mm = tsk->signal->oom_mm; 6138c2ecf20Sopenharmony_ci 6148c2ecf20Sopenharmony_ci /* Retry the mmap_read_trylock(mm) a few times */ 6158c2ecf20Sopenharmony_ci while (attempts++ < MAX_OOM_REAP_RETRIES && !oom_reap_task_mm(tsk, mm)) 6168c2ecf20Sopenharmony_ci schedule_timeout_idle(HZ/10); 6178c2ecf20Sopenharmony_ci 6188c2ecf20Sopenharmony_ci if (attempts <= MAX_OOM_REAP_RETRIES || 6198c2ecf20Sopenharmony_ci test_bit(MMF_OOM_SKIP, &mm->flags)) 6208c2ecf20Sopenharmony_ci goto done; 6218c2ecf20Sopenharmony_ci 6228c2ecf20Sopenharmony_ci pr_info("oom_reaper: unable to reap pid:%d (%s)\n", 6238c2ecf20Sopenharmony_ci task_pid_nr(tsk), tsk->comm); 6248c2ecf20Sopenharmony_ci sched_show_task(tsk); 6258c2ecf20Sopenharmony_ci debug_show_all_locks(); 6268c2ecf20Sopenharmony_ci 6278c2ecf20Sopenharmony_cidone: 6288c2ecf20Sopenharmony_ci tsk->oom_reaper_list = NULL; 6298c2ecf20Sopenharmony_ci 6308c2ecf20Sopenharmony_ci /* 6318c2ecf20Sopenharmony_ci * Hide this mm from OOM killer because it has been either reaped or 6328c2ecf20Sopenharmony_ci * somebody can't call mmap_write_unlock(mm). 6338c2ecf20Sopenharmony_ci */ 6348c2ecf20Sopenharmony_ci set_bit(MMF_OOM_SKIP, &mm->flags); 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_ci /* Drop a reference taken by queue_oom_reaper */ 6378c2ecf20Sopenharmony_ci put_task_struct(tsk); 6388c2ecf20Sopenharmony_ci} 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_cistatic int oom_reaper(void *unused) 6418c2ecf20Sopenharmony_ci{ 6428c2ecf20Sopenharmony_ci while (true) { 6438c2ecf20Sopenharmony_ci struct task_struct *tsk = NULL; 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_ci wait_event_freezable(oom_reaper_wait, oom_reaper_list != NULL); 6468c2ecf20Sopenharmony_ci spin_lock_irq(&oom_reaper_lock); 6478c2ecf20Sopenharmony_ci if (oom_reaper_list != NULL) { 6488c2ecf20Sopenharmony_ci tsk = oom_reaper_list; 6498c2ecf20Sopenharmony_ci oom_reaper_list = tsk->oom_reaper_list; 6508c2ecf20Sopenharmony_ci } 6518c2ecf20Sopenharmony_ci spin_unlock_irq(&oom_reaper_lock); 6528c2ecf20Sopenharmony_ci 6538c2ecf20Sopenharmony_ci if (tsk) 6548c2ecf20Sopenharmony_ci oom_reap_task(tsk); 6558c2ecf20Sopenharmony_ci } 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci return 0; 6588c2ecf20Sopenharmony_ci} 6598c2ecf20Sopenharmony_ci 6608c2ecf20Sopenharmony_cistatic void wake_oom_reaper(struct timer_list *timer) 6618c2ecf20Sopenharmony_ci{ 6628c2ecf20Sopenharmony_ci struct task_struct *tsk = container_of(timer, struct task_struct, 6638c2ecf20Sopenharmony_ci oom_reaper_timer); 6648c2ecf20Sopenharmony_ci struct mm_struct *mm = tsk->signal->oom_mm; 6658c2ecf20Sopenharmony_ci unsigned long flags; 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_ci /* The victim managed to terminate on its own - see exit_mmap */ 6688c2ecf20Sopenharmony_ci if (test_bit(MMF_OOM_SKIP, &mm->flags)) { 6698c2ecf20Sopenharmony_ci put_task_struct(tsk); 6708c2ecf20Sopenharmony_ci return; 6718c2ecf20Sopenharmony_ci } 6728c2ecf20Sopenharmony_ci 6738c2ecf20Sopenharmony_ci spin_lock_irqsave(&oom_reaper_lock, flags); 6748c2ecf20Sopenharmony_ci tsk->oom_reaper_list = oom_reaper_list; 6758c2ecf20Sopenharmony_ci oom_reaper_list = tsk; 6768c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&oom_reaper_lock, flags); 6778c2ecf20Sopenharmony_ci trace_wake_reaper(tsk->pid); 6788c2ecf20Sopenharmony_ci wake_up(&oom_reaper_wait); 6798c2ecf20Sopenharmony_ci} 6808c2ecf20Sopenharmony_ci 6818c2ecf20Sopenharmony_ci/* 6828c2ecf20Sopenharmony_ci * Give the OOM victim time to exit naturally before invoking the oom_reaping. 6838c2ecf20Sopenharmony_ci * The timers timeout is arbitrary... the longer it is, the longer the worst 6848c2ecf20Sopenharmony_ci * case scenario for the OOM can take. If it is too small, the oom_reaper can 6858c2ecf20Sopenharmony_ci * get in the way and release resources needed by the process exit path. 6868c2ecf20Sopenharmony_ci * e.g. The futex robust list can sit in Anon|Private memory that gets reaped 6878c2ecf20Sopenharmony_ci * before the exit path is able to wake the futex waiters. 6888c2ecf20Sopenharmony_ci */ 6898c2ecf20Sopenharmony_ci#define OOM_REAPER_DELAY (2*HZ) 6908c2ecf20Sopenharmony_cistatic void queue_oom_reaper(struct task_struct *tsk) 6918c2ecf20Sopenharmony_ci{ 6928c2ecf20Sopenharmony_ci /* mm is already queued? */ 6938c2ecf20Sopenharmony_ci if (test_and_set_bit(MMF_OOM_REAP_QUEUED, &tsk->signal->oom_mm->flags)) 6948c2ecf20Sopenharmony_ci return; 6958c2ecf20Sopenharmony_ci 6968c2ecf20Sopenharmony_ci get_task_struct(tsk); 6978c2ecf20Sopenharmony_ci timer_setup(&tsk->oom_reaper_timer, wake_oom_reaper, 0); 6988c2ecf20Sopenharmony_ci tsk->oom_reaper_timer.expires = jiffies + OOM_REAPER_DELAY; 6998c2ecf20Sopenharmony_ci add_timer(&tsk->oom_reaper_timer); 7008c2ecf20Sopenharmony_ci} 7018c2ecf20Sopenharmony_ci 7028c2ecf20Sopenharmony_cistatic int __init oom_init(void) 7038c2ecf20Sopenharmony_ci{ 7048c2ecf20Sopenharmony_ci oom_reaper_th = kthread_run(oom_reaper, NULL, "oom_reaper"); 7058c2ecf20Sopenharmony_ci return 0; 7068c2ecf20Sopenharmony_ci} 7078c2ecf20Sopenharmony_cisubsys_initcall(oom_init) 7088c2ecf20Sopenharmony_ci#else 7098c2ecf20Sopenharmony_cistatic inline void queue_oom_reaper(struct task_struct *tsk) 7108c2ecf20Sopenharmony_ci{ 7118c2ecf20Sopenharmony_ci} 7128c2ecf20Sopenharmony_ci#endif /* CONFIG_MMU */ 7138c2ecf20Sopenharmony_ci 7148c2ecf20Sopenharmony_ci/** 7158c2ecf20Sopenharmony_ci * mark_oom_victim - mark the given task as OOM victim 7168c2ecf20Sopenharmony_ci * @tsk: task to mark 7178c2ecf20Sopenharmony_ci * 7188c2ecf20Sopenharmony_ci * Has to be called with oom_lock held and never after 7198c2ecf20Sopenharmony_ci * oom has been disabled already. 7208c2ecf20Sopenharmony_ci * 7218c2ecf20Sopenharmony_ci * tsk->mm has to be non NULL and caller has to guarantee it is stable (either 7228c2ecf20Sopenharmony_ci * under task_lock or operate on the current). 7238c2ecf20Sopenharmony_ci */ 7248c2ecf20Sopenharmony_cistatic void mark_oom_victim(struct task_struct *tsk) 7258c2ecf20Sopenharmony_ci{ 7268c2ecf20Sopenharmony_ci struct mm_struct *mm = tsk->mm; 7278c2ecf20Sopenharmony_ci 7288c2ecf20Sopenharmony_ci WARN_ON(oom_killer_disabled); 7298c2ecf20Sopenharmony_ci /* OOM killer might race with memcg OOM */ 7308c2ecf20Sopenharmony_ci if (test_and_set_tsk_thread_flag(tsk, TIF_MEMDIE)) 7318c2ecf20Sopenharmony_ci return; 7328c2ecf20Sopenharmony_ci 7338c2ecf20Sopenharmony_ci /* oom_mm is bound to the signal struct life time. */ 7348c2ecf20Sopenharmony_ci if (!cmpxchg(&tsk->signal->oom_mm, NULL, mm)) { 7358c2ecf20Sopenharmony_ci mmgrab(tsk->signal->oom_mm); 7368c2ecf20Sopenharmony_ci set_bit(MMF_OOM_VICTIM, &mm->flags); 7378c2ecf20Sopenharmony_ci } 7388c2ecf20Sopenharmony_ci 7398c2ecf20Sopenharmony_ci /* 7408c2ecf20Sopenharmony_ci * Make sure that the task is woken up from uninterruptible sleep 7418c2ecf20Sopenharmony_ci * if it is frozen because OOM killer wouldn't be able to free 7428c2ecf20Sopenharmony_ci * any memory and livelock. freezing_slow_path will tell the freezer 7438c2ecf20Sopenharmony_ci * that TIF_MEMDIE tasks should be ignored. 7448c2ecf20Sopenharmony_ci */ 7458c2ecf20Sopenharmony_ci __thaw_task(tsk); 7468c2ecf20Sopenharmony_ci atomic_inc(&oom_victims); 7478c2ecf20Sopenharmony_ci trace_mark_victim(tsk->pid); 7488c2ecf20Sopenharmony_ci} 7498c2ecf20Sopenharmony_ci 7508c2ecf20Sopenharmony_ci/** 7518c2ecf20Sopenharmony_ci * exit_oom_victim - note the exit of an OOM victim 7528c2ecf20Sopenharmony_ci */ 7538c2ecf20Sopenharmony_civoid exit_oom_victim(void) 7548c2ecf20Sopenharmony_ci{ 7558c2ecf20Sopenharmony_ci clear_thread_flag(TIF_MEMDIE); 7568c2ecf20Sopenharmony_ci 7578c2ecf20Sopenharmony_ci if (!atomic_dec_return(&oom_victims)) 7588c2ecf20Sopenharmony_ci wake_up_all(&oom_victims_wait); 7598c2ecf20Sopenharmony_ci} 7608c2ecf20Sopenharmony_ci 7618c2ecf20Sopenharmony_ci/** 7628c2ecf20Sopenharmony_ci * oom_killer_enable - enable OOM killer 7638c2ecf20Sopenharmony_ci */ 7648c2ecf20Sopenharmony_civoid oom_killer_enable(void) 7658c2ecf20Sopenharmony_ci{ 7668c2ecf20Sopenharmony_ci oom_killer_disabled = false; 7678c2ecf20Sopenharmony_ci pr_info("OOM killer enabled.\n"); 7688c2ecf20Sopenharmony_ci} 7698c2ecf20Sopenharmony_ci 7708c2ecf20Sopenharmony_ci/** 7718c2ecf20Sopenharmony_ci * oom_killer_disable - disable OOM killer 7728c2ecf20Sopenharmony_ci * @timeout: maximum timeout to wait for oom victims in jiffies 7738c2ecf20Sopenharmony_ci * 7748c2ecf20Sopenharmony_ci * Forces all page allocations to fail rather than trigger OOM killer. 7758c2ecf20Sopenharmony_ci * Will block and wait until all OOM victims are killed or the given 7768c2ecf20Sopenharmony_ci * timeout expires. 7778c2ecf20Sopenharmony_ci * 7788c2ecf20Sopenharmony_ci * The function cannot be called when there are runnable user tasks because 7798c2ecf20Sopenharmony_ci * the userspace would see unexpected allocation failures as a result. Any 7808c2ecf20Sopenharmony_ci * new usage of this function should be consulted with MM people. 7818c2ecf20Sopenharmony_ci * 7828c2ecf20Sopenharmony_ci * Returns true if successful and false if the OOM killer cannot be 7838c2ecf20Sopenharmony_ci * disabled. 7848c2ecf20Sopenharmony_ci */ 7858c2ecf20Sopenharmony_cibool oom_killer_disable(signed long timeout) 7868c2ecf20Sopenharmony_ci{ 7878c2ecf20Sopenharmony_ci signed long ret; 7888c2ecf20Sopenharmony_ci 7898c2ecf20Sopenharmony_ci /* 7908c2ecf20Sopenharmony_ci * Make sure to not race with an ongoing OOM killer. Check that the 7918c2ecf20Sopenharmony_ci * current is not killed (possibly due to sharing the victim's memory). 7928c2ecf20Sopenharmony_ci */ 7938c2ecf20Sopenharmony_ci if (mutex_lock_killable(&oom_lock)) 7948c2ecf20Sopenharmony_ci return false; 7958c2ecf20Sopenharmony_ci oom_killer_disabled = true; 7968c2ecf20Sopenharmony_ci mutex_unlock(&oom_lock); 7978c2ecf20Sopenharmony_ci 7988c2ecf20Sopenharmony_ci ret = wait_event_interruptible_timeout(oom_victims_wait, 7998c2ecf20Sopenharmony_ci !atomic_read(&oom_victims), timeout); 8008c2ecf20Sopenharmony_ci if (ret <= 0) { 8018c2ecf20Sopenharmony_ci oom_killer_enable(); 8028c2ecf20Sopenharmony_ci return false; 8038c2ecf20Sopenharmony_ci } 8048c2ecf20Sopenharmony_ci pr_info("OOM killer disabled.\n"); 8058c2ecf20Sopenharmony_ci 8068c2ecf20Sopenharmony_ci return true; 8078c2ecf20Sopenharmony_ci} 8088c2ecf20Sopenharmony_ci 8098c2ecf20Sopenharmony_cistatic inline bool __task_will_free_mem(struct task_struct *task) 8108c2ecf20Sopenharmony_ci{ 8118c2ecf20Sopenharmony_ci struct signal_struct *sig = task->signal; 8128c2ecf20Sopenharmony_ci 8138c2ecf20Sopenharmony_ci /* 8148c2ecf20Sopenharmony_ci * A coredumping process may sleep for an extended period in exit_mm(), 8158c2ecf20Sopenharmony_ci * so the oom killer cannot assume that the process will promptly exit 8168c2ecf20Sopenharmony_ci * and release memory. 8178c2ecf20Sopenharmony_ci */ 8188c2ecf20Sopenharmony_ci if (sig->flags & SIGNAL_GROUP_COREDUMP) 8198c2ecf20Sopenharmony_ci return false; 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_ci if (sig->flags & SIGNAL_GROUP_EXIT) 8228c2ecf20Sopenharmony_ci return true; 8238c2ecf20Sopenharmony_ci 8248c2ecf20Sopenharmony_ci if (thread_group_empty(task) && (task->flags & PF_EXITING)) 8258c2ecf20Sopenharmony_ci return true; 8268c2ecf20Sopenharmony_ci 8278c2ecf20Sopenharmony_ci return false; 8288c2ecf20Sopenharmony_ci} 8298c2ecf20Sopenharmony_ci 8308c2ecf20Sopenharmony_ci/* 8318c2ecf20Sopenharmony_ci * Checks whether the given task is dying or exiting and likely to 8328c2ecf20Sopenharmony_ci * release its address space. This means that all threads and processes 8338c2ecf20Sopenharmony_ci * sharing the same mm have to be killed or exiting. 8348c2ecf20Sopenharmony_ci * Caller has to make sure that task->mm is stable (hold task_lock or 8358c2ecf20Sopenharmony_ci * it operates on the current). 8368c2ecf20Sopenharmony_ci */ 8378c2ecf20Sopenharmony_cistatic bool task_will_free_mem(struct task_struct *task) 8388c2ecf20Sopenharmony_ci{ 8398c2ecf20Sopenharmony_ci struct mm_struct *mm = task->mm; 8408c2ecf20Sopenharmony_ci struct task_struct *p; 8418c2ecf20Sopenharmony_ci bool ret = true; 8428c2ecf20Sopenharmony_ci 8438c2ecf20Sopenharmony_ci /* 8448c2ecf20Sopenharmony_ci * Skip tasks without mm because it might have passed its exit_mm and 8458c2ecf20Sopenharmony_ci * exit_oom_victim. oom_reaper could have rescued that but do not rely 8468c2ecf20Sopenharmony_ci * on that for now. We can consider find_lock_task_mm in future. 8478c2ecf20Sopenharmony_ci */ 8488c2ecf20Sopenharmony_ci if (!mm) 8498c2ecf20Sopenharmony_ci return false; 8508c2ecf20Sopenharmony_ci 8518c2ecf20Sopenharmony_ci if (!__task_will_free_mem(task)) 8528c2ecf20Sopenharmony_ci return false; 8538c2ecf20Sopenharmony_ci 8548c2ecf20Sopenharmony_ci /* 8558c2ecf20Sopenharmony_ci * This task has already been drained by the oom reaper so there are 8568c2ecf20Sopenharmony_ci * only small chances it will free some more 8578c2ecf20Sopenharmony_ci */ 8588c2ecf20Sopenharmony_ci if (test_bit(MMF_OOM_SKIP, &mm->flags)) 8598c2ecf20Sopenharmony_ci return false; 8608c2ecf20Sopenharmony_ci 8618c2ecf20Sopenharmony_ci if (atomic_read(&mm->mm_users) <= 1) 8628c2ecf20Sopenharmony_ci return true; 8638c2ecf20Sopenharmony_ci 8648c2ecf20Sopenharmony_ci /* 8658c2ecf20Sopenharmony_ci * Make sure that all tasks which share the mm with the given tasks 8668c2ecf20Sopenharmony_ci * are dying as well to make sure that a) nobody pins its mm and 8678c2ecf20Sopenharmony_ci * b) the task is also reapable by the oom reaper. 8688c2ecf20Sopenharmony_ci */ 8698c2ecf20Sopenharmony_ci rcu_read_lock(); 8708c2ecf20Sopenharmony_ci for_each_process(p) { 8718c2ecf20Sopenharmony_ci if (!process_shares_mm(p, mm)) 8728c2ecf20Sopenharmony_ci continue; 8738c2ecf20Sopenharmony_ci if (same_thread_group(task, p)) 8748c2ecf20Sopenharmony_ci continue; 8758c2ecf20Sopenharmony_ci ret = __task_will_free_mem(p); 8768c2ecf20Sopenharmony_ci if (!ret) 8778c2ecf20Sopenharmony_ci break; 8788c2ecf20Sopenharmony_ci } 8798c2ecf20Sopenharmony_ci rcu_read_unlock(); 8808c2ecf20Sopenharmony_ci 8818c2ecf20Sopenharmony_ci return ret; 8828c2ecf20Sopenharmony_ci} 8838c2ecf20Sopenharmony_ci 8848c2ecf20Sopenharmony_cistatic void __oom_kill_process(struct task_struct *victim, const char *message) 8858c2ecf20Sopenharmony_ci{ 8868c2ecf20Sopenharmony_ci struct task_struct *p; 8878c2ecf20Sopenharmony_ci struct mm_struct *mm; 8888c2ecf20Sopenharmony_ci bool can_oom_reap = true; 8898c2ecf20Sopenharmony_ci 8908c2ecf20Sopenharmony_ci p = find_lock_task_mm(victim); 8918c2ecf20Sopenharmony_ci if (!p) { 8928c2ecf20Sopenharmony_ci pr_info("%s: OOM victim %d (%s) is already exiting. Skip killing the task\n", 8938c2ecf20Sopenharmony_ci message, task_pid_nr(victim), victim->comm); 8948c2ecf20Sopenharmony_ci put_task_struct(victim); 8958c2ecf20Sopenharmony_ci return; 8968c2ecf20Sopenharmony_ci } else if (victim != p) { 8978c2ecf20Sopenharmony_ci get_task_struct(p); 8988c2ecf20Sopenharmony_ci put_task_struct(victim); 8998c2ecf20Sopenharmony_ci victim = p; 9008c2ecf20Sopenharmony_ci } 9018c2ecf20Sopenharmony_ci 9028c2ecf20Sopenharmony_ci /* Get a reference to safely compare mm after task_unlock(victim) */ 9038c2ecf20Sopenharmony_ci mm = victim->mm; 9048c2ecf20Sopenharmony_ci mmgrab(mm); 9058c2ecf20Sopenharmony_ci 9068c2ecf20Sopenharmony_ci /* Raise event before sending signal: task reaper must see this */ 9078c2ecf20Sopenharmony_ci count_vm_event(OOM_KILL); 9088c2ecf20Sopenharmony_ci memcg_memory_event_mm(mm, MEMCG_OOM_KILL); 9098c2ecf20Sopenharmony_ci 9108c2ecf20Sopenharmony_ci /* 9118c2ecf20Sopenharmony_ci * We should send SIGKILL before granting access to memory reserves 9128c2ecf20Sopenharmony_ci * in order to prevent the OOM victim from depleting the memory 9138c2ecf20Sopenharmony_ci * reserves from the user space under its control. 9148c2ecf20Sopenharmony_ci */ 9158c2ecf20Sopenharmony_ci do_send_sig_info(SIGKILL, SEND_SIG_PRIV, victim, PIDTYPE_TGID); 9168c2ecf20Sopenharmony_ci mark_oom_victim(victim); 9178c2ecf20Sopenharmony_ci 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", 9188c2ecf20Sopenharmony_ci message, task_pid_nr(victim), victim->comm, K(mm->total_vm), 9198c2ecf20Sopenharmony_ci K(get_mm_counter(mm, MM_ANONPAGES)), 9208c2ecf20Sopenharmony_ci K(get_mm_counter(mm, MM_FILEPAGES)), 9218c2ecf20Sopenharmony_ci K(get_mm_counter(mm, MM_SHMEMPAGES)), 9228c2ecf20Sopenharmony_ci from_kuid(&init_user_ns, task_uid(victim)), 9238c2ecf20Sopenharmony_ci mm_pgtables_bytes(mm) >> 10, victim->signal->oom_score_adj); 9248c2ecf20Sopenharmony_ci task_unlock(victim); 9258c2ecf20Sopenharmony_ci 9268c2ecf20Sopenharmony_ci /* 9278c2ecf20Sopenharmony_ci * Kill all user processes sharing victim->mm in other thread groups, if 9288c2ecf20Sopenharmony_ci * any. They don't get access to memory reserves, though, to avoid 9298c2ecf20Sopenharmony_ci * depletion of all memory. This prevents mm->mmap_lock livelock when an 9308c2ecf20Sopenharmony_ci * oom killed thread cannot exit because it requires the semaphore and 9318c2ecf20Sopenharmony_ci * its contended by another thread trying to allocate memory itself. 9328c2ecf20Sopenharmony_ci * That thread will now get access to memory reserves since it has a 9338c2ecf20Sopenharmony_ci * pending fatal signal. 9348c2ecf20Sopenharmony_ci */ 9358c2ecf20Sopenharmony_ci rcu_read_lock(); 9368c2ecf20Sopenharmony_ci for_each_process(p) { 9378c2ecf20Sopenharmony_ci if (!process_shares_mm(p, mm)) 9388c2ecf20Sopenharmony_ci continue; 9398c2ecf20Sopenharmony_ci if (same_thread_group(p, victim)) 9408c2ecf20Sopenharmony_ci continue; 9418c2ecf20Sopenharmony_ci if (is_global_init(p)) { 9428c2ecf20Sopenharmony_ci can_oom_reap = false; 9438c2ecf20Sopenharmony_ci set_bit(MMF_OOM_SKIP, &mm->flags); 9448c2ecf20Sopenharmony_ci pr_info("oom killer %d (%s) has mm pinned by %d (%s)\n", 9458c2ecf20Sopenharmony_ci task_pid_nr(victim), victim->comm, 9468c2ecf20Sopenharmony_ci task_pid_nr(p), p->comm); 9478c2ecf20Sopenharmony_ci continue; 9488c2ecf20Sopenharmony_ci } 9498c2ecf20Sopenharmony_ci /* 9508c2ecf20Sopenharmony_ci * No kthead_use_mm() user needs to read from the userspace so 9518c2ecf20Sopenharmony_ci * we are ok to reap it. 9528c2ecf20Sopenharmony_ci */ 9538c2ecf20Sopenharmony_ci if (unlikely(p->flags & PF_KTHREAD)) 9548c2ecf20Sopenharmony_ci continue; 9558c2ecf20Sopenharmony_ci do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID); 9568c2ecf20Sopenharmony_ci } 9578c2ecf20Sopenharmony_ci rcu_read_unlock(); 9588c2ecf20Sopenharmony_ci 9598c2ecf20Sopenharmony_ci if (can_oom_reap) 9608c2ecf20Sopenharmony_ci queue_oom_reaper(victim); 9618c2ecf20Sopenharmony_ci 9628c2ecf20Sopenharmony_ci mmdrop(mm); 9638c2ecf20Sopenharmony_ci put_task_struct(victim); 9648c2ecf20Sopenharmony_ci} 9658c2ecf20Sopenharmony_ci#undef K 9668c2ecf20Sopenharmony_ci 9678c2ecf20Sopenharmony_ci/* 9688c2ecf20Sopenharmony_ci * Kill provided task unless it's secured by setting 9698c2ecf20Sopenharmony_ci * oom_score_adj to OOM_SCORE_ADJ_MIN. 9708c2ecf20Sopenharmony_ci */ 9718c2ecf20Sopenharmony_cistatic int oom_kill_memcg_member(struct task_struct *task, void *message) 9728c2ecf20Sopenharmony_ci{ 9738c2ecf20Sopenharmony_ci if (task->signal->oom_score_adj != OOM_SCORE_ADJ_MIN && 9748c2ecf20Sopenharmony_ci !is_global_init(task)) { 9758c2ecf20Sopenharmony_ci get_task_struct(task); 9768c2ecf20Sopenharmony_ci __oom_kill_process(task, message); 9778c2ecf20Sopenharmony_ci } 9788c2ecf20Sopenharmony_ci return 0; 9798c2ecf20Sopenharmony_ci} 9808c2ecf20Sopenharmony_ci 9818c2ecf20Sopenharmony_cistatic void oom_kill_process(struct oom_control *oc, const char *message) 9828c2ecf20Sopenharmony_ci{ 9838c2ecf20Sopenharmony_ci struct task_struct *victim = oc->chosen; 9848c2ecf20Sopenharmony_ci struct mem_cgroup *oom_group; 9858c2ecf20Sopenharmony_ci static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL, 9868c2ecf20Sopenharmony_ci DEFAULT_RATELIMIT_BURST); 9878c2ecf20Sopenharmony_ci 9888c2ecf20Sopenharmony_ci /* 9898c2ecf20Sopenharmony_ci * If the task is already exiting, don't alarm the sysadmin or kill 9908c2ecf20Sopenharmony_ci * its children or threads, just give it access to memory reserves 9918c2ecf20Sopenharmony_ci * so it can die quickly 9928c2ecf20Sopenharmony_ci */ 9938c2ecf20Sopenharmony_ci task_lock(victim); 9948c2ecf20Sopenharmony_ci if (task_will_free_mem(victim)) { 9958c2ecf20Sopenharmony_ci mark_oom_victim(victim); 9968c2ecf20Sopenharmony_ci queue_oom_reaper(victim); 9978c2ecf20Sopenharmony_ci task_unlock(victim); 9988c2ecf20Sopenharmony_ci put_task_struct(victim); 9998c2ecf20Sopenharmony_ci return; 10008c2ecf20Sopenharmony_ci } 10018c2ecf20Sopenharmony_ci task_unlock(victim); 10028c2ecf20Sopenharmony_ci 10038c2ecf20Sopenharmony_ci if (__ratelimit(&oom_rs)) 10048c2ecf20Sopenharmony_ci dump_header(oc, victim); 10058c2ecf20Sopenharmony_ci 10068c2ecf20Sopenharmony_ci /* 10078c2ecf20Sopenharmony_ci * Do we need to kill the entire memory cgroup? 10088c2ecf20Sopenharmony_ci * Or even one of the ancestor memory cgroups? 10098c2ecf20Sopenharmony_ci * Check this out before killing the victim task. 10108c2ecf20Sopenharmony_ci */ 10118c2ecf20Sopenharmony_ci oom_group = mem_cgroup_get_oom_group(victim, oc->memcg); 10128c2ecf20Sopenharmony_ci 10138c2ecf20Sopenharmony_ci __oom_kill_process(victim, message); 10148c2ecf20Sopenharmony_ci 10158c2ecf20Sopenharmony_ci /* 10168c2ecf20Sopenharmony_ci * If necessary, kill all tasks in the selected memory cgroup. 10178c2ecf20Sopenharmony_ci */ 10188c2ecf20Sopenharmony_ci if (oom_group) { 10198c2ecf20Sopenharmony_ci mem_cgroup_print_oom_group(oom_group); 10208c2ecf20Sopenharmony_ci mem_cgroup_scan_tasks(oom_group, oom_kill_memcg_member, 10218c2ecf20Sopenharmony_ci (void*)message); 10228c2ecf20Sopenharmony_ci mem_cgroup_put(oom_group); 10238c2ecf20Sopenharmony_ci } 10248c2ecf20Sopenharmony_ci} 10258c2ecf20Sopenharmony_ci 10268c2ecf20Sopenharmony_ci/* 10278c2ecf20Sopenharmony_ci * Determines whether the kernel must panic because of the panic_on_oom sysctl. 10288c2ecf20Sopenharmony_ci */ 10298c2ecf20Sopenharmony_cistatic void check_panic_on_oom(struct oom_control *oc) 10308c2ecf20Sopenharmony_ci{ 10318c2ecf20Sopenharmony_ci if (likely(!sysctl_panic_on_oom)) 10328c2ecf20Sopenharmony_ci return; 10338c2ecf20Sopenharmony_ci if (sysctl_panic_on_oom != 2) { 10348c2ecf20Sopenharmony_ci /* 10358c2ecf20Sopenharmony_ci * panic_on_oom == 1 only affects CONSTRAINT_NONE, the kernel 10368c2ecf20Sopenharmony_ci * does not panic for cpuset, mempolicy, or memcg allocation 10378c2ecf20Sopenharmony_ci * failures. 10388c2ecf20Sopenharmony_ci */ 10398c2ecf20Sopenharmony_ci if (oc->constraint != CONSTRAINT_NONE) 10408c2ecf20Sopenharmony_ci return; 10418c2ecf20Sopenharmony_ci } 10428c2ecf20Sopenharmony_ci /* Do not panic for oom kills triggered by sysrq */ 10438c2ecf20Sopenharmony_ci if (is_sysrq_oom(oc)) 10448c2ecf20Sopenharmony_ci return; 10458c2ecf20Sopenharmony_ci dump_header(oc, NULL); 10468c2ecf20Sopenharmony_ci panic("Out of memory: %s panic_on_oom is enabled\n", 10478c2ecf20Sopenharmony_ci sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide"); 10488c2ecf20Sopenharmony_ci} 10498c2ecf20Sopenharmony_ci 10508c2ecf20Sopenharmony_cistatic BLOCKING_NOTIFIER_HEAD(oom_notify_list); 10518c2ecf20Sopenharmony_ci 10528c2ecf20Sopenharmony_ciint register_oom_notifier(struct notifier_block *nb) 10538c2ecf20Sopenharmony_ci{ 10548c2ecf20Sopenharmony_ci return blocking_notifier_chain_register(&oom_notify_list, nb); 10558c2ecf20Sopenharmony_ci} 10568c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(register_oom_notifier); 10578c2ecf20Sopenharmony_ci 10588c2ecf20Sopenharmony_ciint unregister_oom_notifier(struct notifier_block *nb) 10598c2ecf20Sopenharmony_ci{ 10608c2ecf20Sopenharmony_ci return blocking_notifier_chain_unregister(&oom_notify_list, nb); 10618c2ecf20Sopenharmony_ci} 10628c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(unregister_oom_notifier); 10638c2ecf20Sopenharmony_ci 10648c2ecf20Sopenharmony_ci/** 10658c2ecf20Sopenharmony_ci * out_of_memory - kill the "best" process when we run out of memory 10668c2ecf20Sopenharmony_ci * @oc: pointer to struct oom_control 10678c2ecf20Sopenharmony_ci * 10688c2ecf20Sopenharmony_ci * If we run out of memory, we have the choice between either 10698c2ecf20Sopenharmony_ci * killing a random task (bad), letting the system crash (worse) 10708c2ecf20Sopenharmony_ci * OR try to be smart about which process to kill. Note that we 10718c2ecf20Sopenharmony_ci * don't have to be perfect here, we just have to be good. 10728c2ecf20Sopenharmony_ci */ 10738c2ecf20Sopenharmony_cibool out_of_memory(struct oom_control *oc) 10748c2ecf20Sopenharmony_ci{ 10758c2ecf20Sopenharmony_ci unsigned long freed = 0; 10768c2ecf20Sopenharmony_ci 10778c2ecf20Sopenharmony_ci if (oom_killer_disabled) 10788c2ecf20Sopenharmony_ci return false; 10798c2ecf20Sopenharmony_ci 10808c2ecf20Sopenharmony_ci if (!is_memcg_oom(oc)) { 10818c2ecf20Sopenharmony_ci blocking_notifier_call_chain(&oom_notify_list, 0, &freed); 10828c2ecf20Sopenharmony_ci if (freed > 0) 10838c2ecf20Sopenharmony_ci /* Got some memory back in the last second. */ 10848c2ecf20Sopenharmony_ci return true; 10858c2ecf20Sopenharmony_ci } 10868c2ecf20Sopenharmony_ci 10878c2ecf20Sopenharmony_ci /* 10888c2ecf20Sopenharmony_ci * If current has a pending SIGKILL or is exiting, then automatically 10898c2ecf20Sopenharmony_ci * select it. The goal is to allow it to allocate so that it may 10908c2ecf20Sopenharmony_ci * quickly exit and free its memory. 10918c2ecf20Sopenharmony_ci */ 10928c2ecf20Sopenharmony_ci if (task_will_free_mem(current)) { 10938c2ecf20Sopenharmony_ci mark_oom_victim(current); 10948c2ecf20Sopenharmony_ci queue_oom_reaper(current); 10958c2ecf20Sopenharmony_ci return true; 10968c2ecf20Sopenharmony_ci } 10978c2ecf20Sopenharmony_ci 10988c2ecf20Sopenharmony_ci /* 10998c2ecf20Sopenharmony_ci * The OOM killer does not compensate for IO-less reclaim. 11008c2ecf20Sopenharmony_ci * pagefault_out_of_memory lost its gfp context so we have to 11018c2ecf20Sopenharmony_ci * make sure exclude 0 mask - all other users should have at least 11028c2ecf20Sopenharmony_ci * ___GFP_DIRECT_RECLAIM to get here. But mem_cgroup_oom() has to 11038c2ecf20Sopenharmony_ci * invoke the OOM killer even if it is a GFP_NOFS allocation. 11048c2ecf20Sopenharmony_ci */ 11058c2ecf20Sopenharmony_ci if (oc->gfp_mask && !(oc->gfp_mask & __GFP_FS) && !is_memcg_oom(oc)) 11068c2ecf20Sopenharmony_ci return true; 11078c2ecf20Sopenharmony_ci 11088c2ecf20Sopenharmony_ci /* 11098c2ecf20Sopenharmony_ci * Check if there were limitations on the allocation (only relevant for 11108c2ecf20Sopenharmony_ci * NUMA and memcg) that may require different handling. 11118c2ecf20Sopenharmony_ci */ 11128c2ecf20Sopenharmony_ci oc->constraint = constrained_alloc(oc); 11138c2ecf20Sopenharmony_ci if (oc->constraint != CONSTRAINT_MEMORY_POLICY) 11148c2ecf20Sopenharmony_ci oc->nodemask = NULL; 11158c2ecf20Sopenharmony_ci check_panic_on_oom(oc); 11168c2ecf20Sopenharmony_ci 11178c2ecf20Sopenharmony_ci if (!is_memcg_oom(oc) && sysctl_oom_kill_allocating_task && 11188c2ecf20Sopenharmony_ci current->mm && !oom_unkillable_task(current) && 11198c2ecf20Sopenharmony_ci oom_cpuset_eligible(current, oc) && 11208c2ecf20Sopenharmony_ci current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) { 11218c2ecf20Sopenharmony_ci get_task_struct(current); 11228c2ecf20Sopenharmony_ci oc->chosen = current; 11238c2ecf20Sopenharmony_ci oom_kill_process(oc, "Out of memory (oom_kill_allocating_task)"); 11248c2ecf20Sopenharmony_ci return true; 11258c2ecf20Sopenharmony_ci } 11268c2ecf20Sopenharmony_ci 11278c2ecf20Sopenharmony_ci select_bad_process(oc); 11288c2ecf20Sopenharmony_ci /* Found nothing?!?! */ 11298c2ecf20Sopenharmony_ci if (!oc->chosen) { 11308c2ecf20Sopenharmony_ci dump_header(oc, NULL); 11318c2ecf20Sopenharmony_ci pr_warn("Out of memory and no killable processes...\n"); 11328c2ecf20Sopenharmony_ci /* 11338c2ecf20Sopenharmony_ci * If we got here due to an actual allocation at the 11348c2ecf20Sopenharmony_ci * system level, we cannot survive this and will enter 11358c2ecf20Sopenharmony_ci * an endless loop in the allocator. Bail out now. 11368c2ecf20Sopenharmony_ci */ 11378c2ecf20Sopenharmony_ci if (!is_sysrq_oom(oc) && !is_memcg_oom(oc)) 11388c2ecf20Sopenharmony_ci panic("System is deadlocked on memory\n"); 11398c2ecf20Sopenharmony_ci } 11408c2ecf20Sopenharmony_ci if (oc->chosen && oc->chosen != (void *)-1UL) 11418c2ecf20Sopenharmony_ci oom_kill_process(oc, !is_memcg_oom(oc) ? "Out of memory" : 11428c2ecf20Sopenharmony_ci "Memory cgroup out of memory"); 11438c2ecf20Sopenharmony_ci return !!oc->chosen; 11448c2ecf20Sopenharmony_ci} 11458c2ecf20Sopenharmony_ci 11468c2ecf20Sopenharmony_ci/* 11478c2ecf20Sopenharmony_ci * The pagefault handler calls here because some allocation has failed. We have 11488c2ecf20Sopenharmony_ci * to take care of the memcg OOM here because this is the only safe context without 11498c2ecf20Sopenharmony_ci * any locks held but let the oom killer triggered from the allocation context care 11508c2ecf20Sopenharmony_ci * about the global OOM. 11518c2ecf20Sopenharmony_ci */ 11528c2ecf20Sopenharmony_civoid pagefault_out_of_memory(void) 11538c2ecf20Sopenharmony_ci{ 11548c2ecf20Sopenharmony_ci static DEFINE_RATELIMIT_STATE(pfoom_rs, DEFAULT_RATELIMIT_INTERVAL, 11558c2ecf20Sopenharmony_ci DEFAULT_RATELIMIT_BURST); 11568c2ecf20Sopenharmony_ci 11578c2ecf20Sopenharmony_ci if (mem_cgroup_oom_synchronize(true)) 11588c2ecf20Sopenharmony_ci return; 11598c2ecf20Sopenharmony_ci 11608c2ecf20Sopenharmony_ci if (fatal_signal_pending(current)) 11618c2ecf20Sopenharmony_ci return; 11628c2ecf20Sopenharmony_ci 11638c2ecf20Sopenharmony_ci if (__ratelimit(&pfoom_rs)) 11648c2ecf20Sopenharmony_ci pr_warn("Huh VM_FAULT_OOM leaked out to the #PF handler. Retrying PF\n"); 11658c2ecf20Sopenharmony_ci} 1166