Skip to contentSkip to conversation

Handle oom bypass more gracefully

PATCH In review Michal Hocko 3 participants 41 messages May 26, 2016 – May 30, 2016

Subsystems (5)
lore
Share & tools
lore

Michal Hocko

author maintainer
#0 lore raw

cover Handle oom bypass more gracefully

Hi,
the following 6 patches should put some order to very rare cases of mm shared between processes and make the paths which bypass the oom killer oom reapable and so much more reliable finally. Even though mm shared outside of threadgroup is rare (either use_mm by kernel threads or exotic clone(CLONE_VM) without CLONE_THREAD resp. CLONE_SIGHAND) it makes the current oom killer logic quite hard to follow and evaluate. It is possible to select an oom victim which shares the mm with unkillable process or bypass the oom killer even when other processes sharing the mm are still alive and other weird cases.

Patch 1 optimizes oom_kill_task to skip the costly process iteration when the current oom victim is not sharing mm with other processes. Patch 2 is a clean up of oom_score_adj handling and a preparatory work. Patch 3 enforces oom_adj_score to be consistent between processes sharing the mm to behave consistently with the regular thread groups. Patch 4 tries to handle vforked tasks better in the oom path, patch 5 ensures that all tasks sharing the mm are killed and finally patch 6 should guarantee that task_will_free_mem will always imply reapable bypass of the oom killer.

The patchset is based on the current mmotm tree (mmotm-2016-05-23-16-51). I would really appreciate a deep review as this area is full of land mines but I hope I've made the code much cleaner with less kludges.

I am CCing Oleg (sorry I know you hate this code) but I would feel much better if you double checked my assumptions about locking and vfork behavior.

Michal Hocko (6):
mm, oom: do not loop over all tasks if there are no external tasks sharing mm proc, oom_adj: extract oom_score_adj setting into a helper
mm, oom_adj: make sure processes sharing mm have same view of oom_score_adj mm, oom: skip over vforked tasks
mm, oom: kill all tasks sharing the mm
mm, oom: fortify task_will_free_mem

 fs/proc/base.c      | 168 +++++++++++++++++++++++++++++-----------------------
 include/linux/mm.h  |   2 +
 include/linux/oom.h |  72 ++++++++++++++++++++--
 mm/memcontrol.c     |   4 +-
 mm/oom_kill.c       |  96 ++++++++++--------------------
 5 files changed, 196 insertions(+), 146 deletions(-)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#1 lore raw

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

From: Michal Hocko <[email protected]>

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.

---
 mm/oom_kill.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/oom_kill.c +8 -0
index 5bb2f7698ad7..0e33e912f7e4 100644
@@ -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);
 
Signature · 7 lines
2.8.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw

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

mm/oom_kill.c void oom_kill_process(struct oom_control *oc, struct task_struct *p, · line 827

Quoted 13 lines · Michal Hocko
mm/oom_kill.c +5 -0
index 5bb2f7698ad7..0e33e912f7e4 100644
@@ -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;

Is this really safe? Isn't it possible that victim thread's thread group has more than atomic_read(&mm->mm_users) threads which are past exit_mm() and blocked at exit_task_work() which are before __exit_signal() from release_task() from exit_notify()?

Quoted 5 lines
mm/oom_kill.c +2 -0
+
+	/*
 	 * 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
author maintainer
#3 lore raw

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

Quoted 8 lines · Tetsuo Handa
Quoted 7 lines · Michal Hocko
mm/oom_kill.c +0 -0
index 5bb2f7698ad7..0e33e912f7e4 100644
@@ -820,6 +820,13 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
 	task_unlock(victim);
 
Quoted 11 lines

mm/oom_kill.c void oom_kill_process(struct oom_control *oc, struct task_struct *p, · line 827

Quoted 6 lines
mm/oom_kill.c +5 -0
 	/*
+	 * 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;

Is this really safe? Isn't it possible that victim thread's thread group has more than atomic_read(&mm->mm_users) threads which are past exit_mm() and blocked at exit_task_work() which are before __exit_signal() from release_task() from exit_notify()?

You are right. The race window between exit_mm and __exit_signal is really large. I thought about == check instead but that wouldn't work for the same reason, dang, it looked so promissing.

Scratch this patch then.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#4 lore raw

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

Quoted 21 lines · Michal Hocko
Quoted 8 lines · Tetsuo Handa
Quoted 7 lines · Michal Hocko
mm/oom_kill.c +0 -0
index 5bb2f7698ad7..0e33e912f7e4 100644
@@ -820,6 +820,13 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
 	task_unlock(victim);
 
Quoted 11 lines

mm/oom_kill.c void oom_kill_process(struct oom_control *oc, struct task_struct *p, · line 827

Quoted 6 lines
mm/oom_kill.c +5 -0
 	/*
+	 * 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;

Is this really safe? Isn't it possible that victim thread's thread group has more than atomic_read(&mm->mm_users) threads which are past exit_mm() and blocked at exit_task_work() which are before __exit_signal() from release_task() from exit_notify()?

Quoted 6 lines

You are right. The race window between exit_mm and __exit_signal is really large. I thought about == check instead but that wouldn't work for the same reason, dang, it looked so promissing.

Scratch this patch then.

I think that remembering whether this mm might be shared between multiple thread groups at clone() time (i.e. whether clone(CLONE_VM without CLONE_SIGHAND) was ever requested on this mm) is safe (given that that thread already got SIGKILL or is exiting).

By the way, in oom_kill_process(), how (p->flags & PF_KTHREAD) case can become true when process_shares_mm() is true? Even if it can become true, why can't we reap that mm? Is (p->flags & PF_KTHREAD) case only for not to send SIGKILL rather than not to reap that mm?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#5 lore raw

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

Quoted 26 lines · Tetsuo Handa
Quoted 25 lines · Michal Hocko
Quoted 8 lines · Tetsuo Handa
Quoted 7 lines · Michal Hocko
mm/oom_kill.c +0 -0
index 5bb2f7698ad7..0e33e912f7e4 100644
@@ -820,6 +820,13 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
 	task_unlock(victim);
 
Quoted 11 lines

mm/oom_kill.c void oom_kill_process(struct oom_control *oc, struct task_struct *p, · line 827

Quoted 6 lines
mm/oom_kill.c +5 -0
 	/*
+	 * 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;

Is this really safe? Isn't it possible that victim thread's thread group has more than atomic_read(&mm->mm_users) threads which are past exit_mm() and blocked at exit_task_work() which are before __exit_signal() from release_task() from exit_notify()?

You are right. The race window between exit_mm and __exit_signal is really large. I thought about == check instead but that wouldn't work for the same reason, dang, it looked so promissing.

Quoted 7 lines
Quoted 2 lines

Scratch this patch then.

I think that remembering whether this mm might be shared between multiple thread groups at clone() time (i.e. whether clone(CLONE_VM without CLONE_SIGHAND) was ever requested on this mm) is safe (given that that thread already got SIGKILL or is exiting).

I was already playing with that idea but I didn't want to add anything to the fork path which is really hot. This patch is not really needed for the rest. It just felt like a nice optimization. I do not think it is worth deeper changes in the fast paths.

Quoted 2 lines

By the way, in oom_kill_process(), how (p->flags & PF_KTHREAD) case can become true when process_shares_mm() is true?

not sure I understand. But the PF_KTHREAD check is there to catch use_mm() usage by kernel threads.

Quoted 3 lines

Even if it can become true,
why can't we reap that mm? Is (p->flags & PF_KTHREAD) case only for not to send SIGKILL rather than not to reap that mm?

If we reaped the mm then the kernel thread could blow up when accessing a memory.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#6 lore raw

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

Quoted 10 lines · Michal Hocko
Quoted 4 lines · Tetsuo Handa

I think that remembering whether this mm might be shared between multiple thread groups at clone() time (i.e. whether clone(CLONE_VM without CLONE_SIGHAND) was ever requested on this mm) is safe (given that that thread already got SIGKILL or is exiting).

I was already playing with that idea but I didn't want to add anything to the fork path which is really hot. This patch is not really needed for the rest. It just felt like a nice optimization. I do not think it is worth deeper changes in the fast paths.

"[PATCH 6/6] mm, oom: fortify task_will_free_mem" depends on [PATCH 1/6]. You will need to update [PATCH 6/6].

It seems to me that [PATCH 6/6] resembles
http://lkml.kernel.org/r/[email protected] . I think we will be happy if we can speed up mm_is_reapable() test using "whether this mm might be shared between multiple thread groups" flag. I don't think updating such flag at clone() is too heavy operation to add.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#7 lore raw

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

Quoted 12 lines · Tetsuo Handa
Quoted 10 lines · Michal Hocko
Quoted 4 lines · Tetsuo Handa

I think that remembering whether this mm might be shared between multiple thread groups at clone() time (i.e. whether clone(CLONE_VM without CLONE_SIGHAND) was ever requested on this mm) is safe (given that that thread already got SIGKILL or is exiting).

I was already playing with that idea but I didn't want to add anything to the fork path which is really hot. This patch is not really needed for the rest. It just felt like a nice optimization. I do not think it is worth deeper changes in the fast paths.

Quoted 8 lines

"[PATCH 6/6] mm, oom: fortify task_will_free_mem" depends on [PATCH 1/6]. You will need to update [PATCH 6/6].

It seems to me that [PATCH 6/6] resembles
http://lkml.kernel.org/r/[email protected] . I think we will be happy if we can speed up mm_is_reapable() test using "whether this mm might be shared between multiple thread groups" flag. I don't think updating such flag at clone() is too heavy operation to add.

It is still an operation which is not needed for 99% of situations. So if we do not need it for correctness then I do not think this is worth bothering.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author maintainer
#8 lore raw

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

On Fri 27-05-16 08:45:10, Michal Hocko wrote:
[...]

Quoted 3 lines

It is still an operation which is not needed for 99% of situations. So if we do not need it for correctness then I do not think this is worth bothering.

Since you have pointed out exit_mm vs. __exit_signal race yesterday I was thinking how to make the check reliable. Even atomic_read(mm->mm_users) > get_nr_threads() is not reliable and we can miss other tasks just because the current thread group is mostly past exit_mm. So far I couldn't find a way to tweak this around though. I will think about it more but I am afraid that a flag would be really needed afterall.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author maintainer
#9 lore raw

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

Quoted 11 lines · Michal Hocko

On Fri 27-05-16 08:45:10, Michal Hocko wrote:
[...]

Quoted 3 lines

It is still an operation which is not needed for 99% of situations. So if we do not need it for correctness then I do not think this is worth bothering.

Since you have pointed out exit_mm vs. __exit_signal race yesterday I was thinking how to make the check reliable. Even atomic_read(mm->mm_users) > get_nr_threads() is not reliable and we can miss other tasks just because the current thread group is mostly past exit_mm. So far I couldn't find a way to tweak this around though.

Just for the record I was playing with the following yesterday but I couldn't convince myself that this is safe and reasonable in the first place (I do not like it to be honest).
---

mm/oom_kill.c +29 -0
index 1685890d424e..db027eca8be5 100644
@@ -123,6 +123,35 @@ struct task_struct *find_lock_task_mm(struct task_struct *p)
 	return t;
 }
 
+bool task_has_external_users(struct task_struct *p)
+{
+	struct mm_struct *mm = NULL;
+	struct task_struct *t;
+	int active_threads = 0;
+	bool ret = true;	/* be pessimistic */
+
+	rcu_read_lock();
+	for_each_thread(p, t) {
+		task_lock(t);
+		if (likely(t->mm)) {
+			active_threads++;
+			if (!mm) {
+				mm = t->mm;
+				atomic_inc(&mm->mm_count);
+			}
+		}
+		task_unlock(t);
+	}
+	rcu_read_unlock();
+
+	if (mm) {
+		if (atomic_read(&mm->mm_users) <= active_threads)
+			ret = false;
+		mmdrop(mm);
+	}
+	return ret;
+}
+
 /*
  * order == -1 means the oom kill is required by sysrq, otherwise only
  * for display purposes.
Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#10 lore raw

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

Quoted 11 lines · Michal Hocko
mm/oom_kill.c +11 -0
+bool task_has_external_users(struct task_struct *p)
+{
+	struct mm_struct *mm = NULL;
+	struct task_struct *t;
+	int active_threads = 0;
+	bool ret = true;	/* be pessimistic */
+
+	rcu_read_lock();
+	for_each_thread(p, t) {
+		task_lock(t);
+		if (likely(t->mm)) {

mm/oom_kill.c struct task_struct *find_lock_task_mm(struct task_struct *p) · line 146

Quoted 10 lines
mm/oom_kill.c +10 -0
+			active_threads++;
+			if (!mm) {
+				mm = t->mm;
+				atomic_inc(&mm->mm_count);
+			}
+		}
+		task_unlock(t);
+	}
+	rcu_read_unlock();
+

