Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp253857pxu; Tue, 1 Dec 2020 10:21:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJyGY+nkoyKjQY9UnH0/IpTK6BAEU5iwnDvpcO/LcThlI1NJGjDNV/EDOKRkjIjsV/3Gu/tw X-Received: by 2002:a17:906:ddb:: with SMTP id p27mr1980416eji.39.1606846881873; Tue, 01 Dec 2020 10:21:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606846881; cv=none; d=google.com; s=arc-20160816; b=gC5FHFxZqYwinnbd5oLqDUS59LyZPFFpd5clDfJZk2EWTzPup0zmDS/P1iLZ9QZndk 6rTb+GsRfJseO8P1V3q96+6AnYYB+h9DYvCyJGtJQI3mXcRrOn2GdOHumw3hkNUTCciQ iRx/MfsahIkrvIJHBXAuVRtA65DCC5FepMJGqaWuh9hjqiCGOrS4h+Jk4UIr/H/1Sh1O KxXp2PEKSeWov7rc2487DjtF4e2pnjKotcbS8c12CzlK+UNjwAYwd8d3RfcfVzWmLNxv iMFmdPshz6sNRqK3aVBTL+STgR4kWl9cOp3/UODD/QZHUHnR9MVU+oG02uRtZJ8vwXAr PTTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=Knaa3hMn3ZB58z2rxHSYfWM0rwGuBigLZH+dAppFGI0=; b=mJji1RlyTHBK+5apjDcTsdA/xOaDyqkSSG3JNU6Lr8/ekI7zzx43JBdxL87jizPynM WSD6dHNfKcbeuE2AhvLzti5wZeNWdhRkdDnHojo4k6S/Lo3BH1BNKAfz52x5mFnQQt3Q caympgf5oL/J31ZLmQWQM2LLp9xMguybsrGoGfOXWAd78Y6Cxo/VT1fuf6EblTifKC/Y KV0KBt4HZfl69k84spYYcZjB2qS4NXIVzupoxFvMiNvrjwb2NlVs1pACR+VmBYRHFa7H oYHKzAL0+s+IM7VXPxMzL2YWlxKKTyHqrCCP3pBrZfbLF1z6+4/naMTIz/UcGnsrNni8 69nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=kpFdeEl0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o21si388490ejc.176.2020.12.01.10.20.58; Tue, 01 Dec 2020 10:21:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=kpFdeEl0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404253AbgLASP7 (ORCPT + 99 others); Tue, 1 Dec 2020 13:15:59 -0500 Received: from mail.kernel.org ([198.145.29.99]:59972 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729718AbgLASP6 (ORCPT ); Tue, 1 Dec 2020 13:15:58 -0500 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)" Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Marc Zyngier , Catalin Marinas , James Morse , Julien Thierry , Suzuki K Poulose , Gavin Shan , Quentin Perret , wanghaibin.wang@huawei.com, yezengruan@huawei.com, zhukeqian1@huawei.com, yuzenghui@huawei.com, jiangkunkun@huawei.com, wangjingyi11@huawei.com, lushenming@huawei.com 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-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1dbb71f2-a794-86ab-e1cc-ceb9c1e3dd37@huawei.com> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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 addr, u64 end, u32 level, > > > > > /* 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)); > > > > > kvm_set_valid_leaf_pte(ptep, phys, data->attr, level); > > > > > out: > > > > > data->phys += 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 stage-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 see > 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, > ??????? if (!kvm_block_mapping_supported(addr, end, phys, level)) > ??????????????? return false; > > +?????? if (kvm_pte_valid(*ptep)) > +?????????????? put_page(virt_to_page(ptep)); > + > ??????? if (kvm_set_valid_leaf_pte(ptep, phys, data->attr, level)) > ??????????????? 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, kvm_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 level) > > { > > - kvm_pte_t old = *ptep, pte = kvm_phys_to_pte(pa); > > + kvm_pte_t pte = kvm_phys_to_pte(pa); > > u64 type = (level == KVM_PGTABLE_MAX_LEVELS - 1) ? KVM_PTE_TYPE_PAGE : > > KVM_PTE_TYPE_BLOCK; > > pte |= attr & (KVM_PTE_LEAF_ATTR_LO | KVM_PTE_LEAF_ATTR_HI); > > pte |= FIELD_PREP(KVM_PTE_TYPE, type); > > pte |= KVM_PTE_VALID; > > - > > - /* Tolerate KVM recreating the exact same mapping. */ > > - if (kvm_pte_valid(old)) > > - return old == 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 = *ptep; > > u64 granule = kvm_granule_size(level), phys = 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 = kvm_init_valid_leaf_pte(phys, data->attr, level); > > + if (old != new && !WARN_ON(kvm_pte_valid(old))) > > + smp_store_release(ptep, new); > > + > > data->phys += 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 = *ptep; > > u64 granule = kvm_granule_size(level), phys = 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 = 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 == 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