diff options
author | Oleg Nesterov <oleg@redhat.com> | 2010-08-09 20:18:45 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2010-08-09 23:44:55 -0400 |
commit | dd8e8f405ca386c7ce7cbb996ccd985d283b0e03 (patch) | |
tree | 7863585871d44c0272eb0b93ee909392ee85eb99 | |
parent | b52279406e77be711c068f9a8e970ea6471e089c (diff) |
oom: introduce find_lock_task_mm() to fix !mm false positives
Almost all ->mm == NULL checks in oom_kill.c are wrong.
The current code assumes that the task without ->mm has already released
its memory and ignores the process. However this is not necessarily true
when this process is multithreaded, other live sub-threads can use this
->mm.
- Remove the "if (!p->mm)" check in select_bad_process(), it is
just wrong.
- Add the new helper, find_lock_task_mm(), which finds the live
thread which uses the memory and takes task_lock() to pin ->mm
- change oom_badness() to use this helper instead of just checking
->mm != NULL.
- As David pointed out, select_bad_process() must never choose the
task without ->mm, but no matter what oom_badness() returns the
task can be chosen if nothing else has been found yet.
Change oom_badness() to return int, change it to return -1 if
find_lock_task_mm() fails, and change select_bad_process() to
check points >= 0.
Note! This patch is not enough, we need more changes.
- oom_badness() was fixed, but oom_kill_task() still ignores
the task without ->mm
- oom_forkbomb_penalty() should use find_lock_task_mm() too,
and it also needs other changes to actually find the first
first-descendant children
This will be addressed later.
[kosaki.motohiro@jp.fujitsu.com: use in badness(), __oom_kill_task()]
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: David Rientjes <rientjes@google.com>
Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | mm/oom_kill.c | 74 |
1 files changed, 43 insertions, 31 deletions
diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 0a6e466155d2..9a686aa35a48 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c | |||
@@ -52,6 +52,20 @@ static int has_intersects_mems_allowed(struct task_struct *tsk) | |||
52 | return 0; | 52 | return 0; |
53 | } | 53 | } |
54 | 54 | ||
55 | static struct task_struct *find_lock_task_mm(struct task_struct *p) | ||
56 | { | ||
57 | struct task_struct *t = p; | ||
58 | |||
59 | do { | ||
60 | task_lock(t); | ||
61 | if (likely(t->mm)) | ||
62 | return t; | ||
63 | task_unlock(t); | ||
64 | } while_each_thread(p, t); | ||
65 | |||
66 | return NULL; | ||
67 | } | ||
68 | |||
55 | /** | 69 | /** |
56 | * badness - calculate a numeric value for how bad this task has been | 70 | * badness - calculate a numeric value for how bad this task has been |
57 | * @p: task struct of which task we should calculate | 71 | * @p: task struct of which task we should calculate |
@@ -74,8 +88,8 @@ static int has_intersects_mems_allowed(struct task_struct *tsk) | |||
74 | unsigned long badness(struct task_struct *p, unsigned long uptime) | 88 | unsigned long badness(struct task_struct *p, unsigned long uptime) |
75 | { | 89 | { |
76 | unsigned long points, cpu_time, run_time; | 90 | unsigned long points, cpu_time, run_time; |
77 | struct mm_struct *mm; | ||
78 | struct task_struct *child; | 91 | struct task_struct *child; |
92 | struct task_struct *c, *t; | ||
79 | int oom_adj = p->signal->oom_adj; | 93 | int oom_adj = p->signal->oom_adj; |
80 | struct task_cputime task_time; | 94 | struct task_cputime task_time; |
81 | unsigned long utime; | 95 | unsigned long utime; |
@@ -84,17 +98,14 @@ unsigned long badness(struct task_struct *p, unsigned long uptime) | |||
84 | if (oom_adj == OOM_DISABLE) | 98 | if (oom_adj == OOM_DISABLE) |
85 | return 0; | 99 | return 0; |
86 | 100 | ||
87 | task_lock(p); | 101 | p = find_lock_task_mm(p); |
88 | mm = p->mm; | 102 | if (!p) |
89 | if (!mm) { | ||
90 | task_unlock(p); | ||
91 | return 0; | 103 | return 0; |
92 | } | ||
93 | 104 | ||
94 | /* | 105 | /* |
95 | * The memory size of the process is the basis for the badness. | 106 | * The memory size of the process is the basis for the badness. |
96 | */ | 107 | */ |
97 | points = mm->total_vm; | 108 | points = p->mm->total_vm; |
98 | 109 | ||
99 | /* | 110 | /* |
100 | * After this unlock we can no longer dereference local variable `mm' | 111 | * After this unlock we can no longer dereference local variable `mm' |
@@ -115,12 +126,17 @@ unsigned long badness(struct task_struct *p, unsigned long uptime) | |||
115 | * child is eating the vast majority of memory, adding only half | 126 | * child is eating the vast majority of memory, adding only half |
116 | * to the parents will make the child our kill candidate of choice. | 127 | * to the parents will make the child our kill candidate of choice. |
117 | */ | 128 | */ |
118 | list_for_each_entry(child, &p->children, sibling) { | 129 | t = p; |
119 | task_lock(child); | 130 | do { |
120 | if (child->mm != mm && child->mm) | 131 | list_for_each_entry(c, &t->children, sibling) { |
121 | points += child->mm->total_vm/2 + 1; | 132 | child = find_lock_task_mm(c); |
122 | task_unlock(child); | 133 | if (child) { |
123 | } | 134 | if (child->mm != p->mm) |
135 | points += child->mm->total_vm/2 + 1; | ||
136 | task_unlock(child); | ||
137 | } | ||
138 | } | ||
139 | } while_each_thread(p, t); | ||
124 | 140 | ||
125 | /* | 141 | /* |
126 | * CPU time is in tens of seconds and run time is in thousands | 142 | * CPU time is in tens of seconds and run time is in thousands |
@@ -256,9 +272,6 @@ static struct task_struct *select_bad_process(unsigned long *ppoints, | |||
256 | for_each_process(p) { | 272 | for_each_process(p) { |
257 | unsigned long points; | 273 | unsigned long points; |
258 | 274 | ||
259 | /* skip tasks that have already released their mm */ | ||
260 | if (!p->mm) | ||
261 | continue; | ||
262 | /* skip the init task and kthreads */ | 275 | /* skip the init task and kthreads */ |
263 | if (is_global_init(p) || (p->flags & PF_KTHREAD)) | 276 | if (is_global_init(p) || (p->flags & PF_KTHREAD)) |
264 | continue; | 277 | continue; |
@@ -385,14 +398,9 @@ static void __oom_kill_task(struct task_struct *p, int verbose) | |||
385 | return; | 398 | return; |
386 | } | 399 | } |
387 | 400 | ||
388 | task_lock(p); | 401 | p = find_lock_task_mm(p); |
389 | if (!p->mm) { | 402 | if (!p) |
390 | WARN_ON(1); | ||
391 | printk(KERN_WARNING "tried to kill an mm-less task %d (%s)!\n", | ||
392 | task_pid_nr(p), p->comm); | ||
393 | task_unlock(p); | ||
394 | return; | 403 | return; |
395 | } | ||
396 | 404 | ||
397 | if (verbose) | 405 | if (verbose) |
398 | printk(KERN_ERR "Killed process %d (%s) " | 406 | printk(KERN_ERR "Killed process %d (%s) " |
@@ -437,6 +445,7 @@ static int oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order, | |||
437 | const char *message) | 445 | const char *message) |
438 | { | 446 | { |
439 | struct task_struct *c; | 447 | struct task_struct *c; |
448 | struct task_struct *t = p; | ||
440 | 449 | ||
441 | if (printk_ratelimit()) | 450 | if (printk_ratelimit()) |
442 | dump_header(p, gfp_mask, order, mem); | 451 | dump_header(p, gfp_mask, order, mem); |
@@ -454,14 +463,17 @@ static int oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order, | |||
454 | message, task_pid_nr(p), p->comm, points); | 463 | message, task_pid_nr(p), p->comm, points); |
455 | 464 | ||
456 | /* Try to kill a child first */ | 465 | /* Try to kill a child first */ |
457 | list_for_each_entry(c, &p->children, sibling) { | 466 | do { |
458 | if (c->mm == p->mm) | 467 | list_for_each_entry(c, &t->children, sibling) { |
459 | continue; | 468 | if (c->mm == p->mm) |
460 | if (mem && !task_in_mem_cgroup(c, mem)) | 469 | continue; |
461 | continue; | 470 | if (mem && !task_in_mem_cgroup(c, mem)) |
462 | if (!oom_kill_task(c)) | 471 | continue; |
463 | return 0; | 472 | if (!oom_kill_task(c)) |
464 | } | 473 | return 0; |
474 | } | ||
475 | } while_each_thread(p, t); | ||
476 | |||
465 | return oom_kill_task(p); | 477 | return oom_kill_task(p); |
466 | } | 478 | } |
467 | 479 | ||