oom_kill.c 15 KB

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