From e205498c64dcfce1c9a8628931ae03750339de75 Mon Sep 17 00:00:00 2001 From: DexNom Date: Fri, 27 Nov 2026 23:04:19 +0300 Subject: [PATCH] GPU: a background fence wait reports what went wrong instead of carrying on Two helper threads wait on the host fence with `SetEventOnCompletion` and `WaitForSingleObject`. Both ignored a failed call: the late helper then went straight to the landing step with nothing run, settling flights off a value the card never reached, and the relay slept 1 ms and tried again. A removed device was worse than either: `GetCompletedValue` answers `u64::MAX`, which compares greater than every target, so every wait and every landing check passed at once while the card had run nothing. `wait_host_fence` checks the completion call, the wait's result and the fence value, and fails with a named context; `fence_reached` treats `u64::MAX` as "never reached". The relay's device-removal read is checked too, before it lands anything. Ported from DexNom/VIctory d036cea (pull request #1366), GPL-3.0. (cherry picked from commit 671e25f9850abca01266bab096fed096b7fd0606) --- crates/victory-gpu/src/host.rs | 64 +++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 169acdc..0897d3c 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -8,7 +8,7 @@ use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::{Mutex, OnceLock}; -use windows::Win32::Foundation::{HANDLE, HWND, LPARAM, LRESULT, RECT, WPARAM}; +use windows::Win32::Foundation::{HANDLE, HWND, LPARAM, LRESULT, RECT, WAIT_OBJECT_0, WPARAM}; use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_12_0; use windows::Win32::Graphics::Direct3D12::*; use windows::Win32::Graphics::Dxgi::Common::*; @@ -3722,6 +3722,49 @@ fn land() { } } +#[cfg(test)] +#[test] +fn host_fence_completion_rejects_removed_device() { + assert!(!fence_reached(u64::MAX, 1)); + assert!(!fence_reached(6, 7)); + assert!(fence_reached(7, 7)); + assert!(fence_reached(8, 7)); +} + +/// Whether a fence's completed value shows `target` run. `u64::MAX` is not +/// a value the card reaches: it is what `GetCompletedValue` answers when the +/// device is gone. +fn fence_reached(completed: u64, target: u64) -> bool { + completed != u64::MAX && completed >= target +} + +/// Waits for `target` on `fence` and reports what went wrong instead of +/// carrying on: a failed wait used to fall through to the landing step with +/// nothing run, settling flights off an unsent fence value, and a lost +/// device (a completed value of `u64::MAX`) made every wait pass at once. +/// +/// # Safety +/// +/// `event` is the calling thread's own, never closed while it waits. +unsafe fn wait_host_fence(fence: &ID3D12Fence, target: u64, event: HANDLE, context: &str) { + // SAFETY: the caller's fence and event, alive for the whole wait. + if let Err(e) = unsafe { fence.SetEventOnCompletion(target, event) } { + fail(format_args!( + "{context}: SetEventOnCompletion({target}) failed: {e}" + )); + } + let status = unsafe { WaitForSingleObject(event, INFINITE) }; + if status != WAIT_OBJECT_0 { + fail(format_args!("{context}: fence wait failed: {status:?}")); + } + let completed = unsafe { fence.GetCompletedValue() }; + if !fence_reached(completed, target) { + fail(format_args!( + "{context}: fence {completed} did not reach {target}" + )); + } +} + /// The thread that settles the flights once the card has run them all /// ([`late`]): without it a bottom-of-pipe write would reach the title only /// at the next call that settles, and a title thread that polls such a @@ -3761,14 +3804,10 @@ fn late_helper() { }; // The card first, without the submission lock: the submitting // thread prepares and sends meanwhile. + #[cfg(feature = "diagnostics")] + let _card = crate::timeline::card_wait(Some(target)); // SAFETY: our fence and event. - unsafe { - if host.fence.SetEventOnCompletion(target, event).is_ok() { - #[cfg(feature = "diagnostics")] - let _card = crate::timeline::card_wait(Some(target)); - WaitForSingleObject(event, INFINITE); - } - } + unsafe { wait_host_fence(&host.fence, target, event, "late helper") }; { let mut submitted = host.submitted.lock().unwrap_or_else(|e| e.into_inner()); // Something newer went up meanwhile: the card is waited for @@ -3926,14 +3965,7 @@ fn relay_worker() { #[cfg(feature = "diagnostics")] let _card = crate::timeline::card_wait(Some(target)); // SAFETY: the host's fence and this thread's event. - let waited = unsafe { - host.fence.SetEventOnCompletion(target, event).map(|()| { - WaitForSingleObject(event, INFINITE); - }) - }; - if waited.is_err() { - std::thread::sleep(std::time::Duration::from_millis(1)); - } + unsafe { wait_host_fence(&host.fence, target, event, "relay") }; RELAY_STATS[2].fetch_add(waiting.elapsed().as_micros() as u64, Ordering::Relaxed); } }