oom_kill.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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, const nodemask_t *nodemask)
  259. {
  260. struct task_struct *p;
  261. struct task_struct *chosen = NULL;
  262. struct timespec uptime;
  263. *ppoints = 0;
  264. do_posix_clock_monotonic_gettime(&uptime);
  265. for_each_process(p) {
  266. unsigned long points;
  267. /* skip the init task and kthreads */
  268. if (is_global_init(p) || (p->flags & PF_KTHREAD))
  269. continue;
  270. if (mem && !task_in_mem_cgroup(p, mem))
  271. continue;
  272. if (!has_intersects_mems_allowed(p, nodemask))
  273. continue;
  274. /*
  275. * This task already has access to memory reserves and is
  276. * being killed. Don't allow any other task access to the
  277. * memory reserve.
  278. *
  279. * Note: this may have a chance of deadlock if it gets
  280. * blocked waiting for another task which itself is waiting
  281. * for memory. Is there a better alternative?
  282. */
  283. if (test_tsk_thread_flag(p, TIF_MEMDIE))
  284. return ERR_PTR(-1UL);
  285. /*
  286. * This is in the process of releasing memory so wait for it
  287. * to finish before killing some other task by mistake.
  288. *
  289. * However, if p is the current task, we allow the 'kill' to
  290. * go ahead if it is exiting: this will simply set TIF_MEMDIE,
  291. * which will allow it to gain access to memory reserves in
  292. * the process of exiting and releasing its resources.
  293. * Otherwise we could get an easy OOM deadlock.
  294. */
  295. if ((p->flags & PF_EXITING) && p->mm) {
  296. if (p != current)
  297. return ERR_PTR(-1UL);
  298. chosen = p;
  299. *ppoints = ULONG_MAX;
  300. }
  301. if (p->signal->oom_adj == OOM_DISABLE)
  302. continue;
  303. points = badness(p, uptime.tv_sec);
  304. if (points > *ppoints || !chosen) {
  305. chosen = p;
  306. *ppoints = points;
  307. }
  308. }
  309. return chosen;
  310. }
  311. /**
  312. * dump_tasks - dump current memory state of all system tasks
  313. * @mem: current's memory controller, if constrained
  314. *
  315. * Dumps the current memory state of all system tasks, excluding kernel threads.
  316. * State information includes task's pid, uid, tgid, vm size, rss, cpu, oom_adj
  317. * score, and name.
  318. *
  319. * If the actual is non-NULL, only tasks that are a member of the mem_cgroup are
  320. * shown.
  321. *
  322. * Call with tasklist_lock read-locked.
  323. */
  324. static void dump_tasks(const struct mem_cgroup *mem)
  325. {
  326. struct task_struct *p;
  327. struct task_struct *task;
  328. printk(KERN_INFO "[ pid ] uid tgid total_vm rss cpu oom_adj "
  329. "name\n");
  330. for_each_process(p) {
  331. if (p->flags & PF_KTHREAD)
  332. continue;
  333. if (mem && !task_in_mem_cgroup(p, mem))
  334. continue;
  335. task = find_lock_task_mm(p);
  336. if (!task) {
  337. /*
  338. * This is a kthread or all of p's threads have already
  339. * detached their mm's. There's no need to report
  340. * them; they can't be oom killed anyway.
  341. */
  342. continue;
  343. }
  344. printk(KERN_INFO "[%5d] %5d %5d %8lu %8lu %3u %3d %s\n",
  345. task->pid, __task_cred(task)->uid, task->tgid,
  346. task->mm->total_vm, get_mm_rss(task->mm),
  347. task_cpu(task), task->signal->oom_adj, task->comm);
  348. task_unlock(task);
  349. }
  350. }
  351. static void dump_header(struct task_struct *p, gfp_t gfp_mask, int order,
  352. struct mem_cgroup *mem)
  353. {
  354. task_lock(current);
  355. pr_warning("%s invoked oom-killer: gfp_mask=0x%x, order=%d, "
  356. "oom_adj=%d\n",
  357. current->comm, gfp_mask, order, current->signal->oom_adj);
  358. cpuset_print_task_mems_allowed(current);
  359. task_unlock(current);
  360. dump_stack();
  361. mem_cgroup_print_oom_info(mem, p);
  362. show_mem();
  363. if (sysctl_oom_dump_tasks)
  364. dump_tasks(mem);
  365. }
  366. #define K(x) ((x) << (PAGE_SHIFT-10))
  367. static int oom_kill_task(struct task_struct *p)
  368. {
  369. p = find_lock_task_mm(p);
  370. if (!p || p->signal->oom_adj == OOM_DISABLE) {
  371. task_unlock(p);
  372. return 1;
  373. }
  374. pr_err("Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB\n",
  375. task_pid_nr(p), p->comm, K(p->mm->total_vm),
  376. K(get_mm_counter(p->mm, MM_ANONPAGES)),
  377. K(get_mm_counter(p->mm, MM_FILEPAGES)));
  378. task_unlock(p);
  379. p->rt.time_slice = HZ;
  380. set_tsk_thread_flag(p, TIF_MEMDIE);
  381. force_sig(SIGKILL, p);
  382. return 0;
  383. }
  384. #undef K
  385. static int oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
  386. unsigned long points, struct mem_cgroup *mem,
  387. const char *message)
  388. {
  389. struct task_struct *victim = p;
  390. struct task_struct *child;
  391. struct task_struct *t = p;
  392. unsigned long victim_points = 0;
  393. struct timespec uptime;
  394. if (printk_ratelimit())
  395. dump_header(p, gfp_mask, order, mem);
  396. /*
  397. * If the task is already exiting, don't alarm the sysadmin or kill
  398. * its children or threads, just set TIF_MEMDIE so it can die quickly
  399. */
  400. if (p->flags & PF_EXITING) {
  401. set_tsk_thread_flag(p, TIF_MEMDIE);
  402. return 0;
  403. }
  404. task_lock(p);
  405. pr_err("%s: Kill process %d (%s) score %lu or sacrifice child\n",
  406. message, task_pid_nr(p), p->comm, points);
  407. task_unlock(p);
  408. /*
  409. * If any of p's children has a different mm and is eligible for kill,
  410. * the one with the highest badness() score is sacrificed for its
  411. * parent. This attempts to lose the minimal amount of work done while
  412. * still freeing memory.
  413. */
  414. do_posix_clock_monotonic_gettime(&uptime);
  415. do {
  416. list_for_each_entry(child, &t->children, sibling) {
  417. unsigned long child_points;
  418. if (child->mm == p->mm)
  419. continue;
  420. if (mem && !task_in_mem_cgroup(child, mem))
  421. continue;
  422. /* badness() returns 0 if the thread is unkillable */
  423. child_points = badness(child, uptime.tv_sec);
  424. if (child_points > victim_points) {
  425. victim = child;
  426. victim_points = child_points;
  427. }
  428. }
  429. } while_each_thread(p, t);
  430. return oom_kill_task(victim);
  431. }
  432. /*
  433. * Determines whether the kernel must panic because of the panic_on_oom sysctl.
  434. */
  435. static void check_panic_on_oom(enum oom_constraint constraint, gfp_t gfp_mask,
  436. int order)
  437. {
  438. if (likely(!sysctl_panic_on_oom))
  439. return;
  440. if (sysctl_panic_on_oom != 2) {
  441. /*
  442. * panic_on_oom == 1 only affects CONSTRAINT_NONE, the kernel
  443. * does not panic for cpuset, mempolicy, or memcg allocation
  444. * failures.
  445. */
  446. if (constraint != CONSTRAINT_NONE)
  447. return;
  448. }
  449. read_lock(&tasklist_lock);
  450. dump_header(NULL, gfp_mask, order, NULL);
  451. read_unlock(&tasklist_lock);
  452. panic("Out of memory: %s panic_on_oom is enabled\n",
  453. sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide");
  454. }
  455. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  456. void mem_cgroup_out_of_memory(struct mem_cgroup *mem, gfp_t gfp_mask)
  457. {
  458. unsigned long points = 0;
  459. struct task_struct *p;
  460. check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, 0);
  461. read_lock(&tasklist_lock);
  462. retry:
  463. p = select_bad_process(&points, mem, NULL);
  464. if (!p || PTR_ERR(p) == -1UL)
  465. goto out;
  466. if (oom_kill_process(p, gfp_mask, 0, points, mem,
  467. "Memory cgroup out of memory"))
  468. goto retry;
  469. out:
  470. read_unlock(&tasklist_lock);
  471. }
  472. #endif
  473. static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
  474. int register_oom_notifier(struct notifier_block *nb)
  475. {
  476. return blocking_notifier_chain_register(&oom_notify_list, nb);
  477. }
  478. EXPORT_SYMBOL_GPL(register_oom_notifier);
  479. int unregister_oom_notifier(struct notifier_block *nb)
  480. {
  481. return blocking_notifier_chain_unregister(&oom_notify_list, nb);
  482. }
  483. EXPORT_SYMBOL_GPL(unregister_oom_notifier);
  484. /*
  485. * Try to acquire the OOM killer lock for the zones in zonelist. Returns zero
  486. * if a parallel OOM killing is already taking place that includes a zone in
  487. * the zonelist. Otherwise, locks all zones in the zonelist and returns 1.
  488. */
  489. int try_set_zonelist_oom(struct zonelist *zonelist, gfp_t gfp_mask)
  490. {
  491. struct zoneref *z;
  492. struct zone *zone;
  493. int ret = 1;
  494. spin_lock(&zone_scan_lock);
  495. for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
  496. if (zone_is_oom_locked(zone)) {
  497. ret = 0;
  498. goto out;
  499. }
  500. }
  501. for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
  502. /*
  503. * Lock each zone in the zonelist under zone_scan_lock so a
  504. * parallel invocation of try_set_zonelist_oom() doesn't succeed
  505. * when it shouldn't.
  506. */
  507. zone_set_flag(zone, ZONE_OOM_LOCKED);
  508. }
  509. out:
  510. spin_unlock(&zone_scan_lock);
  511. return ret;
  512. }
  513. /*
  514. * Clears the ZONE_OOM_LOCKED flag for all zones in the zonelist so that failed
  515. * allocation attempts with zonelists containing them may now recall the OOM
  516. * killer, if necessary.
  517. */
  518. void clear_zonelist_oom(struct zonelist *zonelist, gfp_t gfp_mask)
  519. {
  520. struct zoneref *z;
  521. struct zone *zone;
  522. spin_lock(&zone_scan_lock);
  523. for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
  524. zone_clear_flag(zone, ZONE_OOM_LOCKED);
  525. }
  526. spin_unlock(&zone_scan_lock);
  527. }
  528. /*
  529. * Try to acquire the oom killer lock for all system zones. Returns zero if a
  530. * parallel oom killing is taking place, otherwise locks all zones and returns
  531. * non-zero.
  532. */
  533. static int try_set_system_oom(void)
  534. {
  535. struct zone *zone;
  536. int ret = 1;
  537. spin_lock(&zone_scan_lock);
  538. for_each_populated_zone(zone)
  539. if (zone_is_oom_locked(zone)) {
  540. ret = 0;
  541. goto out;
  542. }
  543. for_each_populated_zone(zone)
  544. zone_set_flag(zone, ZONE_OOM_LOCKED);
  545. out:
  546. spin_unlock(&zone_scan_lock);
  547. return ret;
  548. }
  549. /*
  550. * Clears ZONE_OOM_LOCKED for all system zones so that failed allocation
  551. * attempts or page faults may now recall the oom killer, if necessary.
  552. */
  553. static void clear_system_oom(void)
  554. {
  555. struct zone *zone;
  556. spin_lock(&zone_scan_lock);
  557. for_each_populated_zone(zone)
  558. zone_clear_flag(zone, ZONE_OOM_LOCKED);
  559. spin_unlock(&zone_scan_lock);
  560. }
  561. /**
  562. * out_of_memory - kill the "best" process when we run out of memory
  563. * @zonelist: zonelist pointer
  564. * @gfp_mask: memory allocation flags
  565. * @order: amount of memory being requested as a power of 2
  566. * @nodemask: nodemask passed to page allocator
  567. *
  568. * If we run out of memory, we have the choice between either
  569. * killing a random task (bad), letting the system crash (worse)
  570. * OR try to be smart about which process to kill. Note that we
  571. * don't have to be perfect here, we just have to be good.
  572. */
  573. void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask,
  574. int order, nodemask_t *nodemask)
  575. {
  576. struct task_struct *p;
  577. unsigned long freed = 0;
  578. unsigned long points;
  579. enum oom_constraint constraint = CONSTRAINT_NONE;
  580. blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
  581. if (freed > 0)
  582. /* Got some memory back in the last second. */
  583. return;
  584. /*
  585. * If current has a pending SIGKILL, then automatically select it. The
  586. * goal is to allow it to allocate so that it may quickly exit and free
  587. * its memory.
  588. */
  589. if (fatal_signal_pending(current)) {
  590. set_thread_flag(TIF_MEMDIE);
  591. return;
  592. }
  593. /*
  594. * Check if there were limitations on the allocation (only relevant for
  595. * NUMA) that may require different handling.
  596. */
  597. if (zonelist)
  598. constraint = constrained_alloc(zonelist, gfp_mask, nodemask);
  599. check_panic_on_oom(constraint, gfp_mask, order);
  600. read_lock(&tasklist_lock);
  601. if (sysctl_oom_kill_allocating_task) {
  602. /*
  603. * oom_kill_process() needs tasklist_lock held. If it returns
  604. * non-zero, current could not be killed so we must fallback to
  605. * the tasklist scan.
  606. */
  607. if (!oom_kill_process(current, gfp_mask, order, 0, NULL,
  608. "Out of memory (oom_kill_allocating_task)"))
  609. return;
  610. }
  611. retry:
  612. p = select_bad_process(&points, NULL,
  613. constraint == CONSTRAINT_MEMORY_POLICY ? nodemask :
  614. NULL);
  615. if (PTR_ERR(p) == -1UL)
  616. return;
  617. /* Found nothing?!?! Either we hang forever, or we panic. */
  618. if (!p) {
  619. dump_header(NULL, gfp_mask, order, NULL);
  620. read_unlock(&tasklist_lock);
  621. panic("Out of memory and no killable processes...\n");
  622. }
  623. if (oom_kill_process(p, gfp_mask, order, points, NULL,
  624. "Out of memory"))
  625. goto retry;
  626. read_unlock(&tasklist_lock);
  627. /*
  628. * Give "p" a good chance of killing itself before we
  629. * retry to allocate memory unless "p" is current
  630. */
  631. if (!test_thread_flag(TIF_MEMDIE))
  632. schedule_timeout_uninterruptible(1);
  633. }
  634. /*
  635. * The pagefault handler calls here because it is out of memory, so kill a
  636. * memory-hogging task. If a populated zone has ZONE_OOM_LOCKED set, a parallel
  637. * oom killing is already in progress so do nothing. If a task is found with
  638. * TIF_MEMDIE set, it has been killed so do nothing and allow it to exit.
  639. */
  640. void pagefault_out_of_memory(void)
  641. {
  642. if (try_set_system_oom()) {
  643. out_of_memory(NULL, 0, 0, NULL);
  644. clear_system_oom();
  645. }
  646. if (!test_thread_flag(TIF_MEMDIE))
  647. schedule_timeout_uninterruptible(1);
  648. }