Guest RAM: a driver's refusal of a span's dma-buf is remembered for the device's life
Counting the refusal left its cost: on a device whose import tax holds and whose driver refuses every udmabuf (NVIDIA 615.71.09, FAST-PATHS), every guest-RAM chunk asked the source for a dma-buf, failed vkAllocateMemory, let go of one descriptor (the driver closes the clone it was handed) and only then took its pointer. ram_span_as_dma_buf now tells the source's failures (an error, or an answer of other lengths: that span's alone, counted) from the driver's (anything once the source answered one dma-buf of the span: its memory types, the buffer, the import, the bind). A driver's refusal is counted once and remembered in the device's RamSource; every later span is refused at once, unasked and uncounted, and takes its pointer. The decision reads the device's own answer, never its identity. A transient failure of the driver's (memory that ran out) is remembered the same way, recorded in FAST-PATHS' NVIDIA row. Tests: a_driver_s_refused_dma_buf_sends_the_device_s_later_chunks_to_their_pointer forces the tax held and refuses every dma-buf import through a test stand-in for the driver (Memory::import_refusal, cfg(test), answered where the driver's import would run), on every device that imports dma-bufs: two chunks, the source asked once, the failed count 1, the second chunk by its pointer. The route test, where the driver itself refused (NVIDIA's), makes a buffer over the RAM's second chunk and asserts the same with the real driver. Without the remembering both ask the source a second time. (cherry picked from commit abddf2d2f1ed16a5ee6407cae04f7e10dfffb736)
This commit is contained in:
1 parent
c6be8ef2d8
commit
3fe18c4efd
7 files changed
+186
-34
No files matched your search
@@ -536,8 +536,14 @@ impl Device {
|
||||
/// which keeps the import and the source. Refused, everything made released, as
|
||||
/// [`Device::import_from_source`] is, where the device makes no such buffer, the source fails
|
||||
/// or answers more than one chunk, or the driver refuses the dma-buf: each of the last three
|
||||
/// counted (`WorkCounts::host_ram_dma_buf_imports_failed`), as NVIDIA's refusal of a udmabuf
|
||||
/// is (`ERROR_OUT_OF_DEVICE_MEMORY`, 615.71.09).
|
||||
/// counted (`WorkCounts::host_ram_dma_buf_imports_failed`). The driver's refusal, its own
|
||||
/// answer to the dma-buf the source made (NVIDIA's 615.71.09 refuses every udmabuf as out of
|
||||
/// device memory, and lets go of the descriptor it was handed: `memory.rs`'s
|
||||
/// `import_dma_buf`), is remembered for the device's life (`RamSource::refuse_dma_bufs`):
|
||||
/// from then on every span is refused here unasked and uncounted, and takes its pointer, so
|
||||
/// that no later chunk pays a dma-buf, a failed import and a descriptor for nothing. A
|
||||
/// transient failure of the driver's (memory that ran out) is remembered so too; the source's
|
||||
/// failures are that span's alone.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
@@ -549,31 +555,13 @@ impl Device {
|
||||
span: Range<u64>,
|
||||
source: &DmaBufSource,
|
||||
) -> Result<(vk::Buffer, Placement), Error> {
|
||||
let shared = self.shared();
|
||||
if !importer.imports_dma_bufs()
|
||||
|| !self.imports_dma_buf_buffers(vk::BufferCreateFlags::empty())
|
||||
|| shared.host_ram.refuses_dma_bufs()
|
||||
{
|
||||
return Err(not_importable());
|
||||
}
|
||||
// SAFETY: the caller's.
|
||||
let imported = unsafe { self.ram_span_dma_buf(importer, span, source) };
|
||||
if imported.is_err() {
|
||||
self.shared().counts.host_ram_dma_buf_import_failed();
|
||||
}
|
||||
imported
|
||||
}
|
||||
|
||||
/// [`Device::ram_span_as_dma_buf`] on a device that makes such buffers: the source asked for
|
||||
/// the span's dma-buf, and the driver's import of it.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// As [`Device::ram_span_as_dma_buf`]'s.
|
||||
unsafe fn ram_span_dma_buf(
|
||||
&self,
|
||||
importer: &Importer,
|
||||
span: Range<u64>,
|
||||
source: &DmaBufSource,
|
||||
) -> Result<(vk::Buffer, Placement), Error> {
|
||||
let start = NonNull::new(span.start as *mut u8).ok_or_else(not_importable)?;
|
||||
let length = span.end - span.start;
|
||||
let request = DmaBufRequest {
|
||||
@@ -581,12 +569,41 @@ impl Device {
|
||||
length,
|
||||
granule: length,
|
||||
};
|
||||
let chunks = source(request).map_err(|_| not_importable())?;
|
||||
if chunks.len() != 1 || !answers(&chunks, &request, importer.page) {
|
||||
return Err(invalid("use_host_ram", Problem::DmaBuf));
|
||||
let chunks = match source(request) {
|
||||
Ok(chunks) if chunks.len() == 1 && answers(&chunks, &request, importer.page) => chunks,
|
||||
answered => {
|
||||
shared.counts.host_ram_dma_buf_import_failed();
|
||||
return Err(match answered {
|
||||
Ok(_) => invalid("use_host_ram", Problem::DmaBuf),
|
||||
Err(_) => not_importable(),
|
||||
});
|
||||
}
|
||||
};
|
||||
// SAFETY: the caller's.
|
||||
let imported = unsafe { self.ram_span_import(importer, chunks, request, source) };
|
||||
if imported.is_err() {
|
||||
shared.counts.host_ram_dma_buf_import_failed();
|
||||
shared.host_ram.refuse_dma_bufs();
|
||||
}
|
||||
imported
|
||||
}
|
||||
|
||||
/// The driver's import of `chunks`, the one dma-buf `source` answered `request` with
|
||||
/// ([`Device::ram_span_as_dma_buf`]): the `VkBuffer` over it and its placement.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// As [`Device::ram_span_as_dma_buf`]'s, for the request's span.
|
||||
unsafe fn ram_span_import(
|
||||
&self,
|
||||
importer: &Importer,
|
||||
chunks: Vec<DmaBufChunk>,
|
||||
request: DmaBufRequest,
|
||||
source: &DmaBufSource,
|
||||
) -> Result<(vk::Buffer, Placement), Error> {
|
||||
let length = request.length;
|
||||
let window = ImportWindow {
|
||||
start: span.start,
|
||||
start: request.start.as_ptr() as u64,
|
||||
size: length,
|
||||
offset: 0,
|
||||
};
|
||||
@@ -603,7 +620,8 @@ impl Device {
|
||||
)
|
||||
}?;
|
||||
self.shared().counts.dma_buf_imported(pages.memories.len());
|
||||
let placement = Placement::dma_buf(Arc::new(pages), memory, type_index, length, start);
|
||||
let placement =
|
||||
Placement::dma_buf(Arc::new(pages), memory, type_index, length, request.start);
|
||||
Ok((buffer.raw, placement))
|
||||
}
|
||||
|
||||
|
||||
@@ -1972,6 +1972,9 @@ pub(crate) struct RamSource {
|
||||
/// ([`Device::count_host_ram_imports_against`]); 0 for the heaps'.
|
||||
#[cfg(test)]
|
||||
import_heaps: std::sync::atomic::AtomicU64,
|
||||
/// Whether the driver refused a dma-buf a RAM's source made of a span of its pages
|
||||
/// (`Device::ram_span_as_dma_buf`): from then on no span asks a source, for the device's life.
|
||||
dma_bufs_refused: AtomicBool,
|
||||
}
|
||||
|
||||
impl RamSource {
|
||||
@@ -2002,6 +2005,16 @@ impl RamSource {
|
||||
fn uses(&self, ram: &Arc<RamOnDevice>) -> bool {
|
||||
lock(&self.rams).iter().any(|used| Arc::ptr_eq(used, ram))
|
||||
}
|
||||
|
||||
/// Whether the driver refused a span's dma-buf ([`RamSource::refuse_dma_bufs`]).
|
||||
pub(crate) fn refuses_dma_bufs(&self) -> bool {
|
||||
self.dma_bufs_refused.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Remember that the driver refused a span's dma-buf, for the device's life.
|
||||
pub(crate) fn refuse_dma_bufs(&self) {
|
||||
self.dma_bufs_refused.store(true, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
@@ -2420,7 +2433,8 @@ impl Device {
|
||||
/// (its submission checks' import tax, `submit_check.rs`), the pointer where that fails; the
|
||||
/// pointer first elsewhere, the dma-buf where the pointer is refused. Counted:
|
||||
/// `WorkCounts::host_ram_dma_buf_imports` (each import of a dma-buf),
|
||||
/// `host_ram_dma_buf_imports_failed` (each that failed: NVIDIA's driver refuses a udmabuf) and
|
||||
/// `host_ram_dma_buf_imports_failed` (each that failed: NVIDIA's driver refuses a udmabuf,
|
||||
/// which the device then remembers, and no later span asks the source) and
|
||||
/// `host_ram_pointer_imports_failed` (each pointer import that failed).
|
||||
///
|
||||
/// # Safety
|
||||
|
||||
@@ -1931,6 +1931,11 @@ pub(super) struct Memory {
|
||||
/// the lock released where the driver's import runs.
|
||||
#[cfg(test)]
|
||||
pub(crate) import_hold: Option<Arc<dyn Fn() + Send + Sync>>,
|
||||
/// A test's stand-in for a driver that refuses every dma-buf import with this answer, as
|
||||
/// NVIDIA's 615.71.09 refuses a udmabuf ([`import_dma_buf`]): answered where the driver's
|
||||
/// import would run, the driver never asked.
|
||||
#[cfg(test)]
|
||||
pub(crate) import_refusal: Option<vk::Result>,
|
||||
/// A test's stand-in for every heap's budget of kept empty blocks ([`Memory::kept_budget`]).
|
||||
#[cfg(test)]
|
||||
pub(crate) kept_budget_stand_in: Option<u64>,
|
||||
@@ -2093,6 +2098,8 @@ impl Memory {
|
||||
#[cfg(test)]
|
||||
import_hold: None,
|
||||
#[cfg(test)]
|
||||
import_refusal: None,
|
||||
#[cfg(test)]
|
||||
kept_budget_stand_in: None,
|
||||
}
|
||||
}
|
||||
@@ -3721,7 +3728,10 @@ pub(super) fn import_dma_buf(
|
||||
)
|
||||
};
|
||||
#[cfg(test)]
|
||||
let hold = lock(memory).import_hold.clone();
|
||||
let (hold, refusal) = {
|
||||
let memory = lock(memory);
|
||||
(memory.import_hold.clone(), memory.import_refusal)
|
||||
};
|
||||
let mut failure = ImportFailure::NoMemoryType;
|
||||
for type_index in types {
|
||||
// The descriptor the driver takes, which it owns once the import succeeds and may
|
||||
@@ -3756,6 +3766,13 @@ pub(super) fn import_dma_buf(
|
||||
if let Some(hold) = &hold {
|
||||
hold();
|
||||
}
|
||||
// A test's stand-in for a driver that refuses the import: the clone, never handed over,
|
||||
// is closed as it goes.
|
||||
#[cfg(test)]
|
||||
if let Some(refused) = refusal {
|
||||
failure = ImportFailure::Vulkan(refused);
|
||||
continue;
|
||||
}
|
||||
// SAFETY: `info` and its chain live for the call; the type index is the device's and
|
||||
// allows the dma-buf (and the image, unbound and made for dma-bufs, whose size this
|
||||
// is). The descriptor it takes is the clone, whose fate is the driver's either way.
|
||||
|
||||
@@ -13,7 +13,9 @@
|
||||
//! dma-buf of it, the source asked once for the chunk's whole span at the RAM's addresses, with
|
||||
//! a granule of its length (`WorkCounts::host_ram_dma_buf_imports`), or, where the driver
|
||||
//! refuses that dma-buf (NVIDIA's refuses a udmabuf), by its pointer, the refusal counted
|
||||
//! (`host_ram_dma_buf_imports_failed`); where the tax is not held (forced so), the pointer is
|
||||
//! (`host_ram_dma_buf_imports_failed`) and remembered: the next chunk goes to its pointer, the
|
||||
//! source unasked (shown on every device through a stand-in for the driver's refusal, and on
|
||||
//! NVIDIA's by the driver itself); where the tax is not held (forced so), the pointer is
|
||||
//! imported where the driver takes it, the source never asked, and the dma-buf only where it
|
||||
//! refuses it. Either way the buffer is bound in the chunk (`chunk_bound_buffers_made`), reports
|
||||
//! `InHostRam`, and a second buffer over the chunk imports nothing more.
|
||||
@@ -474,8 +476,31 @@ fn where_the_import_tax_holds_a_chunk_takes_its_dma_buf_first() {
|
||||
outside.is_empty(),
|
||||
"the source was asked at the view's addresses: {outside:?}"
|
||||
);
|
||||
// A driver's refusal is the device's to remember (NVIDIA's of every udmabuf): a buffer over
|
||||
// the RAM's second chunk takes that chunk's pointer, the source unasked and nothing counted
|
||||
// again.
|
||||
let third = (refused == 1).then(|| {
|
||||
let (counted, requested) = (device.work_counts(), asked.lock().unwrap().requests.len());
|
||||
let third = buffer(&device, view, start, 64 * MIB + AT, LENGTH);
|
||||
let after = device.work_counts();
|
||||
assert_eq!(
|
||||
asked.lock().unwrap().requests.len(),
|
||||
requested,
|
||||
"the source asked again after the driver refused"
|
||||
);
|
||||
assert_eq!(
|
||||
after.host_ram_dma_buf_imports_failed,
|
||||
counted.host_ram_dma_buf_imports_failed
|
||||
);
|
||||
assert_eq!(
|
||||
after.host_ram_chunks_imported - counted.host_ram_chunks_imported,
|
||||
1
|
||||
);
|
||||
assert_eq!(third.host_backing(), Some(HostBacking::InHostRam));
|
||||
third
|
||||
});
|
||||
// Once the use and the objects go, the keep-alive goes.
|
||||
drop((texture, first, second, used, ram));
|
||||
drop((texture, first, second, third, used, ram));
|
||||
let began = Instant::now();
|
||||
while !dropped.load(Ordering::SeqCst) {
|
||||
assert!(
|
||||
@@ -486,6 +511,79 @@ fn where_the_import_tax_holds_a_chunk_takes_its_dma_buf_first() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Where the driver refuses a chunk's dma-buf (a test's stand-in for NVIDIA's refusal of every
|
||||
/// udmabuf, `Memory::import_refusal`, on every device that imports dma-bufs; the import tax forced
|
||||
/// held), the device remembers the refusal: the RAM's first chunk asks the source, its dma-buf is
|
||||
/// refused and counted (`WorkCounts::host_ram_dma_buf_imports_failed`), and the chunk takes its
|
||||
/// pointer; the second chunk goes to its pointer at once, the source unasked and nothing counted
|
||||
/// again. On a device that refuses pointers to shared memory (RADV's) neither chunk is imported,
|
||||
/// and the source is still asked once.
|
||||
#[test]
|
||||
fn a_driver_s_refused_dma_buf_sends_the_device_s_later_chunks_to_their_pointer() {
|
||||
let scratch = Scratch::new();
|
||||
let Some(device) = held_device(&scratch, None) else {
|
||||
return;
|
||||
};
|
||||
if !imports_dma_bufs(&device) {
|
||||
eprintln!("HOST-RAM-DMA-BUF: the device imports no dma-bufs; nothing to test");
|
||||
return;
|
||||
}
|
||||
(device.shared().submit_checks).force(Check::ImportTax, Verdict::Holds);
|
||||
let refusal = ash::vk::Result::ERROR_OUT_OF_DEVICE_MEMORY;
|
||||
crate::device::lock(&device.shared().memory).import_refusal = Some(refusal);
|
||||
let pointers = device.imports_shared_host_pointers().unwrap();
|
||||
let (ram, start, view, asked, _dropped) = ram(true);
|
||||
let used = device.use_host_ram(&ram).unwrap();
|
||||
let counts = |device: &Device| {
|
||||
let counts = device.work_counts();
|
||||
let requests = asked.lock().unwrap().requests.clone();
|
||||
let imported = (
|
||||
counts.host_ram_chunks_imported,
|
||||
counts.host_ram_dma_buf_imports,
|
||||
);
|
||||
(imported, counts.host_ram_dma_buf_imports_failed, requests)
|
||||
};
|
||||
let (before, failed_before, _) = counts(&device);
|
||||
let first = try_buffer(&device, view, start, AT, LENGTH);
|
||||
let ((chunks, dma_bufs), failed, requests) = counts(&device);
|
||||
let second = try_buffer(&device, view, start, 64 * MIB + AT, LENGTH);
|
||||
let ((chunks_after, dma_bufs_after), failed_after, requests_after) = counts(&device);
|
||||
println!(
|
||||
"HOST-RAM-DMA-BUF {}: the driver refuses the dma-bufs: the first chunk {} ({} refused, the \
|
||||
source asked {}), the second {} ({} refused, the source asked {} in all)",
|
||||
device.name(),
|
||||
match &first {
|
||||
Ok(_) => "made",
|
||||
Err(_) => "refused",
|
||||
},
|
||||
failed - failed_before,
|
||||
requests.len(),
|
||||
match &second {
|
||||
Ok(_) => "made",
|
||||
Err(_) => "refused",
|
||||
},
|
||||
failed_after - failed,
|
||||
requests_after.len(),
|
||||
);
|
||||
assert_eq!(requests, [(start, 64 * MIB, 64 * MIB)]);
|
||||
assert_eq!(failed - failed_before, 1, "the first chunk's refusal");
|
||||
assert_eq!(
|
||||
requests_after, requests,
|
||||
"the source asked for the second chunk"
|
||||
);
|
||||
assert_eq!(failed_after, failed, "the second chunk's dma-buf tried");
|
||||
assert_eq!((dma_bufs, dma_bufs_after), (before.1, before.1));
|
||||
if pointers {
|
||||
// Each chunk by its pointer.
|
||||
assert_eq!((chunks - before.0, chunks_after - chunks), (1, 1));
|
||||
for buffer in [&first, &second] {
|
||||
let backing = buffer.as_ref().map(Buffer::host_backing).ok().flatten();
|
||||
assert_eq!(backing, Some(HostBacking::InHostRam));
|
||||
}
|
||||
}
|
||||
drop((first, second, used, ram));
|
||||
}
|
||||
|
||||
/// Where the pointer goes first (the import tax not held, forced so on the device), a chunk whose
|
||||
/// pointer the device refuses is imported as a dma-buf of the RAM's source: the refusal counted
|
||||
/// (`WorkCounts::host_ram_pointer_imports_failed`), then the dma-buf (`host_ram_dma_buf_imports`).
|
||||
|
||||
@@ -6968,7 +6968,8 @@ pub struct WorkCounts {
|
||||
/// refuses a udmabuf of host pages as out of device memory). Each was then imported by its
|
||||
/// pointer where the dma-buf went first, where the device's held pointer imports add to every
|
||||
/// submission's cost, which that import then adds (`host_ram_pointer_imports_failed` where
|
||||
/// it failed too).
|
||||
/// it failed too). The driver's refusal is remembered for the device's life: no later span
|
||||
/// asks a source, and none is counted here again.
|
||||
pub host_ram_dma_buf_imports_failed: u64,
|
||||
/// Of `host_ram_chunks_imported`, those the device's own thread imported ahead of every
|
||||
/// object over them, so that no making waited for their import: the few chunks past the one
|
||||
|
||||
+4
-1
@@ -1273,7 +1273,10 @@ driver's. Each is open work.
|
||||
well-behaved driver its descriptor back would need a measured driver fact; the pages' other route
|
||||
(importing the pointer) is the one NVIDIA takes for writable pages and is unaffected: a guest
|
||||
RAM's chunk whose dma-buf it refused is imported by its pointer, the refusal counted
|
||||
(`WorkCounts::host_ram_dma_buf_imports_failed`). A device that imports dma-bufs (RADV) is
|
||||
(`WorkCounts::host_ram_dma_buf_imports_failed`) and remembered for the device's life, so the
|
||||
device's later chunks take their pointer without asking the source, and no second descriptor is
|
||||
let go of. A device whose driver failed one such import for want of memory remembers it the same
|
||||
way and pays the import tax for its later chunks. A device that imports dma-bufs (RADV) is
|
||||
**unaudited** for the clone.
|
||||
|
||||
- **A pointer import of pages the kernel cannot pin on NVIDIA** (a dma-buf's mapping, `VM_PFNMAP`)
|
||||
|
||||
+2
-1
@@ -634,7 +634,8 @@ submission, can give the device the guest's RAM once instead:
|
||||
is 64, a chunk's size). A texture over an object bound in such a chunk is an image over a dma-buf
|
||||
of its own pages, as over an object's own dma-buf imports, asked of the same source. A source that
|
||||
fails, or a dma-buf the driver refuses (NVIDIA's refuses a udmabuf), leaves the chunk to its
|
||||
pointer (`WorkCounts::host_ram_dma_buf_imports_failed`).
|
||||
pointer (`WorkCounts::host_ram_dma_buf_imports_failed`); a driver's refusal is remembered for the
|
||||
device's life, and its later chunks take their pointer without asking the source.
|
||||
- `HostRam::with_largest_dma_buf(bytes)` says how long one dma-buf of the source may be (for
|
||||
`udmabuf`, its `size_limit_mb` parameter, read from `/sys/module/udmabuf/parameters/`). On a
|
||||
device whose held imports add to the cost of every submission, with a source, each listed
|
||||
|
||||
Reference in new issue
Block a user