Runtime: a dma-buf import is refused where the Shared type is not host-coherent

What: the dma-buf rule (import.rs, dma_buf_type) now takes the device's Shared
coherence, keyed by the query that already decides it (the Shared type's
HOST_COHERENT flag; the device holds a coherence state exactly where it lacks
it). On a device that holds one, every dma-buf is refused with the new
Limit::ImportCoherence { handle }, whatever the fd's query offers, before any
allocation: Device::import_dma_buf and Device::import_dma_bufs (each fd's rule;
the sparse buffer is destroyed, as for Limit::ImportType). Basis: device. The
pointer import (import_host_memory) is unchanged, so the consumer's fallback
imports those pages by pointer. No new route: the refusal sits inside the
existing rule.

Why: on a Meteor Lake GPU under Mesa's Intel driver and the i915 kernel driver
(no LLC), the fd's query offers type 0 (device-local, host-visible,
host-coherent, write-combined) first, so the rule took the import, and the
runtime never flushes an import because its type is host-coherent. That flag
speaks only for mappings Vulkan makes; the consumer writes the pages through
its own cached mapping. In Linux v7.3, i915_gem_prime_import
(drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c:281-337) makes the object with
i915_gem_object_alloc (kmem_cache_zalloc, gem/i915_gem_object.c) and never calls
i915_gem_object_set_cache_coherency, so pat_index 0 and cache_coherent 0; the
only CPU-cache work is one wbinvd_on_all_cpus at get_pages where !HAS_LLC &&
!IS_DG1 (i915_gem_dmabuf.c:249-261, whose comment says only DG1 "still snoops
transactions even with CACHE_NONE"); on MTL PAT 0 is WB with no coherency. So
the GPU reads DRAM without the guest's lines still in the CPU cache: text and
images missing. The pointer import (userptr, I915_CACHE_LLC, PAT 3) snoops.
No Vulkan query says whether a driver's GPU mapping of a foreign dma-buf sees
the CPU's caches, so the Shared type's flag is a proxy that errs to the safe
side: where it refuses, the pointer route is coherent by its type's flag.

Docs: import.rs's header and both dma-buf calls' doc comments; coherence.rs on
imports; the runtime README's Shared memory's coherence ("Imported blocks take
no call"), Imported blocks (the rule, the proxy and its limit) and its known
gaps; docs/VALIDATION.md on the shared view; docs/REFUSALS.md regenerated; the
api_signatures table blessed (one variant added).

Tests: a_dma_buf_is_refused_where_the_shared_type_is_not_host_coherent (pure,
over Meteor Lake's i915 type list and the Iris Xe's);
every_device_under_the_view_refuses_a_dma_buf_and_imports_host_memory (device,
through the refused device's view); the existing dma-buf device tests take the
new refusal. Verified at this commit: cargo fmt only; the build, the pure
tests and the device run are in the jobs the lane reports.
This commit is contained in:
Anees Iqbal committed 2026-11-26 11:51:26 -08:00
1 parent c241bf280b
commit f6aa8dbdf1
12 files changed
+365 -49

No files matched your search

+33 -6
View File
@@ -577,7 +577,9 @@ On a device whose Shared type is not host-coherent, a range that a command buffe
through an address, with no binding or `use_resource` declaration naming its buffer, is neither
flushed nor invalidated: the consumer declares what its commands reach. The consumer's own host
reads and writes, through its own mappings (The consumer's mappings) or between those points, are
its own. Imported blocks (below) are host-coherent memory and take no call. Exported blocks and
its own. Imported blocks (below) take no call: a pointer import is host-coherent memory by its
type's flag, and such a device imports no dma-buf (`Limit::ImportCoherence`), whose pages the
consumer writes through its own cached mapping, which no flush reaches. Exported blocks and
surfaces are Private memory with no host mapping, and take none either.
### Imported blocks
@@ -599,9 +601,12 @@ consumer's.
process's later allocations fail too: a device-local one after either refusal, and a host-cached
one after the refusal in the host-cached type.
- `Device::import_dma_buf(fd, len, release)` (unix) imports a dma-buf
(`VK_EXT_external_memory_dma_buf`) only into the first memory type the fd's query offers, and
only where that type is host-visible and host-coherent and the buffer's requirements list it;
anything else is `Limit::ImportType`, with nothing imported. It takes the fd (the consumer's
(`VK_EXT_external_memory_dma_buf`) only on a device whose Shared memory type is host-coherent,
only into the first memory type the fd's query offers, and only where that type is host-visible
and host-coherent and the buffer's requirements list it. A device whose Shared type is not
host-coherent (Shared memory's coherence) refuses every dma-buf with `Limit::ImportCoherence`,
whatever its query offers; anything else is `Limit::ImportType`; nothing is imported after
either. It takes the fd (the consumer's
dup): after `Ok` the driver owns it; after a refusal or a failed query the runtime closes it, as
no driver was given it; after a failed import it closes it only where it still names the same
file, as a driver may have closed it and the number may name another file since. The block's
@@ -613,7 +618,8 @@ consumer's.
(`Limit::NoImport`). The block's buffer is one sparse buffer (`VK_BUFFER_CREATE_SPARSE_BINDING_BIT`)
with each dma-buf's import bound at its offset, so the block's `gpu_address` and its buffers run
across the dma-bufs as over one range, and its `contents` is `None` as for one dma-buf. Each fd
takes the dma-buf rule's type of the sparse buffer's types, and every fd the same one. One
takes the dma-buf rule's type of the sparse buffer's types, and every fd the same one; a device
whose Shared type is not host-coherent refuses them with `Limit::ImportCoherence`. One
`vkQueueBindSparse` on the runtime's queue binds the imports (`Metrics::sparse_binds`); the call
does not wait for it. Vulkan orders no sparse bind against command buffers, so the runtime's next
`vkQueueSubmit2` waits for it, once: a semaphore wait orders every later command in submission
@@ -655,7 +661,24 @@ imports, a GPU write reaches them, where Metal drops it.
The two rules differ because a failed dma-buf import is not harmless: on NVIDIA's Linux driver
(620.70.06, an RTX 3090) one failed dma-buf import into a later type the query offered broke every
later allocation of that type, the Shared type among them, so the dma-buf rule attempts nothing it
is not sure of. The host-memory rule takes the first host-visible, host-coherent type offered:
is not sure of.
They differ in coherence too. A type's `VK_MEMORY_PROPERTY_HOST_COHERENT_BIT` speaks only for the
mappings Vulkan makes (`vkMapMemory`); Vulkan promises nothing for writes outside them, and the
consumer writes a dma-buf's pages through its own cached mapping. No Vulkan query says whether a
driver's GPU mapping of a dma-buf from another driver sees the CPU's caches. So the dma-buf rule's
coherence condition is a proxy: the Shared type's flag, which tells whether the device's
host-cached memory is coherent. Where it is not, the rule refuses every dma-buf
(`Limit::ImportCoherence`), which errs to the safe side: there the pointer import of the same pages
(`import_host_memory`) is coherent by its type's flag. On one such device, a Meteor Lake GPU under
Mesa's Intel driver and the i915 kernel driver (read in the sources of Mesa 26.3.1 and Linux 7.3,
not run), the fd's query offers a write-combined, host-coherent type first, and the kernel driver
maps a dma-buf from another driver with no cache coherency, so the GPU can read bytes older than
host writes still in the CPU's cache; its pointer import is snooped. Where the Shared type is
coherent, as on every device of ours, the proxy refuses nothing, and a dma-buf's coherence there
rests on the driver.
The host-memory rule takes the first host-visible, host-coherent type offered:
where RADV (Mesa 26.2, an RX 7900 XTX) refused the pages of a shared mapping of a memfd at the
import, its Shared allocations and a dma-buf import still worked after it. A device that does not
list a handle's extensions, or whose buffers do not import it, refuses that call with
@@ -1060,6 +1083,10 @@ the Direct3D 11 texture's payload until it is freed.
reads their cached types as not coherent (`--cfg m2v_shared_view`, docs/VALIDATION.md), on memory
that is coherent in fact; neither the calls' time nor their effect has been measured on a device
whose memory is not.
- On a device whose Shared type is not host-coherent, every dma-buf import is refused
(`Limit::ImportCoherence`, Imported blocks), so pages that only a dma-buf imports there (a
mapping no pointer import takes) have no no-copy route. The refusal is a proxy, and where the
Shared type is coherent a dma-buf's coherence rests on the driver (Imported blocks).
- On such a device, a host write to a range a command buffer may write, made while the command
buffer runs, may be lost, and so may the GPU's writes to the same atoms. The runtime flushes
each such range just before it invalidates it, and Vulkan states: "If a range of non-coherent
+3 -1
View File
@@ -40,7 +40,9 @@
//! Ranges are of the pool's Shared allocations and the runtime's own, which it maps whole,
//! widened to whole multiples of the device's `nonCoherentAtomSize`: within a block, since a block
//! starts and ends at multiples of 64 KiB and the atom is a power of two of at most 256. An import
//! is never flushed: its type is host-coherent (`crate::import`), and Vulkan maps none of it.
//! is never flushed, and Vulkan maps none of it: a pointer import's type is host-coherent, and on
//! a device that holds a [`Coherence`] the runtime imports no dma-buf (`crate::import`), whose
//! pages the consumer writes through its own cached mapping, which no flush reaches.
//! Each call is counted, with its bytes, in [`Metrics`](crate::Metrics)'s `shared_flushes`,
//! `shared_flushed_bytes`, `shared_invalidates` and `shared_invalidated_bytes`; a call that fails
//! in `shared_coherence_failures`.
+22
View File
@@ -2255,6 +2255,23 @@ pub enum Limit {
/// The types the buffer's (or plane image's) memory requirements list, one bit each.
listed: u32,
},
/// An import of a dma-buf (`Device::import_dma_buf` and `Device::import_dma_bufs`, on Unix) on
/// a device whose Shared memory type, the first host-visible, host-cached type a buffer can
/// use, is not host-coherent, whatever type the fd's query offers: nothing is imported. The
/// device then lacks a dma-buf import coherent with the host's cached mappings. The consumer
/// writes the pages through its own cached mapping, which no Vulkan flush reaches, and no
/// Vulkan query says whether the driver's GPU mapping of a dma-buf from another driver sees the
/// host's caches: on such a device (a Meteor Lake GPU under Mesa's Intel driver and the i915
/// kernel driver) it does not. A pointer import of the same pages
/// ([`Device::import_host_memory`](crate::Device::import_host_memory)) takes a host-coherent
/// type by its flag. Metal's counterpart,
/// `newBufferWithBytesNoCopy:length:options:deallocator:`, takes any page-aligned memory.
///
/// Basis: device (the Shared memory type's `VK_MEMORY_PROPERTY_HOST_COHERENT_BIT`).
ImportCoherence {
/// The handle asked for.
handle: ImportHandle,
},
/// An import of no bytes, or one whose address or length is not a multiple of `alignment`:
/// for host memory the device's `minImportedHostPointerAlignment`, for several dma-bufs each
/// one's length against `Device::dma_buf_granule` on Unix (or the
@@ -3075,6 +3092,11 @@ impl fmt::Display for Limit {
"the {handle:?}'s query offers types {offered:#x}, and no host-visible, \
host-coherent type the import takes of the buffer's {listed:#x}"
),
Limit::ImportCoherence { handle } => write!(
f,
"the device has no {handle:?} import coherent with the host's cached mappings: \
its host-cached memory is not host-coherent"
),
Limit::ImportAlignment {
handle,
len,
@@ -117,6 +117,7 @@ impl Limit {
Limit::AllocationNotMapped { .. } => RefusalBasis::Ours,
Limit::NoImport { .. } => RefusalBasis::Device,
Limit::ImportType { .. } => RefusalBasis::Device,
Limit::ImportCoherence { .. } => RefusalBasis::Device,
Limit::ImportAlignment { .. } => RefusalBasis::Device,
Limit::SparseAddressSpace { .. } => RefusalBasis::Device,
Limit::NoExport { .. } => RefusalBasis::Device,
+69 -29
View File
@@ -9,9 +9,18 @@
//! The two rules differ. A dma-buf is imported only into the first type its fd's query offers,
//! and only where that type is host-visible and host-coherent: on NVIDIA's Linux driver
//! (620.70.06) one failed dma-buf import into a later type broke every later allocation of that
//! type, the Shared type among them, so the rule attempts nothing it is not sure of. Host memory
//! is imported into the first type the pointer's query offers that is host-visible and
//! host-coherent.
//! type, the Shared type among them, so the rule attempts nothing it is not sure of. And it is
//! imported only on a device whose Shared type is host-coherent (one that holds no
//! `crate::coherence::Coherence`), and refused elsewhere ([`Limit::ImportCoherence`]). The
//! consumer writes the pages through its own cached mapping, which no Vulkan flush reaches; a
//! type's `HOST_COHERENT` speaks only for the mappings Vulkan makes, and no Vulkan query says
//! whether the driver's GPU mapping of a dma-buf from another driver sees the host's caches. So
//! the Shared type's flag is a proxy, and the refusal errs to the safe side: where it refuses, a
//! pointer import of the same pages is coherent by its type's flag. On one device it refuses, a
//! Meteor Lake GPU under Mesa's Intel driver and the i915 kernel driver, the kernel driver maps
//! such a dma-buf with no cache coherency, and its pointer import (userptr) snoops. Host memory is
//! imported into the first type the pointer's query offers that is host-visible and
//! host-coherent, on every device.
use crate::block::Block;
#[cfg(unix)]
@@ -413,12 +422,30 @@ const COHERENT: vk::MemoryPropertyFlags = vk::MemoryPropertyFlags::from_raw(
| vk::MemoryPropertyFlags::HOST_COHERENT.as_raw(),
);
/// The dma-buf rule: the first type the fd's query `offered`, where the buffer's requirements
/// `listed` it and it is one of the `coherent` types; none otherwise, whatever else is offered.
/// The dma-buf rule, for an import of `handle`: on a device whose Shared type is host-coherent
/// (`shared_coherent`: the device holds no `crate::coherence::Coherence`), the first type the
/// fd's query `offered`, where the buffer's requirements `listed` it and it is one of the
/// `coherent` types. Err: [`Limit::ImportCoherence`] on a device whose Shared type is not, whatever
/// is offered; [`Limit::ImportType`] where the first type offered is not taken, whatever else is.
#[cfg(unix)]
pub(crate) fn dma_buf_type(offered: u32, listed: u32, coherent: u32) -> Option<u32> {
pub(crate) fn dma_buf_type(
handle: ImportHandle,
shared_coherent: bool,
offered: u32,
listed: u32,
coherent: u32,
) -> Result<u32, Limit> {
if !shared_coherent {
return Err(Limit::ImportCoherence { handle });
}
let first = offered.trailing_zeros();
(first < u32::BITS && (listed & coherent) & (1 << first) != 0).then_some(first)
(first < u32::BITS && (listed & coherent) & (1 << first) != 0)
.then_some(first)
.ok_or(Limit::ImportType {
handle,
offered,
listed,
})
}
/// The host-memory rule: the first type the pointer's query `offered` that the buffer's
@@ -435,10 +462,11 @@ impl Device {
/// the host reaches the bytes through the consumer's own mappings of the pages, so the
/// block's `contents` is `None`, and its zero mark is 0.
///
/// It imports only into the first memory type the fd's query (`vkGetMemoryFdPropertiesKHR`)
/// offers, and only where that type is host-visible and host-coherent and the buffer's memory
/// requirements list it, and returns the block with the index of that type. Every refusal
/// comes before any allocation is attempted, with nothing imported.
/// It imports only on a device whose Shared memory type is host-coherent, only into the first
/// memory type the fd's query (`vkGetMemoryFdPropertiesKHR`) offers, and only where that type
/// is host-visible and host-coherent and the buffer's memory requirements list it, and returns
/// the block with the index of that type. Every refusal comes before any allocation is
/// attempted, with nothing imported.
///
/// The call takes `fd`, the consumer's own fd of the dma-buf (a dup of it, where the consumer
/// keeps one). After `Ok` the driver owns it. After a refusal (`Limit`, [`Error::Quiesced`])
@@ -458,9 +486,14 @@ impl Device {
/// `maxMemoryAllocationCount`.
///
/// Metal takes any page-aligned memory; here a device that offers the dma-buf only in a type
/// the rule does not take refuses it, and the consumer chooses what comes next.
/// the rule does not take refuses it, and so does a device whose Shared memory type is not
/// host-coherent, whatever it offers: the consumer writes the pages through its own cached
/// mapping, and no Vulkan query says whether the driver's mapping of the dma-buf sees the
/// host's caches. The consumer chooses what comes next; a pointer import of the same pages
/// ([`Device::import_host_memory`]) takes a host-coherent type by its flag.
///
/// Err: [`Limit::NoImport`] where the device does not import a dma-buf;
/// [`Limit::ImportCoherence`] where the device's Shared memory type is not host-coherent;
/// [`Limit::ImportType`] where the rule takes no type;
/// [`Limit::ImportAlignment`] for a length of 0 or one below
/// the buffer's memory requirements; [`Limit::BlockLen`] past the
@@ -492,13 +525,16 @@ impl Device {
.map_err(Error::vulkan("vkGetMemoryFdPropertiesKHR"))?;
let needs = memory::import_requirements(core, len, handle.vk());
let offered = properties.memory_type_bits;
let index = dma_buf_type(offered, needs.memory_type_bits, core.imports.coherent).ok_or(
Error::Limit(Limit::ImportType {
handle,
offered,
listed: needs.memory_type_bits,
}),
)?;
let shared_coherent = core.coherence.is_none();
let listed = needs.memory_type_bits;
let index = dma_buf_type(
handle,
shared_coherent,
offered,
listed,
core.imports.coherent,
)
.map_err(Error::Limit)?;
check_size(handle, len, &needs)?;
let identity = identity(fd.as_raw_fd());
let raw = fd.into_raw_fd();
@@ -559,8 +595,8 @@ impl Device {
///
/// Each fd's import takes the dma-buf rule's type ([`Device::import_dma_buf`]) of the types the
/// sparse buffer's memory requirements list, and every fd the same one, which the call returns
/// with the block. Every refusal comes before any allocation is attempted, with nothing
/// imported. The call takes owned fds, as [`Device::import_dma_buf`] does, and returns a block,
/// with the block; as there, a device whose Shared memory type is not host-coherent refuses
/// every fd. Every refusal comes before any allocation is attempted, with nothing imported. The call takes owned fds, as [`Device::import_dma_buf`] does, and returns a block,
/// so a consumer that imports one dma-buf or several places its buffers the same way.
///
/// One `vkQueueBindSparse` on the runtime's queue binds the imports, and the call does not
@@ -586,6 +622,7 @@ impl Device {
/// 0 or one that is no multiple of the granule (or of the sparse buffer's alignment);
/// [`Limit::BlockLen`] past the device's longest allocation or buffer;
/// [`Limit::SparseAddressSpace`];
/// [`Limit::ImportCoherence`] where the device's Shared memory type is not host-coherent;
/// [`Limit::ImportType`] where the rule takes no type for an fd, or
/// another type than the first fd's (then `listed` names that type alone);
/// [`Limit::MemoryAllocations`];
@@ -655,15 +692,18 @@ impl Device {
let listed = index.map_or(needs.memory_type_bits, |first: u32| {
needs.memory_type_bits & (1 << first)
});
match dma_buf_type(offered, listed, core.imports.coherent) {
Some(taken) => index = Some(taken),
None => {
let shared_coherent = core.coherence.is_none();
match dma_buf_type(
handle,
shared_coherent,
offered,
listed,
core.imports.coherent,
) {
Ok(taken) => index = Some(taken),
Err(refusal) => {
destroy();
return Err(Error::Limit(Limit::ImportType {
handle,
offered,
listed,
}));
return Err(Error::Limit(refusal));
}
}
}
@@ -19,7 +19,7 @@ use std::collections::BTreeSet;
use std::fs;
/// Memory with the given types, each in heap 0.
fn memory(types: &[vk::MemoryPropertyFlags]) -> vk::PhysicalDeviceMemoryProperties {
pub(super) fn memory(types: &[vk::MemoryPropertyFlags]) -> vk::PhysicalDeviceMemoryProperties {
let mut memory = vk::PhysicalDeviceMemoryProperties {
memory_type_count: types.len() as u32,
memory_heap_count: 1,
+222 -5
View File
@@ -1,13 +1,20 @@
//! Imports: the two type rules, and under a device cfg, on every device of the cfg's kind, that
//! Imports: the two type rules, the dma-buf rule's refusal on a device whose Shared type is not
//! host-coherent (Meteor Lake's types under Mesa's Intel driver and i915), and under a device cfg,
//! on every device of the cfg's kind, that
//! host memory is imported into the first host-visible, host-coherent type its query offers, that
//! a GPU write reaches it and its release runs only once its memory is freed, that a texture over
//! a buffer in host memory's import is read and written by blits through the counted copy, that an
//! ordinary Shared allocation succeeds after an import of a shared mapping of a file (which RADV
//! refuses at the import), that an import counts against the device's allocations, and that a
//! dma-buf is imported where its first offered type is host-visible and host-coherent and refused
//! at the query, with nothing imported, everywhere else.
//! at the query, with nothing imported, everywhere else; and, on every device the floor opens
//! through the refused device's memory view, that a dma-buf is refused for the Shared type's
//! coherence, alone and several behind one buffer, with nothing imported, and host memory of the
//! same length is imported.
use crate::import::host_type;
#[cfg(unix)]
use crate::{ImportHandle, Limit};
/// The dma-buf rule takes the first type the fd's query offers, only where it is host-visible,
/// host-coherent and listed by the buffer's requirements; never a later one.
@@ -32,13 +39,101 @@ fn a_dma_buf_takes_only_the_first_type_offered() {
for (offered, expected) in cases {
let listed = if offered == 0x06 { 0b101 } else { listed };
assert_eq!(
dma_buf_type(offered, listed, coherent),
dma_buf_type(ImportHandle::DmaBuf, true, offered, listed, coherent).ok(),
expected,
"offered {offered:#x}"
);
}
}
/// On a device whose Shared type is not host-coherent, the dma-buf rule refuses whatever the fd's
/// query offers, and the host-memory rule still takes the pointer's type. Meteor Lake's types
/// under Mesa's Intel driver and the i915 kernel driver (src/intel/vulkan/i915/anv_device.c, a GPU
/// without the shared last-level cache): 0 device-local, host-visible and host-coherent
/// (write-combined), 1 device-local, host-visible and host-cached, not coherent. The floor's
/// choice puts Shared in type 1, so the device holds a coherence state, and the fd's offer of
/// type 0, which the rule took before, is refused; the pointer's offer of type 0 is taken. On the
/// Iris Xe's types (i915 with the last-level cache: 0 device-local, 1 that and host-visible and
/// host-coherent, 2 that and host-cached), Shared is coherent, no state is held, and the fd's offer
/// of type 1 is taken as before.
#[cfg(unix)]
#[test]
fn a_dma_buf_is_refused_where_the_shared_type_is_not_host_coherent() {
use crate::coherence::Coherence;
use crate::floor::choose;
use crate::import::dma_buf_type;
use crate::tests::floor::memory;
use ash::vk::MemoryPropertyFlags as F;
let local = F::DEVICE_LOCAL;
let coherent_type = F::HOST_VISIBLE | F::HOST_COHERENT;
// The rule's `coherent` bits: the host-visible, host-coherent types.
let coherent_bits = |memory: &ash::vk::PhysicalDeviceMemoryProperties| {
(0u32..)
.zip(memory.memory_types_as_slice())
.filter(|(_, kind)| kind.property_flags.contains(coherent_type))
.fold(0, |bits, (index, _)| bits | 1 << index)
};
let meteor_lake = memory(&[
local | coherent_type,
local | F::HOST_VISIBLE | F::HOST_CACHED,
]);
let types = choose(&meteor_lake, u32::MAX).expect("Meteor Lake's types");
assert_eq!(types.shared, 1);
let held = Coherence::of(&meteor_lake, types, 64).is_some();
assert!(
held,
"a coherence state for a Shared type that is not coherent"
);
let coherent = coherent_bits(&meteor_lake);
assert_eq!(coherent, 0b01);
for offered in [0b01, 0b11, 0b10] {
assert_eq!(
dma_buf_type(ImportHandle::DmaBuf, !held, offered, 0b11, coherent),
Err(Limit::ImportCoherence {
handle: ImportHandle::DmaBuf
}),
"offered {offered:#x}"
);
assert_eq!(
dma_buf_type(ImportHandle::DmaBufs, !held, offered, 0b11, coherent),
Err(Limit::ImportCoherence {
handle: ImportHandle::DmaBufs
}),
"offered {offered:#x}"
);
}
// The rule before the state keyed it took the fd's offer of type 0.
assert_eq!(
dma_buf_type(ImportHandle::DmaBuf, true, 0b01, 0b11, coherent),
Ok(0)
);
// The pointer's offer of type 0 is taken.
assert_eq!(host_type(0b01, 0b11, coherent), Some(0));
let iris = memory(&[
local,
local | coherent_type,
local | coherent_type | F::HOST_CACHED,
]);
let types = choose(&iris, u32::MAX).expect("the Iris Xe's types");
assert_eq!(types.shared, 2);
let held = Coherence::of(&iris, types, 64).is_some();
assert!(!held, "no coherence state for a coherent Shared type");
let coherent = coherent_bits(&iris);
assert_eq!(
dma_buf_type(ImportHandle::DmaBuf, !held, 0b110, 0b111, coherent),
Ok(1)
);
assert_eq!(
dma_buf_type(ImportHandle::DmaBuf, !held, 0b001, 0b111, coherent),
Err(Limit::ImportType {
handle: ImportHandle::DmaBuf,
offered: 0b001,
listed: 0b111
})
);
}
/// The host-memory rule takes the first type offered that is host-visible, host-coherent and
/// listed by the buffer's requirements.
#[test]
@@ -855,10 +950,121 @@ pub(crate) mod on_device {
Some(unsafe { OwnedFd::from_raw_fd(fd) })
}
/// Through the refused device's memory view (`each_device_under_the_view`), whose Shared type
/// is not host-coherent, so the device holds a coherence state: a dma-buf is refused with
/// `Limit::ImportCoherence` before any allocation, alone (`import_dma_buf`) and three behind
/// one buffer (`import_dma_bufs`, where the device offers the granule), with nothing imported
/// and no release run; and host memory of the same length is imported by its pointer
/// (`import_host_memory`) into a host-visible, host-coherent type, where the device imports
/// host memory. Each device prints the fd's offer and what the rule answers it without the
/// state: a type there is one the rule took before it was keyed by the state. A device that
/// imports no dma-buf refuses it with `Limit::NoImport`.
#[cfg(target_os = "linux")]
#[test]
fn every_device_under_the_view_refuses_a_dma_buf_and_imports_host_memory() {
use crate::import::dma_buf_type;
use crate::tests::command::on_device::each_device_under_the_view;
use std::os::fd::AsRawFd;
const LEN: u64 = 64 << 10;
let dma_buf_ext = vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT;
each_device_under_the_view("import_coherence", |name, _, device, memory| {
assert!(
device.core().coherence.is_some(),
"{name}: a coherence state"
);
let Some((fd, _memfd)) = dma_buf(LEN) else {
println!("import_coherence: {name}: no /dev/udmabuf here; not run");
return;
};
let counters = device.counters();
let before = counters.gauges();
let Some(fds) = device.core().imports.dma_buf.as_ref() else {
match device.import_dma_buf(fd, LEN, None) {
Err(Error::Limit(Limit::NoImport {
handle: ImportHandle::DmaBuf,
})) => {}
other => panic!("{name}: {other:?}"),
}
println!("import_coherence: {name}: imports no dma-buf, refused typed");
return;
};
let mut properties = vk::MemoryFdPropertiesKHR::default();
// SAFETY: the extensions are enabled; `fd` is open, and the query takes no ownership.
unsafe { fds.get_memory_fd_properties(dma_buf_ext, fd.as_raw_fd(), &mut properties) }
.unwrap_or_else(|error| panic!("{name}: the fd's query: {error}"));
let offered = properties.memory_type_bits;
let listed = crate::memory::import_requirements(device.core(), LEN, dma_buf_ext)
.memory_type_bits;
let coherent = device.core().imports.coherent;
let unkeyed = dma_buf_type(ImportHandle::DmaBuf, true, offered, listed, coherent);
let (release, released) = releasing();
match device.import_dma_buf(fd, LEN, release) {
Err(Error::Limit(Limit::ImportCoherence {
handle: ImportHandle::DmaBuf,
})) => {}
other => panic!("{name}: one dma-buf: {other:?}"),
}
assert_eq!(counters.gauges(), before, "{name}: nothing imported");
assert!(!released.load(Ordering::SeqCst), "{name}: no release run");
let several = match device.dma_buf_granule() {
Some(granule) => {
let memfd = memfd(3 * granule);
let chunks = (0..3)
.map(|at| udmabuf(&memfd, at * granule, granule).map(|fd| (fd, granule)))
.collect::<Option<Vec<_>>>()
.expect("three dma-bufs");
let (release, released) = releasing();
match device.import_dma_bufs(chunks, release) {
Err(Error::Limit(Limit::ImportCoherence {
handle: ImportHandle::DmaBufs,
})) => {}
other => panic!("{name}: three dma-bufs: {other:?}"),
}
assert_eq!(counters.gauges(), before, "{name}: nothing imported");
assert!(!released.load(Ordering::SeqCst), "{name}: no release run");
format!("three dma-bufs of {granule} bytes refused alike")
}
None => "no granule, so no several dma-bufs".to_string(),
};
let host = match device.core().imports.host.as_ref() {
Some(&(_, alignment)) => {
let len = LEN.next_multiple_of(alignment);
let (pointer, mapping) = host_memory(len, alignment);
let (release, host_released) = releasing();
// SAFETY: the memory is the test's, mapped until it unmaps it below.
let (block, memory_type) =
unsafe { device.import_host_memory(pointer, len, release) }
.unwrap_or_else(|error| panic!("{name}: host memory: {error}"));
let flags = memory.memory_types[memory_type as usize].property_flags;
let wanted = vk::MemoryPropertyFlags::HOST_VISIBLE
| vk::MemoryPropertyFlags::HOST_COHERENT;
assert!(
flags.contains(wanted),
"{name}: type {memory_type}, {flags:?}"
);
drop(block);
wait_until(&device, "the release", || {
host_released.load(Ordering::SeqCst)
});
free(mapping);
format!("host memory of {len} bytes imported into type {memory_type}")
}
None => "imports no host memory".to_string(),
};
println!(
"import_coherence: {name}: the fd's query offers {offered:#x} (the buffer lists \
{listed:#x}), which the rule without the state answers {unkeyed:?}; one \
dma-buf refused for the Shared type's coherence, nothing imported; {several}; \
{host}"
);
});
}
/// A dma-buf is imported where the first type its fd's query offers is host-visible and
/// host-coherent, and a GPU fill reaches its pages; elsewhere it is refused at the query with
/// nothing imported, and an ordinary Shared allocation and a host-memory import still
/// succeed after it. A device that does not import a dma-buf refuses it typed.
/// succeed after it. A device that does not import a dma-buf refuses it typed, and so does a
/// device whose Shared type is not host-coherent (under `m2v_shared_view`).
#[cfg(target_os = "linux")]
#[test]
fn every_device_imports_a_dma_buf_only_into_its_first_offered_type() {
@@ -916,13 +1122,24 @@ pub(crate) mod on_device {
{memory_type}"
);
}
Err(Error::Limit(Limit::ImportCoherence {
handle: ImportHandle::DmaBuf,
})) => {
assert!(device.core().coherence.is_some(), "{name}");
assert_eq!(counters.gauges(), before, "{name}: nothing imported");
assert!(!released.load(Ordering::SeqCst), "{name}");
println!("import: {name}: a dma-buf refused for the Shared type's coherence");
}
Err(Error::Limit(Limit::ImportType {
handle: ImportHandle::DmaBuf,
offered,
listed,
})) => {
let coherent = device.core().imports.coherent;
assert_eq!(dma_buf_type(offered, listed, coherent), None, "{name}");
let handle = ImportHandle::DmaBuf;
let rule = dma_buf_type(handle, true, offered, listed, coherent);
assert!(rule.is_err(), "{name}");
assert!(device.core().coherence.is_none(), "{name}");
assert_eq!(counters.gauges(), before, "{name}: nothing imported");
assert!(!released.load(Ordering::SeqCst), "{name}");
let shared = (device.take_block(MemoryClass::Shared, 32 << 20))
@@ -59,7 +59,7 @@ mod on_device {
let (release, released) = releasing();
match device.import_dma_buf(fd, len, release) {
Ok((block, _)) => Some((block, file, released)),
Err(Error::Limit(Limit::ImportType { .. })) => {
Err(Error::Limit(Limit::ImportType { .. } | Limit::ImportCoherence { .. })) => {
println!("{test}: {name}: no type taken for the dma-buf; not run");
None
}
@@ -1053,6 +1053,9 @@ pub(crate) mod on_device {
| Limit::ImportType {
handle: ImportHandle::DmaBuf,
..
}
| Limit::ImportCoherence {
handle: ImportHandle::DmaBuf,
},
),
) => {
@@ -8465,6 +8465,7 @@ variant metal2vulkan-runtime::Limit::BytesLength BytesLength { len: u64, max: u6
variant metal2vulkan-runtime::Limit::DeviceSamplers DeviceSamplers { max: u32 }
variant metal2vulkan-runtime::Limit::ExportType ExportType { handle: ExportHandle, listed: Option<u32> }
variant metal2vulkan-runtime::Limit::ImportAlignment ImportAlignment { handle: ImportHandle, len: u64, alignment: u64 }
variant metal2vulkan-runtime::Limit::ImportCoherence ImportCoherence { handle: ImportHandle }
variant metal2vulkan-runtime::Limit::ImportType ImportType { handle: ImportHandle, offered: u32, listed: u32 }
variant metal2vulkan-runtime::Limit::IndexRange IndexRange { class: IndexClass, first: u32, count: u32 }
variant metal2vulkan-runtime::Limit::LevelElements LevelElements { max: u32 }
Can't render this file because it is too large.
+6 -4
View File
@@ -32,7 +32,7 @@ Columns:
| The translation deadline | 1 | 1 | 0 | 0 | 0 | 1 | 0 |
| Devices below the runtime's floor | 8 | 8 | 0 | 0 | 0 | 0 | 0 |
| The runtime's `Refusal` | 86 | 86 | 0 | 0 | 0 | 0 | 0 |
| The runtime's `Limit` | 19 | 19 | 0 | 0 | 0 | 0 | 0 |
| The runtime's `Limit` | 20 | 20 | 0 | 0 | 0 | 0 | 0 |
| The runtime's `Gap` | 31 | 31 | 0 | 0 | 0 | 0 | 0 |
## Totals by basis
@@ -45,7 +45,7 @@ Columns:
| The translation deadline | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
| Devices below the runtime's floor | 0 | 0 | 0 | 8 | 0 | 0 | 0 | 0 |
| The runtime's `Refusal` | 23 | 25 | 0 | 0 | 38 | 0 | 0 | 0 |
| The runtime's `Limit` | 0 | 0 | 0 | 13 | 6 | 0 | 0 | 0 |
| The runtime's `Limit` | 0 | 0 | 0 | 14 | 6 | 0 | 0 | 0 |
| The runtime's `Gap` | 0 | 0 | 0 | 3 | 28 | 0 | 0 | 0 |
## 1. Translator and front-end refusals (`Unsupported`: a `Feature` and its `Needs`)
@@ -331,7 +331,8 @@ Columns:
| `MemoryAllocations` | A `VkDeviceMemory` allocation past the device's `maxMemoryAllocationCount` live ones, refused before the driver is called | at the device's limit | `device`: the device's `maxMemoryAllocationCount` | 1: `crates/metal2vulkan-runtime/src/memory.rs (allocate)` | `-` |
| `AllocationNotMapped` | A take that no free range fits made an allocation that the consumer's `BlockMappings::map` did not map | every device (a public call's own check of its arguments) | `ours`: the consumer's hook gave the allocation no place in its own address range | 1: `crates/metal2vulkan-runtime/src/memory.rs (own)` | `-` |
| `NoImport` | An import of a handle the device does not take (`Device::import_dma_buf` and `Device::import_dma_bufs` on Unix, `Device::import_host_memory`, a... | at the device's limit | `device`: the device's extensions, features, queue family and external buffer properties | 7: `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_form)`, `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_texture)` (+5) | `-` |
| `ImportType` | An import whose handle's memory type query offers no type the import takes, so nothing is imported | at the device's limit | `device`: the handle's memory type query and the buffer's or image's memory requirements | 4: `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_texture)`, `crates/metal2vulkan-runtime/src/import.rs (import_dma_buf)` (+2) | `-` |
| `ImportType` | An import whose handle's memory type query offers no type the import takes, so nothing is imported | at the device's limit | `device`: the handle's memory type query and the buffer's or image's memory requirements | 3: `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_texture)`, `crates/metal2vulkan-runtime/src/import.rs (dma_buf_type)` (+1) | `-` |
| `ImportCoherence` | An import of a dma-buf (`Device::import_dma_buf` and `Device::import_dma_bufs`, on Unix) on a device whose Shared memory type, the first... | at the device's limit | `device`: the Shared memory type's `VK_MEMORY_PROPERTY_HOST_COHERENT_BIT` | 1: `crates/metal2vulkan-runtime/src/import.rs (dma_buf_type)` | `-` |
| `ImportAlignment` | An import of no bytes, or one whose address or length is not a multiple of `alignment` | at the device's limit | `device`: `minImportedHostPointerAlignment` and the buffer's memory requirements | 4: `crates/metal2vulkan-runtime/src/import.rs (check_len)`, `crates/metal2vulkan-runtime/src/import.rs (check_size)` (+2) | `-` |
| `SparseAddressSpace` | An import over several dma-bufs (`Device::import_dma_bufs`, on Unix) whose sparse buffer would take the device's sparse address space past its... | at the device's limit | `device`: `sparseAddressSpaceSize` | 1: `crates/metal2vulkan-runtime/src/import.rs (reserve)` | `-` |
| `NoExport` | An export of a handle the device does not give (`Device::new_exported_block`) | at the device's limit | `device`: the device's extensions and its external buffer properties | 5: `crates/metal2vulkan-runtime/src/export.rs (handle, 2 sites)`, `crates/metal2vulkan-runtime/src/export.rs (new_exported_block)` (+2) | `-` |
@@ -622,7 +623,8 @@ Each refusal's sites, with the function that builds it.
- `rt:Limit::MemoryAllocations`: `crates/metal2vulkan-runtime/src/memory.rs (allocate)`
- `rt:Limit::AllocationNotMapped`: `crates/metal2vulkan-runtime/src/memory.rs (own)`
- `rt:Limit::NoImport`: `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_form)`; `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_texture)`; `crates/metal2vulkan-runtime/src/d3d11.rs (import_d3d11_surface)`; `crates/metal2vulkan-runtime/src/import.rs (import_dma_buf)`; `crates/metal2vulkan-runtime/src/import.rs (import_dma_bufs)`; `crates/metal2vulkan-runtime/src/import.rs (import_host_memory)`; `crates/metal2vulkan-runtime/src/import.rs (surface_import)`
- `rt:Limit::ImportType`: `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_texture)`; `crates/metal2vulkan-runtime/src/import.rs (import_dma_buf)`; `crates/metal2vulkan-runtime/src/import.rs (import_dma_bufs)`; `crates/metal2vulkan-runtime/src/import.rs (import_host_memory)`
- `rt:Limit::ImportType`: `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_texture)`; `crates/metal2vulkan-runtime/src/import.rs (dma_buf_type)`; `crates/metal2vulkan-runtime/src/import.rs (import_host_memory)`
- `rt:Limit::ImportCoherence`: `crates/metal2vulkan-runtime/src/import.rs (dma_buf_type)`
- `rt:Limit::ImportAlignment`: `crates/metal2vulkan-runtime/src/import.rs (check_len)`; `crates/metal2vulkan-runtime/src/import.rs (check_size)`; `crates/metal2vulkan-runtime/src/import.rs (import_dma_bufs)`; `crates/metal2vulkan-runtime/src/import.rs (import_host_memory)`
- `rt:Limit::SparseAddressSpace`: `crates/metal2vulkan-runtime/src/import.rs (reserve)`
- `rt:Limit::NoExport`: `crates/metal2vulkan-runtime/src/export.rs (handle, 2 sites)`; `crates/metal2vulkan-runtime/src/export.rs (new_exported_block)`; `crates/metal2vulkan-runtime/src/surface.rs (dma_buf_in_place)`; `crates/metal2vulkan-runtime/src/surface.rs (new_exported_surface)`
+3 -2
View File
@@ -59,8 +59,9 @@ i915 lists them. Shared storage then takes a cached type that is not coherent, s
device suite runs Shared memory's flushes and invalidates (the runtime README, "Shared memory's
coherence") on a GPU whose memory is coherent in fact; Vulkan allows both calls on a coherent type.
Lavapipe cannot run it: its one memory type also holds the parameter ring, which needs a coherent
type. Under the view a card whose host-pointer and dma-buf queries offer only its cached types
refuses every import; run it with the import families filtered. Lint the gated code without running
type. Under the view every card refuses every dma-buf import (`Limit::ImportCoherence`), and a
card whose host-pointer query offers only its cached types refuses every pointer import too; run
it with the import families filtered. Lint the gated code without running
it, in its own target directory:
```sh