• Tetsuo Handa's avatar
    mm: check for SIGKILL inside dup_mmap() loop · 655c79bb
    Tetsuo Handa authored
    As a theoretical problem, dup_mmap() of an mm_struct with 60000+ vmas
    can loop while potentially allocating memory, with mm->mmap_sem held for
    write by current thread.  This is bad if current thread was selected as
    an OOM victim, for current thread will continue allocations using memory
    reserves while OOM reaper is unable to reclaim memory.
    
    As an actually observable problem, it is not difficult to make OOM
    reaper unable to reclaim memory if the OOM victim is blocked at
    i_mmap_lock_write() in this loop.  Unfortunately, since nobody can
    explain whether it is safe to use killable wait there, let's check for
    SIGKILL before trying to allocate memory.  Even without an OOM event,
    there is no point with continuing the loop from the beginning if current
    thread is killed.
    
    I tested with debug printk().  This patch should be safe because we
    already fail if security_vm_enough_memory_mm() or
    kmem_cache_alloc(GFP_KERNEL) fails and exit_mmap() handles it.
    
       ***** Aborting dup_mmap() due to SIGKILL *****
       ***** Aborting dup_mmap() due to SIGKILL *****
       ***** Aborting dup_mmap() due to SIGKILL *****
       ***** Aborting dup_mmap() due to SIGKILL *****
       ***** Aborting exit_mmap() due to NULL mmap *****
    
    [akpm@linux-foundation.org: add comment]
    Link: http://lkml.kernel.org/r/201804071938.CDE04681.SOFVQJFtMHOOLF@I-love.SAKURA.ne.jpSigned-off-by: default avatarTetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
    Cc: Alexander Viro <viro@zeniv.linux.org.uk>
    Cc: Rik van Riel <riel@redhat.com>
    Cc: Michal Hocko <mhocko@suse.com>
    Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
    Cc: David Rientjes <rientjes@google.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    655c79bb
fork.c 60.4 KB