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

Open
github-bot wants to merge 1 commit from github-bot/gh-1070 into main AGit
Owner

@⁠DTsordas on GitHub · 2026-11-24 11:16 UTC · original
DTsordas:d3d12x-held-release-keeps-memory → main · +227 −27 · 4 files · 1 commits · open

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

  • A retail title's world load, where most held releases come: on this machine the title stops there on main too (its watchdog, #505), so only its first minute is covered above. The corruption itself (what a reused block looks like on screen) is not observed, only the early free.
  • duudiis's tree (uploads on the title's queue, with #968): the self-deadlock is covered by the unit test only.
  • The sparse path end to end: the test uses a block in the coherent window. The sparse branch goes through the same calls.
  • NVIDIA / Intel; XCL_LATE_RELEASE=0 (nothing is held, so the change does nothing there).

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

**@⁠DTsordas** on GitHub · 2026-11-24 11:16 UTC · [original](https://github.com/xcl-dev/xcl/pull/1070) `DTsordas:d3d12x-held-release-keeps-memory` → `main` · +227 −27 · 4 files · 1 commits · open 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** - A retail title's world load, where most held releases come: on this machine the title stops there on `main` too (its watchdog, #505), so only its first minute is covered above. The corruption itself (what a reused block looks like on screen) is not observed, only the early free. - duudiis's tree (uploads on the title's queue, with #968): the self-deadlock is covered by the unit test only. - The sparse path end to end: the test uses a block in the coherent window. The sparse branch goes through the same calls. - NVIDIA / Intel; `XCL_LATE_RELEASE=0` (nothing is held, so the change does nothing there). **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](https://claude.com/claude-code)
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 #1040

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:42:28 +00:00
Author
Owner

@⁠dcastell1 on GitHub · 2026-11-24 11:21 UTC · original

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.

**@⁠dcastell1** on GitHub · 2026-11-24 11:21 UTC · [original](https://github.com/xcl-dev/xcl/pull/1070#issuecomment-7129985777) 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.
Author
Owner

@⁠duudiis on GitHub · 2026-11-24 12:56 UTC · original

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.

**@⁠duudiis** on GitHub · 2026-11-24 12:56 UTC · [original](https://github.com/xcl-dev/xcl/pull/1070#issuecomment-7132130221) 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 `Release`s 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.
Author
Owner

@⁠DTsordas on GitHub · 2026-11-24 13:55 UTC · original

@⁠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.)

**@⁠DTsordas** on GitHub · 2026-11-24 13:55 UTC · [original](https://github.com/xcl-dev/xcl/pull/1070#issuecomment-7133530207) @⁠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.)
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 08:51 UTC · original

Verification on RTX 3060 Ti (Windows 11): merge conflict with current main (crates/xcl/src/hle/d3d12x/sparse.rs) — needs a rebase before it can be tested.

**@⁠DavidM1911** on GitHub · 2026-11-25 08:51 UTC · [original](https://github.com/xcl-dev/xcl/pull/1070#issuecomment-7157560078) Verification on RTX 3060 Ti (Windows 11): **merge conflict with current `main`** (`crates/xcl/src/hle/d3d12x/sparse.rs`) — needs a rebase before it can be tested.
This pull request has changes conflicting with the target branch.
  • crates/xcl/src/hle/d3d12x/real.rs
  • crates/xcl/src/hle/d3d12x/sparse.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1070/head:github-bot/gh-1070'
git switch 'github-bot/gh-1070'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1070'
git switch 'github-bot/gh-1070'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1070'
git switch 'github-bot/gh-1070'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1070'
git switch 'main'
git merge --squash 'github-bot/gh-1070'
git switch 'main'
git merge --ff-only 'github-bot/gh-1070'
git switch 'main'
git merge 'github-bot/gh-1070'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.