public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Binbin Wu <binbin.wu@linux.intel.com>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Mathias Krause <minipli@grsecurity.net>,
	John Allen <john.allen@amd.com>,
	Rick Edgecombe <rick.p.edgecombe@intel.com>,
	Chao Gao <chao.gao@intel.com>, Xiaoyao Li <xiaoyao.li@intel.com>,
	Jim Mattson <jmattson@google.com>
Subject: Re: [PATCH v2 2/3] KVM: x86: Harden against unexpected adjustments to kvm_cpu_caps
Date: Fri, 30 Jan 2026 16:59:29 +0800	[thread overview]
Message-ID: <a6028500-167a-4900-8ed4-e3d7851969e4@linux.intel.com> (raw)
In-Reply-To: <20260128014310.3255561-3-seanjc@google.com>



On 1/28/2026 9:43 AM, Sean Christopherson wrote:
> Add a flag to track when KVM is actively configuring its CPU caps, and
> WARN if a cap is set or cleared if KVM isn't in its configuration stage.
> Modifying CPU caps after {svm,vmx}_set_cpu_caps() can be fatal to KVM, as
> vendor setup code expects the CPU caps to be frozen at that point, e.g.
> will do additional configuration based on the caps.
> 
> Rename kvm_set_cpu_caps() to kvm_initialize_cpu_caps() to pair with the
> new "finalize", and to make it more obvious that KVM's CPU caps aren't
> fully configured within the function.
> 

Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>

> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
>  arch/x86/kvm/cpuid.c   | 10 ++++++++--
>  arch/x86/kvm/cpuid.h   | 12 +++++++++++-
>  arch/x86/kvm/svm/svm.c |  4 +++-
>  arch/x86/kvm/vmx/vmx.c |  4 +++-
>  4 files changed, 25 insertions(+), 5 deletions(-)
> 
> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
> index 575244af9c9f..7fe4e58a6ebf 100644
> --- a/arch/x86/kvm/cpuid.c
> +++ b/arch/x86/kvm/cpuid.c
> @@ -36,6 +36,9 @@
>  u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly;
>  EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_cpu_caps);
>  
> +bool kvm_is_configuring_cpu_caps __read_mostly;
> +EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_is_configuring_cpu_caps);
> +
>  struct cpuid_xstate_sizes {
>  	u32 eax;
>  	u32 ebx;
> @@ -826,10 +829,13 @@ do {									\
>  /* DS is defined by ptrace-abi.h on 32-bit builds. */
>  #undef DS
>  
> -void kvm_set_cpu_caps(void)
> +void kvm_initialize_cpu_caps(void)
>  {
>  	memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps));
>  
> +	WARN_ON_ONCE(kvm_is_configuring_cpu_caps);
> +	kvm_is_configuring_cpu_caps = true;
> +
>  	BUILD_BUG_ON(sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)) >
>  		     sizeof(boot_cpu_data.x86_capability));
>  
> @@ -1289,7 +1295,7 @@ void kvm_set_cpu_caps(void)
>  		kvm_cpu_cap_clear(X86_FEATURE_RDPID);
>  	}
>  }
> -EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_set_cpu_caps);
> +EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_initialize_cpu_caps);
>  
>  #undef F
>  #undef SCATTERED_F
> diff --git a/arch/x86/kvm/cpuid.h b/arch/x86/kvm/cpuid.h
> index d3f5ae15a7ca..039b8e6f40ba 100644
> --- a/arch/x86/kvm/cpuid.h
> +++ b/arch/x86/kvm/cpuid.h
> @@ -8,7 +8,15 @@
>  #include <uapi/asm/kvm_para.h>
>  
>  extern u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly;
> -void kvm_set_cpu_caps(void);
> +extern bool kvm_is_configuring_cpu_caps __read_mostly;
> +
> +void kvm_initialize_cpu_caps(void);
> +
> +static inline void kvm_finalize_cpu_caps(void)
> +{
> +	WARN_ON_ONCE(!kvm_is_configuring_cpu_caps);
> +	kvm_is_configuring_cpu_caps = false;
> +}
>  
>  void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu);
>  struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2(struct kvm_cpuid_entry2 *entries,
> @@ -188,6 +196,7 @@ static __always_inline void kvm_cpu_cap_clear(unsigned int x86_feature)
>  {
>  	unsigned int x86_leaf = __feature_leaf(x86_feature);
>  
> +	WARN_ON_ONCE(!kvm_is_configuring_cpu_caps);
>  	kvm_cpu_caps[x86_leaf] &= ~__feature_bit(x86_feature);
>  }
>  
> @@ -195,6 +204,7 @@ static __always_inline void kvm_cpu_cap_set(unsigned int x86_feature)
>  {
>  	unsigned int x86_leaf = __feature_leaf(x86_feature);
>  
> +	WARN_ON_ONCE(!kvm_is_configuring_cpu_caps);
>  	kvm_cpu_caps[x86_leaf] |= __feature_bit(x86_feature);
>  }
>  
> diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> index c00a696dacfc..5f0136dbdde6 100644
> --- a/arch/x86/kvm/svm/svm.c
> +++ b/arch/x86/kvm/svm/svm.c
> @@ -5305,7 +5305,7 @@ static __init void svm_adjust_mmio_mask(void)
>  
>  static __init void svm_set_cpu_caps(void)
>  {
> -	kvm_set_cpu_caps();
> +	kvm_initialize_cpu_caps();
>  
>  	kvm_caps.supported_perf_cap = 0;
>  
> @@ -5389,6 +5389,8 @@ static __init void svm_set_cpu_caps(void)
>  	kvm_cpu_cap_clear(X86_FEATURE_MSR_IMM);
>  
>  	kvm_setup_xss_caps();
> +
> +	kvm_finalize_cpu_caps();
>  }
>  
>  static __init int svm_hardware_setup(void)
> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index 9f85c3829890..93ec1e6181e4 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
> @@ -8173,7 +8173,7 @@ static __init u64 vmx_get_perf_capabilities(void)
>  
>  static __init void vmx_set_cpu_caps(void)
>  {
> -	kvm_set_cpu_caps();
> +	kvm_initialize_cpu_caps();
>  
>  	/* CPUID 0x1 */
>  	if (nested)
> @@ -8232,6 +8232,8 @@ static __init void vmx_set_cpu_caps(void)
>  	}
>  
>  	kvm_setup_xss_caps();
> +
> +	kvm_finalize_cpu_caps();
>  }
>  
>  static bool vmx_is_io_intercepted(struct kvm_vcpu *vcpu,


  parent reply	other threads:[~2026-01-30  8:59 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-28  1:43 [PATCH v2 0/3] KVM: x86: CET vs. nVMX fix and hardening Sean Christopherson
2026-01-28  1:43 ` [PATCH v2 1/3] KVM: x86: Explicitly configure supported XSS from {svm,vmx}_set_cpu_caps() Sean Christopherson
2026-01-29  2:55   ` Xiaoyao Li
2026-01-29 15:03     ` Sean Christopherson
2026-01-29  7:34   ` Binbin Wu
2026-01-30  3:23     ` Xiaoyao Li
2026-01-30  5:06       ` Binbin Wu
2026-01-30  8:56   ` Binbin Wu
2026-01-28  1:43 ` [PATCH v2 2/3] KVM: x86: Harden against unexpected adjustments to kvm_cpu_caps Sean Christopherson
2026-01-29  3:09   ` Xiaoyao Li
2026-01-29 15:12     ` Sean Christopherson
2026-01-30  3:01       ` Xiaoyao Li
2026-01-30  8:59   ` Binbin Wu [this message]
2026-01-28  1:43 ` [PATCH v2 3/3] KVM: VMX: Print out "bad" offsets+value on VMCS config mismatch Sean Christopherson
2026-01-29  3:16   ` Xiaoyao Li
2026-01-30  9:17   ` Binbin Wu
2026-02-04  0:10 ` [PATCH v2 0/3] KVM: x86: CET vs. nVMX fix and hardening 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=a6028500-167a-4900-8ed4-e3d7851969e4@linux.intel.com \
    --to=binbin.wu@linux.intel.com \
    --cc=chao.gao@intel.com \
    --cc=jmattson@google.com \
    --cc=john.allen@amd.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=minipli@grsecurity.net \
    --cc=pbonzini@redhat.com \
    --cc=rick.p.edgecombe@intel.com \
    --cc=seanjc@google.com \
    --cc=xiaoyao.li@intel.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