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/sched/mm.h>
26#include <linux/sched/coredump.h>
27#include <linux/sched/task.h>
28#include <linux/sched/debug.h>
29#include <linux/swap.h>
30#include <linux/timex.h>
31#include <linux/jiffies.h>
32#include <linux/cpuset.h>
33#include <linux/export.h>
34#include <linux/notifier.h>
35#include <linux/memcontrol.h>
36#include <linux/mempolicy.h>
37#include <linux/security.h>
38#include <linux/ptrace.h>
39#include <linux/freezer.h>
40#include <linux/ftrace.h>
41#include <linux/ratelimit.h>
42#include <linux/kthread.h>
43#include <linux/init.h>
44#include <linux/mmu_notifier.h>
45
46#include <asm/tlb.h>
47#include "internal.h"
48#include "slab.h"
49
50#define CREATE_TRACE_POINTS
51#include <trace/events/oom.h>
52
53int sysctl_panic_on_oom;
54int sysctl_oom_kill_allocating_task;
55int sysctl_oom_dump_tasks = 1;
56
57DEFINE_MUTEX(oom_lock);
58
59DEFINE_MUTEX(oom_adj_mutex);
60
61static inline bool is_memcg_oom(struct oom_control *oc)
62{
63 return oc->memcg != NULL;
64}
65
66#ifdef CONFIG_NUMA
67
68
69
70
71
72
73
74
75
76
77
78
79static bool oom_cpuset_eligible(struct task_struct *start,
80 struct oom_control *oc)
81{
82 struct task_struct *tsk;
83 bool ret = false;
84 const nodemask_t *mask = oc->nodemask;
85
86 if (is_memcg_oom(oc))
87 return true;
88
89 rcu_read_lock();
90 for_each_thread(start, tsk) {
91 if (mask) {
92
93
94
95
96
97
98 ret = mempolicy_nodemask_intersects(tsk, mask);
99 } else {
100
101
102
103
104 ret = cpuset_mems_allowed_intersects(current, tsk);
105 }
106 if (ret)
107 break;
108 }
109 rcu_read_unlock();
110
111 return ret;
112}
113#else
114static bool oom_cpuset_eligible(struct task_struct *tsk, struct oom_control *oc)
115{
116 return true;
117}
118#endif
119
120
121
122
123
124
125
126struct task_struct *find_lock_task_mm(struct task_struct *p)
127{
128 struct task_struct *t;
129
130 rcu_read_lock();
131
132 for_each_thread(p, t) {
133 task_lock(t);
134 if (likely(t->mm))
135 goto found;
136 task_unlock(t);
137 }
138 t = NULL;
139found:
140 rcu_read_unlock();
141
142 return t;
143}
144
145
146
147
148
149static inline bool is_sysrq_oom(struct oom_control *oc)
150{
151 return oc->order == -1;
152}
153
154
155static bool oom_unkillable_task(struct task_struct *p)
156{
157 if (is_global_init(p))
158 return true;
159 if (p->flags & PF_KTHREAD)
160 return true;
161 return false;
162}
163
164
165
166
167
168static bool is_dump_unreclaim_slabs(void)
169{
170 unsigned long nr_lru;
171
172 nr_lru = global_node_page_state(NR_ACTIVE_ANON) +
173 global_node_page_state(NR_INACTIVE_ANON) +
174 global_node_page_state(NR_ACTIVE_FILE) +
175 global_node_page_state(NR_INACTIVE_FILE) +
176 global_node_page_state(NR_ISOLATED_ANON) +
177 global_node_page_state(NR_ISOLATED_FILE) +
178 global_node_page_state(NR_UNEVICTABLE);
179
180 return (global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B) > nr_lru);
181}
182
183
184
185
186
187
188
189
190
191
192long oom_badness(struct task_struct *p, unsigned long totalpages)
193{
194 long points;
195 long adj;
196
197 if (oom_unkillable_task(p))
198 return LONG_MIN;
199
200 p = find_lock_task_mm(p);
201 if (!p)
202 return LONG_MIN;
203
204
205
206
207
208
209 adj = (long)p->signal->oom_score_adj;
210 if (adj == OOM_SCORE_ADJ_MIN ||
211 test_bit(MMF_OOM_SKIP, &p->mm->flags) ||
212 in_vfork(p)) {
213 task_unlock(p);
214 return LONG_MIN;
215 }
216
217
218
219
220
221 points = get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) +
222 mm_pgtables_bytes(p->mm) / PAGE_SIZE;
223 task_unlock(p);
224
225
226 adj *= totalpages / 1000;
227 points += adj;
228
229 return points;
230}
231
232static const char * const oom_constraint_text[] = {
233 [CONSTRAINT_NONE] = "CONSTRAINT_NONE",
234 [CONSTRAINT_CPUSET] = "CONSTRAINT_CPUSET",
235 [CONSTRAINT_MEMORY_POLICY] = "CONSTRAINT_MEMORY_POLICY",
236 [CONSTRAINT_MEMCG] = "CONSTRAINT_MEMCG",
237};
238
239
240
241
242static enum oom_constraint constrained_alloc(struct oom_control *oc)
243{
244 struct zone *zone;
245 struct zoneref *z;
246 enum zone_type highest_zoneidx = gfp_zone(oc->gfp_mask);
247 bool cpuset_limited = false;
248 int nid;
249
250 if (is_memcg_oom(oc)) {
251 oc->totalpages = mem_cgroup_get_max(oc->memcg) ?: 1;
252 return CONSTRAINT_MEMCG;
253 }
254
255
256 oc->totalpages = totalram_pages() + total_swap_pages;
257
258 if (!IS_ENABLED(CONFIG_NUMA))
259 return CONSTRAINT_NONE;
260
261 if (!oc->zonelist)
262 return CONSTRAINT_NONE;
263
264
265
266
267
268 if (oc->gfp_mask & __GFP_THISNODE)
269 return CONSTRAINT_NONE;
270
271
272
273
274
275
276 if (oc->nodemask &&
277 !nodes_subset(node_states[N_MEMORY], *oc->nodemask)) {
278 oc->totalpages = total_swap_pages;
279 for_each_node_mask(nid, *oc->nodemask)
280 oc->totalpages += node_present_pages(nid);
281 return CONSTRAINT_MEMORY_POLICY;
282 }
283
284
285 for_each_zone_zonelist_nodemask(zone, z, oc->zonelist,
286 highest_zoneidx, oc->nodemask)
287 if (!cpuset_zone_allowed(zone, oc->gfp_mask))
288 cpuset_limited = true;
289
290 if (cpuset_limited) {
291 oc->totalpages = total_swap_pages;
292 for_each_node_mask(nid, cpuset_current_mems_allowed)
293 oc->totalpages += node_present_pages(nid);
294 return CONSTRAINT_CPUSET;
295 }
296 return CONSTRAINT_NONE;
297}
298
299static int oom_evaluate_task(struct task_struct *task, void *arg)
300{
301 struct oom_control *oc = arg;
302 long points;
303
304 if (oom_unkillable_task(task))
305 goto next;
306
307
308 if (!is_memcg_oom(oc) && !oom_cpuset_eligible(task, oc))
309 goto next;
310
311
312
313
314
315
316
317 if (!is_sysrq_oom(oc) && tsk_is_oom_victim(task)) {
318 if (test_bit(MMF_OOM_SKIP, &task->signal->oom_mm->flags))
319 goto next;
320 goto abort;
321 }
322
323
324
325
326
327 if (oom_task_origin(task)) {
328 points = LONG_MAX;
329 goto select;
330 }
331
332 points = oom_badness(task, oc->totalpages);
333 if (points == LONG_MIN || points < oc->chosen_points)
334 goto next;
335
336select:
337 if (oc->chosen)
338 put_task_struct(oc->chosen);
339 get_task_struct(task);
340 oc->chosen = task;
341 oc->chosen_points = points;
342next:
343 return 0;
344abort:
345 if (oc->chosen)
346 put_task_struct(oc->chosen);
347 oc->chosen = (void *)-1UL;
348 return 1;
349}
350
351
352
353
354
355static void select_bad_process(struct oom_control *oc)
356{
357 oc->chosen_points = LONG_MIN;
358
359 if (is_memcg_oom(oc))
360 mem_cgroup_scan_tasks(oc->memcg, oom_evaluate_task, oc);
361 else {
362 struct task_struct *p;
363
364 rcu_read_lock();
365 for_each_process(p)
366 if (oom_evaluate_task(p, oc))
367 break;
368 rcu_read_unlock();
369 }
370}
371
372static int dump_task(struct task_struct *p, void *arg)
373{
374 struct oom_control *oc = arg;
375 struct task_struct *task;
376
377 if (oom_unkillable_task(p))
378 return 0;
379
380
381 if (!is_memcg_oom(oc) && !oom_cpuset_eligible(p, oc))
382 return 0;
383
384 task = find_lock_task_mm(p);
385 if (!task) {
386
387
388
389
390
391 return 0;
392 }
393
394 pr_info("[%7d] %5d %5d %8lu %8lu %8ld %8lu %5hd %s\n",
395 task->pid, from_kuid(&init_user_ns, task_uid(task)),
396 task->tgid, task->mm->total_vm, get_mm_rss(task->mm),
397 mm_pgtables_bytes(task->mm),
398 get_mm_counter(task->mm, MM_SWAPENTS),
399 task->signal->oom_score_adj, task->comm);
400 task_unlock(task);
401
402 return 0;
403}
404
405
406
407
408
409
410
411
412
413
414
415static void dump_tasks(struct oom_control *oc)
416{
417 pr_info("Tasks state (memory values in pages):\n");
418 pr_info("[ pid ] uid tgid total_vm rss pgtables_bytes swapents oom_score_adj name\n");
419
420 if (is_memcg_oom(oc))
421 mem_cgroup_scan_tasks(oc->memcg, dump_task, oc);
422 else {
423 struct task_struct *p;
424
425 rcu_read_lock();
426 for_each_process(p)
427 dump_task(p, oc);
428 rcu_read_unlock();
429 }
430}
431
432static void dump_oom_summary(struct oom_control *oc, struct task_struct *victim)
433{
434
435 pr_info("oom-kill:constraint=%s,nodemask=%*pbl",
436 oom_constraint_text[oc->constraint],
437 nodemask_pr_args(oc->nodemask));
438 cpuset_print_current_mems_allowed();
439 mem_cgroup_print_oom_context(oc->memcg, victim);
440 pr_cont(",task=%s,pid=%d,uid=%d\n", victim->comm, victim->pid,
441 from_kuid(&init_user_ns, task_uid(victim)));
442}
443
444static void dump_header(struct oom_control *oc, struct task_struct *p)
445{
446 pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd\n",
447 current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order,
448 current->signal->oom_score_adj);
449 if (!IS_ENABLED(CONFIG_COMPACTION) && oc->order)
450 pr_warn("COMPACTION is disabled!!!\n");
451
452 dump_stack();
453 if (is_memcg_oom(oc))
454 mem_cgroup_print_oom_meminfo(oc->memcg);
455 else {
456 show_mem(SHOW_MEM_FILTER_NODES, oc->nodemask);
457 if (is_dump_unreclaim_slabs())
458 dump_unreclaimable_slab();
459 }
460 if (sysctl_oom_dump_tasks)
461 dump_tasks(oc);
462 if (p)
463 dump_oom_summary(oc, p);
464}
465
466
467
468
469static atomic_t oom_victims = ATOMIC_INIT(0);
470static DECLARE_WAIT_QUEUE_HEAD(oom_victims_wait);
471
472static bool oom_killer_disabled __read_mostly;
473
474#define K(x) ((x) << (PAGE_SHIFT-10))
475
476
477
478
479
480
481
482bool process_shares_mm(struct task_struct *p, struct mm_struct *mm)
483{
484 struct task_struct *t;
485
486 for_each_thread(p, t) {
487 struct mm_struct *t_mm = READ_ONCE(t->mm);
488 if (t_mm)
489 return t_mm == mm;
490 }
491 return false;
492}
493
494#ifdef CONFIG_MMU
495
496
497
498
499static struct task_struct *oom_reaper_th;
500static DECLARE_WAIT_QUEUE_HEAD(oom_reaper_wait);
501static struct task_struct *oom_reaper_list;
502static DEFINE_SPINLOCK(oom_reaper_lock);
503
504void __oom_reap_task_mm(struct mm_struct *mm)
505{
506 struct vm_area_struct *vma;
507
508
509
510
511
512
513
514 set_bit(MMF_UNSTABLE, &mm->flags);
515
516 for (vma = mm->mmap ; vma; vma = vma->vm_next) {
517 if (!can_madv_lru_vma(vma))
518 continue;
519
520
521
522
523
524
525
526
527
528
529
530 if (vma_is_anonymous(vma) || !(vma->vm_flags & VM_SHARED)) {
531 struct mmu_notifier_range range;
532 struct mmu_gather tlb;
533
534 mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0,
535 vma, mm, vma->vm_start,
536 vma->vm_end);
537 tlb_gather_mmu(&tlb, mm, range.start, range.end);
538 mmu_notifier_invalidate_range_start(&range);
539 unmap_page_range(&tlb, vma, range.start, range.end, NULL);
540 mmu_notifier_invalidate_range_end(&range);
541 tlb_finish_mmu(&tlb, range.start, range.end);
542 }
543 }
544}
545
546static bool oom_reap_task_mm(struct task_struct *tsk, struct mm_struct *mm)
547{
548 if (!mmap_read_trylock(mm)) {
549 trace_skip_task_reaping(tsk->pid);
550 return false;
551 }
552
553
554
555
556
557
558
559 if (mm_has_blockable_invalidate_notifiers(mm)) {
560 mmap_read_unlock(mm);
561 schedule_timeout_idle(HZ);
562 return true;
563 }
564
565
566
567
568
569
570
571 if (test_bit(MMF_OOM_SKIP, &mm->flags)) {
572 mmap_read_unlock(mm);
573 trace_skip_task_reaping(tsk->pid);
574 return true;
575 }
576
577 trace_start_task_reaping(tsk->pid);
578
579 __oom_reap_task_mm(mm);
580
581 pr_info("oom_reaper: reaped process %d (%s), now anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB\n",
582 task_pid_nr(tsk), tsk->comm,
583 K(get_mm_counter(mm, MM_ANONPAGES)),
584 K(get_mm_counter(mm, MM_FILEPAGES)),
585 K(get_mm_counter(mm, MM_SHMEMPAGES)));
586 mmap_read_unlock(mm);
587
588 trace_finish_task_reaping(tsk->pid);
589 return true;
590}
591
592#define MAX_OOM_REAP_RETRIES 10
593static void oom_reap_task(struct task_struct *tsk)
594{
595 int attempts = 0;
596 struct mm_struct *mm = tsk->signal->oom_mm;
597
598
599 while (attempts++ < MAX_OOM_REAP_RETRIES && !oom_reap_task_mm(tsk, mm))
600 schedule_timeout_idle(HZ/10);
601
602 if (attempts <= MAX_OOM_REAP_RETRIES ||
603 test_bit(MMF_OOM_SKIP, &mm->flags))
604 goto done;
605
606 pr_info("oom_reaper: unable to reap pid:%d (%s)\n",
607 task_pid_nr(tsk), tsk->comm);
608 sched_show_task(tsk);
609 debug_show_all_locks();
610
611done:
612 tsk->oom_reaper_list = NULL;
613
614
615
616
617
618 set_bit(MMF_OOM_SKIP, &mm->flags);
619
620
621 put_task_struct(tsk);
622}
623
624static int oom_reaper(void *unused)
625{
626 while (true) {
627 struct task_struct *tsk = NULL;
628
629 wait_event_freezable(oom_reaper_wait, oom_reaper_list != NULL);
630 spin_lock(&oom_reaper_lock);
631 if (oom_reaper_list != NULL) {
632 tsk = oom_reaper_list;
633 oom_reaper_list = tsk->oom_reaper_list;
634 }
635 spin_unlock(&oom_reaper_lock);
636
637 if (tsk)
638 oom_reap_task(tsk);
639 }
640
641 return 0;
642}
643
644static void wake_oom_reaper(struct task_struct *tsk)
645{
646
647 if (test_and_set_bit(MMF_OOM_REAP_QUEUED, &tsk->signal->oom_mm->flags))
648 return;
649
650 get_task_struct(tsk);
651
652 spin_lock(&oom_reaper_lock);
653 tsk->oom_reaper_list = oom_reaper_list;
654 oom_reaper_list = tsk;
655 spin_unlock(&oom_reaper_lock);
656 trace_wake_reaper(tsk->pid);
657 wake_up(&oom_reaper_wait);
658}
659
660static int __init oom_init(void)
661{
662 oom_reaper_th = kthread_run(oom_reaper, NULL, "oom_reaper");
663 return 0;
664}
665subsys_initcall(oom_init)
666#else
667static inline void wake_oom_reaper(struct task_struct *tsk)
668{
669}
670#endif
671
672
673
674
675
676
677
678
679
680
681
682static void mark_oom_victim(struct task_struct *tsk)
683{
684 struct mm_struct *mm = tsk->mm;
685
686 WARN_ON(oom_killer_disabled);
687
688 if (test_and_set_tsk_thread_flag(tsk, TIF_MEMDIE))
689 return;
690
691
692 if (!cmpxchg(&tsk->signal->oom_mm, NULL, mm)) {
693 mmgrab(tsk->signal->oom_mm);
694 set_bit(MMF_OOM_VICTIM, &mm->flags);
695 }
696
697
698
699
700
701
702
703 __thaw_task(tsk);
704 atomic_inc(&oom_victims);
705 trace_mark_victim(tsk->pid);
706}
707
708
709
710
711void exit_oom_victim(void)
712{
713 clear_thread_flag(TIF_MEMDIE);
714
715 if (!atomic_dec_return(&oom_victims))
716 wake_up_all(&oom_victims_wait);
717}
718
719
720
721
722void oom_killer_enable(void)
723{
724 oom_killer_disabled = false;
725 pr_info("OOM killer enabled.\n");
726}
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743bool oom_killer_disable(signed long timeout)
744{
745 signed long ret;
746
747
748
749
750
751 if (mutex_lock_killable(&oom_lock))
752 return false;
753 oom_killer_disabled = true;
754 mutex_unlock(&oom_lock);
755
756 ret = wait_event_interruptible_timeout(oom_victims_wait,
757 !atomic_read(&oom_victims), timeout);
758 if (ret <= 0) {
759 oom_killer_enable();
760 return false;
761 }
762 pr_info("OOM killer disabled.\n");
763
764 return true;
765}
766
767static inline bool __task_will_free_mem(struct task_struct *task)
768{
769 struct signal_struct *sig = task->signal;
770
771
772
773
774
775
776 if (sig->flags & SIGNAL_GROUP_COREDUMP)
777 return false;
778
779 if (sig->flags & SIGNAL_GROUP_EXIT)
780 return true;
781
782 if (thread_group_empty(task) && (task->flags & PF_EXITING))
783 return true;
784
785 return false;
786}
787
788
789
790
791
792
793
794
795static bool task_will_free_mem(struct task_struct *task)
796{
797 struct mm_struct *mm = task->mm;
798 struct task_struct *p;
799 bool ret = true;
800
801
802
803
804
805
806 if (!mm)
807 return false;
808
809 if (!__task_will_free_mem(task))
810 return false;
811
812
813
814
815
816 if (test_bit(MMF_OOM_SKIP, &mm->flags))
817 return false;
818
819 if (atomic_read(&mm->mm_users) <= 1)
820 return true;
821
822
823
824
825
826
827 rcu_read_lock();
828 for_each_process(p) {
829 if (!process_shares_mm(p, mm))
830 continue;
831 if (same_thread_group(task, p))
832 continue;
833 ret = __task_will_free_mem(p);
834 if (!ret)
835 break;
836 }
837 rcu_read_unlock();
838
839 return ret;
840}
841
842static void __oom_kill_process(struct task_struct *victim, const char *message)
843{
844 struct task_struct *p;
845 struct mm_struct *mm;
846 bool can_oom_reap = true;
847
848 p = find_lock_task_mm(victim);
849 if (!p) {
850 pr_info("%s: OOM victim %d (%s) is already exiting. Skip killing the task\n",
851 message, task_pid_nr(victim), victim->comm);
852 put_task_struct(victim);
853 return;
854 } else if (victim != p) {
855 get_task_struct(p);
856 put_task_struct(victim);
857 victim = p;
858 }
859
860
861 mm = victim->mm;
862 mmgrab(mm);
863
864
865 count_vm_event(OOM_KILL);
866 memcg_memory_event_mm(mm, MEMCG_OOM_KILL);
867
868
869
870
871
872
873 do_send_sig_info(SIGKILL, SEND_SIG_PRIV, victim, PIDTYPE_TGID);
874 mark_oom_victim(victim);
875 pr_err("%s: Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB, UID:%u pgtables:%lukB oom_score_adj:%hd\n",
876 message, task_pid_nr(victim), victim->comm, K(mm->total_vm),
877 K(get_mm_counter(mm, MM_ANONPAGES)),
878 K(get_mm_counter(mm, MM_FILEPAGES)),
879 K(get_mm_counter(mm, MM_SHMEMPAGES)),
880 from_kuid(&init_user_ns, task_uid(victim)),
881 mm_pgtables_bytes(mm) >> 10, victim->signal->oom_score_adj);
882 task_unlock(victim);
883
884
885
886
887
888
889
890
891
892
893 rcu_read_lock();
894 for_each_process(p) {
895 if (!process_shares_mm(p, mm))
896 continue;
897 if (same_thread_group(p, victim))
898 continue;
899 if (is_global_init(p)) {
900 can_oom_reap = false;
901 set_bit(MMF_OOM_SKIP, &mm->flags);
902 pr_info("oom killer %d (%s) has mm pinned by %d (%s)\n",
903 task_pid_nr(victim), victim->comm,
904 task_pid_nr(p), p->comm);
905 continue;
906 }
907
908
909
910
911 if (unlikely(p->flags & PF_KTHREAD))
912 continue;
913 do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID);
914 }
915 rcu_read_unlock();
916
917 if (can_oom_reap)
918 wake_oom_reaper(victim);
919
920 mmdrop(mm);
921 put_task_struct(victim);
922}
923#undef K
924
925
926
927
928
929static int oom_kill_memcg_member(struct task_struct *task, void *message)
930{
931 if (task->signal->oom_score_adj != OOM_SCORE_ADJ_MIN &&
932 !is_global_init(task)) {
933 get_task_struct(task);
934 __oom_kill_process(task, message);
935 }
936 return 0;
937}
938
939static void oom_kill_process(struct oom_control *oc, const char *message)
940{
941 struct task_struct *victim = oc->chosen;
942 struct mem_cgroup *oom_group;
943 static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL,
944 DEFAULT_RATELIMIT_BURST);
945
946
947
948
949
950
951 task_lock(victim);
952 if (task_will_free_mem(victim)) {
953 mark_oom_victim(victim);
954 wake_oom_reaper(victim);
955 task_unlock(victim);
956 put_task_struct(victim);
957 return;
958 }
959 task_unlock(victim);
960
961 if (__ratelimit(&oom_rs))
962 dump_header(oc, victim);
963
964
965
966
967
968
969 oom_group = mem_cgroup_get_oom_group(victim, oc->memcg);
970
971 __oom_kill_process(victim, message);
972
973
974
975
976 if (oom_group) {
977 mem_cgroup_print_oom_group(oom_group);
978 mem_cgroup_scan_tasks(oom_group, oom_kill_memcg_member,
979 (void*)message);
980 mem_cgroup_put(oom_group);
981 }
982}
983
984
985
986
987static void check_panic_on_oom(struct oom_control *oc)
988{
989 if (likely(!sysctl_panic_on_oom))
990 return;
991 if (sysctl_panic_on_oom != 2) {
992
993
994
995
996
997 if (oc->constraint != CONSTRAINT_NONE)
998 return;
999 }
1000
1001 if (is_sysrq_oom(oc))
1002 return;
1003 dump_header(oc, NULL);
1004 panic("Out of memory: %s panic_on_oom is enabled\n",
1005 sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide");
1006}
1007
1008static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
1009
1010int register_oom_notifier(struct notifier_block *nb)
1011{
1012 return blocking_notifier_chain_register(&oom_notify_list, nb);
1013}
1014EXPORT_SYMBOL_GPL(register_oom_notifier);
1015
1016int unregister_oom_notifier(struct notifier_block *nb)
1017{
1018 return blocking_notifier_chain_unregister(&oom_notify_list, nb);
1019}
1020EXPORT_SYMBOL_GPL(unregister_oom_notifier);
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031bool out_of_memory(struct oom_control *oc)
1032{
1033 unsigned long freed = 0;
1034
1035 if (oom_killer_disabled)
1036 return false;
1037
1038 if (!is_memcg_oom(oc)) {
1039 blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
1040 if (freed > 0)
1041
1042 return true;
1043 }
1044
1045
1046
1047
1048
1049
1050 if (task_will_free_mem(current)) {
1051 mark_oom_victim(current);
1052 wake_oom_reaper(current);
1053 return true;
1054 }
1055
1056
1057
1058
1059
1060
1061
1062
1063 if (oc->gfp_mask && !(oc->gfp_mask & __GFP_FS) && !is_memcg_oom(oc))
1064 return true;
1065
1066
1067
1068
1069
1070 oc->constraint = constrained_alloc(oc);
1071 if (oc->constraint != CONSTRAINT_MEMORY_POLICY)
1072 oc->nodemask = NULL;
1073 check_panic_on_oom(oc);
1074
1075 if (!is_memcg_oom(oc) && sysctl_oom_kill_allocating_task &&
1076 current->mm && !oom_unkillable_task(current) &&
1077 oom_cpuset_eligible(current, oc) &&
1078 current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {
1079 get_task_struct(current);
1080 oc->chosen = current;
1081 oom_kill_process(oc, "Out of memory (oom_kill_allocating_task)");
1082 return true;
1083 }
1084
1085 select_bad_process(oc);
1086
1087 if (!oc->chosen) {
1088 dump_header(oc, NULL);
1089 pr_warn("Out of memory and no killable processes...\n");
1090
1091
1092
1093
1094
1095 if (!is_sysrq_oom(oc) && !is_memcg_oom(oc))
1096 panic("System is deadlocked on memory\n");
1097 }
1098 if (oc->chosen && oc->chosen != (void *)-1UL) {
1099 oom_kill_process(oc, !is_memcg_oom(oc) ? "Out of memory" :
1100 "Memory cgroup out of memory");
1101
1102
1103
1104
1105 schedule_timeout_killable(1);
1106 }
1107 return !!oc->chosen;
1108}
1109
1110
1111
1112
1113
1114
1115void pagefault_out_of_memory(void)
1116{
1117 struct oom_control oc = {
1118 .zonelist = NULL,
1119 .nodemask = NULL,
1120 .memcg = NULL,
1121 .gfp_mask = 0,
1122 .order = 0,
1123 };
1124
1125 if (mem_cgroup_oom_synchronize(true))
1126 return;
1127
1128 if (!mutex_trylock(&oom_lock))
1129 return;
1130 out_of_memory(&oc);
1131 mutex_unlock(&oom_lock);
1132}
1133