[PATCH v4 22/66] mm: Use maple tree operations for find_vma_intersection() and find_vma()

Vlastimil Babka vbabka at suse.cz
Thu Jan 13 07:53:42 PST 2022


On 12/1/21 15:29, Liam Howlett wrote:
> From: "Liam R. Howlett" <Liam.Howlett at Oracle.com>
> 
> Move find_vma_intersection() to mmap.c and change implementation to
> maple tree.
> 
> When searching for a vma within a range, it is easier to use the maple
> tree interface.  This means the find_vma() call changes to a special
> case of the find_vma_intersection().
> 
> Exported for kvm module.
> 
> Signed-off-by: Liam R. Howlett <Liam.Howlett at Oracle.com>
> ---
>  include/linux/mm.h | 25 ++++++-------------------
>  mm/mmap.c          | 38 ++++++++++++++++++++++++++++----------
>  2 files changed, 34 insertions(+), 29 deletions(-)
> 
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index c08f6d541b8a..39a947f29f6d 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -2812,26 +2812,13 @@ extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long add
>  extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
>  					     struct vm_area_struct **pprev);
>  
> -/**
> - * find_vma_intersection() - Look up the first VMA which intersects the interval
> - * @mm: The process address space.
> - * @start_addr: The inclusive start user address.
> - * @end_addr: The exclusive end user address.
> - *
> - * Returns: The first VMA within the provided range, %NULL otherwise.  Assumes
> - * start_addr < end_addr.
> +/*
> + * Look up the first VMA which intersects the interval [start_addr, end_addr)
> + * NULL if none.  Assume start_addr < end_addr.
>   */
> -static inline
> -struct vm_area_struct *find_vma_intersection(struct mm_struct *mm,
> -					     unsigned long start_addr,
> -					     unsigned long end_addr)
> -{
> -	struct vm_area_struct *vma = find_vma(mm, start_addr);
> -
> -	if (vma && end_addr <= vma->vm_start)
> -		vma = NULL;
> -	return vma;
> -}
> +extern struct vm_area_struct *find_vma_intersection(struct mm_struct *mm,
> +						    unsigned long start_addr,
> +						    unsigned long end_addr);
>  
>  /**
>   * vma_lookup() - Find a VMA at a specific address
> diff --git a/mm/mmap.c b/mm/mmap.c
> index e4c8ce377f2c..c06c5b850e1e 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -2003,29 +2003,47 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
>  EXPORT_SYMBOL(get_unmapped_area);
>  
>  /**
> - * find_vma() - Find the VMA for a given address, or the next vma.
> - * @mm: The mm_struct to check
> - * @addr: The address
> + * find_vma_intersection() - Look up the first VMA which intersects the interval
> + * @mm: The process address space.
> + * @start_addr: The inclusive start user address.
> + * @end_addr: The exclusive end user address.
>   *
> - * Returns: The VMA associated with addr, or the next vma.
> - * May return %NULL in the case of no vma at addr or above.
> + * Returns: The first VMA within the provided range, %NULL otherwise.  Assumes
> + * start_addr < end_addr.
>   */
> -struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
> +struct vm_area_struct *find_vma_intersection(struct mm_struct *mm,
> +					     unsigned long start_addr,
> +					     unsigned long end_addr)
>  {
>  	struct vm_area_struct *vma;
> -	MA_STATE(mas, &mm->mm_mt, addr, addr);
> +	MA_STATE(mas, &mm->mm_mt, start_addr, start_addr);
>  
>  	mmap_assert_locked(mm);
>  	/* Check the cache first. */
> -	vma = vmacache_find(mm, addr);
> +	vma = vmacache_find(mm, start_addr);
>  	if (likely(vma))
>  		return vma;
>  
> -	vma = mas_find(&mas, -1);
> +	vma = mas_find(&mas, end_addr - 1);
>  	if (vma)
> -		vmacache_update(addr, vma);
> +		vmacache_update(mas.index, vma);
>  	return vma;
>  }
> +EXPORT_SYMBOL(find_vma_intersection);
> +
> +/**
> + * find_vma() - Find the VMA for a given address, or the next vma.
> + * @mm:  The mm_struct to check
> + * @addr: The address
> + *
> + * Returns: The VMA associated with addr, or the next vma.
> + * May return NULL in the case of no vma at addr or above.
> + */
> +inline struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)

I think the inline does nothing here (except maybe for LTO builds). Callers
will do a function call to find_vma() and then probably that causes another
function call (likely a tail call) to find_vma_intersection().
You'd have to put this definition of find_vma() to include/linux/mm.h to
make the callers inline it and just add the 0 parameter before doing a
function call of find_vma_intersection() - which would make sense.

> +{
> +	// Note find_vma_intersection will decrease 0 to underflow to ULONG_MAX

Nit: this comment syntax using //.

> +	return find_vma_intersection(mm, addr, 0);
> +}
>  EXPORT_SYMBOL(find_vma);
>  
>  /**




More information about the maple-tree mailing list