[PATCH 6/6] KVM: Harden kvm_vcpu_map() against double-mapping and thus leaking references

From: Sean Christopherson

Date: Thu Jul 23 2026 - 20:51:37 EST


Now that all on-stack maps use CLASS(kvm_vcpu_map_local), i.e. now that all
maps are zero-allocated, explicitly put any existing mappings/references
when establishing a new mapping to harden against KVM bugs leaking memory,
but yell loudly as the owner of the map is still ultimately responsible for
the lifecycle of the mapping.

Suggested-by: Yosry Ahmed <yosry@xxxxxxxxxx>
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
virt/kvm/kvm_main.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 2df8ee9ecf6c..e9e32686e41b 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -3118,6 +3118,9 @@ int __kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map,
.pin = true,
};

+ if (WARN_ON_ONCE(map->hva))
+ kvm_vcpu_unmap(vcpu, map);
+
map->pinned_page = NULL;
map->page = NULL;
map->hva = NULL;
--
2.55.0.229.g6434b31f56-goog