oom_kill.c 13 KB

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