Skip to contentSkip to conversation

FAILED: patch "[PATCH] KVM: x86: Defer vtime accounting 'til after IRQ handling" failed to apply to 5.10-stable tree

PATCH [email protected] 3 participants 5 messages May 14, 2021 – May 27, 2021

Subsystems (3)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

The patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

thanks,

greg k-h

------------------ original commit in Linus's tree ------------------

From 160457140187c5fb127b844e5a85f87f00a01b14 Mon Sep 17 00:00:00 2001
From: Wanpeng Li <[email protected]>
Date: Tue, 4 May 2021 17:27:30 -0700
Subject: [PATCH] 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

arch/x86/kvm/svm/svm.c +3 -3
index 9790c73f2a32..c400def6220b 100644
@@ -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();
arch/x86/kvm/vmx/vmx.c +3 -3
index b21d751407b5..e108fb47855b 100644
@@ -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();
arch/x86/kvm/x86.c +9 -0
index cebdaa1e3cf5..6eda2834fc05 100644
@@ -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) {
#1 lore raw
Quoted 6 lines · <[email protected]> 于2021年5月14日周五 下午9:32写道:

The patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

If I first apply 866a6dadbb02 ("context_tracking: Move guest exit context tracking to separate helpers")
and 88d8220bbf06 ("context_tracking: Move guest exit vtime accounting to separate helpers")

then I can apply this commit cleanly to latest 5.10.y, I suppose it will work for 5.12.

Quoted 4 lines

thanks,

greg k-h

Thanks!
Jack Wang

Quoted 96 lines

------------------ original commit in Linus's tree ------------------

From 160457140187c5fb127b844e5a85f87f00a01b14 Mon Sep 17 00:00:00 2001
From: Wanpeng Li <[email protected]>
Date: Tue, 4 May 2021 17:27:30 -0700
Subject: [PATCH] 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

arch/x86/kvm/svm/svm.c +3 -3
index 9790c73f2a32..c400def6220b 100644
@@ -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();
arch/x86/kvm/vmx/vmx.c +3 -3
index b21d751407b5..e108fb47855b 100644
@@ -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();
arch/x86/kvm/x86.c +9 -0
index cebdaa1e3cf5..6eda2834fc05 100644
@@ -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) {
author maintainer
#2 lore raw
Quoted 8 lines · Jack Wang
Quoted 6 lines · <[email protected]> 于2021年5月14日周五 下午9:32写道:

The patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

Quoted 7 lines

If I first apply 866a6dadbb02 ("context_tracking: Move guest exit context tracking to separate helpers")
and 88d8220bbf06 ("context_tracking: Move guest exit vtime accounting to separate helpers")

then I can apply this commit cleanly to latest 5.10.y, I suppose it will work for 5.12.

That worked, thanks! Now queued up.

greg k-h

#3 lore raw
Quoted 9 lines · Greg Kroah-Hartman
Quoted 8 lines · Jack Wang
Quoted 6 lines · <[email protected]> 于2021年5月14日周五 下午9:32写道:

The patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

Quoted 7 lines
Quoted 7 lines

If I first apply 866a6dadbb02 ("context_tracking: Move guest exit context tracking to separate helpers")
and 88d8220bbf06 ("context_tracking: Move guest exit vtime accounting to separate helpers")

then I can apply this commit cleanly to latest 5.10.y, I suppose it will work for 5.12.

Thanks much!

Quoted 1 line

That worked, thanks! Now queued up.

To not mess up in the future, I assume known dependencies should be tagged "Cc: stable...", even if the dependencies aren't technically bug fixes? The plan all along was that all three patches would have to be picked up, but apparently I thought that would happen automagically.

author maintainer
#4 lore raw
Quoted 18 lines · Sean Christopherson
Quoted 9 lines · Greg Kroah-Hartman
Quoted 8 lines · Jack Wang
Quoted 6 lines · <[email protected]> 于2021年5月14日周五 下午9:32写道:

The patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

Quoted 7 lines
Quoted 7 lines

If I first apply 866a6dadbb02 ("context_tracking: Move guest exit context tracking to separate helpers")
and 88d8220bbf06 ("context_tracking: Move guest exit vtime accounting to separate helpers")

then I can apply this commit cleanly to latest 5.10.y, I suppose it will work for 5.12.

Quoted 6 lines

Thanks much!

Quoted 1 line

That worked, thanks! Now queued up.

To not mess up in the future, I assume known dependencies should be tagged "Cc: stable...", even if the dependencies aren't technically bug fixes?

Yes, the documentation shows also how to do this semi-automatically:

https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html