From cae11a796aa424700977d6d5b28082fc5ebe172e Mon Sep 17 00:00:00 2001 From: Kalmarv <59708937+Kalmarv@users.noreply.github.com> Date: Thu, 26 Nov 2026 19:15:49 -0500 Subject: [PATCH] D3D12.X: segments parked as spares stop holding their PC allocator A title list's extra segments (mid-list splits) are parked as spares at the list's next Reset, and the segments KickoffX takes back become spares too. A spare records again only after its own later Reset, but alloc_pool still counted it as a user of the PC allocator it last recorded on, so that allocator was never free: each later Reset of the title's allocator while those were "in use" put another PC allocator aside, and none was ever reused or released. The previous recording a stand-in replaces at Reset pinned its allocator the same way. alloc_pool::park_list drops the list from its allocator's users (its submit marks stay, so the allocator is reused only once the GPU is past them). title_list::reset parks the moved segments and the replaced recording; KickoffX parks its reclaimed segments after both submits. Measured on the title (RTX 5070 Ti, Series X profile, 240 s untimed runs, object counts from 100 to 240 s): PC allocators 1,137 -> 1,928 without, 728-753 -> 862-897 with; non-local video memory 6.2 -> 9.6 GB without, 5.0 -> 5.4-5.5 GB with (about 4 GB less at 240 s). Timed pairs showed no frame-rate cost (late window 11.99 / 10.76 fps without, 10.76 / 11.76 with; run-to-run spread ~10 %). Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/alloc_pool.rs | 31 +++++++++++++++++++++++++ crates/xcl/src/hle/d3d12x/list_x.rs | 3 +++ crates/xcl/src/hle/d3d12x/title_list.rs | 7 ++++++ 3 files changed, 41 insertions(+) diff --git a/crates/xcl/src/hle/d3d12x/alloc_pool.rs b/crates/xcl/src/hle/d3d12x/alloc_pool.rs index 8b81b85913..37ee46c5c6 100644 --- a/crates/xcl/src/hle/d3d12x/alloc_pool.rs +++ b/crates/xcl/src/hle/d3d12x/alloc_pool.rs @@ -98,6 +98,19 @@ pub(super) fn forget_list(list: usize) { } } +/// `list` (a closed segment) is parked as a spare: its recording is over and it is reset before +/// it records again, so it no longer holds its allocator. Its last submit stays (`in_flight`) +/// and the allocator's own submit marks still keep it until the GPU is past them. +pub(super) fn park_list(list: usize) { + let mut g = STATE.lock().unwrap(); + let Some(s) = g.as_mut() else { return }; + if let Some(backing) = s.of_list.remove(&list) { + if let Some(b) = s.backings.get_mut(&backing) { + b.users.remove(&list); + } + } +} + /// Reset `list` on the title's allocator `alloc`, committing its new recording association /// only after native Reset succeeds. No pool lock is held across the driver call. pub fn reset_list(list: usize, alloc: usize, ty: i32, reset: impl FnOnce(usize) -> HRESULT) -> HRESULT { @@ -331,6 +344,24 @@ unsafe fn title_reset_in(s: &mut State, alloc: *mut c_void, mut reset: impl FnMu mod tests { use super::*; + /// A parked spare stops holding its allocator: once nothing else records on it and no submit + /// is pending, the allocator is free again. + #[test] + fn a_parked_spare_no_longer_holds_its_allocator() { + let mut ids = Box::new([0u8; 2]); + let alloc = ids.as_mut_ptr() as usize; + let spare = alloc + 1; + created_on(spare, alloc, D3D12_COMMAND_LIST_TYPE_DIRECT.0); + let held = |l: usize| STATE.lock().unwrap().as_ref().unwrap().backings[&alloc].users.contains(&l); + assert!(held(spare), "the new list records on the allocator"); + park_list(spare); + let g = STATE.lock().unwrap(); + let s = g.as_ref().unwrap(); + assert!(!s.of_list.contains_key(&spare) && unsafe { s.backings[&alloc].free() }, "a parked spare must not pin the allocator"); + drop(g); + ids[0] = 0; + } + #[test] fn a_spare_whose_reset_fails_stays_available_for_retry() { let (alloc, spare, list) = (0x1000usize, 0x2000usize, 0x3000usize); diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index ee544dec22..0ae0f8c803 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -3377,6 +3377,9 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { for seg in reclaimed { super::bind::reset(seg as *mut c_void, 0, 0); clear_segment_state(seg); + // a spare now: it does not hold its allocator until it records again (after both + // submits above, so the allocator keeps their marks) + super::alloc_pool::park_list(seg); } if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&(primary as usize))) { s.allocator = alloc; diff --git a/crates/xcl/src/hle/d3d12x/title_list.rs b/crates/xcl/src/hle/d3d12x/title_list.rs index a41e5643fc..1cd53cb486 100644 --- a/crates/xcl/src/hle/d3d12x/title_list.rs +++ b/crates/xcl/src/hle/d3d12x/title_list.rs @@ -194,6 +194,13 @@ pub(super) unsafe extern "system" fn reset(this: *mut c_void, alloc: usize, pso: for seg in moved { super::bind::reset(seg as *mut c_void, 0, 0); super::list_x::clear_segment_state(seg); + // a spare records again only after its next Reset: it does not hold its + // allocator meanwhile + super::alloc_pool::park_list(seg); + } + // nor does the previous recording a stand-in now replaces: it is not reset here either + if stand_in.is_some() { + super::alloc_pool::park_list(primary as usize); } } *((this as usize + F_RING_AT) as *mut usize) = 0;