public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] KVM: SVM: small tweaks for sev_hardware_setup
@ 2023-04-04 12:26 Alexander Mikhalitsyn
  2023-04-04 12:26 ` [PATCH 1/2] KVM: SVM: free sev_*asid_bitmap init if SEV init fails Alexander Mikhalitsyn
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Alexander Mikhalitsyn @ 2023-04-04 12:26 UTC (permalink / raw)
  To: pbonzini
  Cc: Alexander Mikhalitsyn, Sean Christopherson, Stéphane Graber,
	kvm, linux-kernel

KVM: SVM: add some info prints to SEV init
    
Let's add a few pr_info's to sev_hardware_setup to make SEV/SEV-ES
enabling a little bit handier for users. Right now it's too hard
to guess why SEV/SEV-ES are failing to enable.

There are a few reasons.
SEV:
- npt is disabled (module parameter)
- CPU lacks some features (sev, decodeassists)
- Maximum SEV ASID is 0

SEV-ES:
- mmio_caching is disabled (module parameter)
- CPU lacks sev_es feature
- Minimum SEV ASID value is 1 (can be adjusted in BIOS/UEFI)

==
   
KVM: SVM: free sev_*asid_bitmap init if SEV init fails

If misc_cg_set_capacity() fails for some reason then we have
a memleak for sev_reclaim_asid_bitmap/sev_asid_bitmap. It's
not a case right now, because misc_cg_set_capacity() just can't
fail and check inside it is always successful.

But let's fix that for code consistency.

Cc: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Stéphane Graber <stgraber@ubuntu.com>
Cc: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>

Alexander Mikhalitsyn (2):
  KVM: SVM: free sev_*asid_bitmap init if SEV init fails
  KVM: SVM: add some info prints to SEV init

 arch/x86/kvm/svm/sev.c | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 1/2] KVM: SVM: free sev_*asid_bitmap init if SEV init fails
  2023-04-04 12:26 [PATCH 0/2] KVM: SVM: small tweaks for sev_hardware_setup Alexander Mikhalitsyn
@ 2023-04-04 12:26 ` Alexander Mikhalitsyn
  2023-04-11 19:47   ` Zhi Wang
  2023-04-04 12:26 ` [PATCH 2/2] KVM: SVM: add some info prints to SEV init Alexander Mikhalitsyn
  2023-04-06  3:32 ` [PATCH 0/2] KVM: SVM: small tweaks for sev_hardware_setup Sean Christopherson
  2 siblings, 1 reply; 13+ messages in thread
From: Alexander Mikhalitsyn @ 2023-04-04 12:26 UTC (permalink / raw)
  To: pbonzini
  Cc: Alexander Mikhalitsyn, Sean Christopherson, Stéphane Graber,
	kvm, linux-kernel

If misc_cg_set_capacity() fails for some reason then we have
a memleak for sev_reclaim_asid_bitmap/sev_asid_bitmap. It's
not a case right now, because misc_cg_set_capacity() just can't
fail and check inside it is always successful.

But let's fix that for code consistency.

Cc: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Stéphane Graber <stgraber@ubuntu.com>
Cc: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
---
 arch/x86/kvm/svm/sev.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index c25aeb550cd9..a42536a0681a 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -2213,8 +2213,13 @@ void __init sev_hardware_setup(void)
 	}
 
 	sev_asid_count = max_sev_asid - min_sev_asid + 1;
-	if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count))
+	if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count)) {
+		bitmap_free(sev_reclaim_asid_bitmap);
+		sev_reclaim_asid_bitmap = NULL;
+		bitmap_free(sev_asid_bitmap);
+		sev_asid_bitmap = NULL;
 		goto out;
+	}
 
 	pr_info("SEV supported: %u ASIDs\n", sev_asid_count);
 	sev_supported = true;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 2/2] KVM: SVM: add some info prints to SEV init
  2023-04-04 12:26 [PATCH 0/2] KVM: SVM: small tweaks for sev_hardware_setup Alexander Mikhalitsyn
  2023-04-04 12:26 ` [PATCH 1/2] KVM: SVM: free sev_*asid_bitmap init if SEV init fails Alexander Mikhalitsyn
@ 2023-04-04 12:26 ` Alexander Mikhalitsyn
  2023-04-11 19:43   ` Zhi Wang
  2023-05-19 18:17   ` Sean Christopherson
  2023-04-06  3:32 ` [PATCH 0/2] KVM: SVM: small tweaks for sev_hardware_setup Sean Christopherson
  2 siblings, 2 replies; 13+ messages in thread
From: Alexander Mikhalitsyn @ 2023-04-04 12:26 UTC (permalink / raw)
  To: pbonzini
  Cc: Alexander Mikhalitsyn, Sean Christopherson, Stéphane Graber,
	kvm, linux-kernel

