oom_kill.c 10 KB

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