Skip to content

Commit e0e6c48

Browse files
committed
refactor(resources): split allocate_guest_memory in 2
Split the actual allocation from the calculations to find the right ranges. This will be used to create the hotpluggable region in the future. Signed-off-by: Riccardo Mancini <mancio@amazon.com>
1 parent 3cd22e6 commit e0e6c48

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/vmm/src/resources.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::path::PathBuf;
66
use std::sync::{Arc, Mutex, MutexGuard};
77

88
use serde::{Deserialize, Serialize};
9+
use vm_memory::GuestAddress;
910

1011
use crate::cpu_config::templates::CustomCpuTemplate;
1112
use crate::device_manager::persist::SharedDeviceType;
@@ -481,11 +482,14 @@ impl VmResources {
481482
Ok(())
482483
}
483484

484-
/// Allocates guest memory in a configuration most appropriate for these [`VmResources`].
485+
/// Allocates the given guest memory regions.
485486
///
486487
/// If vhost-user-blk devices are in use, allocates memfd-backed shared memory, otherwise
487488
/// prefers anonymous memory for performance reasons.
488-
pub fn allocate_guest_memory(&self) -> Result<Vec<GuestRegionMmap>, MemoryError> {
489+
fn allocate_memory_regions(
490+
&self,
491+
regions: &[(GuestAddress, usize)],
492+
) -> Result<Vec<GuestRegionMmap>, MemoryError> {
489493
let vhost_user_device_used = self
490494
.block
491495
.devices
@@ -501,22 +505,27 @@ impl VmResources {
501505
// because that would require running a backend process. If in the future we converge to
502506
// a single way of backing guest memory for vhost-user and non-vhost-user cases,
503507
// that would not be worth the effort.
504-
let regions =
505-
crate::arch::arch_memory_regions(mib_to_bytes(self.machine_config.mem_size_mib));
506508
if vhost_user_device_used {
507509
memory::memfd_backed(
508-
regions.as_ref(),
510+
regions,
509511
self.machine_config.track_dirty_pages,
510512
self.machine_config.huge_pages,
511513
)
512514
} else {
513515
memory::anonymous(
514-
regions.into_iter(),
516+
regions.iter().copied(),
515517
self.machine_config.track_dirty_pages,
516518
self.machine_config.huge_pages,
517519
)
518520
}
519521
}
522+
523+
/// Allocates guest memory in a configuration most appropriate for these [`VmResources`].
524+
pub fn allocate_guest_memory(&self) -> Result<Vec<GuestRegionMmap>, MemoryError> {
525+
let regions =
526+
crate::arch::arch_memory_regions(mib_to_bytes(self.machine_config.mem_size_mib));
527+
self.allocate_memory_regions(&regions)
528+
}
520529
}
521530

522531
impl From<&VmResources> for VmmConfig {

0 commit comments

Comments
 (0)