oom_kill.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. /*
  371. * Send SIGKILL to the selected process irrespective of CAP_SYS_RAW_IO
  372. * flag though it's unlikely that we select a process with CAP_SYS_RAW_IO
  373. * set.
  374. */
  375. static void __oom_kill_task(struct task_struct *p, int verbose)
  376. {
  377. if (is_global_init(p)) {
  378. WARN_ON(1);
  379. printk(KERN_WARNING "tried to kill init!\n");
  380. return;
  381. }
  382. p = find_lock_task_mm(p);
  383. if (!p)
  384. return;
  385. if (verbose)
  386. printk(KERN_ERR "Killed process %d (%s) "
  387. "vsz:%lukB, anon-rss:%lukB, file-rss:%lukB\n",
  388. task_pid_nr(p), p->comm,
  389. K(p->mm->total_vm),
  390. K(get_mm_counter(p->mm, MM_ANONPAGES)),
  391. K(get_mm_counter(p->mm, MM_FILEPAGES)));
  392. task_unlock(p);
  393. /*
  394. * We give our sacrificial lamb high priority and access to
  395. * all the memory it needs. That way it should be able to
  396. * exit() and clear out its resources quickly...
  397. */
  398. p->rt.time_slice = HZ;
  399. set_tsk_thread_flag(p, TIF_MEMDIE);
  400. force_sig(SIGKILL, p);
  401. }
  402. static int oom_kill_task(struct task_struct *p)
  403. {
  404. /* WARNING: mm may not be dereferenced since we did not obtain its
  405. * value from get_task_mm(p). This is OK since all we need to do is
  406. * compare mm to q->mm below.
  407. *
  408. * Furthermore, even if mm contains a non-NULL value, p->mm may
  409. * change to NULL at any time since we do not hold task_lock(p).
  410. * However, this is of no concern to us.
  411. */
  412. if (!p->mm || p->signal->oom_adj == OOM_DISABLE)
  413. return 1;
  414. __oom_kill_task(p, 1);
  415. return 0;
  416. }
  417. static int oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
  418. unsigned long points, struct mem_cgroup *mem,
  419. const char *message)
  420. {
  421. struct task_struct *victim = p;
  422. struct task_struct *child;
  423. struct task_struct *t = p;
  424. unsigned long victim_points = 0;
  425. struct timespec uptime;
  426. if (printk_ratelimit())
  427. dump_header(p, gfp_mask, order, mem);
  428. /*
  429. * If the task is already exiting, don't alarm the sysadmin or kill
  430. * its children or threads, just set TIF_MEMDIE so it can die quickly
  431. */
  432. if (p->flags & PF_EXITING) {
  433. set_tsk_thread_flag(p, TIF_MEMDIE);
  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. if (child->mm == p->mm)
  451. continue;
  452. if (mem && !task_in_mem_cgroup(child, mem))
  453. continue;
  454. /* badness() returns 0 if the thread is unkillable */
  455. child_points = badness(child, uptime.tv_sec);
  456. if (child_points > victim_points) {
  457. victim = child;
  458. victim_points = child_points;
  459. }
  460. }
  461. } while_each_thread(p, t);
  462. return oom_kill_task(victim);
  463. }
  464. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  465. void mem_cgroup_out_of_memory(struct mem_cgroup *mem, gfp_t gfp_mask)
  466. {
  467. unsigned long points = 0;
  468. struct task_struct *p;
  469. if (sysctl_panic_on_oom == 2)
  470. panic("out of memory(memcg). panic_on_oom is selected.\n");
  471. read_lock(&tasklist_lock);
  472. retry:
  473. p = select_bad_process(&points, mem, CONSTRAINT_NONE, NULL);
  474. if (!p || PTR_ERR(p) == -1UL)
  475. goto out;
  476. if (oom_kill_process(p, gfp_mask, 0, points, mem,
  477. "Memory cgroup out of memory"))
  478. goto retry;
  479. out:
  480. read_unlock(&tasklist_lock);
  481. }
  482. #endif
  483. static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
  484. int register_oom_notifier(struct notifier_block *nb)
  485. {
  486. return blocking_notifier_chain_register(&oom_notify_list, nb);
  487. }
  488. EXPORT_SYMBOL_GPL(register_oom_notifier);
  489. int unregister_oom_notifier(struct notifier_block *nb)
  490. {
  491. return blocking_notifier_chain_unregister(&oom_notify_list, nb);
  492. }
  493. EXPORT_SYMBOL_GPL(unregister_oom_notifier);
  494. /*
  495. * Try to acquire the OOM killer lock for the zones in zonelist. Returns zero
  496. * if a parallel OOM killing is already taking place that includes a zone in
  497. * the zonelist. Otherwise, locks all zones in the zonelist and returns 1.
  498. */
  499. int try_set_zone_oom(struct zonelist *zonelist, gfp_t gfp_mask)
  500. {
  501. struct zoneref *z;
  502. struct zone *zone;
  503. int ret = 1;
  504. spin_lock(&zone_scan_lock);
  505. for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
  506. if (zone_is_oom_locked(zone)) {
  507. ret = 0;
  508. goto out;
  509. }
  510. }
  511. for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
  512. /*
  513. * Lock each zone in the zonelist under zone_scan_lock so a
  514. * parallel invocation of try_set_zone_oom() doesn't succeed
  515. * when it shouldn't.
  516. */
  517. zone_set_flag(zone, ZONE_OOM_LOCKED);
  518. }
  519. out:
  520. spin_unlock(&zone_scan_lock);
  521. return ret;
  522. }
  523. /*
  524. * Clears the ZONE_OOM_LOCKED flag for all zones in the zonelist so that failed
  525. * allocation attempts with zonelists containing them may now recall the OOM
  526. * killer, if necessary.
  527. */
  528. void clear_zonelist_oom(struct zonelist *zonelist, gfp_t gfp_mask)
  529. {
  530. struct zoneref *z;
  531. struct zone *zone;
  532. spin_lock(&zone_scan_lock);
  533. for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
  534. zone_clear_flag(zone, ZONE_OOM_LOCKED);
  535. }
  536. spin_unlock(&zone_scan_lock);
  537. }
  538. /*
  539. * Must be called with tasklist_lock held for read.
  540. */
  541. static void __out_of_memory(gfp_t gfp_mask, int order,
  542. enum oom_constraint constraint, const nodemask_t *mask)
  543. {
  544. struct task_struct *p;
  545. unsigned long points;
  546. if (sysctl_oom_kill_allocating_task)
  547. if (!oom_kill_process(current, gfp_mask, order, 0, NULL,
  548. "Out of memory (oom_kill_allocating_task)"))
  549. return;
  550. retry:
  551. /*
  552. * Rambo mode: Shoot down a process and hope it solves whatever
  553. * issues we may have.
  554. */
  555. p = select_bad_process(&points, NULL, constraint, mask);
  556. if (PTR_ERR(p) == -1UL)
  557. return;
  558. /* Found nothing?!?! Either we hang forever, or we panic. */
  559. if (!p) {
  560. read_unlock(&tasklist_lock);
  561. dump_header(NULL, gfp_mask, order, NULL);
  562. panic("Out of memory and no killable processes...\n");
  563. }
  564. if (oom_kill_process(p, gfp_mask, order, points, NULL,
  565. "Out of memory"))
  566. goto retry;
  567. }
  568. /*
  569. * pagefault handler calls into here because it is out of memory but
  570. * doesn't know exactly how or why.
  571. */
  572. void pagefault_out_of_memory(void)
  573. {
  574. unsigned long freed = 0;
  575. blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
  576. if (freed > 0)
  577. /* Got some memory back in the last second. */
  578. return;
  579. if (sysctl_panic_on_oom)
  580. panic("out of memory from page fault. panic_on_oom is selected.\n");
  581. read_lock(&tasklist_lock);
  582. /* unknown gfp_mask and order */
  583. __out_of_memory(0, 0, CONSTRAINT_NONE, NULL);
  584. read_unlock(&tasklist_lock);
  585. /*
  586. * Give "p" a good chance of killing itself before we
  587. * retry to allocate memory.
  588. */
  589. if (!test_thread_flag(TIF_MEMDIE))
  590. schedule_timeout_uninterruptible(1);
  591. }
  592. /**
  593. * out_of_memory - kill the "best" process when we run out of memory
  594. * @zonelist: zonelist pointer
  595. * @gfp_mask: memory allocation flags
  596. * @order: amount of memory being requested as a power of 2
  597. * @nodemask: nodemask passed to page allocator
  598. *
  599. * If we run out of memory, we have the choice between either
  600. * killing a random task (bad), letting the system crash (worse)
  601. * OR try to be smart about which process to kill. Note that we
  602. * don't have to be perfect here, we just have to be good.
  603. */
  604. void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask,
  605. int order, nodemask_t *nodemask)
  606. {
  607. unsigned long freed = 0;
  608. enum oom_constraint constraint;
  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. return;
  621. }
  622. if (sysctl_panic_on_oom == 2) {
  623. dump_header(NULL, gfp_mask, order, NULL);
  624. panic("out of memory. Compulsory panic_on_oom is selected.\n");
  625. }
  626. /*
  627. * Check if there were limitations on the allocation (only relevant for
  628. * NUMA) that may require different handling.
  629. */
  630. constraint = constrained_alloc(zonelist, gfp_mask, nodemask);
  631. read_lock(&tasklist_lock);
  632. if (unlikely(sysctl_panic_on_oom)) {
  633. /*
  634. * panic_on_oom only affects CONSTRAINT_NONE, the kernel
  635. * should not panic for cpuset or mempolicy induced memory
  636. * failures.
  637. */
  638. if (constraint == CONSTRAINT_NONE) {
  639. dump_header(NULL, gfp_mask, order, NULL);
  640. read_unlock(&tasklist_lock);
  641. panic("Out of memory: panic_on_oom is enabled\n");
  642. }
  643. }
  644. __out_of_memory(gfp_mask, order, constraint, nodemask);
  645. read_unlock(&tasklist_lock);
  646. /*
  647. * Give "p" a good chance of killing itself before we
  648. * retry to allocate memory unless "p" is current
  649. */
  650. if (!test_thread_flag(TIF_MEMDIE))
  651. schedule_timeout_uninterruptible(1);
  652. }