fix(libkernel): run APR buffers past an unsatisfied wait on their own queue #3588

Merged
tv7 merged 3 commits from upstream-pr/3588 into main 2026-10-11 21:51:13 +00:00
tv7 commented 2026-10-11 13:50:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tv7 on 2026-10-11T13:50:23Z.


What

sceKernelAprSubmitCommandBuffer and the AMM submit ran the whole buffer on the submitting thread. A WaitOnAddress / WaitOnCounter the submitter itself satisfies after the submit returns therefore never returned. A buffer now runs on the submitting thread until such an unsatisfied wait; the rest of it, and every APR or AMM buffer submitted after it (order kept), runs on one APR thread. Buffers without such a wait behave as before. sceKernelAprSubmitCommandBufferAndGetId returns the submission id; sceKernelAprWaitCommandBuffer and sceAmprAmmWaitCommandBufferCompletion wait for completion.

Marvel's Wolverine (PPSA03671): the Archive Asset IO thread submits a buffer with a WaitOnAddress while holding the archive mutex and sets the address later. On main it spins in _execute, the main thread blocks on that mutex, and loading stops about 40 s in.

Tested

Windows 11, RTX 3070, GCC 15.2.0 UCRT, Release, on main a11c8f7f7.

  • New TestWaitSatisfiedAfterSubmission in GuestAmpr.cpp: the submit of a buffer waiting on an address the test sets afterwards returns, a second buffer does not run before the first, both complete once the address is set. Fails on main (submit does not return within 5 s), passes with the fix.
  • ctest 831/836. The 5 others fail the same way on main on this machine (guest_kernel_sys symlink not permitted, windows_exceptions, guest_filesystem, agc_driver_emulated_color_compare) or only under -j8 (agc_driver_prepared_shaders_debug, passes alone).
  • tools/check_conventions.py passes.
  • Wolverine, with this fix on an earlier main plus other open PRs: loading continues and the intro videos play. Not retested on this exact base.

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 in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes, Claude Code

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4819538992 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3588). Originally posted by [tv7](https://github.com/tv7) on 2026-10-11T13:50:23Z. --- ### What `sceKernelAprSubmitCommandBuffer` and the AMM submit ran the whole buffer on the submitting thread. A `WaitOnAddress` / `WaitOnCounter` the submitter itself satisfies after the submit returns therefore never returned. A buffer now runs on the submitting thread until such an unsatisfied wait; the rest of it, and every APR or AMM buffer submitted after it (order kept), runs on one APR thread. Buffers without such a wait behave as before. `sceKernelAprSubmitCommandBufferAndGetId` returns the submission id; `sceKernelAprWaitCommandBuffer` and `sceAmprAmmWaitCommandBufferCompletion` wait for completion. Marvel's Wolverine (PPSA03671): the `Archive Asset IO` thread submits a buffer with a `WaitOnAddress` while holding the archive mutex and sets the address later. On main it spins in `_execute`, the main thread blocks on that mutex, and loading stops about 40 s in. ### Tested Windows 11, RTX 3070, GCC 15.2.0 UCRT, Release, on main `a11c8f7f7`. - New `TestWaitSatisfiedAfterSubmission` in `GuestAmpr.cpp`: the submit of a buffer waiting on an address the test sets afterwards returns, a second buffer does not run before the first, both complete once the address is set. Fails on main (submit does not return within 5 s), passes with the fix. - ctest 831/836. The 5 others fail the same way on main on this machine (`guest_kernel_sys` symlink not permitted, `windows_exceptions`, `guest_filesystem`, `agc_driver_emulated_color_compare`) or only under `-j8` (`agc_driver_prepared_shaders_debug`, passes alone). - `tools/check_conventions.py` passes. - Wolverine, with this fix on an earlier main plus other open PRs: loading continues and the intro videos play. Not retested on this exact base. ### Checklist - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes, Claude Code 🤖 Generated with [Claude Code](https://claude.com/claude-code)
SleglJan commented 2026-10-11 14:46:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-11T14:46:12Z.


Linux check with 007 First Light (PPSA11386 v01.018.000), which stops at row 6 of #2523 (the APR-submit hang): its loading thread records a WaitOnAddress followed by file reads, submits them through sceKernelAprSubmitCommandBufferAndGetId, and writes the awaited value only after the submit returns. Setup: CachyOS, RTX 3080, GCC 16.2.1, Release, run in a bubblewrap sandbox:

  • main a11c8f7f7 (240 s run): the log stops after 141 shader-cache misses with no further output. That's row 6.
  • main + this PR (32a3047b5): 007 passes row 6 and runs on to row 8, VOP3 source modifiers are not implemented (#2463 (closed) / SleglJan/AnyPS5#21). Row 7 is already merged.

On the same machine TestWaitSatisfiedAfterSubmission (in guest_ampr_markers) passes with this PR. guest_ampr_markers and guest_ampr_threads both pass 10 repeats.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6110213010 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3588#issuecomment-6110213010). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-11T14:46:12Z. --- Linux check with 007 First Light (PPSA11386 v01.018.000), which stops at row 6 of #2523 (the APR-submit hang): its loading thread records a `WaitOnAddress` followed by file reads, submits them through `sceKernelAprSubmitCommandBufferAndGetId`, and writes the awaited value only after the submit returns. Setup: CachyOS, RTX 3080, GCC 16.2.1, Release, run in a bubblewrap sandbox: - **`main` a11c8f7f7** (240 s run): the log stops after 141 shader-cache misses with no further output. That's row 6. - **`main` + this PR (32a3047b5)**: 007 passes row 6 and runs on to row 8, `VOP3 source modifiers are not implemented` (#2463 (closed) / SleglJan/AnyPS5#21). Row 7 is already merged. On the same machine `TestWaitSatisfiedAfterSubmission` (in `guest_ampr_markers`) passes with this PR. `guest_ampr_markers` and `guest_ampr_threads` both pass 10 repeats. AI-assisted: yes (Claude Code)
tv7 commented 2026-10-11 17:53:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tv7 on 2026-10-11T17:53:12Z.


Pushed a follow-up commit, fix(libkernel): give AMM buffers their own queue. The first version put APR and AMM buffers in one queue, and that deadlocks Marvel's Wolverine a little later in loading:

  • APR buffer 449 (priority 5) starts with WaitOnAddress on a counter in the title's data, waiting for 0x...9b. The counter was at 0x...98.
  • That counter is advanced only by the WriteAddress commands of AMM buffers (each AMM buffer is a run of maps, each followed by a WriteAddress of the next counter value). The AMM buffer that writes 0x...99 to 0x...9b was submitted after 449, so the shared queue held it behind 449. Neither ran again, and every later APR and AMM buffer queued behind them.
  • In the game, loading stopped about 30 s after the first GPU work. The render thread spins in its "still loading" loop and the GPU sits idle about 465 ms per frame (about 1.5 FPS, after the intro videos a black screen).

I also tried letting every waiting buffer go independently. That breaks Wolverine differently: a later APR buffer then reads into memory that is not ready yet (APR: read failed). So order within APR matters, and AMM is a separate stream. Each engine now has its own queue and thread. Order is kept inside each one, and a wait in one does not hold up the other.

New test TestAmmNotBehindAprWait (guest_ampr_markers): an APR buffer waits on an address that an AMM buffer submitted after it writes. It fails on the previous head of this PR (sceAmprAmmWaitCommandBufferCompletion does not return within 5 s) and passes now.

Wolverine with this head: loading goes on past that point and reaches a new stop, a D16_UNORM_S8_UINT depth target that NVIDIA cannot attach (separate PR).

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6111931833 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3588#issuecomment-6111931833). Originally posted by [tv7](https://github.com/tv7) on 2026-10-11T17:53:12Z. --- Pushed a follow-up commit, `fix(libkernel): give AMM buffers their own queue`. The first version put APR and AMM buffers in one queue, and that deadlocks Marvel's Wolverine a little later in loading: - APR buffer 449 (priority 5) starts with `WaitOnAddress` on a counter in the title's data, waiting for `0x...9b`. The counter was at `0x...98`. - That counter is advanced only by the `WriteAddress` commands of AMM buffers (each AMM buffer is a run of maps, each followed by a `WriteAddress` of the next counter value). The AMM buffer that writes `0x...99` to `0x...9b` was submitted after 449, so the shared queue held it behind 449. Neither ran again, and every later APR and AMM buffer queued behind them. - In the game, loading stopped about 30 s after the first GPU work. The render thread spins in its "still loading" loop and the GPU sits idle about 465 ms per frame (about 1.5 FPS, after the intro videos a black screen). I also tried letting every waiting buffer go independently. That breaks Wolverine differently: a later APR buffer then reads into memory that is not ready yet (`APR: read failed`). So order within APR matters, and AMM is a separate stream. Each engine now has its own queue and thread. Order is kept inside each one, and a wait in one does not hold up the other. New test `TestAmmNotBehindAprWait` (`guest_ampr_markers`): an APR buffer waits on an address that an AMM buffer submitted after it writes. It fails on the previous head of this PR (`sceAmprAmmWaitCommandBufferCompletion` does not return within 5 s) and passes now. Wolverine with this head: loading goes on past that point and reaches a new stop, a `D16_UNORM_S8_UINT` depth target that NVIDIA cannot attach (separate PR). AI-assisted: yes (Claude Code)
adriadam10 commented 2026-10-11 21:38:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:38:51Z.


Checked on current main: build and the libs/libkernel ctest scope pass (112/112); guest_ampr_markers fails without the change and passes with it. Matches @SleglJan's Linux run.

Added in a commit of our own: a "APR and AMM command buffers" section in docs/dev/ARCHITECTURE.md, since the PR adds a worker thread and two queues. Note that a command that throws on the worker now aborts the process instead of throwing on the submitter.

Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114066369 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3588#issuecomment-6114066369). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T21:38:51Z. --- Checked on current main: build and the libs/libkernel ctest scope pass (112/112); `guest_ampr_markers` fails without the change and passes with it. Matches @SleglJan's Linux run. Added in a commit of our own: a "APR and AMM command buffers" section in `docs/dev/ARCHITECTURE.md`, since the PR adds a worker thread and two queues. Note that a command that throws on the worker now aborts the process instead of throwing on the submitter. Merging once CI is green.
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!3588
No description provided.