Pin that a dropped, uncommitted command buffer touches no host memory once dropped
What: runtime_lane::dropped_uncommitted. A pass of two draws of the owned ob3_index_buffer fixture (a vertex function, rasterization disabled, that marks seen[min(vertex_id, 7)]) writes a no-copy buffer over a second mapping of a page of guest RAM (a shared memory object, as runtime_lane::host_memory maps it; GuestRam is now visible to the lane's modules). The state's creation predicts a point list, so the line's and the triangle's draws each hand their own pipeline's making to the device's pool: the test asserts both went to the pool (pipelines_pooled +2, from an idle pool) and that the pool was still busy as the encoding ended, so the pass was left pending (Deferred::RenderPass, making()). The command buffer is dropped uncommitted, the buffer let go of, and the page unmapped in place at once (replaced by a PROT_NONE mapping, so a CPU access faults). After the makings finish (pipelines_settled), a command buffer committed on the same queue draws with the pipelines now made through a no-copy buffer over the next page, and completes without error. The dropped page keeps every byte, the next page holds exactly the eight marks, a third page is untouched, and the validation layer reports nothing (host_memory::on_device). Why: the consumer now relies on this contract to unmap a guest task's pages as soon as its objects go: an uncommitted command buffer's deferred work, a pipeline-pending draw among it, never reaches the scheduler, which takes committed batches only and hands a host step over only for a committed line's head (gate_heads). Verified on the M3 Max (MoltenVK, --cfg m2v_gpu, debug, under maclock and memcap): the case passed 8 of 8 runs, the pending assertion included; the host_memory lane (25 cases) passed beside it, 3 of 3. Negative control: committing the command buffer before its drop (the deferred work reaching the scheduler) fails the case, "the dropped command buffer's draws reached the guest page": on this GPU a write through the no-copy buffer lands in the shared object's page even after the CPU's view of it is unmapped, which is what the guest RAM check reads. Restored and touched. A hook that hands an uncommitted buffer's step over from inside the runtime is not practical for this lane: validation reaches the runtime only through its public API, so it would be a hidden public entry point in a published crate for one test. clippy --workspace --all-targets --all-features -D warnings, plain and with --cfg m2v_gpu: clean. (cherry picked from commit a35b1b6a2ecbb449b6562bb2678f2d62e2997ce9) (cherry picked from commit 3f7ac2c41f47f76219c4407f530532a60cc5cea8)
This commit is contained in:
1 parent
dd986f0aae
commit
e068edfdde
3 files changed
+176
-5
No files matched your search
@@ -394,6 +394,8 @@ mod draw_work;
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
mod driver_deviations;
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
mod dropped_uncommitted;
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
mod dynamic_libraries;
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
mod escaped_slots;
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
//! A command buffer dropped uncommitted, whose draws' pipelines are still being made, on the
|
||||
//! device the cfg selects, under the validation layer: once the drop returns, nothing of it
|
||||
//! touches host memory, so the program may unmap the pages its draws wrote at once.
|
||||
//!
|
||||
//! The consumer (reims-vgpu) unmaps a guest task's pages as soon as the task's objects are let go
|
||||
//! of, and relies on this: a dropped, uncommitted command buffer's deferred work, a draw whose
|
||||
//! pipeline is still being made among it, never reaches the scheduler, which takes only committed
|
||||
//! batches and hands a host step over only for a committed line's head.
|
||||
//!
|
||||
//! The draws are of the owned fixture `ob3_index_buffer`, a vertex function with rasterization
|
||||
//! disabled that marks `seen[min(vertex_id, 7)]`, here through a no-copy buffer over a second
|
||||
//! mapping of a page of guest RAM (a shared memory object, as `runtime_lane::host_memory` maps
|
||||
//! it). The state's creation predicts a point list's pipeline, so a line's and a triangle's draw
|
||||
//! each hand their own pipeline's making to the device's pool, and the pass records them later
|
||||
//! (`render/pending.rs`).
|
||||
|
||||
use super::host_memory::{no_copy, not_importable, on_device, GuestRam, GUEST_PAGE};
|
||||
use super::{pipelines_settled, WAIT};
|
||||
use crate::air_library::AirLibrary;
|
||||
use metal2vulkan::metal::PrimitiveType;
|
||||
use metal2vulkan::pipeline::{RenderPipelineDescriptor, StageSource};
|
||||
use metal2vulkan_devtools::{engine_dir, llvm_assemble};
|
||||
use metal2vulkan_runtime::{
|
||||
Buffer, CommandBuffer, CommandBufferStatus, CommandQueue, Device, PipelineOptions,
|
||||
RenderPassDescriptor, RenderPipelineState, ResourceOptions,
|
||||
};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
/// The byte every page of the guest RAM holds until a draw marks it.
|
||||
const UNTOUCHED: u8 = 0xa5;
|
||||
|
||||
/// The words a draw's vertex function marks, from the buffer's start.
|
||||
const MARKS: usize = 8;
|
||||
|
||||
/// The fixture's state: `ob3_index_buffer`, rasterization disabled.
|
||||
fn unrasterized_state(device: &Device) -> RenderPipelineState {
|
||||
let bitcode = llvm_assemble(&engine_dir().join("ob3_index_buffer.ll")).unwrap();
|
||||
let library = device.new_library_with_air(&bitcode).unwrap();
|
||||
let function = library.new_function_with_name("ob3_index_buffer").unwrap();
|
||||
let mut descriptor =
|
||||
RenderPipelineDescriptor::new(StageSource::Function(function.function_descriptor()));
|
||||
descriptor.rasterization_enabled = false;
|
||||
device
|
||||
.new_render_pipeline_state(&descriptor, &PipelineOptions::default())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// A no-copy buffer over `len` bytes at `at`, with the consumer's resource options (0).
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// `at..at + len` stays mapped while the buffer lives.
|
||||
unsafe fn over(device: &Device, at: NonNull<u8>, len: usize) -> Buffer {
|
||||
// SAFETY: the caller's contract.
|
||||
match unsafe { no_copy(device, at, len, ResourceOptions::default(), None) } {
|
||||
Ok(buffer) => buffer,
|
||||
Err(error) if not_importable(&error) => {
|
||||
panic!("this device imports no host memory exactly: {error}")
|
||||
}
|
||||
Err(error) => panic!("no-copy buffer: {error}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Encode, into a new command buffer of `queue`, a pass without attachments whose draws of
|
||||
/// `state` (a line's eight vertices, then a triangle's six) mark `seen` at vertex buffer 0. Not
|
||||
/// committed.
|
||||
fn encode_marks(queue: &CommandQueue, state: &RenderPipelineState, seen: &Buffer) -> CommandBuffer {
|
||||
let mut pass = RenderPassDescriptor::new();
|
||||
pass.render_target_width = 1;
|
||||
pass.render_target_height = 1;
|
||||
pass.default_raster_sample_count = 1;
|
||||
let command_buffer = queue.command_buffer().unwrap();
|
||||
let mut encoder = command_buffer.render_command_encoder(&pass).unwrap();
|
||||
encoder.set_render_pipeline_state(state).unwrap();
|
||||
encoder.set_vertex_buffer(Some(seen), 0, 0).unwrap();
|
||||
(encoder.draw_primitives(PrimitiveType::Line, 0, MARKS as u32, 1, 0)).unwrap();
|
||||
(encoder.draw_primitives(PrimitiveType::Triangle, 0, 6, 1, 0)).unwrap();
|
||||
encoder.end_encoding().unwrap();
|
||||
command_buffer
|
||||
}
|
||||
|
||||
/// The guest RAM's bytes of a page the draws marked: each of the [`MARKS`] words 1, every other
|
||||
/// byte untouched.
|
||||
fn marked_page() -> Vec<u8> {
|
||||
let mut page = vec![UNTOUCHED; GUEST_PAGE];
|
||||
for word in page[..4 * MARKS].chunks_mut(4) {
|
||||
word.copy_from_slice(&1u32.to_le_bytes());
|
||||
}
|
||||
page
|
||||
}
|
||||
|
||||
/// A command buffer whose draws' pipelines are still being made as its encoding ends, and whose
|
||||
/// draws write a no-copy buffer over guest pages, is dropped uncommitted; the program lets go of
|
||||
/// the buffer and unmaps the pages at once (replaced in place by an inaccessible mapping, so any
|
||||
/// CPU access of them faults), then waits until the makings are done. The guest RAM's page keeps
|
||||
/// every byte, the validation layer reports nothing, and a command buffer committed after it on
|
||||
/// the same queue draws with the pipelines now made, through a no-copy buffer over the next
|
||||
/// page, which it marks: the same draws reach the guest RAM when they run.
|
||||
#[test]
|
||||
fn a_dropped_uncommitted_command_buffer_touches_no_host_memory_once_dropped() {
|
||||
on_device(|device| {
|
||||
let ram = GuestRam::new(3 * GUEST_PAGE);
|
||||
ram.write(0, &vec![UNTOUCHED; 3 * GUEST_PAGE]);
|
||||
let rw = libc::PROT_READ | libc::PROT_WRITE;
|
||||
let state = unrasterized_state(device);
|
||||
let queue = device.new_command_queue().unwrap();
|
||||
// Nothing of the state's creation is left in the pool: what it holds after the encoding
|
||||
// is the draws'.
|
||||
let before = pipelines_settled(device);
|
||||
let dropped_view = ram.map(GUEST_PAGE, GUEST_PAGE, rw);
|
||||
// SAFETY: the view stays mapped while the buffer lives: the buffer goes first.
|
||||
let dropped_buffer = unsafe { over(device, dropped_view.pointer(), GUEST_PAGE) };
|
||||
let command_buffer = encode_marks(&queue, &state, &dropped_buffer);
|
||||
let encoded = device.work_counts();
|
||||
assert_eq!(
|
||||
encoded.pipelines_pooled - before.pipelines_pooled,
|
||||
2,
|
||||
"the line's and the triangle's pipelines went to the pool"
|
||||
);
|
||||
assert!(
|
||||
encoded.pipeline_pool_busy > 0,
|
||||
"the draws' pipelines were made before the encoding ended: no draw was left pending"
|
||||
);
|
||||
drop(command_buffer);
|
||||
drop(dropped_buffer);
|
||||
// SAFETY: the view's own range, which nothing else uses: the buffer over it is gone.
|
||||
let unmapped = unsafe {
|
||||
libc::mmap(
|
||||
dropped_view.pointer().as_ptr().cast(),
|
||||
GUEST_PAGE,
|
||||
libc::PROT_NONE,
|
||||
libc::MAP_PRIVATE | libc::MAP_ANON | libc::MAP_FIXED,
|
||||
-1,
|
||||
0,
|
||||
)
|
||||
};
|
||||
assert_eq!(
|
||||
unmapped,
|
||||
dropped_view.pointer().as_ptr().cast(),
|
||||
"unmap in place"
|
||||
);
|
||||
pipelines_settled(device);
|
||||
// The same queue, the pipelines made, over the next page.
|
||||
let next_view = ram.map(2 * GUEST_PAGE, GUEST_PAGE, rw);
|
||||
// SAFETY: the view stays mapped until the command buffer completed and the buffer went.
|
||||
let next_buffer = unsafe { over(device, next_view.pointer(), GUEST_PAGE) };
|
||||
let next = encode_marks(&queue, &state, &next_buffer);
|
||||
next.commit().unwrap();
|
||||
next.wait_until_completed_for(WAIT).unwrap();
|
||||
assert_eq!(next.status(), CommandBufferStatus::Completed);
|
||||
assert_eq!(next.error(), None);
|
||||
drop(next);
|
||||
drop(next_buffer);
|
||||
drop(next_view);
|
||||
assert!(
|
||||
ram.read(GUEST_PAGE..2 * GUEST_PAGE) == vec![UNTOUCHED; GUEST_PAGE],
|
||||
"the dropped command buffer's draws reached the guest page"
|
||||
);
|
||||
assert!(
|
||||
ram.read(2 * GUEST_PAGE..3 * GUEST_PAGE) == marked_page(),
|
||||
"the next command buffer's draws marked their guest page"
|
||||
);
|
||||
assert!(
|
||||
ram.read(0..GUEST_PAGE) == vec![UNTOUCHED; GUEST_PAGE],
|
||||
"a page no buffer was over"
|
||||
);
|
||||
drop(dropped_view);
|
||||
});
|
||||
}
|
||||
@@ -62,7 +62,7 @@ pub(super) fn host_page() -> usize {
|
||||
}
|
||||
|
||||
/// One shared memory object of `len` bytes: the consumer's guest RAM.
|
||||
struct GuestRam {
|
||||
pub(super) struct GuestRam {
|
||||
fd: libc::c_int,
|
||||
len: usize,
|
||||
/// The guest RAM's own mapping, through which the CPU writes and reads.
|
||||
@@ -70,7 +70,7 @@ struct GuestRam {
|
||||
}
|
||||
|
||||
impl GuestRam {
|
||||
fn new(len: usize) -> GuestRam {
|
||||
pub(super) fn new(len: usize) -> GuestRam {
|
||||
static NEXT: AtomicUsize = AtomicUsize::new(0);
|
||||
let name = std::ffi::CString::new(format!(
|
||||
"/m2v-rt4-{}-{}",
|
||||
@@ -96,18 +96,18 @@ impl GuestRam {
|
||||
|
||||
/// A second mapping of `len` bytes from `offset`, aligned to the guest page, as the consumer
|
||||
/// maps a task's pages.
|
||||
fn map(&self, offset: usize, len: usize, protection: libc::c_int) -> Mapping {
|
||||
pub(super) fn map(&self, offset: usize, len: usize, protection: libc::c_int) -> Mapping {
|
||||
assert!(offset + len <= self.len);
|
||||
Mapping::of(self.fd, offset, len, GUEST_PAGE, protection)
|
||||
}
|
||||
|
||||
/// A copy of the guest RAM's bytes `range`.
|
||||
fn read(&self, range: std::ops::Range<usize>) -> Vec<u8> {
|
||||
pub(super) fn read(&self, range: std::ops::Range<usize>) -> Vec<u8> {
|
||||
self.view.read(range)
|
||||
}
|
||||
|
||||
/// Write `bytes` into the guest RAM at `offset`.
|
||||
fn write(&self, offset: usize, bytes: &[u8]) {
|
||||
pub(super) fn write(&self, offset: usize, bytes: &[u8]) {
|
||||
self.view.write(offset, bytes);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user