[v2,14/18] KVM: SVM: Check that the current CPU supports SVM in kvm_is_svm_supported()

Message ID 20230310214232.806108-15-seanjc@google.com
State New
Headers
Series x86/reboot: KVM: Clean up "emergency" virt code |

Commit Message

Sean Christopherson March 10, 2023, 9:42 p.m. UTC
  Check "this" CPU instead of the boot CPU when querying SVM support so that
the per-CPU checks done during hardware enabling actually function as
intended, i.e. will detect issues where SVM isn't support on all CPUs.

Disable migration for the use from svm_init() mostly so that the standard
accessors for the per-CPU data can be used without getting yelled at by
CONFIG_DEBUG_PREEMPT=y sanity checks.  Preventing the "disabled by BIOS"
error message from reporting the wrong CPU is largely a bonus, as ensuring
a stable CPU during module load is a non-goal for KVM.

Link: https://lore.kernel.org/all/ZAdxNgv0M6P63odE@google.com
Cc: Kai Huang <kai.huang@intel.com>
Cc: Chao Gao <chao.gao@intel.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/svm/svm.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
  

Comments

Kai Huang March 13, 2023, 2:47 a.m. UTC | #1
On Fri, 2023-03-10 at 13:42 -0800, Sean Christopherson wrote:
> Check "this" CPU instead of the boot CPU when querying SVM support so that
> the per-CPU checks done during hardware enabling actually function as
> intended, i.e. will detect issues where SVM isn't support on all CPUs.
> 
> Disable migration for the use from svm_init() mostly so that the standard
> accessors for the per-CPU data can be used without getting yelled at by
> CONFIG_DEBUG_PREEMPT=y sanity checks.  Preventing the "disabled by BIOS"
> error message from reporting the wrong CPU is largely a bonus, as ensuring
> a stable CPU during module load is a non-goal for KVM.
> 
> Link: https://lore.kernel.org/all/ZAdxNgv0M6P63odE@google.com
> Cc: Kai Huang <kai.huang@intel.com>
> Cc: Chao Gao <chao.gao@intel.com>
> Signed-off-by: Sean Christopherson <seanjc@google.com>

Should we add:

Fixes: c82a5c5c53c5 ("KVM: x86: Do compatibility checks when onlining CPU")

As that commit introduced using raw_smp_processor_id() to get CPU id in
kvm_is_svm_supported() and print the CPU id out in error message?

