diff options
| author | Lorenzo Stoakes <lorenzo.stoakes@oracle.com> | 2025-07-17 17:55:56 +0100 |
|---|---|---|
| committer | Andrew Morton <akpm@linux-foundation.org> | 2025-07-24 19:12:30 -0700 |
| commit | a85dc37186a5842580963496e9718f1ec5bcc0e0 (patch) | |
| tree | 8bec52aefa7fae8cc95bb15ffc0dcca453f62852 /mm | |
| parent | f9f11398d4dac3c85507f31192e318b20b19af61 (diff) | |
mm/mremap: check remap conditions earlier
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.
[lorenzo.stoakes@oracle.com: allow undocumented mremap() shrink behaviour]
Link: https://lkml.kernel.org/r/8fc92a38-c636-465e-9a2f-2c6ac9cb49b8@lucifer.local
Link: https://lkml.kernel.org/r/8b4161ce074901e00602a446d81f182db92b0430.1752770784.git.lorenzo.stoakes@oracle.com
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Jann Horn <jannh@google.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Rik van Riel <riel@surriel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Diffstat (limited to 'mm')
| -rw-r--r-- | mm/mremap.c | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/mm/mremap.c b/mm/mremap.c index db7e773d0884..11a8321a90b8 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -1339,6 +1339,13 @@ static int remap_is_valid(struct vma_remap_struct *vrm) (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))) return -EINVAL; + /* + * We permit crossing of boundaries for the range being unmapped due to + * a shrink. + */ + if (vrm->remap_type == MREMAP_SHRINK) + old_len = new_len; + /* We can't remap across vm area boundaries */ if (old_len > vma->vm_end - addr) return -EFAULT; @@ -1443,10 +1450,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 +1638,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 +1704,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 +1740,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; } |