diff --git a/virtio-devices/src/mem.rs b/virtio-devices/src/mem.rs index dee35b0e3..7ef5f6885 100644 --- a/virtio-devices/src/mem.rs +++ b/virtio-devices/src/mem.rs @@ -393,7 +393,7 @@ pub struct BlocksState { } impl BlocksState { - fn new(region_size: u64) -> Self { + pub fn new(region_size: u64) -> Self { BlocksState { bitmap: vec![false; (region_size / VIRTIO_MEM_DEFAULT_BLOCK_SIZE) as usize], } @@ -810,6 +810,7 @@ impl Mem { initial_size: u64, hugepages: bool, exit_evt: EventFd, + blocks_state: Arc>, ) -> io::Result { let region_len = region.len(); @@ -882,7 +883,7 @@ impl Mem { seccomp_action, hugepages, dma_mapping_handlers: Arc::new(Mutex::new(BTreeMap::new())), - blocks_state: Arc::new(Mutex::new(BlocksState::new(config.region_size))), + blocks_state, exit_evt, }) } diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index b3c9d5539..12909bc74 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -2694,6 +2694,7 @@ impl DeviceManager { self.exit_evt .try_clone() .map_err(DeviceManagerError::EventFd)?, + virtio_mem_zone.blocks_state().clone(), ) .map_err(DeviceManagerError::CreateVirtioMem)?, )); diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index 758febc51..6d34b202b 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -30,6 +30,7 @@ use std::result; use std::sync::{Arc, Barrier, Mutex}; use versionize::{VersionMap, Versionize, VersionizeResult}; use versionize_derive::Versionize; +use virtio_devices::BlocksState; #[cfg(target_arch = "x86_64")] use vm_allocator::GsiApic; use vm_allocator::SystemAllocator; @@ -73,6 +74,7 @@ pub struct VirtioMemZone { resize_handler: virtio_devices::Resize, hotplugged_size: u64, hugepages: bool, + blocks_state: Arc>, } impl VirtioMemZone { @@ -88,6 +90,15 @@ impl VirtioMemZone { pub fn hugepages(&self) -> bool { self.hugepages } + pub fn blocks_state(&self) -> &Arc> { + &self.blocks_state + } + pub fn plugged_ranges(&self) -> MemoryRangeTable { + self.blocks_state + .lock() + .unwrap() + .memory_ranges(self.region.start_addr().raw_value(), true) + } } #[derive(Default)] @@ -699,12 +710,14 @@ impl MemoryManager { virtio_mem_regions.push(region.clone()); let hotplugged_size = zone.hotplugged_size.unwrap_or(0); + let region_size = region.len(); memory_zone.virtio_mem_zone = Some(VirtioMemZone { region, resize_handler: virtio_devices::Resize::new(hotplugged_size) .map_err(Error::EventFdFail)?, hotplugged_size, hugepages: zone.hugepages, + blocks_state: Arc::new(Mutex::new(BlocksState::new(region_size))), }); start_of_device_area = start_addr