Linux: guest memory moved into dma-bufs of memfds

amdgpu takes every user-pointer BO's pages again at each command submission, so the cost of a submission grows with the guest memory imported. On a Steam Deck OLED (RADV) a standalone probe (buffer device address on, as the runtime) measured a submission at 17.2 us with nothing imported, 20.4 us with 192 MiB held as three dma-bufs of 64 MiB, and 345.7 us with the same 192 MiB as 48 host-pointer imports of 4 MiB. One game's race held about 180 MB of imports: the recording thread spent 87% of its time in the kernel and set the frame rate.

Where /dev/udmabuf opens, the device has VK_KHR_external_memory_fd and VK_EXT_external_memory_dma_buf, a buffer of guest_usage() imports as a dma-buf (vkGetPhysicalDeviceExternalBufferProperties, not dedicated only), and the driver's imports are dear (vk_uma_imports_dear), auto takes the migrate way with dma-bufs (VK_UMA_WITH_DMABUF). A region's buffer is plat_gpu_buffer's memfd, sealed against shrinking, and plat_gpu_buffer_dmabuf makes a udmabuf of all of it, which Vulkan imports (imp_make_dmabuf, a host-visible, coherent and cached type where the driver offers one). The guest's pages then move into the memfd with mem_migrate, so the CPU keeps an ordinary shared mapping of them. A region is at most what one udmabuf holds: the module parameter size_limit_mb (/sys/module/udmabuf/parameters/size_limit_mb, 64 on SteamOS, which only root sets), and a list udmabuf has the same total limit. A range larger than that is copied.

A region holds whole windows, and a window is not all the guest's data that can move. Free pages in it stay as they are, inaccessible, with the buffer's pages behind them unused. A change of the mappings over a region with such holes ends it, as a mapping there would give the guest pages the buffer does not hold. Pool pages that a view maps at a second address can't move either: the buffer takes them where they are. The dma-buf is then a list udmabuf of parts (plat_gpu_buffer_dmabuf_parts), the pool's memfd for those pages and the region's own memfd for the rest (mem_region_parts), at most udmabuf's list_limit parts (1024). The pool's memfd is sealed against shrinking for this. A write through the view is found at the region's address (mem_alias_peers), and the pool frees those pages only after their guest pages changed, which ends the region. In one game's town every window that the GPU read and could not have was refused for a view's pages alone. With them, the reads copied over 600 presents went from about 1.25 million to 260,000.

udmabuf pins the memfd's pages when it is made. Nothing may punch the memfd while the dma-buf lives: the CPU would get new pages there and the GPU keep the old ones, with no error. plat_gpu_buffer_free only closes and unmaps. Pages of a region that the guest unmaps or decommits get other pages as in the Android way, and the region goes; its pinned pages stay held until the region's memory is freed. The pinned pages count as Shmem and as GTT.

On the Deck the same race went from the recording thread at 95% of a core (87% in the kernel) to 24% (12%), and the GPU's wait for submitted work from about 1.8 ms to 0.31 ms a submission. check_uma_migrate, check_uma_region_writes and check_uma_region_merge run on dma-buf regions where the driver takes no memfd as host memory and udmabuf works (vk_uma_migrate(2), vk_uma_can_dmabuf); before, they skipped on amdgpu.

