Skip to content

Placements: drop completed unmaps' references after releasing REGISTRY - #3665

Open
Prime1997 wants to merge 1 commit into
xcl-dev:mainfrom
Prime1997:contrib/up-placements-unmap-refs
Open

Prime1997 wants to merge 1 commit into
xcl-dev:mainfrom
Prime1997:contrib/up-placements-unmap-refs

Conversation

@Prime1997

Copy link
Copy Markdown
Collaborator

Requested by Prime

placements::unmap_if_superseded holds the placements REGISTRY mutex while it calls vram::unmap_superseded_target. That call also finishes the other held unmaps on the device whose fences have passed, and it dropped their resource references before returning, so still under REGISTRY. If one of those is the last reference to a target the title released while its unmap was held, the hooked Release runs res_release -> forget_for_recycle -> placements::released -> settle, which locks REGISTRY again on the same thread. The thread then waits for itself, and every later CreatePlacedResource, note_use and held unmap waits behind it: the title hangs. We found it in our fork, where another change makes this case common; on main it is latent but the path is the same. Now the completed references are returned to unmap_if_superseded, which drops them after it lets REGISTRY go. REGISTRY is also locked through a small lock() helper that fails a debug assertion when a thread locks it twice, so tests catch this kind of re-entry instead of hanging.

Related: #3577 (draft) changes how note_use takes REGISTRY; the two touch the same lock line in note_use, so whichever lands second needs a one-line rebase.

What is in it

  • crates/xcl/src/hle/d3d12x/vram.rs: unmap_placed and unmap_superseded_target return the completed held unmaps' references (#[must_use]) instead of dropping them; unmap_superseded, whose callers hold no lock, drops them as before.
  • crates/xcl/src/hle/d3d12x/placements.rs: unmap_if_superseded drops the REGISTRY guard, then the returned references. lock() replaces REGISTRY.lock().unwrap() and keeps a thread-local "held" flag that a debug assertion checks.

Tested

  • RTX 3060 Ti 8 GB, Windows 11, release build: the change has been in our test builds since 30 Nov (no measured before/after on its own).
  • This branch (on main 1c8b551): cargo check -p xcl --lib --tests --target x86_64-pc-windows-gnu passes. The test suite was not run on this exact branch.

Regression run

  • Not run yet: testers welcome.

Not tested

  • The suite and scripts/smoke.ps1 on this branch; AMD.
  • No unit test for the re-entry itself: it needs a live device and a held unmap that completes after the title released its target. The debug assertion covers any test that reaches it.

AI use

  • Claude Code wrote the change and this description.

🤖 Generated with Claude Code

unmap_if_superseded holds placements::REGISTRY across
vram::unmap_superseded_target. Its unmap_placed also collects every
completed held unmap on the device and dropped those references before
returning, so still under REGISTRY. If one of them is the last reference
to a target the title already released while its unmap was held, the
hooked Release runs res_release -> forget_for_recycle ->
placements::released -> settle, which locks REGISTRY again on the same
thread. That deadlocks the thread, and a stuck REGISTRY then blocks
CreatePlacedResource, note_use and every later held unmap.

unmap_placed and unmap_superseded_target now return the completed
references (must_use); unmap_if_superseded decides and unmaps under
REGISTRY, drops the guard, then drops them. unmap_superseded, whose
callers hold no lock, drops them as before. REGISTRY is now locked
through lock(), which keeps a thread-local "held" flag and fails a
debug assertion on re-entry instead of deadlocking silently, so tests
catch this kind of re-entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Prime1997 Prime1997 self-assigned this Dec 2, 2026
@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:memory Virtual/physical memory, arenas, VRAM budget, UMA gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Dec 2, 2026
@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Review (head 15ae802, diff read, not a merge-check; checks red):

  • PR description check is red: no regression run. PR checks refuses the PR because the runtime changes (placements.rs, vram.rs) and Regression run has no xcl-smoke block from this head (15ae802) or from main. Tested also has no suite run on this branch, only cargo check for the x86_64-pc-windows-gnu target. "In our test builds since 30 Nov" is about a fork with other changes, not this commit.
  • No test for the fix. A bug fix needs a test that fails without it. D3D12.X: drop replaced dispatch chain outside CHAINS mutex #3648 fixes the same kind of bug (a replaced value dropped under CHAINS) and tests it without a device: a value whose Drop checks with try_lock that the map's lock is free. A test like that would work here, for example a Drop type in the returned Vec that checks REGISTRY with try_lock. At the least, a #[should_panic] test that calls lock() twice should cover the new assertion. Right now nothing exercises the assertion.
  • "So tests catch this kind of re-entry" does not hold yet. No test goes through lock(). The tests in placements.rs (~717, ~778) call Registry methods directly. placed, settle, note_use and unmap_if_superseded are only reached from the D3D12 hooks on a live device. So the debug_assert in lock() (~388) fires only in a debug build that runs a title. In release builds a re-entry still hangs, as before.

