Zero memory in dma_alloc.
virtio-drivers now documents that this is required.
Bug: 274732281
Test: atest vmbase_example.integration_test
Change-Id: Ie3782e261cac130dd8f7d0c5905d90161789e76b
diff --git a/vmbase/example/src/pci.rs b/vmbase/example/src/pci.rs
index 41a3ff4..384a9c1 100644
--- a/vmbase/example/src/pci.rs
+++ b/vmbase/example/src/pci.rs
@@ -15,7 +15,7 @@
//! Functions to scan the PCI bus for VirtIO device.
use aarch64_paging::paging::MemoryRegion;
-use alloc::alloc::{alloc, dealloc, handle_alloc_error, Layout};
+use alloc::alloc::{alloc_zeroed, dealloc, handle_alloc_error, Layout};
use core::{mem::size_of, ptr::NonNull};
use fdtpci::PciInfo;
use log::{debug, info};
@@ -103,7 +103,7 @@
debug!("dma_alloc: pages={}", pages);
let layout = Layout::from_size_align(pages * PAGE_SIZE, PAGE_SIZE).unwrap();
// Safe because the layout has a non-zero size.
- let vaddr = unsafe { alloc(layout) };
+ let vaddr = unsafe { alloc_zeroed(layout) };
let vaddr =
if let Some(vaddr) = NonNull::new(vaddr) { vaddr } else { handle_alloc_error(layout) };
let paddr = virt_to_phys(vaddr);
diff --git a/vmbase/src/virtio/hal.rs b/vmbase/src/virtio/hal.rs
index ac5b967..36f9e56 100644
--- a/vmbase/src/virtio/hal.rs
+++ b/vmbase/src/virtio/hal.rs
@@ -42,13 +42,14 @@
/// `dma_alloc` ensures the returned DMA buffer is not aliased with any other allocation or
/// reference in the program until it is deallocated by `dma_dealloc` by allocating a unique
/// block of memory using `alloc_shared`, which is guaranteed to allocate valid and unique
- /// memory. We request an alignment of at least `PAGE_SIZE` from `alloc_shared`.
+ /// memory. We request an alignment of at least `PAGE_SIZE` from `alloc_shared`. We zero the
+ /// buffer before returning it.
fn dma_alloc(pages: usize, _direction: BufferDirection) -> (PhysAddr, NonNull<u8>) {
- let vaddr = alloc_shared(dma_layout(pages))
- .expect("Failed to allocate and share VirtIO DMA range with host");
- // TODO(ptosi): Move this zeroing to virtio_drivers, if it silently wants a zeroed region.
+ let layout = dma_layout(pages);
+ let vaddr =
+ alloc_shared(layout).expect("Failed to allocate and share VirtIO DMA range with host");
// SAFETY - vaddr points to a region allocated for the caller so is safe to access.
- unsafe { core::ptr::write_bytes(vaddr.as_ptr(), 0, dma_layout(pages).size()) };
+ unsafe { core::ptr::write_bytes(vaddr.as_ptr(), 0, layout.size()) };
let paddr = virt_to_phys(vaddr);
(paddr, vaddr)
}