From 63c6c78c4efedd717a68299d76ec43e0bbeec567 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 4 Oct 2021 14:47:40 +0200 Subject: [PATCH] vmm: memory_manager: Factorize configuration validation In order to simplify MemoryManager::new() function. let's move the memory configuration validation to its own function. Signed-off-by: Sebastien Boeuf --- vmm/src/memory_manager.rs | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index 69369803e..dc65e0f83 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -544,17 +544,13 @@ impl MemoryManager { Ok(()) } - pub fn new( - vm: Arc, + pub fn validate_memory_config( config: &MemoryConfig, - prefault: Option, - phys_bits: u8, - #[cfg(feature = "tdx")] tdx_enabled: bool, - ) -> Result>, Error> { - let user_provided_zones = config.size == 0; - let mut allow_mem_hotplug: bool = false; + user_provided_zones: bool, + ) -> Result<(u64, Vec, bool), Error> { + let mut allow_mem_hotplug = false; - let (ram_size, zones) = if !user_provided_zones { + if !user_provided_zones { if config.zones.is_some() { error!( "User defined memory regions can't be provided if the \ @@ -608,7 +604,7 @@ impl MemoryManager { prefault: config.prefault, }]; - (config.size, zones) + Ok((config.size, zones, allow_mem_hotplug)) } else { if config.zones.is_none() { error!( @@ -669,8 +665,21 @@ impl MemoryManager { } } - (total_ram_size, zones) - }; + Ok((total_ram_size, zones, allow_mem_hotplug)) + } + } + + pub fn new( + vm: Arc, + config: &MemoryConfig, + prefault: Option, + phys_bits: u8, + #[cfg(feature = "tdx")] tdx_enabled: bool, + ) -> Result>, Error> { + let user_provided_zones = config.size == 0; + + let (ram_size, zones, allow_mem_hotplug) = + Self::validate_memory_config(config, user_provided_zones)?; // Init guest memory let arch_mem_regions = arch::arch_memory_regions(ram_size);