oom_kill.c 12 KB

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