fix(memory): use fallocate(PUNCH_HOLE) for discard_range on MAP_SHARED regions - #6168
Open
AdaAibaby wants to merge 1 commit into
Open
fix(memory): use fallocate(PUNCH_HOLE) for discard_range on MAP_SHARED regions#6168AdaAibaby wants to merge 1 commit into
AdaAibaby wants to merge 1 commit into
Conversation
…D regions madvise(MADV_DONTNEED) has no effect on MAP_SHARED file-backed mappings (such as memfd regions used for huge-pages guest memory): the kernel ignores it for shared pages because other mappers may still be using them. As a result, balloon inflation never actually freed host physical frames when huge pages were enabled. Add a dedicated match arm in GuestRegionMmapExt::discard_range() for MAP_SHARED mappings that uses fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE) instead. This punches a hole in the backing file, causing the kernel to release the physical frames while keeping the virtual mapping intact; subsequent guest accesses fault in fresh zero pages, matching the expected balloon-inflation semantics. The existing MAP_PRIVATE (snapshot restore) and anonymous arms are unchanged. Add test_discard_range_on_memfd to verify the new path actually zeroes the discarded page. Fixes firecracker-microvm#6167
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.
Summary
GuestRegionMmapExt::discard_range()was callingmadvise(MADV_DONTNEED)for all non-private-file-backed mappings. For anonymous regions this works
correctly, but for
MAP_SHAREDmemfd-backed regions (used when huge pagesare enabled) the kernel ignores
MADV_DONTNEEDfor shared pages and returns0 — so balloon inflation never freed any host physical memory.
This PR adds a dedicated match arm for
MAP_SHAREDmappings that callsfallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)instead, whichpunches a hole in the backing memfd file and releases the physical frames.
Subsequent guest accesses fault in fresh zero pages, matching the expected
balloon-inflation semantics.
Changes
src/vmm/src/vstate/memory.rs: addMAP_SHAREDarm toGuestRegionMmapExt::discard_range()usingfallocate(PUNCH_HOLE)test_discard_range_on_memfdthat writes data, callsdiscard_range,and asserts the discarded page reads back as zeroes
Testing
The new test
test_discard_range_on_memfdcovers the happy path (page iszeroed after discard), the second page is unaffected, out-of-range errors,
and the unaligned-offset error path.
Fixes #6167
Hi @Manciukic @JackThomson2 @JamesC1305 Would you mind taking a look when you have a chance? Thanks!