oom_kill.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  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/task.h>
  27. #include <linux/sched/debug.h>
  28. #include <linux/swap.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/timex.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/cpuset.h>
  33. #include <linux/export.h>
  34. #include <linux/notifier.h>
  35. #include <linux/memcontrol.h>
  36. #include <linux/mempolicy.h>
  37. #include <linux/security.h>
  38. #include <linux/ptrace.h>
  39. #include <linux/freezer.h>
  40. #include <linux/ftrace.h>
  41. #include <linux/ratelimit.h>
  42. #include <linux/kthread.h>
  43. #include <linux/init.h>
  44. #include <linux/mmu_notifier.h>
  45. #include <linux/cred.h>
  46. #include <linux/nmi.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. mm_flags_test(MMF_OOM_SKIP, p->mm) ||
  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_sum(p->mm) + get_mm_counter_sum(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 (mm_flags_test(MMF_OOM_SKIP, task->signal->oom_mm))
  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 %8lu %8lu %9lu %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_sum(task->mm),
  357. get_mm_counter_sum(task->mm, MM_ANONPAGES), get_mm_counter_sum(task->mm, MM_FILEPAGES),
  358. get_mm_counter_sum(task->mm, MM_SHMEMPAGES), mm_pgtables_bytes(task->mm),
  359. get_mm_counter_sum(task->mm, MM_SWAPENTS),
  360. task->signal->oom_score_adj, task->comm);
  361. task_unlock(task);
  362. return 0;
  363. }
  364. /**
  365. * dump_tasks - dump current memory state of all system tasks
  366. * @oc: pointer to struct oom_control
  367. *
  368. * Dumps the current memory state of all eligible tasks. Tasks not in the same
  369. * memcg, not in the same cpuset, or bound to a disjoint set of mempolicy nodes
  370. * are not shown.
  371. * State information includes task's pid, uid, tgid, vm size, rss,
  372. * pgtables_bytes, swapents, oom_score_adj value, and name.
  373. */
  374. static void dump_tasks(struct oom_control *oc)
  375. {
  376. pr_info("Tasks state (memory values in pages):\n");
  377. pr_info("[ pid ] uid tgid total_vm rss rss_anon rss_file rss_shmem pgtables_bytes swapents oom_score_adj name\n");
  378. if (is_memcg_oom(oc))
  379. mem_cgroup_scan_tasks(oc->memcg, dump_task, oc);
  380. else {
  381. struct task_struct *p;
  382. int i = 0;
  383. rcu_read_lock();
  384. for_each_process(p) {
  385. /* Avoid potential softlockup warning */
  386. if ((++i & 1023) == 0)
  387. touch_softlockup_watchdog();
  388. dump_task(p, oc);
  389. }
  390. rcu_read_unlock();
  391. }
  392. }
  393. static void dump_oom_victim(struct oom_control *oc, struct task_struct *victim)
  394. {
  395. /* one line summary of the oom killer context. */
  396. pr_info("oom-kill:constraint=%s,nodemask=%*pbl",
  397. oom_constraint_text[oc->constraint],
  398. nodemask_pr_args(oc->nodemask));
  399. cpuset_print_current_mems_allowed();
  400. mem_cgroup_print_oom_context(oc->memcg, victim);
  401. pr_cont(",task=%s,pid=%d,uid=%d\n", victim->comm, victim->pid,
  402. from_kuid(&init_user_ns, task_uid(victim)));
  403. }
  404. static void dump_header(struct oom_control *oc)
  405. {
  406. pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%d\n",
  407. current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order,
  408. current->signal->oom_score_adj);
  409. if (!IS_ENABLED(CONFIG_COMPACTION) && oc->order)
  410. pr_warn("COMPACTION is disabled!!!\n");
  411. dump_stack();
  412. if (is_memcg_oom(oc))
  413. mem_cgroup_print_oom_meminfo(oc->memcg);
  414. else {
  415. __show_mem(SHOW_MEM_FILTER_NODES, oc->nodemask, gfp_zone(oc->gfp_mask));
  416. if (should_dump_unreclaim_slab())
  417. dump_unreclaimable_slab();
  418. }
  419. mem_cgroup_show_protected_memory(oc->memcg);
  420. if (sysctl_oom_dump_tasks)
  421. dump_tasks(oc);
  422. }
  423. /*
  424. * Number of OOM victims in flight
  425. */
  426. static atomic_t oom_victims = ATOMIC_INIT(0);
  427. static DECLARE_WAIT_QUEUE_HEAD(oom_victims_wait);
  428. static bool oom_killer_disabled __read_mostly;
  429. /*
  430. * task->mm can be NULL if the task is the exited group leader. So to
  431. * determine whether the task is using a particular mm, we examine all the
  432. * task's threads: if one of those is using this mm then this task was also
  433. * using it.
  434. */
  435. bool process_shares_mm(const struct task_struct *p, const struct mm_struct *mm)
  436. {
  437. const struct task_struct *t;
  438. for_each_thread(p, t) {
  439. const struct mm_struct *t_mm = READ_ONCE(t->mm);
  440. if (t_mm)
  441. return t_mm == mm;
  442. }
  443. return false;
  444. }
  445. #ifdef CONFIG_MMU
  446. /*
  447. * OOM Reaper kernel thread which tries to reap the memory used by the OOM
  448. * victim (if that is possible) to help the OOM killer to move on.
  449. */
  450. static struct task_struct *oom_reaper_th;
  451. static DECLARE_WAIT_QUEUE_HEAD(oom_reaper_wait);
  452. static struct task_struct *oom_reaper_list;
  453. static DEFINE_SPINLOCK(oom_reaper_lock);
  454. static bool __oom_reap_task_mm(struct mm_struct *mm)
  455. {
  456. struct vm_area_struct *vma;
  457. bool ret = true;
  458. MA_STATE(mas, &mm->mm_mt, ULONG_MAX, ULONG_MAX);
  459. /*
  460. * Tell all users of get_user/copy_from_user etc... that the content
  461. * is no longer stable. No barriers really needed because unmapping
  462. * should imply barriers already and the reader would hit a page fault
  463. * if it stumbled over a reaped memory.
  464. */
  465. mm_flags_set(MMF_UNSTABLE, mm);
  466. /*
  467. * It might start racing with the dying task and compete for shared
  468. * resources - e.g. page table lock contention has been observed.
  469. * Reduce those races by reaping the oom victim from the other end
  470. * of the address space.
  471. */
  472. mas_for_each_rev(&mas, vma, 0) {
  473. if (vma->vm_flags & (VM_HUGETLB|VM_PFNMAP))
  474. continue;
  475. /*
  476. * Only anonymous pages have a good chance to be dropped
  477. * without additional steps which we cannot afford as we
  478. * are OOM already.
  479. *
  480. * We do not even care about fs backed pages because all
  481. * which are reclaimable have already been reclaimed and
  482. * we do not want to block exit_mmap by keeping mm ref
  483. * count elevated without a good reason.
  484. */
  485. if (vma_is_anonymous(vma) || !(vma->vm_flags & VM_SHARED)) {
  486. struct mmu_notifier_range range;
  487. struct mmu_gather tlb;
  488. mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0,
  489. mm, vma->vm_start,
  490. vma->vm_end);
  491. tlb_gather_mmu(&tlb, mm);
  492. if (mmu_notifier_invalidate_range_start_nonblock(&range)) {
  493. tlb_finish_mmu(&tlb);
  494. ret = false;
  495. continue;
  496. }
  497. unmap_page_range(&tlb, vma, range.start, range.end, NULL);
  498. mmu_notifier_invalidate_range_end(&range);
  499. tlb_finish_mmu(&tlb);
  500. }
  501. }
  502. return ret;
  503. }
  504. /*
  505. * Reaps the address space of the given task.
  506. *
  507. * Returns true on success and false if none or part of the address space
  508. * has been reclaimed and the caller should retry later.
  509. */
  510. static bool oom_reap_task_mm(struct task_struct *tsk, struct mm_struct *mm)
  511. {
  512. bool ret = true;
  513. if (!mmap_read_trylock(mm)) {
  514. trace_skip_task_reaping(tsk->pid);
  515. return false;
  516. }
  517. /*
  518. * MMF_OOM_SKIP is set by exit_mmap when the OOM reaper can't
  519. * work on the mm anymore. The check for MMF_OOM_SKIP must run
  520. * under mmap_lock for reading because it serializes against the
  521. * mmap_write_lock();mmap_write_unlock() cycle in exit_mmap().
  522. */
  523. if (mm_flags_test(MMF_OOM_SKIP, mm)) {
  524. trace_skip_task_reaping(tsk->pid);
  525. goto out_unlock;
  526. }
  527. trace_start_task_reaping(tsk->pid);
  528. /* failed to reap part of the address space. Try again later */
  529. ret = __oom_reap_task_mm(mm);
  530. if (!ret)
  531. goto out_finish;
  532. pr_info("oom_reaper: reaped process %d (%s), now anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB\n",
  533. task_pid_nr(tsk), tsk->comm,
  534. K(get_mm_counter_sum(mm, MM_ANONPAGES)),
  535. K(get_mm_counter_sum(mm, MM_FILEPAGES)),
  536. K(get_mm_counter_sum(mm, MM_SHMEMPAGES)));
  537. out_finish:
  538. trace_finish_task_reaping(tsk->pid);
  539. out_unlock:
  540. mmap_read_unlock(mm);
  541. return ret;
  542. }
  543. #define MAX_OOM_REAP_RETRIES 10
  544. static void oom_reap_task(struct task_struct *tsk)
  545. {
  546. int attempts = 0;
  547. struct mm_struct *mm = tsk->signal->oom_mm;
  548. /* Retry the mmap_read_trylock(mm) a few times */
  549. while (attempts++ < MAX_OOM_REAP_RETRIES && !oom_reap_task_mm(tsk, mm))
  550. schedule_timeout_idle(HZ/10);
  551. if (attempts <= MAX_OOM_REAP_RETRIES ||
  552. mm_flags_test(MMF_OOM_SKIP, mm))
  553. goto done;
  554. pr_info("oom_reaper: unable to reap pid:%d (%s)\n",
  555. task_pid_nr(tsk), tsk->comm);
  556. sched_show_task(tsk);
  557. debug_show_all_locks();
  558. done:
  559. tsk->oom_reaper_list = NULL;
  560. /*
  561. * Hide this mm from OOM killer because it has been either reaped or
  562. * somebody can't call mmap_write_unlock(mm).
  563. */
  564. mm_flags_set(MMF_OOM_SKIP, mm);
  565. /* Drop a reference taken by queue_oom_reaper */
  566. put_task_struct(tsk);
  567. }
  568. static int oom_reaper(void *unused)
  569. {
  570. set_freezable();
  571. while (true) {
  572. struct task_struct *tsk = NULL;
  573. wait_event_freezable(oom_reaper_wait, oom_reaper_list != NULL);
  574. spin_lock_irq(&oom_reaper_lock);
  575. if (oom_reaper_list != NULL) {
  576. tsk = oom_reaper_list;
  577. oom_reaper_list = tsk->oom_reaper_list;
  578. }
  579. spin_unlock_irq(&oom_reaper_lock);
  580. if (tsk)
  581. oom_reap_task(tsk);
  582. }
  583. return 0;
  584. }
  585. static void wake_oom_reaper(struct timer_list *timer)
  586. {
  587. struct task_struct *tsk = container_of(timer, struct task_struct,
  588. oom_reaper_timer);
  589. struct mm_struct *mm = tsk->signal->oom_mm;
  590. unsigned long flags;
  591. /* The victim managed to terminate on its own - see exit_mmap */
  592. if (mm_flags_test(MMF_OOM_SKIP, mm)) {
  593. put_task_struct(tsk);
  594. return;
  595. }
  596. spin_lock_irqsave(&oom_reaper_lock, flags);
  597. tsk->oom_reaper_list = oom_reaper_list;
  598. oom_reaper_list = tsk;
  599. spin_unlock_irqrestore(&oom_reaper_lock, flags);
  600. trace_wake_reaper(tsk->pid);
  601. wake_up(&oom_reaper_wait);
  602. }
  603. /*
  604. * Give the OOM victim time to exit naturally before invoking the oom_reaping.
  605. * The timers timeout is arbitrary... the longer it is, the longer the worst
  606. * case scenario for the OOM can take. If it is too small, the oom_reaper can
  607. * get in the way and release resources needed by the process exit path.
  608. * e.g. The futex robust list can sit in Anon|Private memory that gets reaped
  609. * before the exit path is able to wake the futex waiters.
  610. */
  611. #define OOM_REAPER_DELAY (2*HZ)
  612. static void queue_oom_reaper(struct task_struct *tsk)
  613. {
  614. /* mm is already queued? */
  615. if (mm_flags_test_and_set(MMF_OOM_REAP_QUEUED, tsk->signal->oom_mm))
  616. return;
  617. get_task_struct(tsk);
  618. timer_setup(&tsk->oom_reaper_timer, wake_oom_reaper, 0);
  619. tsk->oom_reaper_timer.expires = jiffies + OOM_REAPER_DELAY;
  620. add_timer(&tsk->oom_reaper_timer);
  621. }
  622. #ifdef CONFIG_SYSCTL
  623. static const struct ctl_table vm_oom_kill_table[] = {
  624. {
  625. .procname = "panic_on_oom",
  626. .data = &sysctl_panic_on_oom,
  627. .maxlen = sizeof(sysctl_panic_on_oom),
  628. .mode = 0644,
  629. .proc_handler = proc_dointvec_minmax,
  630. .extra1 = SYSCTL_ZERO,
  631. .extra2 = SYSCTL_TWO,
  632. },
  633. {
  634. .procname = "oom_kill_allocating_task",
  635. .data = &sysctl_oom_kill_allocating_task,
  636. .maxlen = sizeof(sysctl_oom_kill_allocating_task),
  637. .mode = 0644,
  638. .proc_handler = proc_dointvec,
  639. },
  640. {
  641. .procname = "oom_dump_tasks",
  642. .data = &sysctl_oom_dump_tasks,
  643. .maxlen = sizeof(sysctl_oom_dump_tasks),
  644. .mode = 0644,
  645. .proc_handler = proc_dointvec,
  646. },
  647. };
  648. #endif
  649. static int __init oom_init(void)
  650. {
  651. oom_reaper_th = kthread_run(oom_reaper, NULL, "oom_reaper");
  652. #ifdef CONFIG_SYSCTL
  653. register_sysctl_init("vm", vm_oom_kill_table);
  654. #endif
  655. return 0;
  656. }
  657. subsys_initcall(oom_init)
  658. #else
  659. static inline void queue_oom_reaper(struct task_struct *tsk)
  660. {
  661. }
  662. #endif /* CONFIG_MMU */
  663. /**
  664. * mark_oom_victim - mark the given task as OOM victim
  665. * @tsk: task to mark
  666. *
  667. * Has to be called with oom_lock held and never after
  668. * oom has been disabled already.
  669. *
  670. * tsk->mm has to be non NULL and caller has to guarantee it is stable (either
  671. * under task_lock or operate on the current).
  672. */
  673. static void mark_oom_victim(struct task_struct *tsk)
  674. {
  675. const struct cred *cred;
  676. struct mm_struct *mm = tsk->mm;
  677. WARN_ON(oom_killer_disabled);
  678. /* OOM killer might race with memcg OOM */
  679. if (test_and_set_tsk_thread_flag(tsk, TIF_MEMDIE))
  680. return;
  681. /* oom_mm is bound to the signal struct life time. */
  682. if (!cmpxchg(&tsk->signal->oom_mm, NULL, mm))
  683. mmgrab(tsk->signal->oom_mm);
  684. /*
  685. * Make sure that the process is woken up from uninterruptible sleep
  686. * if it is frozen because OOM killer wouldn't be able to free any
  687. * memory and livelock. The freezer will thaw the tasks that are OOM
  688. * victims regardless of the PM freezing and cgroup freezing states.
  689. */
  690. thaw_process(tsk);
  691. atomic_inc(&oom_victims);
  692. cred = get_task_cred(tsk);
  693. trace_mark_victim(tsk, cred->uid.val);
  694. put_cred(cred);
  695. }
  696. /**
  697. * exit_oom_victim - note the exit of an OOM victim
  698. */
  699. void exit_oom_victim(void)
  700. {
  701. clear_thread_flag(TIF_MEMDIE);
  702. if (!atomic_dec_return(&oom_victims))
  703. wake_up_all(&oom_victims_wait);
  704. }
  705. /**
  706. * oom_killer_enable - enable OOM killer
  707. */
  708. void oom_killer_enable(void)
  709. {
  710. oom_killer_disabled = false;
  711. pr_info("OOM killer enabled.\n");
  712. }
  713. /**
  714. * oom_killer_disable - disable OOM killer
  715. * @timeout: maximum timeout to wait for oom victims in jiffies
  716. *
  717. * Forces all page allocations to fail rather than trigger OOM killer.
  718. * Will block and wait until all OOM victims are killed or the given
  719. * timeout expires.
  720. *
  721. * The function cannot be called when there are runnable user tasks because
  722. * the userspace would see unexpected allocation failures as a result. Any
  723. * new usage of this function should be consulted with MM people.
  724. *
  725. * Returns true if successful and false if the OOM killer cannot be
  726. * disabled.
  727. */
  728. bool oom_killer_disable(signed long timeout)
  729. {
  730. signed long ret;
  731. /*
  732. * Make sure to not race with an ongoing OOM killer. Check that the
  733. * current is not killed (possibly due to sharing the victim's memory).
  734. */
  735. if (mutex_lock_killable(&oom_lock))
  736. return false;
  737. oom_killer_disabled = true;
  738. mutex_unlock(&oom_lock);
  739. ret = wait_event_interruptible_timeout(oom_victims_wait,
  740. !atomic_read(&oom_victims), timeout);
  741. if (ret <= 0) {
  742. oom_killer_enable();
  743. return false;
  744. }
  745. pr_info("OOM killer disabled.\n");
  746. return true;
  747. }
  748. static inline bool __task_will_free_mem(struct task_struct *task)
  749. {
  750. struct signal_struct *sig = task->signal;
  751. /*
  752. * A coredumping process may sleep for an extended period in
  753. * coredump_task_exit(), so the oom killer cannot assume that
  754. * the process will promptly exit and release memory.
  755. */
  756. if (sig->core_state)
  757. return false;
  758. if (sig->flags & SIGNAL_GROUP_EXIT)
  759. return true;
  760. if (thread_group_empty(task) && (task->flags & PF_EXITING))
  761. return true;
  762. return false;
  763. }
  764. /*
  765. * Checks whether the given task is dying or exiting and likely to
  766. * release its address space. This means that all threads and processes
  767. * sharing the same mm have to be killed or exiting.
  768. * Caller has to make sure that task->mm is stable (hold task_lock or
  769. * it operates on the current).
  770. */
  771. static bool task_will_free_mem(struct task_struct *task)
  772. {
  773. struct mm_struct *mm = task->mm;
  774. struct task_struct *p;
  775. bool ret = true;
  776. /*
  777. * Skip tasks without mm because it might have passed its exit_mm and
  778. * exit_oom_victim. oom_reaper could have rescued that but do not rely
  779. * on that for now. We can consider find_lock_task_mm in future.
  780. */
  781. if (!mm)
  782. return false;
  783. if (!__task_will_free_mem(task))
  784. return false;
  785. /*
  786. * This task has already been drained by the oom reaper so there are
  787. * only small chances it will free some more
  788. */
  789. if (mm_flags_test(MMF_OOM_SKIP, mm))
  790. return false;
  791. if (atomic_read(&mm->mm_users) <= 1)
  792. return true;
  793. /*
  794. * Make sure that all tasks which share the mm with the given tasks
  795. * are dying as well to make sure that a) nobody pins its mm and
  796. * b) the task is also reapable by the oom reaper.
  797. */
  798. rcu_read_lock();
  799. for_each_process(p) {
  800. if (!process_shares_mm(p, mm))
  801. continue;
  802. if (same_thread_group(task, p))
  803. continue;
  804. ret = __task_will_free_mem(p);
  805. if (!ret)
  806. break;
  807. }
  808. rcu_read_unlock();
  809. return ret;
  810. }
  811. static void __oom_kill_process(struct task_struct *victim, const char *message)
  812. {
  813. struct task_struct *p;
  814. struct mm_struct *mm;
  815. bool can_oom_reap = true;
  816. p = find_lock_task_mm(victim);
  817. if (!p) {
  818. pr_info("%s: OOM victim %d (%s) is already exiting. Skip killing the task\n",
  819. message, task_pid_nr(victim), victim->comm);
  820. put_task_struct(victim);
  821. return;
  822. } else if (victim != p) {
  823. get_task_struct(p);
  824. put_task_struct(victim);
  825. victim = p;
  826. }
  827. /* Get a reference to safely compare mm after task_unlock(victim) */
  828. mm = victim->mm;
  829. mmgrab(mm);
  830. /* Raise event before sending signal: task reaper must see this */
  831. count_vm_event(OOM_KILL);
  832. memcg_memory_event_mm(mm, MEMCG_OOM_KILL);
  833. /*
  834. * We should send SIGKILL before granting access to memory reserves
  835. * in order to prevent the OOM victim from depleting the memory
  836. * reserves from the user space under its control.
  837. */
  838. do_send_sig_info(SIGKILL, SEND_SIG_PRIV, victim, PIDTYPE_TGID);
  839. mark_oom_victim(victim);
  840. 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:%d\n",
  841. message, task_pid_nr(victim), victim->comm, K(mm->total_vm),
  842. K(get_mm_counter_sum(mm, MM_ANONPAGES)),
  843. K(get_mm_counter_sum(mm, MM_FILEPAGES)),
  844. K(get_mm_counter_sum(mm, MM_SHMEMPAGES)),
  845. from_kuid(&init_user_ns, task_uid(victim)),
  846. mm_pgtables_bytes(mm) >> 10, victim->signal->oom_score_adj);
  847. task_unlock(victim);
  848. /*
  849. * Kill all user processes sharing victim->mm in other thread groups, if
  850. * any. They don't get access to memory reserves, though, to avoid
  851. * depletion of all memory. This prevents mm->mmap_lock livelock when an
  852. * oom killed thread cannot exit because it requires the semaphore and
  853. * its contended by another thread trying to allocate memory itself.
  854. * That thread will now get access to memory reserves since it has a
  855. * pending fatal signal.
  856. */
  857. rcu_read_lock();
  858. for_each_process(p) {
  859. if (!process_shares_mm(p, mm))
  860. continue;
  861. if (same_thread_group(p, victim))
  862. continue;
  863. if (is_global_init(p)) {
  864. can_oom_reap = false;
  865. mm_flags_set(MMF_OOM_SKIP, mm);
  866. pr_info("oom killer %d (%s) has mm pinned by %d (%s)\n",
  867. task_pid_nr(victim), victim->comm,
  868. task_pid_nr(p), p->comm);
  869. continue;
  870. }
  871. /*
  872. * No kthread_use_mm() user needs to read from the userspace so
  873. * we are ok to reap it.
  874. */
  875. if (unlikely(p->flags & PF_KTHREAD))
  876. continue;
  877. do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID);
  878. }
  879. rcu_read_unlock();
  880. if (can_oom_reap)
  881. queue_oom_reaper(victim);
  882. mmdrop(mm);
  883. put_task_struct(victim);
  884. }
  885. /*
  886. * Kill provided task unless it's secured by setting
  887. * oom_score_adj to OOM_SCORE_ADJ_MIN.
  888. */
  889. static int oom_kill_memcg_member(struct task_struct *task, void *message)
  890. {
  891. if (task->signal->oom_score_adj != OOM_SCORE_ADJ_MIN &&
  892. !is_global_init(task)) {
  893. get_task_struct(task);
  894. __oom_kill_process(task, message);
  895. }
  896. return 0;
  897. }
  898. static void oom_kill_process(struct oom_control *oc, const char *message)
  899. {
  900. struct task_struct *victim = oc->chosen;
  901. struct mem_cgroup *oom_group;
  902. static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL,
  903. DEFAULT_RATELIMIT_BURST);
  904. /*
  905. * If the task is already exiting, don't alarm the sysadmin or kill
  906. * its children or threads, just give it access to memory reserves
  907. * so it can die quickly
  908. */
  909. task_lock(victim);
  910. if (task_will_free_mem(victim)) {
  911. mark_oom_victim(victim);
  912. queue_oom_reaper(victim);
  913. task_unlock(victim);
  914. put_task_struct(victim);
  915. return;
  916. }
  917. task_unlock(victim);
  918. if (__ratelimit(&oom_rs)) {
  919. dump_header(oc);
  920. dump_oom_victim(oc, victim);
  921. }
  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);
  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. * But mem_cgroup_oom() has to invoke the OOM killer even
  1007. * if it is a GFP_NOFS allocation.
  1008. */
  1009. if (!(oc->gfp_mask & __GFP_FS) && !is_memcg_oom(oc))
  1010. return true;
  1011. /*
  1012. * Check if there were limitations on the allocation (only relevant for
  1013. * NUMA and memcg) that may require different handling.
  1014. */
  1015. oc->constraint = constrained_alloc(oc);
  1016. if (oc->constraint != CONSTRAINT_MEMORY_POLICY)
  1017. oc->nodemask = NULL;
  1018. check_panic_on_oom(oc);
  1019. if (!is_memcg_oom(oc) && sysctl_oom_kill_allocating_task &&
  1020. current->mm && !oom_unkillable_task(current) &&
  1021. oom_cpuset_eligible(current, oc) &&
  1022. current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {
  1023. get_task_struct(current);
  1024. oc->chosen = current;
  1025. oom_kill_process(oc, "Out of memory (oom_kill_allocating_task)");
  1026. return true;
  1027. }
  1028. select_bad_process(oc);
  1029. /* Found nothing?!?! */
  1030. if (!oc->chosen) {
  1031. dump_header(oc);
  1032. pr_warn("Out of memory and no killable processes...\n");
  1033. /*
  1034. * If we got here due to an actual allocation at the
  1035. * system level, we cannot survive this and will enter
  1036. * an endless loop in the allocator. Bail out now.
  1037. */
  1038. if (!is_sysrq_oom(oc) && !is_memcg_oom(oc))
  1039. panic("System is deadlocked on memory\n");
  1040. }
  1041. if (oc->chosen && oc->chosen != (void *)-1UL)
  1042. oom_kill_process(oc, !is_memcg_oom(oc) ? "Out of memory" :
  1043. "Memory cgroup out of memory");
  1044. return !!oc->chosen;
  1045. }
  1046. /*
  1047. * The pagefault handler calls here because some allocation has failed. We have
  1048. * to take care of the memcg OOM here because this is the only safe context without
  1049. * any locks held but let the oom killer triggered from the allocation context care
  1050. * about the global OOM.
  1051. */
  1052. void pagefault_out_of_memory(void)
  1053. {
  1054. static DEFINE_RATELIMIT_STATE(pfoom_rs, DEFAULT_RATELIMIT_INTERVAL,
  1055. DEFAULT_RATELIMIT_BURST);
  1056. if (mem_cgroup_oom_synchronize(true))
  1057. return;
  1058. if (fatal_signal_pending(current))
  1059. return;
  1060. if (__ratelimit(&pfoom_rs))
  1061. pr_warn("Huh VM_FAULT_OOM leaked out to the #PF handler. Retrying PF\n");
  1062. }
  1063. SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
  1064. {
  1065. #ifdef CONFIG_MMU
  1066. struct mm_struct *mm = NULL;
  1067. struct task_struct *task;
  1068. struct task_struct *p;
  1069. unsigned int f_flags;
  1070. bool reap = false;
  1071. long ret = 0;
  1072. if (flags)
  1073. return -EINVAL;
  1074. task = pidfd_get_task(pidfd, &f_flags);
  1075. if (IS_ERR(task))
  1076. return PTR_ERR(task);
  1077. /*
  1078. * Make sure to choose a thread which still has a reference to mm
  1079. * during the group exit
  1080. */
  1081. p = find_lock_task_mm(task);
  1082. if (!p) {
  1083. ret = -ESRCH;
  1084. goto put_task;
  1085. }
  1086. mm = p->mm;
  1087. mmgrab(mm);
  1088. if (task_will_free_mem(p))
  1089. reap = true;
  1090. else {
  1091. /* Error only if the work has not been done already */
  1092. if (!mm_flags_test(MMF_OOM_SKIP, mm))
  1093. ret = -EINVAL;
  1094. }
  1095. task_unlock(p);
  1096. if (!reap)
  1097. goto drop_mm;
  1098. if (mmap_read_lock_killable(mm)) {
  1099. ret = -EINTR;
  1100. goto drop_mm;
  1101. }
  1102. /*
  1103. * Check MMF_OOM_SKIP again under mmap_read_lock protection to ensure
  1104. * possible change in exit_mmap is seen
  1105. */
  1106. if (!mm_flags_test(MMF_OOM_SKIP, mm) && !__oom_reap_task_mm(mm))
  1107. ret = -EAGAIN;
  1108. mmap_read_unlock(mm);
  1109. drop_mm:
  1110. mmdrop(mm);
  1111. put_task:
  1112. put_task_struct(task);
  1113. return ret;
  1114. #else
  1115. return -ENOSYS;
  1116. #endif /* CONFIG_MMU */
  1117. }