> ---
>  arch/x86/kvm/svm/svm.c | 25 +++++++++++++++++++------
>  1 file changed, 19 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> index 2934f185960d..f04b61c3d9d8 100644
> --- a/arch/x86/kvm/svm/svm.c
> +++ b/arch/x86/kvm/svm/svm.c
> @@ -520,18 +520,20 @@ static void svm_init_osvw(struct kvm_vcpu *vcpu)
>  		vcpu->arch.osvw.status |= 1;
>  }
>  
> -static bool kvm_is_svm_supported(void)
> +static bool __kvm_is_svm_supported(void)
>  {
> -	int cpu = raw_smp_processor_id();
> +	int cpu = smp_processor_id();

Since we have made sure __kvm_is_svm_supported() is always performed on a stable
cpu, should we keep using raw_smp_processor_id()?  

It is faster than smp_processor_id() when CONFIG_DEBUG_PREEMPT=y, but yes the
latter can help to catch bug.

> +	struct cpuinfo_x86 *c = &cpu_data(cpu);
> +
>  	u64 vm_cr;
>  
> -	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
> -	    boot_cpu_data.x86_vendor != X86_VENDOR_HYGON) {
> +	if (c->x86_vendor != X86_VENDOR_AMD &&
> +	    c->x86_vendor != X86_VENDOR_HYGON) {
>  		pr_err("CPU %d isn't AMD or Hygon\n", cpu);
>  		return false;
>  	}
>  
> -	if (!boot_cpu_has(X86_FEATURE_SVM)) {
> +	if (!cpu_has(c, X86_FEATURE_SVM)) {
>  		pr_err("SVM not supported by CPU %d\n", cpu);
>  		return false;
>  	}
> @@ -550,9 +552,20 @@ static bool kvm_is_svm_supported(void)
>  	return true;
>  }
>  
> +static bool kvm_is_svm_supported(void)
> +{
> +	bool supported;
> +
> +	migrate_disable();
> +	supported = __kvm_is_svm_supported();
> +	migrate_enable();
> +
> +	return supported;
> +}
> +
>  static int svm_check_processor_compat(void)
>  {
> -	if (!kvm_is_svm_supported())
> +	if (!__kvm_is_svm_supported())
>  		return -EIO;
>  
>  	return 0;
> -- 
> 2.40.0.rc1.284.g88254d51c5-goog
>
  
Sean Christopherson March 13, 2023, 5:29 p.m. UTC | #2
On Mon, Mar 13, 2023, Huang, Kai wrote:
> On Fri, 2023-03-10 at 13:42 -0800, Sean Christopherson wrote:
> > Check "this" CPU instead of the boot CPU when querying SVM support so that
> > the per-CPU checks done during hardware enabling actually function as
> > intended, i.e. will detect issues where SVM isn't support on all CPUs.
> > 
> > Disable migration for the use from svm_init() mostly so that the standard
> > accessors for the per-CPU data can be used without getting yelled at by
> > CONFIG_DEBUG_PREEMPT=y sanity checks.  Preventing the "disabled by BIOS"
> > error message from reporting the wrong CPU is largely a bonus, as ensuring
> > a stable CPU during module load is a non-goal for KVM.
> > 
> > Link: https://lore.kernel.org/all/ZAdxNgv0M6P63odE@google.com
> > Cc: Kai Huang <kai.huang@intel.com>
> > Cc: Chao Gao <chao.gao@intel.com>
> > Signed-off-by: Sean Christopherson <seanjc@google.com>
> 
> Should we add:
> 
> Fixes: c82a5c5c53c5 ("KVM: x86: Do compatibility checks when onlining CPU")
> 
> As that commit introduced using raw_smp_processor_id() to get CPU id in
> kvm_is_svm_supported() and print the CPU id out in error message?

My vote is to not to add a Fixes because using raw_smp_processor_id() and not disabling
migration for module probe case was deliberate and is safe.  I don't want to give the
impression that the existing code is functionally broken.  The only quirk is that
the reporting could be misleading.

That said, I'm not against adding a Fixes tag, because I certainly can't argue
against the reporting being flawed.

> > ---
> >  arch/x86/kvm/svm/svm.c | 25 +++++++++++++++++++------
> >  1 file changed, 19 insertions(+), 6 deletions(-)
> > 
> > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> > index 2934f185960d..f04b61c3d9d8 100644
> > --- a/arch/x86/kvm/svm/svm.c
> > +++ b/arch/x86/kvm/svm/svm.c
> > @@ -520,18 +520,20 @@ static void svm_init_osvw(struct kvm_vcpu *vcpu)
> >  		vcpu->arch.osvw.status |= 1;
> >  }
> >  
> > -static bool kvm_is_svm_supported(void)
> > +static bool __kvm_is_svm_supported(void)
> >  {
> > -	int cpu = raw_smp_processor_id();
> > +	int cpu = smp_processor_id();
> 
> Since we have made sure __kvm_is_svm_supported() is always performed on a stable
> cpu, should we keep using raw_smp_processor_id()? �
> 
> It is faster than smp_processor_id() when CONFIG_DEBUG_PREEMPT=y, but yes the
> latter can help to catch bug.

Most kernels with any amount of CONFIG_DEBUG_* options enabled are comically slow
anyways, I much prefer having the sanity checks than the performance.
  
Kai Huang March 14, 2023, 12:17 a.m. UTC | #3
On Mon, 2023-03-13 at 10:29 -0700, Sean Christopherson wrote:
> On Mon, Mar 13, 2023, Huang, Kai wrote:
> > On Fri, 2023-03-10 at 13:42 -0800, Sean Christopherson wrote:
> > > Check "this" CPU instead of the boot CPU when querying SVM support so that
> > > the per-CPU checks done during hardware enabling actually function as
> > > intended, i.e. will detect issues where SVM isn't support on all CPUs.
> > > 
> > > Disable migration for the use from svm_init() mostly so that the standard
> > > accessors for the per-CPU data can be used without getting yelled at by
> > > CONFIG_DEBUG_PREEMPT=y sanity checks.  Preventing the "disabled by BIOS"
> > > error message from reporting the wrong CPU is largely a bonus, as ensuring
> > > a stable CPU during module load is a non-goal for KVM.
> > > 
> > > Link: https://lore.kernel.org/all/ZAdxNgv0M6P63odE@google.com
> > > Cc: Kai Huang <kai.huang@intel.com>
> > > Cc: Chao Gao <chao.gao@intel.com>
> > > Signed-off-by: Sean Christopherson <seanjc@google.com>
> > 
> > Should we add:
> > 
> > Fixes: c82a5c5c53c5 ("KVM: x86: Do compatibility checks when onlining CPU")
> > 
> > As that commit introduced using raw_smp_processor_id() to get CPU id in
> > kvm_is_svm_supported() and print the CPU id out in error message?
> 
> My vote is to not to add a Fixes because using raw_smp_processor_id() and not disabling
> migration for module probe case was deliberate and is safe.  I don't want to give the
> impression that the existing code is functionally broken.  The only quirk is that
> the reporting could be misleading.
> 
> That said, I'm not against adding a Fixes tag, because I certainly can't argue
> against the reporting being flawed.

Yeah the only issue is the reporting.

And I will leave this to others.

> 
> > > ---
> > >  arch/x86/kvm/svm/svm.c | 25 +++++++++++++++++++------
> > >  1 file changed, 19 insertions(+), 6 deletions(-)
> > > 
> > > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> > > index 2934f185960d..f04b61c3d9d8 100644
> > > --- a/arch/x86/kvm/svm/svm.c
> > > +++ b/arch/x86/kvm/svm/svm.c
> > > @@ -520,18 +520,20 @@ static void svm_init_osvw(struct kvm_vcpu *vcpu)
> > >  		vcpu->arch.osvw.status |= 1;
> > >  }
> > >  
> > > -static bool kvm_is_svm_supported(void)
> > > +static bool __kvm_is_svm_supported(void)
> > >  {
> > > -	int cpu = raw_smp_processor_id();
> > > +	int cpu = smp_processor_id();
> > 
> > Since we have made sure __kvm_is_svm_supported() is always performed on a stable
> > cpu, should we keep using raw_smp_processor_id()? �
> > 
> > It is faster than smp_processor_id() when CONFIG_DEBUG_PREEMPT=y, but yes the
> > latter can help to catch bug.
> 
> Most kernels with any amount of CONFIG_DEBUG_* options enabled are comically slow
> anyways, I much prefer having the sanity checks than the performance.

Yeah fine to me.
  

Patch

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 2934f185960d..f04b61c3d9d8 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -520,18 +520,20 @@  static void svm_init_osvw(struct kvm_vcpu *vcpu)
 		vcpu->arch.osvw.status |= 1;
 }
 
-static bool kvm_is_svm_supported(void)
+static bool __kvm_is_svm_supported(void)
 {
-	int cpu = raw_smp_processor_id();
+	int cpu = smp_processor_id();
+	struct cpuinfo_x86 *c = &cpu_data(cpu);
+
 	u64 vm_cr;
 
-	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
-	    boot_cpu_data.x86_vendor != X86_VENDOR_HYGON) {
+	if (c->x86_vendor != X86_VENDOR_AMD &&
+	    c->x86_vendor != X86_VENDOR_HYGON) {
 		pr_err("CPU %d isn't AMD or Hygon\n", cpu);
 		return false;
 	}
 
-	if (!boot_cpu_has(X86_FEATURE_SVM)) {
+	if (!cpu_has(c, X86_FEATURE_SVM)) {
 		pr_err("SVM not supported by CPU %d\n", cpu);
 		return false;
 	}
@@ -550,9 +552,20 @@  static bool kvm_is_svm_supported(void)
 	return true;
 }
 
+static bool kvm_is_svm_supported(void)
+{
+	bool supported;
+
+	migrate_disable();
+	supported = __kvm_is_svm_supported();
+	migrate_enable();
+
+	return supported;
+}
+
 static int svm_check_processor_compat(void)
 {
-	if (!kvm_is_svm_supported())
+	if (!__kvm_is_svm_supported())
 		return -EIO;
 
 	return 0;