diff mbox series

[v10,09/69] mm: add VMA iterator

Message ID 20220621204632.3370049-10-Liam.Howlett@oracle.com (mailing list archive)
State New
Headers show
Series Introducing the Maple Tree | expand

Commit Message

Liam R. Howlett June 21, 2022, 8:46 p.m. UTC
From: "Matthew Wilcox (Oracle)" <willy@infradead.org>

This thin layer of abstraction over the maple tree state is for iterating
over VMAs.  You can go forwards, go backwards or ask where the iterator
is.  Rename the existing vma_next() to __vma_next() -- it will be removed
by the end of this series.

Link: https://lkml.kernel.org/r/20220504010716.661115-11-Liam.Howlett@oracle.com
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: David Howells <dhowells@redhat.com>
Cc: SeongJae Park <sj@kernel.org>
Cc: Will Deacon <will@kernel.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---
 include/linux/mm.h       | 31 +++++++++++++++++++++++++++++++
 include/linux/mm_types.h | 21 +++++++++++++++++++++
 mm/mmap.c                | 10 +++++-----
 3 files changed, 57 insertions(+), 5 deletions(-)

Comments

David Hildenbrand June 21, 2022, 9:10 p.m. UTC | #1
On 21.06.22 22:46, Liam Howlett wrote:
> From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
> 
> This thin layer of abstraction over the maple tree state is for iterating
> over VMAs.  You can go forwards, go backwards or ask where the iterator
> is.  Rename the existing vma_next() to __vma_next() -- it will be removed
> by the end of this series.
> 
> Link: https://lkml.kernel.org/r/20220504010716.661115-11-Liam.Howlett@oracle.com
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
> Acked-by: Vlastimil Babka <vbabka@suse.cz>
> Cc: Catalin Marinas <catalin.marinas@arm.com>
> Cc: David Howells <dhowells@redhat.com>
> Cc: SeongJae Park <sj@kernel.org>
> Cc: Will Deacon <will@kernel.org>
> Cc: Davidlohr Bueso <dave@stgolabs.net>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> ---
>  include/linux/mm.h       | 31 +++++++++++++++++++++++++++++++
>  include/linux/mm_types.h | 21 +++++++++++++++++++++
>  mm/mmap.c                | 10 +++++-----
>  3 files changed, 57 insertions(+), 5 deletions(-)
> 
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 810b3dd929e4..f22c6f71a18c 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -658,6 +658,37 @@ static inline bool vma_is_accessible(struct vm_area_struct *vma)
>  	return vma->vm_flags & VM_ACCESS_FLAGS;
>  }
>  
> +static inline
> +struct vm_area_struct *vma_find(struct vma_iterator *vmi, unsigned long max)
> +{
> +	return mas_find(&vmi->mas, max);
> +}
> +
> +static inline struct vm_area_struct *vma_next(struct vma_iterator *vmi)
> +{
> +	/*
> +	 * Uses vma_find() to get the first VMA when the iterator starts.
> +	 * Calling mas_next() could skip the first entry.
> +	 */
> +	return vma_find(vmi, ULONG_MAX);
> +}
> +
> +static inline struct vm_area_struct *vma_prev(struct vma_iterator *vmi)
> +{
> +	return mas_prev(&vmi->mas, 0);
> +}
> +
> +static inline unsigned long vma_iter_addr(struct vma_iterator *vmi)
> +{
> +	return vmi->mas.index;
> +}
> +
> +#define for_each_vma(vmi, vma)		while ((vma = vma_next(&(vmi))) != NULL)
> +
> +/* The MM code likes to work with exclusive end addresses */
> +#define for_each_vma_range(vmi, vma, end)				\
> +	while ((vma = vma_find(&(vmi), (end) - 1)) != NULL)
> +
>  #ifdef CONFIG_SHMEM
>  /*
>   * The vma_is_shmem is not inline because it is used only by slow
> diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
> index 254c30def2b2..1485a24796be 100644
> --- a/include/linux/mm_types.h
> +++ b/include/linux/mm_types.h
> @@ -696,6 +696,27 @@ static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
>  	return (struct cpumask *)&mm->cpu_bitmap;
>  }
>  
> +struct vma_iterator {
> +	struct ma_state mas;
> +};
> +
> +#define VMA_ITERATOR(name, mm, addr) 					\
> +	struct vma_iterator name = {					\
> +		.mas = {						\
> +			.tree = &mm->mm_mt,				\
> +			.index = addr,					\
> +			.node = MAS_START,				\
> +		},							\
> +	}
> +

No __* and () macro magic?

I'd have expected at least

tree = &(__mm)->mm_mt,
.index = (__addr),

;)


Reviewed-by: David Hildenbrand <david@redhat.com>
Matthew Wilcox June 23, 2022, 5:03 p.m. UTC | #2
On Tue, Jun 21, 2022 at 11:10:09PM +0200, David Hildenbrand wrote:
> > +#define VMA_ITERATOR(name, mm, addr) 					\
> > +	struct vma_iterator name = {					\
> > +		.mas = {						\
> > +			.tree = &mm->mm_mt,				\
> > +			.index = addr,					\
> > +			.node = MAS_START,				\
> > +		},							\
> > +	}
> > +
> 
> No __* and () macro magic?
> 
> I'd have expected at least
> 
> tree = &(__mm)->mm_mt,
> .index = (__addr),
> 
> ;)

Fair, fair.  Just testing that change now.  I always forget to do those
things until they bite me.

> Reviewed-by: David Hildenbrand <david@redhat.com>

Appreciate the review!

diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index acbd8d03e01e..8bcbffefdc02 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -686,11 +686,11 @@ struct vma_iterator {
 	struct ma_state mas;
 };
 
-#define VMA_ITERATOR(name, mm, addr) 					\
+#define VMA_ITERATOR(name, _mm, _addr)	 				\
 	struct vma_iterator name = {					\
 		.mas = {						\
-			.tree = &mm->mm_mt,				\
-			.index = addr,					\
+			.tree = &(_mm)->mm_mt,				\
+			.index = (_addr),				\
 			.node = MAS_START,				\
 		},							\
 	}
Liam R. Howlett June 24, 2022, 2:31 p.m. UTC | #3
* Matthew Wilcox <willy@infradead.org> [220623 13:03]:
> On Tue, Jun 21, 2022 at 11:10:09PM +0200, David Hildenbrand wrote:
> > > +#define VMA_ITERATOR(name, mm, addr) 					\
> > > +	struct vma_iterator name = {					\
> > > +		.mas = {						\
> > > +			.tree = &mm->mm_mt,				\
> > > +			.index = addr,					\
> > > +			.node = MAS_START,				\
> > > +		},							\
> > > +	}
> > > +
> > 
> > No __* and () macro magic?
> > 
> > I'd have expected at least
> > 
> > tree = &(__mm)->mm_mt,
> > .index = (__addr),
> > 
> > ;)
> 
> Fair, fair.  Just testing that change now.  I always forget to do those
> things until they bite me.
> 
> > Reviewed-by: David Hildenbrand <david@redhat.com>
> 
> Appreciate the review!


I'll also do the same underscores for the maple tree defines where
necessary.

Thanks David.

> 
> diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
> index acbd8d03e01e..8bcbffefdc02 100644
> --- a/include/linux/mm_types.h
> +++ b/include/linux/mm_types.h
> @@ -686,11 +686,11 @@ struct vma_iterator {
>  	struct ma_state mas;
>  };
>  
> -#define VMA_ITERATOR(name, mm, addr) 					\
> +#define VMA_ITERATOR(name, _mm, _addr)	 				\
>  	struct vma_iterator name = {					\
>  		.mas = {						\
> -			.tree = &mm->mm_mt,				\
> -			.index = addr,					\
> +			.tree = &(_mm)->mm_mt,				\
> +			.index = (_addr),				\
>  			.node = MAS_START,				\
>  		},							\
>  	}
diff mbox series

Patch

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 810b3dd929e4..f22c6f71a18c 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -658,6 +658,37 @@  static inline bool vma_is_accessible(struct vm_area_struct *vma)
 	return vma->vm_flags & VM_ACCESS_FLAGS;
 }
 
+static inline
+struct vm_area_struct *vma_find(struct vma_iterator *vmi, unsigned long max)
+{
+	return mas_find(&vmi->mas, max);
+}
+
+static inline struct vm_area_struct *vma_next(struct vma_iterator *vmi)
+{
+	/*
+	 * Uses vma_find() to get the first VMA when the iterator starts.
+	 * Calling mas_next() could skip the first entry.
+	 */
+	return vma_find(vmi, ULONG_MAX);
+}
+
+static inline struct vm_area_struct *vma_prev(struct vma_iterator *vmi)
+{
+	return mas_prev(&vmi->mas, 0);
+}
+
+static inline unsigned long vma_iter_addr(struct vma_iterator *vmi)
+{
+	return vmi->mas.index;
+}
+
+#define for_each_vma(vmi, vma)		while ((vma = vma_next(&(vmi))) != NULL)
+
+/* The MM code likes to work with exclusive end addresses */
+#define for_each_vma_range(vmi, vma, end)				\
+	while ((vma = vma_find(&(vmi), (end) - 1)) != NULL)
+
 #ifdef CONFIG_SHMEM
 /*
  * The vma_is_shmem is not inline because it is used only by slow
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 254c30def2b2..1485a24796be 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -696,6 +696,27 @@  static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
 	return (struct cpumask *)&mm->cpu_bitmap;
 }
 
+struct vma_iterator {
+	struct ma_state mas;
+};
+
+#define VMA_ITERATOR(name, mm, addr) 					\
+	struct vma_iterator name = {					\
+		.mas = {						\
+			.tree = &mm->mm_mt,				\
+			.index = addr,					\
+			.node = MAS_START,				\
+		},							\
+	}
+
+static inline void vma_iter_init(struct vma_iterator *vmi,
+		struct mm_struct *mm, unsigned long addr)
+{
+	vmi->mas.tree = &mm->mm_mt;
+	vmi->mas.index = addr;
+	vmi->mas.node = MAS_START;
+}
+
 struct mmu_gather;
 extern void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm);
 extern void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm);
diff --git a/mm/mmap.c b/mm/mmap.c
index 9c75bd3b78ba..6be7833c781b 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -632,7 +632,7 @@  static int find_vma_links(struct mm_struct *mm, unsigned long addr,
 }
 
 /*
- * vma_next() - Get the next VMA.
+ * __vma_next() - Get the next VMA.
  * @mm: The mm_struct.
  * @vma: The current vma.
  *
@@ -640,7 +640,7 @@  static int find_vma_links(struct mm_struct *mm, unsigned long addr,
  *
  * Returns: The next VMA after @vma.
  */
