Re: [PATCH 12/30] mm/vma: clean up anon_vma_compatible()

Pedro Falcato <[email protected]>
Newsgroups org.kernel.vger.linux-parisc,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-perf-users,org.kernel.vger.linux-sgx,org.kernel.vger.linux-tegra,org.kernel.vger.linux-trace-kernel,org.kvack.linux-mm
Message-ID <[email protected]>
On Mon, Jun 29, 2026 at 01:23:23PM +0100, Lorenzo Stoakes wrote:
> Break up the existing very large conditional, add comments and use
> vma_[start/end]_pgoff() to make clearer what we're doing here.
> 
> No functional change intended.
> 
> Signed-off-by: Lorenzo Stoakes <[email protected]>
> ---
>  mm/vma.c | 21 ++++++++++++++++-----
>  1 file changed, 16 insertions(+), 5 deletions(-)
> 
> diff --git a/mm/vma.c b/mm/vma.c
> index b60375c6c5c3..6296acecf3b7 100644
> --- a/mm/vma.c
> +++ b/mm/vma.c
> @@ -1967,14 +1967,25 @@ static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *
>  {
>  	vma_flags_t diff = vma_flags_diff_pair(&a->flags, &b->flags);
>  
> +	/* Ignore flags that mprotect() can change. */
>  	vma_flags_clear_mask(&diff, VMA_ACCESS_FLAGS);
> +	/* Ignore flags that do not impact merging. */
>  	vma_flags_clear_mask(&diff, VMA_IGNORE_MERGE_FLAGS);
>  
> -	return a->vm_end == b->vm_start &&
> -		mpol_equal(vma_policy(a), vma_policy(b)) &&
> -		a->vm_file == b->vm_file &&
> -		vma_flags_empty(&diff) &&
> -		b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
> +	/* Must be adjacent. */
> +	if (a->vm_end != b->vm_start)
> +		return false;
> +	/* Must have matching policy. */
> +	if (!mpol_equal(vma_policy(a), vma_policy(b)))
> +		return false;
> +	/* Must both be anon or map the same file (MAP_PRIVATE case). */
> +	if (a->vm_file != b->vm_file)
> +		return false;
> +	/* Flags must be equivalent modulo mprotect(). */
> +	if (!vma_flags_empty(&diff))
> +		return false;
> +	/* Page offset must align. */
> +	return vma_end_pgoff(a) == vma_start_pgoff(b);

Very nice.

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

-- 
Pedro
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.