fix(agc): load each REWIND control once with acquire in copySegment #17411

Merged
chitters2004 merged 3 commits from upstream-pr/17411 into main 2026-11-12 23:24:25 +00:00
chitters2004 commented 2026-11-08 19:08:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by chitters2004 on 2026-11-08T19:08:07Z.


What

Driver::copySegment copied every packet from guest memory with a plain insert, including a REWIND packet and its control dword. A title releases one gate and then writes the next gate's control through sceAgcRewindPatchSetRewindState (a release store) while the queue worker copies the tail holding that next REWIND, so the plain copy raced the atomic store.

A REWIND is exactly two dwords (Pm4.cpp sizes it with size(2), sceAgcDcbRewindGetSize returns 8 bytes), so copySegment now requires that and takes the control with one atomic_ref acquire load, the pair of the release store and the order executeRewindTail and noteReleasedTails already use. Other packets are copied as before. The replay tool writes controls in restoreGroup with a plain copy instead; that is fixed separately in #17338.

Tests in tests/Pm4.cpp: agc_driver_pm4_rewind_copy (a writer releases gates while the queue copies the tails behind them) and agc_driver_pm4_rewind_size (a REWIND that is not two dwords throws in copySegment; main already rejected it later, in validation, with invalid packet size). The race is only visible to TSan, which upstream CI does not run (sanitizers.yml runs ASan and UBSan), so the TSan runs below are the evidence; without TSan rewind_copy passes on main too.

Tested

  • Linux, TSan (fork CI, same tests 1 + 31 serial repeats each, fork-only repeat commits): rewind_copy main 0/32 pass (run 42510041542) (data race copySegment read vs SetRewindState store, 32 reports), this change 32/32 and 0 reports (run 42510039286), mutant (plain copy of the control restored) 0/32 (run 42510043667). rewind_size main 0/32 (different message), change 32/32, mutant 32/32.
  • Windows, RTX 3080, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ctest -R agc_driver_pm4: change 28/28 pass, main 27/28 (only the new rewind_size); neither log contains was not found but was requested, neither contains VUID-.
  • agc_driver_pm4_rewind_trace also fails under TSan on main; it is a different race (a plain label read in Pm4::WaitSatisfiedUnchecked against the test's atomic store) and is not changed here.

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: NVIDIA GeForce RTX 3080, driver 621.14, Windows 11 (the Linux TSan runs use no GPU)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5214232866 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17411). Originally posted by [chitters2004](https://github.com/chitters2004) on 2026-11-08T19:08:07Z. --- ### What `Driver::copySegment` copied every packet from guest memory with a plain `insert`, including a REWIND packet and its control dword. A title releases one gate and then writes the next gate's control through `sceAgcRewindPatchSetRewindState` (a release store) while the queue worker copies the tail holding that next REWIND, so the plain copy raced the atomic store. A REWIND is exactly two dwords (`Pm4.cpp` sizes it with `size(2)`, `sceAgcDcbRewindGetSize` returns 8 bytes), so `copySegment` now requires that and takes the control with one `atomic_ref` acquire load, the pair of the release store and the order `executeRewindTail` and `noteReleasedTails` already use. Other packets are copied as before. The replay tool writes controls in `restoreGroup` with a plain copy instead; that is fixed separately in #17338. Tests in `tests/Pm4.cpp`: `agc_driver_pm4_rewind_copy` (a writer releases gates while the queue copies the tails behind them) and `agc_driver_pm4_rewind_size` (a REWIND that is not two dwords throws in `copySegment`; `main` already rejected it later, in validation, with `invalid packet size`). The race is only visible to TSan, which upstream CI does not run (`sanitizers.yml` runs ASan and UBSan), so the TSan runs below are the evidence; without TSan `rewind_copy` passes on `main` too. ### Tested - Linux, TSan (fork CI, same tests 1 + 31 serial repeats each, fork-only repeat commits): `rewind_copy` main 0/32 pass ([run 42510041542](https://github.com/chitters2004/AnyPS5/actions/runs/42510041542)) (data race `copySegment` read vs `SetRewindState` store, 32 reports), this change 32/32 and 0 reports ([run 42510039286](https://github.com/chitters2004/AnyPS5/actions/runs/42510039286)), mutant (plain copy of the control restored) 0/32 ([run 42510043667](https://github.com/chitters2004/AnyPS5/actions/runs/42510043667)). `rewind_size` main 0/32 (different message), change 32/32, mutant 32/32. - Windows, RTX 3080, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ctest -R agc_driver_pm4`: change 28/28 pass, main 27/28 (only the new `rewind_size`); neither log contains `was not found but was requested`, neither contains `VUID-`. - `agc_driver_pm4_rewind_trace` also fails under TSan on main; it is a different race (a plain label read in `Pm4::WaitSatisfiedUnchecked` against the test's atomic store) and is not changed here. ### 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](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: NVIDIA GeForce RTX 3080, driver 621.14, Windows 11 (the Linux TSan runs use no GPU) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!17411
No description provided.