Skip to content

Commit

Permalink
oom_kill: change oom_kill.c to use for_each_thread()
Browse files Browse the repository at this point in the history
Change oom_kill.c to use for_each_thread() rather than the racy
while_each_thread() which can loop forever if we race with exit.

Note also that most users were buggy even if while_each_thread() was
fine, the task can exit even _before_ rcu_read_lock().

Fortunately the new for_each_thread() only requires the stable
task_struct, so this change fixes both problems.

Signed-off-by: Oleg Nesterov <[email protected]>
Reviewed-by: Sergey Dyasly <[email protected]>
Tested-by: Sergey Dyasly <[email protected]>
Reviewed-by: Sameer Nanda <[email protected]>
Cc: "Eric W. Biederman" <[email protected]>
Cc: Frederic Weisbecker <[email protected]>
Cc: Mandeep Singh Baines <[email protected]>
Cc: "Ma, Xindong" <[email protected]>
Reviewed-by: Michal Hocko <[email protected]>
Cc: "Tu, Xiaobing" <[email protected]>
Acked-by: David Rientjes <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
oleg-nesterov authored and torvalds committed Jan 22, 2014
1 parent 0c740d0 commit 1da4db0
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions mm/oom_kill.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static bool has_intersects_mems_allowed(struct task_struct *tsk,
{
struct task_struct *start = tsk;

do {
for_each_thread(start, tsk) {
if (mask) {
/*
* If this is a mempolicy constrained oom, tsk's
Expand All @@ -77,7 +77,7 @@ static bool has_intersects_mems_allowed(struct task_struct *tsk,
if (cpuset_mems_allowed_intersects(current, tsk))
return true;
}
} while_each_thread(start, tsk);
}

return false;
}
Expand All @@ -97,14 +97,14 @@ static bool has_intersects_mems_allowed(struct task_struct *tsk,
*/
struct task_struct *find_lock_task_mm(struct task_struct *p)
{
struct task_struct *t = p;
struct task_struct *t;

do {
for_each_thread(p, t) {
task_lock(t);
if (likely(t->mm))
return t;
task_unlock(t);
} while_each_thread(p, t);
}

return NULL;
}
Expand Down Expand Up @@ -301,7 +301,7 @@ static struct task_struct *select_bad_process(unsigned int *ppoints,
unsigned long chosen_points = 0;

rcu_read_lock();
do_each_thread(g, p) {
for_each_process_thread(g, p) {
unsigned int points;

switch (oom_scan_process_thread(p, totalpages, nodemask,
Expand All @@ -323,7 +323,7 @@ static struct task_struct *select_bad_process(unsigned int *ppoints,
chosen = p;
chosen_points = points;
}
} while_each_thread(g, p);
}
if (chosen)
get_task_struct(chosen);
rcu_read_unlock();
Expand Down Expand Up @@ -406,7 +406,7 @@ void oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
{
struct task_struct *victim = p;
struct task_struct *child;
struct task_struct *t = p;
struct task_struct *t;
struct mm_struct *mm;
unsigned int victim_points = 0;
static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL,
Expand Down Expand Up @@ -437,7 +437,7 @@ void oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
* still freeing memory.
*/
read_lock(&tasklist_lock);
do {
for_each_thread(p, t) {
list_for_each_entry(child, &t->children, sibling) {
unsigned int child_points;

Expand All @@ -455,7 +455,7 @@ void oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
get_task_struct(victim);
}
}
} while_each_thread(p, t);
}
read_unlock(&tasklist_lock);

rcu_read_lock();
Expand Down

0 comments on commit 1da4db0

Please sign in to comment.