Let's add a few pr_info's to sev_hardware_setup to make SEV/SEV-ES
enabling a little bit handier for users. Right now it's too hard
to guess why SEV/SEV-ES are failing to enable.

There are a few reasons.
SEV:
- npt is disabled (module parameter)
- CPU lacks some features (sev, decodeassists)
- Maximum SEV ASID is 0

SEV-ES:
- mmio_caching is disabled (module parameter)
- CPU lacks sev_es feature
- Minimum SEV ASID value is 1 (can be adjusted in BIOS/UEFI)

Cc: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Stéphane Graber <stgraber@ubuntu.com>
Cc: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
---
 arch/x86/kvm/svm/sev.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index a42536a0681a..14cbb8f14c6b 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -2168,17 +2168,24 @@ void __init sev_hardware_setup(void)
 	bool sev_es_supported = false;
 	bool sev_supported = false;
 
-	if (!sev_enabled || !npt_enabled)
+	if (!sev_enabled)
 		goto out;
 
+	if (!npt_enabled) {
+		pr_info("Failed to enable AMD SEV as it requires Nested Paging to be enabled\n");
+		goto out;
+	}
+
 	/*
 	 * SEV must obviously be supported in hardware.  Sanity check that the
 	 * CPU supports decode assists, which is mandatory for SEV guests to
 	 * support instruction emulation.
 	 */
 	if (!boot_cpu_has(X86_FEATURE_SEV) ||
-	    WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_DECODEASSISTS)))
+	    WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_DECODEASSISTS))) {
+		pr_info("Failed to enable AMD SEV as it requires decodeassists and sev CPU features\n");
 		goto out;
+	}
 
 	/* Retrieve SEV CPUID information */
 	cpuid(0x8000001f, &eax, &ebx, &ecx, &edx);
@@ -2188,8 +2195,10 @@ void __init sev_hardware_setup(void)
 
 	/* Maximum number of encrypted guests supported simultaneously */
 	max_sev_asid = ecx;
-	if (!max_sev_asid)
+	if (!max_sev_asid) {
+		pr_info("Failed to enable SEV as the maximum SEV ASID value is 0.\n");
 		goto out;
+	}
 
 	/* Minimum ASID value that should be used for SEV guest */
 	min_sev_asid = edx;
@@ -2234,16 +2243,22 @@ void __init sev_hardware_setup(void)
 	 * instead relies on #NPF(RSVD) being reflected into the guest as #VC
 	 * (the guest can then do a #VMGEXIT to request MMIO emulation).
 	 */
-	if (!enable_mmio_caching)
+	if (!enable_mmio_caching) {
+		pr_info("Failed to enable SEV-ES as it requires MMIO caching to be enabled\n");
 		goto out;
+	}
 
 	/* Does the CPU support SEV-ES? */
-	if (!boot_cpu_has(X86_FEATURE_SEV_ES))
+	if (!boot_cpu_has(X86_FEATURE_SEV_ES)) {
+		pr_info("Failed to enable SEV-ES as it requires sev_es CPU feature\n");
 		goto out;
+	}
 
 	/* Has the system been allocated ASIDs for SEV-ES? */
-	if (min_sev_asid == 1)
+	if (min_sev_asid == 1) {
+		pr_info("Failed to enable SEV-ES as the minimum SEV ASID value is 1.\n");
 		goto out;
+	}
 
 	sev_es_asid_count = min_sev_asid - 1;
 	if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH 0/2] KVM: SVM: small tweaks for sev_hardware_setup
  2023-04-04 12:26 [PATCH 0/2] KVM: SVM: small tweaks for sev_hardware_setup Alexander Mikhalitsyn
  2023-04-04 12:26 ` [PATCH 1/2] KVM: SVM: free sev_*asid_bitmap init if SEV init fails Alexander Mikhalitsyn
  2023-04-04 12:26 ` [PATCH 2/2] KVM: SVM: add some info prints to SEV init Alexander Mikhalitsyn
@ 2023-04-06  3:32 ` Sean Christopherson
  2023-04-06  7:07   ` Aleksandr Mikhalitsyn
  2 siblings, 1 reply; 13+ messages in thread
From: Sean Christopherson @ 2023-04-06  3:32 UTC (permalink / raw)
  To: Alexander Mikhalitsyn; +Cc: pbonzini, Stéphane Graber, kvm, linux-kernel

Please tag patches with RESEND when re-sending the exact patches with different
To/Cc fields.  Not a big deal, but I blinked a few times trying to figure out
if I really had two copies, or if it's just time for me to log off for the night :-)

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 0/2] KVM: SVM: small tweaks for sev_hardware_setup
  2023-04-06  3:32 ` [PATCH 0/2] KVM: SVM: small tweaks for sev_hardware_setup Sean Christopherson
@ 2023-04-06  7:07   ` Aleksandr Mikhalitsyn
  0 siblings, 0 replies; 13+ messages in thread
From: Aleksandr Mikhalitsyn @ 2023-04-06  7:07 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: pbonzini, Stéphane Graber, kvm, linux-kernel

On Thu, Apr 6, 2023 at 5:32 AM Sean Christopherson <seanjc@google.com> wrote:
>
> Please tag patches with RESEND when re-sending the exact patches with different
> To/Cc fields.  Not a big deal, but I blinked a few times trying to figure out
> if I really had two copies, or if it's just time for me to log off for the night :-)

Hi Sean,

yep, I'm sorry about that. First time I've sent patches without proper
CC's by mistake.

Kind regards,
Alex

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 2/2] KVM: SVM: add some info prints to SEV init
  2023-04-04 12:26 ` [PATCH 2/2] KVM: SVM: add some info prints to SEV init Alexander Mikhalitsyn
@ 2023-04-11 19:43   ` Zhi Wang
  2023-04-12 14:55     ` Aleksandr Mikhalitsyn
  2023-05-19 18:17   ` Sean Christopherson
  1 sibling, 1 reply; 13+ messages in thread
From: Zhi Wang @ 2023-04-11 19:43 UTC (permalink / raw)
  To: Alexander Mikhalitsyn
  Cc: pbonzini, Sean Christopherson, Stéphane Graber, kvm,
	linux-kernel

On Tue,  4 Apr 2023 14:26:52 +0200
Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com> wrote:

> Let's add a few pr_info's to sev_hardware_setup to make SEV/SEV-ES
> enabling a little bit handier for users. Right now it's too hard
> to guess why SEV/SEV-ES are failing to enable.
> 
> There are a few reasons.
> SEV:
> - npt is disabled (module parameter)
     ^NPT
> - CPU lacks some features (sev, decodeassists)
> - Maximum SEV ASID is 0
> 
> SEV-ES:
> - mmio_caching is disabled (module parameter)
> - CPU lacks sev_es feature
> - Minimum SEV ASID value is 1 (can be adjusted in BIOS/UEFI)
> 
> Cc: Sean Christopherson <seanjc@google.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Stéphane Graber <stgraber@ubuntu.com>
> Cc: kvm@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> ---
>  arch/x86/kvm/svm/sev.c | 27 +++++++++++++++++++++------
>  1 file changed, 21 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> index a42536a0681a..14cbb8f14c6b 100644
> --- a/arch/x86/kvm/svm/sev.c
> +++ b/arch/x86/kvm/svm/sev.c
> @@ -2168,17 +2168,24 @@ void __init sev_hardware_setup(void)
>  	bool sev_es_supported = false;
>  	bool sev_supported = false;
>  
> -	if (!sev_enabled || !npt_enabled)
> +	if (!sev_enabled)
>  		goto out;
>  
> +	if (!npt_enabled) {
> +		pr_info("Failed to enable AMD SEV as it requires Nested Paging to be enabled\n");
> +		goto out;

Shouldn't we use pr_err() for error message?

> +	}
> +
>  	/*
>  	 * SEV must obviously be supported in hardware.  Sanity check that the
>  	 * CPU supports decode assists, which is mandatory for SEV guests to
>  	 * support instruction emulation.
>  	 */
>  	if (!boot_cpu_has(X86_FEATURE_SEV) ||
> -	    WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_DECODEASSISTS)))
> +	    WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_DECODEASSISTS))) {
> +		pr_info("Failed to enable AMD SEV as it requires decodeassists and sev CPU features\n");
>  		goto out;
> +	}
>  
>  	/* Retrieve SEV CPUID information */
>  	cpuid(0x8000001f, &eax, &ebx, &ecx, &edx);
> @@ -2188,8 +2195,10 @@ void __init sev_hardware_setup(void)
>  
>  	/* Maximum number of encrypted guests supported simultaneously */
>  	max_sev_asid = ecx;
> -	if (!max_sev_asid)
> +	if (!max_sev_asid) {
> +		pr_info("Failed to enable SEV as the maximum SEV ASID value is 0.\n");
>  		goto out;
> +	}
>  
>  	/* Minimum ASID value that should be used for SEV guest */
>  	min_sev_asid = edx;
> @@ -2234,16 +2243,22 @@ void __init sev_hardware_setup(void)
>  	 * instead relies on #NPF(RSVD) being reflected into the guest as #VC
>  	 * (the guest can then do a #VMGEXIT to request MMIO emulation).
>  	 */
> -	if (!enable_mmio_caching)
> +	if (!enable_mmio_caching) {
> +		pr_info("Failed to enable SEV-ES as it requires MMIO caching to be enabled\n");
>  		goto out;
> +	}
>  
>  	/* Does the CPU support SEV-ES? */
> -	if (!boot_cpu_has(X86_FEATURE_SEV_ES))
> +	if (!boot_cpu_has(X86_FEATURE_SEV_ES)) {
> +		pr_info("Failed to enable SEV-ES as it requires sev_es CPU feature\n");
>  		goto out;
> +	}
>  
>  	/* Has the system been allocated ASIDs for SEV-ES? */
> -	if (min_sev_asid == 1)
> +	if (min_sev_asid == 1) {
> +		pr_info("Failed to enable SEV-ES as the minimum SEV ASID value is 1.\n");
>  		goto out;
> +	}
>  
>  	sev_es_asid_count = min_sev_asid - 1;
>  	if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))

As this patch is making sev_hardware_setup()more informative, it would be
better to print both ASID range and count (instead of only ASID count in
the current code). I was suspecting there seems a bug of ASID range allocation
in the current code, but I don't have the HW to test yet... 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/2] KVM: SVM: free sev_*asid_bitmap init if SEV init fails
  2023-04-04 12:26 ` [PATCH 1/2] KVM: SVM: free sev_*asid_bitmap init if SEV init fails Alexander Mikhalitsyn
@ 2023-04-11 19:47   ` Zhi Wang
  2023-04-12 14:52     ` Aleksandr Mikhalitsyn
  0 siblings, 1 reply; 13+ messages in thread
From: Zhi Wang @ 2023-04-11 19:47 UTC (permalink / raw)
  To: Alexander Mikhalitsyn
  Cc: pbonzini, Sean Christopherson, Stéphane Graber, kvm,
	linux-kernel

On Tue,  4 Apr 2023 14:26:51 +0200
Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com> wrote:

> If misc_cg_set_capacity() fails for some reason then we have
> a memleak for sev_reclaim_asid_bitmap/sev_asid_bitmap. It's
> not a case right now, because misc_cg_set_capacity() just can't
> fail and check inside it is always successful.
> 
> But let's fix that for code consistency.
> 
> Cc: Sean Christopherson <seanjc@google.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Stéphane Graber <stgraber@ubuntu.com>
> Cc: kvm@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> ---
>  arch/x86/kvm/svm/sev.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> index c25aeb550cd9..a42536a0681a 100644
> --- a/arch/x86/kvm/svm/sev.c
> +++ b/arch/x86/kvm/svm/sev.c
> @@ -2213,8 +2213,13 @@ void __init sev_hardware_setup(void)
>  	}
>  
>  	sev_asid_count = max_sev_asid - min_sev_asid + 1;
> -	if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count))
> +	if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count)) {
> +		bitmap_free(sev_reclaim_asid_bitmap);
> +		sev_reclaim_asid_bitmap = NULL;
> +		bitmap_free(sev_asid_bitmap);
> +		sev_asid_bitmap = NULL;
>  		goto out;
> +	}
>  
>  	pr_info("SEV supported: %u ASIDs\n", sev_asid_count);
>  	sev_supported = true;

It would be nice that another case can also be fixed:

        sev_es_asid_count = min_sev_asid - 1;
        if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
                goto out; /* <----HERE */

Maybe it would be a good idea to factor out an common error handling path.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/2] KVM: SVM: free sev_*asid_bitmap init if SEV init fails
  2023-04-11 19:47   ` Zhi Wang
@ 2023-04-12 14:52     ` Aleksandr Mikhalitsyn
  2023-04-13  5:07       ` Zhi Wang
  0 siblings, 1 reply; 13+ messages in thread
From: Aleksandr Mikhalitsyn @ 2023-04-12 14:52 UTC (permalink / raw)
  To: Zhi Wang
  Cc: pbonzini, Sean Christopherson, Stéphane Graber, kvm,
	linux-kernel

On Tue, Apr 11, 2023 at 9:47 PM Zhi Wang <zhi.wang.linux@gmail.com> wrote:
>
> On Tue,  4 Apr 2023 14:26:51 +0200
> Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com> wrote:
>
> > If misc_cg_set_capacity() fails for some reason then we have
> > a memleak for sev_reclaim_asid_bitmap/sev_asid_bitmap. It's
> > not a case right now, because misc_cg_set_capacity() just can't
> > fail and check inside it is always successful.
> >
> > But let's fix that for code consistency.
> >
> > Cc: Sean Christopherson <seanjc@google.com>
> > Cc: Paolo Bonzini <pbonzini@redhat.com>
> > Cc: Stéphane Graber <stgraber@ubuntu.com>
> > Cc: kvm@vger.kernel.org
> > Cc: linux-kernel@vger.kernel.org
> > Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> > ---
> >  arch/x86/kvm/svm/sev.c | 7 ++++++-
> >  1 file changed, 6 insertions(+), 1 deletion(-)
> >
> > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> > index c25aeb550cd9..a42536a0681a 100644
> > --- a/arch/x86/kvm/svm/sev.c
> > +++ b/arch/x86/kvm/svm/sev.c
> > @@ -2213,8 +2213,13 @@ void __init sev_hardware_setup(void)
> >       }
> >
> >       sev_asid_count = max_sev_asid - min_sev_asid + 1;
> > -     if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count))
> > +     if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count)) {
> > +             bitmap_free(sev_reclaim_asid_bitmap);
> > +             sev_reclaim_asid_bitmap = NULL;
> > +             bitmap_free(sev_asid_bitmap);
> > +             sev_asid_bitmap = NULL;
> >               goto out;
> > +     }
> >
> >       pr_info("SEV supported: %u ASIDs\n", sev_asid_count);
> >       sev_supported = true;
>
> It would be nice that another case can also be fixed:
>
>         sev_es_asid_count = min_sev_asid - 1;
>         if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
>                 goto out; /* <----HERE */

