• Li Xinhai's avatar
    Revert "mm/rmap.c: reuse mergeable anon_vma as parent when fork" · 23ab76bf
    Li Xinhai authored
    This reverts commit 4e4a9eb9 ("mm/rmap.c: reuse mergeable
    anon_vma as parent when fork").
    
    In dup_mmap(), anon_vma_fork() is called for attaching anon_vma and
    parameter 'tmp' (i.e., the new vma of child) has same ->vm_next and
    ->vm_prev as its parent vma.  That causes the anon_vma used by parent been
    mistakenly shared by child (In anon_vma_clone(), the code added by that
    commit will do this reuse work).
    
    Besides this issue, the design of reusing anon_vma from vma which has gone
    through fork should be avoided ([1]).  So, this patch reverts that commit
    and maintains the consistent logic of reusing anon_vma for
    fork/split/merge vma.
    
    Reusing anon_vma within the process is fine.  But if a vma has gone
    through fork(), then that vma's anon_vma should not be shared with its
    neighbor vma.  As explained in [1], when vma gone through fork(), the
    check for list_is_singular(vma->anon_vma_chain) will be false, and
    don't share anon_vma.
    
    With current issue, one example can clarify more.  Parent process do
    below two steps:
    
    1. p_vma_1 is created and p_anon_vma_1 is prepared;
    
    2. p_vma_2 is created and share p_anon_vma_1; (this is allowed,
       becaues p_vma_1 didn't gothrough fork()); parent process do fork():
    
    3. c_vma_1 is dup from p_vma_1, and has its own c_anon_vma_1
       prepared; at this point, c_vma_1->anon_vma_chain has two items, one
       for p_anon_vma_1 and one for c_anon_vma_1;
    
    4. c_vma_2 is dup from p_vma_2, it is not allowed to share
       c_anon_vma_1, because
    
    c_vma_1->anon_vma_chain has two items.
    [1] commit d0e9fe17 ("Simplify and comment on anon_vma re-use for
        anon_vma_prepare()") explains the test of "list_is_singular()".
    
    Fixes: 4e4a9eb9 ("mm/rmap.c: reuse mergeable anon_vma as parent when fork")
    Signed-off-by: default avatarLi Xinhai <lixinhai.lxh@gmail.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
    Cc: Matthew Wilcox <willy@infradead.org>
    Cc: Johannes Weiner <hannes@cmpxchg.org>
    Cc: Rik van Riel <riel@redhat.com>
    Link: http://lkml.kernel.org/r/1581150928-3214-3-git-send-email-lixinhai.lxh@gmail.comSigned-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    23ab76bf
rmap.c 55.2 KB