diff options
author | David Rientjes <rientjes@google.com> | 2009-06-16 18:32:56 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-06-16 22:47:43 -0400 |
commit | 2ff05b2b4eac2e63d345fc731ea151a060247f53 (patch) | |
tree | 1840bc2d3b381eca5d39869499339b0fcc6eabbf /mm/oom_kill.c | |
parent | c9e444103b5e7a5a3519f9913f59767f92e33baf (diff) |
oom: move oom_adj value from task_struct to mm_struct
The per-task oom_adj value is a characteristic of its mm more than the
task itself since it's not possible to oom kill any thread that shares the
mm. If a task were to be killed while attached to an mm that could not be
freed because another thread were set to OOM_DISABLE, it would have
needlessly been terminated since there is no potential for future memory
freeing.
This patch moves oomkilladj (now more appropriately named oom_adj) from
struct task_struct to struct mm_struct. This requires task_lock() on a
task to check its oom_adj value to protect against exec, but it's already
necessary to take the lock when dereferencing the mm to find the total VM
size for the badness heuristic.
This fixes a livelock if the oom killer chooses a task and another thread
sharing the same memory has an oom_adj value of OOM_DISABLE. This occurs
because oom_kill_task() repeatedly returns 1 and refuses to kill the
chosen task while select_bad_process() will repeatedly choose the same
task during the next retry.
Taking task_lock() in select_bad_process() to check for OOM_DISABLE and in
oom_kill_task() to check for threads sharing the same memory will be
removed in the next patch in this series where it will no longer be
necessary.
Writing to /proc/pid/oom_adj for a kthread will now return -EINVAL since
these threads are immune from oom killing already. They simply report an
oom_adj value of OOM_DISABLE.
Cc: Nick Piggin <npiggin@suse.de>
Cc: Rik van Riel <riel@redhat.com>
Cc: Mel Gorman <mel@csn.ul.ie>
Signed-off-by: David Rientjes <rientjes@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/oom_kill.c')
-rw-r--r-- | mm/oom_kill.c | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/mm/oom_kill.c b/mm/oom_kill.c index a7b2460e922b..b60913520ef3 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c | |||
@@ -58,6 +58,7 @@ unsigned long badness(struct task_struct *p, unsigned long uptime) | |||
58 | unsigned long points, cpu_time, run_time; | 58 | unsigned long points, cpu_time, run_time; |
59 | struct mm_struct *mm; | 59 | struct mm_struct *mm; |
60 | struct task_struct *child; | 60 | struct task_struct *child; |
61 | int oom_adj; | ||
61 | 62 | ||
62 | task_lock(p); | 63 | task_lock(p); |
63 | mm = p->mm; | 64 | mm = p->mm; |
@@ -65,6 +66,7 @@ unsigned long badness(struct task_struct *p, unsigned long uptime) | |||
65 | task_unlock(p); | 66 | task_unlock(p); |
66 | return 0; | 67 | return 0; |
67 | } | 68 | } |
69 | oom_adj = mm->oom_adj; | ||
68 | 70 | ||
69 | /* | 71 | /* |
70 | * The memory size of the process is the basis for the badness. | 72 | * The memory size of the process is the basis for the badness. |
@@ -148,15 +150,15 @@ unsigned long badness(struct task_struct *p, unsigned long uptime) | |||
148 | points /= 8; | 150 | points /= 8; |
149 | 151 | ||
150 | /* | 152 | /* |
151 | * Adjust the score by oomkilladj. | 153 | * Adjust the score by oom_adj. |
152 | */ | 154 | */ |
153 | if (p->oomkilladj) { | 155 | if (oom_adj) { |
154 | if (p->oomkilladj > 0) { | 156 | if (oom_adj > 0) { |
155 | if (!points) | 157 | if (!points) |
156 | points = 1; | 158 | points = 1; |
157 | points <<= p->oomkilladj; | 159 | points <<= oom_adj; |
158 | } else | 160 | } else |
159 | points >>= -(p->oomkilladj); | 161 | points >>= -(oom_adj); |
160 | } | 162 | } |
161 | 163 | ||
162 | #ifdef DEBUG | 164 | #ifdef DEBUG |
@@ -251,8 +253,12 @@ static struct task_struct *select_bad_process(unsigned long *ppoints, | |||
251 | *ppoints = ULONG_MAX; | 253 | *ppoints = ULONG_MAX; |
252 | } | 254 | } |
253 | 255 | ||
254 | if (p->oomkilladj == OOM_DISABLE) | 256 | task_lock(p); |
257 | if (p->mm && p->mm->oom_adj == OOM_DISABLE) { | ||
258 | task_unlock(p); | ||
255 | continue; | 259 | continue; |
260 | } | ||
261 | task_unlock(p); | ||
256 | 262 | ||
257 | points = badness(p, uptime.tv_sec); | 263 | points = badness(p, uptime.tv_sec); |
258 | if (points > *ppoints || !chosen) { | 264 | if (points > *ppoints || !chosen) { |
@@ -304,8 +310,7 @@ static void dump_tasks(const struct mem_cgroup *mem) | |||
304 | } | 310 | } |
305 | printk(KERN_INFO "[%5d] %5d %5d %8lu %8lu %3d %3d %s\n", | 311 | printk(KERN_INFO "[%5d] %5d %5d %8lu %8lu %3d %3d %s\n", |
306 | p->pid, __task_cred(p)->uid, p->tgid, mm->total_vm, | 312 | p->pid, __task_cred(p)->uid, p->tgid, mm->total_vm, |
307 | get_mm_rss(mm), (int)task_cpu(p), p->oomkilladj, | 313 | get_mm_rss(mm), (int)task_cpu(p), mm->oom_adj, p->comm); |
308 | p->comm); | ||
309 | task_unlock(p); | 314 | task_unlock(p); |
310 | } while_each_thread(g, p); | 315 | } while_each_thread(g, p); |
311 | } | 316 | } |
@@ -367,8 +372,12 @@ static int oom_kill_task(struct task_struct *p) | |||
367 | * Don't kill the process if any threads are set to OOM_DISABLE | 372 | * Don't kill the process if any threads are set to OOM_DISABLE |
368 | */ | 373 | */ |
369 | do_each_thread(g, q) { | 374 | do_each_thread(g, q) { |
370 | if (q->mm == mm && q->oomkilladj == OOM_DISABLE) | 375 | task_lock(q); |
376 | if (q->mm == mm && q->mm && q->mm->oom_adj == OOM_DISABLE) { | ||
377 | task_unlock(q); | ||
371 | return 1; | 378 | return 1; |
379 | } | ||
380 | task_unlock(q); | ||
372 | } while_each_thread(g, q); | 381 | } while_each_thread(g, q); |
373 | 382 | ||
374 | __oom_kill_task(p, 1); | 383 | __oom_kill_task(p, 1); |
@@ -393,10 +402,11 @@ static int oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order, | |||
393 | struct task_struct *c; | 402 | struct task_struct *c; |
394 | 403 | ||
395 | if (printk_ratelimit()) { | 404 | if (printk_ratelimit()) { |
396 | printk(KERN_WARNING "%s invoked oom-killer: " | ||
397 | "gfp_mask=0x%x, order=%d, oomkilladj=%d\n", | ||
398 | current->comm, gfp_mask, order, current->oomkilladj); | ||
399 | task_lock(current); | 405 | task_lock(current); |
406 | printk(KERN_WARNING "%s invoked oom-killer: " | ||
407 | "gfp_mask=0x%x, order=%d, oom_adj=%d\n", | ||
408 | current->comm, gfp_mask, order, | ||
409 | current->mm ? current->mm->oom_adj : OOM_DISABLE); | ||
400 | cpuset_print_task_mems_allowed(current); | 410 | cpuset_print_task_mems_allowed(current); |
401 | task_unlock(current); | 411 | task_unlock(current); |
402 | dump_stack(); | 412 | dump_stack(); |