Skip to contentSkip to conversation

Add support for monitoring guest TLB operations

RFC PATCH Superseded Punit Agrawal 3 participants 28 messages Sep 5, 2016 – Sep 6, 2016

Subsystems (5)
Arm (32-bit) KVM arm64 Tracing +1 more subsystems

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add support for monitoring guest TLB operations

Hi,

Although there are no PMU events to monitor TLB operations, ARMv8 supports trapping guest TLB maintenance operations to the hypervisor. This trapping mechanism can be used to monitor the use of guest TLB instructions.

As taking a trap for every TLB operation can have significant overhead, trapping should only be enabled -

* on user request
* for the VM of interest

This patchset adds support to listen to perf trace event state change notifications. The notifications and associated context are then used to enable trapping of guest TLB operations when requested by the user. The trap handling generates trace events (kvm_tlb_invalidate) which can already be counted using existing perf trace functionality.

I would appreciate feedback on the approach to tie the control of TLB monitoring with perf trace events (Patch 1) especially if there are any suggestions on avoiding (or reducing) the overhead of "perf trace" notifications.

I looked at using regfunc/unregfunc tracepoint hooks but they don't include the event context. But the bigger problem was that the callbacks are only called on the first instance of simultaneously executing perf stat invocations.

The patchset is based on v4.8-rc5 and adds support for monitoring guest TLB operations on 64bit hosts. If the approach taken in the patches is acceptable, I'll add 32bit host support as well.

This is the second posting of this patchset. Earlier discussion can be found at [0].

Changes:

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.

With this patchset, 'perf' tool when attached to a VM process can be used to monitor the TLB operations. E.g., to monitor a VM with process id 4166 -

# perf stat -e "kvm:kvm_tlb_invalidate" -p 4166

Perform some operations in VM (running 'make -j 7' on the kernel sources in this instance). Breaking out of perf shows -

Performance counter stats for process id '4166':

         7,471,974      kvm:kvm_tlb_invalidate

     374.235405282 seconds time elapsed

All feedback welcome.

Thanks,
Punit

[0] http://www.mail-archive.com/[email protected]/msg1210715.html

Mark Rutland (2):
arm64: tlbflush.h: add __tlbi() macro
arm64/kvm: hyp: tlb: use __tlbi() helper

Punit Agrawal (5):
perf/trace: Add notification for perf trace events
KVM: Track the pid of the VM process
KVM: arm/arm64: Register perf trace event notifier
arm64: KVM: Handle trappable TLB instructions
arm64: KVM: Enable selective trapping of TLB instructions

 arch/arm/include/asm/kvm_host.h   |   3 +
 arch/arm/kvm/arm.c                |   2 +
 arch/arm64/include/asm/kvm_asm.h  |   1 +
 arch/arm64/include/asm/kvm_host.h |   8 ++
 arch/arm64/include/asm/tlbflush.h |  31 ++++++--
 arch/arm64/kvm/Kconfig            |   4 +
 arch/arm64/kvm/Makefile           |   1 +
 arch/arm64/kvm/hyp/tlb.c          |  81 ++++++++++++++++++--
 arch/arm64/kvm/perf_trace.c       | 154 ++++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c         |  81 ++++++++++++++++++++
 arch/arm64/kvm/trace.h            |  16 ++++
 include/linux/kvm_host.h          |   1 +
 include/linux/trace_events.h      |   3 +
 kernel/trace/trace_event_perf.c   |  24 ++++++
 virt/kvm/kvm_main.c               |   2 +
 15 files changed, 398 insertions(+), 14 deletions(-)
 create mode 100644 arch/arm64/kvm/perf_trace.c
Signature · 1 line
2.8.1
#1 lore raw

RFC PATCH v2 1/7 perf/trace: Add notification for perf trace events

Add a mechanism to notify listeners about perf trace event state changes. This enables listeners to take actions requiring the event context (e.g., attached process).

The notification mechanism can be used to reduce trace point based profiling overhead by enabling/disabling hardware traps for specific contexts (e.g., virtual machines).

Cc Steven Rostedt <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 include/linux/trace_events.h    |  3 +++
 kernel/trace/trace_event_perf.c | 24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
