diff mbox

[3/4] KVM: use heuristic for fast VCPU lookup by id

Message ID 1447922251-54261-4-git-send-email-borntraeger@de.ibm.com
State Accepted
Headers show

Commit Message

Christian Borntraeger Nov. 19, 2015, 8:37 a.m. UTC
From: David Hildenbrand <dahi@linux.vnet.ibm.com>

Usually, VCPU ids match the array index. So let's try a fast
lookup first before falling back to the slow iteration.

Suggested-by: Christian Borntraeger <borntraeger@de.ibm.com>
Reviewed-by: Dominik Dingel <dingel@linux.vnet.ibm.com>
Reviewed-by: Christian Borntraeger <borntraeger@de.ibm.com>
Signed-off-by: David Hildenbrand <dahi@linux.vnet.ibm.com>
Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com>
 include/linux/kvm_host.h | 5 +++++
 1 file changed, 5 insertions(+)
diff mbox


diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index b9f345f..7821137 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -465,6 +465,11 @@  static inline struct kvm_vcpu *kvm_lookup_vcpu(struct kvm *kvm, int id)
 	struct kvm_vcpu *vcpu;
 	int i;
+	if (id < 0 || id >= KVM_MAX_VCPUS)
+		return NULL;
+	vcpu = kvm_get_vcpu(kvm, id);
+	if (vcpu && vcpu->vcpu_id == id)
+		return vcpu;
 	kvm_for_each_vcpu(i, vcpu, kvm)
 		if (vcpu->vcpu_id == id)
 			return vcpu;