[PATCH 1/6] mm, oom: do not loop over all tasks if there are no external tasks sharing mm

From: Michal Hocko
Date: Thu May 26 2016 - 08:41:47 EST

From: Michal Hocko <mhocko@xxxxxxxx>

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 <mhocko@xxxxxxxx>
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,

+ * 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,

if (can_oom_reap)