[PATCH v3 21/66] mm/mmap: Convert count_vma_pages_range() to use ma_state

From: Liam Howlett
Date: Mon Oct 04 2021 - 21:33:21 EST


From: "Liam R. Howlett" <Liam.Howlett@xxxxxxxxxx>

mmap_region() uses a maple state to do all the work so convert the
static inline count_vma_pages_range() to use the same structure.

Signed-off-by: Liam R. Howlett <Liam.Howlett@xxxxxxxxxx>
---
mm/mmap.c | 27 +++++++++++++++++++--------
1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/mm/mmap.c b/mm/mmap.c
index 54838406518d..ec3eac307a25 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -500,30 +500,41 @@ static inline struct vm_area_struct *vma_next(struct mm_struct *mm,
return vma->vm_next;
}

-static unsigned long count_vma_pages_range(struct mm_struct *mm,
- unsigned long addr, unsigned long end)
+/*
+ * count_vma_pages_range() - Count the number of pages in a range.
+ * @mas: The maple state
+ *
+ * The start and end address are in the @mas
+ * @mas must be locked.
+ */
+static unsigned long count_vma_pages_range(struct ma_state *mas)
{
unsigned long nr_pages = 0;
struct vm_area_struct *vma;
unsigned long vm_start, vm_end;
- MA_STATE(mas, &mm->mm_mt, addr, addr);
+ unsigned long end = mas->last;
+ unsigned long addr = mas->index;
+ struct ma_state ma_count = *mas;

+ rcu_read_lock();
/* Find first overlapping mapping */
- vma = mas_find(&mas, end - 1);
+ vma = mas_find(&ma_count, end);
if (!vma)
- return 0;
+ goto unlock;

vm_start = vma->vm_start;
vm_end = vma->vm_end;
- nr_pages = (min(end, vm_end) - max(addr, vm_start)) >> PAGE_SHIFT;
+ nr_pages = (min(end + 1, vm_end) - max(addr, vm_start)) >> PAGE_SHIFT;

/* Iterate over the rest of the overlaps */
- mas_for_each(&mas, vma, end) {
+ mas_for_each(&ma_count, vma, end) {
vm_start = vma->vm_start;
vm_end = vma->vm_end;
nr_pages += (min(end, vm_end) - vm_start) >> PAGE_SHIFT;
}

+unlock:
+ rcu_read_unlock();
return nr_pages;
}

@@ -2599,7 +2610,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
* MAP_FIXED may remove pages of mappings that intersects with
* requested mapping. Account for the pages it would unmap.
*/
- nr_pages = count_vma_pages_range(mm, addr, end);
+ nr_pages = count_vma_pages_range(&mas);

if (!may_expand_vm(mm, vm_flags,
(len >> PAGE_SHIFT) - nr_pages))
--
2.30.2