Skip to content

Commit c5ce823

Browse files
Wanpeng Libonzini
authored andcommitted
KVM: VMX: Optimize tscdeadline timer latency
'Commit d0659d9 ("KVM: x86: add option to advance tscdeadline hrtimer expiration")' advances the tscdeadline (the timer is emulated by hrtimer) expiration in order that the latency which is incurred by hypervisor (apic_timer_fn -> vmentry) can be avoided. This patch adds the advance tscdeadline expiration support to which the tscdeadline timer is emulated by VMX preemption timer to reduce the hypervisor lantency (handle_preemption_timer -> vmentry). The guest can also set an expiration that is very small (for example in Linux if an hrtimer feeds a expiration in the past); in that case we set delta_tsc to 0, leading to an immediately vmexit when delta_tsc is not bigger than advance ns. This patch can reduce ~63% latency (~4450 cycles to ~1660 cycles on a haswell desktop) for kvm-unit-tests/tscdeadline_latency when testing busy waits. Cc: Paolo Bonzini <[email protected]> Cc: Radim Krčmář <[email protected]> Signed-off-by: Wanpeng Li <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 71e9d9a commit c5ce823

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

arch/x86/kvm/vmx.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12435,7 +12435,7 @@ static inline int u64_shl_div_u64(u64 a, unsigned int shift,
1243512435
static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
1243612436
{
1243712437
struct vcpu_vmx *vmx;
12438-
u64 tscl, guest_tscl, delta_tsc;
12438+
u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
1243912439

1244012440
if (kvm_mwait_in_guest(vcpu->kvm))
1244112441
return -EOPNOTSUPP;
@@ -12444,6 +12444,12 @@ static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
1244412444
tscl = rdtsc();
1244512445
guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
1244612446
delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
12447+
lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12448+
12449+
if (delta_tsc > lapic_timer_advance_cycles)
12450+
delta_tsc -= lapic_timer_advance_cycles;
12451+
else
12452+
delta_tsc = 0;
1244712453

1244812454
/* Convert to host delta tsc if tsc scaling is enabled */
1244912455
if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&

arch/x86/kvm/x86.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
138138
/* lapic timer advance (tscdeadline mode only) in nanoseconds */
139139
unsigned int __read_mostly lapic_timer_advance_ns = 0;
140140
module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
141+
EXPORT_SYMBOL_GPL(lapic_timer_advance_ns);
141142

142143
static bool __read_mostly vector_hashing = true;
143144
module_param(vector_hashing, bool, S_IRUGO);

0 commit comments

Comments
 (0)