Skip to content

Commit 4ba7653

Browse files
Haozhong Zhangbonzini
authored andcommitted
KVM: x86: Move TSC scaling logic out of call-back read_l1_tsc()
Both VMX and SVM scales the host TSC in the same way in call-back read_l1_tsc(), so this patch moves the scaling logic from call-back read_l1_tsc() to a common function kvm_read_l1_tsc(). Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 58ea676 commit 4ba7653

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,7 @@ void kvm_define_shared_msr(unsigned index, u32 msr);
12261226
int kvm_set_shared_msr(unsigned index, u64 val, u64 mask);
12271227

12281228
u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc);
1229+
u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc);
12291230

12301231
unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu);
12311232
bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip);

arch/x86/kvm/lapic.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,7 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu)
12501250

12511251
tsc_deadline = apic->lapic_timer.expired_tscdeadline;
12521252
apic->lapic_timer.expired_tscdeadline = 0;
1253-
guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu, rdtsc());
1253+
guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
12541254
trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
12551255

12561256
/* __delay is delay_tsc whenever the hardware has TSC, thus always. */
@@ -1318,7 +1318,7 @@ static void start_apic_timer(struct kvm_lapic *apic)
13181318
local_irq_save(flags);
13191319

13201320
now = apic->lapic_timer.timer.base->get_time();
1321-
guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu, rdtsc());
1321+
guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
13221322
if (likely(tscdeadline > guest_tsc)) {
13231323
ns = (tscdeadline - guest_tsc) * 1000000ULL;
13241324
do_div(ns, this_tsc_khz);

arch/x86/kvm/svm.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2984,8 +2984,7 @@ static int cr8_write_interception(struct vcpu_svm *svm)
29842984
static u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
29852985
{
29862986
struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu));
2987-
return vmcb->control.tsc_offset +
2988-
kvm_scale_tsc(vcpu, host_tsc);
2987+
return vmcb->control.tsc_offset + host_tsc;
29892988
}
29902989

29912990
static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)

arch/x86/kvm/x86.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,12 @@ static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
14011401
return target_tsc - tsc;
14021402
}
14031403

1404+
u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1405+
{
1406+
return kvm_x86_ops->read_l1_tsc(vcpu, kvm_scale_tsc(vcpu, host_tsc));
1407+
}
1408+
EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1409+
14041410
void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
14051411
{
14061412
struct kvm *kvm = vcpu->kvm;
@@ -1738,7 +1744,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
17381744
kernel_ns = get_kernel_ns();
17391745
}
17401746

1741-
tsc_timestamp = kvm_x86_ops->read_l1_tsc(v, host_tsc);
1747+
tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
17421748

17431749
/*
17441750
* We may have to catch up the TSC to match elapsed wall clock
@@ -6545,8 +6551,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
65456551
if (hw_breakpoint_active())
65466552
hw_breakpoint_restore();
65476553

6548-
vcpu->arch.last_guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu,
6549-
rdtsc());
6554+
vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
65506555

65516556
vcpu->mode = OUTSIDE_GUEST_MODE;
65526557
smp_wmb();

0 commit comments

Comments
 (0)