fix(agc): publish a host write's whole units first so a unit shadow cannot land over it #17787
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/17787"
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 Mouhand-Kaddo on 2026-11-09T16:00:42Z.
What
Since #6926, Demon's Souls (PPSA01342) crashes on Linux most times it loads into the world. It segfaults in its own code while loading (
DemonsSouls.elf+0x1788ede). Reverting #6926 or settingAPS5_NO_UNIT_SHADOW=1stops the crash.Why. A file read can lose its bytes to a unit shadow's GPU results.
PrepareHostWriteflushes withReadSite::Store, soFlushForAccessusesPublishScope::PartialUnits. The units the host write covers wholly stay fresh in their slab. PerUnitShadow.hpp, that is right only if the writer then stamps them, and a host write does not stamp: the tracker only learns about it when a collect walks the written pages.address-based heapspublish inGuestBufferMemory::UploadFinish. Its collect runs before the pages are written, so it judges the units fresh and its slab → import copy lands over the file bytes.The fix, as proposed by @JJenkx (co-author):
PrepareHostWriteflushes under its ownReadSite::HostWrite, soFlushForAccesspublishes every unit over the range (PublishScope::Whole) before the write. Nothing is left fresh for a later publish to copy over the file bytes, and a short read keeps the GPU's results in the bytes it does not write. Driver stores (ReadSite::Store) keepPartialUnits.WritesGuestMemory(site)keeps host writes counted as stores in the hook sync profile.publishUnitsmarks its units published before it notes its copy as a pending write, so the host write finds nothing to publish and nothing pending yet.publishUnitsnow counts itself in progress for its whole body (AnyPublishInProgress), and a host write that sees one in progress takes the GPU mutex and waits for pending writes over its range. With none in progress it stays lock-free.Tested
Linux 7.2.15, RX 6900 XT (RADV, Mesa 26.2.7), GCC, Release.
New test
agc_driver_host_write_shadowed_unit. It shadows units of memfd-backed (dma-buf) guest memory and opens aHostWriteover them.main.It skips without
/dev/udmabufor unit shadows.agc_driver_host_write_shadowed_unit,agc_driver_host_write_pending_storage,agc_driver_host_write_hookandagc_driver_gcn_display_present_triggerpass, with and withoutAPS5_PROFILE_DRAW=1.Validation layers (
VK_LAYER_KHRONOS_validation1.4.363):ctest --test-dir build -R 'agc_driver_host_write|agc_driver_gcn_display_present_trigger' -VwithANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer, on this PR and onmain1414f2c640, with and withoutAPS5_PROFILE_DRAW=1. Every log showsInsert instance layer "VK_LAYER_KHRONOS_validation"for each test instance and none has the layer-not-found message. Neither tree logs aVUID-line. Rerun on this head after each rebase ontomain: same result.Demon's Souls PPSA01342 v1.004, output 1920x1080, warm shader caches. Each run presses through the menus and keeps running 45 s after loading starts, past the point where
maincrashes. Runs onmain1414f2c640, before that rebase, interleaved one by one in one session, with nothing else running:main+0x1788ede)Earlier runs on
mainfaea44f22c: the Whole fix without the in-progress check crashed 2 of 20 times, both in the first 10 while another build was running on the machine (+0x10da241in the menus, through the same game call path as the null call above, andnull or misaligned index baseafter loading). The stamp head survived 20 of 20.Host write cost:
agc_driver_host_write_pending_storage_tests --cost 20000, pinned to one core, 5 runs: 73 to 90 ns per 64 KiB host write, the same as the 72 ns measured onmainbefore. A host write takes the GPU mutex only while a publish is in progress anywhere, so it can wait out another thread's publish then; that cost was not measured.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)Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-14T14:35:52Z.
Review: APPROVED
Windows check of head
2d2c4ec1adon two GPUs, merged ontomain3ffcba66e0. The bug reproduces on Windows, and this PR fixes it there, using a Windows port of the new test (below). It may also be what @moong15 sees in Stellar Blade on Windows.Setup: Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (
x86_64-ucrt-posix-seh, r3), Release, run from PowerShell withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1and the Khronos validation layer. GPUs: NVIDIA GeForce RTX 5070 Ti (621.01) and the Ryzen 7 7700X's integrated AMD Radeon (Adrenalin 26.10.2).maindoesn't build on Windows right now (#18037'ssetenvinUploadCacheReadOnlyFlush.cpp), so both sides carry #19899 only so they build.No regressions:
ctest -R '^agc|gpu|vulkan|video_out'onmainand with this PR:mainThe one Radeon failure is
agc_driver_sample_order_grid_referenceon both sides (fixed by #9503). The extra test isagc_driver_host_write_shadowed_unit, which skips here. No other test changes its result, and there are no validation messages on either GPU, on either side.The new test on Windows. As it is, it skips on Windows ("udmabuf imports are Linux only"). Only its setup is Linux-specific. On Windows, a 1 MiB guest-arena block imported through a host pointer stays write-watched: the driver's import probe reports "host imports of write-watched memory keep the write watch: yes … direct host imports of private memory are settled and stay watched" on both GPUs. So I ported the setup, leaving every check as it is (patch below), and ran it 3 times per GPU:
main's driver code (the PR's 5 production files reverted)On
main, both GPUs fail the same 4 cases. In the two "host write over a shadowed unit" cases and "host write during another thread's publish of its unit", byte 0 holds the shadow's GPU results instead of the host write's. "short host write of 65553 of 131072 bytes at +0" fails with "byte 65553 holds 51, not 190". The other 8 short-write cases pass onmaintoo. With this PR every case passes, with no validation messages.The Windows port of HostWriteShadowedUnit.cpp (built and run on both GPUs; macOS keeps a stub)
Windows arena memory is write-watched without
GuestWriteWatchRegister, as inSampledCacheBudget.cpp'sWatchedSurface. 1 MiB is also Windows' floor (settleFloorBytes) for keeping a settled import watched. Themainfailures and the PR passes in the tables above are from this patch plus one diagnostic print. The patch as shown was then built and passed once more on each GPU. I haven't built it on Linux or macOS.I'd suggest adding the Windows setup, so the regression test also covers the platform where Stellar Blade crashes. That's optional for this PR.
Reading the change: tagging host writes
ReadSite::HostWriteinstead ofStoreis what makesFlushForAccesschoosePublishScope::Wholefor them.WritesGuestMemorykeeps the hook-sync profiling treating them as stores. TheAnyPublishInProgresswait covers the case where another thread's publish is already under way. One small, non-blocking point:PublishShadow's skip inside a completion (UnitShadow.cpp,Recorder::InCompletion() && CurrentReadSite() == ReadSite::Store) still names onlyStore. I found no path where a host write runs inside a completion: the driver's ownGuestArena::HostWriteis underOwnHostWrite, soPrepareHostWritereturns early for it. ButWritesGuestMemorythere would keep the two sites alike.Not run: Linux, macOS, and a title (I don't have Demon's Souls or Stellar Blade).
AI-assisted: yes (Claude Code)