diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index efe28c992..9c25b81bd 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -352,20 +352,23 @@ the record the pass is making. on them. - **Host memory without a copy.** `unsafe Device::new_buffer_with_bytes_no_copy` imports host memory, given as a `HostMemory`, through `VK_EXT_external_memory_host`; memory the device cannot - import exactly is refused. `unsafe Device::new_heap_with_bytes_no_copy` is its heap twin. + import exactly is refused, but where the driver refuses the pointer itself (below). + `unsafe Device::new_heap_with_bytes_no_copy` is its heap twin. `Buffer::rebind_bytes_no_copy` and `Heap::rebind_bytes_no_copy` move either to another range, keeping the objects, addresses and IDs. A rebind is refused (`RebindProblem::EncodedUses`) while a command buffer not yet committed, or an open encoder, used the bytes; a render encoder whose attachment lies in them uses them from its open. Where a driver refuses the pointer (RADV refuses shared mappings, a `memfd`'s among them), memory made with `HostMemory::with_dma_buf` is imported from dma-bufs of its pages instead - ([HOWTO](../../docs/HOWTO.md#6-host-memory-a-driver-refuses)); on NVIDIA's driver the runtime + ([HOWTO](../../docs/HOWTO.md#6-host-memory-a-driver-refuses)), and a no-copy buffer or heap + over memory made without a source is a copy of its pages, as below; on NVIDIA's driver the runtime never hands the driver pages the kernel cannot pin (a dma-buf's mapping): they take the source, and else, or where its import fails, a copy, as below. A no-copy buffer's import of such dma-bufs that only blits have read may be released while idle and imported again at its next blit, which then costs time: RADV lists every import in every submission. Nothing of Metal's observes it. A no-copy buffer, heap, or buffer's or heap's rebind whose import the caller's gate - refuses (`HostMemory::with_import_gate`), or whose admitted import fails, is made over a copy of + refuses (`HostMemory::with_import_gate`), or whose admitted import fails, and a no-copy buffer + or heap whose pointer the device refuses with no source of dma-bufs given, is made over a copy of its bytes in memory of the device's own, announced (`copied-host-pages`, [HOWTO](../../docs/HOWTO.md#copied-host-buffers)), wherever the process maps the pages readable (a dma-buf's mapping among them); over pages not mapped readable it is refused. A diff --git a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs index 52e378995..3b3f6264e 100644 --- a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs +++ b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs @@ -875,22 +875,36 @@ impl Device { call, importer, pointer, length, window, source, class, keep, ) } - .map_err(|failed| source_failure(failed, refusal)), - Err(error) => Err(error), + .map_err(|failed| (source_failure(failed, refusal), false)), + // The device's refusal of the pointer, with no source of dma-bufs of the + // pages to take instead. + Err(error) => { + let by_device = error == not_importable(); + Err((error, by_device)) + } }; let (buffer, memory, again) = match imported { Ok(imported) => imported, // An import the device and the gate admitted that then failed, or pages the // runtime refused for the driver that no dma-buf of them served: a copy, // announced. - Err(error) if copyable && copies_after(&error) => { + Err((error, _)) if copyable && copies_after(&error) => { // SAFETY: the caller's contract. let copied = unsafe { self.copied_buffer(copy, release, dirty, CopiedFor::Failed(error)) }; return copied.map(|buffer| noted(kept(buffer, log, kept_runs))); } - Err(error) => return Err(error), + // Pages whose pointer the device refuses (RADV refuses shared memory, a + // `memfd` mapping among it), with no dma-buf source of them: a copy, + // announced, as Metal makes a no-copy buffer over any page. + Err((_, true)) if copyable => { + let why = CopiedFor::DeviceRefused; + // SAFETY: the caller's contract. + let copied = unsafe { self.copied_buffer(copy, release, dirty, why) }; + return copied.map(|buffer| noted(kept(buffer, log, kept_runs))); + } + Err((error, _)) => return Err(error), }; (buffer, memory, again) } @@ -1775,6 +1789,11 @@ impl Device { "the caller's import gate (HostMemory::with_import_gate) refused their \ import" } + CopiedBecause::DeviceRefused => { + "the device refused their pointer's import (VK_EXT_external_memory_host), as \ + RADV refuses shared memory, and no source of dma-bufs of them was given \ + (HostMemory::with_dma_buf)" + } CopiedBecause::Failed => "their import failed", CopiedBecause::Moved => { "they lie at another offset in their page than the buffer's old bytes, \ @@ -1870,6 +1889,7 @@ impl Device { let read_only = matches!(why, CopiedFor::ReadOnly); let (cause, failed) = match why { CopiedFor::Refused => (CopiedBecause::Refused, None), + CopiedFor::DeviceRefused => (CopiedBecause::DeviceRefused, None), CopiedFor::Failed(failed) => (CopiedBecause::Failed, Some(failed)), CopiedFor::ReadOnly => (CopiedBecause::ReadOnly, None), CopiedFor::Unwritable => (CopiedBecause::Unwritable, None), @@ -2260,6 +2280,9 @@ pub(super) fn no_copy_options(options: ResourceOptions) -> (ResourceOptions, boo enum CopiedFor { /// The caller's gate refused their import (`HostMemory::with_import_gate`). Refused, + /// The device refused their pointer's import, and the caller gave no source of dma-bufs of + /// them (`HostMemory::with_dma_buf`). + DeviceRefused, /// The import the device and the gate admitted failed, with this. Failed(Error), /// Metal's private option bit 24 drops the GPU's writes to the memory, and the system maps diff --git a/crates/metal2vulkan-runtime/src/copied.rs b/crates/metal2vulkan-runtime/src/copied.rs index d525b5fdf..b6d8dafc7 100644 --- a/crates/metal2vulkan-runtime/src/copied.rs +++ b/crates/metal2vulkan-runtime/src/copied.rs @@ -1,6 +1,7 @@ //! Copied host buffers: a no-copy buffer whose import of the caller's pages the caller's gate -//! refused ([`HostMemory::with_import_gate`](crate::HostMemory)), or whose import the gate and -//! the device admitted and that then failed (out of memory, another Vulkan error, or the runtime's +//! refused ([`HostMemory::with_import_gate`](crate::HostMemory)), whose pointer the device refused +//! with no source of dma-bufs of the pages given (RADV refuses shared memory, a `memfd` mapping +//! among it), or whose import the gate and the device admitted and that then failed (out of memory, another Vulkan error, or the runtime's //! refusal of pages the kernel cannot pin on a driver whose failed import of them breaks it, //! `RuntimeDriverDefect::UnpinnablePagesBreakHostMemory`, that no dma-buf of them served), is made //! all the same over a copy of its bytes (`Device::buffer_over_host_memory`), announced diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index 1d9a0c625..9b63a39a6 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -2042,7 +2042,10 @@ degradations! { /// A no-copy buffer, a no-copy heap or a buffer's rebind was made over a copy of the /// caller's host pages in memory of the device's own, which no import maps (`copied.rs`; /// a heap's textures shadows of its bytes): the caller's gate - /// (`HostMemory::with_import_gate`) refused the pages' import, or the import the device + /// (`HostMemory::with_import_gate`) refused the pages' import, or the device refused the + /// pointer's import and the caller gave no source of dma-bufs of the pages + /// (`HostMemory::with_dma_buf`; RADV refuses shared memory, a `memfd` mapping among it, + /// `WorkCounts::copied_host_buffers_device_refused`), or the import the device /// and the gate admitted failed, of the pointer or of the caller's dma-bufs (out of /// memory, or another Vulkan error, such as a driver out of the mappings the host kernel /// allows), or the runtime kept pages the kernel cannot pin from a driver whose failed diff --git a/crates/metal2vulkan-runtime/src/host_memory.rs b/crates/metal2vulkan-runtime/src/host_memory.rs index bb8aabe9f..ddca60783 100644 --- a/crates/metal2vulkan-runtime/src/host_memory.rs +++ b/crates/metal2vulkan-runtime/src/host_memory.rs @@ -43,8 +43,8 @@ //! imports a host pointer through amdgpu's userptr, which the kernel takes only over anonymous //! pages. Mesa 26.2.4 answers `VK_ERROR_INVALID_EXTERNAL_HANDLE`, 26.0.8 `VK_ERROR_UNKNOWN`, and //! both are refusals ([`import_refused`]). NVIDIA and lavapipe imported the same shared memory. -//! - **Dma-bufs of the pages** ([`HostMemory::with_dma_buf`]), the last resort before that -//! refusal: where the device refuses the pointer ([`refused`]: the import window, the buffer's +//! - **Dma-bufs of the pages** ([`HostMemory::with_dma_buf`]), the last import before a copy +//! or a refusal: where the device refuses the pointer ([`refused`]: the import window, the buffer's //! binding in it, the memory types, the driver, or the runtime for a driver that breaks on //! pages the kernel cannot pin, [`unpinnable`]) and imports dma-bufs, the buffer is bound in //! imports of the source's dma-bufs of the pages instead (`dma_buf.rs`). Every import the device @@ -259,7 +259,9 @@ impl HostMemory { /// imported. Some drivers also /// refuse shared memory (RADV refused anonymous shared memory and mappings of shared memory /// objects, `memfd` mappings among them); private memory imports everywhere measured. - /// [`HostMemory::with_dma_buf`] imports such memory where the device imports dma-bufs. + /// [`HostMemory::with_dma_buf`] imports such memory where the device imports dma-bufs; a + /// no-copy buffer or heap over it made without one is a copy of the pages, announced + /// (`copied-host-pages`), and a rebind to it is refused. pub unsafe fn new(pointer: NonNull) -> HostMemory { HostMemory { pointer, diff --git a/crates/metal2vulkan-runtime/src/host_shared_pointers.rs b/crates/metal2vulkan-runtime/src/host_shared_pointers.rs index 06390fb85..982925ff3 100644 --- a/crates/metal2vulkan-runtime/src/host_shared_pointers.rs +++ b/crates/metal2vulkan-runtime/src/host_shared_pointers.rs @@ -24,9 +24,11 @@ impl Device { /// released at once, and answered from that after; the import is counted as a no-copy /// buffer's is. False for a device that imports no host memory. /// - /// Where it is false, a no-copy buffer over shared memory is made only from the caller's - /// dma-buf source ([`HostMemory::with_dma_buf`](crate::HostMemory::with_dma_buf)), and a - /// source is what such memory needs, whatever its pages' protection: pages the process maps + /// Where it is false, a no-copy buffer over shared memory is imported only from the caller's + /// dma-buf source ([`HostMemory::with_dma_buf`](crate::HostMemory::with_dma_buf)), and without + /// one is a copy of the pages, announced (`copied-host-pages`, + /// `WorkCounts::copied_host_buffers_device_refused`). A source is what such memory needs to be + /// imported, whatever its pages' protection: pages the process maps /// read-only go to the source without their pointer's import being tried (a dma-buf pins /// nothing of the process's mapping), so a caller need not make a writable second mapping of /// them for the import. Measured: MoltenVK (an M3 Max) and lavapipe answer true, RADV (a diff --git a/crates/metal2vulkan-runtime/src/tests/copied_device.rs b/crates/metal2vulkan-runtime/src/tests/copied_device.rs index 2a52e34ca..c3f30d7d5 100644 --- a/crates/metal2vulkan-runtime/src/tests/copied_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/copied_device.rs @@ -19,6 +19,9 @@ //! copied: the buffer is refused, as their import is. //! - **A dma-buf's mapping** (Linux, where `/dev/udmabuf` opens), whose pages the kernel's read //! cannot pin, is copied where the gate refuses its import, with the same coherence. +//! - **Shared memory the device refuses** (Linux: a `memfd` mapped shared, made with no source of +//! dma-bufs) is copied where the device refuses its pointer (RADV), announced and counted as the +//! device's refusal, with the same coherence; imported elsewhere. //! - **Read-only pages** (a read-only view of shared memory) are a copy whose GPU writes are //! dropped, a buffer's and a rebind's: where the gate refuses their import, counted as a //! refusal; on Linux where it admits it, counted as a copy of unwritable pages @@ -82,6 +85,10 @@ enum Backing { DmaBuf { _mapping: super::copied::DmaBufMapping, }, + /// A `memfd`'s pages mapped shared, read and write, as QEMU maps its + /// `memory-backend-memfd,share=on` guest RAM: unmapped as it drops. + #[cfg(target_os = "linux")] + Shared, } impl Host { @@ -110,6 +117,38 @@ impl Host { }) } + /// Host memory over a `memfd`'s pages mapped shared, read and write, in the pattern of `seed`. + #[cfg(target_os = "linux")] + fn over_shared_memory(seed: u8) -> Host { + use std::os::fd::AsRawFd; + let fd = shared_memory(); + // SAFETY: a new shared mapping of the memfd's `LENGTH` bytes, unmapped as the host drops; + // the descriptor may close once it is mapped. + let mapped = unsafe { + libc::mmap( + std::ptr::null_mut(), + LENGTH, + libc::PROT_READ | libc::PROT_WRITE, + libc::MAP_SHARED, + fd.as_raw_fd(), + 0, + ) + }; + assert_ne!( + mapped, + libc::MAP_FAILED, + "{}", + std::io::Error::last_os_error() + ); + let host = Host { + pointer: NonNull::new(mapped.cast()).unwrap(), + backing: Backing::Shared, + seed, + }; + host.write(0, &host.pattern()); + host + } + /// The pattern it was made with. fn pattern(&self) -> Vec { pattern(self.seed) @@ -166,6 +205,11 @@ impl Drop for Host { // Unmapped by the mapping's own drop. #[cfg(target_os = "linux")] Backing::DmaBuf { .. } => {} + // SAFETY: the mapping of `LENGTH` bytes made for it, which nothing uses any more. + #[cfg(target_os = "linux")] + Backing::Shared => unsafe { + assert_eq!(libc::munmap(self.pointer.as_ptr().cast(), LENGTH), 0); + }, } } } @@ -366,6 +410,44 @@ fn copied_buffer_is_made_where_the_caller_s_gate_refuses_the_import() { /// read cannot pin, whose import the gate refuses, are copied all the same (read with plain loads, /// mapped readable), with the copy's coherence. Linux only, where `/dev/udmabuf` opens. #[cfg(target_os = "linux")] +/// A no-copy buffer over shared memory (a `memfd` mapped shared, as QEMU's +/// `memory-backend-memfd,share=on` guest RAM is) made with no source of dma-bufs of its pages: on a +/// device that refuses the pointer of shared memory (`Device::imports_shared_host_pointers`, RADV) +/// a copy of the pages, announced and counted as the device's refusal, with every copy's +/// coherence, where the call was refused before; elsewhere an import of the pointer. +#[cfg(target_os = "linux")] +#[test] +fn copied_buffer_is_made_over_shared_memory_whose_pointer_the_device_refuses() { + let host = Host::over_shared_memory(9); + let Some((device, heard)) = device() else { + return; + }; + let refuses = !device.imports_shared_host_pointers().unwrap(); + let before = device.work_counts(); + let buffer = host.buffer(&device, None); + let after = device.work_counts(); + let device_refused = + after.copied_host_buffers_device_refused - before.copied_host_buffers_device_refused; + eprintln!( + "COPIED {}: the device refuses shared memory's pointer: {refuses}; the buffer over it is \ + {:?}, {device_refused} copied for the device's refusal", + device.name(), + buffer.host_backing(), + ); + match refuses { + true => { + assert_eq!(device_refused, 1, "counted as the device's refusal"); + assert_eq!(moved(&before, &after), [0, 1, 0, 0, 0], "a copy, no import"); + assert_eq!(announced(&heard), 1, "announced"); + lies_in_a_copy(&device, &buffer, &host); + } + false => { + assert_eq!(device_refused, 0); + assert_eq!(buffer.host_backing(), Some(HostBacking::ImportedByPointer)); + } + } +} + #[test] fn copied_buffer_is_made_over_a_dma_buf_mapping_the_gate_refuses() { let Some(host) = Host::over_dma_buf(9) else { diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 16324ddce..94267fdee 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -336,6 +336,7 @@ pub(crate) struct Counters { host_imports_refused_by_host: AtomicU64, copied_host_buffers_made: AtomicU64, copied_host_buffers_refused: AtomicU64, + copied_host_buffers_device_refused: AtomicU64, copied_host_buffers_degraded: AtomicU64, copied_rebind_pages_moved: AtomicU64, copied_read_only_memory: AtomicU64, @@ -2916,6 +2917,7 @@ impl Counters { match cause { CopiedBecause::Failed => &self.copied_host_buffers_degraded, CopiedBecause::Refused => &self.copied_host_buffers_refused, + CopiedBecause::DeviceRefused => &self.copied_host_buffers_device_refused, CopiedBecause::Moved => &self.copied_rebind_pages_moved, CopiedBecause::ReadOnly => &self.copied_read_only_memory, CopiedBecause::Unwritable => &self.copied_unwritable_pages, @@ -6322,10 +6324,14 @@ pub struct WorkCounts { pub host_imports_refused_by_host: u64, /// The copied host buffers made (`copied-host-pages`): no-copy buffers whose bytes the GPU /// uses in a copy of the caller's pages in memory of the device's own, which no import maps. - /// Each is one of the next five. + /// Each is one of the next six. pub copied_host_buffers_made: u64, /// Of those, the ones made because the caller's gate refused the import. pub copied_host_buffers_refused: u64, + /// Of those, the ones made because the device refused the pointer's import and the caller + /// gave no source of dma-bufs of the pages (`HostMemory::with_dma_buf`): RADV refuses shared + /// memory, a `memfd` mapping among it (`Device::imports_shared_host_pointers`). + pub copied_host_buffers_device_refused: u64, /// Of those, the ones made because an import the device and the gate admitted failed (out /// of memory, another Vulkan error), or because the runtime refused the pages for the driver /// and no dma-buf of them served (`host_imports_unpinnable`). @@ -8062,6 +8068,9 @@ impl Device { copied_host_buffers_refused: counters .copied_host_buffers_refused .load(Ordering::Relaxed), + copied_host_buffers_device_refused: counters + .copied_host_buffers_device_refused + .load(Ordering::Relaxed), copied_host_buffers_degraded: counters .copied_host_buffers_degraded .load(Ordering::Relaxed), @@ -8469,6 +8478,8 @@ impl Device { pub(crate) enum CopiedBecause { /// The caller's gate refused their import (`HostMemory::with_import_gate`). Refused, + /// The device refused their pointer's import, and no source of dma-bufs of them was given. + DeviceRefused, /// The import the device and the gate admitted failed. Failed, /// A rebind's new bytes lie at another offset in their page than the old, where a texture diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 6e0a6ae3a..6532265f2 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2619,6 +2619,7 @@ field metal2vulkan-runtime::WorkCounts.copied_copy_ins_unused pub copied_copy_in field metal2vulkan-runtime::WorkCounts.copied_dropped_bytes pub copied_dropped_bytes: u64 field metal2vulkan-runtime::WorkCounts.copied_heaps_made pub copied_heaps_made: u64 field metal2vulkan-runtime::WorkCounts.copied_host_buffers_degraded pub copied_host_buffers_degraded: u64 +field metal2vulkan-runtime::WorkCounts.copied_host_buffers_device_refused pub copied_host_buffers_device_refused: u64 field metal2vulkan-runtime::WorkCounts.copied_host_buffers_made pub copied_host_buffers_made: u64 field metal2vulkan-runtime::WorkCounts.copied_host_buffers_refused pub copied_host_buffers_refused: u64 field metal2vulkan-runtime::WorkCounts.copied_host_buffers_shared pub copied_host_buffers_shared: u64 diff --git a/docs/HOWTO.md b/docs/HOWTO.md index 69625838d..7b5dbbb7b 100644 --- a/docs/HOWTO.md +++ b/docs/HOWTO.md @@ -197,8 +197,11 @@ under 20 seconds and 500 MiB per attempt. (`VK_EXT_external_memory_host`). Some drivers refuse the pointer of shared memory: RADV (AMD on Linux) imports a pointer through amdgpu's userptr, which takes anonymous pages only, so it refuses `MAP_SHARED` mappings of a `memfd`, a shared memory object or any file (QEMU's -`memory-backend-memfd,share=on` guest RAM among them) with `Unsupported`, `HostMemory`. NVIDIA and -lavapipe import the same memory. +`memory-backend-memfd,share=on` guest RAM among them). NVIDIA and lavapipe import the same memory. +A no-copy buffer or heap over memory the device so refuses, made with no source of dma-bufs of its +pages (below), is a copy of the pages, announced ([Copied host buffers](#copied-host-buffers), +`WorkCounts::copied_host_buffers_device_refused`); a rebind to such memory without a source is +refused with `Unsupported`, `HostMemory`. No driver that pins imported pages imports a mapping the kernel cannot pin (`VM_IO` or `VM_PFNMAP`: a mapping of a dma-buf). NVIDIA's driver fails such an import as out of device memory and then fails @@ -420,6 +423,9 @@ happens where: for a buffer or rebind that lies in a live import or copy of the same bytes, memory whose GPU writes Metal's private option bit 24 drops, a rebind whose bytes moved in their page under a texture (below), or where the device refused the pointer before it; +- the device refuses the pointer of a no-copy buffer or heap made with no source of dma-bufs of its + pages: RADV refuses shared memory, a `memfd` mapping among it (`Device::imports_shared_host_pointers`, + `copied_host_buffers_device_refused`); a rebind to such memory is refused instead; - an import the device and the gate admitted fails, of the pointer or of the dma-bufs of the caller's source (out of memory, another Vulkan error: `copied_host_buffers_degraded`); - on NVIDIA's driver, the pages are a mapping the kernel cannot pin (a dma-buf's), which the