Skip to content

D3D12.X: detach retired segments and reclaim idle aside allocators - #2707

Open
Saksukella wants to merge 4 commits into
xcl-dev:mainfrom
Saksukella:fix/alloc-pool-retire-reclaim
Open

Saksukella wants to merge 4 commits into
xcl-dev:mainfrom
Saksukella:fix/alloc-pool-retire-reclaim

Conversation

@Saksukella

@Saksukella Saksukella commented Nov 26, 2026 •

Copy link
Copy Markdown

What is in it

  • crates/xcl/src/hle/d3d12x/alloc_pool.rs:
    • Add retire_recording(list) to detach a retired split segment or superseded primary list from its old Backing.users while preserving Backing.marks and last_submit (so GPU completion fences and in_flight(list) remain fully enforced).
    • Track Backing.already_reset and reclaim idle free() put-aside backings on title_reset so their held UMA arena blocks and scratch buffers return promptly instead of remaining pinned until a future busy swap on that exact title allocator.
    • Release STATE before calling reset_pc_allocator / CreateCommandAllocator so COM callbacks during allocator reset or resource teardown (such as dropping a pooled prelude ID3D12CommandAllocator -> real::alloc_release -> alloc_pool::forget) cannot self-deadlock on STATE, and release pending UMA blocks per free backing on forget.
    • Guard against concurrent title_reset / reset_list races while STATE is unlocked: track Backing.resetting (Backing::free() requires !self.resetting) with a Condvar (RESET_DONE), reserve the backing in reset_list before invoking native Reset (rolling back on hr < 0), and wait cleanly if a backing is mid-reset.
    • Add unit tests retired_segment_releases_backing_after_gpu_completes_while_preserving_in_flight, clean_title_reset_reclaims_idle_aside_backings_once, stalled_primary_stand_in_reset_keeps_uma_blocks_until_native_backing_resets, reset_list_blocks_concurrent_title_reset_and_rolls_back_on_failure, and reset_list_waits_while_backing_is_resetting.
  • crates/xcl/src/hle/d3d12x/bind.rs & crates/xcl/src/hle/d3d12x/real.rs:
    • Key bind::State.allocator (and therefore console_list::PENDING UMA blocks) by the native PC allocator backing (alloc_pool::recording_backing / alloc_pool::current) rather than the logical title allocator, and move console_list::allocator_reset(raw) into real::reset_pc_allocator(raw) so swapping away from an in-flight primary backing never frees its UMA blocks while the GPU can still read them.
  • crates/xcl/src/hle/d3d12x/title_list.rs & crates/xcl/src/hle/d3d12x/list_x.rs:
    • Call alloc_pool::retire_recording when title_list::reset moves old split/stand-in segments into spares (and for primary when replaced by a stand_in, retiring its UMA blocks via bind::reset(primary, 0, 0)), in put_spare, and for KickoffX-reclaimed segments in list_x.rs after alloc_pool::executed marks them.

Tested

  • Hardware: AMD Radeon RX 9070 XT (driver 32.0.32029.2010), 24-thread x86_64 CPU, 32 GB RAM, Windows; rustc 1.100.0 (0ef1bc386 2026-11-09); debug (tests) and release (xcl.exe).
  • cargo test -p xcl --lib hle::d3d12x::alloc_pool::tests -- --test-threads=1: 9 passed, 0 failed (0.11s, including stalled_primary_stand_in_reset_keeps_uma_blocks_until_native_backing_resets, reset_list_blocks_concurrent_title_reset_and_rolls_back_on_failure, and reset_list_waits_while_backing_is_resetting).
  • cargo test -p xcl --lib hle::d3d12x::bind::tests -- --test-threads=1: 8 passed, 0 failed (0.38s).
  • cargo test -p xcl --lib hle::d3d12x::title_list::tests -- --test-threads=1: 3 passed, 0 failed (0.01s).
  • Also live-tested in 180s 3D workload on amd-dev (b5c0275f): previously coherent UMA exhausted 4095/4095 MB (547 blocks held for 236 allocators at 63.7k draws / 34.6k dispatches), triggering unified memory is full and 0x887a0006; with this fix held blocks dropped by >72% (152 blocks / 75 allocators, 4046/4095 MB at 65.5k draws / 47.0k dispatches) with 0 unified memory is full warnings.

Regression run

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: AMD Radeon RX 9070 XT
title: b8db18c2
seconds: 60
exit: 0x0
frames shown: 1624 (PresentX 1624)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19700 MB
xcl-smoke v1
commit: dff6edc7e64c818a29fe20946911b8c0e5578387
gpu: AMD Radeon RX 9070 XT
title: b8db18c2
seconds: 60
exit: 0x0
frames shown: 1669 (PresentX 1669)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19209 MB

Not tested

AI use

  • Antigravity (Google Gemini): assisted with analyzing the coherent UMA allocator-block leak, keying pending UMA blocks by native allocator backing, adding the resetting/Condvar concurrency guard, writing unit tests, and running local tests and smoke verification. All diffs and measurements were reviewed and verified locally on hardware.

@fromthemoonx

fromthemoonx commented Nov 26, 2026 •

Copy link
Copy Markdown

Rerun on the new head f796b9e (pending UMA blocks keyed by the native backing): still passes on NVIDIA, and the new test is stable.

result
cargo test --workspace --no-fail-fast -- --test-threads=4, f796b9e merged onto main b49be18 (clean) 1438 passed, 0 failed, 47 ignored; 25 SKIPPED lines
the 3 new tests (now with stalled_primary_stand_in_reset_keeps_uma_blocks_until_native_backing_resets), alone, 40 runs each 40 of 40 each
alloc_pool:: + title_list:: + bind:: + console_list:: + real::dispatch_chain:: (27 tests), --test-threads=8, 30 runs 30 of 30
hle::d3d12x::list_x::tests::, --test-threads=4, 100 runs 6 failed runs, all a_dispatch_with_a_pipeline_still_building_waits_at_the_submit, the main flake
e2e debug-layer test the same messages as main (3× id 93, 1× id 1046, 1× warning id 820, 8× id 0)

First run, on 7879954: NVIDIA run: passed; both new tests stable, list_x no different from main in parallel, debug-layer messages as on main.

Tested: 7879954 merged onto main b49be18 (clean). RTX 3060 12 GB (driver 610.88), Ryzen 7 5700X, Windows 11 Pro 26300, Rust 1.100.0 MSVC, debug, Graphics Tools (D3D12 debug layer) installed.

