Skip to content

Commit edfba77

Browse files
petri: gate nested reset on KVM nested-state support
Require KVM_CAP_NESTED_STATE before advertising nested_reset. Cover unsupported KVM hosts and preserve MSHV scrub gating. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Muminul Islam <muminul.islam@microsoft.com>
1 parent d94b4df commit edfba77

6 files changed

Lines changed: 70 additions & 3 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6560,6 +6560,7 @@ dependencies = [
65606560
"build_rs_guest_arch",
65616561
"fs-err",
65626562
"hypervisor_resources",
6563+
"kvm",
65636564
"test_with_tracing",
65646565
"virt_mshv",
65656566
"virt_whp",

‎Guide/src/dev_guide/tests/vmm.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ they use KVM when `/dev/kvm` is accessible. Use the same command on an MSHV
311311
host to validate that backend. The test requires both `nested_virt` and
312312
`nested_reset`. Petri probes partition scrub on a temporary MSHV partition
313313
and skips the test when the kernel does not implement the scrub ioctl.
314+
On KVM, Petri requires a positive `KVM_CHECK_EXTENSION(KVM_CAP_NESTED_STATE)`
315+
result before advertising `nested_reset`; otherwise the test is skipped.
314316
Other probe errors fail host discovery rather than silently skipping tests.
315317
A successful KVM run does not validate the MSHV reset path. Inspect
316318
the OpenVMM logs in the output directory to confirm the selected backend.

‎openvmm/openvmm_hypervisors/Cargo.toml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ rust-version.workspace = true
1111
virt_mshv = ["dep:virt_mshv"]
1212

1313
# Enable building with KVM support.
14-
virt_kvm = []
14+
virt_kvm = ["dep:kvm"]
1515

1616
# Enable building with WHP support.
1717
virt_whp = ["dep:virt_whp"]
@@ -26,6 +26,7 @@ hypervisor_resources.workspace = true
2626
vm_resource.workspace = true
2727

2828
[target.'cfg(target_os = "linux")'.dependencies]
29+
kvm = { workspace = true, optional = true }
2930
virt_mshv = { workspace = true, optional = true }
3031

3132
[target.'cfg(windows)'.dependencies]

‎openvmm/openvmm_hypervisors/src/lib.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,34 @@ pub fn mshv_snp_available() -> bool {
5454
}
5555
}
5656

57+
/// Returns whether KVM supports the nested-state ioctls on this machine.
58+
pub fn kvm_nested_state_available() -> anyhow::Result<bool> {
59+
#[cfg(all(
60+
target_os = "linux",
61+
feature = "virt_kvm",
62+
guest_is_native,
63+
guest_arch = "x86_64"
64+
))]
65+
{
66+
use anyhow::Context as _;
67+
68+
let kvm = ::kvm::Kvm::new().context("open KVM for nested-state probe")?;
69+
Ok(kvm
70+
.check_extension(::kvm::KVM_CAP_NESTED_STATE)
71+
.context("query KVM_CAP_NESTED_STATE")?
72+
> 0)
73+
}
74+
#[cfg(not(all(
75+
target_os = "linux",
76+
feature = "virt_kvm",
77+
guest_is_native,
78+
guest_arch = "x86_64"
79+
)))]
80+
{
81+
Ok(false)
82+
}
83+
}
84+
5785
/// Returns whether MSHV supports partition scrub on this machine.
5886
pub fn mshv_partition_scrub_available() -> anyhow::Result<bool> {
5987
#[cfg(all(target_os = "linux", feature = "virt_mshv", guest_is_native))]

‎petri/src/requirements.rs‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ pub struct HostContext {
111111
pub vpci_supported: bool,
112112
/// Hypervisor backend that OpenVMM will select on this host.
113113
pub openvmm_hypervisor: Option<OpenVmmHypervisor>,
114+
/// Whether KVM supports nested-state ioctls, required for nested reset.
115+
pub kvm_nested_state_supported: bool,
114116
/// Whether MSHV implements partition scrub, required for nested reset.
115117
pub mshv_partition_scrub_supported: bool,
116118
}
@@ -167,6 +169,13 @@ impl HostContext {
167169
None
168170
};
169171

172+
let kvm_nested_state_supported = if openvmm_hypervisor == Some(OpenVmmHypervisor::Kvm) {
173+
openvmm_hypervisors::kvm_nested_state_available()
174+
.expect("failed to probe KVM nested-state support")
175+
} else {
176+
false
177+
};
178+
170179
let mshv_partition_scrub_supported = if openvmm_hypervisor == Some(OpenVmmHypervisor::Mshv)
171180
{
172181
openvmm_hypervisors::mshv_partition_scrub_available()
@@ -213,6 +222,7 @@ impl HostContext {
213222
},
214223
vpci_supported,
215224
openvmm_hypervisor,
225+
kvm_nested_state_supported,
216226
mshv_partition_scrub_supported,
217227
}
218228
}
@@ -357,7 +367,8 @@ fn available_capabilities(context: &HostContext, vmm: VmmType) -> BTreeSet<&'sta
357367
}
358368

359369
if vmm == VmmType::OpenVmm
360-
&& (context.openvmm_hypervisor == Some(OpenVmmHypervisor::Kvm)
370+
&& ((context.openvmm_hypervisor == Some(OpenVmmHypervisor::Kvm)
371+
&& context.kvm_nested_state_supported)
361372
|| (context.openvmm_hypervisor == Some(OpenVmmHypervisor::Mshv)
362373
&& context.mshv_partition_scrub_supported))
363374
{
@@ -419,6 +430,7 @@ mod tests {
419430
execution_environment: ExecutionEnvironment::Baremetal,
420431
vpci_supported: false,
421432
openvmm_hypervisor: Some(openvmm_hypervisor),
433+
kvm_nested_state_supported: false,
422434
mshv_partition_scrub_supported: false,
423435
}
424436
}
@@ -458,7 +470,6 @@ mod tests {
458470
mshv.mshv_partition_scrub_supported = true;
459471
assert!(requirement.is_satisfied(&mshv));
460472

461-
assert!(requirement.is_satisfied(&host_context(OpenVmmHypervisor::Kvm)));
462473
assert!(!requirement.is_satisfied(&host_context(OpenVmmHypervisor::Whp)));
463474
assert!(!requirement.is_satisfied(&host_context(OpenVmmHypervisor::Hvf)));
464475
mshv.openvmm_hypervisor = None;
@@ -472,6 +483,29 @@ mod tests {
472483
);
473484
}
474485

486+
#[test]
487+
fn nested_reset_requires_kvm_nested_state() {
488+
let requirement = TestRequirement::RequiresCapability {
489+
name: capabilities::NESTED_RESET,
490+
vmm: VmmType::OpenVmm,
491+
};
492+
let mut kvm = host_context(OpenVmmHypervisor::Kvm);
493+
assert!(
494+
TestRequirement::RequiresCapability {
495+
name: capabilities::NESTED_VIRT,
496+
vmm: VmmType::OpenVmm,
497+
}
498+
.is_satisfied(&kvm)
499+
);
500+
assert!(!requirement.is_satisfied(&kvm));
501+
kvm.kvm_nested_state_supported = true;
502+
assert!(requirement.is_satisfied(&kvm));
503+
kvm.openvmm_hypervisor = Some(OpenVmmHypervisor::Mshv);
504+
assert!(!requirement.is_satisfied(&kvm));
505+
kvm.openvmm_hypervisor = None;
506+
assert!(!requirement.is_satisfied(&kvm));
507+
}
508+
475509
#[test]
476510
fn openvmm_hypervisor_requirement_matches_the_selected_backend() {
477511
let requirement = TestRequirement::OpenVmmHypervisor(OpenVmmHypervisor::Mshv);

‎petri/src/test.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ mod tests {
694694
execution_environment: ExecutionEnvironment::Baremetal,
695695
vpci_supported: false,
696696
openvmm_hypervisor: hypervisor,
697+
kvm_nested_state_supported: false,
697698
mshv_partition_scrub_supported: false,
698699
}
699700
}

0 commit comments

Comments
 (0)