Commit 8750e72a authored by Radim Krčmář's avatar Radim Krčmář Committed by Paolo Bonzini

KVM: remember position in kvm->vcpus array

Fetching an index for any vcpu in kvm->vcpus array by traversing
the entire array everytime is costly.
This patch remembers the position of each vcpu in kvm->vcpus array
by storing it in vcpus_idx under kvm_vcpu structure.
Signed-off-by: default avatarRadim Krčmář <rkrcmar@redhat.com>
Signed-off-by: default avatarNitesh Narayan Lal <nitesh@redhat.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 662f1d1d
...@@ -266,7 +266,8 @@ struct kvm_vcpu { ...@@ -266,7 +266,8 @@ struct kvm_vcpu {
struct preempt_notifier preempt_notifier; struct preempt_notifier preempt_notifier;
#endif #endif
int cpu; int cpu;
int vcpu_id; int vcpu_id; /* id given by userspace at creation */
int vcpu_idx; /* index in kvm->vcpus array */
int srcu_idx; int srcu_idx;
int mode; int mode;
u64 requests; u64 requests;
...@@ -570,13 +571,7 @@ static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id) ...@@ -570,13 +571,7 @@ static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)
static inline int kvm_vcpu_get_idx(struct kvm_vcpu *vcpu) static inline int kvm_vcpu_get_idx(struct kvm_vcpu *vcpu)
{ {
struct kvm_vcpu *tmp; return vcpu->vcpu_idx;
int idx;
kvm_for_each_vcpu(idx, tmp, vcpu->kvm)
if (tmp == vcpu)
return idx;
BUG();
} }
#define kvm_for_each_memslot(memslot, slots) \ #define kvm_for_each_memslot(memslot, slots) \
......
...@@ -2685,7 +2685,8 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) ...@@ -2685,7 +2685,8 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
goto unlock_vcpu_destroy; goto unlock_vcpu_destroy;
} }
BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]); vcpu->vcpu_idx = atomic_read(&kvm->online_vcpus);
BUG_ON(kvm->vcpus[vcpu->vcpu_idx]);
/* Now it's all set up, let userspace reach it */ /* Now it's all set up, let userspace reach it */
kvm_get_kvm(kvm); kvm_get_kvm(kvm);
...@@ -2695,7 +2696,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) ...@@ -2695,7 +2696,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
goto unlock_vcpu_destroy; goto unlock_vcpu_destroy;
} }
kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu; kvm->vcpus[vcpu->vcpu_idx] = vcpu;
/* /*
* Pairs with smp_rmb() in kvm_get_vcpu. Write kvm->vcpus * Pairs with smp_rmb() in kvm_get_vcpu. Write kvm->vcpus
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment