oom_kill.c 9.8 KB

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