Nope.

There is no leak. Because when we are at this point then sev_supported
= true and everything is fine.

>
> Maybe it would be a good idea to factor out an common error handling path.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 2/2] KVM: SVM: add some info prints to SEV init
  2023-04-11 19:43   ` Zhi Wang
@ 2023-04-12 14:55     ` Aleksandr Mikhalitsyn
  0 siblings, 0 replies; 13+ messages in thread
From: Aleksandr Mikhalitsyn @ 2023-04-12 14:55 UTC (permalink / raw)
  To: Zhi Wang
  Cc: pbonzini, Sean Christopherson, Stéphane Graber, kvm,
	linux-kernel

On Tue, Apr 11, 2023 at 9:43 PM Zhi Wang <zhi.wang.linux@gmail.com> wrote:
>
> On Tue,  4 Apr 2023 14:26:52 +0200
> Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com> wrote:
>
> > Let's add a few pr_info's to sev_hardware_setup to make SEV/SEV-ES
> > enabling a little bit handier for users. Right now it's too hard
> > to guess why SEV/SEV-ES are failing to enable.
> >
> > There are a few reasons.
> > SEV:
> > - npt is disabled (module parameter)
>      ^NPT
> > - CPU lacks some features (sev, decodeassists)
> > - Maximum SEV ASID is 0
> >
> > SEV-ES:
> > - mmio_caching is disabled (module parameter)
> > - CPU lacks sev_es feature
> > - Minimum SEV ASID value is 1 (can be adjusted in BIOS/UEFI)
> >
> > Cc: Sean Christopherson <seanjc@google.com>
> > Cc: Paolo Bonzini <pbonzini@redhat.com>
> > Cc: Stéphane Graber <stgraber@ubuntu.com>
> > Cc: kvm@vger.kernel.org
> > Cc: linux-kernel@vger.kernel.org
> > Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> > ---
> >  arch/x86/kvm/svm/sev.c | 27 +++++++++++++++++++++------
> >  1 file changed, 21 insertions(+), 6 deletions(-)
> >
> > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> > index a42536a0681a..14cbb8f14c6b 100644
> > --- a/arch/x86/kvm/svm/sev.c
> > +++ b/arch/x86/kvm/svm/sev.c
> > @@ -2168,17 +2168,24 @@ void __init sev_hardware_setup(void)
> >       bool sev_es_supported = false;
> >       bool sev_supported = false;
> >
> > -     if (!sev_enabled || !npt_enabled)
> > +     if (!sev_enabled)
> >               goto out;
> >
> > +     if (!npt_enabled) {
> > +             pr_info("Failed to enable AMD SEV as it requires Nested Paging to be enabled\n");
> > +             goto out;
>
> Shouldn't we use pr_err() for error message?

I'm not sure. Because technically that's not an error, that is an
information message about current configuration.


>
> > +     }
> > +
> >       /*
> >        * SEV must obviously be supported in hardware.  Sanity check that the
> >        * CPU supports decode assists, which is mandatory for SEV guests to
> >        * support instruction emulation.
> >        */
> >       if (!boot_cpu_has(X86_FEATURE_SEV) ||
> > -         WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_DECODEASSISTS)))
> > +         WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_DECODEASSISTS))) {
> > +             pr_info("Failed to enable AMD SEV as it requires decodeassists and sev CPU features\n");
> >               goto out;
> > +     }
> >
> >       /* Retrieve SEV CPUID information */
> >       cpuid(0x8000001f, &eax, &ebx, &ecx, &edx);
> > @@ -2188,8 +2195,10 @@ void __init sev_hardware_setup(void)
> >
> >       /* Maximum number of encrypted guests supported simultaneously */
> >       max_sev_asid = ecx;
> > -     if (!max_sev_asid)
> > +     if (!max_sev_asid) {
> > +             pr_info("Failed to enable SEV as the maximum SEV ASID value is 0.\n");
> >               goto out;
> > +     }
> >
> >       /* Minimum ASID value that should be used for SEV guest */
> >       min_sev_asid = edx;
> > @@ -2234,16 +2243,22 @@ void __init sev_hardware_setup(void)
> >        * instead relies on #NPF(RSVD) being reflected into the guest as #VC
> >        * (the guest can then do a #VMGEXIT to request MMIO emulation).
> >        */
> > -     if (!enable_mmio_caching)
> > +     if (!enable_mmio_caching) {
> > +             pr_info("Failed to enable SEV-ES as it requires MMIO caching to be enabled\n");
> >               goto out;
> > +     }
> >
> >       /* Does the CPU support SEV-ES? */
> > -     if (!boot_cpu_has(X86_FEATURE_SEV_ES))
> > +     if (!boot_cpu_has(X86_FEATURE_SEV_ES)) {
> > +             pr_info("Failed to enable SEV-ES as it requires sev_es CPU feature\n");
> >               goto out;
> > +     }
> >
> >       /* Has the system been allocated ASIDs for SEV-ES? */
> > -     if (min_sev_asid == 1)
> > +     if (min_sev_asid == 1) {
> > +             pr_info("Failed to enable SEV-ES as the minimum SEV ASID value is 1.\n");
> >               goto out;
> > +     }
> >
> >       sev_es_asid_count = min_sev_asid - 1;
> >       if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
>
> As this patch is making sev_hardware_setup()more informative, it would be
> better to print both ASID range and count (instead of only ASID count in
> the current code). I was suspecting there seems a bug of ASID range allocation
> in the current code, but I don't have the HW to test yet...

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/2] KVM: SVM: free sev_*asid_bitmap init if SEV init fails
  2023-04-12 14:52     ` Aleksandr Mikhalitsyn
@ 2023-04-13  5:07       ` Zhi Wang
  0 siblings, 0 replies; 13+ messages in thread
From: Zhi Wang @ 2023-04-13  5:07 UTC (permalink / raw)
  To: Aleksandr Mikhalitsyn
  Cc: pbonzini, Sean Christopherson, Stéphane Graber, kvm,
	linux-kernel

On Wed, 12 Apr 2023 16:52:23 +0200
Aleksandr Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com> wrote:

> On Tue, Apr 11, 2023 at 9:47 PM Zhi Wang <zhi.wang.linux@gmail.com> wrote:
> >
> > On Tue,  4 Apr 2023 14:26:51 +0200
> > Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com> wrote:
> >
> > > If misc_cg_set_capacity() fails for some reason then we have
> > > a memleak for sev_reclaim_asid_bitmap/sev_asid_bitmap. It's
> > > not a case right now, because misc_cg_set_capacity() just can't
> > > fail and check inside it is always successful.
> > >
> > > But let's fix that for code consistency.
> > >
> > > Cc: Sean Christopherson <seanjc@google.com>
> > > Cc: Paolo Bonzini <pbonzini@redhat.com>
> > > Cc: Stéphane Graber <stgraber@ubuntu.com>
> > > Cc: kvm@vger.kernel.org
> > > Cc: linux-kernel@vger.kernel.org
> > > Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> > > ---
> > >  arch/x86/kvm/svm/sev.c | 7 ++++++-
> > >  1 file changed, 6 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> > > index c25aeb550cd9..a42536a0681a 100644
> > > --- a/arch/x86/kvm/svm/sev.c
> > > +++ b/arch/x86/kvm/svm/sev.c
> > > @@ -2213,8 +2213,13 @@ void __init sev_hardware_setup(void)
> > >       }
> > >
> > >       sev_asid_count = max_sev_asid - min_sev_asid + 1;
> > > -     if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count))
> > > +     if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count)) {
> > > +             bitmap_free(sev_reclaim_asid_bitmap);
> > > +             sev_reclaim_asid_bitmap = NULL;
> > > +             bitmap_free(sev_asid_bitmap);
> > > +             sev_asid_bitmap = NULL;
> > >               goto out;
> > > +     }
> > >
> > >       pr_info("SEV supported: %u ASIDs\n", sev_asid_count);
> > >       sev_supported = true;
> >
> > It would be nice that another case can also be fixed:
> >
> >         sev_es_asid_count = min_sev_asid - 1;
> >         if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
> >                 goto out; /* <----HERE */
> 
> Nope.
> 
> There is no leak. Because when we are at this point then sev_supported
> = true and everything is fine.
> 
Uh. You are right. Sorry that I was giving this comment based on my on-going
development branch.
> >
> > Maybe it would be a good idea to factor out an common error handling path.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 2/2] KVM: SVM: add some info prints to SEV init
  2023-04-04 12:26 ` [PATCH 2/2] KVM: SVM: add some info prints to SEV init Alexander Mikhalitsyn
  2023-04-11 19:43   ` Zhi Wang
@ 2023-05-19 18:17   ` Sean Christopherson
  2023-05-19 19:03     ` Aleksandr Mikhalitsyn
  1 sibling, 1 reply; 13+ messages in thread
