fix(agc): read an awaited slot only while its wait is still published #22026
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22026"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 instillAwaitedandAwaitedWaits. 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 whileparkedOnTitlechecks 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_storethen 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(behindAgcDriverSettled) works in two steps. It first loads the slot's address and checksawaitsTitle, and only then callsstillAwaitedto compare the label against the wait. When the worker left a wait,noteWaitBlocked(queue, 0, false)clearedslot.shapefirst andslot.addresssecond, andstillAwaiteddid not check the address again. If the worker left the wait between those two steps, the reader:1 & RewindValidis 0, so it judged the wait still blocked.Settled()was then true while the replay was not at the captured park.restoreAtCapturedParkgave 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.
park_restores1 and areplay_sof 0.008 to 0.011 s, against 0.002 s when passing.rewind_storewith the message above on unrelated branches since 2026-11-15.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):readAwaitedreads 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.awaitedUnchangedchecks the generation and the address again.noteRewindWaitFields) before its address. The shape, reference and mask stores are release stores.stillAwaited,AwaitedWaitsandRewindTailsUnreadall go through the snapshot. A stale snapshot means not awaited, skipped, or unread, matching what each did before when the address re-check failed.parkedOnTitlechecks 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,slotsHeldStillrequires that no worker runs (runningWorkersis 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 raisedrunningWorkersor, 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, thenawaitsTitle, thenstillAwaited). With this PR,stillAwaiteditself is safe against the unblock. A function that checks several queues should keep eachblockedOnresult and end withslotsHeldStill, asparkedOnTitledoes. Otherwise it has the cross-queue window described in point 5.Other overlap.
noteWaitBlockedandstillAwaitedasmain, so it does not fix this race. Its newawaitedDwordstracking 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 throughnoteWaitFields,noteWaitBlockedandstillAwaited, so each interleaving is deterministic:stillAwaitedmust be false.RewindValidis clear, not awaited once it is set.AgcDriverAwaitedWaitsreports the blocked wait and drops it after the unblock.runningWorkers. The kept snapshot must not hold still.Mutants, each of which fails the test:
main'sstillAwaitedand unblock (shape cleared, no re-check): the first two cases fail.slotsHeldStillwithout therunningWorkerscheck: the leaving case fails.slotsHeldStillwithout the slot re-check: the left-and-blocked-again case fails.Other results:
main802a0aeb2dwith both commits:agc_driver_awaited_slot_state, theagc_driver_frame_replay*set andagc_driverpass, 49 of 49.agc_driver_frame_replay_rewind_storepasses 60 of 60 repeated runs.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 noVUID-lines.python3 tools/check_conventions.py --base origin/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
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/maina72186d3cf+ #22056 + #22074 (both needed for the macOS build); PR head5f37eef271merged 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.VUID-errors.Insert instance layer "VK_LAYER_KHRONOS_validation"; none logswas 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.agc_driver_f64_*tests skip (the M4 has noshaderFloat64);agc_driver_recorderfails witha kept vertex window was not mirrored(#20831); the recorder tests each log oneVUID-VkDeviceCreateInfo-sType-uniquefor a duplicatedVkPhysicalDevicePortabilitySubsetFeaturesKHRin the test's own device (#21965).agc_driver_awaited_slot_state: passes 3/3 (CPU-only, endsPASS worker reblocked during the check). I did not run it without the fix.AI-assisted: yes
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 withrepo_read.py; I did not run tests or commands. The description reports Linux and Windows CI results, which I did not independently verify.Results
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, andLifecycle.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.
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
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.