Skip to content

D3D12.X: a placed buffer's last Release does not wait for the sparse lock - #1478

Closed
jessenic wants to merge 1 commit into
xcl-dev:mainfrom
jessenic:fix/sparse-forget-off-lock
Closed

jessenic wants to merge 1 commit into
xcl-dev:mainfrom
jessenic:fix/sparse-forget-off-lock

Conversation

@jessenic

@jessenic jessenic commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

A placed buffer's last Release no longer waits for the sparse state's lock. In a retail title's story load, the frame-loop thread blocked there behind threads that held the lock across GPU waits and a tile mapping on a stalled sparse queue. Its heartbeat stopped and the title's own stall check ended the run (+0x23cf683). Measured and symbolised in #505 (comment). Part of #505.

What is in it

  • crates/xcl/src/hle/d3d12x/sparse.rs, one commit:
    • forget_buffer (called by real::res_release for every resource) queues the pointer on a new FORGET list and only tries STATE. If it gets the lock, it applies the queue, or clears it when there is no sparse state.
    • The lock's next holder applies the queue (State::forget_pending → forget_all, the old body unchanged):
      • in before_execute (every title submit);
      • in take_tile, before a new pool is made;
      • in place_buffer, at the start and again right before the new buffer is recorded, because buffers is keyed by the resource's address.
    • Until a queued release is applied, its tiles stay pinned, which only keeps them from being reused. Lock order is STATE → FORGET; forget_buffer holds neither while it takes the other.
    • A Release made while the same thread already holds STATE (from inside a D3D12 call under the lock) now queues instead of deadlocking.
  • Tests:
    • a_released_buffer_is_forgotten_while_the_state_is_locked holds STATE and calls forget_buffer on another thread. On main it fails after its 5 s.
    • a_forgotten_buffer_gives_back_only_its_unused_tiles covers which tiles a forgotten buffer gives back, and a release queued while the lock is held being applied by the next holder. It uses the real GPU, or WARP if there is none.

Related open PRs

Tested

  • RTX 5070 Ti 16 GB (driver 610.88), Ryzen 7 9700X, 32 GB RAM, Windows 11 Pro 26300, rustc 1.97.0; tests debug, title release.
  • cargo test --workspace -- --test-threads=4 on b3fb51c (the same tree as the head 792c69a, which only rewords the commit message): 772 passed, 0 failed, 41 ignored.
  • SKIPPED on this adapter: gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask: no wave64, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware: no wave64.
  • Retail title, scripts/smoke.ps1, 60 s, no input (the start screen; every resource Release goes through forget_buffer there too), against main c160d86. Blocks below.

Regression run

xcl-smoke v1
commit: c160d86ef2efe3a07e4dfb473e4fa72eafb413c0
gpu: NVIDIA GeForce RTX 5070 Ti
title: 7cc04381
seconds: 60
exit: 0x0
frames shown: 1646 (PresentX 1646)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15352 MB
xcl-smoke v1
commit: 792c69a6ddce0e9a7628da1c805a12edffd77203
gpu: NVIDIA GeForce RTX 5070 Ti
title: 7cc04381
seconds: 60
exit: 0x0
frames shown: 1643 (PresentX 1643)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15364 MB

