fix(libSceAgc): serialize command buffer allocation per buffer #20017

Merged
slopezjur merged 2 commits from upstream-pr/20017 into main 2026-11-16 08:14:13 +00:00
slopezjur commented 2026-11-14 11:57:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slopezjur on 2026-11-14T11:57:32Z.


What

Agc::Command::Allocate read cursor_up, reserved space and advanced the cursor with no synchronization. Ghost of Yōtei's main thread and one of its job workers append packets to the same compute command buffer at the same time, so both threads could get the same range and overwrite each other's packets. The driver then rejected the submission (a DISPATCH_DIRECT header where the previous dispatch's group counts belong) and the title terminated at about frame 470.

  • Allocate now serializes Reserve and the cursor advance per buffer. The state is 64 mutexes striped by the CommandBuffer address, so no state is added to the guest-visible CommandBuffer struct.
  • The stripe mutex is not held while the buffer's allocation callback runs (second commit, 2fc3b2c44a, after a review note from @Celegans12 in #675: two buffers can share a stripe, so a callback waiting for a thread that appends to another buffer on the same stripe would deadlock). Instead the buffer is marked as growing: other threads appending to that buffer wait until the callback returns, the callback's own thread can still append to it, and other buffers on the stripe are not blocked.
  • Packet contents are still written after Allocate returns, outside the lock, into ranges no other thread can be given.
  • New test agc_concurrent_allocation, three cases. Eight threads append 20,000 NOPs each to one buffer, each thread with its own NOP size; it checks that the cursor ends exactly at the total size, that every packet has an intact NOP header and zero payload, and that each thread's packet count is complete. A buffer whose callback waits for another thread to append to 256 other buffers must not block those appends. An append to a buffer whose callback is running waits for it, lands in the grown storage and does not call the callback a second time; the callback itself appends to the buffer.

Tested

The branch was rebased onto main 4aa2f8b694 after the runs below (4d15524ac3 was 38f7e5c8da, 2fc3b2c44a was cc8992ea95); the code is unchanged and the runs were not repeated on the new base.

Windows 11, WinLibs GCC 15.2.0 r7, Release, NVIDIA GeForce RTX 4080 SUPER (driver 621.14), Vulkan SDK 1.4.363.0 validation layer.

  • agc_concurrent_allocation against main's Packet.cpp (test only, without the lock): fails 3 of 3 runs (concurrent allocations lost cursor advances).
  • agc_concurrent_allocation on this branch (2fc3b2c44a): passes 5 of 5 runs. With the first commit's lock (stripe mutex held across the callback) the second case fails: appends to other buffers waited for a buffer that was growing.
  • Focused tests on main 2cebbe43ee and on this branch, same machine, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer (ctest -R <pattern> -V): agc_initialization, agc_commands, agc_rewind_patch, agc_static_buffer, agc_nop_and_compute_event, plus the draw write range, write-tracking, snapshot, BDA, GDS and shader disk cache tests.
tests failed layer inserted VUID-
main 49 0 31 of 31 Vulkan processes 0
this PR (2fc3b2c44a) 50 0 31 of 31 0

Ghost of Yōtei

PPSA26344 v02.002.000, DRS 1080p patch, same save restored before every run, runs alternated between the two builds, each capped at 400 s:

build runs unsupported PM4 packet type aborts ran to the cap furthest present
main 2cebbe43ee 3 2 (at 35 s and ~126 s; headers 0x00000008 and 0x40010000, payload dwords read as headers) 0 (third run: access violation in title code at 56 s) 830
this PR 3 0 2 995

The game runs in this section were made with the first commit (4d15524ac3) and have not been repeated with 2fc3b2c44a.

Over all 21 runs with this PR (including runs with two other changes measured on top of it, 150–400 s each) there was no PM4 abort. Three of them still stopped: twice an access violation in title code during the load (48 s; main hits it too), and once sceAgcAcbPopMarker: invalid command buffer cursors at 35 s. Inside libSceAgc only Allocate moves cursor_up, so the cursors were changed by the title's own code while another thread appended; this lock cannot cover that, and I have no general fix for it yet.

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: PPSA26344 #675
  • GPUs tested: NVIDIA GeForce RTX 4080 SUPER, driver 621.14, Windows 11
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5310982206 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20017). Originally posted by [slopezjur](https://github.com/slopezjur) on 2026-11-14T11:57:32Z. --- ### What `Agc::Command::Allocate` read `cursor_up`, reserved space and advanced the cursor with no synchronization. Ghost of Yōtei's main thread and one of its job workers append packets to the same compute command buffer at the same time, so both threads could get the same range and overwrite each other's packets. The driver then rejected the submission (a `DISPATCH_DIRECT` header where the previous dispatch's group counts belong) and the title terminated at about frame 470. - `Allocate` now serializes `Reserve` and the cursor advance per buffer. The state is 64 mutexes striped by the `CommandBuffer` address, so no state is added to the guest-visible `CommandBuffer` struct. - The stripe mutex is not held while the buffer's allocation callback runs (second commit, 2fc3b2c44a, after a review note from @Celegans12 in #675: two buffers can share a stripe, so a callback waiting for a thread that appends to another buffer on the same stripe would deadlock). Instead the buffer is marked as growing: other threads appending to that buffer wait until the callback returns, the callback's own thread can still append to it, and other buffers on the stripe are not blocked. - Packet contents are still written after `Allocate` returns, outside the lock, into ranges no other thread can be given. - New test `agc_concurrent_allocation`, three cases. Eight threads append 20,000 NOPs each to one buffer, each thread with its own NOP size; it checks that the cursor ends exactly at the total size, that every packet has an intact NOP header and zero payload, and that each thread's packet count is complete. A buffer whose callback waits for another thread to append to 256 other buffers must not block those appends. An append to a buffer whose callback is running waits for it, lands in the grown storage and does not call the callback a second time; the callback itself appends to the buffer. ### Tested The branch was rebased onto `main` 4aa2f8b694 after the runs below (4d15524ac3 was 38f7e5c8da, 2fc3b2c44a was cc8992ea95); the code is unchanged and the runs were not repeated on the new base. Windows 11, WinLibs GCC 15.2.0 r7, Release, NVIDIA GeForce RTX 4080 SUPER (driver 621.14), Vulkan SDK 1.4.363.0 validation layer. - `agc_concurrent_allocation` against `main`'s `Packet.cpp` (test only, without the lock): fails 3 of 3 runs (`concurrent allocations lost cursor advances`). - `agc_concurrent_allocation` on this branch (2fc3b2c44a): passes 5 of 5 runs. With the first commit's lock (stripe mutex held across the callback) the second case fails: `appends to other buffers waited for a buffer that was growing`. - Focused tests on `main` 2cebbe43ee and on this branch, same machine, with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `VK_LOADER_DEBUG=layer` (`ctest -R <pattern> -V`): `agc_initialization`, `agc_commands`, `agc_rewind_patch`, `agc_static_buffer`, `agc_nop_and_compute_event`, plus the draw write range, write-tracking, snapshot, BDA, GDS and shader disk cache tests. | | tests | failed | layer inserted | `VUID-` | |---|---|---|---|---| | `main` | 49 | 0 | 31 of 31 Vulkan processes | 0 | | this PR (2fc3b2c44a) | 50 | 0 | 31 of 31 | 0 | #### Ghost of Yōtei PPSA26344 v02.002.000, DRS 1080p patch, same save restored before every run, runs alternated between the two builds, each capped at 400 s: | build | runs | `unsupported PM4 packet type` aborts | ran to the cap | furthest present | |---|---|---|---|---| | `main` 2cebbe43ee | 3 | 2 (at 35 s and ~126 s; headers `0x00000008` and `0x40010000`, payload dwords read as headers) | 0 (third run: access violation in title code at 56 s) | 830 | | this PR | 3 | 0 | 2 | 995 | The game runs in this section were made with the first commit (4d15524ac3) and have not been repeated with 2fc3b2c44a. Over all 21 runs with this PR (including runs with two other changes measured on top of it, 150–400 s each) there was no PM4 abort. Three of them still stopped: twice an access violation in title code during the load (48 s; `main` hits it too), and once `sceAgcAcbPopMarker: invalid command buffer cursors` at 35 s. Inside libSceAgc only `Allocate` moves `cursor_up`, so the cursors were changed by the title's own code while another thread appended; this lock cannot cover that, and I have no general fix for it yet. ### 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: PPSA26344 #675 - [x] GPUs tested: NVIDIA GeForce RTX 4080 SUPER, driver 621.14, Windows 11 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-14 12:40:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T12:40:51Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; I reviewed all three changed files, and no verified blocking defect was found.

What I ran

Reviewed head 38f7e5c8da2a31e0fde7a27187472debeacc1d76 using repo_read.py to inspect the prepared diff and source. No test commands were run.

Results

Check Result Note
Changed source review passed All three changed files read
Tests not run Results in the description are author reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests were run. The reported Windows and console results were not independently verified. review/files.json was not available through the reader; the manifest lists three changed files, all included and read. Excluded files were not listed in the available bundle.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5904370425 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20017#pullrequestreview-5904370425). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-14T12:40:51Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; I reviewed all three changed files, and no verified blocking defect was found. ## What I ran Reviewed head `38f7e5c8da2a31e0fde7a27187472debeacc1d76` using `repo_read.py` to inspect the prepared diff and source. No test commands were run. ## Results | Check | Result | Note | |---|---|---| | Changed source review | passed | All three changed files read | | Tests | not run | Results in the description are author reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No tests were run. The reported Windows and console results were not independently verified. `review/files.json` was not available through the reader; the manifest lists three changed files, all included and read. Excluded files were not listed in the available bundle. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-14 19:20:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T19:20:11Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the inspected allocator and callback paths support the per-buffer serialization described in the PR.

What I ran

  • Read the prepared diff and changed files at head cc8992ea952d945181a570212dc0d04f6d26dfee, plus targeted packet writer and caller context through repo_read.py.
  • No build or tests were run. The PR description reports tests, but those results were not independently verified.

Results

Check Result Note
Changed source review passed All three changed files read
Build and tests not run Author-reported results only

Findings

Blocking

None.

Non-blocking

None.

Not verified

  • Build, tests, and runtime behavior were not independently verified.
  • Read files: core/libs/prx/libSceAgc/CMakeLists.txt, core/libs/prx/libSceAgc/Command/src/Packet.cpp, core/libs/prx/libSceAgc/tests/ConcurrentAllocation.cpp; targeted context: core/libs/prx/libSceAgc/Cb/src/Sync.cpp, core/libs/prx/libSceAgc/Command/src/Memory.cpp, core/libs/prx/libSceAgc/Acb/src/Control.cpp.
  • No changed file was excluded; files.json was not available as a review document.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5906681468 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20017#pullrequestreview-5906681468). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-14T19:20:11Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the inspected allocator and callback paths support the per-buffer serialization described in the PR. ## What I ran - Read the prepared diff and changed files at head `cc8992ea952d945181a570212dc0d04f6d26dfee`, plus targeted packet writer and caller context through `repo_read.py`. - No build or tests were run. The PR description reports tests, but those results were not independently verified. ## Results | Check | Result | Note | |---|---|---| | Changed source review | passed | All three changed files read | | Build and tests | not run | Author-reported results only | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified - Build, tests, and runtime behavior were not independently verified. - Read files: `core/libs/prx/libSceAgc/CMakeLists.txt`, `core/libs/prx/libSceAgc/Command/src/Packet.cpp`, `core/libs/prx/libSceAgc/tests/ConcurrentAllocation.cpp`; targeted context: `core/libs/prx/libSceAgc/Cb/src/Sync.cpp`, `core/libs/prx/libSceAgc/Command/src/Memory.cpp`, `core/libs/prx/libSceAgc/Acb/src/Control.cpp`. - No changed file was excluded; `files.json` was not available as a review document. ## 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!20017
No description provided.