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>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
---
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;
}
--
2.50.1
Hi Andrew, It turns out there's some undocumented, unusual behaviour in mremap() around shrinking of a range which was previously missed, but an LTP test flagged up (seemingly by accident). Basically, if you specify an input range that spans multiple VMAs, this is in nearly all cases rejected (this is the point of this series, after all, for VMA moves). However, it turns out if you a. shrink a range and b. the new size spans only a single VMA in the original range - then this requirement is entirely dropped. So I need to slightly adjust the logic to account for this. I will also be documenting this in the man page as it appears the man page contradicts this or is at least very unclear. I attach a fix-patch, however there's some very trivial conflicts caused due to code being moved around. If you'd therefore prefer me to send a respin, I can do so. This doesn't reflect on the series itself, which with the corner-case VMA iterator stuff sorted is fine, but is rather just an undocumented and unusual behaviour that it seems very few were aware of. With all other tests passing this series should be fine with this fix applied. I've run all self-tests and the LTP tests against this. Cheers, Lorenzo [0]: https://lore.kernel.org/all/202507201002.69144b74-lkp@intel.com/ ----8<---- From 23b95070152b22f7432c4a9da9e4b5718f9d115f Mon Sep 17 00:00:00 2001 From: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Date: Sun, 20 Jul 2025 11:41:48 +0100 Subject: [PATCH] mm/mremap: allow undocumented mremap() shrink behaviour It turns out that, in apparent contradiction to the man page, and at odds with every other mremap() operation - we are allowed to specify an input addr, old_len range that spans any number of VMAs and any number of gaps, as long as we shrink that range to the point at which the new range spans only one. In order to accommodate this, adjust the remap validity check to account for this. Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Reported-by: kernel test robot <oliver.sang@intel.com> Closes: https://lore.kernel.org/oe-lkp/202507201002.69144b74-lkp@intel.com --- mm/mremap.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/mremap.c b/mm/mremap.c index 20844fb91755..11a8321a90b8 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -1339,11 +1339,18 @@ 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; - if (new_len <= old_len) + if (new_len == old_len) return 0; /* Need to be careful about a growing mapping */ -- 2.50.1
On Sun, 20 Jul 2025 12:04:42 +0100 Lorenzo Stoakes <lorenzo.stoakes@oracle.com> wrote: > Hi Andrew, > > It turns out there's some undocumented, unusual behaviour in mremap() > around shrinking of a range which was previously missed, but an LTP test > flagged up (seemingly by accident). > > Basically, if you specify an input range that spans multiple VMAs, this is > in nearly all cases rejected (this is the point of this series, after all, > for VMA moves). > > However, it turns out if you a. shrink a range and b. the new size spans > only a single VMA in the original range - then this requirement is entirely > dropped. > > So I need to slightly adjust the logic to account for this. I will also be > documenting this in the man page as it appears the man page contradicts > this or is at least very unclear. > > I attach a fix-patch, however there's some very trivial conflicts caused > due to code being moved around. > OK, I applied this as a -fix. Moved the two new hunks into check_prep_vma(). Made sure the "We are expanding and the VMA .." hunk landed properly in check_prep_vma(). I've pushed out the result, please check current mm-unstable.
On Sun, Jul 20, 2025 at 11:29:14AM -0700, Andrew Morton wrote: > On Sun, 20 Jul 2025 12:04:42 +0100 Lorenzo Stoakes <lorenzo.stoakes@oracle.com> wrote: > > > Hi Andrew, > > > > It turns out there's some undocumented, unusual behaviour in mremap() > > around shrinking of a range which was previously missed, but an LTP test > > flagged up (seemingly by accident). > > > > Basically, if you specify an input range that spans multiple VMAs, this is > > in nearly all cases rejected (this is the point of this series, after all, > > for VMA moves). > > > > However, it turns out if you a. shrink a range and b. the new size spans > > only a single VMA in the original range - then this requirement is entirely > > dropped. > > > > So I need to slightly adjust the logic to account for this. I will also be > > documenting this in the man page as it appears the man page contradicts > > this or is at least very unclear. > > > > I attach a fix-patch, however there's some very trivial conflicts caused > > due to code being moved around. > > > > OK, I applied this as a -fix. > > Moved the two new hunks into check_prep_vma(). > > Made sure the "We are expanding and the VMA .." hunk landed properly in > check_prep_vma(). > > I've pushed out the result, please check current mm-unstable. Thanks, all looks good to me!
© 2016 - 2025 Red Hat, Inc.