]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - mm/oom_kill.c
fs/ncpfs/dir.c: remove unnecessary new_valid_dev() check
[karo-tx-linux.git] / mm / oom_kill.c
index 1ecc0bcaecc518458765347f8b0fa5d5eed46f75..d13a33918fa23e685bc629bc23f58d97729beb62 100644 (file)
@@ -118,6 +118,15 @@ found:
        return t;
 }
 
+/*
+ * order == -1 means the oom kill is required by sysrq, otherwise only
+ * for display purposes.
+ */
+static inline bool is_sysrq_oom(struct oom_control *oc)
+{
+       return oc->order == -1;
+}
+
 /* return true if the task is not adequate as candidate victim task. */
 static bool oom_unkillable_task(struct task_struct *p,
                struct mem_cgroup *memcg, const nodemask_t *nodemask)
@@ -265,7 +274,7 @@ enum oom_scan_t oom_scan_process_thread(struct oom_control *oc,
         * Don't allow any other task to have access to the reserves.
         */
        if (test_tsk_thread_flag(task, TIF_MEMDIE)) {
-               if (oc->order != -1)
+               if (!is_sysrq_oom(oc))
                        return OOM_SCAN_ABORT;
        }
        if (!task->mm)
@@ -278,7 +287,7 @@ enum oom_scan_t oom_scan_process_thread(struct oom_control *oc,
        if (oom_task_origin(task))
                return OOM_SCAN_SELECT;
 
-       if (task_will_free_mem(task) && oc->order != -1)
+       if (task_will_free_mem(task) && !is_sysrq_oom(oc))
                return OOM_SCAN_ABORT;
 
        return OOM_SCAN_OK;
@@ -377,13 +386,11 @@ static void dump_tasks(struct mem_cgroup *memcg, const nodemask_t *nodemask)
 static void dump_header(struct oom_control *oc, struct task_struct *p,
                        struct mem_cgroup *memcg)
 {
-       task_lock(current);
        pr_warning("%s invoked oom-killer: gfp_mask=0x%x, order=%d, "
                "oom_score_adj=%hd\n",
                current->comm, oc->gfp_mask, oc->order,
                current->signal->oom_score_adj);
-       cpuset_print_task_mems_allowed(current);
-       task_unlock(current);
+       cpuset_print_current_mems_allowed();
        dump_stack();
        if (memcg)
                mem_cgroup_print_oom_info(memcg, p);
@@ -476,6 +483,24 @@ void oom_killer_enable(void)
        oom_killer_disabled = false;
 }
 
+/*
+ * task->mm can be NULL if the task is the exited group leader.  So to
+ * determine whether the task is using a particular mm, we examine all the
+ * task's threads: if one of those is using this mm then this task was also
+ * using it.
+ */
+static bool process_shares_mm(struct task_struct *p, struct mm_struct *mm)
+{
+       struct task_struct *t;
+
+       for_each_thread(p, t) {
+               struct mm_struct *t_mm = READ_ONCE(t->mm);
+               if (t_mm)
+                       return t_mm == mm;
+       }
+       return false;
+}
+
 #define K(x) ((x) << (PAGE_SHIFT-10))
 /*
  * Must be called while holding a reference to p, which will be released upon
@@ -509,10 +534,8 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
        if (__ratelimit(&oom_rs))
                dump_header(oc, p, memcg);
 
-       task_lock(p);
        pr_err("%s: Kill process %d (%s) score %u or sacrifice child\n",
                message, task_pid_nr(p), p->comm, points);
-       task_unlock(p);
 
        /*
         * If any of p's children has a different mm and is eligible for kill,
@@ -525,7 +548,7 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
                list_for_each_entry(child, &t->children, sibling) {
                        unsigned int child_points;
 
-                       if (child->mm == p->mm)
+                       if (process_shares_mm(child, p->mm))
                                continue;
                        /*
                         * oom_badness() returns 0 if the thread is unkillable
@@ -552,8 +575,15 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
                victim = p;
        }
 
-       /* mm cannot safely be dereferenced after task_unlock(victim) */
+       /* Get a reference to safely compare mm after task_unlock(victim) */
        mm = victim->mm;
+       atomic_inc(&mm->mm_count);
+       /*
+        * We should send SIGKILL before setting TIF_MEMDIE in order to prevent
+        * the OOM victim from depleting the memory reserves from the user
+        * space under its control.
+        */
+       do_send_sig_info(SIGKILL, SEND_SIG_FORCED, victim, true);
        mark_oom_victim(victim);
        pr_err("Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB\n",
                task_pid_nr(victim), victim->comm, K(victim->mm->total_vm),
@@ -571,21 +601,21 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
         * pending fatal signal.
         */
        rcu_read_lock();
-       for_each_process(p)
-               if (p->mm == mm && !same_thread_group(p, victim) &&
-                   !(p->flags & PF_KTHREAD)) {
-                       if (p->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
-                               continue;
+       for_each_process(p) {
+               if (!process_shares_mm(p, mm))
+                       continue;
+               if (same_thread_group(p, victim))
+                       continue;
+               if (unlikely(p->flags & PF_KTHREAD))
+                       continue;
+               if (p->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
+                       continue;
 
-                       task_lock(p);   /* Protect ->comm from prctl() */
-                       pr_err("Kill process %d (%s) sharing same memory\n",
-                               task_pid_nr(p), p->comm);
-                       task_unlock(p);
-                       do_send_sig_info(SIGKILL, SEND_SIG_FORCED, p, true);
-               }
+               do_send_sig_info(SIGKILL, SEND_SIG_FORCED, p, true);
+       }
        rcu_read_unlock();
 
-       do_send_sig_info(SIGKILL, SEND_SIG_FORCED, victim, true);
+       mmdrop(mm);
        put_task_struct(victim);
 }
 #undef K
@@ -608,7 +638,7 @@ void check_panic_on_oom(struct oom_control *oc, enum oom_constraint constraint,
                        return;
        }
        /* Do not panic for oom kills triggered by sysrq */
-       if (oc->order == -1)
+       if (is_sysrq_oom(oc))
                return;
        dump_header(oc, NULL, memcg);
        panic("Out of memory: %s panic_on_oom is enabled\n",
@@ -688,7 +718,7 @@ bool out_of_memory(struct oom_control *oc)
 
        p = select_bad_process(oc, &points, totalpages);
        /* Found nothing?!?! Either we hang forever, or we panic. */
-       if (!p && oc->order != -1) {
+       if (!p && !is_sysrq_oom(oc)) {
                dump_header(oc, NULL, NULL);
                panic("Out of memory and no killable processes...\n");
        }