Skip to content

Commit 5c646b7

Browse files
YananWang-hubMarc Zyngier
authored andcommitted
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 <[email protected]> [maz: update commit message, add comment in the code] Signed-off-by: Marc Zyngier <[email protected]> Acked-by: Will Deacon <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 23bde34 commit 5c646b7

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

arch/arm64/kvm/hyp/pgtable.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,15 @@ static bool stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level,
470470
if (!kvm_block_mapping_supported(addr, end, phys, level))
471471
return false;
472472

473+
/*
474+
* If the PTE was already valid, drop the refcount on the table
475+
* early, as it will be bumped-up again in stage2_map_walk_leaf().
476+
* This ensures that the refcount stays constant across a valid to
477+
* valid PTE update.
478+
*/
479+
if (kvm_pte_valid(*ptep))
480+
put_page(virt_to_page(ptep));
481+
473482
if (kvm_set_valid_leaf_pte(ptep, phys, data->attr, level))
474483
goto out;
475484

0 commit comments

Comments
 (0)