Releases. The copy path read guest bytes when a command was recorded, so a release (a syncpoint, a semaphore) could
be performed once the work before it landed and the stream stopped (release.c). In place, the GPU reads the bytes
when it runs the command, and after a release the guest writes the next frame's data there. So a release the stream
stops holding also waits for every command recorded up to then that read guest memory in place
(hold_inplace, vk_uma_bound), the commands after it too, which the console's GPU runs at once.
tests/vulkan_release.c checks it.
Pages that change under an import. The driver pins the pages it imported. When mem.c gives guest pages back
(unmap, decommit), or moves them into the alias pool (promote_nl), the host pages at that guest address are other
pages, and the import still has the old ones. mem.c tells the renderer (mem_set_pages_replaced), and the imports on
those pages are destroyed before anything else is submitted (vk_uma_mapping_end). A driver keeps every import in
every submission, and amdgpu takes all their pages again in each one's ioctl: one page gone fails the submission
(The CS has been rejected (-14), EFAULT), and the device is lost. So before the pages change, the commands that
read an import over them run, and every submission the recording thread holds reaches the driver
(vk_uma_mapping_begin). Pages becoming GPU-owned are trapped, which amdgpu can't take either: the imports over
them go before the trap (vk_uma_owning). An import dropped from the table (the least recently used, one a union
replaced, a save state's load) is destroyed once the submit that last read it is done, or before its pages change
if that comes first. The submit that last read an import is the one its last lookup was in, unless the draw's setup
submitted after the lookup (a wait for a landing) and recorded its use in the next: so until the next draw or
dispatch begins, an import looked up since counts the submit being recorded as its reader (vk_uma_settle).
vulkan_headless (check_uma) unmaps and maps a range again, and checks that the GPU reads the new bytes;
check_uma_page_changes unmaps, traps and drops imports with a submission after each; vulkan_uma_imports.inc
traps pages under a read still running, under two imports, under the import made after a page landed, after an
unmap, and under a lookup used after a submit (SNX_TEST_UMA_ONLY=1 runs these alone, runtime_write or
constants one by name).
Write tracking. amdgpu takes an import's pages again, writable, at each submission, which undoes the protection a
scan put on them: the next scan reports every imported page written with no CPU write (16 pages of 16 after each
submission, none without the import). Trusted, those reports made a render target's write-back keep memory's old
bytes over its pixels (check_rt_overlap), and a submission after a scan faults every protected page in again
(16 ms for 32 MiB imported, against 0.3 ms with no scan). So the scans leave the pages an import holds alone, and
vk_written_since can't tell there (-1): its users compare the bytes. An import's pages count as written once when
it goes (vk_mem_changed), for what the CPU wrote there meanwhile.
Hazards. A write of the runtime's own (gmem_write: a front-end write, a constant buffer update, a target's
write-back) on a page that a submit not done yet reads in place waits for that submit (vk_uma_before_write, the
live pages), and the page is copied from then on (front), so the next write there waits for nothing. Any other
GPU-side write (a landed result) on such a page can change what that command reads: those are counted
(gpu_set_write_watch), not prevented, and the report every 600 presents gives the count:
vulkan: in place over 600 presents: N ranges (M MB) read in guest memory, N copied; N imports made (M MB, T ms),
N failed, N dropped; N imports hold M MB; N hazards (...)
The count is by 4 KiB page, so a constant buffer next to vertices on one page counts too.
Writes in place. The console's GPU runs its work in order: what reads a range in place after it was written in place
comes after a barrier, and so does what writes a range read or written before (order, by page since the last
barrier). A publish records its own barriers before and after its copy, so a draw that reads the result in the next
rendering pass reads it there without ending that pass. A GPU copy of bytes a submit not done yet writes in place
copies them from the import, after a barrier (owned.c's cpu_read, VIC's plane_stage), not from the CPU's view.
The runtime's own CPU readers (gmem_need, gmem_read) wait for that submit, and so does a write of the runtime's
there (vk_uma_before_write). The end of a submit with writes in place makes them visible to the host
(vk_uma_host_barrier), and a release waits for the submit that last bound guest memory in place, a write included
(vk_uma_bound). VIC and NVDEC wait for their pictures before host1x increments the job's syncpoint, which the guest
waits for before it reads them. The report every 600 presents adds N MB written in place (M MB of them engines'
results), N barriers for it, N waits for it.
Every Android driver takes AHardwareBuffers (VK_ANDROID_external_memory_android_hardware_buffer), and Qualcomm's has
no VK_EXT_external_memory_host. Where a device has the first and not the second, vk_memory=2 moves guest memory
into AHardwareBuffers instead of importing it where it is (vk_uma_way; the migrate way in uma.c), and auto copies,
as no phone has measured the moves yet (check_uma_way); uma_memory=2 moves it on any driver that takes them. The
windows a range touches become a region, a buffer of its own that Vulkan imports once, and their pages then move into
it with their bytes, mapped where they were
(mem_migrate: a copy, then the buffer's dma-buf mapped over the range, writers held off by plat_freeze). A region
that a range reaches moves on into one region of their union, once the writes in place pending in it have landed, as
the move copies what its pages hold (check_uma_region_merge). A region is 256 MiB at most: on an Adreno 830
(Snapdragon 8 Elite) Android's allocator gave 256 MiB, and 512 MiB aborted its service. The memory of an
AHardwareBuffer is taken whole when it is made and never swapped, so only what the GPU reads moves, and a region that
goes for idleness or room first moves its pages back into the reservation's own (mem_unmigrate).
A probe on that phone (Android 16) found the buffer's mapping to be the memory AHardwareBuffer_lock gives, the GPU
reading what the CPU wrote and the CPU what the GPU wrote with no sync, the CPU's bandwidth there that of malloc's
memory (31.5 against 31.2 GB/s), mprotect trapping there, and a submission costing the same with 16 or 256 MiB
imported (7 to 30 us), where amdgpu's user pointers cost 6.4 us more per MiB.
A region's buffer is memory of its own, which no change of the guest's pages touches. Pages of a region unmapped,
decommitted or moved into the alias pool (a view, a trap) get other pages there (give_back_nl maps fresh ones, as
decommitting shared pages keeps their bytes); the region goes then with no wait, the commands recorded before read the
bytes it had, and it is destroyed once the last of them is done. Write tracking leaves a region's pages alone as it
leaves an import's: that phone's kernel (6.6) has no PAGEMAP_SCAN, and userfaultfd can't protect a dma-buf mapping.
Off Android plat_gpu_buffer makes a memfd, which the driver imports as host memory: vulkan_headless's
check_uma_migrate runs the migrate way where the driver takes a memfd's pages (lavapipe) and skips where it doesn't
(amdgpu's user pointers take anonymous memory only), and memory_alias moves guest memory into memfds and back.
Made from docs/layers/gpu/ownership.md at 69237645142c. GPL-3.0-or-later.