Fine: by reading, the hang described is real on main. The Release hook takes the last-reference path in real.rs ~2541 and calls vram::forget_for_recycle (~928), which calls placements::released, then settle, which locks REGISTRY again. Before this change that ran inside unmap_if_superseded's lock whenever a completed held unmap held the last reference to a target the title had already released. Dropping the references after the guard leaves no window for address reuse: the returned Vec keeps those objects alive, so their addresses (the registry keys) cannot be reused before the release cleanup runs. That cleanup then runs with no lock held, like any title Release. It is the same order as unmap_superseded's callers, before_execute (vram.rs ~2895-2930, completed_unmaps dropped after PAGES) and map_again/settle. Every early return of unmap_placed now returns an empty Vec. Every REGISTRY lock goes through lock(). #3648 uses the same pattern in another file, and #3651 and #3653 merge cleanly with this PR without overlapping it.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Test report (not a merge-check: PR description red)
base: 1c8b551
head: 15ae802
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2909 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read by one reviewer at this same head; the review is at #3665 (comment) and its findings stand. Most serious: no test for the fix (the code is correct by reading).
performance: 60 s smoke of a retail title, main 1c8b551 vs this merge (15ae802): 1652 -> 1678 frames shown (101 %; last presents per 2 s: 61 60 60), peak commit 18228 -> 17254 MB, 0 device removals, 0 crashes.
assessment: merges, builds and passes the whole suite on current main, and the smoke run is clean; the blocking point is in the review: no test for the fix (the code is correct by reading)
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

@Edgaras0404

Copy link
Copy Markdown

