fix(agc): leave read-trapped pages out of an import's page hashes #16884

Merged
imellag merged 8 commits from upstream-pr/16884 into main 2026-11-12 23:15:08 +00:00
imellag commented 2026-11-07 20:12:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-11-07T20:12:00Z.


What

On macOS, ImportWatched hashes 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, then StorageTexture::FlushPending) waits for GpuMutex. When the importing thread holds GpuMutex, 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_postfix reports the trapped page runs of a range, HashPages does not read those pages and marks them unknown, and stampWindowRuns neither 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 under GpuMutex (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 GpuMutex holder 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 -> ImportWatched joining its hash helpers, the helpers in Watch::fault, and the resolver thread in ResolveLabelTrap -> FlushPending waiting for GpuMutex.

agc_write_tracking_import_over_read_traps (macOS) holds GpuMutex, 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 takes GpuMutex, 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), main 8f7c957ed0 with the macOS build fixes of #16591.

  • agc_write_tracking_import_over_read_traps without the GuestMemory.cpp change: hangs and is killed by its alarm(20) (SIGALRM after 21 s). With it: passes 5 of 5 runs.
  • ctest -R '^agc_write_tracking' with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation: main 3 of 3, this change 4 of 4; neither log has was not found but was requested by env var VK_INSTANCE_LAYERS or a VUID-.
  • ctest -R '^write_watch': 25 of 25 with the change.
  • Not run in a title yet with this head.

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 from Vulkan SDK 1.4.363.0, macOS 27.2
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5205420407 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16884). Originally posted by [imellag](https://github.com/imellag) on 2026-11-07T20:12:00Z. --- ### What On macOS, `ImportWatched` hashes 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`, then `StorageTexture::FlushPending`) waits for `GpuMutex`. When the importing thread holds `GpuMutex`, 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_postfix` reports the trapped page runs of a range, `HashPages` does not read those pages and marks them unknown, and `stampWindowRuns` neither 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 under `GpuMutex` (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 `GpuMutex` holder 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` -> `ImportWatched` joining its hash helpers, the helpers in `Watch::fault`, and the resolver thread in `ResolveLabelTrap` -> `FlushPending` waiting for `GpuMutex`. `agc_write_tracking_import_over_read_traps` (macOS) holds `GpuMutex`, 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 takes `GpuMutex`, 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), `main` 8f7c957ed0 with the macOS build fixes of #16591. - `agc_write_tracking_import_over_read_traps` without the `GuestMemory.cpp` change: hangs and is killed by its `alarm(20)` (`SIGALRM` after 21 s). With it: passes 5 of 5 runs. - `ctest -R '^agc_write_tracking'` with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`: `main` 3 of 3, this change 4 of 4; neither log has `was not found but was requested by env var VK_INSTANCE_LAYERS` or a `VUID-`. - `ctest -R '^write_watch'`: 25 of 25 with the change. - Not run in a title yet with this head. ### 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 from Vulkan SDK 1.4.363.0, macOS 27.2 - [x] AI-assisted: yes
gabichuuuuu commented 2026-11-07 21:59:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T21:59:05Z.

Review: COMMENTED


Read cebaebf25b against main, both commits, including the one from #15729.

The deadlock analysis holds: with GpuMutex held, 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 clears ReadTrapped and restores the page's protection without calling the resolver. The _traps entry stays, but nothing faults on that page any more. After an import under GpuMutex hashes a range (HashPages before the import, stampWindowRuns after), 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_holder doesn'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 before ImportWatched takes the tracker lock (outside the signal handler; GpuMutex is 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, so main hashes serially too and agc_write_tracking_import_under_gpu_mutex passes without the fix (3-vCPU macOS runners, for example). Returning 77 there would avoid a pass that proves nothing; SKIP_RETURN_CODE 77 is already set.

Minor: the holder check makes every import under GpuMutex on macOS hash on one thread, also when no trap is in the range or with APS5_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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5815190061 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16884#pullrequestreview-5815190061). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T21:59:05Z. **Review:** COMMENTED --- Read `cebaebf25b` against `main`, both commits, including the one from #15729. The deadlock analysis holds: with `GpuMutex` held, `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 clears `ReadTrapped` and restores the page's protection without calling the resolver. The `_traps` entry stays, but nothing faults on that page any more. After an import under `GpuMutex` hashes a range (`HashPages` before the import, `stampWindowRuns` after), 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_holder` doesn'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 before `ImportWatched` takes the tracker lock (outside the signal handler; `GpuMutex` is 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, so `main` hashes serially too and `agc_write_tracking_import_under_gpu_mutex` passes without the fix (3-vCPU macOS runners, for example). Returning 77 there would avoid a pass that proves nothing; `SKIP_RETURN_CODE 77` is already set. **Minor:** the holder check makes every import under `GpuMutex` on macOS hash on one thread, also when no trap is in the range or with `APS5_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.
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!16884
No description provided.