fix(agc): read an awaited slot only while its wait is still published #22026

Merged
chitters2004 merged 3 commits from upstream-pr/22026 into main 2026-11-26 04:20:31 +00:00
chitters2004 commented 2026-11-17 02:06:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by chitters2004 on 2026-11-17T02:06:54Z.


What

thuanquang found and diagnosed this race on #12568 (comment, 2026-11-12) and proposed a five-line fix on thuanquang:contact/awaited-slot-order (f015412ae4): clear the address before the shape, and re-read the address in stillAwaited and AwaitedWaits. This PR fixes the same race, plus two cases that fix leaves open, which they pointed out or which follow from it: a reblock at the same address, and a queue that leaves its wait while parkedOnTitle checks another queue. Thanks to thuanquang for the diagnosis.

A frame replay could give up waiting for the captured REWIND park and skip the delta it must re-apply there. agc_driver_frame_replay_rewind_store then fails with "the replay applied the CPU store before the copy that preceded it".

The race. Each queue publishes the wait it is blocked on in an awaited slot: an address, plus the wait's shape, reference and mask. parkedOnTitle (behind AgcDriverSettled) works in two steps. It first loads the slot's address and checks awaitsTitle, and only then calls stillAwaited to compare the label against the wait. When the worker left a wait, noteWaitBlocked(queue, 0, false) cleared slot.shape first and slot.address second, and stillAwaited did not check the address again. If the worker left the wait between those two steps, the reader:

  1. held the gate's address;
  2. read shape 0, which means a REWIND wait;
  3. tested the now-satisfied gate (value 1) as a REWIND control word. 1 & RewindValid is 0, so it judged the wait still blocked.

Settled() was then true while the replay was not at the captured park. restoreAtCapturedPark gave up, and the second re-restore at the REWIND park never ran.

The REWIND wait also published no fields of its own. It relied on the previous unblock to leave shape 0.

What it caused.

  • Every failing replay shows park_restores 1 and a replay_s of 0.008 to 0.011 s, against 0.002 s when passing.
  • Windows CI has failed rewind_store with the message above on unrelated branches since 2026-11-15.
  • Run in isolation in fork CI, it failed 1 time in 186 runs.

The driver has published the wait this way since frame capture was added. The replay's early exit on Settled(), added by our merged #12567 (6cee7614e5), made the race visible.

The change (Driver.hpp, Queues/Submission.cpp, Lifecycle.cpp):

  1. readAwaited reads a slot as one snapshot: a generation, then the address (which must match the one the caller holds), then shape, reference and mask, all with acquire loads. awaitedUnchanged checks the generation and the address again.
  2. Leaving a wait bumps the slot's generation with a release store instead of clearing the shape. A reader that started before the unblock, or before a later block at the same address, therefore sees the snapshot as stale.
  3. A REWIND wait publishes its own fields (noteRewindWaitFields) before its address. The shape, reference and mask stores are release stores.
  4. stillAwaited, AwaitedWaits and RewindTailsUnread all go through the snapshot. A stale snapshot means not awaited, skipped, or unread, matching what each did before when the address re-check failed.
  5. Several queues (second commit). parkedOnTitle checks the queues one after another, so a queue it already passed could leave its wait while it checked a later one and still count as parked. It now keeps each slot's snapshot (blockedOn). After the last queue, slotsHeldStill requires that no worker runs (runningWorkers is 0, read with seq_cst) and that every kept slot still holds the same generation and address. A worker that left a wait during the check has either raised runningWorkers or, if it has blocked again since, bumped its slot's generation.

Callers in other PRs. #20730 and #21225 add submissionPaced, which reads each queue's slot the same way (address, then awaitsTitle, then stillAwaited). With this PR, stillAwaited itself is safe against the unblock. A function that checks several queues should keep each blockedOn result and end with slotsHeldStill, as parkedOnTitle does. Otherwise it has the cross-queue window described in point 5.

Other overlap.

  • #15136 (draft) has the same noteWaitBlocked and stillAwaited as main, so it does not fix this race. Its new awaitedDwords tracking is separate, and only text conflicts are expected.

Tested

Local (Ubuntu 24.04, GCC 13, lavapipe on Mesa llvmpipe, LLVM 20.1.2) and fork CI.

New test agc_driver_awaited_slot_state (tests/execution/AwaitedSlotState.cpp). It drives a queue's awaited slot directly through noteWaitFields, noteWaitBlocked and stillAwaited, so each interleaving is deterministic:

  • A satisfied gate held across the unblock: a reader holds the gate's address, the worker unblocks, and the reader's stillAwaited must be false.
  • A reblock at the same address: fields read before an unblock and a new block at the same address with a different reference must read as stale.
  • A REWIND wait after a memory wait on the same slot: awaited while RewindValid is clear, not awaited once it is set.
  • AgcDriverAwaitedWaits reports the blocked wait and drops it after the unblock.
  • A worker leaving its wait during the check: with the capture hold set, a thread starts the unblock and stops inside it after raising runningWorkers. The kept snapshot must not hold still.
  • A worker that left and blocked again on the same wait: the kept snapshot must not hold still.

Mutants, each of which fails the test:

  • main's stillAwaited and unblock (shape cleared, no re-check): the first two cases fail.
  • No generation bump: the reblock case fails.
  • No shape store for the REWIND wait: the REWIND case fails.
  • slotsHeldStill without the runningWorkers check: the leaving case fails.
  • slotsHeldStill without the slot re-check: the left-and-blocked-again case fails.

Other results:

  • On main 802a0aeb2d with both commits: agc_driver_awaited_slot_state, the agc_driver_frame_replay* set and agc_driver pass, 49 of 49.
  • agc_driver_frame_replay_rewind_store passes 60 of 60 repeated runs.
  • Fork CI run 43996995079 on this head: the Linux lavapipe gate passes 49 of 49 (base 48 of 48); Windows passes 47 of 49 with the same two failures as the base (agc_driver, agc_driver_frame_replay_cold_pipelines), so neither is caused by this change; the Khronos validation layer 1.4.363 loaded and there are no VUID- lines.

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 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: none
  • Unblocks: none
  • GPUs tested: lavapipe (llvmpipe, Mesa, LLVM 20.1.2) on Ubuntu 24.04; fork CI Windows llvmpipe
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5349061011 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22026). Originally posted by [chitters2004](https://github.com/chitters2004) on 2026-11-17T02:06:54Z. --- ### What thuanquang found and diagnosed this race on #12568 ([comment](https://github.com/boykopovar/AnyPS5/pull/12568#issuecomment-6824106637), 2026-11-12) and proposed a five-line fix on `thuanquang:contact/awaited-slot-order` (`f015412ae4`): clear the address before the shape, and re-read the address in `stillAwaited` and `AwaitedWaits`. This PR fixes the same race, plus two cases that fix leaves open, which they pointed out or which follow from it: a reblock at the same address, and a queue that leaves its wait while `parkedOnTitle` checks another queue. Thanks to thuanquang for the diagnosis. A frame replay could give up waiting for the captured REWIND park and skip the delta it must re-apply there. `agc_driver_frame_replay_rewind_store` then fails with "the replay applied the CPU store before the copy that preceded it". **The race.** Each queue publishes the wait it is blocked on in an awaited slot: an address, plus the wait's shape, reference and mask. `parkedOnTitle` (behind `AgcDriverSettled`) works in two steps. It first loads the slot's address and checks `awaitsTitle`, and only then calls `stillAwaited` to compare the label against the wait. When the worker left a wait, `noteWaitBlocked(queue, 0, false)` cleared `slot.shape` first and `slot.address` second, and `stillAwaited` did not check the address again. If the worker left the wait between those two steps, the reader: 1. held the gate's address; 2. read shape 0, which means a REWIND wait; 3. tested the now-satisfied gate (value 1) as a REWIND control word. `1 & RewindValid` is 0, so it judged the wait still blocked. `Settled()` was then true while the replay was not at the captured park. `restoreAtCapturedPark` gave up, and the second re-restore at the REWIND park never ran. The REWIND wait also published no fields of its own. It relied on the previous unblock to leave shape 0. **What it caused.** - Every failing replay shows `park_restores` 1 and a `replay_s` of 0.008 to 0.011 s, against 0.002 s when passing. - Windows CI has failed `rewind_store` with the message above on unrelated branches since 2026-11-15. - Run in isolation in fork CI, it failed 1 time in 186 runs. The driver has published the wait this way since frame capture was added. The replay's early exit on `Settled()`, added by our merged #12567 (6cee7614e5), made the race visible. **The change** (`Driver.hpp`, `Queues/Submission.cpp`, `Lifecycle.cpp`): 1. `readAwaited` reads a slot as one snapshot: a generation, then the address (which must match the one the caller holds), then shape, reference and mask, all with acquire loads. `awaitedUnchanged` checks the generation and the address again. 2. Leaving a wait bumps the slot's generation with a release store instead of clearing the shape. A reader that started before the unblock, or before a later block at the same address, therefore sees the snapshot as stale. 3. A REWIND wait publishes its own fields (`noteRewindWaitFields`) before its address. The shape, reference and mask stores are release stores. 4. `stillAwaited`, `AwaitedWaits` and `RewindTailsUnread` all go through the snapshot. A stale snapshot means not awaited, skipped, or unread, matching what each did before when the address re-check failed. 5. **Several queues** (second commit). `parkedOnTitle` checks the queues one after another, so a queue it already passed could leave its wait while it checked a later one and still count as parked. It now keeps each slot's snapshot (`blockedOn`). After the last queue, `slotsHeldStill` requires that no worker runs (`runningWorkers` is 0, read with seq_cst) and that every kept slot still holds the same generation and address. A worker that left a wait during the check has either raised `runningWorkers` or, if it has blocked again since, bumped its slot's generation. **Callers in other PRs.** #20730 and #21225 add `submissionPaced`, which reads each queue's slot the same way (`address`, then `awaitsTitle`, then `stillAwaited`). With this PR, `stillAwaited` itself is safe against the unblock. A function that checks several queues should keep each `blockedOn` result and end with `slotsHeldStill`, as `parkedOnTitle` does. Otherwise it has the cross-queue window described in point 5. **Other overlap.** - #15136 (draft) has the same `noteWaitBlocked` and `stillAwaited` as `main`, so it does not fix this race. Its new `awaitedDwords` tracking is separate, and only text conflicts are expected. ### Tested Local (Ubuntu 24.04, GCC 13, lavapipe on Mesa llvmpipe, LLVM 20.1.2) and fork CI. New test `agc_driver_awaited_slot_state` (`tests/execution/AwaitedSlotState.cpp`). It drives a queue's awaited slot directly through `noteWaitFields`, `noteWaitBlocked` and `stillAwaited`, so each interleaving is deterministic: - **A satisfied gate held across the unblock:** a reader holds the gate's address, the worker unblocks, and the reader's `stillAwaited` must be false. - **A reblock at the same address:** fields read before an unblock and a new block at the same address with a different reference must read as stale. - **A REWIND wait after a memory wait on the same slot:** awaited while `RewindValid` is clear, not awaited once it is set. - **`AgcDriverAwaitedWaits`** reports the blocked wait and drops it after the unblock. - **A worker leaving its wait during the check:** with the capture hold set, a thread starts the unblock and stops inside it after raising `runningWorkers`. The kept snapshot must not hold still. - **A worker that left and blocked again on the same wait:** the kept snapshot must not hold still. Mutants, each of which fails the test: - `main`'s `stillAwaited` and unblock (shape cleared, no re-check): the first two cases fail. - No generation bump: the reblock case fails. - No shape store for the REWIND wait: the REWIND case fails. - `slotsHeldStill` without the `runningWorkers` check: the leaving case fails. - `slotsHeldStill` without the slot re-check: the left-and-blocked-again case fails. Other results: - On `main` 802a0aeb2d with both commits: `agc_driver_awaited_slot_state`, the `agc_driver_frame_replay*` set and `agc_driver` pass, 49 of 49. - `agc_driver_frame_replay_rewind_store` passes 60 of 60 repeated runs. - [Fork CI run 43996995079](https://github.com/chitters2004/AnyPS5/actions/runs/43996995079) on this head: the Linux lavapipe gate passes 49 of 49 (base 48 of 48); Windows passes 47 of 49 with the same two failures as the base (`agc_driver`, `agc_driver_frame_replay_cold_pipelines`), so neither is caused by this change; the Khronos validation layer 1.4.363 loaded and there are no `VUID-` lines. `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) and the licence notice of code adapted from upstream (see AGENTS.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: lavapipe (llvmpipe, Mesa, LLVM 20.1.2) on Ubuntu 24.04; fork CI Windows llvmpipe - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
kurexw commented 2026-11-17 07:44:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kurexw on 2026-11-17T07:44:47Z.

Review: COMMENTED


macOS GPU test (Apple M4, MoltenVK 1.4.2, validation layer 1.4.363.1, x86-64 build under Rosetta)

Base: upstream/main a72186d3cf + #22056 + #22074 (both needed for the macOS build); PR head 5f37eef271 merged on top. Built clean (only the known libcurl link failure, #17425).
Env per AGENTS.md: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer, ctest -R 'recorder|constant_phi|masked_select|sample_bit_descriptor|pixel_ancillary|scalar_buffer|shader_mode_registers|agc_driver_f64|command_batch|awaited_slot|agc_driver_idle|detiler' -V, 3 runs each on base and PR.

  • Every test has the same status on base and PR in all 3 runs; no new VUID- errors.
  • Every test that creates a Vulkan instance logs Insert instance layer "VK_LAYER_KHRONOS_validation"; none logs was not found but was requested. The tests without layer lines (agc_constant_phi, agc_masked_select, agc_command_batch_*, pixel_ancillary, ...) never touch the loader.
  • Same on base and PR: 11 agc_driver_f64_* tests skip (the M4 has no shaderFloat64); agc_driver_recorder fails with a kept vertex window was not mirrored (#20831); the recorder tests each log one VUID-VkDeviceCreateInfo-sType-unique for a duplicated VkPhysicalDevicePortabilitySubsetFeaturesKHR in the test's own device (#21965).
  • New agc_driver_awaited_slot_state: passes 3/3 (CPU-only, ends PASS worker reblocked during the check). I did not run it without the fix.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5934698877 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22026#pullrequestreview-5934698877). Originally posted by [kurexw](https://github.com/kurexw) on 2026-11-17T07:44:47Z. **Review:** COMMENTED --- **macOS GPU test (Apple M4, MoltenVK 1.4.2, validation layer 1.4.363.1, x86-64 build under Rosetta)** Base: `upstream/main` a72186d3cf + #22056 + #22074 (both needed for the macOS build); PR head `5f37eef271` merged on top. Built clean (only the known libcurl link failure, #17425). Env per AGENTS.md: `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer`, `ctest -R 'recorder|constant_phi|masked_select|sample_bit_descriptor|pixel_ancillary|scalar_buffer|shader_mode_registers|agc_driver_f64|command_batch|awaited_slot|agc_driver_idle|detiler' -V`, 3 runs each on base and PR. - Every test has the same status on base and PR in all 3 runs; no new `VUID-` errors. - Every test that creates a Vulkan instance logs `Insert instance layer "VK_LAYER_KHRONOS_validation"`; none logs `was not found but was requested`. The tests without layer lines (`agc_constant_phi`, `agc_masked_select`, `agc_command_batch_*`, `pixel_ancillary`, ...) never touch the loader. - Same on base and PR: 11 `agc_driver_f64_*` tests skip (the M4 has no `shaderFloat64`); `agc_driver_recorder` fails with `a kept vertex window was not mirrored` (#20831); the recorder tests each log one `VUID-VkDeviceCreateInfo-sType-unique` for a duplicated `VkPhysicalDevicePortabilitySubsetFeaturesKHR` in the test's own device (#21965). - New `agc_driver_awaited_slot_state`: passes 3/3 (CPU-only, ends `PASS worker reblocked during the check`). I did not run it without the fix. AI-assisted: yes
MarcZX10 commented 2026-11-22 06:55:50 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the reviewed code and author-reported CI results support the fix.

What I ran

Reviewed head 5f37eef2714bd11006bd905988882976628658ec. I inspected the prepared diff and targeted repository source with repo_read.py; I did not run tests or commands. The description reports Linux and Windows CI results, which I did not independently verify.

Results

Check Result Note
Source review passed Four changed files inspected
Tests and CI not run Results supplied by author only

Findings

Blocking

None.

Non-blocking

None.

Not verified

I read the diff for all four changed files and targeted ranges in Driver.hpp, Submission.cpp, and Lifecycle.cpp. I did not read the complete changed files, run the test suite, or independently verify the reported CI runs, convention check, or hardware behavior.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6010896083 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22026#pullrequestreview-6010896083). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T06:55:50Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the reviewed code and author-reported CI results support the fix. ## What I ran Reviewed head `5f37eef2714bd11006bd905988882976628658ec`. I inspected the prepared diff and targeted repository source with `repo_read.py`; I did not run tests or commands. The description reports Linux and Windows CI results, which I did not independently verify. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Four changed files inspected | | Tests and CI | not run | Results supplied by author only | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I read the diff for all four changed files and targeted ranges in `Driver.hpp`, `Submission.cpp`, and `Lifecycle.cpp`. I did not read the complete changed files, run the test suite, or independently verify the reported CI runs, convention check, or hardware behavior. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-22 17:58:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T17:58:56Z.

Review: COMMENTED


Reviewed — no blocking findings

Nothing blocks this change; the points below are optional.

What I ran

Reviewed head cf5c0c8b6c93ef84aa74701dc3550242d4cd2ed8. I read the prepared description, commits, diff, manifest, and every changed file listed in the manifest. I did not run commands or tests; test results in the description are author-reported.

Results

Check Result Note
Source review passed All four changed files read
Tests not run Reported results were not run by me
Precheck not run Reported results were not run by me

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not independently verify the reported local or CI runs. No excluded changed files were listed in the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6013628611 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22026#pullrequestreview-6013628611). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T17:58:56Z. **Review:** COMMENTED --- # Reviewed — no blocking findings Nothing blocks this change; the points below are optional. ## What I ran Reviewed head `cf5c0c8b6c93ef84aa74701dc3550242d4cd2ed8`. I read the prepared description, commits, diff, manifest, and every changed file listed in the manifest. I did not run commands or tests; test results in the description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | All four changed files read | | Tests | not run | Reported results were not run by me | | Precheck | not run | Reported results were not run by me | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not independently verify the reported local or CI runs. No excluded changed files were listed in the manifest. ## 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!22026
No description provided.