Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions crates/xcl/src/hle/d3d12x/alloc_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions crates/xcl/src/hle/d3d12x/title_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading