No-copy buffers and host imports now cost the kernel and the encoding thread less, and their
deallocators run in the call Metal runs them in, including when a detach races a commit.
What changes:
- Idle pointer imports. A no-copy buffer's own pointer import is releasable, as its dma-buf
import was: while only blits read it and no pending command buffer holds it, the residency
thread releases it after its idle window (2 s, doubling at each import again up to 64 s), and the
next blit imports it again at the same pointer. Only where the new measured fact
DriverFact::PointerImportsFollowTheirAddress holds (ANV 26.2.4 on the Iris Xe; lavapipe 26.0.8
to 26.2.4): there a pointer import reads the pages mapped at its address at each submission, so
an import again reads what the kept one would. A refused re-import fails the blit's call, typed.
- Deallocator timing. A released import hands its keep-alive and deallocators to the objects a
command buffer still holds, so they run as that memory is freed (or parked and announced, as a
kept import's). A committed but unsubmitted command buffer holds a blit's import objects weakly,
so a rebind or detach before its first submission runs the deallocator in its call, as Metal
does. A detach racing a commit on another thread runs the deallocator in one of the two calls:
a let-go mark on the pages and the commit's hold count, both sequentially consistent, make each
side see the other, and a commit that sees the mark replays its own command buffer at once. The
live list re-reads a buffer's pages where a detach moved them meanwhile. HostDeallocatorDelayed
is no longer announced for these races.
- Fewer kernel queries. A no-copy buffer asks the kernel about its mappings (PROCMAP_QUERY) only
where it makes its own import; a rebind onto a live buffer's bytes lies in that buffer's import
and asks nothing; /proc/self/maps stays open for the queries (reopened in a forked process).
Three blit paths that read the caller's buffer as a read apart now read it as blits, so their
imports stay releasable.
- Fewer rewrites and lookups. A rebind or detach rewrites only the heap slots of textures over the
bytes it moved; host-page alias lookups visit only the length classes that hold ranges and keep
each range's overlapping ranges; a binding and a descriptor each read where a no-copy buffer lies
once.
- Counters (WorkCounts): host_imports_used and host_imports_read_apart (with its six reasons),
host_imports_kept_for_fds and kept imports by cause (of_heap, rebound, pointer, no_stand_in),
heap_slots_rewritten, host_mappings_queried (Linux; 0 elsewhere), host_range_lookups,
host_remaps_reported, host_remaps_over_imports, host_imports_remapped, host_pointer_imports_made
and _bytes_made, no_copy_lies_read, host_imports_rebound_unlisted, host_rebind_imports_made and
_covered.
- Docs: docs/HOWTO.md on both kinds of release, the RLIMIT_NOFILE soft limit a program raises
itself and the new gauges; a FAST-PATHS known gap that a buffer made before a remap reads the new
pages on drivers whose pointer imports follow their address, where Metal keeps the old ones
(HostMemory::new's contract forbids such a remap); the rustdoc of host_range_remapped.
Why: ANV over i915 lists every VkDeviceMemory in every execbuffer, and a pointer import costs about
0.19 us an execbuffer; the consumer's guest keeps hundreds of no-copy buffers live and uses few in
a frame. Its swap thread spent 1.54 ms of kernel time a frame querying mappings of a buffer that
joined a live import. And a released import's deallocator unmapped pages under a live userptr,
losing the device.
Verified:
- Iris Xe, a guest-shaped lane of 520 pointer imports and 43 command buffers a frame, the idle
ones released: objects per execbuffer 536 -> 51, ioctl 4.24-4.31 -> 0.85-1.09 ms a frame,
scheduler CPU 4.96-5.05 -> 1.33-1.74 ms a frame, frame p50 5.27-5.43 -> 2.70-3.11 ms.
- The fact, probed outside the runtime on the Iris Xe and through tests::pointer_residency on it
and lavapipe: a kept import read the pages a remap put at its address.
- The detach race on the Iris Xe and RTX 5080 under the validation layer: without the fix 3 of 3
runs failed (deallocators late or on another thread); with it 12 of 12 runs, 15,360 races, each
deallocator in the detach or the commit, never late.
- A second no-copy buffer over 2040 scattered mappings: 415-418 us -> 0.3-0.6 us. A rebind with
1024 other live textures: 4766 -> 49 us. Alias lookups: a draw -5.3% instructions on host-page
frames (RX 7900 XTX).
- Device and unit tests: freed_memory, pointer_residency, host_residency, blit_holds,
encoder_holds, host_import_counts, host_aliases (up to 45 passed per run); lanes rebinds,
rebind_reach, detach, detach_textures, host_memory, no_copy_heaps, dma_buf_imports, draw_work
(74 passed on lavapipe, 30 on the Iris Xe for the deallocator fixes). clippy -D warnings (plain,
m2v_gpu, m2v_cpu_icd) and rustdoc.