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

From: Binbin Wu

Date: Thu Oct 22 2026 - 22:46:26 EST


On 10/23/2026 5:01 AM, Sean Christopherson wrote:
> From: Changyuan Lyu <changyuanl@xxxxxxxxxx>
>
> 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: stable@xxxxxxxxxxxxxxx
> Cc: Binbin Wu <binbin.wu@xxxxxxxxxxxxxxx>
> Suggested-by: Sean Christopherson <seanjc@xxxxxxxxxx>
> Signed-off-by: Changyuan Lyu <changyuanl@xxxxxxxxxx>
> [sean: keep the WARN, don't key off KVM_TDX_CPUID_NO_SUBLEAF, add comment]
> Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
> ---
> arch/x86/kvm/vmx/tdx.c | 14 +++++++++++++-
> 1 file changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
> index 271c2f94b8dd..37d088f6f7a8 100644
> --- a/arch/x86/kvm/vmx/tdx.c
> +++ b/arch/x86/kvm/vmx/tdx.c
> @@ -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))

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?

> + entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
> + else
> entry->index = 0;
>
> /*