Skip to content

Commit 8750e72

Browse files
rkrcmarbonzini
authored andcommitted
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: Radim Krčmář <[email protected]> Signed-off-by: Nitesh Narayan Lal <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 662f1d1 commit 8750e72

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

include/linux/kvm_host.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ struct kvm_vcpu {
266266
struct preempt_notifier preempt_notifier;
267267
#endif
268268
int cpu;
269-
int vcpu_id;
269+
int vcpu_id; /* id given by userspace at creation */
270+
int vcpu_idx; /* index in kvm->vcpus array */
270271
int srcu_idx;
271272
int mode;
272273
u64 requests;
@@ -570,13 +571,7 @@ static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)
570571

571572
static inline int kvm_vcpu_get_idx(struct kvm_vcpu *vcpu)
572573
{
573-
struct kvm_vcpu *tmp;
574-
int idx;
575-
576-
kvm_for_each_vcpu(idx, tmp, vcpu->kvm)
577-
if (tmp == vcpu)
578-
return idx;
579-
BUG();
574+
return vcpu->vcpu_idx;
580575
}
581576

582577
#define kvm_for_each_memslot(memslot, slots) \

virt/kvm/kvm_main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,7 +2685,8 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
26852685
goto unlock_vcpu_destroy;
26862686
}
26872687

2688-
BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
2688+
vcpu->vcpu_idx = atomic_read(&kvm->online_vcpus);
2689+
BUG_ON(kvm->vcpus[vcpu->vcpu_idx]);
26892690

26902691
/* Now it's all set up, let userspace reach it */
26912692
kvm_get_kvm(kvm);
@@ -2695,7 +2696,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
26952696
goto unlock_vcpu_destroy;
26962697
}
26972698

2698-
kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
2699+
kvm->vcpus[vcpu->vcpu_idx] = vcpu;
26992700

27002701
/*
27012702
* Pairs with smp_rmb() in kvm_get_vcpu. Write kvm->vcpus

0 commit comments

Comments
 (0)