Skip to content

Commit 58ea676

Browse files
Haozhong Zhangbonzini
authored andcommitted
KVM: x86: Move TSC scaling logic out of call-back adjust_tsc_offset()
For both VMX and SVM, if the 2nd argument of call-back adjust_tsc_offset() is the host TSC, then adjust_tsc_offset() will scale it first. This patch moves this common TSC scaling logic to its caller adjust_tsc_offset_host() and rename the call-back adjust_tsc_offset() to adjust_tsc_offset_guest(). Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 07c1419 commit 58ea676

File tree

4 files changed

+19
-22
lines changed

4 files changed

+19
-22
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ struct kvm_x86_ops {
845845
int (*get_lpage_level)(void);
846846
bool (*rdtscp_supported)(void);
847847
bool (*invpcid_supported)(void);
848-
void (*adjust_tsc_offset)(struct kvm_vcpu *vcpu, s64 adjustment, bool host);
848+
void (*adjust_tsc_offset_guest)(struct kvm_vcpu *vcpu, s64 adjustment);
849849

850850
void (*set_tdp_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
851851

@@ -922,17 +922,6 @@ struct kvm_arch_async_pf {
922922

923923
extern struct kvm_x86_ops *kvm_x86_ops;
924924

925-
static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
926-
s64 adjustment)
927-
{
928-
kvm_x86_ops->adjust_tsc_offset(vcpu, adjustment, false);
929-
}
930-
931-
static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
932-
{
933-
kvm_x86_ops->adjust_tsc_offset(vcpu, adjustment, true);
934-
}
935-
936925
int kvm_mmu_module_init(void);
937926
void kvm_mmu_module_exit(void);
938927

arch/x86/kvm/svm.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -983,16 +983,10 @@ static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
983983
mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
984984
}
985985

986-
static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
986+
static void svm_adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, s64 adjustment)
987987
{
988988
struct vcpu_svm *svm = to_svm(vcpu);
989989

990-
if (host) {
991-
if (vcpu->arch.tsc_scaling_ratio != TSC_RATIO_DEFAULT)
992-
WARN_ON(adjustment < 0);
993-
adjustment = kvm_scale_tsc(vcpu, (u64)adjustment);
994-
}
995-
996990
svm->vmcb->control.tsc_offset += adjustment;
997991
if (is_guest_mode(vcpu))
998992
svm->nested.hsave->control.tsc_offset += adjustment;
@@ -4360,7 +4354,7 @@ static struct kvm_x86_ops svm_x86_ops = {
43604354

43614355
.read_tsc_offset = svm_read_tsc_offset,
43624356
.write_tsc_offset = svm_write_tsc_offset,
4363-
.adjust_tsc_offset = svm_adjust_tsc_offset,
4357+
.adjust_tsc_offset_guest = svm_adjust_tsc_offset_guest,
43644358
.read_l1_tsc = svm_read_l1_tsc,
43654359

43664360
.set_tdp_cr3 = set_tdp_cr3,

arch/x86/kvm/vmx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,7 +2413,7 @@ static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
24132413
}
24142414
}
24152415

2416-
static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
2416+
static void vmx_adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, s64 adjustment)
24172417
{
24182418
u64 offset = vmcs_read64(TSC_OFFSET);
24192419

@@ -10807,7 +10807,7 @@ static struct kvm_x86_ops vmx_x86_ops = {
1080710807

1080810808
.read_tsc_offset = vmx_read_tsc_offset,
1080910809
.write_tsc_offset = vmx_write_tsc_offset,
10810-
.adjust_tsc_offset = vmx_adjust_tsc_offset,
10810+
.adjust_tsc_offset_guest = vmx_adjust_tsc_offset_guest,
1081110811
.read_l1_tsc = vmx_read_l1_tsc,
1081210812

1081310813
.set_tdp_cr3 = vmx_set_cr3,

arch/x86/kvm/x86.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,20 @@ void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
15261526

15271527
EXPORT_SYMBOL_GPL(kvm_write_tsc);
15281528

1529+
static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
1530+
s64 adjustment)
1531+
{
1532+
kvm_x86_ops->adjust_tsc_offset_guest(vcpu, adjustment);
1533+
}
1534+
1535+
static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
1536+
{
1537+
if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
1538+
WARN_ON(adjustment < 0);
1539+
adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
1540+
kvm_x86_ops->adjust_tsc_offset_guest(vcpu, adjustment);
1541+
}
1542+
15291543
#ifdef CONFIG_X86_64
15301544

15311545
static cycle_t read_tsc(void)

0 commit comments

Comments
 (0)