include/linux/trace_events.h +3 -0
index be00761..5924032 100644
@@ -505,6 +505,9 @@ perf_trace_buf_submit(void *raw_data, int size, int rctx, u16 type,
 {
 	perf_tp_event(type, count, raw_data, size, regs, head, rctx, task);
 }
+
+extern int perf_trace_notifier_register(struct notifier_block *nb);
+extern int perf_trace_notifier_unregister(struct notifier_block *nb);
 #endif
 
 #endif /* _LINUX_TRACE_EVENT_H */
kernel/trace/trace_event_perf.c +24 -0
index 562fa69..9aaaacf 100644
@@ -6,10 +6,12 @@
  */
 
 #include <linux/module.h>
+#include <linux/notifier.h>
 #include <linux/kprobes.h>
 #include "trace.h"
 
 static char __percpu *perf_trace_buf[PERF_NR_CONTEXTS];
+static RAW_NOTIFIER_HEAD(perf_trace_notifier_list);
 
 /*
  * Force it to be aligned to unsigned long to avoid misaligned accesses
@@ -86,6 +88,26 @@ static int perf_trace_event_perm(struct trace_event_call *tp_event,
 	return 0;
 }
 
+int perf_trace_notifier_register(struct notifier_block *nb)
+{
+	return raw_notifier_chain_register(&perf_trace_notifier_list, nb);
+}
+
+int perf_trace_notifier_unregister(struct notifier_block *nb)
+{
+	return raw_notifier_chain_unregister(&perf_trace_notifier_list, nb);
+}
+
+static void perf_trace_notify(enum trace_reg event, struct perf_event *p_event)
+{
+	/*
+	 * We use raw notifiers here as we are called with the
+	 * event_mutex held.
+	 */
+	raw_notifier_call_chain(&perf_trace_notifier_list,
+				     event, p_event);
+}
+
 static int perf_trace_event_reg(struct trace_event_call *tp_event,
 				struct perf_event *p_event)
 {
@@ -176,6 +198,7 @@ out:
 static int perf_trace_event_open(struct perf_event *p_event)
 {
 	struct trace_event_call *tp_event = p_event->tp_event;
+	perf_trace_notify(TRACE_REG_PERF_OPEN, p_event);
 	return tp_event->class->reg(tp_event, TRACE_REG_PERF_OPEN, p_event);
 }
 
@@ -183,6 +206,7 @@ static void perf_trace_event_close(struct perf_event *p_event)
 {
 	struct trace_event_call *tp_event = p_event->tp_event;
 	tp_event->class->reg(tp_event, TRACE_REG_PERF_CLOSE, p_event);
+	perf_trace_notify(TRACE_REG_PERF_CLOSE, p_event);
 }
 
 static int perf_trace_event_init(struct trace_event_call *tp_event,
Signature · 1 line
2.8.1
#2 lore raw

RFC PATCH v2 2/7 KVM: Track the pid of the VM process

In reply to Punit Agrawal

Userspace tools such as perf can be used to profile individual processes.

Track the PID of the virtual machine process to match profiling requests targeted at it. This can be used to take appropriate action to enable the requested profiling operations for the VMs of interest.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 include/linux/kvm_host.h | 1 +
 virt/kvm/kvm_main.c      | 2 ++
 2 files changed, 3 insertions(+)
include/linux/kvm_host.h +1 -0
index 9c28b4d..7c42c94 100644
@@ -374,6 +374,7 @@ struct kvm_memslots {
 struct kvm {
 	spinlock_t mmu_lock;
 	struct mutex slots_lock;
+	struct pid *pid;
 	struct mm_struct *mm; /* userspace tied to this vm */
 	struct kvm_memslots *memslots[KVM_ADDRESS_SPACE_NUM];
 	struct srcu_struct srcu;
virt/kvm/kvm_main.c +2 -0
index 1950782..ab2535a 100644
@@ -613,6 +613,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
 	spin_lock_init(&kvm->mmu_lock);
 	atomic_inc(&current->mm->mm_count);
 	kvm->mm = current->mm;
+	kvm->pid = get_task_pid(current, PIDTYPE_PID);
 	kvm_eventfd_init(kvm);
 	mutex_init(&kvm->lock);
 	mutex_init(&kvm->irq_lock);
@@ -712,6 +713,7 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	int i;
 	struct mm_struct *mm = kvm->mm;
 
+	put_pid(kvm->pid);
 	kvm_destroy_vm_debugfs(kvm);
 	kvm_arch_sync_events(kvm);
 	spin_lock(&kvm_lock);
Signature · 6 lines
2.8.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#11 lore raw

RFC PATCH v2 3/7 KVM: arm/arm64: Register perf trace event notifier

In reply to Punit Agrawal

Register a notifier to track state changes of perf trace events.

The notifier will enable taking appropriate action for trace events targeting VM.

Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm/include/asm/kvm_host.h   |   3 +
 arch/arm/kvm/arm.c                |   2 +
 arch/arm64/include/asm/kvm_host.h |   8 +++
 arch/arm64/kvm/Kconfig            |   4 ++
 arch/arm64/kvm/Makefile           |   1 +
 arch/arm64/kvm/perf_trace.c       | 122 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 140 insertions(+)
 create mode 100644 arch/arm64/kvm/perf_trace.c
arch/arm/include/asm/kvm_host.h +3 -0
index de338d9..609998e 100644
@@ -280,6 +280,9 @@ static inline int kvm_arch_dev_ioctl_check_extension(struct kvm *kvm, long ext)
 int kvm_perf_init(void);
 int kvm_perf_teardown(void);
 
+static inline int kvm_perf_trace_init(void) { return 0; }
+static inline int kvm_perf_trace_teardown(void) { return 0; }
+
 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/arm.c +2 -0
index 75f130e..e1b99c4 100644
@@ -1220,6 +1220,7 @@ static int init_subsystems(void)
 		goto out;
 
 	kvm_perf_init();
+	kvm_perf_trace_init();
 	kvm_coproc_table_init();
 
 out:
@@ -1411,6 +1412,7 @@ out_err:
 void kvm_arch_exit(void)
 {
 	kvm_perf_teardown();
+	kvm_perf_trace_teardown();
 }
 
 static int arm_init(void)
arch/arm64/include/asm/kvm_host.h +8 -0
index 3eda975..f6ff8e5 100644
@@ -345,6 +345,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_PERF_TRACE)
+static inline int kvm_perf_trace_init(void) { return 0; }
+static inline int kvm_perf_trace_teardown(void) { return 0; }
+#else
+int kvm_perf_trace_init(void);
+int kvm_perf_trace_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 +4 -0
index 9c9edc9..56e9537 100644
@@ -19,6 +19,9 @@ if VIRTUALIZATION
 config KVM_ARM_VGIC_V3
 	bool
 
+config KVM_PERF_TRACE
+        bool
+
 config KVM
 	bool "Kernel-based Virtual Machine (KVM) support"
 	depends on OF
@@ -39,6 +42,7 @@ config KVM
 	select HAVE_KVM_MSI
 	select HAVE_KVM_IRQCHIP
 	select HAVE_KVM_IRQ_ROUTING
+	select KVM_PERF_TRACE if EVENT_TRACING && PERF_EVENTS
 	---help---
 	  Support hosting virtualized guest machines.
 	  We don't support KVM with 16K page tables yet, due to the multiple
arch/arm64/kvm/Makefile +1 -0
index 695eb3c..7d175e4 100644
@@ -19,6 +19,7 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(ARM)/psci.o $(ARM)/perf.o
 kvm-$(CONFIG_KVM_ARM_HOST) += emulate.o inject_fault.o regmap.o
 kvm-$(CONFIG_KVM_ARM_HOST) += hyp.o hyp-init.o handle_exit.o
 kvm-$(CONFIG_KVM_ARM_HOST) += guest.o debug.o reset.o sys_regs.o sys_regs_generic_v8.o
+kvm-$(CONFIG_KVM_PERF_TRACE) += perf_trace.o
 
 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic.o
 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-init.o
arch/arm64/kvm/perf_trace.c new file +122 -0

122 lines not on this page · raw message

Signature · 1 line
2.8.1
#14 lore raw

RFC PATCH v2 4/7 arm64: tlbflush.h: add __tlbi() macro

In reply to Punit Agrawal

From: Mark Rutland <[email protected]>

As with dsb() and isb(), add a __tlbi() helper so that we can avoid distracting asm boilerplate every time we want a TLBI. As some TLBI operations take an argument while others do not, some pre-processor is used to handle these two cases with different assembly blocks.

The existing tlbflush.h code is moved over to use the helper.

Cc Catalin Marinas <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Will Deacon <[email protected]>
[ rename helper to __tlbi, update commit log ]
---
 arch/arm64/include/asm/tlbflush.h | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
arch/arm64/include/asm/tlbflush.h +23 -8
index b460ae2..e9e42a7 100644
@@ -25,6 +25,21 @@
 #include <asm/cputype.h>
 
 /*
+ * Raw TLBI operations. Drivers and most kernel code should use the TLB
+ * management routines below in preference to these. Where necessary, these can
+ * be used to avoid asm() boilerplate.
+ *
+ * Can be used as __tlbi(op) or __tlbi(op, arg), depending on whether a
+ * particular TLBI op takes an argument or not. The macros below handle invoking
+ * the asm with or without the register argument as appropriate.
+ */
+#define __TLBI_0(op, arg)		asm ("tlbi " #op)
+#define __TLBI_1(op, arg)		asm ("tlbi " #op ", %0" : : "r" (arg))
+#define __TLBI_N(op, arg, n, ...)	__TLBI_##n(op, arg)
+
+#define __tlbi(op, ...)		__TLBI_N(op, ##__VA_ARGS__, 1, 0)
+
+/*
  *	TLB Management
  *	==============
  *
@@ -66,7 +81,7 @@
 static inline void local_flush_tlb_all(void)
 {
 	dsb(nshst);
-	asm("tlbi	vmalle1");
+	__tlbi(vmalle1);
 	dsb(nsh);
 	isb();
 }
@@ -74,7 +89,7 @@ static inline void local_flush_tlb_all(void)
 static inline void flush_tlb_all(void)
 {
 	dsb(ishst);
-	asm("tlbi	vmalle1is");
+	__tlbi(vmalle1is);
 	dsb(ish);
 	isb();
 }
@@ -84,7 +99,7 @@ static inline void flush_tlb_mm(struct mm_struct *mm)
 	unsigned long asid = ASID(mm) << 48;
 
 	dsb(ishst);
-	asm("tlbi	aside1is, %0" : : "r" (asid));
+	__tlbi(aside1is, asid);
 	dsb(ish);
 }
 
@@ -94,7 +109,7 @@ static inline void flush_tlb_page(struct vm_area_struct *vma,
 	unsigned long addr = uaddr >> 12 | (ASID(vma->vm_mm) << 48);
 
 	dsb(ishst);
-	asm("tlbi	vale1is, %0" : : "r" (addr));
+	__tlbi(vale1is, addr);
 	dsb(ish);
 }
 
@@ -122,9 +137,9 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma,
 	dsb(ishst);
 	for (addr = start; addr < end; addr += 1 << (PAGE_SHIFT - 12)) {
 		if (last_level)
-			asm("tlbi vale1is, %0" : : "r"(addr));
+			__tlbi(vale1is, addr);
 		else
-			asm("tlbi vae1is, %0" : : "r"(addr));
+			__tlbi(vae1is, addr);
 	}
 	dsb(ish);
 }
@@ -149,7 +164,7 @@ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end
 
 	dsb(ishst);
 	for (addr = start; addr < end; addr += 1 << (PAGE_SHIFT - 12))
-		asm("tlbi vaae1is, %0" : : "r"(addr));
+		__tlbi(vaae1is, addr);
 	dsb(ish);
 	isb();
 }
@@ -163,7 +178,7 @@ static inline void __flush_tlb_pgtable(struct mm_struct *mm,
 {
 	unsigned long addr = uaddr >> 12 | (ASID(mm) << 48);
 
-	asm("tlbi	vae1is, %0" : : "r" (addr));
+	__tlbi(vae1is, addr);
 	dsb(ish);
 }
 
Signature · 1 line
2.8.1
#19 lore raw

RFC PATCH v2 5/7 arm64/kvm: hyp: tlb: use __tlbi() helper

In reply to Punit Agrawal

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.

Cc Marc Zyngier <[email protected]>
[ rename tlbi -> __tlbi, convert additional sites, update commit log ]
---
 arch/arm64/kvm/hyp/tlb.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
arch/arm64/kvm/hyp/tlb.c +6 -6
index be8177c..4cda100 100644
@@ -16,6 +16,7 @@
  */
 
 #include <asm/kvm_hyp.h>
+#include <asm/tlbflush.h>
 
 static void __hyp_text __tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
 {
@@ -32,7 +33,7 @@ static void __hyp_text __tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
 	 * whole of Stage-1. Weep...
 	 */
 	ipa >>= 12;
-	asm volatile("tlbi ipas2e1is, %0" : : "r" (ipa));
+	__tlbi(ipas2e1is, ipa);
 
 	/*
 	 * We have to ensure completion of the invalidation at Stage-2,
@@ -41,7 +42,7 @@ static void __hyp_text __tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
 	 * the Stage-1 invalidation happened first.
 	 */
 	dsb(ish);
-	asm volatile("tlbi vmalle1is" : : );
+	__tlbi(vmalle1is);
 	dsb(ish);
 	isb();
 
@@ -60,7 +61,7 @@ static void __hyp_text __tlb_flush_vmid(struct kvm *kvm)
 	write_sysreg(kvm->arch.vttbr, vttbr_el2);
 	isb();
 
-	asm volatile("tlbi vmalls12e1is" : : );
+	__tlbi(vmalls12e1is);
 	dsb(ish);
 	isb();
 
@@ -72,9 +73,8 @@ __alias(__tlb_flush_vmid) void __kvm_tlb_flush_vmid(struct kvm *kvm);
 static void __hyp_text __tlb_flush_vm_context(void)
 {
 	dsb(ishst);
-	asm volatile("tlbi alle1is	\n"
-		     "ic ialluis	  ": : );
-	dsb(ish);
+	__tlbi(alle1is);
+	__flush_icache_all(); /* contains a dsb(ish) */
 }
 
 __alias(__tlb_flush_vm_context) void __kvm_flush_vm_context(void);
Signature · 1 line
2.8.1
#21 lore raw

RFC PATCH v2 6/7 arm64: KVM: Handle trappable TLB instructions

In reply to Punit Agrawal

The 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 functionality to handle emulating the TLB instructions.

Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm64/include/asm/kvm_asm.h |  1 +
 arch/arm64/kvm/hyp/tlb.c         | 69 ++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c        | 81 ++++++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/trace.h           | 16 ++++++++
 4 files changed, 167 insertions(+)
arch/arm64/include/asm/kvm_asm.h +1 -0
index 7561f63..1ac1cc3 100644
@@ -49,6 +49,7 @@ extern char __kvm_hyp_vector[];
 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_emulate_tlb_invalidate(struct kvm *kvm, u32 sysreg, u64 regval);
 
 extern int __kvm_vcpu_run(struct kvm_vcpu *vcpu);
 
arch/arm64/kvm/hyp/tlb.c +69 -0

72 lines not on this page · raw message

arch/arm64/kvm/sys_regs.c +81 -0

99 lines not on this page · raw message

arch/arm64/kvm/trace.h +16 -0
index 7fb0008..c4d577f 100644
@@ -166,6 +166,22 @@ TRACE_EVENT(kvm_set_guest_debug,
 	TP_printk("vcpu: %p, flags: 0x%08x", __entry->vcpu, __entry->guest_debug)
 );
 
+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_ARM64_KVM_H */
 
Signature · 1 line
2.8.1
#25 lore raw

RFC PATCH v2 7/7 arm64: KVM: Enable selective trapping of TLB instructions

In reply to Punit Agrawal

The TTLB bit of Hypervisor Control Register (HCR_EL2) 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.

Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm64/kvm/perf_trace.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
arch/arm64/kvm/perf_trace.c +32 -0
index 8bacd18..f26da1d 100644
@@ -17,6 +17,8 @@
 #include <linux/kvm_host.h>
 #include <linux/trace_events.h>
 
+#include <asm/kvm_emulate.h>
+
 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,
+	},
 	{ },
 };
 
Signature · 1 line
2.8.1
#3 lore raw

Re: [RFC v2 PATCH 2/7] KVM: Track the pid of the VM process

In reply to Punit Agrawal
Quoted 34 lines · Punit Agrawal

Userspace tools such as perf can be used to profile individual processes.

Track the PID of the virtual machine process to match profiling requests targeted at it. This can be used to take appropriate action to enable the requested profiling operations for the VMs of interest.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 include/linux/kvm_host.h | 1 +
 virt/kvm/kvm_main.c      | 2 ++
 2 files changed, 3 insertions(+)
include/linux/kvm_host.h +1 -0
index 9c28b4d..7c42c94 100644
@@ -374,6 +374,7 @@ struct kvm_memslots {
 struct kvm {
 	spinlock_t mmu_lock;
 	struct mutex slots_lock;
+	struct pid *pid;
 	struct mm_struct *mm; /* userspace tied to this vm */
 	struct kvm_memslots *memslots[KVM_ADDRESS_SPACE_NUM];
 	struct srcu_struct srcu;
virt/kvm/kvm_main.c +0 -0
index 1950782..ab2535a 100644

virt/kvm/kvm_main.c static struct kvm *kvm_create_vm(unsigned long type) · line 616

Quoted 4 lines
virt/kvm/kvm_main.c +1 -0
@@ -613,6 +613,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
 	spin_lock_init(&kvm->mmu_lock);
 	atomic_inc(&current->mm->mm_count);
 	kvm->mm = current->mm;
+	kvm->pid = get_task_pid(current, PIDTYPE_PID);

How dooes this deal with threading? Is the idea that the user by specifying the main thread's pid will enable trapping for all vcpu threads belonging to that VM?

Quoted 14 lines
virt/kvm/kvm_main.c +1 -0
 	kvm_eventfd_init(kvm);
 	mutex_init(&kvm->lock);
 	mutex_init(&kvm->irq_lock);
@@ -712,6 +713,7 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	int i;
 	struct mm_struct *mm = kvm->mm;
 
+	put_pid(kvm->pid);
 	kvm_destroy_vm_debugfs(kvm);
 	kvm_arch_sync_events(kvm);
 	spin_lock(&kvm_lock);
Signature · 1 line
2.8.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#12 lore raw

Re: [RFC v2 PATCH 3/7] KVM: arm/arm64: Register perf trace event notifier

In reply to Punit Agrawal
Quoted 8 lines · Punit Agrawal

Register a notifier to track state changes of perf trace events.

The notifier will enable taking appropriate action for trace events targeting VM.

Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>

Overall this looks reasonable, but I'm wondering if most if this logic should really go in virt/kvm/perf_trace.c and call into arch-specific hooks, similar to the way it works for preempt notifiers.

On the other hand, if arm/arm64 are the only two architectures that are going to use this, creating stubs for the other architectures could be a bit tedious.

Thanks,
-Christoffer

Quoted 228 lines
---
 arch/arm/include/asm/kvm_host.h   |   3 +
 arch/arm/kvm/arm.c                |   2 +
 arch/arm64/include/asm/kvm_host.h |   8 +++
 arch/arm64/kvm/Kconfig            |   4 ++
 arch/arm64/kvm/Makefile           |   1 +
 arch/arm64/kvm/perf_trace.c       | 122 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 140 insertions(+)
 create mode 100644 arch/arm64/kvm/perf_trace.c
arch/arm/include/asm/kvm_host.h +3 -0
index de338d9..609998e 100644
@@ -280,6 +280,9 @@ static inline int kvm_arch_dev_ioctl_check_extension(struct kvm *kvm, long ext)
 int kvm_perf_init(void);
 int kvm_perf_teardown(void);
 
+static inline int kvm_perf_trace_init(void) { return 0; }
+static inline int kvm_perf_trace_teardown(void) { return 0; }
+
 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/arm.c +2 -0
index 75f130e..e1b99c4 100644
@@ -1220,6 +1220,7 @@ static int init_subsystems(void)
 		goto out;
 
 	kvm_perf_init();
+	kvm_perf_trace_init();
 	kvm_coproc_table_init();
 
 out:
@@ -1411,6 +1412,7 @@ out_err:
 void kvm_arch_exit(void)
 {
 	kvm_perf_teardown();
+	kvm_perf_trace_teardown();
 }
 
 static int arm_init(void)
arch/arm64/include/asm/kvm_host.h +8 -0
index 3eda975..f6ff8e5 100644
@@ -345,6 +345,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_PERF_TRACE)
+static inline int kvm_perf_trace_init(void) { return 0; }
+static inline int kvm_perf_trace_teardown(void) { return 0; }
+#else
+int kvm_perf_trace_init(void);
+int kvm_perf_trace_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 +4 -0
index 9c9edc9..56e9537 100644
@@ -19,6 +19,9 @@ if VIRTUALIZATION
 config KVM_ARM_VGIC_V3
 	bool
 
+config KVM_PERF_TRACE
+        bool
+
 config KVM
 	bool "Kernel-based Virtual Machine (KVM) support"
 	depends on OF
@@ -39,6 +42,7 @@ config KVM
 	select HAVE_KVM_MSI
 	select HAVE_KVM_IRQCHIP
 	select HAVE_KVM_IRQ_ROUTING
+	select KVM_PERF_TRACE if EVENT_TRACING && PERF_EVENTS
 	---help---
 	  Support hosting virtualized guest machines.
 	  We don't support KVM with 16K page tables yet, due to the multiple
arch/arm64/kvm/Makefile +1 -0
index 695eb3c..7d175e4 100644
@@ -19,6 +19,7 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(ARM)/psci.o $(ARM)/perf.o
 kvm-$(CONFIG_KVM_ARM_HOST) += emulate.o inject_fault.o regmap.o
 kvm-$(CONFIG_KVM_ARM_HOST) += hyp.o hyp-init.o handle_exit.o
 kvm-$(CONFIG_KVM_ARM_HOST) += guest.o debug.o reset.o sys_regs.o sys_regs_generic_v8.o
+kvm-$(CONFIG_KVM_PERF_TRACE) += perf_trace.o
 
 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic.o
 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-init.o
arch/arm64/kvm/perf_trace.c new file +122 -0
new file mode 100644
index 0000000..8bacd18
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2016 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 <linux/kvm_host.h>
+#include <linux/trace_events.h>
+
+typedef int (*perf_trace_callback_fn)(struct kvm *kvm, bool enable);
+
+struct kvm_trace_hook {
+	char *key;
+	perf_trace_callback_fn setup_fn;
+};
+
+static struct kvm_trace_hook trace_hook[] = {
+	{ },
+};
+
+static perf_trace_callback_fn find_trace_callback(const char *trace_key)
+{
+	int i;
+
+	for (i = 0; trace_hook[i].key; i++)
+		if (!strcmp(trace_key, trace_hook[i].key))
+			return trace_hook[i].setup_fn;
+
+	return NULL;
+}
+
+static int kvm_perf_trace_notifier(struct notifier_block *nb,
+				   unsigned long event, void *data)
+{
+	struct perf_event *p_event = data;
+	struct trace_event_call *tp_event = p_event->tp_event;
+	perf_trace_callback_fn setup_trace_fn;
+	struct kvm *kvm = NULL;
+	struct pid *pid;
+	bool found = false;
+
+	/*
+	 * Is this a trace point?
+	 */
+	if (!(tp_event->flags & TRACE_EVENT_FL_TRACEPOINT))
+		goto out;
+
+	/*
+	 * We'll get here for events we care to monitor for KVM. As we
+	 * only care about events attached to a VM, check that there
+	 * is a task associated with the perf event.
+	 */
+	if (p_event->attach_state != PERF_ATTACH_TASK)
+		goto out;
+
+	/*
+	 * This notifier gets called when perf trace event instance is
+	 * added or removed. Until we can restrict this to events of
+	 * interest in core, minimise the overhead below.
+	 *
+	 * Do we care about it? i.e., is there a callback for this
+	 * trace point?
+	 */
+	setup_trace_fn = find_trace_callback(tp_event->tp->name);
+	if (!setup_trace_fn)
+		goto out;
+
+	pid = get_task_pid(p_event->hw.target, PIDTYPE_PID);
+
+	/*
+	 * Does it match any of the VMs?
+	 */
+	spin_lock(&kvm_lock);
+	list_for_each_entry(kvm, &vm_list, vm_list) {
+		if (kvm->pid == pid) {
+			found = true;
+			break;
+		}
+	}
+	spin_unlock(&kvm_lock);
+
+	put_pid(pid);
+	if (!found)
+		goto out;
+
+	switch (event) {
+	case TRACE_REG_PERF_OPEN:
+		setup_trace_fn(kvm, true);
+		break;
+
+	case TRACE_REG_PERF_CLOSE:
+		setup_trace_fn(kvm, false);
+		break;
+	}
+
+out:
+	return 0;
+}
+
+static struct notifier_block kvm_perf_trace_notifier_block = {
+	.notifier_call = kvm_perf_trace_notifier,
+};
+
+int kvm_perf_trace_init(void)
+{
+	return perf_trace_notifier_register(&kvm_perf_trace_notifier_block);
+}
+
+int kvm_perf_trace_teardown(void)
+{
+	return perf_trace_notifier_unregister(&kvm_perf_trace_notifier_block);
+}
Signature · 1 line
2.8.1
#15 lore raw

Re: [RFC v2 PATCH 4/7] arm64: tlbflush.h: add __tlbi() macro

In reply to Punit Agrawal
Quoted 24 lines · Punit Agrawal

From: Mark Rutland <[email protected]>

As with dsb() and isb(), add a __tlbi() helper so that we can avoid distracting asm boilerplate every time we want a TLBI. As some TLBI operations take an argument while others do not, some pre-processor is used to handle these two cases with different assembly blocks.

The existing tlbflush.h code is moved over to use the helper.

Cc Catalin Marinas <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Will Deacon <[email protected]>
[ rename helper to __tlbi, update commit log ]
---
 arch/arm64/include/asm/tlbflush.h | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
arch/arm64/include/asm/tlbflush.h +0 -0
index b460ae2..e9e42a7 100644

arch/arm64/include/asm/tlbflush.h line 29

Quoted 5 lines
arch/arm64/include/asm/tlbflush.h +2 -0
@@ -25,6 +25,21 @@
 #include <asm/cputype.h>
 
 /*
+ * Raw TLBI operations. Drivers and most kernel code should use the TLB
+ * management routines below in preference to these. Where necessary, these can

in preference to these?

arch/arm64/include/asm/tlbflush.h line 30

Quoted 1 line
arch/arm64/include/asm/tlbflush.h +1 -0
+ * be used to avoid asm() boilerplate.

'these' seem to be a bit overloaded in this commentary.

Quoted 84 lines
arch/arm64/include/asm/tlbflush.h +20 -8
+ *
+ * Can be used as __tlbi(op) or __tlbi(op, arg), depending on whether a
+ * particular TLBI op takes an argument or not. The macros below handle invoking
+ * the asm with or without the register argument as appropriate.
+ */
+#define __TLBI_0(op, arg)		asm ("tlbi " #op)
+#define __TLBI_1(op, arg)		asm ("tlbi " #op ", %0" : : "r" (arg))
+#define __TLBI_N(op, arg, n, ...)	__TLBI_##n(op, arg)
+
+#define __tlbi(op, ...)		__TLBI_N(op, ##__VA_ARGS__, 1, 0)
+
+/*
  *	TLB Management
  *	==============
  *
@@ -66,7 +81,7 @@
 static inline void local_flush_tlb_all(void)
 {
 	dsb(nshst);
-	asm("tlbi	vmalle1");
+	__tlbi(vmalle1);
 	dsb(nsh);
 	isb();
 }
@@ -74,7 +89,7 @@ static inline void local_flush_tlb_all(void)
 static inline void flush_tlb_all(void)
 {
 	dsb(ishst);
-	asm("tlbi	vmalle1is");
+	__tlbi(vmalle1is);
 	dsb(ish);
 	isb();
 }
@@ -84,7 +99,7 @@ static inline void flush_tlb_mm(struct mm_struct *mm)
 	unsigned long asid = ASID(mm) << 48;
 
 	dsb(ishst);
-	asm("tlbi	aside1is, %0" : : "r" (asid));
+	__tlbi(aside1is, asid);
 	dsb(ish);
 }
 
@@ -94,7 +109,7 @@ static inline void flush_tlb_page(struct vm_area_struct *vma,
 	unsigned long addr = uaddr >> 12 | (ASID(vma->vm_mm) << 48);
 
 	dsb(ishst);
-	asm("tlbi	vale1is, %0" : : "r" (addr));
+	__tlbi(vale1is, addr);
 	dsb(ish);
 }
 
@@ -122,9 +137,9 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma,
 	dsb(ishst);
 	for (addr = start; addr < end; addr += 1 << (PAGE_SHIFT - 12)) {
 		if (last_level)
-			asm("tlbi vale1is, %0" : : "r"(addr));
+			__tlbi(vale1is, addr);
 		else
-			asm("tlbi vae1is, %0" : : "r"(addr));
+			__tlbi(vae1is, addr);
 	}
 	dsb(ish);
 }
@@ -149,7 +164,7 @@ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end
 
 	dsb(ishst);
 	for (addr = start; addr < end; addr += 1 << (PAGE_SHIFT - 12))
-		asm("tlbi vaae1is, %0" : : "r"(addr));
+		__tlbi(vaae1is, addr);
 	dsb(ish);
 	isb();
 }
@@ -163,7 +178,7 @@ static inline void __flush_tlb_pgtable(struct mm_struct *mm,
 {
 	unsigned long addr = uaddr >> 12 | (ASID(mm) << 48);
 
-	asm("tlbi	vae1is, %0" : : "r" (addr));
+	__tlbi(vae1is, addr);
 	dsb(ish);
 }
 
Signature · 1 line
2.8.1
Acked-by on v2 5/7
#20 lore raw

Re: [RFC v2 PATCH 5/7] arm64/kvm: hyp: tlb: use __tlbi() helper

In reply to Punit Agrawal
Quoted 11 lines · Punit Agrawal

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.

Cc Marc Zyngier <[email protected]>
[ rename tlbi -> __tlbi, convert additional sites, update commit log ]
#4 lore raw

Re: [RFC v2 PATCH 2/7] KVM: Track the pid of the VM process

In reply to Christoffer Dall

Hi Christoffer,

Quoted 35 lines · Christoffer Dall
Quoted 34 lines · Punit Agrawal

Userspace tools such as perf can be used to profile individual processes.

Track the PID of the virtual machine process to match profiling requests targeted at it. This can be used to take appropriate action to enable the requested profiling operations for the VMs of interest.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 include/linux/kvm_host.h | 1 +
 virt/kvm/kvm_main.c      | 2 ++
 2 files changed, 3 insertions(+)
include/linux/kvm_host.h +1 -0
index 9c28b4d..7c42c94 100644
@@ -374,6 +374,7 @@ struct kvm_memslots {
 struct kvm {
 	spinlock_t mmu_lock;
 	struct mutex slots_lock;
+	struct pid *pid;
 	struct mm_struct *mm; /* userspace tied to this vm */
 	struct kvm_memslots *memslots[KVM_ADDRESS_SPACE_NUM];
 	struct srcu_struct srcu;
virt/kvm/kvm_main.c +0 -0
index 1950782..ab2535a 100644
@@ -613,6 +613,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
Quoted 8 lines

virt/kvm/kvm_main.c static struct kvm *kvm_create_vm(unsigned long type) · line 616

Quoted 4 lines
virt/kvm/kvm_main.c +1 -0
 	spin_lock_init(&kvm->mmu_lock);
 	atomic_inc(&current->mm->mm_count);
 	kvm->mm = current->mm;
+	kvm->pid = get_task_pid(current, PIDTYPE_PID);

How dooes this deal with threading? Is the idea that the user by specifying the main thread's pid will enable trapping for all vcpu threads belonging to that VM?

Yes that's correct - specifying the main thread PID will enable trapping for the VM (all vcpus).

I am happy to move to a more suitable identifier if available.

Thanks,
Punit

Quoted 8 lines
Quoted 8 lines
virt/kvm/kvm_main.c +0 -0
 	kvm_eventfd_init(kvm);
 	mutex_init(&kvm->lock);
 	mutex_init(&kvm->irq_lock);
@@ -712,6 +713,7 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	int i;
 	struct mm_struct *mm = kvm->mm;
 
Quoted 11 lines
Quoted 7 lines
virt/kvm/kvm_main.c +1 -0
+	put_pid(kvm->pid);
 	kvm_destroy_vm_debugfs(kvm);
 	kvm_arch_sync_events(kvm);
 	spin_lock(&kvm_lock);
Signature · 1 line
2.8.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#16 lore raw

Re: [RFC v2 PATCH 4/7] arm64: tlbflush.h: add __tlbi() macro

In reply to Christoffer Dall
Quoted 33 lines · Christoffer Dall
Quoted 24 lines · Punit Agrawal

From: Mark Rutland <[email protected]>

As with dsb() and isb(), add a __tlbi() helper so that we can avoid distracting asm boilerplate every time we want a TLBI. As some TLBI operations take an argument while others do not, some pre-processor is used to handle these two cases with different assembly blocks.

The existing tlbflush.h code is moved over to use the helper.

Cc Catalin Marinas <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Will Deacon <[email protected]>
[ rename helper to __tlbi, update commit log ]
---
 arch/arm64/include/asm/tlbflush.h | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
arch/arm64/include/asm/tlbflush.h +0 -0
index b460ae2..e9e42a7 100644

arch/arm64/include/asm/tlbflush.h line 29

Quoted 5 lines
arch/arm64/include/asm/tlbflush.h +2 -0
@@ -25,6 +25,21 @@
 #include <asm/cputype.h>
 
 /*
+ * Raw TLBI operations. Drivers and most kernel code should use the TLB
+ * management routines below in preference to these. Where necessary, these can

in preference to these?

Quoted 4 lines

arch/arm64/include/asm/tlbflush.h line 30

Quoted 1 line
arch/arm64/include/asm/tlbflush.h +1 -0
+ * be used to avoid asm() boilerplate.

'these' seem to be a bit overloaded in this commentary.

I've locally updated the comment to -

"Raw TLBI operations.

Where necessary, use the __tlbi macro to avoid asm() boilerplate. *Note:* Drivers and most kernel code should use the TLB management routines in preference to the macros."

Is that better?

Quoted 88 lines
Quoted 84 lines
arch/arm64/include/asm/tlbflush.h +20 -8
+ *
+ * Can be used as __tlbi(op) or __tlbi(op, arg), depending on whether a
+ * particular TLBI op takes an argument or not. The macros below handle invoking
+ * the asm with or without the register argument as appropriate.
+ */
+#define __TLBI_0(op, arg)		asm ("tlbi " #op)
+#define __TLBI_1(op, arg)		asm ("tlbi " #op ", %0" : : "r" (arg))
+#define __TLBI_N(op, arg, n, ...)	__TLBI_##n(op, arg)
+
+#define __tlbi(op, ...)		__TLBI_N(op, ##__VA_ARGS__, 1, 0)
+
+/*
  *	TLB Management
  *	==============
  *
@@ -66,7 +81,7 @@
 static inline void local_flush_tlb_all(void)
 {
 	dsb(nshst);
-	asm("tlbi	vmalle1");
+	__tlbi(vmalle1);
 	dsb(nsh);
 	isb();
 }
@@ -74,7 +89,7 @@ static inline void local_flush_tlb_all(void)
 static inline void flush_tlb_all(void)
 {
 	dsb(ishst);
-	asm("tlbi	vmalle1is");
+	__tlbi(vmalle1is);
 	dsb(ish);
 	isb();
 }
@@ -84,7 +99,7 @@ static inline void flush_tlb_mm(struct mm_struct *mm)
 	unsigned long asid = ASID(mm) << 48;
 
 	dsb(ishst);
-	asm("tlbi	aside1is, %0" : : "r" (asid));
+	__tlbi(aside1is, asid);
 	dsb(ish);
 }
 
@@ -94,7 +109,7 @@ static inline void flush_tlb_page(struct vm_area_struct *vma,
 	unsigned long addr = uaddr >> 12 | (ASID(vma->vm_mm) << 48);
 
 	dsb(ishst);
-	asm("tlbi	vale1is, %0" : : "r" (addr));
+	__tlbi(vale1is, addr);
 	dsb(ish);
 }
 
@@ -122,9 +137,9 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma,
 	dsb(ishst);
 	for (addr = start; addr < end; addr += 1 << (PAGE_SHIFT - 12)) {
 		if (last_level)
-			asm("tlbi vale1is, %0" : : "r"(addr));
+			__tlbi(vale1is, addr);
 		else
-			asm("tlbi vae1is, %0" : : "r"(addr));
+			__tlbi(vae1is, addr);
 	}
 	dsb(ish);
 }
@@ -149,7 +164,7 @@ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end
 
 	dsb(ishst);
 	for (addr = start; addr < end; addr += 1 << (PAGE_SHIFT - 12))
-		asm("tlbi vaae1is, %0" : : "r"(addr));
+		__tlbi(vaae1is, addr);
 	dsb(ish);
 	isb();
 }
@@ -163,7 +178,7 @@ static inline void __flush_tlb_pgtable(struct mm_struct *mm,
 {
 	unsigned long addr = uaddr >> 12 | (ASID(mm) << 48);
 
-	asm("tlbi	vae1is, %0" : : "r" (addr));
+	__tlbi(vae1is, addr);
 	dsb(ish);
 }
 
Signature · 1 line
2.8.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#22 lore raw

Re: [RFC v2 PATCH 6/7] arm64: KVM: Handle trappable TLB instructions

In reply to Punit Agrawal
Quoted 37 lines · Punit Agrawal

The 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 functionality to handle emulating the TLB instructions.

Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm64/include/asm/kvm_asm.h |  1 +
 arch/arm64/kvm/hyp/tlb.c         | 69 ++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c        | 81 ++++++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/trace.h           | 16 ++++++++
 4 files changed, 167 insertions(+)
arch/arm64/include/asm/kvm_asm.h +1 -0
index 7561f63..1ac1cc3 100644
@@ -49,6 +49,7 @@ extern char __kvm_hyp_vector[];
 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_emulate_tlb_invalidate(struct kvm *kvm, u32 sysreg, u64 regval);
 
 extern int __kvm_vcpu_run(struct kvm_vcpu *vcpu);
 
arch/arm64/kvm/hyp/tlb.c +1 -0
index 4cda100..1210f58 100644
@@ -78,3 +78,72 @@ static void __hyp_text __tlb_flush_vm_context(void)
 }
 
 __alias(__tlb_flush_vm_context) void __kvm_flush_vm_context(void);
+

arch/arm64/kvm/hyp/tlb.c static void __hyp_text __tlb_flush_vm_context(void) · line 91

Quoted 10 lines
arch/arm64/kvm/hyp/tlb.c +10 -0
+/* Intentionally empty functions */
+static void __hyp_text __switch_to_hyp_role_nvhe(void) { }
+static void __hyp_text __switch_to_host_role_nvhe(void) { }
+
+static void __hyp_text __switch_to_hyp_role_vhe(void)
+{
+	u64 hcr = read_sysreg(hcr_el2);
+
+	hcr &= ~HCR_TGE;
+	write_sysreg(hcr, hcr_el2);

why do we need to clear TGE for the TLB maintenance instructions to work?

Perhaps this is worth explaining in a comment.

Otherwise this looks ok to me.

-Christoffer

Quoted 194 lines
arch/arm64/kvm/hyp/tlb.c +58 -0
+}
+
+static void __hyp_text __switch_to_host_role_vhe(void)
+{
+	u64 hcr = read_sysreg(hcr_el2);
+
+	hcr |= HCR_TGE;
+	write_sysreg(hcr, hcr_el2);
+}
+
+static hyp_alternate_select(__switch_to_hyp_role,
+			    __switch_to_hyp_role_nvhe,
+			    __switch_to_hyp_role_vhe,
+			    ARM64_HAS_VIRT_HOST_EXTN);
+
+static hyp_alternate_select(__switch_to_host_role,
+			    __switch_to_host_role_nvhe,
+			    __switch_to_host_role_vhe,
+			    ARM64_HAS_VIRT_HOST_EXTN);
+
+static void __hyp_text __switch_to_guest_regime(struct kvm *kvm)
+{
+	write_sysreg(kvm->arch.vttbr, vttbr_el2);
+	__switch_to_hyp_role();
+	isb();
+}
+
+static void __hyp_text __switch_to_host_regime(void)
+{
+	__switch_to_host_role();
+	write_sysreg(0, vttbr_el2);
+}
+
+void __hyp_text
+__kvm_emulate_tlb_invalidate(struct kvm *kvm, u32 sys_op, u64 regval)
+{
+	kvm = kern_hyp_va(kvm);
+
+	/*
+	 * Switch to the guest before performing any TLB operations to
+	 * target the appropriate VMID
+	 */
+	__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
+	 */
+	__tlbi(vmalle1is);
+	isb();
+
+	__switch_to_host_regime();
+}
arch/arm64/kvm/sys_regs.c +81 -0
index e51367d..0e70da9 100644
@@ -790,6 +790,18 @@ static bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 	return true;
 }
 
+static bool emulate_tlb_invalidate(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+				  const struct sys_reg_desc *r)
+{
+	u32 opcode = sys_reg(p->Op0, p->Op1, p->CRn, p->CRm, p->Op2);
+
+	kvm_call_hyp(__kvm_emulate_tlb_invalidate,
+		     vcpu->kvm, opcode, p->regval);
+	trace_kvm_tlb_invalidate(*vcpu_pc(vcpu), opcode);
+
+	return true;
+}
+
 /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
 #define DBG_BCR_BVR_WCR_WVR_EL1(n)					\
 	/* DBGBVRn_EL1 */						\
@@ -841,6 +853,35 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b1110), Op2(0b010),
 	  access_dcsw },
 
