From: Michal Hocko <mho...@suse.com>

oom_kill_process makes sure to kill all processes outside of the thread
group which are sharing the mm. This requires to iterate over all tasks.
This is however not a common case so we can optimize it a bit and only
do that path only if we know that there are external users of the mm
struct outside of the thread group.

Signed-off-by: Michal Hocko <mho...@suse.com>
---
 mm/oom_kill.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 5bb2f7698ad7..0e33e912f7e4 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -820,6 +820,13 @@ void oom_kill_process(struct oom_control *oc, struct 
task_struct *p,
        task_unlock(victim);
 
        /*
+        * skip expensive iterations over all tasks if we know that there
+        * are no users outside of threads in the same thread group
+        */
+       if (atomic_read(&mm->mm_users) <= get_nr_threads(victim))
+               goto oom_reap;
+
+       /*
         * Kill all user processes sharing victim->mm in other thread groups, if
         * any.  They don't get access to memory reserves, though, to avoid
         * depletion of all memory.  This prevents mm->mmap_sem livelock when an
@@ -848,6 +855,7 @@ void oom_kill_process(struct oom_control *oc, struct 
task_struct *p,
        }
        rcu_read_unlock();
 
+oom_reap:
        if (can_oom_reap)
                wake_oom_reaper(victim);
 
-- 
2.8.1

Reply via email to