Skip to content

D3D12.X: fail closed on heap retirement fences and sweep idle arenas without PAGES - #2538

Open
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-vram-retire-fail-closed-arenas
Open

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-vram-retire-fail-closed-arenas

Conversation

@Decodere

Copy link
Copy Markdown
Contributor

Part of #1773
Part of #1585
Part of #1833

What is in it

Fixes three VRAM heap retirement and idle arena reclamation bugs in crates/xcl/src/hle/d3d12x/vram.rs (without touching Arenas::take_for):

  • Fail closed on CreateFence/Signal failure in RetireQueue::retire_with: Previously, under VRAM/commit pressure, if make_fence or signal failed on all live queues (with unmap_mark == None), retire_with saw marks.is_empty() and returned heaps immediately to drop while live queues were still using them; if it failed on 1 of N live queues, that queue was omitted so sweep_with dropped the heaps as soon as the remaining queues finished. RetiringBatch::marks is now Option<Vec<(F, u64)>> (None = unarmed), RetireQueue::capture_marks returns None if ANY queue fails make_fence or signal (preserving monotonic *v += 1 on any already-signaled queue), and retire_with returns heaps immediately ONLY when queues.is_empty() && unmap_mark.is_none(). Added RetireQueue::retry_unarmed_with (invoked in retire_heaps_and_sweep before retire_with/sweep_with) so transiently-unarmed batches capture one shared set of fresh marks across all live queues and recover without head-of-line blocking armed ready batches.
  • Reject D3D12 u64::MAX device-removed sentinel in RetireQueue::sweep_with: Added pure helper fence_reached(completed, target) (completed != u64::MAX && completed >= target) and applied it to unmap_mark, b.marks, and dead-queue fence pruning so ID3D12Fence::GetCompletedValue() == u64::MAX on device removal never falsely reaps pending batches or prunes fences.
  • Sweep idle arenas from before_execute when PAGES has no active Some(Pages) entry for the device: Added per-device last_sweep: Vec<(usize, Instant)>, record_sweep, and sweep_dropped_for_if_due (1 s interval, pruning last_sweep when no arenas remain for dev) to Arenas<H>, plus static ARENA_COUNT: AtomicUsize synchronized to arenas.list.len() on every ARENAS list mutation (place, before_execute). When do_reclaim is false and ARENA_COUNT > 0 (e.g. XCL_VRAM_ARENAS=1 or devices with only non-tiled D32S8/MSAA arenas), before_execute runs sweep_dropped_for_if_due and retires reclaimed heaps through retire_heaps_and_sweep, keeping a zero-lock fast path when ARENA_COUNT == 0.
  • Tests (hle::d3d12x::vram::tests, pure CPU, 0 skips):
    • retire_queue_fails_closed_on_fence_or_signal_failure_and_recovers
    • retire_queue_rejects_device_removed_sentinel
    • arenas_sweep_dropped_for_if_due_reclaims_without_pages

Tested

  • Failing-first on unmodified origin/main (e1d84b7d9153f86a97d2ec5d209d196046fc0bea) with the RetireQueue tests:
    • retire_queue_fails_closed_on_fence_or_signal_failure_and_recovers panicked at vram.rs:1703: all-fail must hold heaps.
    • retire_queue_rejects_device_removed_sentinel panicked at vram.rs:1719: u64::MAX sentinel must neither reap nor prune.
  • Post-fix targeted release tests on NVIDIA GeForce RTX 4060 (0 compiler warnings in vram.rs):
    • cargo test --release -p xcl --lib hle::d3d12x::vram::tests -- --test-threads=1 --nocapture: 22 passed; 0 failed; 1 ignored (churn_update_tile_mappings_timing), 0 skipped (all 3 new pure-CPU tests plus gpu_retire_queue_holds_heap_until_blocked_queue_passes_mark, d32s8_targets_take_their_own_size_of_video_memory, and reservable_agrees_with_the_driver passed).
  • Post-fix targeted release tests on WARP (XCL_ADAPTER=warp):
    • cargo test --release -p xcl --lib hle::d3d12x::vram::tests -- --test-threads=1 --nocapture: 22 passed; 0 failed; 1 ignored, 0 skipped.
  • Full release workspace (cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture):
    • 37 test binaries: 1426 passed; 0 failed; 46 ignored.
    • 29 pre-existing runtime SKIPPED messages (21 no wave64 on wave32-only RTX 4060, 3 per-lane form: refused, 3 no D3D12 debug layer / no d3d12SDKLayers.dll, 1 cannot disable the driver cache, 1 no heap windows).