+	/*
+	 * ARMv8 ARM: Table C5-4 TLB maintenance instructions
+	 * (Ref: ARMv8 ARM C5.1 version: ARM DDI 0487A.j)
+	 */
+	/* TLBI VMALLE1IS */
+	{ Op0(1), Op1(0), CRn(8), CRm(3), Op2(0), emulate_tlb_invalidate },
+	/* TLBI VAE1IS */
+	{ Op0(1), Op1(0), CRn(8), CRm(3), Op2(1), emulate_tlb_invalidate },
+	/* TLBI ASIDE1IS */
+	{ Op0(1), Op1(0), CRn(8), CRm(3), Op2(2), emulate_tlb_invalidate },
+	/* TLBI VAAE1IS */
+	{ Op0(1), Op1(0), CRn(8), CRm(3), Op2(3), emulate_tlb_invalidate },
+	/* TLBI VALE1IS */
+	{ Op0(1), Op1(0), CRn(8), CRm(3), Op2(5), emulate_tlb_invalidate },
+	/* TLBI VAALE1IS */
+	{ Op0(1), Op1(0), CRn(8), CRm(3), Op2(7), emulate_tlb_invalidate },
+	/* TLBI VMALLE1 */
+	{ Op0(1), Op1(0), CRn(8), CRm(7), Op2(0), emulate_tlb_invalidate },
+	/* TLBI VAE1 */
+	{ Op0(1), Op1(0), CRn(8), CRm(7), Op2(1), emulate_tlb_invalidate },
+	/* TLBI ASIDE1 */
+	{ Op0(1), Op1(0), CRn(8), CRm(7), Op2(2), emulate_tlb_invalidate },
+	/* TLBI VAAE1 */
+	{ Op0(1), Op1(0), CRn(8), CRm(7), Op2(3), emulate_tlb_invalidate },
+	/* TLBI VALE1 */
+	{ Op0(1), Op1(0), CRn(8), CRm(7), Op2(5), emulate_tlb_invalidate },
+	/* TLBI VAALE1 */
+	{ Op0(1), Op1(0), CRn(8), CRm(7), Op2(7), emulate_tlb_invalidate },
+
 	DBG_BCR_BVR_WCR_WVR_EL1(0),
 	DBG_BCR_BVR_WCR_WVR_EL1(1),
 	/* MDCCINT_EL1 */
