Skip to content

D3D12.X: the sparse state is replaced outside its lock - #443

Open
Kalmarv wants to merge 1 commit into
ivall:mainfrom
Kalmarv:fix/sparse-state-replace-outside-lock
Open

Kalmarv wants to merge 1 commit into
ivall:mainfrom
Kalmarv:fix/sparse-state-replace-outside-lock

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

d3d12x::sparse::init installed its state with *STATE.lock().unwrap() = Some(s), so the state it replaced was dropped while the guard was held. A state that has been used holds the last references to buffers of its own: readback, staging and zeros, made lazily through the adopted device (sparse.rs 830–896). Their release goes through real::res_release, which calls sparse::forget_buffer, which locks STATE again. The thread waits for itself, and every later user of the sparse windows waits behind it. The old state is now taken out under the lock and dropped after it, as planned in the issue.

Fixes #344

The windows get set up again when two first real::init() calls overlap. real::init returns early only once DEVICE is set, and DEVICE is set after open_uma → sparse::init, so GPU tests that start together can both get there. That is how the hang in the issue came about: the issue's suite run stopped in four GPU tests until it was killed. One correction to the issue: what matters is not placed buffers in general but the state's own last-referenced buffers, which is why the test reads back a block before setting the windows up again.

What is in it

  • crates/xcl/src/hle/d3d12x/sparse.rs, sparse::init: STATE.lock().unwrap().replace(s), and the old state is dropped after the guard.
  • crates/xcl/tests/sparse_reinit.rs, the_sparse_state_is_replaced_outside_its_lock, in its own process:
    • real::init(), then a 64 KiB sparse block, written and read back (readback_now), which gives the state its readback buffer;
    • then sparse::init again with the same device, on a second thread. The test waits 60 s for it, so on main it fails instead of hanging the suite.

Tested

  • RTX 5070 Ti, NVIDIA 610.74, Windows 11 Pro 26200; MSVC stable, debug.
  • cargo test --workspace -- --test-threads=4 on 16fc79d (this PR, on main a6f5f7a): 677 passed, 0 failed, 41 ignored (the xcl library: 636 passed, 21 ignored). SKIPPED GPU tests: 2, both no wave64 (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware).
  • The new test, 3 runs each:
    • main a6f5f7a with only the test added: 3 of 3 failed with sparse::init did not return within 60 s (60.3–60.7 s);
    • this change: 3 of 3 passed (0.3–0.5 s).
  • Independent run by @MesshM (D3D12.X: the sparse state is replaced outside its lock #443 (comment)): AMD Radeon RX 9060 XT (RDNA4, driver 32.0.32029.2010), Ryzen 7 5700X, Windows 11 26200, Rust 1.100.0, debug, 16fc79d. scripts/test.ps1: 677 passed, 0 failed, 41 ignored, no SKIPPED GPU tests. With sparse.rs from main and the new test kept, the test fails after 60.33 s (1 run).
  • Independent run by @PHeitor1 (D3D12.X: the sparse state is replaced outside its lock #443 (comment)): RTX 5060 Ti (NVIDIA 621.24), Ryzen 5 5600X, Windows 11 26300, Rust 1.100.0, debug, 16fc79d merged onto main 2997c9a. cargo test --workspace --no-fail-fast -- --test-threads=4: 677 passed, 0 failed, 41 ignored; SKIPPED only the 2 no wave64 tests.

Regression run

main: Pending: no title on our machine yet; our own run follows when we have one.

PR: Pending: no title on our machine yet; our own run follows when we have one.

Not tested

  • Any title, and whether a title can reach sparse::init twice (a second device, for example).
  • Two real::init() calls that really overlap. The test calls sparse::init a second time directly, with the device it already has.
  • Which state ends up installed when two inits do overlap: unchanged by this PR, the last one wins.
  • Wine.
  • macOS / Linux: not built.
  • D3D12.X / kernelx: CommandList slots 131/132, synchronized sparse commit/decommit, and intro pool reservation #236 (open) rewrites the same lines in sparse::init and keeps the drop under the guard (*state = Some(s) while state is the guard), so whichever merges second needs a rebase.

AI use

  • Claude Code, Claude Opus 5.5: wrote the change, the test and this description; the runs above are its own.

🤖 Generated with Claude Code

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 ivall#344

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Test run of 16fc79d51385e52aaa9e14749bb23e8914ce7fb4 on AMD (RDNA4); the PR was tested on NVIDIA only.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.32029.2010; XCL picked it (backend 'real' – AMD Radeon RX 9060 XT (16191 MiB VRAM))
  • Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 677 passed, 0 failed, 41 ignored, exit code 0; xcl lib tests 636 passed, 0 failed, 21 ignored
  • 0 SKIPPED on this adapter, so the two wave64 tests skipped on the RTX 5070 Ti ran and passed here
  • the_sparse_state_is_replaced_outside_its_lock passes in the suite (sparse_reinit: 2 passed, 0.17 s)
  • with sparse.rs from main a6f5f7a and the new test kept, cargo test -p xcl --test sparse_reinit: 0 passed, 1 failed after 60.33 s (sparse::init did not return within 60 s: it waits for the lock it holds: Timeout), as the description says (1 run)

Review of the change: in let old = STATE.lock().unwrap().replace(s); the guard is a temporary of the let statement, so it is dropped at the end of that statement and drop(old) runs without the lock; forget_buffer can then take it. The caller (real.rs:348, in open_uma) holds no other lock around sparse::init. I found nothing to change.

Not tested: no retail title or regression run (I have no unpacked game), no XCL_SAMPLE_* tests, release build; the failing run on main was done once, not three times.

Run and report by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.

@PHeitor1

Copy link
Copy Markdown

Tested 16fc79d merged onto main 2997c9a.

RTX 5060 Ti 8 GB (Blackwell, 32-lane waves), NVIDIA 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native, Graphics Tools + XGameRuntime installed, so the debug-layer tests run), rustc 1.100.0 MSVC, debug, cargo test --workspace --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_*.

Pass: 677 passed, 0 failed, 41 ignored. SKIPPED: only the 2 no wave64 tests (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware). Build clean.

Run and written with Claude Code (Claude Opus 5.5).

@shdann shdann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crates/xcl/tests/sparse_reinit.rs:33 assumes that the second initialization creates at least one window. On 16fc79d, the Windows CI worker completed the call within the timeout and returned zero windows. The test then failed at this assertion. Please check that the adapter can create the second set of windows before using this integration test as the deadlock check. A separate test with a destructor that takes the same lock can cover the lock order on every worker. Failed CI job. Reviewed the diff and CI log with OpenAI Codex (GPT-6).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

D3D12.X: replacing the sparse state deadlocks when the old state still holds placed buffers

4 participants