oom_kill.c 11 KB

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