From: Sean Christopherson @ 2023-05-19 18:17 UTC (permalink / raw)
  To: Alexander Mikhalitsyn; +Cc: pbonzini, Stéphane Graber, kvm, linux-kernel

On Tue, Apr 04, 2023, Alexander Miqqqqkhalitsyn wrote:
> Let's add a few pr_info's to sev_hardware_setup to make SEV/SEV-ES
> enabling a little bit handier for users. Right now it's too hard
> to guess why SEV/SEV-ES are failing to enable.

Hmm, I'm somewhat torn, but I'm against taking this patch, at least not in its
current form.  I appreciated that determining why KVM isn't enabling SEV/SEV-ES
is annoying, but there's very little actionable information provided here that
isn't also super obvious.  I also don't want to start us down a slippery slope
of printing out messages every time KVM doesn't enable a feature.

If someone tries to enable SEV and doesn't check that their CPU supports SEV,
then IMO that's on them.  Ditto for SEV-ES.

The NPT thing is mildly interesting, but practically speaking I don't expect that
to ever be a hindrace for generic enabling.  Ditto for MMIO caching.

The decode assists check is (a) completely unactionable for the vast, vast majority
of users and (b) is a WARN_ON_ONCE() condition.

The ASID stuff is by far the most interesting, but that's also quite interesting
for when SEV and SEV-ES _are_ fully supported.

So if we want to provide the user more info, I'd prefer to do something like the
below, which I think would be more helpful and would avoid my slippery slope
concerns.

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index c25aeb550cd9..eb4c6e3812d9 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -2216,7 +2216,6 @@ void __init sev_hardware_setup(void)
        if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count))
                goto out;
 
-       pr_info("SEV supported: %u ASIDs\n", sev_asid_count);
        sev_supported = true;
 
        /* SEV-ES support requested? */
@@ -2243,11 +2242,16 @@ void __init sev_hardware_setup(void)
        sev_es_asid_count = min_sev_asid - 1;
        if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
                goto out;
-
-       pr_info("SEV-ES supported: %u ASIDs\n", sev_es_asid_count);
        sev_es_supported = true;
 
 out:
+       if (boot_cpu_has(X86_FEATURE_SEV))
+               pr_info("SEV %s (ASIDs %u - %u)\n",
+                       sev_supported ? "enabled" : "disabled", ...);
+       if (boot_cpu_has(X86_FEATURE_SEV_ES))
+               pr_info("SEV-ES %s (ASIDs %u - %u)\n",
+                       sev_es_supported ? "enabled" : "disabled", ...);
+
        sev_enabled = sev_supported;
        sev_es_enabled = sev_es_supported;
 #endif

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH 2/2] KVM: SVM: add some info prints to SEV init
  2023-05-19 18:17   ` Sean Christopherson
@ 2023-05-19 19:03     ` Aleksandr Mikhalitsyn
  2023-05-19 21:02       ` Sean Christopherson
  0 siblings, 1 reply; 13+ messages in thread
From: Aleksandr Mikhalitsyn @ 2023-05-19 19:03 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: pbonzini, Stéphane Graber, kvm, linux-kernel

On Fri, May 19, 2023 at 8:17 PM Sean Christopherson <seanjc@google.com> wrote:
>
> On Tue, Apr 04, 2023, Alexander Miqqqqkhalitsyn wrote:
> > Let's add a few pr_info's to sev_hardware_setup to make SEV/SEV-ES
> > enabling a little bit handier for users. Right now it's too hard
> > to guess why SEV/SEV-ES are failing to enable.
>
> Hmm, I'm somewhat torn, but I'm against taking this patch, at least not in its
> current form.  I appreciated that determining why KVM isn't enabling SEV/SEV-ES
> is annoying, but there's very little actionable information provided here that
> isn't also super obvious.  I also don't want to start us down a slippery slope
> of printing out messages every time KVM doesn't enable a feature.
>
> If someone tries to enable SEV and doesn't check that their CPU supports SEV,
> then IMO that's on them.  Ditto for SEV-ES.
>
> The NPT thing is mildly interesting, but practically speaking I don't expect that
> to ever be a hindrace for generic enabling.  Ditto for MMIO caching.
>
> The decode assists check is (a) completely unactionable for the vast, vast majority
> of users and (b) is a WARN_ON_ONCE() condition.
>
> The ASID stuff is by far the most interesting, but that's also quite interesting
> for when SEV and SEV-ES _are_ fully supported.
>
> So if we want to provide the user more info, I'd prefer to do something like the
> below, which I think would be more helpful and would avoid my slippery slope
> concerns.

Dear Sean,

Thanks for looking into this!

I agree with your points, let's go that way and print only ASID stuff
as it can be not obvious to the end-user.

I'm ready to prepare -v2 if you don't mind.

Kind regards,
Alex

>
> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> index c25aeb550cd9..eb4c6e3812d9 100644
> --- a/arch/x86/kvm/svm/sev.c
> +++ b/arch/x86/kvm/svm/sev.c
> @@ -2216,7 +2216,6 @@ void __init sev_hardware_setup(void)
>         if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count))
>                 goto out;
>
> -       pr_info("SEV supported: %u ASIDs\n", sev_asid_count);
>         sev_supported = true;
>
>         /* SEV-ES support requested? */
> @@ -2243,11 +2242,16 @@ void __init sev_hardware_setup(void)
>         sev_es_asid_count = min_sev_asid - 1;
>         if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
>                 goto out;
> -
> -       pr_info("SEV-ES supported: %u ASIDs\n", sev_es_asid_count);
>         sev_es_supported = true;
>
>  out:
> +       if (boot_cpu_has(X86_FEATURE_SEV))
> +               pr_info("SEV %s (ASIDs %u - %u)\n",
> +                       sev_supported ? "enabled" : "disabled", ...);
> +       if (boot_cpu_has(X86_FEATURE_SEV_ES))
> +               pr_info("SEV-ES %s (ASIDs %u - %u)\n",
> +                       sev_es_supported ? "enabled" : "disabled", ...);
> +
>         sev_enabled = sev_supported;
>         sev_es_enabled = sev_es_supported;
>  #endif

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 2/2] KVM: SVM: add some info prints to SEV init
  2023-05-19 19:03     ` Aleksandr Mikhalitsyn
@ 2023-05-19 21:02       ` Sean Christopherson
  0 siblings, 0 replies; 13+ messages in thread
From: Sean Christopherson @ 2023-05-19 21:02 UTC (permalink / raw)
  To: Aleksandr Mikhalitsyn; +Cc: pbonzini, Stéphane Graber, kvm, linux-kernel

On Fri, May 19, 2023, Aleksandr Mikhalitsyn wrote:
> On Fri, May 19, 2023 at 8:17 PM Sean Christopherson <seanjc@google.com> wrote:
> >
> > On Tue, Apr 04, 2023, Alexander Miqqqqkhalitsyn wrote:
> > > Let's add a few pr_info's to sev_hardware_setup to make SEV/SEV-ES
> > > enabling a little bit handier for users. Right now it's too hard
> > > to guess why SEV/SEV-ES are failing to enable.
> >
> > Hmm, I'm somewhat torn, but I'm against taking this patch, at least not in its
> > current form.  I appreciated that determining why KVM isn't enabling SEV/SEV-ES
> > is annoying, but there's very little actionable information provided here that
> > isn't also super obvious.  I also don't want to start us down a slippery slope
> > of printing out messages every time KVM doesn't enable a feature.
> >
> > If someone tries to enable SEV and doesn't check that their CPU supports SEV,
> > then IMO that's on them.  Ditto for SEV-ES.
> >
> > The NPT thing is mildly interesting, but practically speaking I don't expect that
> > to ever be a hindrace for generic enabling.  Ditto for MMIO caching.
> >
> > The decode assists check is (a) completely unactionable for the vast, vast majority
> > of users and (b) is a WARN_ON_ONCE() condition.
> >
> > The ASID stuff is by far the most interesting, but that's also quite interesting
> > for when SEV and SEV-ES _are_ fully supported.
> >
> > So if we want to provide the user more info, I'd prefer to do something like the
> > below, which I think would be more helpful and would avoid my slippery slope
> > concerns.
> 
> Dear Sean,
> 
> Thanks for looking into this!
> 
> I agree with your points, let's go that way and print only ASID stuff
> as it can be not obvious to the end-user.
> 
> I'm ready to prepare -v2 if you don't mind.

Ya, fire away.  Thanks!

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2023-05-19 21:02 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-04 12:26 [PATCH 0/2] KVM: SVM: small tweaks for sev_hardware_setup Alexander Mikhalitsyn
2023-04-04 12:26 ` [PATCH 1/2] KVM: SVM: free sev_*asid_bitmap init if SEV init fails Alexander Mikhalitsyn
2023-04-11 19:47   ` Zhi Wang
2023-04-12 14:52     ` Aleksandr Mikhalitsyn
2023-04-13  5:07       ` Zhi Wang
2023-04-04 12:26 ` [PATCH 2/2] KVM: SVM: add some info prints to SEV init Alexander Mikhalitsyn
2023-04-11 19:43   ` Zhi Wang
2023-04-12 14:55     ` Aleksandr Mikhalitsyn
2023-05-19 18:17   ` Sean Christopherson
2023-05-19 19:03     ` Aleksandr Mikhalitsyn
2023-05-19 21:02       ` Sean Christopherson
2023-04-06  3:32 ` [PATCH 0/2] KVM: SVM: small tweaks for sev_hardware_setup Sean Christopherson
2023-04-06  7:07   ` Aleksandr Mikhalitsyn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox