Skip to content

Commit e2fe145

Browse files
Michal Hockotorvalds
authored andcommitted
oom_reaper: close race with exiting task
Tetsuo has reported: Out of memory: Kill process 443 (oleg's-test) score 855 or sacrifice child Killed process 443 (oleg's-test) total-vm:493248kB, anon-rss:423880kB, file-rss:4kB, shmem-rss:0kB sh invoked oom-killer: gfp_mask=0x24201ca(GFP_HIGHUSER_MOVABLE|__GFP_COLD), order=0, oom_score_adj=0 sh cpuset=/ mems_allowed=0 CPU: 2 PID: 1 Comm: sh Not tainted 4.6.0-rc7+ torvalds#51 Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013 Call Trace: dump_stack+0x85/0xc8 dump_header+0x5b/0x394 oom_reaper: reaped process 443 (oleg's-test), now anon-rss:0kB, file-rss:0kB, shmem-rss:0kB In other words: __oom_reap_task exit_mm atomic_inc_not_zero tsk->mm = NULL mmput atomic_dec_and_test # > 0 exit_oom_victim # New victim will be # selected <OOM killer invoked> # no TIF_MEMDIE task so we can select a new one unmap_page_range # to release the memory The race exists even without the oom_reaper because anybody who pins the address space and gets preempted might race with exit_mm but oom_reaper made this race more probable. We can address the oom_reaper part by using oom_lock for __oom_reap_task because this would guarantee that a new oom victim will not be selected if the oom reaper might race with the exit path. This doesn't solve the original issue, though, because somebody else still might be pinning mm_users and so __mmput won't be called to release the memory but that is not really realiably solvable because the task will get away from the oom sight as soon as it is unhashed from the task_list and so we cannot guarantee a new victim won't be selected. [akpm@linux-foundation.org: fix use of unused `mm', Per Stephen] [akpm@linux-foundation.org: coding-style fixes] Fixes: aac4536 ("mm, oom: introduce oom reaper") Link: http://lkml.kernel.org/r/1464271493-20008-1-git-send-email-mhocko@kernel.org Signed-off-by: Michal Hocko <mhocko@suse.com> Reported-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f65e91d commit e2fe145

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

mm/oom_kill.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,28 @@ static bool __oom_reap_task(struct task_struct *tsk)
443443
{
444444
struct mmu_gather tlb;
445445
struct vm_area_struct *vma;
446-
struct mm_struct *mm;
446+
struct mm_struct *mm = NULL;
447447
struct task_struct *p;
448448
struct zap_details details = {.check_swap_entries = true,
449449
.ignore_dirty = true};
450450
bool ret = true;
451451

452+
/*
453+
* We have to make sure to not race with the victim exit path
454+
* and cause premature new oom victim selection:
455+
* __oom_reap_task exit_mm
456+
* atomic_inc_not_zero
457+
* mmput
458+
* atomic_dec_and_test
459+
* exit_oom_victim
460+
* [...]
461+
* out_of_memory
462+
* select_bad_process
463+
* # no TIF_MEMDIE task selects new victim
464+
* unmap_page_range # frees some memory
465+
*/
466+
mutex_lock(&oom_lock);
467+
452468
/*
453469
* Make sure we find the associated mm_struct even when the particular
454470
* thread has already terminated and cleared its mm.
@@ -457,19 +473,19 @@ static bool __oom_reap_task(struct task_struct *tsk)
457473
*/
458474
p = find_lock_task_mm(tsk);
459475
if (!p)
460-
return true;
476+
goto unlock_oom;
461477

462478
mm = p->mm;
463479
if (!atomic_inc_not_zero(&mm->mm_users)) {
464480
task_unlock(p);
465-
return true;
481+
goto unlock_oom;
466482
}
467483

468484
task_unlock(p);
469485

470486
if (!down_read_trylock(&mm->mmap_sem)) {
471487
ret = false;
472-
goto out;
488+
goto unlock_oom;
473489
}
474490

475491
tlb_gather_mmu(&tlb, mm, 0, -1);
@@ -511,13 +527,15 @@ static bool __oom_reap_task(struct task_struct *tsk)
511527
* to release its memory.
512528
*/
513529
set_bit(MMF_OOM_REAPED, &mm->flags);
514-
out:
530+
unlock_oom:
531+
mutex_unlock(&oom_lock);
515532
/*
516533
* Drop our reference but make sure the mmput slow path is called from a
517534
* different context because we shouldn't risk we get stuck there and
518535
* put the oom_reaper out of the way.
519536
*/
520-
mmput_async(mm);
537+
if (mm)
538+
mmput_async(mm);
521539
return ret;
522540
}
523541

0 commit comments

Comments
 (0)