oom_kill.c 20 KB

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