Add support for monitoring guest TLB operations
Subsystems (5)
v4 cover Add support for monitoring guest TLB operations
Hi,
This series adds support for a software pmu on the host which can be used to monitor VM operations. For arm and arm64, support is added to monitor guest TLB operations using the host pmu infrastructure.
This posting addresses all feedback on the previous version[0]. Instead of requiring a VM pid as part of the event attribute, it now depends on vcpu thread ids being passed via the standard perf interface (-t). Thread ids instead of process ids (-p) is used as -p when called with the thread id ends up resolving to the parent process.
Prior versions can be found at [1][2][3][4].
As a result of the changes, the user interface has changed as well. With the patchset, TLB operations can be monitored using -
# perf stat -e kvm/tlb_invalidate/ -t 3967,3968 ^C Performance counter stats for thread id '3967,3968':
71 kvm/tlb_invalidate/
10.207587028 seconds time elapsedwhere 3967 and 3968 are the thread ids of the vcpus.
Patches based on v4.10-rc6.
Thanks,
Punit
[0] http://www.mail-archive.com/[email protected]/msg09357.html
[1] http://www.mail-archive.com/[email protected]/msg1210715.html
[2] http://www.mail-archive.com/[email protected]/msg1224353.html
[3] https://marc.info/?l=linux-kernel&m=147376184208258&w=2
[4] https://marc.info/?l=kvm&m=147750373716545&w=2
Changes:
v3 -> v4
* Use vcpupid specified via perf instead of custom VM specifier. This results in simplifying the synchronisation requirements for callbacks
* Dropped change to track VM pid as it's no longer being used
* Moved host pmu to virt/kvm
* Added copyright headers
v2 -> v3
* Replaced perf trace monitoring with software PMU
* Re-ordered patches as a result of the above re-write
v1 -> v2
* New (Patch 6) - Add support for trapping and emulating TLB operations to ARM hosts
* Move common code to handle perf trace notifications to virt/kvm/arm
* Move tracepoint to include/trace/events/kvm.h
* Drop patch to introduce __tlbi helper as it is now merged
* Reorder patches
RFC v2 -> v1
* Dropped the RFC tag
* Patch 2 - Use VM thread group id for identification
* Patch 4 - Update comment for clarity
* Patch 6 - Add comment explaining switch to hype-role when VHE is enabled
* Patch 7 - Add comment to clarify struct kvm_trace_hook
RFC -> RFC v2
* Patch 4 - Rename left-over TLBI macro to __TLBI
* Patch 6 - Replace individual TLB operation emulation with invalidating all stage 1 TLB for the VM. TLB monitoring is expected to be a debug feature and performance is not critical.
Mark Rutland (1):
arm64/kvm: hyp: tlb: use __tlbi() helper
Punit Agrawal (8):
KVM: Add event to trace tlb invalidations
arm: KVM: Handle trappable TLB instructions
arm64: KVM: Handle trappable TLB instructions
kvm: Add host pmu to support VM introspection
kvm: host_pmu: Add support for tracking guest TLB operations
arm64: KVM: Enable support for the host pmu
arm: KVM: Enable support for host pmu
arm: KVM: Initialise the host pmu
arch/arm/include/asm/kvm_asm.h | 2 + arch/arm/include/asm/kvm_host.h | 9 ++ arch/arm/kvm/Kconfig | 1 + arch/arm/kvm/Makefile | 1 + arch/arm/kvm/arm.c | 2 + arch/arm/kvm/coproc.c | 56 +++++++++ arch/arm/kvm/hyp/tlb.c | 33 +++++ arch/arm64/include/asm/kvm_asm.h | 2 + arch/arm64/include/asm/kvm_host.h | 9 ++ arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/Makefile | 1 + arch/arm64/kvm/hyp/tlb.c | 87 +++++++++++++- arch/arm64/kvm/sys_regs.c | 83 +++++++++++++ include/kvm/host_pmu.h | 35 ++++++ include/trace/events/kvm.h | 18 +++ virt/kvm/Kconfig | 3 + virt/kvm/arm/host_pmu_events.c | 82 +++++++++++++ virt/kvm/host_pmu.c | 246 ++++++++++++++++++++++++++++++++++++++ 18 files changed, 665 insertions(+), 6 deletions(-) create mode 100644 include/kvm/host_pmu.h create mode 100644 virt/kvm/arm/host_pmu_events.c create mode 100644 virt/kvm/host_pmu.c
Signature · 1 line
2.11.0
PATCH v4 1/9 arm64/kvm: hyp: tlb: use __tlbi() helper
From: Mark Rutland <[email protected]>
Now that we have a __tlbi() helper, make use of this in the arm64 KVM hyp code to get rid of asm() boilerplate. At the same time, we simplify __tlb_flush_vm_context by using __flush_icache_all(), as this has the appropriate instruction cache maintenance and barrier.
--- arch/arm64/kvm/hyp/tlb.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
arch/arm64/kvm/hyp/tlb.c +6 -6
@@ -16,6 +16,7 @@*/ #include <asm/kvm_hyp.h> +#include <asm/tlbflush.h> void __hyp_text __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa) {
@@ -32,7 +33,7 @@ void __hyp_text __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)* whole of Stage-1. Weep... */ ipa >>=12 ; - asmvolatile ("tlbi ipas2e1is, %0" : :"r" (ipa)); +__tlbi (ipas2e1is, ipa);/* * We have to ensure completion of the invalidation at Stage-2,
@@ -41,7 +42,7 @@ void __hyp_text __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)* the Stage-1 invalidation happened first. */ dsb (ish); - asmvolatile ("tlbi vmalle1is" : : ); +__tlbi (vmalle1is);dsb (ish);isb ();
@@ -57,7 +58,7 @@ void __hyp_text __kvm_tlb_flush_vmid(struct kvm *kvm)write_sysreg (kvm->arch.vttbr, vttbr_el2);isb (); - asmvolatile ("tlbi vmalls12e1is" : : ); +__tlbi (vmalls12e1is);dsb (ish);isb ();
@@ -82,7 +83,6 @@ void __hyp_text __kvm_tlb_flush_local_vmid(struct kvm_vcpu *vcpu) void __hyp_text __kvm_flush_vm_context(void) {dsb (ishst); - asmvolatile ("tlbi alle1is \n" -"ic ialluis " : : ); -dsb (ish); +__tlbi (alle1is); +__flush_icache_all ();/* contains a dsb(ish) */ }
Signature · 1 line
2.11.0
PATCH v4 2/9 KVM: Add event to trace tlb invalidations
In reply to Punit AgrawalAs TLB operations can have an impact on system performance, add a trace event to enable monitoring of guest TLB maintenance operations.
--- include/trace/events/kvm.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
include/trace/events/kvm.h +18 -0
@@ -393,6 +393,24 @@ TRACE_EVENT(kvm_halt_poll_ns,#define trace_kvm_halt_poll_ns_shrink (vcpu_id, new, old) \ trace_kvm_halt_poll_ns(false, vcpu_id, new, old) +TRACE_EVENT (kvm_tlb_invalidate, + TP_PROTO(unsigned long vcpu_pc, u32 opcode), +TP_ARGS (vcpu_pc, opcode), + +TP_STRUCT__entry ( + __field(unsigned long, vcpu_pc) +__field (u32, opcode) + ), + +TP_fast_assign ( + __entry->vcpu_pc = vcpu_pc; + __entry->opcode = opcode; + ), + +TP_printk ("vcpu_pc=0x%16lx opcode=%08x" , __entry->vcpu_pc, + __entry->opcode) +); +#endif /* _TRACE_KVM_MAIN_H */ /* This part must be outside protection */
Signature · 1 line
2.11.0
PATCH v4 3/9 arm: KVM: Handle trappable TLB instructions
In reply to Punit AgrawalIt is possible to enable selective trapping of guest TLB maintenance instructions executed in lower privilege levels to HYP mode. This feature can be used to monitor guest TLB operations.
Add support to emulate the TLB instructions when their execution traps to hyp mode. Also keep track of the number of emulated operations.
--- arch/arm/include/asm/kvm_asm.h | 2 ++ arch/arm/include/asm/kvm_host.h | 1 + arch/arm/kvm/coproc.c | 56 +++++++++++++++++++++++++++++++++++++++++ arch/arm/kvm/hyp/tlb.c | 33 ++++++++++++++++++++++++ 4 files changed, 92 insertions(+)
arch/arm/include/asm/kvm_asm.h +2 -0
@@ -67,6 +67,8 @@ extern void __kvm_flush_vm_context(void);extern void __kvm_tlb_flush_vmid_ipa (struct kvm *kvm,phys_addr_t ipa);extern void __kvm_tlb_flush_vmid (struct kvm *kvm);extern void __kvm_tlb_flush_local_vmid (struct kvm_vcpu *vcpu); +extern void __kvm_emulate_tlb_invalidate (struct kvm *kvm,u32 opcode, +u64 regval);extern int __kvm_vcpu_run (struct kvm_vcpu *vcpu);
arch/arm/include/asm/kvm_host.h +1 -0
@@ -205,6 +205,7 @@ struct kvm_vcpu_stat {u64 mmio_exit_user;u64 mmio_exit_kernel;u64 exits; +u64 tlb_invalidate; };#define vcpu_cp15 (v,r) (v)->arch.ctxt.cp15[r]
arch/arm/kvm/coproc.c +56 -0
68 lines not on this page · raw message
arch/arm/kvm/hyp/tlb.c +33 -0
@@ -76,3 +76,36 @@ void __hyp_text __kvm_flush_vm_context(void)write_sysreg (0 ,ICIALLUIS );dsb (ish); } + +static void __hyp_text __switch_to_guest_regime(struct kvm *kvm) +{ +write_sysreg (kvm->arch.vttbr,VTTBR ); +isb (); +} + +static void __hyp_text __switch_to_host_regime(void) +{ +write_sysreg (0 ,VTTBR ); +} + +void __hyp_text +__kvm_emulate_tlb_invalidate (struct kvm *kvm,u32 opcode,u64 regval) +{ + kvm =kern_hyp_va (kvm); + +__switch_to_guest_regime (kvm); + +/* +* TLB maintenance operations are broadcast to +* inner-shareable domain when HCR_FB is set (default for +* KVM). +* +* Nuke all Stage 1 TLB entries for the VM. This will kill +* performance but it's always safe to do as we don't leave +* behind any strays in the TLB +*/ +write_sysreg (0 ,TLBIALLIS ); +isb (); + +__switch_to_host_regime (); +}
Signature · 1 line
2.11.0
PATCH v4 4/9 arm64: KVM: Handle trappable TLB instructions
In reply to Punit AgrawalThe ARMv8 architecture allows trapping of TLB maintenane instructions from EL0/EL1 to higher exception levels. On encountering a trappable TLB instruction in a guest, an exception is taken to EL2.
Add support to handle emulating the TLB instructions. Also track the number of emulated operations.
--- arch/arm64/include/asm/kvm_asm.h | 2 + arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/hyp/tlb.c | 75 +++++++++++++++++++++++++++++++++++ arch/arm64/kvm/sys_regs.c | 83 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+)
arch/arm64/include/asm/kvm_asm.h +2 -0
@@ -55,6 +55,8 @@ extern void __kvm_flush_vm_context(void);extern void __kvm_tlb_flush_vmid_ipa (struct kvm *kvm,phys_addr_t ipa);extern void __kvm_tlb_flush_vmid (struct kvm *kvm);extern void __kvm_tlb_flush_local_vmid (struct kvm_vcpu *vcpu); +extern void __kvm_emulate_tlb_invalidate (struct kvm *kvm,u32 opcode, +u64 regval);extern int __kvm_vcpu_run (struct kvm_vcpu *vcpu);
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -307,6 +307,7 @@ struct kvm_vcpu_stat {u64 mmio_exit_user;u64 mmio_exit_kernel;u64 exits; +u64 tlb_invalidate; };int kvm_vcpu_preferred_target (struct kvm_vcpu_init *init);
arch/arm64/kvm/hyp/tlb.c +75 -0
78 lines not on this page · raw message
arch/arm64/kvm/sys_regs.c +83 -0
101 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH v4 5/9 kvm: Add host pmu to support VM introspection
In reply to Punit AgrawalAdd a software PMU on the host that supports tracking VM operations (in the form of PMU events). To add support for events, users of this functionality will provide the callbacks to configure the VM to enable/disable the events and read a count of the event's frequency.
The host PMU events can be controlled by tools like perf using standard kernel perf interfaces.
--- include/kvm/host_pmu.h | 35 +++++++ virt/kvm/Kconfig | 3 + virt/kvm/host_pmu.c | 246 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 include/kvm/host_pmu.h create mode 100644 virt/kvm/host_pmu.c
include/kvm/host_pmu.h new file +35 -0
@@ -0,0 +1,35 @@ +/* +* Copyright (C) 2017 ARM Ltd. +* Author: Punit Agrawal <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ +#ifndef _KVM_HOST_PMU_H +#define _KVM_HOST_PMU_H + +#include <linux/kvm_host.h> +#include <linux/perf_event.h> + +typedef void (*configure_event_fn )(struct kvm_vcpu *vcpu,bool enable); +typedef u64 (*get_event_count_fn )(struct kvm_vcpu *vcpu); + +struct kvm_event_cb { +get_event_count_fn get_event_count; +configure_event_fn configure_event; +}; + +int kvm_host_pmu_register (int nr_events,struct kvm_event_cb *cbs, +struct attribute **event_attrs); +void kvm_host_pmu_unregister (void ); + +#endif /* _KVM_HOST_PMU_H */
virt/kvm/Kconfig +3 -0
@@ -50,3 +50,6 @@ config KVM_COMPAT config HAVE_KVM_IRQ_BYPASS bool + +config KVM_HOST_PMU + bool
virt/kvm/host_pmu.c new file +246 -0
246 lines not on this page · raw message
Signature · 6 lines
2.11.0 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 6/9 KVM: arm/arm64: Add support for tracking guest TLB operations
In reply to Punit AgrawalThe host pmu supports monitoring of VM operations. Register a host pmu and provide the necessary callbacks to enable monitoring TLB operations.
--- virt/kvm/arm/host_pmu_events.c | 82 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 virt/kvm/arm/host_pmu_events.c
virt/kvm/arm/host_pmu_events.c new file +82 -0
@@ -0,0 +1,82 @@ +/* +* Copyright (C) 2017 ARM Ltd. +* Author: Punit Agrawal <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ +#include <kvm/host_pmu.h> +#include <linux/device.h> +#include <linux/kernel.h> +#include <linux/kvm_host.h> +#include <linux/sysfs.h> +#include <linux/types.h> + +#include <asm/kvm_emulate.h> + +enum host_pmu_events { + tlb_invalidate, +MAX_EVENTS , +}; + +static u64 get_tlb_invalidate_count (struct kvm_vcpu *vcpu) +{ +return vcpu->stat.tlb_invalidate; +} + +static void configure_tlb_invalidate (struct kvm_vcpu *vcpu,bool enable) +{ +unsigned long hcr; + + hcr =vcpu_get_hcr (vcpu); + +if (enable) + hcr |=HCR_TTLB ; +else + hcr &= ~HCR_TTLB ; + +vcpu_set_hcr (vcpu, hcr); +} + +static ssize_t events_sysfs_show (struct device *dev, +struct device_attribute *attr,char *page) +{ +struct perf_pmu_events_attr *pmu_attr; + + pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr); + +return sprintf (page,"event=0x%03llx\n" , pmu_attr->id); +} +PMU_EVENT_ATTR (tlb_invalidate, event_attr_tlb_invalidate, tlb_invalidate, + events_sysfs_show); + +static struct attribute *event_attrs[] = { + &event_attr_tlb_invalidate.attr.attr, +NULL , +}; + +static struct kvm_event_cb event_callbacks[] = { + { + .get_event_count = get_tlb_invalidate_count, + .configure_event = configure_tlb_invalidate, + }, +}; + +int arm_host_pmu_init (void ) +{ +return kvm_host_pmu_register (MAX_EVENTS , event_callbacks, event_attrs); +} + +void arm_host_pmu_teardown (void ) +{ +kvm_host_pmu_unregister (); +}
Signature · 1 line
2.11.0
PATCH v4 7/9 arm64: KVM: Enable support for the host pmu
In reply to Punit AgrawalAdd the Kconfig option and Makefile update the enable the recently added host pmu.
--- arch/arm64/include/asm/kvm_host.h | 8 ++++++++ arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/Makefile | 1 + 3 files changed, 10 insertions(+)
arch/arm64/include/asm/kvm_host.h +8 -0
@@ -349,6 +349,14 @@ int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,int kvm_perf_init (void );int kvm_perf_teardown (void ); +#if !defined(CONFIG_KVM_HOST_PMU ) +static inline int arm_host_pmu_init (void ) {return 0 ; } +static inline void arm_host_pmu_teardown (void ) { } +#else +int arm_host_pmu_init (void ); +void arm_host_pmu_teardown (void ); +#endif +struct kvm_vcpu *kvm_mpidr_to_vcpu (struct kvm *kvm,unsigned long mpidr); static inline void __cpu_init_hyp_mode(phys_addr_t pgd_ptr,
arch/arm64/kvm/Kconfig +1 -0
@@ -31,6 +31,7 @@ config KVM select KVM_VFIO select HAVE_KVM_EVENTFD select HAVE_KVM_IRQFD + select KVM_HOST_PMU if PERF_EVENTS select KVM_ARM_PMU if HW_PERF_EVENTS select HAVE_KVM_MSI select HAVE_KVM_IRQCHIP
arch/arm64/kvm/Makefile +1 -0
@@ -34,3 +34,4 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-its.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/irqchip.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/arch_timer.o kvm-$(CONFIG_KVM_ARM_PMU) += $(KVM)/arm/pmu.o +kvm-$(CONFIG_KVM_HOST_PMU) += $(KVM)/host_pmu.o $(KVM)/arm/host_pmu_events.o
Signature · 1 line
2.11.0
PATCH v4 8/9 arm: KVM: Enable support for host pmu
In reply to Punit AgrawalAdd the Kconfig option and Makefile updates to enable the recently added support for host pmu.
--- arch/arm/include/asm/kvm_host.h | 8 ++++++++ arch/arm/kvm/Kconfig | 1 + arch/arm/kvm/Makefile | 1 + 3 files changed, 10 insertions(+)
arch/arm/include/asm/kvm_host.h +8 -0
@@ -289,6 +289,14 @@ static inline int kvm_arch_dev_ioctl_check_extension(struct kvm *kvm, long ext)int kvm_perf_init (void );int kvm_perf_teardown (void ); +#if !defined(CONFIG_KVM_HOST_PMU ) +static inline int arm_host_pmu_init (void ) {return 0 ; } +static inline void arm_host_pmu_teardown (void ) { } +#else +int arm_host_pmu_init (void ); +void arm_host_pmu_teardown (void ); +#endif +void kvm_mmu_wp_memory_region (struct kvm *kvm,int slot);struct kvm_vcpu *kvm_mpidr_to_vcpu (struct kvm *kvm,unsigned long mpidr);
arch/arm/kvm/Kconfig +1 -0
@@ -30,6 +30,7 @@ config KVM select SRCU select MMU_NOTIFIER select KVM_VFIO + select KVM_HOST_PMU if PERF_EVENTS select HAVE_KVM_EVENTFD select HAVE_KVM_IRQFD select HAVE_KVM_IRQCHIP
arch/arm/kvm/Makefile +1 -0
@@ -35,3 +35,4 @@ obj-y += $(KVM)/arm/vgic/vgic-kvm-device.o obj-y += $(KVM)/arm/vgic/vgic-its.o obj-y += $(KVM)/irqchip.o obj-y += $(KVM)/arm/arch_timer.o +obj-$(CONFIG_KVM_HOST_PMU) += $(KVM)/host_pmu.o $(KVM)/arm/host_pmu_events.o
Signature · 1 line
2.11.0
PATCH v4 9/9 arm/arm64: KVM: Initialise the host pmu
In reply to Punit AgrawalNow that we've added support for handling TLB traps as well as infrastructure for monitoring these operations, all that remains is initialising the host pmu for arm/arm64.
--- arch/arm/kvm/arm.c | 2 ++ 1 file changed, 2 insertions(+)
arch/arm/kvm/arm.c +2 -0
@@ -1266,6 +1266,7 @@ static int init_subsystems(void) goto out;kvm_perf_init (); +arm_host_pmu_init ();kvm_coproc_table_init (); out:
@@ -1456,6 +1457,7 @@ int kvm_arch_init(void *opaque)void kvm_arch_exit (void ) {kvm_perf_teardown (); +arm_host_pmu_teardown (); }static int arm_init (void )
Signature · 1 line
2.11.0