Skip to content

Commit 122a07c

Browse files
committed
refactor(resources): add method to create a single region
Split the actual allocation from the calculations to find the right ranges, and add a new function to create a single region. This will be used to create the hotpluggable region in the future. Signed-off-by: Riccardo Mancini <[email protected]>
1 parent 2119128 commit 122a07c

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/vmm/src/resources.rs

Lines changed: 27 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;
@@ -463,11 +464,14 @@ impl VmResources {
463464
Ok(())
464465
}
465466

466-
/// Allocates guest memory in a configuration most appropriate for these [`VmResources`].
467+
/// Allocates the given guest memory regions.
467468
///
468469
/// If vhost-user-blk devices are in use, allocates memfd-backed shared memory, otherwise
469470
/// prefers anonymous memory for performance reasons.
470-
pub fn allocate_guest_memory(&self) -> Result<Vec<GuestRegionMmap>, MemoryError> {
471+
fn allocate_memory_regions(
472+
&self,
473+
regions: &[(GuestAddress, usize)],
474+
) -> Result<Vec<GuestRegionMmap>, MemoryError> {
471475
let vhost_user_device_used = self
472476
.block
473477
.devices
@@ -483,22 +487,39 @@ impl VmResources {
483487
// because that would require running a backend process. If in the future we converge to
484488
// a single way of backing guest memory for vhost-user and non-vhost-user cases,
485489
// that would not be worth the effort.
486-
let regions =
487-
crate::arch::arch_memory_regions(mib_to_bytes(self.machine_config.mem_size_mib));
488490
if vhost_user_device_used {
489491
memory::memfd_backed(
490-
regions.as_ref(),
492+
regions,
491493
self.machine_config.track_dirty_pages,
492494
self.machine_config.huge_pages,
493495
)
494496
} else {
495497
memory::anonymous(
496-
regions.into_iter(),
498+
regions.iter().copied(),
497499
self.machine_config.track_dirty_pages,
498500
self.machine_config.huge_pages,
499501
)
500502
}
501503
}
504+
505+
/// Allocates guest memory in a configuration most appropriate for these [`VmResources`].
506+
pub fn allocate_guest_memory(&self) -> Result<Vec<GuestRegionMmap>, MemoryError> {
507+
let regions =
508+
crate::arch::arch_memory_regions(mib_to_bytes(self.machine_config.mem_size_mib));
509+
self.allocate_memory_regions(&regions)
510+
}
511+
512+
/// Allocates a single guest memory region.
513+
pub fn allocate_memory_region(
514+
&self,
515+
start: GuestAddress,
516+
size: usize,
517+
) -> Result<GuestRegionMmap, MemoryError> {
518+
Ok(self
519+
.allocate_memory_regions(&[(start, size)])?
520+
.pop()
521+
.unwrap())
522+
}
502523
}
503524

504525
impl From<&VmResources> for VmmConfig {

0 commit comments

Comments
 (0)