diff --git a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs index ecddbb4d9..c8918e947 100644 --- a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs +++ b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs @@ -915,6 +915,11 @@ impl Device { _ => None, }; let over_import = inside.is_none() && !matches!(within, Within::Free); + // Pages a guest RAM's live chunk import holds some of, at the RAM's own addresses, that + // no chunk binds the buffer in (its bytes cross a chunk's end, or no run resolver routed + // them): a copy too, never a second import of them (`host_ram.rs`). + let over_chunk = (in_chunk.is_none() && inside.is_none() && !drops_writes) + && self.ram_chunks_hold_any(window.start..window.start + window.size); let (buffer, memory, again) = match (in_chunk, inside) { // A buffer (not a heap) bound in an import of its own run alone, its chunk not // imported yet, is released while idle and bound in the chunk's import at its next @@ -959,11 +964,13 @@ impl Device { }); } // Pages a live import holds some of: a copy, the gate unasked and no page pinned. - if over_import { - // SAFETY: the caller's contract. - let copied = unsafe { - self.copied_buffer(copy, release, dirty, CopiedFor::OverImport, naming) + if over_import || over_chunk { + let why = match over_import { + true => CopiedFor::OverImport, + false => CopiedFor::OverRamChunk, }; + // SAFETY: the caller's contract. + let copied = unsafe { self.copied_buffer(copy, release, dirty, why, naming) }; return copied.map(|buffer| noted(kept(buffer, log, kept_runs, made_of))); } let refused_by_host = copyable @@ -1324,6 +1331,11 @@ impl Device { }, _ => None, }; + // And new pages a guest RAM's live chunk import holds some of at the RAM's own + // addresses, which no chunk binds (`host_ram.rs`), as at a buffer's making. + let over_chunk = + (in_chunk.is_none() && inside.is_none() && !moved && !drops_writes) + && self.ram_chunks_hold_any(window.start..window.start + window.size); let over_import = inside.is_none() && !matches!(within, Within::Free); let in_chunk = in_chunk.or(inside); let chunk_bound = in_chunk.is_some(); @@ -1346,6 +1358,7 @@ impl Device { && !moved && !chunk_bound && !over_import + && !over_chunk }); let refused_by_host = match gate { Some(gate) => { @@ -1370,8 +1383,12 @@ impl Device { // Only pages imported here are pinned, and so have their writability asked; pages // the process cannot write are a copy whose GPU writes are dropped // ([`unwritable_pages`]). - let imports = - !chunk_bound && !refused_by_host && !moved && !read_only_copy && !over_import; + let imports = !chunk_bound + && !refused_by_host + && !moved + && !read_only_copy + && !over_import + && !over_chunk; let unwritable = imports && unwritable_pages(&host, writable, &shared.counts)?; let imports = imports && !unwritable; // The heap's images bound to its bytes, and the images in place over the bytes, @@ -1517,6 +1534,7 @@ impl Device { (false, None) if read_only_copy => CopiedBecause::ReadOnly, (false, None) if unwritable => CopiedBecause::Unwritable, (false, None) if over_import => CopiedBecause::OverImport, + (false, None) if over_chunk => CopiedBecause::OverRamChunk, (false, None) => CopiedBecause::Refused, }; let (host, placement, copied) = copy(failed.as_ref())?; @@ -2164,6 +2182,12 @@ impl Device { their pages and not all (or more than a heap's own bytes), and \ VK_EXT_external_memory_host leaves pages imported twice undefined" } + CopiedBecause::OverRamChunk => { + "a live import of a guest RAM's chunk holds some of their pages at the \ + RAM's own addresses, and no chunk binds them (they cross a chunk's end, or \ + no run resolver routed them), and VK_EXT_external_memory_host leaves pages \ + imported twice undefined" + } }; let dropped = match (drops, cause) { (true, CopiedBecause::ReadOnly | CopiedBecause::Unwritable) => { @@ -2258,6 +2282,7 @@ impl Device { CopiedFor::ReadOnly => (CopiedBecause::ReadOnly, None), CopiedFor::Unwritable => (CopiedBecause::Unwritable, None), CopiedFor::OverImport => (CopiedBecause::OverImport, None), + CopiedFor::OverRamChunk => (CopiedBecause::OverRamChunk, None), }; let shared = self.shared(); let raw_device = shared.vk(); @@ -2676,6 +2701,10 @@ enum CopiedFor { /// than a heap's own bytes (`host_aliases.rs`, "One page, one import"): no page is imported /// twice. OverImport, + /// A live import of a guest RAM's chunk holds some of them, at the RAM's own addresses, and no + /// chunk binds the buffer: its bytes cross a chunk's end (the chunks on both sides imported), + /// or no run resolver routed them (`host_ram.rs`). No page is imported twice. + OverRamChunk, } /// An import of a no-copy object's pages that the device and the gate admitted and that failed diff --git a/crates/metal2vulkan-runtime/src/host_ram.rs b/crates/metal2vulkan-runtime/src/host_ram.rs index 9d5097faa..6bb5340f4 100644 --- a/crates/metal2vulkan-runtime/src/host_ram.rs +++ b/crates/metal2vulkan-runtime/src/host_ram.rs @@ -1975,7 +1975,9 @@ pub(crate) enum Routed { start: u64, }, /// More than one run, or one crossing a chunk's end: the object takes the gate's path, the - /// runs kept with it. + /// runs kept with it, or is a copy where a live chunk import holds some of its pages at the + /// RAM's own addresses (`CopiedBecause::OverRamChunk`): one crossing the end of a chunk + /// imported on both sides. Runs { ram: Arc, runs: Vec>, diff --git a/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs b/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs index 75b58fa29..dcbef0f5b 100644 --- a/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs @@ -2128,6 +2128,95 @@ fn a_chunk_a_live_import_made_before_the_use_holds_waits_for_it_to_go() { assert!(settles(&dropped)); } +/// A no-copy heap whose bytes cross the end of a chunk, the chunks on both sides imported (the +/// consumer's 2 MiB heap from 0x3f22000 into a 64 MiB chunk): no chunk binds it, and an import of +/// its own would import pages both chunks' imports hold, so it is a copy of its pages +/// (`CopiedBecause::OverRamChunk`), the gate not asked, whose bytes go from the CPU to the GPU and +/// back across the chunks' end. Nothing is imported over pages a live import holds +/// (`imports_inside_live`). +#[test] +fn a_heap_across_two_imported_chunks_is_a_copy_and_imports_nothing() { + let Some(device) = device() else { + return; + }; + let (ram, start, dropped) = new_ram(); + let used = device.use_host_ram(&ram).unwrap(); + let page = device.shared().host_import.as_ref().unwrap().page; + let (runs, gates): (Arc>, Arc>) = Default::default(); + let memory_at = |at: u64| { + // SAFETY: the RAM's bytes, mapped until its keep-alive goes, after every device let go. + unsafe { HostMemory::new(NonNull::new(at as *mut u8).unwrap()) } + .with_runs(resolver(&runs, itself)) + .with_import_gate(gate(&gates, true)) + }; + let start_counts = device.work_counts(); + // A buffer in each chunk imports it. + let in_chunk = |at: u64| { + // SAFETY: as above. + unsafe { + device.new_buffer_with_bytes_no_copy( + memory_at(at), + 4 * page, + ResourceOptions::default(), + None, + ) + } + .unwrap() + }; + let chunks = ( + in_chunk(start + 16 * page), + in_chunk(start + 64 * MIB + 16 * page), + ); + let before = device.work_counts(); + assert_eq!( + before.chunk_bound_buffers_made - start_counts.chunk_bound_buffers_made, + 2, + "both chunks imported" + ); + // The heap: 2 MiB from 0xde000 (in whole pages) before the first chunk's end. + const HEAP: u64 = 2 * MIB; + let before_end = 0xde000 / page * page; + let at = start + 64 * MIB - before_end; + fill_pattern(at..at + HEAP); + let descriptor = HeapDescriptor::new(HeapType::Placement, HEAP, ResourceOptions::default()); + // SAFETY: as above. + let heap = + unsafe { device.new_heap_with_bytes_no_copy(memory_at(at), &descriptor, None, None) } + .unwrap(); + let after = device.work_counts(); + assert_eq!( + ( + after.copied_over_live_imports - before.copied_over_live_imports, + after.host_pointer_imports_made - before.host_pointer_imports_made, + after.chunk_bound_buffers_made - before.chunk_bound_buffers_made, + after.object_churn.imports_inside_live - start_counts.object_churn.imports_inside_live, + ), + (1, 0, 0, 0), + "a copy, no import of its own, none over a live one's pages" + ); + assert!(gates.lock().unwrap().is_empty(), "the gate was not asked"); + // A buffer placed across the chunks' end reads the RAM's bytes there, and a fill through it + // reaches them. + let offset = before_end - 2 * page; + let placed = heap + .new_buffer_at_offset(4 * page, ResourceOptions::default(), offset) + .unwrap(); + let bytes_at = at + offset..at + offset + 4 * page; + assert_eq!( + gpu_bytes(&device, &placed, 4 * page), + bytes(bytes_at.clone()) + ); + blit(&device, |encoder| { + encoder.fill_buffer(&placed, 0..4 * page, 0xA5).unwrap(); + }); + assert!( + bytes(bytes_at).iter().all(|&byte| byte == 0xA5), + "the fill reached the pages on both sides of the chunks' end" + ); + drop((placed, heap, chunks, 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 2d303b202..e9431ab13 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -3389,7 +3389,9 @@ impl Counters { CopiedBecause::Moved => &self.copied_rebind_pages_moved, CopiedBecause::ReadOnly => &self.copied_read_only_memory, CopiedBecause::Unwritable => &self.copied_unwritable_pages, - CopiedBecause::OverImport => &self.copied_over_live_imports, + CopiedBecause::OverImport | CopiedBecause::OverRamChunk => { + &self.copied_over_live_imports + } } .fetch_add(1, Ordering::Relaxed); } @@ -7261,10 +7263,12 @@ pub struct WorkCounts { pub copied_unwritable_pages: u64, /// Of `copied_host_buffers_made`: no-copy buffers and heaps whose pages a live import of /// another held some of, and not all (a buffer's bytes across the edge of another's or a - /// heap's), or a heap whose pages one held more of than the heap's own bytes, made over a - /// copy so that no page is imported twice (`host_aliases.rs`, "One page, one import"): - /// `VK_EXT_external_memory_host` leaves a host allocation imported twice into a device - /// undefined. Their import is never tried, their gate never asked. + /// heap's), or a heap whose pages one held more of than the heap's own bytes, and those whose + /// pages a live import of a guest RAM's chunk held some of at the RAM's own addresses where no + /// chunk bound them (their bytes across a chunk's end, or routed by no run resolver), made + /// over a copy so that no page is imported twice (`host_aliases.rs`, "One page, one import"; + /// `host_ram.rs`): `VK_EXT_external_memory_host` leaves a host allocation imported twice into + /// a device undefined. Their import is never tried, their gate never asked. pub copied_over_live_imports: u64, /// Of the copies made, those a rebind gave a buffer as its new pages /// (`Buffer::rebind_bytes_no_copy`), in `copied_host_buffers_made` and its kin too. @@ -9792,4 +9796,8 @@ pub(crate) enum CopiedBecause { /// A live import of another buffer or heap holds some of the pages and not all, or more than /// a heap's own bytes: a second import would pin them twice (`host_aliases.rs`). OverImport, + /// A live import of a guest RAM's chunk holds some of the pages, at the RAM's own addresses, + /// and no chunk binds the buffer (its bytes cross a chunk's end, or no run resolver routed + /// them): a second import would pin them twice (`host_ram.rs`). + OverRamChunk, } diff --git a/docs/API.md b/docs/API.md index f876844ef..ed8d83aaf 100644 --- a/docs/API.md +++ b/docs/API.md @@ -3364,7 +3364,10 @@ bound. pages and not all (across its edge, or around it), or a heap's pages and more, so that importing them would import a page twice, which `VK_EXT_external_memory_host` leaves undefined (`copied_over_live_imports`, the gate not asked; a buffer whose pages a live import holds all of - lies inside that import instead, `ObjectChurn::buffers_bound_in_import`). The gate is asked + lies inside that import instead, `ObjectChurn::buffers_bound_in_import`); or a live import of a + guest RAM's chunk holds some of the pages at the RAM's own addresses and no chunk binds them + (they cross a chunk's end with the chunks on both sides imported, or no run resolver routed + them; `copied_over_live_imports` too). The gate is asked before the pages' write permission, where the pointer would be imported. A copy of pages the process maps read-only drops the GPU's writes, as Metal drops them: each batch's completion takes them back out of the copy and writes nothing to the pages (`copied_dropped_bytes`; the detail says so). The detail names which cause it was; for a failed diff --git a/docs/HOWTO.md b/docs/HOWTO.md index b07abf97d..77de4f07f 100644 --- a/docs/HOWTO.md +++ b/docs/HOWTO.md @@ -827,7 +827,11 @@ submission, can give the device the guest's RAM once instead: `length` their total. A source that fails, or answers a dma-buf of another length, leaves the copy to the chunk. - Any other answer (several runs, one run crossing a chunk's end, `None`) takes the paths above: - the gate, then the import or the copy. The runs are kept with the object. + the gate, then the import or the copy. The runs are kept with the object. Where a live chunk + import holds some of the object's pages at the RAM's own addresses (a run crossing the end of a + chunk imported on both sides), the object is a copy of its pages, the gate not asked + (`copied-host-pages`, `copied_over_live_imports`): an import of its own would import those + pages a second time. - Elsewhere the chunks are never released while idle. Once the use is dropped, each is freed when nothing holds it (no buffer bound in it, no command buffer that used one), and the RAM's keep-alive is dropped after the last.