Files
metal2vulkan/docs
Anees Iqbal 442a9fd538 Bind no-copy objects over one run of guest RAM in the device's chunk import
The NVIDIA chunk route's API (HostRam, Device::use_host_ram, HostMemory::with_runs), the chunk
source and one-run route, and a chunk-bound buffer's out-of-bounds writes held to the device's
own containment (a device test and a known gap).

One work packet, landed as 7 commits on archive/development-linear-2026-10-29, in order:

* Add HostRam, Device::use_host_ram and HostMemory::with_runs (d3617f7e1d)

The API of the NVIDIA chunk route (guest RAM imported whole): the types the
chunk source, the GPU copy and the rebind lanes build on.

- HostRam (unsafe new(ranges, keep_alive)): the consumer's guest RAM ranges,
  checked as non-empty, whole host pages, within the address space and apart;
  a typed refusal otherwise (Invalid, Problem::HostRam). with_dirty_pages
  takes one DirtyPages log for the whole RAM whose offsets are host addresses
  (the log's pointer is 0), so one stamp per commit can serve every copy. The
  keep-alive is dropped with the HostRam (forgotten while the process exits).
- Device::use_host_ram(&Arc<HostRam>) -> HostRamUse: a guard, since runtime
  devices live for the process while a lease promises its RAM for one guest
  device's life. Dropping the guard ends the use; a later RAM may be used
  after. Several RAMs whose ranges are apart may be used at once; one whose
  ranges meet a used RAM's (itself among them) is refused as misuse
  (Invalid, Problem::HostRam). A device without VK_EXT_external_memory_host
  is refused (Unsupported, naming it).
- HostMemory::with_runs(RunResolver): RunResolver is
  Arc<dyn Fn(start, len) -> Option<Vec<Range<u64>>> + Send + Sync>, asked with
  the import window, answering host addresses in the RAM's ranges. Stored
  here; the routing that calls it comes in a later commit.

No Metal counterpart for any of it; the doc comments say so. The chunk import
itself (lazy 64 MiB chunks) and the routing in buffer_over_host_memory follow.

Verified: cargo clippy -p metal2vulkan-runtime --all-targets --all-features
-D warnings (Mac); host_ram unit tests 4/4 (range checks, refusals,
keep-alive drop); api_signatures and refusal_surface 7/7 after the re-bless
and regeneration; rustdoc -D warnings for the runtime crate.

* Re-pin the runtime sources at the landing tip (22c47609a9)

* Bind no-copy objects over one run of guest RAM in the device's chunk import (7f07d19b3d)

What: the chunk source and the one-run route (host_ram.rs).
- Chunk source: a device that uses a HostRam imports it by host pointer in
  64 MiB chunks (CHUNK_BYTES), never across a range's end, lazily. Each chunk
  is imported the first time an object over it is made, on that thread, once,
  under a lock of its own (ChunkSlot). It goes through the existing
  Memory::import_host: a coherent type, and the device-address flag where
  bufferDeviceAddress is on. A whole-chunk VkBuffer is kept for the runtime's
  own copies only, never named by a Metal buffer, root entry or binding.
- Chunk lifetime: chunks are never released while idle (they are no
  HostImport, so host_residency never sees them). Once the HostRamUse is
  dropped, a chunk is freed when nothing holds it: Placement gains ram_chunk,
  so every buffer bound in a chunk holds it, and every command buffer that
  used one does too. The RAM, and so its keep-alive, goes after the free
  (release_then's after), dropped as the program's. Exiting leaves it all to
  the process's end.
- Routing in buffer_over_host_memory: a buffer that lies in a live import of
  the same bytes is unchanged. Otherwise, where the device uses a RAM and the
  memory has a resolver, the resolver is asked once with the import window, on
  the calling thread with no lock held, before the writability query and the
  gate. Memory whose GPU writes bit 24 drops is never routed.
  - One run inside one chunk: Device::bind_in_chunk makes the VkBuffer with
    host_buffer against the object's own window, checks it with binds_in
    against that window, and binds it at run offset + binds_in offset in the
    chunk's memory. That offset must be a multiple of the buffer's alignment,
    and the buffer must allow the chunk's memory type. Its placement
    (Placement::ram_chunk) holds the chunk and maps the caller's pointer, so
    contents stays the pointer. The buffer gets no import of its own, the gate
    is not asked, and buffer_of keeps the import fixed (no Again).
  - Several runs, a run crossing a chunk's end, a chunk that cannot be imported
    or a buffer that cannot bind exactly: today's gate path (import, or copy
    where the gate refuses).
  - The RAM and runs are kept on HostPages (ram_runs) until rebind or release,
    for the GPU copy lane.
  - An answer that is not whole pages of one used RAM totalling the window is
    refused: Invalid, Problem::HostRam.
- Heaps over host memory take the same routing (of_heap goes through
  buffer_over_host_memory).
- rebind_host_pages only ignores the new HostParts.runs (`runs: _`). The
  rebind lane asks the resolver there with Device::route_runs and
  bind_in_chunk.
- New WorkCounts: host_ram_chunks_imported, host_ram_chunk_bytes,
  host_ram_chunks_live (a gauge), chunk_bound_buffers_made.
- Docs: HOWTO "Guest RAM imported whole", the runtime README's extension list.
  RUNTIME_VERSION 1697 -> 1698.

Why: the NVIDIA route (the consumer's spec). On a device that refuses
dma-buf views, each import of scattered guest pages takes a kernel mapping per
page, past vm.max_map_count at 4K. One-run objects bound in chunks take none.

How verified (this Mac, M3 Max, MoltenVK, under memcap + maclock):
- host_ram unit tests 8/8: chunk spans and lookup; one run in a chunk, a
  chunk's last pages and a range's short last chunk; several runs and a run
  crossing a chunk (gate path); broken answers refused.
- New device tests tests::host_ram_device 6/6. The path ran: the log prints
  "chunks imported 1, chunk-bound buffers 2".
  - One run binds in the chunk: 1 chunk import of 64 MiB, 0 pointer imports,
    gate not asked, contents == pointer, the GPU reads the bytes, a fill
    reaches them, a second buffer in the chunk imports nothing.
  - Containment: a 1.5-page buffer's copy reach <= its 2-page window, and a
    fill of all of it leaves the sentinel page before and the bytes after it
    unchanged.
  - A no-copy heap binds in the chunk.
  - None, two runs and a crossing run ask the gate once and import; short and
    outside answers are refused.
  - A command buffer held behind an event keeps the chunk (live gauge, and
    the keep-alive not dropped) after the buffer, the use and the RAM are let
    go of; once it completes the chunk is freed and the keep-alive dropped.
  - The same RAM twice and an overlapping RAM are refused, a RAM apart is
    taken, and a new RAM over the same ranges is taken after the first use
    and imports again.
- Regression on the same Mac: 249/249 for
  host_|copied|dirty|rebind|heap|no_copy|dma_buf|in_place|detach|alias|
  degradation|profile (copied_device, dirty_pages_device and the
  host-import tests among them).
- clippy -D warnings plain and --cfg m2v_gpu (runtime, all targets, all
  features); rustdoc -D warnings for the runtime; api_signatures 3/3 after
  the re-bless.
Not run here: NVIDIA (msi 5080) and lavapipe; dma-buf devices' own tests
beyond the Mac's.

RUNTIME_VERSION 1699 at landing.

* Re-derive the generated documents at the landing tip (a9c042f142)

They are generated from the whole case set, and the set grew on `development`
while this branch was in flight, so the copy committed here counted a smaller
one.

Verified: regenerating at this tip leaves no change beyond the `Generated at`
stamp.

* Re-pin the runtime sources at the landing tip (2cfd8d6ef9)

* Hold a chunk-bound buffer's out-of-bounds kernel writes to the device's own containment (1b09d84fad)

What: a device test (tests::host_ram_device) whose kernel, the owned fixture's
scale_and_offset, writes three pages of words through a one-page buffer bound
in a chunk of guest RAM. It asserts that the page before the buffer and the
pages after it keep a sentinel, and that the kernel wrote the buffer itself.
The page-after assertion applies wherever the device keeps the same write,
through a one-page buffer of its own, out of the buffer made after it: the
chunk binding is held to the device's own containment, measured in the same
test. A known gap in docs/FAST-PATHS.md records the device that does not
contain such writes.

Why: containment (c) of the route. A chunk-bound VkBuffer ends at the
object's last page, but the chunk's memory goes on past it, so containment
rests on the device's buffer robustness or in-shader checks, not on the
import's end as a per-object import does.

Found and how verified:
- RTX 5080 laptop (NVIDIA 615.71.09), msi, one hold 06:00-06:02, strict form
  (no device check): host_ram_device 7/7. The chunk's pages after the buffer
  were untouched.
- lavapipe (linux-gate, slot lava, arm64), strict form: 7/7.
- M3 Max (MoltenVK 1.4.2), this form: 15/15 host_ram tests. It prints that
  out-of-bounds writes changed 32720 bytes of the device's own next buffer and
  32720 bytes of the chunk's next pages. A control run (not committed) showed
  that a no-copy pointer import of one page changed 0 bytes past its page. So
  MoltenVK's gap is within an allocation; it predates the route, which
  inherits it.
- clippy --cfg m2v_gpu -D warnings (runtime, all targets).

Also: the RunResolver doc no longer says rebinds ask it; they do not until rebinds are routed (af9f1e057d).

* Re-pin the runtime sources at the landing tip (60c300198f)
2026-10-29 15:43:53 +02:00
..