From ea4bbb5fa3c5baa0679b4483603de795b3f75b5f Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 11 Nov 2026 20:18:03 -0800 Subject: [PATCH] A guest RAM chunk waits while a live import of an object's own holds its pages reims's pin 42 walks on the RTX 2080 counted imports_inside_live = 1 at boot, once in each of two walks, in the second the device's thread imported its first chunks ahead (with a 2 MiB pointer import and host_imports_kept_of_heap in the same second). A no-copy heap made over the RAM's pages before use_host_ram (or with no run resolver) imports its own pages outside the RAM's routing, and the thread's import of the chunk holding them then imported those pages a second time, which VK_EXT_external_memory_host leaves undefined (issue 6). Now RamOnDevice::chunk holds the chunk back while HostAliases::within finds a live import over its span (RamOnDevice::held_back): no import, the device's thread marks it tried, an object over it falls back to the paths of memory no RAM holds (its own import, inside the live import, or a copy), and copy_take sends copies over it through the CPU. Once every such import over the chunk went, the next object over it imports it. Counted: WorkCounts::host_ram_chunks_held_back (api_signatures updated). Docs: host_ram.rs's module doc no longer describes pointer chunks' runs imported alone (the 2073 fix made those dma-buf-only); it states the held-back chunk. host_aliases.rs, churn_counts.rs's imports_inside_live and HOWTO's guest RAM paragraph say the same. Test: host_ram_device::a_chunk_a_live_import_made_before_the_use_holds_waits_for_it_to_go (a heap over the first chunk made before the use; the thread and a buffer's take hold the chunk back, the buffer imports its own pages and reads the RAM's bytes, no import inside a live one; once both went, the next buffer is bound in the chunk). --- .../metal2vulkan-runtime/src/churn_counts.rs | 4 +- .../metal2vulkan-runtime/src/host_aliases.rs | 9 +- crates/metal2vulkan-runtime/src/host_ram.rs | 86 +++++++++----- .../src/tests/host_ram_device.rs | 109 +++++++++++++++++- .../metal2vulkan-runtime/src/work_counts.rs | 17 +++ crates/metal2vulkan/tests/api_signatures.tsv | 1 + docs/HOWTO.md | 6 +- 7 files changed, 196 insertions(+), 36 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/churn_counts.rs b/crates/metal2vulkan-runtime/src/churn_counts.rs index 49f9be469..7e1b600b9 100644 --- a/crates/metal2vulkan-runtime/src/churn_counts.rs +++ b/crates/metal2vulkan-runtime/src/churn_counts.rs @@ -418,7 +418,9 @@ pub struct ObjectChurn { /// (`host_aliases.rs`, "One page, one import"): a no-copy buffer's or heap's own import (a /// heap's own buffer's among them) over a live import of another no-copy buffer or heap, or /// over a live import of a guest RAM's chunk (`host_ram.rs`), and a chunk's import over a - /// live import of a no-copy buffer or heap. A page imported twice, which + /// live import of a no-copy buffer or heap made as the chunk's was being made (a chunk such an + /// import held pages of before is not imported, `WorkCounts::host_ram_chunks_held_back`). A + /// page imported twice, which /// `VK_EXT_external_memory_host` leaves undefined for a host pointer; every other buffer /// over pages a live import holds lies inside it (`buffers_bound_in_import`, or a chunk's /// `WorkCounts::chunk_bound_buffers_made`) or is a copy diff --git a/crates/metal2vulkan-runtime/src/host_aliases.rs b/crates/metal2vulkan-runtime/src/host_aliases.rs index 36a041a0a..1d0b7f444 100644 --- a/crates/metal2vulkan-runtime/src/host_aliases.rs +++ b/crates/metal2vulkan-runtime/src/host_aliases.rs @@ -55,10 +55,11 @@ //! residency release, `host_residency.rs`). A buffer or heap whose pages a live import holds //! some of, and not all, and a heap whose pages a live import holds more of than its own bytes, //! are a copy of the pages instead (`copied.rs`, `WorkCounts::copied_over_live_imports`). -//! `WorkCounts::object_churn.imports_inside_live` counts the imports made where one held some of -//! the pages all the same (two buffers made at once over them), a guest RAM's chunk's among -//! them (`host_ram.rs`, where a run is never imported alone by its pointer while its chunk's -//! import is made). +//! A guest RAM's chunk is not imported while a live import of an object's own holds some of its +//! pages, and a run is never imported alone by its pointer while its chunk's import is made +//! (`host_ram.rs`). `WorkCounts::object_churn.imports_inside_live` counts the imports made where +//! one held some of the pages all the same (two buffers made at once over them, or an object's +//! own import made as a chunk's over its pages was being made), a chunk's among them. //! - **Ownership stays the buffer's.** The added uses are hazard tracking's alone: what a command //! holds and lists (`EncoderCore::touch`, rebind holds, `used_resources`) is named by the //! command's own uses, each buffer by its own key. diff --git a/crates/metal2vulkan-runtime/src/host_ram.rs b/crates/metal2vulkan-runtime/src/host_ram.rs index ca94990e1..9d5097faa 100644 --- a/crates/metal2vulkan-runtime/src/host_ram.rs +++ b/crates/metal2vulkan-runtime/src/host_ram.rs @@ -82,34 +82,36 @@ //! ([`RamOnDevice::ask_for_copy`], `copy.rs`), wherever it lies: so a copy over scattered //! chunks, which moves the window to each in turn, finds them all imported by its next turn. //! Counted: `WorkCounts::host_ram_chunks_imported_ahead`. -//! - **No object waits for a chunk.** An object over a chunk not imported yet (outside the -//! window, or the one the thread is importing, [`Import::Making`]) is bound in an import of its -//! own run alone, made on the thread making the object, which faults in and pins the run's -//! pages and no more ([`RamOnDevice::for_object`]), while the device's thread imports the chunk, -//! first of its window since that take (or as the object asked, [`Plan::listed`]); the objects -//! over the chunk after that are bound in the chunk's import. A buffer's (not a heap's) run -//! import is released while idle, its address never handed out, and the buffer bound in the -//! chunk's import at its next use where that is made by then, else in its run's alone again -//! (`host_residency.rs`). The run's import and the chunk's are two imports of the same pages, as an object's -//! own import of a view of pages a chunk holds is; the object's uses order against those of -//! every other object over its bytes, and of every copy of them through the chunk's import -//! (`copy.rs`), by their host addresses, whichever import each is bound in -//! (`host_aliases.rs`). The run's import is one more allocation, made only where the -//! device's allocations leave the margin the thread leaves (below), and goes with its last -//! holder. That is the trade for no making waiting: up to one more allocation and one more pin -//! of its pages for each object made while its chunk is not imported, for as long as the -//! object lives. A burst of heaps that outruns the thread takes one for each heap over a chunk -//! the thread has not imported yet: 128 of the 256 2 MiB heaps of the upward order of -//! `tests/host_ram_window_bench.rs` on an RTX 5080 laptop, every heap of its first burst of -//! four chunks. Counted: `WorkCounts::run_bound_buffers_made` (each such object), -//! `run_imports_live` (those live). Where the device's thread does not run (never started, or -//! ended at the use's end or the allocation limit's margin) or failed the chunk, where the -//! device's allocations are within that margin, where the device's barriers do not order blits -//! through two imports of the same pages (`RuntimeDriverDefect::AliasedImportBlitsUnordered`, -//! MoltenVK's and ANV's: there every object over a chunk's pages lies in the chunk's one import, -//! whose blits the barriers order), where the run is the whole chunk, or where the run's import -//! fails, the object imports the chunk on its own thread, or waits for the import of it under -//! way. +//! - **An object over a chunk not imported yet** (outside the window, or the one the thread is +//! importing, [`Import::Making`]). Where the device's plan is [`Plan::listed`], its chunks and +//! runs each a dma-buf of the RAM's source, no object waits for a chunk: the object is bound in +//! an import of its own run alone, made on the thread making the object, which pins the run's +//! pages and no more ([`RamOnDevice::for_object`]), while the device's thread imports the chunk +//! as the object asked; the objects over the chunk after that are bound in the chunk's import. +//! A buffer's (not a heap's) run import is released while idle, its address never handed out, +//! and the buffer bound in the chunk's import at its next use where that is made by then, else +//! in its run's alone again (`host_residency.rs`). The run's import and the chunk's are two +//! dma-bufs of the same pages; the object's uses order against those of every other object over +//! its bytes, and of every copy of them through the chunk's import (`copy.rs`), by their host +//! addresses, whichever import each is bound in (`host_aliases.rs`). The run's import is one +//! more allocation, made only where the device's allocations leave the margin the thread leaves +//! (below), and goes with its last holder. Counted: `WorkCounts::run_bound_buffers_made` (each +//! such object), `run_imports_live` (those live). Elsewhere a chunk's import is of its host +//! pointer, and an import of a run's pointer would import its pages a second time, which +//! `VK_EXT_external_memory_host` leaves undefined (its issue 6). There, and where the device's +//! thread does not run (never started, or ended at the use's end or the allocation limit's +//! margin) or failed the chunk, where the device's allocations are within that margin, where +//! the device's barriers do not order blits through two imports of the same pages +//! (`RuntimeDriverDefect::AliasedImportBlitsUnordered`, MoltenVK's and ANV's), where the run is +//! the whole chunk, or where the run's import fails, the object imports the chunk on its own +//! thread, or waits for the import of it under way: every object over a chunk's pages lies in +//! its one import. +//! - **One page, one import.** A chunk some of whose pages a live import of a no-copy buffer's +//! or heap's own holds, at the RAM's own addresses (one made outside the RAM's routing: before +//! the device's use of the RAM began, or with no run resolver), is not imported while any such +//! import lives ([`RamOnDevice::held_back`]): the objects over it meanwhile take their own +//! paths, as over memory no RAM holds, and the copies over it go through the CPU +//! ([`RamOnDevice::copy_take`]). Counted: `WorkCounts::host_ram_chunks_held_back`. //! - **Paced.** The thread runs as the system's batch work (never at a weight the system may //! starve: it holds a chunk's lock and, pinning, the process's memory map lock), yields between //! chunks, and on Linux faults each chunk's pages in 2 MiB at a time before its import, so that @@ -925,6 +927,11 @@ impl RamOnDevice { Import::None => break, } } + if self.held_back(device, importer, slot) { + return Err(crate::error::internal( + "a guest RAM chunk a live import of an object's own holds pages of", + )); + } *import = Import::Making; drop(import); let making = Making { @@ -1018,6 +1025,27 @@ impl RamOnDevice { self.plan.listed } + /// Whether chunk `slot` waits to be imported, as a live import of a no-copy buffer's or + /// heap's own pages holds some of its pages, at the RAM's own addresses: one made outside the + /// RAM's routing, before the device's use of the RAM began or with no run resolver + /// (`Device::route_runs`). `VK_EXT_external_memory_host` leaves a page imported twice + /// undefined (its issue 6), so the chunk is not imported while any such import lives + /// (`host_aliases.rs`): the objects over it take their own paths meanwhile, as over memory no + /// RAM holds, and the copies over it go through the CPU ([`RamOnDevice::copy_take`]). + /// Counted: `WorkCounts::host_ram_chunks_held_back`. + fn held_back(&self, device: &Device, importer: &Importer, slot: usize) -> bool { + let span = &self.slots[slot].span; + let bytes = (span.start, span.end - span.start); + let held = !matches!( + device.shared().aliases.within(bytes, importer.unit()), + Within::Free + ); + if held { + device.shared().counts.host_ram_chunk_held_back(); + } + held + } + /// Whether a chunk's import, made by now, holds any of the RAM's host bytes `runs`. fn chunks_hold_any(&self, runs: &[Range]) -> bool { (self.slots.iter()).any(|slot| { @@ -1175,7 +1203,7 @@ impl RamOnDevice { if self.made(slot).is_some() { return CopyTake::Now; } - if span.refused { + if span.refused || self.held_back(device, importer, slot) { return CopyTake::ThroughCpu; } let running = self.wake.running.load(Ordering::Relaxed) diff --git a/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs b/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs index a50b99948..75b58fa29 100644 --- a/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs @@ -52,7 +52,11 @@ //! two encoders of one command buffer and across queues; so do a buffer in the chunk's import //! and a copy of its pages (`copy_from_host_ram` after a fill through the buffer, //! `copy_to_host_ram` before a copy out of it). Within the allocation limit's margin the object -//! imports its chunk too. +//! imports its chunk too. A heap over the RAM's pages made before the use imports its own +//! pages: the chunk that holds them is held back while that import lives, neither the device's +//! thread nor an object over its other pages importing it (the object imports its own and reads +//! the RAM's bytes), and imported by the first object over it once that import and the +//! object's went. //! //! - **Shared RAM a device refuses.** On a device that refuses pointers to shared memory (RADV), //! a RAM of a `memfd` mapped shared with no dma-buf source imports no chunk, and an object over @@ -67,6 +71,7 @@ //! of device the cfg selects. A device that imports no host memory says so and passes. use super::dispatch_pending::state; +use crate::host_aliases::Within; use crate::host_ram::AHEAD_CHUNKS; use crate::{ Buffer, CommandBufferStatus, Degradation, Device, DeviceKind, DeviceOptions, Error, EventRef, @@ -2021,6 +2026,108 @@ fn a_no_copy_heap_over_one_run_is_bound_in_the_chunk() { assert!(settles(&dropped)); } +/// A heap over a RAM's pages made before the device's use of the RAM began imports its own pages, +/// outside the RAM's routing: the chunk that holds them is not imported while that import lives +/// (one page, one import: `RamOnDevice::held_back`), neither by the device's thread nor for an +/// object over the chunk's other pages, which imports its own and reads the RAM's bytes. Once the +/// heap and that object went, the next object over the chunk imports it and is bound there. No +/// import is made over pages a live one holds (`imports_inside_live`). +#[test] +fn a_chunk_a_live_import_made_before_the_use_holds_waits_for_it_to_go() { + let Some(device) = ahead_device() else { + return; + }; + let (ram, start, dropped) = fresh_ram(&device, 2 * 64 * MIB); + let page = device.shared().host_import.as_ref().unwrap().page; + // The heap: 2 MiB of the RAM's first chunk, made before the use, as a guest's early heap is. + const HEAP: u64 = 2 * MIB; + let heap_at = start + 4 * MIB; + // SAFETY: the RAM's bytes, mapped until its keep-alive goes, after every device let go. + let memory = unsafe { HostMemory::new(NonNull::new(heap_at as *mut u8).unwrap()) }; + let descriptor = HeapDescriptor::new(HeapType::Placement, HEAP, ResourceOptions::default()); + // SAFETY: as above. + let heap = + unsafe { device.new_heap_with_bytes_no_copy(memory, &descriptor, None, None) }.unwrap(); + let before = device.work_counts(); + let inside = |counts: &crate::WorkCounts| { + counts.object_churn.imports_inside_live - before.object_churn.imports_inside_live + }; + let held = |counts: &crate::WorkCounts| { + counts.host_ram_chunks_held_back - before.host_ram_chunks_held_back + }; + let used = device.use_host_ram(&ram).unwrap(); + // The device's thread asks for the RAM's first chunk, and holds it back. + let asked = settled(&device, |counts| held(counts) == 1); + assert_eq!( + (held(&asked), inside(&asked)), + (1, 0), + "the thread held the chunk back" + ); + assert_eq!( + asked.host_ram_chunks_imported_ahead, before.host_ram_chunks_imported_ahead, + "nothing imported ahead before any take" + ); + // A buffer over the chunk's other pages, through the resolver: an import of its own. + let (runs, gates): (Arc>, Arc>) = Default::default(); + let at = start + 32 * MIB; + let over = |at: u64| { + // SAFETY: as above. + let memory = unsafe { HostMemory::new(NonNull::new(at as *mut u8).unwrap()) } + .with_runs(resolver(&runs, itself)) + .with_import_gate(gate(&gates, true)); + // SAFETY: as above. + unsafe { + device.new_buffer_with_bytes_no_copy(memory, 4 * page, ResourceOptions::default(), None) + } + .unwrap() + }; + fill_pattern(at..at + 4 * page); + let own = over(at); + let made = device.work_counts(); + assert_eq!( + ( + made.chunk_bound_buffers_made - before.chunk_bound_buffers_made, + made.run_bound_buffers_made - before.run_bound_buffers_made, + own.host_backing(), + inside(&made), + ), + (0, 0, Some(HostBacking::ImportedByPointer), 0), + "the buffer imported its own pages" + ); + assert!( + held(&made) >= 2, + "the buffer's take held the chunk back too" + ); + assert_eq!(gpu_bytes(&device, &own, 4 * page), bytes(at..at + 4 * page)); + // Once the heap and the buffer went (their imports freed as their command buffer's holds + // go), the next buffer over the chunk imports it. + drop((own, heap)); + let unit = device.shared().host_import.as_ref().unwrap().unit(); + let chunk = (start, 64 * MIB); + let started = Instant::now(); + while !matches!(device.shared().aliases.within(chunk, unit), Within::Free) { + assert!( + started.elapsed() < WAIT, + "the heap's and the buffer's imports never went" + ); + std::thread::sleep(Duration::from_millis(2)); + } + let bound = over(at); + let after = device.work_counts(); + assert_eq!( + after.chunk_bound_buffers_made - made.chunk_bound_buffers_made, + 1, + "the buffer is bound in the chunk" + ); + assert_eq!(inside(&after), 0, "no import over a live one's pages"); + assert_eq!( + gpu_bytes(&device, &bound, 4 * page), + bytes(at..at + 4 * page) + ); + drop((bound, used, ram)); + assert!(settles(&dropped)); +} + #[test] fn other_answers_take_the_gates_path_and_broken_ones_are_refused() { let Some(device) = device() else { diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index a411f2ecc..2d303b202 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -412,6 +412,7 @@ pub(crate) struct Counters { host_ram_pointer_imports_failed: AtomicU64, host_ram_dma_buf_imports_failed: AtomicU64, host_ram_chunks_imported_ahead: AtomicU64, + host_ram_chunks_held_back: AtomicU64, host_ram_chunks_faulted_ahead: AtomicU64, host_ram_chunks_given_back: AtomicU64, host_ram_chunks_released: AtomicU64, @@ -3492,6 +3493,12 @@ impl Counters { (self.host_ram_chunks_imported_ahead).fetch_add(1, Ordering::Relaxed); } + /// A chunk of a guest RAM was not imported, as a live import of an object's own held some of + /// its pages (`host_ram.rs`). + pub(crate) fn host_ram_chunk_held_back(&self) { + (self.host_ram_chunks_held_back).fetch_add(1, Ordering::Relaxed); + } + /// The device's thread faulted a chunk of a guest RAM in before its import (`host_ram.rs`). pub(crate) fn host_ram_chunk_faulted_ahead(&self) { (self.host_ram_chunks_faulted_ahead).fetch_add(1, Ordering::Relaxed); @@ -7430,6 +7437,15 @@ pub struct WorkCounts { /// an object last took. The thread imports while the device's allocations leave a quarter of /// its `maxMemoryAllocationCount`, and stops there. pub host_ram_chunks_imported_ahead: u64, + /// The times a chunk of guest RAM was not imported because a live import of a no-copy + /// buffer's or heap's own pages held some of its pages: one made outside the RAM's routing + /// (before the device's use of the RAM began, or with no run resolver). A page a live import + /// holds is never imported again while it lives (`VK_EXT_external_memory_host`'s issue 6), so + /// the chunk waits for every such import over it to go: the objects over it meanwhile take + /// their own paths, as over memory no RAM holds, and the copies over it go through the CPU. + /// Each ask of the chunk while it waits counts once: the device's thread's, an object's, a + /// copy's. + pub host_ram_chunks_held_back: u64, /// The chunks of guest RAM the device's thread faulted in, on Linux, before it imported them /// ahead of the objects: none of a chunk the device refuses whatever its pages hold (shared /// memory on a device that imports no pointer to it, the RAM given no source of dma-bufs: @@ -9370,6 +9386,7 @@ impl Device { host_ram_chunks_imported_ahead: counters .host_ram_chunks_imported_ahead .load(Ordering::Relaxed), + host_ram_chunks_held_back: counters.host_ram_chunks_held_back.load(Ordering::Relaxed), host_ram_chunks_faulted_ahead: counters .host_ram_chunks_faulted_ahead .load(Ordering::Relaxed), diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 2c73adfcf..d36844d10 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2921,6 +2921,7 @@ field metal2vulkan-runtime::WorkCounts.host_ram_chunk_bytes pub host_ram_chunk_b field metal2vulkan-runtime::WorkCounts.host_ram_chunk_import_nanos pub host_ram_chunk_import_nanos: u64 field metal2vulkan-runtime::WorkCounts.host_ram_chunks_faulted_ahead pub host_ram_chunks_faulted_ahead: u64 field metal2vulkan-runtime::WorkCounts.host_ram_chunks_given_back pub host_ram_chunks_given_back: u64 +field metal2vulkan-runtime::WorkCounts.host_ram_chunks_held_back pub host_ram_chunks_held_back: u64 field metal2vulkan-runtime::WorkCounts.host_ram_chunks_imported pub host_ram_chunks_imported: u64 field metal2vulkan-runtime::WorkCounts.host_ram_chunks_imported_ahead pub host_ram_chunks_imported_ahead: u64 field metal2vulkan-runtime::WorkCounts.host_ram_chunks_live pub host_ram_chunks_live: u64 diff --git a/docs/HOWTO.md b/docs/HOWTO.md index 989ede969..b07abf97d 100644 --- a/docs/HOWTO.md +++ b/docs/HOWTO.md @@ -680,7 +680,11 @@ submission, can give the device the guest's RAM once instead: and `VK_EXT_external_memory_host` leaves the same host allocation imported twice into a device undefined (its issue 6), so no object's run, nor a copy's range, is imported by its own pointer beside the chunk's (`ObjectChurn::imports_inside_live` counts an import made over host - addresses a live import holds). The price is a making that waits for its chunk's import where + addresses a live import holds). Nor is a chunk imported over a live import of an object's own + made outside the RAM's routing (a no-copy heap over the RAM's pages made before + `use_host_ram`, or one with no run resolver): the chunk waits until every such import over its + pages went, the objects over it meanwhile take the paths of memory no RAM holds and the copies + over it go through the CPU (`WorkCounts::host_ram_chunks_held_back`). The price is a making that waits for its chunk's import where the window has not reached the chunk (the window bench below). Only a device whose chunks are dma-bufs (`HostRam::with_dma_buf`, below) imports an object's run alone while its chunk's import is made, as one more dma-buf. The thread faults in no more than the window past the objects, and never goes back for