fix(agc): drop pending storage-image results when the guest unmaps their memory #8932

Closed
BunnyFl wants to merge 1 commit from upstream-pr/8932 into main
BunnyFl commented 2026-10-21 06:39:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-10-21T06:39:44Z.


What

Pending storage-image results are stored when something reads their memory, or by the queue 0 worker once every queue has been idle for 50 ms. An unmap left them pending: since e3ce9bac0 the idle store waits for the unmap's mutation and drops the results only when it then finds the range unregistered. A title that maps new memory over the same addresses before the idle store runs gets the stale results written into its new data.

Heavenly Bodies (PPSA04246, Unity) does this at its first mission load. With APS5_TRACE_MEMORY=1 APS5_TRACE_FLUSH=1, run trace-7 on main:

1423: [memory] reserve 0000000272180000+0x100000 flags=0x0 align=0x40000
1425: [memory] remap fixed 0000000272180000+0xd4000 prot=0xf2 phys=0x6f480000
1799: [memory] remap fixed 0000000272180000+0xd4000 prot=0x0 phys=0xffffffffffffffff
1801: [memory] unmap 0000000272180000+0x100000
10201: [memory] reserve 0000000271a40000+0x1000000 flags=0x0 align=0x40000
10203: [memory] remap fixed 0000000271a40000+0x1000000 prot=0xf2 phys=0x6ee10000
10297: [flush] image 0x272190000+0xc0000 for idle 0x0+0xffffffffffffffff
FATAL: unhandled exception 0xc0000005 on thread 28132 'Loading.PreloadManager'
  rax 000000027219a9e0 ... rdi 000000027219a970 ... r13 000000027219a960

The 16 MiB block mapped at line 10203 is Unity's heap; the idle store at line 10297 writes the image of the unmapped range over it, and the allocator reads the overwritten words (0x27219a960 lies in that image). The other crashes are access violations on other threads (AssetGarbageCollectorHelper, UnityGfxDeviceWorker, Gfx Task Executor, unnamed ones) after the same kind of idle store.

This PR:

  • The unmap observer (retireUnmapped, also called for allocation removals) drops the results pending in images lying wholly inside the removed range through StorageTexture::DiscardPendingInside, the path a fill over a whole image already takes. For block-unit images it drops the pending units inside the range.
  • The observer is installed once a storage image exists (StorageTexture::AnyCreated), next to the multisample and depth guards that already share it.
  • IdleStoreOverUnmap maps the surface again at the same address inside the unmap's mutation and checks that the idle store and FlushAllPending do not write the old results into it. Its previous check that the results are still pending while the mutation is held no longer applies, as they are dropped at the unmap; when the address cannot be mapped again it keeps the previous settle check.
  • The #1970 debt entry notes that an unmap drops only the results pending wholly inside its range.

Lock order: the observer runs under the registry mutation and takes Pending().mutex, which is a leaf (no path takes the registry lock while holding it; see blocksKept). A store that holds a lease finishes before RequireUnpinned lets the unmap proceed.

Still not covered (debt entry): an image only partly inside an unmapped range, and memory reused without an unmap.

Overlap (tools/pr_overlap.py): #7600 also extends retireUnmapped and the observer condition in MultisampleTarget.cpp for its multisampled clear records; whichever merges second keeps both calls and both conditions. #6926 and #8707 edit the same #1970 debt entry for other cases (host writes over pending results, DCC images with several mips); only the text needs merging.

Tested

Windows 11 Pro 10.0.26200, AMD Radeon RX 9070 XT (AMD proprietary driver 26.10.1, LLPC), WinLibs GCC 15.2.0 r7.

Heavenly Bodies (PPSA04246, 01.006.004), converted from the dump on main 53a0a27bb, with this PR rebased onto that commit for the second row. 45 s runs from start into the first mission (cross pressed every 2 s through APS5_PAD_INPUT_FILE), APS5_PROFILE_DRAW=1 APS5_LOG_TIMESTAMPS=1 APS5_TRACE_FLUSH=1 APS5_TRACE_MEMORY=1, after one warm-up run:

runs crashed idle stores into a range unmapped earlier in the run presents per 10 s in the mission
main 53a0a27bb 10 5 17-79 in eight runs, 0 in the two whose idle store ran once 321-394 (the five that survived)
this PR 10 0 0 360-365

