Skip to content

PIT Timer::evaluate is O(elapsed ticks / count); one PIO access after idle time can take hundreds of ms #1067

Closed

Description

@q5438722

Summary

Timer::evaluate steps a periodic counter through elapsed time one period at a time, about 2 loop iterations per period in modes 2 and 3. Only timer 0 arms a VM-time wakeup, so nothing evaluates timers 1 and 2 while the guest leaves the PIT alone. The first PIO access after an idle stretch (any access to ports 0x40–0x43 or 0x61) pays for the whole stretch on the vCPU thread.

Repro

pit_idle_evaluate_cost.txt

Add the attached pit_idle_evaluate_cost.rs (.txt) to vm/devices/chipset/tests/ and run:

cargo test -p chipset --test pit_idle_evaluate_cost

The test enables timer 2's gate (port 0x61) and programs counter 2 with count 2, first in mode 2 and then in mode 3. It advances frozen VM time by 64s and times one read of port 0x61.

Expected

One port access takes microseconds, however long the idle time.

Actual

Debug build:

thread 'access_after_idle_is_cheap' panicked at vm/devices/chipset/tests/pit_idle_evaluate_cost.rs:60:9:
mode 2: one PIO access after 64s idle took 844.280903ms

Release build (counter 2, count 2, mode 2; mode 3 is ~25% slower):

idle VM time one PIO access
2s 5.9ms
4s 11.7ms
8s 23.8ms
64s 204ms

The cost grows linearly, about 3.2ms per idle second; at that rate an hour of idle time would cost ~11s for a single access. With count 65536 the access takes microseconds. Restore can trigger the same thing, because it only checks that the saved last is not after now: a saved state with an old last makes the next access evaluate the whole gap.

Impact

A guest can stall its own vCPU with ordinary programming: counter 2 in a periodic mode with a small count, then idle. A stale or crafted saved state can stall the VMM that restores it.

Suggested fix

In modes 2 and 3 the counter state repeats every period, and only OUT differs between reloads. Once the counter reloads with the same OUT as at its first reload in this call, a full period has passed, so the remaining ticks can be reduced with ticks %= period. The skipped periods only repeat OUT transitions that already happened in this call, and nothing runs between them. The interrupt line therefore ends in the same state, and the interrupt controller latches the same request.

fix.patch

The new unit test test_periodic_fast_forward checks that the result matches one-tick-at-a-time evaluation. It covers modes 2 and 3, binary and BCD, and 11 counts (including 0, 1, 2 and odd counts), at every tick count up to ~4 periods.