Regression run

Before (origin/main e1d84b7d9153f86a97d2ec5d209d196046fc0bea):

xcl-smoke v1
commit: e1d84b7d9153f86a97d2ec5d209d196046fc0bea
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1508 (PresentX 1508)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14016 MB

After (fix-vram-retire-fail-closed-arenas 73ff10e9ba8c4e7d45362bb8637536b52aba1686):

xcl-smoke v1
commit: 73ff10e9ba8c4e7d45362bb8637536b52aba1686
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1542 (PresentX 1542)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14007 MB

Both 60 s runs reach steady 60 60 60 presents per 2 s with 0 device removals, 0 crashes, 1542 vs 1508 frames shown (102.3%), and 14007 MB vs 14016 MB peak commit.

Not tested

  • Real D3D12 CreateFence OOM or DXGI_ERROR_DEVICE_REMOVED injection on hardware (both failure paths are exercised deterministically in the pure-CPU RetireQueue unit tests).
  • D3D12 debug-layer tests (d3d12SDKLayers.dll is not installed on this machine) and native wave64 GPU tests (RTX 4060 is wave32-only).

AI use

Written and verified with Google Antigravity (Gemini 4 Neon). All code changes, failing-first reproductions, targeted RTX 4060 and WARP test runs, the full release workspace suite, and both paired 60 s GPU smoke runs were executed and checked locally.

…without PAGES

Fix three VRAM heap retirement and idle arena reclamation bugs in vram.rs:

1. Fail closed in RetireQueue::retire_with when CreateFence or Signal fails on
   any live queue: change RetiringBatch::marks to Option<Vec<(F, u64)>>, factor
   RetireQueue::capture_marks (returning None on any queue failure while
   keeping fence values monotonic for already-signaled queues), and return
   heaps immediately only when queues.is_empty() && unmap_mark.is_none(). Add
   RetireQueue::retry_unarmed_with (invoked in retire_heaps_and_sweep before
   retire_with/sweep_with) so transiently-unarmed batches capture one shared
   set of fresh marks and recover without head-of-line blocking armed ready
   batches.
2. Reject the D3D12 u64::MAX device-removed fence sentinel via fence_reached
   in RetireQueue::sweep_with so GetCompletedValue() == u64::MAX neither reaps
   pending batches nor prunes dead-queue fences.
3. Sweep idle arenas from before_execute even when PAGES has no active entry
   for the device (e.g. XCL_VRAM_ARENAS=1 or devices with only non-tiled
   D32S8/MSAA arenas): track per-device last_sweep in Arenas<H> with
   sweep_dropped_for_if_due (1 s interval, pruning last_sweep when empty) and
   synchronize ARENA_COUNT to arenas.list.len() on every ARENAS list mutation
   so the zero-arena fast path avoids locking.

Add three pure-CPU unit tests in vram::tests covering fail-closed retirement
and recovery, u64::MAX sentinel rejection, and throttled idle arena sweeping.

Part of xcl-dev#1773
Part of xcl-dev#1585
Part of xcl-dev#1833

Assisted-by: Google Antigravity (Gemini 4 Neon)
@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

@emirduran707

Copy link
Copy Markdown
Contributor

Independent AMD RDNA4 (native wave64) test report on exact head 73ff10e9ba8c4e7d45362bb8637536b52aba1686 (clean detached checkout, no local changes).

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4, native wave64, driver 32.0.31044.16), Ryzen 5 5600, 32 GB RAM, Windows 11 Pro 26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2602.17; tests debug, smoke release.
  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4: 1424 passed, 2 failed, 46 ignored; 8 SKIPPED (6 in lower_dxil::tests::{f16,f32}_programs_on_the_gpu refused by per-lane form; heap_window_out_of_memory_falls_back_to_the_switch_form; pso_measurement_compiles_an_exact_synthetic_record). All native wave64 GPU tests ran and passed.
    • Both PR tests passed: hle::d3d12x::vram::tests::retire_queue_fails_closed_on_fence_or_signal_failure_and_recovers and retire_queue_rejects_device_removed_sentinel.
    • The 2 failures (gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1 seed 722 and fuzz_validate_and_gpu_3 seed 1599) are pre-existing on unmodified main on this GPU.

Regression run (60 s retail-title smoke, XCL_NETWORK=0, release)

xcl-smoke v1
commit: 73ff10e9ba8c4e7d45362bb8637536b52aba1686
gpu: AMD Radeon RX 9060 XT
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1606 (PresentX 1606)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13931 MB

