Skip to content

D3D12.X: run vram recycled-tile zero-fills outside PAGES.lock() to prevent AB-BA deadlock - #3593

Open
iggyvilla wants to merge 1 commit into
xcl-dev:amd-devfrom
iggyvilla:amd-fix-vram-zero-fill-deadlock
Open

iggyvilla wants to merge 1 commit into
xcl-dev:amd-devfrom
iggyvilla:amd-fix-vram-zero-fill-deadlock

Conversation

@iggyvilla

@iggyvilla iggyvilla commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Moves recycled-tile zero-fill D3D12 recording and submission (ZeroRing::new, CreatePlacedResource, fence::wait, kept_bufs.clear, ID3D12CommandAllocator::Reset, ID3D12GraphicsCommandList::Reset, ExecuteCommandLists) and PoolHeaps::take_heap's cached zero_buf COM release outside PAGES.lock(), breaking the live AB-BA deadlock observed between vram::before_execute → flush_zero_tiles → ID3D12CommandAllocator::Reset (holding PAGES, waiting on D3D12Core internal lock) and D3D12Core → res_release → forget_for_recycle → forget_pages (holding D3D12Core lock, waiting on PAGES).

TL;DR — Before vs After

Metric Before (origin/amd-dev 41aa60ce) After (c34d052a)
Recycled-tile zero-fill alloc.Reset / ExecuteCommandLists Inside PAGES.lock() (AB-BA deadlock with res_release → forget_pages) Outside PAGES.lock() via per-device ZeroFlusher (PAGES.try_lock() asserted before alloc.Reset)
Cached zero_buf COM release on heap reclaim Dropped inside PAGES.lock() Returned from take_heap and dropped outside PAGES.lock()
60 s smoke (41aa60ce vs c34d052a) 1,654 frames / 15,902 MB (0 crashes) 1,636 frames / 16,164 MB (0 crashes)

Scope note on remaining PAGES.lock() calls: This PR fixes the observed recycled-tile zero-fill and zero_buf drop deadlock path; native_queue_signal after flush, UpdateTileMappings/Signal in Pages::map/unmap_range, and CreateHeap in bind_range remain under PAGES.lock() and are intentionally left out of scope for this PR. Diff is +516/-142 in a single file (vram.rs: ~370 runtime lines replacing the inline flush path + ~145 test lines) because the ZeroFlusher extraction, before_execute wait/drain handoff, and take_heap unlocked drop must land together to avoid leaving a partial lock-order state.

What is in it

  • crates/xcl/src/hle/d3d12x/vram.rs:
    • Adds per-device ZeroFlusher (state: Mutex<Option<ZeroRing>>, active: AtomicUsize, failed: AtomicBool) so before_execute drains coalesced tile runs under PAGES.lock(), runs ZeroRing::new / CreatePlacedResource / fence::wait / kept_bufs.clear / alloc.Reset / list.Reset / ExecuteCommandLists unlocked in record_and_execute_zero_runs, and blocks heap reclaim while a flush is active.
    • Fixes PoolHeaps::take_heap to extract slots before zero_bufs (preventing early-return drop inside PAGES) and return (ID3D12Heap, Option<ID3D12Resource>) for unlocked release.
    • Removes the in-lock bind_range auto-flush (which also ran under placements::REGISTRY).
    • Adds before_execute_zero_fills_recycled_tiles_without_holding_pages_lock (asserts PAGES.try_lock().is_ok() directly inside record_and_execute_zero_runs immediately before slot.alloc.Reset() during a real before_execute call, plus GPU readback verifying zero-fill) and pool_heaps_take_heap_returns_zero_buffer_for_unlocked_drop.

Tested

  • Hardware: AMD Radeon RX 6650 XT 8 GB (RDNA2, driver 32.0.21045.29003), AMD Ryzen 7 5700X, Windows 11 x64, rustc 1.100.0 (0ef1bc386 2026-11-09), debug tests + release smoke.
  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4: 2,960 passed, 13 failed, 49 ignored. All 13 failures reproduce identically on clean origin/amd-dev 41aa60ce (2,954 passed, 17 failed, 49 ignored: 3 pre-existing RDNA2 f32/NaN tests, 6 parallel sparse/title_textures shared-global contention tests, test_title_runs_and_game_folder_is_untouched, and two_pass_driver_cache_e2e). SKIPPED (7): 4× f16_programs_on_the_gpu, 2× heap-window tests, 1× pso_measurement_compiles_an_exact_synthetic_record (Developer Mode off).
  • cargo test -p xcl --lib -- hle::d3d12x::vram::tests --test-threads=1: 75 passed, 0 failed, 1 ignored, 0 skipped (including a_recycled_page_is_zeroed_before_a_new_texture_uses_it, before_execute_zero_fills_recycled_tiles_without_holding_pages_lock, and pool_heaps_take_heap_returns_zero_buffer_for_unlocked_drop).

Regression run
Base (main, required by pr_check.py):

xcl-smoke v1
commit: d8bd9a38204667f3ca7dd9eeffcc2f1c937b07b0
gpu: AMD Radeon RX 6650 XT
title: 1778c2d9
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 15479 MB

Base (origin/amd-dev merge base):

xcl-smoke v1
commit: 41aa60ce842060a597b7e8724896f7bf6a031af5
gpu: AMD Radeon RX 6650 XT
title: 1778c2d9
seconds: 60
exit: 0x0
frames shown: 1654 (PresentX 1654)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 15902 MB

PR head (c34d052a):

xcl-smoke v1
commit: c34d052ab70d736d1771371b4626b0dac2f5d86b
gpu: AMD Radeon RX 6650 XT
title: 1778c2d9
seconds: 60
exit: 0x0
frames shown: 1636 (PresentX 1636)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16164 MB

Not tested

  • NVIDIA and Intel GPUs, RDNA3/RDNA4, and Wine.

AI use

  • Antigravity (Gemini 4 Neon High) implemented the take_heap early-return fix and deterministic PAGES.try_lock() check before alloc.Reset(), ran the workspace GPU test suite and 60 s smoke, and updated this description.

@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:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) labels Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Review (head 625dba5, diff read, not a merge-check):

  • The new tests do not exercise the deadlock. before_execute_zero_fills_recycled_tiles_without_holding_pages_lock (vram.rs ~6463-6474) holds flusher.state while no flush is running and checks that PAGES can be locked, which passes whatever the flush path does with PAGES; the before_execute call after it (~6476) is single-threaded and covers the same result as a_recycled_page_is_zeroed_before_a_new_texture_uses_it. Neither new test would fail if the flush still held PAGES across alloc.Reset().
  • D3D12 calls still run under PAGES. Only the zero-fill recording moves out of the lock: native_queue_signal still runs under PAGES in flush_zero_tiles_unlocked (~2085), Pages::map still calls UpdateTileMappings and Signal under it (~2429, ~2443), and bind_range still calls alloc_pool_heap (CreateHeap) under it (~2628). If the runtime lock held by the res_release → forget_pages thread in the description is also taken by any of these calls, the same AB-BA cycle remains; the description's "acyclic" lock order is not shown by the code.
  • Tested section. It reports only cargo test -p xcl --lib -- hle::d3d12x::vram::tests, not the whole-suite run CONTRIBUTING.md asks for, and names recycled_page_pool_tile_is_zeroed_before_next_texture_uses_it, a test that does not exist at this head (the existing one is a_recycled_page_is_zeroed_before_a_new_texture_uses_it, ~5006).

Fine: the active counter and the Wait step keep a queue from reserving its mapping wait while drained tiles still wait for their signal; heap reclaim is blocked while a flush runs; install_zero_buffer drops stale buffers when the heap changed; evicted zero_bufs are released outside PAGES.

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

@Edgaras0404

Copy link
Copy Markdown

merge-check claim
head: 625dba5

@github-actions github-actions Bot added needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Dec 2, 2026
@Edgaras0404

Copy link
Copy Markdown

