Re: [PATCH v4 5/6] mm: always lock new vma before inserting into vma tree

From: Jann Horn
Date: Mon Aug 14 2023 - 10:55:30 EST


@akpm can you fix this up?

On Fri, Aug 4, 2023 at 5:27 PM Suren Baghdasaryan <surenb@xxxxxxxxxx> wrote:
> 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.
>
> Suggested-by: Jann Horn <jannh@xxxxxxxxxx>
> Signed-off-by: Suren Baghdasaryan <surenb@xxxxxxxxxx>
> Reviewed-by: Liam R. Howlett <Liam.Howlett@xxxxxxxxxx>
> ---
> mm/mmap.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/mm/mmap.c b/mm/mmap.c
> index 3937479d0e07..850a39dee075 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -412,6 +412,8 @@ static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma)
> if (vma_iter_prealloc(&vmi))
> return -ENOMEM;
>
> + vma_start_write(vma);
> +
> if (vma->vm_file) {
> mapping = vma->vm_file->f_mapping;
> i_mmap_lock_write(mapping);

Something went wrong when this part of the patch was applied, because
of a conflict with "mm/mmap: move vma operations to mm_struct out of
the critical section of file mapping lock"; see how this patch ended
up in the mm tree:
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git/commit/?id=26cb4dafc13871ab68a4fb480ca1e19381cff392

> @@ -403,6 +403,8 @@ static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma)
>
> vma_iter_store(&vmi, vma);
>
> + vma_start_write(vma);
> +
> if (vma->vm_file) {
> mapping = vma->vm_file->f_mapping;
> i_mmap_lock_write(mapping);

The "vma_start_write()" has to be ordered before the
"vma_iter_store(&vmi, vma)".