]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
KVM: x86: Defer vtime accounting 'til after IRQ handling
authorWanpeng Li <wanpengli@tencent.com>
Wed, 5 May 2021 00:27:30 +0000 (17:27 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 28 May 2021 11:17:43 +0000 (13:17 +0200)
commit 160457140187c5fb127b844e5a85f87f00a01b14 upstream.

Defer the call to account guest time until after servicing any IRQ(s)
that happened in the guest or immediately after VM-Exit.  Tick-based
accounting of vCPU time relies on PF_VCPU being set when the tick IRQ
handler runs, and IRQs are blocked throughout the main sequence of
vcpu_enter_guest(), including the call into vendor code to actually
enter and exit the guest.

This fixes a bug where reported guest time remains '0', even when
running an infinite loop in the guest:

  https://bugzilla.kernel.org/show_bug.cgi?id=209831

Fixes: 87fa7f3e98a131 ("x86/kvm: Move context tracking where it belongs")
Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Co-developed-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20210505002735.1684165-4-seanjc@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/x86/kvm/svm/svm.c
arch/x86/kvm/vmx/vmx.c
arch/x86/kvm/x86.c

index ca7a717477e702c84f46a3c5d96e4e1ad645c84a..9d4eb114613c2bc0cca171bd4beea567bb1f0779 100644 (file)
@@ -3532,15 +3532,15 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu,
         * have them in state 'on' as recorded before entering guest mode.
         * Same as enter_from_user_mode().
         *
-        * guest_exit_irqoff() restores host context and reinstates RCU if
-        * enabled and required.
+        * context_tracking_guest_exit() restores host context and reinstates
+        * RCU if enabled and required.
         *
         * This needs to be done before the below as native_read_msr()
         * contains a tracepoint and x86_spec_ctrl_restore_host() calls
         * into world and some more.
         */
        lockdep_hardirqs_off(CALLER_ADDR0);
-       guest_exit_irqoff();
+       context_tracking_guest_exit();
 
        instrumentation_begin();
        trace_hardirqs_off_finish();
index d7f8d2167fda0be0fedb2d5e7ce734b1d762842b..45877364e6829168af0b94910d7319c80393f0c6 100644 (file)
@@ -6640,15 +6640,15 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
         * have them in state 'on' as recorded before entering guest mode.
         * Same as enter_from_user_mode().
         *
-        * guest_exit_irqoff() restores host context and reinstates RCU if
-        * enabled and required.
+        * context_tracking_guest_exit() restores host context and reinstates
+        * RCU if enabled and required.
         *
         * This needs to be done before the below as native_read_msr()
         * contains a tracepoint and x86_spec_ctrl_restore_host() calls
         * into world and some more.
         */
        lockdep_hardirqs_off(CALLER_ADDR0);
-       guest_exit_irqoff();
+       context_tracking_guest_exit();
 
        instrumentation_begin();
        trace_hardirqs_off_finish();
index c071a83d543ae2db055042c7cfa96ff3f1e06ce4..7f767d59b09d31cab9ff8a5e0cb06d6e3547c4a8 100644 (file)
@@ -9063,6 +9063,15 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
        local_irq_disable();
        kvm_after_interrupt(vcpu);
 
+       /*
+        * Wait until after servicing IRQs to account guest time so that any
+        * ticks that occurred while running the guest are properly accounted
+        * to the guest.  Waiting until IRQs are enabled degrades the accuracy
+        * of accounting via context tracking, but the loss of accuracy is
+        * acceptable for all known use cases.
+        */
+       vtime_account_guest_exit();
+
        if (lapic_in_kernel(vcpu)) {
                s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
                if (delta != S64_MIN) {