Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3486198pxu; Mon, 30 Nov 2020 04:22:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJzg78dOmRgzU/WWbxDszsH27gcm675Sc38Il2VEh6O3jnqnxrCcsPzsb2lBNAdwl7hjUL+O X-Received: by 2002:a17:906:fa12:: with SMTP id lo18mr5031417ejb.354.1606738972396; Mon, 30 Nov 2020 04:22:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606738972; cv=none; d=google.com; s=arc-20160816; b=q6Uu1fqrvOKYJ7/KdRBfBuF4usz+35xu+hsUDk01wNdRuViIar7V4RtXeF1KBbhSzD j6RxNMiUnUQzYivyxgpHqONM4D0njoMSu+8+aDWyacms3HNYRwWlrYha/aac+7QoudSA 6o0uhWx8p5eemg5Tt9LAVcNosG+t6c1WCehcA/m2f2C+QhQ1UzzMRdP6LcTLKhBEHEw8 1jOP4FletcVOYeDEJVf1XHICRTpaIoOSfp0qQZSV6U41hBK/rydu+uG8iOZM+7Fo26q8 Hc7WIIQaKS0pikIFdmke+ADTd0JMBAN5wUG48AJl31Fsv5q+ejPrQ4sgqfqsNA3I7Rfq zOXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=G8v623NtIPmQb9IzE5jVfBTMMEaMrlJUGHeWgalaRzg=; b=fEX3Gmv2CSC3UfAHYkSZ1ixuU+22Q70lpblT001+NgXKfXpsyN8KEcJi/A+QY27v5z kzdYWL5OOFTnR8v9XgAgy6O3kLGkZuLW2CiY9gR1NOPZG63R553q7eE/G0ypfeGka/q4 szohV16QigIeiUz/2Sr7w54ne0d5gY7wr0UGrN0PkqbVaXkQtg4gUCpvZf9Hv/iHJGAa SndiZ1pSSlfLiNaASij6JPBtmrLRvfQMCqoX3wfQz7zR4eH7d9rfLhfRVRja0S3LIg5D ibVpG5j7IQWcbuHI7JIYot02tsBxO25637TZaX4vNE8Wx7vH7zVxXDBaq6goPFAcDvMp frFw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p1si11217495edb.422.2020.11.30.04.22.29; Mon, 30 Nov 2020 04:22:52 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726463AbgK3MT4 (ORCPT + 99 others); Mon, 30 Nov 2020 07:19:56 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:9074 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726114AbgK3MT4 (ORCPT ); Mon, 30 Nov 2020 07:19:56 -0500 Received: from DGGEMS402-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Cl45T31PPzLxpp; Mon, 30 Nov 2020 20:18:37 +0800 (CST) Received: from DESKTOP-TMVL5KK.china.huawei.com (10.174.186.123) by DGGEMS402-HUB.china.huawei.com (10.3.19.202) with Microsoft SMTP Server id 14.3.487.0; Mon, 30 Nov 2020 20:19:02 +0800 From: Yanan Wang To: , , Marc Zyngier , Catalin Marinas , Will Deacon , James Morse , "Julien Thierry" , Suzuki K Poulose , Gavin Shan , Quentin Perret CC: , , , , , , , Yanan Wang Subject: [RFC PATCH 1/3] KVM: arm64: Fix possible memory leak in kvm stage2 Date: Mon, 30 Nov 2020 20:18:45 +0800 Message-ID: <20201130121847.91808-2-wangyanan55@huawei.com> X-Mailer: git-send-email 2.8.4.windows.1 In-Reply-To: <20201130121847.91808-1-wangyanan55@huawei.com> References: <20201130121847.91808-1-wangyanan55@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.174.186.123] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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)); 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; @@ -512,7 +514,7 @@ static int stage2_map_walk_leaf(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, } if (stage2_map_walker_try_leaf(addr, end, level, ptep, data)) - goto out_get_page; + return 0; if (WARN_ON(level == KVM_PGTABLE_MAX_LEVELS - 1)) return -EINVAL; @@ -536,9 +538,8 @@ static int stage2_map_walk_leaf(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, } kvm_set_table_pte(ptep, childp); - -out_get_page: get_page(page); + return 0; } -- 2.19.1