Re: [PATCH 28/30] mm/vma: use guard clauses in can_vma_merge_[before, after]()

Pedro Falcato <[email protected]> Thu, 2 Jul 2026 12:41:51 +0100
Newsgroups org.kernel.vger.linux-sgx,dev.linux.lists.damon,dev.linux.lists.iommu,dev.linux.lists.nvdimm,org.freedesktop.lists.dri-devel,org.kernel.vger.kvm,org.kernel.vger.linux-arm-msm,org.kernel.vger.linux-fsdevel,org.kernel.vger.linux-kernel,org.kernel.vger.linux-parisc,org.kernel.vger.linux-perf-users,org.kernel.vger.linux-tegra,org.kernel.vger.linux-trace-kernel
Message-ID <[email protected]>
On Mon, Jun 29, 2026 at 01:23:39PM +0100, Lorenzo Stoakes wrote:
> Rather than combining a bunch of conditionals in a single expression,
> simplify by inverting the mergeability requirements into guard clauses.
> 
> that is - instead of checking what must be true for the conditions to be
> met, instead check the inverse of the requirements and return false if any
> are true, defaulting to true.
> 
> No functional change intended.
> 
> Signed-off-by: Lorenzo Stoakes <[email protected]>
> ---
>  mm/vma.c | 27 ++++++++++++++-------------
>  1 file changed, 14 insertions(+), 13 deletions(-)
> 
> diff --git a/mm/vma.c b/mm/vma.c
> index 5c3062e0e706..7201199fc668 100644
> --- a/mm/vma.c
> +++ b/mm/vma.c
> @@ -215,13 +215,13 @@ static void init_multi_vma_prep(struct vma_prepare *vp,
>   */
>  static bool can_vma_merge_before(struct vma_merge_struct *vmg)
>  {
> -	if (is_mergeable_vma(vmg, /* merge_next = */ true) &&
> -	    is_mergeable_anon_vma(vmg, /* merge_next = */ true)) {
> -		if (vmg_end_pgoff(vmg) == vma_start_pgoff(vmg->next))
> -			return true;
> -	}
> -
> -	return false;
> +	if (!is_mergeable_vma(vmg, /* merge_next = */ true))
> +		return false;
> +	if (!is_mergeable_anon_vma(vmg, /* merge_next = */ true))
> +		return false;
> +	if (vmg_end_pgoff(vmg) != vma_start_pgoff(vmg->next))
> +		return false;
> +	return true;
>  }
>  
>  /*
> @@ -235,12 +235,13 @@ static bool can_vma_merge_before(struct vma_merge_struct *vmg)
>   */
>  static bool can_vma_merge_after(struct vma_merge_struct *vmg)
>  {
> -	if (is_mergeable_vma(vmg, /* merge_next = */ false) &&
> -	    is_mergeable_anon_vma(vmg, /* merge_next = */ false)) {
> -		if (vma_end_pgoff(vmg->prev) == vmg_start_pgoff(vmg))
> -			return true;
> -	}
> -	return false;
> +	if (!is_mergeable_vma(vmg, /* merge_next = */ false))
> +		return false;
> +	if (!is_mergeable_anon_vma(vmg, /* merge_next = */ false))
> +		return false;
> +	if (vma_end_pgoff(vmg->prev) != vmg_start_pgoff(vmg))
> +		return false;
> +	return true;
>  }
>  
>  static void __vma_link_file(struct vm_area_struct *vma,

Looks nicer, thanks.

Reviewed-by: Pedro Falcato <[email protected]>

-- 
Pedro