linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "tip-bot2 for Wanpeng Li" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Wanpeng Li <wanpengli@tencent.com>,
	Sean Christopherson <seanjc@google.com>,
	stable@vger.kernel.org, x86@kernel.org,
	linux-kernel@vger.kernel.org
Subject: [tip: x86/urgent] KVM: x86: Defer vtime accounting 'til after IRQ handling
Date: Thu, 06 May 2021 12:14:04 -0000	[thread overview]
Message-ID: <162030324464.29796.14687279478344934242.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20210505002735.1684165-4-seanjc@google.com>

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     160457140187c5fb127b844e5a85f87f00a01b14
Gitweb:        https://git.kernel.org/tip/160457140187c5fb127b844e5a85f87f00a01b14
Author:        Wanpeng Li <wanpengli@tencent.com>
AuthorDate:    Tue, 04 May 2021 17:27:30 -07:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Wed, 05 May 2021 22:54:11 +02:00

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: 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
---
 arch/x86/kvm/svm/svm.c |  6 +++---
 arch/x86/kvm/vmx/vmx.c |  6 +++---
 arch/x86/kvm/x86.c     |  9 +++++++++
 3 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 9790c73..c400def 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -3753,15 +3753,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();
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index b21d751..e108fb4 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -6703,15 +6703,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();
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index cebdaa1..6eda283 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -9315,6 +9315,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) {

  parent reply	other threads:[~2021-05-06 12:14 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-05  0:27 [PATCH v4 0/8] KVM: Fix tick-based accounting for x86 guests Sean Christopherson
2021-05-05  0:27 ` [PATCH v4 1/8] context_tracking: Move guest exit context tracking to separate helpers Sean Christopherson
2021-05-06 12:14   ` [tip: x86/urgent] " tip-bot2 for Wanpeng Li
2021-05-05  0:27 ` [PATCH v4 2/8] context_tracking: Move guest exit vtime accounting " Sean Christopherson
2021-05-06 12:14   ` [tip: x86/urgent] " tip-bot2 for Wanpeng Li
2021-05-05  0:27 ` [PATCH v4 3/8] KVM: x86: Defer vtime accounting 'til after IRQ handling Sean Christopherson
2021-05-05 20:14   ` Thomas Gleixner
2021-05-06 12:14   ` tip-bot2 for Wanpeng Li [this message]
2021-05-05  0:27 ` [PATCH v4 4/8] sched/vtime: Move vtime accounting external declarations above inlines Sean Christopherson
2021-05-06 12:14   ` [tip: x86/urgent] " tip-bot2 for Sean Christopherson
2021-05-05  0:27 ` [PATCH v4 5/8] sched/vtime: Move guest enter/exit vtime accounting to vtime.h Sean Christopherson
2021-05-06 12:14   ` [tip: x86/urgent] " tip-bot2 for Sean Christopherson
2021-05-05  0:27 ` [PATCH v4 6/8] context_tracking: Consolidate guest enter/exit wrappers Sean Christopherson
2021-05-06 12:14   ` [tip: x86/urgent] " tip-bot2 for Sean Christopherson
2021-05-05  0:27 ` [PATCH v4 7/8] context_tracking: KVM: Move guest enter/exit wrappers to KVM's domain Sean Christopherson
2021-05-06 12:14   ` [tip: x86/urgent] " tip-bot2 for Sean Christopherson
2021-05-05  0:27 ` [PATCH v4 8/8] KVM: x86: Consolidate guest enter/exit logic to common helpers Sean Christopherson
2021-05-06 12:14   ` [tip: x86/urgent] " tip-bot2 for Sean Christopherson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=162030324464.29796.14687279478344934242.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=seanjc@google.com \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=wanpengli@tencent.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).