oom_kill.c 7.4 KB

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