Skip to contentSkip to conversation

mmap: Remove do_brk_munmap() optimization

PATCH New Liam Howlett 1 participant 4 messages Jul 19, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover mmap: Remove do_brk_munmap() optimization

Andrew,

The following is a series of changes to remove the do_brk_munmap() optimization that Hugh flagged as an issue. Please apply patch 1 to (mm/mmap: change do_brk_flags() to expand existing VMA and add do_brk_munmap()) and the last two should replace patches in the v11 set.

2/3 should replace 28/69
3/3 should replace 29/69

The end result should look like this:

static int do_brk_munmap(struct ma_state *mas, struct vm_area_struct *vma,
unsigned long newbrk, unsigned long oldbrk, struct list_head *uf)

{       
        struct mm_struct *mm = vma->vm_mm;                                                                          
        int ret;                                                                                                    
        
        arch_unmap(mm, newbrk, oldbrk);
        ret = do_mas_align_munmap(mas, vma, mm, newbrk, oldbrk, uf, true);                                          
        validate_mm_mt(mm);                                                                                         
        return ret;                                                                                                 
}

Thanks,
Liam

#1 lore raw

PATCH 1/3 mmap: Remove do_brk_munmap() optimization

This optimization does not work with the locking.

Fixes f12fee814843 (mm/mmap: change do_brk_flags() to expand existing VMA and add do_brk_munmap())
---
 mm/mmap.c | 61 ++-----------------------------------------------------
 1 file changed, 2 insertions(+), 59 deletions(-)
mm/mmap.c +2 -59
index 2f19976ea817..4fc1b3098c91 100644
@@ -2861,67 +2861,10 @@ static int do_brk_munmap(struct ma_state *mas, struct vm_area_struct *vma,
 			 struct list_head *uf)
 {
 	struct mm_struct *mm = vma->vm_mm;
-	struct vm_area_struct unmap;
-	unsigned long unmap_pages;
-	int ret = 1;
+	int ret;
 
 	arch_unmap(mm, newbrk, oldbrk);
-
-	if (likely((vma->vm_end < oldbrk) ||
-		   ((vma->vm_start == newbrk) && (vma->vm_end == oldbrk)))) {
-		/* remove entire mapping(s) */
-		mas_set(mas, newbrk);
-		if (vma->vm_start != newbrk)
-			mas_reset(mas); /* cause a re-walk for the first overlap. */
-		ret = __do_munmap(mm, newbrk, oldbrk - newbrk, uf, true);
-		goto munmap_full_vma;
-	}
-
-	vma_init(&unmap, mm);
-	unmap.vm_start = newbrk;
-	unmap.vm_end = oldbrk;
-	ret = userfaultfd_unmap_prep(&unmap, newbrk, oldbrk, uf);
-	if (ret)
-		return ret;
-	ret = 1;
-
-	/* Change the oldbrk of vma to the newbrk of the munmap area */
-	vma_adjust_trans_huge(vma, vma->vm_start, newbrk, 0);
-	if (mas_preallocate(mas, vma, GFP_KERNEL))
-		return -ENOMEM;
-
-	if (vma->anon_vma) {
-		anon_vma_lock_write(vma->anon_vma);
-		anon_vma_interval_tree_pre_update_vma(vma);
-	}
-
-	vma->vm_end = newbrk;
-	vma_init(&unmap, mm);
-	unmap.vm_start = newbrk;
-	unmap.vm_end = oldbrk;
-	if (vma->anon_vma)
-		vma_set_anonymous(&unmap);
-
-	vma_mas_remove(&unmap, mas);
-
-	vmacache_invalidate(vma->vm_mm);
-	if (vma->anon_vma) {
-		anon_vma_interval_tree_post_update_vma(vma);
-		anon_vma_unlock_write(vma->anon_vma);
-	}
-
-	unmap_pages = vma_pages(&unmap);
-	if (vma->vm_flags & VM_LOCKED)
-		mm->locked_vm -= unmap_pages;
-
-	mmap_write_downgrade(mm);
-	unmap_region(mm, &unmap, vma, newbrk, oldbrk);
-	/* Statistics */
-	vm_stat_account(mm, vma->vm_flags, -unmap_pages);
-	if (vma->vm_flags & VM_ACCOUNT)
-		vm_unacct_memory(unmap_pages);
-
-munmap_full_vma:
+	ret = __do_munmap(mm, newbrk, oldbrk - newbrk, uf, true);
 	validate_mm_mt(mm);
 	return ret;
 }
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/3 mm/mmap: reorganize munmap to use maple states

