From d96812a91dc968bb9db8707d41339b09931087d6 Mon Sep 17 00:00:00 2001 From: Kalmarv <59708937+Kalmarv@users.noreply.github.com> Date: Mon, 23 Nov 2026 19:14:41 -0500 Subject: [PATCH] D3D12.X: the sparse state is replaced outside its lock sparse::init installed its state with *STATE.lock() = Some(s): the state it replaced was dropped while the guard was held. Once used, that state holds the last references to buffers of its own (readback, staging, zeros), all made through the adopted device; their release goes through real::res_release, which calls sparse::forget_buffer, which locks STATE again - the thread waited for itself, and every later user of the sparse windows behind it. The windows are set up again when two first real::init calls overlap, as GPU tests starting together do: a suite run hung in four GPU tests until it was killed. The old state is now taken out under the lock and dropped after it. Fixes #344 Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/xcl/src/hle/d3d12x/sparse.rs | 5 +++- crates/xcl/tests/sparse_reinit.rs | 41 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 crates/xcl/tests/sparse_reinit.rs diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index 22c5a22fa..0eb79f0eb 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -546,7 +546,10 @@ pub unsafe fn init(dev: &ID3D12Device) -> Vec<(usize, usize, u64)> { match made { Ok(s) => { crate::xlog_info!("D3D12.X: {} sparse window(s) for the title's own memory (tiled buffers on video memory)", out.len()); - *STATE.lock().unwrap() = Some(s); + // a state this replaces (the windows set up again) goes after the lock: the release of + // its last buffers reaches `forget_buffer`, which takes the lock (#344) + let old = STATE.lock().unwrap().replace(s); + drop(old); for (a, b) in std::mem::take(&mut *EARLY.lock().unwrap()) { committed(a, b - a); } diff --git a/crates/xcl/tests/sparse_reinit.rs b/crates/xcl/tests/sparse_reinit.rs new file mode 100644 index 000000000..dc6936781 --- /dev/null +++ b/crates/xcl/tests/sparse_reinit.rs @@ -0,0 +1,41 @@ +//! The sparse windows set up again while the state they replace holds resources of its own +//! (#344): the old state was dropped under its lock, and the release of its last buffers +//! (`real::res_release` → `sparse::forget_buffer`) took the lock again – the thread waited for +//! itself. Its own process: when the test fails, that thread stays blocked, holding the lock. + +use std::time::Duration; + +use windows::core::Interface; +use windows::Win32::Graphics::Direct3D12::ID3D12Device; + +use xcl::hle::d3d12x::{real, sparse}; + +#[test] +fn the_sparse_state_is_replaced_outside_its_lock() { + if real::init().is_err() { + xcl::gpu::recomp::runner::skip_test("no GPU"); + return; + } + // no sparse windows on the GPU (WARP makes no reserved buffers that large): no state to replace + if real::uma_gpu_va() == 0 { + xcl::gpu::recomp::runner::skip_test("no GPU UMA"); + return; + } + unsafe { + // the state in use: a block of the windows, written and read back (its readback buffer is + // the state's own) + let (va, len) = sparse::alloc(0x1_0000, 0x1_0000).expect("a sparse block"); + std::ptr::write_bytes(va as *mut u8, 0x5A, len); + sparse::readback_now(va, len); + // set up again, as two first `real::init` calls at once do + let dev = real::device_raw() as usize; + let (done, wait) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let dev = ID3D12Device::from_raw_borrowed(&(dev as *mut std::ffi::c_void)).expect("the device").clone(); + let _ = done.send(sparse::init(&dev).len()); + }); + let windows = wait.recv_timeout(Duration::from_secs(60)).expect("sparse::init did not return within 60 s: it waits for the lock it holds"); + assert!(windows > 0, "the sparse windows were set up again"); + assert!(sparse::free(va)); + } +}