fix(libc): give a read-trapped page back to a GPU mutex holder instead of waiting for the resolver #15729

Merged
imellag merged 2 commits from upstream-pr/15729 into main 2026-11-12 20:36:37 +00:00
imellag commented 2026-11-04 10:04:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-11-04T10:04:03Z.


What

On macOS a thread that holds GpuMutex and faults on a read-trapped page deadlocks with the read trap resolver. #13499's debt entry already lists it: "A thread holding GpuMutex touching a trapped image waits forever". Stray (PPSA02100) reaches it at the start of gameplay. Two sample captures of the hang show the same two threads:

  • StorageTexture::FlushAllPendingUntil (the idle store, through the CPU) -> FlushPending -> writeBack -> writeBackLayers -> GuestMemory::ReadCommitted, holding GpuMutex and faulting on a trapped page;
  • the resolver thread: ResolveLabelTrap -> FlushForAccess -> StorageTexture::AccessKeptByCpu -> GpuMutexType::lock(), waiting for that mutex.

writeBackLayers releases the traps on its pages first (releaseReadTraps(true)). The release misses a trap the resolver already took for another reader: ReleaseReads finds it no longer in _traps, and untrap skips pages that another thread is resolving. The page stays without access, the store's own read faults, its fault waits for the resolver, and the resolver waits for the store's mutex. The game's render thread then never finishes, and after 120 s UE4 aborts with GameThread timed out waiting for RenderThread.

The resolver can only make progress through GpuMutex, so a holder of that mutex must not wait for it. GuestWriteWatchSetReadTrapBypass_nid_postfix takes a predicate. When it returns true for the faulting thread, the fault gives the page its untrapped protection back at once, as it already does for the resolver's own thread, instead of queuing for the resolver. The driver sets it, together with its resolver, to GuestMemory::GpuMutex().HeldByThisThread(). Only the faulting page is given back; the trap stays in place on the image's other pages and is released by the store as before. The debt entry now says what the holder gets: the page without the store.

macOS only, like the read traps. Linux and Windows are unchanged.

Tested

macOS 27.2, Apple M4 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), x86-64 Release under Rosetta 2. main doesn't build on macOS on its own, so both sides use the support branch support/macos-boot-1103 (5ac8a62297): main 1e658521a0 + the macOS port PRs still open (#7631 direct memory, #7804 process and thread queries, #4630 sceKernelRaiseException, #11437) and five local build fixes marked (support only). None of them touches the read traps. The change side is that branch + this commit.