In reply to Liam Howlett

From: "Liam R. Howlett" <[email protected]>

Remove __do_munmap() in favour of do_munmap(), do_mas_munmap(), and do_mas_align_munmap().

do_munmap() is a wrapper to create a maple state for any callers that have not been converted to the maple tree.

do_mas_munmap() takes a maple state to mumap a range. This is just a small function which checks for error conditions and aligns the end of the range.

do_mas_align_munmap() uses the aligned range to mumap a range. do_mas_align_munmap() starts with the first VMA in the range, then finds the last VMA in the range. Both start and end are split if necessary. Then the VMAs are removed from the linked list and the mm mlock count is updated at the same time. Followed by a single tree operation of overwriting the area in with a NULL. Finally, the detached list is unmapped and freed.

By reorganizing the munmap calls as outlined, it is now possible to avoid extra work of aligning pre-aligned callers which are known to be safe, avoid extra VMA lookups or tree walks for modifications.

detach_vmas_to_be_unmapped() is no longer used, so drop this code.

vm_brk_flags() can just call the do_mas_munmap() as it checks for intersecting VMAs directly.

---
 include/linux/mm.h |   5 +-
 mm/mmap.c          | 228 ++++++++++++++++++++++++++++-----------------
 mm/mremap.c        |  17 ++--
 3 files changed, 158 insertions(+), 92 deletions(-)
include/linux/mm.h +3 -2
index a250fd86fde9..75ac5664af69 100644
@@ -2724,8 +2724,9 @@ extern unsigned long mmap_region(struct file *file, unsigned long addr,
 extern unsigned long do_mmap(struct file *file, unsigned long addr,
 	unsigned long len, unsigned long prot, unsigned long flags,
 	unsigned long pgoff, unsigned long *populate, struct list_head *uf);
-extern int __do_munmap(struct mm_struct *, unsigned long, size_t,
-		       struct list_head *uf, bool downgrade);
+extern int do_mas_munmap(struct ma_state *mas, struct mm_struct *mm,
+			 unsigned long start, size_t len, struct list_head *uf,
+			 bool downgrade);
 extern int do_munmap(struct mm_struct *, unsigned long, size_t,
 		     struct list_head *uf);
 extern int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior);
mm/mmap.c +145 -83

313 lines not on this page · raw message

mm/mremap.c +10 -7
index b522cd0259a0..e0fba9004246 100644
@@ -975,20 +975,23 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
 	/*
 	 * Always allow a shrinking remap: that just unmaps
 	 * the unnecessary pages..
-	 * __do_munmap does all the needed commit accounting, and
+	 * do_mas_munmap does all the needed commit accounting, and
 	 * downgrades mmap_lock to read if so directed.
 	 */
 	if (old_len >= new_len) {
 		int retval;
+		MA_STATE(mas, &mm->mm_mt, addr + new_len, addr + new_len);
 
-		retval = __do_munmap(mm, addr+new_len, old_len - new_len,
-				  &uf_unmap, true);
-		if (retval < 0 && old_len != new_len) {
-			ret = retval;
-			goto out;
+		retval = do_mas_munmap(&mas, mm, addr + new_len,
+				       old_len - new_len, &uf_unmap, true);
 		/* Returning 1 indicates mmap_lock is downgraded to read. */
-		} else if (retval == 1)
+		if (retval == 1) {
 			downgraded = true;
+		} else if (retval < 0 && old_len != new_len) {
+			ret = retval;
+			goto out;
+		}
+
 		ret = addr;
 		goto out;
 	}
Signature · 1 line
2.35.1
#3 lore raw

PATCH 3/3 mm/mmap: change do_brk_munmap() to use

In reply to Liam Howlett

From: "Liam R. Howlett" <[email protected]>

do_brk_munmap() has already aligned the address and has a maple tree state to be used. Use the new do_mas_align_munmap() to avoid unnecessary alignment and error checks.

---
 mm/mmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/mmap.c +1 -1
index ddd5040ecc9f..10ea2f146d4f 100644
@@ -3085,7 +3085,7 @@ static int do_brk_munmap(struct ma_state *mas, struct vm_area_struct *vma,
 	int ret;
 
 	arch_unmap(mm, newbrk, oldbrk);
-	ret = do_mas_munmap(mas, mm, newbrk, oldbrk-newbrk, uf, true);
+	ret = do_mas_align_munmap(mas, vma, mm, newbrk, oldbrk, uf, true);
 	validate_mm_mt(mm);
 	return ret;
 }
Signature · 1 line
2.35.1