Skip to contentSkip to conversation

KVM: arm64: Drop last page ref in kvm_pgtable_stage2_free_removed()

PATCH Applied Oliver Upton 3 participants 3 messages May 30, 2023 – May 31, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: arm64: Drop last page ref in kvm_pgtable_stage2_free_removed()

The reference count on page table allocations is increased for every 'counted' PTE (valid or donated) in the table in addition to the initial reference from ->zalloc_page(). kvm_pgtable_stage2_free_removed() fails to drop the last reference on the root of the table walk, meaning we leak memory.

Fix it by dropping the last reference after the free walker returns, at which point all references for 'counted' PTEs have been released.

Fixes 5c359cca1faf ("KVM: arm64: Tear down unlinked stage-2 subtree after break-before-make")
---
 arch/arm64/kvm/hyp/pgtable.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/kvm/hyp/pgtable.c +3 -0
index e1eacffbc41f..95dae02ccc2e 100644
@@ -1332,4 +1332,7 @@ void kvm_pgtable_stage2_free_removed(struct kvm_pgtable_mm_ops *mm_ops, void *pg
 	};
 
 	WARN_ON(__kvm_pgtable_walk(&data, mm_ops, ptep, level + 1));
+
+	WARN_ON(mm_ops->page_count(pgtable) != 1);
+	mm_ops->put_page(pgtable);
 }

base-commit: 811154e234db72f0a11557a84ba9640f8b3bc823

Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Tested-by on v1 1/1
#1 lore raw
Quoted 7 lines · Oliver Upton

The reference count on page table allocations is increased for every 'counted' PTE (valid or donated) in the table in addition to the initial reference from ->zalloc_page(). kvm_pgtable_stage2_free_removed() fails to drop the last reference on the root of the table walk, meaning we leak memory.

Quoted 7 lines

Fix it by dropping the last reference after the free walker returns, at which point all references for 'counted' PTEs have been released.

Fixes 5c359cca1faf ("KVM: arm64: Tear down unlinked stage-2 subtree after break-before-make")
maintainer Applied
#2 lore raw
In reply to Oliver Upton
Quoted 8 lines · Oliver Upton

The reference count on page table allocations is increased for every 'counted' PTE (valid or donated) in the table in addition to the initial reference from ->zalloc_page(). kvm_pgtable_stage2_free_removed() fails to drop the last reference on the root of the table walk, meaning we leak memory.

Fix it by dropping the last reference after the free walker returns, at which point all references for 'counted' PTEs have been released.

Applied to fixes, thanks!

[1/1] KVM: arm64: Drop last page ref in kvm_pgtable_stage2_free_removed()

      commit: f6a27d6dc51b288106adaf053cff9c9b9cc12c4e

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.