diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index e1de6bfeb..906960fde 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -66,12 +66,21 @@ struct HotPlugState { #[derive(Clone)] pub struct NumaNode { memory_regions: Vec>, + cpus: Vec, } impl NumaNode { pub fn memory_regions(&self) -> &Vec> { &self.memory_regions } + + pub fn cpus(&self) -> &Vec { + &self.cpus + } + + pub fn cpus_mut(&mut self) -> &mut Vec { + &mut self.cpus + } } pub type NumaNodes = BTreeMap; @@ -356,6 +365,7 @@ impl MemoryManager { node_id, NumaNode { memory_regions: vec![region.clone()], + cpus: Vec::new(), }, ); } @@ -1233,6 +1243,10 @@ impl MemoryManager { pub fn numa_nodes(&self) -> &NumaNodes { &self.numa_nodes } + + pub fn numa_nodes_mut(&mut self) -> &mut NumaNodes { + &mut self.numa_nodes + } } #[cfg(feature = "acpi")] diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index 16fb30b22..bf5d8dcc5 100644 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -24,8 +24,8 @@ extern crate vm_allocator; extern crate vm_memory; use crate::config::{ - DeviceConfig, DiskConfig, FsConfig, HotplugMethod, NetConfig, PmemConfig, ValidationError, - VmConfig, VsockConfig, + DeviceConfig, DiskConfig, FsConfig, HotplugMethod, NetConfig, NumaConfig, PmemConfig, + ValidationError, VmConfig, VsockConfig, }; use crate::cpu; use crate::device_manager::{self, get_win_size, Console, DeviceManager, DeviceManagerError}; @@ -311,6 +311,11 @@ impl Vm { .transpose() .map_err(Error::InitramfsFile)?; + // Update NUMA based on NumaConfig. + if let Some(numa_cfg) = config.lock().unwrap().numa.clone() { + Self::update_numa(numa_cfg, &memory_manager)?; + } + Ok(Vm { kernel, initramfs, @@ -328,6 +333,24 @@ impl Vm { }) } + fn update_numa( + configs: Vec, + memory_manager: &Arc>, + ) -> Result<()> { + let mut mm = memory_manager.lock().unwrap(); + let numa_nodes = mm.numa_nodes_mut(); + + for config in configs.iter() { + if let Some(cpus) = &config.cpus { + if let Some(node) = numa_nodes.get_mut(&config.id) { + node.cpus_mut().extend(cpus); + } + } + } + + Ok(()) + } + pub fn new( config: Arc>, exit_evt: EventFd,