Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion crates/xcl/src/hle/d3d12x/late_release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ use windows::Win32::Graphics::Direct3D12::*;
const SEAL_EVERY: Duration = Duration::from_millis(10);
/// how often the thread here looks for batches that can go
const TICK: Duration = Duration::from_millis(4);
/// a sealed batch whose fences have not advanced for this long is let go without waiting: the GPU is
/// not making progress (a hang, or an impending removal), so holding its resources only grows the WDDM
/// kernel allocation toward an out-of-memory device reset that would mask the real cause. Safe – a GPU
/// that is not progressing will not touch the memory, and a lost device makes the references moot. Only
/// on a true stall, never on a high count alone: under a lag (fences advancing, just behind) the
/// resources are still in flight and freeing them would be a use-after-free.
const STALL_FORCE: Duration = Duration::from_secs(5);

/// What is let go when the queues are past it.
#[derive(Clone, Copy, PartialEq, Debug)]
Expand All @@ -40,6 +47,8 @@ struct Batch {
dead: Vec<Dead>,
/// (fence, value) a queue: all reached = nothing submitted before the batch closed is left
marks: Vec<(ID3D12Fence, u64)>,
/// when the batch was sealed, for the `STALL_FORCE` timeout
sealed_at: Instant,
}

#[derive(Default)]
Expand Down Expand Up @@ -88,13 +97,23 @@ impl Held {
if ok {
self.last_seal = Some(Instant::now());
let dead = std::mem::take(&mut self.open);
self.sealed.push_back(Batch { dead, marks });
self.sealed.push_back(Batch { dead, marks, sealed_at: Instant::now() });
}
}
let mut gone = Vec::new();
while self.sealed.front().is_some_and(|b| b.marks.iter().all(|(f, v)| super::real::fence_completed(&f) >= *v)) {
gone.extend(self.sealed.pop_front().unwrap().dead);
}
// Fences are not advancing (the GPU is hung or being removed): let the oldest batches go
// anyway rather than hold until the WDDM kernel pool is exhausted and the device resets,
// which would mask the real cause. See `STALL_FORCE`.
while self.sealed.front().is_some_and(|b| b.sealed_at.elapsed() >= STALL_FORCE) {
static WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
if !WARNED.swap(true, std::sync::atomic::Ordering::Relaxed) {
crate::xlog_warn!("late_release: GPU fences have not advanced for {STALL_FORCE:?}; freeing held resources without waiting (a hang or device removal is in progress)");
}
gone.extend(self.sealed.pop_front().unwrap().dead);
}
gone
}
}
Expand Down Expand Up @@ -352,6 +371,34 @@ mod tests {
assert!(held.is_empty());
}

/// A sealed batch whose queue never gets past it (a hung GPU, fences stalled) is let go once it
/// has been held for `STALL_FORCE`, so a wedged device does not grow the held set until the WDDM
/// kernel pool is exhausted and the device resets.
#[test]
fn a_stalled_batch_is_freed_after_the_timeout() {
unsafe {
let Ok(dev) = crate::gpu::agility::create_device(None, windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0) else {
return crate::gpu::recomp::runner::skip_test("no GPU");
};
let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_DIRECT, ..Default::default() }).unwrap();
// the queue never gets past the batch: it waits on a fence nobody signals (a wedged GPU)
let gate: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
queue.Wait(&gate, 1).unwrap();
let mut held = Held::default();
held.open.push(Dead::Resource(0x1000));
let queues = [queue.clone()];
// seals the batch; the queue is wedged, so its fence never advances and it stays held
assert!(held.drain(&queues, true).is_empty());
assert!(!held.is_empty());
// age the sealed batch past STALL_FORCE without a real wait
held.sealed.front_mut().unwrap().sealed_at = Instant::now() - STALL_FORCE - Duration::from_secs(1);
// the fence still has not advanced, but the stall timeout lets it go
assert_eq!(held.drain(&queues, false), vec![Dead::Resource(0x1000)]);
assert!(held.is_empty());
gate.Signal(1).unwrap(); // let the wedged queue drain before it drops
}
}

/// A pipeline state XCL lets go goes into a batch (a list in flight may bind it), and the
/// thread here releases it once the queues are past – through its COM Release. (That a batch
/// waits for a busy queue is `a_held_resource_goes_when_the_queue_is_past_its_batch`.)
Expand Down
Loading