mm: always lock new vma before inserting into vma tree

commit ad9f006351c3368171458ae7ab14d72f031b239f
Author: Suren Baghdasaryan <surenb@google.com>
Date:   Fri Aug 4 08:27:23 2023 -0700

    mm: always lock new vma before inserting into vma tree

    While it's not strictly necessary to lock a newly created vma before
    adding it into the vma tree (as long as no further changes are performed
    to it), it seems like a good policy to lock it and prevent accidental
    changes after it becomes visible to the page faults. Lock the vma before
    adding it into the vma tree.

    [akpm@linux-foundation.org: fix reject fixing in vma_link(), per Jann]
    Link: https://lkml.kernel.org/r/20230804152724.3090321-6-surenb@google.com
    Suggested-by: Jann Horn <jannh@google.com>
    Signed-off-by: Suren Baghdasaryan <surenb@google.com>
    Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Cc: Linus Torvalds <torvalds@linuxfoundation.org>
    Cc: Jann Horn <jannh@google.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-5619
Signed-off-by: Nico Pache <npache@redhat.com>
This commit is contained in:
Nico Pache 2024-04-15 19:04:21 -06:00
parent 0f34ae86f8
commit 4263d177b8
1 changed files with 7 additions and 3 deletions

View File

@ -423,12 +423,15 @@ static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma)
if (vma_iter_prealloc(&vmi))
return -ENOMEM;
vma_start_write(vma);
vma_iter_store(&vmi, vma);
if (vma->vm_file) {
mapping = vma->vm_file->f_mapping;
i_mmap_lock_write(mapping);
}
vma_iter_store(&vmi, vma);
if (mapping) {
__vma_link_file(vma, mapping);
@ -488,7 +491,8 @@ static inline void vma_prepare(struct vma_prepare *vp)
vma_start_write(vp->vma);
if (vp->adj_next)
vma_start_write(vp->adj_next);
/* vp->insert is always a newly created VMA, no need for locking */
if (vp->insert)
vma_start_write(vp->insert);
if (vp->remove)
vma_start_write(vp->remove);
if (vp->remove2)
@ -3030,6 +3034,7 @@ static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,
vma->vm_pgoff = addr >> PAGE_SHIFT;
vm_flags_init(vma, flags);
vma->vm_page_prot = vm_get_page_prot(flags);
vma_start_write(vma);
if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL))
goto mas_store_fail;
@ -3277,7 +3282,6 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
get_file(new_vma->vm_file);
if (new_vma->vm_ops && new_vma->vm_ops->open)
new_vma->vm_ops->open(new_vma);
vma_start_write(new_vma);
if (vma_link(mm, new_vma))
goto out_vma_link;
*need_rmap_locks = false;