macOS: a read trap resolved on the importing thread deadlocks ImportWatched #15742
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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-04T10:55:33Z.
On macOS, an import over memory that holds a label read trap (#13499, #13678) can deadlock the importing thread, because the trap is resolved on that thread while it holds the import's locks.
What happens
GuestMemory::ImportWatchedtakes the write tracker's lock, and withimportMayWriteGuestit opens aGuestArena::HostWriteover the import's pages.GuestWriteWatchBeginHostWrite(#13442) does not start a host write over a trapped page:withoutTrapsresolves the trap first, on the calling thread. The driver's resolver,ResolveLabelTrap, stores the pending image throughFlushForAccess, and that path needs locks the importing thread already holds:FlushForAccess->StorageTexture::AccessKeptByCpu->collectWrites->lockTracker.importAllocationandimportRunAtrun underHostImports::mutex, and the store reachesFlushPending->writeBack->writeBackLayers->HostImportFor, which takes it again.Neither lock is recursive, so the thread waits for itself.
samplecaptures from Stray (PPSA02100) at the start of gameplay. Case 1 (twice):Case 2, with the tracker lock released around the host write (local experiment):
The trapped page lies in a 64 KiB block the import only partly watches, so the readability check and the hashes
ImportWatchedmakes over its watched blocks skip it, and only the host write reaches it.Why the obvious fixes don't work
TrackerLock::Suspension, asleavedoes) only moves the deadlock to the host import mutex (case 2).ImportWatchedthen takes the partly watched block out of the watch, andUnregister->dropTrapsgives the page back without storing the image. A later CPU read sees the old guest bytes instead of the GPU's results.So the trap has to be resolved, without the import's locks held. That seems to need the import entry points (
HostImportFor,AcquireRegistered, the build paths that callimportAllocation) to find the traps over the run under the lock, release it, resolve them, and retry. That reaches into most of the import code, so I'd rather ask how you'd like it done before writing it.Reproduction
A macOS case for
agc_write_tracking(core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp). The resolver collects writes uncached, as the driver's does; onmainthe import never returns and the test dies withSIGALRMafter 10 s:Environment
macOS 27.2, Apple M4 Pro, MoltenVK 1.4.2, x86-64 Release under Rosetta 2,
main1e658521a0+ the open macOS port PRs (support branchsupport/macos-boot-1103). In Stray it shows up in about 1 of 10 runs, more often when more images stay trapped.#15729 fixes a different read trap deadlock (a
GpuMutexholder faulting on a trapped page while the resolver waits for that mutex); this one remains with it applied.cc @Gonic0, since the read traps are yours: does a retry at the import entry points sound right to you, or is there a simpler place to resolve them?