(sorry for html)

Why do we even bother with locking?
Why not just merge my original patch? (The link is in Vladimir's message)
It provides much more elegant (and working!) solution for this problem.
David, how did you miss it in the first place?

Oh.. and by the way. I was hitting the same bug in other
while_each_thread loops in oom_kill.c. Anyway, goodluck ;)

14 ΞΟΡΒ. 2013 Η. 2:18 ΠΟΜΨΪΟΧΑΤΕΜΨ "Sameer Nanda" <snanda@chromium.org> ΞΑΠΙΣΑΜ:
The selection of the process to be killed happens in two spots:
first in select_bad_process and then a further refinement by
looking for child processes in oom_kill_process. Since this is
a two step process, it is possible that the process selected by
select_bad_process may get a SIGKILL just before oom_kill_process
executes. If this were to happen, __unhash_process deletes this
process from the thread_group list. This results in oom_kill_process
getting stuck in an infinite loop when traversing the thread_group
list of the selected process.

Fix this race by adding a pid_alive check for the selected process
with tasklist_lock held in oom_kill_process.

Signed-off-by: Sameer Nanda <snanda@chromium.org>
---
šinclude/linux/sched.h | š5 +++++
šmm/oom_kill.c š š š š | 34 +++++++++++++++++++++-------------
š2 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index e27baee..8975dbb 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2156,6 +2156,11 @@ extern bool current_is_single_threaded(void);
š#define do_each_thread(g, t) \
š š š š for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do

+/*
+ * Careful: while_each_thread is not RCU safe. Callers should hold
+ * read_lock(tasklist_lock) across while_each_thread loops.
+ */
+
š#define while_each_thread(g, t) \
š š š š while ((t = next_thread(t)) != g)

diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 6738c47..0d1f804 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -412,31 +412,33 @@ void oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
š š š š static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL,
š š š š š š š š š š š š š š š š š š š š š š š DEFAULT_RATELIMIT_BURST);

+ š š š if (__ratelimit(&oom_rs))
+ š š š š š š š dump_header(p, gfp_mask, order, memcg, nodemask);
+
+ š š š task_lock(p);
+ š š š pr_err("%s: Kill process %d (%s) score %d or sacrifice child\n",
+ š š š š š š š message, task_pid_nr(p), p->comm, points);
+ š š š task_unlock(p);
+
+ š š š read_lock(&tasklist_lock);
+
š š š š /*
š š š š š* If the task is already exiting, don't alarm the sysadmin or kill
š š š š š* its children or threads, just set TIF_MEMDIE so it can die quickly
š š š š š*/
- š š š if (p->flags & PF_EXITING) {
+ š š š if (p->flags & PF_EXITING || !pid_alive(p)) {
š š š š š š š š set_tsk_thread_flag(p, TIF_MEMDIE);
š š š š š š š š put_task_struct(p);
+ š š š š š š š read_unlock(&tasklist_lock);
š š š š š š š š return;
š š š š }

- š š š if (__ratelimit(&oom_rs))
- š š š š š š š dump_header(p, gfp_mask, order, memcg, nodemask);
-
- š š š task_lock(p);
- š š š pr_err("%s: Kill process %d (%s) score %d or sacrifice child\n",
- š š š š š š š message, task_pid_nr(p), p->comm, points);
- š š š task_unlock(p);
-
š š š š /*
š š š š š* If any of p's children has a different mm and is eligible for kill,
š š š š š* the one with the highest oom_badness() score is sacrificed for its
š š š š š* parent. šThis attempts to lose the minimal amount of work done while
š š š š š* still freeing memory.
š š š š š*/
- š š š read_lock(&tasklist_lock);
š š š š do {
š š š š š š š š list_for_each_entry(child, &t->children, sibling) {
š š š š š š š š š š š š unsigned int child_points;
@@ -456,12 +458,17 @@ void oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
š š š š š š š š š š š š }
š š š š š š š š }
š š š š } while_each_thread(p, t);
- š š š read_unlock(&tasklist_lock);

- š š š rcu_read_lock();
š š š š p = find_lock_task_mm(victim);
+
+ š š š /*
+ š š š š* Since while_each_thread is currently not RCU safe, this unlock of
+ š š š š* tasklist_lock may need to be moved further down if any additional
+ š š š š* while_each_thread loops get added to this function.
+ š š š š*/
+ š š š read_unlock(&tasklist_lock);
+
š š š š if (!p) {
- š š š š š š š rcu_read_unlock();
š š š š š š š š put_task_struct(victim);
š š š š š š š š return;
š š š š } else if (victim != p) {
@@ -487,6 +494,7 @@ void oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
š š š š š* That thread will now get access to memory reserves since it has a
š š š š š* pending fatal signal.
š š š š š*/
+ š š š rcu_read_lock();
š š š š for_each_process(p)
š š š š š š š š if (p->mm == mm && !same_thread_group(p, victim) &&
š š š š š š š š š š !(p->flags & PF_KTHREAD)) {
--
1.8.4.1