From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80EAAC64E7B for ; Tue, 1 Dec 2020 18:16:43 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E6EAC208C3 for ; Tue, 1 Dec 2020 18:16:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="RM/rhQjM"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="kpFdeEl0" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E6EAC208C3 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=jySI1CoofTGVn9UJCDyi/UWVnw/KIao0F8yPSZ7hcCA=; b=RM/rhQjM0EFnY6EeL59ZePe4J HjKMVd7jy/ZnRAQPzWRV6NVDACdkUiHRRMred+/5+FjJchCghpEsWnrq8QwLAaWkyfZfFNMzXoQNR 9cBwFyMIeVMemtE24a+rf19PR1hL7ghqZQbrp05yDd7B6X82VMVjxvbLDOkofUJ9vdjXP0ZhTO0SW nSWmRn+lwz+6npSMa/sA1TENxT0BXoBlqgMAF9kbzCHZAOn7tp307+SfaW6v+XlwyQsri7vd107ht ARTlExb+/W579Jgu2eHBSMAg0FoTlltkmd/1yjyU40Y3IQrllD8GdhnkeE/ny0KlMaNPk14KpT8YB Qc3A0wKrA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kkABO-0005Je-0s; Tue, 01 Dec 2020 18:15:22 +0000 Received: from mail.kernel.org ([198.145.29.99]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kkABK-0005JG-I5 for linux-arm-kernel@lists.infradead.org; Tue, 01 Dec 2020 18:15:19 +0000 Received: from willie-the-truck (236.31.169.217.in-addr.arpa [217.169.31.236]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CA448208C3; Tue, 1 Dec 2020 18:15:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1606846517; bh=zuuNW3myEYeuP4hfE786k0NOWyKpsRtFl8nsy2luZOY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=kpFdeEl0zE9D471TiGNhfFyiISal4gBNFkSLSdc6rj3PSM8yqOo0PqF8uHtqtjwlH BSHBXNZeVnFfzCfUt0bGLn7Q583XEQ0DTDafS7WT1Nif3MnFV8RFvwnmFOxf5QZlj3 ENIE+Zz+FHp1kmnqNTiWK3wY8QODj2bOIqz4KD48= Date: Tue, 1 Dec 2020 18:15:11 +0000 From: Will Deacon To: "wangyanan (Y)" Subject: Re: [RFC PATCH 1/3] KVM: arm64: Fix possible memory leak in kvm stage2 Message-ID: <20201201181510.GA27955@willie-the-truck> References: <20201130121847.91808-1-wangyanan55@huawei.com> <20201130121847.91808-2-wangyanan55@huawei.com> <20201130132133.GA24837@willie-the-truck> <20201201141632.GC26973@willie-the-truck> <1dbb71f2-a794-86ab-e1cc-ceb9c1e3dd37@huawei.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1dbb71f2-a794-86ab-e1cc-ceb9c1e3dd37@huawei.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201201_131518_757557_1D56243F X-CRM114-Status: GOOD ( 27.26 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: jiangkunkun@huawei.com, Gavin Shan , Suzuki K Poulose , Marc Zyngier , wangjingyi11@huawei.com, Quentin Perret , lushenming@huawei.com, linux-kernel@vger.kernel.org, yezengruan@huawei.com, James Morse , linux-arm-kernel@lists.infradead.org, Catalin Marinas , yuzenghui@huawei.com, wanghaibin.wang@huawei.com, zhukeqian1@huawei.com, Julien Thierry Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, Dec 02, 2020 at 01:19:35AM +0800, wangyanan (Y) wrote: > On 2020/12/1 22:16, Will Deacon wrote: > > On Tue, Dec 01, 2020 at 03:21:23PM +0800, wangyanan (Y) wrote: > > > On 2020/11/30 21:21, Will Deacon wrote: > > > > On Mon, Nov 30, 2020 at 08:18:45PM +0800, Yanan Wang wrote: > > > > > @@ -476,6 +477,7 @@ static bool stage2_map_walker_try_leaf(u64 ad= dr, u64 end, u32 level, > > > > > /* There's an existing valid leaf entry, so perform break-bef= ore-make */ > > > > > kvm_set_invalid_pte(ptep); > > > > > kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, data->mmu, addr, level= ); > > > > > + put_page(virt_to_page(ptep)); > > > > > kvm_set_valid_leaf_pte(ptep, phys, data->attr, level); > > > > > out: > > > > > data->phys +=3D granule; > > > > Isn't this hunk alone sufficient to solve the problem? > > > > = > > > Not sufficient enough. When the old ptep is valid and old pte equlas = new > > > pte, in this case, "True" is also returned by kvm_set_valid_leaf_pte() > > > = > > > and get_page() will still be called. > > I had a go at fixing this without introducing refcounting to the hyp st= age-1 > > case, and ended up with the diff below. What do you think? > = > Functionally this diff looks fine to me. A small comment inline, please s= ee > below. > = > I had made an alternative fix (after sending v1) and it looks much more > concise. > = > If you're ok with it, I can send it as v2 (together with patch#2 and #3) > after some tests. Thanks. > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > index 0271b4a3b9fe..b232bdd142a6 100644 > --- a/arch/arm64/kvm/hyp/pgtable.c > +++ b/arch/arm64/kvm/hyp/pgtable.c > @@ -470,6 +470,9 @@ static bool stage2_map_walker_try_leaf(u64 addr, u64 > end, u32 level, > =A0=A0=A0=A0=A0=A0=A0 if (!kvm_block_mapping_supported(addr, end, phys, l= evel)) > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 return false; > = > +=A0=A0=A0=A0=A0=A0 if (kvm_pte_valid(*ptep)) > +=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 put_page(virt_to_page(ptep)); > + > =A0=A0=A0=A0=A0=A0=A0 if (kvm_set_valid_leaf_pte(ptep, phys, data->attr, = level)) > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 goto out; This is certainly smaller, but see below. > > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > > index 0271b4a3b9fe..78e2c0dc47ae 100644 > > --- a/arch/arm64/kvm/hyp/pgtable.c > > +++ b/arch/arm64/kvm/hyp/pgtable.c > > @@ -170,23 +170,16 @@ static void kvm_set_table_pte(kvm_pte_t *ptep, kv= m_pte_t *childp) > > smp_store_release(ptep, pte); > > } > > -static bool kvm_set_valid_leaf_pte(kvm_pte_t *ptep, u64 pa, kvm_pte_t = attr, > > - u32 level) > > +static kvm_pte_t kvm_init_valid_leaf_pte(u64 pa, kvm_pte_t attr, u32 l= evel) > > { > > - kvm_pte_t old =3D *ptep, pte =3D kvm_phys_to_pte(pa); > > + kvm_pte_t pte =3D kvm_phys_to_pte(pa); > > u64 type =3D (level =3D=3D KVM_PGTABLE_MAX_LEVELS - 1) ? KVM_PTE_TYP= E_PAGE : > > KVM_PTE_TYPE_BLOCK; > > pte |=3D attr & (KVM_PTE_LEAF_ATTR_LO | KVM_PTE_LEAF_ATTR_HI); > > pte |=3D FIELD_PREP(KVM_PTE_TYPE, type); > > pte |=3D KVM_PTE_VALID; > > - > > - /* Tolerate KVM recreating the exact same mapping. */ > > - if (kvm_pte_valid(old)) > > - return old =3D=3D pte; > > - > > - smp_store_release(ptep, pte); > > - return true; > > + return pte; > > } > > static int kvm_pgtable_visitor_cb(struct kvm_pgtable_walk_data *data,= u64 addr, > > @@ -341,12 +334,17 @@ static int hyp_map_set_prot_attr(enum kvm_pgtable= _prot prot, > > static bool hyp_map_walker_try_leaf(u64 addr, u64 end, u32 level, > > kvm_pte_t *ptep, struct hyp_map_data *data) > > { > > + kvm_pte_t new, old =3D *ptep; > > u64 granule =3D kvm_granule_size(level), phys =3D data->phys; > > if (!kvm_block_mapping_supported(addr, end, phys, level)) > > return false; > > - WARN_ON(!kvm_set_valid_leaf_pte(ptep, phys, data->attr, level)); > > + /* Tolerate KVM recreating the exact same mapping. */ > > + new =3D kvm_init_valid_leaf_pte(phys, data->attr, level); > > + if (old !=3D new && !WARN_ON(kvm_pte_valid(old))) > > + smp_store_release(ptep, new); > > + > > data->phys +=3D granule; > > return true; > > } > > @@ -465,19 +463,24 @@ static bool stage2_map_walker_try_leaf(u64 addr, = u64 end, u32 level, > > kvm_pte_t *ptep, > > struct stage2_map_data *data) > > { > > + kvm_pte_t new, old =3D *ptep; > > u64 granule =3D kvm_granule_size(level), phys =3D data->phys; > > if (!kvm_block_mapping_supported(addr, end, phys, level)) > > return false; > > - if (kvm_set_valid_leaf_pte(ptep, phys, data->attr, level)) > > - goto out; > > + new =3D kvm_init_valid_leaf_pte(phys, data->attr, level); > > + if (kvm_pte_valid(old)) { > > + /* > > + * There's an existing valid leaf entry, so perform > > + * break-before-make. > > + */ > > + kvm_set_invalid_pte(ptep); > > + kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, data->mmu, addr, level); > > + put_page(virt_to_page(ptep)); > = > When old PTE is valid and equals to the new one, we will also perform > break-before-make and the new PTE installation. But they're actually not > necessary in this case. Agreed, but I don't think that case should happen for stage-2 mappings. That's why I prefer my diff here, as it makes that 'old =3D=3D new' logic specific to the hyp mappings. But I'll leave it all up to you (these diffs are only intended to be helpful). Will _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel