From 0afd93fd5001c55ad2219e8c49c520480a8cf6df Mon Sep 17 00:00:00 2001 From: gmanka Date: Thu, 26 Nov 2026 23:30:07 +0500 Subject: [PATCH] GPU: return dynamic slots only to the heap that reserved them A recorded list can retain dynamic descriptors after the title releases its heap. If another title heap lands at that address before list reset, the old release can mark slots in the replacement heap as available, even if another recorded list owns them. Include a host heap identity in each reservation. Check it under the heap-registry lock before returning slots. The regression passes a retired heap identity through lookup of a full replacement heap, then verifies that only the replacement's own identity can release a slot. Based-on: https://github.com/fxceless/victory-fork/pull/1324 Tested-on: RX 6800 and Lavapipe, Fedora Linux 45, Linux native Assisted-by: gpt-6.2 sol xhigh --- crates/victory-gpu/src/descriptor.rs | 40 +++++++++++++++++++++-- crates/victory-gpu/src/descriptor/host.rs | 9 +++++ crates/victory-gpu/src/draw.rs | 10 +++--- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index 80026f9f5b..ff368a5730 100644 --- a/crates/victory-gpu/src/descriptor.rs +++ b/crates/victory-gpu/src/descriptor.rs @@ -138,13 +138,14 @@ fn mirror(handle: u64) { } } -/// Gives back dynamic slots of the host heap behind the title heap at -/// `heap`, if that heap still exists. -pub(crate) fn release_dynamic(heap: usize, first: u32, n: u32) { +/// Gives back slots only to the host heap that originally reserved them. +/// A new title heap can be registered at the same address before a list resets. +pub(crate) fn release_dynamic(heap: usize, id: u64, first: u32, n: u32) { let heaps = SHADER_HEAPS.lock().unwrap_or_else(|e| e.into_inner()); if heaps.iter().any(|&(_, _, h)| h == heap) // SAFETY: registered heaps are live (removed when dropped). && let Some(Some(host)) = unsafe { &*(heap as *const Heap) }.host.get() + && host.id() == id { host.release(first, n); } @@ -657,6 +658,39 @@ mod tests { create_heap(std::ptr::null(), &extended.0, &HEAP_IIDS[0], out) } + pub(crate) fn shader_visible_view_heap(count: u32) -> This { + let mut heap = std::ptr::null(); + let desc = HeapDesc { kind: VIEWS_KIND, count, flags: SHADER_VISIBLE, nodes: 0 }; + assert_eq!(create(desc, Placement { address: 0, size: 0 }, &mut heap), S_OK); + heap + } + + #[test] + fn a_stale_list_cannot_release_slots_owned_by_a_replacement_heap() { + let retired = shader_visible_view_heap(4); + let Some(host) = heap(retired).host() else { + eprintln!("no host descriptor heap; skipped"); + release(retired); + return; + }; + let retired_id = host.id(); + let retired_slot = host.dynamic(1).unwrap(); + release(retired); + + let replacement = shader_visible_view_heap(4); + let host = heap(replacement).host().unwrap(); + // SAFETY: the replacement heap is live for this test. + let capacity = unsafe { host.heap.GetDesc().NumDescriptors } - host.count(); + assert_eq!(host.dynamic(capacity), Some(retired_slot)); + // A reused title address makes registry lookup find the replacement. + release_dynamic(replacement as usize, retired_id, retired_slot, 1); + assert_eq!(host.dynamic(1), None); + + release_dynamic(replacement as usize, host.id(), retired_slot, 1); + assert_eq!(host.dynamic(1), Some(retired_slot)); + release(replacement); + } + #[test] fn heaps_hold_count_descriptors_from_their_start() { let null = std::ptr::null(); diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 43dba24285..995f218634 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -6,6 +6,7 @@ //! reads it through a table and finds the slot changed. use std::sync::Mutex; +use std::sync::atomic::{AtomicU64, Ordering}; use windows::Win32::Graphics::Direct3D12::*; use windows::Win32::Graphics::Dxgi::Common::*; @@ -15,6 +16,7 @@ use crate::host::host; pub(crate) struct HostHeap { pub(crate) heap: ID3D12DescriptorHeap, + id: u64, cpu: usize, step: u32, /// Slots mirroring the title's; view heaps have [`DYNAMIC`] more. @@ -32,6 +34,8 @@ pub(crate) struct HostHeap { next: Mutex<(u32, Vec)>, } +static NEXT_HEAP_ID: AtomicU64 = AtomicU64::new(1); + /// Host heaps' GPU ranges (start, bytes, bytes per slot, sampler), for /// device-removed reports: a shader indexing a heap past its end faults. static GPU_RANGES: Mutex> = Mutex::new(Vec::new()); @@ -116,6 +120,7 @@ impl HostHeap { )); let h = HostHeap { heap, + id: NEXT_HEAP_ID.fetch_add(1, Ordering::Relaxed), cpu, step, count, @@ -240,6 +245,10 @@ impl HostHeap { self.count } + pub(crate) fn id(&self) -> u64 { + self.id + } + /// Gives back `n` dynamic slots from `first` (an index [`dynamic`] /// returned). /// diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index 7efe77ec9a..1bd6830b78 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -74,15 +74,15 @@ pub(crate) struct State { /// Tables made afresh in this list: (table, what it holds) → first /// dynamic slot. fresh_tables: std::collections::HashMap<(u64, u64), u32>, - /// The dynamic slots the list holds: title heap, first, count; given + /// The dynamic slots the list holds: title heap, host ID, first, count; given /// back when the list is reset or released. - dynamic: Vec<(usize, u32, u32)>, + dynamic: Vec<(usize, u64, u32, u32)>, } impl Drop for State { fn drop(&mut self) { - for &(heap, first, n) in &self.dynamic { - crate::descriptor::release_dynamic(heap, first, n); + for &(heap, id, first, n) in &self.dynamic { + crate::descriptor::release_dynamic(heap, id, first, n); } } } @@ -718,7 +718,7 @@ impl State { }; self.fresh_tables.insert(key, first); self.dynamic - .push((std::ptr::from_ref(heap) as usize, first, n)); + .push((std::ptr::from_ref(heap) as usize, host.id(), first, n)); // The whole range: a shader may index past the entries its // used-resource list names (descriptor arrays). for k in 0..n {