Not tested

  • The scenario that showed the hang (XCL_PSO_WAIT_MS=50 and thread dumps through the story load) has not been rerun with this change, so whether the frame loop now gets further there is not measured. Without XCL_PSO_WAIT_MS the title stops earlier for another reason (pipeline builds; kernelx: Sleep does not count the time a bind waits for a pipeline build #1288), and the smoke runs stay on the start screen, short of the story load.
  • Still made under STATE, so still able to block (separate changes):
    • sparse::free from res_release for a resource that owns its sparse unified-memory block (decommitted takes STATE; found in review by @GonZiin, who measured it waiting with a probe). Deferring it needs the reservation kept out of reuse until the decommit is done, so it is left for its own PR;
    • before_execute / mapped_ranges on every submit;
    • the readback waits (D3D12.X: waits for the sparse queue do not hold the sparse state's lock #1385 takes those off the lock);
    • UpdateTileMappings in map_tiles.
  • The stalled sparse queue itself is not addressed.
  • Unchanged: in res_release, the object is released before forget_buffer runs. So a buffer placed at the same address in between can still be unpinned by the late forget, as on main.
  • AMD and Intel GPUs. Wine.

AI use

  • Claude Code with Claude Opus 5.5: traced the hang from thread dumps of the retail title, designed and wrote the change and the tests (with review by several agents), ran the tests and the regression runs, and wrote this description.

🤖 Generated with Claude Code

@futurepro62

futurepro62 commented Nov 24, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head b3fb51c (on its base b7aa9f3).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 772 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: a_released_buffer_is_forgotten_while_the_state_is_locked, a_forgotten_buffer_gives_back_only_its_unused_tiles.
  • Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 772 passed, 0 failed, 41 ignored, no skips.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

@eizispark

Copy link
Copy Markdown

Hardware test report — commit b3fb51c

Checked b3fb51cfbde3f95947c4d7c59cf6fe278d2ba11f (base b7aa9f3bd212088e0d927439284dc5ed325ce3c7; verified clean merge with current main 9e3831e1f570d3646e53ab753b19ec07eb8f1485): pass.

Tested

  • Hardware: Windows 11 x64, AMD Ryzen 7 9800X3D, AMD Radeon RX 9070 XT (RDNA4, native wave32 + wave64, driver 32.0.32015.16001), 32 GB RAM, rustc 1.100.0 (0ef1bc386 2026-11-09), release + test profile
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 772 passed, 0 failed, 41 ignored (GPU tests skipped on this adapter: none), including hle::d3d12x::sparse::tests::a_released_buffer_is_forgotten_while_the_state_is_locked, hle::d3d12x::sparse::tests::a_forgotten_buffer_gives_back_only_its_unused_tiles, and both wave64 hardware tests (gpu_wave32_program_in_wave64_hardware and gpu_geometry_wave32_prefix_count_over_a_mask)

Regression run
main (b7aa9f3):

xcl-smoke v1
commit: b7aa9f3bd212088e0d927439284dc5ed325ce3c7
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1678 (PresentX 1678)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14855 MB

PR (b3fb51c):

xcl-smoke v1
commit: b3fb51cfbde3f95947c4d7c59cf6fe278d2ba11f
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1679 (PresentX 1679)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14859 MB

Not tested

  • NVIDIA/Intel GPUs, Wine/vkd3d-proton, world-load runs past 60 s where STATE is held across a stalled GPU wait, XCL_SAMPLE_*

AI use

  • Antigravity (Google DeepMind, Gemini 4 Carbon) — ran the workspace test suite and 60 s retail smoke run on this commit and formatted the report.

@GonZiin GonZiin 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.

The final Release can still wait for STATE when the resource owns its sparse UMA allocation. place_console_buffer_sparse records that resource with owned = true. After the new non-blocking forget_buffer, res_release removes the entry from UMA_BUFFERS and calls sparse::free → decommitted → STATE.lock(). This is a separate lock acquisition from the one this PR removes.

Tested

  • Head b3fb51cfbde3f95947c4d7c59cf6fe278d2ba11f, Windows 11 Pro build 26300, Ryzen 7 7800X3D, Rust 1.100.0 MSVC, debug. Used XCL_LATE_RELEASE=0 and XCL_COHERENT_MB=16 in an isolated checkout.
  • cargo test -p xcl --lib a_released_buffer_is_forgotten_while_the_state_is_locked -- --nocapture --test-threads=1 passed (1 passed, 0 failed, 0 ignored).
  • cargo test -p xcl --lib review_last_release_of_owned_sparse_buffer_does_not_wait_for_state -- --nocapture --test-threads=1 ran a temporary probe using the actual res_release wrapper, a mock COM last-release callback, and a real sparse UMA allocation. With STATE held, the untracked control returned. The owned sparse release timed out after 400 ms and finished once the lock was released (0 passed, 1 failed, 0 ignored).

Please narrow the claim to the placed-buffer bookkeeping, or cover the owned sparse path too. If decommit is deferred, the reservation must stay unavailable for reuse until decommit finishes.

Not tested

Retail title behavior and the default late-release setting.

AI use

Codex (gpt-6.2-sol) traced the call path, wrote and ran the probe, and drafted this review.

@KaliemSB

Copy link
Copy Markdown

Test report: RTX 4060 (NVIDIA, 32-lane), XCL_DRED=1: main stops presenting at ~6 s in both rounds (4 and 3 frames in 60 s); this PR runs normally (296 and 277)

Tested

  • NVIDIA GeForce RTX 4060 8 GB (Ada), driver 621.24 (32.0.16.2124); 32 GB RAM; Windows 11 Pro 26100; DXC 1.9.2609.5 (XCL_DXC_DIR).
  • This PR's head b3fb51c and main 9e3831e, release builds of each. The newer commits on main since this PR's base don't touch sparse.rs, real.rs or alloc_pool.rs.
  • scripts/smoke.ps1 -Game <retail title> -Seconds 60 with XCL_DRED=1, alternating main / PR, two rounds. About 14.7 GB of commit free at start in every run (under the 17 GB check, so the 512 MB coherent window), the same on both sides.
run frames shown presents per 2 s (last 3) device removals crashes
main, round 1 4 0 1 3 0 0
this PR, round 1 296 14 15 16 0 0
main, round 2 3 0 1 2 0 0
this PR, round 2 277 11 12 14 0 0
  • An earlier freeze of main (b7aa9f3) on this machine, with XCL_DRED=1 and the older driver 596.49, had a thread dump symbolised with the build's PDB. It showed the cycle this PR describes:
    • q_execute_command_lists → title_textures::refresh → sparse::before_execute → State::upload_dirty → State::begin_slot → ID3D12CommandAllocator::Reset, waiting in D3D12Core with the sparse-state lock held;
    • list_x::reset_x → alloc_pool::reset_list → D3D12Core → real::res_release → sparse::forget_buffer, waiting for that lock;
    • and XMemMapPhysicalPagesScatter → sparse::committed, also waiting for it.
  • Without XCL_DRED, earlier the same day (main b7aa9f3 268 frames, this PR 311), no device removal and no crash on either.
  • cargo test --workspace -- --test-threads=4 on b3fb51c (debug): 772 passed, 0 failed, 41 ignored. SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware (no wave64). Both of this PR's tests (a_released_buffer_is_forgotten_while_the_state_is_locked, a_forgotten_buffer_gives_back_only_its_unused_tiles) ran on the RTX 4060 and passed.

Not tested: no thread dump of the frozen main runs on this commit, and nothing past 60 s with DRED.

AI use: Claude Code (Claude Opus 5.5) ran the builds, tests and runs, symbolised the thread dump and wrote this report. The runs are from the machine named.

xcl-smoke blocks (main, then this PR; round 1)
xcl-smoke v1
commit: 9e3831e1f570d3646e53ab753b19ec07eb8f1485
gpu: NVIDIA GeForce RTX 4060
title: gta6_dump
seconds: 60
exit: 0x0
frames shown: 4 (PresentX 4)
presents per 2 s (last 3): 0 1 3
device removals: 0
crashes: 0
peak commit: 7503 MB
xcl-smoke v1
commit: b3fb51cfbde3f95947c4d7c59cf6fe278d2ba11f
gpu: NVIDIA GeForce RTX 4060
title: gta6_dump
seconds: 60
exit: 0x0
frames shown: 296 (PresentX 296)
presents per 2 s (last 3): 14 15 16
device removals: 0
crashes: 0
peak commit: 18076 MB
xcl-smoke blocks (round 2)
xcl-smoke v1
commit: 9e3831e1f570d3646e53ab753b19ec07eb8f1485
gpu: NVIDIA GeForce RTX 4060
title: gta6_dump
seconds: 60
exit: 0x0
frames shown: 3 (PresentX 3)
presents per 2 s (last 3): 0 1 2
device removals: 0
crashes: 0
peak commit: 7838 MB
xcl-smoke v1
commit: b3fb51cfbde3f95947c4d7c59cf6fe278d2ba11f
gpu: NVIDIA GeForce RTX 4060
title: gta6_dump
seconds: 60
exit: 0x0
frames shown: 277 (PresentX 277)
presents per 2 s (last 3): 11 12 14
device removals: 0
crashes: 0
peak commit: 18427 MB

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 24, 2026
…README

Integrates 10 open upstream PRs on top of upstream/main (c160d86):
- xcl-dev#1643: per-queue cpu_writes worker threads (eliminating cross-queue
  head-of-line stalls) and atomic 64-bit immediate writes
- xcl-dev#1618: skip DXC validator where the runtime accepts the BYPASS hash
- xcl-dev#1535, xcl-dev#1536: PresentX COPY_SOURCE barrier and sparse upload COMMON
  state transitions
- xcl-dev#1516: route stream graphics PSO creation through tracked path
- xcl-dev#1478: avoid taking sparse state lock on resource final Release
- xcl-dev#1473: implement S_QUADMASK_B32 and S_QUADMASK_B64
- xcl-dev#1621, xcl-dev#1625: frame clock revalidation and short uncompressed
  DirectStorage request zero-padding
- xcl-dev#1557: accept no buffer context before Start in XMA2 voice test under
  Wine

Verified under GE-Proton11-14 + vkd3d-proton ec20719452e916b:
- Release build (x86_64-pc-windows-msvc): ok (32.14s)
- Proton unit tests (heap_window_tests, dxil::core_tests, cpu_writes,
  frame, sparse, present, dstorage, xma2_source_voice,
  gpu_sparse_vram_descriptors_buffers_and_xmem, gpu_valu_float_and_int):
  71 passed, 0 failed, 2 ignored, 0 skipped.
@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

…lock

A retail title's frame loop releases resources every frame. Each last
Release goes through real::res_release to sparse::forget_buffer, which
took the sparse state's lock. Other threads hold that lock across GPU
waits (readback_now -> State::wait_for, 5 s at a time) and across
UpdateTileMappings in map_tiles. When the sparse queue stopped moving
(story load with XCL_PSO_WAIT_MS=50, RTX 5070 Ti), thread dumps showed
the frame-loop thread in Mutex::lock_contended <- forget_buffer <-
res_release from 162 s to the end. Its heartbeat stopped, and the
title's stall check ended it at 186.7 s (xcl-dev#505).

forget_buffer now queues the buffer and only tries the lock. The lock's
next holder applies what is queued: in before_execute (every submit),
in take_tile before a new pool is made, and in place_buffer before and
right before a new buffer is recorded under an address that may have
been in the queue. With no sparse state the queue is cleared. Until a
queued release is applied, its tiles stay pinned, which only keeps them
from being reused.

Not covered: a resource that owns its sparse unified-memory block still
reaches sparse::free -> decommitted from res_release, which takes the
same lock (found in review).

Test a_released_buffer_is_forgotten_while_the_state_is_locked fails on
main after its 5 s (forget_buffer waited for the lock).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jessenic
jessenic force-pushed the fix/sparse-forget-off-lock branch from b3fb51c to 792c69a Compare November 24, 2026 19:11
@jessenic jessenic changed the title D3D12.X: a resource's last Release does not wait for the sparse state's lock D3D12.X: a placed buffer's last Release does not wait for the sparse lock Nov 24, 2026
@jessenic

Copy link
Copy Markdown
Contributor Author

@GonZiin thanks, you're right, and thanks for the probe. The title claimed more than the change does: sparse::free → decommitted from res_release, for a resource that owns its sparse unified-memory block, still takes STATE. I narrowed the claim rather than widening the change:

  • Title and commit are now "a placed buffer's last Release does not wait for the sparse lock" (head 792c69a; same tree as b3fb51c, only the message changed). The commit message and Not tested name the owned-block path and credit your finding.
  • Deferring that decommit is a separate change: as you say, the reservation must stay out of reuse until the decommit is done. I'd rather do it in its own PR with a test like your probe than grow this one.
  • Smoke pair rerun for the new head against current main c160d86 (60 s, both clean, 1646 / 1643 frames), in the description.

AI use: Claude Code (Claude Opus 5.5) made the change and wrote this reply.

@alexodee

alexodee commented Nov 24, 2026 •

Copy link
Copy Markdown

This also fixes a second path into the same deadlock: with XCL_DRED=1, the retail title (1.0.2.1) hung at 0 FPS on both starts I tried on c160d86 (RTX 3070 Ti Laptop, driver 596.36, Windows 11 26200). Without XCL_DRED it ran at 30 FPS.

Thread dump (XCL_THREAD_DUMP_AT=60,120, symbolised with the release PDB):

title thread (holds a D3D12Core lock, waits for STATE)
  Mutex::lock_contended
  xcl::hle::d3d12x::sparse::forget_buffer
  xcl::hle::d3d12x::real::res_release
  D3D12Core.dll+0x8a10c < +0x106809 < +0x418a3 < +0x209ee9
  xcl::hle::d3d12x::alloc_pool::reset_list
  xcl::hle::d3d12x::list_x::reset_x
  game_scarlett_master.exe+0x2474cd4

queue dispatch thread (holds STATE, waits for the D3D12Core lock)
  msvcp_win.dll+0x13191 (mutex)
  D3D12Core.dll+0x421d0 < +0x41b97
  ID3D12CommandAllocator::Reset
  xcl::hle::d3d12x::sparse::State::begin_slot
  xcl::hle::d3d12x::sparse::State::upload_dirty
  xcl::hle::d3d12x::sparse::before_execute
  xcl::hle::d3d12x::title_textures::refresh
  xcl::hle::d3d12x::real::q_execute_command_lists

I wrote the same change locally before finding this PR (forget_buffer only tries STATE and queues the pointer otherwise; the queue is applied in before_execute and in place_buffer before the new buffer is recorded). With it, the same XCL_DRED=1 run no longer hangs: it presented at least 2,400 frames (about 30 fps), then ended with a device removal at 112 s, reported in #675. I did not check which screen it had reached, and this is one run.

So, as a data point: the same approach removed the XCL_DRED=1 hang here. I tested my own version of the change, not this PR's commit.

Edited: corrected claims I had not verified (every start, story load, GPU page fault). Analysis and text written with AI assistance.

@throny

throny commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Two contributors checked independently and found that #1697 solves the same problem (for #505) in a different way: #1607, group D42. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint, not a request from the maintainers; nobody from the project has adopted #1607 so far.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

@jessenic

Copy link
Copy Markdown
Contributor Author

Closing in favour of #1697 (@shzyjbr06-dev), which covers everything this PR does and more: the same non-blocking forget_buffer with the queue drained in before_execute and place_buffer, plus per-buffer generation IDs (which also close the address-reuse race noted in @GonZiin's review), a lock-free pool_bytes, and CreateReservedResource / CreateHeap outside STATE. Thanks for the hint (#1607, D42).

What was measured on this PR, in case it helps #1697's review, since the mechanism is the same:

Thanks everyone who tested and reviewed.

AI use: Claude Code (Claude Opus 5.5) compared the two PRs and wrote this comment.

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.

8 participants