From: Marc Zyngier Subject: [PATCH 1/3] arm64: KVM: Yield CPU when vcpu executes a WFE Date: Fri, 8 Nov 2013 10:07:14 +0000 Message-ID: <1383905236-32741-2-git-send-email-marc.zyngier@arm.com> References: <1383905236-32741-1-git-send-email-marc.zyngier@arm.com> Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Cc: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, Christoffer Dall To: Gleb Natapov , Paolo Bonzini Return-path: Received: from service87.mimecast.com ([91.220.42.44]:52220 "EHLO service87.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753126Ab3KHKH2 (ORCPT ); Fri, 8 Nov 2013 05:07:28 -0500 In-Reply-To: <1383905236-32741-1-git-send-email-marc.zyngier@arm.com> Sender: kvm-owner@vger.kernel.org List-ID: On an (even slightly) oversubscribed system, spinlocks are quickly becoming a bottleneck, as some vcpus are spinning, waiting for a lock to be released, while the vcpu holding the lock may not be running at all. The solution is to trap blocking WFEs and tell KVM that we're now spinning. This ensures that other vpus will get a scheduling boost, allowing the lock to be released more quickly. Also, using CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT slightly improves the performance when the VM is severely overcommited. Acked-by: Christoffer Dall Signed-off-by: Marc Zyngier --- arch/arm64/include/asm/kvm_arm.h | 8 ++++++-- arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/handle_exit.c | 18 +++++++++++++----- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/arch/arm64/include/asm/kvm_arm.h b/arch/arm64/include/asm/kvm_= arm.h index a5f28e2..c98ef47 100644 --- a/arch/arm64/include/asm/kvm_arm.h +++ b/arch/arm64/include/asm/kvm_arm.h @@ -63,6 +63,7 @@ * TAC:=09=09Trap ACTLR * TSC:=09=09Trap SMC * TSW:=09=09Trap cache operations by set/way + * TWE:=09=09Trap WFE * TWI:=09=09Trap WFI * TIDCP:=09Trap L2CTLR/L2ECTLR * BSU_IS:=09Upgrade barriers to the inner shareable domain @@ -72,8 +73,9 @@ * FMO:=09=09Override CPSR.F and enable signaling with VF * SWIO:=09Turn set/way invalidates into set/way clean+invalidate */ -#define HCR_GUEST_FLAGS (HCR_TSC | HCR_TSW | HCR_TWI | HCR_VM | HCR_BSU_IS= | \ -=09=09=09 HCR_FB | HCR_TAC | HCR_AMO | HCR_IMO | HCR_FMO | \ +#define HCR_GUEST_FLAGS (HCR_TSC | HCR_TSW | HCR_TWE | HCR_TWI | HCR_VM | = \ +=09=09=09 HCR_BSU_IS | HCR_FB | HCR_TAC | \ +=09=09=09 HCR_AMO | HCR_IMO | HCR_FMO | \ =09=09=09 HCR_SWIO | HCR_TIDCP | HCR_RW) #define HCR_VIRT_EXCP_MASK (HCR_VA | HCR_VI | HCR_VF) =20 @@ -242,4 +244,6 @@ =20 #define ESR_EL2_EC_xABT_xFSR_EXTABT=090x10 =20 +#define ESR_EL2_EC_WFI_ISS_WFE=09(1 << 0) + #endif /* __ARM64_KVM_ARM_H__ */ diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 21e9082..4480ab3 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -21,6 +21,7 @@ config KVM =09select MMU_NOTIFIER =09select PREEMPT_NOTIFIERS =09select ANON_INODES +=09select HAVE_KVM_CPU_RELAX_INTERCEPT =09select KVM_MMIO =09select KVM_ARM_HOST =09select KVM_ARM_VGIC diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index 9beaca03..8da5606 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -47,21 +47,29 @@ static int handle_smc(struct kvm_vcpu *vcpu, struct kvm= _run *run) } =20 /** - * kvm_handle_wfi - handle a wait-for-interrupts instruction executed by a= guest + * kvm_handle_wfx - handle a wait-for-interrupts or wait-for-event + *=09=09 instruction executed by a guest + * * @vcpu:=09the vcpu pointer * - * Simply call kvm_vcpu_block(), which will halt execution of + * WFE: Yield the CPU and come back to this vcpu when the scheduler + * decides to. + * WFI: Simply call kvm_vcpu_block(), which will halt execution of * world-switches and schedule other host processes until there is an * incoming IRQ or FIQ to the VM. */ -static int kvm_handle_wfi(struct kvm_vcpu *vcpu, struct kvm_run *run) +static int kvm_handle_wfx(struct kvm_vcpu *vcpu, struct kvm_run *run) { -=09kvm_vcpu_block(vcpu); +=09if (kvm_vcpu_get_hsr(vcpu) & ESR_EL2_EC_WFI_ISS_WFE) +=09=09kvm_vcpu_on_spin(vcpu); +=09else +=09=09kvm_vcpu_block(vcpu); + =09return 1; } =20 static exit_handle_fn arm_exit_handlers[] =3D { -=09[ESR_EL2_EC_WFI]=09=3D kvm_handle_wfi, +=09[ESR_EL2_EC_WFI]=09=3D kvm_handle_wfx, =09[ESR_EL2_EC_CP15_32]=09=3D kvm_handle_cp15_32, =09[ESR_EL2_EC_CP15_64]=09=3D kvm_handle_cp15_64, =09[ESR_EL2_EC_CP14_MR]=09=3D kvm_handle_cp14_access, --=20 1.8.2.3