From 2ad8fac62409be435a4d68c9879f93a1680ecc22 Mon Sep 17 00:00:00 2001 From: Yuhong Zhong Date: Wed, 18 Sep 2024 12:42:32 -0500 Subject: [PATCH] vmm: memory_manager: Fix bound checks for memory hotplug Bound checks for virtio-mem and ACPI memory hotplug are off by one and two, respectively. This prevents users to fully use the reserved memory hotplug size. For ACPI, if we specific `--memory size=2G,hotplug_size=4G` and run `ch-remote resize --memory 6G`, cloud-hypervisor will report the following error because of the incorrect bound check: ` ERROR:vmm/src/lib.rs:1631 -- Error when resizing VM: MemoryManager(InsufficientHotplugRam)` Similarly, for virtio-mem, cloud-hypervisor will fail the incorrect bound check and abort the resize. The VM will see the following error in dmesg: `virtio_mem virtio3: unknown error, marking device broken: -22` This patch has fixed both bound checks and ensure that users can hot add memory up to the reserved hotplug size. Signed-off-by: Yuhong Zhong --- virtio-devices/src/mem.rs | 2 +- vmm/src/memory_manager.rs | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/virtio-devices/src/mem.rs b/virtio-devices/src/mem.rs index 434401377..9ad21bd90 100644 --- a/virtio-devices/src/mem.rs +++ b/virtio-devices/src/mem.rs @@ -270,7 +270,7 @@ impl VirtioMemConfig { // in the usable region. if addr % self.block_size != 0 || size == 0 - || (addr < self.addr || addr + size >= self.addr + self.usable_region_size) + || (addr < self.addr || addr + size > self.addr + self.usable_region_size) { return false; } diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index ecbfe8817..798f641ef 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -1675,7 +1675,11 @@ impl MemoryManager { let start_addr = MemoryManager::start_addr(self.guest_memory.memory().last_addr(), true)?; - if start_addr.checked_add(size.try_into().unwrap()).unwrap() >= self.end_of_ram_area { + if start_addr + .checked_add((size - 1).try_into().unwrap()) + .unwrap() + > self.end_of_ram_area + { return Err(Error::InsufficientHotplugRam); }