[vmbase] Adjust refill layout size in memory allocation
This cl adjusts the refill layout size to the next power of two
when allocating memory. This is necessary because the actual size
of the memory allocated in the subsequent call `alloc_aligned` is
the max of the next power of two and the alignment.
This is clear based on the implementation of `alloc_aligned`
but is not mentioned in its documentation[1].
Without this adjustment, the `alloc_aligned` call will fail if
given a layout whose size is not a power of two.
[1] https://docs.rs/buddy_system_allocator/latest/buddy_system_allocator/struct.FrameAllocator.html#method.alloc_aligned
Bug: 274441673
Test: atest MicrodroidTests
Change-Id: I37d7a2805aa6788d22c2cd5bfc8c701fad1f007d
diff --git a/vmbase/src/memory/shared.rs b/vmbase/src/memory/shared.rs
index 173c0ec..4dd37dd 100644
--- a/vmbase/src/memory/shared.rs
+++ b/vmbase/src/memory/shared.rs
@@ -27,6 +27,7 @@
use alloc::vec::Vec;
use buddy_system_allocator::{FrameAllocator, LockedFrameAllocator};
use core::alloc::Layout;
+use core::cmp::max;
use core::mem::size_of;
use core::num::NonZeroUsize;
use core::ops::Range;
@@ -354,6 +355,11 @@
}
fn try_shared_alloc(layout: Layout) -> Option<NonNull<u8>> {
+ // Adjusts the layout size to the max of the next power of two and the alignment,
+ // as this is the actual size of the memory allocated in `alloc_aligned()`.
+ let size = max(layout.size().next_power_of_two(), layout.align());
+ let layout = Layout::from_size_align(size, layout.align()).unwrap();
+
let mut shared_pool = SHARED_POOL.get().unwrap().lock();
if let Some(buffer) = shared_pool.alloc_aligned(layout) {