An `as` cast silently drops the bits that do not fit the target type, and nothing at the cast shows whether that can happen. As part of a larger effort to reduce the number of `as` casts in Tyr, convert `SeatInfo::slot` from `u8` to `usize`.
The slot index is stored as a `u8`, while the code that uses it takes a `usize`. The casts follow from that choice. The index is cast with `as` on the way into a `SeatInfo` and on the way out, and `SlotManager::new()` rejects more than 256 slots so that the cast to `u8` cannot truncate. Store the index as `usize`, which removes the casts and the limit. Signed-off-by: Daniel Almeida <[email protected]> --- drivers/gpu/drm/tyr/mmu/address_space.rs | 15 +++------------ drivers/gpu/drm/tyr/slot.rs | 23 +++++++++-------------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/drivers/gpu/drm/tyr/mmu/address_space.rs b/drivers/gpu/drm/tyr/mmu/address_space.rs index 76e3d2df6afd..dd6377bee989 100644 --- a/drivers/gpu/drm/tyr/mmu/address_space.rs +++ b/drivers/gpu/drm/tyr/mmu/address_space.rs @@ -467,10 +467,7 @@ pub(super) fn start_vm_update( ) -> Result { let seat = vm_as_data.as_seat.access(self); match seat.slot() { - Some(slot) => { - let as_nr = slot as usize; - self.as_start_update(as_nr, region) - } + Some(slot) => self.as_start_update(slot, region), _ => Ok(()), } } @@ -479,10 +476,7 @@ pub(super) fn start_vm_update( pub(super) fn end_vm_update(&mut self, vm_as_data: &VmAsData<'drm>) -> Result { let seat = vm_as_data.as_seat.access(self); match seat.slot() { - Some(slot) => { - let as_nr = slot as usize; - self.as_end_update(as_nr) - } + Some(slot) => self.as_end_update(slot), _ => Ok(()), } } @@ -491,10 +485,7 @@ pub(super) fn end_vm_update(&mut self, vm_as_data: &VmAsData<'drm>) -> Result { pub(super) fn flush_vm(&mut self, vm_as_data: &VmAsData<'drm>) -> Result { let seat = vm_as_data.as_seat.access(self); match seat.slot() { - Some(slot) => { - let as_nr = slot as usize; - self.as_flush(as_nr) - } + Some(slot) => self.as_flush(slot), _ => Ok(()), } } diff --git a/drivers/gpu/drm/tyr/slot.rs b/drivers/gpu/drm/tyr/slot.rs index d194ead53f71..0e153dc025c5 100644 --- a/drivers/gpu/drm/tyr/slot.rs +++ b/drivers/gpu/drm/tyr/slot.rs @@ -47,7 +47,7 @@ pub(crate) struct SeatInfo { /// it means the object has been evicted from the hardware slot, /// and a new slot needs to be acquired to make this object /// resident again. - slot: u8, + slot: usize, /// Sequence number encoding the last time this seat was active. /// We also use it to check if a slot is still bound to a seat. @@ -98,7 +98,7 @@ impl Seat { /// If the seat is not `Seat::Active` we can't trust the /// `SeatInfo`. In that case `None` is returned, otherwise /// `Some(SeatInfo::slot)` is returned. - pub(crate) fn slot(&self) -> Option<u8> { + pub(crate) fn slot(&self) -> Option<usize> { match self { Self::Active(info) => Some(info.slot), _ => None, @@ -174,10 +174,6 @@ pub(crate) fn new(manager: T, slot_count: usize) -> Result<Self> { if slot_count > MAX_SLOTS { return Err(EINVAL); } - // Since the slot index is stored in SeatInfo as a u8, the maximum number of slots is 256. - if slot_count > u8::MAX as usize + 1 { - return Err(EINVAL); - } Ok(Self { manager, @@ -193,7 +189,7 @@ fn record_active_slot(&mut self, slot_idx: usize, slot_data: T::SlotData) { let cur_seqno = self.use_seqno; *T::seat(&slot_data).access_mut(self) = Seat::Active(SeatInfo { - slot: slot_idx as u8, + slot: slot_idx, seqno: cur_seqno, }); @@ -221,7 +217,7 @@ fn reactivate_slot(&mut self, slot_idx: usize, slot_data: &T::SlotData) -> Resul }; *T::seat(slot_data).access_mut(self) = Seat::Active(SeatInfo { - slot: slot_idx as u8, + slot: slot_idx, seqno: cur_seqno, }); @@ -325,8 +321,8 @@ fn evict_slot(&mut self, slot_idx: usize, locked_seat: &LockedSeat<T, MAX_SLOTS> /// If they don't match, the seat is stale and is reset to `NoSeat`. fn check_seat(&mut self, locked_seat: &LockedSeat<T, MAX_SLOTS>) { let (slot_idx, seat_seqno, is_active) = match locked_seat.access(self) { - Seat::Active(seat_info) => (seat_info.slot as usize, seat_info.seqno, true), - Seat::Idle(seat_info) => (seat_info.slot as usize, seat_info.seqno, false), + Seat::Active(seat_info) => (seat_info.slot, seat_info.seqno, true), + Seat::Idle(seat_info) => (seat_info.slot, seat_info.seqno, false), _ => return, }; @@ -353,7 +349,7 @@ pub(crate) fn activate(&mut self, slot_data: T::SlotData) -> Result { // Copy out only the slot index so the borrow of slot_data ends here. let slot_idx = match T::seat(&slot_data).access(self) { - Seat::Active(seat_info) | Seat::Idle(seat_info) => Some(seat_info.slot as usize), + Seat::Active(seat_info) | Seat::Idle(seat_info) => Some(seat_info.slot), Seat::NoSeat => None, }; @@ -368,7 +364,7 @@ pub(crate) fn activate(&mut self, slot_data: T::SlotData) -> Result { pub(crate) fn idle(&mut self, locked_seat: &LockedSeat<T, MAX_SLOTS>) -> Result { self.check_seat(locked_seat); if let Seat::Active(seat_info) = locked_seat.access(self) { - self.idle_slot(seat_info.slot as usize, locked_seat)?; + self.idle_slot(seat_info.slot, locked_seat)?; } Ok(()) } @@ -379,8 +375,7 @@ pub(crate) fn evict(&mut self, locked_seat: &LockedSeat<T, MAX_SLOTS>) -> Result match locked_seat.access(self) { Seat::Active(seat_info) | Seat::Idle(seat_info) => { - let slot_idx = seat_info.slot as usize; - self.evict_slot(slot_idx, locked_seat)?; + self.evict_slot(seat_info.slot, locked_seat)?; } _ => (), } -- 2.56.0
