Skip to content

D3D12.X: a held resource keeps its memory until its Release goes - #1070

Open
DTsordas wants to merge 1 commit into
xcl-dev:mainfrom
DTsordas:d3d12x-held-release-keeps-memory
Open

DTsordas wants to merge 1 commit into
xcl-dev:mainfrom
DTsordas:d3d12x-held-release-keeps-memory

Conversation

@DTsordas

@DTsordas DTsordas commented Nov 24, 2026 •

Copy link
Copy Markdown

Late release (e7da8a1) holds a resource whose last Release comes while a queue may still have work on it. But res_release still gave back, at the title's Release, what XCL had placed under the resource. The unified-memory block went back on the free list, so the next resource XCL placed could get it while the GPU still used it. A sparse block was decommitted and its address range returned, and the buffer's tiles were unpinned (sparse::forget_buffer → release_if_unused). The held resource's in-flight work could then read another resource's data or unmapped tiles. e7da8a1 measured 356 held releases on the retail title.

XCL's bookkeeping for the resource still goes at the title's Release, as before (a held object keeps its address). A held resource's block and its tile pins go to late_release with it. Once the queues are past it, its tiles are unpinned and a sparse block is freed before its final Release (while the resource is alive, its address is no other resource's), and a block in the coherent window after it. A release that is not held frees at once, as before. vram::forget (arena release, which has its own delay) and title_textures::forget (bookkeeping) are unchanged. Fixes #1040.

Since 9aa0e9a (duudiis's review below): the drain no longer waits for the sparse state's lock. It runs inside XCL's own submits and inside D3D12 calls that make a resource's last Release, where the lock's holder can be waiting for that thread (the paths #789 and #968 take the lock off). sparse::try_forget_held takes the lock only if it is free; otherwise the resource goes back as an already-passed batch and goes at the next drain.

What is in it

  • crates/xcl/src/hle/d3d12x/real.rs: res_release passes the block to late_release::hold when it holds the resource, and skips sparse::forget_buffer then.
  • crates/xcl/src/hle/d3d12x/late_release.rs: held entries carry their block. let_go unpins the tiles and frees a sparse block (sparse::try_forget_held), releases the resource, then frees a coherent block; an entry the sparse state is busy for goes back to the next drain (Held::again). The two existing tests now carry the pair.
  • crates/xcl/src/hle/d3d12x/sparse.rs: try_forget_held (STATE.try_lock()). forget_buffer's loop and free's decommit are shared with it (State::forget_buffer, free_with, decommitted_in); lock_for_test (test-only).
  • crates/xcl/src/gpu/uma.rs: Uma::is_allocated, test-only (#[cfg(test)]).
  • Tests:
    • held_release_tests::a_held_resource_keeps_its_unified_memory_until_it_goes: a known queue stands still on a fence; an adopted buffer gets a unified-memory block recorded as place_console_buffer does; its last Release goes through res_release. The block must stay allocated while the queue has work, and go once it has passed. On main it fails at the first check ("the block under a held resource went back while a queue still had work").
    • late_release::tests::letting_go_does_not_wait_for_the_sparse_state: with the sparse state locked on another thread, letting a resource go returns at once and keeps it for later. With the drain's earlier blocking forget_buffer it fails after 5 s ("the drain waited for the sparse state's lock").
    • late_release::tests::a_resource_put_back_goes_at_the_next_drain: a resource put back goes at the next drain, ahead of a batch a stalled queue has not passed.

Tested

  • AMD Radeon RX 9060 XT (driver 32.0.32029.2010), Ryzen 5 5600X, Windows 11 Pro 26300, Rust 1.100.0 MSVC, debug; DXC 1.9.2602.17 (SDK 10.0.26100.0).
  • cargo test --workspace --no-fail-fast -- --test-threads=4 at d0ebe34 (on main 47efa2e): 744 passed, 0 failed, 41 ignored, 0 SKIPPED.
  • cargo test -p xcl --lib -- late_release held_release_tests hle::d3d12x::sparse at d0ebe34: 18 passed, 0 failed, 1 ignored.
  • held_release_tests fails on main and passes with the change (checked when this PR was opened; main has not moved since).

Regression run

The title's 60 s smoke run (scripts/smoke.ps1, release builds, same machine, back to back).

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: AMD Radeon RX 9060 XT
title: Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1350 (PresentX 1351)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17525 MB

PR:

xcl-smoke v1
commit: d0ebe34473f3668b1ed9a58bbd2294cbbbed90c4
gpu: AMD Radeon RX 9060 XT
title: Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1333 (PresentX 1334)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17839 MB

Not tested

AI use

  • Claude Code (Claude Opus 5.5): found the bug, wrote the test, the fix (and its rework after the review) and this description. I read the diff and ran the tests above.

🤖 Generated with Claude Code

@dcastell1

Copy link
Copy Markdown

Verified on Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.100.0 MSVC 14.44.35207, debug build, on main@47efa2e (baseline 741 passed, 0 failed, 41 ignored on this box):

patch -p1 < this PR            # clean, no fuzz
cargo test --workspace -- --test-threads=4
742 passed; 0 failed; 41 ignored   (exit 0)

One more passing test than the baseline; a held resource keeping its memory until its last use is covered and green.

Tree restored afterwards (full diff against a clean checkout), hardware adapter, no Wine, no game data. AI-assisted (opencode): applied the patch to a built tree, ran the suite, reverted it.

@duudiis

duudiis commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

This deadlocks a retail title a few seconds into its intro on our tree, which has this PR merged: the submit thread waits for itself.

late_release::drain_known now calls sparse::forget_buffer, which locks sparse::STATE. The drain runs inside ExecuteCommandLists (present::note_execute), and XCL's own sparse-window uploads go through ExecuteCommandLists while sparse::before_execute holds STATE:

q_execute_command_lists (title's submit)
  title_textures::refresh
    sparse::before_execute            <- STATE locked
      State::upload_dirty
        State::submit_batch
          title_textures::own_work(ExecuteCommandLists on the title's queue, #262)
            q_execute_command_lists (hooked)
              present::note_execute
                late_release::drain_known
                  sparse::forget_buffer   <- STATE.lock(): waits for itself

Every thread after it waits on STATE too (title Releases going through late_release::hold, XCL's pipeline workers), so no frame is presented after that point. It shows up only once a held resource that has a sparse buffer is due when an upload batch is submitted, so on our runs it hit at 6 s, 8.7 s and 16 s. The stacks are from a thread dump (XCL_THREAD_DUMP_AT), symbolized against the build's PDB.

What fixed it for us (our fork, D3D12.X: late release's drain no longer waits for the sparse state its own thread holds):

  • drain_known returns at once inside title_textures::own_work (is_own_work()): the caller may hold XCL's locks. The held resources go at the next title submit or on the late-release thread.
  • The drain frees sparse buffers with a try_forget_buffer (STATE.try_lock()). When the state is locked, the resource is put back as an already-passed batch (Batch { marks: [] }, taken at the next drain) instead of waiting.

With both, the intro plays again on the same build. I can open that as a PR stacked on this one if that's useful.

Tested: Windows 11, RTX (NVIDIA, 12 GB), Intel CPU; cargo test -p xcl --lib --release -- --test-threads=2 with the change: 1055 passed, 0 failed (aperture tests run separately: 3 passed); new tests sparse::try_forget_buffer_does_not_wait_for_the_state_this_thread_holds, late_release::a_requeued_batch_goes_at_the_next_drain and no_drain_inside_xcls_own_work.

AI use: found from the thread dump and written by Claude Code (Claude Opus 5.5); the call path was checked against this PR's head.

@DTsordas
DTsordas marked this pull request as ready for review November 24, 2026 13:19
Late release (e7da8a1) holds a resource whose last Release comes while a
queue may still use it, but res_release still gave back what XCL placed
under it at the title's Release: the unified-memory block went back on
the free list (the next resource could get it while the GPU still used
it), and a sparse block was decommitted and its tiles unpinned.

The bookkeeping still goes at the title's Release. A held resource's
block and its sparse tile pins go to late_release. Once the queues are
past it, its tiles are unpinned and a sparse block is freed before its
final Release (its address is no other resource's yet), and a block in a
coherent window after it. A release that is not held frees at once, as
before.

The drain does not wait for the sparse state's lock for this
(sparse::try_forget_held): it runs inside XCL's own submits and inside
D3D12 calls that make a last Release, where the lock's holder can be
waiting for that thread. A resource it cannot let go then goes at the
next drain.

Fixes xcl-dev#1040

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DTsordas

Copy link
Copy Markdown
Author

@duudiis thanks for the stacks and the call path.

On upstream main (47efa2e) that exact chain doesn't form: State::upload_dirty submits through submit_slot on the sparse state's own queue, which sparse::init creates before the device is adopted, so its ExecuteCommandLists never reaches q_execute_command_lists / present::note_execute. In your tree the uploads go out on the title's queue (the #262 change), and #968 is in it too, I think (without #968, drain_known would already block at sparse::queue()). But the hazard is in this PR all the same. The drain runs where the sparse state's holder can be waiting for that thread: your path, and the one #789 / #968 take the lock off (a last Release that D3D12 makes inside its own call). Once those two merge, this PR would put the lock back on that path.

Pushed d0ebe34 (squashed into the one commit). The drain no longer waits for the state: sparse::try_forget_held unpins the tiles and frees a sparse block only when STATE.try_lock() succeeds. Otherwise the resource goes back as an already-passed batch and goes at the next drain (the next submit, or the late-release thread within 4 ms). Same idea as your try_forget_buffer + requeue, plus the sparse block's free, since sparse::free → decommitted takes the state's lock too. I left out the is_own_work() early return: with the try-lock, a drain inside XCL's own work puts the resources back instead of waiting.

Tests: late_release::tests::letting_go_does_not_wait_for_the_sparse_state (with the drain's previous blocking forget_buffer it fails after 5 s: "the drain waited for the sparse state's lock") and a_resource_put_back_goes_at_the_next_drain. Full suite at d0ebe34: 744 passed, 0 failed, 41 ignored, 0 SKIPPED (RX 9060 XT); the smoke pair in the description is redone on it.

If you can, could you run d0ebe34 in the tree where the intro stood still? Then a stacked PR shouldn't be needed for this, unless your tree needs the own_work guard for something I've missed.

(Traced and written with Claude Code, Claude Opus 5.5; the call path checked against upstream main only, not your tree.)

Revens2 added a commit to Revens2/xcl that referenced this pull request Nov 24, 2026
Integrates upstream PRs ivall#1055 and ivall#1102 (issue ivall#1031) and ivall#1070 (issue ivall#1040).

Assisted-by: xcl-ops (deepseek-v4.1-flash)
Trace-Issue: ivall#1031
CalvyPZ pushed a commit to CalvyPZ/xcl that referenced this pull request Nov 24, 2026
…1394)

mem_heap_release gave a heap's HEAP_UMA block back at the title's last
Release, also while a queue could still read the buffers placed in it
(they live in the block, not in the PC heap). A coherent block went
back on the free list, where the next allocation could take it; a
sparse one was decommitted.

A heap with a block is now held like a resource (late_release), and its
block goes to late_release::hold with it (xcl-dev#1070), to be given back once
the queues are past the work in flight. A heap without a block goes at
once, as before.

The test holds a queue on a fence, releases a heap with a block and
checks the block stays allocated until the fence is signalled; it fails
without the change.
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: a held last Release gives the resource's unified memory / sparse tiles back while the queues still use it

3 participants