[<prev] [next>] [day] [month] [year] [list]
Message-Id: <1634724053-73627-1-git-send-email-wanpengli@tencent.com>
Date: Wed, 20 Oct 2021 03:00:53 -0700
From: Wanpeng Li <kernellwp@...il.com>
To: linux-kernel@...r.kernel.org, kvm@...r.kernel.org
Cc: Paolo Bonzini <pbonzini@...hat.com>,
Sean Christopherson <seanjc@...gle.com>,
Vitaly Kuznetsov <vkuznets@...hat.com>,
Wanpeng Li <wanpengli@...cent.com>,
Jim Mattson <jmattson@...gle.com>,
Joerg Roedel <joro@...tes.org>
Subject: [PATCH v4] KVM: Avoid expensive "should kick" operation for running vCPU
From: Wanpeng Li <wanpengli@...cent.com>
Avoid the moderately expensive "should kick" operation if this pCPU
is currently running the target vCPU.
Signed-off-by: Wanpeng Li <wanpengli@...cent.com>
---
v3 -> v4:
* check running vCPU in a separate patch
v2 -> v3:
* use kvm_arch_vcpu_get_wait()
v1 -> v2:
* move checking running vCPU logic to kvm_vcpu_kick
* check rcuwait_active(&vcpu->wait) etc
virt/kvm/kvm_main.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 7851f3a1b5f7..4a4684e55ef5 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -3325,11 +3325,22 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
* vCPU also requires it to leave IN_GUEST_MODE.
*/
me = get_cpu();
+
+ /*
+ * avoid the moderately expensive "should kick" operation if this pCPU
+ * is currently running the target vcpu, in which case it's a KVM bug
+ * if the vCPU is in the inner run loop.
+ */
+ if (vcpu == __this_cpu_read(kvm_running_vcpu) &&
+ !WARN_ON_ONCE(vcpu->mode == IN_GUEST_MODE))
+ goto out;
+
if (kvm_arch_vcpu_should_kick(vcpu)) {
cpu = READ_ONCE(vcpu->cpu);
if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
smp_send_reschedule(cpu);
}
+out:
put_cpu();
}
EXPORT_SYMBOL_GPL(kvm_vcpu_kick);
--
2.25.1
Powered by blists - more mailing lists