diff options
author | Yanan Wang <wangyanan55@huawei.com> | 2020-12-01 21:10:32 +0100 |
---|---|---|
committer | Marc Zyngier <maz@kernel.org> | 2020-12-02 10:42:24 +0100 |
commit | 5c646b7e1d8bcb12317426287c516dfa4c5171c2 (patch) | |
tree | e898f5189dcf00d995c7c7b36a9c8a29298167b3 /arch/arm64/kvm/hyp | |
parent | KVM: arm64: vgic-v3: Drop the reporting of GICR_TYPER.Last for userspace (diff) | |
download | linux-5c646b7e1d8bcb12317426287c516dfa4c5171c2.tar.xz linux-5c646b7e1d8bcb12317426287c516dfa4c5171c2.zip |
KVM: arm64: Fix memory leak on stage2 update of a valid PTE
When installing a new leaf PTE onto an invalid ptep, we need to
get_page(ptep) to account for the new mapping.
However, simply updating a valid PTE shouldn't result in any
additional refcounting, as there is new mapping. This otherwise
results in a page being forever wasted.
Address this by fixing-up the refcount in stage2_map_walker_try_leaf()
if the PTE was already valid, balancing out the later get_page()
in stage2_map_walk_leaf().
Signed-off-by: Yanan Wang <wangyanan55@huawei.com>
[maz: update commit message, add comment in the code]
Signed-off-by: Marc Zyngier <maz@kernel.org>
Acked-by: Will Deacon <will@kernel.org>
Link: https://lore.kernel.org/r/20201201201034.116760-2-wangyanan55@huawei.com
Diffstat (limited to 'arch/arm64/kvm/hyp')
-rw-r--r-- | arch/arm64/kvm/hyp/pgtable.c | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 0271b4a3b9fe..2beba1dc40ec 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -470,6 +470,15 @@ 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 the PTE was already valid, drop the refcount on the table + * early, as it will be bumped-up again in stage2_map_walk_leaf(). + * This ensures that the refcount stays constant across a valid to + * valid PTE update. + */ + if (kvm_pte_valid(*ptep)) + put_page(virt_to_page(ptep)); + if (kvm_set_valid_leaf_pte(ptep, phys, data->attr, level)) goto out; |