Every crashed run had 61 or more such stores. The crashes are access violations on Loading.PreloadManager, Gfx Task Executor and unnamed threads, and once unsupported PM4 packet type: header 0x00000000 in an overwritten command buffer. With this PR the idle store still runs (37-42 stores per run instead of 88-145); only the stores into unmapped ranges are gone. Earlier, on main 7ab4b2e56 with the same settings, main crashed in 6 of 15 runs and this PR in 0 of 15; without APS5_TRACE_FLUSH/APS5_TRACE_MEMORY, main crashed in 2 of 15. One of three played runs on 7ab4b2e56 also crashed there (AssetGarbageCollectorHelper).

GPU tests on main 0aa55ef29 and on this PR rebased onto it, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R "idle_store|multisample_retirement|native_multisample_depth|cached_image_multisample|host_import|guest_memory_lifetime|flip_stores|pipelined_fill|pattern_buffer_fill|fill_cover|depth_storage_reuse|memory_value_fill|pending_exact|clamped_global_fill|constant_dword_fill" -V:

  • main with this PR's IdleStoreOverUnmap.cpp: 30 of 31 passed, agc_driver_idle_store_over_unmap failed with idle store over unmap: results of the unmapped surface were stored into the memory mapped over it.
  • This PR: 31 of 31 passed.
  • Both: agc_host_import_sparse skipped (udmabuf imports are Linux only); the validation layer loaded (no was not found but was requested by env var VK_INSTANCE_LAYERS) and neither log has a VUID- error.

Rebased onto main 3e91c8edf after #7600 merged. #7600 also extends retireUnmapped and the unmap observer condition in MultisampleTarget.cpp; the rebase keeps both calls (forgetSingleClears and DiscardPendingInside) and both conditions (anySingleClears() and StorageTexture::AnyCreated()), and changes nothing else. The GPU tests above were run again there, with agc_driver_graphics and agc_driver_msaa_dcc_clear_2/4/8 (where #7600's tests are) added: main with this PR's test 34 of 35 passed (agc_driver_idle_store_over_unmap failed with the same message), this PR 35 of 35, agc_host_import_sparse skipped, the validation layer loaded and no VUID- error. tools/check_conventions.py --base upstream/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: PPSA04246 #8931
  • GPUs tested: AMD Radeon RX 9070 XT, AMD proprietary driver 26.10.1 (LLPC), Windows 11 Pro 10.0.26200
  • AI-assisted: yes

