fix(agc): leave read-trapped pages out of an import's page hashes #16884
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16884"
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 imellag on 2026-11-07T20:12:00Z.
What
On macOS,
ImportWatchedhashes the pages of a watched range before the import (HashPages) and the pages written during it after the import (stampWindowRuns), split over up to four threads (SplitPages). A page of the range can carry a label read trap (StoreReadbackBeforeLabel). Reading it from the hash faults and waits for the read-trap resolver, and the resolver (ResolveLabelTrap, thenStorageTexture::FlushPending) waits forGpuMutex. When the importing thread holdsGpuMutex, which the on-demand imports from draws and dispatches do, the hash never finishes: a helper thread waits for the resolver while the holder waits for the helper, or, below four hardware threads, the holder itself waits for the resolver.The hash now leaves read-trapped pages alone.
GuestWriteWatchReadTraps_nid_postfixreports the trapped page runs of a range,HashPagesdoes not read those pages and marks them unknown, andstampWindowRunsneither reads a trapped page nor counts an unknown one as unchanged, so such a page is stamped as written, the conservative outcome. No trap fires during an import, the traps stay in place, and the first read after the import stores the image as before. Traps are armed underGpuMutex(labels), so none appears between the query and the reads of an import that holds it. On Linux and Windows there are no read traps and the query reports none.An earlier head of this PR hashed on the
GpuMutexholder with #15729's bypass instead; as the review below found, that took the traps off whole import ranges for every thread, so guest threads could read the pages unstored.Seen with Astro Bot (PPSA21564) on macOS with map-time imports turned off locally, so every range went through the on-demand import under the GPU mutex: the title hung right after its first draw, with the importer thread in
GuestBufferMemory::AcquireRegistered->ImportWatchedjoining its hash helpers, the helpers inWatch::fault, and the resolver thread inResolveLabelTrap->FlushPendingwaiting forGpuMutex.agc_write_tracking_import_over_read_traps(macOS) holdsGpuMutex, imports a 16 MiB watched range whose callback stores to every page and then arms a read trap over the range, with a resolver that takesGpuMutex, stores a known byte and releases the trap. It checks that the import finishes, that the range is still trapped afterwards, and that a read from another thread then sees the byte the trap stored.Tested
macOS 27.2, Apple M4 Pro, x86_64 Release under Rosetta 2, Vulkan SDK 1.4.363.0 (MoltenVK),
main8f7c957ed0with the macOS build fixes of #16591.agc_write_tracking_import_over_read_trapswithout theGuestMemory.cppchange: hangs and is killed by itsalarm(20)(SIGALRMafter 21 s). With it: passes 5 of 5 runs.ctest -R '^agc_write_tracking'withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation:main3 of 3, this change 4 of 4; neither log haswas not found but was requested by env var VK_INSTANCE_LAYERSor aVUID-.ctest -R '^write_watch': 25 of 25 with the change.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)Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T21:59:05Z.
Review: COMMENTED
Read
cebaebf25bagainstmain, both commits, including the one from #15729.The deadlock analysis holds: with
GpuMutexheld,SplitPages(GuestMemory.cpp:1032) now hashes on the caller, so no helper waits on the resolver while the holder waits for the helpers.The bypass un-traps the page for every thread. In
resolveTrap(GuestWriteWatch.cpp:707-711), the bypass branch clearsReadTrappedand restores the page's protection without calling the resolver. The_trapsentry stays, but nothing faults on that page any more. After an import underGpuMutexhashes a range (HashPagesbefore the import,stampWindowRunsafter), every trapped page in it can be read unstored by guest threads until the next label or a store of that image, so a guest that waited for the label can read stale results. The debt entry only says the holder reads it unstored.write_watch_read_trap_holderdoesn't catch this because its reader faults before the holder reads and is already queued to the resolver. One fix: resolve the import range's traps on the holder beforeImportWatchedtakes the tracker lock (outside the signal handler;GpuMutexis recursive). Otherwise, state this in the debt entry and add a read from another thread after the import to the test.The import test doesn't reproduce on small hosts.
workers = min(4, hardware_concurrency()/2, 4096/1024)is 1 with fewer than 4 hardware threads, somainhashes serially too andagc_write_tracking_import_under_gpu_mutexpasses without the fix (3-vCPU macOS runners, for example). Returning 77 there would avoid a pass that proves nothing;SKIP_RETURN_CODE 77is already set.Minor: the holder check makes every import under
GpuMutexon macOS hash on one thread, also when no trap is in the range or withAPS5_NO_LABEL_READ_TRAP=1. Gating it on a trap overlapping the range would keep the split elsewhere. The debt line also reads "unstored. a system call".Not run here (macOS-only path); this is from reading the code at the head.
AI-assisted: yes.