Skip to content

Commit 1604571

Browse files
Wanpeng LiKAGA-KOKO
authored andcommitted
KVM: x86: Defer vtime accounting 'til after IRQ handling
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: 87fa7f3 ("x86/kvm: Move context tracking where it belongs") Suggested-by: Thomas Gleixner <[email protected]> Co-developed-by: Sean Christopherson <[email protected]> Signed-off-by: Wanpeng Li <[email protected]> Signed-off-by: Sean Christopherson <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected]
1 parent 88d8220 commit 1604571

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

arch/x86/kvm/svm/svm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3753,15 +3753,15 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
37533753
* have them in state 'on' as recorded before entering guest mode.
37543754
* Same as enter_from_user_mode().
37553755
*
3756-
* guest_exit_irqoff() restores host context and reinstates RCU if
3757-
* enabled and required.
3756+
* context_tracking_guest_exit() restores host context and reinstates
3757+
* RCU if enabled and required.
37583758
*
37593759
* This needs to be done before the below as native_read_msr()
37603760
* contains a tracepoint and x86_spec_ctrl_restore_host() calls
37613761
* into world and some more.
37623762
*/
37633763
lockdep_hardirqs_off(CALLER_ADDR0);
3764-
guest_exit_irqoff();
3764+
context_tracking_guest_exit();
37653765

37663766
instrumentation_begin();
37673767
trace_hardirqs_off_finish();

arch/x86/kvm/vmx/vmx.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6703,15 +6703,15 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
67036703
* have them in state 'on' as recorded before entering guest mode.
67046704
* Same as enter_from_user_mode().
67056705
*
6706-
* guest_exit_irqoff() restores host context and reinstates RCU if
6707-
* enabled and required.
6706+
* context_tracking_guest_exit() restores host context and reinstates
6707+
* RCU if enabled and required.
67086708
*
67096709
* This needs to be done before the below as native_read_msr()
67106710
* contains a tracepoint and x86_spec_ctrl_restore_host() calls
67116711
* into world and some more.
67126712
*/
67136713
lockdep_hardirqs_off(CALLER_ADDR0);
6714-
guest_exit_irqoff();
6714+
context_tracking_guest_exit();
67156715

67166716
instrumentation_begin();
67176717
trace_hardirqs_off_finish();

arch/x86/kvm/x86.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9315,6 +9315,15 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
93159315
local_irq_disable();
93169316
kvm_after_interrupt(vcpu);
93179317

9318+
/*
9319+
* Wait until after servicing IRQs to account guest time so that any
9320+
* ticks that occurred while running the guest are properly accounted
9321+
* to the guest. Waiting until IRQs are enabled degrades the accuracy
9322+
* of accounting via context tracking, but the loss of accuracy is
9323+
* acceptable for all known use cases.
9324+
*/
9325+
vtime_account_guest_exit();
9326+
93189327
if (lapic_in_kernel(vcpu)) {
93199328
s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
93209329
if (delta != S64_MIN) {

0 commit comments

Comments
 (0)