merge-check
base: 41aa60c
head: 625dba5
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> incomplete: the -p xcl --lib binary aborted (exit 0x80004005 after D3D12 queue drain: Signal failed: The parameter is incorrect (0x80070057)) before printing its result; the full-lib rerun observed 1991 ok, 3 failed, 23 ignored, and the integration binaries printed 139 passed, 2 failed, 20 ignored; SKIPPED: 6 (4 f16 subcases: unsupported V_MUL_F16 / V_PK_MUL_F16; 2 heap-window opt-ins); failing: gpu::recomp::{f32_programs_on_the_gpu, gpu_f32_compares_with_nan, gpu_float_compares_random}, the lib abort, and gpu_two_pass_cache_e2e::two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs (assertion at that test's line 584) all reproduce on base 41aa60ce; one-off hle::d3d12x::list_x::tests::a_put_off_call_whose_close_fails_is_not_submitted in the first merge run did not reproduce (passes alone on both trees and in the full-lib rerun); targeted cargo test -p xcl --lib -- hle::d3d12x::vram::tests --test-threads=1 -> 75 passed, 0 failed, 1 ignored, both new tests pass
review: the new regression test does not exercise the deadlock it guards: before_execute_zero_fills_recycled_tiles_without_holding_pages_lock (vram.rs:6389) holds ZeroFlusher::state while no flush is running, checks that PAGES can still be locked, then calls before_execute single-threaded; it never has one thread hold PAGES while entering D3D12Core, so it would pass with the flush still holding PAGES across alloc.Reset(). The fix also leaves D3D12 calls under PAGES: native_queue_signal at vram.rs:2085 inside flush_zero_tiles_unlocked, UpdateTileMappings + Signal in Pages::map at 2429/2443 (and unmap_range at 2461/2474), and CreateHeap via alloc_pool_heap at 2628 in bind_range; the description's acyclic order is not shown for these, so the AB-BA path may remain. The description's Tested section reports only the targeted vram suite, not the whole-workspace run CONTRIBUTING.md asks for, and cites recycled_page_pool_tile_is_zeroed_before_next_texture_uses_it, which does not exist at this head (the existing test is a_recycled_page_is_zeroed_before_a_new_texture_uses_it, vram.rs:5006). Minor: take_heap (1746) can drop a cached zero buffer inside PAGES on its slots...take()? early return. Scope is +510/-142 in one file (over the ~400 target, under 800; the description explains it). The rest of the concurrency plumbing (active counter + Wait step, heap reclaim blocked during a flush, stale zero-buffer drops, evicted zero_bufs released outside PAGES) checked out. Not tested here: NVIDIA/Intel and RDNA3/RDNA4; the 60 s smoke below covers only this RX 6700 XT.
performance: measured on base 41aa60ce and the merge result 0b0100ab (clean merge of 625dba55), same title, 60 s each, with Enter every 5 s: base 1615 frames and 16,487 MB peak commit; merge 1618 frames (+0.2%) and 17,726 MB (1.08x), with 0 device removals and 0 crashes in both. Inside the 80%/125% bounds. A 120 s pair was not usable for a tight bound (base 2016-2687 frames / 22-31 GB, merge 2248-2751 / 20-35 GB) because the cutoff lands in different world-load phases. The default path adds one atomic load/branch per before_execute and serializes zero-fill flushes on ZeroFlusher::state; no cost was measured.
verdict: not yet: the regression test does not exercise the deadlock it guards, D3D12 calls remain under PAGES.lock() (native_queue_signal, UpdateTileMappings, CreateHeap), so the claimed acyclic order is unproven, and the description's Tested section has no whole-workspace suite run and cites a test that does not exist. The build and the 60 s smoke are clean, and every suite failure reproduces on the base.
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, DeepSeek V4.1 Flash

@github-actions github-actions Bot removed needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Dec 2, 2026
@iggyvilla
iggyvilla force-pushed the amd-fix-vram-zero-fill-deadlock branch from 625dba5 to c34d052 Compare December 2, 2026 06:15
@iggyvilla

Copy link
Copy Markdown
Collaborator Author

Updated to c34d052a addressing @Foniass's and @Edgaras0404's reviews:\n- Regression test: before_execute_zero_fills_recycled_tiles_without_holding_pages_lock now asserts PAGES.try_lock().is_ok() directly inside record_and_execute_zero_runs immediately before slot.alloc.Reset() during a real before_execute call (panics deterministically if PAGES is held across alloc.Reset()).\n- take_heap minor fix: extracts slots before zero_bufs so an early None return cannot drop a cached zero_buf inside PAGES.\n- Scope & Tested: narrowed the description's lock-order claim strictly to the recycled-tile zero-fill and zero_buf drop path (explicitly noting that native_queue_signal, UpdateTileMappings, and CreateHeap remain under PAGES), fixed the test name typo (a_recycled_page_is_zeroed_before_a_new_texture_uses_it), and added whole-workspace GPU test counts + 41aa60ce merge-base smoke.

@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Follow-up review (head c34d052, since 625dba5, diff read, not a merge-check):

  • The new tests do not exercise the deadlock: fixed. before_execute_zero_fills_recycled_tiles_without_holding_pages_lock now sets a test-only flag, and record_and_execute_zero_runs itself checks PAGES.try_lock() right before alloc.Reset() (vram.rs ~1982-1987); the test asserts the check ran once, so it fails if the flush holds PAGES across the reset.
  • The Tested section: fixed. It gives a whole-suite run (2,960 passed, 13 failed, all 13 also failing on amd-dev 41aa60ce) and names the tests that exist.
  • D3D12 calls still run under PAGES: still open. native_queue_signal in flush_zero_tiles_unlocked, UpdateTileMappings / Signal in Pages::map and CreateHeap in bind_range are still made with PAGES held, so the description's "acyclic" lock order is still only shown for the zero-fill reset.

New: none. take_heap now takes the zero buffer only once the slot is known to exist (~1747).

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

@sandivd

sandivd commented Dec 2, 2026

Copy link
Copy Markdown

merge-check claim
head: 625dba5

@sandivd

sandivd commented Dec 2, 2026

Copy link
Copy Markdown

merge-check
base: 41aa60c
head: 625dba5
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2963 passed, 8 failed, 49 ignored; SKIPPED: 24 (22 no-wave64 GPU subcases, 2 heap-window opt-ins); failing: gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu, gpu::recomp::tests::gpu_fract_f32_edges, hle::d3d12x::sparse::tests::private_physical_pages_reach_the_gpu_on_mapping, hle::d3d12x::title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels, and hle::d3d12x::title_textures::tests::{remapped_pages_are_compared_with_what_they_held, stream_recheck_keeps_the_whole_upload_baseline_over_partial_uploads, stream_recheck_partial_upload_without_an_entry_queues_a_whole_upload, stream_recheck_schedule_catches_late_writes_and_resets_milestones}; all eight fail on base 41aa60c too — the last four pass alone on both trees and fail in the full run only through the poisoned lock after the placed-texture test.
review: The specific #3069 cycle is broken: the zero-fill recording (own_work, allocator/list Reset, ExecuteCommandLists) now runs under ZeroFlusher::state with PAGES released, so the COPY_SCRATCH -> res_release -> PAGES thread no longer waits on a PAGES holder inside D3D12Core. The fix is not complete: flush_zero_tiles_unlocked still calls native_queue_signal under PAGES (vram.rs:2085), Pages::map/unmap still call UpdateTileMappings + Signal under PAGES (2429/2443, 2461/2474), and bind_range calls CreateHeap under it (2628); if any of those takes D3D12Core's internal lock, the same AB-BA pattern remains, so the description's acyclic order is not shown. The new test does not exercise the deadlock: before_execute_zero_fills_recycled_tiles_without_holding_pages_lock (6389) holds ZeroFlusher::state with no flush running and checks PAGES can be locked, which passes whatever the flush does; the before_execute call after it is single-threaded and the zeroing it checks already worked on base, so it would pass with the flush still under PAGES. CONTRIBUTING asks for a test that fails without the fix. The Tested section cites recycled_page_pool_tile_is_zeroed_before_next_texture_uses_it, which does not exist at this head (the existing test is a_recycled_page_is_zeroed_before_a_new_texture_uses_it, 5006), and reports only the vram subset (75 passed, 0 failed, 1 ignored, reproduced), not the whole workspace. I checked the reported take_heap early-return drop: it is not reachable in the current callers, so I did not count it. Scope +510/-142 in one file is over the ~400 target but under 800; the rest of the concurrency plumbing (active counter + Wait, no reclaim during a flush, stale/evicted zero_bufs dropped outside PAGES, ring fence_val updated under state) checked out. Not tested here: the retail world-load / XCL_DRED deadlock scenario itself.
performance: Not measured here (no game data). The author's and the first check's 60 s smokes on AMD RX 6650 XT / RX 6700 XT show +0.2-1.9% frames and 1.08-1.10x peak commit, inside the 80-125% bounds. By default the change adds one atomic load/branch per before_execute and serializes zero-fill flushes on a per-device mutex; no cost was measured.
verdict: not yet: the new test does not exercise the deadlock it guards, native_queue_signal (and Pages::map/unmap) still call D3D12Core under PAGES, so the claimed acyclic order is not shown, and the description cites a test that does not exist and reports no whole-workspace run. The specific #3069 flush -> list Reset -> COPY_SCRATCH cycle is broken, the merge is clean, the build passes, and every test failure reproduces on the base.
hardware / tool and model: NVIDIA GeForce RTX 3050 Ti Laptop GPU (driver 32.0.16.1656, default adapter; no wave64) + AMD Radeon(TM) Graphics (driver 31.0.21925.2001, adapter 0 for the placed-texture test); AMD Ryzen 7 5800H; Windows 11 Home Single Language 10.0.26300 / OpenCode, DeepSeek V4.1 Flash

@github-actions github-actions Bot added needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Dec 2, 2026
@Pipodi

Pipodi commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

merge-check claim withdrawn
head: c34d052

Withdrawn: we have no AMD hardware to check this amd-dev PR against. Sorry for the noise; the slot is free.

@thehollowedone

Copy link
Copy Markdown
Contributor

Source follow-up on current head c34d052a. I agree with the earlier merge-check finding that the new test now covers the narrow alloc.Reset() invariant, while the wider lock-order claim remains open.

The remaining cycle has a concrete lock edge. native_queue_signal takes queue_submit::with's lease before calling the PC queue's Signal. The revised zero-fill path calls it while holding PAGES in flush_zero_tiles_unlocked; Pages::map and unmap still signal under PAGES too. Meanwhile record_and_execute_zero_runs calls native_queue_execute on that same mapping queue after releasing PAGES, under the same submission lease. If that driver call releases a resource and reaches res_release → forget_for_recycle → PAGES, one thread can hold the lease waiting for PAGES while another holds PAGES waiting for the lease. The new test checks only the allocator reset and cannot exclude this schedule.

The follow-up needs to move the remaining mapping-queue operations outside PAGES while preserving map/zero-fill/signal order and the published fence value. A deterministic two-thread regression should pause one thread after taking the queue lease, start the other on a PAGES-guarded signal, and verify both complete after the first thread enters the resource-release callback. I traced this in source; I have not run that interleaving or a title smoke on this head. This is a review follow-up, not a second merge-check.

AI use: OpenAI Codex (GPT-6 Sol) traced the current lock and callback paths.

@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: 41aa60c
head: c34d052
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2963 passed, 9 failed, 49 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 amd-dev); failing: gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu, gpu::recomp::tests::gpu_fract_f32_edges, hle::d3d12x::sparse::tests::private_physical_pages_reach_the_gpu_on_mapping, hle::d3d12x::title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels, hle::d3d12x::title_textures::tests::remapped_pages_are_compared_with_what_they_held, hle::d3d12x::title_textures::tests::stream_recheck_keeps_the_whole_upload_baseline_over_partial_uploads, hle::d3d12x::title_textures::tests::stream_recheck_partial_upload_without_an_entry_queues_a_whole_upload, hle::d3d12x::title_textures::tests::stream_recheck_schedule_catches_late_writes_and_resets_milestones (the same tests fail on amd-dev 41aa60c alone on this card, so none is from this PR)
review: read by one reviewer at this same head; the review is at #3593 (comment) and its findings stand. Most serious: native_queue_signal, UpdateTileMappings and CreateHeap still run while PAGES is held, which the description now leaves out of scope on purpose.
performance: 60 s smoke of a retail title, amd-dev 41aa60c vs this merge (c34d052): 1671 -> 1670 frames shown (99 %; last presents per 2 s: 61 60 60), peak commit 16722 -> 16738 MB, 0 device removals, 0 crashes.
verdict: safe to merge: no blocking finding in the review (the points there are minor), the suite passes and the smoke run is clean
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.

