git: 018bd70c4ac9 - main - vmm: do not let the RTC's periodic interrupt drift
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 16 Nov 2026 19:00:35 UTC
The branch main has been updated by jhb:
URL: https://cgit.FreeBSD.org/src/commit/?id=018bd70c4ac9c3e7b985ab775bf371cc59a22759
commit 018bd70c4ac9c3e7b985ab775bf371cc59a22759
Author: Wanpeng Qian <wanpengqian@gmail.com>
AuthorDate: 2026-11-16 19:00:17 +0000
Commit: John Baldwin <jhb@FreeBSD.org>
CommitDate: 2026-11-16 19:00:17 +0000
vmm: do not let the RTC's periodic interrupt drift
The callout of the periodic interrupt was rearmed from its handler with
a timeout of one period from the time the handler ran. Each period was
longer than programmed by the latency of the callout, so the interrupt
came at a lower rate than the guest asked for. A guest that counts
these interrupts to keep its time runs slow.
Give the callout absolute deadlines one period apart, as vatpit does.
If a deadline has already passed when it is computed, for instance
after the VM was paused, start again from the current time.
Signed-off-by: Wanpeng Qian <wanpengqian@gmail.com>
Reviewed by: jhb
Sponsored by: keelos.dev
Differential Revision: https://reviews.freebsd.org/D60233
---
sys/amd64/vmm/io/vrtc.c | 28 +++++++++++++++++++++-------
1 file changed, 21 insertions(+), 7 deletions(-)
diff --git a/sys/amd64/vmm/io/vrtc.c b/sys/amd64/vmm/io/vrtc.c
index c2f579c30c84..5752a99e2444 100644
--- a/sys/amd64/vmm/io/vrtc.c
+++ b/sys/amd64/vmm/io/vrtc.c
@@ -80,6 +80,7 @@ struct vrtc {
u_int addr; /* RTC register to read or write */
sbintime_t base_uptime;
time_t base_rtctime;
+ sbintime_t callout_time;
struct rtcdev rtcdev;
};
@@ -520,8 +521,9 @@ vrtc_freq(struct vrtc *vrtc)
}
static void
-vrtc_callout_reset(struct vrtc *vrtc, sbintime_t freqsbt)
+vrtc_callout_reset(struct vrtc *vrtc, sbintime_t freqsbt, bool periodic)
{
+ sbintime_t now;
KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
@@ -533,8 +535,20 @@ vrtc_callout_reset(struct vrtc *vrtc, sbintime_t freqsbt)
return;
}
VM_CTR1(vrtc->vm, "RTC callout frequency %d hz", SBT_1S / freqsbt);
- callout_reset_sbt(&vrtc->callout, freqsbt, 0, vrtc_callout_handler,
- vrtc, 0);
+
+ /*
+ * From the handler, the next deadline is one period after the previous
+ * one, not one period after the handler ran: else the latency of each
+ * callout adds up and the interrupt comes at a lower rate than the
+ * guest programmed. Start afresh if that deadline has passed.
+ */
+ now = sbinuptime();
+ if (periodic)
+ vrtc->callout_time += freqsbt;
+ if (!periodic || vrtc->callout_time < now)
+ vrtc->callout_time = now + freqsbt;
+ callout_reset_sbt(&vrtc->callout, vrtc->callout_time, 0,
+ vrtc_callout_handler, vrtc, C_ABSOLUTE);
}
static void
@@ -571,7 +585,7 @@ vrtc_callout_handler(void *arg)
freqsbt = vrtc_freq(vrtc);
KASSERT(freqsbt != 0, ("%s: vrtc frequency cannot be zero", __func__));
- vrtc_callout_reset(vrtc, freqsbt);
+ vrtc_callout_reset(vrtc, freqsbt, true);
done:
VRTC_UNLOCK(vrtc);
}
@@ -690,7 +704,7 @@ vrtc_set_reg_b(struct vrtc *vrtc, uint8_t newval)
*/
newfreq = vrtc_freq(vrtc);
if (newfreq != oldfreq)
- vrtc_callout_reset(vrtc, newfreq);
+ vrtc_callout_reset(vrtc, newfreq, false);
else
vrtc_callout_check(vrtc, newfreq);
@@ -742,7 +756,7 @@ vrtc_set_reg_a(struct vrtc *vrtc, uint8_t newval)
*/
newfreq = vrtc_freq(vrtc);
if (newfreq != oldfreq)
- vrtc_callout_reset(vrtc, newfreq);
+ vrtc_callout_reset(vrtc, newfreq, false);
else
vrtc_callout_check(vrtc, newfreq);
}
@@ -1054,7 +1068,7 @@ vrtc_snapshot(struct vrtc *vrtc, struct vm_snapshot_meta *meta)
SNAPSHOT_BUF_OR_LEAVE(vrtc->rtcdev.nvram2, sizeof(vrtc->rtcdev.nvram2),
meta, ret, done);
- vrtc_callout_reset(vrtc, vrtc_freq(vrtc));
+ vrtc_callout_reset(vrtc, vrtc_freq(vrtc), false);
done:
VRTC_UNLOCK(vrtc);