KVM: x86: Honor EFER_LMSLE_MBZ
Subsystems (5)
Sean Christopherson
maintainerv3 cover KVM: x86: Honor EFER_LMSLE_MBZ
Jim's series to try to clean up the EFER.LMSLE_MBZ mess (and what a mess it is). I didn't touch the KVM patches, and barely touched the selftests, i.e. Jim's testing from v2 should still hold.
v3: Slot in a patch to add kvm_is_nested_virtualization_enabled() instead of
open coding the check and relying on the X86_FEATURE_SVM cheak to make sure it's kvm_amd.ko under the hood (the patch is constructed so that it will trigger a noisy conflict with the in-flight host_cpu_is_intel_compatible).
v2: https://lore.kernel.org/all/[email protected]
v1: https://lore.kernel.org/all/[email protected]
Jim Mattson (4):
KVM: x86: Advertise EFER_LMSLE_MBZ when KVM disallows EFER.LMSLE
KVM: x86: Honor the guest's EFER_LMSLE_MBZ
KVM: selftests: Rename svm_nested_clear_efer_svme to
svm_nested_efer_test
KVM: selftests: Add coverage for the EFER_LMSLE_MBZ defeature
Sean Christopherson (1):
KVM: selftests: Add module param API to check if nested virtualization
is enabled
Documentation/virt/kvm/api.rst | 25 ++ arch/x86/kvm/cpuid.c | 20 ++ arch/x86/kvm/msrs.c | 12 +- arch/x86/kvm/svm/svm.c | 21 +- arch/x86/kvm/vmx/vmx.c | 7 + arch/x86/kvm/x86.c | 9 +- tools/testing/selftests/kvm/Makefile.kvm | 2 +- .../selftests/kvm/include/x86/processor.h | 32 ++- .../kvm/x86/svm_nested_clear_efer_svme.c | 50 ---- .../selftests/kvm/x86/svm_nested_efer_test.c | 266 ++++++++++++++++++ 10 files changed, 386 insertions(+), 58 deletions(-) delete mode 100644 tools/testing/selftests/kvm/x86/svm_nested_clear_efer_svme.c create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_efer_test.c
base-commit: d4b7fb647204f0c81dfeae2d1a708e4d858e0c94
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v3 1/5 KVM: x86: Advertise EFER_LMSLE_MBZ when KVM disallows EFER.LMSLE
From: Jim Mattson <[email protected]>
Set EFER_LMSLE_MBZ, CPUID.80000008H:EBX[bit 20], in KVM's supported CPUID whenever KVM refuses to set EFER.LMSLE, not just when hardware enumerates the defeature. KVM allows EFER.LMSLE if and only if nested SVM is supported, but passes through hardware's EFER_LMSLE_MBZ as-is, i.e. KVM tells userspace that long mode segment limits are available on an Intel CPU, and on an AMD CPU with kvm_amd.nested=0, and then rejects WRMSR(EFER) with EFER.LMSLE=1. The bit means precisely "EFER.LMSLE must be zero."
Note, this is purely an enumeration change. EFER.LMSLE is already gated on nested SVM: kvm_setup_efer_caps() adds EFER_LMSLE to supported_efer_bits only if X86_FEATURE_SVM is supported, and svm_set_cpu_caps() sets X86_FEATURE_SVM if and only if "nested" is true. KVM's handling of EFER is unchanged; only what KVM tells userspace changes.
Note #2, KVM now sets the defeature on Intel CPUs as well. That is no different than KVM setting NullSelectorClearsBase, another AMD-defined bit, on CPUs that don't have the errata. And LMSLE's dependency on nested SVM is a historical artifact of commit eec4b140c924 ("KVM: SVM: Allow EFER.LMSLE to be set with nested svm"), not an architectural requirement.
Be aware that this changes KVM_GET_SUPPORTED_CPUID on all Intel hosts and on AMD hosts with kvm_amd.nested=0, i.e. userspace that reflects KVM's supported CPUID into the guest will start enumerating EFER_LMSLE_MBZ=1 to newly created guests. That's the correct enumeration, and it takes away only an EFER bit that KVM has never allowed to be set on such hosts, but it *is* guest visible. Deliberately omit a Fixes: tag: the misenumeration is benign in practice, as KVM's WRMSR(EFER) behavior is and always has been correct, and backporting a guest-visible CPUID change isn't worth the risk.
Opportunistically key the EFER_LMSLE decision off kvm_cpu_cap_has() instead of boot_cpu_has(), as PASSTHROUGH_F() sets EFER_LMSLE_MBZ based on *raw* CPUID, i.e. "clearcpuid=13:20" would otherwise get KVM to enumerate the defeature and allow EFER.LMSLE=1, which is guaranteed to fail on VMRUN.
Document KVM's enumeration of the defeature in api.rst.
--- Documentation/virt/kvm/api.rst | 11 +++++++++++ arch/x86/kvm/cpuid.c | 4 ++++ arch/x86/kvm/svm/svm.c | 10 ++++++++++ arch/x86/kvm/vmx/vmx.c | 7 +++++++ arch/x86/kvm/x86.c | 9 ++++++++- 5 files changed, 40 insertions(+), 1 deletion(-)
Documentation/virt/kvm/api.rst +11 -0
@@ -9635,6 +9635,17 @@ On older versions of Linux, CPU[EAX=1]:ECX[24] (TSC_DEADLINE) is not reported by is present and the kernel has enabled in-kernel emulation of the local APIC. On newer versions, ``KVM_GET_SUPPORTED_CPUID`` does report the bit as available. +Long mode segment limits +~~~~~~~~~~~~~~~~~~~~~~~~ + +CPU[EAX=0x80000008]:EBX[20] (EFER_LMSLE_MBZ) is a "defeature" bit: it is set +when the CPU does *not* support long mode segment limits, and so requires +EFER.LMSLE to be zero. KVM reports the bit via ``KVM_GET_SUPPORTED_CPUID`` if +and only if KVM refuses to set EFER.LMSLE, i.e. if the CPU doesn't support long +mode segment limits, or if nested SVM is unsupported. KVM therefore reports +the bit on all Intel hosts, as KVM allows EFER.LMSLE only when nested SVM is +enabled. + CPU topology ~~~~~~~~~~~~
arch/x86/kvm/cpuid.c +4 -0
@@ -1171,6 +1171,10 @@ void kvm_initialize_cpu_caps(void)F (AMD_STIBP ),F (AMD_STIBP_ALWAYS_ON ),F (AMD_IBRS_SAME_MODE ), +/* +* Vendor code also sets EFER_LMSLE_MBZ if KVM itself +* can't support EFER.LMSLE, e.g. if nested SVM is disabled. +*/ PASSTHROUGH_F (EFER_LMSLE_MBZ ),F (AMD_PSFD ), F(AMD_IBPB_RET),
arch/x86/kvm/svm/svm.c +10 -0
@@ -5571,6 +5571,16 @@ static __init void svm_set_cpu_caps(void) boot_cpu_has(X86_FEATURE_AMD_SSBD))kvm_cpu_cap_set (X86_FEATURE_VIRT_SSBD ); +/* +* Tell userspace that EFER.LMSLE must be zero if nested SVM is +* disabled, as KVM allows EFER.LMSLE if and only if nested SVM is +* supported (a historical artifact of commit eec4b140c924 ("KVM: SVM: +* Allow EFER.LMSLE to be set with nested svm"), not an architectural +* requirement). +*/ +if (!nested) +kvm_cpu_cap_set (X86_FEATURE_EFER_LMSLE_MBZ ); +if (enable_pmu) {/* * Enumerate support for PERFCTR_CORE if and only if KVM has
arch/x86/kvm/vmx/vmx.c +7 -0
@@ -8137,6 +8137,13 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_clear (X86_FEATURE_IBT ); } +/* +* CPUID 0x80000008. Tell userspace that EFER.LMSLE must be zero; KVM +* never allows EFER.LMSLE to be set on Intel CPUs, as KVM supports long +* mode segment limits only in conjunction with nested SVM. +*/ +kvm_cpu_cap_set (X86_FEATURE_EFER_LMSLE_MBZ ); +kvm_setup_xss_caps ();kvm_finalize_cpu_caps (); }
arch/x86/kvm/x86.c +8 -1
@@ -6930,7 +6930,14 @@ static void kvm_setup_efer_caps(void)if (kvm_cpu_cap_has (X86_FEATURE_SVM )) { kvm_caps.supported_efer_bits |=EFER_SVME ; -if (!boot_cpu_has (X86_FEATURE_EFER_LMSLE_MBZ )) + +/* +* Enumerating EFER_LMSLE_MBZ and allowing EFER.LMSLE=1 +* would be nonsensical. Note, vendor code sets the defeature +* if KVM can't support EFER.LMSLE for any reason, i.e. this +* needs to consult KVM's capabilities, not just raw CPUID. +*/ +if (!kvm_cpu_cap_has (X86_FEATURE_EFER_LMSLE_MBZ )) kvm_caps.supported_efer_bits |=EFER_LMSLE ; } }
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v3 2/5 KVM: x86: Honor the guest's EFER_LMSLE_MBZ
In reply to Sean ChristophersonFrom: Jim Mattson <[email protected]>
Reject guest attempts to set EFER.LMSLE if userspace enumerates EFER_LMSLE_MBZ, CPUID.80000008H:EBX[bit 20], in guest CPUID, so that userspace can hide long mode segment limits from the guest even on CPUs that support LMSLE, e.g. for migration compatibility between Rome and Milan.
Enumerate the defeature as *partially* emulated, i.e. as supported by KVM but never advertised, so that kvm_vcpu_after_set_cpuid() propagates userspace's bit into vcpu->arch.cpu_caps. Checking guest_cpu_cap_has() in __kvm_valid_efer() doesn't suffice on its own, because KVM clears the defeature in kvm_cpu_caps precisely on the CPUs where it needs to be emulated, i.e. cpu_caps would never hold the bit no matter what userspace puts in guest CPUID. Don't advertise the defeature in KVM_GET_SUPPORTED_CPUID, as that would take EFER.LMSLE away from userspace that blindly stuffs KVM's supported CPUID into KVM_SET_CPUID2. MONITOR/MWAIT gets the same treatment.
Enforce the defeature only for guest-initiated writes, as it's a guest CPUID consistency check, not a host capability. See commit 11988499e62b ("KVM: x86:
Skip EFER vs. guest CPUID checks for host-initiated writes"). Note, KVM_SET_SREGS does reject EFER.LMSLE, as it runs the full set of guest CPUID checks, as does nested VMRUN, i.e. L1 can't sneak EFER.LMSLE into L2 via vmcb12.
Mask EFER_LMSLE out of the value consumed by efer_trap(), which handles SVM_EXIT_EFER_WRITE_TRAP. EFER writes are *trapped*, not intercepted, i.e. hardware has already committed the write by the time KVM gains control, and the trap is enabled if and only if the guest is SEV-ES, whose EFER lives in the encrypted VMSA and so can't be fixed up by KVM. Rejecting the write would inject a #GP *and* leave EFER.LMSLE set in the guest, which is strictly worse than honoring a write that hardware itself allowed. EFER_SVME is already masked out for a similar "KVM can't enforce this here" reason.
Opportunistically fix the whitespace damage around __kvm_valid_efer().
Document the resulting two-level contract in api.rst, i.e. that userspace may set the defeature even when KVM doesn't enumerate it.
--- Documentation/virt/kvm/api.rst | 14 ++++++++++++++ arch/x86/kvm/cpuid.c | 16 ++++++++++++++++ arch/x86/kvm/msrs.c | 12 +++++++++++- arch/x86/kvm/svm/svm.c | 11 ++++++++++- 4 files changed, 51 insertions(+), 2 deletions(-)
Documentation/virt/kvm/api.rst +14 -0
@@ -9646,6 +9646,20 @@ mode segment limits, or if nested SVM is unsupported. KVM therefore reports the bit on all Intel hosts, as KVM allows EFER.LMSLE only when nested SVM is enabled. +KVM never reports the bit via ``KVM_GET_EMULATED_CPUID``, but userspace may set +it via ``KVM_SET_CPUID2`` even on a host where KVM doesn't report it. KVM +honors the guest's enumeration and rejects EFER.LMSLE=1 accordingly. That lets +userspace defeature a vCPU on a host that *does* support long mode segment +limits, so that the vCPU can later be migrated to a host that doesn't, e.g. so +that a vCPU created on AMD Rome can be migrated to Milan and later, which +dropped support for long mode segment limits. The opposite direction needs no +emulation, as a host that lacks long mode segment limits already enumerates the +defeature. + +Note, ``KVM_SET_MSRS`` is exempt from the check, as host-initiated MSR writes +skip guest CPUID checks so that userspace can set MSRs before it sets guest +CPUID. ``KVM_SET_SREGS`` and nested VMRUN are not exempt. + CPU topology ~~~~~~~~~~~~
arch/x86/kvm/cpuid.c +16 -0
@@ -1419,6 +1419,22 @@ static int cpuid_func_emulated(struct kvm_cpuid_entry2 *entry, u32 func, u32 indif (kvm_cpu_cap_has (X86_FEATURE_RDTSCP )) entry->ecx =feature_bit (RDPID );return 1 ; +case 0x80000008 : +/* +* Honor the guest's EFER_LMSLE_MBZ even if the underlying CPU +* allows setting EFER.LMSLE, e.g. to allow migrating a vCPU +* between hosts with and without EFER.LMSLE support. To avoid +* breaking existing setups that reflect KVM's supported CPUID +* into the guest, KVM doesn't advertise EFER_LMSLE_MBZ unless +* KVM *can't* support EFER.LMSLE=1. +*/ +if (include_partially_emulated && + !kvm_cpu_cap_has (X86_FEATURE_EFER_LMSLE_MBZ )) { + entry->ebx |=feature_bit (EFER_LMSLE_MBZ ); +return 1 ; + } +/* Nothing in 0x80000008 is fully emulated, don't emit an entry. */ +return 0 ;default :return 0 ; }
arch/x86/kvm/msrs.c +11 -1
@@ -598,9 +598,19 @@ static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)if (efer &EFER_NX && !guest_cpu_cap_has (vcpu,X86_FEATURE_NX ))return false; +/* +* EFER_LMSLE_MBZ is a "defeature" bit, i.e. is set when the CPU does +* *not* support long mode segment limits, and so is the only EFER +* check whose polarity is inverted: EFER.LMSLE is legal if and only if +* the guest does *not* have the defeature. +*/ +if (efer &EFER_LMSLE && +guest_cpu_cap_has (vcpu,X86_FEATURE_EFER_LMSLE_MBZ )) +return false; +return true; - } +bool kvm_valid_efer (struct kvm_vcpu *vcpu,u64 efer) {if (efer & ~kvm_caps.supported_efer_bits)
arch/x86/kvm/svm/svm.c +10 -1
@@ -2759,10 +2759,19 @@ static int efer_trap(struct kvm_vcpu *vcpu)* bit in svm_set_efer(), but __kvm_valid_efer() checks it against * whether the guest has X86_FEATURE_SVM - this avoids a failure if * the guest doesn't have X86_FEATURE_SVM. +* +* Clear EFER_LMSLE for a related reason: EFER writes are *trapped*, +* not intercepted, i.e. hardware has already committed the write by +* the time KVM gains control, and the trap is enabled if and only if +* the guest is SEV-ES, whose EFER lives in the encrypted VMSA and so +* can't be fixed up by KVM. Rejecting EFER.LMSLE=1 would inject a #GP +* *and* leave EFER.LMSLE set in the guest, which is strictly worse +* than honoring a write that hardware itself allowed. */ msr_info.host_initiated = false; msr_info.index =MSR_EFER ; - msr_info.data =to_svm (vcpu)->vmcb->control.exit_info_1 & ~EFER_SVME ; + msr_info.data =to_svm (vcpu)->vmcb->control.exit_info_1 & + ~(EFER_SVME |EFER_LMSLE ); ret =kvm_set_msr_common (vcpu, &msr_info);return kvm_complete_insn_gp (vcpu, ret);
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v3 3/5 KVM: selftests: Rename svm_nested_clear_efer_svme to svm_nested_efer_test
In reply to Sean ChristophersonFrom: Jim Mattson <[email protected]>
Rename svm_nested_clear_efer_svme.c to svm_nested_efer_test.c so that the test can host coverage for other EFER bits whose behavior is tied to nested SVM, and so that the name matches the svm_nested_<topic>_test convention used by svm_nested_pat_test, svm_nested_shutdown_test, etc.
No functional change intended.
---
tools/testing/selftests/kvm/Makefile.kvm | 2 +-
.../{svm_nested_clear_efer_svme.c => svm_nested_efer_test.c} | 0
2 files changed, 1 insertion(+), 1 deletion(-)
rename tools/testing/selftests/kvm/x86/{svm_nested_clear_efer_svme.c => svm_nested_efer_test.c} (100%)tools/testing/selftests/kvm/Makefile.kvm +1 -1
@@ -117,7 +117,7 @@ TEST_GEN_PROGS_x86 += x86/state_test TEST_GEN_PROGS_x86 += x86/vmx_preemption_timer_test TEST_GEN_PROGS_x86 += x86/svm_vmcall_test TEST_GEN_PROGS_x86 += x86/svm_int_ctl_test -TEST_GEN_PROGS_x86 += x86/svm_nested_clear_efer_svme +TEST_GEN_PROGS_x86 += x86/svm_nested_efer_test TEST_GEN_PROGS_x86 += x86/svm_nested_shutdown_test TEST_GEN_PROGS_x86 += x86/svm_nested_soft_inject_test TEST_GEN_PROGS_x86 += x86/svm_nested_vmcb12_gpa
tools/testing/selftests/kvm/x86/svm_nested_efer_test.c renamed from tools/testing/selftests/kvm/x86/svm_nested_clear_efer_svme.c +0 -0
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v3 4/5 KVM: selftests: Add module param API to check if nested virtualization is enabled
In reply to Sean ChristophersonAdd an API to check if the nested virtualization module param is enabled, e.g. so that tests can verify KVM's own enumeration of the corresponding CPU feature. Provide a big warning above the API to make it as obvious as possible clear that tests should use "kvm_cpu_has(X86_FEATURE_VMX) || kvm_cpu_has(X86_FEATURE_SVM)" when checking for nested virtualization. It doesn't really matter which method tests use, as KVM is supposed to keep "nested" synchronized with VMX/SVM enumeration, but make the comment super scary to try and prevent KVM selftests from having two different ways of doing the same thing.
Opportunistically provide helpers to get vendor params that are common to both Intel and AMD, mainly to avoid having to copy+paste more code every time a new param comes along, but also to create a more blatant conflict with an in-flight series to add support for Zhaoxin CPUs, which will add a host_cpu_is_intel_compatible flag.
--- .../selftests/kvm/include/x86/processor.h | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +27 -4
@@ -1429,6 +1429,14 @@ static inline bool get_kvm_amd_param_bool(const char *param)return kvm_get_module_param_bool ("kvm_amd" , param); } +static inline int get_common_kvm_vendor_param_bool (const char *param) +{ +if (host_cpu_is_intel) +return get_kvm_intel_param_bool (param); + +return get_kvm_amd_param_bool (param); +} +static inline int get_kvm_intel_param_integer (const char *param) {return kvm_get_module_param_integer ("kvm_intel" , param);
@@ -1439,6 +1447,14 @@ static inline int get_kvm_amd_param_integer(const char *param)return kvm_get_module_param_integer ("kvm_amd" , param); } +static inline int get_common_kvm_vendor_param_integer (const char *param) +{ +if (host_cpu_is_intel) +return get_kvm_intel_param_integer (param); + +return get_kvm_amd_param_integer (param); +} +static inline bool kvm_is_pmu_enabled (void ) {return get_kvm_param_bool ("enable_pmu" );
@@ -1446,10 +1462,7 @@ static inline bool kvm_is_pmu_enabled(void)static inline bool kvm_is_mediated_pmu_enabled (void ) { -if (host_cpu_is_intel) -return get_kvm_intel_param_bool ("enable_mediated_pmu" ); - -return get_kvm_amd_param_bool ("enable_mediated_pmu" ); +return get_common_kvm_vendor_param_bool ("enable_mediated_pmu" ); }static inline bool kvm_is_forced_emulation_enabled (void )
@@ -1472,6 +1485,16 @@ static inline bool kvm_is_lbrv_enabled(void)return !!get_kvm_amd_param_integer ("lbrv" ); } +/* +* Do NOT use this to check for nVMX or nSVM support. Querying kvm_cpu_has() +* for either of X86_FEATURE_{VMX,SVM} is the idiomatic way to check for nested +* virtualization support. Use this *only* to validate KVM's own enumeration. +*/ +static inline bool kvm_is_nested_virtualization_enabled (void ) +{ +return get_common_kvm_vendor_param_integer ("nested" ); +} +u64 *vm_get_pte (struct kvm_vm *vm,gva_t gva);u64 kvm_hypercall (u64 nr,u64 a0,u64 a1,u64 a2,u64 a3);
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[email protected]
AI reviewRe: [PATCH v3 4/5] KVM: selftests: Add module param API to check if nested virtualization is enabled
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] kvm: selftests: kvm_is_nested_virtualization_enabled() aborts on Intel - [Low] kvm: selftests…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] kvm: selftests: kvm_is_nested_virtualization_enabled() aborts on Intel
- [Low] kvm: selftests: get_common_kvm_vendor_param_bool() returns int instead of bool --
commit 680296c2620e4e5c37fadd1bdce211406dcc250c
Author: Sean Christopherson <[email protected]>
KVM: selftests: Add module param API to check if nested virtualization is enabled
Add an API to check if the nested virtualization module param is enabled, e.g. so that tests can verify KVM's own enumeration of the corresponding CPU feature. Provide a big warning above the API to make it as obvious as possible clear that tests should use "kvm_cpu_has(X86_FEATURE_VMX) || kvm_cpu_has(X86_FEATURE_SVM)" when checking for nested virtualization.
tools/testing/selftests/kvm/include/x86/processor.h static inline bool get_kvm_amd_param_bool(const char *param) · line 1432
Quoted 9 lines
tools/testing/selftests/kvm/include/x86/processor.h +1
-0@@ -1429,6 +1429,14 @@ static inline bool get_kvm_amd_param_bool(const char *param)return kvm_get_module_param_bool ("kvm_amd" , param); } +static inline int get_common_kvm_vendor_param_bool (const char *param)
[Severity: Low]
Does this function have a type mismatch? It evaluates vendor-specific boolean parameter helpers, but returns an int instead of a bool.
Quoted 6 lines
tools/testing/selftests/kvm/include/x86/processor.h +6
-0+{ +if (host_cpu_is_intel) +return get_kvm_intel_param_bool (param); + +return get_kvm_amd_param_bool (param); +}
[ ... ]
tools/testing/selftests/kvm/include/x86/processor.h static inline bool kvm_is_lbrv_enabled(void) · line 1495
Quoted 12 lines
tools/testing/selftests/kvm/include/x86/processor.h +8
-0@@ -1472,6 +1485,16 @@ static inline bool kvm_is_lbrv_enabled(void)return !!get_kvm_amd_param_integer ("lbrv" ); } +/* +* Do NOT use this to check for nVMX or nSVM support. Querying kvm_cpu_has() +* for either of X86_FEATURE_{VMX,SVM} is the idiomatic way to check for nested +* virtualization support. Use this *only* to validate KVM's own enumeration. +*/ +static inline bool kvm_is_nested_virtualization_enabled (void ) +{ +return get_common_kvm_vendor_param_integer ("nested" );
[Severity: Medium]
Will this unconditionally abort the test suite on Intel hosts?
The VMX "nested" module parameter is a boolean exposed via sysfs as "Y\n" or "N\n". If this helper uses the integer reader on Intel, it will read "Y\n" and pass "Y" directly to atoi_paranoid(), which throws a TEST_ASSERT because strtol() fails to parse "Y" as an integer:
tools/testing/selftests/kvm/lib/test_util.c:atoi_paranoid() {
...
TEST_ASSERT(num_str != end_ptr,
"strtol(\"%s\") didn't find a valid integer.", num_str);
...
}Quoted 5 lines
tools/testing/selftests/kvm/include/x86/processor.h +2
-0+} +u64 *vm_get_pte (struct kvm_vm *vm,gva_t gva);u64 kvm_hypercall (u64 nr,u64 a0,u64 a1,u64 a2,u64 a3);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Sean Christopherson
maintainerRe: [PATCH v3 4/5] KVM: selftests: Add module param API to check if nested virtualization is enabled
Quoted 26 lines · On Wed, Sep 30, 2026, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] kvm: selftests:kvm_is_nested_virtualization_enabled()aborts on Intel
- [Low] kvm: selftests:get_common_kvm_vendor_param_bool()returns int instead of bool --commit
680296c2620e4e5c37fadd1bdce211406dcc250c
Author: Sean Christopherson <[email protected]>KVM: selftests: Add module param API to check if nested virtualization is enabled
Add an API to check if the nested virtualization module param is enabled, e.g. so that tests can verify KVM's own enumeration of the corresponding CPU feature. Provide a big warning above the API to make it as obvious as possible clear that tests should use "
kvm_cpu_has(X86_FEATURE_VMX)||kvm_cpu_has(X86_FEATURE_SVM)" when checking for nested virtualization.Quoted 8 lines
tools/testing/selftests/kvm/include/x86/processor.h +0
-0@@ -1429,6 +1429,14 @@ static inline bool get_kvm_amd_param_bool(const char *param)return kvm_get_module_param_bool ("kvm_amd" , param); }
Quoted 5 lines
tools/testing/selftests/kvm/include/x86/processor.h static inline bool get_kvm_amd_param_bool(const char *param) · line 1432
Quoted 1 line
tools/testing/selftests/kvm/include/x86/processor.h +1
-0+static inline int get_common_kvm_vendor_param_bool (const char *param)[Severity: Low]
Does this function have a type mismatch? It evaluates vendor-specific boolean parameter helpers, but returns an int instead of a bool.
That part is intentional.
Quoted 13 lines
Quoted 6 lines
tools/testing/selftests/kvm/include/x86/processor.h +6
-0+{ +if (host_cpu_is_intel) +return get_kvm_intel_param_bool (param); + +return get_kvm_amd_param_bool (param); +}[ ... ]
Quoted 4 lines
tools/testing/selftests/kvm/include/x86/processor.h +0
-0@@ -1472,6 +1485,16 @@ static inline bool kvm_is_lbrv_enabled(void)return !!get_kvm_amd_param_integer ("lbrv" ); }
Quoted 11 lines
tools/testing/selftests/kvm/include/x86/processor.h static inline bool kvm_is_lbrv_enabled(void) · line 1495
Quoted 8 lines
tools/testing/selftests/kvm/include/x86/processor.h +8
-0+/* +* Do NOT use this to check for nVMX or nSVM support. Querying kvm_cpu_has() +* for either of X86_FEATURE_{VMX,SVM} is the idiomatic way to check for nested +* virtualization support. Use this *only* to validate KVM's own enumeration. +*/ +static inline bool kvm_is_nested_virtualization_enabled (void ) +{ +return get_common_kvm_vendor_param_integer ("nested" );[Severity: Medium]
Will this unconditionally abort the test suite on Intel hosts?
Ugh, yes. I had a brain fart and mentally inverted the X86_FEATURE_SVM result, i.e. thought I was testing this helper.
Quoted 10 lines
The VMX "nested" module parameter is a boolean exposed via sysfs as "Y\n" or "N\n". If this helper uses the integer reader on Intel, it will read "Y\n" and pass "Y" directly to
atoi_paranoid(), which throws aTEST_ASSERTbecausestrtol()fails to parse "Y" as an integer:tools/testing/selftests/kvm/lib/test_util.c:atoi_paranoid() { ... TEST_ASSERT(num_str != end_ptr, "strtol(\"%s\") didn't find a valid integer.", num_str); ...
OMG, I hate KVM. I'll just do the obvious thing, and then make a mental note to call out the semantic "conflict" in the pull request. *sigh*
/*
* Do NOT use this to check for nVMX or nSVM support. Querying kvm_cpu_has()
* for either of X86_FEATURE_{VMX,SVM} is the idiomatic way to check for nested
* virtualization support. Use this *only* to validate KVM's own enumeration. */
static inline bool kvm_is_nested_virtualization_enabled(void)
{
if (host_cpu_is_intel)
return get_kvm_intel_param_bool("nested");
return get_kvm_amd_param_integer("nested");
}Sean Christopherson
maintainerPATCH v3 5/5 KVM: selftests: Add coverage for the EFER_LMSLE_MBZ defeature
In reply to Sean ChristophersonFrom: Jim Mattson <[email protected]>
Add coverage for KVM's virtualization of EFER_LMSLE_MBZ, CPUID.80000008H:EBX[bit 20], which is set when the CPU does *not* support long mode segment limits. Verify that KVM's enumeration of the defeature matches the expectation derived from hardware plus kvm_amd's "nested" module param, so that the assertion doesn't compare KVM's enumeration against itself, and that WRMSR(EFER), nested VMRUN, and KVM_SET_SREGS reject EFER.LMSLE=1 if and only if guest CPUID enumerates the defeature. Also verify that host-initiated KVM_SET_MSRS is exempt from the check, as host-initiated writes skip guest CPUID checks so that userspace can set MSRs before it sets guest CPUID.
Extend svm_nested_efer_test rather than add yet another test binary for a single CPUID bit; the test already has the L1/L2 harness needed to exercise EFER.LMSLE. Note, only a CPU that supports LMSLE, i.e. Rome and earlier, exercises the emulation path; elsewhere KVM enumerates the defeature straight from hardware.
Opportunistically drop the unnecessary #include of vmx.h.
--- .../selftests/kvm/include/x86/processor.h | 1 + .../selftests/kvm/x86/svm_nested_efer_test.c | 244 +++++++++++++++++- 2 files changed, 231 insertions(+), 14 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +1 -0
@@ -216,6 +216,7 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_INVTSC KVM_X86_CPU_FEATURE(0x80000007, 0, EDX, 8)#define X86_FEATURE_RDPRU KVM_X86_CPU_FEATURE(0x80000008, 0, EBX, 4)#define X86_FEATURE_AMD_IBPB KVM_X86_CPU_FEATURE(0x80000008, 0, EBX, 12) +#define X86_FEATURE_EFER_LMSLE_MBZ KVM_X86_CPU_FEATURE(0x80000008, 0, EBX, 20)#define X86_FEATURE_NPT KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 0)#define X86_FEATURE_LBRV KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 1)#define X86_FEATURE_NRIPS KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 3)
tools/testing/selftests/kvm/x86/svm_nested_efer_test.c +230 -14
277 lines not on this page · raw message
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog