From 6dc0189e432add7a5acea9f5615bc8c31461e6e2 Mon Sep 17 00:00:00 2001 From: Ruihan Li Date: Tue, 27 May 2025 11:08:47 +0800 Subject: [PATCH] Remove unnecssary overflow checks --- ostd/src/mm/dma/dma_coherent.rs | 15 +++++++++------ ostd/src/mm/dma/dma_stream.rs | 12 ++++++------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/ostd/src/mm/dma/dma_coherent.rs b/ostd/src/mm/dma/dma_coherent.rs index 937bdd24..1ae52bb3 100644 --- a/ostd/src/mm/dma/dma_coherent.rs +++ b/ostd/src/mm/dma/dma_coherent.rs @@ -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 { - 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); } } diff --git a/ostd/src/mm/dma/dma_stream.rs b/ostd/src/mm/dma/dma_stream.rs index e959b680..e0f3035b 100644 --- a/ostd/src/mm/dma/dma_stream.rs +++ b/ostd/src/mm/dma/dma_stream.rs @@ -54,13 +54,13 @@ impl DmaStream { direction: DmaDirection, is_cache_coherent: bool, ) -> Result { - 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); } }