Three follow-ups to remembering a driver's refusal of a guest RAM's dma-buf
(3fe18c4efd):
1. Run-list imports consult the remembered refusal. ram_runs_as_dma_buf now
returns not-importable, the source of run lists unasked, once the device
remembers a refusal, and remembers the driver's own refusal of a list's
dma-buf the same way a span's is (the source's failures stay the list's
alone). Before, every copy over a chunk not imported asked the source of
run lists again and paid a udmabuf and a failed import.
2. A RAM's plan asks the driver before it lists the RAM. Plan::listed means
the RAM's chunks are its dma-bufs; it was chosen from the import tax alone,
so on a taxed driver that refuses udmabufs (NVIDIA 615.71.09) the RAM got
512 MiB chunks imported by pointer, none ahead. ram_plan now, where the plan
would be listed, asks the driver with the RAM's first host page
(Device::ram_takes_dma_bufs: ram_span_as_dma_buf over one page, released at
once, the precedent being shared_refused's one-page question), and lists the
RAM only where the driver took it. A refusal there is counted and
remembered, so the RAM and every RAM after it keep 64 MiB chunks imported
ahead by pointer. Re-choosing a live RAM's plan was rejected: its chunk
geometry and its thread are fixed when the use begins. discard_import takes
the raw VkBuffer so the question's import can be released.
3. The listed tests (where_each_listed_import_costs..., where_the_ram_fits...,
the two tile tests through copies_by_lists) no longer expect dma-buf imports
wherever the measured tax holds. They force the tax held, so the listed path
runs on every device in every run, and read the plan the device chose
(Device::host_ram_chunks), asserting it is listed exactly where the device
imports dma-bufs and the driver took the use's page. The resident test now
allows the use its one page.
Tests:
- a_driver_s_refused_dma_buf_sends_copies_past_the_source_of_run_lists (new):
stand-in refusal set after a listed use; the first copy asks the source of
run lists once and the source of dma-bufs not at all, the second asks
neither (fails without the consult or without remembering a list's refusal).
- a_driver_s_refusal_as_the_use_begins_keeps_the_ram_s_chunks_short (new):
stand-in refusal before the use; the RAM is not listed, the source asked for
the first page only, the refusal counted once (fails without 2).
- a_driver_s_refused_dma_buf_sends_the_device_s_later_chunks_to_their_pointer:
the stand-in now refuses after the use; a RAM used after the refusal is not
listed and its source is unasked.
- The route test reads the use's question and the plan.
Docs: host_ram module, use_host_ram, WorkCounts, HOWTO and the FAST-PATHS
NVIDIA dma-buf row describe the question and the remembered refusal.
Verification: in the follow-up message (gem10 lavapipe suite and negative
controls; 5080 release).
(cherry picked from commit 77f336e98c2a31aa56e45c5eb7659fa28f6f3ad6)
148 lines
6.9 KiB
Rust
148 lines
6.9 KiB
Rust
//! Whether a device imports a pointer to shared memory ([`Device::imports_shared_host_pointers`]),
|
|
//! found by importing one page of it.
|
|
//!
|
|
//! Drivers differ there, and nothing the device reports says which way: `VK_EXT_external_memory_
|
|
//! host` answers the memory types a pointer may take, and RADV answers them for a pointer whose
|
|
//! allocation it then refuses. RADV imports a pointer through amdgpu's userptr, which the kernel
|
|
//! takes only over anonymous private pages: it refused anonymous shared memory and every mapping
|
|
//! of a file, `memfd`s among them, typed. NVIDIA and lavapipe imported the same memory. So the
|
|
//! device is asked by doing it, once: one page of a `memfd` mapped shared, read and write (on
|
|
//! Linux, as QEMU's `memory-backend-memfd,share=on` guest RAM is; anonymous shared memory on other
|
|
//! Unix systems), imported as a no-copy buffer's pages are, and released at once.
|
|
|
|
use super::error::{Error, MemoryKind};
|
|
use super::host_memory::{import_window, refused};
|
|
use super::memory::MemoryClass;
|
|
use super::Device;
|
|
use std::ptr::NonNull;
|
|
|
|
impl Device {
|
|
/// Whether the device imports a pointer to shared memory, as a no-copy buffer's import takes
|
|
/// it (`VK_EXT_external_memory_host`): a runtime extension on Unix systems, with no Metal
|
|
/// counterpart (Metal takes any page). Found once per device by importing one page of a
|
|
/// `memfd` mapped shared, read and write (anonymous shared memory off Linux), which is
|
|
/// released at once, and answered from that after; the import is counted as a no-copy
|
|
/// buffer's is. False for a device that imports no host memory.
|
|
///
|
|
/// Where it is false, a no-copy buffer over shared memory is imported only from the caller's
|
|
/// dma-buf source ([`HostMemory::with_dma_buf`](crate::HostMemory::with_dma_buf)), and without
|
|
/// 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
|
|
/// nothing of the process's mapping), so a caller need not make a writable second mapping of
|
|
/// them for the import. Measured: MoltenVK (an M3 Max) and lavapipe answer true, RADV (a
|
|
/// Radeon 780M and an RX 7900 XTX, Mesa 26.2.4) false; NVIDIA's driver imported such memory
|
|
/// where the no-copy buffers were measured, though this answer was not asked there.
|
|
///
|
|
/// Fails only where the answer cannot be found: out of host memory for the page, or the
|
|
/// device lost. The device's refusal of the pointer is no failure but the answer, false.
|
|
pub fn imports_shared_host_pointers(&self) -> Result<bool, Error> {
|
|
self.at_serving(|device| device.imports_shared_host_pointers_now())
|
|
}
|
|
|
|
/// [`Device::imports_shared_host_pointers`] on the generation that serves (`device/line.rs`).
|
|
pub(crate) fn imports_shared_host_pointers_now(&self) -> Result<bool, Error> {
|
|
let shared = self.shared();
|
|
if let Some(&answer) = shared.shared_pointer_imports.get() {
|
|
return Ok(answer);
|
|
}
|
|
let Ok(importer) = shared.host_import.as_ref() else {
|
|
return Ok(false);
|
|
};
|
|
if self.is_lost() {
|
|
return Err(self.lost_refusal());
|
|
}
|
|
let unit = importer.page.max(importer.alignment);
|
|
let memory = SharedPages::new(2 * unit).ok_or(Error::OutOfMemory(MemoryKind::Host))?;
|
|
// The first whole unit of the pages: the import's window is exactly it.
|
|
let at = (memory.at.as_ptr() as u64).next_multiple_of(unit);
|
|
let pointer = NonNull::new(at as *mut u8).ok_or(Error::OutOfMemory(MemoryKind::Host))?;
|
|
let window = import_window(at, unit, importer.page, importer.alignment)
|
|
.map_err(|_| Error::OutOfMemory(MemoryKind::Host))?;
|
|
// SAFETY: the window's pages are this call's mapping, which stays mapped until the
|
|
// placement is freed below.
|
|
let imported = unsafe {
|
|
self.import_pointer(importer, pointer, unit, &window, MemoryClass::Shared, false)
|
|
};
|
|
let answer = match imported {
|
|
Ok((buffer, placement)) => {
|
|
self.discard_import(buffer.raw, placement);
|
|
true
|
|
}
|
|
Err(error) if refused(&error) => false,
|
|
Err(error) => return Err(error),
|
|
};
|
|
drop(memory);
|
|
Ok(*shared.shared_pointer_imports.get_or_init(|| answer))
|
|
}
|
|
}
|
|
|
|
/// Pages of shared memory mapped read and write, unmapped when it drops: of a `memfd` on Linux,
|
|
/// anonymous elsewhere.
|
|
struct SharedPages {
|
|
at: NonNull<u8>,
|
|
length: usize,
|
|
}
|
|
|
|
impl SharedPages {
|
|
/// `length` bytes of them; `None` where they cannot be made.
|
|
fn new(length: u64) -> Option<SharedPages> {
|
|
let length = usize::try_from(length).ok()?;
|
|
let (flags, fd) = SharedPages::backing(length)?;
|
|
// SAFETY: a fresh mapping of `fd`'s first `length` bytes (or of anonymous memory, for -1),
|
|
// which the kernel places.
|
|
let at = unsafe {
|
|
libc::mmap(
|
|
std::ptr::null_mut(),
|
|
length,
|
|
libc::PROT_READ | libc::PROT_WRITE,
|
|
flags,
|
|
fd.as_ref().map_or(-1, std::os::fd::AsRawFd::as_raw_fd),
|
|
0,
|
|
)
|
|
};
|
|
// The mapping keeps the file; its descriptor closes here.
|
|
drop(fd);
|
|
if at == libc::MAP_FAILED {
|
|
return None;
|
|
}
|
|
Some(SharedPages {
|
|
at: NonNull::new(at.cast())?,
|
|
length,
|
|
})
|
|
}
|
|
|
|
/// The mapping's flags and file: a `memfd` of `length` bytes on Linux.
|
|
#[cfg(target_os = "linux")]
|
|
fn backing(length: usize) -> Option<(libc::c_int, Option<std::os::fd::OwnedFd>)> {
|
|
use std::os::fd::FromRawFd;
|
|
// SAFETY: a name and flags the call takes.
|
|
let fd = unsafe {
|
|
libc::memfd_create(c"metal2vulkan-shared-pointer".as_ptr(), libc::MFD_CLOEXEC)
|
|
};
|
|
if fd < 0 {
|
|
return None;
|
|
}
|
|
// SAFETY: `fd` was just made and is owned here alone.
|
|
let fd = unsafe { std::os::fd::OwnedFd::from_raw_fd(fd) };
|
|
// SAFETY: a live descriptor of a `memfd` this call made.
|
|
let sized =
|
|
unsafe { libc::ftruncate(std::os::fd::AsRawFd::as_raw_fd(&fd), length as libc::off_t) };
|
|
(sized == 0).then_some((libc::MAP_SHARED, Some(fd)))
|
|
}
|
|
|
|
/// The mapping's flags: anonymous shared memory off Linux.
|
|
#[cfg(not(target_os = "linux"))]
|
|
fn backing(_length: usize) -> Option<(libc::c_int, Option<std::os::fd::OwnedFd>)> {
|
|
Some((libc::MAP_SHARED | libc::MAP_ANON, None))
|
|
}
|
|
}
|
|
|
|
impl Drop for SharedPages {
|
|
fn drop(&mut self) {
|
|
// SAFETY: this object's own mapping, which nothing imports any more.
|
|
unsafe { libc::munmap(self.at.as_ptr().cast(), self.length) };
|
|
}
|
|
}
|