diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 50ad83b69..c790cf928 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -53,7 +53,9 @@ use vm_device::interrupt::{ }; use vm_device::{Migratable, MigratableError, Pausable, Snapshotable}; use vm_memory::guest_memory::FileOffset; -use vm_memory::{Address, GuestAddress, GuestAddressSpace, GuestUsize, MmapRegion}; +use vm_memory::{ + Address, GuestAddress, GuestAddressSpace, GuestRegionMmap, GuestUsize, MmapRegion, +}; #[cfg(feature = "pci_support")] use vm_virtio::transport::VirtioPciDevice; use vm_virtio::transport::VirtioTransport; @@ -1874,7 +1876,7 @@ impl DeviceManager { self.cmdline_additions.as_slice() } - pub fn update_memory(&self) -> DeviceManagerResult<()> { + pub fn update_memory(&self, _new_region: &Arc) -> DeviceManagerResult<()> { let memory = self.memory_manager.lock().unwrap().guest_memory(); for (virtio_device, _) in self.virtio_devices.iter() { virtio_device diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index d25f55783..77cfd833e 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -379,7 +379,7 @@ impl MemoryManager { Ok(()) } - fn hotplug_ram_region(&mut self, size: usize) -> Result<(), Error> { + fn hotplug_ram_region(&mut self, size: usize) -> Result, Error> { info!("Hotplugging new RAM: {}", size); // Check that there is a free slot @@ -434,9 +434,9 @@ impl MemoryManager { self.next_hotplug_slot += 1; - self.add_region(region)?; + self.add_region(Arc::clone(®ion))?; - Ok(()) + Ok(region) } pub fn guest_memory(&self) -> GuestMemoryAtomic { @@ -526,25 +526,28 @@ impl MemoryManager { Ok(()) } - pub fn resize(&mut self, desired_ram: u64) -> Result { - let mut resized = false; + /// In case this function resulted in adding a new memory region to the + /// guest memory, the new region is returned to the caller. The virtio-mem + /// use case never adds a new region as the whole hotpluggable memory has + /// already been allocated at boot time. + pub fn resize(&mut self, desired_ram: u64) -> Result>, Error> { + let mut region: Option> = None; match self.hotplug_method { HotplugMethod::VirtioMem => { if desired_ram >= self.boot_ram { self.virtiomem_resize(desired_ram - self.boot_ram)?; self.current_ram = desired_ram; - resized = true; } } HotplugMethod::Acpi => { if desired_ram >= self.current_ram { - self.hotplug_ram_region((desired_ram - self.current_ram) as usize)?; + region = + Some(self.hotplug_ram_region((desired_ram - self.current_ram) as usize)?); self.current_ram = desired_ram; - resized = true; } } } - Ok(resized) + Ok(region) } } diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index 7061fc224..00ed696f6 100755 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -606,17 +606,18 @@ impl Vm { } if let Some(desired_memory) = desired_memory { - if self + let new_region = self .memory_manager .lock() .unwrap() .resize(desired_memory) - .map_err(Error::MemoryManager)? - { + .map_err(Error::MemoryManager)?; + + if let Some(new_region) = &new_region { self.device_manager .lock() .unwrap() - .update_memory() + .update_memory(&new_region) .map_err(Error::DeviceManager)?; let memory_config = &self.config.lock().unwrap().memory; @@ -632,9 +633,9 @@ impl Vm { } } - // We update the VM config regardless of the actual guest resize operation - // result (true or false, happened or not), so that if the VM reboots it - // will be running with the last configure memory size. + // We update the VM config regardless of the actual guest resize + // operation result (happened or not), so that if the VM reboots + // it will be running with the last configure memory size. self.config.lock().unwrap().memory.size = desired_memory; } Ok(())