mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <seanjc@google.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Jim Mattson <jmattson@google.com>
Subject: [PATCH v3 4/5] KVM: selftests: Add module param API to check if nested virtualization is enabled
Date: Tue, 29 Sep 2026 17:21:39 -0700	[thread overview]
Message-ID: <20260930002140.3174449-5-seanjc@google.com> (raw)
In-Reply-To: <20260930002140.3174449-1-seanjc@google.com>

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@zhaoxin.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 .../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


  parent reply	other threads:[~2026-09-30  0:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-30  0:21 [PATCH v3 0/5] KVM: x86: Honor EFER_LMSLE_MBZ Sean Christopherson
2026-09-30  0:21 ` [PATCH v3 1/5] KVM: x86: Advertise EFER_LMSLE_MBZ when KVM disallows EFER.LMSLE Sean Christopherson
2026-09-30  0:21 ` [PATCH v3 2/5] KVM: x86: Honor the guest's EFER_LMSLE_MBZ Sean Christopherson
2026-09-30  0:21 ` [PATCH v3 3/5] KVM: selftests: Rename svm_nested_clear_efer_svme to svm_nested_efer_test Sean Christopherson
2026-09-30  0:21 ` Sean Christopherson [this message]
2026-09-30  0:21 ` [PATCH v3 5/5] KVM: selftests: Add coverage for the EFER_LMSLE_MBZ defeature Sean Christopherson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260930002140.3174449-5-seanjc@google.com \
    --to=seanjc@google.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®