dma-buf imports try the types the host does not map first

NVIDIA's 620.70.06 lists a udmabuf of host pages as its type 0 (system memory, no property
flag) and its type 4 (host-visible, coherent and cached, the Shared class's first), refuses
every import as type 4 with ERROR_OUT_OF_DEVICE_MEMORY, and imports it as type 0. Measured on
an RTX 3090 (tests::export_after_dma_buf, raw vkAllocateMemory answers on fresh devices): once
it refused an import as type 4, it answers ERROR_OUT_OF_DEVICE_MEMORY to every allocation of
its device-local type 1 exportable as an opaque fd or a dma-buf (with the device-address flag
or without), at 12,441,600 bytes and at 64 MiB, for the device's life; plain and
device-address-only allocations are still made. After a successful import as type 0, every
chain is made. Whether the import was dedicated does not matter. The runtime's first Shared
dma-buf import tried type 4 first, so every later Private exportable buffer overflowed into
system memory (type 0, DeviceMemoryOverflow).

The runtime never maps a dma-buf import (the CPU reaches the pages through the caller's own
mapping), so a type the host does not map serves it as well. dma_buf_candidates now orders the
types the host does not map first, then those it maps, each in the class's order
(dma_buf_class_order, the order before), and dma_buf_attempts tries every form (dedicated to
nothing, then to the buffer) of the first group before any form of the second. On NVIDIA the
import ends as type 0, as before, without the refused attempt. A driver that lists only
host-mapped types for a dma-buf keeps the class's order.

Tests:
- memory::dma_buf_imports_try_the_types_the_host_does_not_map_first (unit): both orders, and the
  attempts' order, for NVIDIA's types and for a set whose listed types are all host-mapped.
- export_after_dma_buf::a_private_exportable_buffer_after_a_dma_buf_import_lies_in_device_local_memory
  (renamed): asserts each export lies in a DEVICE_LOCAL type with the control's flags and no
  overflow counted. It failed on the RTX 3090 before this change (type 0, one overflow each).
- export_after_dma_buf::the_driver_s_answers_after_each_kind_of_dma_buf_import: kept as the
  driver's evidence, printed and not asserted.
- export_after_dma_buf::the_type_each_class_s_dma_buf_import_takes_against_the_class_s_order:
  prints, per class, the type the runtime's import takes and the type the class's order took,
  for a udmabuf and for a dma-buf of the device's own device-local memory; asserts they agree
  where both orders are the same.
docs/FAST-PATHS.md's NVIDIA dma-buf gap records the driver's refusal and the order.
This commit is contained in:
Anees Iqbal committed 2026-11-11 21:20:02 -08:00
1 parent 9cae29397c
commit c267e94e74
4 files changed
+370 -55

No files matched your search

+68 -21
View File
@@ -913,20 +913,41 @@ 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: 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. 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, where the driver imports the dma-buf as no type of the
/// class: 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.
/// 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
/// 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.
pub(super) fn dma_buf_candidates(
types: &[vk::MemoryType],
allowed: u32,
class: MemoryClass,
unified: bool,
) -> Vec<u32> {
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
}
/// 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).
pub(super) fn dma_buf_class_order(
types: &[vk::MemoryType],
allowed: u32,
class: MemoryClass,
unified: bool,
) -> Vec<u32> {
let mut ranked = candidates(types, allowed, class, unified);
let importable = importable_types(types);
@@ -937,6 +958,32 @@ pub(super) fn dma_buf_candidates(
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)
}
/// 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`]).
pub(super) fn dma_buf_attempts(
types: &[vk::MemoryType],
ranked: &[u32],
dedications: &[bool],
) -> Vec<(bool, u32)> {
let mut attempts: Vec<(bool, u32)> = (dedications.iter())
.flat_map(|&dedicated| {
ranked
.iter()
.map(move |&type_index| (dedicated, type_index))
})
.collect();
// Stable: the forms keep their order within each group.
attempts.sort_by_key(|&(_, type_index)| host_mapped(types, type_index));
attempts
}
/// `ranked` (a dma-buf import's attempts, each a form and a type, [`import_dma_buf`]) with
/// `worked`, the attempt the device's last import of its kind succeeded as, moved first where it
/// is among them; the others keep their order, so an attempt refused once is still tried.
@@ -4375,9 +4422,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 of the class first and
/// then the dma-buf's others ([`dma_buf_candidates`]), in each form `purpose` takes
/// ([`DmaBufFor`]), 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`, 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
/// 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
@@ -4394,12 +4442,13 @@ pub(super) enum DmaBufFor {
///
/// The form and type that worked go first because a driver may list a type for the dma-buf that
/// it then refuses, or refuse a form: NVIDIA lists a udmabuf of host pages as types 0 and 4
/// (host-visible, coherent and cached, the class's first) and refuses 4 as out of device memory
/// (620.70.06 on an RTX 2080 and an RTX 3090, measured), and 615.71.09 refuses most udmabufs of
/// scattered pages dedicated to nothing, of 16 KiB to 63 MiB, and imports them dedicated to their
/// buffer (measured on an RTX 5080 Laptop GPU: 4346 of the leak probe's 6000 imports). So every
/// import made a failed import first, and a failed import there is where the driver's faults lie
/// (below). Every form and type stays among those tried, so a refusal that passes is tried again.
/// (host-visible, coherent and cached, the class's first, tried after 0) and refuses 4 as out of
/// device memory (620.70.06 on an RTX 2080 and an RTX 3090, measured), and 615.71.09 refuses most
/// udmabufs of scattered pages dedicated to nothing, of 16 KiB to 63 MiB, and imports them
/// dedicated to their buffer (measured on an RTX 5080 Laptop GPU: 4346 of the leak probe's 6000
/// imports). So an import may make a failed import first, and a failed import there is where the
/// driver's faults lie (below). Every form and type stays among those tried, so a refusal that
/// passes is tried again.
///
/// An import of a kind that no import has yet succeeded as on the device holds the device's lock
/// of such first imports through its attempts (`Memory::dma_buf_first_imports`), so those imports,
@@ -4442,9 +4491,7 @@ pub(super) fn import_dma_buf(
let (ranked, device_address, first_imports) = {
let memory = lock(memory);
let types = dma_buf_candidates(&memory.types, allowed, class, memory.unified);
let ranked: Vec<(bool, u32)> = (dedications.iter())
.flat_map(|&dedicated| types.iter().map(move |&type_index| (dedicated, type_index)))
.collect();
let ranked = dma_buf_attempts(&memory.types, &types, dedications);
let worked = memory.dma_buf_imported_as.get(&kind).copied();
let first_imports = worked
.is_none()
@@ -13,9 +13,18 @@
//! and after the exports (the runtime reads neither: a placement overflows only where the
//! driver's `vkAllocateMemory` answers out of memory for every device-local type).
//!
//! It prints its findings (`EXPORT-AFTER-DMA-BUF`) and asserts only that the exports' memory is
//! where the control's is. Where `/dev/udmabuf` cannot be opened, or the device exports no such
//! memory, it says so and passes.
//! It prints its findings (`EXPORT-AFTER-DMA-BUF`) and asserts that each export lies in a
//! device-local memory type with the control's flags, no overflow counted. Where `/dev/udmabuf`
//! cannot be opened, or the device exports no such memory, it says so and passes.
//!
//! 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.
//!
//! A device test: compiled only under the GPU safety cfgs (`m2v_gpu`, `m2v_cpu_icd`), on Linux.
@@ -298,7 +307,7 @@ fn raw_import(device: &Device, dma_buf: &OwnedFd, size: u64, type_index: u32) ->
}
/// The memory types `dma_buf` may be imported as, as bits (`vkGetMemoryFdPropertiesKHR`).
fn dma_buf_types(device: &Device, dma_buf: &OwnedFd) -> u32 {
fn dma_buf_types(device: &Device, dma_buf: &OwnedFd) -> Result<u32, vk::Result> {
// SAFETY: as in `budgets`.
let handles = unsafe { device.raw() };
let loader = ash::khr::external_memory_fd::Device::new(handles.instance, handles.device);
@@ -311,8 +320,7 @@ fn dma_buf_types(device: &Device, dma_buf: &OwnedFd) -> u32 {
&mut properties,
)
}
.unwrap();
properties.memory_type_bits
.map(|()| properties.memory_type_bits)
}
/// A udmabuf of [`PAGES`] scattered pages of a sealed `memfd` of [`FILE_PAGES`], as the probe's.
@@ -344,12 +352,16 @@ fn scattered_udmabuf(page: u64) -> OwnedFd {
}
/// What a device answers after each kind of dma-buf import, each on a device of its own: none
/// (the control); the test's own import of a scattered udmabuf as the type the driver last imports
/// it as, and as each other type it lists; the runtime's no-copy buffer over the udmabuf's mapping
/// (the probe), as it imports it, and with its import dedicated to nothing refused (a test's
/// stand-in), so it is dedicated to the buffer. After each: the driver's answers to device-local
/// allocations of a pooled block's bytes and of the exportable buffer's, of each chain, and where
/// the runtime places the exportable buffers. It prints them (`EXPORT-AFTER-DMA-BUF-RAW`).
/// (the control); the test's own import of a scattered udmabuf as each type the driver lists for
/// it; the runtime's no-copy buffer over the udmabuf's mapping (the probe), as it imports it, and
/// with its import dedicated to nothing refused (a test's stand-in), so it is dedicated to the
/// buffer. After each: the driver's answers to device-local allocations of a pooled block's bytes
/// and of the exportable buffer's, of each chain, and where the runtime places the exportable
/// buffers. It prints them (`EXPORT-AFTER-DMA-BUF-RAW`) and asserts nothing: it is the evidence
/// of a driver's behaviour, which a driver may change. Measured on NVIDIA's 620.70.06 on an RTX
/// 3090: after a refused import as type 4 (host-visible) every exportable chain answered
/// `ERROR_OUT_OF_DEVICE_MEMORY` (-2) at both sizes, the plain and device-address ones made; after
/// a successful import as type 0, every chain was made.
#[test]
fn the_driver_s_answers_after_each_kind_of_dma_buf_import() {
const RAW: &str = "EXPORT-AFTER-DMA-BUF-RAW";
@@ -384,7 +396,7 @@ fn the_driver_s_answers_after_each_kind_of_dma_buf_import() {
// The test's own imports, one type at a time, each on a device of its own.
let probe_device = device(&scratch.0);
let udmabuf = scattered_udmabuf(page);
let types = dma_buf_types(&probe_device, &udmabuf);
let types = dma_buf_types(&probe_device, &udmabuf).unwrap();
drop(probe_device);
eprintln!("{RAW}: the udmabuf's types {types:#b}");
for type_index in (0..32).filter(|at| types & (1 << at) != 0) {
@@ -416,8 +428,11 @@ fn the_driver_s_answers_after_each_kind_of_dma_buf_import() {
);
}
/// After the runtime's import of a dma-buf (the probe's), a `Private` buffer exportable as an
/// opaque file descriptor and as a dma-buf lies in a device-local type, as on a device that
/// imported none, with no overflow counted.
#[test]
fn a_private_exportable_buffer_after_a_dedicated_dma_buf_import_lies_where_it_would_without() {
fn a_private_exportable_buffer_after_a_dma_buf_import_lies_in_device_local_memory() {
if !udmabuf_openable(TEST) {
return;
}
@@ -450,9 +465,203 @@ fn a_private_exportable_buffer_after_a_dedicated_dma_buf_import_lies_where_it_wo
{live}; heaps before {before:?}, after its free {after_free:?}, after the exports \
{after:?}; opaque fd {fd:?}; dma-buf {dma_buf:?}"
);
assert_eq!(fd.map(|found| found.1), control_fd.map(|found| found.1));
assert_eq!(
dma_buf.map(|found| found.1),
control_dma_buf.map(|found| found.1)
);
let local = vk::MemoryPropertyFlags::DEVICE_LOCAL.as_raw();
for (export, found, control) in [
("opaque fd", fd, control_fd),
("dma-buf", dma_buf, control_dma_buf),
] {
assert_eq!(found.is_some(), control.is_some(), "{export}: exported");
let (Some(found), Some(control)) = (found, control) else {
continue;
};
assert_eq!(found.1, control.1, "{export}: the control's type flags");
assert_ne!(found.1 & local, 0, "{export}: a device-local type");
assert_eq!(found.3, 0, "{export}: no overflow");
}
}
/// A dma-buf the device exports of `bytes` bytes of its device-local memory (a type of
/// `DEVICE_LOCAL` alone, else the first with it), with the memory, which the caller frees; `None`
/// where the driver refuses either.
fn device_local_dma_buf(device: &Device, bytes: u64) -> Option<(OwnedFd, vk::DeviceMemory)> {
// SAFETY: as in `budgets`.
let handles = unsafe { device.raw() };
// SAFETY: the physical device is the instance's.
let memory = unsafe {
handles
.instance
.get_physical_device_memory_properties(handles.physical_device)
};
let local = device_local_type(device).or_else(|| {
(0..memory.memory_type_count).find(|&at| {
(memory.memory_types[at as usize].property_flags)
.contains(vk::MemoryPropertyFlags::DEVICE_LOCAL)
})
})?;
let mut exportable = vk::ExportMemoryAllocateInfo::default()
.handle_types(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT);
let mut flags =
vk::MemoryAllocateFlagsInfo::default().flags(vk::MemoryAllocateFlags::DEVICE_ADDRESS);
let info = vk::MemoryAllocateInfo::default()
.allocation_size(bytes)
.memory_type_index(local)
.push_next(&mut exportable)
.push_next(&mut flags);
// SAFETY: a valid allocation of the device's type, freed by the caller.
let made = unsafe { handles.device.allocate_memory(&info, None) }.ok()?;
let loader = ash::khr::external_memory_fd::Device::new(handles.instance, handles.device);
let info = vk::MemoryGetFdInfoKHR::default()
.memory(made)
.handle_type(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT);
// SAFETY: memory just made, exportable as a dma-buf.
match unsafe { loader.get_memory_fd(&info) } {
// SAFETY: the driver hands the descriptor over.
Ok(fd) => Some((
unsafe { <OwnedFd as std::os::fd::FromRawFd>::from_raw_fd(fd) },
made,
)),
Err(_) => {
// SAFETY: just made, used by nothing.
unsafe { handles.device.free_memory(made, None) };
None
}
}
}
/// The memory types a buffer of `bytes` bytes, importable from a dma-buf, takes, as bits.
fn dma_buf_buffer_types(device: &Device, bytes: u64) -> u32 {
// SAFETY: as in `budgets`.
let handles = unsafe { device.raw() };
let mut external = vk::ExternalMemoryBufferCreateInfo::default()
.handle_types(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT);
let info = vk::BufferCreateInfo::default()
.size(bytes)
.usage(
vk::BufferUsageFlags::TRANSFER_SRC
| vk::BufferUsageFlags::TRANSFER_DST
| vk::BufferUsageFlags::STORAGE_BUFFER,
)
.push_next(&mut external);
// SAFETY: a valid buffer, destroyed at once.
let buffer = unsafe { handles.device.create_buffer(&info, None) }.unwrap();
// SAFETY: the buffer just made.
let bits = unsafe { handles.device.get_buffer_memory_requirements(buffer) }.memory_type_bits;
// SAFETY: just made, bound to nothing.
unsafe { handles.device.destroy_buffer(buffer, None) };
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
/// 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
/// to nothing) and the first type of the class's order the driver imports the dma-buf as (the
/// test's own imports, made after the runtime's, so that a refusal among them reaches none of
/// the runtime's), each with its flags. It prints them (`DMA-BUF-IMPORT-TYPES`), and asserts that
/// where both orders are the same the runtime's import took the type the class's order took.
#[test]
fn the_type_each_class_s_dma_buf_import_takes_against_the_class_s_order() {
const TYPES: &str = "DMA-BUF-IMPORT-TYPES";
use crate::memory::{
dma_buf_candidates, dma_buf_class_order, import_dma_buf, DmaBufFor, MemoryClass,
};
let scratch = Scratch::new();
let device = device(&scratch.0);
let page = device
.shared()
.host_import
.as_ref()
.map_or(4096, |import| import.page);
let bytes = PAGES * page;
// SAFETY: as in `budgets`.
let handles = unsafe { device.raw() };
// SAFETY: the physical device is the instance's.
let properties = unsafe {
handles
.instance
.get_physical_device_memory_properties(handles.physical_device)
};
let types = &properties.memory_types[..properties.memory_type_count as usize];
let unified = device.properties().has_unified_memory;
let flags = |index: u32| types[index as usize].property_flags;
let mut sources = Vec::new();
// The device's own dma-buf first: no import was refused yet when it is made.
let own = device_local_dma_buf(&device, bytes);
match &own {
Some((fd, _)) => sources.push(("device-local", fd.try_clone().unwrap())),
None => eprintln!(
"{TYPES} on {}: no dma-buf of device-local memory",
device.name()
),
}
match udmabuf_openable(TYPES) {
true => sources.push(("udmabuf", scattered_udmabuf(page))),
false => eprintln!("{TYPES} on {}: no udmabuf", device.name()),
}
let buffer_types = dma_buf_buffer_types(&device, bytes);
let classes = [
MemoryClass::Shared,
MemoryClass::WriteCombined,
MemoryClass::Private,
MemoryClass::Managed,
];
// Per source and class: the allowed types, both orders and the runtime's import's type.
let mut found = Vec::new();
for (source, fd) in &sources {
let listed = match dma_buf_types(&device, fd) {
Ok(listed) => listed,
Err(error) => {
eprintln!(
"{TYPES} on {}: {source}: types refused, {error:?}",
device.name()
);
continue;
}
};
let allowed = listed & buffer_types;
for class in classes {
let before = dma_buf_class_order(types, allowed, class, unified);
let after = dma_buf_candidates(types, allowed, class, unified);
let handed = fd.try_clone().unwrap();
let imported = import_dma_buf(
&device.shared().memory,
device.shared().vk(),
handed,
bytes,
(allowed, class),
DmaBufFor::Shared,
);
let took = imported.map(|(memory, type_index, _)| {
// SAFETY: the import just made, used by nothing.
unsafe { handles.device.free_memory(memory, None) };
type_index
});
found.push((*source, class, allowed, before, after, took));
}
}
// The test's own imports in the class's order, after every one of the runtime's.
for (source, class, allowed, before, after, took) in found {
let fd = &sources.iter().find(|(name, _)| *name == source).unwrap().1;
let took_before = (before.iter())
.copied()
.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 \
{:?}",
device.name(),
took.map(|index| (index, flags(index))),
took_before.map(|index| (index, flags(index))),
);
if before == after {
assert_eq!(took.ok(), took_before, "{source}, {class:?}");
}
}
drop(sources);
if let Some((fd, memory)) = own {
drop(fd);
// SAFETY: the test's memory, which no import holds any more.
unsafe { handles.device.free_memory(memory, None) };
}
}
+65 -13
View File
@@ -1,8 +1,8 @@
use crate::memory::{
candidates, dma_buf_candidates, fd_buffer_types, host_cached, host_cached_placement,
host_image_types, host_incoherent, kept_budget, overflow_candidates, placement_order,
pooled_block_bytes, worked_first, MemoryClass, Pool, BLOCK_BYTES, KEPT_EMPTY_BLOCKS,
KEPT_EMPTY_BYTES, KEPT_EMPTY_FOR,
candidates, dma_buf_attempts, dma_buf_candidates, dma_buf_class_order, fd_buffer_types,
host_cached, host_cached_placement, host_image_types, host_incoherent, kept_budget,
overflow_candidates, placement_order, pooled_block_bytes, worked_first, MemoryClass, Pool,
BLOCK_BYTES, KEPT_EMPTY_BLOCKS, KEPT_EMPTY_BYTES, KEPT_EMPTY_FOR,
};
use ash::vk;
use std::time::Instant;
@@ -57,13 +57,16 @@ fn classes_pick_their_memory_types_best_first() {
);
}
/// An import of a dma-buf tries its 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 4, the class's, then type 0, system
/// memory with no property flag, which that driver imports a udmabuf as.
/// 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.
#[test]
fn dma_buf_imports_try_the_class_s_types_then_the_dma_buf_s_others() {
fn dma_buf_imports_try_the_types_the_host_does_not_map_first() {
use vk::MemoryPropertyFlags as P;
let host = P::HOST_VISIBLE | P::HOST_COHERENT;
let nvidia = [
@@ -75,15 +78,64 @@ fn dma_buf_imports_try_the_class_s_types_then_the_dma_buf_s_others() {
ty(P::DEVICE_LOCAL | host),
];
assert_eq!(
dma_buf_candidates(&nvidia, 0x11, MemoryClass::Shared, false),
dma_buf_class_order(&nvidia, 0x11, MemoryClass::Shared, false),
[4, 0]
);
// The class's own order first, the others after it in Vulkan's.
assert_eq!(
dma_buf_candidates(&nvidia, 0x3f, MemoryClass::Shared, false),
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.
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]
);
// 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<u32>, Vec<u32>) = private.iter().partition(|&&index| index < 3);
assert_eq!(
dma_buf_candidates(&nvidia, 0x3f, MemoryClass::Private, false),
[unmapped, 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
// before either as type 4.
let ranked = dma_buf_candidates(&nvidia, 0x11, MemoryClass::Shared, false);
assert_eq!(
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 = [
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);
}
// Never a protected, lazily allocated or AMD device-coherent type.
let others = [
ty(P::PROTECTED),
+9 -2
View File
@@ -1703,8 +1703,15 @@ driver's. Each is open work.
Laptop, its RAM's first page dedicated to its buffer); an object's run alone of a few pages the
driver may refuse so, and the object then takes the runtime's other paths (`host_ram.rs`'s
`bind_in_chunk`). It lists a udmabuf of host pages as two memory types, 0 and 4 (host-visible,
coherent and cached, the one the runtime ranks first), and refuses every import as 4 as out of
device memory (620.70.06 on an RTX 2080 and an RTX 3090, measured). 615.71.09 also refuses
coherent and cached, the `Shared` class's), and refuses every import as 4 as out of device
memory (620.70.06 on an RTX 2080 and an RTX 3090, measured). Once it refused an import as 4,
620.70.06 refuses every later allocation of device-local memory exportable as an opaque file
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
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)