I don't like this. We might sleep here long enough to change mm_users.

mm/oom_kill.c struct task_struct *find_lock_task_mm(struct task_struct *p) · line 153

Quoted 7 lines
mm/oom_kill.c +7 -0
+	if (mm) {
+		if (atomic_read(&mm->mm_users) <= active_threads)
+			ret = false;
+		mmdrop(mm);
+	}
+	return ret;
+}

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#11 lore raw

PATCH 2/6 proc, oom_adj: extract oom_score_adj setting into a helper

In reply to Michal Hocko

From: Michal Hocko <[email protected]>

Currently we have two proc interfaces to set oom_score_adj. The legacy /proc/<pid>/oom_adj and /proc/<pid>/oom_score_adj which both have their specific handlers. Big part of the logic is duplicated so extract the common code into __set_oom_adj helper. Legacy knob still expects some details slightly different so make sure those are handled same way - e.g. the legacy mode ignores oom_score_adj_min and it warns about the usage.

This patch shouldn't introduce any functional changes.

---
 fs/proc/base.c | 133 +++++++++++++++++++++++++--------------------------------
 1 file changed, 59 insertions(+), 74 deletions(-)
fs/proc/base.c +59 -74
index be73f4d0cb01..23679673bf5a 100644
@@ -1041,6 +1041,63 @@ static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
 }
 
+static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
+{
+	struct task_struct *task;
+	unsigned long flags;
+	int err = 0;
+
+	task = get_proc_task(file_inode(file));
+	if (!task) {
+		err = -ESRCH;
+		goto out;
+	}
+
+	task_lock(task);
+	if (!task->mm) {
+		err = -EINVAL;
+		goto err_task_lock;
+	}
+
+	if (!lock_task_sighand(task, &flags)) {
+		err = -ESRCH;
+		goto err_task_lock;
+	}
+
+	if (legacy) {
+		if (oom_adj < task->signal->oom_score_adj &&
+				!capable(CAP_SYS_RESOURCE)) {
+			err = -EACCES;
+			goto err_sighand;
+		}
+		/*
+		 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
+		 * /proc/pid/oom_score_adj instead.
+		 */
+		pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
+			  current->comm, task_pid_nr(current), task_pid_nr(task),
+			  task_pid_nr(task));
+	} else {
+		if ((short)oom_adj < task->signal->oom_score_adj_min &&
+				!capable(CAP_SYS_RESOURCE)) {
+			err = -EACCES;
+			goto err_sighand;
+		}
+	}
+
+	task->signal->oom_score_adj = oom_adj;
+	if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
+		task->signal->oom_score_adj_min = (short)oom_adj;
+	trace_oom_score_adj_update(task);
+err_sighand:
+	unlock_task_sighand(task, &flags);
+err_task_lock:
+	task_unlock(task);
+	put_task_struct(task);
+out:
+	return err;
+}
+
 /*
  * /proc/pid/oom_adj exists solely for backwards compatibility with previous
  * kernels.  The effective policy is defined by oom_score_adj, which has a
@@ -1054,10 +1111,8 @@ static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
 			     size_t count, loff_t *ppos)
 {
-	struct task_struct *task;
 	char buffer[PROC_NUMBUF];
 	int oom_adj;
-	unsigned long flags;
 	int err;
 
 	memset(buffer, 0, sizeof(buffer));
@@ -1077,23 +1132,6 @@ static ssize_t oom_adj_write(struct file *file, const char __user *buf,
 		goto out;
 	}
 
-	task = get_proc_task(file_inode(file));
-	if (!task) {
-		err = -ESRCH;
-		goto out;
-	}
-
-	task_lock(task);
-	if (!task->mm) {
-		err = -EINVAL;
-		goto err_task_lock;
-	}
-
-	if (!lock_task_sighand(task, &flags)) {
-		err = -ESRCH;
-		goto err_task_lock;
-	}
-
 	/*
 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
 	 * value is always attainable.
@@ -1103,27 +1141,8 @@ static ssize_t oom_adj_write(struct file *file, const char __user *buf,
 	else
 		oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
 
-	if (oom_adj < task->signal->oom_score_adj &&
-	    !capable(CAP_SYS_RESOURCE)) {
-		err = -EACCES;
-		goto err_sighand;
-	}
-
-	/*
-	 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
-	 * /proc/pid/oom_score_adj instead.
-	 */
-	pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
-		  current->comm, task_pid_nr(current), task_pid_nr(task),
-		  task_pid_nr(task));
+	err = __set_oom_adj(file, oom_adj, true);
 
-	task->signal->oom_score_adj = oom_adj;
-	trace_oom_score_adj_update(task);
-err_sighand:
-	unlock_task_sighand(task, &flags);
-err_task_lock:
-	task_unlock(task);
-	put_task_struct(task);
 out:
 	return err < 0 ? err : count;
 }
@@ -1157,9 +1176,7 @@ static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
 					size_t count, loff_t *ppos)
 {
-	struct task_struct *task;
 	char buffer[PROC_NUMBUF];
-	unsigned long flags;
 	int oom_score_adj;
 	int err;
 
@@ -1180,39 +1197,7 @@ static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
 		goto out;
 	}
 
-	task = get_proc_task(file_inode(file));
-	if (!task) {
-		err = -ESRCH;
-		goto out;
-	}
-
-	task_lock(task);
-	if (!task->mm) {
-		err = -EINVAL;
-		goto err_task_lock;
-	}
-
-	if (!lock_task_sighand(task, &flags)) {
-		err = -ESRCH;
-		goto err_task_lock;
-	}
-
-	if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
-			!capable(CAP_SYS_RESOURCE)) {
-		err = -EACCES;
-		goto err_sighand;
-	}
-
-	task->signal->oom_score_adj = (short)oom_score_adj;
-	if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
-		task->signal->oom_score_adj_min = (short)oom_score_adj;
-	trace_oom_score_adj_update(task);
-
-err_sighand:
-	unlock_task_sighand(task, &flags);
-err_task_lock:
-	task_unlock(task);
-	put_task_struct(task);
+	err = __set_oom_adj(file, oom_score_adj, false);
 out:
 	return err < 0 ? err : count;
 }
Signature · 7 lines
2.8.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author maintainer
#12 lore raw

PATCH 3/6 mm, oom_adj: make sure processes sharing mm have same view of oom_score_adj

In reply to Michal Hocko

From: Michal Hocko <[email protected]>

oom_score_adj is shared for the thread groups (via struct signal) but this is not sufficient to cover processes sharing mm (CLONE_VM without CLONE_THREAD resp. CLONE_SIGHAND) and so we can easily end up in a situation when some processes update their oom_score_adj and confuse the oom killer. In the worst case some of those processes might hide from oom killer altogether via OOM_SCORE_ADJ_MIN while others are eligible. OOM killer would then pick up those eligible but won't be allowed to kill others sharing the same mm so the mm wouldn't release the mm and so the memory.

It would be ideal to have the oom_score_adj per mm_struct becuase that is the natural entity OOM killer considers. But this will not work because some programs are doing

	vfork()
	set_oom_adj()
	exec()

We can achieve the same though. oom_score_adj write handler can set the oom_score_adj for all processes sharing the same mm if the task is not in the middle of vfork. As a result all the processes will share the same oom_score_adj.

Note that we have to serialize all the oom_score_adj writers now to guarantee they do not interleave and generate inconsistent results.

---
 fs/proc/base.c     | 35 +++++++++++++++++++++++++++++++++++
 include/linux/mm.h |  2 ++
 mm/oom_kill.c      |  2 +-
 3 files changed, 38 insertions(+), 1 deletion(-)
fs/proc/base.c +35 -0
index 23679673bf5a..e3ee4fb1930c 100644
@@ -1043,10 +1043,13 @@ static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
 
 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 {
+	static DEFINE_MUTEX(oom_adj_mutex);
+	struct mm_struct *mm = NULL;
 	struct task_struct *task;
 	unsigned long flags;
 	int err = 0;
 
+	mutex_lock(&oom_adj_mutex);
 	task = get_proc_task(file_inode(file));
 	if (!task) {
 		err = -ESRCH;
@@ -1085,6 +1088,20 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 		}
 	}
 
+	/*
+	 * If we are not in the vfork and share mm with other processes we
+	 * have to propagate the score otherwise we would have a schizophrenic
+	 * requirements for the same mm. We can use racy check because we
+	 * only risk the slow path.
+	 */
+	if (!task->vfork_done &&
+			atomic_read(&task->mm->mm_users) > get_nr_threads(task)) {
+		mm = task->mm;
+
+		/* pin the mm so it doesn't go away and get reused */
+		atomic_inc(&mm->mm_count);
+	}
+
 	task->signal->oom_score_adj = oom_adj;
 	if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
 		task->signal->oom_score_adj_min = (short)oom_adj;