New write_watch_read-trap-holder in core/libs/tests/WriteWatch.cpp: a resolver that needs a lock the test thread holds, a second thread that faults on the trapped page first, then a read by the lock holder.

  • Base (this test with main's implementation): times out, SIGALRM after 20 s.
  • This change: passes 30 times in a row; the holder reads the old byte, the other reader gets the resolver's byte, and the trap is resolved once.
  • All 26 write_watch_* tests pass, on the head rebased onto main b11c7bf84f as well.

Focused AGC tests, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ctest -R '^(agc_small_tiled_shadow_label|agc_driver_storage_flush_ordering|agc_driver_linear_readback_label|agc_recorder_completion_labels|agc_driver_unimported_label_order|agc_driver_alias_copy_label|agc_driver_label_after_import_copy|write_watch_.*)$' -V:

  • Base: 31 of 32 pass (the new test is not there). This change: 31 of 33.
  • agc_small_tiled_shadow_label aborts on both sides with a label stored a small image inside a pending large tiled one instead of trapping its reads (#13678's check); unchanged by this PR.
  • agc_driver_alias_copy_label is flaky on both sides: 9 of 20 pass on the base, 11 of 20 with this change.
  • Both logs: VUID-VkDeviceCreateInfo-pProperties-04451 4 times (the device created without VK_KHR_portability_subset, #13942) and no other VUID-; neither has was not found but was requested by env var VK_INSTANCE_LAYERS.

Stray, main 1e658521a0 + support branch + #11551 + #9356 (needed to reach gameplay on MoltenVK), 3840x2160:

  • Without this change: the game stops at the start of gameplay in 2 of 12 runs, and in 4 of 8 with more read traps kept alive (a local experiment that leaves trapped images out of the flip store). sample caught this deadlock in both stops it was taken on.
  • With this change and the same experiment, 15 runs: 3 still stopped. The two that sample caught are in a different deadlock that this PR does not touch: GuestMemory::ImportWatched takes the write tracker's lock, opens a host write over the import, and GuestWriteWatchBeginHostWrite resolves a trap inside it on the same thread, which reaches collectWrites and takes the tracker's lock again. That one gets its own PR.

Astro Bot (PPSA21564), tested by @Gonic0 on macOS 27.3, M5 Pro, KosmicKrisp (comment): without this change the game hangs right after hub_crashsite_tutorial loads in 3 of 3 runs, with the same pair of threads reached through ShaderResources::revalidateFull -> StorageTexture::refresh -> writeBackLayers -> cpuLeftUnchanged; with head fa4ea09852 merged onto the same base, 2 of 2 runs enter hub_crashsite_tutorial and keep presenting until the 300 s limit.

tools/check_conventions.py --base upstream/main passes. tools/pr_overlap.py --branch fix/libc-read-trap-mutex-holder finds no overlapping lines among the PRs it could merge; it could not compare 87 PRs that conflict with main and share Texture.cpp or docs/dev/debt/libSceAgcDriver.md.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M4 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), macOS 27.2
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5152474161 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15729). Originally posted by [imellag](https://github.com/imellag) on 2026-11-04T10:04:03Z. --- ### What On macOS a thread that holds `GpuMutex` and faults on a read-trapped page deadlocks with the read trap resolver. #13499's debt entry already lists it: "A thread holding `GpuMutex` touching a trapped image waits forever". Stray (PPSA02100) reaches it at the start of gameplay. Two `sample` captures of the hang show the same two threads: - `StorageTexture::FlushAllPendingUntil` (the idle store, through the CPU) -> `FlushPending` -> `writeBack` -> `writeBackLayers` -> `GuestMemory::ReadCommitted`, holding `GpuMutex` and faulting on a trapped page; - the resolver thread: `ResolveLabelTrap` -> `FlushForAccess` -> `StorageTexture::AccessKeptByCpu` -> `GpuMutexType::lock()`, waiting for that mutex. `writeBackLayers` releases the traps on its pages first (`releaseReadTraps(true)`). The release misses a trap the resolver already took for another reader: `ReleaseReads` finds it no longer in `_traps`, and `untrap` skips pages that another thread is resolving. The page stays without access, the store's own read faults, its fault waits for the resolver, and the resolver waits for the store's mutex. The game's render thread then never finishes, and after 120 s UE4 aborts with `GameThread timed out waiting for RenderThread`. The resolver can only make progress through `GpuMutex`, so a holder of that mutex must not wait for it. `GuestWriteWatchSetReadTrapBypass_nid_postfix` takes a predicate. When it returns true for the faulting thread, the fault gives the page its untrapped protection back at once, as it already does for the resolver's own thread, instead of queuing for the resolver. The driver sets it, together with its resolver, to `GuestMemory::GpuMutex().HeldByThisThread()`. Only the faulting page is given back; the trap stays in place on the image's other pages and is released by the store as before. The debt entry now says what the holder gets: the page without the store. macOS only, like the read traps. Linux and Windows are unchanged. ### Tested macOS 27.2, Apple M4 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), x86-64 Release under Rosetta 2. `main` doesn't build on macOS on its own, so both sides use the support branch [`support/macos-boot-1103`](https://github.com/imellag/AnyPS5/commits/support/macos-boot-1103) (`5ac8a62297`): `main` `1e658521a0` + the macOS port PRs still open (#7631 direct memory, #7804 process and thread queries, #4630 `sceKernelRaiseException`, #11437) and five local build fixes marked `(support only)`. None of them touches the read traps. The change side is that branch + this commit. New `write_watch_read-trap-holder` in `core/libs/tests/WriteWatch.cpp`: a resolver that needs a lock the test thread holds, a second thread that faults on the trapped page first, then a read by the lock holder. - Base (this test with main's implementation): times out, `SIGALRM` after 20 s. - This change: passes 30 times in a row; the holder reads the old byte, the other reader gets the resolver's byte, and the trap is resolved once. - All 26 `write_watch_*` tests pass, on the head rebased onto `main` `b11c7bf84f` as well. Focused AGC tests, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ctest -R '^(agc_small_tiled_shadow_label|agc_driver_storage_flush_ordering|agc_driver_linear_readback_label|agc_recorder_completion_labels|agc_driver_unimported_label_order|agc_driver_alias_copy_label|agc_driver_label_after_import_copy|write_watch_.*)$' -V`: - Base: 31 of 32 pass (the new test is not there). This change: 31 of 33. - `agc_small_tiled_shadow_label` aborts on both sides with `a label stored a small image inside a pending large tiled one instead of trapping its reads` (#13678's check); unchanged by this PR. - `agc_driver_alias_copy_label` is flaky on both sides: 9 of 20 pass on the base, 11 of 20 with this change. - Both logs: `VUID-VkDeviceCreateInfo-pProperties-04451` 4 times (the device created without `VK_KHR_portability_subset`, #13942) and no other `VUID-`; neither has `was not found but was requested by env var VK_INSTANCE_LAYERS`. Stray, `main` `1e658521a0` + support branch + #11551 + #9356 (needed to reach gameplay on MoltenVK), 3840x2160: - Without this change: the game stops at the start of gameplay in 2 of 12 runs, and in 4 of 8 with more read traps kept alive (a local experiment that leaves trapped images out of the flip store). `sample` caught this deadlock in both stops it was taken on. - With this change and the same experiment, 15 runs: 3 still stopped. The two that `sample` caught are in a different deadlock that this PR does not touch: `GuestMemory::ImportWatched` takes the write tracker's lock, opens a host write over the import, and `GuestWriteWatchBeginHostWrite` resolves a trap inside it on the same thread, which reaches `collectWrites` and takes the tracker's lock again. That one gets its own PR. Astro Bot (PPSA21564), tested by @Gonic0 on macOS 27.3, M5 Pro, KosmicKrisp ([comment](https://github.com/boykopovar/AnyPS5/pull/15729#issuecomment-6785621581)): without this change the game hangs right after `hub_crashsite_tutorial` loads in 3 of 3 runs, with the same pair of threads reached through `ShaderResources::revalidateFull` -> `StorageTexture::refresh` -> `writeBackLayers` -> `cpuLeftUnchanged`; with head `fa4ea09852` merged onto the same base, 2 of 2 runs enter `hub_crashsite_tutorial` and keep presenting until the 300 s limit. `tools/check_conventions.py --base upstream/main` passes. `tools/pr_overlap.py --branch fix/libc-read-trap-mutex-holder` finds no overlapping lines among the PRs it could merge; it could not compare 87 PRs that conflict with `main` and share `Texture.cpp` or `docs/dev/debt/libSceAgcDriver.md`. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Apple M4 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), macOS 27.2 - [x] AI-assisted: yes
Sign in to join this conversation.
No reviewers
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!15729
No description provided.