oom_kill.c 9.7 KB

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