fix(agc): read a released REWIND tail before replay restores later memory #17642

Merged
chitters2004 merged 8 commits from upstream-pr/17642 into main 2026-11-16 08:08:30 +00:00
chitters2004 commented 2026-11-09 06:22:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by chitters2004 on 2026-11-09T06:22:55Z.


What

A queue copies a REWIND tail, and every indirect buffer the tail calls, only when it reaches the tail. agc_frame_replay released a tail and went straight on to the next memory delta. That delta could overwrite the indirect buffer the tail still pointed at, so the queue read the newer bytes with the older length. On a GTA V capture the result was truncated PM4 packet: a 4-dword indirect buffer whose memory a later delta had refilled with an 8-dword packet. The drain then waited 10 minutes on a worker that had already failed.

  • The driver keeps the tails it has accepted but not yet copied. A tail is added at Submit or when a copied tail chains another, and removed when executeRewindTail copies it. A copy removes one record of its queue and address, so when one REWIND buffer is submitted twice, the second submission's record stays until it copies the tail too. The list is cleared on a driver failure.
  • AgcDriverRewindTailsUnread_nid_postfix reports a released tail that is still unread, unless its queue is parked on a wait that only the title can satisfy and that is not satisfied yet. The comparison is shared with the PM4 wait through Pm4::WaitCompares.
  • Replay waits on it before each memory delta, up to the busy drain timeout. It warns, or throws under --strict.
  • The drain calls AgcDriverCheckFailure_nid_postfix, so a driver failure ends the replay instead of hanging it.

Related: #15136 changes how the capture reads deltas; this change is on the replay side and does not overlap it in behaviour.

Tested

This head 95e6f02b70 is f5d6a66a0a plus two commits:

  • 2d35c8804b: copying a tail removed every record with its address, so after the first of two submissions of one REWIND buffer copied the tail, the replay counted the second submission's copy as read. agc_driver_frame_replay_rewind_shared_tail submits the buffer twice, releases it and holds the first copy's flip in WaitForFlipRoom; it fails on f5d6a66a0a ("the second submission's unread copy of the same tail was forgotten") and passes 5 of 5 with the change.
  • 95e6f02b70 (test only): awaitTailsRead runs inside the replay's settled hold, where a worker leaving a wait cannot move. It returns at once there because the driver does not settle while a released tail is unread: the stalled serial completes after execute(tail), and parkedOnTitle re-reads the control. agc_driver_frame_replay_rewind_settled_tail releases a parked REWIND inside a hold and requires the driver not to report settled, then requires the next hold to find the tail read. A REWIND park counted as parked whatever its control holds fails it.

Linux (Ubuntu 24.04, lavapipe, Mesa llvmpipe LLVM 20.1.2) on 95e6f02b70:

  • ctest -R agc_driver_frame_replay: 47 of 47 pass.
  • ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer with layer 1.4.363.1 and VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1, ctest -R '^agc_driver_frame_replay(_rewind.*|_busy_queue)?$' -V: 13 of 13 pass, 31 Insert instance layer "VK_LAYER_KHRONOS_validation" lines, no missing-layer line, 0 VUID- and 0 SYNC-HAZARD lines. No sync canary ran, so this is not a synchronization-validation claim.

Windows 11, RTX 3080, driver 621.24, on 95e6f02b70, with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer. Each of these exits 0:

  • ctest --test-dir build -R "^agc_driver_frame_replay" -V
  • agc_driver_frame_replay_rewind_reuse with --repeat until-fail:20
  • VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1 ctest -V -R '^agc_driver_frame_replay(_rewind.*|_busy_queue)?$': the rewind and busy-queue frame_replay tests with synchronization validation on. No sync canary ran, so this is not a synchronization-validation claim.

Across the 28 replay logs, 0 lines contain VUID-.

Earlier, on 0363616cf8 (Windows 11 26220, RTX 3080, driver 621.01, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R "^agc_driver_frame_replay" -V), main with the new agc_driver_frame_replay_rewind_reuse test passed 33 of 34: the new test fails on its first run with the replay did not run the REWIND tail as captured. With this change it passes. The test and its registration are unchanged between 0363616cf8 and 95e6f02b70.

A GTA V (PPSA04264) prologue capture recorded before calls format 2, replayed with the version check relaxed locally, using --repeat 5 --no-pace, APS5_DRAW_PIPELINE=1 APS5_PIPELINED_DRAWS=32 APS5_PIPELINE_DISPATCH=1.

The replay needs #17338: without it, it stops at guest writes over native multisample depth are not implemented. So it ran on 95e6f02b70 merged with #17338's head 025f5a150e, built with #20845 for an upright picture on main after #16053.

Before this change, 7 runs logged truncated PM4 packet, and the drain then hung on blocked on nothing. With it, all 5 runs exit 0 and replay all 5 passes:

  • every pass's picture is identical to the captured one (pixels_identical 1.000000, maximum channel difference 0);
  • no pace timeouts, released waits or synthesized submissions;
  • 0 VUID- lines;
  • no truncated PM4 packet, unread-tail or blocked on nothing line.

Replay time: 29.4 s on the cold first run, then 16.2 to 19.6 s.

A second GTA V capture (rk4), --repeat 5 --no-pace, same pipeline environment, same RTX 3080, 5 runs per build. Without this change (main 7085a08145a with #17338, without #20845), all 5 runs stop with truncated PM4 packet. With it (95e6f02b70 merged with #17338's head 025f5a150e, plus #20845), all 5 runs exit 0 with pixels_identical 1.000000 and maximum channel difference 0. Every frame of every run is identical to the captured one.

Merging with #17338: restoreGroup takes #17338's REWIND candidate loop unchanged. It holds every candidate control word out of the bulk copy and writes it afterwards with a release store; on a repeated restore it writes only the clear words. That covers this PR's kept words, including those whose valid bit is clear, so this PR's own loop over them is dropped. In tests/cmake/agc_driver_frame_replay_tests.cmake, the conflict cuts this PR's rewind_shared_tail/rewind_settled_tail foreach before its if(WIN32) PATH block and endforeach(), which sit in the shared lines after the conflict. Both foreach blocks keep their own if(WIN32) block.

Unread REWIND tails are recorded only after agc_frame_replay calls AgcDriverTrackRewindTails_nid_postfix at start-up, so title runs pay one relaxed atomic load per tail instead of tailsMutex.

python3 tools/check_conventions.py --base origin/main passes.

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.24, Windows 11; lavapipe (llvmpipe, Mesa) on Ubuntu 24.04
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5219679228 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17642). Originally posted by [chitters2004](https://github.com/chitters2004) on 2026-11-09T06:22:55Z. --- ### What A queue copies a REWIND tail, and every indirect buffer the tail calls, only when it reaches the tail. `agc_frame_replay` released a tail and went straight on to the next memory delta. That delta could overwrite the indirect buffer the tail still pointed at, so the queue read the newer bytes with the older length. On a GTA V capture the result was `truncated PM4 packet`: a 4-dword indirect buffer whose memory a later delta had refilled with an 8-dword packet. The drain then waited 10 minutes on a worker that had already failed. - The driver keeps the tails it has accepted but not yet copied. A tail is added at `Submit` or when a copied tail chains another, and removed when `executeRewindTail` copies it. A copy removes one record of its queue and address, so when one REWIND buffer is submitted twice, the second submission's record stays until it copies the tail too. The list is cleared on a driver failure. - `AgcDriverRewindTailsUnread_nid_postfix` reports a released tail that is still unread, unless its queue is parked on a wait that only the title can satisfy and that is not satisfied yet. The comparison is shared with the PM4 wait through `Pm4::WaitCompares`. - Replay waits on it before each memory delta, up to the busy drain timeout. It warns, or throws under `--strict`. - The drain calls `AgcDriverCheckFailure_nid_postfix`, so a driver failure ends the replay instead of hanging it. Related: #15136 changes how the capture reads deltas; this change is on the replay side and does not overlap it in behaviour. ### Tested This head `95e6f02b70` is `f5d6a66a0a` plus two commits: - `2d35c8804b`: copying a tail removed every record with its address, so after the first of two submissions of one REWIND buffer copied the tail, the replay counted the second submission's copy as read. `agc_driver_frame_replay_rewind_shared_tail` submits the buffer twice, releases it and holds the first copy's flip in `WaitForFlipRoom`; it fails on `f5d6a66a0a` ("the second submission's unread copy of the same tail was forgotten") and passes 5 of 5 with the change. - `95e6f02b70` (test only): `awaitTailsRead` runs inside the replay's settled hold, where a worker leaving a wait cannot move. It returns at once there because the driver does not settle while a released tail is unread: the stalled serial completes after `execute(tail)`, and `parkedOnTitle` re-reads the control. `agc_driver_frame_replay_rewind_settled_tail` releases a parked REWIND inside a hold and requires the driver not to report settled, then requires the next hold to find the tail read. A REWIND park counted as parked whatever its control holds fails it. Linux (Ubuntu 24.04, lavapipe, Mesa llvmpipe LLVM 20.1.2) on `95e6f02b70`: - `ctest -R agc_driver_frame_replay`: 47 of 47 pass. - `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer` with layer 1.4.363.1 and `VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1`, `ctest -R '^agc_driver_frame_replay(_rewind.*|_busy_queue)?$' -V`: 13 of 13 pass, 31 `Insert instance layer "VK_LAYER_KHRONOS_validation"` lines, no missing-layer line, 0 `VUID-` and 0 `SYNC-HAZARD` lines. No sync canary ran, so this is not a synchronization-validation claim. Windows 11, RTX 3080, driver 621.24, on `95e6f02b70`, with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer`. Each of these exits 0: - `ctest --test-dir build -R "^agc_driver_frame_replay" -V` - `agc_driver_frame_replay_rewind_reuse` with `--repeat until-fail:20` - `VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1 ctest -V -R '^agc_driver_frame_replay(_rewind.*|_busy_queue)?$'`: the rewind and busy-queue frame_replay tests with synchronization validation on. No sync canary ran, so this is not a synchronization-validation claim. Across the 28 replay logs, 0 lines contain `VUID-`. Earlier, on `0363616cf8` (Windows 11 26220, RTX 3080, driver 621.01, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R "^agc_driver_frame_replay" -V`), `main` with the new `agc_driver_frame_replay_rewind_reuse` test passed 33 of 34: the new test fails on its first run with `the replay did not run the REWIND tail as captured`. With this change it passes. The test and its registration are unchanged between `0363616cf8` and `95e6f02b70`. A GTA V (PPSA04264) prologue capture recorded before calls format 2, replayed with the version check relaxed locally, using `--repeat 5 --no-pace`, `APS5_DRAW_PIPELINE=1 APS5_PIPELINED_DRAWS=32 APS5_PIPELINE_DISPATCH=1`. The replay needs #17338: without it, it stops at `guest writes over native multisample depth are not implemented`. So it ran on `95e6f02b70` merged with #17338's head `025f5a150e`, built with #20845 for an upright picture on `main` after #16053. Before this change, 7 runs logged `truncated PM4 packet`, and the drain then hung on `blocked on nothing`. With it, all 5 runs exit 0 and replay all 5 passes: - every pass's picture is identical to the captured one (`pixels_identical` 1.000000, maximum channel difference 0); - no pace timeouts, released waits or synthesized submissions; - 0 `VUID-` lines; - no `truncated PM4 packet`, unread-tail or `blocked on nothing` line. Replay time: 29.4 s on the cold first run, then 16.2 to 19.6 s. A second GTA V capture (rk4), `--repeat 5 --no-pace`, same pipeline environment, same RTX 3080, 5 runs per build. Without this change (main `7085a08145a` with #17338, without #20845), all 5 runs stop with `truncated PM4 packet`. With it (`95e6f02b70` merged with #17338's head `025f5a150e`, plus #20845), all 5 runs exit 0 with `pixels_identical` 1.000000 and maximum channel difference 0. Every frame of every run is identical to the captured one. Merging with #17338: `restoreGroup` takes #17338's REWIND candidate loop unchanged. It holds every candidate control word out of the bulk copy and writes it afterwards with a release store; on a repeated restore it writes only the clear words. That covers this PR's kept words, including those whose valid bit is clear, so this PR's own loop over them is dropped. In `tests/cmake/agc_driver_frame_replay_tests.cmake`, the conflict cuts this PR's `rewind_shared_tail`/`rewind_settled_tail` foreach before its `if(WIN32)` PATH block and `endforeach()`, which sit in the shared lines after the conflict. Both foreach blocks keep their own `if(WIN32)` block. Unread REWIND tails are recorded only after `agc_frame_replay` calls `AgcDriverTrackRewindTails_nid_postfix` at start-up, so title runs pay one relaxed atomic load per tail instead of `tailsMutex`. `python3 tools/check_conventions.py --base origin/main` passes. ### 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.24, Windows 11; lavapipe (llvmpipe, Mesa) on Ubuntu 24.04 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
milescoviello commented 2026-11-09 11:54:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-09T11:54:11Z.

Review: APPROVED


Linux check, to go with your Windows runs. On this host the new test also catches the bug on main.

Setup: Ubuntu 26.04.1, RTX 4070 Ti, NVIDIA 615.71.09, GCC 15.2, Release. ctest -R '^agc_driver_frame_replay', 3 runs per build:

build result
merge base a55d18f626 33 of 33 pass, 3 of 3 runs
merge base + only this PR's tests/FrameReplay.cpp and its cmake (70f3d12ded) agc_driver_frame_replay_rewind_reuse fails in 2 of 3 runs with FAILED: the replay did not run the REWIND tail as captured; the other 33 pass
this head 9bd5d256f5 34 of 34 pass, 3 of 3 runs

With VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (1.4.341, VK_LOADER_DEBUG=layer), ANYPS5_REQUIRE_VULKAN=1 and ANYPS5_NO_SHADER_CACHE=1, ctest -V, re-run on the current head 939088a0d4 against its merge base 4b71464421:

  • the base passes 33 of 33 and the head 34 of 34;
  • 0 VUID- lines on either side;
  • 42 of 42 pass on the base and 43 of 43 on the head; the ^agc_driver_frame_replay set has grown with main since the table above. The loader inserted the layer 34 and 36 times, once per Vulkan device created, so the layer had work.

The failure on base is intermittent (1 of 3 runs passed), which fits a race between the queue's tail copy and the next delta.

From reading:

  • Every accepted tail is tracked. Both tail paths, the pipelined SubmitEnd path (PacketExecution.cpp:620-628) and the non-pipelined end of the same function (:650-652), go through executeRewindTail. So every tail added at Submit is removed when its copy runs.
  • Cross-queue chains don't stall the replay. Suppose a tail's queue waits on a label another queue writes only after a title-satisfied wait. awaitsTitle is a global quiescence check (no running workers, no order holders, no pending completions, no recorder write over the address). So the parked chain reads as waiting on the title, and RewindTailsUnread returns false.
  • A driver failure ends the drain instead of hanging it. The drain() try/catch plus AgcDriverCheckFailure_nid_postfix in its loop surface the failure through failure.

One small cost note, not a blocker: noteTailRead takes tailsMutex on every Submit with a REWIND tail and every executeRewindTail, in title runs too, while only agc_frame_replay reads the list. If a title with many tails shows it, the list could be kept only while a replay is attached.

Edited 11-09: the validation numbers above come from a re-run with layer 1.4.341, where VK_LOADER_DEBUG=layer shows the loader inserting it in every test. The 1.4.363 layer my first run pointed at wasn't installed, and these test programs don't print the loader's "not found" error, so that run validated nothing (#17896).

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5825471876 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17642#pullrequestreview-5825471876). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-09T11:54:11Z. **Review:** APPROVED --- Linux check, to go with your Windows runs. On this host the new test also catches the bug on `main`. **Setup:** Ubuntu 26.04.1, RTX 4070 Ti, NVIDIA 615.71.09, GCC 15.2, Release. `ctest -R '^agc_driver_frame_replay'`, 3 runs per build: | build | result | |---|---| | merge base `a55d18f626` | 33 of 33 pass, 3 of 3 runs | | merge base + only this PR's `tests/FrameReplay.cpp` and its cmake (`70f3d12ded`) | `agc_driver_frame_replay_rewind_reuse` fails in 2 of 3 runs with `FAILED: the replay did not run the REWIND tail as captured`; the other 33 pass | | this head `9bd5d256f5` | 34 of 34 pass, 3 of 3 runs | With `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (1.4.341, `VK_LOADER_DEBUG=layer`), `ANYPS5_REQUIRE_VULKAN=1` and `ANYPS5_NO_SHADER_CACHE=1`, `ctest -V`, re-run on the current head `939088a0d4` against its merge base `4b71464421`: - the base passes 33 of 33 and the head 34 of 34; - 0 `VUID-` lines on either side; - 42 of 42 pass on the base and 43 of 43 on the head; the `^agc_driver_frame_replay` set has grown with `main` since the table above. The loader inserted the layer 34 and 36 times, once per Vulkan device created, so the layer had work. The failure on base is intermittent (1 of 3 runs passed), which fits a race between the queue's tail copy and the next delta. From reading: - **Every accepted tail is tracked.** Both tail paths, the pipelined `SubmitEnd` path (`PacketExecution.cpp:620-628`) and the non-pipelined end of the same function (`:650-652`), go through `executeRewindTail`. So every tail added at `Submit` is removed when its copy runs. - **Cross-queue chains don't stall the replay.** Suppose a tail's queue waits on a label another queue writes only after a title-satisfied wait. `awaitsTitle` is a global quiescence check (no running workers, no order holders, no pending completions, no recorder write over the address). So the parked chain reads as waiting on the title, and `RewindTailsUnread` returns false. - **A driver failure ends the drain instead of hanging it.** The `drain()` try/catch plus `AgcDriverCheckFailure_nid_postfix` in its loop surface the failure through `failure`. One small cost note, not a blocker: `noteTailRead` takes `tailsMutex` on every `Submit` with a REWIND tail and every `executeRewindTail`, in title runs too, while only `agc_frame_replay` reads the list. If a title with many tails shows it, the list could be kept only while a replay is attached. *Edited 11-09: the validation numbers above come from a re-run with layer 1.4.341, where `VK_LOADER_DEBUG=layer` shows the loader inserting it in every test. The 1.4.363 layer my first run pointed at wasn't installed, and these test programs don't print the loader's "not found" error, so that run validated nothing (#17896).* AI-assisted: yes (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!17642
No description provided.