Skip to content

Commit d014cd7

Browse files
tehcasterakpm00
authored andcommitted
mm, mremap: fix mremap() expanding for vma's with vm_ops->close()
Fabian has reported another regression in 6.1 due to ca3d76b ("mm: add merging after mremap resize"). The problem is that vma_merge() can fail when vma has a vm_ops->close() method, causing is_mergeable_vma() test to be negative. This was happening for vma mapping a file from fuse-overlayfs, which does have the method. But when we are simply expanding the vma, we never remove it due to the "merge" with the added area, so the test should not prevent the expansion. As a quick fix, check for such vmas and expand them using vma_adjust() directly as was done before commit ca3d76b. For a more robust long term solution we should try to limit the check for vma_ops->close only to cases that actually result in vma removal, so that no merge would be prevented unnecessarily. [[email protected]: fix indenting whitespace, reflow comment] Link: https://lkml.kernel.org/r/[email protected] Fixes: ca3d76b ("mm: add merging after mremap resize") Signed-off-by: Vlastimil Babka <[email protected]> Reported-by: Fabian Vogt <[email protected]> Link: https://bugzilla.suse.com/show_bug.cgi?id=1206359#c35 Tested-by: Fabian Vogt <[email protected]> Cc: Jakub Matěna <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 72e544b commit d014cd7

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

mm/mremap.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,16 +1027,29 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
10271027
}
10281028

10291029
/*
1030-
* Function vma_merge() is called on the extension we are adding to
1031-
* the already existing vma, vma_merge() will merge this extension with
1032-
* the already existing vma (expand operation itself) and possibly also
1033-
* with the next vma if it becomes adjacent to the expanded vma and
1034-
* otherwise compatible.
1030+
* Function vma_merge() is called on the extension we
1031+
* are adding to the already existing vma, vma_merge()
1032+
* will merge this extension with the already existing
1033+
* vma (expand operation itself) and possibly also with
1034+
* the next vma if it becomes adjacent to the expanded
1035+
* vma and otherwise compatible.
1036+
*
1037+
* However, vma_merge() can currently fail due to
1038+
* is_mergeable_vma() check for vm_ops->close (see the
1039+
* comment there). Yet this should not prevent vma
1040+
* expanding, so perform a simple expand for such vma.
1041+
* Ideally the check for close op should be only done
1042+
* when a vma would be actually removed due to a merge.
10351043
*/
1036-
vma = vma_merge(mm, vma, extension_start, extension_end,
1044+
if (!vma->vm_ops || !vma->vm_ops->close) {
1045+
vma = vma_merge(mm, vma, extension_start, extension_end,
10371046
vma->vm_flags, vma->anon_vma, vma->vm_file,
10381047
extension_pgoff, vma_policy(vma),
10391048
vma->vm_userfaultfd_ctx, anon_vma_name(vma));
1049+
} else if (vma_adjust(vma, vma->vm_start, addr + new_len,
1050+
vma->vm_pgoff, NULL)) {
1051+
vma = NULL;
1052+
}
10401053
if (!vma) {
10411054
vm_unacct_memory(pages);
10421055
ret = -ENOMEM;

0 commit comments

Comments
 (0)