Skip to content
Draft
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
63 changes: 60 additions & 3 deletions crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,48 @@ impl<'a> SubmissionChunk<'a> {
}
}

/// Submit one exact title chunk, adding a retirement receipt only when an opt-in recording can
/// read the shared view heap. The ordinary path stays on `SubmissionGate::submit`, which avoids
/// an extra queue marker when descriptor-epoch tracking is disabled or the chunk does not use it.
pub(super) unsafe fn submit_title_chunk(
gate: &mut super::alloc_pool::SubmissionGate,
queue: *mut c_void,
lists: &[*mut c_void],
protect_shared_view: bool,
prepare: impl FnOnce(),
execute: impl FnOnce(),
) -> bool {
if !protect_shared_view {
return gate.submit(queue, lists, prepare, execute);
}
let mut epoch = None;
let submitted = gate.submit_receipted(
queue,
lists,
|| {
epoch = Some(super::vram::reserve_shared_view_epoch());
prepare();
},
execute,
);
match submitted {
super::alloc_pool::ReceiptedSubmission::NotExecuted => {
if let Some(epoch) = epoch {
super::vram::cancel_shared_view_epoch(epoch);
}
false
}
super::alloc_pool::ReceiptedSubmission::Executed(receipt) => {
super::vram::attach_shared_view_epoch(epoch.expect("executed shared-view submission without an epoch"), receipt);
true
}
super::alloc_pool::ReceiptedSubmission::ExecutedUntracked => {
super::vram::poison_shared_view_epoch(epoch.expect("executed shared-view submission without an epoch"));
true
}
}
}

/// Record a newly created title command queue so `KickoffX` can find a compatible queue.
pub fn on_queue_create(queue: usize, device: usize, ty: D3D12_COMMAND_LIST_TYPE) {
if queue == 0 {
Expand Down Expand Up @@ -4203,7 +4245,7 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) {
waits: Vec::new(), sparse_writes: Vec::new(), texture_writes: Vec::new(), texture_transitions: Vec::new(), uses_shared_view_heap: false, write64_signals: Vec::new(), cpu_writes: Vec::new(),
}),
};
let KickoffEffects { waits, sparse_writes, texture_writes, texture_transitions, uses_shared_view_heap: _uses_shared_view_heap, write64_signals, cpu_writes } = effects;
let KickoffEffects { waits, sparse_writes, texture_writes, texture_transitions, uses_shared_view_heap, write64_signals, cpu_writes } = effects;
if let Some(wrapper) = super::title_list::wrapper_of(this) {
record_deferred(wrapper);
}
Expand All @@ -4219,6 +4261,7 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) {
let mut prior_cpu_completions = Vec::new();
let mut wait_after_cpu_write = false;
let mut gate = super::alloc_pool::SubmissionGate::default();
let track_shared_view_epochs = super::vram::srv_tracking_enabled();
if let Some((_, ref prior, _)) = kickoff_split {
if !prior.is_empty() {
let plan = submission_plan(prior);
Expand All @@ -4227,7 +4270,14 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) {
let (completions, _) = execute_submission_plan_with_completions(q_raw, &plan, &waits, |chunk| {
super::queue_submit::with(q_raw as usize, || {
let lists = chunk.lists();
if !gate.submit(q_raw, lists, || {}, || exec(q_raw, lists.len() as u32, lists.as_ptr())) {
if !submit_title_chunk(
&mut gate,
q_raw,
lists,
track_shared_view_epochs && chunk.uses_shared_view_heap(),
|| {},
|| exec(q_raw, lists.len() as u32, lists.as_ptr()),
) {
return (false, None);
}
submitted.extend_from_slice(lists);
Expand All @@ -4254,7 +4304,14 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) {

let one = [this];
let current_submitted = super::queue_submit::with(q_raw as usize, || {
let submitted = gate.submit(q_raw, &one, || {}, || exec(q_raw, 1, one.as_ptr()));
let submitted = submit_title_chunk(
&mut gate,
q_raw,
&one,
track_shared_view_epochs && uses_shared_view_heap,
|| {},
|| exec(q_raw, 1, one.as_ptr()),
);
if submitted {
if let Some(trace) = &current_trace {
mark_trace(trace, ListEvent::Executed);
Expand Down
5 changes: 4 additions & 1 deletion crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9562,12 +9562,15 @@ unsafe extern "system" fn q_execute_command_lists(this: *mut c_void, n: u32, lis
let mut chunks = 0usize;
let mut gate = super::alloc_pool::SubmissionGate::default();
let mut submitted = Vec::with_capacity(mapped.len());
let track_shared_view_epochs = super::vram::srv_tracking_enabled();
let _ = super::list_x::execute_submission_plan_until_failure_on(this, &plan, |chunk| {
super::queue_submit::with(this as usize, || {
let lists = chunk.lists();
if !gate.submit(
if !super::list_x::submit_title_chunk(
&mut gate,
this,
lists,
track_shared_view_epochs && chunk.uses_shared_view_heap(),
|| {
let _ = super::alloc_pool::note_resubmits(this, lists);
},
Expand Down
201 changes: 201 additions & 0 deletions crates/xcl/src/hle/d3d12x/vram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,119 @@ pub(super) fn srv_tracking_enabled() -> bool {
#[cfg(test)]
static SRV_TRACKING_FOR_TEST: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) struct SharedViewEpoch(u64);

enum EpochRetirement<R> {
Reserved,
Receipt(R),
Poisoned,
}

struct SharedViewEpochs<R> {
next: u64,
poisoned: bool,
pending: Vec<(SharedViewEpoch, EpochRetirement<R>)>,
}

impl<R> SharedViewEpochs<R> {
const fn new() -> Self {
Self { next: 0, poisoned: false, pending: Vec::new() }
}

fn reserve(&mut self) -> SharedViewEpoch {
self.next = self.next.checked_add(1).expect("shared view heap epoch overflow");
let epoch = SharedViewEpoch(self.next);
self.pending.push((epoch, EpochRetirement::Reserved));
epoch
}

fn cancel(&mut self, epoch: SharedViewEpoch) {
match self.pending.iter().position(|entry| entry.0 == epoch) {
Some(i) if matches!(self.pending[i].1, EpochRetirement::Reserved) => {
self.pending.remove(i);
}
_ => self.poisoned = true,
}
}

fn attach(&mut self, epoch: SharedViewEpoch, receipt: R) {
match self.pending.iter_mut().find(|entry| entry.0 == epoch) {
Some((_, state @ EpochRetirement::Reserved)) => *state = EpochRetirement::Receipt(receipt),
_ => self.poisoned = true,
}
}

fn poison(&mut self, epoch: SharedViewEpoch) {
match self.pending.iter_mut().find(|entry| entry.0 == epoch) {
Some((_, state @ EpochRetirement::Reserved)) => *state = EpochRetirement::Poisoned,
_ => self.poisoned = true,
}
}

fn sweep(&mut self, complete: impl Fn(&R) -> bool) {
self.pending.retain(|(_, state)| !matches!(state, EpochRetirement::Receipt(receipt) if complete(receipt)));
}

fn quiescent(&mut self, complete: impl Fn(&R) -> bool) -> bool {
self.sweep(complete);
!self.poisoned && self.pending.is_empty()
}
}

fn with_quiescent_epochs<R, T>(
tracking_enabled: bool,
epochs: &Mutex<SharedViewEpochs<R>>,
complete: impl Fn(&R) -> bool,
mutation: impl FnOnce() -> T,
) -> Option<T> {
if !tracking_enabled {
return None;
}
let mut epochs = epochs.lock().unwrap();
if !epochs.quiescent(complete) {
return None;
}
let result = mutation();
drop(epochs);
Some(result)
}

static SHARED_VIEW_EPOCHS: Mutex<SharedViewEpochs<super::alloc_pool::SubmissionReceipt>> =
Mutex::new(SharedViewEpochs::new());

pub(super) fn reserve_shared_view_epoch() -> SharedViewEpoch {
let mut epochs = SHARED_VIEW_EPOCHS.lock().unwrap();
epochs.sweep(super::alloc_pool::SubmissionReceipt::is_complete);
epochs.reserve()
}

pub(super) fn cancel_shared_view_epoch(epoch: SharedViewEpoch) {
SHARED_VIEW_EPOCHS.lock().unwrap().cancel(epoch);
}

pub(super) fn attach_shared_view_epoch(epoch: SharedViewEpoch, receipt: super::alloc_pool::SubmissionReceipt) {
SHARED_VIEW_EPOCHS.lock().unwrap().attach(epoch, receipt);
}

pub(super) fn poison_shared_view_epoch(epoch: SharedViewEpoch) {
SHARED_VIEW_EPOCHS.lock().unwrap().poison(epoch);
}

/// Run a descriptor mutation only while every tracked shared-view submission is retired, holding
/// the epoch lock until the mutation ends so a new reservation cannot cross the check. This lock
/// is outermost: the closure may take descriptor backing/tracker locks, but must not submit GPU
/// work or call anything that reserves another shared-view epoch.
///
/// A missing post-execute receipt permanently returns `None`, so an untracked submission (or a
/// reused queue address) can never be mistaken for quiescence.
#[allow(dead_code)] // consumed by the later descriptor-rewrite stage
pub(super) fn with_quiescent_shared_view_heap<T>(mutation: impl FnOnce() -> T) -> Option<T> {
with_quiescent_epochs(
srv_tracking_enabled(), &SHARED_VIEW_EPOCHS, super::alloc_pool::SubmissionReceipt::is_complete, mutation,
)
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(super) struct SrvSlot {
heap_generation: u64,
Expand Down Expand Up @@ -2248,6 +2361,94 @@ pub unsafe fn place(this: *mut c_void, va: usize, desc: &D3D12_RESOURCE_DESC, st
mod tests {
use super::*;

#[derive(Clone)]
struct FakeReceipt {
fence: Arc<std::sync::atomic::AtomicU64>,
value: u64,
}

fn fake_complete(receipt: &FakeReceipt) -> bool {
receipt.fence.load(std::sync::atomic::Ordering::Relaxed) >= receipt.value
}

#[test]
fn shared_view_epochs_cancel_retire_and_remain_monotonic() {
let mut epochs = SharedViewEpochs::new();
let cancelled = epochs.reserve();
let retiring = epochs.reserve();
assert!(cancelled.0 < retiring.0);
epochs.cancel(cancelled);

let fence = Arc::new(std::sync::atomic::AtomicU64::new(0));
epochs.attach(retiring, FakeReceipt { fence: Arc::clone(&fence), value: 3 });
assert!(!epochs.quiescent(fake_complete));
fence.store(3, std::sync::atomic::Ordering::Relaxed);
assert!(epochs.quiescent(fake_complete));
assert!(epochs.reserve().0 > retiring.0, "retirement never reuses an epoch identity");
}

#[test]
fn reused_queue_address_cannot_retire_an_old_epoch() {
let mut epochs = SharedViewEpochs::new();
let old_epoch = epochs.reserve();
let old_generation_fence = Arc::new(std::sync::atomic::AtomicU64::new(0));
epochs.attach(old_epoch, FakeReceipt { fence: Arc::clone(&old_generation_fence), value: 7 });

// A new queue generation at the same numeric address owns another fence. Its progress
// cannot satisfy the strong fence owner retained by the old epoch's receipt.
let replacement_generation_fence = Arc::new(std::sync::atomic::AtomicU64::new(99));
assert_eq!(replacement_generation_fence.load(std::sync::atomic::Ordering::Relaxed), 99);
assert!(!epochs.quiescent(fake_complete));
old_generation_fence.store(7, std::sync::atomic::Ordering::Relaxed);
assert!(epochs.quiescent(fake_complete));
}

#[test]
fn untracked_shared_view_epoch_never_becomes_quiescent() {
let mut epochs = SharedViewEpochs::<FakeReceipt>::new();
let epoch = epochs.reserve();
epochs.poison(epoch);
assert!(!epochs.quiescent(fake_complete));
}

#[test]
fn disabled_tracking_cannot_authorize_descriptor_mutation() {
let epochs = Mutex::new(SharedViewEpochs::<FakeReceipt>::new());
let mut ran = false;
assert!(with_quiescent_epochs(false, &epochs, fake_complete, || ran = true).is_none());
assert!(!ran);
}

#[test]
fn quiescent_mutation_excludes_new_reservations() {
let epochs = Arc::new(Mutex::new(SharedViewEpochs::<FakeReceipt>::new()));
let (entered_tx, entered_rx) = std::sync::mpsc::channel();
let (release_tx, release_rx) = std::sync::mpsc::channel();
let mutation_epochs = Arc::clone(&epochs);
let mutation = std::thread::spawn(move || {
with_quiescent_epochs(true, &mutation_epochs, fake_complete, || {
entered_tx.send(()).unwrap();
release_rx.recv().unwrap();
})
});
entered_rx.recv().unwrap();

let (attempted_tx, attempted_rx) = std::sync::mpsc::channel();
let (reserved_tx, reserved_rx) = std::sync::mpsc::channel();
let reserve_epochs = Arc::clone(&epochs);
let reserve = std::thread::spawn(move || {
attempted_tx.send(()).unwrap();
let epoch = reserve_epochs.lock().unwrap().reserve();
reserved_tx.send(epoch).unwrap();
});
attempted_rx.recv().unwrap();
assert!(reserved_rx.recv_timeout(std::time::Duration::from_millis(50)).is_err());
release_tx.send(()).unwrap();
assert!(mutation.join().unwrap().is_some());
assert_eq!(reserved_rx.recv().unwrap(), SharedViewEpoch(1));
reserve.join().unwrap();
}

fn tracked_entry(resource: usize, pc_srv: u64) -> TrackedSrv {
let mut words = [0u32; 8];
words[0] = pc_srv as u32;
Expand Down
Loading