[PATCH v3 4/5] KVM: selftests: Add module param API to check if nested virtualization is enabled

From: Sean Christopherson

Date: Tue Sep 29 2026 - 20:27:13 EST


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. 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.

Link: https://lore.kernel.org/all/20260910115414.3015260-2-ewanhai-oc@xxxxxxxxxxx
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
.../selftests/kvm/include/x86/processor.h | 31 ++++++++++++++++---
1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
index 6e6f70035508..1608ddb91b6e 100644
--- a/tools/testing/selftests/kvm/include/x86/processor.h
+++ b/tools/testing/selftests/kvm/include/x86/processor.h
@@ -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);
--
2.56.0.rc1.315.gc6ed9934b7-goog