//! Fence-guarded `SlabRing` DMA ownership state machine. //! //! Directly implements the transitions verified in `proofs/slab_ring_dma.lean`: //! `Free -> IoInFlight -> GpuReady -> GpuInUse -> Free`. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum SlotState { Free, IoInFlight { req_id: u64 }, GpuReady { tag: u64 }, GpuInUse { tag: u64, fence: u64 }, } #[derive(Debug, Clone, PartialEq, Eq)] pub enum SlabError { OutOfBounds { idx: usize, cap: usize }, InvalidTransition { idx: usize, state: SlotState, op: &'static str }, TagMismatch { idx: usize, expected: u64, actual: u64 }, FenceMismatch { idx: usize, expected: u64, actual: u64 }, NoFreeSlot, } impl std::fmt::Display for SlabError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{self:?}") } } impl std::error::Error for SlabError {} /// Tracks ownership of `N` page-aligned zero-copy UMA buffers shared between `io_uring` and HIP. #[derive(Debug, Clone)] pub struct SlabRing { slots: Vec, slot_bytes: usize, } impl SlabRing { pub fn new(num_slots: usize, slot_bytes: usize) -> Self { assert!(num_slots > 0, "SlabRing requires at least 1 slot"); Self { slots: vec![SlotState::Free; num_slots], slot_bytes, } } pub fn len(&self) -> usize { self.slots.len() } pub fn is_empty(&self) -> bool { self.slots.is_empty() } pub fn slot_bytes(&self) -> usize { self.slot_bytes } pub fn state(&self, idx: usize) -> Result { self.slots .get(idx) .copied() .ok_or(SlabError::OutOfBounds { idx, cap: self.slots.len() }) } pub fn find_free(&self) -> Option { self.slots.iter().position(|&s| s == SlotState::Free) } pub fn submit_io(&mut self, idx: usize, req_id: u64) -> Result<(), SlabError> { let s = self.state(idx)?; if s != SlotState::Free { return Err(SlabError::InvalidTransition { idx, state: s, op: "submit_io" }); } self.slots[idx] = SlotState::IoInFlight { req_id }; Ok(()) } pub fn complete_io(&mut self, idx: usize, req_id: u64, tag: u64) -> Result<(), SlabError> { match self.state(idx)? { SlotState::IoInFlight { req_id: cur } if cur == req_id => { self.slots[idx] = SlotState::GpuReady { tag }; Ok(()) } other => Err(SlabError::InvalidTransition { idx, state: other, op: "complete_io" }), } } pub fn abort_io(&mut self, idx: usize, req_id: u64) -> Result<(), SlabError> { match self.state(idx)? { SlotState::IoInFlight { req_id: cur } if cur == req_id => { self.slots[idx] = SlotState::Free; Ok(()) } other => Err(SlabError::InvalidTransition { idx, state: other, op: "abort_io" }), } } pub fn launch_gpu(&mut self, idx: usize, expected_tag: u64, fence: u64) -> Result<(), SlabError> { match self.state(idx)? { SlotState::GpuReady { tag } if tag == expected_tag => { self.slots[idx] = SlotState::GpuInUse { tag, fence }; Ok(()) } SlotState::GpuReady { tag } => { Err(SlabError::TagMismatch { idx, expected: expected_tag, actual: tag }) } other => Err(SlabError::InvalidTransition { idx, state: other, op: "launch_gpu" }), } } pub fn discard_ready(&mut self, idx: usize) -> Result<(), SlabError> { match self.state(idx)? { SlotState::GpuReady { .. } => { self.slots[idx] = SlotState::Free; Ok(()) } other => Err(SlabError::InvalidTransition { idx, state: other, op: "discard_ready" }), } } pub fn complete_gpu(&mut self, idx: usize, fence: u64) -> Result<(), SlabError> { match self.state(idx)? { SlotState::GpuInUse { fence: cur, .. } if cur == fence => { self.slots[idx] = SlotState::Free; Ok(()) } SlotState::GpuInUse { fence: cur, .. } => { Err(SlabError::FenceMismatch { idx, expected: cur, actual: fence }) } other => Err(SlabError::InvalidTransition { idx, state: other, op: "complete_gpu" }), } } } #[cfg(test)] mod tests { use super::*; #[test] fn full_ping_pong_lifecycle_and_safety() { let mut ring = SlabRing::new(3, 4 * 1024 * 1024); let s0 = ring.find_free().unwrap(); assert_eq!(s0, 0); // Cannot launch GPU before DMA completes assert!(ring.launch_gpu(s0, 10, 1).is_err()); ring.submit_io(s0, 100).unwrap(); // Cannot launch GPU while IO is still in flight assert!(ring.launch_gpu(s0, 10, 1).is_err()); ring.complete_io(s0, 100, 10).unwrap(); // Cannot overwrite with new IO while GpuReady or GpuInUse assert!(ring.submit_io(s0, 101).is_err()); ring.launch_gpu(s0, 10, 42).unwrap(); assert!(ring.submit_io(s0, 102).is_err()); assert!(ring.complete_gpu(s0, 999).is_err()); // wrong fence rejected ring.complete_gpu(s0, 42).unwrap(); assert_eq!(ring.state(s0).unwrap(), SlotState::Free); } }