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=-16.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable 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 B7270C64E7B for ; Tue, 1 Dec 2020 07:22:45 +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 2BE442085B for ; Tue, 1 Dec 2020 07:22:45 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="j54jD9JT" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2BE442085B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=huawei.com 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-Type: Content-Transfer-Encoding:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date:Message-ID:From: References:To:Subject:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=zfQfyvqUvxXZgBriVcKf8+QJVTUVXJECP1cknws7buQ=; b=j54jD9JTz/y0Yyf9n4DLj3hMg mJmaC7PTgK021m9SRxNmr5l1faP4Es7EwnKfTkspslz47HRlJYjTFHixFBraRvFWOzS07HC3XgezZ i+iYDXW7vaiXwPCEcDG8HgN4BzygeMxkAjsrEFoDv3+LgOEa1Hk9d5ZHIGqubdAqqjezO//Nafxha RNxsv29zVuh+hoFRU0frkodvOzOVCKffsV9TgqwHFEzEZco9k/+Wq0qGSau/fvoIeI/2g2KFdXiO8 FOzmWRkmifGDqK/wRiheMgkKaApN6NfK/Mpv1ojkfHX2o4xV79iTvJ+K+BcKEFADfjO3oV42KjQaE emdHSzwZQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kjzyh-00043X-Ux; Tue, 01 Dec 2020 07:21:35 +0000 Received: from szxga08-in.huawei.com ([45.249.212.255]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kjzye-00040s-9p for linux-arm-kernel@lists.infradead.org; Tue, 01 Dec 2020 07:21:33 +0000 Received: from dggeme715-chm.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4ClYRF2cLqz13P90; Tue, 1 Dec 2020 15:20:41 +0800 (CST) Received: from [10.174.186.123] (10.174.186.123) by dggeme715-chm.china.huawei.com (10.1.199.111) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1913.5; Tue, 1 Dec 2020 15:21:24 +0800 Subject: Re: [RFC PATCH 1/3] KVM: arm64: Fix possible memory leak in kvm stage2 To: Will Deacon References: <20201130121847.91808-1-wangyanan55@huawei.com> <20201130121847.91808-2-wangyanan55@huawei.com> <20201130132133.GA24837@willie-the-truck> From: "wangyanan (Y)" Message-ID: Date: Tue, 1 Dec 2020 15:21:23 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0 MIME-Version: 1.0 In-Reply-To: <20201130132133.GA24837@willie-the-truck> Content-Language: en-US X-Originating-IP: [10.174.186.123] X-ClientProxiedBy: dggeme714-chm.china.huawei.com (10.1.199.110) To dggeme715-chm.china.huawei.com (10.1.199.111) X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201201_022132_862263_68D53EDE X-CRM114-Status: GOOD ( 18.46 ) 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-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Will, On 2020/11/30 21:21, Will Deacon wrote: > On Mon, Nov 30, 2020 at 08:18:45PM +0800, Yanan Wang wrote: >> When installing a new leaf pte onto an invalid ptep, we need to get_page(ptep). >> When just updating a valid leaf ptep, we shouldn't get_page(ptep). >> Incorrect page_count of translation tables might lead to memory leak, >> when unmapping a stage 2 memory range. > Did you find this by inspection, or did you hit this in practice? I'd be > interested to see the backtrace for mapping over an existing mapping. Actually this is found by inspection. In the current code, get_page() will uniformly called at "out_get_page" in function stage2_map_walk_leaf(), no matter the old ptep is valid or not. When using stage2_unmap_walker() API to unmap a memory range, some page-table pages might not be freed if page_count of the pages is not right. >> Signed-off-by: Yanan Wang >> --- >> arch/arm64/kvm/hyp/pgtable.c | 7 ++++--- >> 1 file changed, 4 insertions(+), 3 deletions(-) >> >> diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c >> index 0271b4a3b9fe..696b6aa83faf 100644 >> --- a/arch/arm64/kvm/hyp/pgtable.c >> +++ b/arch/arm64/kvm/hyp/pgtable.c >> @@ -186,6 +186,7 @@ static bool kvm_set_valid_leaf_pte(kvm_pte_t *ptep, u64 pa, kvm_pte_t attr, >> return old == pte; >> >> smp_store_release(ptep, pte); >> + get_page(virt_to_page(ptep)); > This is also used for the hypervisor stage-1 page-table, so I'd prefer to > leave this function as-is. I agree at this point. >> return true; >> } >> >> @@ -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? > > Will > . 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. Yanan _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel