Skip to content

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

Open
Gonic0 wants to merge 3 commits into
boykopovar:mainfrom
Gonic0:fix/agc-import-read-trap-deadlock
Open

Gonic0 wants to merge 3 commits into
boykopovar:mainfrom
Gonic0:fix/agc-import-read-trap-deadlock

Conversation

@Gonic0

@Gonic0 Gonic0 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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 fix(libc): order read-trap resolution against the GPU mutex #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 fix(libcurl): build the curl backend for the macOS target architecture #17425, build(audio): link one SDL copy on macOS #11049 and feat(shader): emulate 64-bit GDS atomics on devices without them #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 and the test skips. #19054 (Linux read traps) edits the #ifdef lines around the same ImportWatched call; whichever lands second needs a small rebase.

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 macOS: a read trap resolved on the importing thread deadlocks ImportWatched #15742
  • Unblocks: PPSA04264 Grand Theft Auto V (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

…f resolving the trap under the import locks

On macOS, an import opens a host write over its pages while it holds the
host import mutex and the write tracker's lock. A label read trap set on
those pages after the import's access check made the host write run the
trap's resolver on the importing thread, and the driver's resolver needs
both locks to store the pending image, so the thread waited for itself
(boykopovar#15742).

The import now opens its host write with a non-resolving attempt that
checks for traps and registers the write under the watch's lock in one
step. If a trap covers the pages, or another thread is resolving one,
the attempt opens nothing and hands the traps to the watch's resolver
thread. The import is refused without being remembered as failed, so the
request falls back to copies and a later one imports the run.

Fixes boykopovar#15742

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Other open pull requests say they fix the same issue, so they may implement the same thing:

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1835 core/libs/prx/libc/src/GuestArena.cpp
PR 4448 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 5372 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 6186 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 6188 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6192 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6197 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 6209 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6481 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 7642 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 9508 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 9852 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 10193 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 10492 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 10497 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 12157 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 12792 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libc/include/GuestWriteWatch.hpp
core/libs/prx/libc/src/GuestWriteWatch.cpp
PR 12840 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 12944 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 13148 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 13863 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 13920 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 13928 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 13929 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 14473 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 15136 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 15295 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 15296 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 15297 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 15570 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 15621 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 16831 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 16879 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 18173 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 18390 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 20084 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 20315 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
core/libs/prx/libc/include/GuestArena.hpp
core/libs/prx/libc/src/GuestArena.cpp
PR 20580 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
core/libs/prx/libc/include/GuestWriteWatch.hpp
core/libs/prx/libc/src/GuestWriteWatch.cpp
PR 21155 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 21649 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 22062 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 22190 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 22434 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 22550 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 22647 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 25610 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 26340 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp

@4le22io

4le22io commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

A Windows + NVIDIA check of the current head 59a57c1 for regressions, since the change touches shared GuestWriteWatch/GuestArena code. No regressions. As expected, the new test skips here (skipped: read traps exist only on macOS), so this run says nothing about the macOS deadlock itself.

System: Windows 11 Pro 10.0.26200, NVIDIA GeForce RTX 4060 (10de:2882, Ada), driver 621.01, Intel Core i5-12400F, WinLibs GCC 15.2.0 r7 (UCRT), Release, BUILD_TESTING=ON. Environment: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer (Vulkan SDK 1.4.363.1).

Trees: main 47e83fc, and main + 59a57c1 (git merge --no-ff, clean merge, 9 files).

Run: ctest -R '^.*(write_watch|host_import|import_watch|read_trap|label|guest_arena|host_write|write_track).*$' -V, the same pattern as the description. 3 runs per tree:

tree tests result VUID Insert instance layer
main 98 95 pass, 3 skip, every run 0 76
this PR 99 95 pass, 4 skip, every run 0 76
  • The 3 skips on both trees are agc_driver_host_import_dma_buf_watch, agc_driver_host_write_shadowed_unit and agc_host_import_sparse. With this PR, the fourth skip is the new agc_driver_import_over_read_trap.
  • None of the 6 failures listed for macOS (agc_driver_label_ordered_flush, agc_write_tracking, …) fail on Windows, on either tree.

ctest -R '^agc' -j 8 with the layer: 2100 tests on main and 2101 with this PR. Both trees have the same single failure, agc_driver_store_over_untracked (#22670, fix in #22764), already tracked on this GPU. The layer was inserted 1913 times on both trees. There are 0 was not found but was requested lines and 0 VUID- lines.

I didn't count a control for this PR: with main's GuestMemory.cpp, GuestBufferMemory.cpp and libc write-watch files, the results on Windows are the same as with this PR, because the new test skips.

AI-assisted: yes (Claude Code). It merged the PR, ran the builds and the test sets, and drafted and posted this comment.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@Gonic0 I pushed 8c5e89a on top of your head: the skip line now reads "skipped, read traps exist only on macOS". The test runner's skip check only accepts a reason after "skipped," or "SKIP:", so the colon form failed every Linux batch this PR was in.

One question for you: #19054 adds read traps on Linux. Once it lands, should this test run on Linux too instead of skipping under __APPLE__?

@Gonic0

Gonic0 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @oneandonlydean, 8c5e89a is right: tools/check_skips.py only reads a reason after skipped, or SKIP:, so the colon form could not be classified.

On Linux: yes. Once #19054 lands, GuestWriteWatchTrapReads and the read-trap resolver exist there too, so agc_driver_import_over_read_trap should run on Linux instead of skipping under __APPLE__. Until then the API is macOS-only on main, so I'd keep this PR as it is and widen the guard in a follow-up after #19054 is merged.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 24, 2026
…-trap-deadlock

# Conflicts:
#	core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 24, 2026
@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown

RTX 3070 run of 812e1df8fe, merged with --no-ff onto main 6fde67a5eb, against main alone.

Tested

  • NVIDIA GeForce RTX 3070 (driver 616.56, subgroup 32), Windows 11 Pro 26100, WinLibs GCC 15.2.0, Ninja, Release; ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation.
  • ctest -R "^agc_driver_import_over_read_trap|^agc_import_settle_", 3 runs each:
    • main: 11/11 passed in all 3 runs.
    • this PR: 11/11 passed in all 3 runs.
  • Validation layer inserted in every Vulkan test (11 per run on the PR side), 0 VUID- messages. Build: 0 failed targets.

Not tested: AMD, titles.

AI-assisted (Claude Code).

@github-actions github-actions Bot added the review queue Next in the maintainer review queue: checks pass, no open change request, no conflict label Nov 25, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build docs fix error Closes an issue (Fixes #N) libc review queue Next in the maintainer review queue: checks pass, no open change request, no conflict unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants