D3D12.X: a held resource keeps its memory until its Release goes #1070
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!1070
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1070"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@DTsordas on GitHub · 2026-11-24 11:16 UTC · original
DTsordas:d3d12x-held-release-keeps-memory→main· +227 −27 · 4 files · 1 commits · openLate release (
e7da8a1) holds a resource whose lastReleasecomes while a queue may still have work on it. Butres_releasestill gave back, at the title'sRelease, 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.e7da8a1measured 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 tolate_releasewith it. Once the queues are past it, its tiles are unpinned and a sparse block is freed before its finalRelease(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) andtitle_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 lastRelease, where the lock's holder can be waiting for that thread (the paths #789 and #968 take the lock off).sparse::try_forget_heldtakes 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_releasepasses the block tolate_release::holdwhen it holds the resource, and skipssparse::forget_bufferthen.crates/xcl/src/hle/d3d12x/late_release.rs: held entries carry their block.let_gounpins 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 andfree'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)]).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 asplace_console_bufferdoes; its lastReleasegoes throughres_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 blockingforget_bufferit 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
cargo test --workspace --no-fail-fast -- --test-threads=4atd0ebe34(on main47efa2e): 744 passed, 0 failed, 41 ignored, 0 SKIPPED.cargo test -p xcl --lib -- late_release held_release_tests hle::d3d12x::sparseatd0ebe34: 18 passed, 0 failed, 1 ignored.held_release_testsfails 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:
PR:
Not tested
maintoo (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.XCL_LATE_RELEASE=0(nothing is held, so the change does nothing there).AI use
🤖 Generated with Claude Code
@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):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 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_knownnow callssparse::forget_buffer, which lockssparse::STATE. The drain runs insideExecuteCommandLists(present::note_execute), and XCL's own sparse-window uploads go throughExecuteCommandListswhilesparse::before_executeholdsSTATE:Every thread after it waits on
STATEtoo (titleReleases going throughlate_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_knownreturns at once insidetitle_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.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=2with the change: 1055 passed, 0 failed (aperture tests run separately: 3 passed); new testssparse::try_forget_buffer_does_not_wait_for_the_state_this_thread_holds,late_release::a_requeued_batch_goes_at_the_next_drainandno_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 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_dirtysubmits throughsubmit_sloton the sparse state's own queue, whichsparse::initcreates before the device is adopted, so itsExecuteCommandListsnever reachesq_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_knownwould already block atsparse::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 lastReleasethat 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_heldunpins the tiles and frees a sparse block only whenSTATE.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 yourtry_forget_buffer+ requeue, plus the sparse block's free, sincesparse::free→decommittedtakes the state's lock too. I left out theis_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 blockingforget_bufferit fails after 5 s: "the drain waited for the sparse state's lock") anda_resource_put_back_goes_at_the_next_drain. Full suite atd0ebe34: 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
d0ebe34in the tree where the intro stood still? Then a stacked PR shouldn't be needed for this, unless your tree needs theown_workguard for something I've missed.(Traced and written with Claude Code, Claude Opus 5.5; the call path checked against upstream
mainonly, not your tree.)@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.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.Merge
Merge the changes and update on Forgejo.