No-copy memory whose pointer the device refuses and whose source fails is a copy, as with no source

On RADV a no-copy buffer or heap over shared memory (a memfd mapping) made with no dma-buf
source is a copy of the pages since 3f69d1af77, but one made with a source that fails was
still refused (Unsupported, HostMemory): source_failure kept the dma-bufs' refusal unless the
pointer's refusal was the runtime's unpinnable one, and the caller marked it not the device's.
Metal makes a no-copy buffer over any page, and rule 2 lets only what Metal refuses or what
cannot run fail; the pages are readable, so the copy runs.

buffer_over_host_memory now carries whether the device was asked the pointer: where it refused
it (not_importable) and no dma-buf of the pages served either (the source failed, or the device
refused its dma-bufs, refused()), the outcome is the device's refusal, as with no source: a copy,
announced and counted as copied_host_buffers_device_refused. Unchanged: the unpinnable path
(copied as Failed), misuse (chunks that break the source's contract stay Invalid, DmaBuf), a lost
device, memory imported only as dma-bufs and windows the import alignment does not cover (no
pointer asked: still refused, as without a source), and rebinds (refused without a working
source, as before).

Docs: CopiedFor and CopiedBecause::DeviceRefused, the copied-host-pages detail and catalogue
entry (degradation.rs, docs/API.md), WorkCounts::copied_host_buffers_device_refused, copied.rs,
dma_buf.rs's refusals, HostMemory::with_dma_buf, imports_shared_host_pointers, the runtime
README and HOWTO section 6 and Copied host buffers.

Tests: copied_device's shared-memory test makes a second buffer, with a source that fails, over
pages of its own: on a device that refuses shared memory's pointer a copy counted as the
device's refusal, the source called once; elsewhere the pointer's import, no source called. The
runtime lane dma_buf_imports (renamed shared_memory_without_a_working_source_is_a_copy_and_
misuse_refused) expects the failing source's copy on RADV, over a view of its own.

Verified: cargo fmt only; the 780M runs (with a negative control on the base's
buffer_descriptor.rs) are dispatched next.

(cherry picked from commit a30909d44aec64136e29f57b5a5dacca8568d3bc)
This commit is contained in:
Anees Iqbal committed 2026-11-09 00:34:52 -08:00
1 parent bb8c60238b
commit f36d32dfd3
12 files changed
+164 -72

No files matched your search

+2 -1
View File
@@ -377,7 +377,8 @@ the record the pass is making.
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, 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
or heap whose pointer the device refuses with no dma-buf of its pages serving it (no source
given, or one that failed or whose dma-bufs the device refused), 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
@@ -855,6 +855,9 @@ impl Device {
// idle and import them again (`host_residency.rs`), as it may its own import of the
// pointer where the driver's pointer imports follow their address; a heap's import
// stays, as does an import of a read-only view.
// Whether the device was asked the pointer, whose refusal a failed source leaves
// standing (the window takes it, and the memory is no dma-buf-only memory).
let by_pointer = host.by_pointer;
let imported =
match unsafe { self.import_by_pointer(importer, pointer, length, host, class) }
{
@@ -875,7 +878,14 @@ impl Device {
call, importer, pointer, length, window, source, class, keep,
)
}
.map_err(|failed| (source_failure(failed, refusal), false)),
.map_err(|failed| {
// No dma-buf of the pages served either (the source failed, or the
// device refused its dma-bufs): the device's refusal of the pointer,
// as where no source is given.
let by_device =
by_pointer && refused(&failed) && refusal == not_importable();
(source_failure(failed, refusal), by_device)
}),
// The device's refusal of the pointer, with no source of dma-bufs of the
// pages to take instead.
Err(error) => {
@@ -896,8 +906,9 @@ impl Device {
return copied.map(|buffer| noted(kept(buffer, log, kept_runs)));
}
// 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.
// `memfd` mapping among it), with no dma-buf of them served (no source given,
// a source that failed, dma-bufs the device refused): 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.
@@ -1791,8 +1802,9 @@ impl Device {
}
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)"
RADV refuses shared memory, and no dma-buf of them served it: no source of \
them was given (HostMemory::with_dma_buf), or it failed, or the device \
refused its dma-bufs"
}
CopiedBecause::Failed => "their import failed",
CopiedBecause::Moved => {
@@ -2280,8 +2292,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`).
/// The device refused their pointer's import, and no dma-buf of them served it: the caller
/// gave no source of them (`HostMemory::with_dma_buf`), or its source failed, or the device
/// refused its dma-bufs.
DeviceRefused,
/// The import the device and the gate admitted failed, with this.
Failed(Error),
+7 -6
View File
@@ -1,11 +1,12 @@
//! 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)), 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
//! (`copied-host-pages`). No import maps the pages, so they take no kernel mapping.
//! with no dma-buf of the pages serving it (no source of them given, or one that failed or whose
//! dma-bufs the device refused; 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 (`copied-host-pages`). No import maps the pages, so they take no kernel mapping.
//!
//! - **The copy.** A `VkBuffer` of the buffer's length in host-visible, coherent memory of the
//! runtime's own ([`MemoryClass::Shared`](crate::memory::MemoryClass)), which the GPU uses; the
@@ -2047,8 +2047,9 @@ degradations! {
/// 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 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,
/// pointer's import and no dma-buf of the pages served it, the caller giving no source of
/// them (`HostMemory::with_dma_buf`) or one that failed or whose dma-bufs the device
/// refused (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
+8 -4
View File
@@ -70,10 +70,14 @@
//! - **Rebinds** of a heap onto pages imported as dma-bufs make each such texture's image again
//! over its own new pages before the rebind settles (`rebind.rs`), and are refused
//! (`NotImplemented(RebindTextures)`) where one is not made.
//! - **Refused** (the pointer's refusal: `Unsupported`, `HostMemory`): a source that fails, chunks
//! the device imports as no type the buffer takes, a driver that refuses a chunk, and a sparse
//! buffer whose alignment misses the granule asked. Counted: `WorkCounts::dma_buf_imports` (each
//! chunk) and `WorkCounts::dma_buf_sparse_buffers`.
//! - **Not imported**: a source that fails, chunks the device imports as no type the buffer takes,
//! a driver that refuses a chunk, and a sparse buffer whose alignment misses the granule asked.
//! The pointer's refusal stands: a no-copy buffer or heap whose pointer the device refused is a
//! copy of the pages, as where no source is given (`copied.rs`,
//! `WorkCounts::copied_host_buffers_device_refused`); a rebind, memory imported only as dma-bufs
//! and pages whose window the device's import alignment does not cover are refused
//! (`Unsupported`, `HostMemory`). Counted: `WorkCounts::dma_buf_imports` (each chunk) and
//! `WorkCounts::dma_buf_sparse_buffers`.
use super::buffer::{usage, Pages};
use super::buffer_descriptor::HostBuffer;
@@ -282,9 +282,12 @@ impl HostMemory {
/// A texture over such pages (over the buffer, placed in the heap, or a view of one) is an
/// image over a dma-buf of its own pages where the device lists
/// `VK_EXT_image_drm_format_modifier` and `VK_EXT_queue_family_foreign`, else a shadow of the
/// bytes (announced `shadowed-texture`). Refused as [`HostMemory::new`]'s memory is
/// (`Unsupported`, `HostMemory`) where the device imports no chunk or the source fails; chunks
/// that break the source's contract are misuse (`Invalid`, `Problem::DmaBuf`). Measured: RADV
/// bytes (announced `shadowed-texture`). Where the device imports no chunk or the source
/// fails, the pointer's refusal stands: a no-copy buffer or heap is a copy of the pages, as
/// [`HostMemory::new`]'s memory is where the device refuses its pointer (`copied-host-pages`,
/// `WorkCounts::copied_host_buffers_device_refused`), and a rebind is refused (`Unsupported`,
/// `HostMemory`); chunks that break the source's contract are misuse (`Invalid`,
/// `Problem::DmaBuf`). Measured: RADV
/// refuses the pointer of `memfd` and other shared mappings and imports `udmabuf`s of their
/// pages; NVIDIA and lavapipe import the pointer and never call the source. ANV (Mesa 26.2.6 on
/// i915) imports the pointer of a `memfd`'s mapping and refuses that of a dma-buf's mapping
@@ -26,7 +26,7 @@ impl Device {
///
/// 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`,
/// one that works 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
@@ -182,7 +182,12 @@ impl Host {
Some(gate) => memory.with_import_gate(gate),
None => memory,
};
// SAFETY: as above.
self.buffer_over(device, memory)
}
/// A no-copy buffer over its bytes, made with `memory`, which names them.
fn buffer_over(&self, device: &Device, memory: HostMemory) -> Buffer {
// SAFETY: as for `buffer`.
unsafe {
device.new_buffer_with_bytes_no_copy(
memory,
@@ -407,40 +412,75 @@ fn copied_buffer_is_made_where_the_caller_s_gate_refuses_the_import() {
}
/// 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.
/// `memory-backend-memfd,share=on` guest RAM is) made with no source of dma-bufs of its pages, and
/// one made with a source that fails: on a device that refuses the pointer of shared memory
/// (`Device::imports_shared_host_pointers`, RADV) each a copy of the pages, announced (once) and
/// counted as the device's refusal, with every copy's coherence, the source called once, where
/// the call was refused before; elsewhere an import of the pointer, the source never called.
#[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);
use std::sync::atomic::{AtomicUsize, Ordering};
// Pages of their own each, so that neither buffer lies in the other's copy.
let hosts = [Host::over_shared_memory(9), Host::over_shared_memory(10)];
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));
let calls = Arc::new(AtomicUsize::new(0));
for (host, failing) in hosts.iter().zip([false, true]) {
let memory = match failing {
// SAFETY: as for `Host::buffer`.
false => unsafe { HostMemory::new(host.pointer) },
true => {
let counted = Arc::clone(&calls);
let source: crate::DmaBufSource = Arc::new(move |_| {
counted.fetch_add(1, Ordering::Relaxed);
Err(std::io::Error::other("no dma-buf"))
});
// SAFETY: as for `Host::buffer`, and the source answers no chunk.
unsafe { HostMemory::with_dma_buf(host.pointer, source) }
}
};
let source = match failing {
true => "a source that fails",
false => "no source",
};
let before = device.work_counts();
let buffer = host.buffer_over(&device, memory);
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 \
with {source} is {:?}, {device_refused} copied for the device's refusal, the source \
called {} times",
device.name(),
buffer.host_backing(),
calls.load(Ordering::Relaxed)
);
match refuses {
true => {
assert_eq!(
device_refused, 1,
"{source}: counted as the device's refusal"
);
assert_eq!(moved(&before, &after), [0, 1, 0, 0, 0], "{source}: a copy");
assert_eq!(announced(&heard), 1, "{source}: announced once");
lies_in_a_copy(&device, &buffer, host);
assert_eq!(calls.load(Ordering::Relaxed), usize::from(failing));
}
false => {
assert_eq!(device_refused, 0);
assert_eq!(buffer.host_backing(), Some(HostBacking::ImportedByPointer));
assert_eq!(
calls.load(Ordering::Relaxed),
0,
"{source}: no source called"
);
}
}
drop(buffer);
}
}
@@ -6750,9 +6750,10 @@ pub struct WorkCounts {
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`).
/// Of those, the ones made because the device refused the pointer's import and no dma-buf of
/// the pages served it: the caller gave no source of them (`HostMemory::with_dma_buf`), or
/// its source failed, or the device refused its dma-bufs. 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
@@ -9084,7 +9085,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.
/// The device refused their pointer's import, and no dma-buf of them served it: no source of
/// them was given, or it failed, or the device refused its dma-bufs.
DeviceRefused,
/// The import the device and the gate admitted failed.
Failed,
@@ -1087,15 +1087,15 @@ fn a_keep_alive_goes_after_the_imports_of_its_pages() {
});
}
/// The refusals: memfd memory with no source is no refusal, made on every device: where the
/// device refuses its pointer (RADV) a copy of its pages (`copied.rs`), counted as the device's
/// refusal (`WorkCounts::copied_host_buffers_device_refused`), else an import of the pointer. A
/// source that fails keeps the device's refusal, called once; chunks that do not cover the pages,
/// and a chunk whose descriptor is no dma-buf (the memfd itself), are the caller's misuse
/// (`Invalid`, `DmaBuf`), their descriptors closed. No dma-buf import is counted for any of them.
/// Where the device imports the pointer, each is made and no source is called.
/// Without a working source: memfd memory with no source, and with a source that fails, is no
/// refusal, made on every device: where the device refuses its pointer (RADV) a copy of its pages
/// (`copied.rs`), counted as the device's refusal (`WorkCounts::copied_host_buffers_device_refused`),
/// the failing source called once, else an import of the pointer, no source called. Chunks that
/// do not cover the pages, and a chunk whose descriptor is no dma-buf (the memfd itself), are the
/// caller's misuse (`Invalid`, `DmaBuf`), their descriptors closed. No dma-buf import is counted
/// for any of them. Where the device imports the pointer, each is made and no source is called.
#[test]
fn shared_memory_is_refused_without_a_working_source() {
fn shared_memory_without_a_working_source_is_a_copy_and_misuse_refused() {
const TEST: &str = "dma-buf refusals";
on_device(|device| {
let page = host_page();
@@ -1148,8 +1148,31 @@ fn shared_memory_is_refused_without_a_working_source() {
*counted.lock().unwrap() += 1;
Err(std::io::Error::other("no dma-buf"))
});
let failed = no_copy(device, &view, 0, 2 * page, failing);
check("a failing source", failed, not_importable);
// A view of its own, so that it lies in no copy the first buffer left.
let other = ram.view(&[(page, 2 * page)]);
let failed = no_copy(device, &other, 0, 2 * page, failing)
.unwrap_or_else(|error| panic!("{TEST}: a failing source: {error:?}"));
let copied = device.work_counts().copied_host_buffers_device_refused
- before_copies.copied_host_buffers_device_refused;
let backing = failed.host_backing();
match refuses {
Some(true) => assert_eq!(
(backing, copied),
(Some(HostBacking::Copied), 2),
"{TEST}: a failing source: a copy, for the device's refusal"
),
Some(false) => assert_eq!(
(backing, copied),
(Some(HostBacking::ImportedByPointer), 0),
"{TEST}: a failing source: the pointer's import"
),
None => eprintln!(
"{TEST}: a failing source, on {}: {backing:?}, {copied} copied for the device's \
refusal",
device.name()
),
}
drop(failed);
let expected_calls = usize::from(refuses == Some(true));
if refuses.is_some() {
assert_eq!(*calls.lock().unwrap(), expected_calls, "{TEST}: calls");
+4 -2
View File
@@ -3110,8 +3110,10 @@ bound.
- `metal2vulkan-degrade-copied-host-pages` (precision). `CopiedHostPages`: 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 (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 and the gate admitted failed, of the pointer or of the caller's dma-bufs (out of memory, or
the caller's gate (`HostMemory::with_import_gate`) refused the pages' import, or the device
refused the pointer's import and no dma-buf of the pages served it (no source given, or one that
failed or whose dma-bufs the device refused; RADV refuses shared memory, a `memfd` mapping among
it, `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 (a dma-buf's mapping) from NVIDIA's driver, whose failed
import of them breaks it (`RuntimeDriverDefect::UnpinnablePagesBreakHostMemory`), and no dma-buf
+7 -5
View File
@@ -199,9 +199,10 @@ Linux) imports a pointer through amdgpu's userptr, which takes anonymous pages o
`MAP_SHARED` mappings of a `memfd`, a shared memory object or any file (QEMU's
`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`.
pages (below), or with one that fails or whose dma-bufs the device refuses, 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 working
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
@@ -423,8 +424,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`,
- the device refuses the pointer of a no-copy buffer or heap, and no dma-buf of its pages serves
it (no source given, or one that fails or whose dma-bufs the device refuses): 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`);