@@ -1094,7 +1111,25 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 err_task_lock:
 	task_unlock(task);
 	put_task_struct(task);
+
+	if (mm) {
+		struct task_struct *p;
+
+		rcu_read_lock();
+		for_each_process(p) {
+			task_lock(p);
+			if (!p->vfork_done && process_shares_mm(p, mm)) {
+				p->signal->oom_score_adj = oom_adj;
+				if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
+					p->signal->oom_score_adj_min = (short)oom_adj;
+			}
+			task_unlock(p);
+		}
+		rcu_read_unlock();
+		mmdrop(mm);
+	}
 out:
+	mutex_unlock(&oom_adj_mutex);
 	return err;
 }
 
include/linux/mm.h +2 -0
index 05102822912c..b44d3d792a00 100644
@@ -2248,6 +2248,8 @@ static inline int in_gate_area(struct mm_struct *mm, unsigned long addr)
 }
 #endif	/* __HAVE_ARCH_GATE_AREA */
 
+extern bool process_shares_mm(struct task_struct *p, struct mm_struct *mm);
+
 #ifdef CONFIG_SYSCTL
 extern int sysctl_drop_caches;
 int drop_caches_sysctl_handler(struct ctl_table *, int,
mm/oom_kill.c +1 -1
index 0e33e912f7e4..eeccb4d7e7f5 100644
@@ -416,7 +416,7 @@ bool oom_killer_disabled __read_mostly;
  * 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)
