Remove unnecssary overflow checks

This commit is contained in:
Ruihan Li 2025-05-27 11:08:47 +08:00 committed by Junyang Zhang
parent 282a0f216f
commit 6dc0189e43
2 changed files with 15 additions and 12 deletions

View File

@ -54,13 +54,13 @@ impl DmaCoherent {
/// The method fails if any part of the given `segment`
/// already belongs to a DMA mapping.
pub fn map(segment: USegment, is_cache_coherent: bool) -> core::result::Result<Self, DmaError> {
let frame_count = segment.size() / PAGE_SIZE;
let start_paddr = segment.start_paddr();
let frame_count = segment.size() / PAGE_SIZE;
if !check_and_insert_dma_mapping(start_paddr, frame_count) {
return Err(DmaError::AlreadyMapped);
}
// Ensure that the addresses used later will not overflow
start_paddr.checked_add(frame_count * PAGE_SIZE).unwrap();
if !is_cache_coherent {
let page_table = KERNEL_PAGE_TABLE.get().unwrap();
let vaddr = paddr_to_vaddr(start_paddr);
@ -72,6 +72,7 @@ impl DmaCoherent {
.unwrap();
}
}
let start_daddr = match dma_type() {
DmaType::Direct => {
#[cfg(target_arch = "x86_64")]
@ -100,6 +101,7 @@ impl DmaCoherent {
start_paddr as Daddr
}
};
Ok(Self {
inner: Arc::new(DmaCoherentInner {
segment,
@ -130,10 +132,9 @@ impl Deref for DmaCoherent {
impl Drop for DmaCoherentInner {
fn drop(&mut self) {
let frame_count = self.segment.size() / PAGE_SIZE;
let start_paddr = self.segment.start_paddr();
// Ensure that the addresses used later will not overflow
start_paddr.checked_add(frame_count * PAGE_SIZE).unwrap();
let frame_count = self.segment.size() / PAGE_SIZE;
match dma_type() {
DmaType::Direct => {
#[cfg(target_arch = "x86_64")]
@ -157,6 +158,7 @@ impl Drop for DmaCoherentInner {
}
}
}
if !self.is_cache_coherent {
let page_table = KERNEL_PAGE_TABLE.get().unwrap();
let vaddr = paddr_to_vaddr(start_paddr);
@ -168,6 +170,7 @@ impl Drop for DmaCoherentInner {
.unwrap();
}
}
remove_dma_mapping(start_paddr, frame_count);
}
}

View File

@ -54,13 +54,13 @@ impl DmaStream {
direction: DmaDirection,
is_cache_coherent: bool,
) -> Result<Self, DmaError> {
let frame_count = segment.size() / PAGE_SIZE;
let start_paddr = segment.start_paddr();
let frame_count = segment.size() / PAGE_SIZE;
if !check_and_insert_dma_mapping(start_paddr, frame_count) {
return Err(DmaError::AlreadyMapped);
}
// Ensure that the addresses used later will not overflow
start_paddr.checked_add(frame_count * PAGE_SIZE).unwrap();
let start_daddr = match dma_type() {
DmaType::Direct => {
#[cfg(target_arch = "x86_64")]
@ -170,10 +170,9 @@ impl HasDaddr for DmaStream {
impl Drop for DmaStreamInner {
fn drop(&mut self) {
let frame_count = self.segment.size() / PAGE_SIZE;
let start_paddr = self.segment.start_paddr();
// Ensure that the addresses used later will not overflow
start_paddr.checked_add(frame_count * PAGE_SIZE).unwrap();
let frame_count = self.segment.size() / PAGE_SIZE;
match dma_type() {
DmaType::Direct => {
#[cfg(target_arch = "x86_64")]
@ -198,6 +197,7 @@ impl Drop for DmaStreamInner {
}
}
}
remove_dma_mapping(start_paddr, frame_count);
}
}