Activity

  1. ppenna commented on Nov 13, 2026

    @ppenna
    Contributor

    Already fixed on dev. microsoft/openvmm#4586, merged upstream on 2026-10-28 as 023329997, makes the PIT skip whole periods, and dev has pinned it since #1075, merged on 2026-11-01 as 4f9a3214f, which pinned microVM v2.4 at 998b9f240. Before that, dev's pins carried the microVM chain's earlier versions of the same change, so this issue reproduces only on upstream's PIT before #4586, which its fix.patch applies to. The fix still holds at dev 8e9e9eb74, which pins OpenVMM 6f57a6d61, with the PIT fixes for #1063, #1065, and #1066 on top.

    What fixed it.

    • chipset: fast-forward periodic PIT counting over long intervals openvmm#4586 makes Timer::evaluate skip whole periods in modes 2 and 3. When an active rate generator or square wave has just reloaded (ce == cr with its count loaded, so null is false), the remaining ticks are reduced modulo the period: the count, 0x10000 or 10000 for a count of 0, and 0x10001 or 10001 for a mode 3 count of 1. Instead of replaying every skipped transition, OUT toggles once and back, so the line ends at the same level and an edge-triggered controller latches the same request. A mode 2 count of 1, which reloads every tick without lowering OUT, stops at once.
    • Every evaluate call therefore takes a bounded number of loop iterations, whatever the elapsed time, on every counter. Neither the first PIO access after idle time nor a restored last far in the past costs more than microseconds, and neither does ChangeDeviceState::start, which evaluates the counters after a restore.
    • Its tests are long_elapsed_interval_fast_forwards_periodic_pit, periodic_fast_forward_matches_incremental_from_each_phase, count_one_rate_generator_does_not_emit_irq0_edges, reprogrammed_periodic_count_loads_before_fast_forward, low_gate_pauses_periodic_fast_forward, and long_elapsed_interval_coalesces_periodic_irq0_edges.
    • Every dev pin since NVX's first commit has carried a version of this fast-forward. The chain's last one, 938b14343 (v2.3 position 023), was dropped by the v2.4 rebase of openvmm: pin microVM v2.4, rebased onto upstream main #1075 for upstream's merged version, which also handles counts of 1 and a reprogrammed count that has not loaded yet.

    Evidence. Each check ran on Windows x86-64 with Rust 1.97.1, in a checkout of the named revision with this issue's pit_idle_evaluate_cost test, extended with timing tables, added to vm/devices/chipset/tests/: cargo test -p chipset --test pit_idle_evaluate_cost -- --nocapture --test-threads=1, in debug and with --release, and cargo test -p chipset pit for the unit tests. The PIT does not depend on the host or the hypervisor. Times are for one read of port 0x61 after programming counter 2 with its gate enabled, unless noted, and are medians of 3 to 5 runs; the machine was shared, so the slow times vary by up to 2x between runs.

    OpenVMM revision Check Result
    43eb9f848, upstream main before #4586 (PIT blob 7df675e98) This issue's access_after_idle_is_cheap Fails: mode 2: one PIO access after 64s idle took 1.1323916s in debug and 114.4013ms in release
    43eb9f848, release Count 2, binary, after 2 s, 4 s, 8 s, 64 s, and 1 h of idle VM time Mode 2: 5.2 ms, 8.6 ms, 15.0 ms, 118 ms, and 14.9 s. Mode 3: 3.4 ms, 7.2 ms, 19.7 ms, 168 ms, and 25.4 s. Counter 0, whose interrupt line is connected, takes 2.9 s after 64 s
    43eb9f848, release First access after restoring a saved state whose last is 64 s or 1 h old, count 2 0.19 to 0.29 s after 64 s, and 19 to 39 s after 1 h
    3d7512af6, NVX's pin when this issue was filed, release The same test, and the tables for count 2 with idle times and restore gaps up to 1 h Passes; every access and restore step takes at most 0.4 µs
    6f57a6d61, the pin of dev 8e9e9eb74 This issue's test, debug and release Passes
    6f57a6d61 Counts 1, 2, and 3 in modes 2 and 3, binary and BCD, after 2 s, 4 s, 8 s, 64 s, 1 h, 1 day, and 365 days of idle time; counters 0, 1, and 2 after 365 days Flat in idle time: medians of 0.2 to 0.4 µs in release and 0.5 to 2.9 µs in debug; the slowest single run took 0.9 µs in release and 0.24 ms in debug
    6f57a6d61 Restore with last 1 h, 1 day, 365 days, and 100 years old, and with a crafted last of 0 and last_frac_ns of 99 at VM time 2^63 - 1 (100 ns units), then start() and the first access Each step takes at most 0.4 µs (release) and 6.2 µs (debug) in median, and no single run above 36 µs
    6f57a6d61 cargo test -p chipset pit with the scratch tests below, debug and release 29 of 29 pass, including the 23 existing PIT tests

    Scratch tests at 6f57a6d61, beside the existing ones, with the release run's counts:

    • This issue's test_periodic_fast_forward, unchanged, passes.
    • A stronger version checks every tick count from 1 to four periods plus three, with the true periods of counts 0 and 1 (up to 262,151 ticks), in modes 2 and 3, binary and BCD, for the patch's 11 counts. At all 926,276 tick counts, the whole TimerState matches one-tick evaluation and upstream's evaluation before #4586 in one call, and OUT rises and falls the same way, with at most 5 loop iterations per call.
    • 5,000 random programming sequences of 200 steps cover all eight mode encodings, latches, every access mode, reprogramming while counting, gate changes, and reads. After each of 460,482 evaluations, the state and OUT's edges match upstream's evaluation before #4586, and for the 426,946 of up to 100,000 ticks, one-tick evaluation too.
    • From 17,920 states that programming reaches and 2,000,000 random crafted states, evaluating 64 s, 1 h, and 365 days of ticks, 2^40 ticks, u64::MAX - 1, and u64::MAX ticks takes at most 7 loop iterations.
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