RUNTIME 2352: Host RAM: an object moves the window to its chunk only once it holds it

RamOnDevice::chunk moved the device's thread's window to an object's chunk
(take + notify) before the object marked the chunk's import in flight. The
window's first slot is the chunk just taken, so the woken thread could find it
not imported and import it By::Ahead in the object's place: counted as imported
ahead and marked untaken, with the object waiting for that import at the
thread's pace (SCHED_BATCH on Linux, utility QoS on Apple). Off Linux the thread
does no fault-in before an import, so it reaches the claims lock at once; that
is the failure seen once on the Windows mini under nextest -j 4:
a_chunk_bound_buffer_and_a_copy_of_its_pages_order_by_their_host_bytes saw ahead
imports 3 where 2 were expected (chunks 0 and 2 by the thread, then 3 within
settled()'s 300 ms).

Now the window moves (RamOnDevice::took) once the object holds the chunk: found
made, marked in flight by it, or held back. The thread, woken by the move,
finds the chunk in flight or made and imports the ones past it.

Test: an_object_holds_its_chunk_before_the_window_moves_to_it holds the
object's call 300 ms before it holds the chunk (RamSource::taking_pause, a new
cfg(test) pause beside making_pause) and checks the object imported its chunk
and the thread one past it alone.

Evidence (lane jobs, msi): m3-max-32427: clippy -p metal2vulkan-runtime --all-targets --all-features -D warnings 0;
lavapipe 78/78 (host_ram_device, host_ram_copy_device, host_ram:: units) incl. the new
an_object_holds_its_chunk_before_the_window_moves_to_it and the original a_chunk_bound_buffer_and_a_copy_of_its_pages_...
Negative control m3-max-32428 (the take back at the top of chunk()): the focused test fails, "the object imported its
chunk, the thread not in its place: left 0, right 1". The Windows mini runs follow in the lane.

Lane commit: 10dfe36dce.
This commit is contained in:
Anees Iqbal committed 2026-11-15 07:31:27 -08:00
1 parent 891307e5ac
commit 155f97fbfd
3 files changed
+95 -10

No files matched your search

