Skip to content

Commit d5f416c

Browse files
ljskernelakpm00
authored andcommitted
mm/mremap: catch invalid multi VMA moves earlier
Previously, any attempt to solely move a VMA would require that the span specified reside within the span of that single VMA, with no gaps before or afterwards. After commit d23cb64 ("mm/mremap: permit mremap() move of multiple VMAs"), the multi VMA move permitted a gap to exist only after VMAs. This was done to provide maximum flexibility. However, We have consequently permitted this behaviour for the move of a single VMA including those not eligible for multi VMA move. The change introduced here means that we no longer permit non-eligible VMAs from being moved in this way. This is consistent, as it means all eligible VMA moves are treated the same, and all non-eligible moves are treated as they were before. This change does not break previous behaviour, which equally would have disallowed such a move (only in all cases). [lorenzo.stoakes@oracle.com: do not incorrectly reference invalid VMA in VM_WARN_ON_ONCE()] Link: https://lkml.kernel.org/r/b6dbda20-667e-4053-abae-8ed4fa84bb6c@lucifer.local Link: https://lkml.kernel.org/r/2b5aad5681573be85b5b8fac61399af6fb6b68b6.1754218667.git.lorenzo.stoakes@oracle.com Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Reviewed-by: Vlastimil Babka <vbabka@suse.cz> Cc: David Hildenbrand <david@redhat.com> Cc: Jann Horn <jannh@google.com> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Mike Rapoport <rppt@kernel.org> Cc: Suren Baghdasaryan <surenb@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 7c91e0b commit d5f416c

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

mm/mremap.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,10 +1820,11 @@ static unsigned long remap_move(struct vma_remap_struct *vrm)
18201820
unsigned long start = vrm->addr;
18211821
unsigned long end = vrm->addr + vrm->old_len;
18221822
unsigned long new_addr = vrm->new_addr;
1823-
bool allowed = true, seen_vma = false;
18241823
unsigned long target_addr = new_addr;
18251824
unsigned long res = -EFAULT;
18261825
unsigned long last_end;
1826+
bool seen_vma = false;
1827+
18271828
VMA_ITERATOR(vmi, current->mm, start);
18281829

18291830
/*
@@ -1836,9 +1837,7 @@ static unsigned long remap_move(struct vma_remap_struct *vrm)
18361837
unsigned long addr = max(vma->vm_start, start);
18371838
unsigned long len = min(end, vma->vm_end) - addr;
18381839
unsigned long offset, res_vma;
1839-
1840-
if (!allowed)
1841-
return -EFAULT;
1840+
bool multi_allowed;
18421841

18431842
/* No gap permitted at the start of the range. */
18441843
if (!seen_vma && start < vma->vm_start)
@@ -1867,9 +1866,15 @@ static unsigned long remap_move(struct vma_remap_struct *vrm)
18671866
vrm->new_addr = target_addr + offset;
18681867
vrm->old_len = vrm->new_len = len;
18691868

1870-
allowed = vma_multi_allowed(vma);
1871-
if (seen_vma && !allowed)
1872-
return -EFAULT;
1869+
multi_allowed = vma_multi_allowed(vma);
1870+
if (!multi_allowed) {
1871+
/* This is not the first VMA, abort immediately. */
1872+
if (seen_vma)
1873+
return -EFAULT;
1874+
/* This is the first, but there are more, abort. */
1875+
if (vma->vm_end < end)
1876+
return -EFAULT;
1877+
}
18731878

18741879
res_vma = check_prep_vma(vrm);
18751880
if (!res_vma)
@@ -1878,7 +1883,7 @@ static unsigned long remap_move(struct vma_remap_struct *vrm)
18781883
return res_vma;
18791884

18801885
if (!seen_vma) {
1881-
VM_WARN_ON_ONCE(allowed && res_vma != new_addr);
1886+
VM_WARN_ON_ONCE(multi_allowed && res_vma != new_addr);
18821887
res = res_vma;
18831888
}
18841889

0 commit comments

Comments
 (0)