Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux
[firefly-linux-kernel-4.4.55.git] / mm / oom_kill.c
index 0d8ad1ebd1d1b6164bd8088d982b6ef6da5a653d..37b1b1903fb2c6aa84d9e25faf7e53381121e647 100644 (file)
@@ -102,14 +102,19 @@ struct task_struct *find_lock_task_mm(struct task_struct *p)
 {
        struct task_struct *t;
 
+       rcu_read_lock();
+
        for_each_thread(p, t) {
                task_lock(t);
                if (likely(t->mm))
-                       return t;
+                       goto found;
                task_unlock(t);
        }
+       t = NULL;
+found:
+       rcu_read_unlock();
 
-       return NULL;
+       return t;
 }
 
 /* return true if the task is not adequate as candidate victim task. */
@@ -322,10 +327,14 @@ static struct task_struct *select_bad_process(unsigned int *ppoints,
                        break;
                };
                points = oom_badness(p, NULL, nodemask, totalpages);
-               if (points > chosen_points) {
-                       chosen = p;
-                       chosen_points = points;
-               }
+               if (!points || points < chosen_points)
+                       continue;
+               /* Prefer thread group leaders for display purposes */
+               if (points == chosen_points && thread_group_leader(chosen))
+                       continue;
+
+               chosen = p;
+               chosen_points = points;
        }
        if (chosen)
                get_task_struct(chosen);
@@ -461,10 +470,8 @@ void oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
        }
        read_unlock(&tasklist_lock);
 
-       rcu_read_lock();
        p = find_lock_task_mm(victim);
        if (!p) {
-               rcu_read_unlock();
                put_task_struct(victim);
                return;
        } else if (victim != p) {
@@ -490,6 +497,7 @@ void oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
         * That thread will now get access to memory reserves since it has a
         * pending fatal signal.
         */
+       rcu_read_lock();
        for_each_process(p)
                if (p->mm == mm && !same_thread_group(p, victim) &&
                    !(p->flags & PF_KTHREAD)) {