result
cargo test --workspace --no-fail-fast -- --test-threads=4 1437 passed, 0 failed, 47 ignored; 25 SKIPPED lines
retired_segment_releases_backing_after_gpu_completes_while_preserving_in_flight, clean_title_reset_reclaims_idle_aside_backings_once, alone, 40 runs each 40 of 40 each
hle::d3d12x::alloc_pool:: + title_list:: + real::dispatch_chain:: (11 tests), --test-threads=8, 30 runs 30 of 30
hle::d3d12x::list_x::tests::, --test-threads=4, 100 runs 7 failed runs, only in a_dispatch_with_a_pipeline_still_building_waits_at_the_submit (7×) and rotating_queue_self_deadlock_… (3×), the two that flake on main the same way (#2118, #2547)
e2e test_title_draws_with_a_root_signature_under_the_debug_layer, merged vs main the same: 3× error id 93, 1× error id 1046, 1× warning id 820, 8× id 0 at exit

Not tested: a title run, Intel, a release build.

AI use: Claude Code (Claude Opus 5.5) ran the suite, the repeats and the debug-layer comparison, and wrote this report.

@luckyteagirl luckyteagirl 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.

At head 7879954925faaa048293f28857febf9eb8aa503b, the new bind::reset(primary, 0, 0) runs precisely when title_list::reset selects a stand-in because the primary PC list is still in flight (title_list.rs:157-201). bind::reset moves that primary recording's UMA blocks to console_list::PENDING under its old logical title allocator (bind.rs:75-87). A subsequent successful title-allocator Reset can swap to a different native backing while the old primary is still in flight, but real::alloc_reset unconditionally calls console_list::allocator_reset(this) on success (real.rs:7047-7057), which frees all pending blocks under that logical allocator. The GPU can therefore still read blocks freed by the new stand-in path. Before this PR, the stand-in branch did not transfer the primary's bind blocks at that point. Could those blocks stay owned by the old primary/native backing until its fence completes, with the pending-key/reset path tracking that backing rather than the logical allocator? A regression with a GPU-stalled primary, stand-in list reset, and title-allocator reset would distinguish the two lifetimes.

This is an exact-head source review; I did not run a GPU-stalled title reproduction. AI use: OpenAI Codex (GPT-6.2 Sol) traced the interleaving; I checked the bind, pool, and allocator-reset paths before posting.

@sage-sage

Copy link
Copy Markdown

Test report: passes on NVIDIA. Exact head 78799549 (its base is current main b49be18, so head = merged).

Tested

  • RTX 4080 SUPER (driver 621.40), Ryzen 5 7600X, Windows 11 26H2 build 26300.10075, rustc 1.100.0 MSVC, DXC 1.9.2602.17 (Windows SDK 10.0.26100), debug build
  • cargo test --workspace -- --test-threads=4: 1437 passed, 0 failed, 47 ignored; the 2 new tests pass
  • SKIPPED: 29 lines, the same as on main on this machine (wave64, debug layer, Developer Mode, heap windows)

Not tested

  • No game, so not split segments and allocator reuse on a title.

Claude Code (Claude Opus 5.5) ran the tests and wrote this report.

@Saksukella

Copy link
Copy Markdown
Author

Good catch @luckyteagirl — fixed in f796b9e6. bind::State.allocator (and thus console_list::PENDING) is now keyed by the native PC allocator backing (alloc_pool::recording_backing / alloc_pool::current) rather than the logical title allocator, and console_list::allocator_reset now runs inside real::reset_pc_allocator (and for free backings in alloc_pool::forget) rather than unconditionally on alloc_reset(this). Added the regression test stalled_primary_stand_in_reset_keeps_uma_blocks_until_native_backing_resets covering a GPU-stalled primary, stand-in reset, title-allocator swap reset, and post-fence reclaim. Re-ran alloc_pool (7/7), bind (8/8), title_list (3/3), and a 60s RX 9070 XT smoke run (1669 frames, 0 removals, 0 crashes).

@Kalmarv

Kalmarv commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

A gap in retire_recording's call sites, found while reading this PR next to #2404 (a code read, not a run). At 068671d:

  • A list split mid-recording that the title submits with KickoffX goes through title_list::reclaim_for_kickoff (list_x.rs:3330). That moves the reclaimed segments, the active one included, into the wrapper's spares (title_list.rs:344) without retire_recording.
  • They are submitted just after: prior in the loop that follows, and the active segment at alloc_pool::executed(q_raw, &one) (list_x.rs:3355). After that they stay in their old backing's users, the way the Reset path's segments did before this PR. So that PC allocator stays "aside with a list on it" until the spare is reset elsewhere.
  • Here the detach has to come after executed() marks them. Detached before it, the submit would leave their backing unmarked, and the backing could be reset while the GPU runs it.

Suggested fix: call super::alloc_pool::retire_recording(seg); in the for seg in reclaimed loop at list_x.rs:3377-3380 (next to bind::reset and clear_segment_state), which runs after line 3355.

AI use: Claude Code with Claude Opus 5.5 read the code and wrote this note.

@Saksukella

Copy link
Copy Markdown
Author

Good catch @Kalmarv — added super::alloc_pool::retire_recording(seg); in the KickoffX reclaimed-segments loop at list_x.rs (after alloc_pool::executed marks them) in dff6edc7 (along with dropping an orphaned ResetCurrent backing if forget raced). Re-ran alloc_pool::tests (9/9) and 60s RX 9070 XT smoke (1669 frames, 0 removals, 0 crashes).

@thehollowedone

Copy link
Copy Markdown

Exact-head allocator lifetime check on dff6edc7 (including the new KickoffX change). I found two paths the current nine allocator tests do not cover. Both fail with the test-only witnesses below; the existing nine pass.

  1. The previous backing can be reset while a list's native Reset is still pending. try_reserve_backing moves a list from old backing A to current backing B before calling native Reset(B). If a concurrent title allocator reset reclaims the now apparently free A, and Reset(B) fails, rollback_backing restores the list→A mapping after A's commands have been discarded. The witness starts with a closed recording on A, resets toward B, reclaims A inside the reset callback, returns E_FAIL, and observes A was reset. The existing rollback test starts with no previous recording. Reservation · Reset/rollback. Microsoft says an allocator reset reuses the memory behind recorded commands and must wait for GPU work on that allocator: allocator Reset contract.
  2. Final release can discard pending UMA ownership for a busy original backing. After A is put aside for B, forget(A) visits [A, B, A]. The first A visit removes its busy Backing; the second sees no record, treats A as free, and calls console_list::allocator_reset(A). The witness leaves A held by a live recording and confirms its pending UMA entry has disappeared. With actual UMA blocks, that reset path calls free_uma. Release loop · UMA release.

For the first path, reserve B while keeping A's existing user until native Reset succeeds; then move the association. On failure, release only the temporary B reservation. For the second, deduplicate backing IDs before inspecting/removing them. Deduplication alone prevents the early drain but does not solve eventual cleanup: a busy backing's pending UMA and fence/list ownership must survive final title-allocator release until safe reclamation, including allocator-address reuse.

Tested: Windows 11 build 26300, Ryzen 7 9800X3D, RTX 3080 Ti / 621.35, rustc 1.99.1 MSVC, debug. On this head with only the two test functions added:

  • cargo test --offline --locked -p xcl --lib reset_list_preserves_previous_backing_until_native_success -- --nocapture --test-threads=1: 0 passed, 1 failed; previous A was reset before the native callback returned.
  • cargo test --offline --locked -p xcl --lib forget_does_not_release_a_busy_original_backing_twice -- --nocapture --test-threads=1: 0 passed, 1 failed; pending entry was empty.
  • Existing hle::d3d12x::alloc_pool::tests with those two names skipped: 9 passed, 0 failed.
Test-only diff against dff6edc
diff --git a/crates/xcl/src/hle/d3d12x/alloc_pool.rs b/crates/xcl/src/hle/d3d12x/alloc_pool.rs
index df8a39e..f558306 100644
--- a/crates/xcl/src/hle/d3d12x/alloc_pool.rs
+++ b/crates/xcl/src/hle/d3d12x/alloc_pool.rs
@@ -1092,6 +1092,66 @@ mod tests {
         unsafe { forget(alloc) };
     }
 
+    #[test]
+    fn reset_list_preserves_previous_backing_until_native_success() {
+        let mut ids = Box::new([0u8; 5]);
+        let base = ids.as_mut_ptr() as usize;
+        let (alloc, old, current, spare, list) = (base, base + 1, base + 2, base + 3, base + 4);
+        {
+            let mut g = STATE.lock().unwrap();
+            let s = g.get_or_insert_with(State::default);
+            s.types.insert(alloc, D3D12_COMMAND_LIST_TYPE_DIRECT.0);
+            s.cur.insert(alloc, current);
+            s.aside.insert(alloc, vec![old, spare]);
+            s.backings.insert(spare, Backing { already_reset: true, ..Backing::default() });
+            move_list(s, list, old);
+        }
+
+        let mut native_resets = Vec::new();
+        let hr = reset_list(list, alloc, D3D12_COMMAND_LIST_TYPE_DIRECT.0, |backing| {
+            assert_eq!(backing, current);
+            let mut g = STATE.lock().unwrap();
+            let s = g.as_mut().unwrap();
+            assert_eq!(
+                unsafe { title_reset_in(s, alloc as *mut c_void, |raw| {
+                    native_resets.push(raw as usize);
+                    0
+                }) },
+                Some(0)
+            );
+            crate::util::E_FAIL
+        });
+
+        assert_eq!(hr, crate::util::E_FAIL);
+        assert_eq!(recording_backing(list), Some(old));
+        assert!(!native_resets.contains(&old), "the previous recording was reset before native Reset succeeded");
+    }
+
+    #[test]
+    fn forget_does_not_release_a_busy_original_backing_twice() {
+        let mut ids = Box::new([0u8; 3]);
+        let base = ids.as_mut_ptr() as usize;
+        let (alloc, next, list) = (base, base + 1, base + 2);
+        {
+            let mut g = STATE.lock().unwrap();
+            let s = g.get_or_insert_with(State::default);
+            s.types.insert(alloc, D3D12_COMMAND_LIST_TYPE_DIRECT.0);
+            s.aside.insert(alloc, vec![alloc]);
+            s.cur.insert(alloc, next);
+            move_list(s, list, alloc);
+            s.backings.entry(next).or_default().users.insert(list + 1);
+        }
+        super::super::console_list::hold_until_reset(alloc, vec![0x1111]);
+
+        unsafe { forget(alloc) };
+
+        assert_eq!(
+            super::super::console_list::take_pending_for_test(alloc),
+            vec![0x1111],
+            "a live old recording must keep its pending UMA blocks"
+        );
+    }
+
     #[test]
     fn reset_list_waits_while_backing_is_resetting() {
         let mut ids = Box::new([0u8; 2]);

These are synthetic state tests; they do not reproduce a retail crash or measure GPU memory pressure. I did not run the full workspace suite or paired smoke on this head. No game files or diagnostics were used.

AI use: Codex (GPT-6.2 Sol) inspected the paths, added and ran the witnesses, and drafted this comment; Codex (GPT-6.2 Astra) independently reviewed the allocator lifetimes.

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