[01/94] mm: Add vma_lookup()
diff mbox series

Message ID 20210428153542.2814175-2-Liam.Howlett@Oracle.com
State New, archived
Headers show
Series
  • Introducing the Maple Tree
Related show

Commit Message

Liam Howlett April 28, 2021, 3:35 p.m. UTC
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().

Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
---
 include/linux/mm.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

Comments

Michel Lespinasse May 1, 2021, 5:04 a.m. UTC | #1
On Wed, Apr 28, 2021 at 03:35:43PM +0000, Liam Howlett wrote:
> 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().
> 
> Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>

This seems like a good API to have, and I agree it's less error prone than
having every caller check the vma->vm_start address.

Minor nitpick, I would prefer if the implementation used find_vma()
and then checked the vma->vm_start address - I don't like using [i, i+1)
intervals to implement stabbing queries.

But other than that, I think this (and the other patches adding
corresponding call sites) is safe for merging.

--
Michel "walken" Lespinasse
Liam Howlett May 3, 2021, 4:08 p.m. UTC | #2
* Michel Lespinasse <michel@lespinasse.org> [210501 01:04]:
> On Wed, Apr 28, 2021 at 03:35:43PM +0000, Liam Howlett wrote:
> > 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().
> > 
> > Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
> 
> This seems like a good API to have, and I agree it's less error prone than
> having every caller check the vma->vm_start address.
> 
> Minor nitpick, I would prefer if the implementation used find_vma()
> and then checked the vma->vm_start address - I don't like using [i, i+1)
> intervals to implement stabbing queries.

Okay, I will make that change.

> 
> But other than that, I think this (and the other patches adding
> corresponding call sites) is safe for merging.
> 

Thanks,
Liam

Patch
diff mbox series

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 25b9041f9925..7f7dff6ad884 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2689,6 +2689,19 @@  static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * m
 	return vma;
 }
 
+/**
+ * vma_lookup() - Find a VMA at a specific address
+ * @mm: The process address space.
+ * @addr: The user address.
+ *
+ * Return: The vm_area_struct at the given address, %NULL otherwise.
+ */
+static inline
+struct vm_area_struct *vma_lookup(struct mm_struct *mm, unsigned long addr)
+{
+       return find_vma_intersection(mm, addr, addr + 1);
+}
+
 static inline unsigned long vm_start_gap(struct vm_area_struct *vma)
 {
 	unsigned long vm_start = vma->vm_start;