diff --git a/Cargo.toml b/Cargo.toml index 85cfa8b26..6c6039834 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,11 +33,11 @@ reims-vgpu-graphics = { path = "crates/reims-vgpu-graphics" } reims-vgpu-video = { path = "crates/reims-vgpu-video" } # metal2vulkan's Metal types, for the Metal enums and descriptor values the runtime's calls take, # from metal2vulkan's public repository at the commit both name. -metal2vulkan = { git = "https://github.com/steelbrain/metal2vulkan", rev = "9f3ff1aa7544001b7e80c29c407bf77fa307961d" } +metal2vulkan = { git = "https://github.com/steelbrain/metal2vulkan", rev = "77f3f2d99d3792acd9fee6536c52cf0960fb521d" } # The Vulkan platform's runtime (architecture D1), metal2vulkan-runtime: a Metal-shaped API on # Vulkan, one path for every device, under reims-vgpu-vulkan's `rt_platform` and `seam` and # reims-vgpu's `pool::rt_blocks`. -metal2vulkan-runtime = { git = "https://github.com/steelbrain/metal2vulkan", rev = "9f3ff1aa7544001b7e80c29c407bf77fa307961d" } +metal2vulkan-runtime = { git = "https://github.com/steelbrain/metal2vulkan", rev = "77f3f2d99d3792acd9fee6536c52cf0960fb521d" } # The hashes of the Vulkan platform's metallib sources (reims-vgpu-vulkan/build.rs). sha2 = "0.10.9" # A display window's swapchain on Linux (reims-vgpu-vulkan/src/swapchain.rs), at the runtime's diff --git a/crates/reims-vgpu-vulkan/src/rt_platform/buffer.rs b/crates/reims-vgpu-vulkan/src/rt_platform/buffer.rs index d264111b8..5a610e39c 100644 --- a/crates/reims-vgpu-vulkan/src/rt_platform/buffer.rs +++ b/crates/reims-vgpu-vulkan/src/rt_platform/buffer.rs @@ -516,14 +516,18 @@ impl RtBuffer { } /// The device address of its first byte where rt made it, for the device's history of - /// released ranges and a lost device's walk (`super::fault`): an exportable buffer's from its - /// export's record of the block (`super::export`), any other's rt's `gpuAddress`. So these - /// diagnostics hand out no address of an exported block, which rt's `gpuAddress` would. + /// released ranges and a lost device's walk (`super::fault`): rt's `gpuAddress`; `None` for + /// an exportable buffer, in an exported block of its own (`super::export`), whose address + /// these diagnostics never ask. rt's `gpuAddress` of a buffer in an exported block hands out + /// an address in it (rt's `Metrics::exported_blocks_addressed`), after which a submission + /// that writes through an address copies the textures over the block in; the block's handle + /// states none (`BlockInfo::gpu_address`). So such a buffer is in neither the placement nor + /// the history. pub(super) fn fault_address(&self) -> Option { let made = self.rt.get()?; #[cfg(unix)] - if let Some(exported) = self.exported.get() { - return Some(exported.address()); + if self.exported.get().is_some() { + return None; } Some(made.gpu_address()) } @@ -800,16 +804,20 @@ impl HostMappings { impl KnownBlocks { /// Knows `block` by its GPU address, in place of a block known there before, holding the - /// copy it is, where it is one. - pub fn know(&self, block: rt::Block) { + /// copy it is, where it is one: the address, which rt states for every block it takes or + /// imports (`BlockInfo::gpu_address`). A block that states none, an exported one alone, which + /// no caller hands it, is not known (no pin can lie in it): `None`. + pub fn know(&self, block: rt::Block) -> Option { + let at = block.gpu_address?; let copy = super::copied::of(block.id); let id = block.id; - let replaced = self.known.lock().insert(block.gpu_address, block); + let replaced = self.known.lock().insert(at, block); let mut copies = self.copies.lock(); let gone = replaced.and_then(|old| copies.remove(&old.id)); let held = copy.and_then(|copy| copies.insert(id, copy)); drop(copies); drop((gone, held)); + Some(at) } /// Forgets the block `id`, giving back the handle it held, and lets go of the copy it is, @@ -891,11 +899,14 @@ impl KnownBlocks { self.mapped.0.lock().keys().copied().collect() } - /// Each block known now, for a lost device's fault (`super::fault`); `None` where the set's - /// lock was not had within `bound`. - pub(super) fn fault_blocks(&self, bound: std::time::Duration) -> Option> { + /// Each block known now, with the GPU address it is known at, for a lost device's fault + /// (`super::fault`); `None` where the set's lock was not had within `bound`. + pub(super) fn fault_blocks( + &self, + bound: std::time::Duration, + ) -> Option> { let known = self.known.try_lock_for(bound)?; - Some(known.values().map(|block| **block).collect()) + Some(known.iter().map(|(&at, block)| (at, **block)).collect()) } } @@ -1402,6 +1413,18 @@ fn in_known( } } +/// Logs that rt states no GPU address of block `block`, which it took or imported for `sel` +/// (`BlockInfo::gpu_address`): rt states one for every block it takes or imports, an exported +/// block alone stating none, so the line marks a break of rt's contract, and the caller answers +/// as where rt refuses the block. The logger writes the first such line and counts the rest. +pub(super) fn unaddressed(sel: &'static str, block: rt::BlockId) { + log::error!( + target: TARGET, + "{sel}: rt's block {} states no GPU address, so the call answers as refused", + block.get() + ); +} + /// A block of `class` for `length` bytes, for `sel`; `None` where rt refuses it, counted. A /// Private one's take counts rt's Private allocations where they rise past their most /// ([`PRIVATE_ALLOCATION_MOST_CODE`]). @@ -2409,13 +2432,15 @@ fn pointer_refused() -> Option { /// is made and counted ([`import_pages`]), as a copy that drops the GPU's writes where `access` is /// read-only (the module's docs, "Read-only memory"), read once where the pages never change /// (`PagesAccess::Unchanging`), known to `device` from now (`KnownBlocks::know`): the -/// block and the bytes' offset in it. A no-copy buffer over the bytes pinned at the block's -/// address plus their offset is then that range of the block ("A pinned address"), with no import -/// of its own. The caller forgets the block (`KnownBlocks::forget`) as the last such buffer goes; -/// rt frees the import once no buffer over it and no command buffer that used it lives, then -/// runs `then`; a copy's release runs it as the copy goes, which the known block holds until -/// forgotten (the module's docs, "A copy at the refusal point"). `None` where Metal or rt refuses -/// it, counted, `then` dropped unrun. +/// block, the bytes' offset in it and their GPU address, the block's plus the offset. A no-copy +/// buffer over the bytes pinned at that address is then that range of the block ("A pinned +/// address"), with no import of its own. The caller forgets the block (`KnownBlocks::forget`) as +/// the last such buffer goes; rt frees the import once no buffer over it and no command buffer +/// that used it lives, then runs `then`; a copy's release runs it as the copy goes, which the +/// known block holds until forgotten (the module's docs, "A copy at the refusal point"). `None` +/// where Metal or rt refuses it, counted, `then` dropped unrun; and where rt states no GPU +/// address of the import, which it states for every import ([`unaddressed`]): the import is then +/// not known, and rt runs `then` as it frees it. pub fn import_known( device: &RtObj, sel: &'static str, @@ -2423,11 +2448,14 @@ pub fn import_known( length: u64, access: PagesAccess, then: impl FnOnce() + Send + 'static, -) -> Option<(rt::Block, u64)> { +) -> Option<(rt::Block, u64, u64)> { let (block, offset) = import_pages(device, sel, first, length, access, then)?; let _fresh = super::copied::Fresh(block.id); - device.known_blocks().know(block.clone()); - Some((block, offset)) + let Some(at) = device.known_blocks().know(block.clone()) else { + unaddressed(sel, block.id); + return None; + }; + Some((block, offset, at.saturating_add(offset))) } /// The imports of the host's memory live now. @@ -3085,20 +3113,22 @@ mod tests { private.expect("a Private one"), ); let known = device.known_blocks().clone(); - known.know(shared.clone()); - known.know(private.clone()); + let shared_at = known.know(shared.clone()).expect("a taken block's address"); + let private_at = known + .know(private.clone()) + .expect("a taken block's address"); let mapping = shared.contents.expect("a Shared block's mapping").as_ptr(); let written = HostPtr::from_ptr(mapping.wrapping_add(0x100)); // SAFETY: the block is mapped while it lives, and 0x100 + 64 bytes lie in it. unsafe { written.as_ptr::().write_bytes(0xab, 64) }; - let pin = shared.gpu_address + 0x100; + let pin = shared_at + 0x100; let made = device .new_buffer(pinned(pin, 0, None)) .expect("never a raise"); let made = made.expect("a buffer in the known block"); assert_eq!((made.gpu_address(), made.contents()), (pin, written)); assert_eq!(read(made.contents(), 64), [0xab; 64]); - let pin = private.gpu_address + 0x200; + let pin = private_at + 0x200; let made = device .new_buffer(pinned(pin, 0x20, None)) .expect("never a raise"); @@ -3106,7 +3136,7 @@ mod tests { made.expect("a Private buffer in its block").gpu_address(), pin ); - let pin = shared.gpu_address + 0x100; + let pin = shared_at + 0x100; let made = device .new_buffer(pinned(pin, 0x20, None)) .expect("never a raise"); @@ -3136,7 +3166,7 @@ mod tests { // the pin in the Private block where the GPU keeps one, else counted as not kept too. let unified = device.rt().has_unified_memory(); let (unpinned, imports) = (counted(UNPINNED_CODE), counted(IMPORTS_CODE)); - let pin = private.gpu_address + 0x800; + let pin = private_at + 0x800; for (options, kept) in [(0, false), (0x10, !unified)] { // SAFETY: the block's mapping lives while the test holds the block. let bytes = unsafe { HostRange::new(written, 64) }; @@ -3144,11 +3174,7 @@ mod tests { let made = made .expect("never a raise") .expect("a buffer over the block"); - let at = if kept { - pin - } else { - shared.gpu_address + 0x100 - }; + let at = if kept { pin } else { shared_at + 0x100 }; assert_eq!((made.gpu_address(), made.contents()), (at, written)); } assert_eq!(counted(IMPORTS_CODE), imports); @@ -3168,7 +3194,7 @@ mod tests { unsafe { at.as_ptr::().write_bytes(0xcd, 64) }; let freed = Arc::new(AtomicBool::new(false)); let done = Arc::clone(&freed); - let pin = private.gpu_address + 0x400; + let pin = private_at + 0x400; // SAFETY: the memory stays allocated until the deallocator ran (below). let bytes = unsafe { HostRange::new(at, 64) }; let desc = BufferDesc { @@ -3638,18 +3664,18 @@ mod tests { let block = (device.rt()).take_block(rt::MemoryClass::Private, 0x10000); let block = block.expect("a Private block"); let known = device.known_blocks().clone(); - known.know(block.clone()); + let block_at = known.know(block.clone()).expect("a taken block's address"); let queue = device.new_command_queue().expect("a command queue"); let out = device .new_buffer_of_length(on, 64, 0) .expect("never a raise"); let out = out.expect("a Shared buffer"); let private = |at: u64| { - let made = device.new_buffer(pinned(block.gpu_address + at, 0x20, None)); + let made = device.new_buffer(pinned(block_at + at, 0x20, None)); let made = made .expect("never a raise") .expect("a Private buffer in its block"); - assert_eq!(made.gpu_address(), block.gpu_address + at); + assert_eq!(made.gpu_address(), block_at + at); made }; let read_back = |buffer: &RtObj| { @@ -4161,11 +4187,13 @@ mod tests { PagesAccess::Writable, then, ); - let (block, offset) = known.expect("a known import, as the buffer's above"); + let (block, offset, pin) = known.expect("a known import, as the buffer's above"); let mapped = device.known_blocks().contents_of(&block); assert_eq!(block.contents, None); assert_eq!(mapped.map(|first| first + offset), Some(at.addr().to_u64())); - let (pin, imports) = (block.gpu_address + offset, counted(IMPORTS_CODE)); + let address = block.gpu_address.expect("an import's address"); + assert_eq!(pin, address + offset); + let imports = counted(IMPORTS_CODE); // SAFETY: the pages are the test's own, never freed. let bytes = unsafe { HostRange::new(at, 64) }; let made = device.new_buffer(pinned(pin, 0, Some(bytes))); @@ -4869,10 +4897,12 @@ mod tests { PagesAccess::Writable, then, ); - let (block, offset) = known.expect("a known import over a copy"); + let (block, offset, pin) = known.expect("a known import over a copy"); let home = device.known_blocks().contents_of(&block); assert_eq!(home.map(|home| home + offset), Some(first.addr().to_u64())); - let (pin, imports) = (block.gpu_address + offset, counted(IMPORTS_CODE)); + let address = block.gpu_address.expect("a copy's address"); + assert_eq!(pin, address + offset); + let imports = counted(IMPORTS_CODE); // SAFETY: the memory is never freed. let bytes = unsafe { HostRange::new(first, 64) }; let made = device.new_buffer(pinned(pin, 0, Some(bytes))); diff --git a/crates/reims-vgpu-vulkan/src/rt_platform/diag.rs b/crates/reims-vgpu-vulkan/src/rt_platform/diag.rs index c56bef43c..12e442f79 100644 --- a/crates/reims-vgpu-vulkan/src/rt_platform/diag.rs +++ b/crates/reims-vgpu-vulkan/src/rt_platform/diag.rs @@ -750,7 +750,8 @@ pub fn copy_made(block: rt::BlockId) { } /// Notes block `block`, imported now for `sel` (`super::buffer::import_pages`): a copy of the -/// pages where [`copy_made`] noted it, else the pages themselves. +/// pages where [`copy_made`] noted it, else the pages themselves. Its range is kept where it +/// states its GPU address, which rt states for every import (`BlockInfo::gpu_address`). pub fn imported(block: &rt::BlockInfo, sel: &'static str) { let origin = Origin { sel, @@ -758,13 +759,15 @@ pub fn imported(block: &rt::BlockInfo, sel: &'static str) { thread: thread_class(), copy: COPIES.lock().remove(&block.id), }; - let imported = Imported { - origin, - gpu_address: block.gpu_address, - len: block.len, - class: block.class, - }; - IMPORTED.lock().insert(block.id, imported); + if let Some(gpu_address) = block.gpu_address { + let imported = Imported { + origin, + gpu_address, + len: block.len, + class: block.class, + }; + IMPORTED.lock().insert(block.id, imported); + } *IMPORTS.lock().entry(origin).or_insert(0) += 1; } diff --git a/crates/reims-vgpu-vulkan/src/rt_platform/export.rs b/crates/reims-vgpu-vulkan/src/rt_platform/export.rs index 834f9c4b3..93d145c89 100644 --- a/crates/reims-vgpu-vulkan/src/rt_platform/export.rs +++ b/crates/reims-vgpu-vulkan/src/rt_platform/export.rs @@ -256,13 +256,6 @@ impl Exported { }) } - /// The device address of the buffer's first byte, from the block's record rt gave at its make - /// (`BlockInfo::gpu_address`), with no ask of rt's `gpuAddress`, which hands out an address - /// of the block. - pub(super) fn address(&self) -> u64 { - self.block.gpu_address.saturating_add(self.offset) - } - /// rt's acquire of what the other API wrote into the block (the module's docs, "The /// acquire"); `Err` says why rt refused it. pub(super) fn acquire(&self) -> Result<(), String> { @@ -299,10 +292,12 @@ mod tests { /// Exportable buffers on the GPU the test holds, where its device exports an opaque fd: a /// Private buffer of the asked length at a GPU address, its byte 32 on a multiple of 256 /// bytes of its memory where asked; each export a new fd of the dedicated allocation, which - /// holds the buffer, none as a dma-buf; the acquire taken; the address the fault history - /// takes from the export's record its `gpuAddress`. A plain buffer exports nothing and takes - /// no acquire, and options of other storage are declined uncounted. Where the device exports - /// no opaque fd, the refusal is counted and the make answers `None`. + /// holds the buffer, none as a dma-buf; the acquire taken. No address of its block goes out + /// at the make or to the fault history, which takes none of it (rt's count of the exported + /// blocks addressed stays), until its `gpuAddress` is read, which counts its block. A plain + /// buffer exports nothing and takes no acquire, and its fault address is its `gpuAddress`; + /// options of other storage are declined uncounted. Where the device exports no opaque fd, the + /// refusal is counted and the make answers `None`. #[test] fn exportable_buffers_on_vulkan() { use std::os::fd::AsRawFd as _; @@ -311,6 +306,8 @@ mod tests { let on = OnFault::Return; let length = 3840 * 2160 * 3 / 2 + 32; let refusals = counted(EXPORT_REFUSED_CODE); + let addressed = || device.rt().counters().metrics().exported_blocks_addressed; + let before = addressed(); let made = device.new_exportable_buffer_of_length(on, length, 0x20, Some(32)); let Some(buffer) = made.expect("never a raise") else { assert_eq!(counted(EXPORT_REFUSED_CODE), refusals + 1); @@ -320,8 +317,11 @@ mod tests { assert_eq!(counted(EXPORT_REFUSED_CODE), refusals); assert_eq!((buffer.length(), buffer.storage_mode()), (length, 2)); assert_eq!(buffer.resource_options(), 0x220); + assert_eq!(buffer.fault_address(), None); + assert_eq!(addressed() - before, 0, "no address of the block out"); assert_ne!(buffer.gpu_address(), 0); - assert_eq!(buffer.fault_address(), Some(buffer.gpu_address())); + assert_eq!(addressed() - before, 1, "the buffer's address read"); + assert_eq!(buffer.fault_address(), None); let first = buffer.export_memory().expect("an export"); let second = buffer.export_memory().expect("another"); assert_ne!(first.fd.as_raw_fd(), second.fd.as_raw_fd()); @@ -340,7 +340,8 @@ mod tests { .expect("never a raise") .expect("an exportable buffer"); assert_eq!(unaligned.export_memory().expect("an export").offset, 0); - assert_eq!(unaligned.fault_address(), Some(unaligned.gpu_address())); + assert_eq!(unaligned.fault_address(), None); + assert_eq!(addressed() - before, 1, "no address of its block out"); let plain = (device.new_buffer_of_length(on, 4096, 0x20)) .expect("never a raise") @@ -357,10 +358,11 @@ mod tests { /// Dma-buf buffers on the GPU the test holds, where its device exports a dma-buf: a Private /// buffer of the asked length at a GPU address, as far into its block as puts its byte 32 on /// a multiple of 256 bytes of the memory; each export a new dma-buf of the dedicated - /// allocation, none as an opaque fd; the acquire taken; the address the fault history takes - /// from the export's record its `gpuAddress`. With no byte named, the buffer lies at its - /// block's first byte. Options of other storage are declined uncounted. Where the device - /// exports no dma-buf, the refusal is counted and the make answers `None`. + /// allocation, none as an opaque fd; the acquire taken; no address of its block out at the + /// make or to the fault history, until its `gpuAddress` is read, as an opaque fd's. With no + /// byte named, the buffer lies at its block's first byte. Options of other storage are + /// declined uncounted. Where the device exports no dma-buf, the refusal is counted and the + /// make answers `None`. #[test] fn dma_buf_buffers_on_vulkan() { use std::os::fd::AsRawFd as _; @@ -369,6 +371,8 @@ mod tests { let on = OnFault::Return; let length = 3840 * 2160 * 3 / 2 + 32; let refusals = counted(EXPORT_REFUSED_CODE); + let addressed = || device.rt().counters().metrics().exported_blocks_addressed; + let before = addressed(); let made = device.new_dma_buf_buffer_of_length_padding_unobserved(on, length, 0x20, Some(32)); let Some(buffer) = made.expect("never a raise") else { @@ -378,8 +382,11 @@ mod tests { }; assert_eq!(counted(EXPORT_REFUSED_CODE), refusals); assert_eq!((buffer.length(), buffer.storage_mode()), (length, 2)); + assert_eq!(buffer.fault_address(), None); + assert_eq!(addressed() - before, 0, "no address of the block out"); assert_ne!(buffer.gpu_address(), 0); - assert_eq!(buffer.fault_address(), Some(buffer.gpu_address())); + assert_eq!(addressed() - before, 1, "the buffer's address read"); + assert_eq!(buffer.fault_address(), None); let first = buffer.export_dma_buf().expect("an export"); let second = buffer.export_dma_buf().expect("another"); assert_ne!(first.fd.as_raw_fd(), second.fd.as_raw_fd()); @@ -397,7 +404,8 @@ mod tests { device.new_dma_buf_buffer_of_length_padding_unobserved(on, 4096, 0x20, None); let unaligned = unaligned.expect("never a raise").expect("a dma-buf buffer"); assert_eq!(unaligned.export_dma_buf().expect("an export").offset, 0); - assert_eq!(unaligned.fault_address(), Some(unaligned.gpu_address())); + assert_eq!(unaligned.fault_address(), None); + assert_eq!(addressed() - before, 1, "no address of its block out"); let shared = device.new_dma_buf_buffer_of_length_padding_unobserved(on, 4096, 0, None); assert!(shared.expect("never a raise").is_none()); assert_eq!(counted(EXPORT_REFUSED_CODE), refusals); @@ -407,9 +415,9 @@ mod tests { /// docs, "Deferred exports"): a Private buffer of the asked length that holds no memory, at /// GPU address 0, until its first use; an export is such a use, and makes the memory exported /// as the eager make's is, its byte 32 on a multiple of 256 bytes, each export a new handle - /// of the dedicated allocation, the other handle's export refused, the acquire taken, the - /// address the fault history takes from the export's record its `gpuAddress`; a use - /// that is no export makes it so too. Where the device exports no such handle, the memory is + /// of the dedicated allocation, the other handle's export refused, the acquire taken, no + /// address of its block out at the make or to the fault history; a use that is no export + /// makes it so too. Where the device exports no such handle, the memory is /// made as any deferred buffer's, the refusal counted, and the export is refused. Options of /// other storage make the eager make's buffer, or none, as it does. #[test] @@ -463,8 +471,8 @@ mod tests { continue; }; assert!(buffer.memory_made(), "{handle:?}: the export made it"); + assert_eq!(buffer.fault_address(), None, "{handle:?}: exported"); assert_ne!(buffer.gpu_address(), 0); - assert_eq!(buffer.fault_address(), Some(buffer.gpu_address())); assert_eq!(counted(EXPORT_REFUSED_CODE), refusals); let second = export(&buffer, handle).expect("another"); assert_ne!(first.fd.as_raw_fd(), second.fd.as_raw_fd()); diff --git a/crates/reims-vgpu-vulkan/src/rt_platform/fault.rs b/crates/reims-vgpu-vulkan/src/rt_platform/fault.rs index 48a35fce3..99a0904f0 100644 --- a/crates/reims-vgpu-vulkan/src/rt_platform/fault.rs +++ b/crates/reims-vgpu-vulkan/src/rt_platform/fault.rs @@ -48,7 +48,9 @@ //! 1. The live objects of the decoders' tables whose GPU range is known, walked at the loss //! ([`LiveObjects`], `reims_vgpu`'s `graphics::fault_walk`): buffers, heaps, textures over a //! buffer, and the `Shared` buffers over two-copy `Managed` textures' CPU copies, each removed -//! entry kept for an open view among them. A deferred buffer rt has not made yet has no range. +//! entry kept for an open view among them. A deferred buffer rt has not made yet has no range, +//! nor has a buffer in an exported block of its own (`super::export`), whose address the +//! placement never asks of rt (`RtBuffer::fault_address`), nor a texture over such a buffer. //! A texture rt made as an image of its own, in a heap or not, has no GPU range the platform //! knows: one in a heap lies in its heap's range; any other's memory is rt's alone. //! 2. The blocks still mapped that the host sees ([`MappedBlock`]): rt's pool allocations @@ -68,7 +70,8 @@ //! counted. An object's range is recorded as its platform object goes (the guest released it and //! the host let go of it): a buffer rt made, a heap, a two-copy `Managed` texture's CPU-copy //! buffer, and a texture over a buffer while that buffer is there (one whose buffer went first is -//! not recorded: the buffer's range, recorded as it went, holds its bytes). Each entry +//! not recorded: the buffer's range, recorded as it went, holds its bytes); not a buffer in an +//! exported block, nor a texture over one, which have no range (above). Each entry //! ([`Release`]) holds the object's kind, its guest object id, whose bits from 40 are its context //! ([`Named`]: told as a decoder's table stores the object, `reims_vgpu`'s //! `graphics::fault_walk::name`), its storage mode, its range's first byte's GPU address and @@ -1213,7 +1216,7 @@ pub fn texture_parts(context: u32, id: u64, gone: bool, texture: &RtObj) -> Parts { - let (block, offset, size) = heap.fault_span(); + let (block, block_address, offset, size) = heap.fault_span(); Parts { object: Some(LiveObject { context, @@ -1232,7 +1235,7 @@ pub fn heap_parts(context: u32, id: u64, gone: bool, heap: &RtObj) -> Pa ), class: block.class, import: import_kind(heap.device_known(), block.id), - gpu_address: block.gpu_address, + gpu_address: block_address, len: block.len, }), } @@ -1392,7 +1395,7 @@ impl FaultWatch { Some(known) => format!("{} known blocks", known.len()), None => "no known blocks (their lock was not had in time)".to_owned(), }; - for info in known.unwrap_or_default() { + for (gpu_address, info) in known.unwrap_or_default() { ids.insert(info.id); blocks.push(MappedBlock { what: format!( @@ -1402,7 +1405,7 @@ impl FaultWatch { ), class: info.class, import: import_kind(&self.known, info.id), - gpu_address: info.gpu_address, + gpu_address, len: info.len, }); } diff --git a/crates/reims-vgpu-vulkan/src/rt_platform/heap.rs b/crates/reims-vgpu-vulkan/src/rt_platform/heap.rs index 0ed82793e..bcbfab2b4 100644 --- a/crates/reims-vgpu-vulkan/src/rt_platform/heap.rs +++ b/crates/reims-vgpu-vulkan/src/rt_platform/heap.rs @@ -112,6 +112,9 @@ const MODES: u64 = 0xff; pub struct RtHeap { /// The block it is a range of, held. block: rt::Block, + /// The GPU address of the block's first byte, which rt states for every block a heap lies in + /// (a taken or imported one), read once at the make. + block_address: u64, /// Its first byte in the block. offset: u64, /// Its size: the descriptor's, rounded up to [`HEAP_PAGE`]. @@ -147,7 +150,7 @@ impl Drop for RtHeap { if id != 0 { let kind = self.named.kind(super::fault::ObjectKind::Heap); let storage = (self.options >> 4) & 0xf; - let gpu_address = self.block.gpu_address + self.offset; + let gpu_address = self.block_address + self.offset; let releases = self.device.releases(); releases.push(kind, id, storage, (gpu_address, self.size)); } @@ -168,10 +171,10 @@ impl RtHeap { self.block.id } - /// The block it is a range of, its first byte's offset there and its size, for a lost - /// device's fault (`super::fault`). - pub(super) fn fault_span(&self) -> (&rt::Block, u64, u64) { - (&self.block, self.offset, self.size) + /// The block it is a range of, the GPU address of the block's first byte, the heap's first + /// byte's offset there and its size, for a lost device's fault (`super::fault`). + pub(super) fn fault_span(&self) -> (&rt::Block, u64, u64, u64) { + (&self.block, self.block_address, self.offset, self.size) } /// The blocks its device knows (`super::fault`). @@ -245,19 +248,23 @@ pub fn of_desc(device: &RtObj, desc: &HeapDesc, held: u64) -> Option known, + let known = pin.and_then(|pin| at(pin).map(|known| (known, pin))); + let (block, offset, block_address) = match known { + Some(((block, offset), pin)) => (block, offset, pin - offset), None => { if let Some(pin) = pin { unpinned(SEL, pin); } - (zeroed(device, SEL, class, size)?, 0) + let block = zeroed(device, SEL, class, size)?; + let address = address_of(&block, SEL)?; + (block, 0, address) } }; if read_only && !super::copied::drops(block.id) { buffer::read_only(SEL); } - Some(heap(device, block, offset, size, placement, options, None)) + let placed = (block, block_address, offset); + Some(heap(device, placed, size, placement, options, None)) } /// A block of `class` of the `size` bytes of a heap of its own, for `sel`, zeroed: a Shared one @@ -315,8 +322,9 @@ pub fn over_host_memory( let first = bytes.ptr(); let pin = desc.pinned_gpu_address; let at = |pin| known_at(device, pin, size, class, Some(first)); - let (block, offset, deallocator) = match pin.and_then(at) { - Some((block, offset)) => (block, offset, deallocator), + let known = pin.and_then(|pin| at(pin).map(|known| (known, pin))); + let (block, offset, block_address, deallocator) = match known { + Some(((block, offset), pin)) => (block, offset, pin - offset, deallocator), None => { if let Some(pin) = pin { unpinned(SEL, pin); @@ -332,7 +340,8 @@ pub fn over_host_memory( } }; let (block, offset) = buffer::import_pages(device, SEL, first, size, access, then)?; - (block, offset, None) + let address = address_of(&block, SEL)?; + (block, offset, address, None) } }; // A copy just made is the heap's to hold from here (`super::copied::Fresh`). @@ -340,23 +349,27 @@ pub fn over_host_memory( if read_only && !super::copied::drops(block.id) { buffer::read_only(SEL); } - Some(heap( - device, - block, - offset, - size, - placement, - options, - deallocator, - )) + let placed = (block, block_address, offset); + Some(heap(device, placed, size, placement, options, deallocator)) } -/// The heap's handle: `size` bytes at `offset` in `block`, answering `options`, running -/// `deallocator` as it goes where given. +/// The GPU address of the first byte of `block`, which rt took or imported for a heap of `sel` +/// (`BlockInfo::gpu_address`); `None` where rt states none, logged ([`buffer::unaddressed`]), +/// the heap then nil. A block that the device knows is known at its address, which a heap pinned +/// in it reads from its pin instead. +fn address_of(block: &rt::Block, sel: &'static str) -> Option { + let address = block.gpu_address; + if address.is_none() { + buffer::unaddressed(sel, block.id); + } + address +} + +/// The heap's handle: `size` bytes at `offset` in `block`, whose first byte's GPU address is +/// `block_address`, answering `options`, running `deallocator` as it goes where given. fn heap( device: &RtObj, - block: rt::Block, - offset: u64, + (block, block_address, offset): (rt::Block, u64, u64), size: u64, placement: bool, options: u64, @@ -364,6 +377,7 @@ fn heap( ) -> RtObj { RtObj::new(RtHeap { named: super::fault::Named::default(), + block_address, offset, size, placement, @@ -407,7 +421,7 @@ impl RtObj { impl Heap for RtObj { /// Its first byte's: Vulkan's device address in its block. fn gpu_address(&self) -> u64 { - self.block.gpu_address + self.offset + self.block_address + self.offset } /// rt's buffer of the heap's range at `offset`, its bytes as they are, tracked as the heap and @@ -645,9 +659,10 @@ mod tests { // Pinned heaps. let block = (device.rt()).take_block(rt::MemoryClass::Shared, size); let block = block.expect("a Shared block"); - device.known_blocks().know(block.clone()); + let block_at = device.known_blocks().know(block.clone()); + let block_at = block_at.expect("a taken block's address"); let unpinned = counted(UNPINNED_CODE); - let pin = block.gpu_address + 0x4000; + let pin = block_at + 0x4000; let known = heap(&placement(0x4000, 0, Some(pin))).expect("a heap in the block"); assert_eq!(known.gpu_address(), pin); let other = heap(&placement(0x4000, 0x20, Some(pin))).expect("a Private heap"); @@ -681,7 +696,7 @@ mod tests { let first = HostPtr::from_ptr(mapping.wrapping_add(0x8000)); // SAFETY: the block's mapping lives while the test holds the block. let bytes = unsafe { HostRange::new(first, 0x4000) }; - let pin = block.gpu_address + 0x8000; + let pin = block_at + 0x8000; let made = device.new_heap_over_host_memory(bytes, &placement(0x4000, 0, Some(pin)), None); let over_block = made .expect("never a raise") @@ -904,18 +919,19 @@ mod tests { "a block of {len:#x} bytes at {:#x} in its allocation", block.offset ); - device.known_blocks().know(block.clone()); + let block_at = device.known_blocks().know(block.clone()); + let block_at = block_at.expect("a taken block's address"); let what = format!("a {size:#x}-byte heap's texture at {align:#x}"); - let off = heap(&placement(size, 0x20, Some(block.gpu_address + 0x8000))); + let off = heap(&placement(size, 0x20, Some(block_at + 0x8000))); let off = off.expect("a heap in the block"); let placed = off.new_texture(&texture, 0).expect("never a raise"); assert_eq!(placed.is_some(), align <= 0x8000, "{what}, 32 KiB in"); refused += u64::from(align > 0x8000); - let at = heap(&placement(size, 0x20, Some(block.gpu_address + pinned))); + let at = heap(&placement(size, 0x20, Some(block_at + pinned))); let at = at.expect("a heap in the block"); - assert_eq!(at.gpu_address(), block.gpu_address + pinned); + assert_eq!(at.gpu_address(), block_at + pinned); let own = heap(&placement(size, 0x20, None)).expect("a heap of its own"); for (placed_in, offset) in [(&at, 0), (&at, align), (&own, 0), (&own, align)] { let placed = placed_in diff --git a/crates/reims-vgpu/src/platform/rt.rs b/crates/reims-vgpu/src/platform/rt.rs index bc8f99711..160821d29 100644 --- a/crates/reims-vgpu/src/platform/rt.rs +++ b/crates/reims-vgpu/src/platform/rt.rs @@ -143,10 +143,10 @@ fn import_view( ) -> Option<(u64, Box)> { let sel = "newBufferWithBytesNoCopy:length:options:deallocator:"; let imported = rt_platform::import_known(device, sel, first, length, access, then); - let (block, offset) = imported?; + let (block, _, at) = imported?; let (known, id) = (device.known_blocks().clone(), block.id); let forget: Box = Box::new(move || drop(known.forget(id))); - Some((block.gpu_address.saturating_add(offset), forget)) + Some((at, forget)) } /// The files behind `map`'s ranges, registered while what this returns lives diff --git a/crates/reims-vgpu/src/pool/rt_blocks.rs b/crates/reims-vgpu/src/pool/rt_blocks.rs index 732aa7b15..ea0ae46f2 100644 --- a/crates/reims-vgpu/src/pool/rt_blocks.rs +++ b/crates/reims-vgpu/src/pool/rt_blocks.rs @@ -77,7 +77,8 @@ pub(crate) trait Knowing: Send + Sync { impl Knowing for reims_vgpu_vulkan::rt_platform::KnownBlocks { fn know(&self, block: rt::Block) { - reims_vgpu_vulkan::rt_platform::KnownBlocks::know(self, block); + // The take reads the block's address before it knows it. + let _ = reims_vgpu_vulkan::rt_platform::KnownBlocks::know(self, block); } fn forget(&self, id: rt::BlockId) { @@ -90,7 +91,9 @@ impl Knowing for reims_vgpu_vulkan::rt_platform::KnownBlocks { impl BlockSource for RtBlocks { /// rt's block of `class` at least `len` long, known from now. Err: [`BlockError::Length`] past /// the device's largest block (rt's `Limit::BlockLen`); [`BlockError::OutOfMemory`] for rt's - /// every other answer: its other limits, a device out of memory or lost, or one quiesced. + /// every other answer: its other limits, a device out of memory or lost, or one quiesced; and + /// for a block that states no GPU address (rt's `BlockInfo::gpu_address`), which rt states for + /// every block it takes, logged, the block given back. fn take_block(&self, class: MemoryClass, len: u64) -> Result { let rt_class = match class { MemoryClass::Private => rt::MemoryClass::Private, @@ -111,11 +114,19 @@ impl BlockSource for RtBlocks { rt::Error::Limit(rt::Limit::BlockLen { .. }) => BlockError::Length, _ => BlockError::OutOfMemory, })?; + let Some(gpu_address) = block.gpu_address else { + log::error!( + "pool: rt's block {} of {len} bytes states no GPU address: the take is refused", + block.id.get() + ); + let _ = self.device.rt().release_block(block); + return Err(BlockError::OutOfMemory); + }; let info = BlockInfo { id: BlockId(block.id.get()), class, len: block.len, - gpu_address: block.gpu_address, + gpu_address, contents: block.contents, }; self.known.know(block.clone()); @@ -165,9 +176,8 @@ mod tests { impl Knowing for Known { fn know(&self, block: rt::Block) { - self.0 - .lock() - .insert(block.id.get(), (block.gpu_address, block.len)); + let at = block.gpu_address.expect("a taken block's address"); + self.0.lock().insert(block.id.get(), (at, block.len)); } fn forget(&self, id: rt::BlockId) {