fix(agc): wait for an image another thread is storing before a host read #7769
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7769"
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 jotitadev on 2026-10-19T06:50:53Z.
What
Fixes the race reported in #7744.
StorageTexture::FlushPendingmoves the overlapping images frompending.texturestopending.flushing, then stores them under the GPU mutex. A host read of the same range on another thread finds nothing intexturesand returns without waiting, so it reads guest memory before the store lands. The queue-idle store (StoreIdleResults: GPU mutex, thenFlushAllPending("idle")) is the usual other thread.StorageTexture::WaitForFlushing(address, bytes, except). When an image influshingoverlaps the range and this thread does not hold the GPU mutex, it takes the GPU mutex, releases it, and checks again until no overlapping image is left influshing. It returns whether it waited.Recorder::SyncThrough. It also releases the mutex before it erases its images fromflushing.FlushPendingcalls it when it lists no image and returnstrueif it waited. That return is what tells the flush hook to sync the recorded store. Without it, the read still saw zeros after the wait.FlushForAccess) calls it on its C4 path, which skipsFlushPendingwhenAccessKeptByCpulists no pending image. That is the path a file write's host read takes by default. A fix inFlushPendingalone left the repro at 200 of 200.flushingis empty almost always. An atomicflushingCount, written under the registry mutex next to each change offlushing, keeps the hot path to one atomic load.Lock order:
WaitForFlushingnever takes the GPU mutex while it holds the registry mutex.StoreIdleResults,FlushAllPendingand theUnregisterofFlushPendingtake the GPU mutex first and the registry mutex second, so the order stays the same. A thread that already holds the GPU mutex does not wait: the other writer may be blocked on that mutex. Readers on the hook path already take the GPU mutex (AccessKeptByCpu, the recorder sync), so the wait adds no new lock order.agc_driver_storage_read_during_idle_storeis a new test that reproduces #7744. A thread takes the GPU mutex and runsFlushAllPending. The main thread waits untilFindPendingreturns null for the surface, so the image is influshing, and then callsGuestMemory::Read. This runs for 20 rounds, and the test fails if any read sees the bytes from before the store. It covers the flush hook path, which a plainGuestMemory::Readtakes.agc_driver_storage_file_writealso gets the concurrent flush case from #7757, by @MarcZX10 (his test only, with co-authorship in the commit). One thread'sFlushPendingstores the surface while another thread holds the GPU mutex, and a second thread callsFlushPendingand thenGuestMemory::Readon the same range. That covers a reader that callsFlushPendingitself. Folded in at @oneandonlydean's suggestion in #7757.Tested
Current head
ba72cb78fmerged ontomain118266aec(clean), same machine and drivers as below,ANYPS5_NO_SHADER_CACHE=1:main+ only the two test filesagc_driver_storage_file_write, lavapipeconcurrent guest read returned before an overlapping image was storedagc_driver_storage_file_write, RTX 3070agc_driver_storage_read_during_idle_store, lavapipeagc_driver_storage_read_during_idle_store, RTX 3070ctest -R "^agc"(-j4) onmain118266aec: lavapipe 787 of 787, RTX 3070 782 of 782 (withoutagc_display_buffer_dcc,agc_driver_flipandagc_driver_lds_atomics64). With this change: 788 of 788 and 783 of 783, the same skips.agc_driver_bda_devicenow passes onmainhere too.check_conventions.py --base origin/mainandcheck_test_registration.pypass.Earlier runs, before the #7757 test was added:
Intel i7-8700, Ubuntu 26.04, GCC 15.2, Release,
BUILD_TESTING=ON, this branch onmain63e4a6380. Earlier runs onmain9f5d4bd92gave the same results. lavapipe is Mesa 26.0.3 withGALLIUM_OVERRIDE_CPU_CAPS=avx.mainagc_driver_storage_file_write"wrote only zeros", lavapipe,SIGSTOP70 ms every 20 ms, 100 runsAPS5_HOOK_FLUSH_CPU_BLOCKS=0, where the hook goes throughFlushPending.agc_driver_storage_file_writefailures underSIGSTOPstill happen. They come from the test's assumption that the images stay pending (for examplean unrelated pending GPU image was flushed), which #7745 addressed (merged).ctest -R "^agc"(-j4) has the same results asmainapart from the new test:agc_display_buffer_dcc,agc_driver_flipandagc_driver_lds_atomics64. The one failure isagc_driver_bda_device, which fails onmaintoo (#7584).python3 tools/check_conventions.py --base origin/mainandtools/check_test_registration.pypass.pr_overlap.pyreports no line conflicts.Not tested: Windows. The change uses only
GpuMutex,std::atomicandstd::this_thread::yield.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 github-actions[bot] on 2026-10-19T07:01:03Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppL1058-1059core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppL1492, L3675-3677, L3690-3692Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppImported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-19T08:04:13Z.
A Windows run, since the description lists Windows as not tested: RX 6700 XT (gfx1031, RDNA 2), AMD driver 32.0.21045.5002, Windows 11 26200, WinLibs GCC 15.2.0 r7, Release.
main7f596df48, then the same tree with this PR's head5937ef03emerged (clean merge).agc_driver_storage_read_during_idle_storemain+ only the new testidle store read: a host read did not wait for the image another thread was storingmain+ this PR0 of 20 host reads saw the memory before the storeSo the race reproduces on Windows with AMD's driver, and the PR closes it. The runs are also shorter with the PR: about 1.1 to 1.6 s each, against 4.9 to 6.7 s on
main.With the PR, and every related test executable rebuilt against it, the 33 tests matching
storage|idle|flush|host_read|write_backpass in each of threectest -j4runs, andagc_driver_storage_file_writepasses 20 of 20. (An earlier version of this comment mentioned twoagc_driver_storage_file_writefailures. They happened only in my first run, where that test's executable was still built againstmain's headers while the library had this PR. Rebuilt, it doesn't fail, so please disregard them.)Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T11:00:05Z.
Thanks @oneandonlydean for reproducing it on the 4090. I added the concurrent
FlushPendingreader case from #7757 toagc_driver_storage_file_writeinba72cb78f, keeping @MarcZX10's authorship (test only, co-authored commit).Head
ba72cb78fonmain118266aec, i7-8700, Ubuntu 26.04, RTX 3070 (595.99.02) and lavapipe (Mesa 26.0.3), 5 runs each:main+ only the test filesagc_driver_storage_file_writeconcurrent guest read returned before an overlapping image was storedagc_driver_storage_read_during_idle_storectest -R "^agc"matchesmainapart from the new test (lavapipe 788 of 788, RTX 3070 783 of 783). Conventions and test registration pass. The description is updated.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-19T11:58:00Z.
The new head
ba72cb78fon Windows/AMD: RX 6700 XT (gfx1031), driver 32.0.21045.5002, Windows 11 26200, WinLibs GCC 15.2.0 r7, Release,main7a36de66b. Full builds in both columns, so every test executable matches its tree.main+ only this PR's two test filesmain+ this PRagc_driver_storage_read_during_idle_storea host read did not wait for the image another thread was storingagc_driver_storage_file_write(with the new concurrentFlushPendingreader)concurrent guest read returned before an overlapping image was storedstorage|idle|flush|host_read|write_back(-j4)