oom_kill.c 20 KB

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