Skip to content

Commit ce6d42f

Browse files
howletttorvalds
authored andcommitted
mm: add vma_lookup(), update find_vma_intersection() comments
Patch series "mm: Add vma_lookup()", v2. Many places in the kernel use find_vma() to get a vma and then check the start address of the vma to ensure the next vma was not returned. Other places use the find_vma_intersection() call with add, addr + 1 as the range; looking for just the vma at a specific address. The third use of find_vma() is by developers who do not know that the function starts searching at the provided address upwards for the next vma. This results in a bug that is often overlooked for a long time. Adding the new vma_lookup() function will allow for cleaner code by removing the find_vma() calls which check limits, making find_vma_intersection() calls of a single address to be shorter, and potentially reduce the incorrect uses of find_vma(). This patch (of 22): Many places in the kernel use find_vma() to get a vma and then check the start address of the vma to ensure the next vma was not returned. Other places use the find_vma_intersection() call with add, addr + 1 as the range; looking for just the vma at a specific address. The third use of find_vma() is by developers who do not know that the function starts searching at the provided address upwards for the next vma. This results in a bug that is often overlooked for a long time. Adding the new vma_lookup() function will allow for cleaner code by removing the find_vma() calls which check limits, making find_vma_intersection() calls of a single address to be shorter, and potentially reduce the incorrect uses of find_vma(). Also change find_vma_intersection() comments and declaration to be of the correct length and add kernel documentation style comment. Link: https://lkml.kernel.org/r/20210521174745.2219620-1-Liam.Howlett@Oracle.com Link: https://lkml.kernel.org/r/20210521174745.2219620-2-Liam.Howlett@Oracle.com Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com> Reviewed-by: Laurent Dufour <ldufour@linux.ibm.com> Acked-by: David Hildenbrand <david@redhat.com> Acked-by: Davidlohr Bueso <dbueso@suse.de> Cc: David Miller <davem@davemloft.net> Cc: Geert Uytterhoeven <geert@linux-m68k.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 2797e79 commit ce6d42f

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

include/linux/mm.h

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2676,17 +2676,45 @@ extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long add
26762676
extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
26772677
struct vm_area_struct **pprev);
26782678

2679-
/* Look up the first VMA which intersects the interval start_addr..end_addr-1,
2680-
NULL if none. Assume start_addr < end_addr. */
2681-
static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * mm, unsigned long start_addr, unsigned long end_addr)
2679+
/**
2680+
* find_vma_intersection() - Look up the first VMA which intersects the interval
2681+
* @mm: The process address space.
2682+
* @start_addr: The inclusive start user address.
2683+
* @end_addr: The exclusive end user address.
2684+
*
2685+
* Returns: The first VMA within the provided range, %NULL otherwise. Assumes
2686+
* start_addr < end_addr.
2687+
*/
2688+
static inline
2689+
struct vm_area_struct *find_vma_intersection(struct mm_struct *mm,
2690+
unsigned long start_addr,
2691+
unsigned long end_addr)
26822692
{
2683-
struct vm_area_struct * vma = find_vma(mm,start_addr);
2693+
struct vm_area_struct *vma = find_vma(mm, start_addr);
26842694

26852695
if (vma && end_addr <= vma->vm_start)
26862696
vma = NULL;
26872697
return vma;
26882698
}
26892699

2700+
/**
2701+
* vma_lookup() - Find a VMA at a specific address
2702+
* @mm: The process address space.
2703+
* @addr: The user address.
2704+
*
2705+
* Return: The vm_area_struct at the given address, %NULL otherwise.
2706+
*/
2707+
static inline
2708+
struct vm_area_struct *vma_lookup(struct mm_struct *mm, unsigned long addr)
2709+
{
2710+
struct vm_area_struct *vma = find_vma(mm, addr);
2711+
2712+
if (vma && addr < vma->vm_start)
2713+
vma = NULL;
2714+
2715+
return vma;
2716+
}
2717+
26902718
static inline unsigned long vm_start_gap(struct vm_area_struct *vma)
26912719
{
26922720
unsigned long vm_start = vma->vm_start;

0 commit comments

Comments
 (0)