oom_kill.c 34 KB

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