Skip to content

openvmm: time-ABI restore panics in a log line if the host clock steps back #1375

Closed

Description

@tangruize

LoadedVm::time_abi_restore logs each phase's duration by subtracting one Duration from another. If the host's monotonic clock steps back between the reading at the end of the TSC set and the reading at the end of the LAPIC or VM-time phase, the subtraction panics with "overflow when subtracting durations", in release builds too. The microVM restore then aborts, after the TSCs, LAPICs and VM time were already set. This happens on any backend, at OpenVMM's default info log level. Only a faulty host clock triggers it: std::time::Instant assumes a monotonic clock, which some hosts and VMs do not provide.

Reproduction

Not reproduced: it needs a host clock that steps backwards. The panic happens when lapic_advanced or vm_time_advanced (each started.elapsed(), read at the end of its phase) is smaller than tsc_set.

Cause

The "time ABI restore clock" event subtracts the started.elapsed() readings from each other:

// openvmm/openvmm_core/src/worker/dispatch/time_abi.rs
let tsc_set = started.elapsed();
// ...
let ((lapic_result, lapic_advanced), (vm_time_result, vm_time_advanced)) =
    (lapic_advance, vm_time_advance).join().await;
// ...
tracing::info!(
    tsc_set_us = tsc_set.as_micros() as u64,
    lapic_us = (lapic_advanced - tsc_set).as_micros() as u64,
    vm_time_us = (vm_time_advanced - tsc_set).as_micros() as u64,

Instant::elapsed saturates, but subtracting one Duration from another does not.

Fix

fix.patch computes both values with saturating_sub before the event, so a backwards step logs 0 and the restore completes. Only these two log fields change. The patch adds no test, since Instant cannot be injected here.

Activity

  1. tangruize commented on Nov 8, 2026

    @tangruize
    ContributorAuthor

    Proposed patch

    Patch against OpenVMM 3cf58a5234ed824435825c9f87187f5129219724. Save the contents below as fix.patch and run git apply fix.patch.

    fix.patch
    diff --git a/openvmm/openvmm_core/src/worker/dispatch/time_abi.rs b/openvmm/openvmm_core/src/worker/dispatch/time_abi.rs
    index c2cdeeb..db8c651 100644
    --- a/openvmm/openvmm_core/src/worker/dispatch/time_abi.rs
    +++ b/openvmm/openvmm_core/src/worker/dispatch/time_abi.rs
    @@ -1201,11 +1201,14 @@ pub(super) async fn time_abi_restore(&mut self, input: RestoreTimeInput) -> anyh
             let finished = started.elapsed();
             // Per-phase durations attribute the restore.time_abi_clock profile
             // phase. The LAPIC and VM time phases overlap; each is measured from
    -        // the end of the TSC set.
    +        // the end of the TSC set. A faulty host monotonic clock may step
    +        // backwards; clamp the logged phase duration instead of panicking.
    +        let lapic_us = lapic_advanced.saturating_sub(tsc_set).as_micros() as u64;
    +        let vm_time_us = vm_time_advanced.saturating_sub(tsc_set).as_micros() as u64;
             tracing::info!(
                 tsc_set_us = tsc_set.as_micros() as u64,
    -            lapic_us = (lapic_advanced - tsc_set).as_micros() as u64,
    -            vm_time_us = (vm_time_advanced - tsc_set).as_micros() as u64,
    +            lapic_us,
    +            vm_time_us,
                 total_us = finished.as_micros() as u64,
                 "time ABI restore clock"
             );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions