Message ID | 20230404122652.275005-2-aleksandr.mikhalitsyn@canonical.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2990265vqo; Tue, 4 Apr 2023 05:33:51 -0700 (PDT) X-Google-Smtp-Source: AKy350aGbKjMcbZQle72CEh7usA0ceFOccX+tNAVvKNPBRREHkTGKRcZwOH4sZr1zB460UxqdhS/ X-Received: by 2002:a05:6a20:89a7:b0:d9:91d2:b81e with SMTP id h39-20020a056a2089a700b000d991d2b81emr1809985pzg.62.1680611630872; Tue, 04 Apr 2023 05:33:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680611630; cv=none; d=google.com; s=arc-20160816; b=v4ll15ZmHxnw58uXfuAfY41ILjUKVDMMVJHsh/MG4oTc0+FUMTfMUwDSFk91D9hjRE QiDtT8POYYQmlmcyMWSE+uXY14LQD0IwMz58sEJ3L8SiJRfAsFgqlav1U3E9I+YEk1K9 nTTW3ZyCyk/pYZE5J0amw/KXQC+izaGuRvxeljHdWK620s+RB9lCmtMwJbuGWv6M14O1 eAdfiVprmhPUUhAxpiEaYpGqmRBhBruWOPQep3iatRpWsbno2cxV8tyEtyRMLO3RzZLJ ZsYAxaqtjkNpPgtafAvv5JOTz/NVYlG+4fi2R9yoXtQ+faA5gNfKnGHfij+hW1RhCy4w mORQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=RdaT2Pf0VFYL4LUkKTTIwRSg1FtfHR7MN47zcz2wKyA=; b=zoJBLl+301wkbZ2WKFxURTLolXyQp8yN5vsoNP9ywpOVswjiueIVCsoOLJPiN1/GJP lX2s3inyn3owPEqWqoczurSdpj9oBQ7p2sooLpw3TiVx8nxDJmU2yPT+cdT6hnFsZK84 ihpach/hgEasq69KgdzSDuoE6ZhUQ1+xW2WV+yq//5gPTjuq/XhciLloSJ8fFLEcvbNq ix7i2x+kNKzBTts9CswTSbjAbZQfbmoZnj0HG9PDAb7513FNA6sNiN1/mAx5TlWvJkrt 0cq4SGyOdCDnG55WUBzCOVw0Ye3K11AEnbcv+MQRY8xQ11gwOz+FegvOjVxQoHuhjktT N9JQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=aHe3F6RQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i186-20020a6254c3000000b005d8fd3b36ecsi10311146pfb.228.2023.04.04.05.33.37; Tue, 04 Apr 2023 05:33:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=aHe3F6RQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234811AbjDDM1p (ORCPT <rfc822;a1648639935@gmail.com> + 99 others); Tue, 4 Apr 2023 08:27:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234683AbjDDM1n (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 4 Apr 2023 08:27:43 -0400 Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 813573C3C for <linux-kernel@vger.kernel.org>; Tue, 4 Apr 2023 05:27:25 -0700 (PDT) Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 654C03F23A for <linux-kernel@vger.kernel.org>; Tue, 4 Apr 2023 12:26:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1680611219; bh=RdaT2Pf0VFYL4LUkKTTIwRSg1FtfHR7MN47zcz2wKyA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=aHe3F6RQfdEcG4uPH9hUB2IGCgshaKDfIf/qx2+8ogqeaojCkpR+2XliN2nG1/ffk /JWUFCB9Z7PL6WgWW8p3D5zH8mGL3c25SqoF1Z+zZGLLAwmbmn1SlkAtQq+Uielj+e 9RgdxD4huigkkDVyvNZds8BVXKdWpeN98GbNuK2/uxgtS50a2XyTN7BFrNrEodeif0 /97RIyXT6ea8O3A+at947QhkLjTDrrN0bF/9EvNtmMlcy2sfp4YAqRi1FbglCDtnZp PgjyJwC77VnFoAjURZkH4turyXyA0Sgd75jwZnaFExsCK9ZfOniLCeU4ohoNAxxD3g lATKJQjS5+h1A== Received: by mail-ed1-f70.google.com with SMTP id k30-20020a50ce5e000000b00500544ebfb1so45587100edj.7 for <linux-kernel@vger.kernel.org>; Tue, 04 Apr 2023 05:26:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680611218; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RdaT2Pf0VFYL4LUkKTTIwRSg1FtfHR7MN47zcz2wKyA=; b=YoAeZWco/aIJ1az7828/GbDh1k0KWrfdjWFOrqesl7zqzYzSbdBLVzIn8cXidbfcBr I7L8xQhW04EaQ6ZhmsHqz4DyKm2AtMVlsEUACS8Khf3QF5dVdp3Nw7Hym37/R+IduUHz QrO0nhJZgtrX46yfsqDei7BqZzO+w2ZNh8S3po/tR2U0GnUHBN0xsFjOT0R00OHabBLA UpZOxnkDg+Eb185Dp4FW5ujuVnC1/Uu+d75tFXYecgS0Dc4s5f2kJcwyx1jdnaA6qy1w ZS+XMLswr6AYRwyDfyHJLrL77MzhMxmQD7UAth7egdo1ZrYH+yNtbtrbkrG4xws4xTEE b2Dw== X-Gm-Message-State: AAQBX9ceUD8H4OD0Js2VOEmwXjuofYrPAnGTrs7beM4CAX097IEMRYvi XqkW5oibQuvqCN/ck8k2lsFOWdDkSup+JaRrK/NmGFF+FV9p48tpKe/fl8B0roa4D+024LgubFg ncQZr1CgBOLQxHXoCuTLuvirueqdDfjg2BAMEK4yYfg== X-Received: by 2002:a17:906:2c5a:b0:92f:d1ec:a7d7 with SMTP id f26-20020a1709062c5a00b0092fd1eca7d7mr2173268ejh.15.1680611218108; Tue, 04 Apr 2023 05:26:58 -0700 (PDT) X-Received: by 2002:a17:906:2c5a:b0:92f:d1ec:a7d7 with SMTP id f26-20020a1709062c5a00b0092fd1eca7d7mr2173252ejh.15.1680611217810; Tue, 04 Apr 2023 05:26:57 -0700 (PDT) Received: from amikhalitsyn.. ([95.91.208.118]) by smtp.gmail.com with ESMTPSA id p18-20020a170906229200b00930ba362216sm6033056eja.176.2023.04.04.05.26.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 05:26:57 -0700 (PDT) From: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com> To: pbonzini@redhat.com Cc: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>, Sean Christopherson <seanjc@google.com>, =?utf-8?q?St=C3=A9phane_Graber?= <stgraber@ubuntu.com>, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] KVM: SVM: free sev_*asid_bitmap init if SEV init fails Date: Tue, 4 Apr 2023 14:26:51 +0200 Message-Id: <20230404122652.275005-2-aleksandr.mikhalitsyn@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230404122652.275005-1-aleksandr.mikhalitsyn@canonical.com> References: <20230404122652.275005-1-aleksandr.mikhalitsyn@canonical.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762249021667069838?= X-GMAIL-MSGID: =?utf-8?q?1762249021667069838?= |
Series |
KVM: SVM: small tweaks for sev_hardware_setup
|
|
Commit Message
Aleksandr 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
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.
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.
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 --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;