Skip to contentSkip to conversation

x86/cpuid: KVM: TDX CPUID fixes

PATCH Applied Sean Christopherson 5 participants 22 messages Oct 22 – Nov 13

Subsystems (5)
Stable branch x86 KVM KVM x86 +1 more subsystems
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

v2 cover x86/cpuid: KVM: TDX CPUID fixes

Boris or Dave, can you grab patch 1 for 7.4, ideally sooner than later? That would avoid having to carry a stable topic branch for a trivial change.

Annotate CPUID entries that have a significant index when reporting TDX capabilities to userspace, and enforce that userspace honors significant indices to avoid creating a misconfiguring between KVM and the TDX Module, which in the worst case scenario could lead to host state getting clobbered.

Patch 1 is a fix to mark function 0x23 as being indexed. That change is needed to avoid tripping a WARN sanity check that KVM and the TDX Module agree on which functions are indexed.

I'd like to take this through the KVM tree as it seems extremely unlikely the cpuid_function_is_indexed() will conflict or grow other dependencies, i.e. creating a shared topic branch for this seems rather silly. Actually, a better alternative would be to rush patch 1 into 7.4 so that I can use 7.4-rc2 as a base (even rc3 or rc4 would be a-ok). /me adds an intro

Note, SIGNIFCANT_INDEX isn't a typo, KVM's uAPI macro is misspelled...

v2:
- Add CPUID.0x23 to the indexed set. [Rick, Binbin]
- Document that zeroing the index when it's supposed to be ignored is KVM ABI that at least QEMU relies on. [Binbin]
- Zero out all of flags to ensure garbage isn't reported to userspace.

v1: https://lore.kernel.org/all/[email protected]

Changyuan Lyu (1):
KVM: TDX: Set SIGNIFCANT_INDEX flag as appropriate when filling CPUID
entries

Sean Christopherson (2):
x86/cpuid: Mark CPUID function 0x23 as being indexed
KVM: TDX: Reject KVM_TDX_INIT_VM if indexed CPUID functions don't
match

 arch/x86/include/asm/cpuid/api.h |  1 +
 arch/x86/kvm/vmx/tdx.c           | 22 +++++++++++++++++++---
 2 files changed, 20 insertions(+), 3 deletions(-)

base-commit: 814607cd40df80d5727957adaa857e0feca43497

Signature · 1 line
2.56.0.257.gf7497c7d27-goog
author maintainer
#1 lore raw

PATCH v2 1/3 x86/cpuid: Mark CPUID function 0x23 as being indexed

Add function 0x23 to the set of CPUID functions that are indexed, as per the SDM and the many sub-leafs defined in leaf_types.h.

---
 arch/x86/include/asm/cpuid/api.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpuid/api.h +1 -0
index 82eddfa2347b..25b22b173f2a 100644
@@ -181,6 +181,7 @@ static __always_inline bool cpuid_function_is_indexed(u32 function)
 	case 0x1d:
 	case 0x1e:
 	case 0x1f:
+	case 0x23:
 	case 0x24:
 	case 0x8000001d:
 		return true;
Signature · 1 line
2.56.0.257.gf7497c7d27-goog
#2 lore raw

[tip: x86/urgent] x86/cpuid: Mark CPUID function 0x23 as being indexed

The following commit has been merged into the x86/urgent branch of tip:

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     108775fa28fe04a16f19a828a779689f3d0ea9f0
Gitweb:        https://git.kernel.org/tip/108775fa28fe04a16f19a828a779689f3d0ea9f0
Author:        Sean Christopherson <[email protected]>
AuthorDate:    Thu, 22 Oct 2026 14:01:32 -07:00
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Thu, 22 Oct 2026 18:39:16 -07:00

x86/cpuid: Mark CPUID function 0x23 as being indexed

Add function 0x23 to the set of CPUID functions that are indexed, as per the SDM and the many sub-leafs defined in leaf_types.h.

---
 arch/x86/include/asm/cpuid/api.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpuid/api.h +1 -0
index 2d9f3d4..759b12d 100644
@@ -181,6 +181,7 @@ static __always_inline bool cpuid_function_is_indexed(u32 function)
 	case 0x1d:
 	case 0x1e:
 	case 0x1f:
+	case 0x23:
 	case 0x24:
 	case 0x8000001d:
 		return true;
Reviewed-by on v2 1/3
#3 lore raw

Re: [PATCH v2 1/3] x86/cpuid: Mark CPUID function 0x23 as being indexed

In reply to Sean Christopherson
Quoted 7 lines · Sean Christopherson

Add function 0x23 to the set of CPUID functions that are indexed, as per the SDM and the many sub-leafs defined in leaf_types.h.

Quoted 16 lines
---
 arch/x86/include/asm/cpuid/api.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpuid/api.h +1 -0
index 82eddfa2347b..25b22b173f2a 100644
@@ -181,6 +181,7 @@ static __always_inline bool cpuid_function_is_indexed(u32 function)
 	case 0x1d:
 	case 0x1e:
 	case 0x1f:
+	case 0x23:
 	case 0x24:
 	case 0x8000001d:
 		return true;
author maintainer
#4 lore raw

PATCH v2 2/3 KVM: TDX: Set SIGNIFCANT_INDEX flag as appropriate when filling CPUID entries

In reply to Sean Christopherson

From: Changyuan Lyu <[email protected]>

Set the KVM_CPUID_FLAG_SIGNIFCANT_INDEX flag in the kvm_cpuid_entry2 structures returned by KVM_TDX_CAPABILITIES if the CPUID is indexed, otherwise KVM could confuse userspace and induce a configuration error. This also ensures consistency with the CPUID entries returned by KVM_GET_SUPPORTED_CPUID.

WARN if KVM and the TDX Module don't align on which functions have significant indices, e.g. to help ensure the kernel is updated when the TDX Module learns about new CPUID leaves, and vice versa.

