fix(agc): check host imports outside the pending storage image mutex when storing drawn linear targets #26273

Merged
Gonic0 merged 2 commits from upstream-pr/26273 into main 2026-11-24 18:16:06 +00:00
Gonic0 commented 2026-11-23 15:44:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gonic0 on 2026-11-23T15:44:27Z.


What

On macOS, StorageTexture::StoreDrawnLinear holds GpuMutex and the pending storage image mutex while it calls HostImportCovers for each drawn linear target it may read-trap at a label, and HostImportCovers takes the host imports mutex. ImportMappedRanges holds the host imports mutex and then takes the pending mutex through StorageTexture::AnyPendingOverlaps. Two threads can each hold the lock the other waits for. A sample of Grand Theft Auto V (PPSA04264) caught it once:

thread A: ImportMappedRanges -> StorageTexture::AnyPendingOverlaps            (holds imports, waits for pending)
thread B: StorageTexture::StoreDrawnLinear -> HostImportCovers -> Mutex::lockContended   (holds pending, waits for imports)

The pending mutex is already a MapImportGate::LeafMutex, and StoreSmallPending already asks HostImportCovers after releasing it.

  • StoreDrawnLinear collects the dirty drawn targets it may trap, as shared_ptrs, under the pending mutex and releases it. It then asks HostImportCovers for each, and takes the pending mutex again to set traps (trapReadsAtLabel, which reads the pending set) on the covered ones that are still drawn and dirty. Uncovered ones, and any whose weak_from_this has expired, are stored as before.
  • ImportsMutex::lock now aborts when lock order is checked and the thread holds a leaf lock (MapImportGate::LeafLockHeld).
  • APS5_ASSERT_LOCK_ORDER turns on that leaf and imports lock-order checking without the GpuMutex assertions of APS5_ASSERT_GPU_LOCK, which abort earlier in test fixtures that drive the device without GpuMutex (for example the f32 flush probe at device creation). It is documented in docs/dev/ENVIRONMENT.md.
  • agc_driver_linear_target_completion_stores_lock_order runs the existing linear target completion store test with APS5_ASSERT_LOCK_ORDER=1. That test reaches StoreDrawnLinear at a label with a covered drawn linear target.

Tested

Apple M5 Pro, macOS 27.3, MoltenVK (Vulkan SDK 1.4.363), x86_64 Release under Rosetta 2, main 26508fa3f9. The same build directory for main and this change. ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1.

  • agc_driver_linear_target_completion_stores_lock_order: 3 of 3 pass. With this change's assertion and without its StoreDrawnLinear change, 3 of 3 abort with [lock] ASSERT: the host imports mutex is taken while this thread holds the live or pending storage image mutex.
  • 87 tests matching linear_target|label|map_gate|flush_exclusion|unwatch_hook_lock_order|host_import|drawn|read_trap|small_pending|alpha_to_coverage, one run each side with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer (Khronos validation layer 1.4.363). Each side's log shows 75 Insert instance layer lines, no was not found, and no VUID-.
    • The same 5 fail on main and with this change: agc_driver_label_ordered_flush, _unimported, agc_driver_unimported_label_order and its _draw_pipeline and _plan_verify variants.
    • 21 were not built on either side.
  • The same 87 tests with APS5_ASSERT_LOCK_ORDER=1 and this change: no [lock] ASSERT. agc_driver_label_after_import_copy failed once under -j4 and passed 4 of 4 alone, with and without the variable.
  • Title: Grand Theft Auto V (PPSA04264), with a pad script that reaches the prologue, on a tree that also has #17425, #11049, #25978 and #26221.
    • The deadlock itself is a rare race: 6 runs without this change all reached the prologue.
    • With APS5_ASSERT_LOCK_ORDER=1, 2 of 2 runs without the StoreDrawnLinear change abort on the assertion within 10 s.
    • With this change, a run with the variable reaches the prologue with no assertion, and 3 runs without it reach the prologue as before.
  • tools/check_conventions.py --base upstream/main and tools/env_reference.py --check pass.

Not run on Linux or Windows. There, the trap path in StoreDrawnLinear is compiled out, and the new check fires only when lock order is checked.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: PPSA04264 #1203
  • GPUs tested: Apple M5 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), macOS 27.3
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5456250111 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26273). Originally posted by [Gonic0](https://github.com/Gonic0) on 2026-11-23T15:44:27Z. --- ### What On macOS, `StorageTexture::StoreDrawnLinear` holds `GpuMutex` and the pending storage image mutex while it calls `HostImportCovers` for each drawn linear target it may read-trap at a label, and `HostImportCovers` takes the host imports mutex. `ImportMappedRanges` holds the host imports mutex and then takes the pending mutex through `StorageTexture::AnyPendingOverlaps`. Two threads can each hold the lock the other waits for. A `sample` of Grand Theft Auto V (PPSA04264) caught it once: ``` thread A: ImportMappedRanges -> StorageTexture::AnyPendingOverlaps (holds imports, waits for pending) thread B: StorageTexture::StoreDrawnLinear -> HostImportCovers -> Mutex::lockContended (holds pending, waits for imports) ``` The pending mutex is already a `MapImportGate::LeafMutex`, and `StoreSmallPending` already asks `HostImportCovers` after releasing it. - `StoreDrawnLinear` collects the dirty drawn targets it may trap, as `shared_ptr`s, under the pending mutex and releases it. It then asks `HostImportCovers` for each, and takes the pending mutex again to set traps (`trapReadsAtLabel`, which reads the pending set) on the covered ones that are still drawn and dirty. Uncovered ones, and any whose `weak_from_this` has expired, are stored as before. - `ImportsMutex::lock` now aborts when lock order is checked and the thread holds a leaf lock (`MapImportGate::LeafLockHeld`). - `APS5_ASSERT_LOCK_ORDER` turns on that leaf and imports lock-order checking without the `GpuMutex` assertions of `APS5_ASSERT_GPU_LOCK`, which abort earlier in test fixtures that drive the device without `GpuMutex` (for example the f32 flush probe at device creation). It is documented in `docs/dev/ENVIRONMENT.md`. - `agc_driver_linear_target_completion_stores_lock_order` runs the existing linear target completion store test with `APS5_ASSERT_LOCK_ORDER=1`. That test reaches `StoreDrawnLinear` at a label with a covered drawn linear target. ### Tested Apple M5 Pro, macOS 27.3, MoltenVK (Vulkan SDK 1.4.363), x86_64 Release under Rosetta 2, `main` `26508fa3f9`. The same build directory for `main` and this change. `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`. - `agc_driver_linear_target_completion_stores_lock_order`: 3 of 3 pass. With this change's assertion and without its `StoreDrawnLinear` change, 3 of 3 abort with `[lock] ASSERT: the host imports mutex is taken while this thread holds the live or pending storage image mutex`. - 87 tests matching `linear_target|label|map_gate|flush_exclusion|unwatch_hook_lock_order|host_import|drawn|read_trap|small_pending|alpha_to_coverage`, one run each side with `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer` (Khronos validation layer 1.4.363). Each side's log shows 75 `Insert instance layer` lines, no `was not found`, and no `VUID-`. - The same 5 fail on `main` and with this change: `agc_driver_label_ordered_flush`, `_unimported`, `agc_driver_unimported_label_order` and its `_draw_pipeline` and `_plan_verify` variants. - 21 were not built on either side. - The same 87 tests with `APS5_ASSERT_LOCK_ORDER=1` and this change: no `[lock] ASSERT`. `agc_driver_label_after_import_copy` failed once under `-j4` and passed 4 of 4 alone, with and without the variable. - Title: Grand Theft Auto V (PPSA04264), with a pad script that reaches the prologue, on a tree that also has #17425, #11049, #25978 and #26221. - The deadlock itself is a rare race: 6 runs without this change all reached the prologue. - With `APS5_ASSERT_LOCK_ORDER=1`, 2 of 2 runs without the `StoreDrawnLinear` change abort on the assertion within 10 s. - With this change, a run with the variable reaches the prologue with no assertion, and 3 runs without it reach the prologue as before. - `tools/check_conventions.py --base upstream/main` and `tools/env_reference.py --check` pass. Not run on Linux or Windows. There, the trap path in `StoreDrawnLinear` is compiled out, and the new check fires only when lock order is checked. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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: PPSA04264 #1203 - [x] GPUs tested: Apple M5 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), macOS 27.3 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!26273
No description provided.