feat(agc): import shared direct memory through udmabuf on Linux #943
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/943"
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 alfalfa-star on 2026-10-06T19:01:19Z.
What
On Linux with an AMD GPU, no host import of direct memory ever succeeded, so every direct-memory range the GPU used was copied.
Since #203, direct memory is a memfd mapped
MAP_SHARED(before that it was a/tmpfile). RADV imports a host pointer through amdgpu userptr, and libdrm'samdgpu_create_bo_from_user_memsetsAMDGPU_GEM_USERPTR_ANONONLY, so the kernel refuses any file-backed VMA. Measured on Linux 7.2 with libdrm 2.4.134:amdgpu_create_bo_from_user_memMAP_PRIVATEMAP_SHAREDEPERMMAP_SHAREDEPERMIn the driver this showed up as
[gpu] host import of … failed at vkAllocateMemory (-1000072003); falling back to copies. In PPSA14632 all 466 import attempts failed, and every one was arw-s /memfd:direct memorymapping.This PR imports such ranges as dma-bufs instead:
createImporttries the host pointer import first, so a driver that accepts shared mappings behaves as before. When that fails and libkernel reports a memfd backing for the range, the range's slice of the memfd becomes a dma-buf through/dev/udmabuf(UDMABUF_CREATE). It is then imported withVK_EXT_external_memory_dma_buf(VK_KHR_external_memory_fd), bound to the same buffer usage and device address as a host pointer import.MFD_ALLOW_SEALINGand sealed withF_SEAL_SHRINK | F_SEAL_GROWafterftruncate, as udmabuf requires. It is never resized afterwards.GuestArenaSetSharedBacking_nid_postfix(libc). The driver asksGuestArenaSharedBacking_nid_postfix(address, bytes, &file, &offset)and gets a CLOEXEC duplicate of the memfd and the range's offset in it. The range must lie in direct mappings that continue in one backing; adjacent mappings that continue contiguously count as one. Flexible memory, unmapped ranges and non-contiguous ranges return false./dev/udmabufcannot be opened, or the range exceeds udmabuf'ssize_limit_mb(64 MiB by default), the import fails as before and the range is copied.APS5_TRACE_HOST_IMPORTnow prints the/proc/self/mapslines behind a failed import, as it already printed theVirtualQueryregions on Windows.Windows is unchanged.
Tested
Linux: Arch, kernel 7.2.7, GCC 16.2.1, RX 9070 XT (RADV GFX1201, Mesa 26.2.3).
ctest: 359/359 pass.New
CheckDirectMemorySharedBackinginguest_memory_testscovers:A standalone check of the import path (memfd slice → udmabuf → dma-buf → RADV, then
vkCmdCopyBuffer/vkCmdFillBuffer) found 0 wrong words in either direction and nothing written outside the slice. The imported memory type is host-visible, coherent and cached. udmabuf accepted 64 MiB and refused 65 MiB.PPSA14632 (LEGO Horizon Adventures,
--to-intel), 3-minute runs from boot into its boot-flow movie, on a test branch that also has #157:[guestmem]readThe remaining failures are ranges above 64 MiB (
UDMABUF_CREATE): 112 to 576 MiB whole allocations, plus #290 windows above 64 MiB. The title's present rate did not change (5 per 10 s), so its frame rate is limited by something else.Windows: not tested. The Windows code paths are unchanged.
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)importAllocationaround thecreateImportcall this PR extends, and the two cherry-pick onto each other without conflicts./dev/udmabufisroot:kvm 0660on many distributions; this test machine had a per-user ACL on it.🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-07T08:38:55Z.
A note for whoever tests this on RADV: with these imports active, PPSA14632 (LEGO Horizon Adventures) hit amdgpu page faults and a ring reset within 1.5 to 3 minutes, and Unreal then aborted with
GameThread timed out waiting for RenderThread after 120.00 secs:The cause is not in this PR. It is a RADV bug that the larger number of in-place bindings exposes sooner. RADV (Mesa 26.0 and later) gives a
FREE_DESCRIPTOR_SETpool's allocatorbo_size + RADV_POOL_HEAP_OFFSETas its size, so a set can be placed up to 32 bytes past the pool's buffer. Our chain pools are 0x26000 bytes, a page multiple. In theRADV_DEBUG=hangdump, a 160-byte set sat at offset 0x25f80, and the shader'ss_load_b128of its storage buffer descriptor read 0x26000. Under GPU-assisted validation, the same overrun showed up on the CPU side as a segfault inradv_write_sampler_descriptor. A standalone repro on RADV: aFREE_DESCRIPTOR_SETpool sized for four sampled images (128 bytes) accepts a set of five (160 bytes), while a linear pool returnsVK_ERROR_OUT_OF_POOL_MEMORY.#1065 keeps the overrun on the pool buffer's last page. With it and this PR, the title ran 5 minutes without a fault. With the udmabuf
size_limit_mbraised to 1024, its 576 and 463 MiB direct memory buffers are imported too, guest memory upload time drops from 190 s to 13 s over the run, and the present rate goes from 5 to 15 per 10 s.Environment: Arch Linux, kernel 7.2.9, Mesa 26.2.4 RADV, RX 9070 XT (RDNA4), i7-13700KF.
Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-07T14:13:43Z.
Follow-up commit fc81399d fixes a large slowdown this PR caused on its own.
The import write-watch probe imports private anonymous scratch pages through
VK_EXT_external_memory_host. On amdgpu that path makes the pages read as written after every GPU submission (4 of 4 scratch pages written after a GPU read), so the probe decided that imported ranges are compared, not watched. Before this PR no import succeeded on Linux/AMD, so that decision never mattered. With the udmabuf path, most of direct memory gets imported, every import was unwatched, and every surface in direct memory lost its write stamps:untracked);On PPSA14632 that was over 800 GiB of storage uploads in 90 s.
udmabuf takes the memfd's pages without touching the process page tables, so these imports don't need that treatment. The commit:
ImportWatchedalready absorbs the pages the import itself touches);ProbeDmaBufImportWriteProtection: a sealed shared memfd scratch range imported through udmabuf, read on the GPU, then stored to from the CPU. dma-buf imports stay watched only when the GPU read marks no page written and the CPU store is still collected. Otherwise, or if the probe fails, they are compared like host-pointer imports;APS5_WRITE_WATCH_IMPORTS=watch|unwatchstill overrides both kinds.On RADV (RX 9070 XT, kernel 7.2.9):
PPSA14632 storage uploads drop from ~2.7 s to ~0.1 s of GPU time per 10 s, with no
untrackeduploads left. Presents go from ~10 to ~17 per 10 s with this alone, and to ~38 together with two separate fixes I'll open next (a larger draw-snapshot budget and a staging pool budget scaled to the device heap).ctest: 359/359 on this branch.Imported from boykopovar/AnyPS5. Originally posted by EdouardSence on 2026-10-07T16:46:51Z.
Test on the same GPU, with PPSA21567 (Astro Bot).
Setup: Linux (CachyOS, kernel 7.2.8, GCC 16.2.1), Ryzen 7 7800X3D, RX 9070 XT (RADV GFX1201, Mesa 26.2.3). oneandonlydean's
astrobot(93faece1) with this PR's two commits cherry-picked (aa83df4f, fc81399d; no conflict), bundledlibc.prxexcluded andstd::_Fiopenexported (both only to get this branch to load PPSA21567).host import of … failed at vkAllocateMemory (-1000072003), thenFATAL: an address-based build copies 12403 MiB of 35 registered ranges, past 64 MiB. The largest ranges are 0x10ca000000+0x120000000 (4608 MiB), 0x1000000000+0xca000000 (3232 MiB) and 0x500000000+0x87c00000 (2172 MiB).[write-watch] dma-buf imports keep write protection: yes, the FATAL is gone, and 7 imports still fail. All 7 are mappings of the executable itself (4 read-only, 3 file-backedrw-p), not direct memory.udmabuf.size_limit_mb=8192, plus an ACL on/dev/udmabuf(root:kvm 660here, and the user is not inkvm). Given the range sizes above, the default 64 MiB would leave the large ranges copied; I did not run with the default.AddSnapshot:compute shader 0x5005f6100: AGC graphics: guest snapshot differs from registered memory. WithAPS5_NO_SNAPSHOT_CHECK=1the title runs through the intro. I have not established whether the dma-buf path causes that difference.rw-pexecutable mappings become writable image mirrors that each address-based build compares in full (~3.7 ms per BDA draw here). #1176 makes them anonymous so they import.AI-assisted: yes (Claude Code ran the builds and wrote this report; the runs are on my machine).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T21:21:01Z.
Rebased onto main (mechanical conflict with
ClearImageMirrorsinGuestBufferMemory.cpp). Build + ctest 406/406 on RADV (gfx1035). RanimportWatchTestson RADV with/dev/udmabuf:dma-buf import probe: 0 of 4 scratch pages written after a GPU read, 0 after the import, 1 after a CPU store, which matches your RDNA4 numbers.Added two test commits:
Recorder.cpp: the testDevicenever enabledVK_KHR_external_memory_fd/VK_EXT_external_memory_dma_buf, socontext.dmaBufImportwas always false and the new dma-buf probe branch never ran. It is enabled now when the device has both extensions.GuestMemory.cpp: added a range that is not 16 KiB aligned (offset == page * 2 + 0x1000). Before this, droppingphys % PS5_PAGE_SIZEfrom the returned offset still passed. Also added a gap after a direct mapping, which must be refused.Note on merge order: your comment says that with these imports active, RADV's descriptor pool overrun faults sooner, and that #1065 fixes it. #1065 should land before or together with this PR.