Files
metal2vulkan/crates/metal2vulkan-runtime/src/host_backing.rs
T
Anees Iqbal 158f7220ff Pages the process maps read-only are a copy whose GPU writes are dropped, on every device
On Linux, a no-copy buffer, heap or rebind over pages the process maps read-only went to the
caller's dma-buf source wherever it gave one, and so did pages an import alignment above the
host page left with no pointer import (by_pointer false), which skip the gate. A dma-buf of the
pages takes the GPU's writes, which Metal drops (an M3 Max under macOS 27.0.1: the CPU reads
read-only pages as they were). The runtime announced that as read-only-pages-written. Without a
source the pages were refused, where Metal makes the buffer. The owner's rule (10-23) is to drop
such writes on every device, never refuse them.

Such pages are now a copy whose GPU writes are dropped (copied.rs), with or without a source,
which is never called for them. The gate is still asked first where the pointer would be
imported, and its refusal is still counted as a refusal. Counted:
WorkCounts::copied_unwritable_pages (new, public), announced as copied-host-pages with its own
cause. Degradation::ReadOnlyPagesWritten is removed (public: api_signatures, docs/API.md's
catalogue, the lane's notice registry), and so are HostWindow::unwritable and
degrade_read_only_pages.

Tests: the dma_buf_imports lane test is now read_only_pages_are_a_copy_whose_gpu_writes_are_
dropped. It fails at the base, where the sourced buffer's fill reaches the file's pages and the
unsourced one is refused. The host_memory lane test a_read_only_page_is_a_copy_on_linux_and_
read_elsewhere replaces the refusal test. The runtime's copied_device tests cover a new buffer
and a rebind over read-only pages whose gate admits them: a copy, the source unasked. The
keep-alive order test uses writable memfd pages, which RADV imports as dma-bufs all the same.

(cherry picked from commit 68805d8a5742590e157b089f336c17e8e1dc4ef4)
2026-11-07 13:13:19 -08:00

81 lines
4.7 KiB
Rust

//! What the GPU uses for a buffer over the caller's host memory ([`Buffer::host_backing`], a
//! runtime extension): the caller's pages themselves, through one of the device's imports of
//! them, or a copy of them (`copied.rs`). Metal has no counterpart: a no-copy buffer's bytes are
//! always the caller's pages there.
//!
//! The answer is read from where the buffer lies now (`buffer::Lying`): its import's shape
//! (`buffer::Shape`: dma-bufs, a guest RAM's chunk, else a pointer) and whether that import is a
//! copy. It reads no page and takes no import's objects, so it keeps no import imported
//! (`host_residency.rs`).
use super::buffer::{Buffer, Lying};
/// What the GPU reads and writes for a buffer over the caller's host memory
/// ([`Buffer::host_backing`], a runtime extension): the caller's pages, through one of the
/// device's imports of them, or a copy of them. Metal has no counterpart: a no-copy buffer's bytes
/// are always the caller's pages there (`newBufferWithBytesNoCopy:length:options:deallocator:`),
/// so [`HostBacking::Copied`] is the one form that differs from Metal's.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum HostBacking {
/// In place: the device imported the pages by their pointer (`VK_EXT_external_memory_host`),
/// or for memory whose GPU writes Metal's private option bit 24 drops, by the pointer of a
/// read-only view of them. An import the device may release while idle and import again at
/// its next use (a runtime-internal residency, which nothing of Metal's observes) answers so
/// throughout.
ImportedByPointer,
/// In place: the device imported dma-bufs of the pages that the caller's source made
/// ([`HostMemory::with_dma_buf`](crate::HostMemory::with_dma_buf)), where it refused their
/// pointer. Released while idle and imported again as an import by pointer may be.
ImportedAsDmaBuf,
/// In place: bound in the device's import of a chunk of a guest RAM it uses
/// ([`Device::use_host_ram`](crate::Device::use_host_ram),
/// [`HostMemory::with_runs`](crate::HostMemory::with_runs)), with no import of its own.
InHostRam,
/// A copy of the pages in memory of the device's own, which the GPU reads and writes in their
/// place: the caller's gate refused their import
/// ([`HostMemory::with_import_gate`](crate::HostMemory::with_import_gate)), the import the
/// device and the gate admitted failed, a rebind's new bytes lie at another offset in their
/// page than the old under a texture over the buffer, on Linux the process maps some of the
/// pages read-only, or off macOS the memory is of Metal's private option bit 24; the copy of
/// read-only pages or bit-24 memory drops the GPU's writes. Announced as the
/// [`copied-host-pages`](crate::Degradation::CopiedHostPages) degradation: each commit writes
/// into the copy the bytes the CPU changed in the pages, and each batch's completion writes
/// back the bytes the GPU changed, where Metal's GPU uses the pages themselves.
Copied,
/// In no pages: detached ([`Buffer::detach_backing`]) until a rebind gives it some.
Detached,
}
impl Buffer {
/// What the GPU uses for the buffer's bytes now, where they are the caller's host memory: a
/// no-copy buffer's, or for a buffer placed in a heap over the caller's host memory
/// (`Device::new_heap_with_bytes_no_copy`) its heap's ([`HostBacking`]). `None` for any other
/// buffer. A runtime extension; Metal has no counterpart, as a no-copy buffer's bytes are
/// always the caller's pages there: [`HostBacking::Copied`] answers where they are a copy
/// instead, announced as the
/// [`copied-host-pages`](crate::Degradation::CopiedHostPages) degradation.
///
/// The answer changes only where the buffer's pages do: at its
/// [`rebind_bytes_no_copy`](Buffer::rebind_bytes_no_copy), which may move it between an import
/// and a copy, and its [`detach_backing`](Buffer::detach_backing) (for a heap's buffer, its
/// heap's); between them it stays. A query alone: it reads no page and keeps no import
/// imported, unlike [`gpu_address`](Buffer::gpu_address).
pub fn host_backing(&self) -> Option<HostBacking> {
let pages = match self.pages_cell()?.lying() {
Lying::In(pages) => pages,
Lying::Detached(_) => return Some(HostBacking::Detached),
};
let backing = if pages.copied() {
HostBacking::Copied
} else if pages.in_ram_chunk() {
HostBacking::InHostRam
} else if pages.over_dma_buf() {
HostBacking::ImportedAsDmaBuf
} else {
HostBacking::ImportedByPointer
};
Some(backing)
}
}