Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
[firefly-linux-kernel-4.4.55.git] / mm / oom_kill.c
index 1ecc0bcaecc518458765347f8b0fa5d5eed46f75..e4778285d8d13c6700da7697d42c658624dc2b64 100644 (file)
@@ -377,13 +377,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 +474,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 +525,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 +539,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 +566,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 +592,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