oom_kill.c 15 KB

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