Re: [PATCH v2 3/4] mm/vma: enforce VMA fork limit on unfaulted,faulted mremap merge too
From: Jeongjun Park
Date: Tue Jan 06 2026 - 10:23:37 EST
Lorenzo Stoakes <lorenzo.stoakes@xxxxxxxxxx> wrote:
>
> The is_mergeable_anon_vma() function uses vmg->middle as the source
> VMA. However when merging a new VMA, this field is NULL.
>
> In all cases except mremap(), the new VMA will either be newly established
> and thus lack an anon_vma, or will be an expansion of an existing VMA thus
> we do not care about whether VMA is CoW'd or not.
>
> In the case of an mremap(), we can end up in a situation where we can
> accidentally allow an unfaulted/faulted merge with a VMA that has been
> forked, violating the general rule that we do not permit this for reasons
> of anon_vma lock scalability.
>
> Now we have the ability to be aware of the fact we are copying a VMA and
> also know which VMA that is, we can explicitly check for this, so do so.
>
> This is pertinent since commit 879bca0a2c4f ("mm/vma: fix incorrectly
> disallowed anonymous VMA merges"), as this patch permits unfaulted/faulted
> merges that were previously disallowed running afoul of this issue.
>
> While we are here, vma_had_uncowed_parents() is a confusing name, so make
> it simple and rename it to vma_is_fork_child().
>
> Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@xxxxxxxxxx>
> Fixes: 879bca0a2c4f ("mm/vma: fix incorrectly disallowed anonymous VMA merges")
> Cc: stable@xxxxxxxxxx
> ---
Reviewed-by: Jeongjun Park <aha310510@xxxxxxxxx>
> mm/vma.c | 27 +++++++++++++++------------
> 1 file changed, 15 insertions(+), 12 deletions(-)
>
> diff --git a/mm/vma.c b/mm/vma.c
> index 660f4732f8a5..fb45a6be7417 100644
> --- a/mm/vma.c
> +++ b/mm/vma.c
> @@ -67,18 +67,13 @@ struct mmap_state {
> .state = VMA_MERGE_START, \
> }
>
> -/*
> - * If, at any point, the VMA had unCoW'd mappings from parents, it will maintain
> - * more than one anon_vma_chain connecting it to more than one anon_vma. A merge
> - * would mean a wider range of folios sharing the root anon_vma lock, and thus
> - * potential lock contention, we do not wish to encourage merging such that this
> - * scales to a problem.
> - */
> -static bool vma_had_uncowed_parents(struct vm_area_struct *vma)
> +/* Was this VMA ever forked from a parent, i.e. maybe contains CoW mappings? */
> +static bool vma_is_fork_child(struct vm_area_struct *vma)
> {
> /*
> * The list_is_singular() test is to avoid merging VMA cloned from
> - * parents. This can improve scalability caused by anon_vma lock.
> + * parents. This can improve scalability caused by the anon_vma root
> + * lock.
> */
> return vma && vma->anon_vma && !list_is_singular(&vma->anon_vma_chain);
> }
> @@ -115,11 +110,19 @@ static bool is_mergeable_anon_vma(struct vma_merge_struct *vmg, bool merge_next)
> VM_WARN_ON(src && src_anon != src->anon_vma);
>
> /* Case 1 - we will dup_anon_vma() from src into tgt. */
> - if (!tgt_anon && src_anon)
> - return !vma_had_uncowed_parents(src);
> + if (!tgt_anon && src_anon) {
> + struct vm_area_struct *copied_from = vmg->copied_from;
> +
> + if (vma_is_fork_child(src))
> + return false;
> + if (vma_is_fork_child(copied_from))
> + return false;
> +
> + return true;
> + }
> /* Case 2 - we will simply use tgt's anon_vma. */
> if (tgt_anon && !src_anon)
> - return !vma_had_uncowed_parents(tgt);
> + return !vma_is_fork_child(tgt);
> /* Case 3 - the anon_vma's are already shared. */
> return src_anon == tgt_anon;
> }
> --
> 2.52.0
>