From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934451AbcJZRnI (ORCPT ); Wed, 26 Oct 2016 13:43:08 -0400 Received: from foss.arm.com ([217.140.101.70]:48534 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933819AbcJZRmz (ORCPT ); Wed, 26 Oct 2016 13:42:55 -0400 From: Punit Agrawal To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org Cc: Punit Agrawal , Christoffer Dall , Marc Zyngier , Steven Rostedt , Ingo Molnar , Will Deacon Subject: [PATCH v2 8/8] KVM: arm/arm64: Enable selective trapping of TLB instructions Date: Wed, 26 Oct 2016 18:41:48 +0100 Message-Id: <20161026174148.17172-9-punit.agrawal@arm.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20161026174148.17172-1-punit.agrawal@arm.com> References: <20161026174148.17172-1-punit.agrawal@arm.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The TTLB bit of Hypervisor Control Register controls the trapping of guest TLB maintenance instructions. Taking the trap requires a switch to the hypervisor and is an expensive operation. Enable selective trapping of guest TLB instructions when the associated perf trace event is enabled for a specific virtual machine. Signed-off-by: Punit Agrawal Cc: Christoffer Dall Cc: Marc Zyngier --- virt/kvm/arm/perf_trace.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/virt/kvm/arm/perf_trace.c b/virt/kvm/arm/perf_trace.c index 1cafbc9..649ca55 100644 --- a/virt/kvm/arm/perf_trace.c +++ b/virt/kvm/arm/perf_trace.c @@ -17,6 +17,8 @@ #include #include +#include + typedef int (*perf_trace_callback_fn)(struct kvm *kvm, bool enable); struct kvm_trace_hook { @@ -24,7 +26,37 @@ struct kvm_trace_hook { perf_trace_callback_fn setup_fn; }; +static int tlb_invalidate_trap(struct kvm *kvm, bool enable) +{ + int i; + struct kvm_vcpu *vcpu; + + /* + * Halt the VM to ensure atomic update across all vcpus (this + * avoids racy behaviour against other modifications of + * HCR_EL2 such as kvm_toggle_cache/kvm_set_way_flush). + */ + kvm_arm_halt_guest(kvm); + kvm_for_each_vcpu(i, vcpu, kvm) { + unsigned long hcr = vcpu_get_hcr(vcpu); + + if (enable) + hcr |= HCR_TTLB; + else + hcr &= ~HCR_TTLB; + + vcpu_set_hcr(vcpu, hcr); + } + kvm_arm_resume_guest(kvm); + + return 0; +} + static struct kvm_trace_hook trace_hook[] = { + { + .key = "kvm_tlb_invalidate", + .setup_fn = tlb_invalidate_trap, + }, { }, }; -- 2.9.3