Re: [PATCH v2 25/49] KVM: x86: Harden CPU capabilities processing against out-of-scope features

From: Sean Christopherson
Date: Tue Jul 09 2024 - 14:11:36 EST


On Thu, Jul 04, 2024, Maxim Levitsky wrote:
> On Fri, 2024-05-17 at 10:39 -0700, Sean Christopherson wrote:
> > +/*
> > + * For kernel-defined leafs, mask the boot CPU's pre-populated value. For KVM-
> > + * defined leafs, explicitly set the leaf, as KVM is the one and only authority.
> > + */
> > +#define kvm_cpu_cap_init(leaf, mask) \
> > +do { \
> > + const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32); \
> > + const u32 __maybe_unused kvm_cpu_cap_init_in_progress = leaf; \
>
> Why not to #define the kvm_cpu_cap_init_in_progress as well instead of a variable?

Macros can't #define new macros. A macro could be used, but it would require the
caller to #define and #undef the macro, e.g.

#define kvm_cpu_cap_init_in_progress CPUID_1_ECX
kvm_cpu_cap_init(CPUID_1_ECX, ...)
#undef kvm_cpu_cap_init_in_progress

but, stating the obvious, that's ugly and is less robust than automatically
"defining" the in-progress leaf in kvm_cpu_cap_init().