diff mbox series

[1/2] KVM: SVM: free sev_*asid_bitmap init if SEV init fails

Message ID 20230404122652.275005-2-aleksandr.mikhalitsyn@canonical.com (mailing list archive)
State New, archived
Headers show
Series KVM: SVM: small tweaks for sev_hardware_setup | expand

Commit Message

Alexander Mikhalitsyn April 4, 2023, 12:26 p.m. UTC
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(-)

Comments

Zhi Wang April 11, 2023, 7:47 p.m. UTC | #1
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.
Alexander Mikhalitsyn April 12, 2023, 2:52 p.m. UTC | #2
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.
Zhi Wang April 13, 2023, 5:07 a.m. UTC | #3
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.
diff mbox series

Patch

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;