@@ -1329,6 +1370,46 @@ static const struct sys_reg_desc cp15_regs[] = {
 	{ Op1( 0), CRn( 7), CRm(10), Op2( 2), access_dcsw },
 	{ Op1( 0), CRn( 7), CRm(14), Op2( 2), access_dcsw },
 
+	/*
+	 * TLB operations
+	 */
+	/* TLBIALLIS */
+	{ Op1( 0), CRn( 8), CRm( 3), Op2( 0), emulate_tlb_invalidate},
+	/* TLBIMVAIS */
+	{ Op1( 0), CRn( 8), CRm( 3), Op2( 1), emulate_tlb_invalidate},
+	/* TLBIASIDIS */
+	{ Op1( 0), CRn( 8), CRm( 3), Op2( 2), emulate_tlb_invalidate},
+	/* TLBIMVAAIS */
+	{ Op1( 0), CRn( 8), CRm( 3), Op2( 3), emulate_tlb_invalidate},
+	/* TLBIMVALIS */
+	{ Op1( 0), CRn( 8), CRm( 3), Op2( 5), emulate_tlb_invalidate},
+	/* TLBIMVAALIS */
+	{ Op1( 0), CRn( 8), CRm( 3), Op2( 7), emulate_tlb_invalidate},
+	/* ITLBIALL */
+	{ Op1( 0), CRn( 8), CRm( 5), Op2( 0), emulate_tlb_invalidate},
+	/* ITLBIMVA */
+	{ Op1( 0), CRn( 8), CRm( 5), Op2( 1), emulate_tlb_invalidate},
+	/* ITLBIASID */
+	{ Op1( 0), CRn( 8), CRm( 5), Op2( 2), emulate_tlb_invalidate},
+	/* DTLBIALL */
+	{ Op1( 0), CRn( 8), CRm( 6), Op2( 0), emulate_tlb_invalidate},
+	/* DTLBIMVA */
+	{ Op1( 0), CRn( 8), CRm( 6), Op2( 1), emulate_tlb_invalidate},
+	/* DTLBIASID */
+	{ Op1( 0), CRn( 8), CRm( 6), Op2( 2), emulate_tlb_invalidate},
+	/* TLBIALL */
+	{ Op1( 0), CRn( 8), CRm( 7), Op2( 0), emulate_tlb_invalidate},
+	/* TLBIMVA */
+	{ Op1( 0), CRn( 8), CRm( 7), Op2( 1), emulate_tlb_invalidate},
+	/* TLBIASID */
+	{ Op1( 0), CRn( 8), CRm( 7), Op2( 2), emulate_tlb_invalidate},
+	/* TLBIMVAA */
+	{ Op1( 0), CRn( 8), CRm( 7), Op2( 3), emulate_tlb_invalidate},
+	/* TLBIMVAL */
+	{ Op1( 0), CRn( 8), CRm( 7), Op2( 5), emulate_tlb_invalidate},
+	/* TLBIMVAAL */
+	{ Op1( 0), CRn( 8), CRm( 7), Op2( 7), emulate_tlb_invalidate},
+
 	/* PMU */
 	{ Op1( 0), CRn( 9), CRm(12), Op2( 0), access_pmcr },
 	{ Op1( 0), CRn( 9), CRm(12), Op2( 1), access_pmcnten },
arch/arm64/kvm/trace.h +16 -0
index 7fb0008..c4d577f 100644
@@ -166,6 +166,22 @@ TRACE_EVENT(kvm_set_guest_debug,
 	TP_printk("vcpu: %p, flags: 0x%08x", __entry->vcpu, __entry->guest_debug)
 );
 
+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_ARM64_KVM_H */
 
Signature · 1 line
2.8.1
#26 lore raw

Re: [RFC v2 PATCH 7/7] arm64: KVM: Enable selective trapping of TLB instructions

In reply to Punit Agrawal
Quoted 50 lines · Punit Agrawal

The TTLB bit of Hypervisor Control Register (HCR_EL2) 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.

Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm64/kvm/perf_trace.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
arch/arm64/kvm/perf_trace.c +21 -0
index 8bacd18..f26da1d 100644
@@ -17,6 +17,8 @@
 #include <linux/kvm_host.h>
 #include <linux/trace_events.h>
 
+#include <asm/kvm_emulate.h>
+
 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;
+

arch/arm64/kvm/perf_trace.c struct kvm_trace_hook { · line 57

Quoted 10 lines
arch/arm64/kvm/perf_trace.c +9 -0
+		vcpu_set_hcr(vcpu, hcr);
+	}
+	kvm_arm_resume_guest(kvm);
+
+	return 0;
+}
+
 static struct kvm_trace_hook trace_hook[] = {
+	{
+		.key = "kvm_tlb_invalidate",

is this key defined elsewhere? If not, I think the name is ambiguous, because it's unclear if this means 'kvm the subsystem' or 'a kvm guest', by looking purely at the string.

Quoted 8 lines
arch/arm64/kvm/perf_trace.c +2 -0
+		.setup_fn = tlb_invalidate_trap,
+	},
 	{ },
 };
 
