oom_kill.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * linux/mm/oom_kill.c
  3. *
  4. * Copyright (C) 1998,2000 Rik van Riel
  5. * Thanks go out to Claus Fischer for some serious inspiration and
  6. * for goading me into coding this file...
  7. * Copyright (C) 2010 Google, Inc.
  8. * Rewritten by David Rientjes
  9. *
  10. * The routines in this file are used to kill a process when
  11. * we're seriously out of memory. This gets called from __alloc_pages()
  12. * in mm/page_alloc.c when we really run out of memory.
  13. *
  14. * Since we won't call these routines often (on a well-configured
  15. * machine) this file will double as a 'coding guide' and a signpost
  16. * for newbie kernel hackers. It features several pointers to major
  17. * kernel subsystems and hints as to where to find out what things do.
  18. */
  19. #include <linux/oom.h>
  20. #include <linux/mm.h>
  21. #include <linux/err.h>
  22. #include <linux/gfp.h>
  23. #include <linux/sched.h>
  24. #include <linux/swap.h>
  25. #include <linux/timex.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/cpuset.h>
  28. #include <linux/module.h>
  29. #include <linux/notifier.h>
  30. #include <linux/memcontrol.h>
  31. #include <linux/mempolicy.h>
  32. #include <linux/security.h>
  33. #include <linux/ptrace.h>
  34. int sysctl_panic_on_oom;
  35. int sysctl_oom_kill_allocating_task;
  36. int sysctl_oom_dump_tasks = 1;
  37. static DEFINE_SPINLOCK(zone_scan_lock);
  38. #ifdef CONFIG_NUMA
  39. /**
  40. * has_intersects_mems_allowed() - check task eligiblity for kill
  41. * @tsk: task struct of which task to consider
  42. * @mask: nodemask passed to page allocator for mempolicy ooms
  43. *
  44. * Task eligibility is determined by whether or not a candidate task, @tsk,
  45. * shares the same mempolicy nodes as current if it is bound by such a policy
  46. * and whether or not it has the same set of allowed cpuset nodes.
  47. */
  48. static bool has_intersects_mems_allowed(struct task_struct *tsk,
  49. const nodemask_t *mask)
  50. {
  51. struct task_struct *start = tsk;
  52. do {
  53. if (mask) {
  54. /*
  55. * If this is a mempolicy constrained oom, tsk's
  56. * cpuset is irrelevant. Only return true if its
  57. * mempolicy intersects current, otherwise it may be
  58. * needlessly killed.
  59. */
  60. if (mempolicy_nodemask_intersects(tsk, mask))
  61. return true;
  62. } else {
  63. /*
  64. * This is not a mempolicy constrained oom, so only
  65. * check the mems of tsk's cpuset.
  66. */
  67. if (cpuset_mems_allowed_intersects(current, tsk))
  68. return true;
  69. }
  70. } while_each_thread(start, tsk);
  71. return false;
  72. }
  73. #else
  74. static bool has_intersects_mems_allowed(struct task_struct *tsk,
  75. const nodemask_t *mask)
  76. {
  77. return true;
  78. }
  79. #endif /* CONFIG_NUMA */
  80. /*
  81. * If this is a system OOM (not a memcg OOM) and the task selected to be
  82. * killed is not already running at high (RT) priorities, speed up the
  83. * recovery by boosting the dying task to the lowest FIFO priority.
  84. * That helps with the recovery and avoids interfering with RT tasks.
  85. */
  86. static void boost_dying_task_prio(struct task_struct *p,
  87. struct mem_cgroup *mem)
  88. {
  89. struct sched_param param = { .sched_priority = 1 };
  90. if (mem)
  91. return;
  92. if (!rt_task(p))
  93. sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
  94. }
  95. /*
  96. * The process p may have detached its own ->mm while exiting or through
  97. * use_mm(), but one or more of its subthreads may still have a valid
  98. * pointer. Return p, or any of its subthreads with a valid ->mm, with
  99. * task_lock() held.
  100. */
  101. struct task_struct *find_lock_task_mm(struct task_struct *p)
  102. {
  103. struct task_struct *t = p;
  104. do {
  105. task_lock(t);
  106. if (likely(t->mm))
  107. return t;
  108. task_unlock(t);
  109. } while_each_thread(p, t);
  110. return NULL;
  111. }
  112. /* return true if the task is not adequate as candidate victim task. */
  113. static bool oom_unkillable_task(struct task_struct *p,
  114. const struct mem_cgroup *mem, const nodemask_t *nodemask)
  115. {
  116. if (is_global_init(p))
  117. return true;
  118. if (p->flags & PF_KTHREAD)
  119. return true;
  120. /* When mem_cgroup_out_of_memory() and p is not member of the group */
  121. if (mem && !task_in_mem_cgroup(p, mem))
  122. return true;
  123. /* p may not have freeable memory in nodemask */
  124. if (!has_intersects_mems_allowed(p, nodemask))
  125. return true;
  126. return false;
  127. }
  128. /**
  129. * oom_badness - heuristic function to determine which candidate task to kill
  130. * @p: task struct of which task we should calculate
  131. * @totalpages: total present RAM allowed for page allocation
  132. *
  133. * The heuristic for determining which task to kill is made to be as simple and
  134. * predictable as possible. The goal is to return the highest value for the
  135. * task consuming the most memory to avoid subsequent oom failures.
  136. */
  137. unsigned int oom_badness(struct task_struct *p, struct mem_cgroup *mem,
  138. const nodemask_t *nodemask, unsigned long totalpages)
  139. {
  140. int points;
  141. if (oom_unkillable_task(p, mem, nodemask))
  142. return 0;
  143. p = find_lock_task_mm(p);
  144. if (!p)
  145. return 0;
  146. /*
  147. * Shortcut check for a thread sharing p->mm that is OOM_SCORE_ADJ_MIN
  148. * so the entire heuristic doesn't need to be executed for something
  149. * that cannot be killed.
  150. */
  151. if (atomic_read(&p->mm->oom_disable_count)) {
  152. task_unlock(p);
  153. return 0;
  154. }
  155. /*
  156. * When the PF_OOM_ORIGIN bit is set, it indicates the task should have
  157. * priority for oom killing.
  158. */
  159. if (p->flags & PF_OOM_ORIGIN) {
  160. task_unlock(p);
  161. return 1000;
  162. }
  163. /*
  164. * The memory controller may have a limit of 0 bytes, so avoid a divide
  165. * by zero, if necessary.
  166. */
  167. if (!totalpages)
  168. totalpages = 1;
  169. /*
  170. * The baseline for the badness score is the proportion of RAM that each
  171. * task's rss and swap space use.
  172. */
  173. points = (get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS)) * 1000 /
  174. totalpages;
  175. task_unlock(p);
  176. /*
  177. * Root processes get 3% bonus, just like the __vm_enough_memory()
  178. * implementation used by LSMs.
  179. */
  180. if (has_capability_noaudit(p, CAP_SYS_ADMIN))
  181. points -= 30;
  182. /*
  183. * /proc/pid/oom_score_adj ranges from -1000 to +1000 such that it may
  184. * either completely disable oom killing or always prefer a certain
  185. * task.
  186. */
  187. points += p->signal->oom_score_adj;
  188. /*
  189. * Never return 0 for an eligible task that may be killed since it's
  190. * possible that no single user task uses more than 0.1% of memory and
  191. * no single admin tasks uses more than 3.0%.
  192. */
  193. if (points <= 0)
  194. return 1;
  195. return (points < 1000) ? points : 1000;
  196. }
  197. /*
  198. * Determine the type of allocation constraint.
  199. */
  200. #ifdef CONFIG_NUMA
  201. static enum oom_constraint constrained_alloc(struct zonelist *zonelist,
  202. gfp_t gfp_mask, nodemask_t *nodemask,
  203. unsigned long *totalpages)
  204. {
  205. struct zone *zone;
  206. struct zoneref *z;
  207. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  208. bool cpuset_limited = false;
  209. int nid;
  210. /* Default to all available memory */
  211. *totalpages = totalram_pages + total_swap_pages;
  212. if (!zonelist)
  213. return CONSTRAINT_NONE;
  214. /*
  215. * Reach here only when __GFP_NOFAIL is used. So, we should avoid
  216. * to kill current.We have to random task kill in this case.
  217. * Hopefully, CONSTRAINT_THISNODE...but no way to handle it, now.
  218. */
  219. if (gfp_mask & __GFP_THISNODE)
  220. return CONSTRAINT_NONE;
  221. /*
  222. * This is not a __GFP_THISNODE allocation, so a truncated nodemask in
  223. * the page allocator means a mempolicy is in effect. Cpuset policy
  224. * is enforced in get_page_from_freelist().
  225. */
  226. if (nodemask && !nodes_subset(node_states[N_HIGH_MEMORY], *nodemask)) {
  227. *totalpages = total_swap_pages;
  228. for_each_node_mask(nid, *nodemask)
  229. *totalpages += node_spanned_pages(nid);
  230. return CONSTRAINT_MEMORY_POLICY;
  231. }
  232. /* Check this allocation failure is caused by cpuset's wall function */
  233. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  234. high_zoneidx, nodemask)
  235. if (!cpuset_zone_allowed_softwall(zone, gfp_mask))
  236. cpuset_limited = true;
  237. if (cpuset_limited) {
  238. *totalpages = total_swap_pages;
  239. for_each_node_mask(nid, cpuset_current_mems_allowed)
  240. *totalpages += node_spanned_pages(nid);
  241. return CONSTRAINT_CPUSET;
  242. }
  243. return CONSTRAINT_NONE;
  244. }
  245. #else
  246. static enum oom_constraint constrained_alloc(struct zonelist *zonelist,
  247. gfp_t gfp_mask, nodemask_t *nodemask,
  248. unsigned long *totalpages)
  249. {
  250. *totalpages = totalram_pages + total_swap_pages;
  251. return CONSTRAINT_NONE;
  252. }
  253. #endif
  254. /*
  255. * Simple selection loop. We chose the process with the highest
  256. * number of 'points'. We expect the caller will lock the tasklist.
  257. *
  258. * (not docbooked, we don't want this one cluttering up the manual)
  259. */
  260. static struct task_struct *select_bad_process(unsigned int *ppoints,
  261. unsigned long totalpages, struct mem_cgroup *mem,
  262. const nodemask_t *nodemask)
  263. {
  264. struct task_struct *g, *p;
  265. struct task_struct *chosen = NULL;
  266. *ppoints = 0;
  267. do_each_thread(g, p) {
  268. unsigned int points;
  269. if (!p->mm)
  270. continue;
  271. if (oom_unkillable_task(p, mem, nodemask))
  272. continue;
  273. /*
  274. * This task already has access to memory reserves and is
  275. * being killed. Don't allow any other task access to the
  276. * memory reserve.
  277. *
  278. * Note: this may have a chance of deadlock if it gets
  279. * blocked waiting for another task which itself is waiting
  280. * for memory. Is there a better alternative?
  281. */
  282. if (test_tsk_thread_flag(p, TIF_MEMDIE))
  283. return ERR_PTR(-1UL);
  284. if (p->flags & PF_EXITING) {
  285. /*
  286. * If p is the current task and is in the process of
  287. * releasing memory, we allow the "kill" to set
  288. * TIF_MEMDIE, which will allow it to gain access to
  289. * memory reserves. Otherwise, it may stall forever.
  290. *
  291. * The loop isn't broken here, however, in case other
  292. * threads are found to have already been oom killed.
  293. */
  294. if (p == current) {
  295. chosen = p;
  296. *ppoints = 1000;
  297. } else {
  298. /*
  299. * If this task is not being ptraced on exit,
  300. * then wait for it to finish before killing
  301. * some other task unnecessarily.
  302. */
  303. if (!(task_ptrace(p->group_leader) &
  304. PT_TRACE_EXIT))
  305. return ERR_PTR(-1UL);
  306. }
  307. }
  308. points = oom_badness(p, mem, nodemask, totalpages);
  309. if (points > *ppoints) {
  310. chosen = p;
  311. *ppoints = points;
  312. }
  313. } while_each_thread(g, p);
  314. return chosen;
  315. }
  316. /**
  317. * dump_tasks - dump current memory state of all system tasks
  318. * @mem: current's memory controller, if constrained
  319. * @nodemask: nodemask passed to page allocator for mempolicy ooms
  320. *
  321. * Dumps the current memory state of all eligible tasks. Tasks not in the same
  322. * memcg, not in the same cpuset, or bound to a disjoint set of mempolicy nodes
  323. * are not shown.
  324. * State information includes task's pid, uid, tgid, vm size, rss, cpu, oom_adj
  325. * value, oom_score_adj value, and name.
  326. *
  327. * Call with tasklist_lock read-locked.
  328. */
  329. static void dump_tasks(const struct mem_cgroup *mem, const nodemask_t *nodemask)
  330. {
  331. struct task_struct *p;
  332. struct task_struct *task;
  333. pr_info("[ pid ] uid tgid total_vm rss cpu oom_adj oom_score_adj name\n");
  334. for_each_process(p) {
  335. if (oom_unkillable_task(p, mem, nodemask))
  336. continue;
  337. task = find_lock_task_mm(p);
  338. if (!task) {
  339. /*
  340. * This is a kthread or all of p's threads have already
  341. * detached their mm's. There's no need to report
  342. * them; they can't be oom killed anyway.
  343. */
  344. continue;
  345. }
  346. pr_info("[%5d] %5d %5d %8lu %8lu %3u %3d %5d %s\n",
  347. task->pid, task_uid(task), task->tgid,
  348. task->mm->total_vm, get_mm_rss(task->mm),
  349. task_cpu(task), task->signal->oom_adj,
  350. task->signal->oom_score_adj, task->comm);
  351. task_unlock(task);
  352. }
  353. }
  354. static void dump_header(struct task_struct *p, gfp_t gfp_mask, int order,
  355. struct mem_cgroup *mem, const nodemask_t *nodemask)
  356. {
  357. task_lock(current);
  358. pr_warning("%s invoked oom-killer: gfp_mask=0x%x, order=%d, "
  359. "oom_adj=%d, oom_score_adj=%d\n",
  360. current->comm, gfp_mask, order, current->signal->oom_adj,
  361. current->signal->oom_score_adj);
  362. cpuset_print_task_mems_allowed(current);
  363. task_unlock(current);
  364. dump_stack();
  365. mem_cgroup_print_oom_info(mem, p);
  366. show_mem(SHOW_MEM_FILTER_NODES);
  367. if (sysctl_oom_dump_tasks)
  368. dump_tasks(mem, nodemask);
  369. }
  370. #define K(x) ((x) << (PAGE_SHIFT-10))
  371. static int oom_kill_task(struct task_struct *p, struct mem_cgroup *mem)
  372. {
  373. struct task_struct *q;
  374. struct mm_struct *mm;
  375. p = find_lock_task_mm(p);
  376. if (!p)
  377. return 1;
  378. /* mm cannot be safely dereferenced after task_unlock(p) */
  379. mm = p->mm;
  380. pr_err("Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB\n",
  381. task_pid_nr(p), p->comm, K(p->mm->total_vm),
  382. K(get_mm_counter(p->mm, MM_ANONPAGES)),
  383. K(get_mm_counter(p->mm, MM_FILEPAGES)));
  384. task_unlock(p);
  385. /*
  386. * Kill all processes sharing p->mm in other thread groups, if any.
  387. * They don't get access to memory reserves or a higher scheduler
  388. * priority, though, to avoid depletion of all memory or task
  389. * starvation. This prevents mm->mmap_sem livelock when an oom killed
  390. * task cannot exit because it requires the semaphore and its contended
  391. * by another thread trying to allocate memory itself. That thread will
  392. * now get access to memory reserves since it has a pending fatal
  393. * signal.
  394. */
  395. for_each_process(q)
  396. if (q->mm == mm && !same_thread_group(q, p)) {
  397. task_lock(q); /* Protect ->comm from prctl() */
  398. pr_err("Kill process %d (%s) sharing same memory\n",
  399. task_pid_nr(q), q->comm);
  400. task_unlock(q);
  401. force_sig(SIGKILL, q);
  402. }
  403. set_tsk_thread_flag(p, TIF_MEMDIE);
  404. force_sig(SIGKILL, p);
  405. /*
  406. * We give our sacrificial lamb high priority and access to
  407. * all the memory it needs. That way it should be able to
  408. * exit() and clear out its resources quickly...
  409. */
  410. boost_dying_task_prio(p, mem);
  411. return 0;
  412. }
  413. #undef K
  414. static int oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
  415. unsigned int points, unsigned long totalpages,
  416. struct mem_cgroup *mem, nodemask_t *nodemask,
  417. const char *message)
  418. {
  419. struct task_struct *victim = p;
  420. struct task_struct *child;
  421. struct task_struct *t = p;
  422. unsigned int victim_points = 0;
  423. if (printk_ratelimit())
  424. dump_header(p, gfp_mask, order, mem, nodemask);
  425. /*
  426. * If the task is already exiting, don't alarm the sysadmin or kill
  427. * its children or threads, just set TIF_MEMDIE so it can die quickly
  428. */
  429. if (p->flags & PF_EXITING) {
  430. set_tsk_thread_flag(p, TIF_MEMDIE);
  431. boost_dying_task_prio(p, mem);
  432. return 0;
  433. }
  434. task_lock(p);
  435. pr_err("%s: Kill process %d (%s) score %d or sacrifice child\n",
  436. message, task_pid_nr(p), p->comm, points);
  437. task_unlock(p);
  438. /*
  439. * If any of p's children has a different mm and is eligible for kill,
  440. * the one with the highest badness() score is sacrificed for its
  441. * parent. This attempts to lose the minimal amount of work done while
  442. * still freeing memory.
  443. */
  444. do {
  445. list_for_each_entry(child, &t->children, sibling) {
  446. unsigned int child_points;
  447. if (child->mm == p->mm)
  448. continue;
  449. /*
  450. * oom_badness() returns 0 if the thread is unkillable
  451. */
  452. child_points = oom_badness(child, mem, nodemask,
  453. totalpages);
  454. if (child_points > victim_points) {
  455. victim = child;
  456. victim_points = child_points;
  457. }
  458. }
  459. } while_each_thread(p, t);
  460. return oom_kill_task(victim, mem);
  461. }
  462. /*
  463. * Determines whether the kernel must panic because of the panic_on_oom sysctl.
  464. */
  465. static void check_panic_on_oom(enum oom_constraint constraint, gfp_t gfp_mask,
  466. int order, const nodemask_t *nodemask)
  467. {
  468. if (likely(!sysctl_panic_on_oom))
  469. return;
  470. if (sysctl_panic_on_oom != 2) {
  471. /*
  472. * panic_on_oom == 1 only affects CONSTRAINT_NONE, the kernel
  473. * does not panic for cpuset, mempolicy, or memcg allocation
  474. * failures.
  475. */
  476. if (constraint != CONSTRAINT_NONE)
  477. return;
  478. }
  479. read_lock(&tasklist_lock);
  480. dump_header(NULL, gfp_mask, order, NULL, nodemask);
  481. read_unlock(&tasklist_lock);
  482. panic("Out of memory: %s panic_on_oom is enabled\n",
  483. sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide");
  484. }
  485. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  486. void mem_cgroup_out_of_memory(struct mem_cgroup *mem, gfp_t gfp_mask)
  487. {
  488. unsigned long limit;
  489. unsigned int points = 0;
  490. struct task_struct *p;
  491. /*
  492. * If current has a pending SIGKILL, then automatically select it. The
  493. * goal is to allow it to allocate so that it may quickly exit and free
  494. * its memory.
  495. */
  496. if (fatal_signal_pending(current)) {
  497. set_thread_flag(TIF_MEMDIE);
  498. boost_dying_task_prio(current, NULL);
  499. return;
  500. }
  501. check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, 0, NULL);
  502. limit = mem_cgroup_get_limit(mem) >> PAGE_SHIFT;
  503. read_lock(&tasklist_lock);
  504. retry:
  505. p = select_bad_process(&points, limit, mem, NULL);
  506. if (!p || PTR_ERR(p) == -1UL)
  507. goto out;
  508. if (oom_kill_process(p, gfp_mask, 0, points, limit, mem, NULL,
  509. "Memory cgroup out of memory"))
  510. goto retry;
  511. out:
  512. read_unlock(&tasklist_lock);
  513. }
  514. #endif
  515. static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
  516. int register_oom_notifier(struct notifier_block *nb)
  517. {
  518. return blocking_notifier_chain_register(&oom_notify_list, nb);
  519. }
  520. EXPORT_SYMBOL_GPL(register_oom_notifier);
  521. int unregister_oom_notifier(struct notifier_block *nb)
  522. {
  523. return blocking_notifier_chain_unregister(&oom_notify_list, nb);
  524. }
  525. EXPORT_SYMBOL_GPL(unregister_oom_notifier);
  526. /*
  527. * Try to acquire the OOM killer lock for the zones in zonelist. Returns zero
  528. * if a parallel OOM killing is already taking place that includes a zone in
  529. * the zonelist. Otherwise, locks all zones in the zonelist and returns 1.
  530. */
  531. int try_set_zonelist_oom(struct zonelist *zonelist, gfp_t gfp_mask)
  532. {
  533. struct zoneref *z;
  534. struct zone *zone;
  535. int ret = 1;
  536. spin_lock(&zone_scan_lock);
  537. for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
  538. if (zone_is_oom_locked(zone)) {
  539. ret = 0;
  540. goto out;
  541. }
  542. }
  543. for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
  544. /*
  545. * Lock each zone in the zonelist under zone_scan_lock so a
  546. * parallel invocation of try_set_zonelist_oom() doesn't succeed
  547. * when it shouldn't.
  548. */
  549. zone_set_flag(zone, ZONE_OOM_LOCKED);
  550. }
  551. out:
  552. spin_unlock(&zone_scan_lock);
  553. return ret;
  554. }
  555. /*
  556. * Clears the ZONE_OOM_LOCKED flag for all zones in the zonelist so that failed
  557. * allocation attempts with zonelists containing them may now recall the OOM
  558. * killer, if necessary.
  559. */
  560. void clear_zonelist_oom(struct zonelist *zonelist, gfp_t gfp_mask)
  561. {
  562. struct zoneref *z;
  563. struct zone *zone;
  564. spin_lock(&zone_scan_lock);
  565. for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
  566. zone_clear_flag(zone, ZONE_OOM_LOCKED);
  567. }
  568. spin_unlock(&zone_scan_lock);
  569. }
  570. /*
  571. * Try to acquire the oom killer lock for all system zones. Returns zero if a
  572. * parallel oom killing is taking place, otherwise locks all zones and returns
  573. * non-zero.
  574. */
  575. static int try_set_system_oom(void)
  576. {
  577. struct zone *zone;
  578. int ret = 1;
  579. spin_lock(&zone_scan_lock);
  580. for_each_populated_zone(zone)
  581. if (zone_is_oom_locked(zone)) {
  582. ret = 0;
  583. goto out;
  584. }
  585. for_each_populated_zone(zone)
  586. zone_set_flag(zone, ZONE_OOM_LOCKED);
  587. out:
  588. spin_unlock(&zone_scan_lock);
  589. return ret;
  590. }
  591. /*
  592. * Clears ZONE_OOM_LOCKED for all system zones so that failed allocation
  593. * attempts or page faults may now recall the oom killer, if necessary.
  594. */
  595. static void clear_system_oom(void)
  596. {
  597. struct zone *zone;
  598. spin_lock(&zone_scan_lock);
  599. for_each_populated_zone(zone)
  600. zone_clear_flag(zone, ZONE_OOM_LOCKED);
  601. spin_unlock(&zone_scan_lock);
  602. }
  603. /**
  604. * out_of_memory - kill the "best" process when we run out of memory
  605. * @zonelist: zonelist pointer
  606. * @gfp_mask: memory allocation flags
  607. * @order: amount of memory being requested as a power of 2
  608. * @nodemask: nodemask passed to page allocator
  609. *
  610. * If we run out of memory, we have the choice between either
  611. * killing a random task (bad), letting the system crash (worse)
  612. * OR try to be smart about which process to kill. Note that we
  613. * don't have to be perfect here, we just have to be good.
  614. */
  615. void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask,
  616. int order, nodemask_t *nodemask)
  617. {
  618. const nodemask_t *mpol_mask;
  619. struct task_struct *p;
  620. unsigned long totalpages;
  621. unsigned long freed = 0;
  622. unsigned int points;
  623. enum oom_constraint constraint = CONSTRAINT_NONE;
  624. int killed = 0;
  625. blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
  626. if (freed > 0)
  627. /* Got some memory back in the last second. */
  628. return;
  629. /*
  630. * If current has a pending SIGKILL, then automatically select it. The
  631. * goal is to allow it to allocate so that it may quickly exit and free
  632. * its memory.
  633. */
  634. if (fatal_signal_pending(current)) {
  635. set_thread_flag(TIF_MEMDIE);
  636. boost_dying_task_prio(current, NULL);
  637. return;
  638. }
  639. /*
  640. * Check if there were limitations on the allocation (only relevant for
  641. * NUMA) that may require different handling.
  642. */
  643. constraint = constrained_alloc(zonelist, gfp_mask, nodemask,
  644. &totalpages);
  645. mpol_mask = (constraint == CONSTRAINT_MEMORY_POLICY) ? nodemask : NULL;
  646. check_panic_on_oom(constraint, gfp_mask, order, mpol_mask);
  647. read_lock(&tasklist_lock);
  648. if (sysctl_oom_kill_allocating_task &&
  649. !oom_unkillable_task(current, NULL, nodemask) &&
  650. current->mm && !atomic_read(&current->mm->oom_disable_count)) {
  651. /*
  652. * oom_kill_process() needs tasklist_lock held. If it returns
  653. * non-zero, current could not be killed so we must fallback to
  654. * the tasklist scan.
  655. */
  656. if (!oom_kill_process(current, gfp_mask, order, 0, totalpages,
  657. NULL, nodemask,
  658. "Out of memory (oom_kill_allocating_task)"))
  659. goto out;
  660. }
  661. retry:
  662. p = select_bad_process(&points, totalpages, NULL, mpol_mask);
  663. if (PTR_ERR(p) == -1UL)
  664. goto out;
  665. /* Found nothing?!?! Either we hang forever, or we panic. */
  666. if (!p) {
  667. dump_header(NULL, gfp_mask, order, NULL, mpol_mask);
  668. read_unlock(&tasklist_lock);
  669. panic("Out of memory and no killable processes...\n");
  670. }
  671. if (oom_kill_process(p, gfp_mask, order, points, totalpages, NULL,
  672. nodemask, "Out of memory"))
  673. goto retry;
  674. killed = 1;
  675. out:
  676. read_unlock(&tasklist_lock);
  677. /*
  678. * Give "p" a good chance of killing itself before we
  679. * retry to allocate memory unless "p" is current
  680. */
  681. if (killed && !test_thread_flag(TIF_MEMDIE))
  682. schedule_timeout_uninterruptible(1);
  683. }
  684. /*
  685. * The pagefault handler calls here because it is out of memory, so kill a
  686. * memory-hogging task. If a populated zone has ZONE_OOM_LOCKED set, a parallel
  687. * oom killing is already in progress so do nothing. If a task is found with
  688. * TIF_MEMDIE set, it has been killed so do nothing and allow it to exit.
  689. */
  690. void pagefault_out_of_memory(void)
  691. {
  692. if (try_set_system_oom()) {
  693. out_of_memory(NULL, 0, 0, NULL);
  694. clear_system_oom();
  695. }
  696. if (!test_thread_flag(TIF_MEMDIE))
  697. schedule_timeout_uninterruptible(1);
  698. }