Re: [PATCH v3 1/4] KVM: TDX: Track configurable CPUID bits allowed by KVM

From: Binbin Wu

Date: Tue Sep 01 2026 - 04:29:49 EST


On 9/1/2026 2:29 PM, Tony Lindgren wrote:
> On Thu, Aug 27, 2026 at 11:18:34AM +0800, Binbin Wu wrote:
>> --- a/arch/x86/kvm/vmx/tdx.c
>> +++ b/arch/x86/kvm/vmx/tdx.c
>> @@ -52,6 +52,149 @@
>> __TDX_BUG_ON(__err, #__fn, __kvm, ", " #a1 " 0x%llx, " #a2 ", 0x%llx, " #a3 " 0x%llx", \
>> a1, a2, a3)
>>
>> +static u32 tdx_cpu_cfg_caps[NR_KVM_CPU_CAPS] __ro_after_init;
>> +static_assert(ARRAY_SIZE(tdx_cpu_cfg_caps) == ARRAY_SIZE(kvm_cpu_caps));
>> +
>> +#define TDX_VALIDATE_CPU_CAP_USAGE(name) \
>> + BUILD_BUG_ON(__feature_leaf(X86_FEATURE_##name) != \
>> + tdx_cpu_cap_init_in_progress)
>> +
>> +/* For feature bit that KVM advertised through kvm_cpu_caps[]. */
>> +#define TDX_CFG_F(name) \
>> +({ \
>> + TDX_VALIDATE_CPU_CAP_USAGE(name); \
>> + tdx_cfg_caps |= feature_bit(name); \
>> +})
>> +
>> +/*
>> + * For feature bit KVM allows for TDX guests even though it is not advertised
>> + * through kvm_cpu_caps[], e.g. MWAIT.
>> + */
>> +#define TDX_CFG_EXTRA_F(name) \
>> +({ \
>> + TDX_VALIDATE_CPU_CAP_USAGE(name); \
>> + tdx_cfg_extra_caps |= feature_bit(name); \
>> +})
>> +
>> +#define tdx_cpu_cfg_cap_init(leaf, feature_initializers...) \
>> +do { \
>> + const u32 __maybe_unused tdx_cpu_cap_init_in_progress = leaf; \
>> + u32 tdx_cfg_extra_caps = 0; \
>> + u32 tdx_cfg_caps = 0; \
>> + \
>> + feature_initializers \
>> + tdx_cpu_cfg_caps[leaf] = (tdx_cfg_caps & kvm_cpu_caps[leaf]) | \
>> + tdx_cfg_extra_caps; \
>> +} while (0)
>
> How about make some of the above into just static functions for easier
> readabilyt?

Thanks for your review!

This deliberately mirrors the existing kvm_cpu_cap_init() / F() / XXX_F()
pattern in arch/x86/kvm/cpuid.c.

Similar to kvm_cpu_cap_init(), tdx_cpu_cfg_cap_init() takes a variadic list of
statements that mutate two local accumulators (tdx_cfg_caps, tdx_cfg_extra_caps).
That's inherently a macro-scoped construct.

>
> And then drop the __maybe_unused for tdx_cpu_cap_init_in_progress?