Repository navigation
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
I found a similar finding with autoresearch, but it found that the version in this PR causes a regression for
Here's the vibecoded patch it came up with: master...EtiennePerot:gvisor:autoresearch/decommit-cancels-unloaded |
d678b49 to
966e200
Compare
|
Thanks @EtiennePerot, I agree with the cancel-instead-of-await approach. I replaced my implementation with your two commits ( I checked it in two ways:
This matches your numbers: no stale pages, and |
02881d1 to
77bd7bf
Compare
manninglucas
left a comment
There was a problem hiding this comment.
Thanks, the production change looks correct. Cancelling unstarted loads under apfl.mu, waiting only for reads already in flight, and waking cancelled waiters after the zeroing all hold up under tracing (lock order, waiter split/wake accounting, finishLocked preconditions, error paths). The new tests fail without the fix for the right reasons: 9 of 11 tests, and 18 of 20 stress seeds.
- Commit history: the merge bot applies every commit (CONTRIBUTING.md, "does not yet support squash merging"). This branch would land merge commit 05dd0c3, where
pgalloc_testdoesn't compile (3-argStartAsyncPagesFileLoad), and 52fe47f's EIO-from-madvisemmchange, which 77bd7bf reverts. Please rebase onto master as two commits that each build, thefinishLockedrefactor and then the fix with its tests. Sign off each one (git commit -s, now required by CONTRIBUTING.md), and describe the final behavior in the commit message without referring to earlier versions ("no longer returns an error", "same as on master"). - Rebase breakage:
pkg/sentry/pgalloc/BUILDconflicts with master. After the rebase,decommit_test.gowon't compile, becausenewSaveTestMemoryFilenow takesdirand arm64hostarch.PageSizeis auintptrvar (see inline). - Test robustness: the in-flight Decommit tests don't synchronize on Decommit actually waiting, and the
TotalUsageassertions depend on the host's shmem THP setting (see inline).
Security: This fixes an in-sandbox Integrity bug: after madvise(MADV_DONTNEED) during background restore, the application could read stale checkpoint data instead of zeroes. The change adds no host syscalls, FDs or paths and can't cross the sandbox boundary. It introduces no new panic, deadlock or unbounded allocation reachable from SandboxUser. Decommit now blocks only on reads already in flight, the same way page faults already do.
Requesting changes before approval.
[🤖] This review is part of an experimental auto-review process to help speed up gVisor review velocity. It is a work in progress. Assisted-By: Gemini Carbon
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //go:build !pagesize_64k |
There was a problem hiding this comment.
After the rebase this PR needs, the file doesn't compile. newSaveTestMemoryFile now takes a dir argument (pass "" at lines 127, 177, 305 and 625), and on arm64 hostarch.PageSize is a uintptr var, so expressions like int64(page) * hostarch.PageSize and off / hostarch.PageSize fail. Drop this now-dead pagesize_64k tag and convert explicitly, e.g. a local pageSize := uint64(hostarch.PageSize) plus int64(...) where offsets are compared, as master's save_restore_test.go does.
| "//pkg/errors/linuxerr", | ||
| "//pkg/hostarch", | ||
| "//pkg/memutil", | ||
| "//pkg/safemem", |
There was a problem hiding this comment.
No test in this target imports //pkg/safemem (or, at this head, //pkg/errors/linuxerr); they're left over from the deleted decommit_restore_test.go. When you resolve the BUILD conflict, keep master's srcs/deps and add only decommit_test.go and //pkg/sync.
| inflight := startDecommit(f, decommitTestPage(fr, 0)) | ||
| awaitDecommit(t, startDecommit(f, decommitTestPages(fr, 4, 8))) | ||
| l.releaseReads() | ||
| awaitDecommit(t, inflight) | ||
| if err := l.wait(t); err != nil { |
There was a problem hiding this comment.
Nothing checks that this Decommit reached awaitLoad before releaseReads(). If it runs after the read completes, the test still passes without covering the in-flight case. Call awaitLoadWaiter(t, f) before releaseReads() here, and before expectBlocked at lines 414 and 549, so the wait is confirmed deterministically.
| if err := l.wait(t); err != nil { | ||
| t.Fatalf("async page loading failed: %v", err) | ||
| } | ||
| checkCommitted(t, f, 11) |
There was a problem hiding this comment.
TotalUsage() is the memfd's st_blocks. With shmem_enabled set to always or within_size, the first fault into this 1 GiB memfd can allocate a 512-page shmem folio, so this check and the stress bound at line 709 fail on such hosts. Skip only the TotalUsage comparisons unless /sys/kernel/mm/transparent_hugepage/shmem_enabled is missing or [never]/[deny]/[advise], and keep the content and knownCommittedBytes checks.
| // Async page loading failed. Unloaded pages in fr will never be | ||
| // loaded, but reads of them may still be in flight, and are no | ||
| // longer marked started. Leave them unloaded so that accesses to | ||
| // them fail, consistently with other unloaded pages. |
There was a problem hiding this comment.
The defer in main() calls ar.Close(), which stops in-flight reads, before it clears started, so in-flight reads are never "no longer marked started". The real window is between the errVal store and Close() returning, and it's harmless because those pages stay unloaded. Please say that here, and add a one-line comment above amfl.awaitLoad(fr) (line 2193) explaining why its error is ignored.
| // If this emptied amfl.unloaded, no read completion may follow to finish | ||
| // amfl. (If we didn't cancel anything, amfl.unloaded was empty already and | ||
| // amfl may already be finished.) |
There was a problem hiding this comment.
!cancelled doesn't imply that unloaded was empty. Suggest: "Finish amfl only if this call emptied amfl.unloaded; otherwise the read completion or LoadFrom() that emptied it has already called finishLocked()."
| t.Helper() | ||
| select { | ||
| case <-done: | ||
| t.Fatalf("%s returned before the read of decommitted pages completed", what) |
There was a problem hiding this comment.
This message is wrong for the MapInternal caller (page 0 isn't decommitted) and the SaveTo caller. Use a neutral message such as "%s returned while reads were still blocked".
77bd7bf to
7b90775
Compare
Share MemoryFile load completion between LoadFrom and the async page loader. Callers hold the loader locks and have finished adding and loading pages for the file. Co-authored-by: Etienne Perot <[email protected]> Signed-off-by: gaojingyu <[email protected]>
During background restore, MADV_DONTNEED on part of a huge page backing private anonymous memory must leave the pages zeroed. Otherwise the async page loader can write checkpoint data into the decommitted pages, and the application reads stale data instead of zeroes. MemoryFile.Decommit cancels unstarted reads of the range and zeroes the pages in place. If a read of the range is already in flight, Decommit returns false without changing the range, and MemoryManager.Decommit invalidates the PMA and drops its references instead. Background reads hold temporary references, so the old backing is not recycled during I/O and a refault allocates fresh zeroed pages. Awaited reads are excluded by activeMu or by save-side quiescence. Decommit does not wait for checkpoint I/O. Keep cancelled pages pending until the backing is zeroed and memory accounting is updated. If async page loading failed, unloaded pages remain inaccessible. Finish loading only after the last pending pages complete. Cover cancellation, in-flight reads, the PMA-invalidation fallback, waiter accounting, save and restore, and load failures. Check page contents independently of the host's shmem huge page policy. Co-authored-by: Etienne Perot <[email protected]> Signed-off-by: gaojingyu <[email protected]>
7b90775 to
d09c845
Compare
During background restore,
MADV_DONTNEEDon part of a huge page backing private anonymous memory can expose checkpointed contents instead of zeroes.MemoryManager.Decommitmay keep the huge page and callMemoryFile.Decommitto zero the range in place, while the asynchronous page loader still has checkpoint reads outstanding for the same backing. This affects hosts where application huge pages are enabled (shmem_enabled=advise). OtherMADV_DONTNEEDcalls drop page references instead, andcancelWasteLoadalready handles them.runsc restoredefaults to--background(#16399), so this is reachable with default restore flags.COMPATIBILITY:REGRESSION-GVISOR: Go programs restored in the background on such hosts can get stale data in newly allocated objects. The Go runtime returns memory with
MADV_DONTNEEDand assumes that the memory is zero when it reuses it.Fix
The fix distinguishes reads already in flight from reads that have not started:
MemoryFile.Decommitreturnsfalsewithout changing the range.MemoryManager.Decommitinvalidates the affected PMAs and drops their references. Existing read waiters and background reads retain independent references until the old reads finish; later faults allocate new backing and read as zero. This avoids holdingMemoryManager.activeMuwhile waiting for checkpoint I/O. The loader only performs I/O and never hole-punches the range.beginDecommitmarks the rangeaplDecommittingbut leaves it inunloaded, so newMapInternal/DataFDcalls still wait and the read queues skip it. Physical zeroing and memory accounting happen outside the loader lock.completeDecommitthen removes the range, settles waiters, advancesminUnloaded, and finishes the MemoryFile if appropriate. Thus completion cannot be published before the pages are zero.MemoryManager.Decommit's interface is unchanged, and no new checkpoint-read error path is added.asyncMemoryFileLoad.finishLockedfor this completion path.The cancellation approach and the first
pgallocimplementation come from Etienne Perot's patch (master...EtiennePerot:gvisor:autoresearch/decommit-cancels-unloaded).Tests
decommit_test.gocovers cancelled loads, in-flight read conflicts, waiters whose pages are cancelled alone or together with pages being read, completion after the last cancellation, read failures, memory accounting, saving during loading, and a randomized stress test.The mixed-waiter regression uses
synctestand explicit read/zeroing gates: page 0's read completes while page 1 is marked for decommit but not yet zeroed. The test checks that the shared waiter still has page 1 pending, a newDataFDremains blocked, an overlapping decommit is declined, and the loader does not report success. After zeroing is released, bothMapInternalandDataFDreturn and page 1 reads as zero. The same test also checks that a failed assertion does not consume the fixture's only completion result.mm_test.goverifies PMA invalidation, allocation of replacement backing, and zero contents after the old read completes. It also containsBenchmarkAsyncDecommit.Performance
BenchmarkAsyncDecommitmeasures the incremental cost of the two-phase completion barrier relative to the PMA-fallback implementation without that barrier. The same benchmark source runs against both production implementations; checkpoint setup and fixture teardown are excluded.Measured on an i7-12700KF with Go 1.27.3, fixed CPUs,
GOMAXPROCS=2, interleaved before/after runs, and 24 independent samples per side:activeMuin that scenarioThe absolute increments are 64.9 ns per in-place cancellation and 80.2 ns of
activeMuhold time. The benchmark does not isolate application fault time spent waiting foractiveMu, the read-queue cost of skipping marked ranges, or the tail latency when the last zeroing fully overlaps loader completion.Earlier revisions were also exercised with C and Go end-to-end probes. Those results described an older implementation and are not claimed as results for the current head.
Validation
//pkg/sentry/pgalloc:pgalloc_testand//pkg/sentry/mm:mm_testpass, as do the affectednogotargets.pgalloc_testpasses with--config=race; the three new benchmark scenarios also pass race-enabled smoke runs.make lintandmake lint-ccpass.mm_testwith--config=racedoes not complete in this environment:TestDataASUpdates, and thenTestMRemapCopyOntoSourcewhen the former is skipped, fail in the existing ptrace subprocess setup before reaching the Decommit cases.This bug fix is separate from background prefetch throttling in #15721.
Assisted-by: OpenAI Codex
Assisted-by: Claude Code
🤖 Generated with Claude Code