linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Vlastimil Babka <vbabka@suse.cz>
To: "Jakub Matěna" <matenajakub@gmail.com>, akpm@linux-foundation.org
Cc: linux-mm@kvack.org, patches@lists.linux.dev,
	linux-kernel@vger.kernel.org, mhocko@kernel.org,
	mgorman@techsingularity.net, willy@infradead.org,
	liam.howlett@oracle.com, hughd@google.com, kirill@shutemov.name,
	riel@surriel.com, rostedt@goodmis.org, peterz@infradead.org
Subject: Re: [PATCH v4 1/2] [PATCH 1/2] mm: refactor of vma_merge()
Date: Thu, 16 Jun 2022 10:17:40 +0200	[thread overview]
Message-ID: <e4ab8e0e-dfe7-556d-d8f4-f017cf0db59e@suse.cz> (raw)
In-Reply-To: <20220603145719.1012094-2-matenajakub@gmail.com>

On 6/3/22 16:57, Jakub Matěna wrote:
> Refactor vma_merge() to make it shorter and more understandable.
> Main change is the elimination of code duplicity in the case of
> merge next check. This is done by first doing checks and caching
> the results before executing the merge itself. The variable 'area' is
> divided into 'mid' and 'res' as previously it was used for two purposes,
> as the middle VMA between prev and next and also as the result of the
> merge itself. Exit paths are also unified.
> 
> Signed-off-by: Jakub Matěna <matenajakub@gmail.com>

Reviewed-by: Vlastimil Babka <vbabka@suse.cz>

> ---
>  mm/mmap.c | 87 +++++++++++++++++++++++--------------------------------
>  1 file changed, 37 insertions(+), 50 deletions(-)
> 
> diff --git a/mm/mmap.c b/mm/mmap.c
> index 313b57d55a63..91100fdc400a 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -1170,8 +1170,10 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,
>  			struct anon_vma_name *anon_name)
>  {
>  	pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
> -	struct vm_area_struct *area, *next;
> -	int err;
> +	struct vm_area_struct *mid, *next, *res;
> +	int err = -1;
> +	bool merge_prev = false;
> +	bool merge_next = false;
>  
>  	/*
>  	 * We later require that vma->vm_flags == vm_flags,
> @@ -1181,75 +1183,60 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,
>  		return NULL;
>  
>  	next = find_vma(mm, prev ? prev->vm_end : 0);
> -	area = next;
> -	if (area && area->vm_end == end)		/* cases 6, 7, 8 */
> +	mid = next;
> +	if (next && next->vm_end == end)		/* cases 6, 7, 8 */
>  		next = find_vma(mm, next->vm_end);
>  
>  	/* verify some invariant that must be enforced by the caller */
>  	VM_WARN_ON(prev && addr <= prev->vm_start);
> -	VM_WARN_ON(area && end > area->vm_end);
> +	VM_WARN_ON(mid && end > mid->vm_end);
>  	VM_WARN_ON(addr >= end);
>  
> -	/*
> -	 * Can it merge with the predecessor?
> -	 */
> +	/* Can we merge the predecessor? */
>  	if (prev && prev->vm_end == addr &&
>  			mpol_equal(vma_policy(prev), policy) &&
>  			can_vma_merge_after(prev, vm_flags,
>  					    anon_vma, file, pgoff,
>  					    vm_userfaultfd_ctx, anon_name)) {
> -		/*
> -		 * OK, it can.  Can we now merge in the successor as well?
> -		 */
> -		if (next && end == next->vm_start &&
> -				mpol_equal(policy, vma_policy(next)) &&
> -				can_vma_merge_before(next, vm_flags,
> -						     anon_vma, file,
> -						     pgoff+pglen,
> -						     vm_userfaultfd_ctx, anon_name) &&
> -				is_mergeable_anon_vma(prev->anon_vma,
> -						      next->anon_vma, NULL)) {
> -							/* cases 1, 6 */
> -			err = __vma_adjust(prev, prev->vm_start,
> -					 next->vm_end, prev->vm_pgoff, NULL,
> -					 prev);
> -		} else					/* cases 2, 5, 7 */
> -			err = __vma_adjust(prev, prev->vm_start,
> -					 end, prev->vm_pgoff, NULL, prev);
> -		if (err)
> -			return NULL;
> -		khugepaged_enter_vma(prev, vm_flags);
> -		return prev;
> +		merge_prev = true;
>  	}
> -
> -	/*
> -	 * Can this new request be merged in front of next?
> -	 */
> +	/* Can we merge the successor? */
>  	if (next && end == next->vm_start &&
>  			mpol_equal(policy, vma_policy(next)) &&
>  			can_vma_merge_before(next, vm_flags,
>  					     anon_vma, file, pgoff+pglen,
>  					     vm_userfaultfd_ctx, anon_name)) {
> +		merge_next = true;
> +	}
> +	/* Can we merge both the predecessor and the successor? */
> +	if (merge_prev && merge_next &&
> +			is_mergeable_anon_vma(prev->anon_vma,
> +				next->anon_vma, NULL)) {	 /* cases 1, 6 */
> +		err = __vma_adjust(prev, prev->vm_start,
> +					next->vm_end, prev->vm_pgoff, NULL,
> +					prev);
> +		res = prev;
> +	} else if (merge_prev) {			/* cases 2, 5, 7 */
> +		err = __vma_adjust(prev, prev->vm_start,
> +					end, prev->vm_pgoff, NULL, prev);
> +		res = prev;
> +	} else if (merge_next) {
>  		if (prev && addr < prev->vm_end)	/* case 4 */
>  			err = __vma_adjust(prev, prev->vm_start,
> -					 addr, prev->vm_pgoff, NULL, next);
> -		else {					/* cases 3, 8 */
> -			err = __vma_adjust(area, addr, next->vm_end,
> -					 next->vm_pgoff - pglen, NULL, next);
> -			/*
> -			 * In case 3 area is already equal to next and
> -			 * this is a noop, but in case 8 "area" has
> -			 * been removed and next was expanded over it.
> -			 */
> -			area = next;
> -		}
> -		if (err)
> -			return NULL;
> -		khugepaged_enter_vma(area, vm_flags);
> -		return area;
> +					addr, prev->vm_pgoff, NULL, next);
> +		else					/* cases 3, 8 */
> +			err = __vma_adjust(mid, addr, next->vm_end,
> +					next->vm_pgoff - pglen, NULL, next);
> +		res = next;
>  	}
>  
> -	return NULL;
> +	/*
> +	 * Cannot merge with predecessor or successor or error in __vma_adjust?
> +	 */
> +	if (err)
> +		return NULL;
> +	khugepaged_enter_vma(res, vm_flags);
> +	return res;
>  }
>  
>  /*



  reply	other threads:[~2022-06-16  8:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-03 14:57 [PATCH v4 0/2] Refactor of vma_merge and new merge call Jakub Matěna
2022-06-03 14:57 ` [PATCH v4 1/2] [PATCH 1/2] mm: refactor of vma_merge() Jakub Matěna
2022-06-16  8:17   ` Vlastimil Babka [this message]
2022-06-03 14:57 ` [PATCH v4 2/2] [PATCH 2/2] mm: add merging after mremap resize Jakub Matěna
2022-06-16  8:23   ` Vlastimil Babka

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e4ab8e0e-dfe7-556d-d8f4-f017cf0db59e@suse.cz \
    --to=vbabka@suse.cz \
    --cc=akpm@linux-foundation.org \
    --cc=hughd@google.com \
    --cc=kirill@shutemov.name \
    --cc=liam.howlett@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=matenajakub@gmail.com \
    --cc=mgorman@techsingularity.net \
    --cc=mhocko@kernel.org \
    --cc=patches@lists.linux.dev \
    --cc=peterz@infradead.org \
    --cc=riel@surriel.com \
    --cc=rostedt@goodmis.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).