2019-07-05 14:55:27

by Wanpeng Li

[permalink] [raw]
Subject: [PATCH v6 0/2] KVM: LAPIC: Implement Exitless Timer

Dedicated instances are currently disturbed by unnecessary jitter due
to the emulated lapic timers fire on the same pCPUs which vCPUs resident.
There is no hardware virtual timer on Intel for guest like ARM. Both
programming timer in guest and the emulated timer fires incur vmexits.
This patchset tries to avoid vmexit which is incurred by the emulated
timer fires in dedicated instance scenario.

When nohz_full is enabled in dedicated instances scenario, the unpinned
timer will be moved to the nearest busy housekeepers after commit
9642d18eee2cd (nohz: Affine unpinned timers to housekeepers) and commit
444969223c8 ("sched/nohz: Fix affine unpinned timers mess"). However,
KVM always makes lapic timer pinned to the pCPU which vCPU residents, the
reason is explained by commit 61abdbe0 (kvm: x86: make lapic hrtimer
pinned). Actually, these emulated timers can be offload to the housekeeping
cpus since APICv is really common in recent years. The guest timer interrupt
is injected by posted-interrupt which is delivered by housekeeping cpu
once the emulated timer fires.

The host admin should fine tuned, e.g. dedicated instances scenario w/
nohz_full cover the pCPUs which vCPUs resident, several pCPUs surplus
for busy housekeeping, disable mwait/hlt/pause vmexits to keep in non-root
mode, ~3% redis performance benefit can be observed on Skylake server.

w/o patchset:

VM-EXIT Samples Samples% Time% Min Time Max Time Avg time

EXTERNAL_INTERRUPT 42916 49.43% 39.30% 0.47us 106.09us 0.71us ( +- 1.09% )

w/ patchset:

VM-EXIT Samples Samples% Time% Min Time Max Time Avg time

EXTERNAL_INTERRUPT 6871 9.29% 2.96% 0.44us 57.88us 0.72us ( +- 4.02% )

Cc: Paolo Bonzini <[email protected]>
Cc: Radim Krčmář <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

v5 -> v6:
* don't overwrites whatever the user specified
* introduce kvm_can_post_timer_interrupt and kvm_use_posted_timer_interrupt
* remove kvm_hlt_in_guest() condition
* squash all of 2/3/4 together

v4 -> v5:
* update patch description in patch 1/4
* feed latest apic->lapic_timer.expired_tscdeadline to kvm_wait_lapic_expire()
* squash advance timer handling to patch 2/4

v3 -> v4:
* drop the HRTIMER_MODE_ABS_PINNED, add kick after set pending timer
* don't posted inject already-expired timer

v2 -> v3:
* disarming the vmx preemption timer when posted_interrupt_inject_timer_enabled()
* check kvm_hlt_in_guest instead

v1 -> v2:
* check vcpu_halt_in_guest
* move module parameter from kvm-intel to kvm
* add housekeeping_enabled
* rename apic_timer_expired_pi to kvm_apic_inject_pending_timer_irqs



Wanpeng Li (2):
KVM: LAPIC: Make lapic timer unpinned
KVM: LAPIC: Inject timer interrupt via posted interrupt

arch/x86/kvm/lapic.c | 60 +++++++++++++++++++++++++++++------------
arch/x86/kvm/lapic.h | 3 ++-
arch/x86/kvm/svm.c | 2 +-
arch/x86/kvm/vmx/vmx.c | 5 ++--
arch/x86/kvm/x86.c | 12 +++++----
arch/x86/kvm/x86.h | 2 ++
include/linux/sched/isolation.h | 2 ++
kernel/sched/isolation.c | 6 +++++
8 files changed, 66 insertions(+), 26 deletions(-)

--
1.8.3.1


2019-07-05 14:55:31

by Wanpeng Li

[permalink] [raw]
Subject: [PATCH v6 1/2] KVM: LAPIC: Make lapic timer unpinned

From: Wanpeng Li <[email protected]>

Commit 61abdbe0bcc2 ("kvm: x86: make lapic hrtimer pinned") pinned the
lapic timer to avoid to wait until the next kvm exit for the guest to
see KVM_REQ_PENDING_TIMER set. There is another solution to give a kick
after setting the KVM_REQ_PENDING_TIMER bit, make lapic timer unpinned
will be used in follow up patches.

Cc: Paolo Bonzini <[email protected]>
Cc: Radim Krčmář <[email protected]>
Cc: Marcelo Tosatti <[email protected]>
Signed-off-by: Wanpeng Li <[email protected]>
---
arch/x86/kvm/lapic.c | 8 ++++----
arch/x86/kvm/x86.c | 6 +-----
2 files changed, 5 insertions(+), 9 deletions(-)

diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index fcf42a3..9f09100 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -1581,7 +1581,7 @@ static void start_sw_tscdeadline(struct kvm_lapic *apic)
likely(ns > apic->lapic_timer.timer_advance_ns)) {
expire = ktime_add_ns(now, ns);
expire = ktime_sub_ns(expire, ktimer->timer_advance_ns);
- hrtimer_start(&ktimer->timer, expire, HRTIMER_MODE_ABS_PINNED);
+ hrtimer_start(&ktimer->timer, expire, HRTIMER_MODE_ABS);
} else
apic_timer_expired(apic);

@@ -1683,7 +1683,7 @@ static void start_sw_period(struct kvm_lapic *apic)

hrtimer_start(&apic->lapic_timer.timer,
apic->lapic_timer.target_expiration,
- HRTIMER_MODE_ABS_PINNED);
+ HRTIMER_MODE_ABS);
}

bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
@@ -2320,7 +2320,7 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns)
apic->vcpu = vcpu;

hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
- HRTIMER_MODE_ABS_PINNED);
+ HRTIMER_MODE_ABS);
apic->lapic_timer.timer.function = apic_timer_fn;
if (timer_advance_ns == -1) {
apic->lapic_timer.timer_advance_ns = 1000;
@@ -2509,7 +2509,7 @@ void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)

timer = &vcpu->arch.apic->lapic_timer.timer;
if (hrtimer_cancel(timer))
- hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
+ hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
}

/*
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 3a7cd935..e199ac7 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1437,12 +1437,8 @@ static void update_pvclock_gtod(struct timekeeper *tk)

void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
{
- /*
- * Note: KVM_REQ_PENDING_TIMER is implicitly checked in
- * vcpu_enter_guest. This function is only called from
- * the physical CPU that is running vcpu.
- */
kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
+ kvm_vcpu_kick(vcpu);
}

static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
--
1.8.3.1