+bool process_shares_mm(struct task_struct *p, struct mm_struct *mm)
 {
 	struct task_struct *t;
 
Signature · 1 line
2.8.1
author maintainer
#13 lore raw

Re: [PATCH 3/6] mm, oom_adj: make sure processes sharing mm have same view of oom_score_adj

And here again. Get rid of the mm_users check because it is not reliable.
---

#14 lore raw

Re: [PATCH 3/6] mm, oom_adj: make sure processes sharing mm have same view of oom_score_adj

On Fri, May 27, 2016 at 01:18:03PM +0200, Michal Hocko wrote:
...

Quoted 7 lines
(quoted diff) +3 -0
@@ -1087,7 +1105,25 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 	unlock_task_sighand(task, &flags);
 err_put_task:
 	put_task_struct(task);
+
+	if (mm) {
+		struct task_struct *p;

Review static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) · line 1120

Quoted 10 lines
(quoted diff) +10 -0
+
+		rcu_read_lock();
+		for_each_process(p) {
+			task_lock(p);
+			if (!p->vfork_done && process_shares_mm(p, mm)) {
+				p->signal->oom_score_adj = oom_adj;
+				if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
+					p->signal->oom_score_adj_min = (short)oom_adj;
+			}
+			task_unlock(p);

I.e. you write to /proc/pid1/oom_score_adj and get /proc/pid2/oom_score_adj updated if pid1 and pid2 share mm? IMO that looks unexpected from userspace pov.

May be, we'd better add mm->oom_score_adj and set it to the min signal->oom_score_adj over all processes sharing it? This would require iterating over all processes every time oom_score_adj gets updated, but that's a slow path.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#15 lore raw

Re: [PATCH 3/6] mm, oom_adj: make sure processes sharing mm have same view of oom_score_adj

Quoted 15 lines · Vladimir Davydov

On Fri, May 27, 2016 at 01:18:03PM +0200, Michal Hocko wrote:
...

Quoted 13 lines
(quoted diff) +9 -0
@@ -1087,7 +1105,25 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 	unlock_task_sighand(task, &flags);
 err_put_task:
 	put_task_struct(task);
+
+	if (mm) {
+		struct task_struct *p;
+
+		rcu_read_lock();
+		for_each_process(p) {
+			task_lock(p);
+			if (!p->vfork_done && process_shares_mm(p, mm)) {
+				p->signal->oom_score_adj = oom_adj;
Quoted 8 lines

Review static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) · line 1120

Quoted 4 lines
(quoted diff) +4 -0
+				if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
+					p->signal->oom_score_adj_min = (short)oom_adj;
+			}
+			task_unlock(p);

I.e. you write to /proc/pid1/oom_score_adj and get /proc/pid2/oom_score_adj updated if pid1 and pid2 share mm? IMO that looks unexpected from userspace pov.

How much different it is from threads in the same thread group? Processes sharing the mm without signals is a rather weird threading model isn't it? Currently we just lie to users about their oom_score_adj in this weird corner case. The only exception was OOM_SCORE_ADJ_MIN where we really didn't kill the task but all other values are simply ignored in practice.

Quoted 4 lines

May be, we'd better add mm->oom_score_adj and set it to the min signal->oom_score_adj over all processes sharing it? This would require iterating over all processes every time oom_score_adj gets updated, but that's a slow path.

Not sure I understand. So you would prefer that mm->oom_score_adj might disagree with p->signal->oom_score_adj?

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#16 lore raw

Re: [PATCH 3/6] mm, oom_adj: make sure processes sharing mm have same view of oom_score_adj

Quoted 25 lines · Michal Hocko
Quoted 15 lines · Vladimir Davydov

On Fri, May 27, 2016 at 01:18:03PM +0200, Michal Hocko wrote:
...

Quoted 13 lines
(quoted diff) +9 -0
@@ -1087,7 +1105,25 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 	unlock_task_sighand(task, &flags);
 err_put_task:
 	put_task_struct(task);
+
+	if (mm) {
+		struct task_struct *p;
+
+		rcu_read_lock();
+		for_each_process(p) {
+			task_lock(p);
+			if (!p->vfork_done && process_shares_mm(p, mm)) {
+				p->signal->oom_score_adj = oom_adj;
Quoted 8 lines

Review static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) · line 1120

Quoted 4 lines
(quoted diff) +4 -0
+				if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
+					p->signal->oom_score_adj_min = (short)oom_adj;
+			}
+			task_unlock(p);

I.e. you write to /proc/pid1/oom_score_adj and get /proc/pid2/oom_score_adj updated if pid1 and pid2 share mm? IMO that looks unexpected from userspace pov.

Quoted 3 lines

How much different it is from threads in the same thread group? Processes sharing the mm without signals is a rather weird threading model isn't it?

I think so too. I wouldn't be surprised if it turned out that nobody had ever used it. But may be there's someone out there who does.

Quoted 2 lines

Currently we just lie to users about their oom_score_adj in this weird corner case.

Hmm, looks like a bug, but nobody has ever complained about it.

Quoted 11 lines

The only exception was OOM_SCORE_ADJ_MIN
where we really didn't kill the task but all other values are simply ignored in practice.

Quoted 4 lines

May be, we'd better add mm->oom_score_adj and set it to the min signal->oom_score_adj over all processes sharing it? This would require iterating over all processes every time oom_score_adj gets updated, but that's a slow path.

Not sure I understand. So you would prefer that mm->oom_score_adj might disagree with p->signal->oom_score_adj?

No, I wouldn't. I'd rather agree that oom_score_adj should be per mm, because we choose the victim basing solely on mm stats.

What I mean is we don't touch p->signal->oom_score_adj of other tasks sharing mm, but instead store minimal oom_score_adj over all tasks sharing mm in the mm_struct whenever a task's oom_score_adj is modified. And use mm->oom_score_adj instead of signal->oom_score_adj in oom killer code. This would save us from any accusations of user API modifications and it would also make the oom code a bit easier to follow IMHO.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#17 lore raw

Re: [PATCH 3/6] mm, oom_adj: make sure processes sharing mm have same view of oom_score_adj

Quoted 26 lines · Vladimir Davydov
Quoted 25 lines · Michal Hocko
Quoted 23 lines · Vladimir Davydov

On Fri, May 27, 2016 at 01:18:03PM +0200, Michal Hocko wrote:
...

Quoted 7 lines
(quoted diff) +3 -0
@@ -1087,7 +1105,25 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 	unlock_task_sighand(task, &flags);
 err_put_task:
 	put_task_struct(task);
+
+	if (mm) {
+		struct task_struct *p;

Review static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) · line 1120

Quoted 10 lines
(quoted diff) +10 -0
+
+		rcu_read_lock();
+		for_each_process(p) {
+			task_lock(p);
+			if (!p->vfork_done && process_shares_mm(p, mm)) {
+				p->signal->oom_score_adj = oom_adj;
+				if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
+					p->signal->oom_score_adj_min = (short)oom_adj;
+			}
+			task_unlock(p);

I.e. you write to /proc/pid1/oom_score_adj and get /proc/pid2/oom_score_adj updated if pid1 and pid2 share mm? IMO that looks unexpected from userspace pov.

Quoted 6 lines
Quoted 3 lines

How much different it is from threads in the same thread group? Processes sharing the mm without signals is a rather weird threading model isn't it?

I think so too. I wouldn't be surprised if it turned out that nobody had ever used it. But may be there's someone out there who does.

I have heard some rumors about users. But I haven't heard anything about their oom_score_adj usage patterns.

Quoted 4 lines
Quoted 2 lines

Currently we just lie to users about their oom_score_adj in this weird corner case.

Hmm, looks like a bug, but nobody has ever complained about it.

Yes and that leads me to a suspicion that we can do that. Maybe I should just add a note into the log that we are doing that so that people can complain? Something like the following

fs/proc/base.c +5 -1
index fa0b3ca94dfb..7f3495415719 100644
@@ -1104,7 +1104,6 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 err_sighand:
 	unlock_task_sighand(task, &flags);
 err_put_task:
-	put_task_struct(task);
 
 	if (mm) {
 		struct task_struct *p;
@@ -1113,6 +1112,10 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 		for_each_process(p) {
 			task_lock(p);
 			if (!p->vfork_done && process_shares_mm(p, mm)) {
+				pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
+						task_pid_nr(p), p->comm,
+						p->signal->oom_score_adj, oom_adj,
+						task_pid_nr(task), task->comm);
 				p->signal->oom_score_adj = oom_adj;
 				if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
 					p->signal->oom_score_adj_min = (short)oom_adj;
@@ -1122,6 +1125,7 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 		rcu_read_unlock();
 		mmdrop(mm);
 	}
+	put_task_struct(task);
 out:
 	mutex_unlock(&oom_adj_mutex);
 	return err;
Quoted 15 lines
Quoted 11 lines

The only exception was OOM_SCORE_ADJ_MIN
where we really didn't kill the task but all other values are simply ignored in practice.

Quoted 4 lines

May be, we'd better add mm->oom_score_adj and set it to the min signal->oom_score_adj over all processes sharing it? This would require iterating over all processes every time oom_score_adj gets updated, but that's a slow path.

Not sure I understand. So you would prefer that mm->oom_score_adj might disagree with p->signal->oom_score_adj?

No, I wouldn't. I'd rather agree that oom_score_adj should be per mm, because we choose the victim basing solely on mm stats.

Quoted 6 lines

What I mean is we don't touch p->signal->oom_score_adj of other tasks sharing mm, but instead store minimal oom_score_adj over all tasks sharing mm in the mm_struct whenever a task's oom_score_adj is modified. And use mm->oom_score_adj instead of signal->oom_score_adj in oom killer code. This would save us from any accusations of user API modifications and it would also make the oom code a bit easier to follow IMHO.

I understand your point but this is essentially lying because we consider a different value than the user can observe in userspace. Consider somebody doing insanity like

current->oom_score_adj = OOM_SCORE_ADJ_MIN
p = clone(CLONE_VM)
p->oom_score_adj = OOM_SCORE_ADJ_MAX

so one process would want to be always selected while the other one doesn't want to get killed. All they can see is that everything is put in place until the oom killer comes over and ignores that.

I think we should just be explicit. Maybe we want to treat OOM_SCORE_ADJ_MIN special - e.g. do not even try to set oom_score_adj if one of the sharing tasks is oom disabled. But I would rather wait for somebody to complain and explain why the usecase really makes sense than be all silent with implicit behavior.

Btw. we have already had per mm oom_core_adj but we had to revert it due to vfork behavior. See 0753ba01e126 ("mm: revert "oom: move oom_adj value""). This patch gets us back except it handles the vfork issue.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#18 lore raw

Re: [PATCH 3/6] mm, oom_adj: make sure processes sharing mm have same view of oom_score_adj

Quoted 58 lines · Michal Hocko
Quoted 26 lines · Vladimir Davydov
Quoted 25 lines · Michal Hocko
Quoted 23 lines · Vladimir Davydov

On Fri, May 27, 2016 at 01:18:03PM +0200, Michal Hocko wrote:
...

Quoted 7 lines
(quoted diff) +3 -0
@@ -1087,7 +1105,25 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 	unlock_task_sighand(task, &flags);
 err_put_task:
 	put_task_struct(task);
+
+	if (mm) {
+		struct task_struct *p;

Review static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) · line 1120

Quoted 10 lines
(quoted diff) +10 -0
+
+		rcu_read_lock();
+		for_each_process(p) {
+			task_lock(p);
+			if (!p->vfork_done && process_shares_mm(p, mm)) {
+				p->signal->oom_score_adj = oom_adj;
+				if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
+					p->signal->oom_score_adj_min = (short)oom_adj;
+			}
+			task_unlock(p);

I.e. you write to /proc/pid1/oom_score_adj and get /proc/pid2/oom_score_adj updated if pid1 and pid2 share mm? IMO that looks unexpected from userspace pov.

Quoted 6 lines
Quoted 3 lines

How much different it is from threads in the same thread group? Processes sharing the mm without signals is a rather weird threading model isn't it?

I think so too. I wouldn't be surprised if it turned out that nobody had ever used it. But may be there's someone out there who does.

I have heard some rumors about users. But I haven't heard anything about their oom_score_adj usage patterns.

Quoted 4 lines
Quoted 2 lines

Currently we just lie to users about their oom_score_adj in this weird corner case.

Hmm, looks like a bug, but nobody has ever complained about it.

Yes and that leads me to a suspicion that we can do that. Maybe I should just add a note into the log that we are doing that so that people can complain? Something like the following

fs/proc/base.c +0 -1
index fa0b3ca94dfb..7f3495415719 100644
@@ -1104,7 +1104,6 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 err_sighand:
 	unlock_task_sighand(task, &flags);
 err_put_task:
-	put_task_struct(task);
 
 	if (mm) {
 		struct task_struct *p;

fs/proc/base.c static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) · line 1118

Quoted 7 lines
fs/proc/base.c +4 -0
@@ -1113,6 +1112,10 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 		for_each_process(p) {
 			task_lock(p);
 			if (!p->vfork_done && process_shares_mm(p, mm)) {
+				pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
+						task_pid_nr(p), p->comm,
+						p->signal->oom_score_adj, oom_adj,
+						task_pid_nr(task), task->comm);

IMO this could be acceptable from userspace pov, but I don't very much like how vfork is special-cased here and in oom killer code.

Quoted 38 lines
fs/proc/base.c +1 -0
 				p->signal->oom_score_adj = oom_adj;
 				if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
 					p->signal->oom_score_adj_min = (short)oom_adj;
@@ -1122,6 +1125,7 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 		rcu_read_unlock();
 		mmdrop(mm);
 	}
+	put_task_struct(task);
 out:
 	mutex_unlock(&oom_adj_mutex);
 	return err;
Quoted 16 lines
Quoted 5 lines

The only exception was OOM_SCORE_ADJ_MIN
where we really didn't kill the task but all other values are simply ignored in practice.

Quoted 7 lines
Quoted 4 lines

May be, we'd better add mm->oom_score_adj and set it to the min signal->oom_score_adj over all processes sharing it? This would require iterating over all processes every time oom_score_adj gets updated, but that's a slow path.

Not sure I understand. So you would prefer that mm->oom_score_adj might disagree with p->signal->oom_score_adj?

No, I wouldn't. I'd rather agree that oom_score_adj should be per mm, because we choose the victim basing solely on mm stats.

Quoted 6 lines

What I mean is we don't touch p->signal->oom_score_adj of other tasks sharing mm, but instead store minimal oom_score_adj over all tasks sharing mm in the mm_struct whenever a task's oom_score_adj is modified. And use mm->oom_score_adj instead of signal->oom_score_adj in oom killer code. This would save us from any accusations of user API modifications and it would also make the oom code a bit easier to follow IMHO.

I understand your point but this is essentially lying because we consider a different value than the user can observe in userspace. Consider somebody doing insanity like

Quoted 7 lines

current->oom_score_adj = OOM_SCORE_ADJ_MIN
p = clone(CLONE_VM)
p->oom_score_adj = OOM_SCORE_ADJ_MAX

so one process would want to be always selected while the other one doesn't want to get killed. All they can see is that everything is put in place until the oom killer comes over and ignores that.

If we stored minimal oom_score_adj in mm struct, oom killer wouldn't kill any of these processes, and it looks fine to me as long as we want oom killer to be mm based, not task or signal_struct based.

Come to think of it, it'd be difficult to keep mm->oom_score_adj in sync with p->signal->oom_score_adj, because we would need to update mm->oom_score_adj not only on /proc write, but also on fork. May be, we could keep all signal_structs sharing mm linked in per mm list so that we could quickly update mm->oom_score_adj on fork? That way we wouldn't need to special case vfork.

Quoted 10 lines

I think we should just be explicit. Maybe we want to treat OOM_SCORE_ADJ_MIN special - e.g. do not even try to set oom_score_adj if one of the sharing tasks is oom disabled. But I would rather wait for somebody to complain and explain why the usecase really makes sense than be all silent with implicit behavior.

Btw. we have already had per mm oom_core_adj but we had to revert it due to vfork behavior. See 0753ba01e126 ("mm: revert "oom: move oom_adj value""). This patch gets us back except it handles the vfork issue.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#19 lore raw

Re: [PATCH 3/6] mm, oom_adj: make sure processes sharing mm have same view of oom_score_adj

Quoted 1 line · Vladimir Davydov

On Mon, May 30, 2016 at 11:39:50AM +0200, Michal Hocko wrote:

[...]

Quoted 13 lines
Quoted 13 lines

Yes and that leads me to a suspicion that we can do that. Maybe I should just add a note into the log that we are doing that so that people can complain? Something like the following

fs/proc/base.c +0 -1
index fa0b3ca94dfb..7f3495415719 100644
@@ -1104,7 +1104,6 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 err_sighand:
 	unlock_task_sighand(task, &flags);
 err_put_task:
-	put_task_struct(task);
 
Quoted 13 lines

fs/proc/base.c static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) · line 1118

Quoted 10 lines
fs/proc/base.c +4 -0
 	if (mm) {
 		struct task_struct *p;
@@ -1113,6 +1112,10 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 		for_each_process(p) {
 			task_lock(p);
 			if (!p->vfork_done && process_shares_mm(p, mm)) {
+				pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
+						task_pid_nr(p), p->comm,
+						p->signal->oom_score_adj, oom_adj,
+						task_pid_nr(task), task->comm);

IMO this could be acceptable from userspace pov, but I don't very much like how vfork is special-cased here and in oom killer code.

Well, the vfork has to be special cased here. We definitely have to support

	vfork()
	set_oom_score_adj()
	exec()

use case. And I do not see other way without adding something to the clone hot paths which sounds like not justifiable considering we are talking about a really rare usecase that basically nobody cares about.

[...]

Quoted 8 lines
Quoted 3 lines

so one process would want to be always selected while the other one doesn't want to get killed. All they can see is that everything is put in place until the oom killer comes over and ignores that.

If we stored minimal oom_score_adj in mm struct, oom killer wouldn't kill any of these processes, and it looks fine to me as long as we want oom killer to be mm based, not task or signal_struct based.

Quoted 6 lines

Come to think of it, it'd be difficult to keep mm->oom_score_adj in sync with p->signal->oom_score_adj, because we would need to update mm->oom_score_adj not only on /proc write, but also on fork. May be, we could keep all signal_structs sharing mm linked in per mm list so that we could quickly update mm->oom_score_adj on fork? That way we wouldn't need to special case vfork.

Yes the current approach is slightly racy but I do not see that would matter all that much. What you are suggesting might work but I am not really sure we want to complicate the whole thing now. Sure if we see that those races are real we can try to find a better solution, but I would like to start as easy as possible and placing all the logic into the oom_score_adj proc handler sounds like a good spot to me.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#20 lore raw

Re: [PATCH 3/6] mm, oom_adj: make sure processes sharing mm have same view of oom_score_adj

Quoted 32 lines · Michal Hocko
Quoted 1 line · Vladimir Davydov

On Mon, May 30, 2016 at 11:39:50AM +0200, Michal Hocko wrote:

[...]

Quoted 13 lines
Quoted 13 lines

Yes and that leads me to a suspicion that we can do that. Maybe I should just add a note into the log that we are doing that so that people can complain? Something like the following

fs/proc/base.c +0 -1
index fa0b3ca94dfb..7f3495415719 100644
@@ -1104,7 +1104,6 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 err_sighand:
 	unlock_task_sighand(task, &flags);
 err_put_task:
-	put_task_struct(task);
 
Quoted 13 lines

fs/proc/base.c static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) · line 1118

Quoted 10 lines
fs/proc/base.c +4 -0
 	if (mm) {
 		struct task_struct *p;
@@ -1113,6 +1112,10 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 		for_each_process(p) {
 			task_lock(p);
 			if (!p->vfork_done && process_shares_mm(p, mm)) {
+				pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
+						task_pid_nr(p), p->comm,
+						p->signal->oom_score_adj, oom_adj,
+						task_pid_nr(task), task->comm);

IMO this could be acceptable from userspace pov, but I don't very much like how vfork is special-cased here and in oom killer code.

Well, the vfork has to be special cased here. We definitely have to support

Quoted 7 lines
	vfork()
	set_oom_score_adj()
	exec()

use case. And I do not see other way without adding something to the clone hot paths which sounds like not justifiable considering we are talking about a really rare usecase that basically nobody cares about.

I don't think that vfork->exec use case is rare. Quite the contrary, I'm pretty sure it's used often, because in contrast to fork->exec it avoids copying page tables, which can be very expensive for fat processes.

Frankly, I don't understand why you are so determined not to add anything to the fork path. Of course, if the overhead were that dramatic, we would have to forget the idea, but if it were say <= 0.1 % for a contrived test that calls fork in a loop, IMHO the modification would be justified.

Quoted 17 lines

[...]

Quoted 8 lines
Quoted 3 lines

so one process would want to be always selected while the other one doesn't want to get killed. All they can see is that everything is put in place until the oom killer comes over and ignores that.

If we stored minimal oom_score_adj in mm struct, oom killer wouldn't kill any of these processes, and it looks fine to me as long as we want oom killer to be mm based, not task or signal_struct based.

Quoted 6 lines

Come to think of it, it'd be difficult to keep mm->oom_score_adj in sync with p->signal->oom_score_adj, because we would need to update mm->oom_score_adj not only on /proc write, but also on fork. May be, we could keep all signal_structs sharing mm linked in per mm list so that we could quickly update mm->oom_score_adj on fork? That way we wouldn't need to special case vfork.

Quoted 6 lines

Yes the current approach is slightly racy but I do not see that would matter all that much. What you are suggesting might work but I am not really sure we want to complicate the whole thing now. Sure if we see that those races are real we can try to find a better solution, but I would like to start as easy as possible and placing all the logic into the oom_score_adj proc handler sounds like a good spot to me.

IMHO with all those p->vfork_done and p->signal_struct->oom_score_adj checks the oom code is becoming more difficult to understand. Linking signal_struct in a per mm list would probably require more code, but IMHO it would be easier to follow.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#21 lore raw

Re: [PATCH 3/6] mm, oom_adj: make sure processes sharing mm have same view of oom_score_adj

Quoted 37 lines · Vladimir Davydov
Quoted 36 lines · Michal Hocko
Quoted 1 line · Vladimir Davydov

On Mon, May 30, 2016 at 11:39:50AM +0200, Michal Hocko wrote:

[...]

Quoted 13 lines
Quoted 13 lines

Yes and that leads me to a suspicion that we can do that. Maybe I should just add a note into the log that we are doing that so that people can complain? Something like the following

fs/proc/base.c +0 -1
index fa0b3ca94dfb..7f3495415719 100644
@@ -1104,7 +1104,6 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 err_sighand:
 	unlock_task_sighand(task, &flags);
 err_put_task:
-	put_task_struct(task);
 
Quoted 13 lines

fs/proc/base.c static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) · line 1118

Quoted 10 lines
fs/proc/base.c +4 -0
 	if (mm) {
 		struct task_struct *p;
@@ -1113,6 +1112,10 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 		for_each_process(p) {
 			task_lock(p);
 			if (!p->vfork_done && process_shares_mm(p, mm)) {
+				pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
+						task_pid_nr(p), p->comm,
+						p->signal->oom_score_adj, oom_adj,
+						task_pid_nr(task), task->comm);

IMO this could be acceptable from userspace pov, but I don't very much like how vfork is special-cased here and in oom killer code.

Well, the vfork has to be special cased here. We definitely have to support

	vfork()
	set_oom_score_adj()
	exec()
Quoted 7 lines
Quoted 3 lines

use case. And I do not see other way without adding something to the clone hot paths which sounds like not justifiable considering we are talking about a really rare usecase that basically nobody cares about.

I don't think that vfork->exec use case is rare. Quite the contrary, I'm pretty sure it's used often, because in contrast to fork->exec it avoids copying page tables, which can be very expensive for fat processes.

Ohh, yes, the way I put it is ambiguous. What I wanted to say is that the oom is really unlikely so it doesn't justify hot path changes.

Quoted 2 lines

Frankly, I don't understand why you are so determined not to add anything to the fork path.

It is not just the fork path. It would require touching exit path as well and all that code is quite complex already. I would prefer if the oom related complexity stay in the oom proper.

Quoted 4 lines

Of course, if the overhead were that
dramatic, we would have to forget the idea, but if it were say <= 0.1 % for a contrived test that calls fork in a loop, IMHO the modification would be justified.

But why if the proc handler resp. oom_kill_process paths can handle most cases and the occasional races should be tolerate able AFAICS.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author maintainer
#22 lore raw

PATCH 4/6 mm, oom: skip over vforked tasks

In reply to Michal Hocko

From: Michal Hocko <[email protected]>

vforked tasks are not really sitting on memory so it doesn't matter much to kill them. Parents are waiting for vforked task killable so it is better to chose parent which is the real mm owner. Teach oom_badness to ignore all tasks which haven't passed mm_release. oom_kill_process should ignore them as well because they will drop the mm soon and they will not block oom_reaper because they cannot touch any memory.

---
 mm/oom_kill.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
mm/oom_kill.c +11 -2
index eeccb4d7e7f5..d1cbaaa1a666 100644
@@ -176,11 +176,13 @@ unsigned long oom_badness(struct task_struct *p, struct mem_cgroup *memcg,
 
 	/*
 	 * Do not even consider tasks which are explicitly marked oom
-	 * unkillable or have been already oom reaped.
+	 * unkillable or have been already oom reaped or they are in
+	 * the middle of vfork
 	 */
 	adj = (long)p->signal->oom_score_adj;
 	if (adj == OOM_SCORE_ADJ_MIN ||
-			test_bit(MMF_OOM_REAPED, &p->mm->flags)) {
+			test_bit(MMF_OOM_REAPED, &p->mm->flags) ||
+			p->vfork_done) {
 		task_unlock(p);
 		return 0;
 	}
@@ -839,6 +841,13 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
 	for_each_process(p) {
 		if (!process_shares_mm(p, mm))
 			continue;
+		/*
+		 * vforked tasks are ignored because they will drop the mm soon
+		 * hopefully and even if not they will not mind being oom
+		 * reaped because they cannot touch any memory.
+		 */
+		if (p->vfork_done)
+			continue;
 		if (same_thread_group(p, victim))
 			continue;
 		if (unlikely(p->flags & PF_KTHREAD) || is_global_init(p) ||
Signature · 7 lines
2.8.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#23 lore raw

Re: [PATCH 4/6] mm, oom: skip over vforked tasks

Quoted 8 lines · Michal Hocko

From: Michal Hocko <[email protected]>

vforked tasks are not really sitting on memory so it doesn't matter much to kill them. Parents are waiting for vforked task killable so it is better to chose parent which is the real mm owner. Teach oom_badness to ignore all tasks which haven't passed mm_release. oom_kill_process should ignore them as well because they will drop the mm soon and they will not block oom_reaper because they cannot touch any memory.

That is, if a process calls vfork->exec to spawn a child, and a newly spawned child happens to invoke oom somewhere in exec, instead of killing the child, which hasn't done anything yet, we'll kill the main process while the child continues to run. Not sure if it's really bad though.

...

mm/oom_kill.c void oom_kill_process(struct oom_control *oc, struct task_struct *p, · line 847

Quoted 8 lines
mm/oom_kill.c +4 -0
@@ -839,6 +841,13 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
 	for_each_process(p) {
 		if (!process_shares_mm(p, mm))
 			continue;
+		/*
+		 * vforked tasks are ignored because they will drop the mm soon
+		 * hopefully and even if not they will not mind being oom
+		 * reaped because they cannot touch any memory.

They shouldn't modify memory, but they still can touch it AFAIK.

mm/oom_kill.c void oom_kill_process(struct oom_control *oc, struct task_struct *p, · line 853

Quoted 6 lines
mm/oom_kill.c +3 -0
+		 */
+		if (p->vfork_done)
+			continue;
 		if (same_thread_group(p, victim))
 			continue;
 		if (unlikely(p->flags & PF_KTHREAD) || is_global_init(p) ||

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#24 lore raw

Re: [PATCH 4/6] mm, oom: skip over vforked tasks

Quoted 1 line · Vladimir Davydov

On Thu, May 26, 2016 at 02:40:13PM +0200, Michal Hocko wrote:

[...]

Quoted 10 lines

mm/oom_kill.c void oom_kill_process(struct oom_control *oc, struct task_struct *p, · line 847

Quoted 8 lines
mm/oom_kill.c +4 -0
@@ -839,6 +841,13 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
 	for_each_process(p) {
 		if (!process_shares_mm(p, mm))
 			continue;
+		/*
+		 * vforked tasks are ignored because they will drop the mm soon
+		 * hopefully and even if not they will not mind being oom
+		 * reaped because they cannot touch any memory.

They shouldn't modify memory, but they still can touch it AFAIK.

You are right. This means that the vforked child might see zero pages. Let me think whether this is acceptable or not.

Thanks!

Quoted 7 lines
Quoted 7 lines
mm/oom_kill.c +3 -0
+		 */
+		if (p->vfork_done)
+			continue;
 		if (same_thread_group(p, victim))
 			continue;
 		if (unlikely(p->flags & PF_KTHREAD) || is_global_init(p) ||
Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author maintainer
#25 lore raw

Re: [PATCH 4/6] mm, oom: skip over vforked tasks

Quoted 14 lines · Michal Hocko
Quoted 1 line · Vladimir Davydov

On Thu, May 26, 2016 at 02:40:13PM +0200, Michal Hocko wrote:

[...]

Quoted 10 lines

mm/oom_kill.c void oom_kill_process(struct oom_control *oc, struct task_struct *p, · line 847

Quoted 8 lines
mm/oom_kill.c +4 -0
@@ -839,6 +841,13 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
 	for_each_process(p) {
 		if (!process_shares_mm(p, mm))
 			continue;
+		/*
+		 * vforked tasks are ignored because they will drop the mm soon
+		 * hopefully and even if not they will not mind being oom
+		 * reaped because they cannot touch any memory.

They shouldn't modify memory, but they still can touch it AFAIK.

Quoted 2 lines

You are right. This means that the vforked child might see zero pages. Let me think whether this is acceptable or not.

OK, I was thinking about it some more and I think you have a good point here. I can see two options here:
- keep vforked task alive and skip the oom reaper. If the victim exits normally and the oom wouldn't get resolved the vforked task will be selected in the next round because the victim would clean up vfork_done state in wait_for_vfork_done. We are still risking that the victim gets stuck though
- kill vforked task and so it would be reapable.

The later sounds more robust to me because we invoke the oom_reaper and the side effect shouldn't be really a problem because the vforked task couldn't have done a lot of useful work anyway. So I will drop this patch and update "mm, oom: fortify task_will_free_mem" to skip the the vfork check as well.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#26 lore raw

Re: [PATCH 4/6] mm, oom: skip over vforked tasks

Quoted 18 lines · Michal Hocko
Quoted 14 lines · Michal Hocko
Quoted 1 line · Vladimir Davydov

On Thu, May 26, 2016 at 02:40:13PM +0200, Michal Hocko wrote:

[...]

Quoted 10 lines

mm/oom_kill.c void oom_kill_process(struct oom_control *oc, struct task_struct *p, · line 847

Quoted 8 lines
mm/oom_kill.c +4 -0
@@ -839,6 +841,13 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
 	for_each_process(p) {
 		if (!process_shares_mm(p, mm))
 			continue;
+		/*
+		 * vforked tasks are ignored because they will drop the mm soon
+		 * hopefully and even if not they will not mind being oom
+		 * reaped because they cannot touch any memory.

They shouldn't modify memory, but they still can touch it AFAIK.

Quoted 2 lines

You are right. This means that the vforked child might see zero pages. Let me think whether this is acceptable or not.

Quoted 8 lines

OK, I was thinking about it some more and I think you have a good point here. I can see two options here:
- keep vforked task alive and skip the oom reaper. If the victim exits normally and the oom wouldn't get resolved the vforked task will be selected in the next round because the victim would clean up vfork_done state in wait_for_vfork_done. We are still risking that the victim gets stuck though
- kill vforked task and so it would be reapable.

IMHO it all depends on what we're trying to achieve. If we want per task oom, which could make some sense since a task can consume a lot of mem via e.g. pipe buffers, we would go with option #1. However, it's rather difficult to find out how much of kmem a task consumes w/o using kmemcg, so IMHO per-mm approach makes more sense in general. In this case I think we should kill both vforked task and its parent if their mm was selected provided their oom_score_adj allows that.

Quoted 6 lines

The later sounds more robust to me because we invoke the oom_reaper and the side effect shouldn't be really a problem because the vforked task couldn't have done a lot of useful work anyway. So I will drop this patch and update "mm, oom: fortify task_will_free_mem" to skip the the vfork check as well.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#27 lore raw

Re: [PATCH 4/6] mm, oom: skip over vforked tasks

Quoted 28 lines · Vladimir Davydov
Quoted 18 lines · Michal Hocko
Quoted 14 lines · Michal Hocko
Quoted 1 line · Vladimir Davydov

On Thu, May 26, 2016 at 02:40:13PM +0200, Michal Hocko wrote:

[...]

Quoted 10 lines

mm/oom_kill.c void oom_kill_process(struct oom_control *oc, struct task_struct *p, · line 847

Quoted 8 lines
mm/oom_kill.c +4 -0
@@ -839,6 +841,13 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
 	for_each_process(p) {
 		if (!process_shares_mm(p, mm))
 			continue;
+		/*
+		 * vforked tasks are ignored because they will drop the mm soon
+		 * hopefully and even if not they will not mind being oom
+		 * reaped because they cannot touch any memory.

They shouldn't modify memory, but they still can touch it AFAIK.

Quoted 2 lines

You are right. This means that the vforked child might see zero pages. Let me think whether this is acceptable or not.

Quoted 8 lines

OK, I was thinking about it some more and I think you have a good point here. I can see two options here:
- keep vforked task alive and skip the oom reaper. If the victim exits normally and the oom wouldn't get resolved the vforked task will be selected in the next round because the victim would clean up vfork_done state in wait_for_vfork_done. We are still risking that the victim gets stuck though
- kill vforked task and so it would be reapable.

Quoted 7 lines

IMHO it all depends on what we're trying to achieve. If we want per task oom, which could make some sense since a task can consume a lot of mem via e.g. pipe buffers, we would go with option #1. However, it's rather difficult to find out how much of kmem a task consumes w/o using kmemcg, so IMHO per-mm approach makes more sense in general. In this case I think we should kill both vforked task and its parent if their mm was selected provided their oom_score_adj allows that.

Yes agreed. Going with per-mm is a safier behavior because the vast majority of the consumed memory should be per mm not per task_struct.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author maintainer
#29 lore raw

PATCH 5/6 mm, oom: kill all tasks sharing the mm

In reply to Michal Hocko

From: Michal Hocko <[email protected]>

Currently oom_kill_process skips both the oom reaper and SIG_KILL if a process sharing the same mm is unkillable via OOM_ADJUST_MIN. After "mm,
oom_adj: make sure processes sharing mm have same view of oom_score_adj" all such processes are sharing the same value so we shouldn't see such a task at all (oom_badness would rule them out).
Moreover after "mm, oom: skip over vforked tasks" we even cannot encounter vfork task so we can allow both SIG_KILL and oom reaper. A potential race is highly unlikely but possible. It would happen if __set_oom_adj raced with select_bad_process and then it is OK to consider the old value or with fork when it should be acceptable as well.
Let's add a little note to the log so that people would tell us that this really happens in the real life and it matters.

---
 mm/oom_kill.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
mm/oom_kill.c +6 -2
index d1cbaaa1a666..008c5b4732de 100644
@@ -850,8 +850,7 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
 			continue;
 		if (same_thread_group(p, victim))
 			continue;
-		if (unlikely(p->flags & PF_KTHREAD) || is_global_init(p) ||
-		    p->signal->oom_score_adj == OOM_SCORE_ADJ_MIN) {
+		if (unlikely(p->flags & PF_KTHREAD) || is_global_init(p)) {
 			/*
 			 * We cannot use oom_reaper for the mm shared by this
 			 * process because it wouldn't get killed and so the
@@ -860,6 +859,11 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,
 			can_oom_reap = false;
 			continue;
 		}
+		if (p->signal->oom_score_adj == OOM_ADJUST_MIN)
+			pr_warn("%s pid=%d shares mm with oom disabled %s pid=%d. Seems like misconfiguration, killing anyway!"
+					" Report at [email protected]\n",
+					victim->comm, task_pid_nr(victim),
+					p->comm, task_pid_nr(p));
 		do_send_sig_info(SIGKILL, SEND_SIG_FORCED, p, true);
 	}
 	rcu_read_unlock();
Signature · 1 line
2.8.1
author maintainer
#30 lore raw

PATCH 6/6 mm, oom: fortify task_will_free_mem

In reply to Michal Hocko

From: Michal Hocko <[email protected]>

task_will_free_mem is rather weak. It doesn't really tell whether the task has chance to drop its mm. 98748bd72200 ("oom: consider multi-threaded tasks in task_will_free_mem") made a first step into making it more robust for multi-threaded applications so now we know that the whole process is going down. This builds on top for more complex scenarios where mm is shared between different processes (CLONE_VM without CLONE_THREAD resp CLONE_SIGHAND).

Make sure that all processes sharing the mm are killed or exiting. This will allow us to replace try_oom_reaper by wake_oom_reaper. Therefore all paths which bypass the oom killer are now reapable and so they shouldn't lock up the oom killer.

Drop the mm checks for the bypass because those are not really guaranteeing anything as the condition might change at any time after task_lock is dropped.

---
 include/linux/oom.h | 72 +++++++++++++++++++++++++++++++++++++++++++++++++----
 mm/memcontrol.c     |  4 +--
 mm/oom_kill.c       | 65 ++++-------------------------------------------
 3 files changed, 74 insertions(+), 67 deletions(-)
include/linux/oom.h +67 -5
index 83469522690a..412c4ecb42b1 100644
@@ -70,9 +70,9 @@ static inline bool oom_task_origin(const struct task_struct *p)
 extern void mark_oom_victim(struct task_struct *tsk);
 
 #ifdef CONFIG_MMU
-extern void try_oom_reaper(struct task_struct *tsk);
+extern void wake_oom_reaper(struct task_struct *tsk);
 #else
-static inline void try_oom_reaper(struct task_struct *tsk)
+static inline void wake_oom_reaper(struct task_struct *tsk)
 {
 }
 #endif
@@ -105,7 +105,7 @@ extern void oom_killer_enable(void);
 
 extern struct task_struct *find_lock_task_mm(struct task_struct *p);
 
-static inline bool task_will_free_mem(struct task_struct *task)
+static inline bool __task_will_free_mem(struct task_struct *task)
 {
 	struct signal_struct *sig = task->signal;
 
@@ -117,13 +117,75 @@ static inline bool task_will_free_mem(struct task_struct *task)
 	if (sig->flags & SIGNAL_GROUP_COREDUMP)
 		return false;
 
-	if (!(task->flags & PF_EXITING))
+	if (!(task->flags & PF_EXITING || fatal_signal_pending(task)))
 		return false;
 
 	/* Make sure that the whole thread group is going down */
-	if (!thread_group_empty(task) && !(sig->flags & SIGNAL_GROUP_EXIT))
+	if (!thread_group_empty(task) &&
+		!(sig->flags & SIGNAL_GROUP_EXIT || fatal_signal_pending(task)))
+		return false;
+
+	return true;
+}
+
+/*
+ * Checks whether the given task is dying or exiting and likely to
+ * release its address space. This means that all threads and processes
+ * sharing the same mm have to be killed or exiting.
+ */
+static inline bool task_will_free_mem(struct task_struct *task)
+{
+	struct mm_struct *mm = NULL;
+	struct task_struct *p;
+
+	/*
+	 * If the process has passed exit_mm we have to skip it because
+	 * we have lost a link to other tasks sharing this mm, we do not
+	 * have anything to reap and the task might then get stuck waiting
+	 * for parent as zombie and we do not want it to hold TIF_MEMDIE
+	 */
+	p = find_lock_task_mm(task);
+	if (!p)
 		return false;
 
+	if (!__task_will_free_mem(p)) {
+		task_unlock(p);
+		return false;
+	}
+
+	/*
+	 * Check whether there are other processes sharing the mm - they all have
+	 * to be killed or exiting.
+	 */
+	if (atomic_read(&p->mm->mm_users) > get_nr_threads(p)) {
+		mm = p->mm;
+		/* pin the mm to not get freed and reused */
+		atomic_inc(&mm->mm_count);
+	}
+	task_unlock(p);
+
+	if (mm) {
+		rcu_read_lock();
+		for_each_process(p) {
+			bool vfork;
+
+			/*
+			 * skip over vforked tasks because they are mostly
+			 * independent and will drop the mm soon
+			 */
+			task_lock(p);
+			vfork = p->vfork_done;
+			task_unlock(p);
+			if (vfork)
+				continue;
+
+			if (!__task_will_free_mem(p))
+				break;
+		}
+		rcu_read_unlock();
+		mmdrop(mm);
+	}
+
 	return true;
 }
 
mm/memcontrol.c +2 -2
index f6477a9dbe7a..878a4308164c 100644
@@ -1273,9 +1273,9 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	 * select it.  The goal is to allow it to allocate so that it may
 	 * quickly exit and free its memory.
 	 */
-	if (fatal_signal_pending(current) || task_will_free_mem(current)) {
+	if (task_will_free_mem(current)) {
 		mark_oom_victim(current);
-		try_oom_reaper(current);
+		wake_oom_reaper(current);
 		goto unlock;
 	}
 
mm/oom_kill.c +5 -60

101 lines not on this page · raw message

Signature · 7 lines
2.8.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#31 lore raw

Re: [PATCH 6/6] mm, oom: fortify task_will_free_mem

Quoted 46 lines · Michal Hocko
include/linux/oom.h +44 -0
+/*
+ * Checks whether the given task is dying or exiting and likely to
+ * release its address space. This means that all threads and processes
+ * sharing the same mm have to be killed or exiting.
+ */
+static inline bool task_will_free_mem(struct task_struct *task)
+{
+	struct mm_struct *mm = NULL;
+	struct task_struct *p;
+
+	/*
+	 * If the process has passed exit_mm we have to skip it because
+	 * we have lost a link to other tasks sharing this mm, we do not
+	 * have anything to reap and the task might then get stuck waiting
+	 * for parent as zombie and we do not want it to hold TIF_MEMDIE
+	 */
+	p = find_lock_task_mm(task);
+	if (!p)
 		return false;
 
+	if (!__task_will_free_mem(p)) {
+		task_unlock(p);
+		return false;
+	}
+
+	/*
+	 * Check whether there are other processes sharing the mm - they all have
+	 * to be killed or exiting.
+	 */
+	if (atomic_read(&p->mm->mm_users) > get_nr_threads(p)) {
+		mm = p->mm;
+		/* pin the mm to not get freed and reused */
+		atomic_inc(&mm->mm_count);
+	}
+	task_unlock(p);
+
+	if (mm) {
+		rcu_read_lock();
+		for_each_process(p) {
+			bool vfork;
+
+			/*
+			 * skip over vforked tasks because they are mostly
+			 * independent and will drop the mm soon
+			 */
+			task_lock(p);

include/linux/oom.h static inline bool task_will_free_mem(struct task_struct *task) · line 186

Quoted 10 lines
include/linux/oom.h +10 -0
+			vfork = p->vfork_done;
+			task_unlock(p);
+			if (vfork)
+				continue;
+
+			if (!__task_will_free_mem(p))
+				break;
+		}
+		rcu_read_unlock();
+		mmdrop(mm);

Did you forget "if (something) return false;" here?

Review

Quoted 2 lines
(quoted diff) +2 -0
+	}
+

If task_will_free_mem() == true is always followed by mark_oom_victim() and wake_oom_reaper(), can we call them from here?

Quoted 2 lines

return true;
}

author maintainer
#32 lore raw

Re: [PATCH 6/6] mm, oom: fortify task_will_free_mem

On Thu 26-05-16 23:11:47, Tetsuo Handa wrote:
[...]

Quoted 14 lines
Quoted 14 lines
(quoted diff) +14 -0
+	if (mm) {
+		rcu_read_lock();
+		for_each_process(p) {
+			bool vfork;
+
+			/*
+			 * skip over vforked tasks because they are mostly
+			 * independent and will drop the mm soon
+			 */
+			task_lock(p);
+			vfork = p->vfork_done;
+			task_unlock(p);
+			if (vfork)
+				continue;
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+
+			if (!__task_will_free_mem(p))
+				break;
+		}
+		rcu_read_unlock();
+		mmdrop(mm);

Did you forget "if (something) return false;" here?

Dohh, I screwed when cleaning up the code before submission. Thanks for catching that.

Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+	}
+

If task_will_free_mem() == true is always followed by mark_oom_victim() and wake_oom_reaper(), can we call them from here?

I would rather keep the function doing only the check. We never know when this could be reused somewhere else. All the callsites are quite trivial so we do not duplicate a lot of code.
---

Quoted 1 line

From 0b94b4d6f749f322279e911e00abf1390fea4b2b Mon Sep 17 00:00:00 2001

From: Michal Hocko <[email protected]>
Date: Thu, 26 May 2016 09:38:32 +0200
Subject: [PATCH] mm, oom: fortify task_will_free_mem

task_will_free_mem is rather weak. It doesn't really tell whether the task has chance to drop its mm. 98748bd72200 ("oom: consider multi-threaded tasks in task_will_free_mem") made a first step into making it more robust for multi-threaded applications so now we know that the whole process is going down. This builds on top for more complex scenarios where mm is shared between different processes (CLONE_VM without CLONE_THREAD resp CLONE_SIGHAND).

Make sure that all processes sharing the mm are killed or exiting. This will allow us to replace try_oom_reaper by wake_oom_reaper. Therefore all paths which bypass the oom killer are now reapable and so they shouldn't lock up the oom killer.

Drop the mm checks for the bypass because those are not really guaranteeing anything as the condition might change at any time after task_lock is dropped.

---
 include/linux/oom.h | 74 +++++++++++++++++++++++++++++++++++++++++++++++++----
 mm/memcontrol.c     |  4 +--
 mm/oom_kill.c       | 65 ++++------------------------------------------
 3 files changed, 76 insertions(+), 67 deletions(-)
include/linux/oom.h +69 -5
index 83469522690a..91d90a5885dc 100644
@@ -70,9 +70,9 @@ static inline bool oom_task_origin(const struct task_struct *p)
 extern void mark_oom_victim(struct task_struct *tsk);
 
 #ifdef CONFIG_MMU
-extern void try_oom_reaper(struct task_struct *tsk);
+extern void wake_oom_reaper(struct task_struct *tsk);
 #else
-static inline void try_oom_reaper(struct task_struct *tsk)
+static inline void wake_oom_reaper(struct task_struct *tsk)
 {
 }
 #endif
@@ -105,7 +105,7 @@ extern void oom_killer_enable(void);
 
 extern struct task_struct *find_lock_task_mm(struct task_struct *p);
 
-static inline bool task_will_free_mem(struct task_struct *task)
+static inline bool __task_will_free_mem(struct task_struct *task)
 {
 	struct signal_struct *sig = task->signal;
 
@@ -117,16 +117,80 @@ static inline bool task_will_free_mem(struct task_struct *task)
 	if (sig->flags & SIGNAL_GROUP_COREDUMP)
 		return false;
 
-	if (!(task->flags & PF_EXITING))
+	if (!(task->flags & PF_EXITING || fatal_signal_pending(task)))
 		return false;
 
 	/* Make sure that the whole thread group is going down */
-	if (!thread_group_empty(task) && !(sig->flags & SIGNAL_GROUP_EXIT))
+	if (!thread_group_empty(task) &&
+		!(sig->flags & SIGNAL_GROUP_EXIT || fatal_signal_pending(task)))
 		return false;
 
 	return true;
 }
 
+/*
+ * Checks whether the given task is dying or exiting and likely to
+ * release its address space. This means that all threads and processes
+ * sharing the same mm have to be killed or exiting.
+ */
+static inline bool task_will_free_mem(struct task_struct *task)
+{
+	struct mm_struct *mm = NULL;
+	struct task_struct *p;
+	bool ret = false;
+
+	/*
+	 * If the process has passed exit_mm we have to skip it because
+	 * we have lost a link to other tasks sharing this mm, we do not
+	 * have anything to reap and the task might then get stuck waiting
+	 * for parent as zombie and we do not want it to hold TIF_MEMDIE
+	 */
+	p = find_lock_task_mm(task);
+	if (!p)
+		return false;
+
+	if (!__task_will_free_mem(p)) {
+		task_unlock(p);
+		return false;
+	}
+
+	/*
+	 * Check whether there are other processes sharing the mm - they all have
+	 * to be killed or exiting.
+	 */
+	if (atomic_read(&p->mm->mm_users) > get_nr_threads(p)) {
+		mm = p->mm;
+		/* pin the mm to not get freed and reused */
+		atomic_inc(&mm->mm_count);
+	}
+	task_unlock(p);
+
+	if (mm) {
+		rcu_read_lock();
+		for_each_process(p) {
+			bool vfork;
+
+			/*
+			 * skip over vforked tasks because they are mostly
+			 * independent and will drop the mm soon
+			 */
+			task_lock(p);
+			vfork = p->vfork_done;
+			task_unlock(p);
+			if (vfork)
+				continue;
+
+			ret = __task_will_free_mem(p);
+			if (!ret)
+				break;
+		}
+		rcu_read_unlock();
+		mmdrop(mm);
+	}
+
+	return ret;
+}
+
 /* sysctls */
 extern int sysctl_oom_dump_tasks;
 extern int sysctl_oom_kill_allocating_task;
mm/memcontrol.c +2 -2
index f6477a9dbe7a..878a4308164c 100644
@@ -1273,9 +1273,9 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	 * select it.  The goal is to allow it to allocate so that it may
 	 * quickly exit and free its memory.
 	 */
-	if (fatal_signal_pending(current) || task_will_free_mem(current)) {
+	if (task_will_free_mem(current)) {
 		mark_oom_victim(current);
-		try_oom_reaper(current);
+		wake_oom_reaper(current);
 		goto unlock;
 	}
 
mm/oom_kill.c +5 -60

101 lines not on this page · raw message

Signature · 5 lines
2.8.1

-- 
Michal Hocko
SUSE Labs
#33 lore raw

Re: [PATCH 6/6] mm, oom: fortify task_will_free_mem

include/linux/oom.h static inline bool task_will_free_mem(struct task_struct *task) · line 140

Quoted 10 lines · Michal Hocko
include/linux/oom.h +10 -0
+/*
+ * Checks whether the given task is dying or exiting and likely to
+ * release its address space. This means that all threads and processes
+ * sharing the same mm have to be killed or exiting.
+ */
+static inline bool task_will_free_mem(struct task_struct *task)
+{
+	struct mm_struct *mm = NULL;
+	struct task_struct *p;
+	bool ret = false;

If atomic_read(&p->mm->mm_users) <= get_nr_threads(p), this returns "false". According to previous version, I think this is "bool ret = true;".

Quoted 42 lines
include/linux/oom.h +42 -0
+
+	/*
+	 * If the process has passed exit_mm we have to skip it because
+	 * we have lost a link to other tasks sharing this mm, we do not
+	 * have anything to reap and the task might then get stuck waiting
+	 * for parent as zombie and we do not want it to hold TIF_MEMDIE
+	 */
+	p = find_lock_task_mm(task);
+	if (!p)
+		return false;
+
+	if (!__task_will_free_mem(p)) {
+		task_unlock(p);
+		return false;
+	}
+
+	/*
+	 * Check whether there are other processes sharing the mm - they all have
+	 * to be killed or exiting.
+	 */
+	if (atomic_read(&p->mm->mm_users) > get_nr_threads(p)) {
+		mm = p->mm;
+		/* pin the mm to not get freed and reused */
+		atomic_inc(&mm->mm_count);
+	}
+	task_unlock(p);
+
+	if (mm) {
+		rcu_read_lock();
+		for_each_process(p) {
+			bool vfork;
+
+			/*
+			 * skip over vforked tasks because they are mostly
+			 * independent and will drop the mm soon
+			 */
+			task_lock(p);
+			vfork = p->vfork_done;
+			task_unlock(p);
+			if (vfork)
+				continue;
+

include/linux/oom.h static inline bool task_will_free_mem(struct task_struct *task) · line 192

Quoted 10 lines
include/linux/oom.h +10 -0
+			ret = __task_will_free_mem(p);
+			if (!ret)
+				break;
+		}
+		rcu_read_unlock();
+		mmdrop(mm);
+	}
+
+	return ret;
+}

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#34 lore raw

Re: [PATCH 6/6] mm, oom: fortify task_will_free_mem

Quoted 14 lines · Tetsuo Handa

include/linux/oom.h static inline bool task_will_free_mem(struct task_struct *task) · line 140

Quoted 10 lines · Michal Hocko
include/linux/oom.h +10 -0
+/*
+ * Checks whether the given task is dying or exiting and likely to
+ * release its address space. This means that all threads and processes
+ * sharing the same mm have to be killed or exiting.
+ */
+static inline bool task_will_free_mem(struct task_struct *task)
+{
+	struct mm_struct *mm = NULL;
+	struct task_struct *p;
+	bool ret = false;

If atomic_read(&p->mm->mm_users) <= get_nr_threads(p), this returns "false". According to previous version, I think this is "bool ret = true;".

true. Thanks for catching this. Fixed locally.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author maintainer
#35 lore raw

Re: [PATCH 6/6] mm, oom: fortify task_will_free_mem

In reply to Michal Hocko

I have updated the patch to not rely on the mm_users check because it is not reliable as pointed by Tetsuo and we really want this function to be reliable. I do not have a good and reliable way to check for existence of external users sharing the mm so we are checking the whole list unconditionally if mm_users > 1. This should be acceptable because we are doing this only in the slow path of task_will_free_mem. We can surely make this more optimum later.
---

author maintainer
#36 lore raw
In reply to Michal Hocko

JFYI, I plan to repost the series early next week after I review all the pieces again properly with a clean head. If some parts are not sound or completely unacceptable in principle then let me know of course.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#37 lore raw
Quoted 3 lines · Michal Hocko

JFYI, I plan to repost the series early next week after I review all the pieces again properly with a clean head. If some parts are not sound or completely unacceptable in principle then let me know of course.

I don't think we can apply this series.

[PATCH 1/6] is unreliable and will be dropped.

[PATCH 2/6] would be OK as a clean up.

[PATCH 3/6] will change user visible part. We deprecated /proc/pid/oom_adj in Aug 2010 (nearly 6 years ago) by commit 51b1bd2ace1595b7 ("oom: deprecate oom_adj tunable") but we still preserve that behavior, don't we? I think [PATCH 3/6] will need 5 to 10 years of get-acquainted period in order to make sure that no end users will depend on current behavior. This is not something we can change now.

[PATCH 4/6] is unsafe as Vladimir commented.

[PATCH 5/6] will also change user visible part. We need get-acquainted period. This is not something we can change now.

[PATCH 6/6] seems to be unsafe as I commented on a different thread
( http://lkml.kernel.org/r/[email protected] ).

You are trying to make the OOM killer as per mm_struct operation. But I think we need to tolerate the OOM killer as per signal_struct operation.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#38 lore raw
Quoted 9 lines · Tetsuo Handa
Quoted 3 lines · Michal Hocko

JFYI, I plan to repost the series early next week after I review all the pieces again properly with a clean head. If some parts are not sound or completely unacceptable in principle then let me know of course.

I don't think we can apply this series.

[PATCH 1/6] is unreliable and will be dropped.

Quoted 8 lines

[PATCH 2/6] would be OK as a clean up.

[PATCH 3/6] will change user visible part. We deprecated /proc/pid/oom_adj in Aug 2010 (nearly 6 years ago) by commit 51b1bd2ace1595b7 ("oom: deprecate oom_adj tunable") but we still preserve that behavior, don't we? I think [PATCH 3/6] will need 5 to 10 years of get-acquainted period in order to make sure that no end users will depend on current behavior. This is not something we can change now.

I would really like to hear a case where this would break anything.

Quoted 5 lines

[PATCH 4/6] is unsafe as Vladimir commented.

[PATCH 5/6] will also change user visible part. We need get-acquainted period. This is not something we can change now.

Do you care to describe how and who would be affected?

Quoted 2 lines

[PATCH 6/6] seems to be unsafe as I commented on a different thread
( http://lkml.kernel.org/r/[email protected] ).

I already have fixes for those.

Quoted 2 lines

You are trying to make the OOM killer as per mm_struct operation. But I think we need to tolerate the OOM killer as per signal_struct operation.

Signal struct based approach is full of weird behavior which just leads to corner cases. I think going mm struct way is the only sensible approach. So far, the only road block seems to be a question whether changing oom_score_adj for processes out of thread group is acceptable. I argue that it should be because this is what we effectively do we just to not express that to the userspace. Well. except for OOM_SCORE_ADJ_MIN which we might handle specially in some way. So let's argue about potential drawback of the user visible behavior change has, who might be affected and how rather than blocking a sensible approach.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#39 lore raw
Quoted 6 lines · Michal Hocko
Quoted 2 lines

You are trying to make the OOM killer as per mm_struct operation. But I think we need to tolerate the OOM killer as per signal_struct operation.

Signal struct based approach is full of weird behavior which just leads to corner cases. I think going mm struct way is the only sensible approach.

I don't think so. What are corner cases the OOM reaper cannot handle with signal_struct based approach?

The OOM-killer decides based on "struct mm_struct" but it is a weakness of the OOM-killer that it cares only "struct mm_struct". It is possible that waiting for termination of only one thread releases a lot of memory (e.g. by closing pipe's file descriptor) and the OOM-killer needs to send SIGKILL to nobody. From point of view of least killing, trying to wait for exiting task_struct is better than needlessly killing the entire thread groups using some mm_struct. The problem of per task_struct approach is that we have no trigger to give up waiting for that thread if that thread seems to got stuck.

Commit 98748bd722005be9 ("oom: consider multi-threaded tasks in task_will_free_mem") changed from per task_struct approach to per signal_struct approach. And I think that current situation is reasonable because signal_struct is a unit for reacting to SIGKILL. If somebody implements userspace OOM-killer (maybe lowmemory killer?), current situation allows such OOM-killer not to worry about OOM_SCORE_ADJ_MIN or use_mm(). It is still possible that waiting for termination of only one thread group releases a lot of memory. The problem here is that we have no trigger to give up waiting for that thread group if that thread group seems to got stuck. But it is trivial to use the OOM-reaper as a trigger to give up.

Given that said, if everybody can agree with making the OOM-killer per "struct mm_struct" operation, I think reimplementing oom_disable_count which was removed by commit c9f01245b6a7d77d ("oom: remove oom_disable_count") (i.e. do not select an OOM victim unless all thread groups using that mm_struct is killable) seems to be better than ignoring what userspace told to do (i.e. select an OOM victim even if some thread groups using that mm_struct is not killable). Userspace knows the risk of setting OOM_SCORE_ADJ_MIN; it is a strong request like __GFP_NOFAIL allocation. We have global oom_lock which avoids race condition. Since writing to /proc/pid/oom_score_adj is not frequent, we can afford mutex_lock_killable(&oom_lock). We can interpret use_mm() request as setting OOM_SCORE_ADJ_MIN.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#40 lore raw
Quoted 10 lines · Tetsuo Handa
Quoted 6 lines · Michal Hocko
Quoted 2 lines

You are trying to make the OOM killer as per mm_struct operation. But I think we need to tolerate the OOM killer as per signal_struct operation.

Signal struct based approach is full of weird behavior which just leads to corner cases. I think going mm struct way is the only sensible approach.

I don't think so. What are corner cases the OOM reaper cannot handle with signal_struct based approach?

E.g. all the mm shared outside of the thread group with weird inconsistencies crap.

Quoted 5 lines

The OOM-killer decides based on "struct mm_struct" but it is a weakness of the OOM-killer that it cares only "struct mm_struct". It is possible that waiting for termination of only one thread releases a lot of memory (e.g. by closing pipe's file descriptor) and the OOM-killer needs to send SIGKILL to nobody.

How can a thread release pipe's memory when other threads are sharing the same fd?

[...]

Quoted 11 lines

Given that said, if everybody can agree with making the OOM-killer per "struct mm_struct" operation, I think reimplementing oom_disable_count which was removed by commit c9f01245b6a7d77d ("oom: remove oom_disable_count") (i.e. do not select an OOM victim unless all thread groups using that mm_struct is killable) seems to be better than ignoring what userspace told to do (i.e. select an OOM victim even if some thread groups using that mm_struct is not killable). Userspace knows the risk of setting OOM_SCORE_ADJ_MIN; it is a strong request like __GFP_NOFAIL allocation. We have global oom_lock which avoids race condition. Since writing to /proc/pid/oom_score_adj is not frequent, we can afford mutex_lock_killable(&oom_lock). We can interpret use_mm() request as setting OOM_SCORE_ADJ_MIN.

I am not really sure oom_lock is even needed. It is highly unlikely we would race with an ongoing OOM killer. And even then the lock doesn't bring much better semantic.

Regarding oom_disable_count, I think the current approach of
http://lkml.kernel.org/r/[email protected]
has one large advantage. The userspace can simply check the current situation while any internal flag/counter/whatever hides that implementation fact and so the userspace has no means to deal with it.

Sure, it can be argued that changing oom_score_adj behind process back is nasty but we already do that for threads and nobody seems to complain. Shared mm between processes is just a different model of threading from the MM point of view. Or is this thinking wrong in principle?

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>