]> www.infradead.org Git - users/willy/linux.git/commitdiff
mm/mmap: Convert count_vma_pages_range() to use ma_state
authorLiam R. Howlett <Liam.Howlett@Oracle.com>
Thu, 27 May 2021 15:23:50 +0000 (11:23 -0400)
committerMatthew Wilcox (Oracle) <willy@infradead.org>
Wed, 20 Oct 2021 20:00:32 +0000 (16:00 -0400)
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@Oracle.com>
mm/mmap.c

index 89a5a52d122269d51c811aff98c8aa5bb00909ee..dabc03cf3b498f5f4bb1d33a90d3fd8209fb6bf4 100644 (file)
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -509,30 +509,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;
 }
 
@@ -2611,7 +2622,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))