Re: [PATCH 4/5] mm: make vmg->target consistent and further simplify commit_merge()

From: Vlastimil Babka
Date: Wed Jan 29 2025 - 09:45:39 EST


On 1/27/25 16:50, Lorenzo Stoakes wrote:
> It is confusing for vmg->target to sometimes be the target merged VMA and
> in one case not.
>
> Fix this by having commit_merge() use its awareness of the
> __VMG_FLAG_ADJUST_NEXT_START case to know that it is manipulating a
> separate vma, abstracted in the 'vma' local variable.
>
> Place removal and adjust VMA determination logic into
> init_multi_vma_prep(), as the flags give us enough information to do so,
> and since this is the function that sets up the vma_prepare struct it makes
> sense to do so here.
>
> Doing this significantly simplifies commit_merge(), allowing us to
> eliminate the 'merge_target' handling, initialise the VMA iterator in a
> more sensible place and simply return vmg->target consistently.
>
> This also allows us to simplify setting vmg->target in
> vma_merge_existing_range() since we are then left only with two cases -
> merge left (or both) where the target is vmg->prev or merge right in which
> the target is vmg->next.
>
> This makes it easy for somebody reading the code to know what VMA will
> actually be the one returned and merged into and removes a great deal of
> the confusing 'adjust' nonsense.
>
> This patch has no change in functional behaviour.
>
> Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@xxxxxxxxxx>

Reviewed-by: Vlastimil Babka <vbabka@xxxxxxx>


> @@ -906,7 +911,6 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(
> * merged VMA is NOT vmg->target, but rather vmg->next.
> */

Is this comment now also obsolete?

> vmg->merge_flags |= __VMG_FLAG_ADJUST_NEXT_START;
> - vmg->target = middle;
> vmg->start = middle->vm_start;
> vmg->end = start;
> vmg->pgoff = middle->vm_pgoff;
> diff --git a/mm/vma.h b/mm/vma.h
> index ddf567359880..5be43e2bba3f 100644
> --- a/mm/vma.h
> +++ b/mm/vma.h
> @@ -113,11 +113,7 @@ struct vma_merge_struct {
> struct vm_area_struct *prev;
> struct vm_area_struct *middle;
> struct vm_area_struct *next;
> - /*
> - * This is the VMA we ultimately target to become the merged VMA, except
> - * for the one exception of merge right, shrink next (for details of
> - * this scenario see vma_merge_existing_range()).
> - */
> + /* This is the VMA we ultimately target to become the merged VMA. */
> struct vm_area_struct *target;
> /*
> * Initially, the start, end, pgoff fields are provided by the caller