1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/oom.h>
21#include <linux/mm.h>
22#include <linux/err.h>
23#include <linux/gfp.h>
24#include <linux/sched.h>
25#include <linux/swap.h>
26#include <linux/timex.h>
27#include <linux/jiffies.h>
28#include <linux/cpuset.h>
29#include <linux/export.h>
30#include <linux/notifier.h>
31#include <linux/memcontrol.h>
32#include <linux/mempolicy.h>
33#include <linux/security.h>
34#include <linux/ptrace.h>
35#include <linux/freezer.h>
36#include <linux/ftrace.h>
37#include <linux/ratelimit.h>
38
39#define CREATE_TRACE_POINTS
40#include <trace/events/oom.h>
41
42int sysctl_panic_on_oom;
43int sysctl_oom_kill_allocating_task;
44int sysctl_oom_dump_tasks = 1;
45static DEFINE_SPINLOCK(zone_scan_lock);
46
47#ifdef CONFIG_NUMA
48
49
50
51
52
53
54
55
56
57static bool has_intersects_mems_allowed(struct task_struct *start,
58 const nodemask_t *mask)
59{
60 struct task_struct *tsk;
61 bool ret = false;
62
63 rcu_read_lock();
64 for_each_thread(start, tsk) {
65 if (mask) {
66
67
68
69
70
71
72 ret = mempolicy_nodemask_intersects(tsk, mask);
73 } else {
74
75
76
77
78 ret = cpuset_mems_allowed_intersects(current, tsk);
79 }
80 if (ret)
81 break;
82 }
83 rcu_read_unlock();
84
85 return ret;
86}
87#else
88static bool has_intersects_mems_allowed(struct task_struct *tsk,
89 const nodemask_t *mask)
90{
91 return true;
92}
93#endif
94
95
96
97
98
99
100
101struct task_struct *find_lock_task_mm(struct task_struct *p)
102{
103 struct task_struct *t;
104
105 rcu_read_lock();
106
107 for_each_thread(p, t) {
108 task_lock(t);
109 if (likely(t->mm))
110 goto found;
111 task_unlock(t);
112 }
113 t = NULL;
114found:
115 rcu_read_unlock();
116
117 return t;
118}
119
120
121static bool oom_unkillable_task(struct task_struct *p,
122 struct mem_cgroup *memcg, const nodemask_t *nodemask)
123{
124 if (is_global_init(p))
125 return true;
126 if (p->flags & PF_KTHREAD)
127 return true;
128
129
130 if (memcg && !task_in_mem_cgroup(p, memcg))
131 return true;
132
133
134 if (!has_intersects_mems_allowed(p, nodemask))
135 return true;
136
137 return false;
138}
139
140
141
142
143
144
145
146
147
148
149unsigned long oom_badness(struct task_struct *p, struct mem_cgroup *memcg,
150 const nodemask_t *nodemask, unsigned long totalpages)
151{
152 long points;
153 long adj;
154
155 if (oom_unkillable_task(p, memcg, nodemask))
156 return 0;
157
158 p = find_lock_task_mm(p);
159 if (!p)
160 return 0;
161
162 adj = (long)p->signal->oom_score_adj;
163 if (adj == OOM_SCORE_ADJ_MIN) {
164 task_unlock(p);
165 return 0;
166 }
167
168
169
170
171
172 points = get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) +
173 atomic_long_read(&p->mm->nr_ptes) + mm_nr_pmds(p->mm);
174 task_unlock(p);
175
176
177
178
179
180 if (has_capability_noaudit(p, CAP_SYS_ADMIN))
181 points -= (points * 3) / 100;
182
183
184 adj *= totalpages / 1000;
185 points += adj;
186
187
188
189
190
191 return points > 0 ? points : 1;
192}
193
194
195
196
197#ifdef CONFIG_NUMA
198static enum oom_constraint constrained_alloc(struct zonelist *zonelist,
199 gfp_t gfp_mask, nodemask_t *nodemask,
200 unsigned long *totalpages)
201{
202 struct zone *zone;
203 struct zoneref *z;
204 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
205 bool cpuset_limited = false;
206 int nid;
207
208
209 *totalpages = totalram_pages + total_swap_pages;
210
211 if (!zonelist)
212 return CONSTRAINT_NONE;
213
214
215
216
217
218 if (gfp_mask & __GFP_THISNODE)
219 return CONSTRAINT_NONE;
220
221
222
223
224
225
226 if (nodemask && !nodes_subset(node_states[N_MEMORY], *nodemask)) {
227 *totalpages = total_swap_pages;
228 for_each_node_mask(nid, *nodemask)
229 *totalpages += node_spanned_pages(nid);
230 return CONSTRAINT_MEMORY_POLICY;
231 }
232
233
234 for_each_zone_zonelist_nodemask(zone, z, zonelist,
235 high_zoneidx, nodemask)
236 if (!cpuset_zone_allowed(zone, gfp_mask))
237 cpuset_limited = true;
238
239 if (cpuset_limited) {
240 *totalpages = total_swap_pages;
241 for_each_node_mask(nid, cpuset_current_mems_allowed)
242 *totalpages += node_spanned_pages(nid);
243 return CONSTRAINT_CPUSET;
244 }
245 return CONSTRAINT_NONE;
246}
247#else
248static enum oom_constraint constrained_alloc(struct zonelist *zonelist,
249 gfp_t gfp_mask, nodemask_t *nodemask,
250 unsigned long *totalpages)
251{
252 *totalpages = totalram_pages + total_swap_pages;
253 return CONSTRAINT_NONE;
254}
255#endif
256
257enum oom_scan_t oom_scan_process_thread(struct task_struct *task,
258 unsigned long totalpages, const nodemask_t *nodemask,
259 bool force_kill)
260{
261 if (oom_unkillable_task(task, NULL, nodemask))
262 return OOM_SCAN_CONTINUE;
263
264
265
266
267
268 if (test_tsk_thread_flag(task, TIF_MEMDIE)) {
269 if (!force_kill)
270 return OOM_SCAN_ABORT;
271 }
272 if (!task->mm)
273 return OOM_SCAN_CONTINUE;
274
275
276
277
278
279 if (oom_task_origin(task))
280 return OOM_SCAN_SELECT;
281
282 if (task_will_free_mem(task) && !force_kill)
283 return OOM_SCAN_ABORT;
284
285 return OOM_SCAN_OK;
286}
287
288
289
290
291
292
293
294static struct task_struct *select_bad_process(unsigned int *ppoints,
295 unsigned long totalpages, const nodemask_t *nodemask,
296 bool force_kill)
297{
298 struct task_struct *g, *p;
299 struct task_struct *chosen = NULL;
300 unsigned long chosen_points = 0;
301
302 rcu_read_lock();
303 for_each_process_thread(g, p) {
304 unsigned int points;
305
306 switch (oom_scan_process_thread(p, totalpages, nodemask,
307 force_kill)) {
308 case OOM_SCAN_SELECT:
309 chosen = p;
310 chosen_points = ULONG_MAX;
311
312 case OOM_SCAN_CONTINUE:
313 continue;
314 case OOM_SCAN_ABORT:
315 rcu_read_unlock();
316 return (struct task_struct *)(-1UL);
317 case OOM_SCAN_OK:
318 break;
319 };
320 points = oom_badness(p, NULL, nodemask, totalpages);
321 if (!points || points < chosen_points)
322 continue;
323
324 if (points == chosen_points && thread_group_leader(chosen))
325 continue;
326
327 chosen = p;
328 chosen_points = points;
329 }
330 if (chosen)
331 get_task_struct(chosen);
332 rcu_read_unlock();
333
334 *ppoints = chosen_points * 1000 / totalpages;
335 return chosen;
336}
337
338
339
340
341
342
343
344
345
346
347
348
349static void dump_tasks(struct mem_cgroup *memcg, const nodemask_t *nodemask)
350{
351 struct task_struct *p;
352 struct task_struct *task;
353
354 pr_info("[ pid ] uid tgid total_vm rss nr_ptes nr_pmds swapents oom_score_adj name\n");
355 rcu_read_lock();
356 for_each_process(p) {
357 if (oom_unkillable_task(p, memcg, nodemask))
358 continue;
359
360 task = find_lock_task_mm(p);
361 if (!task) {
362
363
364
365
366
367 continue;
368 }
369
370 pr_info("[%5d] %5d %5d %8lu %8lu %7ld %7ld %8lu %5hd %s\n",
371 task->pid, from_kuid(&init_user_ns, task_uid(task)),
372 task->tgid, task->mm->total_vm, get_mm_rss(task->mm),
373 atomic_long_read(&task->mm->nr_ptes),
374 mm_nr_pmds(task->mm),
375 get_mm_counter(task->mm, MM_SWAPENTS),
376 task->signal->oom_score_adj, task->comm);
377 task_unlock(task);
378 }
379 rcu_read_unlock();
380}
381
382static void dump_header(struct task_struct *p, gfp_t gfp_mask, int order,
383 struct mem_cgroup *memcg, const nodemask_t *nodemask)
384{
385 task_lock(current);
386 pr_warning("%s invoked oom-killer: gfp_mask=0x%x, order=%d, "
387 "oom_score_adj=%hd\n",
388 current->comm, gfp_mask, order,
389 current->signal->oom_score_adj);
390 cpuset_print_task_mems_allowed(current);
391 task_unlock(current);
392 dump_stack();
393 if (memcg)
394 mem_cgroup_print_oom_info(memcg, p);
395 else
396 show_mem(SHOW_MEM_FILTER_NODES);
397 if (sysctl_oom_dump_tasks)
398 dump_tasks(memcg, nodemask);
399}
400
401
402
403
404static atomic_t oom_victims = ATOMIC_INIT(0);
405static DECLARE_WAIT_QUEUE_HEAD(oom_victims_wait);
406
407bool oom_killer_disabled __read_mostly;
408static DECLARE_RWSEM(oom_sem);
409
410
411
412
413
414
415
416
417void mark_tsk_oom_victim(struct task_struct *tsk)
418{
419 WARN_ON(oom_killer_disabled);
420
421 if (test_and_set_tsk_thread_flag(tsk, TIF_MEMDIE))
422 return;
423
424
425
426
427
428
429 __thaw_task(tsk);
430 atomic_inc(&oom_victims);
431}
432
433
434
435
436
437
438void unmark_oom_victim(void)
439{
440 if (!test_and_clear_thread_flag(TIF_MEMDIE))
441 return;
442
443 down_read(&oom_sem);
444
445
446
447
448 if (!atomic_dec_return(&oom_victims) && oom_killer_disabled)
449 wake_up_all(&oom_victims_wait);
450 up_read(&oom_sem);
451}
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466bool oom_killer_disable(void)
467{
468
469
470
471
472 down_write(&oom_sem);
473 if (test_thread_flag(TIF_MEMDIE)) {
474 up_write(&oom_sem);
475 return false;
476 }
477
478 oom_killer_disabled = true;
479 up_write(&oom_sem);
480
481 wait_event(oom_victims_wait, !atomic_read(&oom_victims));
482
483 return true;
484}
485
486
487
488
489void oom_killer_enable(void)
490{
491 down_write(&oom_sem);
492 oom_killer_disabled = false;
493 up_write(&oom_sem);
494}
495
496#define K(x) ((x) << (PAGE_SHIFT-10))
497
498
499
500
501void oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
502 unsigned int points, unsigned long totalpages,
503 struct mem_cgroup *memcg, nodemask_t *nodemask,
504 const char *message)
505{
506 struct task_struct *victim = p;
507 struct task_struct *child;
508 struct task_struct *t;
509 struct mm_struct *mm;
510 unsigned int victim_points = 0;
511 static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL,
512 DEFAULT_RATELIMIT_BURST);
513
514
515
516
517
518 task_lock(p);
519 if (p->mm && task_will_free_mem(p)) {
520 mark_tsk_oom_victim(p);
521 task_unlock(p);
522 put_task_struct(p);
523 return;
524 }
525 task_unlock(p);
526
527 if (__ratelimit(&oom_rs))
528 dump_header(p, gfp_mask, order, memcg, nodemask);
529
530 task_lock(p);
531 pr_err("%s: Kill process %d (%s) score %d or sacrifice child\n",
532 message, task_pid_nr(p), p->comm, points);
533 task_unlock(p);
534
535
536
537
538
539
540
541 read_lock(&tasklist_lock);
542 for_each_thread(p, t) {
543 list_for_each_entry(child, &t->children, sibling) {
544 unsigned int child_points;
545
546 if (child->mm == p->mm)
547 continue;
548
549
550
551 child_points = oom_badness(child, memcg, nodemask,
552 totalpages);
553 if (child_points > victim_points) {
554 put_task_struct(victim);
555 victim = child;
556 victim_points = child_points;
557 get_task_struct(victim);
558 }
559 }
560 }
561 read_unlock(&tasklist_lock);
562
563 p = find_lock_task_mm(victim);
564 if (!p) {
565 put_task_struct(victim);
566 return;
567 } else if (victim != p) {
568 get_task_struct(p);
569 put_task_struct(victim);
570 victim = p;
571 }
572
573
574 mm = victim->mm;
575 mark_tsk_oom_victim(victim);
576 pr_err("Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB\n",
577 task_pid_nr(victim), victim->comm, K(victim->mm->total_vm),
578 K(get_mm_counter(victim->mm, MM_ANONPAGES)),
579 K(get_mm_counter(victim->mm, MM_FILEPAGES)));
580 task_unlock(victim);
581
582
583
584
585
586
587
588
589
590
591 rcu_read_lock();
592 for_each_process(p)
593 if (p->mm == mm && !same_thread_group(p, victim) &&
594 !(p->flags & PF_KTHREAD)) {
595 if (p->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
596 continue;
597
598 task_lock(p);
599 pr_err("Kill process %d (%s) sharing same memory\n",
600 task_pid_nr(p), p->comm);
601 task_unlock(p);
602 do_send_sig_info(SIGKILL, SEND_SIG_FORCED, p, true);
603 }
604 rcu_read_unlock();
605
606 do_send_sig_info(SIGKILL, SEND_SIG_FORCED, victim, true);
607 put_task_struct(victim);
608}
609#undef K
610
611
612
613
614void check_panic_on_oom(enum oom_constraint constraint, gfp_t gfp_mask,
615 int order, const nodemask_t *nodemask)
616{
617 if (likely(!sysctl_panic_on_oom))
618 return;
619 if (sysctl_panic_on_oom != 2) {
620
621
622
623
624
625 if (constraint != CONSTRAINT_NONE)
626 return;
627 }
628 dump_header(NULL, gfp_mask, order, NULL, nodemask);
629 panic("Out of memory: %s panic_on_oom is enabled\n",
630 sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide");
631}
632
633static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
634
635int register_oom_notifier(struct notifier_block *nb)
636{
637 return blocking_notifier_chain_register(&oom_notify_list, nb);
638}
639EXPORT_SYMBOL_GPL(register_oom_notifier);
640
641int unregister_oom_notifier(struct notifier_block *nb)
642{
643 return blocking_notifier_chain_unregister(&oom_notify_list, nb);
644}
645EXPORT_SYMBOL_GPL(unregister_oom_notifier);
646
647
648
649
650
651
652bool oom_zonelist_trylock(struct zonelist *zonelist, gfp_t gfp_mask)
653{
654 struct zoneref *z;
655 struct zone *zone;
656 bool ret = true;
657
658 spin_lock(&zone_scan_lock);
659 for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask))
660 if (test_bit(ZONE_OOM_LOCKED, &zone->flags)) {
661 ret = false;
662 goto out;
663 }
664
665
666
667
668
669 for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask))
670 set_bit(ZONE_OOM_LOCKED, &zone->flags);
671
672out:
673 spin_unlock(&zone_scan_lock);
674 return ret;
675}
676
677
678
679
680
681
682void oom_zonelist_unlock(struct zonelist *zonelist, gfp_t gfp_mask)
683{
684 struct zoneref *z;
685 struct zone *zone;
686
687 spin_lock(&zone_scan_lock);
688 for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask))
689 clear_bit(ZONE_OOM_LOCKED, &zone->flags);
690 spin_unlock(&zone_scan_lock);
691}
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706static void __out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask,
707 int order, nodemask_t *nodemask, bool force_kill)
708{
709 const nodemask_t *mpol_mask;
710 struct task_struct *p;
711 unsigned long totalpages;
712 unsigned long freed = 0;
713 unsigned int uninitialized_var(points);
714 enum oom_constraint constraint = CONSTRAINT_NONE;
715 int killed = 0;
716
717 blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
718 if (freed > 0)
719
720 return;
721
722
723
724
725
726
727
728
729
730 if (current->mm &&
731 (fatal_signal_pending(current) || task_will_free_mem(current))) {
732 mark_tsk_oom_victim(current);
733 return;
734 }
735
736
737
738
739
740 constraint = constrained_alloc(zonelist, gfp_mask, nodemask,
741 &totalpages);
742 mpol_mask = (constraint == CONSTRAINT_MEMORY_POLICY) ? nodemask : NULL;
743 check_panic_on_oom(constraint, gfp_mask, order, mpol_mask);
744
745 if (sysctl_oom_kill_allocating_task && current->mm &&
746 !oom_unkillable_task(current, NULL, nodemask) &&
747 current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {
748 get_task_struct(current);
749 oom_kill_process(current, gfp_mask, order, 0, totalpages, NULL,
750 nodemask,
751 "Out of memory (oom_kill_allocating_task)");
752 goto out;
753 }
754
755 p = select_bad_process(&points, totalpages, mpol_mask, force_kill);
756
757 if (!p) {
758 dump_header(NULL, gfp_mask, order, NULL, mpol_mask);
759 panic("Out of memory and no killable processes...\n");
760 }
761 if (p != (void *)-1UL) {
762 oom_kill_process(p, gfp_mask, order, points, totalpages, NULL,
763 nodemask, "Out of memory");
764 killed = 1;
765 }
766out:
767
768
769
770
771 if (killed)
772 schedule_timeout_killable(1);
773}
774
775
776
777
778
779
780
781
782
783
784
785
786bool out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask,
787 int order, nodemask_t *nodemask, bool force_kill)
788{
789 bool ret = false;
790
791 down_read(&oom_sem);
792 if (!oom_killer_disabled) {
793 __out_of_memory(zonelist, gfp_mask, order, nodemask, force_kill);
794 ret = true;
795 }
796 up_read(&oom_sem);
797
798 return ret;
799}
800
801
802
803
804
805
806void pagefault_out_of_memory(void)
807{
808 struct zonelist *zonelist;
809
810 down_read(&oom_sem);
811 if (mem_cgroup_oom_synchronize(true))
812 goto unlock;
813
814 zonelist = node_zonelist(first_memory_node, GFP_KERNEL);
815 if (oom_zonelist_trylock(zonelist, GFP_KERNEL)) {
816 if (!oom_killer_disabled)
817 __out_of_memory(NULL, 0, 0, NULL, false);
818 else
819
820
821
822
823
824
825 WARN_ON(test_thread_flag(TIF_MEMDIE));
826
827 oom_zonelist_unlock(zonelist, GFP_KERNEL);
828 }
829unlock:
830 up_read(&oom_sem);
831}
832