diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs index cc3de34b7..2f64d881f 100644 --- a/crates/metal2vulkan-runtime/src/memory.rs +++ b/crates/metal2vulkan-runtime/src/memory.rs @@ -913,20 +913,25 @@ pub(super) fn placement_order( } /// The memory types an import of a dma-buf takes among `types` (the bits of `allowed`, the types -/// both the dma-buf and what is bound in it take), in the order they are tried: those the host -/// does not map first, then those it maps, each in [`dma_buf_class_order`]'s order. The runtime -/// never maps such an import (the CPU reaches the pages through the caller's own mapping of -/// them), so a type the host does not map serves it as well, and a driver's refused import as a -/// type the host maps can break the device's later allocations: NVIDIA's 620.70.06 on an RTX -/// 3090 lists a udmabuf's types as its type 0 (system memory, no property flag) and its type 4 -/// (host-visible, coherent and cached), answers `ERROR_OUT_OF_DEVICE_MEMORY` for the import into -/// type 4, with a dedicated allocation or without, and imports it as type 0; once it refused an -/// import into type 4, it answers `ERROR_OUT_OF_DEVICE_MEMORY` to every allocation of its +/// both the dma-buf and what is bound in it take), in the order they are tried: +/// [`dma_buf_class_order`]'s, but the system-memory types the host maps (`HOST_VISIBLE` without +/// `DEVICE_LOCAL`, [`mapped_system_memory`]) after every other. The runtime never maps such an +/// import (the CPU reaches the pages through the caller's own mapping of them), so a +/// system-memory type the host does not map serves it as well, and a driver's refused import as +/// one it maps can break the device's later allocations: NVIDIA's 620.70.06 on an RTX 3090 lists +/// a udmabuf's types as its type 0 (system memory, no property flag) and its type 4 (system +/// memory, host-visible, coherent and cached), answers `ERROR_OUT_OF_DEVICE_MEMORY` for the import +/// into type 4, with a dedicated allocation or without, and imports it as type 0; once it refused +/// an import into type 4, it answers `ERROR_OUT_OF_DEVICE_MEMORY` to every allocation of its /// device-local type 1 exportable as an opaque file descriptor or a dma-buf, of 12,441,600 bytes /// and of 64 MiB, for the device's life, where it made each before (measured, /// `tests::export_after_dma_buf`), and the runtime's exportable buffers overflowed into system -/// memory. A driver that lists only types the host maps for a dma-buf (RADV's, ANV's and -/// lavapipe's udmabufs) takes the class's order as before. +/// memory. A device-local type keeps the class's place whatever the host's flags on it: on a +/// unified-memory GPU they may choose how the GPU caches its accesses to the pages (ANV's +/// coherent cached type, which the class's order takes first for `Shared` memory on an Iris Xe, +/// Mesa 26.2.x, where its type of `DEVICE_LOCAL` alone would come first otherwise). So ANV's, +/// RADV's and lavapipe's imports, whose types are device-local or all host-mapped, take the +/// class's order as before. pub(super) fn dma_buf_candidates( types: &[vk::MemoryType], allowed: u32, @@ -935,14 +940,14 @@ pub(super) fn dma_buf_candidates( ) -> Vec { let mut ranked = dma_buf_class_order(types, allowed, class, unified); // Stable: each group keeps the class's order. - ranked.sort_by_key(|&index| host_mapped(types, index)); + ranked.sort_by_key(|&index| mapped_system_memory(types, index)); ranked } /// The memory types of `types` among `allowed` a dma-buf's import takes, in its class's order: /// the [`candidates`] of `class` first, then every other type of `allowed` the runtime may /// allocate ([`allocatable`]) that is neither protected nor lazily allocated, in Vulkan's order -/// ([`dma_buf_candidates`] tries those the host does not map among them first). +/// ([`dma_buf_candidates`] tries the system-memory types the host maps among them last). pub(super) fn dma_buf_class_order( types: &[vk::MemoryType], allowed: u32, @@ -958,15 +963,18 @@ pub(super) fn dma_buf_class_order( ranked } -/// Whether the host maps memory of type `index` of `types` (`HOST_VISIBLE`). -fn host_mapped(types: &[vk::MemoryType], index: u32) -> bool { - (types[index as usize].property_flags).contains(vk::MemoryPropertyFlags::HOST_VISIBLE) +/// Whether memory of type `index` of `types` is system memory the host maps (`HOST_VISIBLE` +/// without `DEVICE_LOCAL`). +fn mapped_system_memory(types: &[vk::MemoryType], index: u32) -> bool { + use vk::MemoryPropertyFlags as P; + let flags = types[index as usize].property_flags; + flags.contains(P::HOST_VISIBLE) && !flags.contains(P::DEVICE_LOCAL) } /// A dma-buf import's attempts, each a form (dedicated to what it backs, or not) and a type: the -/// types the host does not map among `ranked` (a [`dma_buf_candidates`] order) in each of the -/// `dedications` in turn, then those it maps, so that no form is tried as a type the host maps -/// while a type it does not map is left untried ([`dma_buf_candidates`]). +/// types of `ranked` (a [`dma_buf_candidates`] order) other than the system-memory ones the host +/// maps in each of the `dedications` in turn, then those, so that no form is tried as a +/// system-memory type the host maps while another type is left untried ([`dma_buf_candidates`]). pub(super) fn dma_buf_attempts( types: &[vk::MemoryType], ranked: &[u32], @@ -980,7 +988,7 @@ pub(super) fn dma_buf_attempts( }) .collect(); // Stable: the forms keep their order within each group. - attempts.sort_by_key(|&(_, type_index)| host_mapped(types, type_index)); + attempts.sort_by_key(|&(_, type_index)| mapped_system_memory(types, type_index)); attempts } @@ -4422,10 +4430,10 @@ pub(super) enum DmaBufFor { } /// Import the dma-buf `fd`, `size` bytes, as memory of `memory`'s best type among `allowed` (the -/// types both the dma-buf and what is bound in it take) for `class`, those the host does not map -/// first, then those it maps, each in the class's order ([`dma_buf_candidates`]), in each form -/// `purpose` takes ([`DmaBufFor`]), every form of the first before any of the second -/// ([`dma_buf_attempts`]), but the form and type the device's last import of the same kind (what it +/// types both the dma-buf and what is bound in it take) for `class`, in the class's order but the +/// system-memory types the host maps last ([`dma_buf_candidates`]), in each form `purpose` takes +/// ([`DmaBufFor`]), every form of the other types before any of those ([`dma_buf_attempts`]), +/// but the form and type the device's last import of the same kind (what it /// backs, and its class) succeeded as first ([`worked_first`]): the memory, its type and whether /// it is dedicated to its buffer ([`DmaBufFor::Buffer`]), the caller's to free (`dma_buf.rs`'s /// `DmaBufPages`), in no block and never mapped. Allocated with the device-address flag where the diff --git a/crates/metal2vulkan-runtime/src/tests/export_after_dma_buf.rs b/crates/metal2vulkan-runtime/src/tests/export_after_dma_buf.rs index 68779b87a..7e3d2dc85 100644 --- a/crates/metal2vulkan-runtime/src/tests/export_after_dma_buf.rs +++ b/crates/metal2vulkan-runtime/src/tests/export_after_dma_buf.rs @@ -19,12 +19,12 @@ //! //! The driver's behaviour behind it, as evidence (`EXPORT-AFTER-DMA-BUF-RAW`, printed, not //! asserted: a driver may change it): NVIDIA's 620.70.06 on an RTX 3090, once it refused an -//! import of the udmabuf into a type the host maps (its type 4, `ERROR_OUT_OF_DEVICE_MEMORY`), -//! answers `ERROR_OUT_OF_DEVICE_MEMORY` to every exportable allocation of its device-local type, -//! for the device's life, where its successful import into type 0 leaves them made. So the -//! runtime's imports try the types the host does not map first (`memory::dma_buf_candidates`), -//! and `DMA-BUF-IMPORT-TYPES` prints where that order puts each class's import on the device under -//! test, against the class's order alone. +//! import of the udmabuf into a system-memory type the host maps (its type 4, +//! `ERROR_OUT_OF_DEVICE_MEMORY`), answers `ERROR_OUT_OF_DEVICE_MEMORY` to every exportable +//! allocation of its device-local type, for the device's life, where its successful import into +//! type 0 leaves them made. So the runtime's imports try the system-memory types the host maps +//! last (`memory::dma_buf_candidates`), and `DMA-BUF-IMPORT-TYPES` prints where that order puts +//! each class's import on the device under test, against the class's order alone. //! //! A device test: compiled only under the GPU safety cfgs (`m2v_gpu`, `m2v_cpu_icd`), on Linux. @@ -551,8 +551,8 @@ fn dma_buf_buffer_types(device: &Device, bytes: u64) -> u32 { bits } -/// Where the runtime's order of a dma-buf import's types (those the host does not map first, -/// `memory::dma_buf_candidates`) puts a buffer's import of each class, against the class's order +/// Where the runtime's order of a dma-buf import's types (the system-memory types the host maps +/// last, `memory::dma_buf_candidates`) puts a buffer's import of each class, against the class's order /// alone (`memory::dma_buf_class_order`, the order before): for a scattered udmabuf and for a /// dma-buf of the device's own device-local memory, the types the driver lists and a buffer /// takes, both orders, the type the runtime's import took (`memory::import_dma_buf`, dedicated @@ -648,7 +648,7 @@ fn the_type_each_class_s_dma_buf_import_takes_against_the_class_s_order() { .find(|&index| raw_import(&device, fd, bytes, index) == 0); eprintln!( "{TYPES} on {}: {source}, {class:?}: allowed {allowed:#b}; class order {before:?}, \ - unmapped first {after:?}; the runtime's import took {:?}, the class's order takes \ + runtime order {after:?}; the runtime's import took {:?}, the class's order takes \ {:?}", device.name(), took.map(|index| (index, flags(index))), diff --git a/crates/metal2vulkan-runtime/src/tests/memory.rs b/crates/metal2vulkan-runtime/src/tests/memory.rs index 775b57d90..abc08437b 100644 --- a/crates/metal2vulkan-runtime/src/tests/memory.rs +++ b/crates/metal2vulkan-runtime/src/tests/memory.rs @@ -57,16 +57,18 @@ fn classes_pick_their_memory_types_best_first() { ); } -/// An import of a dma-buf tries the types the host does not map first, then those it maps, each -/// in its class's order: the class's types first, then every other type the dma-buf and what is -/// bound in it take that the runtime may allocate and that is neither protected nor lazily -/// allocated, in Vulkan's order. With an RTX 3090's types (NVIDIA's 620.70.06), whose udmabuf -/// lists types 0 and 4, a `Shared` import tries type 0, system memory with no property flag, -/// which that driver imports a udmabuf as, before type 4, the class's, whose refused import -/// breaks the device's exportable device-local allocations. Each form of an import tries a type -/// the host does not map before any form tries one it maps. +/// An import of a dma-buf tries its class's order (the class's types first, then every other +/// type the dma-buf and what is bound in it take that the runtime may allocate and that is +/// neither protected nor lazily allocated, in Vulkan's order), but the system-memory types the +/// host maps (host-visible, not device-local) last. With an RTX 3090's types (NVIDIA's +/// 620.70.06), whose udmabuf lists types 0 and 4, a `Shared` import tries type 0, system memory +/// with no property flag, which that driver imports a udmabuf as, before type 4, the class's, +/// whose refused import breaks the device's exportable device-local allocations. Each form of an +/// import tries the other types before any form tries a system-memory type the host maps. Where +/// the types are all device-local (ANV's on an Iris Xe) or all host-mapped system memory (RADV's +/// for a udmabuf), the class's order stands. #[test] -fn dma_buf_imports_try_the_types_the_host_does_not_map_first() { +fn dma_buf_imports_try_the_system_memory_types_the_host_maps_last() { use vk::MemoryPropertyFlags as P; let host = P::HOST_VISIBLE | P::HOST_COHERENT; let nvidia = [ @@ -85,22 +87,22 @@ fn dma_buf_imports_try_the_types_the_host_does_not_map_first() { dma_buf_candidates(&nvidia, 0x11, MemoryClass::Shared, false), [0, 4] ); - // The class's own order first, the others after it in Vulkan's; then the types the host - // does not map before those it maps, each group in that order. + // The class's own order first, the others after it in Vulkan's; then the system-memory types + // the host maps (3 and 4) after the rest, each group in that order. assert_eq!( dma_buf_class_order(&nvidia, 0x3f, MemoryClass::Shared, false), [4, 5, 3, 0, 1, 2] ); assert_eq!( dma_buf_candidates(&nvidia, 0x3f, MemoryClass::Shared, false), - [0, 1, 2, 4, 5, 3] + [5, 0, 1, 2, 4, 3] ); - // Types 0 to 2 are the ones the host does not map. let private = dma_buf_class_order(&nvidia, 0x3f, MemoryClass::Private, false); - let (unmapped, mapped): (Vec, Vec) = private.iter().partition(|&&index| index < 3); + let (others, mapped): (Vec, Vec) = + private.iter().partition(|&&index| index != 3 && index != 4); assert_eq!( dma_buf_candidates(&nvidia, 0x3f, MemoryClass::Private, false), - [unmapped, mapped].concat() + [others, mapped].concat() ); assert!(dma_buf_candidates(&nvidia, 0, MemoryClass::Shared, false).is_empty()); // A buffer's import, dedicated to nothing and then to the buffer: both forms as type 0 @@ -110,31 +112,39 @@ fn dma_buf_imports_try_the_types_the_host_does_not_map_first() { dma_buf_attempts(&nvidia, &ranked, &[false, true]), [(false, 0), (true, 0), (false, 4), (true, 4)] ); - // Where every type the dma-buf lists is one the host maps (RADV's, ANV's and lavapipe's - // udmabufs), the class's order and the forms' stand. - let mapped = [ + // Where every type the dma-buf lists is device-local (ANV's on an Iris Xe: 0x1, 0x7, 0xf, + // unified) or host-mapped system memory (RADV's for a udmabuf on a discrete GPU), the + // class's order and the forms' stand. + let anv = [ + ty(P::DEVICE_LOCAL), + ty(P::DEVICE_LOCAL | host), + ty(P::DEVICE_LOCAL | host | P::HOST_CACHED), + ]; + let gtt = [ ty(P::DEVICE_LOCAL), ty(host), ty(P::DEVICE_LOCAL | host), ty(host | P::HOST_CACHED), ]; - for class in [ - MemoryClass::Shared, - MemoryClass::WriteCombined, - MemoryClass::Private, - MemoryClass::Managed, - ] { - let ranked = dma_buf_candidates(&mapped, 0b1110, class, false); - assert_eq!( - ranked, - dma_buf_class_order(&mapped, 0b1110, class, false), - "{class:?}" - ); - let forms: Vec<(bool, u32)> = [false, true] - .into_iter() - .flat_map(|dedicated| ranked.iter().map(move |&index| (dedicated, index))) - .collect(); - assert_eq!(dma_buf_attempts(&mapped, &ranked, &[false, true]), forms); + for (types, allowed, unified) in [(&anv[..], 0b111, true), (>t[..], 0b1010, false)] { + for class in [ + MemoryClass::Shared, + MemoryClass::WriteCombined, + MemoryClass::Private, + MemoryClass::Managed, + ] { + let ranked = dma_buf_candidates(types, allowed, class, unified); + assert_eq!( + ranked, + dma_buf_class_order(types, allowed, class, unified), + "{class:?}" + ); + let forms: Vec<(bool, u32)> = [false, true] + .into_iter() + .flat_map(|dedicated| ranked.iter().map(move |&index| (dedicated, index))) + .collect(); + assert_eq!(dma_buf_attempts(types, &ranked, &[false, true]), forms); + } } // Never a protected, lazily allocated or AMD device-coherent type. let others = [ diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index cba7073cd..aac0f2771 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -1709,9 +1709,11 @@ driver's. Each is open work. descriptor or a dma-buf, as out of device memory, for the device's life, where it made each before and makes them after a successful import as 0 (measured on an RTX 3090, `tests::export_after_dma_buf`): an exportable buffer then overflowed into system memory. So a - dma-buf import tries the memory types the host does not map before those it maps, in every - form (`memory.rs`'s `dma_buf_candidates`; the runtime maps no dma-buf import), and NVIDIA's - udmabuf imports never try type 4 where type 0 takes them. 615.71.09 also refuses + dma-buf import tries the system-memory types the host maps (host-visible, not device-local) + after every other type, in every form (`memory.rs`'s `dma_buf_candidates`; the runtime maps no + dma-buf import), and NVIDIA's udmabuf imports never try type 4 where type 0 takes them. + Device-local types keep their class's order, so ANV's, RADV's and lavapipe's imports are as + before. 615.71.09 also refuses most udmabufs of scattered pages dedicated to nothing, of 16 KiB to 63 MiB, and takes them dedicated to their buffer (measured on an RTX 5080 Laptop GPU: 4346 of the leak probe's 6000 imports). So an import tries first the form and type its kind (what it backs, and its class)