oom_kill.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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/sched.h>
  21. #include <linux/swap.h>
  22. #include <linux/timex.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/cpuset.h>
  25. #include <linux/module.h>
  26. #include <linux/notifier.h>
  27. #include <linux/memcontrol.h>
  28. #include <linux/security.h>
  29. int sysctl_panic_on_oom;
  30. int sysctl_oom_kill_allocating_task;
  31. int sysctl_oom_dump_tasks;
  32. static DEFINE_SPINLOCK(zone_scan_lock);
  33. /* #define DEBUG */
  34. /*
  35. * Is all threads of the target process nodes overlap ours?
  36. */
  37. static int has_intersects_mems_allowed(struct task_struct *tsk)
  38. {
  39. struct task_struct *t;
  40. t = tsk;
  41. do {
  42. if (cpuset_mems_allowed_intersects(current, t))
  43. return 1;
  44. t = next_thread(t);
  45. } while (t != tsk);
  46. return 0;
  47. }
  48. /**
  49. * badness - calculate a numeric value for how bad this task has been
  50. * @p: task struct of which task we should calculate
  51. * @uptime: current uptime in seconds
  52. *
  53. * The formula used is relatively simple and documented inline in the
  54. * function. The main rationale is that we want to select a good task
  55. * to kill when we run out of memory.
  56. *
  57. * Good in this context means that:
  58. * 1) we lose the minimum amount of work done
  59. * 2) we recover a large amount of memory
  60. * 3) we don't kill anything innocent of eating tons of memory
  61. * 4) we want to kill the minimum amount of processes (one)
  62. * 5) we try to kill the process the user expects us to kill, this
  63. * algorithm has been meticulously tuned to meet the principle
  64. * of least surprise ... (be careful when you change it)
  65. */
  66. unsigned long badness(struct task_struct *p, unsigned long uptime)
  67. {
  68. unsigned long points, cpu_time, run_time;
  69. struct mm_struct *mm;
  70. struct task_struct *child;
  71. int oom_adj = p->signal->oom_adj;
  72. struct task_cputime task_time;
  73. unsigned long utime;
  74. unsigned long stime;
  75. if (oom_adj == OOM_DISABLE)
  76. return 0;
  77. task_lock(p);
  78. mm = p->mm;
  79. if (!mm) {
  80. task_unlock(p);
  81. return 0;
  82. }
  83. /*
  84. * The memory size of the process is the basis for the badness.
  85. */
  86. points = mm->total_vm;
  87. /*
  88. * After this unlock we can no longer dereference local variable `mm'
  89. */
  90. task_unlock(p);
  91. /*
  92. * swapoff can easily use up all memory, so kill those first.
  93. */
  94. if (p->flags & PF_OOM_ORIGIN)
  95. return ULONG_MAX;
  96. /*
  97. * Processes which fork a lot of child processes are likely
  98. * a good choice. We add half the vmsize of the children if they
  99. * have an own mm. This prevents forking servers to flood the
  100. * machine with an endless amount of children. In case a single
  101. * child is eating the vast majority of memory, adding only half
  102. * to the parents will make the child our kill candidate of choice.
  103. */
  104. list_for_each_entry(child, &p->children, sibling) {
  105. task_lock(child);
  106. if (child->mm != mm && child->mm)
  107. points += child->mm->total_vm/2 + 1;
  108. task_unlock(child);
  109. }
  110. /*
  111. * CPU time is in tens of seconds and run time is in thousands
  112. * of seconds. There is no particular reason for this other than
  113. * that it turned out to work very well in practice.
  114. */
  115. thread_group_cputime(p, &task_time);
  116. utime = cputime_to_jiffies(task_time.utime);
  117. stime = cputime_to_jiffies(task_time.stime);
  118. cpu_time = (utime + stime) >> (SHIFT_HZ + 3);
  119. if (uptime >= p->start_time.tv_sec)
  120. run_time = (uptime - p->start_time.tv_sec) >> 10;
  121. else
  122. run_time = 0;
  123. if (cpu_time)
  124. points /= int_sqrt(cpu_time);
  125. if (run_time)
  126. points /= int_sqrt(int_sqrt(run_time));
  127. /*
  128. * Niced processes are most likely less important, so double
  129. * their badness points.
  130. */
  131. if (task_nice(p) > 0)
  132. points *= 2;
  133. /*
  134. * Superuser processes are usually more important, so we make it
  135. * less likely that we kill those.
  136. */
  137. if (has_capability_noaudit(p, CAP_SYS_ADMIN) ||
  138. has_capability_noaudit(p, CAP_SYS_RESOURCE))
  139. points /= 4;
  140. /*
  141. * We don't want to kill a process with direct hardware access.
  142. * Not only could that mess up the hardware, but usually users
  143. * tend to only have this flag set on applications they think
  144. * of as important.
  145. */
  146. if (has_capability_noaudit(p, CAP_SYS_RAWIO))
  147. points /= 4;
  148. /*
  149. * If p's nodes don't overlap ours, it may still help to kill p
  150. * because p may have allocated or otherwise mapped memory on
  151. * this node before. However it will be less likely.
  152. */
  153. if (!has_intersects_mems_allowed(p))
  154. points /= 8;
  155. /*
  156. * Adjust the score by oom_adj.
  157. */
  158. if (oom_adj) {
  159. if (oom_adj > 0) {
  160. if (!points)
  161. points = 1;
  162. points <<= oom_adj;
  163. } else
  164. points >>= -(oom_adj);
  165. }
  166. #ifdef DEBUG
  167. printk(KERN_DEBUG "OOMkill: task %d (%s) got %lu points\n",
  168. p->pid, p->comm, points);
  169. #endif
  170. return points;
  171. }
  172. /*
  173. * Determine the type of allocation constraint.
  174. */
  175. static inline enum oom_constraint constrained_alloc(struct zonelist *zonelist,
  176. gfp_t gfp_mask)
  177. {
  178. #ifdef CONFIG_NUMA
  179. struct zone *zone;
  180. struct zoneref *z;
  181. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  182. nodemask_t nodes = node_states[N_HIGH_MEMORY];
  183. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx)
  184. if (cpuset_zone_allowed_softwall(zone, gfp_mask))
  185. node_clear(zone_to_nid(zone), nodes);
  186. else
  187. return CONSTRAINT_CPUSET;
  188. if (!nodes_empty(nodes))
  189. return CONSTRAINT_MEMORY_POLICY;
  190. #endif
  191. return CONSTRAINT_NONE;
  192. }
  193. /*
  194. * Simple selection loop. We chose the process with the highest
  195. * number of 'points'. We expect the caller will lock the tasklist.
  196. *
  197. * (not docbooked, we don't want this one cluttering up the manual)
  198. */
  199. static struct task_struct *select_bad_process(unsigned long *ppoints,
  200. struct mem_cgroup *mem)
  201. {
  202. struct task_struct *p;
  203. struct task_struct *chosen = NULL;
  204. struct timespec uptime;
  205. *ppoints = 0;
  206. do_posix_clock_monotonic_gettime(&uptime);
  207. for_each_process(p) {
  208. unsigned long points;
  209. /*
  210. * skip kernel threads and tasks which have already released
  211. * their mm.
  212. */
  213. if (!p->mm)
  214. continue;
  215. /* skip the init task */
  216. if (is_global_init(p))
  217. continue;
  218. if (mem && !task_in_mem_cgroup(p, mem))
  219. continue;
  220. /*
  221. * This task already has access to memory reserves and is
  222. * being killed. Don't allow any other task access to the
  223. * memory reserve.
  224. *
  225. * Note: this may have a chance of deadlock if it gets
  226. * blocked waiting for another task which itself is waiting
  227. * for memory. Is there a better alternative?
  228. */
  229. if (test_tsk_thread_flag(p, TIF_MEMDIE))
  230. return ERR_PTR(-1UL);
  231. /*
  232. * This is in the process of releasing memory so wait for it
  233. * to finish before killing some other task by mistake.
  234. *
  235. * However, if p is the current task, we allow the 'kill' to
  236. * go ahead if it is exiting: this will simply set TIF_MEMDIE,
  237. * which will allow it to gain access to memory reserves in
  238. * the process of exiting and releasing its resources.
  239. * Otherwise we could get an easy OOM deadlock.
  240. */
  241. if (p->flags & PF_EXITING) {
  242. if (p != current)
  243. return ERR_PTR(-1UL);
  244. chosen = p;
  245. *ppoints = ULONG_MAX;
  246. }
  247. if (p->signal->oom_adj == OOM_DISABLE)
  248. continue;
  249. points = badness(p, uptime.tv_sec);
  250. if (points > *ppoints || !chosen) {
  251. chosen = p;
  252. *ppoints = points;
  253. }
  254. }
  255. return chosen;
  256. }
  257. /**
  258. * dump_tasks - dump current memory state of all system tasks
  259. * @mem: target memory controller
  260. *
  261. * Dumps the current memory state of all system tasks, excluding kernel threads.
  262. * State information includes task's pid, uid, tgid, vm size, rss, cpu, oom_adj
  263. * score, and name.
  264. *
  265. * If the actual is non-NULL, only tasks that are a member of the mem_cgroup are
  266. * shown.
  267. *
  268. * Call with tasklist_lock read-locked.
  269. */
  270. static void dump_tasks(const struct mem_cgroup *mem)
  271. {
  272. struct task_struct *g, *p;
  273. printk(KERN_INFO "[ pid ] uid tgid total_vm rss cpu oom_adj "
  274. "name\n");
  275. do_each_thread(g, p) {
  276. struct mm_struct *mm;
  277. if (mem && !task_in_mem_cgroup(p, mem))
  278. continue;
  279. if (!thread_group_leader(p))
  280. continue;
  281. task_lock(p);
  282. mm = p->mm;
  283. if (!mm) {
  284. /*
  285. * total_vm and rss sizes do not exist for tasks with no
  286. * mm so there's no need to report them; they can't be
  287. * oom killed anyway.
  288. */
  289. task_unlock(p);
  290. continue;
  291. }
  292. printk(KERN_INFO "[%5d] %5d %5d %8lu %8lu %3d %3d %s\n",
  293. p->pid, __task_cred(p)->uid, p->tgid, mm->total_vm,
  294. get_mm_rss(mm), (int)task_cpu(p), p->signal->oom_adj,
  295. p->comm);
  296. task_unlock(p);
  297. } while_each_thread(g, p);
  298. }
  299. static void dump_header(gfp_t gfp_mask, int order, struct mem_cgroup *mem)
  300. {
  301. pr_warning("%s invoked oom-killer: gfp_mask=0x%x, order=%d, "
  302. "oom_adj=%d\n",
  303. current->comm, gfp_mask, order, current->signal->oom_adj);
  304. task_lock(current);
  305. cpuset_print_task_mems_allowed(current);
  306. task_unlock(current);
  307. dump_stack();
  308. mem_cgroup_print_oom_info(mem, current);
  309. show_mem();
  310. if (sysctl_oom_dump_tasks)
  311. dump_tasks(mem);
  312. }
  313. /*
  314. * Send SIGKILL to the selected process irrespective of CAP_SYS_RAW_IO
  315. * flag though it's unlikely that we select a process with CAP_SYS_RAW_IO
  316. * set.
  317. */
  318. static void __oom_kill_task(struct task_struct *p, int verbose)
  319. {
  320. if (is_global_init(p)) {
  321. WARN_ON(1);
  322. printk(KERN_WARNING "tried to kill init!\n");
  323. return;
  324. }
  325. if (!p->mm) {
  326. WARN_ON(1);
  327. printk(KERN_WARNING "tried to kill an mm-less task!\n");
  328. return;
  329. }
  330. if (verbose)
  331. printk(KERN_ERR "Killed process %d (%s)\n",
  332. task_pid_nr(p), p->comm);
  333. /*
  334. * We give our sacrificial lamb high priority and access to
  335. * all the memory it needs. That way it should be able to
  336. * exit() and clear out its resources quickly...
  337. */
  338. p->rt.time_slice = HZ;
  339. set_tsk_thread_flag(p, TIF_MEMDIE);
  340. force_sig(SIGKILL, p);
  341. }
  342. static int oom_kill_task(struct task_struct *p)
  343. {
  344. /* WARNING: mm may not be dereferenced since we did not obtain its
  345. * value from get_task_mm(p). This is OK since all we need to do is
  346. * compare mm to q->mm below.
  347. *
  348. * Furthermore, even if mm contains a non-NULL value, p->mm may
  349. * change to NULL at any time since we do not hold task_lock(p).
  350. * However, this is of no concern to us.
  351. */
  352. if (!p->mm || p->signal->oom_adj == OOM_DISABLE)
  353. return 1;
  354. __oom_kill_task(p, 1);
  355. return 0;
  356. }
  357. static int oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
  358. unsigned long points, struct mem_cgroup *mem,
  359. const char *message)
  360. {
  361. struct task_struct *c;
  362. if (printk_ratelimit())
  363. dump_header(gfp_mask, order, mem);
  364. /*
  365. * If the task is already exiting, don't alarm the sysadmin or kill
  366. * its children or threads, just set TIF_MEMDIE so it can die quickly
  367. */
  368. if (p->flags & PF_EXITING) {
  369. __oom_kill_task(p, 0);
  370. return 0;
  371. }
  372. printk(KERN_ERR "%s: kill process %d (%s) score %li or a child\n",
  373. message, task_pid_nr(p), p->comm, points);
  374. /* Try to kill a child first */
  375. list_for_each_entry(c, &p->children, sibling) {
  376. if (c->mm == p->mm)
  377. continue;
  378. if (!oom_kill_task(c))
  379. return 0;
  380. }
  381. return oom_kill_task(p);
  382. }
  383. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  384. void mem_cgroup_out_of_memory(struct mem_cgroup *mem, gfp_t gfp_mask)
  385. {
  386. unsigned long points = 0;
  387. struct task_struct *p;
  388. read_lock(&tasklist_lock);
  389. retry:
  390. p = select_bad_process(&points, mem);
  391. if (PTR_ERR(p) == -1UL)
  392. goto out;
  393. if (!p)
  394. p = current;
  395. if (oom_kill_process(p, gfp_mask, 0, points, mem,
  396. "Memory cgroup out of memory"))
  397. goto retry;
  398. out:
  399. read_unlock(&tasklist_lock);
  400. }
  401. #endif
  402. static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
  403. int register_oom_notifier(struct notifier_block *nb)
  404. {
  405. return blocking_notifier_chain_register(&oom_notify_list, nb);
  406. }
  407. EXPORT_SYMBOL_GPL(register_oom_notifier);
  408. int unregister_oom_notifier(struct notifier_block *nb)
  409. {
  410. return blocking_notifier_chain_unregister(&oom_notify_list, nb);
  411. }
  412. EXPORT_SYMBOL_GPL(unregister_oom_notifier);
  413. /*
  414. * Try to acquire the OOM killer lock for the zones in zonelist. Returns zero
  415. * if a parallel OOM killing is already taking place that includes a zone in
  416. * the zonelist. Otherwise, locks all zones in the zonelist and returns 1.
  417. */
  418. int try_set_zone_oom(struct zonelist *zonelist, gfp_t gfp_mask)
  419. {
  420. struct zoneref *z;
  421. struct zone *zone;
  422. int ret = 1;
  423. spin_lock(&zone_scan_lock);
  424. for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
  425. if (zone_is_oom_locked(zone)) {
  426. ret = 0;
  427. goto out;
  428. }
  429. }
  430. for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
  431. /*
  432. * Lock each zone in the zonelist under zone_scan_lock so a
  433. * parallel invocation of try_set_zone_oom() doesn't succeed
  434. * when it shouldn't.
  435. */
  436. zone_set_flag(zone, ZONE_OOM_LOCKED);
  437. }
  438. out:
  439. spin_unlock(&zone_scan_lock);
  440. return ret;
  441. }
  442. /*
  443. * Clears the ZONE_OOM_LOCKED flag for all zones in the zonelist so that failed
  444. * allocation attempts with zonelists containing them may now recall the OOM
  445. * killer, if necessary.
  446. */
  447. void clear_zonelist_oom(struct zonelist *zonelist, gfp_t gfp_mask)
  448. {
  449. struct zoneref *z;
  450. struct zone *zone;
  451. spin_lock(&zone_scan_lock);
  452. for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
  453. zone_clear_flag(zone, ZONE_OOM_LOCKED);
  454. }
  455. spin_unlock(&zone_scan_lock);
  456. }
  457. /*
  458. * Must be called with tasklist_lock held for read.
  459. */
  460. static void __out_of_memory(gfp_t gfp_mask, int order)
  461. {
  462. struct task_struct *p;
  463. unsigned long points;
  464. if (sysctl_oom_kill_allocating_task)
  465. if (!oom_kill_process(current, gfp_mask, order, 0, NULL,
  466. "Out of memory (oom_kill_allocating_task)"))
  467. return;
  468. retry:
  469. /*
  470. * Rambo mode: Shoot down a process and hope it solves whatever
  471. * issues we may have.
  472. */
  473. p = select_bad_process(&points, NULL);
  474. if (PTR_ERR(p) == -1UL)
  475. return;
  476. /* Found nothing?!?! Either we hang forever, or we panic. */
  477. if (!p) {
  478. read_unlock(&tasklist_lock);
  479. dump_header(gfp_mask, order, NULL);
  480. panic("Out of memory and no killable processes...\n");
  481. }
  482. if (oom_kill_process(p, gfp_mask, order, points, NULL,
  483. "Out of memory"))
  484. goto retry;
  485. }
  486. /*
  487. * pagefault handler calls into here because it is out of memory but
  488. * doesn't know exactly how or why.
  489. */
  490. void pagefault_out_of_memory(void)
  491. {
  492. unsigned long freed = 0;
  493. blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
  494. if (freed > 0)
  495. /* Got some memory back in the last second. */
  496. return;
  497. /*
  498. * If this is from memcg, oom-killer is already invoked.
  499. * and not worth to go system-wide-oom.
  500. */
  501. if (mem_cgroup_oom_called(current))
  502. goto rest_and_return;
  503. if (sysctl_panic_on_oom)
  504. panic("out of memory from page fault. panic_on_oom is selected.\n");
  505. read_lock(&tasklist_lock);
  506. __out_of_memory(0, 0); /* unknown gfp_mask and order */
  507. read_unlock(&tasklist_lock);
  508. /*
  509. * Give "p" a good chance of killing itself before we
  510. * retry to allocate memory.
  511. */
  512. rest_and_return:
  513. if (!test_thread_flag(TIF_MEMDIE))
  514. schedule_timeout_uninterruptible(1);
  515. }
  516. /**
  517. * out_of_memory - kill the "best" process when we run out of memory
  518. * @zonelist: zonelist pointer
  519. * @gfp_mask: memory allocation flags
  520. * @order: amount of memory being requested as a power of 2
  521. *
  522. * If we run out of memory, we have the choice between either
  523. * killing a random task (bad), letting the system crash (worse)
  524. * OR try to be smart about which process to kill. Note that we
  525. * don't have to be perfect here, we just have to be good.
  526. */
  527. void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, int order)
  528. {
  529. unsigned long freed = 0;
  530. enum oom_constraint constraint;
  531. blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
  532. if (freed > 0)
  533. /* Got some memory back in the last second. */
  534. return;
  535. if (sysctl_panic_on_oom == 2) {
  536. dump_header(gfp_mask, order, NULL);
  537. panic("out of memory. Compulsory panic_on_oom is selected.\n");
  538. }
  539. /*
  540. * Check if there were limitations on the allocation (only relevant for
  541. * NUMA) that may require different handling.
  542. */
  543. constraint = constrained_alloc(zonelist, gfp_mask);
  544. read_lock(&tasklist_lock);
  545. switch (constraint) {
  546. case CONSTRAINT_MEMORY_POLICY:
  547. oom_kill_process(current, gfp_mask, order, 0, NULL,
  548. "No available memory (MPOL_BIND)");
  549. break;
  550. case CONSTRAINT_NONE:
  551. if (sysctl_panic_on_oom) {
  552. dump_header(gfp_mask, order, NULL);
  553. panic("out of memory. panic_on_oom is selected\n");
  554. }
  555. /* Fall-through */
  556. case CONSTRAINT_CPUSET:
  557. __out_of_memory(gfp_mask, order);
  558. break;
  559. }
  560. read_unlock(&tasklist_lock);
  561. /*
  562. * Give "p" a good chance of killing itself before we
  563. * retry to allocate memory unless "p" is current
  564. */
  565. if (!test_thread_flag(TIF_MEMDIE))
  566. schedule_timeout_uninterruptible(1);
  567. }