The investigation, the change and this description were prepared with AI assistance (Claude Code); the in-game observations were made by me.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4947489752 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8932). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-10-21T06:39:44Z. --- ### What Pending storage-image results are stored when something reads their memory, or by the queue 0 worker once every queue has been idle for 50 ms. An unmap left them pending: since e3ce9bac0 the idle store waits for the unmap's mutation and drops the results only when it then finds the range unregistered. A title that maps new memory over the same addresses before the idle store runs gets the stale results written into its new data. Heavenly Bodies (PPSA04246, Unity) does this at its first mission load. With `APS5_TRACE_MEMORY=1 APS5_TRACE_FLUSH=1`, run trace-7 on `main`: ``` 1423: [memory] reserve 0000000272180000+0x100000 flags=0x0 align=0x40000 1425: [memory] remap fixed 0000000272180000+0xd4000 prot=0xf2 phys=0x6f480000 1799: [memory] remap fixed 0000000272180000+0xd4000 prot=0x0 phys=0xffffffffffffffff 1801: [memory] unmap 0000000272180000+0x100000 10201: [memory] reserve 0000000271a40000+0x1000000 flags=0x0 align=0x40000 10203: [memory] remap fixed 0000000271a40000+0x1000000 prot=0xf2 phys=0x6ee10000 10297: [flush] image 0x272190000+0xc0000 for idle 0x0+0xffffffffffffffff FATAL: unhandled exception 0xc0000005 on thread 28132 'Loading.PreloadManager' rax 000000027219a9e0 ... rdi 000000027219a970 ... r13 000000027219a960 ``` The 16 MiB block mapped at line 10203 is Unity's heap; the idle store at line 10297 writes the image of the unmapped range over it, and the allocator reads the overwritten words (`0x27219a960` lies in that image). The other crashes are access violations on other threads (`AssetGarbageCollectorHelper`, `UnityGfxDeviceWorker`, `Gfx Task Executor`, unnamed ones) after the same kind of idle store. This PR: - The unmap observer (`retireUnmapped`, also called for allocation removals) drops the results pending in images lying wholly inside the removed range through `StorageTexture::DiscardPendingInside`, the path a fill over a whole image already takes. For block-unit images it drops the pending units inside the range. - The observer is installed once a storage image exists (`StorageTexture::AnyCreated`), next to the multisample and depth guards that already share it. - `IdleStoreOverUnmap` maps the surface again at the same address inside the unmap's mutation and checks that the idle store and `FlushAllPending` do not write the old results into it. Its previous check that the results are still pending while the mutation is held no longer applies, as they are dropped at the unmap; when the address cannot be mapped again it keeps the previous settle check. - The #1970 debt entry notes that an unmap drops only the results pending wholly inside its range. Lock order: the observer runs under the registry mutation and takes `Pending().mutex`, which is a leaf (no path takes the registry lock while holding it; see `blocksKept`). A store that holds a lease finishes before `RequireUnpinned` lets the unmap proceed. Still not covered (debt entry): an image only partly inside an unmapped range, and memory reused without an unmap. Overlap (`tools/pr_overlap.py`): #7600 also extends `retireUnmapped` and the observer condition in `MultisampleTarget.cpp` for its multisampled clear records; whichever merges second keeps both calls and both conditions. #6926 and #8707 edit the same #1970 debt entry for other cases (host writes over pending results, DCC images with several mips); only the text needs merging. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 Pro 10.0.26200, AMD Radeon RX 9070 XT (AMD proprietary driver 26.10.1, LLPC), WinLibs GCC 15.2.0 r7. Heavenly Bodies (PPSA04246, 01.006.004), converted from the dump on `main` 53a0a27bb, with this PR rebased onto that commit for the second row. 45 s runs from start into the first mission (cross pressed every 2 s through `APS5_PAD_INPUT_FILE`), `APS5_PROFILE_DRAW=1 APS5_LOG_TIMESTAMPS=1 APS5_TRACE_FLUSH=1 APS5_TRACE_MEMORY=1`, after one warm-up run: | | runs | crashed | idle stores into a range unmapped earlier in the run | presents per 10 s in the mission | |---|---|---|---|---| | `main` 53a0a27bb | 10 | 5 | 17-79 in eight runs, 0 in the two whose idle store ran once | 321-394 (the five that survived) | | this PR | 10 | 0 | 0 | 360-365 | Every crashed run had 61 or more such stores. The crashes are access violations on `Loading.PreloadManager`, `Gfx Task Executor` and unnamed threads, and once `unsupported PM4 packet type: header 0x00000000` in an overwritten command buffer. With this PR the idle store still runs (37-42 stores per run instead of 88-145); only the stores into unmapped ranges are gone. Earlier, on `main` 7ab4b2e56 with the same settings, `main` crashed in 6 of 15 runs and this PR in 0 of 15; without `APS5_TRACE_FLUSH`/`APS5_TRACE_MEMORY`, `main` crashed in 2 of 15. One of three played runs on 7ab4b2e56 also crashed there (`AssetGarbageCollectorHelper`). GPU tests on `main` 0aa55ef29 and on this PR rebased onto it, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R "idle_store|multisample_retirement|native_multisample_depth|cached_image_multisample|host_import|guest_memory_lifetime|flip_stores|pipelined_fill|pattern_buffer_fill|fill_cover|depth_storage_reuse|memory_value_fill|pending_exact|clamped_global_fill|constant_dword_fill" -V`: - `main` with this PR's `IdleStoreOverUnmap.cpp`: 30 of 31 passed, `agc_driver_idle_store_over_unmap` failed with `idle store over unmap: results of the unmapped surface were stored into the memory mapped over it`. - This PR: 31 of 31 passed. - Both: `agc_host_import_sparse` skipped (udmabuf imports are Linux only); the validation layer loaded (no `was not found but was requested by env var VK_INSTANCE_LAYERS`) and neither log has a `VUID-` error. Rebased onto `main` 3e91c8edf after #7600 merged. #7600 also extends `retireUnmapped` and the unmap observer condition in `MultisampleTarget.cpp`; the rebase keeps both calls (`forgetSingleClears` and `DiscardPendingInside`) and both conditions (`anySingleClears()` and `StorageTexture::AnyCreated()`), and changes nothing else. The GPU tests above were run again there, with `agc_driver_graphics` and `agc_driver_msaa_dcc_clear_2/4/8` (where #7600's tests are) added: `main` with this PR's test 34 of 35 passed (`agc_driver_idle_store_over_unmap` failed with the same message), this PR 35 of 35, `agc_host_import_sparse` skipped, the validation layer loaded and no `VUID-` error. `tools/check_conventions.py --base upstream/main` passes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: PPSA04246 #8931 - [x] GPUs tested: AMD Radeon RX 9070 XT, AMD proprietary driver 26.10.1 (LLPC), Windows 11 Pro 10.0.26200 - [x] AI-assisted: yes The investigation, the change and this description were prepared with AI assistance (Claude Code); the in-game observations were made by me. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
simnJS commented 2026-10-21 15:51:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simnJS on 2026-10-21T15:51:28Z.