Signature · 1 line
2.8.1

Otherwise looks ok to me.

-Christoffer

#5 lore raw

Re: [RFC v2 PATCH 2/7] KVM: Track the pid of the VM process

In reply to Punit Agrawal
Quoted 48 lines · Punit Agrawal

Hi Christoffer,

Quoted 35 lines · Christoffer Dall
Quoted 34 lines · Punit Agrawal

Userspace tools such as perf can be used to profile individual processes.

Track the PID of the virtual machine process to match profiling requests targeted at it. This can be used to take appropriate action to enable the requested profiling operations for the VMs of interest.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 include/linux/kvm_host.h | 1 +
 virt/kvm/kvm_main.c      | 2 ++
 2 files changed, 3 insertions(+)
include/linux/kvm_host.h +1 -0
index 9c28b4d..7c42c94 100644
@@ -374,6 +374,7 @@ struct kvm_memslots {
 struct kvm {
 	spinlock_t mmu_lock;
 	struct mutex slots_lock;
+	struct pid *pid;
 	struct mm_struct *mm; /* userspace tied to this vm */
 	struct kvm_memslots *memslots[KVM_ADDRESS_SPACE_NUM];
 	struct srcu_struct srcu;
virt/kvm/kvm_main.c +0 -0
index 1950782..ab2535a 100644
@@ -613,6 +613,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
Quoted 8 lines

virt/kvm/kvm_main.c static struct kvm *kvm_create_vm(unsigned long type) · line 616

Quoted 4 lines
virt/kvm/kvm_main.c +1 -0
 	spin_lock_init(&kvm->mmu_lock);
 	atomic_inc(&current->mm->mm_count);
 	kvm->mm = current->mm;
+	kvm->pid = get_task_pid(current, PIDTYPE_PID);

How dooes this deal with threading? Is the idea that the user by specifying the main thread's pid will enable trapping for all vcpu threads belonging to that VM?

Quoted 5 lines

Yes that's correct - specifying the main thread PID will enable trapping for the VM (all vcpus).

I am happy to move to a more suitable identifier if available.

What is the 'main thread' ?

Does something mandate that the VM is created by the thread group leader? If not, is it not a bit strange from a user perspective, that you have to find the specific subthread pid that created the vm to enable this tracing for all vcpu threads and that the tgid doesn't work in this case?

Thanks,
-Christoffer
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#17 lore raw

Re: [RFC v2 PATCH 4/7] arm64: tlbflush.h: add __tlbi() macro

In reply to Punit Agrawal
Quoted 42 lines · Punit Agrawal
Quoted 33 lines · Christoffer Dall
Quoted 24 lines · Punit Agrawal

From: Mark Rutland <[email protected]>

As with dsb() and isb(), add a __tlbi() helper so that we can avoid distracting asm boilerplate every time we want a TLBI. As some TLBI operations take an argument while others do not, some pre-processor is used to handle these two cases with different assembly blocks.

The existing tlbflush.h code is moved over to use the helper.

Cc Catalin Marinas <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Will Deacon <[email protected]>
[ rename helper to __tlbi, update commit log ]
---
 arch/arm64/include/asm/tlbflush.h | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
arch/arm64/include/asm/tlbflush.h +0 -0
index b460ae2..e9e42a7 100644

arch/arm64/include/asm/tlbflush.h line 29

Quoted 5 lines
arch/arm64/include/asm/tlbflush.h +2 -0
@@ -25,6 +25,21 @@
 #include <asm/cputype.h>
 
 /*
+ * Raw TLBI operations. Drivers and most kernel code should use the TLB
+ * management routines below in preference to these. Where necessary, these can

in preference to these?

Quoted 4 lines

arch/arm64/include/asm/tlbflush.h line 30

Quoted 1 line
arch/arm64/include/asm/tlbflush.h +1 -0
+ * be used to avoid asm() boilerplate.

'these' seem to be a bit overloaded in this commentary.

I've locally updated the comment to -

Quoted 7 lines

"Raw TLBI operations.

Where necessary, use the __tlbi macro to avoid asm() boilerplate. *Note:* Drivers and most kernel code should use the TLB management routines in preference to the macros."

Is that better?

Yes. I would get rid of the *Note:* part, but that's really nit-picking.

Thanks,
-Christoffer

Quoted 89 lines
Quoted 89 lines
Quoted 85 lines
arch/arm64/include/asm/tlbflush.h +20 -8
+ *
+ * Can be used as __tlbi(op) or __tlbi(op, arg), depending on whether a
+ * particular TLBI op takes an argument or not. The macros below handle invoking
+ * the asm with or without the register argument as appropriate.
+ */
+#define __TLBI_0(op, arg)		asm ("tlbi " #op)
+#define __TLBI_1(op, arg)		asm ("tlbi " #op ", %0" : : "r" (arg))
+#define __TLBI_N(op, arg, n, ...)	__TLBI_##n(op, arg)
+
+#define __tlbi(op, ...)		__TLBI_N(op, ##__VA_ARGS__, 1, 0)
+
+/*
  *	TLB Management
  *	==============
  *
@@ -66,7 +81,7 @@
 static inline void local_flush_tlb_all(void)
 {
 	dsb(nshst);
-	asm("tlbi	vmalle1");
+	__tlbi(vmalle1);
 	dsb(nsh);
 	isb();
 }
@@ -74,7 +89,7 @@ static inline void local_flush_tlb_all(void)
 static inline void flush_tlb_all(void)
 {
 	dsb(ishst);
-	asm("tlbi	vmalle1is");
+	__tlbi(vmalle1is);
 	dsb(ish);
 	isb();
 }
@@ -84,7 +99,7 @@ static inline void flush_tlb_mm(struct mm_struct *mm)
 	unsigned long asid = ASID(mm) << 48;
 
 	dsb(ishst);
-	asm("tlbi	aside1is, %0" : : "r" (asid));
+	__tlbi(aside1is, asid);
 	dsb(ish);
 }
 
@@ -94,7 +109,7 @@ static inline void flush_tlb_page(struct vm_area_struct *vma,
 	unsigned long addr = uaddr >> 12 | (ASID(vma->vm_mm) << 48);
 
 	dsb(ishst);
-	asm("tlbi	vale1is, %0" : : "r" (addr));
+	__tlbi(vale1is, addr);
 	dsb(ish);
 }
 
@@ -122,9 +137,9 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma,
 	dsb(ishst);
 	for (addr = start; addr < end; addr += 1 << (PAGE_SHIFT - 12)) {
 		if (last_level)
-			asm("tlbi vale1is, %0" : : "r"(addr));
+			__tlbi(vale1is, addr);
 		else
-			asm("tlbi vae1is, %0" : : "r"(addr));
+			__tlbi(vae1is, addr);
 	}
 	dsb(ish);
 }
@@ -149,7 +164,7 @@ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end
 
 	dsb(ishst);
 	for (addr = start; addr < end; addr += 1 << (PAGE_SHIFT - 12))
-		asm("tlbi vaae1is, %0" : : "r"(addr));
+		__tlbi(vaae1is, addr);
 	dsb(ish);
 	isb();
 }
@@ -163,7 +178,7 @@ static inline void __flush_tlb_pgtable(struct mm_struct *mm,
 {
 	unsigned long addr = uaddr >> 12 | (ASID(mm) << 48);
 
-	asm("tlbi	vae1is, %0" : : "r" (addr));
+	__tlbi(vae1is, addr);
 	dsb(ish);
 }
 
Signature · 1 line
2.8.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#6 lore raw

Re: [RFC v2 PATCH 2/7] KVM: Track the pid of the VM process

In reply to Christoffer Dall
Quoted 55 lines · Christoffer Dall
Quoted 48 lines · Punit Agrawal

Hi Christoffer,

Quoted 35 lines · Christoffer Dall
Quoted 34 lines · Punit Agrawal

Userspace tools such as perf can be used to profile individual processes.

Track the PID of the virtual machine process to match profiling requests targeted at it. This can be used to take appropriate action to enable the requested profiling operations for the VMs of interest.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 include/linux/kvm_host.h | 1 +
 virt/kvm/kvm_main.c      | 2 ++
 2 files changed, 3 insertions(+)
include/linux/kvm_host.h +1 -0
index 9c28b4d..7c42c94 100644
@@ -374,6 +374,7 @@ struct kvm_memslots {
 struct kvm {
 	spinlock_t mmu_lock;
 	struct mutex slots_lock;
+	struct pid *pid;
 	struct mm_struct *mm; /* userspace tied to this vm */
 	struct kvm_memslots *memslots[KVM_ADDRESS_SPACE_NUM];
 	struct srcu_struct srcu;
virt/kvm/kvm_main.c +0 -0
index 1950782..ab2535a 100644
@@ -613,6 +613,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
Quoted 8 lines

virt/kvm/kvm_main.c static struct kvm *kvm_create_vm(unsigned long type) · line 616

Quoted 4 lines
virt/kvm/kvm_main.c +1 -0
 	spin_lock_init(&kvm->mmu_lock);
 	atomic_inc(&current->mm->mm_count);
 	kvm->mm = current->mm;
+	kvm->pid = get_task_pid(current, PIDTYPE_PID);

How dooes this deal with threading? Is the idea that the user by specifying the main thread's pid will enable trapping for all vcpu threads belonging to that VM?

Quoted 5 lines

Yes that's correct - specifying the main thread PID will enable trapping for the VM (all vcpus).

I am happy to move to a more suitable identifier if available.

Quoted 7 lines

What is the 'main thread' ?

Does something mandate that the VM is created by the thread group leader? If not, is it not a bit strange from a user perspective, that you have to find the specific subthread pid that created the vm to enable this tracing for all vcpu threads and that the tgid doesn't work in this case?

Let me correct my terminology usage - the value recorded above (and used to identify the VM) should be the tgid. It is confusing because 'ps' reports it as pid.

I picked the value as existing KVM code already uses the PID of the creating task (see kvm_create_vm_debugfs) to export VM statistics in debugfs.

If I've got this wrong, then kvm_create_vm_debugfs also likely needs an update.

What do you think?

Quoted 7 lines

Thanks,
-Christoffer
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#7 lore raw

Re: [RFC v2 PATCH 2/7] KVM: Track the pid of the VM process

Quoted 69 lines · Punit Agrawal
Quoted 55 lines · Christoffer Dall
Quoted 48 lines · Punit Agrawal

Hi Christoffer,

Quoted 35 lines · Christoffer Dall
Quoted 34 lines · Punit Agrawal

Userspace tools such as perf can be used to profile individual processes.

Track the PID of the virtual machine process to match profiling requests targeted at it. This can be used to take appropriate action to enable the requested profiling operations for the VMs of interest.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 include/linux/kvm_host.h | 1 +
 virt/kvm/kvm_main.c      | 2 ++
 2 files changed, 3 insertions(+)
include/linux/kvm_host.h +1 -0
index 9c28b4d..7c42c94 100644
@@ -374,6 +374,7 @@ struct kvm_memslots {
 struct kvm {
 	spinlock_t mmu_lock;
 	struct mutex slots_lock;
+	struct pid *pid;
 	struct mm_struct *mm; /* userspace tied to this vm */
 	struct kvm_memslots *memslots[KVM_ADDRESS_SPACE_NUM];
 	struct srcu_struct srcu;
virt/kvm/kvm_main.c +0 -0
index 1950782..ab2535a 100644
@@ -613,6 +613,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
Quoted 8 lines

virt/kvm/kvm_main.c static struct kvm *kvm_create_vm(unsigned long type) · line 616

Quoted 4 lines
virt/kvm/kvm_main.c +1 -0
 	spin_lock_init(&kvm->mmu_lock);
 	atomic_inc(&current->mm->mm_count);
 	kvm->mm = current->mm;
+	kvm->pid = get_task_pid(current, PIDTYPE_PID);

How dooes this deal with threading? Is the idea that the user by specifying the main thread's pid will enable trapping for all vcpu threads belonging to that VM?

Quoted 5 lines

Yes that's correct - specifying the main thread PID will enable trapping for the VM (all vcpus).

I am happy to move to a more suitable identifier if available.

Quoted 7 lines

What is the 'main thread' ?

Does something mandate that the VM is created by the thread group leader? If not, is it not a bit strange from a user perspective, that you have to find the specific subthread pid that created the vm to enable this tracing for all vcpu threads and that the tgid doesn't work in this case?

Let me correct my terminology usage - the value recorded above (and used to identify the VM) should be the tgid. It is confusing because 'ps' reports it as pid.

Quoted 9 lines

I picked the value as existing KVM code already uses the PID of the creating task (see kvm_create_vm_debugfs) to export VM statistics in debugfs.

If I've got this wrong, then kvm_create_vm_debugfs also likely needs an update.

What do you think?

When you do get_task_pid(current, PIDTYPE_PID) it actually gets the kernel view of a PID which is the thead-id from userspace's point of view, right?

I don't see why this has to be the same as the debugfs code, as there it makes potentially more sense to thread-specific, but for your case, are you not targeting the behavior that a user can do "ps aux | grep qemu" or whatever, and then set tracing for the reported PID (which is actually a tgid)?

If this is indeed the case, then I don't think the current code supports this if QEMU was ever changed to create the VM with a different thread than the tgl.

That being said, I'm typically wrong when I talk about userspace.

-Christoffer

#27 lore raw

Re: [RFC v2 PATCH 7/7] arm64: KVM: Enable selective trapping of TLB instructions

In reply to Christoffer Dall
Quoted 57 lines · Christoffer Dall
Quoted 56 lines · Punit Agrawal

The TTLB bit of Hypervisor Control Register (HCR_EL2) 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.

Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm64/kvm/perf_trace.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
arch/arm64/kvm/perf_trace.c +27 -0
index 8bacd18..f26da1d 100644
@@ -17,6 +17,8 @@
 #include <linux/kvm_host.h>
 #include <linux/trace_events.h>
 
+#include <asm/kvm_emulate.h>
+
 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;
+}
Quoted 8 lines

arch/arm64/kvm/perf_trace.c struct kvm_trace_hook { · line 57

Quoted 4 lines
arch/arm64/kvm/perf_trace.c +3 -0
+
 static struct kvm_trace_hook trace_hook[] = {
+	{
+		.key = "kvm_tlb_invalidate",

is this key defined elsewhere? If not, I think the name is ambiguous, because it's unclear if this means 'kvm the subsystem' or 'a kvm guest', by looking purely at the string.

The key is the name of the tracepoint that the callback is executed against. Here it is the tracepoint defined in Patch 6/7.

I've now added a comment to the definition of "struct kvm_trace_hook" to explain what the key is.

Quoted 17 lines
Quoted 9 lines
arch/arm64/kvm/perf_trace.c +2 -0
+		.setup_fn = tlb_invalidate_trap,
+	},
 	{ },
 };
 
Signature · 1 line
2.8.1

Otherwise looks ok to me.

-Christoffer
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#8 lore raw

Re: [RFC v2 PATCH 2/7] KVM: Track the pid of the VM process

In reply to Christoffer Dall
Quoted 74 lines · Christoffer Dall
Quoted 73 lines · Punit Agrawal
Quoted 55 lines · Christoffer Dall
Quoted 48 lines · Punit Agrawal

Hi Christoffer,

Quoted 35 lines · Christoffer Dall
Quoted 34 lines · Punit Agrawal

Userspace tools such as perf can be used to profile individual processes.

Track the PID of the virtual machine process to match profiling requests targeted at it. This can be used to take appropriate action to enable the requested profiling operations for the VMs of interest.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 include/linux/kvm_host.h | 1 +
 virt/kvm/kvm_main.c      | 2 ++
 2 files changed, 3 insertions(+)
include/linux/kvm_host.h +1 -0
index 9c28b4d..7c42c94 100644
@@ -374,6 +374,7 @@ struct kvm_memslots {
 struct kvm {
 	spinlock_t mmu_lock;
 	struct mutex slots_lock;
+	struct pid *pid;
 	struct mm_struct *mm; /* userspace tied to this vm */
 	struct kvm_memslots *memslots[KVM_ADDRESS_SPACE_NUM];
 	struct srcu_struct srcu;
virt/kvm/kvm_main.c +0 -0
index 1950782..ab2535a 100644
@@ -613,6 +613,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
Quoted 8 lines

virt/kvm/kvm_main.c static struct kvm *kvm_create_vm(unsigned long type) · line 616

Quoted 4 lines
virt/kvm/kvm_main.c +1 -0
 	spin_lock_init(&kvm->mmu_lock);
 	atomic_inc(&current->mm->mm_count);
 	kvm->mm = current->mm;
+	kvm->pid = get_task_pid(current, PIDTYPE_PID);

How dooes this deal with threading? Is the idea that the user by specifying the main thread's pid will enable trapping for all vcpu threads belonging to that VM?

Quoted 5 lines

Yes that's correct - specifying the main thread PID will enable trapping for the VM (all vcpus).

I am happy to move to a more suitable identifier if available.

Quoted 7 lines

What is the 'main thread' ?

Does something mandate that the VM is created by the thread group leader? If not, is it not a bit strange from a user perspective, that you have to find the specific subthread pid that created the vm to enable this tracing for all vcpu threads and that the tgid doesn't work in this case?

Let me correct my terminology usage - the value recorded above (and used to identify the VM) should be the tgid. It is confusing because 'ps' reports it as pid.

I picked the value as existing KVM code already uses the PID of the creating task (see kvm_create_vm_debugfs) to export VM statistics in debugfs.

Quoted 8 lines
Quoted 5 lines

If I've got this wrong, then kvm_create_vm_debugfs also likely needs an update.

What do you think?

When you do get_task_pid(current, PIDTYPE_PID) it actually gets the kernel view of a PID which is the thead-id from userspace's point of view, right?

That makes sense. It seems to works here because the pid of the first task is also the tgid of the group. And I reckon it's the same assumption being made with debugfs code (more below).

I've changed the first argument of the call to get_task_pid to current->group_leader.

Quoted 6 lines

I don't see why this has to be the same as the debugfs code, as there it makes potentially more sense to thread-specific, but for your case, are you not targeting the behavior that a user can do "ps aux | grep qemu" or whatever, and then set tracing for the reported PID (which is actually a tgid)?

The debugfs stats are not thread (vcpu) specific but for the VM.

Both values, debugfs and here, are being used to represent the VM to the user. A mismatch in these identifiers will be very confusing.

If you agree, I can separately send a patch to address this for VM debugfs directory as well.

Quoted 5 lines

If this is indeed the case, then I don't think the current code supports this if QEMU was ever changed to create the VM with a different thread than the tgl.

Quoted 7 lines

That being said, I'm typically wrong when I talk about userspace.

-Christoffer
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#23 lore raw

Re: [RFC v2 PATCH 6/7] arm64: KVM: Handle trappable TLB instructions

In reply to Christoffer Dall
Quoted 52 lines · Christoffer Dall
Quoted 37 lines · Punit Agrawal

The 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 functionality to handle emulating the TLB instructions.

Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm64/include/asm/kvm_asm.h |  1 +
 arch/arm64/kvm/hyp/tlb.c         | 69 ++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c        | 81 ++++++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/trace.h           | 16 ++++++++
 4 files changed, 167 insertions(+)
arch/arm64/include/asm/kvm_asm.h +1 -0
index 7561f63..1ac1cc3 100644
@@ -49,6 +49,7 @@ extern char __kvm_hyp_vector[];
 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_emulate_tlb_invalidate(struct kvm *kvm, u32 sysreg, u64 regval);
 
 extern int __kvm_vcpu_run(struct kvm_vcpu *vcpu);
 
arch/arm64/kvm/hyp/tlb.c +1 -0
index 4cda100..1210f58 100644
@@ -78,3 +78,72 @@ static void __hyp_text __tlb_flush_vm_context(void)
 }
 
 __alias(__tlb_flush_vm_context) void __kvm_flush_vm_context(void);
+

arch/arm64/kvm/hyp/tlb.c static void __hyp_text __tlb_flush_vm_context(void) · line 91

Quoted 10 lines
arch/arm64/kvm/hyp/tlb.c +10 -0
+/* Intentionally empty functions */
+static void __hyp_text __switch_to_hyp_role_nvhe(void) { }
+static void __hyp_text __switch_to_host_role_nvhe(void) { }
+
+static void __hyp_text __switch_to_hyp_role_vhe(void)
+{
+	u64 hcr = read_sysreg(hcr_el2);
+
+	hcr &= ~HCR_TGE;
+	write_sysreg(hcr, hcr_el2);

why do we need to clear TGE for the TLB maintenance instructions to work?

Quoted 1 line

Perhaps this is worth explaining in a comment.

I've added the following comment before clearing TGE bit.

"When VHE is enabled and HCR_EL2.TGE=1, EL1&0 TLB operations apply to EL2&0 translation regime. As we prepare to emulate guest TLB operation clear HCR_TGE to target TLB operations to EL1&0 (guest)."

Quoted 2 lines

Otherwise this looks ok to me.

Thanks for taking a look.

Punit

Quoted 201 lines

-Christoffer

Quoted 194 lines
arch/arm64/kvm/hyp/tlb.c +58 -0
+}
+
+static void __hyp_text __switch_to_host_role_vhe(void)
+{
+	u64 hcr = read_sysreg(hcr_el2);
+
+	hcr |= HCR_TGE;
+	write_sysreg(hcr, hcr_el2);
+}
+
+static hyp_alternate_select(__switch_to_hyp_role,
+			    __switch_to_hyp_role_nvhe,
+			    __switch_to_hyp_role_vhe,
+			    ARM64_HAS_VIRT_HOST_EXTN);
+
+static hyp_alternate_select(__switch_to_host_role,
+			    __switch_to_host_role_nvhe,
+			    __switch_to_host_role_vhe,
+			    ARM64_HAS_VIRT_HOST_EXTN);
+
+static void __hyp_text __switch_to_guest_regime(struct kvm *kvm)
+{
+	write_sysreg(kvm->arch.vttbr, vttbr_el2);
+	__switch_to_hyp_role();
+	isb();
+}
+
+static void __hyp_text __switch_to_host_regime(void)
+{
+	__switch_to_host_role();
+	write_sysreg(0, vttbr_el2);
+}
+
+void __hyp_text
+__kvm_emulate_tlb_invalidate(struct kvm *kvm, u32 sys_op, u64 regval)
+{
+	kvm = kern_hyp_va(kvm);
+
+	/*
+	 * Switch to the guest before performing any TLB operations to
+	 * target the appropriate VMID
+	 */
+	__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
+	 */
+	__tlbi(vmalle1is);
+	isb();
+
+	__switch_to_host_regime();
+}
arch/arm64/kvm/sys_regs.c +81 -0
index e51367d..0e70da9 100644
@@ -790,6 +790,18 @@ static bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 	return true;
 }
 
+static bool emulate_tlb_invalidate(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+				  const struct sys_reg_desc *r)
+{
+	u32 opcode = sys_reg(p->Op0, p->Op1, p->CRn, p->CRm, p->Op2);
+
+	kvm_call_hyp(__kvm_emulate_tlb_invalidate,
+		     vcpu->kvm, opcode, p->regval);
+	trace_kvm_tlb_invalidate(*vcpu_pc(vcpu), opcode);
+
+	return true;
+}
+
 /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
 #define DBG_BCR_BVR_WCR_WVR_EL1(n)					\
 	/* DBGBVRn_EL1 */						\
@@ -841,6 +853,35 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b1110), Op2(0b010),
 	  access_dcsw },
 
+	/*
+	 * ARMv8 ARM: Table C5-4 TLB maintenance instructions
+	 * (Ref: ARMv8 ARM C5.1 version: ARM DDI 0487A.j)
+	 */
+	/* TLBI VMALLE1IS */
+	{ Op0(1), Op1(0), CRn(8), CRm(3), Op2(0), emulate_tlb_invalidate },
+	/* TLBI VAE1IS */
+	{ Op0(1), Op1(0), CRn(8), CRm(3), Op2(1), emulate_tlb_invalidate },
+	/* TLBI ASIDE1IS */
+	{ Op0(1), Op1(0), CRn(8), CRm(3), Op2(2), emulate_tlb_invalidate },
+	/* TLBI VAAE1IS */
+	{ Op0(1), Op1(0), CRn(8), CRm(3), Op2(3), emulate_tlb_invalidate },
+	/* TLBI VALE1IS */
+	{ Op0(1), Op1(0), CRn(8), CRm(3), Op2(5), emulate_tlb_invalidate },
+	/* TLBI VAALE1IS */
+	{ Op0(1), Op1(0), CRn(8), CRm(3), Op2(7), emulate_tlb_invalidate },
+	/* TLBI VMALLE1 */
+	{ Op0(1), Op1(0), CRn(8), CRm(7), Op2(0), emulate_tlb_invalidate },
+	/* TLBI VAE1 */
+	{ Op0(1), Op1(0), CRn(8), CRm(7), Op2(1), emulate_tlb_invalidate },
+	/* TLBI ASIDE1 */
+	{ Op0(1), Op1(0), CRn(8), CRm(7), Op2(2), emulate_tlb_invalidate },
+	/* TLBI VAAE1 */
+	{ Op0(1), Op1(0), CRn(8), CRm(7), Op2(3), emulate_tlb_invalidate },
+	/* TLBI VALE1 */
+	{ Op0(1), Op1(0), CRn(8), CRm(7), Op2(5), emulate_tlb_invalidate },
+	/* TLBI VAALE1 */
+	{ Op0(1), Op1(0), CRn(8), CRm(7), Op2(7), emulate_tlb_invalidate },
+
 	DBG_BCR_BVR_WCR_WVR_EL1(0),
 	DBG_BCR_BVR_WCR_WVR_EL1(1),
 	/* MDCCINT_EL1 */
@@ -1329,6 +1370,46 @@ static const struct sys_reg_desc cp15_regs[] = {
 	{ Op1( 0), CRn( 7), CRm(10), Op2( 2), access_dcsw },
 	{ Op1( 0), CRn( 7), CRm(14), Op2( 2), access_dcsw },
 
+	/*
+	 * TLB operations
+	 */
+	/* TLBIALLIS */
+	{ Op1( 0), CRn( 8), CRm( 3), Op2( 0), emulate_tlb_invalidate},
+	/* TLBIMVAIS */
+	{ Op1( 0), CRn( 8), CRm( 3), Op2( 1), emulate_tlb_invalidate},
+	/* TLBIASIDIS */
+	{ Op1( 0), CRn( 8), CRm( 3), Op2( 2), emulate_tlb_invalidate},
+	/* TLBIMVAAIS */
+	{ Op1( 0), CRn( 8), CRm( 3), Op2( 3), emulate_tlb_invalidate},
+	/* TLBIMVALIS */
+	{ Op1( 0), CRn( 8), CRm( 3), Op2( 5), emulate_tlb_invalidate},
+	/* TLBIMVAALIS */
+	{ Op1( 0), CRn( 8), CRm( 3), Op2( 7), emulate_tlb_invalidate},
+	/* ITLBIALL */
+	{ Op1( 0), CRn( 8), CRm( 5), Op2( 0), emulate_tlb_invalidate},
+	/* ITLBIMVA */
+	{ Op1( 0), CRn( 8), CRm( 5), Op2( 1), emulate_tlb_invalidate},
+	/* ITLBIASID */
+	{ Op1( 0), CRn( 8), CRm( 5), Op2( 2), emulate_tlb_invalidate},
+	/* DTLBIALL */
+	{ Op1( 0), CRn( 8), CRm( 6), Op2( 0), emulate_tlb_invalidate},
+	/* DTLBIMVA */
+	{ Op1( 0), CRn( 8), CRm( 6), Op2( 1), emulate_tlb_invalidate},
+	/* DTLBIASID */
+	{ Op1( 0), CRn( 8), CRm( 6), Op2( 2), emulate_tlb_invalidate},
+	/* TLBIALL */
+	{ Op1( 0), CRn( 8), CRm( 7), Op2( 0), emulate_tlb_invalidate},
+	/* TLBIMVA */
+	{ Op1( 0), CRn( 8), CRm( 7), Op2( 1), emulate_tlb_invalidate},
+	/* TLBIASID */
+	{ Op1( 0), CRn( 8), CRm( 7), Op2( 2), emulate_tlb_invalidate},
+	/* TLBIMVAA */
+	{ Op1( 0), CRn( 8), CRm( 7), Op2( 3), emulate_tlb_invalidate},
+	/* TLBIMVAL */
+	{ Op1( 0), CRn( 8), CRm( 7), Op2( 5), emulate_tlb_invalidate},
+	/* TLBIMVAAL */
+	{ Op1( 0), CRn( 8), CRm( 7), Op2( 7), emulate_tlb_invalidate},
+
 	/* PMU */
 	{ Op1( 0), CRn( 9), CRm(12), Op2( 0), access_pmcr },
 	{ Op1( 0), CRn( 9), CRm(12), Op2( 1), access_pmcnten },
arch/arm64/kvm/trace.h +16 -0
index 7fb0008..c4d577f 100644
@@ -166,6 +166,22 @@ TRACE_EVENT(kvm_set_guest_debug,
 	TP_printk("vcpu: %p, flags: 0x%08x", __entry->vcpu, __entry->guest_debug)
 );
 
+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_ARM64_KVM_H */
 
Signature · 1 line
2.8.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#13 lore raw

Re: [RFC v2 PATCH 3/7] KVM: arm/arm64: Register perf trace event notifier

In reply to Christoffer Dall
Quoted 10 lines · Christoffer Dall
Quoted 8 lines · Punit Agrawal

Register a notifier to track state changes of perf trace events.

The notifier will enable taking appropriate action for trace events targeting VM.

Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
Quoted 7 lines

Overall this looks reasonable, but I'm wondering if most if this logic should really go in virt/kvm/perf_trace.c and call into arch-specific hooks, similar to the way it works for preempt notifiers.

On the other hand, if arm/arm64 are the only two architectures that are going to use this, creating stubs for the other architectures could be a bit tedious.

If the series looks to be headed in the right direction, I'll be adding support for 32bit ARM host as well.

Most of the code here can be shared between arm and arm64. At that point, it might be worth moving the logic here to a commong location with the architecture code responsible for registering it's kvm_trace_hooks as part of the initialisation (or something along those lines).

I'll wait a bit for maintainer feedback on Patch 1 before re-spinning the series. Patch 1 ultimately decides what the user facing interface for this functionality looks like.

Thanks for your feedback on the series.

Punit

Quoted 236 lines

Thanks,
-Christoffer

Quoted 228 lines
---
 arch/arm/include/asm/kvm_host.h   |   3 +
 arch/arm/kvm/arm.c                |   2 +
 arch/arm64/include/asm/kvm_host.h |   8 +++
 arch/arm64/kvm/Kconfig            |   4 ++
 arch/arm64/kvm/Makefile           |   1 +
 arch/arm64/kvm/perf_trace.c       | 122 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 140 insertions(+)
 create mode 100644 arch/arm64/kvm/perf_trace.c
arch/arm/include/asm/kvm_host.h +3 -0
index de338d9..609998e 100644
@@ -280,6 +280,9 @@ static inline int kvm_arch_dev_ioctl_check_extension(struct kvm *kvm, long ext)
 int kvm_perf_init(void);
 int kvm_perf_teardown(void);
 
+static inline int kvm_perf_trace_init(void) { return 0; }
+static inline int kvm_perf_trace_teardown(void) { return 0; }
+
 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/arm.c +2 -0
index 75f130e..e1b99c4 100644
@@ -1220,6 +1220,7 @@ static int init_subsystems(void)
 		goto out;
 
 	kvm_perf_init();
+	kvm_perf_trace_init();
 	kvm_coproc_table_init();
 
 out:
@@ -1411,6 +1412,7 @@ out_err:
 void kvm_arch_exit(void)
 {
 	kvm_perf_teardown();
+	kvm_perf_trace_teardown();
 }
 
 static int arm_init(void)
arch/arm64/include/asm/kvm_host.h +8 -0
index 3eda975..f6ff8e5 100644
@@ -345,6 +345,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_PERF_TRACE)
+static inline int kvm_perf_trace_init(void) { return 0; }
+static inline int kvm_perf_trace_teardown(void) { return 0; }
+#else
+int kvm_perf_trace_init(void);
+int kvm_perf_trace_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 +4 -0
index 9c9edc9..56e9537 100644
@@ -19,6 +19,9 @@ if VIRTUALIZATION
 config KVM_ARM_VGIC_V3
 	bool
 
+config KVM_PERF_TRACE
+        bool
+
 config KVM
 	bool "Kernel-based Virtual Machine (KVM) support"
 	depends on OF
@@ -39,6 +42,7 @@ config KVM
 	select HAVE_KVM_MSI
 	select HAVE_KVM_IRQCHIP
 	select HAVE_KVM_IRQ_ROUTING
+	select KVM_PERF_TRACE if EVENT_TRACING && PERF_EVENTS
 	---help---
 	  Support hosting virtualized guest machines.
 	  We don't support KVM with 16K page tables yet, due to the multiple
arch/arm64/kvm/Makefile +1 -0
index 695eb3c..7d175e4 100644
@@ -19,6 +19,7 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(ARM)/psci.o $(ARM)/perf.o
 kvm-$(CONFIG_KVM_ARM_HOST) += emulate.o inject_fault.o regmap.o
 kvm-$(CONFIG_KVM_ARM_HOST) += hyp.o hyp-init.o handle_exit.o
 kvm-$(CONFIG_KVM_ARM_HOST) += guest.o debug.o reset.o sys_regs.o sys_regs_generic_v8.o
+kvm-$(CONFIG_KVM_PERF_TRACE) += perf_trace.o
 
 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic.o
 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-init.o
arch/arm64/kvm/perf_trace.c new file +122 -0
new file mode 100644
index 0000000..8bacd18
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2016 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 <linux/kvm_host.h>
+#include <linux/trace_events.h>
+
+typedef int (*perf_trace_callback_fn)(struct kvm *kvm, bool enable);
+
+struct kvm_trace_hook {
+	char *key;
+	perf_trace_callback_fn setup_fn;
+};
+
+static struct kvm_trace_hook trace_hook[] = {
+	{ },
+};
+
+static perf_trace_callback_fn find_trace_callback(const char *trace_key)
+{
+	int i;
+
+	for (i = 0; trace_hook[i].key; i++)
+		if (!strcmp(trace_key, trace_hook[i].key))
+			return trace_hook[i].setup_fn;
+
+	return NULL;
+}
+
+static int kvm_perf_trace_notifier(struct notifier_block *nb,
+				   unsigned long event, void *data)
+{
+	struct perf_event *p_event = data;
+	struct trace_event_call *tp_event = p_event->tp_event;
+	perf_trace_callback_fn setup_trace_fn;
+	struct kvm *kvm = NULL;
+	struct pid *pid;
+	bool found = false;
+
+	/*
+	 * Is this a trace point?
+	 */
+	if (!(tp_event->flags & TRACE_EVENT_FL_TRACEPOINT))
+		goto out;
+
+	/*
+	 * We'll get here for events we care to monitor for KVM. As we
+	 * only care about events attached to a VM, check that there
+	 * is a task associated with the perf event.
+	 */
+	if (p_event->attach_state != PERF_ATTACH_TASK)
+		goto out;
+
+	/*
+	 * This notifier gets called when perf trace event instance is
+	 * added or removed. Until we can restrict this to events of
+	 * interest in core, minimise the overhead below.
+	 *
+	 * Do we care about it? i.e., is there a callback for this
+	 * trace point?
+	 */
+	setup_trace_fn = find_trace_callback(tp_event->tp->name);
+	if (!setup_trace_fn)
+		goto out;
+
+	pid = get_task_pid(p_event->hw.target, PIDTYPE_PID);
+
+	/*
+	 * Does it match any of the VMs?
+	 */
+	spin_lock(&kvm_lock);
+	list_for_each_entry(kvm, &vm_list, vm_list) {
+		if (kvm->pid == pid) {
+			found = true;
+			break;
+		}
+	}
+	spin_unlock(&kvm_lock);
+
+	put_pid(pid);
+	if (!found)
+		goto out;
+
+	switch (event) {
+	case TRACE_REG_PERF_OPEN:
+		setup_trace_fn(kvm, true);
+		break;
+
+	case TRACE_REG_PERF_CLOSE:
+		setup_trace_fn(kvm, false);
+		break;
+	}
+
+out:
+	return 0;
+}
+
+static struct notifier_block kvm_perf_trace_notifier_block = {
+	.notifier_call = kvm_perf_trace_notifier,
+};
+
+int kvm_perf_trace_init(void)
+{
+	return perf_trace_notifier_register(&kvm_perf_trace_notifier_block);
+}
+
+int kvm_perf_trace_teardown(void)
+{
+	return perf_trace_notifier_unregister(&kvm_perf_trace_notifier_block);
+}
Signature · 1 line
2.8.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#9 lore raw

Re: [RFC v2 PATCH 2/7] KVM: Track the pid of the VM process

In reply to Punit Agrawal
Quoted 81 lines · Punit Agrawal
Quoted 79 lines · Christoffer Dall
Quoted 78 lines · Punit Agrawal
Quoted 55 lines · Christoffer Dall
Quoted 48 lines · Punit Agrawal

Hi Christoffer,

Quoted 35 lines · Christoffer Dall
Quoted 34 lines · Punit Agrawal

Userspace tools such as perf can be used to profile individual processes.

Track the PID of the virtual machine process to match profiling requests targeted at it. This can be used to take appropriate action to enable the requested profiling operations for the VMs of interest.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 include/linux/kvm_host.h | 1 +
 virt/kvm/kvm_main.c      | 2 ++
 2 files changed, 3 insertions(+)
include/linux/kvm_host.h +1 -0
index 9c28b4d..7c42c94 100644
@@ -374,6 +374,7 @@ struct kvm_memslots {
 struct kvm {
 	spinlock_t mmu_lock;
 	struct mutex slots_lock;
+	struct pid *pid;
 	struct mm_struct *mm; /* userspace tied to this vm */
 	struct kvm_memslots *memslots[KVM_ADDRESS_SPACE_NUM];
 	struct srcu_struct srcu;
virt/kvm/kvm_main.c +0 -0
index 1950782..ab2535a 100644
@@ -613,6 +613,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
Quoted 8 lines

virt/kvm/kvm_main.c static struct kvm *kvm_create_vm(unsigned long type) · line 616

Quoted 4 lines
virt/kvm/kvm_main.c +1 -0
 	spin_lock_init(&kvm->mmu_lock);
 	atomic_inc(&current->mm->mm_count);
 	kvm->mm = current->mm;
+	kvm->pid = get_task_pid(current, PIDTYPE_PID);

How dooes this deal with threading? Is the idea that the user by specifying the main thread's pid will enable trapping for all vcpu threads belonging to that VM?

Quoted 5 lines

Yes that's correct - specifying the main thread PID will enable trapping for the VM (all vcpus).

I am happy to move to a more suitable identifier if available.

Quoted 7 lines

What is the 'main thread' ?

Does something mandate that the VM is created by the thread group leader? If not, is it not a bit strange from a user perspective, that you have to find the specific subthread pid that created the vm to enable this tracing for all vcpu threads and that the tgid doesn't work in this case?

Let me correct my terminology usage - the value recorded above (and used to identify the VM) should be the tgid. It is confusing because 'ps' reports it as pid.

I picked the value as existing KVM code already uses the PID of the creating task (see kvm_create_vm_debugfs) to export VM statistics in debugfs.

If I've got this wrong, then kvm_create_vm_debugfs also likely needs an update.

What do you think?

Quoted 7 lines
Quoted 3 lines

When you do get_task_pid(current, PIDTYPE_PID) it actually gets the kernel view of a PID which is the thead-id from userspace's point of view, right?

That makes sense. It seems to works here because the pid of the first task is also the tgid of the group. And I reckon it's the same assumption being made with debugfs code (more below).

That is probably the implementation of all QEMU versions and kvmtool versions out there.

Quoted 11 lines

I've changed the first argument of the call to get_task_pid to current->group_leader.

Quoted 6 lines

I don't see why this has to be the same as the debugfs code, as there it makes potentially more sense to thread-specific, but for your case, are you not targeting the behavior that a user can do "ps aux | grep qemu" or whatever, and then set tracing for the reported PID (which is actually a tgid)?

Quoted 8 lines

The debugfs stats are not thread (vcpu) specific but for the VM.

Both values, debugfs and here, are being used to represent the VM to the user. A mismatch in these identifiers will be very confusing.

If you agree, I can separately send a patch to address this for VM debugfs directory as well.

I don't know how the debugfs stuff is used or was intended, so I really can't speak for that. It seems less weird to me with debugfs, because I imagine it can be used by simply looking at what exists in the debugfs directory and mapping that to a VM.

In your case, there's a clear expectation from the user that using the tgid should cover this VM, and it will be weird if that's not the case.

-Christoffer

#10 lore raw

Re: [RFC v2 PATCH 2/7] KVM: Track the pid of the VM process

Quoted 109 lines · Christoffer Dall
Quoted 81 lines · Punit Agrawal
Quoted 79 lines · Christoffer Dall
Quoted 78 lines · Punit Agrawal
Quoted 55 lines · Christoffer Dall
Quoted 48 lines · Punit Agrawal

Hi Christoffer,

Quoted 35 lines · Christoffer Dall
Quoted 34 lines · Punit Agrawal

Userspace tools such as perf can be used to profile individual processes.

Track the PID of the virtual machine process to match profiling requests targeted at it. This can be used to take appropriate action to enable the requested profiling operations for the VMs of interest.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 include/linux/kvm_host.h | 1 +
 virt/kvm/kvm_main.c      | 2 ++
 2 files changed, 3 insertions(+)
include/linux/kvm_host.h +1 -0
index 9c28b4d..7c42c94 100644
@@ -374,6 +374,7 @@ struct kvm_memslots {
 struct kvm {
 	spinlock_t mmu_lock;
 	struct mutex slots_lock;
+	struct pid *pid;
 	struct mm_struct *mm; /* userspace tied to this vm */
 	struct kvm_memslots *memslots[KVM_ADDRESS_SPACE_NUM];
 	struct srcu_struct srcu;
virt/kvm/kvm_main.c +0 -0
index 1950782..ab2535a 100644
@@ -613,6 +613,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
Quoted 8 lines

virt/kvm/kvm_main.c static struct kvm *kvm_create_vm(unsigned long type) · line 616

Quoted 4 lines
virt/kvm/kvm_main.c +1 -0
 	spin_lock_init(&kvm->mmu_lock);
 	atomic_inc(&current->mm->mm_count);
 	kvm->mm = current->mm;
+	kvm->pid = get_task_pid(current, PIDTYPE_PID);

How dooes this deal with threading? Is the idea that the user by specifying the main thread's pid will enable trapping for all vcpu threads belonging to that VM?

Quoted 5 lines

Yes that's correct - specifying the main thread PID will enable trapping for the VM (all vcpus).

I am happy to move to a more suitable identifier if available.

Quoted 7 lines

What is the 'main thread' ?

Does something mandate that the VM is created by the thread group leader? If not, is it not a bit strange from a user perspective, that you have to find the specific subthread pid that created the vm to enable this tracing for all vcpu threads and that the tgid doesn't work in this case?

Let me correct my terminology usage - the value recorded above (and used to identify the VM) should be the tgid. It is confusing because 'ps' reports it as pid.

I picked the value as existing KVM code already uses the PID of the creating task (see kvm_create_vm_debugfs) to export VM statistics in debugfs.

If I've got this wrong, then kvm_create_vm_debugfs also likely needs an update.

What do you think?

Quoted 7 lines
Quoted 3 lines

When you do get_task_pid(current, PIDTYPE_PID) it actually gets the kernel view of a PID which is the thead-id from userspace's point of view, right?

That makes sense. It seems to works here because the pid of the first task is also the tgid of the group. And I reckon it's the same assumption being made with debugfs code (more below).

That is probably the implementation of all QEMU versions and kvmtool versions out there.

Quoted 16 lines

I've changed the first argument of the call to get_task_pid to current->group_leader.

Quoted 6 lines

I don't see why this has to be the same as the debugfs code, as there it makes potentially more sense to thread-specific, but for your case, are you not targeting the behavior that a user can do "ps aux | grep qemu" or whatever, and then set tracing for the reported PID (which is actually a tgid)?

The debugfs stats are not thread (vcpu) specific but for the VM.

Both values, debugfs and here, are being used to represent the VM to the user. A mismatch in these identifiers will be very confusing.

Quoted 8 lines
Quoted 3 lines

If you agree, I can separately send a patch to address this for VM debugfs directory as well.

I don't know how the debugfs stuff is used or was intended, so I really can't speak for that. It seems less weird to me with debugfs, because I imagine it can be used by simply looking at what exists in the debugfs directory and mapping that to a VM.

Ok, I'll let debugfs stuff be as is then.

Quoted 4 lines

In your case, there's a clear expectation from the user that using the tgid should cover this VM, and it will be weird if that's not the case.

Right. Switching over to current->group_leader should avert problems if userspace tools do things differently.

Thanks,
Punit

Quoted 6 lines

-Christoffer
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#24 lore raw

Re: [RFC v2 PATCH 6/7] arm64: KVM: Handle trappable TLB instructions

In reply to Punit Agrawal
Quoted 54 lines · Punit Agrawal
Quoted 52 lines · Christoffer Dall
Quoted 37 lines · Punit Agrawal

The 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 functionality to handle emulating the TLB instructions.

Cc Christoffer Dall <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm64/include/asm/kvm_asm.h |  1 +
 arch/arm64/kvm/hyp/tlb.c         | 69 ++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c        | 81 ++++++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/trace.h           | 16 ++++++++
 4 files changed, 167 insertions(+)
arch/arm64/include/asm/kvm_asm.h +1 -0
index 7561f63..1ac1cc3 100644
@@ -49,6 +49,7 @@ extern char __kvm_hyp_vector[];
 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_emulate_tlb_invalidate(struct kvm *kvm, u32 sysreg, u64 regval);
 
 extern int __kvm_vcpu_run(struct kvm_vcpu *vcpu);
 
arch/arm64/kvm/hyp/tlb.c +1 -0
index 4cda100..1210f58 100644
@@ -78,3 +78,72 @@ static void __hyp_text __tlb_flush_vm_context(void)
 }
 
 __alias(__tlb_flush_vm_context) void __kvm_flush_vm_context(void);
+

arch/arm64/kvm/hyp/tlb.c static void __hyp_text __tlb_flush_vm_context(void) · line 91

Quoted 10 lines
arch/arm64/kvm/hyp/tlb.c +10 -0
+/* Intentionally empty functions */
+static void __hyp_text __switch_to_hyp_role_nvhe(void) { }
+static void __hyp_text __switch_to_host_role_nvhe(void) { }
+
+static void __hyp_text __switch_to_hyp_role_vhe(void)
+{
+	u64 hcr = read_sysreg(hcr_el2);
+
+	hcr &= ~HCR_TGE;
+	write_sysreg(hcr, hcr_el2);

why do we need to clear TGE for the TLB maintenance instructions to work?

Quoted 9 lines
Quoted 1 line

Perhaps this is worth explaining in a comment.

I've added the following comment before clearing TGE bit.

"When VHE is enabled and HCR_EL2.TGE=1, EL1&0 TLB operations apply to EL2&0 translation regime. As we prepare to emulate guest TLB operation clear HCR_TGE to target TLB operations to EL1&0 (guest)."

Ah, right, obvious when I read this comment.

Thanks,
-Christoffer

Reviewed-by on v2 4/7
#18 lore raw

Re: [RFC v2 PATCH 4/7] arm64: tlbflush.h: add __tlbi() macro

In reply to Punit Agrawal
Quoted 30 lines · Punit Agrawal

From: Mark Rutland <[email protected]>

As with dsb() and isb(), add a __tlbi() helper so that we can avoid distracting asm boilerplate every time we want a TLBI. As some TLBI operations take an argument while others do not, some pre-processor is used to handle these two cases with different assembly blocks.

The existing tlbflush.h code is moved over to use the helper.

Cc Catalin Marinas <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Will Deacon <[email protected]>
[ rename helper to __tlbi, update commit log ]
---
 arch/arm64/include/asm/tlbflush.h | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
arch/arm64/include/asm/tlbflush.h +3 -0
index b460ae2..e9e42a7 100644
@@ -25,6 +25,21 @@
 #include <asm/cputype.h>
 
 /*
+ * Raw TLBI operations. Drivers and most kernel code should use the TLB
+ * management routines below in preference to these. Where necessary, these can
+ * be used to avoid asm() boilerplate.

arch/arm64/include/asm/tlbflush.h line 40

Quoted 10 lines
arch/arm64/include/asm/tlbflush.h +10 -0
+ *
+ * Can be used as __tlbi(op) or __tlbi(op, arg), depending on whether a
+ * particular TLBI op takes an argument or not. The macros below handle invoking
+ * the asm with or without the register argument as appropriate.
+ */
+#define __TLBI_0(op, arg)		asm ("tlbi " #op)
+#define __TLBI_1(op, arg)		asm ("tlbi " #op ", %0" : : "r" (arg))
+#define __TLBI_N(op, arg, n, ...)	__TLBI_##n(op, arg)
+
+#define __tlbi(op, ...)		__TLBI_N(op, ##__VA_ARGS__, 1, 0)

This looks ok to me now. With Christoffer's comments address:

Will