KVM: x86: Set PF_VCPU when processing IRQs to fix tick-based accounting
Sean Christopherson
maintainerRFC PATCH 1/1 KVM: x86: Set PF_VCPU when processing IRQs to fix tick-based accounting
Temporarily set PF_VCPU while processing IRQ VM-Exits so that a tick IRQ accounts the time slice to the guest. Tick-based accounting of guest time is currently broken as PF_VCPU is only set during the relatively short VM-Enter sequence, which runs entirely with IRQs disabled, and IRQs that occur in the guest are processed well after PF_VCPU is cleared.
Keep PF_VCPU set across both VMX's processing of already-acked IRQs (handle_exit_irqoff()) and the explicit IRQ window (SVM's processing, plus ticks that occur immediately after VM-Exit on both VMX and SVM).
---
This is quite obnoxious, hence the RFC, but I can't think of a clever, less ugly way to fix the accounting.
arch/x86/kvm/x86.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/kvm/x86.c +8 -0
@@ -9118,6 +9118,13 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) vcpu->mode =OUTSIDE_GUEST_MODE ;smp_wmb (); +/* +* Temporarily pretend this task is running a vCPU when potentially +* processing an IRQ exit, including the below opening of an IRQ +* window. Tick-based accounting of guest time relies on PF_VCPU +* being set when the tick IRQ handler runs. +*/ + current->flags |=PF_VCPU ;static_call (kvm_x86_handle_exit_irqoff)(vcpu);/*
@@ -9132,6 +9139,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) ++vcpu->stat.exits;local_irq_disable ();kvm_after_interrupt (vcpu); + current->flags &= ~PF_VCPU ;if (lapic_in_kernel (vcpu)) {s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
Signature · 1 line
2.30.0.478.g8a0d178c01-goog
Quoted 28 lines · Sean Christopherson
Temporarily set
PF_VCPUwhile processing IRQ VM-Exits so that a tick IRQ accounts the time slice to the guest. Tick-based accounting of guest time is currently broken asPF_VCPUis only set during the relatively short VM-Enter sequence, which runs entirely with IRQs disabled, and IRQs that occur in the guest are processed well afterPF_VCPUis cleared.Keep
PF_VCPUset across both VMX's processing of already-acked IRQs (handle_exit_irqoff()) and the explicit IRQ window (SVM's processing, plus ticks that occur immediately after VM-Exit on both VMX and SVM).Fixes 87fa7f3e98a1 ("x86/kvm: Move context tracking where it belongs")Cc Peter Zijlstra <[email protected]>Cc Thomas Gleixner <[email protected]>Signed-off-by Sean Christopherson <[email protected]>---
This is quite obnoxious, hence the RFC, but I can't think of a clever, less ugly way to fix the accounting.
arch/x86/kvm/x86.c | 8 ++++++++ 1 file changed, 8 insertions(+)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 9127
Quoted 10 lines
arch/x86/kvm/x86.c +7
-0@@ -9118,6 +9118,13 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) vcpu->mode =OUTSIDE_GUEST_MODE ;smp_wmb (); +/* +* Temporarily pretend this task is running a vCPU when potentially +* processing an IRQ exit, including the below opening of an IRQ +* window. Tick-based accounting of guest time relies on PF_VCPU +* being set when the tick IRQ handler runs. +*/ + current->flags |=PF_VCPU ;
Should we do it only when !vtime_accounting_enabled_cpu() maybe?
Quoted 11 lines
arch/x86/kvm/x86.c +1
-0static_call (kvm_x86_handle_exit_irqoff)(vcpu);/* @@ -9132,6 +9139,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) ++vcpu->stat.exits;local_irq_disable ();kvm_after_interrupt (vcpu); + current->flags &= ~PF_VCPU ;if (lapic_in_kernel (vcpu)) {s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
Signature · 1 line
Vitaly