mm/mremap: account mm->locked_vm correctly for MREMAP_DONTUNMAP

When a VMA is mremap()'d with MREMAP_DONTUNMAP set, that results in the
VMA being copied, but the source VMA not being unmapped.

If the VMA is mlock()'d this is a legal operation, though the source VMA
has its VMA_LOCKED_BIT cleared.

However this is done in dontunmap_complete(), after mm->locked_vm was
incremented via vrm_stat_account(), resulting in double-counting.

Worse, this is not even corrected when source VMA is unmapped, due to the
VMA_LOCKED_BIT flag having been cleared.

This all works fine in the usual mremap() case (without MREMAP_DONTUNMAP),
as the source VMA is unmapped with VMA_LOCKED_BIT intact, at which time
mm->locked_vm is decremented accordingly.

Resolve the issue by invoking vrm_stat_account() only after
dontunmap_complete() has run.

Note that MREMAP_DONTUNMAP requires old_len == new_len, so no need to
account for a delta in size in this case.

The bug was introduced by commit b714ccb02a ("mm/mremap: complete
refactor of move_vma()") which incorrectly reordered the accounting and
the clearing of the VMA_LOCKED_BIT flag.

Link: https://lore.kernel.org/[email protected]
Fixes: b714ccb02a ("mm/mremap: complete refactor of move_vma()")
Signed-off-by: Lorenzo Stoakes (ARM) <[email protected]>
Reported-by: sashiko-bot <[email protected]>
Closes: https://sashiko.dev/#/patchset/[email protected]
Reported-by: Kunwu Chan <[email protected]>
Closes: https://lore.kernel.org/all/[email protected]/
Acked-by: Vlastimil Babka (SUSE) <[email protected]>
Tested-by: Kunwu Chan <[email protected]>
Reviewed-by: Kunwu Chan <[email protected]>
Cc: Jann Horn <[email protected]>
Cc: Liam R. Howlett <[email protected]>
Cc: Pedro Falcato <[email protected]>
Cc: <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
This commit is contained in:
Lorenzo Stoakes (ARM) authored and Andrew Morton committed 2026-09-05 11:47:47 -07:00
1 parent e384abeb55
commit 397432cab1
1 file changed
+4 -5
+4 -5
View File
@@ -1355,12 +1355,11 @@ static void dontunmap_complete(struct vma_remap_struct *vrm,
if (vma_is_anonymous(vma) && !vma->vm_file)
vma_set_pgoff(vma, pgoff_unfaulted);
}
/* Because we won't unmap we don't need to touch locked_vm. */
}
static unsigned long move_vma(struct vma_remap_struct *vrm)
{
const bool is_dontunmap = vrm->flags & MREMAP_DONTUNMAP;
struct mm_struct *mm = current->mm;
struct vm_area_struct *new_vma;
unsigned long hiwater_vm;
@@ -1401,10 +1400,10 @@ static unsigned long move_vma(struct vma_remap_struct *vrm)
*/
hiwater_vm = mm->hiwater_vm;
vrm_stat_account(vrm, vrm->new_len);
if (unlikely(!err && (vrm->flags & MREMAP_DONTUNMAP)))
if (unlikely(is_dontunmap && !err))
dontunmap_complete(vrm, new_vma);
else
vrm_stat_account(vrm, vrm->new_len);
if (!is_dontunmap || err)
unmap_source_vma(vrm);
mm->hiwater_vm = hiwater_vm;