You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
openvmm: RTC periodic catch-up stalls restore with billions of iterations #1164
An RTC with periodic interrupts enabled can spend excessive time catching up after snapshot downtime. At 8192 Hz, 30 days of advancement leave roughly 21 billion missed periods, delaying restored execution while the device processes them individually.
In vm/vmcore/src/vmtime.rs, VmTimerPeriodic::poll_timeout advances the deadline only one period per iteration:
letmut res = Poll::Pending;whileletPoll::Ready(now) = self.vmtime.poll_timeout(cx){
res = Poll::Ready(now);let time = last_timeout.wrapping_add(period);self.vmtime.set_timeout(time);*last_timeout = time;}
res
Every replacement deadline remains overdue until the loop traverses the entire backlog. The RTC only latches one periodic-interrupt flag, so visiting each missed deadline adds work without delivering additional useful interrupts.
Trigger
Enable periodic interrupts with RTC status A's rate selector 3, advance stopped VM time by a long snapshot downtime, and poll the restored RTC. The same helper also backs update-ended interrupts, although their one-second period produces a smaller backlog.
Fix
fix.patch skips whole periods arithmetically while preserving the existing 100 ns deadline grid and wraparound ordering. It registers the next wake without an unbounded loop when a short period expires during polling, and rounds sub-resolution periods up to 100 ns.
Catch-up still produces one pending interrupt rather than replaying every missed tick. The patch does not change the saved-state format or resolve the separate question of preserving timer phase across restore.
An RTC with periodic interrupts enabled can spend excessive time catching up after snapshot downtime. At 8192 Hz, 30 days of advancement leave roughly 21 billion missed periods, delaying restored execution while the device processes them individually.
In vm/vmcore/src/vmtime.rs, VmTimerPeriodic::poll_timeout advances the deadline only one period per iteration:
let mut res = Poll::Pending;
while let Poll::Ready(now) = self.vmtime.poll_timeout(cx) {
res = Poll::Ready(now);
let time = last_timeout.wrapping_add(period);
self.vmtime.set_timeout(time);
*last_timeout = time;
}
res
Every replacement deadline remains overdue until the loop traverses the entire backlog. The RTC only latches one periodic-interrupt flag, so visiting each missed deadline adds work without delivering additional useful interrupts.
Trigger
Enable periodic interrupts with RTC status A's rate selector 3, advance stopped VM time by a long snapshot downtime, and poll the restored RTC. The same helper also backs update-ended interrupts, although their one-second period produces a smaller backlog.
Fix
fix.patch skips whole periods arithmetically while preserving the existing 100 ns deadline grid and wraparound ordering. It registers the next wake without an unbounded loop when a short period expires during polling, and rounds sub-resolution periods up to 100 ns.
Catch-up still produces one pending interrupt rather than replaying every missed tick. The patch does not change the saved-state format or resolve the separate question of preserving timer phase across restore.
just add one reproduction test case:
use pal_async::DefaultPool;use std::future::poll_fn;use std::sync::mpsc;use std::time::Duration;use vmcore::vmtime::SavedState;use vmcore::vmtime::VmTime;use vmcore::vmtime::VmTimeKeeper;use vmcore::vmtime::VmTimerPeriodic;/// Runs `f` on its own thread and fails if it does not finish within 10s:/// a spinning `poll_timeout` blocks its executor thread for good.fnwithin_10s(what:&'staticstr,f:implFnOnce() + Send + 'static){let(done, finished) = mpsc::channel();
std::thread::spawn(move || {f();let _ = done.send(());});
finished
.recv_timeout(Duration::from_secs(10)).unwrap_or_else(|_| {panic!("{what}: VmTimerPeriodic::poll_timeout did not return within 10s")});}/// Starts VM time at 0 and a periodic timer with `period`, then waits for/// the timer to fire twice.fnfires_twice(period:Duration){DefaultPool::run_with(async |driver| {letmut keeper = VmTimeKeeper::new(&driver,VmTime::from_100ns(0));let source = keeper.builder().build(&driver).await.unwrap();letmut timer = VmTimerPeriodic::new(source.access("periodic"));
keeper.start().await;
timer.start(period);poll_fn(|cx| timer.poll_timeout(cx)).await;poll_fn(|cx| timer.poll_timeout(cx)).await;
keeper.stop().await;});}/// Control: an ordinary period.#[test]fnperiodic_timer_fires(){within_10s("1ms period", || fires_twice(Duration::from_millis(1)));}/// A period shorter than the 100ns resolution of VM time.#[test]fnsub_resolution_period_does_not_spin(){within_10s("50ns period", || fires_twice(Duration::from_nanos(50)));}/// VM time moves far ahead of the timer's last deadline, as a restore or a/// downtime advance does: the missed periods must be skipped at once.#[test]fnmissed_periods_are_skipped_at_once(){within_10s("catch-up after 2^50 units", || {DefaultPool::run_with(async |driver| {letmut keeper = VmTimeKeeper::new(&driver,VmTime::from_100ns(0));let source = keeper.builder().build(&driver).await.unwrap();letmut timer = VmTimerPeriodic::new(source.access("periodic"));
keeper.start().await;
timer.start(Duration::from_millis(1));poll_fn(|cx| timer.poll_timeout(cx)).await;
keeper.stop().await;// ~3.6 years later, that is ~10^11 periods of 1ms.
keeper
.restore(SavedState::from_vmtime(VmTime::from_100ns(1 << 50))).await;
keeper.start().await;let now = poll_fn(|cx| timer.poll_timeout(cx)).await;assert!(!now.is_before(VmTime::from_100ns(1 << 50)));// And it keeps firing.poll_fn(|cx| timer.poll_timeout(cx)).await;
keeper.stop().await;});});}
Activity
tangruize commentedon Nov 1, 2026
Proposed patch
Patch against OpenVMM
998b9f2402050cd16d1faa897bc831ef1434be48. Save the contents below asfix.patchand rungit apply fix.patch.fix.patchq5438722 commentedon Nov 3, 2026
just add one reproduction test case:
openvmm: pin the periodic timer's one-step catch-up
vmcore: skip a periodic timer's missed periods at once