Recorder: a slot let go of once the process is exiting keeps its driver's entry points

dma_buf_leak_4_threads ended in SIGABRT on the RTX 5080 Laptop GPU (NVIDIA 615.71.09, round job
m3-max-16620 at 600b535875). One of the two causes: once a test had failed and the process
exited, a device that dropped after the exit guard closed let go of its recorder slot, clearing
the slot's entry points, while a thread of that device's (the queue-switch check making its
objects) was still inside a section and recording through the slot. The slot's missing-command
thunk then panicked inside an extern "system" function, which aborts.

Every other path that lets go of a slot is the device's ordinary drop, which runs after the
device's threads stopped and so with no section of theirs live. Only an exit leaves sections
running past the slot's release: a section that started runs to its end. Slot::drop now enters
the exit guard; once the guard has closed it returns at once and leaves the slot as it is, with
its entry points stored and its index taken, just as the exit leaves the device to the process's
end.

Test: exit_guard::a_slot_let_go_of_while_the_process_exits_keeps_its_commands. A forked child
routes a stand-in device's functions through a slot and registers the hook. An exit handler that
runs after the hook drops the slot and records through it, and the child expects the stand-in to
be reached.

Verified: the test passes on CPU (potluck m3-max-16978) and in the RTX 3090 run (m3-max-16941).
It fails without the guard check (m3-max-17034, whose child aborts in the thunk).
This commit is contained in:
Anees Iqbal committed 2026-11-11 12:48:57 -08:00
1 parent 73050a9d1e
commit fbfa0da385
2 files changed
+87 -2

No files matched your search

@@ -8,8 +8,10 @@
//! (`device/functions.rs`, the exit guard's table among them) and the extension commands it loads
//! ([`Slot::route`]) hold the slot's entry points. Each is generic over its slot, so it finds its
//! device's command with one load, without a lock or a lookup by handle; a slot is let go of
//! with its device, after `vkDestroyDevice`. A source test fails when a file outside this module
//! names a command that is not routed.
//! with its device, after `vkDestroyDevice`, and never once the process is exiting: then it
//! stays with the device the exit leaves to the process's end, whose threads' sections may
//! still record through it ([`Slot`]'s drop). A source test fails when a file outside this
//! module names a command that is not routed.
//! - **Counted by kind.** Each command recorded through a slot counts once for its kind, one
//! relaxed increment into a fixed array of the slot's indexed by the command, whatever the
//! command buffer (`WorkCounts::commands_recorded`). Each slot keeps its counts in shards, one a
@@ -116,7 +118,16 @@ impl Slot {
}
impl Drop for Slot {
/// Let go of the slot, its device destroyed: a release, made in a section of the exit guard
/// (`exit_guard.rs`). Once the process is exiting the slot is left as it is, its driver's
/// entry points stored, as the device is left to the process's end: a device whose teardown
/// the exit cut short, or one that drops once the guard closed, leaves its threads' sections
/// in progress running (a section started runs to its end), and one of them recording through
/// a cleared slot would end the process inside the slot's entry point (`thunks::missing`).
fn drop(&mut self) {
let Some(_busy) = crate::exit_guard::enter() else {
return;
};
thunks::clear(self.index);
TAKEN[self.index].store(false, Ordering::Release);
}
@@ -1197,6 +1197,80 @@ fn an_exit_runs_the_handlers_before_the_hook_once_the_work_ended() {
}
}
/// A device's slot among those its commands go through (`recorder`), let go of once the process
/// is exiting, stays as it was: a command recorded through it after reaches the driver's entry
/// point. A device whose last handle drops once the guard closed (or whose teardown the exit cut
/// short) leaves its threads' sections in progress running, and one recording through its slot
/// meanwhile (the queue-switch check's thread making its objects) once found the slot's driver
/// entry points cleared, its entry point ended the process (`SIGABRT`, a panic that cannot unwind
/// out of an `extern "system"` function) after a GPU recovery's test failed (RTX 5080 Laptop GPU,
/// NVIDIA 615.71.09). The child routes a stand-in device's functions through a slot, registers the
/// hook, and from an exit handler that runs after it lets go of the slot and records through it.
#[cfg(all(target_os = "linux", target_env = "gnu"))]
#[test]
fn a_slot_let_go_of_while_the_process_exits_keeps_its_commands() {
use ash::vk;
/// The routed functions and their slot, until the exit handler takes them.
static ROUTED: Mutex<Option<(ash::Device, crate::recorder::Slot)>> = Mutex::new(None);
/// The stand-in's resets recorded.
static RESETS: AtomicUsize = AtomicUsize::new(0);
unsafe extern "system" fn reset(_: vk::CommandBuffer, _: vk::QueryPool, _: u32, _: u32) {
RESETS.fetch_add(1, Ordering::SeqCst);
}
/// After the hook: the guard closed, the slot let go of, a reset recorded through it; 44 where
/// it reached the stand-in, 45 where the guard was open.
extern "C" fn after_the_hook() {
let taken = ROUTED.lock().map(|mut routed| routed.take());
let Ok(Some((routed, slot))) = taken else {
// SAFETY: ends the child at once, from its exit handlers.
unsafe { libc::_exit(46) };
};
let closed = exit_guard::closed();
drop(slot);
// SAFETY: the stand-in reads none of its parameters.
unsafe {
routed.cmd_reset_query_pool(vk::CommandBuffer::null(), vk::QueryPool::null(), 0, 1)
};
let status = match closed && RESETS.load(Ordering::SeqCst) == 2 {
true => 44,
false => 45,
};
// SAFETY: as above.
unsafe { libc::_exit(status) };
}
fn child() {
let none = |_: &std::ffi::CStr| std::ptr::null::<std::ffi::c_void>();
let mut v1_0 = ash::DeviceFnV1_0::load(none);
v1_0.cmd_reset_query_pool = reset;
let device = ash::Device::from_parts_1_3(
vk::Device::null(),
v1_0,
ash::DeviceFnV1_1::load(none),
ash::DeviceFnV1_2::load(none),
ash::DeviceFnV1_3::load(none),
);
// SAFETY: the stand-in's functions are not routed yet; no Vulkan call reaches a device.
let (routed, slot) = unsafe { crate::recorder::route(device) };
let slot = slot.expect("a slot free in the child");
// SAFETY: as above.
unsafe {
routed.cmd_reset_query_pool(vk::CommandBuffer::null(), vk::QueryPool::null(), 0, 1)
};
assert_eq!(RESETS.load(Ordering::SeqCst), 1, "the routed reset");
*ROUTED.lock().unwrap() = Some((routed, slot));
// Registered before the hook, so it runs after it.
// SAFETY: a function with no arguments that never unwinds (it ends the child).
unsafe { libc::atexit(after_the_hook) };
exit_guard::register();
}
let exit = child_exit(child, 0);
assert_eq!(
exit.status, 44,
"45: the guard was open after the hook; 46: nothing to let go of; its standard error: {}",
exit.stderr
);
}
/// Where the hook learns the exit's status, an exit while the driver still runs the runtime's work
/// once the hook's wait reached its bound (a section that never ends: a driver deadlocked inside a
/// submission, as the GPU-assisted validation layer 1.4.357 was; a CPU device's work that never