+36 -8
View File
@@ -951,10 +951,12 @@ impl RamOnDevice {
/// [`Import::Making`], so an object over the chunk the device's thread (or another object)
/// is importing waits for that import. A failed import is not kept: the next object over the
/// chunk tries again. An object's call moves the device's thread's window to this chunk
/// ([`RamOnDevice::take`]), and wakes the thread where that moved it, on a device that
/// imports ahead (not [`Plan::listed`]). An import ahead is counted once the chunk is in its
/// slot, so that the count of imports ahead never runs before the chunks an object finds
/// there.
/// ([`RamOnDevice::took`]) once the object holds the chunk: found made, marked in flight by
/// the object itself, or held back. The window's first chunk is the one taken, so a thread
/// woken before the object marked it in flight could import it in the object's place, the
/// object then waiting for that import at the thread's pace and the chunk counted as
/// imported ahead. An import ahead is counted once the chunk is in its slot, so that the
/// count of imports ahead never runs before the chunks an object finds there.
fn chunk(
&self,
device: &Device,
@@ -962,8 +964,12 @@ impl RamOnDevice {
slot: usize,
by: By,
) -> Result<Arc<RamChunk>, Error> {
if by == By::Object && !self.plan.listed && self.take(slot) {
self.wake.notify();
// A test's pause of an object's call, before it holds the chunk.
#[cfg(test)]
let pause = lock(&device.shared().host_ram.taking_pause).clone();
#[cfg(test)]
if let Some(pause) = pause.filter(|_| by == By::Object) {
pause(self.slots[slot].span.clone());
}
let span = &self.slots[slot];
let mut import = lock(&span.import);
@@ -973,7 +979,12 @@ impl RamOnDevice {
let claims = loop {
loop {
match &*import {
Import::Made(chunk) => return Ok(Arc::clone(chunk)),
Import::Made(chunk) => {
let chunk = Arc::clone(chunk);
drop(import);
self.took(by, slot);
return Ok(chunk);
}
Import::Making => {
import = (span.made.wait(import)).unwrap_or_else(PoisonError::into_inner);
}
@@ -992,6 +1003,8 @@ impl RamOnDevice {
};
let beside = match self.held_back(device, importer, slot, &claims) {
Held::Back => {
drop((import, claims));
self.took(by, slot);
return Err(crate::error::internal(
"a guest RAM chunk a live import of an object's own holds pages of",
));
@@ -1006,6 +1019,7 @@ impl RamOnDevice {
slot: span,
ended: false,
};
self.took(by, slot);
// A test's pause of the import, marked in flight, before it is made.
#[cfg(test)]
let pause = lock(&device.shared().host_ram.making_pause).clone();
@@ -1640,6 +1654,15 @@ impl RamOnDevice {
gone
}
/// An object's call over chunk `slot` ([`RamOnDevice::chunk`]) holds it now, made or in
/// flight: the window moves to it ([`RamOnDevice::take`]), and the device's thread is woken
/// where that moved it, on a device that imports ahead (not [`Plan::listed`]).
fn took(&self, by: By, slot: usize) {
if by == By::Object && !self.plan.listed && self.take(slot) {
self.wake.notify();
}
}
/// An object took chunk `slot`: the window moves to it ([`Window::after_take`]) unless it is
/// the chunk last taken. Whether it moved.
fn take(&self, slot: usize) -> bool {
@@ -2330,7 +2353,8 @@ fn place_runs(
Ok((run.end <= spans[slot].end).then_some(slot))
}
/// A test's pause in a chunk's import ([`RamSource::making_pause`]), given the chunk's span.
/// A test's pause in a chunk's import ([`RamSource::making_pause`]) or an object's take of it
/// ([`RamSource::taking_pause`]), given the chunk's span.
#[cfg(test)]
pub(crate) type MakingPause = Arc<dyn Fn(Range<u64>) + Send + Sync>;
@@ -2350,6 +2374,10 @@ pub(crate) struct RamSource {
/// marked in flight (`Import::Making`) and before it is made ([`RamOnDevice::chunk`]).
#[cfg(test)]
pub(crate) making_pause: Mutex<Option<MakingPause>>,
/// The tests' pause in an object's call for a chunk's import, called with the chunk's span
/// before the object holds the chunk ([`RamOnDevice::chunk`]).
#[cfg(test)]
pub(crate) taking_pause: Mutex<Option<MakingPause>>,
/// Whether the driver refused a dma-buf a RAM's source made of a span or a list of its pages
/// for a cause that lasts (`Device::ram_span_as_dma_buf`, `ram_runs_as_dma_buf`): from then
/// on, for the device's life, no span or list asks a source, and no RAM's plan is
@@ -39,7 +39,9 @@
//! back for a chunk a jump skipped. A run of heaps that stays inside the window imports nothing
//! on the thread making them; a heap outside it imports its own chunk. An object over a chunk
//! the thread imported, made while the chunk's state is looked at (as the thread looks at each
//! chunk of its window), is bound in the chunk.
//! chunk of its window), is bound in the chunk. An object over a chunk outside the window moves
//! the window only once it holds the chunk: the thread never imports that chunk in its place,
//! however long the object takes to reach it, and imports the one past it.
//! - **Down the RAM.** A heap over a chunk not imported, at the RAM's top, imports the chunk, in
//! which the heaps over it after that are bound, and its bytes go from the CPU to the GPU and
//! back; a heap next to it below imports its chunk and opens the window below, so a run of heaps
@@ -991,6 +993,61 @@ fn the_window_ahead_moves_with_the_objects_and_imports_no_more() {
);
}
/// An object over a chunk outside the window, held before it holds the chunk
/// (`RamSource::taking_pause`) for longer than the device's thread takes to start a chunk's
/// import, imports the chunk itself: the window moves to the chunk only once the object marked
/// its import in flight, so the thread, woken by the move, finds it in flight and imports the one
/// past it alone. Were the window moved before, the thread would import the object's chunk
/// meanwhile (counted ahead), and the object wait for that import.
#[test]
fn an_object_holds_its_chunk_before_the_window_moves_to_it() {
let Some(device) = ahead_device() else {
return;
};
let (ram, start, dropped) = fresh_ram(&device, 4 * 64 * MIB);
let before = device.work_counts();
let used = device.use_host_ram(&ram).unwrap();
let ahead = |counts: &crate::WorkCounts| {
counts.host_ram_chunks_imported_ahead - before.host_ram_chunks_imported_ahead
};
let by_objects = |counts: &crate::WorkCounts| {
counts.host_ram_chunks_imported - before.host_ram_chunks_imported - ahead(counts)
};
settled(&device, |counts| ahead(counts) == 1);
let page = device.shared().host_import.as_ref().unwrap().page;
// The object's call held for as long as `settled` gives the thread to show an import.
*device.shared().host_ram.taking_pause.lock().unwrap() = Some(Arc::new(|_span| {
std::thread::sleep(Duration::from_millis(300))
}));
let (runs, gates): (Arc<Mutex<_>>, Arc<Mutex<_>>) = Default::default();
let in_chunk = buffer(
&device,
start + 2 * 64 * MIB + 8 * page,
4 * page,
resolver(&runs, itself),
gate(&gates, true),
)
.unwrap();
*device.shared().host_ram.taking_pause.lock().unwrap() = None;
assert_eq!(
by_objects(&device.work_counts()),
1,
"the object imported its chunk, the thread not in its place"
);
let made = settled(&device, |counts| ahead(counts) == 2);
assert_eq!(
(ahead(&made), by_objects(&made)),
(2, 1),
"the thread imported the chunk past the object's alone"
);
assert_eq!(in_chunk.host_backing(), Some(HostBacking::InHostRam));
drop((in_chunk, used, ram));
assert!(
settles(&dropped),
"the keep-alive goes after the last chunk"
);
}
/// On a device that refuses pointers to shared memory (`Device::imports_shared_host_pointers`
/// false: RADV, whose amdgpu userptr takes private anonymous pages alone), a RAM of shared memory
/// (a `memfd` mapped shared, as QEMU's `memory-backend-memfd,share=on` RAM is) imports no chunk,
+1 -1
View File
@@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 523;
/// changes no behaviour, and then updates that hash alone with the commit saying why, or changes
/// behaviour and also bumps this version. The commit that bumps it says what changed; this comment
/// keeps no history.
pub const RUNTIME_VERSION: u32 = 2351;
pub const RUNTIME_VERSION: u32 = 2352;
/// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex,
/// of the manifest, the build script, and exactly the files the crate roots' module tree compiles