Skip to content

Commit 3b8a5df

Browse files
Wanpeng Libonzini
authored andcommitted
KVM: LAPIC: Tune lapic_timer_advance_ns automatically
In cloud environment, lapic_timer_advance_ns is needed to be tuned for every CPU generations, and every host kernel versions(the kvm-unit-tests/tscdeadline_latency.flat is 5700 cycles for upstream kernel and 9600 cycles for our 3.10 product kernel, both preemption_timer=N, Skylake server). This patch adds the capability to automatically tune lapic_timer_advance_ns step by step, the initial value is 1000ns as 'commit d0659d9 ("KVM: x86: add option to advance tscdeadline hrtimer expiration")' recommended, it will be reduced when it is too early, and increased when it is too late. The guest_tsc and tsc_deadline are hard to equal, so we assume we are done when the delta is within a small scope e.g. 100 cycles. This patch reduces latency (kvm-unit-tests/tscdeadline_latency, busy waits, preemption_timer enabled) from ~2600 cyles to ~1200 cyles on our Skylake server. Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Radim Krčmář <rkrcmar@redhat.com> Cc: Liran Alon <liran.alon@oracle.com> Signed-off-by: Wanpeng Li <wanpengli@tencent.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent efebf0a commit 3b8a5df

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

arch/x86/kvm/lapic.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
#define APIC_BROADCAST 0xFF
7171
#define X2APIC_BROADCAST 0xFFFFFFFFul
7272

73+
static bool lapic_timer_advance_adjust_done = false;
74+
#define LAPIC_TIMER_ADVANCE_ADJUST_DONE 100
75+
/* step-by-step approximation to mitigate fluctuation */
76+
#define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8
77+
7378
static inline int apic_test_vector(int vec, void *bitmap)
7479
{
7580
return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1472,7 +1477,7 @@ static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
14721477
void wait_lapic_expire(struct kvm_vcpu *vcpu)
14731478
{
14741479
struct kvm_lapic *apic = vcpu->arch.apic;
1475-
u64 guest_tsc, tsc_deadline;
1480+
u64 guest_tsc, tsc_deadline, ns;
14761481

14771482
if (!lapic_in_kernel(vcpu))
14781483
return;
@@ -1492,6 +1497,24 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu)
14921497
if (guest_tsc < tsc_deadline)
14931498
__delay(min(tsc_deadline - guest_tsc,
14941499
nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
1500+
1501+
if (!lapic_timer_advance_adjust_done) {
1502+
/* too early */
1503+
if (guest_tsc < tsc_deadline) {
1504+
ns = (tsc_deadline - guest_tsc) * 1000000ULL;
1505+
do_div(ns, vcpu->arch.virtual_tsc_khz);
1506+
lapic_timer_advance_ns -= min((unsigned int)ns,
1507+
lapic_timer_advance_ns / LAPIC_TIMER_ADVANCE_ADJUST_STEP);
1508+
} else {
1509+
/* too late */
1510+
ns = (guest_tsc - tsc_deadline) * 1000000ULL;
1511+
do_div(ns, vcpu->arch.virtual_tsc_khz);
1512+
lapic_timer_advance_ns += min((unsigned int)ns,
1513+
lapic_timer_advance_ns / LAPIC_TIMER_ADVANCE_ADJUST_STEP);
1514+
}
1515+
if (abs(guest_tsc - tsc_deadline) < LAPIC_TIMER_ADVANCE_ADJUST_DONE)
1516+
lapic_timer_advance_adjust_done = true;
1517+
}
14951518
}
14961519

14971520
static void start_sw_tscdeadline(struct kvm_lapic *apic)

arch/x86/kvm/x86.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static u32 __read_mostly tsc_tolerance_ppm = 250;
136136
module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
137137

138138
/* lapic timer advance (tscdeadline mode only) in nanoseconds */
139-
unsigned int __read_mostly lapic_timer_advance_ns = 0;
139+
unsigned int __read_mostly lapic_timer_advance_ns = 1000;
140140
module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
141141
EXPORT_SYMBOL_GPL(lapic_timer_advance_ns);
142142

0 commit comments

Comments
 (0)