linux/mm/oom_kill.c
<<
>>
Prefs
   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 *  Copyright (C)  2010  Google, Inc.
   8 *      Rewritten by David Rientjes
   9 *
  10 *  The routines in this file are used to kill a process when
  11 *  we're seriously out of memory. This gets called from __alloc_pages()
  12 *  in mm/page_alloc.c when we really run out of memory.
  13 *
  14 *  Since we won't call these routines often (on a well-configured
  15 *  machine) this file will double as a 'coding guide' and a signpost
  16 *  for newbie kernel hackers. It features several pointers to major
  17 *  kernel subsystems and hints as to where to find out what things do.
  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/* Serializes oom_score_adj and oom_score_adj_min updates */
  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 * oom_cpuset_eligible() - check task eligiblity for kill
  69 * @start: task struct of which task to consider
  70 * @oc: pointer to struct oom_control
  71 *
  72 * Task eligibility is determined by whether or not a candidate task, @tsk,
  73 * shares the same mempolicy nodes as current if it is bound by such a policy
  74 * and whether or not it has the same set of allowed cpuset nodes.
  75 *
  76 * This function is assuming oom-killer context and 'current' has triggered
  77 * the oom-killer.
  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                         * If this is a mempolicy constrained oom, tsk's
  94                         * cpuset is irrelevant.  Only return true if its
  95                         * mempolicy intersects current, otherwise it may be
  96                         * needlessly killed.
  97                         */
  98                        ret = mempolicy_nodemask_intersects(tsk, mask);
  99                } else {
 100                        /*
 101                         * This is not a mempolicy constrained oom, so only
 102                         * check the mems of tsk's cpuset.
 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 /* CONFIG_NUMA */
 119
 120/*
 121 * The process p may have detached its own ->mm while exiting or through
 122 * kthread_use_mm(), but one or more of its subthreads may still have a valid
 123 * pointer.  Return p, or any of its subthreads with a valid ->mm, with
 124 * task_lock() held.
 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 * order == -1 means the oom kill is required by sysrq, otherwise only
 147 * for display purposes.
 148 */
 149static inline bool is_sysrq_oom(struct oom_control *oc)
 150{
 151        return oc->order == -1;
 152}
 153
 154/* return true if the task is not adequate as candidate victim task. */
 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 * Print out unreclaimble slabs info when unreclaimable slabs amount is greater
 166 * than all user memory (LRU pages)
 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 * oom_badness - heuristic function to determine which candidate task to kill
 185 * @p: task struct of which task we should calculate
 186 * @totalpages: total present RAM allowed for page allocation
 187 *
 188 * The heuristic for determining which task to kill is made to be as simple and
 189 * predictable as possible.  The goal is to return the highest value for the
 190 * task consuming the most memory to avoid subsequent oom failures.
 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         * Do not even consider tasks which are explicitly marked oom
 206         * unkillable or have been already oom reaped or the are in
 207         * the middle of vfork
 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         * The baseline for the badness score is the proportion of RAM that each
 219         * task's rss, pagetable and swap space use.
 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        /* Normalize to oom_score_adj units */
 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 * Determine the type of allocation constraint.
 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        /* Default to all available memory */
 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         * Reach here only when __GFP_NOFAIL is used. So, we should avoid
 265         * to kill current.We have to random task kill in this case.
 266         * Hopefully, CONSTRAINT_THISNODE...but no way to handle it, now.
 267         */
 268        if (oc->gfp_mask & __GFP_THISNODE)
 269                return CONSTRAINT_NONE;
 270
 271        /*
 272         * This is not a __GFP_THISNODE allocation, so a truncated nodemask in
 273         * the page allocator means a mempolicy is in effect.  Cpuset policy
 274         * is enforced in get_page_from_freelist().
 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        /* Check this allocation failure is caused by cpuset's wall function */
 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        /* p may not have freeable memory in nodemask */
 308        if (!is_memcg_oom(oc) && !oom_cpuset_eligible(task, oc))
 309                goto next;
 310
 311        /*
 312         * This task already has access to memory reserves and is being killed.
 313         * Don't allow any other task to have access to the reserves unless
 314         * the task has MMF_OOM_SKIP because chances that it would release
 315         * any memory is quite low.
 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         * If task is allocating a lot of memory and has been marked to be
 325         * killed first if it triggers an oom, then select it.
 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 * Simple selection loop. We choose the process with the highest number of
 353 * 'points'. In case scan was aborted, oc->chosen is set to -1.
 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        /* p may not have freeable memory in nodemask */
 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                 * This is a kthread or all of p's threads have already
 388                 * detached their mm's.  There's no need to report
 389                 * them; they can't be oom killed anyway.
 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 * dump_tasks - dump current memory state of all system tasks
 407 * @oc: pointer to struct oom_control
 408 *
 409 * Dumps the current memory state of all eligible tasks.  Tasks not in the same
 410 * memcg, not in the same cpuset, or bound to a disjoint set of mempolicy nodes
 411 * are not shown.
 412 * State information includes task's pid, uid, tgid, vm size, rss,
 413 * pgtables_bytes, swapents, oom_score_adj value, and name.
 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        /* one line summary of the oom killer context. */
 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 * Number of OOM victims in flight
 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 * task->mm can be NULL if the task is the exited group leader.  So to
 478 * determine whether the task is using a particular mm, we examine all the
 479 * task's threads: if one of those is using this mm then this task was also
 480 * using it.
 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 * OOM Reaper kernel thread which tries to reap the memory used by the OOM
 497 * victim (if that is possible) to help the OOM killer to move on.
 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         * Tell all users of get_user/copy_from_user etc... that the content
 510         * is no longer stable. No barriers really needed because unmapping
 511         * should imply barriers already and the reader would hit a page fault
 512         * if it stumbled over a reaped memory.
 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                 * Only anonymous pages have a good chance to be dropped
 522                 * without additional steps which we cannot afford as we
 523                 * are OOM already.
 524                 *
 525                 * We do not even care about fs backed pages because all
 526                 * which are reclaimable have already been reclaimed and
 527                 * we do not want to block exit_mmap by keeping mm ref
 528                 * count elevated without a good reason.
 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         * If the mm has invalidate_{start,end}() notifiers that could block,
 555         * sleep to give the oom victim some more time.
 556         * TODO: we really want to get rid of this ugly hack and make sure that
 557         * notifiers cannot block for unbounded amount of time
 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         * MMF_OOM_SKIP is set by exit_mmap when the OOM reaper can't
 567         * work on the mm anymore. The check for MMF_OOM_SKIP must run
 568         * under mmap_lock for reading because it serializes against the
 569         * mmap_write_lock();mmap_write_unlock() cycle in exit_mmap().
 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        /* Retry the mmap_read_trylock(mm) a few times */
 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         * Hide this mm from OOM killer because it has been either reaped or
 616         * somebody can't call mmap_write_unlock(mm).
 617         */
 618        set_bit(MMF_OOM_SKIP, &mm->flags);
 619
 620        /* Drop a reference taken by wake_oom_reaper */
 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        /* mm is already queued? */
 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 /* CONFIG_MMU */
 671
 672/**
 673 * mark_oom_victim - mark the given task as OOM victim
 674 * @tsk: task to mark
 675 *
 676 * Has to be called with oom_lock held and never after
 677 * oom has been disabled already.
 678 *
 679 * tsk->mm has to be non NULL and caller has to guarantee it is stable (either
 680 * under task_lock or operate on the current).
 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        /* OOM killer might race with memcg OOM */
 688        if (test_and_set_tsk_thread_flag(tsk, TIF_MEMDIE))
 689                return;
 690
 691        /* oom_mm is bound to the signal struct life time. */
 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         * Make sure that the task is woken up from uninterruptible sleep
 699         * if it is frozen because OOM killer wouldn't be able to free
 700         * any memory and livelock. freezing_slow_path will tell the freezer
 701         * that TIF_MEMDIE tasks should be ignored.
 702         */
 703        __thaw_task(tsk);
 704        atomic_inc(&oom_victims);
 705        trace_mark_victim(tsk->pid);
 706}
 707
 708/**
 709 * exit_oom_victim - note the exit of an OOM victim
 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 * oom_killer_enable - enable OOM killer
 721 */
 722void oom_killer_enable(void)
 723{
 724        oom_killer_disabled = false;
 725        pr_info("OOM killer enabled.\n");
 726}
 727
 728/**
 729 * oom_killer_disable - disable OOM killer
 730 * @timeout: maximum timeout to wait for oom victims in jiffies
 731 *
 732 * Forces all page allocations to fail rather than trigger OOM killer.
 733 * Will block and wait until all OOM victims are killed or the given
 734 * timeout expires.
 735 *
 736 * The function cannot be called when there are runnable user tasks because
 737 * the userspace would see unexpected allocation failures as a result. Any
 738 * new usage of this function should be consulted with MM people.
 739 *
 740 * Returns true if successful and false if the OOM killer cannot be
 741 * disabled.
 742 */
 743bool oom_killer_disable(signed long timeout)
 744{
 745        signed long ret;
 746
 747        /*
 748         * Make sure to not race with an ongoing OOM killer. Check that the
 749         * current is not killed (possibly due to sharing the victim's memory).
 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         * A coredumping process may sleep for an extended period in exit_mm(),
 773         * so the oom killer cannot assume that the process will promptly exit
 774         * and release memory.
 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 * Checks whether the given task is dying or exiting and likely to
 790 * release its address space. This means that all threads and processes
 791 * sharing the same mm have to be killed or exiting.
 792 * Caller has to make sure that task->mm is stable (hold task_lock or
 793 * it operates on the current).
 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         * Skip tasks without mm because it might have passed its exit_mm and
 803         * exit_oom_victim. oom_reaper could have rescued that but do not rely
 804         * on that for now. We can consider find_lock_task_mm in future.
 805         */
 806        if (!mm)
 807                return false;
 808
 809        if (!__task_will_free_mem(task))
 810                return false;
 811
 812        /*
 813         * This task has already been drained by the oom reaper so there are
 814         * only small chances it will free some more
 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         * Make sure that all tasks which share the mm with the given tasks
 824         * are dying as well to make sure that a) nobody pins its mm and
 825         * b) the task is also reapable by the oom reaper.
 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        /* Get a reference to safely compare mm after task_unlock(victim) */
 861        mm = victim->mm;
 862        mmgrab(mm);
 863
 864        /* Raise event before sending signal: task reaper must see this */
 865        count_vm_event(OOM_KILL);
 866        memcg_memory_event_mm(mm, MEMCG_OOM_KILL);
 867
 868        /*
 869         * We should send SIGKILL before granting access to memory reserves
 870         * in order to prevent the OOM victim from depleting the memory
 871         * reserves from the user space under its control.
 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         * Kill all user processes sharing victim->mm in other thread groups, if
 886         * any.  They don't get access to memory reserves, though, to avoid
 887         * depletion of all memory.  This prevents mm->mmap_lock livelock when an
 888         * oom killed thread cannot exit because it requires the semaphore and
 889         * its contended by another thread trying to allocate memory itself.
 890         * That thread will now get access to memory reserves since it has a
 891         * pending fatal signal.
 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                 * No kthead_use_mm() user needs to read from the userspace so
 909                 * we are ok to reap it.
 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 * Kill provided task unless it's secured by setting
 927 * oom_score_adj to OOM_SCORE_ADJ_MIN.
 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         * If the task is already exiting, don't alarm the sysadmin or kill
 948         * its children or threads, just give it access to memory reserves
 949         * so it can die quickly
 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         * Do we need to kill the entire memory cgroup?
 966         * Or even one of the ancestor memory cgroups?
 967         * Check this out before killing the victim task.
 968         */
 969        oom_group = mem_cgroup_get_oom_group(victim, oc->memcg);
 970
 971        __oom_kill_process(victim, message);
 972
 973        /*
 974         * If necessary, kill all tasks in the selected memory cgroup.
 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 * Determines whether the kernel must panic because of the panic_on_oom sysctl.
 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                 * panic_on_oom == 1 only affects CONSTRAINT_NONE, the kernel
 994                 * does not panic for cpuset, mempolicy, or memcg allocation
 995                 * failures.
 996                 */
 997                if (oc->constraint != CONSTRAINT_NONE)
 998                        return;
 999        }
1000        /* Do not panic for oom kills triggered by sysrq */
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 * out_of_memory - kill the "best" process when we run out of memory
1024 * @oc: pointer to struct oom_control
1025 *
1026 * If we run out of memory, we have the choice between either
1027 * killing a random task (bad), letting the system crash (worse)
1028 * OR try to be smart about which process to kill. Note that we
1029 * don't have to be perfect here, we just have to be good.
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                        /* Got some memory back in the last second. */
1042                        return true;
1043        }
1044
1045        /*
1046         * If current has a pending SIGKILL or is exiting, then automatically
1047         * select it.  The goal is to allow it to allocate so that it may
1048         * quickly exit and free its memory.
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         * The OOM killer does not compensate for IO-less reclaim.
1058         * pagefault_out_of_memory lost its gfp context so we have to
1059         * make sure exclude 0 mask - all other users should have at least
1060         * ___GFP_DIRECT_RECLAIM to get here. But mem_cgroup_oom() has to
1061         * invoke the OOM killer even if it is a GFP_NOFS allocation.
1062         */
1063        if (oc->gfp_mask && !(oc->gfp_mask & __GFP_FS) && !is_memcg_oom(oc))
1064                return true;
1065
1066        /*
1067         * Check if there were limitations on the allocation (only relevant for
1068         * NUMA and memcg) that may require different handling.
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        /* Found nothing?!?! */
1087        if (!oc->chosen) {
1088                dump_header(oc, NULL);
1089                pr_warn("Out of memory and no killable processes...\n");
1090                /*
1091                 * If we got here due to an actual allocation at the
1092                 * system level, we cannot survive this and will enter
1093                 * an endless loop in the allocator. Bail out now.
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                 * Give the killed process a good chance to exit before trying
1103                 * to allocate memory again.
1104                 */
1105                schedule_timeout_killable(1);
1106        }
1107        return !!oc->chosen;
1108}
1109
1110/*
1111 * The pagefault handler calls here because it is out of memory, so kill a
1112 * memory-hogging task. If oom_lock is held by somebody else, a parallel oom
1113 * killing is already in progress so do nothing.
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