macOS: a read trap resolved on the importing thread deadlocks ImportWatched #15742

Closed
opened 2026-11-04 10:55:33 +00:00 by imellag · 0 comments
imellag commented 2026-11-04 10:55:33 +00:00 (Migrated from github.com)
Contributor

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::ImportWatched takes the write tracker's lock, and with importMayWriteGuest it opens a GuestArena::HostWrite over the import's pages. GuestWriteWatchBeginHostWrite (#13442) does not start a host write over a trapped page: withoutTraps resolves the trap first, on the calling thread. The driver's resolver, ResolveLabelTrap, stores the pending image through FlushForAccess, and that path needs locks the importing thread already holds:

  1. The write tracker's lock: FlushForAccess -> StorageTexture::AccessKeptByCpu -> collectWrites -> lockTracker.
  2. The host import mutex: importAllocation and importRunAt run under HostImports::mutex, and the store reaches FlushPending -> writeBack -> writeBackLayers -> HostImportFor, which takes it again.

Neither lock is recursive, so the thread waits for itself.

sample captures from Stray (PPSA02100) at the start of gameplay. Case 1 (twice):

ShaderResources::prepareAddressBindings -> GuestBufferMemory::AcquireRegistered -> importAllocation -> importRunAt
-> GuestMemory::ImportWatched -> GuestArenaBeginHostWrite_nid_postfix -> GuestWriteWatchBeginHostWrite_nid_postfix
-> Watch::resolve -> ResolveLabelTrap -> FlushForAccess -> StorageTexture::AccessKeptByCpu -> collectWrites -> lockTracker

Case 2, with the tracker lock released around the host write (local experiment):

importRunAt -> GuestMemory::ImportWatched -> GuestArenaBeginHostWrite_nid_postfix -> GuestWriteWatchBeginHostWrite_nid_postfix
-> Watch::resolve -> ResolveLabelTrap -> FlushForAccess -> StorageTexture::FlushPending -> writeBack -> writeBackLayers
-> HostImportFor -> Mutex::lockContended

The trapped page lies in a 64 KiB block the import only partly watches, so the readability check and the hashes ImportWatched makes over its watched blocks skip it, and only the host write reaches it.

Why the obvious fixes don't work

  • Releasing the tracker lock around the host write (TrackerLock::Suspension, as leave does) only moves the deadlock to the host import mutex (case 2).
  • Not resolving the trap in the import's host write leaves the trap in place, but ImportWatched then takes the partly watched block out of the watch, and Unregister -> dropTraps gives 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 call importAllocation) 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; on main the import never returns and the test dies with SIGALRM after 10 s:

#ifdef __APPLE__
std::atomic<int> importTrapResolves{0};

void ResolveImportTrap(std::uintptr_t begin, std::uintptr_t end) {
    ++importTrapResolves;
    static_cast<void>(CollectWritesUncached(begin, end - begin));
    GuestWriteWatch::GuestWriteWatchReleaseReads_nid_postfix(begin, end - begin);
}

void CheckImportOverReadTrap() {
    const auto page = static_cast<std::size_t>(sysconf(_SC_PAGESIZE));
    void* memory = AllocateWatched(2 * Block);
    const auto base = reinterpret_cast<std::uint64_t>(memory);
    std::memset(memory, 0x11, 2 * Block);
    Unwatch(base + Block + 2 * page, Block - 2 * page);
    BumpCollectEpoch();
    Require(CollectWrites(base, Block + 2 * page) != 0, "the import block was not collected");
    Require(GuestWriteWatch::GuestWriteWatchSetReadTrapResolver_nid_postfix(&ResolveImportTrap), "cannot set the read trap resolver");
    Require(GuestWriteWatch::GuestWriteWatchTrapReads_nid_postfix(base + Block + page, page), "cannot set a read trap");
    ::alarm(10);
    Require(ImportWatched(base, 2 * Block, [] { return true; }), "an import over a read-trapped page failed");
    ::alarm(0);
    Require(importTrapResolves.load() == 1, "the import did not resolve the read trap once before its host write");
}
#endif

Environment

macOS 27.2, Apple M4 Pro, MoltenVK 1.4.2, x86-64 Release under Rosetta 2, main 1e658521a0 + the open macOS port PRs (support branch support/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 GpuMutex holder 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?

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6201312364 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/15742). Originally posted by [imellag](https://github.com/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::ImportWatched` takes the write tracker's lock, and with `importMayWriteGuest` it opens a `GuestArena::HostWrite` over the import's pages. `GuestWriteWatchBeginHostWrite` (#13442) does not start a host write over a trapped page: `withoutTraps` resolves the trap first, on the calling thread. The driver's resolver, `ResolveLabelTrap`, stores the pending image through `FlushForAccess`, and that path needs locks the importing thread already holds: 1. The write tracker's lock: `FlushForAccess` -> `StorageTexture::AccessKeptByCpu` -> `collectWrites` -> `lockTracker`. 2. The host import mutex: `importAllocation` and `importRunAt` run under `HostImports::mutex`, and the store reaches `FlushPending` -> `writeBack` -> `writeBackLayers` -> `HostImportFor`, which takes it again. Neither lock is recursive, so the thread waits for itself. `sample` captures from Stray (PPSA02100) at the start of gameplay. Case 1 (twice): ``` ShaderResources::prepareAddressBindings -> GuestBufferMemory::AcquireRegistered -> importAllocation -> importRunAt -> GuestMemory::ImportWatched -> GuestArenaBeginHostWrite_nid_postfix -> GuestWriteWatchBeginHostWrite_nid_postfix -> Watch::resolve -> ResolveLabelTrap -> FlushForAccess -> StorageTexture::AccessKeptByCpu -> collectWrites -> lockTracker ``` Case 2, with the tracker lock released around the host write (local experiment): ``` importRunAt -> GuestMemory::ImportWatched -> GuestArenaBeginHostWrite_nid_postfix -> GuestWriteWatchBeginHostWrite_nid_postfix -> Watch::resolve -> ResolveLabelTrap -> FlushForAccess -> StorageTexture::FlushPending -> writeBack -> writeBackLayers -> HostImportFor -> Mutex::lockContended ``` The trapped page lies in a 64 KiB block the import only partly watches, so the readability check and the hashes `ImportWatched` makes over its watched blocks skip it, and only the host write reaches it. ### Why the obvious fixes don't work - Releasing the tracker lock around the host write (`TrackerLock::Suspension`, as `leave` does) only moves the deadlock to the host import mutex (case 2). - Not resolving the trap in the import's host write leaves the trap in place, but `ImportWatched` then takes the partly watched block out of the watch, and `Unregister` -> `dropTraps` gives 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 call `importAllocation`) 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; on `main` the import never returns and the test dies with `SIGALRM` after 10 s: ```cpp #ifdef __APPLE__ std::atomic<int> importTrapResolves{0}; void ResolveImportTrap(std::uintptr_t begin, std::uintptr_t end) { ++importTrapResolves; static_cast<void>(CollectWritesUncached(begin, end - begin)); GuestWriteWatch::GuestWriteWatchReleaseReads_nid_postfix(begin, end - begin); } void CheckImportOverReadTrap() { const auto page = static_cast<std::size_t>(sysconf(_SC_PAGESIZE)); void* memory = AllocateWatched(2 * Block); const auto base = reinterpret_cast<std::uint64_t>(memory); std::memset(memory, 0x11, 2 * Block); Unwatch(base + Block + 2 * page, Block - 2 * page); BumpCollectEpoch(); Require(CollectWrites(base, Block + 2 * page) != 0, "the import block was not collected"); Require(GuestWriteWatch::GuestWriteWatchSetReadTrapResolver_nid_postfix(&ResolveImportTrap), "cannot set the read trap resolver"); Require(GuestWriteWatch::GuestWriteWatchTrapReads_nid_postfix(base + Block + page, page), "cannot set a read trap"); ::alarm(10); Require(ImportWatched(base, 2 * Block, [] { return true; }), "an import over a read-trapped page failed"); ::alarm(0); Require(importTrapResolves.load() == 1, "the import did not resolve the read trap once before its host write"); } #endif ``` ### Environment macOS 27.2, Apple M4 Pro, MoltenVK 1.4.2, x86-64 Release under Rosetta 2, `main` `1e658521a0` + the open macOS port PRs (support branch [`support/macos-boot-1103`](https://github.com/imellag/AnyPS5/commits/support/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 `GpuMutex` holder 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?
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#15742
No description provided.