vmm: virtio-mem: Enforce alignment and size requirements

The virtio-mem driver is generating some warnings regarding both size
and alignment of the virtio-mem region if not based on 128MiB:

The alignment of the physical start address can make some memory
unusable.
The alignment of the physical end address can make some memory
unusable.

For these reasons, the current patch enforces virtio-mem regions to be
128MiB aligned and checks the size provided by the user is a multiple of
128MiB.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2020-09-14 11:36:31 +02:00
parent eb7b923e22
commit 1798ed8194
2 changed files with 9 additions and 9 deletions

View File

@ -42,8 +42,11 @@ use vmm_sys_util::eventfd::EventFd;
const QUEUE_SIZE: u16 = 128; const QUEUE_SIZE: u16 = 128;
const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE]; const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE];
// 128MiB is the standard memory block size in Linux. A virtio-mem region must
// be aligned on this size, and the region size must be a multiple of it.
pub const VIRTIO_MEM_ALIGN_SIZE: u64 = 128 * 1024 * 1024;
// Use 2 MiB alignment so transparent hugepages can be used by KVM. // Use 2 MiB alignment so transparent hugepages can be used by KVM.
pub const VIRTIO_MEM_DEFAULT_BLOCK_SIZE: u64 = 512 * 4096; const VIRTIO_MEM_DEFAULT_BLOCK_SIZE: u64 = 512 * 4096;
const VIRTIO_MEM_USABLE_EXTENT: u64 = 256 * 1024 * 1024; const VIRTIO_MEM_USABLE_EXTENT: u64 = 256 * 1024 * 1024;
// Request processed successfully, applicable for // Request processed successfully, applicable for
@ -702,13 +705,12 @@ impl Mem {
) -> io::Result<Mem> { ) -> io::Result<Mem> {
let region_len = region.len(); let region_len = region.len();
if region_len != region_len / VIRTIO_MEM_DEFAULT_BLOCK_SIZE * VIRTIO_MEM_DEFAULT_BLOCK_SIZE if region_len != region_len / VIRTIO_MEM_ALIGN_SIZE * VIRTIO_MEM_ALIGN_SIZE {
{
return Err(io::Error::new( return Err(io::Error::new(
io::ErrorKind::Other, io::ErrorKind::Other,
format!( format!(
"Virtio-mem size is not aligned with {}", "Virtio-mem size is not aligned with {}",
VIRTIO_MEM_DEFAULT_BLOCK_SIZE VIRTIO_MEM_ALIGN_SIZE
), ),
)); ));
} }

View File

@ -523,11 +523,9 @@ impl MemoryManager {
} else { } else {
// Alignment must be "natural" i.e. same as size of block // Alignment must be "natural" i.e. same as size of block
let start_addr = GuestAddress( let start_addr = GuestAddress(
(start_of_device_area.0 (start_of_device_area.0 + virtio_devices::VIRTIO_MEM_ALIGN_SIZE - 1)
+ virtio_devices::VIRTIO_MEM_DEFAULT_BLOCK_SIZE / virtio_devices::VIRTIO_MEM_ALIGN_SIZE
- 1) * virtio_devices::VIRTIO_MEM_ALIGN_SIZE,
/ virtio_devices::VIRTIO_MEM_DEFAULT_BLOCK_SIZE
* virtio_devices::VIRTIO_MEM_DEFAULT_BLOCK_SIZE,
); );
let region = MemoryManager::create_ram_region( let region = MemoryManager::create_ram_region(