Place the in-place check's buffers outside the Metal address space
What: the device's in-place check (`in_place_check.rs`) makes its 40 MiB buffer per class of pages at `AddressPlacement::Outside`: the Fresh class through `Device::new_buffer_placed` (what `new_buffer` does, at a placement; `new_buffer` is it at `Any`), the Pointer and DmaBuf classes through `no_copy_kept`'s descriptor `with_placement(Outside)`. address.rs's "Outside the space" and the check's module docs say so. Why: the check runs on a thread of its own on a new RUNTIME_VERSION's first device per host, while the program makes its resources. At `Any` its buffers took the lowest free 40 MiB of the Metal space, from 2^32, so a program pinning there (`AddressPlacement::At`, `setPinnedGPUAddress:`) got `PlacementConflict::Held` for a range no object of its own held, breaking address.rs's rule that the runtime's own buffers lie in `[2^47, 2^48)`. The consumer's repro refused a pin at 2^32 in 5 runs of 5 on MoltenVK. The placement is the only thing that changes: the address space hands out a number, and the buffers' memory, fresh pages, import, image and first move do not read it (`placed_new_buffer`, `place_window`), so no verdict can. Test: `tests::in_place_check::the_in_place_check_takes_no_range_of_the_metal_space` (device test): with the space past its first GiB held by one address pool and a 16 KiB pin every 16 MiB of that GiB held through the check (no free run fits a check buffer), the check answers every class as a reference device's check over an empty space, and the pins between the held ones, made and released in turn while it runs, are all made. Without the fix it fails every time: Pointer and Fresh come back Unchecked (their buffers found no range). Verified on the M3 Max (MoltenVK): the test fails on the base (Pointer and Fresh Unchecked against Passed) and passes with the fix (Pointer Passed, Fresh Passed, DmaBuf Unchecked, as the reference; 4921 rounds of pins while the check ran). (cherry picked from commit 113c132f47b2f9c258c3a22251d949419a9f36e4)
This commit is contained in:
1 parent
bd264821bf
commit
cbd20364dd
5 files changed
+132
-8
No files matched your search
@@ -42,9 +42,11 @@
|
||||
//! buffer in its slots is gone. Which slot a buffer takes, and when a pool is needed or goes, is
|
||||
//! the caller's to model, as are Metal's own allocations in its pools.
|
||||
//! - **Outside the space**: the runtime's own buffers (the index buffers it generates, staging for
|
||||
//! fills, uploads and copies, scratch, tile memory, shadows) take ranges of a second space above
|
||||
//! it, `[2^47, 2^48)`, so a program's buffer addresses never depend on when the runtime's own
|
||||
//! work completes, as Metal's own allocations never move a program's.
|
||||
//! fills, uploads and copies, scratch, tile memory, shadows, and the buffers of the device's
|
||||
//! in-place check, `in_place_check.rs`, which runs on a thread of its own while the program makes
|
||||
//! its resources) take ranges of a second space above it, `[2^47, 2^48)`, so a program's buffer
|
||||
//! addresses never depend on whether or when the runtime's own work runs or completes, as
|
||||
//! Metal's own allocations never move a program's, and no pin of the program's meets them.
|
||||
//! [`AddressPlacement::Outside`], a runtime extension, puts a caller's helper buffer there too. A
|
||||
//! pin never reaches it.
|
||||
//! - **Indirect command buffers' records**: an indirect command buffer's ID is the one Metal's pool
|
||||
|
||||
@@ -1913,10 +1913,22 @@ impl Device {
|
||||
/// answers nil, its validation stopping it), `Memoryless` storage
|
||||
/// (`Invalid`).
|
||||
pub fn new_buffer(&self, length: u64, options: ResourceOptions) -> Result<Buffer, Error> {
|
||||
self.new_buffer_placed(length, options, AddressPlacement::Any)
|
||||
}
|
||||
|
||||
/// [`Device::new_buffer`] at `placement` in the address space: made, refused, announced and
|
||||
/// kept among the program's resources as that is, at `AddressPlacement::Outside` for a buffer
|
||||
/// whose address Metal shows no program, so that no program buffer's address depends on it
|
||||
/// (`address.rs`).
|
||||
pub(super) fn new_buffer_placed(
|
||||
&self,
|
||||
length: u64,
|
||||
options: ResourceOptions,
|
||||
placement: AddressPlacement,
|
||||
) -> Result<Buffer, Error> {
|
||||
self.keep_room();
|
||||
let options = self.own_memory_options("new_buffer", options)?;
|
||||
let (buffer, class) =
|
||||
self.buffer_of_length("new_buffer", length, options, AddressPlacement::Any)?;
|
||||
let (buffer, class) = self.buffer_of_length("new_buffer", length, options, placement)?;
|
||||
self.degrade_system_memory(&buffer, class, || {
|
||||
format!("a `Shared` buffer of {length} bytes")
|
||||
});
|
||||
|
||||
@@ -15,6 +15,11 @@
|
||||
//! must then read the CPU's write where the CPU wrote it, and the pattern elsewhere. Each class
|
||||
//! runs every shape of [`SHAPES`] (texels of 1, 2, 4, 8 and 16 bytes, rows apart or one after
|
||||
//! another, and 3840x2160 at 4 bytes) [`ROUNDS`] times.
|
||||
//! - **Its buffers' addresses** lie outside the Metal address space (`AddressPlacement::Outside`,
|
||||
//! `address.rs`), each made as the product makes its class's otherwise: the check runs while
|
||||
//! the program makes its resources, so a range of the space it took would refuse a pin of the
|
||||
//! program's there, or move a range the runtime picks for it. Its pages, their import, the
|
||||
//! image and the move do not depend on the address, so neither does a verdict.
|
||||
//! - **Its cost** is the pattern's fill and compare of each window, at the speed of a copy and a
|
||||
//! comparison of memory (a 3840x2160 surface's 33 MB among them), and each image's making and
|
||||
//! first move: the writes themselves run only while the move does.
|
||||
@@ -42,7 +47,9 @@ use crate::exit_guard;
|
||||
use crate::host_memory::HostMemory;
|
||||
use crate::in_place::{encode, mark_checking, record_name, PagesClass, Verdict};
|
||||
use crate::values::ResourceOptions;
|
||||
use crate::{Buffer, BufferContents, BufferDescriptor, Device, TextureDescriptor};
|
||||
use crate::{
|
||||
AddressPlacement, Buffer, BufferContents, BufferDescriptor, Device, TextureDescriptor,
|
||||
};
|
||||
use metal2vulkan::device::HostPageTexturePolicy;
|
||||
use metal2vulkan::metal::{PixelFormat, TextureUsage};
|
||||
use std::ptr::NonNull;
|
||||
@@ -285,7 +292,9 @@ impl ClassPages {
|
||||
match class {
|
||||
PagesClass::Pointer => pointer_pages(device, length),
|
||||
PagesClass::Fresh => {
|
||||
let buffer = device.new_buffer(length, ResourceOptions::shared()).ok()?;
|
||||
let buffer = device
|
||||
.new_buffer_placed(length, ResourceOptions::shared(), AddressPlacement::Outside)
|
||||
.ok()?;
|
||||
if !buffer.in_fresh_pages() {
|
||||
return None;
|
||||
}
|
||||
@@ -330,7 +339,8 @@ fn no_copy_kept(
|
||||
deallocator: None,
|
||||
keep_alive: Some(pages),
|
||||
};
|
||||
let descriptor = BufferDescriptor::new(length, ResourceOptions::shared(), contents);
|
||||
let descriptor = BufferDescriptor::new(length, ResourceOptions::shared(), contents)
|
||||
.with_placement(AddressPlacement::Outside);
|
||||
device.new_buffer_with_descriptor(descriptor).ok()
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
//! The device's check of the first move in place (`in_place_check.rs`) takes no range of the Metal
|
||||
//! address space: its buffers, one per class of pages, lie outside it (`address.rs`, "Outside the
|
||||
//! space"), so no pin or pick of the program's depends on whether or when the check runs, and the
|
||||
//! space's state changes no verdict.
|
||||
//!
|
||||
//! A device test: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`,
|
||||
//! `m2v_cpu_icd`), on the kind of device the cfg selects. Each device's shader cache is a fresh
|
||||
//! directory, so no record holds its verdicts and its check runs.
|
||||
|
||||
use super::blit_holds::{device, Scratch};
|
||||
use crate::address::{PAGE, SPACE_BASE, SPACE_END};
|
||||
use crate::{
|
||||
AddressPlacement, Buffer, BufferContents, BufferDescriptor, Device, Error, GpuAddress,
|
||||
ResourceOptions,
|
||||
};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
/// The longest a check may take.
|
||||
const WAIT: Duration = Duration::from_secs(120);
|
||||
|
||||
/// The low part of the space the pins cover: its first GiB.
|
||||
const LOW: u64 = 1 << 30;
|
||||
|
||||
/// A pin every this many bytes of [`LOW`], of [`PINNED`] bytes: every second one held through the
|
||||
/// check leaves no free run of the space a check's buffer of 40 MiB fits in.
|
||||
const STRIDE: u64 = 8 << 20;
|
||||
const PINNED: u64 = 16 << 10;
|
||||
|
||||
/// A buffer of [`PINNED`] bytes pinned at `at`.
|
||||
fn pin(device: &Device, at: u64) -> Result<Buffer, Error> {
|
||||
let descriptor = BufferDescriptor::new(PINNED, ResourceOptions::shared(), BufferContents::Zero)
|
||||
.with_placement(AddressPlacement::At(GpuAddress(at)));
|
||||
device.new_buffer_with_descriptor(descriptor)
|
||||
}
|
||||
|
||||
/// The pin addresses of [`LOW`] at even multiples of [`STRIDE`], or at odd ones.
|
||||
fn pins(odd: bool) -> impl Iterator<Item = u64> {
|
||||
(0..LOW / STRIDE)
|
||||
.filter(move |k| k % 2 == u64::from(odd))
|
||||
.map(|k| SPACE_BASE + k * STRIDE)
|
||||
}
|
||||
|
||||
/// A device whose check runs with the Metal space held but for runs shorter than a check's
|
||||
/// buffer (the space past its first GiB by one pool, a pin every 16 MiB of that GiB) answers
|
||||
/// every class as a device whose check ran with the space empty does, and each pin between the
|
||||
/// held ones, made and released in turn until the check settles, is made. A check whose buffer
|
||||
/// took a range of the space would find none: that class would be unchecked, or a pin refused
|
||||
/// (`PlacementConflict::Held`).
|
||||
#[test]
|
||||
fn the_in_place_check_takes_no_range_of_the_metal_space() {
|
||||
let verdicts = |device: &Device| -> Vec<String> {
|
||||
device.in_place_check_report().into_iter().take(3).collect()
|
||||
};
|
||||
let reference = {
|
||||
let scratch = Scratch::new("in-place-check-reference");
|
||||
let device = device(&scratch.0);
|
||||
assert!(
|
||||
device.settle_in_place_checks(WAIT),
|
||||
"the reference check answered"
|
||||
);
|
||||
verdicts(&device)
|
||||
};
|
||||
let scratch = Scratch::new("in-place-check-placement");
|
||||
let device = device(&scratch.0);
|
||||
let rest = SPACE_BASE + LOW;
|
||||
// The pool's span is its pages and one guard page, to the space's end.
|
||||
let pool = device
|
||||
.reserve_address_pool(
|
||||
AddressPlacement::At(GpuAddress(rest)),
|
||||
SPACE_END - rest - PAGE,
|
||||
)
|
||||
.unwrap();
|
||||
let held: Vec<Buffer> = pins(false)
|
||||
.map(|at| pin(&device, at).unwrap_or_else(|error| panic!("the pin at {at:#x}: {error}")))
|
||||
.collect();
|
||||
let started = Instant::now();
|
||||
let mut rounds = 0;
|
||||
while !device.settle_in_place_checks(Duration::ZERO) {
|
||||
assert!(started.elapsed() < WAIT, "the check answered");
|
||||
let made: Vec<Buffer> = pins(true)
|
||||
.map(|at| {
|
||||
pin(&device, at).unwrap_or_else(|error| {
|
||||
panic!("the pin at {at:#x} while the check ran: {error}")
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
drop(made);
|
||||
rounds += 1;
|
||||
}
|
||||
let answered = verdicts(&device);
|
||||
eprintln!(
|
||||
"IN-PLACE-CHECK-PLACEMENT on {}: {answered:?} with the space held, {rounds} rounds of \
|
||||
pins while it ran; {reference:?} with it empty",
|
||||
device.name()
|
||||
);
|
||||
assert_eq!(answered, reference, "the verdicts with the space held");
|
||||
drop((held, pool));
|
||||
}
|
||||
@@ -153,6 +153,8 @@ mod host_step_neutrals;
|
||||
mod image_barriers;
|
||||
mod image_plan;
|
||||
mod in_place;
|
||||
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
|
||||
mod in_place_check;
|
||||
mod inbox;
|
||||
mod indirect_command_buffer;
|
||||
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
|
||||
|
||||
Reference in new issue
Block a user