@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) and removed needs-1st-check Passes gates; needs first independent merge-check (#3041) labels Dec 2, 2026

@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 c34d052a: 3 finding(s) (1 critical, 2 medium), most severe first.

  1. Critical crates/xcl/src/hle/d3d12x/vram.rs:2091: flush_zero_tiles_unlocked still calls native_queue_signal (ID3D12CommandQueue::Signal, which enters D3D12Core) while holding PAGES.lock(), so the AB-BA deadlock this PR targets can still happen on the recycled-tile zero-fill path. (inline)
  2. Medium crates/xcl/src/hle/d3d12x/vram.rs:2042: ActiveFlushGuard is created only after flusher.state.lock().unwrap() succeeds, so if the mutex is poisoned, the active increment made in before_execute is never undone. (inline)
  3. Medium crates/xcl/src/hle/d3d12x/vram.rs:2097: Not certain about this one: when the zero-fill has executed but the device's Pages entry is gone (removed or replaced between the unlocked ExecuteCommandLists and the final PAGES block, which the new test itself does), nothing is signall.... (inline)

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.

}
if executed {
pages.value += 1;
let _ = super::real::native_queue_signal(&pages.queue, &pages.fence, pages.value);

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.

Critical: flush_zero_tiles_unlocked still calls native_queue_signal (ID3D12CommandQueue::Signal, which enters D3D12Core) while holding PAGES.lock(), so the AB-BA deadlock this PR targets can still happen on the recycled-tile zero-fill path. Thread A finishes the unlocked ExecuteCommandLists, takes PAGES to install buffers and bump pages.value, then calls Signal and blocks on D3D12Core's internal lock. Thread B is inside D3D12Core holding that lock and calls res_release -> forget_for_recycle -> forget_pages -> PAGES.lock(), and both threads hang. It's the same lock pair, now through Signal instead of alloc.Reset. Signalling after PAGES is released, then recording the value under PAGES, would close it.

) {
// Declare `ring_guard` before `_active_guard` so `_active_guard` decrements `flusher.active`
// while `flusher.state` is still held; `ZeroFlushStep::Wait` waiters then see `active == 0`.
let mut ring_guard = flusher.state.lock().unwrap();

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.

Medium: ActiveFlushGuard is created only after flusher.state.lock().unwrap() succeeds, so if the mutex is poisoned, the active increment made in before_execute is never undone. After any earlier panic while state was held (e.g. a D3D12 wrapper panic), the next Drain panics at lock().unwrap() with active stuck at 1 or more. From then on every before_execute on that device takes the Wait branch, and its own flusher.state.lock().unwrap() panics on every title submit. Creating the guard before taking the lock (or recovering the poisoned guard) would avoid it.

None
}
} else {
stale_bufs.extend(created.into_iter().map(|(_, _, b)| b));

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.

Medium: Not certain about this one: when the zero-fill has executed but the device's Pages entry is gone (removed or replaced between the unlocked ExecuteCommandLists and the final PAGES block, which the new test itself does), nothing is signalled and ring.slots[slot_idx].fence_val isn't updated. The slot looks free while its allocator is still in use on the GPU, so the next flush on that ring calls alloc.Reset() on an executing allocator (a D3D12 error or device removal), and the ZeroFlusher Arc can drop the ring's allocator and list while their work is in flight. Signalling the ring's own fence regardless of whether Pages still exists would keep the slot honest.

@bmjask

bmjask commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.

@Hurtibabi

Hurtibabi commented Dec 2, 2026 •

Copy link
Copy Markdown

merge-check
base: 41aa60c
head: c34d052
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2963 passed, 8 failed, 49 ignored; SKIPPED: 28 (19 no wave64, 3 wave32 per-lane forms refused, 2 no d3d12SDKLayers.dll, 2 no heap windows, 1 no D3D12 debug layer, 1 cannot disable the driver cache without Developer Mode); failing: gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu, gpu::recomp::tests::gpu_fract_f32_edges, hle::d3d12x::sparse::tests::private_physical_pages_reach_the_gpu_on_mapping, and 5 hle::d3d12x::title_textures::tests (placed_title_textures_share_vram_and_preserve_pixels, remapped_pages_are_compared_with_what_they_held, stream_recheck_keeps_the_whole_upload_baseline_over_partial_uploads, stream_recheck_partial_upload_without_an_entry_queues_a_whole_upload, stream_recheck_schedule_catches_late_writes_and_resets_milestones). All 8 fail on the base too: the same 8 with cargo test -p xcl --lib -- <the 8> --exact --test-threads=4 -> 0 passed, 8 failed on amd-dev 41aa60c and on this head, same panic sites. (The merge is a fast-forward: the head sits on the current amd-dev.)
review: read the whole diff (vram.rs +516/-142). The described cycle is broken: ZeroRing::new, CreatePlacedResource, the ring-slot fence::wait, kept_bufs.clear, alloc.Reset / list.Reset and ExecuteCommandLists now run in record_and_execute_zero_runs with only ZeroFlusher::state held, PAGES is taken after it (no reverse order found: the Wait step takes state with PAGES released), heap reclaim waits until no flush is active, and the cached zero_buf is now released outside PAGES. The two new tests run and pass on this NVIDIA GPU (before_execute_zero_fills_recycled_tiles_without_holding_pages_lock, pool_heaps_take_heap_returns_zero_buffer_for_unlocked_drop). Findings, none blocking: (1) the new ZeroFlusher doc says PAGES is held only for "brief metadata updates", but flush_zero_tiles_unlocked still calls native_queue_signal (a queue Signal through queue_submit::with) under PAGES, as do Pages::map / unmap_range and bind_range's CreateHeap; the description leaves those out of scope, so the same class of AB-BA wait can remain there. (2) The new test checks that PAGES is free at alloc.Reset, not the two-thread deadlock itself. (3) bind_range no longer flushes at 256 queued tiles, so recycled tiles are zeroed only at the next before_execute. Size is over 400 lines, with the reason given in the description. No duplicate: #2759 adds the zero-fill on main and #2981 is a broad amd-dev PR on other tile-mapping races; neither makes this change, and #1607 has no group for it. Not tested by me: a retail title (no smoke run here), the deadlock itself, AMD and Intel.
performance: reasoned, not measured: the same GPU work as before (one batched zero-fill per submit), now without PAGES held across it, so other threads wait less on PAGES; one extra uncontended mutex per flush. No smoke run on this merge.
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 4090 (driver 621.40 / 32.0.16.2140), Intel Core i7-14700K, Windows 11 Pro 10.0.26200.9974, rustc 1.100.1, debug / Claude Code, Claude Opus 5.5 (claude-opus-5-5)

@github-actions github-actions Bot added ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) labels Dec 2, 2026
@github-actions github-actions Bot removed the needs:amd-test Needs a test report or merge-check on an AMD GPU label Dec 2, 2026
@throny throny added needs:amd-test Needs a test report or merge-check on an AMD GPU needs:title-run Runtime change: needs a merge-check with a real title run into the world (#3041) and removed ready-to-merge Has 2 independent safe merge-checks on current head (#3041) labels Dec 2, 2026
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:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) needs:amd-test Needs a test report or merge-check on an AMD GPU needs:title-run Runtime change: needs a merge-check with a real title run into the world (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants