When we expand or move a VMA, this requires a number of additional checks to be performed.
Make it really obvious under what circumstances these checks must be performed and aggregate all the checks in one place by invoking this in check_prep_vma().
We have to adjust the checks to account for shrink + move operations by checking new_len <= old_len rather than new_len == old_len.
No functional change intended.
Signed-off-by: Lorenzo Stoakes lorenzo.stoakes@oracle.com --- mm/mremap.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)
diff --git a/mm/mremap.c b/mm/mremap.c index db7e773d0884..20844fb91755 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -1343,7 +1343,7 @@ static int remap_is_valid(struct vma_remap_struct *vrm) if (old_len > vma->vm_end - addr) return -EFAULT;
- if (new_len == old_len) + if (new_len <= old_len) return 0;
/* Need to be careful about a growing mapping */ @@ -1443,10 +1443,6 @@ static unsigned long mremap_to(struct vma_remap_struct *vrm) vrm->old_len = vrm->new_len; }
- err = remap_is_valid(vrm); - if (err) - return err; - /* MREMAP_DONTUNMAP expands by old_len since old_len == new_len */ if (vrm->flags & MREMAP_DONTUNMAP) { vm_flags_t vm_flags = vrm->vma->vm_flags; @@ -1635,10 +1631,6 @@ static unsigned long expand_vma(struct vma_remap_struct *vrm) { unsigned long err;
- err = remap_is_valid(vrm); - if (err) - return err; - /* * [addr, old_len) spans precisely to the end of the VMA, so try to * expand it in-place. @@ -1705,6 +1697,21 @@ static unsigned long mremap_at(struct vma_remap_struct *vrm) return -EINVAL; }
+/* + * Will this operation result in the VMA being expanded or moved and thus need + * to map a new portion of virtual address space? + */ +static bool vrm_will_map_new(struct vma_remap_struct *vrm) +{ + if (vrm->remap_type == MREMAP_EXPAND) + return true; + + if (vrm_implies_new_addr(vrm)) + return true; + + return false; +} + static int check_prep_vma(struct vma_remap_struct *vrm) { struct vm_area_struct *vma = vrm->vma; @@ -1726,6 +1733,9 @@ static int check_prep_vma(struct vma_remap_struct *vrm) if (!vrm_implies_new_addr(vrm)) vrm->new_addr = vrm->addr;
+ if (vrm_will_map_new(vrm)) + return remap_is_valid(vrm); + return 0; }