Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Zhao Liu <zhao1.liu@intel.com>
To: Jim Mattson <jmattson@google.com>, Chao Gao <chao.gao@intel.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <seanjc@google.com>,
	kvm@vger.kernel.org, isaku.yamahata@gmail.com,
	linux-kernel@vger.kernel.org, Zhao Liu <zhao1.liu@intel.com>
Subject: Re: [PATCH v2 00/36] KVM: VMX APIC timer virtualization support
Date: Fri, 4 Dec 2026 13:36:20 +0800	[thread overview]
Message-ID: <axJRVAkwQepal4Ox@intel.com> (raw)
In-Reply-To: <amtF1vo8WdZU0OBf@intel.com>

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


      reply	other threads:[~2026-12-04  5:36 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-05 17:43 [PATCH v2 00/36] KVM: VMX APIC timer virtualization support isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 01/36] KVM: VMX: Detect APIC timer virtualization bit isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 02/36] KVM: x86: Implement APIC virt timer helpers with callbacks isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 03/36] KVM: x86/lapic: Start/stop sw/hv timer on vCPU un/block isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 04/36] KVM: x86/lapic: Wire DEADLINE MSR update to guest virtual TSC deadline isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 05/36] KVM: x86/lapic: Add a trace point for guest virtual timer isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 06/36] KVM: VMX: Implement the hooks for VMX guest virtual deadline timer isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 07/36] KVM: VMX: Update APIC timer virtualization on apicv changed isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 08/36] KVM: nVMX: Disallow/allow guest APIC timer virtualization switch to/from L2 isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 09/36] KVM: nVMX: Pass struct msr_data to VMX MSRs emulation isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 10/36] KVM: nVMX: Supports VMX tertiary controls and GUEST_APIC_TIMER bit isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 11/36] KVM: nVMX: Add alignment check for struct vmcs12 isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 12/36] KVM: nVMX: Add tertiary VM-execution control VMCS support isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 13/36] KVM: nVMX: Update intercept on TSC deadline MSR isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 14/36] KVM: nVMX: Handle virtual timer vector VMCS field isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 15/36] KVM: VMX: Make vmx_calc_deadline_l1_to_host() non-static isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 16/36] KVM: nVMX: Enable guest deadline and its shadow VMCS field isaku.yamahata
2026-03-06 11:14   ` kernel test robot
2026-03-05 17:43 ` [PATCH v2 17/36] KVM: nVMX: Add VM entry checks related to APIC timer virtualization isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 18/36] KVM: nVMX: Add check vmread/vmwrite on tertiary control isaku.yamahata
2026-03-05 17:43 ` [PATCH v2 19/36] KVM: nVMX: Add check VMCS index for guest timer virtualization isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 20/36] KVM: VMX: Advertise tertiary controls to the user space isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 21/36] KVM: VMX: dump_vmcs() support the guest virt timer isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 22/36] KVM: VMX: Enable APIC timer virtualization isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 23/36] KVM: VMX: Introduce module parameter for APIC virt timer support isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 24/36] KVM: nVMX: Introduce module parameter for nested APIC timer virtualization isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 25/36] KVM: selftests: Add a test to measure local timer latency isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 26/36] KVM: selftests: Add nVMX support to timer_latency test case isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 27/36] KVM: selftests: Add test for nVMX MSR_IA32_VMX_PROCBASED_CTLS3 isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 28/36] KVM: selftests: Add test vmx_set_nested_state_test with EVMCS disabled isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 29/36] KVM: selftests: Add tests nested state of APIC timer virtualization isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 30/36] KVM: selftests: Add VMCS access test to " isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 31/36] KVM: selftests: Add serialize() helper and X86_FEATURE_SERIALIZE isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 32/36] KVM: selftests: Test cases for L1 APIC timer virtualization isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 33/36] KVM: selftests: Add tests for nVMX to vmx_apic_timer_virt isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 34/36] KVM: selftests: Add a global option to disable in-kernel irqchip isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 35/36] KVM: selftests: Test VMX apic timer virt with inkernel apic disable isaku.yamahata
2026-03-05 17:44 ` [PATCH v2 36/36] Documentation: KVM: x86: Update documentation of struct vmcs12 isaku.yamahata
2026-07-16  8:30 ` [PATCH v2 00/36] KVM: VMX APIC timer virtualization support Chao Gao
2026-07-29  1:34   ` Chao Gao
2026-07-29  6:05     ` Jim Mattson
2026-07-29  9:59       ` Chao Gao
2026-07-29 17:39         ` Jim Mattson
2026-07-30 12:38           ` Chao Gao
2026-12-04  5:36             ` Zhao Liu [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=axJRVAkwQepal4Ox@intel.com \
    --to=zhao1.liu@intel.com \
    --cc=chao.gao@intel.com \
    --cc=isaku.yamahata@gmail.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox