From 73a197aa1813b8b813e9bcd8a1fc786b8715ef14 Mon Sep 17 00:00:00 2001 From: ArtuArtaza <71021164+ArtuArtaza@users.noreply.github.com> Date: Tue, 1 Dec 2026 15:52:02 -0300 Subject: [PATCH] D3D12.X: an idle allocator's pool state goes before its last Release alloc_release dropped what XCL keeps by a command allocator's address (the pool's namespace and type, the dispatch chain's and list_x's state) after the Release that frees the object. In between, the driver can give that address to an allocator another thread creates, and the cleanup then ran on the new one: alloc_pool::forget took its type and stand-ins, and marked one that already held a recording for release by the pool. For an allocator nothing uses, the cleanup now runs while the hook still holds its probe reference, before the Release that frees it. One the pool keeps for the GPU, and a Release that did not see the last reference, are cleaned up after it as before. sage-sage's #2701 proposed this order for heaps, queues and allocators; the allocator part is not on main. Measured on main b2b1f109 (RX 6700 XT, driver 32.0.21045.29003) with a GPU test that creates allocators between the Release and the cleanup until one comes back at the freed address: it failed 5 of 5 runs there and passed 5 of 5 with this order. That test needs a hook in alloc_release and is not part of this commit. Assisted-by: Claude Code (Claude Fable 5.2) --- crates/xcl/src/hle/d3d12x/alloc_pool.rs | 40 ++++++++++++++++++++++++- crates/xcl/src/hle/d3d12x/real.rs | 19 +++++++++--- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/alloc_pool.rs b/crates/xcl/src/hle/d3d12x/alloc_pool.rs index bbb56bdc3f..56c33288bf 100644 --- a/crates/xcl/src/hle/d3d12x/alloc_pool.rs +++ b/crates/xcl/src/hle/d3d12x/alloc_pool.rs @@ -250,14 +250,24 @@ pub(super) fn forget_queue(id: super::present::QueueId) { drop(gone); } +/// Release through a temporary reference. When the caller's was the last one, a busy allocator's +/// probe reference is handed to the pool (`retain_probe`), and an idle one's books are dropped +/// (`before_last`) before the last Release: the address is still this object's then. pub(super) fn release_with_probe( add_ref: impl FnOnce(), mut release: impl FnMut() -> u32, retain_probe: impl FnOnce() -> bool, + before_last: impl FnOnce(), ) -> (u32, bool) { add_ref(); let after_caller = release(); - if after_caller == 1 && retain_probe() { (0, true) } else { (release(), false) } + if after_caller == 1 { + if retain_probe() { + return (0, true); + } + before_last(); + } + (release(), false) } /// Transfer the caller's temporary AddRef probe into pool ownership when releasing `raw` would @@ -1624,6 +1634,7 @@ mod tests { retained.set(retained.get() + 1); true }, + || panic!("a busy allocator keeps its books: the pool holds it"), ); assert_eq!((remaining, transferred, refs.get(), retained.get()), (0, true, 1, 1)); refs.set(refs.get() - 1); // the out-of-lock sweep releases the transferred probe @@ -1641,10 +1652,37 @@ mod tests { retained.set(retained.get() + 1); true }, + || panic!("another reference is left: nothing ends"), ); assert_eq!((remaining, transferred, refs.get(), retained.get()), (1, false, 1, 0)); } + /// The last Release of an allocator nothing uses: its books go while the object is still + /// there, so that a new allocator the driver makes at its address keeps its own. (After the + /// Release, `forget` took the new one's type and stand-ins, and marked one with a recording + /// for release by the pool.) + #[test] + fn an_idle_allocators_books_go_before_its_last_release() { + use std::cell::{Cell, RefCell}; + let refs = Cell::new(1u32); + let events = RefCell::new(Vec::new()); + let (remaining, transferred) = release_with_probe( + || refs.set(refs.get() + 1), + || { + refs.set(refs.get() - 1); + events.borrow_mut().push(if refs.get() == 0 { "freed" } else { "probe released" }); + refs.get() + }, + || { + events.borrow_mut().push("asked the pool"); + false + }, + || events.borrow_mut().push("books dropped"), + ); + assert_eq!((remaining, transferred), (0, false)); + assert_eq!(*events.borrow(), ["probe released", "asked the pool", "books dropped", "freed"]); + } + #[test] fn release_probe_retries_a_same_address_backing_replacement() { use std::mem::{offset_of, size_of}; diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index a9dad2dd11..f860ed3413 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -11549,15 +11549,26 @@ unsafe extern "system" fn alloc_release(this: *mut c_void) -> u32 { return f(this); } let add_ref: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(orig_fn(this, 1)); + // What XCL keeps by the allocator's address goes while this call still holds its last + // reference: once the driver has freed it, another thread can be given a new allocator at + // that address, and these would then be that one's books. + let ended = std::cell::Cell::new(false); + let end = || { + ended.set(true); + dispatch_chain::allocator_reset(this as usize); + super::alloc_pool::forget(this as usize); + super::list_x::on_allocator_release(this as usize); + }; let (n, _) = super::alloc_pool::release_with_probe( || { add_ref(this); }, || f(this), || super::alloc_pool::retain_release_probe(this), + end, ); - if n == 0 { - dispatch_chain::allocator_reset(this as usize); - super::alloc_pool::forget(this as usize); - super::list_x::on_allocator_release(this as usize); + // (one the pool keeps for the GPU, or two Releases at once that both missed the last + // reference: after the Release, as before) + if n == 0 && !ended.get() { + end(); } n }