fix(agc): store idle write-backs and publishes on the CPU at completion, keeping blocks the guest wrote in flight #9521

Merged
Raghav314 merged 7 commits from upstream-pr/9521 into main 2026-10-27 01:58:04 +00:00
Raghav314 commented 2026-10-22 00:09:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Raghav314 on 2026-10-22T00:09:24Z.


What

When every queue idles, the driver stores all pending storage image results back to guest memory. For a host import that store was a GPU copy that landed after the batch ran, so stores the guest made while the batch was in flight were overwritten by the older results.

PPSA16064 (Minecraft Dungeons II) reuses a freed 3D storage image's memory for a CPU-filled volume during loading, and crashes on the stale texels.

  • Idle write-backs now take the CPU path at completion.
  • Idle shadow publishes copy into a host buffer and are stored per 64 KiB unit at completion, unless the guest wrote that unit since the publish began collecting writes.

Tested

Linux (CachyOS), x86-64, GCC, 7800X3D / RTX 4090, NVIDIA 615.78.08, on main a6dcea927.

  • New test agc_storage_idle_writeback_race: refills a dead storage volume's memory on the CPU while its results are pending, idles every queue, and checks that the refill survives in watched memory and in GPU-mapped direct memory, both for a plain idle write-back and after a shadow publish. Before the fix, the direct-memory cases lose the refill. A third case writes a selected unit and collects it after the publish has chosen its units but before it records the copy, and checks that the write survives completion; with the write-tracker baseline taken where it was before, it loses all 256 bytes.
  • The whole ctest (1821) with these two commits and one more since split out: all pass except agc_driver_sample_order, agc_driver_sample_order_grid_reference and agc_driver_sample_order_recipe_depth_locations, which fail the same way without this change. The storage, write-back and recorder tests (37) on this branch alone: all pass. Conventions pass.

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. Conflicts with #6682 (UnitShadow.cpp) and #9508 (CMakeLists.txt, tests/Recorder.cpp); whichever merges second gets rebased
  • Unblocks: PPSA16064 #8868
  • GPUs tested: NVIDIA GeForce RTX 4090, 615.78.08, Linux 7.2.10 (CachyOS)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4960056149 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9521). Originally posted by [Raghav314](https://github.com/Raghav314) on 2026-10-22T00:09:24Z. --- ### What When every queue idles, the driver stores all pending storage image results back to guest memory. For a host import that store was a GPU copy that landed after the batch ran, so stores the guest made while the batch was in flight were overwritten by the older results. PPSA16064 (Minecraft Dungeons II) reuses a freed 3D storage image's memory for a CPU-filled volume during loading, and crashes on the stale texels. - Idle write-backs now take the CPU path at completion. - Idle shadow publishes copy into a host buffer and are stored per 64 KiB unit at completion, unless the guest wrote that unit since the publish began collecting writes. ### Tested Linux (CachyOS), x86-64, GCC, 7800X3D / RTX 4090, NVIDIA 615.78.08, on `main` a6dcea927. - New test `agc_storage_idle_writeback_race`: refills a dead storage volume's memory on the CPU while its results are pending, idles every queue, and checks that the refill survives in watched memory and in GPU-mapped direct memory, both for a plain idle write-back and after a shadow publish. Before the fix, the direct-memory cases lose the refill. A third case writes a selected unit and collects it after the publish has chosen its units but before it records the copy, and checks that the write survives completion; with the write-tracker baseline taken where it was before, it loses all 256 bytes. - The whole ctest (1821) with these two commits and one more since split out: all pass except `agc_driver_sample_order`, `agc_driver_sample_order_grid_reference` and `agc_driver_sample_order_recipe_depth_locations`, which fail the same way without this change. The storage, write-back and recorder tests (37) on this branch alone: all pass. Conventions pass. ### 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. Conflicts with #6682 (`UnitShadow.cpp`) and #9508 (`CMakeLists.txt`, `tests/Recorder.cpp`); whichever merges second gets rebased - [x] Unblocks: PPSA16064 #8868 - [x] GPUs tested: NVIDIA GeForce RTX 4090, 615.78.08, Linux 7.2.10 (CachyOS) - [x] AI-assisted: yes
MarcZX10 commented 2026-10-22 00:13:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T00:13:10Z.

Review: CHANGES_REQUESTED


I found a race in the new CPU-shadow-publish completion guard (UnitShadow.cpp:261, 268). publishUnits has already collected writes, selected the live slab ranges, and built copies before recordCopiesOnCpu samples checked. If the guest writes one of those blocks after that selection but before checked, a concurrent write-watch collection can stamp the write at or below the sampled generation; the completion callback then treats the range as unchanged and stores the older slab bytes over the guest write. This matches GuestMemory::TrackerGeneration’s documented concurrent-walk behavior: a walk still running when the generation is read may absorb later stores with a stamp not newer than that value. The added regression writes only after FlushAllPending returns, which is after checked is captured, so it does not exercise this interval. Please take the comparison baseline before copy selection/collection and pass it through to completion (or otherwise make selection and store detection race-safe), and add coverage that forces a guest write in this interval. The reported tests do not prove this boundary safe.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5607760087 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9521#pullrequestreview-5607760087). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T00:13:10Z. **Review:** CHANGES_REQUESTED --- I found a race in the new CPU-shadow-publish completion guard (`UnitShadow.cpp:261, 268`). `publishUnits` has already collected writes, selected the live slab ranges, and built `copies` before `recordCopiesOnCpu` samples `checked`. If the guest writes one of those blocks after that selection but before `checked`, a concurrent write-watch collection can stamp the write at or below the sampled generation; the completion callback then treats the range as unchanged and stores the older slab bytes over the guest write. This matches `GuestMemory::TrackerGeneration`’s documented concurrent-walk behavior: a walk still running when the generation is read may absorb later stores with a stamp not newer than that value. The added regression writes only after `FlushAllPending` returns, which is after `checked` is captured, so it does not exercise this interval. Please take the comparison baseline before copy selection/collection and pass it through to completion (or otherwise make selection and store detection race-safe), and add coverage that forces a guest write in this interval. The reported tests do not prove this boundary safe.
MarcZX10 commented 2026-10-22 00:13:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T00:13:26Z.


The reported regression is a crash in Minecraft Dungeons II (PPSA16064), and its Game bring-up discussion is #8868. Please update the checklist from Unblocks: none to Unblocks: PPSA16064 #8868, as required for a title-specific crash fix.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6329463009 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9521#issuecomment-6329463009). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T00:13:26Z. --- The reported regression is a crash in Minecraft Dungeons II (PPSA16064), and its Game bring-up discussion is #8868. Please update the checklist from `Unblocks: none` to `Unblocks: PPSA16064 #8868`, as required for a title-specific crash fix.
MarcZX10 commented 2026-10-22 00:19:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T00:19:08Z.


A live overlap scan for this head also reports #6682 and #9508. I manually compared the current heads with git merge-tree: #6682 conflicts in UnitShadow.cpp, and #9508 conflicts in CMakeLists.txt and tests/Recorder.cpp. These are real patch conflicts around this PR’s shadow-publish path and recorder-test registration. The checklist says Depends on: none; please coordinate merge order with those authors and update the dependency line before merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6329543609 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9521#issuecomment-6329543609). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T00:19:08Z. --- A live overlap scan for this head also reports #6682 and #9508. I manually compared the current heads with `git merge-tree`: #6682 conflicts in `UnitShadow.cpp`, and #9508 conflicts in `CMakeLists.txt` and `tests/Recorder.cpp`. These are real patch conflicts around this PR’s shadow-publish path and recorder-test registration. The checklist says `Depends on: none`; please coordinate merge order with those authors and update the dependency line before merge.
Raghav314 commented 2026-10-22 00:39:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Raghav314 on 2026-10-22T00:39:42Z.


@MarcZX10 thanks, the race was real.

  • publishUnits now takes the tracker generation before it collects writes and selects units, and passes it to the completion. A store that lands while the copy is being built is now newer than the baseline, so completion keeps it instead of overwriting it.
  • The test has a new case for this: a hook runs after unit selection and before the copy is recorded, writes into a selected unit and collects it. The case checks the bytes survive completion. With the baseline where it was before, it loses all 256 bytes.
  • Checklist: Unblocks: PPSA16064 #8868.
  • On the overlap with #6682 and #9508: whichever of them merges second, I'll rebase this one (or #9508) on top; I've noted that in the checklist.

LLM assistance used.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6329819084 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9521#issuecomment-6329819084). Originally posted by [Raghav314](https://github.com/Raghav314) on 2026-10-22T00:39:42Z. --- @MarcZX10 thanks, the race was real. - `publishUnits` now takes the tracker generation before it collects writes and selects units, and passes it to the completion. A store that lands while the copy is being built is now newer than the baseline, so completion keeps it instead of overwriting it. - The test has a new case for this: a hook runs after unit selection and before the copy is recorded, writes into a selected unit and collects it. The case checks the bytes survive completion. With the baseline where it was before, it loses all 256 bytes. - Checklist: `Unblocks: PPSA16064 #8868`. - On the overlap with #6682 and #9508: whichever of them merges second, I'll rebase this one (or #9508) on top; I've noted that in the checklist. LLM assistance used.
MarcZX10 commented 2026-10-22 02:03:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T02:03:34Z.


Thanks for adding the generation-baseline regression and correcting the title discussion entry. The current branch note says “whichever merges second gets rebased,” but the live conflicts mean there are two different orders: #6682 edits the same UnitShadow.cpp completion path, and #9508 conflicts in the test registration (CMakeLists.txt, tests/Recorder.cpp). Please state the intended dependency/order explicitly (or confirm both merge orders are safe and who rebases each branch) and list required predecessors on Depends on before merge. I have not run the tests locally; the hosted conventions/eligibility/report checks pass and Linux/Windows/relinker jobs are skipped.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6330906305 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9521#issuecomment-6330906305). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T02:03:34Z. --- Thanks for adding the generation-baseline regression and correcting the title discussion entry. The current branch note says “whichever merges second gets rebased,” but the live conflicts mean there are two different orders: #6682 edits the same `UnitShadow.cpp` completion path, and #9508 conflicts in the test registration (`CMakeLists.txt`, `tests/Recorder.cpp`). Please state the intended dependency/order explicitly (or confirm both merge orders are safe and who rebases each branch) and list required predecessors on `Depends on` before merge. I have not run the tests locally; the hosted conventions/eligibility/report checks pass and Linux/Windows/relinker jobs are skipped.
MarcZX10 commented 2026-10-22 11:42:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:42:11Z.

Review: CHANGES_REQUESTED


The latest head 3e7c7bd03aef appears to fix the tracker race I reported: publishUnits snapshots the generation before the uncached write collection and unit selection, passes that baseline to completion, and the new hook writes/collects a selected unit after selection but before copy recording. The negative-control result in the PR history is consistent with that test. Current diff-check and conventions pass, and the individual branch merge with current main is clean.

Merge readiness still has three blockers:

  1. This branch is 2 ahead / 119 behind current main (merge base a6dcea927). Refresh it and run the required same-machine/same-GPU AGC comparison on current main and this head with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, using ctest -V. The report must confirm the layer was found, compare new VUID- messages, and name GPU vendor/model/driver/OS. Existing suite results and listed GPU are useful but do not include this required comparison.
  2. The current body says “whichever merges second gets rebased,” but the contribution rule requires an agreed order and Depends on when one must land first. The live pairwise conflicts remain: #6682 conflicts in UnitShadow.cpp; #9508 conflicts in CMakeLists.txt and tests/Recorder.cpp. Please coordinate #6682’s order and update the dependency/order note so the affected branch preserves both changes. (The #9508 branch is yours, so please also record its concrete order.)
  3. The newly generated overlap report lists #9674, #9675 and #9676 on CMakeLists.txt/Texture.cpp; inspect those current open diffs and update the overlap coordination record if any of those edits need ordering.

I did not run local GPU tests/builds in this pass.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5613587393 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9521#pullrequestreview-5613587393). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T11:42:11Z. **Review:** CHANGES_REQUESTED --- The latest head `3e7c7bd03aef` appears to fix the tracker race I reported: `publishUnits` snapshots the generation before the uncached write collection and unit selection, passes that baseline to completion, and the new hook writes/collects a selected unit after selection but before copy recording. The negative-control result in the PR history is consistent with that test. Current diff-check and conventions pass, and the individual branch merge with current `main` is clean. Merge readiness still has three blockers: 1. This branch is 2 ahead / 119 behind current `main` (merge base `a6dcea927`). Refresh it and run the required same-machine/same-GPU AGC comparison on current `main` and this head with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, using `ctest -V`. The report must confirm the layer was found, compare new `VUID-` messages, and name GPU vendor/model/driver/OS. Existing suite results and listed GPU are useful but do not include this required comparison. 2. The current body says “whichever merges second gets rebased,” but the contribution rule requires an agreed order and `Depends on` when one must land first. The live pairwise conflicts remain: #6682 conflicts in `UnitShadow.cpp`; #9508 conflicts in `CMakeLists.txt` and `tests/Recorder.cpp`. Please coordinate #6682’s order and update the dependency/order note so the affected branch preserves both changes. (The #9508 branch is yours, so please also record its concrete order.) 3. The newly generated overlap report lists #9674, #9675 and #9676 on `CMakeLists.txt`/`Texture.cpp`; inspect those current open diffs and update the overlap coordination record if any of those edits need ordering. I did not run local GPU tests/builds in this pass.
MarcZX10 commented 2026-10-22 11:42:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:42:24Z.


Correction/addendum to my review: I completed the pairwise merge checks with #9674, #9675 and #9676. All three merge cleanly with this head, so their shared CMakeLists.txt/Texture.cpp paths do not currently establish an ordering dependency. The listed overlap should still be acknowledged in the next overlap report, but I found no added blocker from those three PRs.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6339882156 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9521#issuecomment-6339882156). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T11:42:24Z. --- Correction/addendum to my review: I completed the pairwise merge checks with #9674, #9675 and #9676. All three merge cleanly with this head, so their shared `CMakeLists.txt`/`Texture.cpp` paths do not currently establish an ordering dependency. The listed overlap should still be acknowledged in the next overlap report, but I found no added blocker from those three PRs.
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!9521
No description provided.