Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm: use maple tree operations for find_vma_intersection()

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.

Exported find_vma_intersection() for kvm module.

Link: https://lkml.kernel.org/r/20220906194824.2110408-24-Liam.Howlett@oracle.com
Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
Tested-by: Yu Zhao <yuzhao@google.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: David Howells <dhowells@redhat.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
Cc: SeongJae Park <sj@kernel.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Liam R. Howlett and committed by
Andrew Morton
abdba2dd 2e7ce7d3

+44 -18
+4 -18
include/linux/mm.h
··· 2778 2778 extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr, 2779 2779 struct vm_area_struct **pprev); 2780 2780 2781 - /** 2782 - * find_vma_intersection() - Look up the first VMA which intersects the interval 2783 - * @mm: The process address space. 2784 - * @start_addr: The inclusive start user address. 2785 - * @end_addr: The exclusive end user address. 2786 - * 2787 - * Returns: The first VMA within the provided range, %NULL otherwise. Assumes 2788 - * start_addr < end_addr. 2781 + /* 2782 + * Look up the first VMA which intersects the interval [start_addr, end_addr) 2783 + * NULL if none. Assume start_addr < end_addr. 2789 2784 */ 2790 - static inline 2791 2785 struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, 2792 - unsigned long start_addr, 2793 - unsigned long end_addr) 2794 - { 2795 - struct vm_area_struct *vma = find_vma(mm, start_addr); 2796 - 2797 - if (vma && end_addr <= vma->vm_start) 2798 - vma = NULL; 2799 - return vma; 2800 - } 2786 + unsigned long start_addr, unsigned long end_addr); 2801 2787 2802 2788 /** 2803 2789 * vma_lookup() - Find a VMA at a specific address
+29
mm/mmap.c
··· 2062 2062 EXPORT_SYMBOL(get_unmapped_area); 2063 2063 2064 2064 /** 2065 + * find_vma_intersection() - Look up the first VMA which intersects the interval 2066 + * @mm: The process address space. 2067 + * @start_addr: The inclusive start user address. 2068 + * @end_addr: The exclusive end user address. 2069 + * 2070 + * Returns: The first VMA within the provided range, %NULL otherwise. Assumes 2071 + * start_addr < end_addr. 2072 + */ 2073 + struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, 2074 + unsigned long start_addr, 2075 + unsigned long end_addr) 2076 + { 2077 + struct vm_area_struct *vma; 2078 + unsigned long index = start_addr; 2079 + 2080 + mmap_assert_locked(mm); 2081 + /* Check the cache first. */ 2082 + vma = vmacache_find(mm, start_addr); 2083 + if (likely(vma)) 2084 + return vma; 2085 + 2086 + vma = mt_find(&mm->mm_mt, &index, end_addr - 1); 2087 + if (vma) 2088 + vmacache_update(start_addr, vma); 2089 + return vma; 2090 + } 2091 + EXPORT_SYMBOL(find_vma_intersection); 2092 + 2093 + /** 2065 2094 * find_vma() - Find the VMA for a given address, or the next VMA. 2066 2095 * @mm: The mm_struct to check 2067 2096 * @addr: The address
+11
mm/nommu.c
··· 642 642 vm_area_free(vma); 643 643 } 644 644 645 + struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, 646 + unsigned long start_addr, 647 + unsigned long end_addr) 648 + { 649 + unsigned long index = start_addr; 650 + 651 + mmap_assert_locked(mm); 652 + return mt_find(&mm->mm_mt, &index, end_addr - 1); 653 + } 654 + EXPORT_SYMBOL(find_vma_intersection); 655 + 645 656 /* 646 657 * look up the first VMA in which addr resides, NULL if none 647 658 * - should be called with mm->mmap_lock at least held readlocked