From b2fa64cf1e88ac16b263fffaedc9c1c89f14bc1b Mon Sep 17 00:00:00 2001
From: "Liam R. Howlett" <Liam.Howlett@oracle.com>
Date: Fri, 21 Apr 2023 11:08:22 -0400
Subject: [PATCH] mm: Avoid rewalk in mmap_region

Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
---
 mm/mmap.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/mm/mmap.c b/mm/mmap.c
index 36fb185e91b7..06e033b3cc62 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -2645,9 +2645,13 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 		goto expanded;
 	}
 
+cannot_expand:
+	if (prev)
+		mas_next_range(&mas, ULONG_MAX);
+	BUG_ON(mas.last < addr);
+	BUG_ON(mas.index > end - 1);
 	mas.index = addr;
 	mas.last = end - 1;
-cannot_expand:
 	/*
 	 * Determine the object being mapped and call the appropriate
 	 * specific mapper. the address has already been validated, but
@@ -2744,7 +2748,7 @@ cannot_expand:
 	if (vma->vm_file)
 		i_mmap_lock_write(vma->vm_file->f_mapping);
 
-	vma_mas_store(vma, &mas);
+	mas_store_prealloc(&mas, vma);
 	mm->map_count++;
 	if (vma->vm_file) {
 		if (vma->vm_flags & VM_SHARED)
-- 
2.50.1