fix(agc): settle dma-buf imports on Linux on the settle queue #5905
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5905"
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 hugodearaujo on 2026-10-14T15:12:28Z.
What
Hades (PPSA03355) stops about 9 s after launch on current
main(Linux, RADV), every time, with no input: a SIGSEGV in its bundled libc'smemcpy, writing to address 0, called fromapp.elf+0x308059. The title's texture upload copies into a texture whose creation failed: its own renderer (The-Forge/Prospero/Common_3/Renderer/Prospero.cpp) takes the "Failed to allocate memory for texture" path atapp.elf+0x31aa39, because a 256 MiB staging pool it allocates from (mapped at0x301a00000) is full.How the pool fills (measured on
4469ea22f):0x311a00000+0xc0000000), which is imported as a sparse dma-buf (#5309).vkQueueSubmitafter that import took 1.85 to 1.89 s (measured insideRecorder::Submit), and it lands in the title's second frame. The flip submission of that frame is held behind the queue 0 worker for that time (#1745), so the frame lasts about 2.5 s.Allocations per frame, counted at the title's allocator (
app.elf+0x315e00), keyed by its frame fence:This PR settles every dma-buf import of 1 MiB or more right after it is made, the way #6060 settles Windows host imports of write-watched private memory:
settleImportcopies 4 bytes out of the new import on the settle queue and waits for that copy. The kernel then maps the pages in that submission, on the thread that made the import.shareDmaBufImport) instead of aliased as on Windows, since a sparse import is bound to many memory objects.mainthe first submission that used the import did. That took 0.47 to 2.2 s for the 3 GiB range of Hades and 0.49 and 4.3 s for the 4.5 GiB range of ASTRO BOT in the runs below; both titles map these ranges at start-up. A title that maps large ranges during gameplay would block its mapping thread for as long (TechnicalDebt, Settled dma-buf imports).The Windows path is unchanged: the same queue selection, alias and call site, with the
#ifdefs moved.Measured
RX 9070 XT (RADV, Mesa 26.2.4), Linux 7.2.9, udmabuf
size_limit_mb64, no input. The runs alternate betweenmain(4224d240a) and this PR:mainOne more run of this PR on its own also reached the end.
Settle times with
APS5_TRACE_HOST_IMPORT=1:Silksong and ASTRO BOT reach the end of their runs, as on
main. About 19 GiB of swap was in use on the machine during these runs, which makes pinning the large imports slower.Tested
agc_pending_key_storesandagc_driver_tessellation, which fail the same way onmain(4224d240a).agc_driver_sparse_dma_buf_importnow checks that the 1 MiB import and the sparse import made at map time are each settled (HostImportsSettled) when they are dma-buf imports. A device can import shared memory through host pointers instead, as lavapipe does, and such imports are not settled (9bd554a64); a device with a settle queue that imports that way would have failed the check. The #5390 report suggests ANV on an Intel Arc B390 imports that way, which I could not check. On RADV both checks run and pass 3 of 3, and with the settle call removed the test fails at the first of them. Under lavapipe (Mesa 26.2.4, one queue) there is no settle queue, and the test passes 3 of 3 with that note, as @jotitadev found.^agc.*(import|dma|sparse|shadow|unit)pass on RADV and under lavapipe, exceptagc_shadowed_key_stores, which skips on both.audio_out_shutdownandguest_kqueue(0xc0000409) out of 1288;main(4224d240a) on the same VM fails those two andaudio_out2_shutdown. The GPU tests skip in this VM.tools/check_conventions.pypasses.tools/pr_overlap.pylists only file overlaps; no open PR settles imports or changes the settle queue.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T17:17:56Z.
Review: COMMENTED
The diagnosis is careful. A 1.9 s first
vkQueueSubmitwhile the kernel maps a 3 GiB dma-buf lands in the title's second frame, and its own staging pool can't cover it. Moving that cost to map time is a reasonable answer.My concern is the skip. When another thread holds the GPU mutex,
touchDmaBufImportsis skipped (as the debt entry says), so the fix only applies when the map happens while nothing is recording. That holds for Hades' start-up mapping. For a title that maps a large range while rendering, the GPU mutex is likely held: the queue worker takes it for every packet. The stall then returns to the frame, and whether it does depends on timing. Two things would make this easier to judge:MappedDmaBufImportsTouchedcounts the touches. A counter for skipped touches, printed with the import stats, would show whether the Hades runs (and Silksong/ASTRO BOT) ever hit the skip, and would make a regression visible when a title does.main.Small point: each call creates and destroys a command pool, a scratch buffer, its memory and a fence. That's fine for a start-up-only path, but if the sparse path runs for every large map, keeping one pool and scratch buffer on the import state would avoid the churn.
Imported from boykopovar/AnyPS5. Originally posted by hugodearaujo on 2026-10-14T18:02:55Z.
Thanks. I made all three changes in 3b7090fa0.
MappedDmaBufTouches()returns how many imports were used at the map, deferred, used by the worker, or retired before it got to them. The first 8 skips print a[gpu]line. WithAPS5_TRACE_HOST_IMPORT=1, every copy prints its size, where it ran (at the map or by the queue worker) and its time.ClearHostImports). Each use resets the pool and the fence.Where the skip happens, from runs with the trace on (RX 9070 XT, RADV, no input):
So Hades does hit the skip, but only for the 256 MiB ranges it maps after start-up. The worker then pays up to about 15 ms for them, before it executes its next submission (on
main, that submission'svkQueueSubmitpaid it). All 8 runs reached the end of their time limit with no crash.Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T19:11:42Z.
Thanks. The deferral and the counters answer both points, and the table shows Hades does hit the skip.
The new path shows one more cost.
touchDmaBufImportswaits on its fence (vkWaitForFences,UINT64_MAX) while it holds the GPU mutex. The touch goes tocontext.queue, the same queue the recorder submits to. On a single hardware queue, the fence signals only after the GPU has finished everything already queued there. On the worker path, that means a full GPU drain before each submission that follows a deferred map, and every other thread that needs the GPU mutex waits behind it. That is probably part of the 15–28 ms "worker's longest copy" in the table, on top of the page mapping itself. The map path has the same wait, so with Silksong's 68–74 touches per run at the map, a guest thread now drains the queue during play.The cost this PR targets is paid in
vkQueueSubmit(the 1.9 s first submit in the description), not in the GPU running a 4-byte copy. So the submit alone should be enough. Wait for the fence only before the command buffer is reused, at the next touch (where it will almost always have signalled already) and inClearHostImports. The trace line could then time the submit and the wait separately, which would show where the time goes.Smaller: on the map path,
touchDmaBufImportsruns after thestate.mutexscope has ended, but it still iteratesstate.importsand usesstate.touch. Its comment says it runs under the import state's mutex, and the worker path does take it. Another thread mapping at the same time can changestate.importswhile this one reads it. Two fixes would work. One is to takestate.mutexaround the call (GPU mutex first, thenstate.mutex, as inTouchDeferredDmaBufImports). The other is to collect the buffers under it, as long as an import can't be released without the GPU mutex.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:11:27Z.
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/GuestBufferMemory.cppL2559-2560core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppL2559-2560core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppL274core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppL274-275core/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppL72-73core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppL195-196, L274, L652-655, L735-736, L2297-2298, L2559docs/dev/ARCHITECTURE.mdL112docs/dev/debt/libSceAgcDriver.mdL77-78core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppL2559-2560docs/dev/ARCHITECTURE.mdL111-112Some 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/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppdocs/dev/ARCHITECTURE.mddocs/dev/debt/libSceAgcDriver.mddocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/execution/SparseDmaBufImport.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/ARCHITECTURE.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppImported from boykopovar/AnyPS5. Originally posted by hugodearaujo on 2026-10-15T14:22:25Z.
Thanks. Both points went away with a different approach, now in
e8b7361a1, rebased onmain.#6060 landed this morning with a settle queue on Windows:
settleImportcopies 4 bytes out of a new import on a queue of its own and waits for that copy, because the imports lock comes after the GPU mutex and the main queue can't be used there. That is the constraint this PR had, so it now uses that path on Linux instead of the main queue. The settle queue is made when the device imports dma-buf memory, and every dma-buf import of 1 MiB or more is settled right after it is made. Nothing on that path takes the GPU mutex, so nothing is skipped or left to the queue worker, and the fence covers only the copy: the settle queue has no other work, so the recorder's queue is no longer drained. The command pool, the deferred list and the counters from the earlier heads are gone.What I measured on the old path before switching, since it answers your question about the cost:
vkQueueSubmit, as you expected: without the wait, the 3 GiB import's submit took 0.27–1.8 s at the map.On the smaller point: in 3b7090fa0 the map path did take
state.mutexafter the GPU mutex, right before the call (GuestBufferMemory.cpp:2501), but that code is gone now.A correction to my last reply: "All 8 runs reached the end of their time limit with no crash" left out that two Silksong runs and one ASTRO BOT run then crashed at exit, after SIGTERM. Those are the known exit crashes (#5614, #5864), not this path.
The description has the numbers for the new head: in runs alternating with
main, Hades stopped in 12 of 12 runs onmainand in 0 of 12 with this PR.Imported from boykopovar/AnyPS5. Originally posted by hugodearaujo on 2026-10-15T15:14:32Z.
A second title that needs this: ASTRO BOT (PPSA21567). On
main, right after it maps its largest ranges (4.5 GiB at0x10ca000000, and in one run 3.2 GiB at0x1000000000), the first graphics-queue submission that uses them pays the page mapping. Compute queues0x40and0x48wait on that queue's labels, and theirWAIT_REG_MEM_64gives up after 1000 ms:60 s runs from launch with no input, RX 9070 XT (RADV),
APS5_TRACE_HOST_IMPORT=1:main(4224d240a)4224d240aff23657f3With this PR the same imports are settled at the map (the 4.5 GiB one in 0.5–4.3 s), and no queue waits past its timeout.
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-15T18:20:57Z.
The settle queue is the right home for this. The fence covers only the copy, nothing waits on the GPU mutex, and the recorder's queue is no longer drained. The ASTRO BOT
WAIT_REG_MEM_64timeouts are a more convincing case than the Hades stall alone. Thanks for measuring the fence-reuse variant too: 13–15 waits of up to 41 ms per Silksong run shows why waiting only before reuse wasn't enough.One thing worth saying in the description: the 0.5–4.3 s for the 4.5 GiB import is now paid by the guest thread inside its map call, where on
mainit landed in a submission. At load time that's the better place. A title that maps large ranges during gameplay would see that thread block for the same time, so a line in the debt entry would help whoever traces such a stall later.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-15T22:16:08Z.
Review: COMMENTED
Linux results on NVIDIA and lavapipe. On both, the new path is not taken and the two new checks in
agc_driver_sparse_dma_buf_importdo not run.Machine: NVIDIA GeForce RTX 3070, driver 595.99.02, Vulkan 1.4.329, Ubuntu 26.04 (kernel 7.0.0-38), i7-8700. GCC 15.2, Release,
BUILD_TESTING=ON. Heade8b7361a1merged ontomain31cbb55d7, no conflicts./dev/udmabufis accessible andsize_limit_mbis 64.ANYPS5_GPU="NVIDIA GeForce RTX 3070", confirmed in the logs.GPUs tested: NVIDIA GeForce RTX 3070 (595.99.02), Linux; llvmpipe (Mesa 26.0.3), Linux
dmaBufImportis false on the proprietary driver (VulkanDevice.cppexcludesVK_DRIVER_ID_NVIDIA_PROPRIETARY, and this driver does not exposeVK_EXT_external_memory_dma_bufanyway). Sosettlesis false, no settle queue is created, and the test printsnote: the device has no settle queue, so dma-buf imports are not settledand passes. The imports in that test go through host pointers here. This is not a regression, just a path this device never takes. The graphics family has 16 queues, so the device would get a settle queue if it imported dma-bufs.VK_DRIVER_FILESset to the lavapipe ICD only): lavapipe does exposeVK_EXT_external_memory_dma_buf, but it has a single queue family with one queue. So no settle queue is created there either, and the test prints the same note and passes. The fork CI runs on lavapipe, so CI does not exercise the twoHostImportsSettledchecks.agc_driver_sparse_dma_buf_import,agc_host_import_sparse,agc_host_import_retireandagc_driver_host_import_at_mappass 3/3 with this PR and 2/2 onmain, withAPS5_TRACE_HOST_IMPORT=1.ctest -R "^agc" -E "agc_display_buffer_dcc|agc_driver_flip|agc_driver_lds_atomics64" -j1 --timeout 60: 591/593 pass, the same as unchangedmainhere.agc_driver_stencil_ref_exportis skipped (noVK_EXT_shader_stencil_export), andagc_driver_store_over_untrackedfails as it does onmain(#6385).So far only the RADV run in the description covers the settle itself.
Written with AI assistance (Claude Code). I ran the tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by hugodearaujo on 2026-10-16T11:29:49Z.
@CnRJay Done in
9bd554a64. The debt entry (Settled dma-buf imports) and the description now say that an import made at map time settles inside the guest's map call, so the mapping thread waits while the kernel maps the pages: 0.47 to 2.2 s for Hades' 3 GiB range and 0.49 and 4.3 s for ASTRO BOT's 4.5 GiB range in the measured runs. Both titles map these ranges at start-up, and the entry says that a title mapping large ranges during gameplay stalls its mapping thread for as long.@jotitadev Thanks for running it on both. Your results led to one more fix in the same commit.
createImporttries a host-pointer import first and uses dma-buf only when that fails, and lavapipe accepts host pointers for shared memory; on #5390 that broke a lead check. The settle applies only to dma-buf imports, so on a device that has a settle queue and imports shared memory through host pointers, the twoHostImportsSettledchecks would have failed. The test now expects a settle only for a dma-buf import and prints a note otherwise. Neither of your devices reaches that case, since neither has a settle queue, but a device with more queues that accepts host pointers would. ANV on Intel may be one; I could not check that.I ran it again on RADV and with lavapipe from Mesa 26.2.4. On RADV both settle checks run and pass 3 of 3, and with the settle call removed the test fails at the first of them. Under lavapipe it passes 3 of 3 with the note that there is no settle queue, as you saw. The 21 tests matching
^agc.*(import|dma|sparse|shadow|unit)pass on both, exceptagc_shadowed_key_stores, which skips on both. So the settle itself is still covered only on RADV, as you said.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T12:14:39Z.
Checked the settle-queue selection on Linux (second queue of the main family, or another copy-capable family), the concurrent sharing of dma-buf import buffers with the settle family, that the Windows path is unchanged apart from the moved
#ifdefs, and the debt/ARCHITECTURE entries (the map-time stall is documented). Build and AgcDriver ctest on RADV pass (agc_driver_tessellationcrashes, reported as failing onmaintoo);agc_driver_sparse_dma_buf_importfails to build without the change. Merging once the full Build is green.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T18:03:48Z.
Re-checked against current
main(93e54cedd, includes #5950): merges cleanly, AgcDriver build and ctest pass on RADV (agc_driver_sparse_dma_buf_importpasses;agc_driver_tessellationcrashes, reported as failing onmaintoo). Merging once the full Build is green.