RUNTIME 2321: An indirect command buffer's home waits for its placing inside an exit-guard section

Home::drop waits on the host for the staging submission that placed the records in
device-local memory. The drop runs wherever the buffer's last hold lets go, and
that can be a command buffer's completion on its queue's waiter or a handler
thread. When the host had not seen the placing submission complete, the wait's
vkWaitSemaphores ran there outside any section. In debug builds exit_guard::check
ends the process at such a call: the XTX abort of
runtime_lane::resource_ids::an_indirect_command_buffer_s_id_is_held_as_metal_holds_it
at pin 94 (queue-wait-0, complete_rest's drop of the residency).

The wait now runs inside a section, as texture/staging_copy.rs's Flushed::drop
does. While the process exits no section starts: the drop returns without
waiting, and the records' buffer stays to the end, as release.rs leaves every
object then. exit_guard::check is unchanged. vkWaitSemaphores is already in the
sections' call table.

Verified: cargo fmt. The device runs of the previous commit's test are reported
with the landing.

Test: Device test: a command buffer's completion that lets go of an indirect command buffer last waits for its placing.
On a device that places indirect command buffer records in device-local memory, a
buffer the program lets go of while a command buffer that uses it is in flight goes
as that command buffer completes, on a queue's waiter or a handler thread. Its
home's drop then waits for the staging submission that placed the records. When the
host has not seen that submission complete, the wait calls vkWaitSemaphores, which
the exit guard requires to run inside a section on the runtime's threads.

The test holds the staging submissions on a test timeline
(Device::hold_staging_submissions), places a Private buffer's records through a
compute encoder's useResource, commits, lets go of the buffer, then signals the
timeline. The command buffer completes only after the placing submission, and
nothing waits for it on the host before the drop. A test-only count in Home::drop
(RUNTIME_PLACING_WAITS) records a wait for an unseen placing submission on a
runtime thread, so a pass on a placing device is not vacuous. A device that keeps
records in the host's memory says so and passes.

This commit has the test alone. On a placing device, without the section the next
commit adds, the drop's call ends the process (exit_guard::check): the XTX abort of
runtime_lane::resource_ids::an_indirect_command_buffer_s_id_is_held_as_metal_holds_it
at pin 94.

Why: on the RX 7900 XTX at pin 94 (m3-max-30428), runtime_lane::resource_ids::an_indirect_command_buffer_s_id_is_held_as_metal_holds_it
aborted in a debug build: "the runtime's thread metal2vulkan-queue-wait-0 reached the device outside an
exit-guard section". The queue waiter's completion dropped the last Arc of an indirect command buffer, and
Home::drop's wait for the staging submission that placed the records (vkWaitSemaphores) ran with no section.

Verified (lane devlegs, msi m3-max-30615): clippy -D warnings, runtime crate, plain and --cfg m2v_gpu 0;
the m2v_gpu archive at the fix built. Landed (main) at once: the 5080 negative control (test without the
fix aborts, with it passes) and the 3090 run follow on development.
RUNTIME 2321: an exiting process's last drop no longer waits for a placing it cannot guard.
This commit is contained in:
Anees Iqbal committed 2026-11-14 17:06:15 -08:00
1 parent 12ae9f668b
commit 7765813cb4
5 files changed
+102 -3

No files matched your search

@@ -670,6 +670,13 @@ impl Device {
}
}
/// The staging queue's value the host saw complete (0 on a device without one), read without
/// the wait of no time a [`staging_wait`](Self::staging_wait) makes: whether a wait for a
/// submission would reach the driver.
pub(crate) fn staging_seen(&self) -> u64 {
(self.shared().staging.as_ref()).map_or(0, |staging| staging.done.value())
}
/// The fill queue's family and that family's queue flags, where the device has one.
pub(crate) fn fill_queue_family(&self) -> Option<(u32, vk::QueueFlags)> {
(self.shared().fill_queue.as_ref()).map(|queue| (queue.family, queue.flags))
@@ -128,6 +128,8 @@ mod set_each;
mod set_states;
mod writers;
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) use home::RUNTIME_PLACING_WAITS;
use home::{Home, Reach};
pub use set_each::{IndirectComputeSet, IndirectRenderSet};
use set_states::{Keyed, SetKeys, SetStates, StateKey};
@@ -45,6 +45,7 @@ use crate::degradation::Degradation;
use crate::device::lock;
use crate::encoder::EncoderCore;
use crate::error::{internal, Error};
use crate::exit_guard;
use crate::texture::transfer_barrier;
use crate::{Device, StorageMode};
use ash::vk;
@@ -137,13 +138,34 @@ impl Home {
}
}
/// The drops of homes that waited for the submission that placed their records, which the host
/// had not seen complete, on one of the runtime's threads (a command buffer's completion that let
/// go of the buffer last), in the device tests' builds: a test's evidence that the wait ran there
/// (`tests::icb_records_home`).
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) static RUNTIME_PLACING_WAITS: std::sync::atomic::AtomicU64 =
std::sync::atomic::AtomicU64::new(0);
impl Drop for Home {
fn drop(&mut self) {
// The submission that set the device-local records' bytes holds nothing: the records go
// once it completed.
// once it completed. A section of the exit guard, wherever the last hold lets go (a
// command buffer's completion, on its queue's waiter or a handler thread): while the
// process exits, the records stay to its end, as their buffer's release leaves them
// (`exit_guard`, `release.rs`).
let state = lock(&self.state);
if let (Some(records), false) = (self.device.get(), state.init_seen) {
let _ = records.generation().wait_staging(state.init);
let Some(_busy) = exit_guard::enter() else {
return;
};
let device = records.generation();
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
let unseen = device.staging_seen() < state.init && exit_guard::on_runtime_thread();
let _ = device.wait_staging(state.init);
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
if unseen {
RUNTIME_PLACING_WAITS.fetch_add(1, Ordering::Relaxed);
}
}
}
}
@@ -23,6 +23,7 @@ use crate::{
RenderPipelineState, ResourceOptions, Size, StoreAction, Texture, TextureDescriptor,
WorkCounts,
};
use ash::vk;
use metal2vulkan::bind::indirect_commands::written_flag_offset;
use metal2vulkan::metal::{
IndirectCommandType, PixelFormat, PrimitiveType, RenderStages, ResourceUsage, TextureUsage,
@@ -30,6 +31,7 @@ use metal2vulkan::metal::{
use metal2vulkan::pipeline::{
ColorAttachmentDescriptor, ComputePipelineDescriptor, RenderPipelineDescriptor, StageSource,
};
use std::sync::atomic::Ordering;
use std::sync::{Arc, Mutex};
/// The commands of a buffer, and the cells of the target: a command draws the lower half of the
@@ -623,3 +625,69 @@ fn a_gpu_reset_then_an_encode_then_an_execution_are_exact() {
}
}
}
/// A test timeline of `device`'s at value 0, which [`Device::hold_staging_submissions`] holds the
/// staging submissions on until the test signals it.
fn hold_timeline(device: &Device) -> vk::Semaphore {
let mut kind = vk::SemaphoreTypeCreateInfo::default()
.semaphore_type(vk::SemaphoreType::TIMELINE)
.initial_value(0);
let info = vk::SemaphoreCreateInfo::default().push_next(&mut kind);
// SAFETY: the device is live and enables `timelineSemaphore`; `info` is live for the call.
unsafe { device.shared().vk().create_semaphore(&info, None) }.unwrap()
}
/// A buffer the program lets go of while a command buffer that uses it is in flight goes as
/// that command buffer completes, on one of the runtime's threads (its queue's waiter, or a
/// handler thread): its home waits there for the staging submission that placed its records in
/// device-local memory, which the host has not seen complete, inside a section of the exit guard
/// (`home.rs`), as every Vulkan call of the runtime's threads (`exit_guard::check`, which in these
/// builds ends the process at one made outside a section). The placing submission is held on the
/// GPU until the command buffer was committed and the buffer let go of
/// ([`Device::hold_staging_submissions`]), so the command buffer completes after it, and nothing
/// waits for it on the host before the drop. A device that keeps every buffer's records in the
/// host's memory places none and waits for none.
#[test]
fn a_buffer_its_command_buffer_lets_go_of_last_waits_for_its_placing_inside_a_section() {
let ours = Ours::new("icb-home-last-hold");
let name = ours.device.name().to_owned();
if !ours.placing() {
println!("{name}: keeps every buffer's records in the host's memory: nothing placed");
return;
}
let hold = hold_timeline(&ours.device);
ours.device.hold_staging_submissions(Some((hold, 1)));
let icb = ours.icb(ResourceOptions::private());
// Its residency in a command buffer: the GPU's first use, which places the records, held
// until the command buffer completes.
let queue = ours.device.new_command_queue().unwrap();
let command_buffer = queue.command_buffer().unwrap();
let mut encoder = command_buffer.compute_command_encoder().unwrap();
encoder
.use_resource(icb.as_resource(), ResourceUsage::READ)
.unwrap();
encoder.end_encoding().unwrap();
ours.device.hold_staging_submissions(None);
command_buffer.commit().unwrap();
let waits = || crate::indirect_command_buffer::RUNTIME_PLACING_WAITS.load(Ordering::Relaxed);
let before = (ours.device.work_counts(), waits());
drop(icb);
let signal = vk::SemaphoreSignalInfo::default().semaphore(hold).value(1);
// SAFETY: a host signal of the test's timeline, above its value.
unsafe { ours.device.shared().vk().signal_semaphore(&signal) }.unwrap();
wait(&command_buffer);
let after = (ours.device.work_counts(), waits());
let where_seen = after.0.completed_where_seen - before.0.completed_where_seen;
println!(
"{name}: the command buffer's completion let go of the buffer last ({where_seen} \
completed where seen); {} drops waited for the placing submission on a runtime thread",
after.1 - before.1
);
assert_eq!(
after.1 - before.1,
1,
"{name}: the drop waited for the placing submission on one of the runtime's threads"
);
// SAFETY: the semaphore is the test's, and no submission waits on it any more.
unsafe { ours.device.shared().vk().destroy_semaphore(hold, None) };
}
+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 = 2320;
pub const RUNTIME_VERSION: u32 = 2321;
/// 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