Skip to content

Commit 6b73cff

Browse files
howlettakpm00
authored andcommitted
mm: change munmap splitting order and move_vma()
Splitting can be more efficient when the order is not of concern. Change do_vmi_align_munmap() to reduce walking of the tree during split operations. move_vma() must also be altered to remove the dependency of keeping the original VMA as the active part of the split. Transition to using vma iterator to look up the prev and/or next vma after munmap. [Liam.Howlett@oracle.com: fix vma iterator initialization] Link: https://lkml.kernel.org/r/20230126212011.980350-1-Liam.Howlett@oracle.com Link: https://lkml.kernel.org/r/20230120162650.984577-39-Liam.Howlett@oracle.com Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent cc8d1b0 commit 6b73cff

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

mm/mmap.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,21 +2329,9 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,
23292329
for_each_vma_range(*vmi, next, end) {
23302330
/* Does it split the end? */
23312331
if (next->vm_end > end) {
2332-
struct vm_area_struct *split;
2333-
2334-
error = __split_vma(vmi, next, end, 1);
2332+
error = __split_vma(vmi, next, end, 0);
23352333
if (error)
23362334
goto end_split_failed;
2337-
2338-
split = vma_prev(vmi);
2339-
error = munmap_sidetree(split, &mas_detach);
2340-
if (error)
2341-
goto munmap_sidetree_failed;
2342-
2343-
count++;
2344-
if (vma == next)
2345-
vma = split;
2346-
break;
23472335
}
23482336
error = munmap_sidetree(next, &mas_detach);
23492337
if (error)
@@ -2356,9 +2344,7 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,
23562344
#endif
23572345
}
23582346

2359-
if (!next)
2360-
next = vma_next(vmi);
2361-
2347+
next = vma_next(vmi);
23622348
if (unlikely(uf)) {
23632349
/*
23642350
* If userfaultfd_unmap_prep returns an error the vmas

mm/mremap.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,12 @@ static unsigned long move_vma(struct vm_area_struct *vma,
580580
unsigned long vm_flags = vma->vm_flags;
581581
unsigned long new_pgoff;
582582
unsigned long moved_len;
583-
unsigned long excess = 0;
583+
unsigned long account_start = 0;
584+
unsigned long account_end = 0;
584585
unsigned long hiwater_vm;
585-
int split = 0;
586586
int err = 0;
587587
bool need_rmap_locks;
588+
struct vma_iterator vmi;
588589

589590
/*
590591
* We'd prefer to avoid failure later on in do_munmap:
@@ -662,10 +663,10 @@ static unsigned long move_vma(struct vm_area_struct *vma,
662663
/* Conceal VM_ACCOUNT so old reservation is not undone */
663664
if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP)) {
664665
vma->vm_flags &= ~VM_ACCOUNT;
665-
excess = vma->vm_end - vma->vm_start - old_len;
666-
if (old_addr > vma->vm_start &&
667-
old_addr + old_len < vma->vm_end)
668-
split = 1;
666+
if (vma->vm_start < old_addr)
667+
account_start = vma->vm_start;
668+
if (vma->vm_end > old_addr + old_len)
669+
account_end = vma->vm_end;
669670
}
670671

671672
/*
@@ -700,11 +701,12 @@ static unsigned long move_vma(struct vm_area_struct *vma,
700701
return new_addr;
701702
}
702703

703-
if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) {
704+
vma_iter_init(&vmi, mm, old_addr);
705+
if (do_vmi_munmap(&vmi, mm, old_addr, old_len, uf_unmap, false) < 0) {
704706
/* OOM: unable to split vma, just get accounts right */
705707
if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP))
706708
vm_acct_memory(old_len >> PAGE_SHIFT);
707-
excess = 0;
709+
account_start = account_end = 0;
708710
}
709711

710712
if (vm_flags & VM_LOCKED) {
@@ -715,10 +717,14 @@ static unsigned long move_vma(struct vm_area_struct *vma,
715717
mm->hiwater_vm = hiwater_vm;
716718

717719
/* Restore VM_ACCOUNT if one or two pieces of vma left */
718-
if (excess) {
720+
if (account_start) {
721+
vma = vma_prev(&vmi);
722+
vma->vm_flags |= VM_ACCOUNT;
723+
}
724+
725+
if (account_end) {
726+
vma = vma_next(&vmi);
719727
vma->vm_flags |= VM_ACCOUNT;
720-
if (split)
721-
find_vma(mm, vma->vm_end)->vm_flags |= VM_ACCOUNT;
722728
}
723729

724730
return new_addr;

0 commit comments

Comments
 (0)