oom_kill.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  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...
  8. * Copyright (C) 2010 Google, Inc.
  9. * Rewritten by David Rientjes
  10. *
  11. * The routines in this file are used to kill a process when
  12. * 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.
  14. *
  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. #include <linux/oom.h>
  21. #include <linux/mm.h>
  22. #include <linux/err.h>
  23. #include <linux/gfp.h>
  24. #include <linux/sched.h>
  25. #include <linux/sched/mm.h>
  26. #include <linux/sched/coredump.h>
  27. #include <linux/sched/task.h>
  28. #include <linux/sched/debug.h>
  29. #include <linux/swap.h>
  30. #include <linux/syscalls.h>
  31. #include <linux/timex.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/cpuset.h>
  34. #include <linux/export.h>
  35. #include <linux/notifier.h>
  36. #include <linux/memcontrol.h>
  37. #include <linux/mempolicy.h>
  38. #include <linux/security.h>
  39. #include <linux/ptrace.h>
  40. #include <linux/freezer.h>
  41. #include <linux/ftrace.h>
  42. #include <linux/ratelimit.h>
  43. #include <linux/kthread.h>
  44. #include <linux/init.h>
  45. #include <linux/mmu_notifier.h>
  46. #include <linux/cred.h>
  47. #include <asm/tlb.h>
  48. #include "internal.h"
  49. #include "slab.h"
  50. #define CREATE_TRACE_POINTS
  51. #include <trace/events/oom.h>
  52. static int sysctl_panic_on_oom;
  53. static int sysctl_oom_kill_allocating_task;
  54. static int sysctl_oom_dump_tasks = 1;
  55. /*
  56. * Serializes oom killer invocations (out_of_memory()) from all contexts to
  57. * prevent from over eager oom killing (e.g. when the oom killer is invoked
  58. * from different domains).
  59. *
  60. * oom_killer_disable() relies on this lock to stabilize oom_killer_disabled
  61. * and mark_oom_victim
  62. */
  63. DEFINE_MUTEX(oom_lock);
  64. /* Serializes oom_score_adj and oom_score_adj_min updates */
  65. DEFINE_MUTEX(oom_adj_mutex);
  66. static inline bool is_memcg_oom(struct oom_control *oc)
  67. {
  68. return oc->memcg != NULL;
  69. }
  70. #ifdef CONFIG_NUMA
  71. /**
  72. * oom_cpuset_eligible() - check task eligibility for kill
  73. * @start: task struct of which task to consider
  74. * @oc: pointer to struct oom_control
  75. *
  76. * Task eligibility is determined by whether or not a candidate task, @tsk,
  77. * shares the same mempolicy nodes as current if it is bound by such a policy
  78. * and whether or not it has the same set of allowed cpuset nodes.
  79. *
  80. * This function is assuming oom-killer context and 'current' has triggered
  81. * the oom-killer.
  82. */
  83. static bool oom_cpuset_eligible(struct task_struct *start,
  84. struct oom_control *oc)
  85. {
  86. struct task_struct *tsk;
  87. bool ret = false;
  88. const nodemask_t *mask = oc->nodemask;
  89. rcu_read_lock();
  90. for_each_thread(start, tsk) {
  91. if (mask) {
  92. /*
  93. * If this is a mempolicy constrained oom, tsk's
  94. * cpuset is irrelevant. Only return true if its
  95. * mempolicy intersects current, otherwise it may be
  96. * needlessly killed.
  97. */
  98. ret = mempolicy_in_oom_domain(tsk, mask);
  99. } else {
  100. /*
  101. * This is not a mempolicy constrained oom, so only
  102. * check the mems of tsk's cpuset.
  103. */
  104. ret = cpuset_mems_allowed_intersects(current, tsk);
  105. }
  106. if (ret)
  107. break;
  108. }
  109. rcu_read_unlock();
  110. return ret;
  111. }
  112. #else
  113. static bool oom_cpuset_eligible(struct task_struct *tsk, struct oom_control *oc)
  114. {
  115. return true;
  116. }
  117. #endif /* CONFIG_NUMA */
  118. /*
  119. * The process p may have detached its own ->mm while exiting or through
  120. * kthread_use_mm(), but one or more of its subthreads may still have a valid
  121. * pointer. Return p, or any of its subthreads with a valid ->mm, with
  122. * task_lock() held.
  123. */
  124. struct task_struct *find_lock_task_mm(struct task_struct *p)
  125. {
  126. struct task_struct *t;
  127. rcu_read_lock();
  128. for_each_thread(p, t) {
  129. task_lock(t);
  130. if (likely(t->mm))
  131. goto found;
  132. task_unlock(t);
  133. }
  134. t = NULL;
  135. found:
  136. rcu_read_unlock();
  137. return t;
  138. }
  139. /*
  140. * order == -1 means the oom kill is required by sysrq, otherwise only
  141. * for display purposes.
  142. */
  143. static inline bool is_sysrq_oom(struct oom_control *oc)
  144. {
  145. return oc->order == -1;
  146. }
  147. /* return true if the task is not adequate as candidate victim task. */
  148. static bool oom_unkillable_task(struct task_struct *p)
  149. {
  150. if (is_global_init(p))
  151. return true;
  152. if (p->flags & PF_KTHREAD)
  153. return true;
  154. return false;
  155. }
  156. /*
  157. * Check whether unreclaimable slab amount is greater than
  158. * all user memory(LRU pages).
  159. * dump_unreclaimable_slab() could help in the case that
  160. * oom due to too much unreclaimable slab used by kernel.
  161. */
  162. static bool should_dump_unreclaim_slab(void)
  163. {
  164. unsigned long nr_lru;
  165. nr_lru = global_node_page_state(NR_ACTIVE_ANON) +
  166. global_node_page_state(NR_INACTIVE_ANON) +
  167. global_node_page_state(NR_ACTIVE_FILE) +
  168. global_node_page_state(NR_INACTIVE_FILE) +
  169. global_node_page_state(NR_ISOLATED_ANON) +
  170. global_node_page_state(NR_ISOLATED_FILE) +
  171. global_node_page_state(NR_UNEVICTABLE);
  172. return (global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B) > nr_lru);
  173. }
  174. /**
  175. * oom_badness - heuristic function to determine which candidate task to kill
  176. * @p: task struct of which task we should calculate
  177. * @totalpages: total present RAM allowed for page allocation
  178. *
  179. * The heuristic for determining which task to kill is made to be as simple and
  180. * predictable as possible. The goal is to return the highest value for the
  181. * task consuming the most memory to avoid subsequent oom failures.
  182. */
  183. long oom_badness(struct task_struct *p, unsigned long totalpages)
  184. {
  185. long points;
  186. long adj;
  187. if (oom_unkillable_task(p))
  188. return LONG_MIN;
  189. p = find_lock_task_mm(p);
  190. if (!p)
  191. return LONG_MIN;
  192. /*
  193. * Do not even consider tasks which are explicitly marked oom
  194. * unkillable or have been already oom reaped or the are in
  195. * the middle of vfork
  196. */
  197. adj = (long)p->signal->oom_score_adj;
  198. if (adj == OOM_SCORE_ADJ_MIN ||
  199. test_bit(MMF_OOM_SKIP, &p->mm->flags) ||
  200. in_vfork(p)) {
  201. task_unlock(p);
  202. return LONG_MIN;
  203. }
  204. /*
  205. * The baseline for the badness score is the proportion of RAM that each
  206. * task's rss, pagetable and swap space use.
  207. */
  208. points = get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) +
  209. mm_pgtables_bytes(p->mm) / PAGE_SIZE;
  210. task_unlock(p);
  211. /* Normalize to oom_score_adj units */
  212. adj *= totalpages / 1000;
  213. points += adj;
  214. return points;
  215. }
  216. static const char * const oom_constraint_text[] = {
  217. [CONSTRAINT_NONE] = "CONSTRAINT_NONE",
  218. [CONSTRAINT_CPUSET] = "CONSTRAINT_CPUSET",
  219. [CONSTRAINT_MEMORY_POLICY] = "CONSTRAINT_MEMORY_POLICY",
  220. [CONSTRAINT_MEMCG] = "CONSTRAINT_MEMCG",
  221. };
  222. /*
  223. * Determine the type of allocation constraint.
  224. */
  225. static enum oom_constraint constrained_alloc(struct oom_control *oc)
  226. {
  227. struct zone *zone;
  228. struct zoneref *z;
  229. enum zone_type highest_zoneidx = gfp_zone(oc->gfp_mask);
  230. bool cpuset_limited = false;
  231. int nid;
  232. if (is_memcg_oom(oc)) {
  233. oc->totalpages = mem_cgroup_get_max(oc->memcg) ?: 1;
  234. return CONSTRAINT_MEMCG;
  235. }
  236. /* Default to all available memory */
  237. oc->totalpages = totalram_pages() + total_swap_pages;
  238. if (!IS_ENABLED(CONFIG_NUMA))
  239. return CONSTRAINT_NONE;
  240. if (!oc->zonelist)
  241. return CONSTRAINT_NONE;
  242. /*
  243. * Reach here only when __GFP_NOFAIL is used. So, we should avoid
  244. * to kill current.We have to random task kill in this case.
  245. * Hopefully, CONSTRAINT_THISNODE...but no way to handle it, now.
  246. */
  247. if (oc->gfp_mask & __GFP_THISNODE)
  248. return CONSTRAINT_NONE;
  249. /*
  250. * This is not a __GFP_THISNODE allocation, so a truncated nodemask in
  251. * the page allocator means a mempolicy is in effect. Cpuset policy
  252. * is enforced in get_page_from_freelist().
  253. */
  254. if (oc->nodemask &&
  255. !nodes_subset(node_states[N_MEMORY], *oc->nodemask)) {
  256. oc->totalpages = total_swap_pages;
  257. for_each_node_mask(nid, *oc->nodemask)
  258. oc->totalpages += node_present_pages(nid);
  259. return CONSTRAINT_MEMORY_POLICY;
  260. }
  261. /* Check this allocation failure is caused by cpuset's wall function */
  262. for_each_zone_zonelist_nodemask(zone, z, oc->zonelist,
  263. highest_zoneidx, oc->nodemask)
  264. if (!cpuset_zone_allowed(zone, oc->gfp_mask))
  265. cpuset_limited = true;
  266. if (cpuset_limited) {
  267. oc->totalpages = total_swap_pages;
  268. for_each_node_mask(nid, cpuset_current_mems_allowed)
  269. oc->totalpages += node_present_pages(nid);
  270. return CONSTRAINT_CPUSET;
  271. }
  272. return CONSTRAINT_NONE;
  273. }
  274. static int oom_evaluate_task(struct task_struct *task, void *arg)
  275. {
  276. struct oom_control *oc = arg;
  277. long points;
  278. if (oom_unkillable_task(task))
  279. goto next;
  280. /* p may not have freeable memory in nodemask */
  281. if (!is_memcg_oom(oc) && !oom_cpuset_eligible(task, oc))
  282. goto next;
  283. /*
  284. * This task already has access to memory reserves and is being killed.
  285. * Don't allow any other task to have access to the reserves unless
  286. * the task has MMF_OOM_SKIP because chances that it would release
  287. * any memory is quite low.
  288. */
  289. if (!is_sysrq_oom(oc) && tsk_is_oom_victim(task)) {
  290. if (test_bit(MMF_OOM_SKIP, &task->signal->oom_mm->flags))
  291. goto next;
  292. goto abort;
  293. }
  294. /*
  295. * If task is allocating a lot of memory and has been marked to be
  296. * killed first if it triggers an oom, then select it.
  297. */
  298. if (oom_task_origin(task)) {
  299. points = LONG_MAX;
  300. goto select;
  301. }
  302. points = oom_badness(task, oc->totalpages);
  303. if (points == LONG_MIN || points < oc->chosen_points)
  304. goto next;
  305. select:
  306. if (oc->chosen)
  307. put_task_struct(oc->chosen);
  308. get_task_struct(task);
  309. oc->chosen = task;
  310. oc->chosen_points = points;
  311. next:
  312. return 0;
  313. abort:
  314. if (oc->chosen)
  315. put_task_struct(oc->chosen);
  316. oc->chosen = (void *)-1UL;
  317. return 1;
  318. }
  319. /*
  320. * Simple selection loop. We choose the process with the highest number of
  321. * 'points'. In case scan was aborted, oc->chosen is set to -1.
  322. */
  323. static void select_bad_process(struct oom_control *oc)
  324. {
  325. oc->chosen_points = LONG_MIN;
  326. if (is_memcg_oom(oc))
  327. mem_cgroup_scan_tasks(oc->memcg, oom_evaluate_task, oc);
  328. else {
  329. struct task_struct *p;
  330. rcu_read_lock();
  331. for_each_process(p)
  332. if (oom_evaluate_task(p, oc))
  333. break;
  334. rcu_read_unlock();
  335. }
  336. }
  337. static int dump_task(struct task_struct *p, void *arg)
  338. {
  339. struct oom_control *oc = arg;
  340. struct task_struct *task;
  341. if (oom_unkillable_task(p))
  342. return 0;
  343. /* p may not have freeable memory in nodemask */
  344. if (!is_memcg_oom(oc) && !oom_cpuset_eligible(p, oc))
  345. return 0;
  346. task = find_lock_task_mm(p);
  347. if (!task) {
  348. /*
  349. * All of p's threads have already detached their mm's. There's
  350. * no need to report them; they can't be oom killed anyway.
  351. */
  352. return 0;
  353. }
  354. pr_info("[%7d] %5d %5d %8lu %8lu %8ld %8lu %5hd %s\n",
  355. task->pid, from_kuid(&init_user_ns, task_uid(task)),
  356. task->tgid, task->mm->total_vm, get_mm_rss(task->mm),
  357. mm_pgtables_bytes(task->mm),
  358. get_mm_counter(task->mm, MM_SWAPENTS),
  359. task->signal->oom_score_adj, task->comm);
  360. task_unlock(task);
  361. return 0;
  362. }
  363. /**
  364. * dump_tasks - dump current memory state of all system tasks
  365. * @oc: pointer to struct oom_control
  366. *
  367. * Dumps the current memory state of all eligible tasks. Tasks not in the same
  368. * memcg, not in the same cpuset, or bound to a disjoint set of mempolicy nodes
  369. * are not shown.
  370. * State information includes task's pid, uid, tgid, vm size, rss,
  371. * pgtables_bytes, swapents, oom_score_adj value, and name.
  372. */
  373. void dump_tasks(struct oom_control *oc)
  374. {
  375. pr_info("Tasks state (memory values in pages):\n");
  376. pr_info("[ pid ] uid tgid total_vm rss pgtables_bytes swapents oom_score_adj name\n");
  377. if (is_memcg_oom(oc))
  378. mem_cgroup_scan_tasks(oc->memcg, dump_task, oc);
  379. else {
  380. struct task_struct *p;
  381. rcu_read_lock();
  382. for_each_process(p)
  383. dump_task(p, oc);
  384. rcu_read_unlock();
  385. }
  386. }
  387. EXPORT_SYMBOL_GPL(dump_tasks);
  388. static void dump_oom_summary(struct oom_control *oc, struct task_struct *victim)
  389. {
  390. /* one line summary of the oom killer context. */
  391. pr_info("oom-kill:constraint=%s,nodemask=%*pbl",
  392. oom_constraint_text[oc->constraint],
  393. nodemask_pr_args(oc->nodemask));
  394. cpuset_print_current_mems_allowed();
  395. mem_cgroup_print_oom_context(oc->memcg, victim);
  396. pr_cont(",task=%s,pid=%d,uid=%d\n", victim->comm, victim->pid,
  397. from_kuid(&init_user_ns, task_uid(victim)));
  398. }
  399. static void dump_header(struct oom_control *oc, struct task_struct *p)
  400. {
  401. pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd\n",
  402. current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order,
  403. current->signal->oom_score_adj);
  404. if (!IS_ENABLED(CONFIG_COMPACTION) && oc->order)
  405. pr_warn("COMPACTION is disabled!!!\n");
  406. dump_stack();
  407. if (is_memcg_oom(oc))
  408. mem_cgroup_print_oom_meminfo(oc->memcg);
  409. else {
  410. __show_mem(SHOW_MEM_FILTER_NODES, oc->nodemask, gfp_zone(oc->gfp_mask));
  411. if (should_dump_unreclaim_slab())
  412. dump_unreclaimable_slab();
  413. }
  414. if (sysctl_oom_dump_tasks)
  415. dump_tasks(oc);
  416. if (p)
  417. dump_oom_summary(oc, p);
  418. }
  419. /*
  420. * Number of OOM victims in flight
  421. */
  422. static atomic_t oom_victims = ATOMIC_INIT(0);
  423. static DECLARE_WAIT_QUEUE_HEAD(oom_victims_wait);
  424. static bool oom_killer_disabled __read_mostly;
  425. #define K(x) ((x) << (PAGE_SHIFT-10))
  426. /*
  427. * task->mm can be NULL if the task is the exited group leader. So to
  428. * determine whether the task is using a particular mm, we examine all the
  429. * task's threads: if one of those is using this mm then this task was also
  430. * using it.
  431. */
  432. bool process_shares_mm(struct task_struct *p, struct mm_struct *mm)
  433. {
  434. struct task_struct *t;
  435. for_each_thread(p, t) {
  436. struct mm_struct *t_mm = READ_ONCE(t->mm);
  437. if (t_mm)
  438. return t_mm == mm;
  439. }
  440. return false;
  441. }
  442. #ifdef CONFIG_MMU
  443. /*
  444. * OOM Reaper kernel thread which tries to reap the memory used by the OOM
  445. * victim (if that is possible) to help the OOM killer to move on.
  446. */
  447. static struct task_struct *oom_reaper_th;
  448. static DECLARE_WAIT_QUEUE_HEAD(oom_reaper_wait);
  449. static struct task_struct *oom_reaper_list;
  450. static DEFINE_SPINLOCK(oom_reaper_lock);
  451. static bool __oom_reap_task_mm(struct mm_struct *mm)
  452. {
  453. struct vm_area_struct *vma;
  454. bool ret = true;
  455. VMA_ITERATOR(vmi, mm, 0);
  456. /*
  457. * Tell all users of get_user/copy_from_user etc... that the content
  458. * is no longer stable. No barriers really needed because unmapping
  459. * should imply barriers already and the reader would hit a page fault
  460. * if it stumbled over a reaped memory.
  461. */
  462. set_bit(MMF_UNSTABLE, &mm->flags);
  463. for_each_vma(vmi, vma) {
  464. if (vma->vm_flags & (VM_HUGETLB|VM_PFNMAP))
  465. continue;
  466. /*
  467. * Only anonymous pages have a good chance to be dropped
  468. * without additional steps which we cannot afford as we
  469. * are OOM already.
  470. *
  471. * We do not even care about fs backed pages because all
  472. * which are reclaimable have already been reclaimed and
  473. * we do not want to block exit_mmap by keeping mm ref
  474. * count elevated without a good reason.
  475. */
  476. if (vma_is_anonymous(vma) || !(vma->vm_flags & VM_SHARED)) {
  477. struct mmu_notifier_range range;
  478. struct mmu_gather tlb;
  479. mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0,
  480. vma, mm, vma->vm_start,
  481. vma->vm_end);
  482. tlb_gather_mmu(&tlb, mm);
  483. if (mmu_notifier_invalidate_range_start_nonblock(&range)) {
  484. tlb_finish_mmu(&tlb);
  485. ret = false;
  486. continue;
  487. }
  488. unmap_page_range(&tlb, vma, range.start, range.end, NULL);
  489. mmu_notifier_invalidate_range_end(&range);
  490. tlb_finish_mmu(&tlb);
  491. }
  492. }
  493. return ret;
  494. }
  495. /*
  496. * Reaps the address space of the give task.
  497. *
  498. * Returns true on success and false if none or part of the address space
  499. * has been reclaimed and the caller should retry later.
  500. */
  501. static bool oom_reap_task_mm(struct task_struct *tsk, struct mm_struct *mm)
  502. {
  503. bool ret = true;
  504. if (!mmap_read_trylock(mm)) {
  505. trace_skip_task_reaping(tsk->pid);
  506. return false;
  507. }
  508. /*
  509. * MMF_OOM_SKIP is set by exit_mmap when the OOM reaper can't
  510. * work on the mm anymore. The check for MMF_OOM_SKIP must run
  511. * under mmap_lock for reading because it serializes against the
  512. * mmap_write_lock();mmap_write_unlock() cycle in exit_mmap().
  513. */
  514. if (test_bit(MMF_OOM_SKIP, &mm->flags)) {
  515. trace_skip_task_reaping(tsk->pid);
  516. goto out_unlock;
  517. }
  518. trace_start_task_reaping(tsk->pid);
  519. /* failed to reap part of the address space. Try again later */
  520. ret = __oom_reap_task_mm(mm);
  521. if (!ret)
  522. goto out_finish;
  523. pr_info("oom_reaper: reaped process %d (%s), now anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB\n",
  524. task_pid_nr(tsk), tsk->comm,
  525. K(get_mm_counter(mm, MM_ANONPAGES)),
  526. K(get_mm_counter(mm, MM_FILEPAGES)),
  527. K(get_mm_counter(mm, MM_SHMEMPAGES)));
  528. out_finish:
  529. trace_finish_task_reaping(tsk->pid);
  530. out_unlock:
  531. mmap_read_unlock(mm);
  532. return ret;
  533. }
  534. #define MAX_OOM_REAP_RETRIES 10
  535. static void oom_reap_task(struct task_struct *tsk)
  536. {
  537. int attempts = 0;
  538. struct mm_struct *mm = tsk->signal->oom_mm;
  539. /* Retry the mmap_read_trylock(mm) a few times */
  540. while (attempts++ < MAX_OOM_REAP_RETRIES && !oom_reap_task_mm(tsk, mm))
  541. schedule_timeout_idle(HZ/10);
  542. if (attempts <= MAX_OOM_REAP_RETRIES ||
  543. test_bit(MMF_OOM_SKIP, &mm->flags))
  544. goto done;
  545. pr_info("oom_reaper: unable to reap pid:%d (%s)\n",
  546. task_pid_nr(tsk), tsk->comm);
  547. sched_show_task(tsk);
  548. debug_show_all_locks();
  549. done:
  550. tsk->oom_reaper_list = NULL;
  551. /*
  552. * Hide this mm from OOM killer because it has been either reaped or
  553. * somebody can't call mmap_write_unlock(mm).
  554. */
  555. set_bit(MMF_OOM_SKIP, &mm->flags);
  556. /* Drop a reference taken by queue_oom_reaper */
  557. put_task_struct(tsk);
  558. }
  559. static int oom_reaper(void *unused)
  560. {
  561. set_freezable();
  562. while (true) {
  563. struct task_struct *tsk = NULL;
  564. wait_event_freezable(oom_reaper_wait, oom_reaper_list != NULL);
  565. spin_lock_irq(&oom_reaper_lock);
  566. if (oom_reaper_list != NULL) {
  567. tsk = oom_reaper_list;
  568. oom_reaper_list = tsk->oom_reaper_list;
  569. }
  570. spin_unlock_irq(&oom_reaper_lock);
  571. if (tsk)
  572. oom_reap_task(tsk);
  573. }
  574. return 0;
  575. }
  576. static void wake_oom_reaper(struct timer_list *timer)
  577. {
  578. struct task_struct *tsk = container_of(timer, struct task_struct,
  579. oom_reaper_timer);
  580. struct mm_struct *mm = tsk->signal->oom_mm;
  581. unsigned long flags;
  582. /* The victim managed to terminate on its own - see exit_mmap */
  583. if (test_bit(MMF_OOM_SKIP, &mm->flags)) {
  584. put_task_struct(tsk);
  585. return;
  586. }
  587. spin_lock_irqsave(&oom_reaper_lock, flags);
  588. tsk->oom_reaper_list = oom_reaper_list;
  589. oom_reaper_list = tsk;
  590. spin_unlock_irqrestore(&oom_reaper_lock, flags);
  591. trace_wake_reaper(tsk->pid);
  592. wake_up(&oom_reaper_wait);
  593. }
  594. /*
  595. * Give the OOM victim time to exit naturally before invoking the oom_reaping.
  596. * The timers timeout is arbitrary... the longer it is, the longer the worst
  597. * case scenario for the OOM can take. If it is too small, the oom_reaper can
  598. * get in the way and release resources needed by the process exit path.
  599. * e.g. The futex robust list can sit in Anon|Private memory that gets reaped
  600. * before the exit path is able to wake the futex waiters.
  601. */
  602. #define OOM_REAPER_DELAY (2*HZ)
  603. static void queue_oom_reaper(struct task_struct *tsk)
  604. {
  605. /* mm is already queued? */
  606. if (test_and_set_bit(MMF_OOM_REAP_QUEUED, &tsk->signal->oom_mm->flags))
  607. return;
  608. get_task_struct(tsk);
  609. timer_setup(&tsk->oom_reaper_timer, wake_oom_reaper, 0);
  610. tsk->oom_reaper_timer.expires = jiffies + OOM_REAPER_DELAY;
  611. add_timer(&tsk->oom_reaper_timer);
  612. }
  613. #ifdef CONFIG_SYSCTL
  614. static struct ctl_table vm_oom_kill_table[] = {
  615. {
  616. .procname = "panic_on_oom",
  617. .data = &sysctl_panic_on_oom,
  618. .maxlen = sizeof(sysctl_panic_on_oom),
  619. .mode = 0644,
  620. .proc_handler = proc_dointvec_minmax,
  621. .extra1 = SYSCTL_ZERO,
  622. .extra2 = SYSCTL_TWO,
  623. },
  624. {
  625. .procname = "oom_kill_allocating_task",
  626. .data = &sysctl_oom_kill_allocating_task,
  627. .maxlen = sizeof(sysctl_oom_kill_allocating_task),
  628. .mode = 0644,
  629. .proc_handler = proc_dointvec,
  630. },
  631. {
  632. .procname = "oom_dump_tasks",
  633. .data = &sysctl_oom_dump_tasks,
  634. .maxlen = sizeof(sysctl_oom_dump_tasks),
  635. .mode = 0644,
  636. .proc_handler = proc_dointvec,
  637. },
  638. {}
  639. };
  640. #endif
  641. static int __init oom_init(void)
  642. {
  643. oom_reaper_th = kthread_run(oom_reaper, NULL, "oom_reaper");
  644. #ifdef CONFIG_SYSCTL
  645. register_sysctl_init("vm", vm_oom_kill_table);
  646. #endif
  647. return 0;
  648. }
  649. subsys_initcall(oom_init)
  650. #else
  651. static inline void queue_oom_reaper(struct task_struct *tsk)
  652. {
  653. }
  654. #endif /* CONFIG_MMU */
  655. /**
  656. * tsk->mm has to be non NULL and caller has to guarantee it is stable (either
  657. * under task_lock or operate on the current).
  658. */
  659. static void __mark_oom_victim(struct task_struct *tsk)
  660. {
  661. struct mm_struct *mm = tsk->mm;
  662. if (!cmpxchg(&tsk->signal->oom_mm, NULL, mm)) {
  663. mmgrab(tsk->signal->oom_mm);
  664. }
  665. }
  666. /**
  667. * mark_oom_victim - mark the given task as OOM victim
  668. * @tsk: task to mark
  669. *
  670. * Has to be called with oom_lock held and never after
  671. * oom has been disabled already.
  672. *
  673. * tsk->mm has to be non NULL and caller has to guarantee it is stable (either
  674. * under task_lock or operate on the current).
  675. */
  676. static void mark_oom_victim(struct task_struct *tsk)
  677. {
  678. const struct cred *cred;
  679. WARN_ON(oom_killer_disabled);
  680. /* OOM killer might race with memcg OOM */
  681. if (test_and_set_tsk_thread_flag(tsk, TIF_MEMDIE))
  682. return;
  683. /* oom_mm is bound to the signal struct life time. */
  684. __mark_oom_victim(tsk);
  685. /*
  686. * Make sure that the task is woken up from uninterruptible sleep
  687. * if it is frozen because OOM killer wouldn't be able to free
  688. * any memory and livelock. freezing_slow_path will tell the freezer
  689. * that TIF_MEMDIE tasks should be ignored.
  690. */
  691. __thaw_task(tsk);
  692. atomic_inc(&oom_victims);
  693. cred = get_task_cred(tsk);
  694. trace_mark_victim(tsk, cred->uid.val);
  695. put_cred(cred);
  696. }
  697. /**
  698. * exit_oom_victim - note the exit of an OOM victim
  699. */
  700. void exit_oom_victim(void)
  701. {
  702. clear_thread_flag(TIF_MEMDIE);
  703. if (!atomic_dec_return(&oom_victims))
  704. wake_up_all(&oom_victims_wait);
  705. }
  706. /**
  707. * oom_killer_enable - enable OOM killer
  708. */
  709. void oom_killer_enable(void)
  710. {
  711. oom_killer_disabled = false;
  712. pr_info("OOM killer enabled.\n");
  713. }
  714. /**
  715. * oom_killer_disable - disable OOM killer
  716. * @timeout: maximum timeout to wait for oom victims in jiffies
  717. *
  718. * Forces all page allocations to fail rather than trigger OOM killer.
  719. * Will block and wait until all OOM victims are killed or the given
  720. * timeout expires.
  721. *
  722. * The function cannot be called when there are runnable user tasks because
  723. * the userspace would see unexpected allocation failures as a result. Any
  724. * new usage of this function should be consulted with MM people.
  725. *
  726. * Returns true if successful and false if the OOM killer cannot be
  727. * disabled.
  728. */
  729. bool oom_killer_disable(signed long timeout)
  730. {
  731. signed long ret;
  732. /*
  733. * Make sure to not race with an ongoing OOM killer. Check that the
  734. * current is not killed (possibly due to sharing the victim's memory).
  735. */
  736. if (mutex_lock_killable(&oom_lock))
  737. return false;
  738. oom_killer_disabled = true;
  739. mutex_unlock(&oom_lock);
  740. ret = wait_event_interruptible_timeout(oom_victims_wait,
  741. !atomic_read(&oom_victims), timeout);
  742. if (ret <= 0) {
  743. oom_killer_enable();
  744. return false;
  745. }
  746. pr_info("OOM killer disabled.\n");
  747. return true;
  748. }
  749. static inline bool __task_will_free_mem(struct task_struct *task)
  750. {
  751. struct signal_struct *sig = task->signal;
  752. /*
  753. * A coredumping process may sleep for an extended period in
  754. * coredump_task_exit(), so the oom killer cannot assume that
  755. * the process will promptly exit and release memory.
  756. */
  757. if (sig->core_state)
  758. return false;
  759. if (sig->flags & SIGNAL_GROUP_EXIT)
  760. return true;
  761. if (thread_group_empty(task) && (task->flags & PF_EXITING))
  762. return true;
  763. return false;
  764. }
  765. /*
  766. * Checks whether the given task is dying or exiting and likely to
  767. * release its address space. This means that all threads and processes
  768. * sharing the same mm have to be killed or exiting.
  769. * Caller has to make sure that task->mm is stable (hold task_lock or
  770. * it operates on the current).
  771. */
  772. static bool task_will_free_mem(struct task_struct *task)
  773. {
  774. struct mm_struct *mm = task->mm;
  775. struct task_struct *p;
  776. bool ret = true;
  777. /*
  778. * Skip tasks without mm because it might have passed its exit_mm and
  779. * exit_oom_victim. oom_reaper could have rescued that but do not rely
  780. * on that for now. We can consider find_lock_task_mm in future.
  781. */
  782. if (!mm)
  783. return false;
  784. if (!__task_will_free_mem(task))
  785. return false;
  786. /*
  787. * This task has already been drained by the oom reaper so there are
  788. * only small chances it will free some more
  789. */
  790. if (test_bit(MMF_OOM_SKIP, &mm->flags))
  791. return false;
  792. if (atomic_read(&mm->mm_users) <= 1)
  793. return true;
  794. /*
  795. * Make sure that all tasks which share the mm with the given tasks
  796. * are dying as well to make sure that a) nobody pins its mm and
  797. * b) the task is also reapable by the oom reaper.
  798. */
  799. rcu_read_lock();
  800. for_each_process(p) {
  801. if (!process_shares_mm(p, mm))
  802. continue;
  803. if (same_thread_group(task, p))
  804. continue;
  805. ret = __task_will_free_mem(p);
  806. if (!ret)
  807. break;
  808. }
  809. rcu_read_unlock();
  810. return ret;
  811. }
  812. static void __oom_kill_process(struct task_struct *victim, const char *message)
  813. {
  814. struct task_struct *p;
  815. struct mm_struct *mm;
  816. bool can_oom_reap = true;
  817. p = find_lock_task_mm(victim);
  818. if (!p) {
  819. pr_info("%s: OOM victim %d (%s) is already exiting. Skip killing the task\n",
  820. message, task_pid_nr(victim), victim->comm);
  821. put_task_struct(victim);
  822. return;
  823. } else if (victim != p) {
  824. get_task_struct(p);
  825. put_task_struct(victim);
  826. victim = p;
  827. }
  828. /* Get a reference to safely compare mm after task_unlock(victim) */
  829. mm = victim->mm;
  830. mmgrab(mm);
  831. /* Raise event before sending signal: task reaper must see this */
  832. count_vm_event(OOM_KILL);
  833. memcg_memory_event_mm(mm, MEMCG_OOM_KILL);
  834. /*
  835. * We should send SIGKILL before granting access to memory reserves
  836. * in order to prevent the OOM victim from depleting the memory
  837. * reserves from the user space under its control.
  838. */
  839. do_send_sig_info(SIGKILL, SEND_SIG_PRIV, victim, PIDTYPE_TGID);
  840. mark_oom_victim(victim);
  841. 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",
  842. message, task_pid_nr(victim), victim->comm, K(mm->total_vm),
  843. K(get_mm_counter(mm, MM_ANONPAGES)),
  844. K(get_mm_counter(mm, MM_FILEPAGES)),
  845. K(get_mm_counter(mm, MM_SHMEMPAGES)),
  846. from_kuid(&init_user_ns, task_uid(victim)),
  847. mm_pgtables_bytes(mm) >> 10, victim->signal->oom_score_adj);
  848. task_unlock(victim);
  849. /*
  850. * Kill all user processes sharing victim->mm in other thread groups, if
  851. * any. They don't get access to memory reserves, though, to avoid
  852. * depletion of all memory. This prevents mm->mmap_lock livelock when an
  853. * oom killed thread cannot exit because it requires the semaphore and
  854. * its contended by another thread trying to allocate memory itself.
  855. * That thread will now get access to memory reserves since it has a
  856. * pending fatal signal.
  857. */
  858. rcu_read_lock();
  859. for_each_process(p) {
  860. if (!process_shares_mm(p, mm))
  861. continue;
  862. if (same_thread_group(p, victim))
  863. continue;
  864. if (is_global_init(p)) {
  865. can_oom_reap = false;
  866. set_bit(MMF_OOM_SKIP, &mm->flags);
  867. pr_info("oom killer %d (%s) has mm pinned by %d (%s)\n",
  868. task_pid_nr(victim), victim->comm,
  869. task_pid_nr(p), p->comm);
  870. continue;
  871. }
  872. /*
  873. * No kthread_use_mm() user needs to read from the userspace so
  874. * we are ok to reap it.
  875. */
  876. if (unlikely(p->flags & PF_KTHREAD))
  877. continue;
  878. do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID);
  879. }
  880. rcu_read_unlock();
  881. if (can_oom_reap)
  882. queue_oom_reaper(victim);
  883. mmdrop(mm);
  884. put_task_struct(victim);
  885. }
  886. #undef K
  887. /*
  888. * Kill provided task unless it's secured by setting
  889. * oom_score_adj to OOM_SCORE_ADJ_MIN.
  890. */
  891. static int oom_kill_memcg_member(struct task_struct *task, void *message)
  892. {
  893. if (task->signal->oom_score_adj != OOM_SCORE_ADJ_MIN &&
  894. !is_global_init(task)) {
  895. get_task_struct(task);
  896. __oom_kill_process(task, message);
  897. }
  898. return 0;
  899. }
  900. static void oom_kill_process(struct oom_control *oc, const char *message)
  901. {
  902. struct task_struct *victim = oc->chosen;
  903. struct mem_cgroup *oom_group;
  904. static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL,
  905. DEFAULT_RATELIMIT_BURST);
  906. /*
  907. * If the task is already exiting, don't alarm the sysadmin or kill
  908. * its children or threads, just give it access to memory reserves
  909. * so it can die quickly
  910. */
  911. task_lock(victim);
  912. if (task_will_free_mem(victim)) {
  913. mark_oom_victim(victim);
  914. queue_oom_reaper(victim);
  915. task_unlock(victim);
  916. put_task_struct(victim);
  917. return;
  918. }
  919. task_unlock(victim);
  920. if (__ratelimit(&oom_rs))
  921. dump_header(oc, victim);
  922. /*
  923. * Do we need to kill the entire memory cgroup?
  924. * Or even one of the ancestor memory cgroups?
  925. * Check this out before killing the victim task.
  926. */
  927. oom_group = mem_cgroup_get_oom_group(victim, oc->memcg);
  928. __oom_kill_process(victim, message);
  929. /*
  930. * If necessary, kill all tasks in the selected memory cgroup.
  931. */
  932. if (oom_group) {
  933. memcg_memory_event(oom_group, MEMCG_OOM_GROUP_KILL);
  934. mem_cgroup_print_oom_group(oom_group);
  935. mem_cgroup_scan_tasks(oom_group, oom_kill_memcg_member,
  936. (void *)message);
  937. mem_cgroup_put(oom_group);
  938. }
  939. }
  940. /*
  941. * Determines whether the kernel must panic because of the panic_on_oom sysctl.
  942. */
  943. static void check_panic_on_oom(struct oom_control *oc)
  944. {
  945. if (likely(!sysctl_panic_on_oom))
  946. return;
  947. if (sysctl_panic_on_oom != 2) {
  948. /*
  949. * panic_on_oom == 1 only affects CONSTRAINT_NONE, the kernel
  950. * does not panic for cpuset, mempolicy, or memcg allocation
  951. * failures.
  952. */
  953. if (oc->constraint != CONSTRAINT_NONE)
  954. return;
  955. }
  956. /* Do not panic for oom kills triggered by sysrq */
  957. if (is_sysrq_oom(oc))
  958. return;
  959. dump_header(oc, NULL);
  960. panic("Out of memory: %s panic_on_oom is enabled\n",
  961. sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide");
  962. }
  963. static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
  964. int register_oom_notifier(struct notifier_block *nb)
  965. {
  966. return blocking_notifier_chain_register(&oom_notify_list, nb);
  967. }
  968. EXPORT_SYMBOL_GPL(register_oom_notifier);
  969. int unregister_oom_notifier(struct notifier_block *nb)
  970. {
  971. return blocking_notifier_chain_unregister(&oom_notify_list, nb);
  972. }
  973. EXPORT_SYMBOL_GPL(unregister_oom_notifier);
  974. /**
  975. * out_of_memory - kill the "best" process when we run out of memory
  976. * @oc: pointer to struct oom_control
  977. *
  978. * If we run out of memory, we have the choice between either
  979. * killing a random task (bad), letting the system crash (worse)
  980. * OR try to be smart about which process to kill. Note that we
  981. * don't have to be perfect here, we just have to be good.
  982. */
  983. bool out_of_memory(struct oom_control *oc)
  984. {
  985. unsigned long freed = 0;
  986. if (oom_killer_disabled)
  987. return false;
  988. if (!is_memcg_oom(oc)) {
  989. blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
  990. if (freed > 0 && !is_sysrq_oom(oc))
  991. /* Got some memory back in the last second. */
  992. return true;
  993. }
  994. /*
  995. * If current has a pending SIGKILL or is exiting, then automatically
  996. * select it. The goal is to allow it to allocate so that it may
  997. * quickly exit and free its memory.
  998. */
  999. if (task_will_free_mem(current)) {
  1000. mark_oom_victim(current);
  1001. queue_oom_reaper(current);
  1002. return true;
  1003. }
  1004. /*
  1005. * The OOM killer does not compensate for IO-less reclaim.
  1006. * pagefault_out_of_memory lost its gfp context so we have to
  1007. * make sure exclude 0 mask - all other users should have at least
  1008. * ___GFP_DIRECT_RECLAIM to get here. But mem_cgroup_oom() has to
  1009. * invoke the OOM killer even if it is a GFP_NOFS allocation.
  1010. */
  1011. if (oc->gfp_mask && !(oc->gfp_mask & __GFP_FS) && !is_memcg_oom(oc))
  1012. return true;
  1013. /*
  1014. * Check if there were limitations on the allocation (only relevant for
  1015. * NUMA and memcg) that may require different handling.
  1016. */
  1017. oc->constraint = constrained_alloc(oc);
  1018. if (oc->constraint != CONSTRAINT_MEMORY_POLICY)
  1019. oc->nodemask = NULL;
  1020. check_panic_on_oom(oc);
  1021. if (!is_memcg_oom(oc) && sysctl_oom_kill_allocating_task &&
  1022. current->mm && !oom_unkillable_task(current) &&
  1023. oom_cpuset_eligible(current, oc) &&
  1024. current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {
  1025. get_task_struct(current);
  1026. oc->chosen = current;
  1027. oom_kill_process(oc, "Out of memory (oom_kill_allocating_task)");
  1028. return true;
  1029. }
  1030. select_bad_process(oc);
  1031. /* Found nothing?!?! */
  1032. if (!oc->chosen) {
  1033. dump_header(oc, NULL);
  1034. pr_warn("Out of memory and no killable processes...\n");
  1035. /*
  1036. * If we got here due to an actual allocation at the
  1037. * system level, we cannot survive this and will enter
  1038. * an endless loop in the allocator. Bail out now.
  1039. */
  1040. if (!is_sysrq_oom(oc) && !is_memcg_oom(oc))
  1041. panic("System is deadlocked on memory\n");
  1042. }
  1043. if (oc->chosen && oc->chosen != (void *)-1UL)
  1044. oom_kill_process(oc, !is_memcg_oom(oc) ? "Out of memory" :
  1045. "Memory cgroup out of memory");
  1046. return !!oc->chosen;
  1047. }
  1048. /*
  1049. * The pagefault handler calls here because some allocation has failed. We have
  1050. * to take care of the memcg OOM here because this is the only safe context without
  1051. * any locks held but let the oom killer triggered from the allocation context care
  1052. * about the global OOM.
  1053. */
  1054. void pagefault_out_of_memory(void)
  1055. {
  1056. static DEFINE_RATELIMIT_STATE(pfoom_rs, DEFAULT_RATELIMIT_INTERVAL,
  1057. DEFAULT_RATELIMIT_BURST);
  1058. if (mem_cgroup_oom_synchronize(true))
  1059. return;
  1060. if (fatal_signal_pending(current))
  1061. return;
  1062. if (__ratelimit(&pfoom_rs))
  1063. pr_warn("Huh VM_FAULT_OOM leaked out to the #PF handler. Retrying PF\n");
  1064. }
  1065. SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
  1066. {
  1067. #ifdef CONFIG_MMU
  1068. struct mm_struct *mm = NULL;
  1069. struct task_struct *task;
  1070. struct task_struct *p;
  1071. unsigned int f_flags;
  1072. bool reap = false;
  1073. long ret = 0;
  1074. if (flags)
  1075. return -EINVAL;
  1076. task = pidfd_get_task(pidfd, &f_flags);
  1077. if (IS_ERR(task))
  1078. return PTR_ERR(task);
  1079. /*
  1080. * Make sure to choose a thread which still has a reference to mm
  1081. * during the group exit
  1082. */
  1083. p = find_lock_task_mm(task);
  1084. if (!p) {
  1085. ret = -ESRCH;
  1086. goto put_task;
  1087. }
  1088. mm = p->mm;
  1089. mmgrab(mm);
  1090. if (task_will_free_mem(p))
  1091. reap = true;
  1092. else {
  1093. /* Error only if the work has not been done already */
  1094. if (!test_bit(MMF_OOM_SKIP, &mm->flags))
  1095. ret = -EINVAL;
  1096. }
  1097. task_unlock(p);
  1098. if (!reap)
  1099. goto drop_mm;
  1100. if (mmap_read_lock_killable(mm)) {
  1101. ret = -EINTR;
  1102. goto drop_mm;
  1103. }
  1104. /*
  1105. * Check MMF_OOM_SKIP again under mmap_read_lock protection to ensure
  1106. * possible change in exit_mmap is seen
  1107. */
  1108. if (!test_bit(MMF_OOM_SKIP, &mm->flags) && !__oom_reap_task_mm(mm))
  1109. ret = -EAGAIN;
  1110. mmap_read_unlock(mm);
  1111. drop_mm:
  1112. mmdrop(mm);
  1113. put_task:
  1114. put_task_struct(task);
  1115. return ret;
  1116. #else
  1117. return -ENOSYS;
  1118. #endif /* CONFIG_MMU */
  1119. }
  1120. void add_to_oom_reaper(struct task_struct *p)
  1121. {
  1122. p = find_lock_task_mm(p);
  1123. if (!p)
  1124. return;
  1125. if (task_will_free_mem(p)) {
  1126. __mark_oom_victim(p);
  1127. queue_oom_reaper(p);
  1128. }
  1129. task_unlock(p);
  1130. }