AGC: guest unmap releases host-imported pages before the import is freed; NVIDIA can lose the device #2352
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 popcoder565 on 2026-10-09T16:09:22Z.
When guest memory is unmapped, its pages are released before the AGC driver retires the Vulkan host import that covers them. For host-pointer imports the Vulkan spec requires the opposite: the application "must ensure that the imported memory range remains valid and accessible for the lifetime of the imported memory object". On Windows with NVIDIA 616.92, freeing an import whose pages are already gone loses the device when the release comes soon after the import.
Where
GuestAllocationsUnmap_nid_postfix(core/libs/prx/libc/src/GuestAllocations.cpp) runsapplyfirst, which releases the pages:munmapinDirectMemory.cppcallsGuestArenaResetand the arena'sRelease, orVirtualFree(MEM_DECOMMIT). Only then does it swap the registry.refreshImports, fromHostImportForand the other callers inGuestBufferMemory.cpp).retireImportcan also leave thevkFreeMemoryto the end of the open batch.WaitForLeases), which runs only while a build holds a lease on the range, and the invalidator (ForgetPages), which runs afterapply.Measurements
Setup: Windows 10 (19045), RTX 3060 Laptop (NVIDIA 616.92), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release.
A local probe (not part of any PR) imports a 17 MiB guest-arena block with
HostImportForand releases it with the same arena calls as the unmap path. It then lets the nextHostImportForretire the import.vkQueueWaitIdlereports the queue as healthy before the retire'svkFreeMemoryand as lost after it:The same order caused the intermittent device loss in
agc_driver_recorder_tests(#2155):unitShadowTestsreleased its block before the import was retired. #2299 changes that test.Not established
A possible direction, if this is worth handling: retire the imports that overlap the range before
applyreleases the pages, and wait for any batch that may still read them. That could be a hook like the pin waiter.🤖 Investigated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T17:33:20Z.
The runtime unmap path is fixed on MarcZX100's fork branch.
The pin waiter now receives the mutation range, drains GPU work, and retires every overlapping host import before the unmap callback releases pages. A host import holds a strong reference to its registered range until retirement completes. The recorder test checks that the import is already gone inside the unmap callback, before releasing the block.
Verified: the
agc_driver_recorder_teststarget builds with GCC 12.2 on Linux, Conventions passes against currentmain, andagc_driver_recorder_tests --host-import-unmap-onlypasses on lavapipe (host import unmap lifetime: ok). The full recorder executable still fails earlier in the existing refresh-store case from #2155/#2248 on this upstreammainhead, before reaching the new test; I am not counting that run as a pass. Windows hardware and the full CI build matrix have not been run for this commit.Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T19:15:53Z.
@MarcZX100 I ran
1d1e9896on Windows with NVIDIA, where this device loss shows up. Setup: Windows 10 (19045), RTX 3060 Laptop (NVIDIA 616.92), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, merged ontomainae3d5f95together with #2248.It fixes the unmap path. A local probe imports a 17 MiB guest-arena block with
HostImportForand right away unmaps it throughMutation::Unmap. Its callback releases the pages with the same arena calls asmunmap. The probe then lets the next lookup run and checks the queue:main8b393b6e+ #22481d1e9896--host-import-unmap-onlypasses 10 of 10.agc_driver_recorder_tests30 times without #2299, and every run got through all its checks. So theunitShadowTestsdevice loss is gone too:Removegoes through the same pin wait.On Windows, though, the process now crashes at exit. Those 30 recorder runs crash this way, and in ctest so do
agc_driver_sampled_cache_budgetandagc_driver_sampled_cache_budget_memory. Each prints its pass line, then takes an access violation innvoglv64.dll. The call comes from the static destructor ofImports()(__tcf_…Imports…imports) and goes throughBufferPool::~BufferPool.refreshImportsnow stores a fullContextcopy in the staticHostImports(state.context = context). ItsbufferPoolshared pointer keeps the device's pool alive until static destruction, after theVkDeviceis gone. Two ways to avoid it: keep only whatretireImportneeds, the waydevice,destroyBufferandfreeMemoryare kept, or drop the copy when the device goes away. Apart from those two tests, the full ctest passes (505 of 507).🤖 Addressed by Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T20:47:31Z.
Follow-up: release the cached device context at shutdown
The Windows report found a second lifetime problem after the unmap ordering fix: the static host-import registry retained a full
Context, whosebufferPoolshared pointer survived until static destruction, after the Vulkan device was gone.I updated my existing branch
fix/host-import-unmap-lifetimeand pushed:2d07e1a8— retire overlapping host imports before guest unmap.baadd9d3— keep only the device handle and Vulkan destroy function pointers in deferred import retirement; the static registry no longer owns aContextor itsBufferPool.Validation on current
main8b393b6e(Linux x86-64, GCC 12.2, Release):agc_driver_recorder_testsbuilds.agc_driver_recorder_tests --host-import-unmap-onlypasses on lavapipe.agc_driver_sampled_cache_budgetandagc_driver_sampled_cache_budget_memoryboth pass on lavapipe (2/2).git diff --check upstream/main...HEADpass.These Linux results cover the same sample-cache tests that exited with access violations on Windows/NVIDIA, but cannot reproduce that driver-specific shutdown failure. @popcoder565 or anyone with the reported Windows/NVIDIA setup, could you rerun the focused unmap test and the recorder/sample-cache tests, checking that each process exits without an access violation in
nvoglv64.dll? The previous Windows report established that the first commit fixed the device loss, but that report predates this shutdown-lifetime follow-up.No PR was opened.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T21:45:01Z.
Rebased validation on current
mainI rebased the existing fork branch
fix/host-import-unmap-lifetimeonto upstreammaine603f3f5and force-updated it after confirming the old fork head wasbaadd9d3. The change remains the two focused commits for retiring overlapping host imports before guest unmap and avoiding a retained device-pool reference during deferred destruction.On the rebased source (Linux x86-64, GCC 12.2, Release, lavapipe):
agc_driver_recorder_testsbuilds;--host-import-unmap-onlypasses (host import unmap lifetime: ok).agc_driver_sampled_cache_budgetandagc_driver_sampled_cache_budget_memorypass (2/2).python3 tools/check_conventions.py --base upstream/mainandgit diff --check upstream/main...HEADpass.This Linux validation does not reproduce the Windows/NVIDIA shutdown access violation reported above. The retest on the updated branch is still needed to establish that the process exits cleanly on the affected driver. No PR has been opened.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T21:57:15Z.
Current-base refresh after #2299 merged
I rebased the existing
fix/host-import-unmap-lifetimebranch onto upstreammainaad16d05and pushed headc6bffc00to my fork after confirming the old remote head was58d8ebca. Upstream has now merged #2299, whose test-only cleanup covers theunitShadowTeststeardown. This branch still addresses the separate runtime unmap path: retire overlapping imports before guest pages are released and avoid retaining the device pool through deferred destruction.On the current source (Linux x86-64, GCC 12.2, Release, lavapipe):
agc_driver_recorder_testsbuilds;--host-import-unmap-onlypasses (host import unmap lifetime: ok).agc_driver_sampled_cache_budgetandagc_driver_sampled_cache_budget_memorypass (2/2).git diff --check upstream/main...HEADpass.The updated Linux run still cannot verify the Windows/NVIDIA shutdown path reported earlier. A Windows retest on this branch, including clean process exit for the recorder and sampled-cache tests, remains needed. No PR has been opened.
Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T22:34:14Z.
@MarcZX100 I reran your
c6bffc00on Windows: Windows 10 (19045), RTX 3060 Laptop (NVIDIA 616.92), MinGW-w64 GCC 15.2.0, Release. It was merged ontomain64fe7439together with #2248, plus #2462's two-line fix, which currentmainneeds to build on Windows.The exit crash is gone, and the unmap path still holds:
main), and the import was already retired at release in every run.agc_driver_recorder_testspasses 20 of 20,--host-import-unmap-only10 of 10, andagc_driver_sampled_cache_budgetand_memory5 of 5 each.But
agc_driver_graphicsnow segfaults in every run (10 of 10), atrip 0. The stack goesimportCrossingTests→GuestAllocationsRemove→WaitForLeasesAndImports→retireImport→~RetiredImport, which calls a nulldestroyBuffer.The cause is that
mockContext()has nodevice. Sostate.devicestaysVK_NULL_HANDLE, equal tocontext.device.refreshImportsthen never takes the device-change branch that loadsdestroyBufferandfreeMemory. Before this commit, the retire looked both functions up through the context, which the mock provides.Loading them whenever they are still null fixes it locally:
With that,
agc_driver_graphicspasses 5 of 5 and the full ctest passes (543 of 543). The unmap probe shows 0 of 20 device losses, and the recorder test passes 10 of 10.🤖 Addressed by Claude Code
Imported from boykopovar/AnyPS5. Originally posted by zaydzaari on 2026-10-09T23:32:32Z.
@MarcZX100 @popcoder565
I independently validated the Issue #2352 candidate (
c6bffc00) on another NVIDIA GPU generation.Environment
Results on the candidate:
agc_driver_recorder_tests --host-import-unmap-only: 10/10 passed, exit code 0; no device losses observed.agc_driver_graphics_tests: reproducible0xC0000005access violation at address0x0, matching the nulldestroyBufferissue already reported.Validation of the proposed fix:
I applied only the two initialization lines in
refreshImports:No destructor null guards were included.
With that change:
agc_driver_graphics_tests: 5/5 passed, clean exit code 0.agc_driver_recorder_tests --host-import-unmap-only: 10/10 passed.guest_memory_tests,agc_write_tracking_tests, andguest_agc_driver_resources_testspassed.The unrestricted recorder suite still exits unsuccessfully in
storeRunTests; I have not established that this is caused by the candidate patch.This independently corroborates the pointer-initialization fix on an RTX 5070. It also avoids potentially hiding cleanup failures by silently skipping Vulkan destruction when a function pointer is missing.
These are focused Windows/NVIDIA validation results, not a full cross-platform or retail-game compatibility certification.