fix(libc): keep write tracking when a run of shared guest pages is split #11552
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11552"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-26T18:35:36Z.
What
Since #9505, a run of shared guest pages is one host view. When a guest
munmaporMAP_FIXEDremap covers part of a run,breakViewtakes these steps:PAGE_EXECUTE_READWRITE.Between steps 2 and 3, a store from another guest thread to an armed (write-tracked, read-only) page of the surviving part does not fault. So
HandleWritenever runs. The page then returns to read-only witharmed,seenand its clean range unchanged.Collectnever reports the store, and the GPU keeps a stale copy.breakViewnow copies the armed 4 KiB subpages of the run before the unmap. After the restore, it compares each one with its copy and invalidates the subpages whose bytes changed. Single-threaded results are the same as onmain, because a subpage is reported only when its bytes changed during the window.This is point 2 of the post-merge review of #9505, which its author asked us to fix (comment).
Cheaper detection, tried and ruled out (Windows 11 26200, small probe programs):
VirtualProtectcannot raise a view mappedPAGE_EXECUTE_READorPAGE_READONLYto read-write (error 87).PAGE_GUARD. A guard fires once per page. After any thread's first access, a store from a second thread goes through, so it isn't exact.QueryWorkingSetExlet you ask which pages of the new view were touched during the window. Neither records stores made in kernel mode (for exampleReadFileinto the page). After a working-set trim,QueryWorkingSetExreports no page as touched.Cost. The copy and compare cost scales with the run's armed bytes. Splitting a run with no armed page costs the same as on
main. A split of a fully armed run is slower: each value is the median of 7 splits of one run, measured with this header and withmain's on the same machine:mainTwo alternating rounds are shown. A persistent copy buffer roughly halves the armed cost (17 ms and 335 ms), but it keeps up to a run's size of memory, so this change allocates per split instead. The remaining cost is mostly the first touch of each page of the freshly mapped view.
Tested
Windows 11 26200, Ryzen 5 5600X, WinLibs GCC 15.2.0 r7, Release. Head
18095f023bonmainf95acfc948.windows_write_watch. It maps a 64-page run as one view, arms every other page from 12 to 50, and splits the run at page 10. Meanwhile a writer thread pollsVirtualQueryon page 50 and stores a new byte only while it seesPAGE_EXECUTE_READWRITE, which happens only inside the window.Collect(Armed)must then report exactly that subpage when its byte changed, and nothing when it didn't. The check stops after 16 stores in the window or 2000 rounds, and it fails if no store landed in the window at all, so a run where the race never happened cannot pass.main'sWindowsMappings.hpp: fails in 5 of 5 runs witha store that raced the split of its run was not reported, within the first round of each run.a split of a run reported a page nobody stored to.^(windows_write_watch|windows_mappings_release.*|guest_memory|windows_shared_map_cost)$: 7 of 7 pass, in 3 of 3 rounds.Not tested: Linux and macOS (
WindowsMappingsis Windows-only). No title is known to split armed runs. Persona 5 Royal's reachable scenes made one flexible map and no unmap in a 330 s memory trace.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T18:45:53Z.
Review: CHANGES_REQUESTED
The new race check can pass without exercising the race. The loop stops after
Roundseven whenracedremains zero, and every iteration then accepts an emptyCollection::Armedresult becausestoredis false. That makes scheduler timing capable of turning this regression test into a false green (and the current hosted Windows product job is skipped). Please assert after the loop that at least one store was observed during the writable window, or otherwise synchronize the writer so the test proves that the window was hit before it reports success. The reported 8/8 runs are useful, but they do not make the zero-race path fail.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T18:51:24Z.
Review: APPROVED
The new
Require(raced > 0, ...)closes the false-green path I flagged:racedincrements only after the writer observesPAGE_EXECUTE_READWRITEand performs its store, and the check now fails if no such store occurs in the 2,000-round limit. The author reports 10/10 runs on Windows with the regression, 5/5 failures on the old implementation, and 7 focused write-watch/mapping tests passing in 3 rounds. I did not run Windows locally. Current conventions, eligibility, and labels pass; the applicable product/relinker checks are still running or skipped by path policy.