fix(agc): refuse a host import over read-trapped pages instead of resolving the trap under its locks #26221

Merged
Gonic0 merged 4 commits from upstream-pr/26221 into main 2026-12-02 19:41:14 +00:00
Gonic0 commented 2026-11-23 13:55:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gonic0 on 2026-11-23T13:55:21Z.


What

On macOS, a host import opens a host write over its pages (GuestMemory::ImportWatched, importMayWriteGuest) while it holds the host import mutex and the write tracker's lock. If a label read trap is set on those pages after the import's access check, GuestWriteWatchBeginHostWrite runs the trap's resolver on the importing thread. The driver's resolver (ResolveLabelTrap) needs both locks to store the pending image, so the thread waits for itself (#15742):

HostImportFor -> importRunAt -> GuestMemory::ImportWatched -> GuestArenaBeginHostWrite -> GuestWriteWatchBeginHostWrite
-> Watch::resolve -> ResolveLabelTrap -> ... -> lockTracker
  • GuestWriteWatchTryBeginHostWrite_nid_postfix (and GuestArenaTryBeginHostWrite_nid_no_patch, GuestArena::HostWrite(..., HostWrite::Try)) checks for traps and registers the host write under the watch's lock in one step. If a trap covers the range, or another thread is resolving one there, it opens nothing, queues the traps on the watch's resolver thread without waiting for them, and returns false. TrapReads already refuses pages inside an open host write, so a trap cannot be set between the check and the import.
  • ImportWatched opens its host write with that attempt and returns without running the import when it is refused. importRunAt returns nullptr for that request without recording it in failed, failedJoins or the failure counts, so the request falls back to copies and a later request imports the run once the resolver thread has stored the image.
  • The resolver thread frees a queued slot itself when nothing waits on it.
  • A debt entry records the refused request and the remaining window: the import's page hash skips trapped pages, but a trap set between that check and the hash's read still waits for the resolver while the import locks are held.

#20580 also targets #15742: it queues the run's traps in importRunAt before the import starts and orders the resolver thread against GpuMutex. A trap set after that check still reaches BeginHostWrite. The new test sets the trap from the unwatch hook that ImportWatched calls after importRunAt's checks. It hangs on main and on main + #20580 with the same stack, and passes with this change. This pull request does not depend on #20580.

Tested

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

  • New agc_driver_import_over_read_trap: 8 of 8 runs pass with this change. On main, 3 of 3 runs hang until the test's 10 s alarm (SIGALRM); on main plus #20580's non-test changes, 3 of 3 hang the same way. A sample of each hang shows ImportWatched -> GuestArenaBeginHostWrite -> GuestWriteWatchBeginHostWrite -> Watch::resolve -> Resolve -> lockTracker.
  • 99 tests matching write_watch|host_import|import_watch|read_trap|label|guest_arena|host_write|write_track, run on each side without and then with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer (Khronos validation layer 1.4.363; each side's log shows 53 Insert instance layer lines, no was not found, no VUID-): the same 6 fail on main and with this change, with identical messages (agc_driver_label_ordered_flush, _unimported, agc_driver_unimported_label_order and its _draw_pipeline and _plan_verify variants, agc_write_tracking); 11 were not built on either side. Everything else passes on both.
  • Title: Grand Theft Auto V (PPSA04264), with a pad script that reaches the prologue, on a tree that also has #17425, #11049 and #25978 (which this machine needs to build and run it) and a local workaround for a separate lock-order inversion in StoreDrawnLinear. Without this change, 2 of 2 runs that got past start-up froze in the prologue, with sample showing the stack above (ResolveLabelTrap -> lockTracker). With it, 3 of 3 runs played through the pad script into the prologue and ran until a GPU hang that is not caused by this change.
  • tools/check_conventions.py --base upstream/main and tools/debt_fold.py --check pass.

Not run on Linux or Windows, where the new export only forwards to the existing host write. The test is built and registered only on macOS, the one platform where this refusal exists.

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: Fixes #15742
  • 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:5454444565 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26221). Originally posted by [Gonic0](https://github.com/Gonic0) on 2026-11-23T13:55:21Z. --- ### What On macOS, a host import opens a host write over its pages (`GuestMemory::ImportWatched`, `importMayWriteGuest`) while it holds the host import mutex and the write tracker's lock. If a label read trap is set on those pages after the import's access check, `GuestWriteWatchBeginHostWrite` runs the trap's resolver on the importing thread. The driver's resolver (`ResolveLabelTrap`) needs both locks to store the pending image, so the thread waits for itself (#15742): ``` HostImportFor -> importRunAt -> GuestMemory::ImportWatched -> GuestArenaBeginHostWrite -> GuestWriteWatchBeginHostWrite -> Watch::resolve -> ResolveLabelTrap -> ... -> lockTracker ``` - `GuestWriteWatchTryBeginHostWrite_nid_postfix` (and `GuestArenaTryBeginHostWrite_nid_no_patch`, `GuestArena::HostWrite(..., HostWrite::Try)`) checks for traps and registers the host write under the watch's lock in one step. If a trap covers the range, or another thread is resolving one there, it opens nothing, queues the traps on the watch's resolver thread without waiting for them, and returns false. `TrapReads` already refuses pages inside an open host write, so a trap cannot be set between the check and the import. - `ImportWatched` opens its host write with that attempt and returns without running the import when it is refused. `importRunAt` returns `nullptr` for that request without recording it in `failed`, `failedJoins` or the failure counts, so the request falls back to copies and a later request imports the run once the resolver thread has stored the image. - The resolver thread frees a queued slot itself when nothing waits on it. - A debt entry records the refused request and the remaining window: the import's page hash skips trapped pages, but a trap set between that check and the hash's read still waits for the resolver while the import locks are held. #20580 also targets #15742: it queues the run's traps in `importRunAt` before the import starts and orders the resolver thread against `GpuMutex`. A trap set after that check still reaches `BeginHostWrite`. The new test sets the trap from the unwatch hook that `ImportWatched` calls after `importRunAt`'s checks. It hangs on `main` and on `main` + #20580 with the same stack, and passes with this change. This pull request does not depend on #20580. ### Tested Apple M5 Pro, macOS 27.3, MoltenVK (Vulkan SDK 1.4.363), x86_64 Release under Rosetta 2, `main` `d4cca38480`. The same build directory for `main` and this change. `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`. - New `agc_driver_import_over_read_trap`: 8 of 8 runs pass with this change. On `main`, 3 of 3 runs hang until the test's 10 s `alarm` (SIGALRM); on `main` plus #20580's non-test changes, 3 of 3 hang the same way. A `sample` of each hang shows `ImportWatched -> GuestArenaBeginHostWrite -> GuestWriteWatchBeginHostWrite -> Watch::resolve -> Resolve -> lockTracker`. - 99 tests matching `write_watch|host_import|import_watch|read_trap|label|guest_arena|host_write|write_track`, run on each side without and then with `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer` (Khronos validation layer 1.4.363; each side's log shows 53 `Insert instance layer` lines, no `was not found`, no `VUID-`): the same 6 fail on `main` and with this change, with identical messages (`agc_driver_label_ordered_flush`, `_unimported`, `agc_driver_unimported_label_order` and its `_draw_pipeline` and `_plan_verify` variants, `agc_write_tracking`); 11 were not built on either side. Everything else passes on both. - Title: Grand Theft Auto V (PPSA04264), with a pad script that reaches the prologue, on a tree that also has #17425, #11049 and #25978 (which this machine needs to build and run it) and a local workaround for a separate lock-order inversion in `StoreDrawnLinear`. Without this change, 2 of 2 runs that got past start-up froze in the prologue, with `sample` showing the stack above (`ResolveLabelTrap -> lockTracker`). With it, 3 of 3 runs played through the pad script into the prologue and ran until a GPU hang that is not caused by this change. - `tools/check_conventions.py --base upstream/main` and `tools/debt_fold.py --check` pass. Not run on Linux or Windows, where the new export only forwards to the existing host write. The test is built and registered only on macOS, the one platform where this refusal exists. ### 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: Fixes #15742 - [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)
MarcZX10 commented 2026-11-25 02:54:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T02:54:42Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change, and the inspected implementation and regression test support merging.

What I ran

Reviewed head 812e1df8fe1f7792288dd472c4eb7f4a75e63b7a. No build or test commands were run; the description reports author-run test results separately.

Results

Check Result Note
Changed code review passed Core paths and test inspected
Build and tests not run Author reports results in the PR description

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full text of core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp was excluded from the prepared review bundle as too large; only its changed range and adjacent importRunAt logic were read through the repository reader. All other changed files listed as reviewed in manifest.json were read. Build/test results and behavior on Linux, Windows, and physical consoles were not independently verified. files.json was not provided as a separate document; the bundle listing and manifest identify the excluded file.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6051158862 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26221#pullrequestreview-6051158862). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-25T02:54:42Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change, and the inspected implementation and regression test support merging. ## What I ran Reviewed head `812e1df8fe1f7792288dd472c4eb7f4a75e63b7a`. No build or test commands were run; the description reports author-run test results separately. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | Core paths and test inspected | | Build and tests | not run | Author reports results in the PR description | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The full text of `core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp` was excluded from the prepared review bundle as too large; only its changed range and adjacent `importRunAt` logic were read through the repository reader. All other changed files listed as reviewed in `manifest.json` were read. Build/test results and behavior on Linux, Windows, and physical consoles were not independently verified. `files.json` was not provided as a separate document; the bundle listing and manifest identify the excluded file. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-25 22:29:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T22:29:19Z.

Review: APPROVED


Reviewed — looks good

The change at head 9a938510af446d18bb8a6f2192796e8c7a8f3185 refuses a watched host import when a read trap overlaps it, then queues trap resolution without waiting under the import locks.

What I ran

No commands were run; this review used the prepared diff and pinned reader. I did not run the author-reported builds or tests.

Results

Check Result Note
Changed code review passed Relevant changed paths and callers inspected
Build and tests not run Results in the description are author-reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not independently verify the reported macOS runs, platform behavior, or Linux and Windows builds. The full text of GuestBufferMemory.cpp was too large for the review folder; I read the changed hunk and the relevant importRunAt implementation and callers through the pinned reader. The excluded debt document docs/dev/debt/changes/fix-agc-import-read-trap-deadlock.md was not reviewed.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6065871572 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26221#pullrequestreview-6065871572). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-25T22:29:19Z. **Review:** APPROVED --- # Reviewed — looks good The change at head `9a938510af446d18bb8a6f2192796e8c7a8f3185` refuses a watched host import when a read trap overlaps it, then queues trap resolution without waiting under the import locks. ## What I ran No commands were run; this review used the prepared diff and pinned reader. I did not run the author-reported builds or tests. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | Relevant changed paths and callers inspected | | Build and tests | not run | Results in the description are author-reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not independently verify the reported macOS runs, platform behavior, or Linux and Windows builds. The full text of `GuestBufferMemory.cpp` was too large for the review folder; I read the changed hunk and the relevant `importRunAt` implementation and callers through the pinned reader. The excluded debt document `docs/dev/debt/changes/fix-agc-import-read-trap-deadlock.md` was not reviewed. ## To get this to a good state Nothing is required.
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!26221
No description provided.