Re: [PATCH v2 00/36] KVM: VMX APIC timer virtualization support
From: Zhao Liu
Date: Fri Dec 04 2026 - 00:36:35 EST
Hi Jim, Chao,
I've been evaluating these optimization ideas on CWF. Here are my
thoughts and what I found.
> >I'm not a fan of the hv_timer approach. One major difference between
> >the VMX preemption timer and the virtual TSC deadline MSR is that the
> >VMX preemption timer has to be reprogrammed every time that KVM
> >switches from hrtimer to hv_timer.
>
> For the preemption timer value written to the VMCS, yes. But
> hv_deadline_tsc (the absolute host TSC deadline, set in vmx_set_hv_timer())
> doesn't necessarily need recomputation across hrtimer/hv_timer switches.
>
> If this reprogram/recomputation cost matters, we can optimize the
> block/unblock path for the preemption timer to avoid that, and then the
> same optimization would apply naturally to APIC timer virtualization.
>
> The idea is: don't call cancel_hv_timer() when switching to hrtimer and
> don't call set_hv_timer() when switching back. cancel_hv_timer() can
> be deferred to virtual interrupt injection time (alongside setting
> tscdeadline to 0, which means disarming the timer architecturally).
> I think this works for TSC-deadline and oneshot modes; periodic mode
> needs more thought since the timer needs to be re-armed after each expiry.
For the virtual TSC deadline timer, the existing series already
avoids the reprogramming. The patch 3, "KVM: x86/lapic: Start/stop sw/hv
timer on vCPU un/block" leaves the guest deadline armed while the vCPU
is blocked. The host hrtimer is only used to wake the vCPU, and the
virtual timer is disarmed only when the interrupt is actually injected,
which is essentially what Chao described above. So the guest deadline
isn't reprogrammed across block/unblock during normal operation.
Reusing hv_timer is mainly about sharing the existing lapic interface
for switching between the hrtimer and hardware timers (preemption timer
or virtual apic timer).
> >The virtual TSC deadline timer
> >never needs reprogramming during normal operation.
>
> >Another difference
> >is that KVM should never inject a virtual local APIC timer interrupt
> >when the virtual TSC deadline timer is in use. Even if the vCPU was
> >blocked and the hrtimer fired to wake it up, KVM should just let the
> >hardware deliver the virtual interrupt on the next VM-entry.
I tried the "let hardware deliver it" approach as a PoC: after the
wakeup, KVM keeps the expired deadline armed (of virtual apic timer) and
leaves the interrupt to the next VM-entry.
Functionally it works, but it needed the extra helper/interface Chao
listed below.
> I agree this is the ideal behavior. But I don't see what's wrong with
> KVM delivering the interrupt.
>
> IMO, letting hardware deliver the interrupt after block would require:
>
> 1. We still need to mark the timer as pending to wake the vCPU from
> block. But to let hardware do the injection on VM-entry, we'd
> then need to consume the pending event without actually injecting
> the interrupt.
Right, in my PoC this is needed. The wakeup check has to treat an armed,
expired virtual deadline as a pending event, otherwise the vCPU can go
back to blocking.
> 2. Tweaking kvm_wait_lapic_expire(). The deadline was advanced when
> arming the hrtimer, so the vCPU wakes slightly before the actual
> deadline. Currently kvm_wait_lapic_expire() scans vIRR to decide
> whether to busy-wait until the deadline, but if we rely on
> hardware to deliver the interrupt, the vector won't be in vIRR
> and we'd need a different indicator.
Yes, kvm_wait_lapic_expire() also needs a separate indicator, because the
vector is no longer in vIRR.
> Given this, I'd prefer to let KVM inject the interrupt for this specific
> "hrtimer fired to wakeup" case — it works correctly as is and avoids the
> additional complexity.
Overall, there aren't many changes to the code. But what's important and
interesting is, it actually hurts latency on this path :-(.
In my measurements, timer latency on the halt-wakeup path was worse (
around ~10% timer latency regression) than when KVM injects the
interrupt itself.
The reason is where the work lands relative to the deadline. With lapic
timer advance (timer_advance_ns), KVM wakes the vCPU slightly early and
busy-waits until the deadline just before VM-entry:
- When KVM injects the interrupt, the injection happens before that
busy-wait, so its cost is hidden in the advance window. At the
deadline, VM-entry only has to deliver an interrupt that is already
pending.
- When hardware delivers it (via virtual apic timer), the processor can
only recognize the expired deadline and post the interrupt after
VM entry, i.e. after the deadline has already passed. That work then
sits directly on the latency critical path.
On this path the hrtimer has already done the real work, which is waking
the vCPU. Doing the injection in software as well costs nothing extra on
the critical path, whereas handing it to hardware adds latency.
Therefore, I think the following two cases need for different ways:
1). When the vCPU is running: this is for sure where the virtual APIC
timer delivers the best performance.
2). When the vCPU is blocked and needs to be woken up: the host timer
fires early (timer advance), and having KVM inject the interrupt
hides that cost before the deadline, so software injection is the
better choice.
Thanks,
Zhao