Opportunistically add a comment to call out that zeroing the index when it isn't significant is ABI that userspace relies on, e.g. QEMU looks for '0' instead of checking KVM_CPUID_FLAG_SIGNIFCANT_INDEX.

Fixes 61bb28279623 ("KVM: TDX: Get system-wide info about TDX module on initialization")
Cc Binbin Wu <[email protected]>
[sean: keep the WARN, don't key off KVM_TDX_CPUID_NO_SUBLEAF, add comment]
---
 arch/x86/kvm/vmx/tdx.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/tdx.c +13 -1
index 271c2f94b8dd..37d088f6f7a8 100644
@@ -2,6 +2,7 @@
 #include <linux/cleanup.h>
 #include <linux/cpu.h>
 #include <asm/cpufeature.h>
+#include <asm/cpuid/api.h>
 #include <asm/fpu/xcr.h>
 #include <linux/misc_cgroup.h>
 #include <linux/mmu_context.h>
@@ -163,12 +164,23 @@ static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i
 
 	entry->function = (u32)td_conf->cpuid_config_leaves[idx];
 	entry->index = td_conf->cpuid_config_leaves[idx] >> 32;
+	entry->flags = 0;
 	entry->eax = (u32)td_conf->cpuid_config_values[idx][0];
 	entry->ebx = td_conf->cpuid_config_values[idx][0] >> 32;
 	entry->ecx = (u32)td_conf->cpuid_config_values[idx][1];
 	entry->edx = td_conf->cpuid_config_values[idx][1] >> 32;
 
-	if (entry->index == KVM_TDX_CPUID_NO_SUBLEAF)
+	WARN_ON_ONCE(cpuid_function_is_indexed(entry->function) !=
+		     (entry->index != KVM_TDX_CPUID_NO_SUBLEAF));
+
+	/*
+	 * Explicitly zero the index when it's not significant, even though it
+	 * _should_ be meaningless.  Userspace relies on the index being zero
+	 * (the ABI was inherited from KVM_GET_SUPPORTED_CPUID).
+	 */
+	if (cpuid_function_is_indexed(entry->function))
+		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+	else
 		entry->index = 0;
 
 	/*
Signature · 1 line
2.56.0.257.gf7497c7d27-goog
#5 lore raw

Re: [PATCH v2 2/3] KVM: TDX: Set SIGNIFCANT_INDEX flag as appropriate when filling CPUID entries

Quoted 49 lines · Sean Christopherson

From: Changyuan Lyu <[email protected]>

Set the KVM_CPUID_FLAG_SIGNIFCANT_INDEX flag in the kvm_cpuid_entry2 structures returned by KVM_TDX_CAPABILITIES if the CPUID is indexed, otherwise KVM could confuse userspace and induce a configuration error. This also ensures consistency with the CPUID entries returned by KVM_GET_SUPPORTED_CPUID.

WARN if KVM and the TDX Module don't align on which functions have significant indices, e.g. to help ensure the kernel is updated when the TDX Module learns about new CPUID leaves, and vice versa.

Opportunistically add a comment to call out that zeroing the index when it isn't significant is ABI that userspace relies on, e.g. QEMU looks for '0' instead of checking KVM_CPUID_FLAG_SIGNIFCANT_INDEX.

Fixes 61bb28279623 ("KVM: TDX: Get system-wide info about TDX module on initialization")
Cc Binbin Wu <[email protected]>
[sean: keep the WARN, don't key off KVM_TDX_CPUID_NO_SUBLEAF, add comment]
---
 arch/x86/kvm/vmx/tdx.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/tdx.c +2 -0
index 271c2f94b8dd..37d088f6f7a8 100644
@@ -2,6 +2,7 @@
 #include <linux/cleanup.h>
 #include <linux/cpu.h>
 #include <asm/cpufeature.h>
+#include <asm/cpuid/api.h>
 #include <asm/fpu/xcr.h>
 #include <linux/misc_cgroup.h>
 #include <linux/mmu_context.h>
@@ -163,12 +164,23 @@ static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i
 
 	entry->function = (u32)td_conf->cpuid_config_leaves[idx];
 	entry->index = td_conf->cpuid_config_leaves[idx] >> 32;
+	entry->flags = 0;
 	entry->eax = (u32)td_conf->cpuid_config_values[idx][0];
 	entry->ebx = td_conf->cpuid_config_values[idx][0] >> 32;
 	entry->ecx = (u32)td_conf->cpuid_config_values[idx][1];
 	entry->edx = td_conf->cpuid_config_values[idx][1] >> 32;
 

arch/x86/kvm/vmx/tdx.c static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i · line 181

Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +9 -1
-	if (entry->index == KVM_TDX_CPUID_NO_SUBLEAF)
+	WARN_ON_ONCE(cpuid_function_is_indexed(entry->function) !=
+		     (entry->index != KVM_TDX_CPUID_NO_SUBLEAF));
+
+	/*
+	 * Explicitly zero the index when it's not significant, even though it
+	 * _should_ be meaningless.  Userspace relies on the index being zero
+	 * (the ABI was inherited from KVM_GET_SUPPORTED_CPUID).
+	 */
+	if (cpuid_function_is_indexed(entry->function))

If the information about whether the entry is indexed mismatched, i.e. the warning above fired, should we use the information from the TDX module as the standard?

Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +2 -0
+		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+	else
 		entry->index = 0;
 
 	/*
author maintainer
#6 lore raw

Re: [PATCH v2 2/3] KVM: TDX: Set SIGNIFCANT_INDEX flag as appropriate when filling CPUID entries

Quoted 23 lines · Binbin Wu
Quoted 22 lines · Sean Christopherson
arch/x86/kvm/vmx/tdx.c +2 -0
index 271c2f94b8dd..37d088f6f7a8 100644
@@ -2,6 +2,7 @@
 #include <linux/cleanup.h>
 #include <linux/cpu.h>
 #include <asm/cpufeature.h>
+#include <asm/cpuid/api.h>
 #include <asm/fpu/xcr.h>
 #include <linux/misc_cgroup.h>
 #include <linux/mmu_context.h>
@@ -163,12 +164,23 @@ static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i
 
 	entry->function = (u32)td_conf->cpuid_config_leaves[idx];
 	entry->index = td_conf->cpuid_config_leaves[idx] >> 32;
+	entry->flags = 0;
 	entry->eax = (u32)td_conf->cpuid_config_values[idx][0];
 	entry->ebx = td_conf->cpuid_config_values[idx][0] >> 32;
 	entry->ecx = (u32)td_conf->cpuid_config_values[idx][1];
 	entry->edx = td_conf->cpuid_config_values[idx][1] >> 32;
 
Quoted 14 lines

arch/x86/kvm/vmx/tdx.c static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i · line 181

Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +9 -1
-	if (entry->index == KVM_TDX_CPUID_NO_SUBLEAF)
+	WARN_ON_ONCE(cpuid_function_is_indexed(entry->function) !=
+		     (entry->index != KVM_TDX_CPUID_NO_SUBLEAF));
+
+	/*
+	 * Explicitly zero the index when it's not significant, even though it
+	 * _should_ be meaningless.  Userspace relies on the index being zero
+	 * (the ABI was inherited from KVM_GET_SUPPORTED_CPUID).
+	 */
+	if (cpuid_function_is_indexed(entry->function))

If the information about whether the entry is indexed mismatched, i.e. the warning above fired, should we use the information from the TDX module as the standard?

IMO, no. All of this information is available at module load. If we want to harden KVM (and we probably should), then we should apply these sanity checks (or equivalents) during tdx_hardware_setup() and disable TDX if any of them fail.

Anything else is a hope and a prayer.

#7 lore raw

Re: [PATCH v2 2/3] KVM: TDX: Set SIGNIFCANT_INDEX flag as appropriate when filling CPUID entries

Quoted 39 lines · Sean Christopherson
Quoted 23 lines · Binbin Wu
Quoted 22 lines · Sean Christopherson
arch/x86/kvm/vmx/tdx.c +2 -0
index 271c2f94b8dd..37d088f6f7a8 100644
@@ -2,6 +2,7 @@
 #include <linux/cleanup.h>
 #include <linux/cpu.h>
 #include <asm/cpufeature.h>
+#include <asm/cpuid/api.h>
 #include <asm/fpu/xcr.h>
 #include <linux/misc_cgroup.h>
 #include <linux/mmu_context.h>
@@ -163,12 +164,23 @@ static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i
 
 	entry->function = (u32)td_conf->cpuid_config_leaves[idx];
 	entry->index = td_conf->cpuid_config_leaves[idx] >> 32;
+	entry->flags = 0;
 	entry->eax = (u32)td_conf->cpuid_config_values[idx][0];
 	entry->ebx = td_conf->cpuid_config_values[idx][0] >> 32;
 	entry->ecx = (u32)td_conf->cpuid_config_values[idx][1];
 	entry->edx = td_conf->cpuid_config_values[idx][1] >> 32;
 
Quoted 14 lines

arch/x86/kvm/vmx/tdx.c static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i · line 181

Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +9 -1
-	if (entry->index == KVM_TDX_CPUID_NO_SUBLEAF)
+	WARN_ON_ONCE(cpuid_function_is_indexed(entry->function) !=
+		     (entry->index != KVM_TDX_CPUID_NO_SUBLEAF));
+
+	/*
+	 * Explicitly zero the index when it's not significant, even though it
+	 * _should_ be meaningless.  Userspace relies on the index being zero
+	 * (the ABI was inherited from KVM_GET_SUPPORTED_CPUID).
+	 */
+	if (cpuid_function_is_indexed(entry->function))

If the information about whether the entry is indexed mismatched, i.e. the warning above fired, should we use the information from the TDX module as the standard?

Quoted 3 lines

IMO, no. All of this information is available at module load. If we want to harden KVM (and we probably should), then we should apply these sanity checks (or equivalents) during tdx_hardware_setup() and disable TDX if any of them fail.

OK.

In practice, I don't think it is a problem unless cpuid_function_is_indexed() is missing an indexed leaf that KVM cares about. If it isn't missing one, unknown CPUID leafs will still be rejected later by the allowlist-based TDX CPUID validation.

There is also a false-positive risk: a new TDX module could report an indexed leaf that KVM doesn't know about. One way to avoid that is for KVM to fail TDX module initialization only when there is a mismatch *and* the leaf is listed in reverse_cpuid[]. I think that can be handled separately.

I'll rebase my TDX CPUID validation patch series on top of this series.

Quoted 2 lines

Anything else is a hope and a prayer.

#8 lore raw

Re: [PATCH v2 2/3] KVM: TDX: Set SIGNIFCANT_INDEX flag as appropriate when filling CPUID entries

In reply to Sean Christopherson
Quoted 42 lines · Sean Christopherson

From: Changyuan Lyu <[email protected]>

Set the KVM_CPUID_FLAG_SIGNIFCANT_INDEX flag in the kvm_cpuid_entry2 structures returned by KVM_TDX_CAPABILITIES if the CPUID is indexed, otherwise KVM could confuse userspace and induce a configuration error. This also ensures consistency with the CPUID entries returned by KVM_GET_SUPPORTED_CPUID.

WARN if KVM and the TDX Module don't align on which functions have significant indices, e.g. to help ensure the kernel is updated when the TDX Module learns about new CPUID leaves, and vice versa.

Opportunistically add a comment to call out that zeroing the index when it isn't significant is ABI that userspace relies on, e.g. QEMU looks for '0' instead of checking KVM_CPUID_FLAG_SIGNIFCANT_INDEX.

Fixes 61bb28279623 ("KVM: TDX: Get system-wide info about TDX module on initialization")
Cc Binbin Wu <[email protected]>
[sean: keep the WARN, don't key off KVM_TDX_CPUID_NO_SUBLEAF, add comment]
---
 arch/x86/kvm/vmx/tdx.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/tdx.c +1 -0
index 271c2f94b8dd..37d088f6f7a8 100644
@@ -2,6 +2,7 @@
 #include <linux/cleanup.h>
 #include <linux/cpu.h>
 #include <asm/cpufeature.h>
+#include <asm/cpuid/api.h>
 #include <asm/fpu/xcr.h>
 #include <linux/misc_cgroup.h>
 #include <linux/mmu_context.h>
@@ -163,12 +164,23 @@ static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i
 
 	entry->function = (u32)td_conf->cpuid_config_leaves[idx];

arch/x86/kvm/vmx/tdx.c static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i · line 174

Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +3 -1
 	entry->index = td_conf->cpuid_config_leaves[idx] >> 32;
+	entry->flags = 0;
 	entry->eax = (u32)td_conf->cpuid_config_values[idx][0];
 	entry->ebx = td_conf->cpuid_config_values[idx][0] >> 32;
 	entry->ecx = (u32)td_conf->cpuid_config_values[idx][1];
 	entry->edx = td_conf->cpuid_config_values[idx][1] >> 32;
 
-	if (entry->index == KVM_TDX_CPUID_NO_SUBLEAF)
+	WARN_ON_ONCE(cpuid_function_is_indexed(entry->function) !=
+		     (entry->index != KVM_TDX_CPUID_NO_SUBLEAF));

I think the WARN_ON_ONCE() is very likely to be triggered in the future when people run old kernel with new TDX modules.

Quoted 11 lines
arch/x86/kvm/vmx/tdx.c +8 -0
+	/*
+	 * Explicitly zero the index when it's not significant, even though it
+	 * _should_ be meaningless.  Userspace relies on the index being zero
+	 * (the ABI was inherited from KVM_GET_SUPPORTED_CPUID).
+	 */
+	if (cpuid_function_is_indexed(entry->function))
+		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+	else
 		entry->index = 0;
 
 	/*
author maintainer
#9 lore raw

Re: [PATCH v2 2/3] KVM: TDX: Set SIGNIFCANT_INDEX flag as appropriate when filling CPUID entries

Quoted 23 lines · Xiaoyao Li
Quoted 22 lines · Sean Christopherson
arch/x86/kvm/vmx/tdx.c +2 -0
index 271c2f94b8dd..37d088f6f7a8 100644
@@ -2,6 +2,7 @@
 #include <linux/cleanup.h>
 #include <linux/cpu.h>
 #include <asm/cpufeature.h>
+#include <asm/cpuid/api.h>
 #include <asm/fpu/xcr.h>
 #include <linux/misc_cgroup.h>
 #include <linux/mmu_context.h>
@@ -163,12 +164,23 @@ static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i
 
 	entry->function = (u32)td_conf->cpuid_config_leaves[idx];
 	entry->index = td_conf->cpuid_config_leaves[idx] >> 32;
+	entry->flags = 0;
 	entry->eax = (u32)td_conf->cpuid_config_values[idx][0];
 	entry->ebx = td_conf->cpuid_config_values[idx][0] >> 32;
 	entry->ecx = (u32)td_conf->cpuid_config_values[idx][1];
 	entry->edx = td_conf->cpuid_config_values[idx][1] >> 32;
 
Quoted 6 lines

arch/x86/kvm/vmx/tdx.c static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i · line 174

Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +2 -1
-	if (entry->index == KVM_TDX_CPUID_NO_SUBLEAF)
+	WARN_ON_ONCE(cpuid_function_is_indexed(entry->function) !=
+		     (entry->index != KVM_TDX_CPUID_NO_SUBLEAF));

I think the WARN_ON_ONCE() is very likely to be triggered in the future when people run old kernel with new TDX modules.

Then upstream patches to make that not happen. Intel insists on treating the TDX Module like hardware. So, treat new TDX Modules like new hardware. Upstream support for the new "hardware" before it ships, cc'd to [email protected] if necessary.

Because IMO, the WARN is doing exactly what it's intended to do: alerting the admin that they are running a potentially dangerous setup.

Quoted 13 lines
Quoted 11 lines
arch/x86/kvm/vmx/tdx.c +8 -0
+	/*
+	 * Explicitly zero the index when it's not significant, even though it
+	 * _should_ be meaningless.  Userspace relies on the index being zero
+	 * (the ABI was inherited from KVM_GET_SUPPORTED_CPUID).
+	 */
+	if (cpuid_function_is_indexed(entry->function))
+		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+	else
 		entry->index = 0;
 
 	/*
#10 lore raw

Re: [PATCH v2 2/3] KVM: TDX: Set SIGNIFCANT_INDEX flag as appropriate when filling CPUID entries

Quoted 31 lines · Sean Christopherson
Quoted 23 lines · Xiaoyao Li
Quoted 22 lines · Sean Christopherson
arch/x86/kvm/vmx/tdx.c +2 -0
index 271c2f94b8dd..37d088f6f7a8 100644
@@ -2,6 +2,7 @@
 #include <linux/cleanup.h>
 #include <linux/cpu.h>
 #include <asm/cpufeature.h>
+#include <asm/cpuid/api.h>
 #include <asm/fpu/xcr.h>
 #include <linux/misc_cgroup.h>
 #include <linux/mmu_context.h>
@@ -163,12 +164,23 @@ static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i
 
 	entry->function = (u32)td_conf->cpuid_config_leaves[idx];
 	entry->index = td_conf->cpuid_config_leaves[idx] >> 32;
+	entry->flags = 0;
 	entry->eax = (u32)td_conf->cpuid_config_values[idx][0];
 	entry->ebx = td_conf->cpuid_config_values[idx][0] >> 32;
 	entry->ecx = (u32)td_conf->cpuid_config_values[idx][1];
 	entry->edx = td_conf->cpuid_config_values[idx][1] >> 32;
 
Quoted 6 lines

arch/x86/kvm/vmx/tdx.c static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i · line 174

Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +2 -1
-	if (entry->index == KVM_TDX_CPUID_NO_SUBLEAF)
+	WARN_ON_ONCE(cpuid_function_is_indexed(entry->function) !=
+		     (entry->index != KVM_TDX_CPUID_NO_SUBLEAF));

I think the WARN_ON_ONCE() is very likely to be triggered in the future when people run old kernel with new TDX modules.

Quoted 7 lines

Then upstream patches to make that not happen. Intel insists on treating the TDX Module like hardware. So, treat new TDX Modules like new hardware. Upstream support for the new "hardware" before it ships, cc'd to [email protected] if necessary.

Because IMO, the WARN is doing exactly what it's intended to do: alerting the admin that they are running a potentially dangerous setup.

For this specific case, does it mean every later patch to add a new leaf in cpuid_function_is_indexed() needs to be backported to stable?

author maintainer
#11 lore raw

Re: [PATCH v2 2/3] KVM: TDX: Set SIGNIFCANT_INDEX flag as appropriate when filling CPUID entries

Quoted 37 lines · Xiaoyao Li
Quoted 36 lines · Sean Christopherson
Quoted 23 lines · Xiaoyao Li
Quoted 22 lines · Sean Christopherson
arch/x86/kvm/vmx/tdx.c +2 -0
index 271c2f94b8dd..37d088f6f7a8 100644
@@ -2,6 +2,7 @@
 #include <linux/cleanup.h>
 #include <linux/cpu.h>
 #include <asm/cpufeature.h>
+#include <asm/cpuid/api.h>
 #include <asm/fpu/xcr.h>
 #include <linux/misc_cgroup.h>
 #include <linux/mmu_context.h>
@@ -163,12 +164,23 @@ static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i
 
 	entry->function = (u32)td_conf->cpuid_config_leaves[idx];
 	entry->index = td_conf->cpuid_config_leaves[idx] >> 32;
+	entry->flags = 0;
 	entry->eax = (u32)td_conf->cpuid_config_values[idx][0];
 	entry->ebx = td_conf->cpuid_config_values[idx][0] >> 32;
 	entry->ecx = (u32)td_conf->cpuid_config_values[idx][1];
 	entry->edx = td_conf->cpuid_config_values[idx][1] >> 32;
 
Quoted 6 lines

arch/x86/kvm/vmx/tdx.c static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i · line 174

Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +2 -1
-	if (entry->index == KVM_TDX_CPUID_NO_SUBLEAF)
+	WARN_ON_ONCE(cpuid_function_is_indexed(entry->function) !=
+		     (entry->index != KVM_TDX_CPUID_NO_SUBLEAF));

I think the WARN_ON_ONCE() is very likely to be triggered in the future when people run old kernel with new TDX modules.

Then upstream patches to make that not happen. Intel insists on treating the TDX Module like hardware. So, treat new TDX Modules like new hardware. Upstream support for the new "hardware" before it ships, cc'd to [email protected] if necessary.

Quoted 5 lines
Quoted 2 lines

Because IMO, the WARN is doing exactly what it's intended to do: alerting the admin that they are running a potentially dangerous setup.

For this specific case, does it mean every later patch to add a new leaf in cpuid_function_is_indexed() needs to be backported to stable?

If you want to ship a new TDX Module that enumerates features in those leaves and works as-is on older kernels, yes. Again, this isn't really any different than enabling new hardware. E.g. a kernel from 2024 generally doesn't work flawlessly on a CPU being released in 2026.

#12 lore raw

Re: [PATCH v2 2/3] KVM: TDX: Set SIGNIFCANT_INDEX flag as appropriate when filling CPUID entries

Quoted 41 lines · Sean Christopherson
Quoted 40 lines · Xiaoyao Li
Quoted 38 lines · Sean Christopherson
Quoted 23 lines · Xiaoyao Li
Quoted 22 lines · Sean Christopherson
arch/x86/kvm/vmx/tdx.c +2 -0
index 271c2f94b8dd..37d088f6f7a8 100644
@@ -2,6 +2,7 @@
 #include <linux/cleanup.h>
 #include <linux/cpu.h>
 #include <asm/cpufeature.h>
+#include <asm/cpuid/api.h>
 #include <asm/fpu/xcr.h>
 #include <linux/misc_cgroup.h>
 #include <linux/mmu_context.h>
@@ -163,12 +164,23 @@ static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i
 
 	entry->function = (u32)td_conf->cpuid_config_leaves[idx];
 	entry->index = td_conf->cpuid_config_leaves[idx] >> 32;
+	entry->flags = 0;
 	entry->eax = (u32)td_conf->cpuid_config_values[idx][0];
 	entry->ebx = td_conf->cpuid_config_values[idx][0] >> 32;
 	entry->ecx = (u32)td_conf->cpuid_config_values[idx][1];
 	entry->edx = td_conf->cpuid_config_values[idx][1] >> 32;
 
Quoted 6 lines

arch/x86/kvm/vmx/tdx.c static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i · line 174

Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +2 -1
-	if (entry->index == KVM_TDX_CPUID_NO_SUBLEAF)
+	WARN_ON_ONCE(cpuid_function_is_indexed(entry->function) !=
+		     (entry->index != KVM_TDX_CPUID_NO_SUBLEAF));

I think the WARN_ON_ONCE() is very likely to be triggered in the future when people run old kernel with new TDX modules.

Then upstream patches to make that not happen. Intel insists on treating the TDX Module like hardware. So, treat new TDX Modules like new hardware. Upstream support for the new "hardware" before it ships, cc'd to [email protected] if necessary.

Because IMO, the WARN is doing exactly what it's intended to do: alerting the admin that they are running a potentially dangerous setup.

Quoted 7 lines
Quoted 2 lines

For this specific case, does it mean every later patch to add a new leaf in cpuid_function_is_indexed() needs to be backported to stable?

If you want to ship a new TDX Module that enumerates features in those leaves and works as-is on older kernels, yes. Again, this isn't really any different than enabling new hardware. E.g. a kernel from 2024 generally doesn't work flawlessly on a CPU being released in 2026.

OK. Fair enough.

Reviewed-by on v2 2/3
#13 lore raw

Re: [PATCH v2 2/3] KVM: TDX: Set SIGNIFCANT_INDEX flag as appropriate when filling CPUID entries

In reply to Sean Christopherson
Quoted 16 lines · Sean Christopherson

From: Changyuan Lyu <[email protected]>

Set the KVM_CPUID_FLAG_SIGNIFCANT_INDEX flag in the kvm_cpuid_entry2 structures returned by KVM_TDX_CAPABILITIES if the CPUID is indexed, otherwise KVM could confuse userspace and induce a configuration error. This also ensures consistency with the CPUID entries returned by KVM_GET_SUPPORTED_CPUID.

WARN if KVM and the TDX Module don't align on which functions have significant indices, e.g. to help ensure the kernel is updated when the TDX Module learns about new CPUID leaves, and vice versa.

Opportunistically add a comment to call out that zeroing the index when it isn't significant is ABI that userspace relies on, e.g. QEMU looks for '0' instead of checking KVM_CPUID_FLAG_SIGNIFCANT_INDEX.

Quoted 7 lines
Fixes 61bb28279623 ("KVM: TDX: Get system-wide info about TDX module on initialization")
Cc Binbin Wu <[email protected]>
[sean: keep the WARN, don't key off KVM_TDX_CPUID_NO_SUBLEAF, add comment]
Quoted 41 lines
---
 arch/x86/kvm/vmx/tdx.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/tdx.c +13 -1
index 271c2f94b8dd..37d088f6f7a8 100644
@@ -2,6 +2,7 @@
 #include <linux/cleanup.h>
 #include <linux/cpu.h>
 #include <asm/cpufeature.h>
+#include <asm/cpuid/api.h>
 #include <asm/fpu/xcr.h>
 #include <linux/misc_cgroup.h>
 #include <linux/mmu_context.h>
@@ -163,12 +164,23 @@ static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i
 
 	entry->function = (u32)td_conf->cpuid_config_leaves[idx];
 	entry->index = td_conf->cpuid_config_leaves[idx] >> 32;
+	entry->flags = 0;
 	entry->eax = (u32)td_conf->cpuid_config_values[idx][0];
 	entry->ebx = td_conf->cpuid_config_values[idx][0] >> 32;
 	entry->ecx = (u32)td_conf->cpuid_config_values[idx][1];
 	entry->edx = td_conf->cpuid_config_values[idx][1] >> 32;
 
-	if (entry->index == KVM_TDX_CPUID_NO_SUBLEAF)
+	WARN_ON_ONCE(cpuid_function_is_indexed(entry->function) !=
+		     (entry->index != KVM_TDX_CPUID_NO_SUBLEAF));
+
+	/*
+	 * Explicitly zero the index when it's not significant, even though it
+	 * _should_ be meaningless.  Userspace relies on the index being zero
+	 * (the ABI was inherited from KVM_GET_SUPPORTED_CPUID).
+	 */
+	if (cpuid_function_is_indexed(entry->function))
+		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+	else
 		entry->index = 0;
 
 	/*
author maintainer
#14 lore raw

PATCH v2 3/3 KVM: TDX: Reject KVM_TDX_INIT_VM if indexed CPUID functions don't match

In reply to Sean Christopherson

Reject KVM_TDX_INIT_VM if userspace doesn't configure the canonical CPUID entries correctly with respect to KVM_CPUID_FLAG_SIGNIFCANT_INDEX to fix a bug where userspace could bypass the TSX and WAITPKG checks by simply not marking a CPUID.0x7 entry as being indexed. This also sets the stage for more in-depth validation of the incoming CPUID configuration.

This is obviously an ABI change and diverges from KVM's general approach of "let userspace do what it wants". In practice, no "real" VMM that supports TDX misconfigures a TD's CPUID entries in this way, i.e. the probability of actually breaking userspace is basically nil. And unlike normal VMs, where KVM has full control and thus can deal with bizarre CPUID input without too much fuss, TDX VMs require extra care due to the TDX Module also reacting to the CPUID configuration, i.e. KVM_TDX_INIT_VM needs to be more strict than KVM_SET_CPUID{,2} to avoid creating potentially dangerous-to-the-host setups.

Opportunistically rename the helper to make it more obvious that it's checking if the passed in @entry is unsupported, as opposed to returning a set of unsupported entries or something.

Fixes 6d415778f106 ("KVM: TDX: Disable support for TSX and WAITPKG")
---
 arch/x86/kvm/vmx/tdx.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +6 -2
index 37d088f6f7a8..6abf92ffec24 100644
@@ -151,8 +151,12 @@ static void tdx_clear_unsupported_cpuid(struct kvm_cpuid_entry2 *entry)
 		clear_waitpkg(entry);
 }
 
-static bool tdx_unsupported_cpuid(const struct kvm_cpuid_entry2 *entry)
+static bool tdx_is_unsupported_cpuid_entry(const struct kvm_cpuid_entry2 *entry)
 {
+	if (cpuid_function_is_indexed(entry->function) !=
+	    !!(entry->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX))
+		return true;
+
 	return has_tsx(entry) || has_waitpkg(entry);
 }
 
@@ -2393,7 +2397,7 @@ static int setup_tdparams_cpuids(struct kvm_cpuid2 *cpuid,
 		if (!entry)
 			continue;
 
-		if (tdx_unsupported_cpuid(entry))
+		if (tdx_is_unsupported_cpuid_entry(entry))
 			return -EINVAL;
 
 		copy_cnt++;
Signature · 1 line
2.56.0.257.gf7497c7d27-goog
#15 lore raw

Re: [PATCH v2 3/3] KVM: TDX: Reject KVM_TDX_INIT_VM if indexed CPUID functions don't match

Quoted 34 lines · Sean Christopherson

Reject KVM_TDX_INIT_VM if userspace doesn't configure the canonical CPUID entries correctly with respect to KVM_CPUID_FLAG_SIGNIFCANT_INDEX to fix a bug where userspace could bypass the TSX and WAITPKG checks by simply not marking a CPUID.0x7 entry as being indexed. This also sets the stage for more in-depth validation of the incoming CPUID configuration.

This is obviously an ABI change and diverges from KVM's general approach of "let userspace do what it wants". In practice, no "real" VMM that supports TDX misconfigures a TD's CPUID entries in this way, i.e. the probability of actually breaking userspace is basically nil. And unlike normal VMs, where KVM has full control and thus can deal with bizarre CPUID input without too much fuss, TDX VMs require extra care due to the TDX Module also reacting to the CPUID configuration, i.e. KVM_TDX_INIT_VM needs to be more strict than KVM_SET_CPUID{,2} to avoid creating potentially dangerous-to-the-host setups.

Opportunistically rename the helper to make it more obvious that it's checking if the passed in @entry is unsupported, as opposed to returning a set of unsupported entries or something.

Fixes 6d415778f106 ("KVM: TDX: Disable support for TSX and WAITPKG")
---
 arch/x86/kvm/vmx/tdx.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +0 -0
index 37d088f6f7a8..6abf92ffec24 100644

arch/x86/kvm/vmx/tdx.c static void tdx_clear_unsupported_cpuid(struct kvm_cpuid_entry2 *entry) · line 158

Quoted 9 lines
arch/x86/kvm/vmx/tdx.c +4 -1
@@ -151,8 +151,12 @@ static void tdx_clear_unsupported_cpuid(struct kvm_cpuid_entry2 *entry)
 		clear_waitpkg(entry);
 }
 
-static bool tdx_unsupported_cpuid(const struct kvm_cpuid_entry2 *entry)
+static bool tdx_is_unsupported_cpuid_entry(const struct kvm_cpuid_entry2 *entry)
 {
+	if (cpuid_function_is_indexed(entry->function) !=
+	    !!(entry->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX))
+		return true;

Similar to patch 2, if cpuid_function_is_indexed(entry->function) misses some function, the value of cpuid_function_is_indexed(entry->function) is not accurate.

For current code, it's OK, since all unsupported bits are in leaf 7. In the future, with the TDX CPUID validation patch set
https://lore.kernel.org/all/[email protected]/
It still allow userspace to by pass some check in theory.

Quoted 13 lines
arch/x86/kvm/vmx/tdx.c +2 -1
+
 	return has_tsx(entry) || has_waitpkg(entry);
 }
 
@@ -2393,7 +2397,7 @@ static int setup_tdparams_cpuids(struct kvm_cpuid2 *cpuid,
 		if (!entry)
 			continue;
 
-		if (tdx_unsupported_cpuid(entry))
+		if (tdx_is_unsupported_cpuid_entry(entry))
 			return -EINVAL;
 
 		copy_cnt++;
#16 lore raw

Re: [PATCH v2 3/3] KVM: TDX: Reject KVM_TDX_INIT_VM if indexed CPUID functions don't match

Quoted 48 lines · Binbin Wu
Quoted 34 lines · Sean Christopherson

Reject KVM_TDX_INIT_VM if userspace doesn't configure the canonical CPUID entries correctly with respect to KVM_CPUID_FLAG_SIGNIFCANT_INDEX to fix a bug where userspace could bypass the TSX and WAITPKG checks by simply not marking a CPUID.0x7 entry as being indexed. This also sets the stage for more in-depth validation of the incoming CPUID configuration.

This is obviously an ABI change and diverges from KVM's general approach of "let userspace do what it wants". In practice, no "real" VMM that supports TDX misconfigures a TD's CPUID entries in this way, i.e. the probability of actually breaking userspace is basically nil. And unlike normal VMs, where KVM has full control and thus can deal with bizarre CPUID input without too much fuss, TDX VMs require extra care due to the TDX Module also reacting to the CPUID configuration, i.e. KVM_TDX_INIT_VM needs to be more strict than KVM_SET_CPUID{,2} to avoid creating potentially dangerous-to-the-host setups.

Opportunistically rename the helper to make it more obvious that it's checking if the passed in @entry is unsupported, as opposed to returning a set of unsupported entries or something.

Fixes 6d415778f106 ("KVM: TDX: Disable support for TSX and WAITPKG")
---
 arch/x86/kvm/vmx/tdx.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +0 -0
index 37d088f6f7a8..6abf92ffec24 100644

arch/x86/kvm/vmx/tdx.c static void tdx_clear_unsupported_cpuid(struct kvm_cpuid_entry2 *entry) · line 158

Quoted 9 lines
arch/x86/kvm/vmx/tdx.c +4 -1
@@ -151,8 +151,12 @@ static void tdx_clear_unsupported_cpuid(struct kvm_cpuid_entry2 *entry)
 		clear_waitpkg(entry);
 }
 
-static bool tdx_unsupported_cpuid(const struct kvm_cpuid_entry2 *entry)
+static bool tdx_is_unsupported_cpuid_entry(const struct kvm_cpuid_entry2 *entry)
 {
+	if (cpuid_function_is_indexed(entry->function) !=
+	    !!(entry->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX))
+		return true;

Similar to patch 2, if cpuid_function_is_indexed(entry->function) misses some function, the value of cpuid_function_is_indexed(entry->function) is not accurate.

Quoted 4 lines

For current code, it's OK, since all unsupported bits are in leaf 7. In the future, with the TDX CPUID validation patch set
https://lore.kernel.org/all/[email protected]/
It still allow userspace to by pass some check in theory.

some idea.

If we decide to honor TDX module's report over KVM's cpuid_function_is_indexed() in patch2, then how about something below:

arch/x86/kvm/vmx/tdx.c +0 -0
index 6abf92ffec24..1456f4c5c6b3 100644
@@ -151,10 +151,10 @@ static void tdx_clear_unsupported_cpuid(struct

kvm_cpuid_entry2 *entry)

                clear_waitpkg(entry);

}

-static bool tdx_is_unsupported_cpuid_entry(const struct kvm_cpuid_entry2 *entry) +static bool tdx_is_unsupported_cpuid_entry(const struct kvm_cpuid_entry2 *entry,
+ bool significant_index)
{
- if (cpuid_function_is_indexed(entry->function) !=
- !!(entry->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX))
+ if (significant_index && !(entry->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX))

                return true;

        return has_tsx(entry) || has_waitpkg(entry);
@@ -2397,7 +2397,8 @@ static int setup_tdparams_cpuids(struct kvm_cpuid2 *cpuid,
                if (!entry)
                        continue;

- if (tdx_is_unsupported_cpuid_entry(entry))
+ if (tdx_is_unsupported_cpuid_entry(entry,
+ tmp->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX))

                        return -EINVAL;

                copy_cnt++;
Reviewed-by on v2 3/3
#17 lore raw

Re: [PATCH v2 3/3] KVM: TDX: Reject KVM_TDX_INIT_VM if indexed CPUID functions don't match

In reply to Sean Christopherson
Quoted 20 lines · Sean Christopherson

Reject KVM_TDX_INIT_VM if userspace doesn't configure the canonical CPUID entries correctly with respect to KVM_CPUID_FLAG_SIGNIFCANT_INDEX to fix a bug where userspace could bypass the TSX and WAITPKG checks by simply not marking a CPUID.0x7 entry as being indexed. This also sets the stage for more in-depth validation of the incoming CPUID configuration.

This is obviously an ABI change and diverges from KVM's general approach of "let userspace do what it wants". In practice, no "real" VMM that supports TDX misconfigures a TD's CPUID entries in this way, i.e. the probability of actually breaking userspace is basically nil. And unlike normal VMs, where KVM has full control and thus can deal with bizarre CPUID input without too much fuss, TDX VMs require extra care due to the TDX Module also reacting to the CPUID configuration, i.e. KVM_TDX_INIT_VM needs to be more strict than KVM_SET_CPUID{,2} to avoid creating potentially dangerous-to-the-host setups.

Opportunistically rename the helper to make it more obvious that it's checking if the passed in @entry is unsupported, as opposed to returning a set of unsupported entries or something.

Quoted 5 lines
Fixes 6d415778f106 ("KVM: TDX: Disable support for TSX and WAITPKG")

Given the discussion of patch2 has settled down,

maintainer Tested-by on the v2 series
#18 lore raw
In reply to Sean Christopherson
Quoted 8 lines · Sean Christopherson

Changyuan Lyu (1):
  KVM: TDX: Set SIGNIFCANT_INDEX flag as appropriate when filling CPUID entries

Sean Christopherson (2):
  x86/cpuid: Mark CPUID function 0x23 as being indexed
  KVM: TDX: Reject KVM_TDX_INIT_VM if indexed CPUID functions don't match

Ran it through our automated tests:

Reviewed-by on the v2 series
#20 lore raw
In reply to Sean Christopherson
Quoted 9 lines · Sean Christopherson

Changyuan Lyu (1):
KVM: TDX: Set SIGNIFCANT_INDEX flag as appropriate when filling CPUID
entries

Sean Christopherson (2):
x86/cpuid: Mark CPUID function 0x23 as being indexed
KVM: TDX: Reject KVM_TDX_INIT_VM if indexed CPUID functions don't
match

author maintainer
#21 lore raw
In reply to Sean Christopherson
Quoted 9 lines · Sean Christopherson

Boris or Dave, can you grab patch 1 for 7.4, ideally sooner than later? That would avoid having to carry a stable topic branch for a trivial change.

Annotate CPUID entries that have a significant index when reporting TDX capabilities to userspace, and enforce that userspace honors significant indices to avoid creating a misconfiguring between KVM and the TDX Module, which in the worst case scenario could lead to host state getting clobbered.

[...]

Applied 2 and 3 to kvm-x86 vmx. Thanks Boris for fast-tracking the CPUID change!

[1/3] x86/cpuid: Mark CPUID function 0x23 as being indexed

      [already upstream as 108775fa28fe04a16f19a828a779689f3d0ea9f0]
[2/3] KVM: TDX: Set SIGNIFCANT_INDEX flag as appropriate when filling CPUID entries
      https://github.com/kvm-x86/linux/commit/bfce14e80e89
[3/3] KVM: TDX: Reject KVM_TDX_INIT_VM if indexed CPUID functions don't match
      https://github.com/kvm-x86/linux/commit/c576a80cfc4e

--
https://github.com/kvm-x86/linux/tree/next