Skip to content

Commit acab782

Browse files
committed
refactor: move check for not exceeding memslot limit into struct Vm
This is to prepare for memory regions being registered to a VM incrementally. Signed-off-by: Patrick Roy <[email protected]>
1 parent b48ee01 commit acab782

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

src/vmm/src/builder.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ fn create_vmm_and_vcpus(
163163
// Set up Kvm Vm and register memory regions.
164164
// Build custom CPU config if a custom template is provided.
165165
let mut vm = Vm::new(&kvm)?;
166-
kvm.check_memory(&guest_memory)?;
167166
vm.memory_init(&guest_memory)?;
168167

169168
let resource_allocator = ResourceAllocator::new()?;

src/vmm/src/vstate/kvm.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use serde::{Deserialize, Serialize};
1010
#[cfg(target_arch = "x86_64")]
1111
use crate::arch::x86_64::xstate::{XstateError, request_dynamic_xstate_features};
1212
use crate::cpu_config::templates::KvmCapability;
13-
use crate::vstate::memory::{GuestMemory, GuestMemoryMmap};
1413

1514
/// Errors associated with the wrappers over KVM ioctls.
1615
/// Needs `rustfmt::skip` to make multiline comments work
@@ -27,8 +26,6 @@ pub enum KvmError {
2726
#[cfg(target_arch = "x86_64")]
2827
/// Failed to get supported cpuid: {0}
2928
GetSupportedCpuId(kvm_ioctls::Error),
30-
/// The number of configured slots is bigger than the maximum reported by KVM
31-
NotEnoughMemorySlots,
3229
#[cfg(target_arch = "x86_64")]
3330
/// Failed to request permission for dynamic XSTATE features: {0}
3431
XstateFeatures(XstateError),
@@ -39,8 +36,6 @@ pub enum KvmError {
3936
pub struct Kvm {
4037
/// KVM fd.
4138
pub fd: KvmFd,
42-
/// Maximum number of memory slots allowed by KVM.
43-
pub max_memslots: usize,
4439
/// Additional capabilities that were specified in cpu template.
4540
pub kvm_cap_modifiers: Vec<KvmCapability>,
4641

@@ -65,13 +60,10 @@ impl Kvm {
6560
// Check that all desired capabilities are supported.
6661
Self::check_capabilities(&kvm_fd, &total_caps).map_err(KvmError::Capabilities)?;
6762

68-
let max_memslots = kvm_fd.get_nr_memslots();
69-
7063
#[cfg(target_arch = "aarch64")]
7164
{
7265
Ok(Self {
7366
fd: kvm_fd,
74-
max_memslots,
7567
kvm_cap_modifiers,
7668
})
7769
}
@@ -86,7 +78,6 @@ impl Kvm {
8678

8779
Ok(Kvm {
8880
fd: kvm_fd,
89-
max_memslots,
9081
kvm_cap_modifiers,
9182
supported_cpuid,
9283
})
@@ -99,15 +90,6 @@ impl Kvm {
9990
crate::arch::x86_64::msr::get_msrs_to_save(&self.fd)
10091
}
10192

102-
/// Check guest memory does not have more regions than kvm allows.
103-
pub fn check_memory(&self, guest_mem: &GuestMemoryMmap) -> Result<(), KvmError> {
104-
if guest_mem.num_regions() > self.max_memslots {
105-
Err(KvmError::NotEnoughMemorySlots)
106-
} else {
107-
Ok(())
108-
}
109-
}
110-
11193
fn combine_capabilities(kvm_cap_modifiers: &[KvmCapability]) -> Vec<u32> {
11294
let mut total_caps = Self::DEFAULT_CAPABILITIES.to_vec();
11395
for modifier in kvm_cap_modifiers.iter() {
@@ -143,6 +125,11 @@ impl Kvm {
143125
kvm_cap_modifiers: self.kvm_cap_modifiers.clone(),
144126
}
145127
}
128+
129+
/// Returns the maximal number of memslots allowed in a [`Vm`]
130+
pub fn max_nr_memslots(&self) -> usize {
131+
self.fd.get_nr_memslots()
132+
}
146133
}
147134
#[cfg(target_arch = "aarch64")]
148135
/// Optional capabilities.

src/vmm/src/vstate/vm/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::vstate::vcpu::VcpuError;
2727
#[derive(Debug)]
2828
struct VmCommon {
2929
fd: VmFd,
30+
max_memslots: usize,
3031
}
3132

3233
/// Errors associated with the wrappers over KVM ioctls.
@@ -44,6 +45,8 @@ pub enum VmError {
4445
EventFd(std::io::Error),
4546
/// Failed to create vcpu: {0}
4647
CreateVcpu(VcpuError),
48+
/// The number of configured slots is bigger than the maximum reported by KVM
49+
NotEnoughMemorySlots,
4750
}
4851

4952
/// Contains Vm functions that are usable across CPU architectures
@@ -85,7 +88,10 @@ impl Vm {
8588
attempt += 1;
8689
};
8790

88-
Ok(VmCommon { fd })
91+
Ok(VmCommon {
92+
fd,
93+
max_memslots: kvm.max_nr_memslots(),
94+
})
8995
}
9096

9197
/// Creates the specified number of [`Vcpu`]s.
@@ -110,6 +116,10 @@ impl Vm {
110116

111117
/// Initializes the guest memory.
112118
pub fn memory_init(&self, guest_mem: &GuestMemoryMmap) -> Result<(), VmError> {
119+
if guest_mem.num_regions() > self.common.max_memslots {
120+
return Err(VmError::NotEnoughMemorySlots);
121+
}
122+
113123
self.set_kvm_memory_regions(guest_mem)
114124
}
115125

0 commit comments

Comments
 (0)