-static inline struct vm_area_struct *vma_next(struct mm_struct *mm,
+static inline struct vm_area_struct *__vma_next(struct mm_struct *mm,
 					 struct vm_area_struct *vma)
 {
 	if (!vma)
@@ -1333,7 +1333,7 @@  struct vm_area_struct *vma_merge(struct mm_struct *mm,
 	if (vm_flags & VM_SPECIAL)
 		return NULL;
 
-	next = vma_next(mm, prev);
+	next = __vma_next(mm, prev);
 	area = next;
 	if (area && area->vm_end == end)		/* cases 6, 7, 8 */
 		next = next->vm_next;
@@ -2881,7 +2881,7 @@  static void unmap_region(struct mm_struct *mm,
 		struct vm_area_struct *vma, struct vm_area_struct *prev,
 		unsigned long start, unsigned long end)
 {
-	struct vm_area_struct *next = vma_next(mm, prev);
+	struct vm_area_struct *next = __vma_next(mm, prev);
 	struct mmu_gather tlb;
 
 	lru_add_drain();
@@ -3086,7 +3086,7 @@  int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,
 		if (error)
 			goto split_failed;
 	}
-	vma = vma_next(mm, prev);
+	vma = __vma_next(mm, prev);
 
 	if (unlikely(uf)) {
 		/*