Test report (not a merge-check: PR description is red — the description has no regression run; Build and test is green on this head)
base: 5eaadb5
head: 15ae802
merge: clean (merge commit e473ffc2d7157c978d8dfec09628a6e05b0c5290)
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> lib: 1158 ok, 278 failed, 26 ignored (no test result: line; the binary aborts at exit 0x80004005 after 40 watchdog thread dumps); the other binaries: 138 passed, 2 failed, 20 ignored; SKIPPED: 1 (hle::d3d12x::bind::tests::two_pass_binds_set_the_shared_view_heap: no heap windows (opt-in: XCL_HEAP_WINDOWS=1)); failing: the same 278 lib tests fail on base (base: 1153 ok, 278 failed, 26 ignored, same abort; a lib-only rerun of each tree fails the identical 278 names, and across the two full-suite runs only the two tests in flight when the device went away differ), gpu_two_pass_cache_e2e::two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs fails on base too (child at line 472, parent at line 584), and gpu_compute_e2e loses its process to a device removal 0x887a0020 on base too. Cause: open #3694 (#3655 in main since d71578f; split-wave64 compute gives CreateComputePipelineState 0x887A0005 and removes the device on RDNA2). Targeted on the merge: hle::d3d12x::vram::tests 85 passed / 0 failed / 1 ignored, hle::d3d12x::placements::tests 13 passed / 0 failed.
review: whole diff read. Most serious: (1) no test for the fix. The PR says so; placed, settle, note_use and unmap_if_superseded are reached only from the D3D12 hooks on a live device, the tests in placements.rs call Registry methods directly, and nothing exercises the new lock() assertion, so a regression in this order still goes unnoticed. (2) Regression run is empty and Tested has only cargo check --target x86_64-pc-windows-gnu on this branch, so PR description is red and the PR is not mergeable yet. (3) The assertion is debug_assert!: in release builds a re-entry still deadlocks, so it is a diagnostic, not a fix for an unknown path. (4) By reading the fix is correct: the returned Vec<ID3D12Resource> keeps the completed unmaps alive, so no target address can be reused before their release cleanup runs; they are dropped after REGISTRY is let go; every REGISTRY lock now goes through lock(); remap_if_unmapped's cancel branch also drops a held unmap's resource under REGISTRY, but it returns early once the target was forgotten, so it cannot be the last reference there. Scope: one logical change, +79/−20 in 2 files.
performance: no cost expected — a Vec return, a Cell<bool> per lock, and REGISTRY held for a shorter time (the release work moved out of it). Measured on AMD, 40 s scripts/smoke.ps1 on 1c8b5514 (the last main before #3655, because current main cannot start the title): base 1023 frames / 17,904 MB, this PR 1030 frames / 17,925 MB (101 % frames, 100 % peak commit), 0 device removals, 0 crashes, presents per 2 s 61 57 60 vs 61 59 60. 40 s and not 60 s because this title dies in amdxc64.dll at ~45 s on this GPU (open #3544). Current main 5eaadb54 cannot be compared: it exits 0x887a0020 at 4.2 s on a 62 KB split-wave64 compute PSO (#3694).
assessment: nothing in this PR regresses the base — the 278 suite failures, the two integration failures and the title crash are identical on base 5eaadb54, and all come from open #3694. What blocks the PR is its own description and test: no test for the fix, and Regression run empty (PR description red).
hardware / tool and model: AMD Radeon RX 6700 XT 12 GB (RDNA2), driver 32.0.21045.29003; AMD Ryzen 5 5600X; Windows 10 build 19045 / OpenCode with DeepSeek V4.1 Flash 1017

Smoke blocks, if the author wants them for a regression run (base 1c8b5514, merge aec87505 = this head merged onto it):

xcl-smoke v1
commit: 1c8b551
gpu: AMD Radeon RX 6700 XT
title: 1778c2d9
seconds: 40
exit: 0x0
frames shown: 1023 (PresentX 1023)
presents per 2 s (last 3): 61 57 60
device removals: 0
crashes: 0
peak commit: 17904 MB

xcl-smoke v1
commit: aec87505f96097f4d88351ca53663175289a0e5b
gpu: AMD Radeon RX 6700 XT
title: 1778c2d9
seconds: 40
exit: 0x0
frames shown: 1030 (PresentX 1030)
presents per 2 s (last 3): 61 59 60
device removals: 0
crashes: 0
peak commit: 17925 MB

AI-written from a real run (OpenCode, DeepSeek V4.1 Flash 1017); posted only after the human checks each point.

@bmjask bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review of 15ae8026: 1 finding(s) (1 medium), most severe first.

  1. Medium crates/xcl/src/hle/d3d12x/vram.rs:2575: see below.

On lines outside the diff:

  1. Medium crates/xcl/src/hle/d3d12x/vram.rs:2575: Not certain about this one, because it depends on a queue-lease holder reaching REGISTRY: unmap_placed calls native_queue_signal, which waits for queue_submit's per-queue lease, while placements::unmap_if_superseded still holds REGISTRY, so here REGISTRY is taken before the queue lease. If thread B (drain_known -> unmap_if_superseded) holds REGISTRY and waits for queue Q's lease, while thread A holds Q's lease (inside ExecuteCommandLists or XCL setup work) and reaches REGISTRY (a last Release -> res_release -> forget_for_recycle -> placements::released -> settle -> lock(), or note_use), each waits on the other and the title hangs, the same class of hang this PR fixes. Returning the signals and issuing them after REGISTRY is dropped, as the PR now does for the references, would close it.

These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.

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

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:memory Virtual/physical memory, arenas, VRAM budget, UMA gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants