oom: oom_kill_process() needs to check that p is unkillable
[sfrench/cifs-2.6.git] / mm / oom_kill.c
index cba18c06e50853884a7ff1eeb4ccc8619730f21c..3999747aef48bb390a6ea3c8d7ff66ac550e8993 100644 (file)
@@ -101,6 +101,26 @@ static struct task_struct *find_lock_task_mm(struct task_struct *p)
        return NULL;
 }
 
+/* return true if the task is not adequate as candidate victim task. */
+static bool oom_unkillable_task(struct task_struct *p, struct mem_cgroup *mem,
+                          const nodemask_t *nodemask)
+{
+       if (is_global_init(p))
+               return true;
+       if (p->flags & PF_KTHREAD)
+               return true;
+
+       /* When mem_cgroup_out_of_memory() and p is not member of the group */
+       if (mem && !task_in_mem_cgroup(p, mem))
+               return true;
+
+       /* p may not have freeable memory in nodemask */
+       if (!has_intersects_mems_allowed(p, nodemask))
+               return true;
+
+       return false;
+}
+
 /**
  * badness - calculate a numeric value for how bad this task has been
  * @p: task struct of which task we should calculate
@@ -295,12 +315,7 @@ static struct task_struct *select_bad_process(unsigned long *ppoints,
        for_each_process(p) {
                unsigned long points;
 
-               /* skip the init task and kthreads */
-               if (is_global_init(p) || (p->flags & PF_KTHREAD))
-                       continue;
-               if (mem && !task_in_mem_cgroup(p, mem))
-                       continue;
-               if (!has_intersects_mems_allowed(p, nodemask))
+               if (oom_unkillable_task(p, mem, nodemask))
                        continue;
 
                /*
@@ -429,7 +444,7 @@ static int oom_kill_task(struct task_struct *p)
 
 static int oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
                            unsigned long points, struct mem_cgroup *mem,
-                           const char *message)
+                           nodemask_t *nodemask, const char *message)
 {
        struct task_struct *victim = p;
        struct task_struct *child;
@@ -467,7 +482,7 @@ static int oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
 
                        if (child->mm == p->mm)
                                continue;
-                       if (mem && !task_in_mem_cgroup(child, mem))
+                       if (oom_unkillable_task(p, mem, nodemask))
                                continue;
 
                        /* badness() returns 0 if the thread is unkillable */
@@ -519,7 +534,7 @@ retry:
        if (!p || PTR_ERR(p) == -1UL)
                goto out;
 
-       if (oom_kill_process(p, gfp_mask, 0, points, mem,
+       if (oom_kill_process(p, gfp_mask, 0, points, mem, NULL,
                                "Memory cgroup out of memory"))
                goto retry;
 out:
@@ -628,41 +643,6 @@ static void clear_system_oom(void)
        spin_unlock(&zone_scan_lock);
 }
 
-
-/*
- * Must be called with tasklist_lock held for read.
- */
-static void __out_of_memory(gfp_t gfp_mask, int order, const nodemask_t *mask)
-{
-       struct task_struct *p;
-       unsigned long points;
-
-       if (sysctl_oom_kill_allocating_task)
-               if (!oom_kill_process(current, gfp_mask, order, 0, NULL,
-                               "Out of memory (oom_kill_allocating_task)"))
-                       return;
-retry:
-       /*
-        * Rambo mode: Shoot down a process and hope it solves whatever
-        * issues we may have.
-        */
-       p = select_bad_process(&points, NULL, mask);
-
-       if (PTR_ERR(p) == -1UL)
-               return;
-
-       /* Found nothing?!?! Either we hang forever, or we panic. */
-       if (!p) {
-               dump_header(NULL, gfp_mask, order, NULL);
-               read_unlock(&tasklist_lock);
-               panic("Out of memory and no killable processes...\n");
-       }
-
-       if (oom_kill_process(p, gfp_mask, order, points, NULL,
-                            "Out of memory"))
-               goto retry;
-}
-
 /**
  * out_of_memory - kill the "best" process when we run out of memory
  * @zonelist: zonelist pointer
@@ -678,7 +658,9 @@ retry:
 void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask,
                int order, nodemask_t *nodemask)
 {
+       struct task_struct *p;
        unsigned long freed = 0;
+       unsigned long points;
        enum oom_constraint constraint = CONSTRAINT_NONE;
 
        blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
@@ -703,10 +685,38 @@ void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask,
        if (zonelist)
                constraint = constrained_alloc(zonelist, gfp_mask, nodemask);
        check_panic_on_oom(constraint, gfp_mask, order);
+
        read_lock(&tasklist_lock);
-       __out_of_memory(gfp_mask, order,
+       if (sysctl_oom_kill_allocating_task &&
+           !oom_unkillable_task(current, NULL, nodemask)) {
+               /*
+                * oom_kill_process() needs tasklist_lock held.  If it returns
+                * non-zero, current could not be killed so we must fallback to
+                * the tasklist scan.
+                */
+               if (!oom_kill_process(current, gfp_mask, order, 0, NULL,
+                               nodemask,
+                               "Out of memory (oom_kill_allocating_task)"))
+                       return;
+       }
+
+retry:
+       p = select_bad_process(&points, NULL,
                        constraint == CONSTRAINT_MEMORY_POLICY ? nodemask :
                                                                 NULL);
+       if (PTR_ERR(p) == -1UL)
+               return;
+
+       /* Found nothing?!?! Either we hang forever, or we panic. */
+       if (!p) {
+               dump_header(NULL, gfp_mask, order, NULL);
+               read_unlock(&tasklist_lock);
+               panic("Out of memory and no killable processes...\n");
+       }
+
+       if (oom_kill_process(p, gfp_mask, order, points, NULL, nodemask,
+                            "Out of memory"))
+               goto retry;
        read_unlock(&tasklist_lock);
 
        /*