From 899d9400c9f1e05539861f2fb9e0adf5806af6b9 Mon Sep 17 00:00:00 2001 From: "Liam R. Howlett" Date: Mon, 24 Apr 2023 11:33:47 -0400 Subject: [PATCH] mm: Use vma_iter_next_range() in mmap_region() When checking the surrounding VMAs, the VMA iterator is moved to the previous VMA. If they can merge, then the VMA iterator is in the correct position. If they are not, then the iterator needs to be moved back to the gap that will be filled. Use vma_iter_next_range() to move the iterator to the next range instead of re-walking the VMA tree. Signed-off-by: Liam R. Howlett --- mm/mmap.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index c0140a556870..4c7c0e2c8fab 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2575,8 +2575,10 @@ unsigned long mmap_region(struct file *file, unsigned long addr, next = vma_next(&vmi); prev = vma_prev(&vmi); - if (vm_flags & VM_SPECIAL) + if (vm_flags & VM_SPECIAL) { + vma_iter_next_range(&vmi); goto cannot_expand; + } /* Attempt to expand an old mapping */ /* Check next */ @@ -2597,6 +2599,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr, merge_start = prev->vm_start; vma = prev; vm_pgoff = prev->vm_pgoff; + } else { + vma_iter_next_range(&vmi); } @@ -2607,6 +2611,9 @@ unsigned long mmap_region(struct file *file, unsigned long addr, goto expanded; } + if (prev && prev == vma) /* vma_expand() failed to expand prev */ + vma_iter_next_range(&vmi); + cannot_expand: /* * Determine the object being mapped and call the appropriate @@ -2619,7 +2626,6 @@ cannot_expand: goto unacct_error; } - vma_iter_set(&vmi, addr); vma->vm_start = addr; vma->vm_end = end; vm_flags_init(vma, vm_flags); -- 2.50.1