NVIDIA, Windows. GeForce RTX 4080 SUPER, driver 616.92 (32.0.16.1692), Windows 11 Pro 26200, Ryzen 9 7950X3D, WinLibs GCC 15.2.0 r7, Release.

This PR 470938394 merged into main cecb540a6 (clean merge), and the same tree with the three non-test files (Texture.hpp, MultisampleTarget.cpp, Texture.cpp) put back to main. ANYPS5_GPU=NVIDIA ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ctest --test-dir build -R "^(agc_driver_idle_store_over_unmap|agc_driver_storage_read_during_idle_store|agc_driver_multisample_retirement|agc_driver_native_multisample_depth|agc_driver_msaa_dcc_clear_2|agc_driver_msaa_dcc_clear_4|agc_driver_msaa_dcc_clear_8|agc_driver_graphics)$" --timeout 120 --repeat until-fail:3 -V. Every Vulkan test logged Selected GPU name=NVIDIA GeForce RTX 4080 SUPER vendor=0x10de device=0x2702; none skipped.

test this PR main code with this PR's test
agc_driver_idle_store_over_unmap 3/3, 1.5-1.6 s 0/3, failed with idle store over unmap: results of the unmapped surface were stored into the memory mapped over it
agc_driver_storage_read_during_idle_store 3/3, 0.5-0.6 s 3/3
agc_driver_multisample_retirement 3/3, 0.3-0.4 s 3/3
agc_driver_native_multisample_depth 3/3, 0.5-0.7 s 3/3
agc_driver_msaa_dcc_clear_2/4/8 3/3 each, 0.5-0.6 s 3/3 each
agc_driver_graphics 3/3, 6.4-6.5 s 3/3, 7.0-7.5 s

With this PR the idle store test did not print skipped the remap check, so the remap check ran. On main code, --repeat until-fail stopped at the first failure; two more runs of that test alone failed the same way. agc_driver_graphics uses a mocked Vulkan device and does not run on the GPU.

Not run with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation: the Vulkan SDK (Khronos validation layer) is not installed on this machine.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6320223541 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8932#issuecomment-6320223541). Originally posted by [simnJS](https://github.com/simnJS) on 2026-10-21T15:51:28Z. --- NVIDIA, Windows. GeForce RTX 4080 SUPER, driver 616.92 (32.0.16.1692), Windows 11 Pro 26200, Ryzen 9 7950X3D, WinLibs GCC 15.2.0 r7, Release. This PR 470938394 merged into `main` cecb540a6 (clean merge), and the same tree with the three non-test files (`Texture.hpp`, `MultisampleTarget.cpp`, `Texture.cpp`) put back to `main`. `ANYPS5_GPU=NVIDIA ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ctest --test-dir build -R "^(agc_driver_idle_store_over_unmap|agc_driver_storage_read_during_idle_store|agc_driver_multisample_retirement|agc_driver_native_multisample_depth|agc_driver_msaa_dcc_clear_2|agc_driver_msaa_dcc_clear_4|agc_driver_msaa_dcc_clear_8|agc_driver_graphics)$" --timeout 120 --repeat until-fail:3 -V`. Every Vulkan test logged `Selected GPU name=NVIDIA GeForce RTX 4080 SUPER vendor=0x10de device=0x2702`; none skipped. | test | this PR | `main` code with this PR's test | |---|---|---| | agc_driver_idle_store_over_unmap | 3/3, 1.5-1.6 s | 0/3, failed with `idle store over unmap: results of the unmapped surface were stored into the memory mapped over it` | | agc_driver_storage_read_during_idle_store | 3/3, 0.5-0.6 s | 3/3 | | agc_driver_multisample_retirement | 3/3, 0.3-0.4 s | 3/3 | | agc_driver_native_multisample_depth | 3/3, 0.5-0.7 s | 3/3 | | agc_driver_msaa_dcc_clear_2/4/8 | 3/3 each, 0.5-0.6 s | 3/3 each | | agc_driver_graphics | 3/3, 6.4-6.5 s | 3/3, 7.0-7.5 s | With this PR the idle store test did not print `skipped the remap check`, so the remap check ran. On `main` code, `--repeat until-fail` stopped at the first failure; two more runs of that test alone failed the same way. `agc_driver_graphics` uses a mocked Vulkan device and does not run on the GPU. Not run with `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`: the Vulkan SDK (Khronos validation layer) is not installed on this machine. AI-assisted: yes

Pull request closed

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!8932
No description provided.