Add `DmaCoherent::alloc_uninit()` and defer zeroing to `DmaCoherent::alloc()`

This commit is contained in:
Tao Su 2026-01-07 10:11:11 +00:00 committed by Tate, Hongliang Tian
parent 922b204280
commit fc2e8d95c9
3 changed files with 21 additions and 4 deletions

View File

@ -18,7 +18,7 @@ pub enum PageConvertError {
///
/// It invokes the [`map_gpa`] TDVMCALL to convert those pages into Intel TDX
/// shared pages. Due to the conversion, any existing data on the pages will
/// be erased.
/// be lost.
///
/// # Safety
///
@ -38,7 +38,7 @@ pub unsafe fn unprotect_gpa_tdvm_call(gpa: Paddr, size: usize) -> Result<(), Pag
///
/// It invokes the [`map_gpa`] TDVMCALL and the [`accept_page`] TDCALL to
/// convert those pages into Intel TDX private pages. Due to the conversion,
/// any existing data on the pages will be erased.
/// any existing data on the pages will be zeroed.
///
/// # Safety
///

View File

@ -37,14 +37,29 @@ enum Inner {
impl DmaCoherent {
/// Allocates a region of physical memory for coherent DMA access.
///
/// The memory of the newly-allocated DMA buffer is initialized to zeros.
///
/// The `is_cache_coherent` argument specifies whether the target device
/// that the DMA mapping is prepared for can access the main memory in a
/// CPU cache coherent way or not.
pub fn alloc(nframes: usize, is_cache_coherent: bool) -> Result<Self, Error> {
Self::alloc_uninit(nframes, is_cache_coherent).inspect(|dma| {
dma.writer().fill_zeros(dma.size());
})
}
/// Allocates a region of physical memory for coherent DMA access
/// without initialization.
///
/// This method is the same as [`DmaCoherent::alloc`]
/// except that it skips zeroing the memory of newly-allocated DMA region.
pub fn alloc_uninit(nframes: usize, is_cache_coherent: bool) -> Result<Self, Error> {
let cvm = cvm_need_private_protection();
let (inner, paddr_range) = if is_cache_coherent && !cvm {
let segment = FrameAllocOptions::new().alloc_segment(nframes)?;
let segment = FrameAllocOptions::new()
.zeroed(false)
.alloc_segment(nframes)?;
let paddr_range = segment.paddr_range();
(Inner::Segment(segment), paddr_range)

View File

@ -76,7 +76,9 @@ pub(super) fn alloc_kva(
is_cache_coherent: bool,
) -> Result<(KVirtArea, Paddr), Error> {
let segment = Segment::from_unsized(
FrameAllocOptions::new().alloc_segment_with(nframes, |_| DmaBufferMeta)?,
FrameAllocOptions::new()
.zeroed(false)
.alloc_segment_with(nframes, |_| DmaBufferMeta)?,
);
#[cfg_attr(not(target_arch = "x86_64"), expect(unused_labels))]