vmm, virtio-devices: Move BlocksState creation to the MemoryManager

By creating the BlocksState object in the MemoryManager, we can directly
provide it to the virtio-mem device when being created. This will allow
the MemoryManager through each VirtioMemZone to have a handle onto the
blocks that are plugged at any point in time.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2021-09-23 10:42:00 +02:00 committed by Bo Chen
parent 4450c44fbc
commit e390775bcb
3 changed files with 17 additions and 2 deletions

View File

@ -393,7 +393,7 @@ pub struct BlocksState {
} }
impl BlocksState { impl BlocksState {
fn new(region_size: u64) -> Self { pub fn new(region_size: u64) -> Self {
BlocksState { BlocksState {
bitmap: vec![false; (region_size / VIRTIO_MEM_DEFAULT_BLOCK_SIZE) as usize], bitmap: vec![false; (region_size / VIRTIO_MEM_DEFAULT_BLOCK_SIZE) as usize],
} }
@ -810,6 +810,7 @@ impl Mem {
initial_size: u64, initial_size: u64,
hugepages: bool, hugepages: bool,
exit_evt: EventFd, exit_evt: EventFd,
blocks_state: Arc<Mutex<BlocksState>>,
) -> io::Result<Mem> { ) -> io::Result<Mem> {
let region_len = region.len(); let region_len = region.len();
@ -882,7 +883,7 @@ impl Mem {
seccomp_action, seccomp_action,
hugepages, hugepages,
dma_mapping_handlers: Arc::new(Mutex::new(BTreeMap::new())), dma_mapping_handlers: Arc::new(Mutex::new(BTreeMap::new())),
blocks_state: Arc::new(Mutex::new(BlocksState::new(config.region_size))), blocks_state,
exit_evt, exit_evt,
}) })
} }

View File

@ -2694,6 +2694,7 @@ impl DeviceManager {
self.exit_evt self.exit_evt
.try_clone() .try_clone()
.map_err(DeviceManagerError::EventFd)?, .map_err(DeviceManagerError::EventFd)?,
virtio_mem_zone.blocks_state().clone(),
) )
.map_err(DeviceManagerError::CreateVirtioMem)?, .map_err(DeviceManagerError::CreateVirtioMem)?,
)); ));

View File

@ -30,6 +30,7 @@ use std::result;
use std::sync::{Arc, Barrier, Mutex}; use std::sync::{Arc, Barrier, Mutex};
use versionize::{VersionMap, Versionize, VersionizeResult}; use versionize::{VersionMap, Versionize, VersionizeResult};
use versionize_derive::Versionize; use versionize_derive::Versionize;
use virtio_devices::BlocksState;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
use vm_allocator::GsiApic; use vm_allocator::GsiApic;
use vm_allocator::SystemAllocator; use vm_allocator::SystemAllocator;
@ -73,6 +74,7 @@ pub struct VirtioMemZone {
resize_handler: virtio_devices::Resize, resize_handler: virtio_devices::Resize,
hotplugged_size: u64, hotplugged_size: u64,
hugepages: bool, hugepages: bool,
blocks_state: Arc<Mutex<BlocksState>>,
} }
impl VirtioMemZone { impl VirtioMemZone {
@ -88,6 +90,15 @@ impl VirtioMemZone {
pub fn hugepages(&self) -> bool { pub fn hugepages(&self) -> bool {
self.hugepages self.hugepages
} }
pub fn blocks_state(&self) -> &Arc<Mutex<BlocksState>> {
&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)] #[derive(Default)]
@ -699,12 +710,14 @@ impl MemoryManager {
virtio_mem_regions.push(region.clone()); virtio_mem_regions.push(region.clone());
let hotplugged_size = zone.hotplugged_size.unwrap_or(0); let hotplugged_size = zone.hotplugged_size.unwrap_or(0);
let region_size = region.len();
memory_zone.virtio_mem_zone = Some(VirtioMemZone { memory_zone.virtio_mem_zone = Some(VirtioMemZone {
region, region,
resize_handler: virtio_devices::Resize::new(hotplugged_size) resize_handler: virtio_devices::Resize::new(hotplugged_size)
.map_err(Error::EventFdFail)?, .map_err(Error::EventFdFail)?,
hotplugged_size, hotplugged_size,
hugepages: zone.hugepages, hugepages: zone.hugepages,
blocks_state: Arc::new(Mutex::new(BlocksState::new(region_size))),
}); });
start_of_device_area = start_addr start_of_device_area = start_addr