Repository navigation
Conversation
6fa418c to
bdc3050
Compare
| case linux.MADV_FREE: | ||
| // Linux accepts MADV_FREE on private anonymous mappings. Pages may | ||
| // remain until reclaim; gVisor has no lazy reclaim path, so Decommit | ||
| // frees them immediately, which MADV_FREE allows. | ||
| return 0, nil, t.MemoryManager().DecommitMadvFree(addr, length) |
There was a problem hiding this comment.
MADV_FREE is supposed to be async and asks the kernel to lazily free the range, while MADV_DONTNEED is sync and asks the kernel to free pages immediately. I don't think we should symlink MADV_FREE into MADV_DONTNEED like this.
There was a problem hiding this comment.
It's not an illegal change. In fact, Linux itself supported MADV_FREE like this on swapless systems (or systems where swap was full) between 4.5 and 4.11 (2016-2017):
- introduced in https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=854e9ed09dedf0c19ac8640e91bcc74bc3f9e5c9
- removed fallback to MADV_DONTNEED in https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=93e06c7a645343d222c9a838834a51042eebbbf7
The contract of MADV_FREE is that the kernel can lazily reclaim, but can reclaim at any time.
Bug-for-bug implementation would require a notion of memory pressure in gVisor, with memory.max set, and things that gVisor isn't tracking today. Meanwhile, this is kinda cheap and easy to implement the feature.
There was a problem hiding this comment.
I agree that this is a valid implementation of MADV_FREE. I have an old implementation of MADV_FREE that used pgalloc evictable ranges to delay MADV_FREE, but it was measurably slower than synchronous MADV_FREE (what this commit does) or leaving it unimplemented.
| case linux.MADV_FREE: | ||
| // Linux accepts MADV_FREE on private anonymous mappings. Pages may | ||
| // remain until reclaim; gVisor has no lazy reclaim path, so Decommit | ||
| // frees them immediately, which MADV_FREE allows. |
There was a problem hiding this comment.
Nothing in this comment is relevant to the code here, it's just staleness bait. Delete it.
| if vma.mlockMode != memmap.MLockNone { | ||
| return linuxerr.EINVAL | ||
| } | ||
| if madvFree && (vma.mappable != nil || !vma.private) { |
There was a problem hiding this comment.
Shared anonymous mappings have a non-nil mappable, so I think only the vma.mappable != nil check is required.
There was a problem hiding this comment.
Right, shared anon gets a tmpfs zero file as its mappable, so vma.mappable != nil covers it. Dropped the !vma.private check; RejectsSharedAnonMapping still passes with only the mappable check. Added the madvise_free_single_vma() / vma_is_anonymous() cite next to that EINVAL.
| // DecommitMadvFree implements madvise(MADV_FREE): same decommit as | ||
| // MADV_DONTNEED, but only for private anonymous mappings. Linux returns EINVAL | ||
| // for file-backed and shared mappings (mm/madvise.c:madvise_dontneed_free). | ||
| func (mm *MemoryManager) DecommitMadvFree(addr hostarch.Addr, length uint64) error { |
There was a problem hiding this comment.
Name this method MadviseFree and only describe it as implementing madvise(MADV_FREE). If you want to document that this currently decommits memory synchronously, do so in the function body rather than the function comment since it's not contractual (we could change this transparently in the future). If you want to document that the behavior on file-backed and shared mappings is consistent with Linux, it would be better to do so where EINVAL is actually returned in MM.decommit() (and the correct function to cite is mm/madvise.c:madvise_free_single_vma(), which actually checks vma_is_anonymous().)
There was a problem hiding this comment.
Done: renamed to MadviseFree. The doc comment now just says it implements madvise(MADV_FREE), and the note that it currently decommits synchronously is in the body.
| case linux.MADV_FREE: | ||
| // Linux accepts MADV_FREE on private anonymous mappings. Pages may | ||
| // remain until reclaim; gVisor has no lazy reclaim path, so Decommit | ||
| // frees them immediately, which MADV_FREE allows. | ||
| return 0, nil, t.MemoryManager().DecommitMadvFree(addr, length) |
There was a problem hiding this comment.
I agree that this is a valid implementation of MADV_FREE. I have an old implementation of MADV_FREE that used pgalloc evictable ranges to delay MADV_FREE, but it was measurably slower than synchronous MADV_FREE (what this commit does) or leaving it unimplemented.
|
Tried to look for regressions here, I'm manually going over pgalloc PRs that may have a perf impact since we don't have good benchmark tests. Claude found some regressions here, I rewrote and simplified the below.
|
|
@EtiennePerot @nixprime I see Etienne's benchmark, but also Jamie's note that delayed free's were slower. runsc appears to not consume host memory pressure signals (loader.go#L1240 (https://github.com/google/gvisor/blob/3fdb673dc0a300a9a975ce3f5d356f285687c1e9/runsc/boot/loader.go#L1240)), so DelayedEviction evicts as soon as the releaser goroutine is idle. A MarkEvictable-based MADV_FREE would probably usually evict before the app reuses the page, so it pays the same refault as synchronous decommit, plus the write-protect and mprotect overhead. That would fit Jamie's result that his lazy version was slower. Thoughts? |
bdc3050 to
b8bddb3
Compare
|
I went with the synchronous for now still... but we can switch gears if y'all want. |
|
ping @EtiennePerot and @nixprime |
b8bddb3 to
494b1c4
Compare
|
@nixprime @EtiennePerot friendly ping on this: v2 (synchronous 🤖 Posted by an AI agent (Cursor · skill |
be23ab1 to
d07036f
Compare
madvise(MADV_FREE) returns EINVAL for every mapping. Allocators such as jemalloc and Ruby's fiber pool use MADV_FREE to tell the kernel that freed pages may be discarded; on EINVAL they fall back to a slower purge path or keep the memory. Accept MADV_FREE on private anonymous mappings and decommit the range synchronously, as for MADV_DONTNEED. MADV_FREE allows the kernel to free the pages at any time, and Linux 4.5 to 4.11 did the same on systems without swap. Data written after the call is kept: it lands on a fresh zero-filled page. Linux fidelity: mm/madvise.c:madvise_free_single_vma() returns EINVAL for any vma that is not vma_is_anonymous(), so file-backed and shared anonymous mappings return EINVAL; madvise_dontneed_free_valid_vma() returns EINVAL for locked mappings, which the existing MADV_DONTNEED check already covers. Divergence: pages are freed at the call instead of at memory-pressure reclaim, so their old contents read as zeros right away and RSS drops immediately; smaps has no LazyFree accounting. Tests: MadviseFreeTest.AcceptsPrivateAnonPage fails with EINVAL without the change and passes with it. RejectsPrivateFileMapping and RejectsSharedAnonMapping fail if the anonymous-mapping check is removed. madvise_test passes on native, runsc_ptrace and runsc_systrap_directfs, each with _save and _save_resume; //pkg/sentry/mm:mm_test passes. Assisted-by: Cortex Code Signed-off-by: Chris Koch <[email protected]>
d07036f to
d203932
Compare
shayonj
left a comment
There was a problem hiding this comment.
claude on behalf of shayon
I took a look but remain conflicted. Perhaps someone else with more exposure can chime in.
| switch adv { | ||
| case linux.MADV_DONTNEED: | ||
| return 0, nil, t.MemoryManager().Decommit(addr, length) | ||
| case linux.MADV_FREE: |
There was a problem hiding this comment.
I took it for a spin and noticed fs:rubydev goes from 28.0s to 29.9s on systrap and from 32.3s to 40.8s on KVM, rebased on current master. I wonder if freeing at the call can be cheap enough here, since app memory is a host memfd and the host can’t free shmem lazily. I also tried keeping the pmas and only punching the memfd, so reuse is a host fault instead of a sentry fault, but rubydev still came in at +6% on systrap and +25% on KVM.
| // +checklocksexclude:mm.activeMu | ||
| func (mm *MemoryManager) MadviseFree(addr hostarch.Addr, length uint64) error { | ||
| // MADV_FREE permits the kernel to free the pages at any time, so this | ||
| // currently decommits them synchronously, as for MADV_DONTNEED. |
There was a problem hiding this comment.
Looking at the callers, I’m wondering if leaving the pages in place would be the better shape. Ruby’s fiber pool and musl’s mallocng ignore the result, so they already keep these pages on master, while Go with madvdontneed=0 and jemalloc’s boot probe act on it. With pages kept, rubydev was 28.4s on systrap and 32.2s on KVM, and jemalloc kept the same gain. The cost is that callers that fall back to MADV_DONTNEED today lose that reclaim under a memory limit.
| // | ||
| // +checklocksexclude:mm.mappingMu | ||
| // +checklocksexclude:mm.activeMu | ||
| func (mm *MemoryManager) MadviseFree(addr hostarch.Addr, length uint64) error { |
There was a problem hiding this comment.
I noticed that dropping the pma lets a pinned range, such as one from nvproxy or vfio, diverge from the app after MADV_FREE, because the app refaults onto a new page. Linux skips pages that have extra references, so they stay shared.
| case linux.MADV_DONTNEED: | ||
| return 0, nil, t.MemoryManager().Decommit(addr, length) | ||
| case linux.MADV_FREE: | ||
| return 0, nil, t.MemoryManager().MadviseFree(addr, length) |
There was a problem hiding this comment.
Small one: the madvise entries in linux64.go don’t mention MADV_FREE yet.
Summary
madvise(MADV_FREE)returnsEINVALfor every mapping. Allocators such as jemalloc and Ruby's fiber pool use it to tell the kernel that freed pages may be discarded, and onEINVALthey fall back to a slower purge path or keep the memory. This change acceptsMADV_FREEon private anonymous mappings and frees the pages at the call, asMADV_DONTNEEDdoes. That is within theMADV_FREEcontract (the kernel may free the pages at any time), and Linux 4.5 to 4.11 did the same on systems without swap.Linux Fidelity
mm/madvise.c:madvise_free_single_vma():EINVALunlessvma_is_anonymous(), so file-backed and shared anonymous mappings returnEINVAL.mm/madvise.c:madvise_dontneed_free_valid_vma():EINVALfor locked mappings (same check asMADV_DONTNEED).Divergences:
LazyFreeaccounting in/proc/[pid]/smaps.