Skip to content

Commit 514883e

Browse files
Wanpeng Ligregkh
authored andcommitted
KVM: x86: Defer vtime accounting 'til after IRQ handling
commit 1604571 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: 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] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 7706830 commit 514883e

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
@@ -3532,15 +3532,15 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu,
35323532
* have them in state 'on' as recorded before entering guest mode.
35333533
* Same as enter_from_user_mode().
35343534
*
3535-
* guest_exit_irqoff() restores host context and reinstates RCU if
3536-
* enabled and required.
3535+
* context_tracking_guest_exit() restores host context and reinstates
3536+
* RCU if enabled and required.
35373537
*
35383538
* This needs to be done before the below as native_read_msr()
35393539
* contains a tracepoint and x86_spec_ctrl_restore_host() calls
35403540
* into world and some more.
35413541
*/
35423542
lockdep_hardirqs_off(CALLER_ADDR0);
3543-
guest_exit_irqoff();
3543+
context_tracking_guest_exit();
35443544

35453545
instrumentation_begin();
35463546
trace_hardirqs_off_finish();

arch/x86/kvm/vmx/vmx.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6640,15 +6640,15 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
66406640
* have them in state 'on' as recorded before entering guest mode.
66416641
* Same as enter_from_user_mode().
66426642
*
6643-
* guest_exit_irqoff() restores host context and reinstates RCU if
6644-
* enabled and required.
6643+
* context_tracking_guest_exit() restores host context and reinstates
6644+
* RCU if enabled and required.
66456645
*
66466646
* This needs to be done before the below as native_read_msr()
66476647
* contains a tracepoint and x86_spec_ctrl_restore_host() calls
66486648
* into world and some more.
66496649
*/
66506650
lockdep_hardirqs_off(CALLER_ADDR0);
6651-
guest_exit_irqoff();
6651+
context_tracking_guest_exit();
66526652

66536653
instrumentation_begin();
66546654
trace_hardirqs_off_finish();

arch/x86/kvm/x86.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9063,6 +9063,15 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
90639063
local_irq_disable();
90649064
kvm_after_interrupt(vcpu);
90659065

9066+
/*
9067+
* Wait until after servicing IRQs to account guest time so that any
9068+
* ticks that occurred while running the guest are properly accounted
9069+
* to the guest. Waiting until IRQs are enabled degrades the accuracy
9070+
* of accounting via context tracking, but the loss of accuracy is
9071+
* acceptable for all known use cases.
9072+
*/
9073+
vtime_account_guest_exit();
9074+
90669075
if (lapic_in_kernel(vcpu)) {
90679076
s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
90689077
if (delta != S64_MIN) {

0 commit comments

Comments
 (0)