Write tracking watches a dma-buf region's pages as the rest of guest memory (Imp.tracked, which vk_uma_import_in passes over), where it leaves an import's alone: the driver pinned the pages once, when the udmabuf was made, and leaves the CPU's page table alone at each submission. A probe on kernel 7.2.7 found userfaultfd's write-protect with PAGEMAP_SCAN reporting exactly the pages written on a memfd mapping whose pages a udmabuf holds, and minor faults registering there too. A region made or gone is no write. mem_migrate and mem_unmigrate keep what the tracking says of each page (plat_vm_share, plat_vm_unshare): the pages a scan would report are noted before the move, no scan runs during it, the new mapping is registered (ww_remapped, where every page reads as written) and the others are protected again, and the range stays read only from plat_freeze to plat_thaw, so no write comes between; tracking by protection keeps each page's state and protection through the move. A region that goes marks none of its pages changed (imp_destroy). A page marked written with no write reads as newer than a render target drawn there whose pixels are not in memory yet, and a texture over that target loads memory's bytes. A watch in place is none either. No scan looks under its trap, so its pages are scanned before they are trapped (vk_ww_watching). Let go of, they read as written, so they are scanned as they open (own.c's open hook, vk_ww_open), with their leaves claimed over both: of what that scan finds, only the pages whose bytes are not those from before count. The GPU's writes in place there reach the caches when their submit is done (gpu_mem_written_cpu), as for imports. check_uma_migrate checks that a region's pages answer vk_written_since, that a CPU write there is found, and that a page nobody wrote stays unwritten through a region made, moved on into a bigger one and gone; memory_alias checks the moves under both trackings. In the race above, the caches stopped comparing the regions' bytes: about 12.0 and 12.5 fps over its last 40 s against 10.7 and 11.2 with the regions untracked.

Placed maps

VK_EXT_map_memory_placed maps device memory at an address the caller gives: the guest heap as driver memory, with nothing to import or pin, and none of the per-submit cost of user pointers on amdgpu. vk/uma.c has it, off by default (vk_uma_placed, which check_uma_placed and check_uma_placed_view turn on), on a device with memoryMapPlaced and memoryUnmapReserve:

On an RX 6700 XT (RADV) a chunk reads and writes as fast as malloc'd memory, the GPU and the CPU see each other's writes with no flush, mprotect traps on it, and a submission costs the same with 256 MiB placed as with none. Before it is on by default: write tracking by protection on its pages (or every texture over the heap is hashed again at each check); one sparse buffer over the heap with each chunk bound into it (a range across two chunks is copied now); and a view and its source counted as one address by the in-place bookkeeping (live, out).

Measurements on a discrete GPU (RTX 2080, 246 MiB BAR)

Each scene is the benchmark save and script of one game, -H --fps-limit=0 --vsync=0, with NVN's calls run by the runtime, and vk_gpu_profile=1 for the queue's times. The machine ran other jobs (load 4 to 16), so a single frame rate moves by several frames a second from run to run.

Where the time goes in the copy path, measured before the branch moved to a newer base: a present's time, the GPU's busy time in its command buffers, and its idle time between submissions, split by what the CPU did. The present's time includes the profile's own cost, so it is longer than the frame rate gives:

ScenePresentGPU busyIdle, CPU still recordingIdle, after a GPU wait of the GPU threadIdle, submitted, not startedPCIe RX / TX
A (a town street, 28 fps)35.9 ms15.3 ms10.6 ms2.3 ms7.8 ms576 / 239 MB/s
B (a field, 28 fps)36.8 ms11.5 ms19.2 ms0.9 ms5.2 ms243 / 279 MB/s
C (a tower, 30 fps)44.5 ms8.7 ms24.9 ms0.0 ms10.8 ms243 / 123 MB/s
D (stairs, 37 fps)27.4 ms11.8 ms12.2 ms0.1 ms3.4 ms448 / 910 MB/s

The GPU is idle for 60 to 80% of a present, and nearly all of that is the GPU thread recording slower than the GPU runs. Round trips (the GPU thread waiting for the GPU, then landing results) are 0 to 2.3 ms a present, and no vk_finish waits are left in these scenes. The bus carries well under 1 GB/s each way, against about 12 GB/s that PCIe 3.0 x16 gives. These scenes are bound by the CPU's work on the GPU thread, not by PCIe or GPU stalls. "Submitted, not started" (3 to 11 ms) is the recording thread's lag and the compute queue's semaphores: the next thing to look at.

In place forced on this GPU (--vk-memory=2) ran all four scenes to their end with no validation message and with screenshots that differ from the copy path's only where the scene moves (animation, the time of a camera move). That build asked the driver again for every range in a refused window: scene B fell to 5 frames a second, the reason for the list of refused spans. Scene D's imports were all refused (a page in each window the driver could not pin), so it copied as before but paid for the releases' longer gates (29 frames a second against 37 copied).

The same two scenes A and B on the newer base, two rounds of the unchanged runtime against --vk-memory=2, with no profile:

SceneCopy, frames a secondIn place, frames a secondCopy, PCIe RX / TXIn place, PCIe RX / TX
A20.2, 14.935.0, 36.7998 / 315, 322 / 130 MB/s1303 / 400, 1231 / 381 MB/s
B23.9, 27.829.0, 29.2237 / 267, 192 / 210 MB/s409 / 1072, 464 / 1131 MB/s

With the profile, scene A's copy path took 103 ms a present, 45 ms of it with the GPU idle while the GPU thread recorded, and staged 5.5 GB every 600 presents. In place it took 29 ms, 13 ms of that idle, and staged 433 MB. The time that went is the GPU thread's hash and copy of each changed vertex range: the cost that a GPU sharing memory pays for nothing. Scene B was the same either way with the profile (34 ms a present). There the in place run put the frame's device ring in host memory (25 GB every 600 presents across the bus), which the copy run did not: why is not found yet, and it is where scene B's higher TX comes from.

The screenshots matched in both scenes, but for scene A's last one (difference 32 to 35 of 255, two copy runs 3): in place ran at twice the frame rate, and the game's camera had turned further by then. In place capped at 18 frames a second (--fps-limit=18) matched the copy runs there too (1.6 and 3.9). Under the validation layer with synchronization validation, scene A gave the same 30 messages in both modes (pipeline interface and output warnings, and a write-after-write of two image-to-buffer copies, all in the copy run too).

The hazard counter found 2,500 GPU-side writes every 600 presents in scene A on pages that unfinished work read in place, 300 in scene B. They are counted by page, so constant buffers beside vertices count too, and the screenshots show nothing. Which of them are real is not traced yet.


Made from docs/layers/gpu/ownership.md at 69237645142c. GPL-3.0-or-later.