KVM: x86: vcpu_idx related cleanups
Subsystems (4)
Sean Christopherson
maintainercover KVM: x86: vcpu_idx related cleanups
Drop KVM's obsolete kvm_vcpu_get_idx() wrapper, and use vcpu_idx to identify vCPU0 when updating guest time.
Sean Christopherson (2):
KVM: x86: Query vcpu->vcpu_idx directly and drop its accessor
KVM: x86: Identify vCPU0 by its vcpu_idx instead of walking vCPUs
array
arch/x86/kvm/hyperv.c | 7 +++---- arch/x86/kvm/hyperv.h | 2 +- arch/x86/kvm/x86.c | 2 +- include/linux/kvm_host.h | 5 ----- 4 files changed, 5 insertions(+), 11 deletions(-)
Signature · 1 line
2.33.0.309.g3052b89438-goog
Sean Christopherson
maintainerPATCH 1/2 KVM: x86: Query vcpu->vcpu_idx directly and drop its accessor
Read vcpu->vcpu_idx directly instead of bouncing through the one-line wrapper, kvm_vcpu_get_idx(), and drop the wrapper. The wrapper is a remnant of the original implementation and serves no purpose; remove it before it gains more users.
Back when kvm_vcpu_get_idx() was added by commit 497d72d80a78 ("KVM: Add kvm_vcpu_get_idx to get vcpu index in kvm->vcpus"), the implementation was more than just a simple wrapper as vcpu->vcpu_idx did not exist and retrieving the index meant walking over the vCPU array to find the given vCPU.
When vcpu_idx was introduced by commit 8750e72a79dd ("KVM: remember position in kvm->vcpus array"), the helper was left behind, likely to avoid extra thrash (but even then there were only two users, the original arm usage having been removed at some point in the past).
No functional change intended.
--- arch/x86/kvm/hyperv.c | 7 +++---- arch/x86/kvm/hyperv.h | 2 +- include/linux/kvm_host.h | 5 ----- 3 files changed, 4 insertions(+), 10 deletions(-)
arch/x86/kvm/hyperv.c +3 -4
@@ -939,7 +939,7 @@ static int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)for (i =0 ; i <ARRAY_SIZE (hv_vcpu->stimer); i++)stimer_init (&hv_vcpu->stimer[i], i); - hv_vcpu->vp_index =kvm_vcpu_get_idx (vcpu); + hv_vcpu->vp_index = vcpu->vcpu_idx;return 0 ; }
@@ -1444,7 +1444,6 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)switch (msr) {case HV_X64_MSR_VP_INDEX : {struct kvm_hv *hv =to_kvm_hv (vcpu->kvm); -int vcpu_idx =kvm_vcpu_get_idx (vcpu);u32 new_vp_index = (u32 )data;if (!host || new_vp_index >=KVM_MAX_VCPUS )
@@ -1459,9 +1458,9 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)* VP index is changing, adjust num_mismatched_vp_indexes if * it now matches or no longer matches vcpu_idx. */ -if (hv_vcpu->vp_index == vcpu_idx) +if (hv_vcpu->vp_index == vcpu->vcpu_idx)atomic_inc (&hv->num_mismatched_vp_indexes); -else if (new_vp_index == vcpu_idx) +else if (new_vp_index == vcpu->vcpu_idx)atomic_dec (&hv->num_mismatched_vp_indexes); hv_vcpu->vp_index = new_vp_index;
arch/x86/kvm/hyperv.h +1 -1
@@ -83,7 +83,7 @@ static inline u32 kvm_hv_get_vpindex(struct kvm_vcpu *vcpu) {struct kvm_vcpu_hv *hv_vcpu =to_hv_vcpu (vcpu); -return hv_vcpu ? hv_vcpu->vp_index :kvm_vcpu_get_idx (vcpu); +return hv_vcpu ? hv_vcpu->vp_index : vcpu->vcpu_idx; }int kvm_hv_set_msr_common (struct kvm_vcpu *vcpu,u32 msr,u64 data,bool host);
include/linux/kvm_host.h +0 -5
@@ -721,11 +721,6 @@ static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)return NULL ; } -static inline int kvm_vcpu_get_idx (struct kvm_vcpu *vcpu) -{ -return vcpu->vcpu_idx; -} -#define kvm_for_each_memslot (memslot, slots) \ for (memslot = &slots->memslots[0]; \ memslot < slots->memslots + slots->used_slots; memslot++) \
Signature · 1 line
2.33.0.309.g3052b89438-goog
Re: [PATCH 1/2] KVM: x86: Query vcpu->vcpu_idx directly and drop its accessor
Quoted 78 lines · Sean Christopherson
Read
vcpu->vcpu_idxdirectly instead of bouncing through the one-line wrapper,kvm_vcpu_get_idx(), and drop the wrapper. The wrapper is a remnant of the original implementation and serves no purpose; remove it before it gains more users.Back when
kvm_vcpu_get_idx()was added by commit497d72d80a78("KVM: Addkvm_vcpu_get_idxto get vcpu index inkvm->vcpus"), the implementation was more than just a simple wrapper asvcpu->vcpu_idxdid not exist and retrieving the index meant walking over the vCPU array to find the given vCPU.When
vcpu_idxwas introduced by commit8750e72a79dd("KVM: remember position inkvm->vcpusarray"), the helper was left behind, likely to avoid extra thrash (but even then there were only two users, the original arm usage having been removed at some point in the past).No functional change intended.
Suggested-by Vitaly Kuznetsov <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/hyperv.c | 7 +++---- arch/x86/kvm/hyperv.h | 2 +- include/linux/kvm_host.h | 5 ----- 3 files changed, 4 insertions(+), 10 deletions(-)arch/x86/kvm/hyperv.c +3
-4@@ -939,7 +939,7 @@ static int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)for (i =0 ; i <ARRAY_SIZE (hv_vcpu->stimer); i++)stimer_init (&hv_vcpu->stimer[i], i); - hv_vcpu->vp_index =kvm_vcpu_get_idx (vcpu); + hv_vcpu->vp_index = vcpu->vcpu_idx;return 0 ; }@@ -1444,7 +1444,6 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)switch (msr) {case HV_X64_MSR_VP_INDEX : {struct kvm_hv *hv =to_kvm_hv (vcpu->kvm); -int vcpu_idx =kvm_vcpu_get_idx (vcpu);u32 new_vp_index = (u32 )data;if (!host || new_vp_index >=KVM_MAX_VCPUS )@@ -1459,9 +1458,9 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)* VP index is changing, adjust num_mismatched_vp_indexes if * it now matches or no longer matches vcpu_idx. */ -if (hv_vcpu->vp_index == vcpu_idx) +if (hv_vcpu->vp_index == vcpu->vcpu_idx)atomic_inc (&hv->num_mismatched_vp_indexes); -else if (new_vp_index == vcpu_idx) +else if (new_vp_index == vcpu->vcpu_idx)atomic_dec (&hv->num_mismatched_vp_indexes); hv_vcpu->vp_index = new_vp_index;arch/x86/kvm/hyperv.h +1
-1@@ -83,7 +83,7 @@ static inline u32 kvm_hv_get_vpindex(struct kvm_vcpu *vcpu) {struct kvm_vcpu_hv *hv_vcpu =to_hv_vcpu (vcpu); -return hv_vcpu ? hv_vcpu->vp_index :kvm_vcpu_get_idx (vcpu); +return hv_vcpu ? hv_vcpu->vp_index : vcpu->vcpu_idx; }int kvm_hv_set_msr_common (struct kvm_vcpu *vcpu,u32 msr,u64 data,bool host);include/linux/kvm_host.h +0
-0@@ -721,11 +721,6 @@ static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)return NULL ;
include/linux/kvm_host.h static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id) · line 726
Quoted 10 lines
include/linux/kvm_host.h +0
-5} -static inline int kvm_vcpu_get_idx (struct kvm_vcpu *vcpu) -{ -return vcpu->vcpu_idx; -} -#define kvm_for_each_memslot (memslot, slots) \ for (memslot = &slots->memslots[0]; \ memslot < slots->memslots + slots->used_slots; memslot++) \
Best regards,
Maxim Levitsky
Re: [PATCH 1/2] KVM: x86: Query vcpu->vcpu_idx directly and drop its accessor
In reply to Sean ChristophersonQuoted 78 lines · Sean Christopherson
Read
vcpu->vcpu_idxdirectly instead of bouncing through the one-line wrapper,kvm_vcpu_get_idx(), and drop the wrapper. The wrapper is a remnant of the original implementation and serves no purpose; remove it before it gains more users.Back when
kvm_vcpu_get_idx()was added by commit497d72d80a78("KVM: Addkvm_vcpu_get_idxto get vcpu index inkvm->vcpus"), the implementation was more than just a simple wrapper asvcpu->vcpu_idxdid not exist and retrieving the index meant walking over the vCPU array to find the given vCPU.When
vcpu_idxwas introduced by commit8750e72a79dd("KVM: remember position inkvm->vcpusarray"), the helper was left behind, likely to avoid extra thrash (but even then there were only two users, the original arm usage having been removed at some point in the past).No functional change intended.
Suggested-by Vitaly Kuznetsov <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/hyperv.c | 7 +++---- arch/x86/kvm/hyperv.h | 2 +- include/linux/kvm_host.h | 5 ----- 3 files changed, 4 insertions(+), 10 deletions(-)arch/x86/kvm/hyperv.c +3
-4@@ -939,7 +939,7 @@ static int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)for (i =0 ; i <ARRAY_SIZE (hv_vcpu->stimer); i++)stimer_init (&hv_vcpu->stimer[i], i); - hv_vcpu->vp_index =kvm_vcpu_get_idx (vcpu); + hv_vcpu->vp_index = vcpu->vcpu_idx;return 0 ; }@@ -1444,7 +1444,6 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)switch (msr) {case HV_X64_MSR_VP_INDEX : {struct kvm_hv *hv =to_kvm_hv (vcpu->kvm); -int vcpu_idx =kvm_vcpu_get_idx (vcpu);u32 new_vp_index = (u32 )data;if (!host || new_vp_index >=KVM_MAX_VCPUS )@@ -1459,9 +1458,9 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)* VP index is changing, adjust num_mismatched_vp_indexes if * it now matches or no longer matches vcpu_idx. */ -if (hv_vcpu->vp_index == vcpu_idx) +if (hv_vcpu->vp_index == vcpu->vcpu_idx)atomic_inc (&hv->num_mismatched_vp_indexes); -else if (new_vp_index == vcpu_idx) +else if (new_vp_index == vcpu->vcpu_idx)atomic_dec (&hv->num_mismatched_vp_indexes); hv_vcpu->vp_index = new_vp_index;arch/x86/kvm/hyperv.h +1
-1@@ -83,7 +83,7 @@ static inline u32 kvm_hv_get_vpindex(struct kvm_vcpu *vcpu) {struct kvm_vcpu_hv *hv_vcpu =to_hv_vcpu (vcpu); -return hv_vcpu ? hv_vcpu->vp_index :kvm_vcpu_get_idx (vcpu); +return hv_vcpu ? hv_vcpu->vp_index : vcpu->vcpu_idx; }int kvm_hv_set_msr_common (struct kvm_vcpu *vcpu,u32 msr,u64 data,bool host);include/linux/kvm_host.h +0
-0@@ -721,11 +721,6 @@ static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)return NULL ;
include/linux/kvm_host.h static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id) · line 726
Quoted 10 lines
include/linux/kvm_host.h +0
-5} -static inline int kvm_vcpu_get_idx (struct kvm_vcpu *vcpu) -{ -return vcpu->vcpu_idx; -} -#define kvm_for_each_memslot (memslot, slots) \ for (memslot = &slots->memslots[0]; \ memslot < slots->memslots + slots->used_slots; memslot++) \
Signature · 1 line
Vitaly
Sean Christopherson
maintainerPATCH 2/2 KVM: x86: Identify vCPU0 by its vcpu_idx instead of walking vCPUs array
In reply to Sean ChristophersonUse vcpu_idx to identify vCPU0 when updating HyperV's TSC page, which is shared by all vCPUs and "owned" by vCPU0 (because vCPU0 is the only vCPU that's guaranteed to exist). Using kvm_get_vcpu() to find vCPU works, but it's a rather odd and suboptimal method to check the index of a given vCPU.
No functional change intended.
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/x86.c +1 -1
@@ -2969,7 +2969,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) offsetof(struct compat_vcpu_info, time));if (vcpu->xen.vcpu_time_info_set)kvm_setup_pvclock_page (v, &vcpu->xen.vcpu_time_info_cache,0 ); -if (v ==kvm_get_vcpu (v->kvm,0 )) +if (!v->vcpu_idx)kvm_hv_setup_tsc_page (v->kvm, &vcpu->hv_clock);return 0 ; }
Signature · 1 line
2.33.0.309.g3052b89438-goog
Re: [PATCH 2/2] KVM: x86: Identify vCPU0 by its vcpu_idx instead of walking vCPUs array
Quoted 10 lines · Sean Christopherson
Use
vcpu_idxto identify vCPU0 when updating HyperV's TSC page, which is shared by all vCPUs and "owned" by vCPU0 (because vCPU0 is the only vCPU that's guaranteed to exist). Usingkvm_get_vcpu()to find vCPU works, but it's a rather odd and suboptimal method to check the index of a given vCPU.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>
Re: [PATCH 2/2] KVM: x86: Identify vCPU0 by its vcpu_idx instead of walking vCPUs array
In reply to Sean ChristophersonQuoted 18 lines · Sean Christopherson
Use
vcpu_idxto identify vCPU0 when updating HyperV's TSC page, which is shared by all vCPUs and "owned" by vCPU0 (because vCPU0 is the only vCPU that's guaranteed to exist). Usingkvm_get_vcpu()to find vCPU works, but it's a rather odd and suboptimal method to check the index of a given vCPU.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c static int kvm_guest_time_update(struct kvm_vcpu *v) · line 2975
Quoted 8 lines
arch/x86/kvm/x86.c +1
-1@@ -2969,7 +2969,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) offsetof(struct compat_vcpu_info, time));if (vcpu->xen.vcpu_time_info_set)kvm_setup_pvclock_page (v, &vcpu->xen.vcpu_time_info_cache,0 ); -if (v ==kvm_get_vcpu (v->kvm,0 )) +if (!v->vcpu_idx)kvm_hv_setup_tsc_page (v->kvm, &vcpu->hv_clock);return 0 ; }
Best regards,
Maxim Levitsky
Re: [PATCH 2/2] KVM: x86: Identify vCPU0 by its vcpu_idx instead of walking vCPUs array
In reply to Sean ChristophersonQuoted 18 lines · Sean Christopherson
Use
vcpu_idxto identify vCPU0 when updating HyperV's TSC page, which is shared by all vCPUs and "owned" by vCPU0 (because vCPU0 is the only vCPU that's guaranteed to exist). Usingkvm_get_vcpu()to find vCPU works, but it's a rather odd and suboptimal method to check the index of a given vCPU.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c static int kvm_guest_time_update(struct kvm_vcpu *v) · line 2975
Quoted 8 lines
arch/x86/kvm/x86.c +1
-1@@ -2969,7 +2969,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) offsetof(struct compat_vcpu_info, time));if (vcpu->xen.vcpu_time_info_set)kvm_setup_pvclock_page (v, &vcpu->xen.vcpu_time_info_cache,0 ); -if (v ==kvm_get_vcpu (v->kvm,0 )) +if (!v->vcpu_idx)kvm_hv_setup_tsc_page (v->kvm, &vcpu->hv_clock);return 0 ; }
" ... instead of walking vCPUs array" in the Subject line is a bit confusing because kvm_get_vcpu() doesn't actually walk anything, it just returns 'kvm->vcpus[i]' after checking that we actually have that many vCPUs. The patch itself is OK, so
Signature · 1 line
Vitaly
Sean Christopherson
maintainerRe: [PATCH 2/2] KVM: x86: Identify vCPU0 by its vcpu_idx instead of walking vCPUs array
Quoted 25 lines · Vitaly Kuznetsov
Quoted 23 lines · Sean Christopherson
Use
vcpu_idxto identify vCPU0 when updating HyperV's TSC page, which is shared by all vCPUs and "owned" by vCPU0 (because vCPU0 is the only vCPU that's guaranteed to exist). Usingkvm_get_vcpu()to find vCPU works, but it's a rather odd and suboptimal method to check the index of a given vCPU.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/x86.c +1
-1@@ -2969,7 +2969,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) offsetof(struct compat_vcpu_info, time));if (vcpu->xen.vcpu_time_info_set)kvm_setup_pvclock_page (v, &vcpu->xen.vcpu_time_info_cache,0 ); -if (v ==kvm_get_vcpu (v->kvm,0 )) +if (!v->vcpu_idx)
Quoted 8 lines
arch/x86/kvm/x86.c static int kvm_guest_time_update(struct kvm_vcpu *v) · line 2975
Quoted 3 lines
arch/x86/kvm/x86.c +0
-0kvm_hv_setup_tsc_page (v->kvm, &vcpu->hv_clock);return 0 ; }" ... instead of walking vCPUs array" in the Subject line is a bit confusing because
kvm_get_vcpu()doesn't actually walk anything, it just returns 'kvm->vcpus[i]' after checking that we actually have that many vCPUs. The patch itself is OK, so
Argh, yes, I have a feeling I wrote the changelog after digging into the history of kvm_get_vcpu().
Paolo, can you tweak the shortlog to:
KVM: x86: Identify vCPU0 by its vcpu_idx instead of its vCPUs array entry
Paolo Bonzini
maintainerRe: [PATCH 2/2] KVM: x86: Identify vCPU0 by its vcpu_idx instead of walking vCPUs array
Quoted 35 lines · Sean Christopherson
Quoted 25 lines · Vitaly Kuznetsov
Quoted 23 lines · Sean Christopherson
Use
vcpu_idxto identify vCPU0 when updating HyperV's TSC page, which is shared by all vCPUs and "owned" by vCPU0 (because vCPU0 is the only vCPU that's guaranteed to exist). Usingkvm_get_vcpu()to find vCPU works, but it's a rather odd and suboptimal method to check the index of a given vCPU.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/x86.c +1
-1@@ -2969,7 +2969,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) offsetof(struct compat_vcpu_info, time));if (vcpu->xen.vcpu_time_info_set)kvm_setup_pvclock_page (v, &vcpu->xen.vcpu_time_info_cache,0 ); -if (v ==kvm_get_vcpu (v->kvm,0 )) +if (!v->vcpu_idx)Quoted 8 lines
arch/x86/kvm/x86.c static int kvm_guest_time_update(struct kvm_vcpu *v) · line 2975
Quoted 3 lines
arch/x86/kvm/x86.c +0
-0kvm_hv_setup_tsc_page (v->kvm, &vcpu->hv_clock);return 0 ; }" ... instead of walking vCPUs array" in the Subject line is a bit confusing because
kvm_get_vcpu()doesn't actually walk anything, it just returns 'kvm->vcpus[i]' after checking that we actually have that many vCPUs. The patch itself is OK, so
Quoted 7 lines
Argh, yes, I have a feeling I wrote the changelog after digging into the history of
kvm_get_vcpu().Paolo, can you tweak the shortlog to:
KVM: x86: Identify vCPU0 by its
vcpu_idxinstead of its vCPUs array entry
Done and queued. Patch 1 required some further s390 changes.
Paolo