oom_kill.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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/sched.h>
  20. #include <linux/swap.h>
  21. #include <linux/timex.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/cpuset.h>
  24. #include <linux/module.h>
  25. #include <linux/notifier.h>
  26. int sysctl_panic_on_oom;
  27. /* #define DEBUG */
  28. /**
  29. * badness - calculate a numeric value for how bad this task has been
  30. * @p: task struct of which task we should calculate
  31. * @uptime: current uptime in seconds
  32. *
  33. * The formula used is relatively simple and documented inline in the
  34. * function. The main rationale is that we want to select a good task
  35. * to kill when we run out of memory.
  36. *
  37. * Good in this context means that:
  38. * 1) we lose the minimum amount of work done
  39. * 2) we recover a large amount of memory
  40. * 3) we don't kill anything innocent of eating tons of memory
  41. * 4) we want to kill the minimum amount of processes (one)
  42. * 5) we try to kill the process the user expects us to kill, this
  43. * algorithm has been meticulously tuned to meet the principle
  44. * of least surprise ... (be careful when you change it)
  45. */
  46. unsigned long badness(struct task_struct *p, unsigned long uptime)
  47. {
  48. unsigned long points, cpu_time, run_time, s;
  49. struct mm_struct *mm;
  50. struct task_struct *child;
  51. task_lock(p);
  52. mm = p->mm;
  53. if (!mm) {
  54. task_unlock(p);
  55. return 0;
  56. }
  57. /*
  58. * The memory size of the process is the basis for the badness.
  59. */
  60. points = mm->total_vm;
  61. /*
  62. * After this unlock we can no longer dereference local variable `mm'
  63. */
  64. task_unlock(p);
  65. /*
  66. * swapoff can easily use up all memory, so kill those first.
  67. */
  68. if (p->flags & PF_SWAPOFF)
  69. return ULONG_MAX;
  70. /*
  71. * Processes which fork a lot of child processes are likely
  72. * a good choice. We add half the vmsize of the children if they
  73. * have an own mm. This prevents forking servers to flood the
  74. * machine with an endless amount of children. In case a single
  75. * child is eating the vast majority of memory, adding only half
  76. * to the parents will make the child our kill candidate of choice.
  77. */
  78. list_for_each_entry(child, &p->children, sibling) {
  79. task_lock(child);
  80. if (child->mm != mm && child->mm)
  81. points += child->mm->total_vm/2 + 1;
  82. task_unlock(child);
  83. }
  84. /*
  85. * CPU time is in tens of seconds and run time is in thousands
  86. * of seconds. There is no particular reason for this other than
  87. * that it turned out to work very well in practice.
  88. */
  89. cpu_time = (cputime_to_jiffies(p->utime) + cputime_to_jiffies(p->stime))
  90. >> (SHIFT_HZ + 3);
  91. if (uptime >= p->start_time.tv_sec)
  92. run_time = (uptime - p->start_time.tv_sec) >> 10;
  93. else
  94. run_time = 0;
  95. s = int_sqrt(cpu_time);
  96. if (s)
  97. points /= s;
  98. s = int_sqrt(int_sqrt(run_time));
  99. if (s)
  100. points /= s;
  101. /*
  102. * Niced processes are most likely less important, so double
  103. * their badness points.
  104. */
  105. if (task_nice(p) > 0)
  106. points *= 2;
  107. /*
  108. * Superuser processes are usually more important, so we make it
  109. * less likely that we kill those.
  110. */
  111. if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_ADMIN) ||
  112. p->uid == 0 || p->euid == 0)
  113. points /= 4;
  114. /*
  115. * We don't want to kill a process with direct hardware access.
  116. * Not only could that mess up the hardware, but usually users
  117. * tend to only have this flag set on applications they think
  118. * of as important.
  119. */
  120. if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO))
  121. points /= 4;
  122. /*
  123. * If p's nodes don't overlap ours, it may still help to kill p
  124. * because p may have allocated or otherwise mapped memory on
  125. * this node before. However it will be less likely.
  126. */
  127. if (!cpuset_excl_nodes_overlap(p))
  128. points /= 8;
  129. /*
  130. * Adjust the score by oomkilladj.
  131. */
  132. if (p->oomkilladj) {
  133. if (p->oomkilladj > 0)
  134. points <<= p->oomkilladj;
  135. else
  136. points >>= -(p->oomkilladj);
  137. }
  138. #ifdef DEBUG
  139. printk(KERN_DEBUG "OOMkill: task %d (%s) got %d points\n",
  140. p->pid, p->comm, points);
  141. #endif
  142. return points;
  143. }
  144. /*
  145. * Types of limitations to the nodes from which allocations may occur
  146. */
  147. #define CONSTRAINT_NONE 1
  148. #define CONSTRAINT_MEMORY_POLICY 2
  149. #define CONSTRAINT_CPUSET 3
  150. /*
  151. * Determine the type of allocation constraint.
  152. */
  153. static inline int constrained_alloc(struct zonelist *zonelist, gfp_t gfp_mask)
  154. {
  155. #ifdef CONFIG_NUMA
  156. struct zone **z;
  157. nodemask_t nodes;
  158. int node;
  159. /* node has memory ? */
  160. for_each_online_node(node)
  161. if (NODE_DATA(node)->node_present_pages)
  162. node_set(node, nodes);
  163. for (z = zonelist->zones; *z; z++)
  164. if (cpuset_zone_allowed_softwall(*z, gfp_mask))
  165. node_clear(zone_to_nid(*z), nodes);
  166. else
  167. return CONSTRAINT_CPUSET;
  168. if (!nodes_empty(nodes))
  169. return CONSTRAINT_MEMORY_POLICY;
  170. #endif
  171. return CONSTRAINT_NONE;
  172. }
  173. /*
  174. * Simple selection loop. We chose the process with the highest
  175. * number of 'points'. We expect the caller will lock the tasklist.
  176. *
  177. * (not docbooked, we don't want this one cluttering up the manual)
  178. */
  179. static struct task_struct *select_bad_process(unsigned long *ppoints)
  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_init(p))
  196. continue;
  197. /*
  198. * This task already has access to memory reserves and is
  199. * being killed. Don't allow any other task access to the
  200. * memory reserve.
  201. *
  202. * Note: this may have a chance of deadlock if it gets
  203. * blocked waiting for another task which itself is waiting
  204. * for memory. Is there a better alternative?
  205. */
  206. if (test_tsk_thread_flag(p, TIF_MEMDIE))
  207. return ERR_PTR(-1UL);
  208. /*
  209. * This is in the process of releasing memory so wait for it
  210. * to finish before killing some other task by mistake.
  211. *
  212. * However, if p is the current task, we allow the 'kill' to
  213. * go ahead if it is exiting: this will simply set TIF_MEMDIE,
  214. * which will allow it to gain access to memory reserves in
  215. * the process of exiting and releasing its resources.
  216. * Otherwise we could get an easy OOM deadlock.
  217. */
  218. if (p->flags & PF_EXITING) {
  219. if (p != current)
  220. return ERR_PTR(-1UL);
  221. chosen = p;
  222. *ppoints = ULONG_MAX;
  223. }
  224. if (p->oomkilladj == OOM_DISABLE)
  225. continue;
  226. points = badness(p, uptime.tv_sec);
  227. if (points > *ppoints || !chosen) {
  228. chosen = p;
  229. *ppoints = points;
  230. }
  231. } while_each_thread(g, p);
  232. return chosen;
  233. }
  234. /**
  235. * Send SIGKILL to the selected process irrespective of CAP_SYS_RAW_IO
  236. * flag though it's unlikely that we select a process with CAP_SYS_RAW_IO
  237. * set.
  238. */
  239. static void __oom_kill_task(struct task_struct *p, int verbose)
  240. {
  241. if (is_init(p)) {
  242. WARN_ON(1);
  243. printk(KERN_WARNING "tried to kill init!\n");
  244. return;
  245. }
  246. if (!p->mm) {
  247. WARN_ON(1);
  248. printk(KERN_WARNING "tried to kill an mm-less task!\n");
  249. return;
  250. }
  251. if (verbose)
  252. printk(KERN_ERR "Killed process %d (%s)\n", p->pid, p->comm);
  253. /*
  254. * We give our sacrificial lamb high priority and access to
  255. * all the memory it needs. That way it should be able to
  256. * exit() and clear out its resources quickly...
  257. */
  258. p->time_slice = HZ;
  259. set_tsk_thread_flag(p, TIF_MEMDIE);
  260. force_sig(SIGKILL, p);
  261. }
  262. static int oom_kill_task(struct task_struct *p)
  263. {
  264. struct mm_struct *mm;
  265. struct task_struct *g, *q;
  266. mm = p->mm;
  267. /* WARNING: mm may not be dereferenced since we did not obtain its
  268. * value from get_task_mm(p). This is OK since all we need to do is
  269. * compare mm to q->mm below.
  270. *
  271. * Furthermore, even if mm contains a non-NULL value, p->mm may
  272. * change to NULL at any time since we do not hold task_lock(p).
  273. * However, this is of no concern to us.
  274. */
  275. if (mm == NULL)
  276. return 1;
  277. /*
  278. * Don't kill the process if any threads are set to OOM_DISABLE
  279. */
  280. do_each_thread(g, q) {
  281. if (q->mm == mm && p->oomkilladj == OOM_DISABLE)
  282. return 1;
  283. } while_each_thread(g, q);
  284. __oom_kill_task(p, 1);
  285. /*
  286. * kill all processes that share the ->mm (i.e. all threads),
  287. * but are in a different thread group. Don't let them have access
  288. * to memory reserves though, otherwise we might deplete all memory.
  289. */
  290. do_each_thread(g, q) {
  291. if (q->mm == mm && q->tgid != p->tgid)
  292. force_sig(SIGKILL, p);
  293. } while_each_thread(g, q);
  294. return 0;
  295. }
  296. static int oom_kill_process(struct task_struct *p, unsigned long points,
  297. const char *message)
  298. {
  299. struct task_struct *c;
  300. struct list_head *tsk;
  301. /*
  302. * If the task is already exiting, don't alarm the sysadmin or kill
  303. * its children or threads, just set TIF_MEMDIE so it can die quickly
  304. */
  305. if (p->flags & PF_EXITING) {
  306. __oom_kill_task(p, 0);
  307. return 0;
  308. }
  309. printk(KERN_ERR "%s: kill process %d (%s) score %li or a child\n",
  310. message, p->pid, p->comm, points);
  311. /* Try to kill a child first */
  312. list_for_each(tsk, &p->children) {
  313. c = list_entry(tsk, struct task_struct, sibling);
  314. if (c->mm == p->mm)
  315. continue;
  316. if (!oom_kill_task(c))
  317. return 0;
  318. }
  319. return oom_kill_task(p);
  320. }
  321. static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
  322. int register_oom_notifier(struct notifier_block *nb)
  323. {
  324. return blocking_notifier_chain_register(&oom_notify_list, nb);
  325. }
  326. EXPORT_SYMBOL_GPL(register_oom_notifier);
  327. int unregister_oom_notifier(struct notifier_block *nb)
  328. {
  329. return blocking_notifier_chain_unregister(&oom_notify_list, nb);
  330. }
  331. EXPORT_SYMBOL_GPL(unregister_oom_notifier);
  332. /**
  333. * out_of_memory - kill the "best" process when we run out of memory
  334. *
  335. * If we run out of memory, we have the choice between either
  336. * killing a random task (bad), letting the system crash (worse)
  337. * OR try to be smart about which process to kill. Note that we
  338. * don't have to be perfect here, we just have to be good.
  339. */
  340. void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, int order)
  341. {
  342. struct task_struct *p;
  343. unsigned long points = 0;
  344. unsigned long freed = 0;
  345. blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
  346. if (freed > 0)
  347. /* Got some memory back in the last second. */
  348. return;
  349. if (printk_ratelimit()) {
  350. printk(KERN_WARNING "%s invoked oom-killer: "
  351. "gfp_mask=0x%x, order=%d, oomkilladj=%d\n",
  352. current->comm, gfp_mask, order, current->oomkilladj);
  353. dump_stack();
  354. show_mem();
  355. }
  356. cpuset_lock();
  357. read_lock(&tasklist_lock);
  358. /*
  359. * Check if there were limitations on the allocation (only relevant for
  360. * NUMA) that may require different handling.
  361. */
  362. switch (constrained_alloc(zonelist, gfp_mask)) {
  363. case CONSTRAINT_MEMORY_POLICY:
  364. oom_kill_process(current, points,
  365. "No available memory (MPOL_BIND)");
  366. break;
  367. case CONSTRAINT_CPUSET:
  368. oom_kill_process(current, points,
  369. "No available memory in cpuset");
  370. break;
  371. case CONSTRAINT_NONE:
  372. if (sysctl_panic_on_oom)
  373. panic("out of memory. panic_on_oom is selected\n");
  374. retry:
  375. /*
  376. * Rambo mode: Shoot down a process and hope it solves whatever
  377. * issues we may have.
  378. */
  379. p = select_bad_process(&points);
  380. if (PTR_ERR(p) == -1UL)
  381. goto out;
  382. /* Found nothing?!?! Either we hang forever, or we panic. */
  383. if (!p) {
  384. read_unlock(&tasklist_lock);
  385. cpuset_unlock();
  386. panic("Out of memory and no killable processes...\n");
  387. }
  388. if (oom_kill_process(p, points, "Out of memory"))
  389. goto retry;
  390. break;
  391. }
  392. out:
  393. read_unlock(&tasklist_lock);
  394. cpuset_unlock();
  395. /*
  396. * Give "p" a good chance of killing itself before we
  397. * retry to allocate memory unless "p" is current
  398. */
  399. if (!test_thread_flag(TIF_MEMDIE))
  400. schedule_timeout_uninterruptible(1);
  401. }