KVM: x86/pmu: Add module param to batch emulated-instruction reprograms
PATCH v2 1/1 KVM: x86/pmu: Add module param to batch emulated-instruction reprograms
On a host with an emulated vPMU (guest PMU MSR accesses trap and each guest counter is backed by a host perf_event), the per-emulated- instruction PMU accounting added by commit 9cd803d496e7 ("KVM: x86:
Update vPMCs when retiring instructions") is pathologically expensive.
kvm_pmu_incr_counter() requests a counter reprogram (KVM_REQ_PMU) on every emulated instruction that matches a programmed counter. The reprogram is drained on the vCPU's next VM-entry, where reprogram_counter() runs the full pmc_pause_counter() + perf_event_period() + perf_event_enable() sequence: ctx->mutex, a ctx_resched() of the PMU context, and a burst of serialized PMU-MSR writes. Because the batched reprogram is serviced on the next VM-entry regardless of which exit preceded it, ordinary exits -- notably the guest's 1kHz timer tick -- absorb the cost, inflating timer interrupts into the hundreds of microseconds and, under some workloads, escalating to a guest CSD lockup.
Add emulated_counter_reprogram_tolerance, a module parameter bounding how many emulated instructions may accumulate on a counter before KVM forces a reprogram. 0 (default) preserves today's behavior of reprogramming on every emulated instruction. A larger value batches the reprograms.
Batching does not lose counts: pmc_read_counter() and reprogram_counter() already fold pmc->emulated_counter into the value observed on guest counter reads, so RDPMC/RDMSR remain accurate. The only effect is that an overflow-driven PMI may be delivered up to "tolerance" instructions late -- acceptable given the overflow PMI is not cycle-accurate to begin with. The knob only affects the emulated vPMU; a mediated vPMU increments its counter directly and is unchanged.
---
v1 (RFC): https://lore.kernel.org/kvm/[email protected]/
Changes since v1:
- Drop the new KVM_CAP_X86_DISABLE_PMU_SW_ACCOUNTING capability and its Documentation, i.e. add no new uAPI.
- Instead of disabling emulated-instruction accounting outright, batch the counter reprograms behind a tolerance threshold, per David's suggestion to bound the reprogram rate rather than skip accounting [1]. This keeps guest counter reads accurate and only bounds how late an overflow PMI is delivered.
- Expose the threshold as the emulated_counter_reprogram_tolerance module param (uint, 0644); 0 preserves current behavior.
[1] https://lore.kernel.org/kvm/[email protected]/
Results (single run, not averaged over multiple runs):
Host: AMD EPYC 7R13 (Milan), emulated vPMU. Guest: 8 vCPUs under an 8-thread busy load, running a binary that toggles a PMU counter group cross-vCPU once per second while an instructions-retired counter is resident. Latency is the duration of the guest's local-APIC timer interrupt handler (__sysvec_apic_timer_interrupt), measured in-guest with ftrace function_graph over 30s (~160k ticks per pass). The module param was changed live on one running guest between passes.
tolerance mean p50 p99 p99.9 max
---------- -------- ------- ------- ------- -------- 0 (default) 148.973 11.080 464.200 469.569 919.690 (us) 1000 3.194 2.950 7.251 9.831 470.100 100000 2.974 2.940 3.869 4.400 8.060 10000000 3.054 2.940 7.129 9.211 209.880
These are numbers from a single run and are meant only to show the order of magnitude, not to be precise.
arch/x86/kvm/pmu.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
arch/x86/kvm/pmu.c +17 -1
@@ -39,6 +39,14 @@ bool __read_mostly enable_pmu = true;EXPORT_SYMBOL_FOR_KVM_INTERNAL (enable_pmu);module_param (enable_pmu, bool,0444 ); +/* +* Number of KVM-emulated instructions that may accumulate on an emulated +* (perf-based) vPMU counter before KVM forces a counter reprogram to fold the +* emulated count into the backing perf_event +*/ +static uint __read_mostly emulated_counter_reprogram_tolerance; +module_param (emulated_counter_reprogram_tolerance, uint,0644 ); +/* Enable/disabled mediated PMU virtualization. */ bool __read_mostly enable_mediated_pmu;EXPORT_SYMBOL_FOR_KVM_INTERNAL (enable_mediated_pmu);
@@ -1072,7 +1080,15 @@ static void kvm_pmu_incr_counter(struct kvm_pmc *pmc)*/ if (!kvm_vcpu_has_mediated_pmu (vcpu)) { pmc->emulated_counter++; -kvm_pmu_request_counter_reprogram (pmc); + +/* +* Batch reprograms: only force one once the accumulated +* emulated count exceeds the tolerance. The count is still +* reflected in guest counter reads via pmc->emulated_counter; +* this only bounds how late an overflow-driven PMI arrives. +*/ +if (pmc->emulated_counter > emulated_counter_reprogram_tolerance) +kvm_pmu_request_counter_reprogram (pmc);return ; }
base-commit: 1590cf0329716306e948a8fc29f1d3ee87d3989f
Signature · 1 line
2.47.3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Batching emulated counter reprograms delays or permanently loses overflow PMIs and corrupts…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Batching emulated counter reprograms delays or permanently loses overflow PMIs and corrupts GLOBAL_STATUS across live migration.
- [Medium] Single-wrap overflow detection in pmc_pause_counter() silently fails if the batched emulated counts exceed the counter's bitmask.
- [Low] Unprotected concurrent read of the emulated_counter_reprogram_tolerance module parameter.
--
--- Patch [1]: [PATCH v2] KVM: x86/pmu: Add module param to batch emulated-instruction reprograms --- commit 85f50daf9aa2d9bd44f19438d6b61d03c98f6c16
Author: Luka Absandze <[email protected]>
KVM: x86/pmu: Add module param to batch emulated-instruction reprograms
This patch introduces a module parameter to batch PMU counter reprograms triggered by emulated instructions. It defers the reprogramming until the accumulated emulated count exceeds the specified tolerance, which reduces the overhead on VM-entry.
Quoted 4 lines
arch/x86/kvm/pmu.c +0
-0
[ ... ]
arch/x86/kvm/pmu.c bool __read_mostly enable_pmu = true; · line 48
Quoted 11 lines
arch/x86/kvm/pmu.c +7
-0@@ -39,6 +39,14 @@ bool __read_mostly enable_pmu = true;EXPORT_SYMBOL_FOR_KVM_INTERNAL (enable_pmu);module_param (enable_pmu, bool,0444 ); +/* +* Number of KVM-emulated instructions that may accumulate on an emulated +* (perf-based) vPMU counter before KVM forces a counter reprogram to fold the +* emulated count into the backing perf_event +*/ +static uint __read_mostly emulated_counter_reprogram_tolerance; +module_param (emulated_counter_reprogram_tolerance, uint,0644 );
[Severity: Low]
Since the module parameter is exposed via sysfs with 0644 permissions, the host administrator can modify it dynamically.
Could the unprotected concurrent read of emulated_counter_reprogram_tolerance in kvm_pmu_incr_counter() lead to inconsistent reads?
Using READ_ONCE() when accessing this shared mutable state might prevent potential compiler optimization issues.
[ ... ]
Quoted 4 lines
arch/x86/kvm/pmu.c +0
-0@@ -1072,7 +1080,15 @@ static void kvm_pmu_incr_counter(struct kvm_pmc *pmc)*/ if (!kvm_vcpu_has_mediated_pmu (vcpu)) { pmc->emulated_counter++;
arch/x86/kvm/pmu.c static void kvm_pmu_incr_counter(struct kvm_pmc *pmc) · line 1091
Quoted 10 lines
arch/x86/kvm/pmu.c +9
-1-kvm_pmu_request_counter_reprogram (pmc); + +/* +* Batch reprograms: only force one once the accumulated +* emulated count exceeds the tolerance. The count is still +* reflected in guest counter reads via pmc->emulated_counter; +* this only bounds how late an overflow-driven PMI arrives. +*/ +if (pmc->emulated_counter > emulated_counter_reprogram_tolerance) +kvm_pmu_request_counter_reprogram (pmc);
[Severity: High]
Does this batching logic risk permanently delaying or dropping overflow detection and PMI injection?
KVM relies on pmc_pause_counter() during a requested reprogram to detect overflows and inject PMIs. By deferring the reprogram until the accumulated emulated count exceeds the tolerance, the logical counter value becomes decoupled from its overflow state.
If the guest stops executing the specific instruction before the tolerance is reached, the reprogram is never requested and the PMI might be permanently lost.
Additionally, during live migration, KVM_GET_MSRS saves the combined counter value but the unmodified GLOBAL_STATUS. Could the target VM restore the counter with emulated_counter implicitly zeroed, completely losing the pending overflow?
[Severity: Medium]
Can deferring the reprogram create an issue with the single-wrap overflow detection?
If a host administrator sets emulated_counter_reprogram_tolerance to a pathologically high value like UINT_MAX, pmc->emulated_counter can exceed the counter's bitmask.
Looking at pmc_pause_counter() in arch/x86/kvm/pmu.c:
counter += pmc->emulated_counter;pmc->counter = counter & pmc_bitmask(pmc);
...
return pmc->counter < prev_counter;
If emulated_counter reaches exactly 2^32, adding it to a 32-bit counter wraps the value exactly once, resulting in pmc->counter == prev_counter. Will the less-than check evaluate to false, causing KVM to miss the overflow?
Quoted 3 lines
arch/x86/kvm/pmu.c +0
-0return ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 2 lines · On 2026-07-23 11:18, [email protected] wrote:
Using
READ_ONCE()when accessing this shared mutable state might prevent potential compiler optimization issues.
Fair.
Quoted 4 lines
[Severity: High]
Does this batching logic risk permanently delaying or dropping overflow detection and PMI injection?
Quoted 8 lines
KVM relies on
pmc_pause_counter()during a requested reprogram to detect overflows and inject PMIs. By deferring the reprogram until the accumulated emulated count exceeds the tolerance, the logical counter value becomes decoupled from its overflow state.If the guest stops executing the specific instruction before the tolerance is reached, the reprogram is never requested and the PMI might be permanently lost.
This is what we've been discussing. PMIs will be delayed by up to tolerance. If the guest stops executing instructions at this point, then so be it.
Quoted 4 lines
Additionally, during live migration,
KVM_GET_MSRSsaves the combined counter value but the unmodifiedGLOBAL_STATUS. Could the target VM restore the counter withemulated_counterimplicitly zeroed, completely losing the pending overflow?
This is true. We fold in the emulated counter without checking for a potential overflow. So yes, we could lose one NMI across migration however this does not seem to be new. The same scenario could happen today with a vCPU that takes a VMEXIT and gets migrated with the emulated counter not yet reflected in hardware.
Quoted 3 lines
[Severity: Medium]
Can deferring the reprogram create an issue with the single-wrap overflow detection?
I suppose we should do something like:
u64 tolerance = min_t(u64,
READ_ONCE(emulated_counter_reprogram_tolerance),
pmc_bitmask(pmc)); if (pmc->emulated_counter > tolerance)
kvm_pmu_request_counter_reprogram(pmc);