oom_kill.c 21 KB

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