* [PATCH v3 0/2] KVM: x86: fix some kvm period timer BUG
@ 2025-10-22 15:00 fuqiang wang
2025-10-22 15:00 ` [PATCH v3 1/2] avoid hv timer fallback to sw timer if delay exceeds period fuqiang wang
2025-10-22 15:00 ` [PATCH v3 2/2] fix hardlockup when waking VM after long suspend fuqiang wang
0 siblings, 2 replies; 3+ messages in thread
From: fuqiang wang @ 2025-10-22 15:00 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, x86, H . Peter Anvin,
Maxim Levitsky, kvm, linux-kernel
Cc: fuqiang wang, yu chen, dongxu zhang
The following two patches fix some bugs in the x86 KVM periodic timer.
=======
patch 1
=======
The first patch fixes a problem where, if the next period has already
expired, e.g. due to the period being smaller than the delay in processing
the timer, the hv timer will switch to the software timer and never switch
back.
The reproduction steps and patch verification results at link [1].
=======
patch 2
=======
The second patch fixes an issue where, if the first patch has not been
applied, resuming a virtual machine after it has been suspended for a long
time may trigger a hard lockup. Link [2] also talks about this issue.
Link [2] also talks about this issue, but I don't think it can actually
reproduce the problem. Because of commit [3], as long as the KVM timer is
running, target_expiration will keep catching up to now (unless every
single delay from timer virtualization is longer than the period, which is
a pretty extreme case). Also, patch 2 is based on the changes in link [2],
but with some differences: In link [2], target_expiration is updated to
"now - period", and I'm not sure why it doesn't just catch up to now-maybe
I'm missing something? In patch 2, I tentatively set target_expiration to
catch up to now.
The reproduction steps and patch verification results at link [4].
=============================================================
other questions -- Should the two patches be merged into one?
=============================================================
If we end up making target_expiration catch up to now, patch 1 and patch 2
could probably be combined, since we wouldn't need the delta > 0 check
anymore. But keeping them separate helps clearly show the two different
problems we're fixing.
However, the hardlockup issue only occurs when patch 1 is not merged, which
leads to the commit message for the second patch repeatedly mentioning
"...previous patch is merged". I'm not sure if this is appropriate, so I
would like to hear your suggestions.
[1]: https://github.com/cai-fuqiang/kernel_test/tree/master/period_timer_test
[2]: https://lore.kernel.org/kvm/YgahsSubOgFtyorl@fuller.cnet/
[3]: d8f2f498d9ed ("x86/kvm: fix LAPIC timer drift when guest uses periodic mode")
[4]: https://github.com/cai-fuqiang/md/tree/master/case/intel_kvm_period_timer
Changes in v3:
- Fix: advanced SW timer (hrtimer) expiration does not catch up to current
time.
- optimize the commit message of patch 2
- link to v2: https://lore.kernel.org/all/20251021154052.17132-1-fuqiang.wng@gmail.com/
Changes in v2:
- Added a bugfix for hardlockup in v2
- link to v1: https://lore.kernel.org/all/20251013125117.87739-1-fuqiang.wng@gmail.com/
fuqiang wang (2):
avoid hv timer fallback to sw timer if delay exceeds period
fix hardlockup when waking VM after long suspend
arch/x86/kvm/lapic.c | 32 ++++++++++++++++++++++++--------
1 file changed, 24 insertions(+), 8 deletions(-)
--
2.47.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v3 1/2] avoid hv timer fallback to sw timer if delay exceeds period
2025-10-22 15:00 [PATCH v3 0/2] KVM: x86: fix some kvm period timer BUG fuqiang wang
@ 2025-10-22 15:00 ` fuqiang wang
2025-10-22 15:00 ` [PATCH v3 2/2] fix hardlockup when waking VM after long suspend fuqiang wang
1 sibling, 0 replies; 3+ messages in thread
From: fuqiang wang @ 2025-10-22 15:00 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, x86, H . Peter Anvin,
Maxim Levitsky, kvm, linux-kernel
Cc: fuqiang wang, yu chen, dongxu zhang
When the guest uses the APIC periodic timer, if the next period has already
expired, e.g. due to the period being smaller than the delay in processing
the timer, the delta will be negative. nsec_to_cycles() may then convert
this delta into an absolute value larger than guest_l1_tsc, resulting in a
negative tscdeadline. Since the hv timer supports a maximum bit width of
cpu_preemption_timer_multi + 32, this causes the hv timer setup to fail and
switch to the sw timer.
Moreover, due to the commit 98c25ead5eda ("KVM: VMX: Move preemption timer
<=> hrtimer dance to common x86"), if the guest is using the sw timer
before blocking, it will continue to use the sw timer after being woken up,
and will not switch back to the hv timer until the relevant APIC timer
register is reprogrammed. Since the periodic timer does not require
frequent APIC timer register programming, the guest may continue to use the
software timer for an extended period.
Fixes: d8f2f498d9ed ("x86/kvm: fix LAPIC timer drift when guest uses periodic mode")
Signed-off-by: fuqiang wang <fuqiang.wng@gmail.com>
---
arch/x86/kvm/lapic.c | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index 0ae7f913d782..fa07a303767c 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -2131,18 +2131,26 @@ static void advance_periodic_target_expiration(struct kvm_lapic *apic)
ktime_t delta;
/*
- * Synchronize both deadlines to the same time source or
- * differences in the periods (caused by differences in the
- * underlying clocks or numerical approximation errors) will
- * cause the two to drift apart over time as the errors
- * accumulate.
+ * Use kernel time as the time source for both deadlines so that they
+ * stay synchronized. Computing each deadline independently will cause
+ * the two deadlines to drift apart over time as differences in the
+ * periods accumulate, e.g. due to differences in the underlying clocks
+ * or numerical approximation errors.
*/
apic->lapic_timer.target_expiration =
ktime_add_ns(apic->lapic_timer.target_expiration,
apic->lapic_timer.period);
delta = ktime_sub(apic->lapic_timer.target_expiration, now);
- apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
- nsec_to_cycles(apic->vcpu, delta);
+
+ /*
+ * Don't adjust the tscdeadline if the next period has already expired,
+ * e.g. due to software overhead resulting in delays larger than the
+ * period. Blindly adding a negative delta could cause the deadline to
+ * become excessively large due to the deadline being an unsigned value.
+ */
+ apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl);
+ if (delta > 0)
+ apic->lapic_timer.tscdeadline += nsec_to_cycles(apic->vcpu, delta);
}
static void start_sw_period(struct kvm_lapic *apic)
--
2.47.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v3 2/2] fix hardlockup when waking VM after long suspend
2025-10-22 15:00 [PATCH v3 0/2] KVM: x86: fix some kvm period timer BUG fuqiang wang
2025-10-22 15:00 ` [PATCH v3 1/2] avoid hv timer fallback to sw timer if delay exceeds period fuqiang wang
@ 2025-10-22 15:00 ` fuqiang wang
1 sibling, 0 replies; 3+ messages in thread
From: fuqiang wang @ 2025-10-22 15:00 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, x86, H . Peter Anvin,
Maxim Levitsky, kvm, linux-kernel
Cc: fuqiang wang, yu chen, dongxu zhang
When a virtual machine uses the HV timer during suspend, the KVM timer does
not advance. Upon waking after a long period, there may be a significant
gap between target_expiration and the current time. Since each timer
expiration only advances target_expiration by one period, the expiration
handler can be invoked repeatedly to catch up.
Prior to the previous patch, if the advanced target_expiration remained
less than the current time, tscdeadline could be set to a negative value.
This would cause HV timer setup to fail and fallback to the SW timer. After
switching to SW timer, apic_timer_fn could be repeatedly executed within a
single clock interrupt handler, resulting in a hardlockup:
NMI watchdog: Watchdog detected hard LOCKUP on cpu 45
...
RIP: 0010:advance_periodic_target_expiration+0x4d/0x80 [kvm]
...
RSP: 0018:ff4f88f5d98d8ef0 EFLAGS: 00000046
RAX: fff0103f91be678e RBX: fff0103f91be678e RCX: 00843a7d9e127bcc
RDX: 0000000000000002 RSI: 0052ca4003697505 RDI: ff440d5bfbdbd500
RBP: ff440d5956f99200 R08: ff2ff2a42deb6a84 R09: 000000000002a6c0
R10: 0122d794016332b3 R11: 0000000000000000 R12: ff440db1af39cfc0
R13: ff440db1af39cfc0 R14: ffffffffc0d4a560 R15: ff440db1af39d0f8
FS: 00007f04a6ffd700(0000) GS:ff440db1af380000(0000) knlGS:000000e38a3b8000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 000000d5651feff8 CR3: 000000684e038002 CR4: 0000000000773ee0
PKRU: 55555554
Call Trace:
<IRQ>
apic_timer_fn+0x31/0x50 [kvm]
__hrtimer_run_queues+0x100/0x280
hrtimer_interrupt+0x100/0x210
? ttwu_do_wakeup+0x19/0x160
smp_apic_timer_interrupt+0x6a/0x130
apic_timer_interrupt+0xf/0x20
</IRQ>
With the previous patch applied, HV timer no longer falls back to SW timer.
Additionally, while target_expiration is catching up to the current time,
the VMX-preemption timer is set to 0 before each VM entry. According to
Intel SDM 27.7.4 ("VMX-Preemption Timer"), if the timer has already expired
at VM entry, a VM exit occurs before any guest instruction executes. As a
result, the guest cannot run instructions during this period and cannot
reach vcpu_block() to switch to the SW timer, preventing hardlockup.
However, unnecessary repeated catch-ups should still be avoided. Therefore,
if the advanced target_expiration is still less than the current time, we
immediately catch up to the current time in the handler.
Signed-off-by: fuqiang wang <fuqiang.wng@gmail.com>
---
arch/x86/kvm/lapic.c | 24 ++++++++++++++++--------
1 file changed, 16 insertions(+), 8 deletions(-)
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index fa07a303767c..307e2d6c3450 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -2140,17 +2140,25 @@ static void advance_periodic_target_expiration(struct kvm_lapic *apic)
apic->lapic_timer.target_expiration =
ktime_add_ns(apic->lapic_timer.target_expiration,
apic->lapic_timer.period);
- delta = ktime_sub(apic->lapic_timer.target_expiration, now);
/*
- * Don't adjust the tscdeadline if the next period has already expired,
- * e.g. due to software overhead resulting in delays larger than the
- * period. Blindly adding a negative delta could cause the deadline to
- * become excessively large due to the deadline being an unsigned value.
+ * When the vm is suspend, the hv timer also stops advancing. After it
+ * is resumed, this may result in a large delta. If the
+ * target_expiration only advances by one period each time, it will
+ * cause KVM to frequently handle timer expirations.
*/
+ if (apic->lapic_timer.period > 0 &&
+ ktime_before(apic->lapic_timer.target_expiration, now))
+ apic->lapic_timer.target_expiration = now;
+
+ delta = ktime_sub(apic->lapic_timer.target_expiration, now);
apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl);
- if (delta > 0)
- apic->lapic_timer.tscdeadline += nsec_to_cycles(apic->vcpu, delta);
+ /*
+ * Note: delta must not be negative. Otherwise, blindly adding a
+ * negative delta could cause the deadline to become excessively large
+ * due to the deadline being an unsigned value.
+ */
+ apic->lapic_timer.tscdeadline += nsec_to_cycles(apic->vcpu, delta);
}
static void start_sw_period(struct kvm_lapic *apic)
@@ -2980,7 +2988,7 @@ static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
if (lapic_is_periodic(apic)) {
advance_periodic_target_expiration(apic);
- hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
+ hrtimer_set_expires(&ktimer->timer, ktimer->target_expiration);
return HRTIMER_RESTART;
} else
return HRTIMER_NORESTART;
--
2.47.0
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-10-22 15:01 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-10-22 15:00 [PATCH v3 0/2] KVM: x86: fix some kvm period timer BUG fuqiang wang
2025-10-22 15:00 ` [PATCH v3 1/2] avoid hv timer fallback to sw timer if delay exceeds period fuqiang wang
2025-10-22 15:00 ` [PATCH v3 2/2] fix hardlockup when waking VM after long suspend fuqiang wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®