Skip to contentSkip to conversation

KVM: X86: Sanity check on gfn before removal

PATCH Applied Peter Xu 2 participants 2 messages Apr 16, 2020 – May 4, 2020

Subsystems (3)
lore
Share & tools
lore

Peter Xu

author
#0 lore raw

PATCH 1/1 KVM: X86: Sanity check on gfn before removal

The index returned by kvm_async_pf_gfn_slot() will be removed when an async pf gfn is going to be removed. However kvm_async_pf_gfn_slot() is not reliable in that it can return the last key it loops over even if the gfn is not found in the async gfn array. It should never happen, but it's still better to sanity check against that to make sure no unexpected gfn will be removed.

---
 arch/x86/kvm/x86.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kvm/x86.c +4 -0
index fc74dafa72ff..f1c6e604dd12 100644
@@ -10308,6 +10308,10 @@ static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
 	u32 i, j, k;
 
 	i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
+
+	if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn))
+		return;
+
 	while (true) {
 		vcpu->arch.apf.gfns[i] = ~0;
 		do {
Signature · 1 line
2.24.1
maintainer Applied
#1 lore raw
Quoted 18 lines · Peter Xu

The index returned by kvm_async_pf_gfn_slot() will be removed when an async pf gfn is going to be removed. However kvm_async_pf_gfn_slot() is not reliable in that it can return the last key it loops over even if the gfn is not found in the async gfn array. It should never happen, but it's still better to sanity check against that to make sure no unexpected gfn will be removed.

---
 arch/x86/kvm/x86.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kvm/x86.c +0 -0
index fc74dafa72ff..f1c6e604dd12 100644

arch/x86/kvm/x86.c static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) · line 10317

Quoted 10 lines
arch/x86/kvm/x86.c +4 -0
@@ -10308,6 +10308,10 @@ static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
 	u32 i, j, k;
 
 	i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
+
+	if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn))
+		return;
+
 	while (true) {
 		vcpu->arch.apf.gfns[i] = ~0;
 		do {

Queued, thanks.

Paolo