mm: implement mremap(2) - #1412
Closed
gburd wants to merge 1 commit into
Closed
Conversation
mremap() was missing entirely on OSv: it was not in the syscall table and had no libc entry point, so applications and allocators (glibc/musl realloc of mmap-backed chunks calls the internal __mremap) that resize an mmap region failed. Add mmu::mremap() in core/mmu.cc, built on the existing VMA machinery and the public self-locking map_anon()/map_file()/munmap() primitives. It handles a single, wholly mapped source vma (Linux requires the old range to fall in one mapping too; a range spanning several vmas or a hole yields EFAULT): - shrink / no-op: drop the tail in place, address unchanged; - grow in place when the region immediately after the source is free; - grow with MREMAP_MAYMOVE: allocate a new region of the new size, migrate the contents (anon: copy; file-backed: remap the file at the same offset so the grown tail is served by the fault path with no copy), then unmap the old range. The source vma is inspected once under vma_list_mutex to snapshot its perm/flags/type/file/offset and whether its tail is free, so the rest of the function can call the self-locking primitives without nesting the non-recursive vma write lock. MREMAP_FIXED is not supported yet and returns EINVAL rather than doing the wrong thing; the anon and file-backed paths cover the callers we have. Wire it as syscall SYS_mremap (via long_mremap, mirroring long_mmap since the return value is a pointer) with a matching tracepoint, add the libc mremap()/__mremap() entry points, and export both symbols from libc.so.6 and ld-musl.so.1. Add tests/tst-mremap.cc covering grow, shrink, in-place-blocked (ENOMEM without MREMAP_MAYMOVE), file-backed move, and the EINVAL/EFAULT error paths. Passes on OSv under KVM and on native Linux (semantics match). tst-mmap continues to pass (no regression in the VMA path).
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Implements
mremap(2), which was missing entirely on OSv: it was not in thesyscall table and had no libc entry point, so applications and allocators that
resize an mmap region failed (glibc/musl
reallocof an mmap-backed chunk callsthe internal
__mremap).How
mmu::mremap()(core/mmu.cc) is built on the existing VMA machinery and thepublic, self-locking
map_anon()/map_file()/munmap()primitives. It handlesa single, wholly mapped source vma (Linux likewise requires the old range to
fall within one mapping; a range spanning several vmas or a hole yields
EFAULT):MREMAP_MAYMOVE: allocate a new region of the new size, migratethe contents (anonymous: copy; file-backed: remap the file at the same offset
so the grown tail is served by the fault path with no copy), then unmap the old
range.
The source vma is inspected once under
vma_list_mutexto snapshot itsperm/flags/type/file/offset and whether its tail is free, so the rest of the
function can call the self-locking primitives without nesting the non-recursive
vma write lock.
MREMAP_FIXEDis not supported yet and returnsEINVALrather than doing thewrong thing; the anonymous and file-backed paths cover the callers we have.
Wired as syscall
SYS_mremap(vialong_mremap, mirroringlong_mmapsince thereturn value is a pointer) with a matching tracepoint, plus the libc
mremap()/__mremap()entry points, and both symbols exported fromlibc.so.6andld-musl.so.1.Testing
tests/tst-mremap.cccovers grow, shrink, in-place-blocked (ENOMEMwithoutMREMAP_MAYMOVE), file-backed move, and theEINVAL/EFAULTerror paths. Itpasses on OSv under KVM and on native Linux (semantics match).
tst-mmapcontinues to pass, so there is no regression in the VMA path.