(Matched main 68a284a baseline on the same machine today: 1573 frames, 14420 MB peak, 0 device removals, 0 crashes.)

Not tested

  • D3D12 debug layer; Intel GPUs.

AI use

  • Antigravity with Gemini 4 Neon ran the build, test suite, and smoke run and drafted this report.

@KOLESNiii KOLESNiii mentioned this pull request Nov 25, 2026
@SlopTart

Copy link
Copy Markdown

Tested: current PR head merged onto current origin/main (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target x86_64-pc-windows-gnu run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29).

gpu_ suite (cold cache): 165 passed / 3 failed, no crashes. The three are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, promoted-UAV-clear driver gap, PR #2490); none caused by this PR.
Targeted run of retire_queue_fails_closed_on_fence_or_signal_failure_and_recovers and retire_queue_rejects_device_removed_sentinel: 2 passed. The sentinel test is a failure-path unit test; it does not exercise a real device-lost event on this lane.

Not tested: retail titles (no title data on this lane, counts only).

AI use: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…upstream xcl-dev#2538)

(cherry picked from commit b2502155fba95a85f6879303f998af07f2714e09)
@andrewDoing

Copy link
Copy Markdown

I tested the no-PAGES idle-arena path on RTX 5070 / NVIDIA 616.92 using the same test-only fixture against parent e1d84b7d9153f86a97d2ec5d209d196046fc0bea and head 73ff10e9ba8c4e7d45362bb8637536b52aba1686, with isolated build targets.

The fixture places and releases a 64×64 RGBA8 render target in a 65,536-byte arena, verifies there is no active PAGES entry, waits 2.2 seconds, and drives before_execute. A native queue is held on a CPU-signaled fence while the test tracks the exact heap in the active arena and retirement queue.

  • Parent: the heap remained in the active arena both while blocked and after the fence completed; the candidate expectation failed.
  • PR head: the heap moved to retirement while the queue was blocked and was absent from both tracked owners after fence completion and another before_execute call. One focused test passed, with no skips.

This tests XCL heap ownership in a synthetic single-queue case. It does not measure driver residency, actual title VRAM savings, frame time, or behavior under physical memory pressure.

AI use: OpenAI Codex with GPT-6.2 Astra built and ran the fixture; Codex reviewed and prepared this report.

@ClosetPie107

Copy link
Copy Markdown

Independent test report under Wine/Proton (not native Windows) for head 73ff10e9.

Setup

  • AMD Radeon RX 9070 (RDNA4, RADV GFX1201, Mesa 26.3.1), Ryzen 5 9600X, CachyOS Linux 7.3.5
  • Proton Experimental experimental-11.0-20261112, vkd3d-proton master ec207194 with a local patch dropping the lpVtbl checks in d3d12_command_allocator_from_iface / d3d12_command_list_from_iface, DXC 1.9.2609.5, Rust 1.96.0, x86_64-pc-windows-gnu
  • Head merged onto main 83b00818, test commit 1fcf2e6e

Tests (debug, XCL_REQUIRE_GPU=wave64, --test-threads=1)

  • hle::d3d12x::vram::: 22 passed, 0 failed, 1 ignored (main: 19 passed, 0 failed, 1 ignored)
  • cargo test -p xcl --lib -- --test-threads=4 --skip compositor_blends_ui_plane_over_scene_plane: 1323 passed, 8 failed, 27 ignored (main the same run: 1319 passed, 9 failed, 27 ignored). The failures are all among main's. Without the --skip, the lib binary lost the device in that test (RADV The CS has been rejected), the test-order problem main has (see my report on D3D12.X: patch assert-checked vtables in place under Wine (#175) #2477).

Retail title, 300 s (release, from a copy of the same warm-cache snapshot; XCL_DIRECT_DXIL=1 XCL_PREPARE=1 XCL_PSO_PRELOAD=1 XCL_PSO_WAIT_MS=50 XCL_TEXTURE_LEVEL_PAGES=1 XCL_COHERENT_MB=1024 VKD3D_CONFIG=ignore_shared_fence, A pressed every 10 s)

  • 1 run: exit 0, no crash, no device removal, 1,982 presents, 10,441 MiB of video memory at 90 s, the card full (16.2 GiB) later on as on main. main, 4 runs: all clean, 1,892–2,122 presents, 10,148–10,265 MiB at 90 s.

Not tested

  • Native Windows, NVIDIA/Intel
  • The failure paths themselves (no fence or signal failure and no device removal happened in the run)

AI use

  • Claude Code (Claude Opus 5.5) wrote the test scripts, ran the tests and runs on this machine and wrote this report.

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.

6 participants