oom_kill.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. /* #define DEBUG */
  24. /**
  25. * oom_badness - calculate a numeric value for how bad this task has been
  26. * @p: task struct of which task we should calculate
  27. * @uptime: current uptime in seconds
  28. *
  29. * The formula used is relatively simple and documented inline in the
  30. * function. The main rationale is that we want to select a good task
  31. * to kill when we run out of memory.
  32. *
  33. * Good in this context means that:
  34. * 1) we lose the minimum amount of work done
  35. * 2) we recover a large amount of memory
  36. * 3) we don't kill anything innocent of eating tons of memory
  37. * 4) we want to kill the minimum amount of processes (one)
  38. * 5) we try to kill the process the user expects us to kill, this
  39. * algorithm has been meticulously tuned to meet the principle
  40. * of least surprise ... (be careful when you change it)
  41. */
  42. unsigned long badness(struct task_struct *p, unsigned long uptime)
  43. {
  44. unsigned long points, cpu_time, run_time, s;
  45. struct list_head *tsk;
  46. if (!p->mm)
  47. return 0;
  48. /*
  49. * The memory size of the process is the basis for the badness.
  50. */
  51. points = p->mm->total_vm;
  52. /*
  53. * Processes which fork a lot of child processes are likely
  54. * a good choice. We add half the vmsize of the children if they
  55. * have an own mm. This prevents forking servers to flood the
  56. * machine with an endless amount of children. In case a single
  57. * child is eating the vast majority of memory, adding only half
  58. * to the parents will make the child our kill candidate of choice.
  59. */
  60. list_for_each(tsk, &p->children) {
  61. struct task_struct *chld;
  62. chld = list_entry(tsk, struct task_struct, sibling);
  63. if (chld->mm != p->mm && chld->mm)
  64. points += chld->mm->total_vm/2 + 1;
  65. }
  66. /*
  67. * CPU time is in tens of seconds and run time is in thousands
  68. * of seconds. There is no particular reason for this other than
  69. * that it turned out to work very well in practice.
  70. */
  71. cpu_time = (cputime_to_jiffies(p->utime) + cputime_to_jiffies(p->stime))
  72. >> (SHIFT_HZ + 3);
  73. if (uptime >= p->start_time.tv_sec)
  74. run_time = (uptime - p->start_time.tv_sec) >> 10;
  75. else
  76. run_time = 0;
  77. s = int_sqrt(cpu_time);
  78. if (s)
  79. points /= s;
  80. s = int_sqrt(int_sqrt(run_time));
  81. if (s)
  82. points /= s;
  83. /*
  84. * Niced processes are most likely less important, so double
  85. * their badness points.
  86. */
  87. if (task_nice(p) > 0)
  88. points *= 2;
  89. /*
  90. * Superuser processes are usually more important, so we make it
  91. * less likely that we kill those.
  92. */
  93. if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_ADMIN) ||
  94. p->uid == 0 || p->euid == 0)
  95. points /= 4;
  96. /*
  97. * We don't want to kill a process with direct hardware access.
  98. * Not only could that mess up the hardware, but usually users
  99. * tend to only have this flag set on applications they think
  100. * of as important.
  101. */
  102. if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO))
  103. points /= 4;
  104. /*
  105. * Adjust the score by oomkilladj.
  106. */
  107. if (p->oomkilladj) {
  108. if (p->oomkilladj > 0)
  109. points <<= p->oomkilladj;
  110. else
  111. points >>= -(p->oomkilladj);
  112. }
  113. #ifdef DEBUG
  114. printk(KERN_DEBUG "OOMkill: task %d (%s) got %d points\n",
  115. p->pid, p->comm, points);
  116. #endif
  117. return points;
  118. }
  119. /*
  120. * Simple selection loop. We chose the process with the highest
  121. * number of 'points'. We expect the caller will lock the tasklist.
  122. *
  123. * (not docbooked, we don't want this one cluttering up the manual)
  124. */
  125. static struct task_struct *select_bad_process(unsigned long *ppoints)
  126. {
  127. struct task_struct *g, *p;
  128. struct task_struct *chosen = NULL;
  129. struct timespec uptime;
  130. *ppoints = 0;
  131. do_posix_clock_monotonic_gettime(&uptime);
  132. do_each_thread(g, p) {
  133. unsigned long points;
  134. int releasing;
  135. /* skip the init task with pid == 1 */
  136. if (p->pid == 1)
  137. continue;
  138. if (p->oomkilladj == OOM_DISABLE)
  139. continue;
  140. /* If p's nodes don't overlap ours, it won't help to kill p. */
  141. if (!cpuset_excl_nodes_overlap(p))
  142. continue;
  143. /*
  144. * This is in the process of releasing memory so for wait it
  145. * to finish before killing some other task by mistake.
  146. */
  147. releasing = test_tsk_thread_flag(p, TIF_MEMDIE) ||
  148. p->flags & PF_EXITING;
  149. if (releasing && !(p->flags & PF_DEAD))
  150. return ERR_PTR(-1UL);
  151. if (p->flags & PF_SWAPOFF)
  152. return p;
  153. points = badness(p, uptime.tv_sec);
  154. if (points > *ppoints || !chosen) {
  155. chosen = p;
  156. *ppoints = points;
  157. }
  158. } while_each_thread(g, p);
  159. return chosen;
  160. }
  161. /**
  162. * We must be careful though to never send SIGKILL a process with
  163. * CAP_SYS_RAW_IO set, send SIGTERM instead (but it's unlikely that
  164. * we select a process with CAP_SYS_RAW_IO set).
  165. */
  166. static void __oom_kill_task(task_t *p)
  167. {
  168. if (p->pid == 1) {
  169. WARN_ON(1);
  170. printk(KERN_WARNING "tried to kill init!\n");
  171. return;
  172. }
  173. task_lock(p);
  174. if (!p->mm || p->mm == &init_mm) {
  175. WARN_ON(1);
  176. printk(KERN_WARNING "tried to kill an mm-less task!\n");
  177. task_unlock(p);
  178. return;
  179. }
  180. task_unlock(p);
  181. printk(KERN_ERR "Out of Memory: Killed process %d (%s).\n",
  182. p->pid, p->comm);
  183. /*
  184. * We give our sacrificial lamb high priority and access to
  185. * all the memory it needs. That way it should be able to
  186. * exit() and clear out its resources quickly...
  187. */
  188. p->time_slice = HZ;
  189. set_tsk_thread_flag(p, TIF_MEMDIE);
  190. force_sig(SIGKILL, p);
  191. }
  192. static struct mm_struct *oom_kill_task(task_t *p)
  193. {
  194. struct mm_struct *mm = get_task_mm(p);
  195. task_t * g, * q;
  196. if (!mm)
  197. return NULL;
  198. if (mm == &init_mm) {
  199. mmput(mm);
  200. return NULL;
  201. }
  202. __oom_kill_task(p);
  203. /*
  204. * kill all processes that share the ->mm (i.e. all threads),
  205. * but are in a different thread group
  206. */
  207. do_each_thread(g, q)
  208. if (q->mm == mm && q->tgid != p->tgid)
  209. __oom_kill_task(q);
  210. while_each_thread(g, q);
  211. return mm;
  212. }
  213. static struct mm_struct *oom_kill_process(struct task_struct *p,
  214. unsigned long points)
  215. {
  216. struct mm_struct *mm;
  217. struct task_struct *c;
  218. struct list_head *tsk;
  219. printk(KERN_ERR "Out of Memory: Kill process %d (%s) score %li and "
  220. "children.\n", p->pid, p->comm, points);
  221. /* Try to kill a child first */
  222. list_for_each(tsk, &p->children) {
  223. c = list_entry(tsk, struct task_struct, sibling);
  224. if (c->mm == p->mm)
  225. continue;
  226. mm = oom_kill_task(c);
  227. if (mm)
  228. return mm;
  229. }
  230. return oom_kill_task(p);
  231. }
  232. /**
  233. * oom_kill - kill the "best" process when we run out of memory
  234. *
  235. * If we run out of memory, we have the choice between either
  236. * killing a random task (bad), letting the system crash (worse)
  237. * OR try to be smart about which process to kill. Note that we
  238. * don't have to be perfect here, we just have to be good.
  239. */
  240. void out_of_memory(gfp_t gfp_mask, int order)
  241. {
  242. struct mm_struct *mm = NULL;
  243. task_t * p;
  244. unsigned long points;
  245. if (printk_ratelimit()) {
  246. printk("oom-killer: gfp_mask=0x%x, order=%d\n",
  247. gfp_mask, order);
  248. dump_stack();
  249. show_mem();
  250. }
  251. cpuset_lock();
  252. read_lock(&tasklist_lock);
  253. retry:
  254. p = select_bad_process(&points);
  255. if (PTR_ERR(p) == -1UL)
  256. goto out;
  257. /* Found nothing?!?! Either we hang forever, or we panic. */
  258. if (!p) {
  259. read_unlock(&tasklist_lock);
  260. cpuset_unlock();
  261. panic("Out of memory and no killable processes...\n");
  262. }
  263. mm = oom_kill_process(p, points);
  264. if (!mm)
  265. goto retry;
  266. out:
  267. read_unlock(&tasklist_lock);
  268. cpuset_unlock();
  269. if (mm)
  270. mmput(mm);
  271. /*
  272. * Give "p" a good chance of killing itself before we
  273. * retry to allocate memory unless "p" is current
  274. */
  275. if (!test_thread_flag(TIF_MEMDIE))
  276. schedule_timeout_interruptible(1);
  277. }