Skip to content

Commit 9805cf0

Browse files
Wanpeng Libonzini
authored andcommitted
KVM: LAPIC: Narrow the timer latency between wait_lapic_expire and world switch
Let's treat lapic_timer_advance_ns automatic tuning logic as hypervisor overhead, move it before wait_lapic_expire instead of between wait_lapic_expire and the world switch, the wait duration should be calculated by the up-to-date guest_tsc after the overhead of automatic tuning logic. This patch reduces ~30+ cycles for kvm-unit-tests/tscdeadline-latency when testing busy waits. Signed-off-by: Wanpeng Li <[email protected]> Message-Id: <[email protected]> Reviewed-by: Sean Christopherson <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent fb0f947 commit 9805cf0

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

arch/x86/kvm/lapic.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,11 +1598,19 @@ static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)
15981598
guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
15991599
apic->lapic_timer.advance_expire_delta = guest_tsc - tsc_deadline;
16001600

1601+
if (lapic_timer_advance_dynamic) {
1602+
adjust_lapic_timer_advance(vcpu, apic->lapic_timer.advance_expire_delta);
1603+
/*
1604+
* If the timer fired early, reread the TSC to account for the
1605+
* overhead of the above adjustment to avoid waiting longer
1606+
* than is necessary.
1607+
*/
1608+
if (guest_tsc < tsc_deadline)
1609+
guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1610+
}
1611+
16011612
if (guest_tsc < tsc_deadline)
16021613
__wait_lapic_expire(vcpu, tsc_deadline - guest_tsc);
1603-
1604-
if (lapic_timer_advance_dynamic)
1605-
adjust_lapic_timer_advance(vcpu, apic->lapic_timer.advance_expire_delta);
16061614
}
16071615

16081616
void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)

0 commit comments

Comments
 (0)